summaryrefslogtreecommitdiff
path: root/kecho.c
blob: 7bae6d266b44464bad760bc1a57fe67a99046c56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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)
{
	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);