summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2008-10-30 23:45:03 -0400
committerRay Strode <rstrode@redhat.com>2008-10-30 23:45:03 -0400
commitabc8c83bb3cb823b7fbb77c4015c7dbed78f82c0 (patch)
tree4d90fc8c6dc24af6f3bfd6e8ca9895b4a24931fa
parenta82c51425220bf3e034a9ec7240ac039a695c396 (diff)
Add initial cut at /sys/kernel/echo/*HEADmaster
The idea is you can do echo "foo" > /sys/kernel/echo/debug or whatever and the kernel will printk it
-rw-r--r--kecho.c104
1 files changed, 102 insertions, 2 deletions
diff --git a/kecho.c b/kecho.c
index cbdbb40..7bae6d2 100644
--- a/kecho.c
+++ b/kecho.c
@@ -1,14 +1,114 @@
-#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/err.h>
#include <linux/init.h>
+#include <linux/kdev_t.h>
+#include <linux/module.h>
+
+static struct kobject *echo_kobj;
+
+static ssize_t echo_show(struct kobject *kobj, struct kobj_attribute *attr,
+ char *buf)
+{
+ return 0;
+}
+
+static ssize_t echo_store(struct kobject *kobj, struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ ssize_t ret;
+
+ if (count > INT_MAX) {
+ count = INT_MAX;
+ }
+
+ if (strcmp (attr->attr.name, "emergency") == 0) {
+ ret = printk(KERN_EMERG "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "alert") == 0) {
+ ret = printk(KERN_ALERT "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "critical") == 0) {
+ ret = printk(KERN_CRIT "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "error") == 0) {
+ ret = printk(KERN_ERR "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "warning") == 0) {
+ ret = printk(KERN_WARNING "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "notice") == 0) {
+ ret = printk(KERN_NOTICE "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "info") == 0) {
+ ret = printk(KERN_INFO "%*s", (int) count, buf);
+ } else if (strcmp (attr->attr.name, "debug") == 0) {
+ ret = printk(KERN_DEBUG "%*s", (int) count, buf);
+ } else {
+ ret = -EINVAL;
+ }
+
+ return ret;
+}
+
+static struct kobj_attribute emergency_attribute =
+ __ATTR(emergency, 0200, echo_show, echo_store);
+
+static struct kobj_attribute alert_attribute =
+ __ATTR(alert, 0200, echo_show, echo_store);
+
+static struct kobj_attribute critical_attribute =
+ __ATTR(critical, 0200, echo_show, echo_store);
+
+static struct kobj_attribute error_attribute =
+ __ATTR(error, 0200, echo_show, echo_store);
+
+static struct kobj_attribute warning_attribute =
+ __ATTR(warning, 0200, echo_show, echo_store);
+
+static struct kobj_attribute notice_attribute =
+ __ATTR(notice, 0200, echo_show, echo_store);
+
+static struct kobj_attribute info_attribute =
+ __ATTR(info, 0200, echo_show, echo_store);
+
+static struct kobj_attribute debug_attribute =
+ __ATTR(debug, 0200, echo_show, echo_store);
+
+static struct attribute *echo_attributes[] = {
+ &emergency_attribute.attr,
+ &alert_attribute.attr,
+ &critical_attribute.attr,
+ &error_attribute.attr,
+ &warning_attribute.attr,
+ &notice_attribute.attr,
+ &info_attribute.attr,
+ &debug_attribute.attr,
+ NULL
+};
+
+static struct attribute_group echo_attribute_group = {
+ .attrs = echo_attributes
+};
MODULE_LICENSE("GPL");
static int __init kecho_init(void)
{
- return 0;
+ int ret;
+
+ ret = 0;
+
+ echo_kobj = kobject_create_and_add("echo", kernel_kobj);
+
+ if (echo_kobj == NULL) {
+ ret = -ENOMEM;
+ }
+
+ if (!ret)
+ ret = sysfs_create_group(echo_kobj, &echo_attribute_group);
+
+ if (ret)
+ kobject_put(echo_kobj);
+
+ return ret;
}
static void __exit kecho_cleanup(void)
{
+ kobject_put(echo_kobj);
}
module_init(kecho_init);