xref: /linux/kernel/ksysfs.c (revision 42fda66387daa53538ae13a2c858396aaf037158)
1 /*
2  * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
3  * 		     are not related to any other subsystem
4  *
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This file is release under the GPLv2
8  *
9  */
10 
11 #include <linux/kobject.h>
12 #include <linux/string.h>
13 #include <linux/sysfs.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kexec.h>
17 #include <linux/sched.h>
18 
19 #define KERNEL_ATTR_RO(_name) \
20 static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
21 
22 #define KERNEL_ATTR_RW(_name) \
23 static struct subsys_attribute _name##_attr = \
24 	__ATTR(_name, 0644, _name##_show, _name##_store)
25 
26 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
27 /* current uevent sequence number */
28 static ssize_t uevent_seqnum_show(struct kset *kset, char *page)
29 {
30 	return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum);
31 }
32 KERNEL_ATTR_RO(uevent_seqnum);
33 
34 /* uevent helper program, used during early boo */
35 static ssize_t uevent_helper_show(struct kset *kset, char *page)
36 {
37 	return sprintf(page, "%s\n", uevent_helper);
38 }
39 static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count)
40 {
41 	if (count+1 > UEVENT_HELPER_PATH_LEN)
42 		return -ENOENT;
43 	memcpy(uevent_helper, page, count);
44 	uevent_helper[count] = '\0';
45 	if (count && uevent_helper[count-1] == '\n')
46 		uevent_helper[count-1] = '\0';
47 	return count;
48 }
49 KERNEL_ATTR_RW(uevent_helper);
50 #endif
51 
52 #ifdef CONFIG_KEXEC
53 static ssize_t kexec_loaded_show(struct kset *kset, char *page)
54 {
55 	return sprintf(page, "%d\n", !!kexec_image);
56 }
57 KERNEL_ATTR_RO(kexec_loaded);
58 
59 static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page)
60 {
61 	return sprintf(page, "%d\n", !!kexec_crash_image);
62 }
63 KERNEL_ATTR_RO(kexec_crash_loaded);
64 #endif /* CONFIG_KEXEC */
65 
66 /*
67  * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
68  */
69 extern const void __start_notes __attribute__((weak));
70 extern const void __stop_notes __attribute__((weak));
71 #define	notes_size (&__stop_notes - &__start_notes)
72 
73 static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
74 			  char *buf, loff_t off, size_t count)
75 {
76 	memcpy(buf, &__start_notes + off, count);
77 	return count;
78 }
79 
80 static struct bin_attribute notes_attr = {
81 	.attr = {
82 		.name = "notes",
83 		.mode = S_IRUGO,
84 	},
85 	.read = &notes_read,
86 };
87 
88 decl_subsys(kernel, NULL, NULL);
89 EXPORT_SYMBOL_GPL(kernel_subsys);
90 
91 static struct attribute * kernel_attrs[] = {
92 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
93 	&uevent_seqnum_attr.attr,
94 	&uevent_helper_attr.attr,
95 #endif
96 #ifdef CONFIG_KEXEC
97 	&kexec_loaded_attr.attr,
98 	&kexec_crash_loaded_attr.attr,
99 #endif
100 	NULL
101 };
102 
103 static struct attribute_group kernel_attr_group = {
104 	.attrs = kernel_attrs,
105 };
106 
107 static int __init ksysfs_init(void)
108 {
109 	int error = subsystem_register(&kernel_subsys);
110 	if (!error)
111 		error = sysfs_create_group(&kernel_subsys.kobj,
112 					   &kernel_attr_group);
113 
114 	if (!error && notes_size > 0) {
115 		notes_attr.size = notes_size;
116 		error = sysfs_create_bin_file(&kernel_subsys.kobj,
117 					      &notes_attr);
118 	}
119 
120 	/*
121 	 * Create "/sys/kernel/uids" directory and corresponding root user's
122 	 * directory under it.
123 	 */
124 	if (!error)
125 		error = uids_kobject_init();
126 
127 	return error;
128 }
129 
130 core_initcall(ksysfs_init);
131