#include #include #include #include #include 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, ¬ice_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) { 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); module_exit(kecho_cleanup);