xref: /openbmc/linux/kernel/ksysfs.c (revision 384740dc)
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 kobj_attribute _name##_attr = __ATTR_RO(_name)
21 
22 #define KERNEL_ATTR_RW(_name) \
23 static struct kobj_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 kobject *kobj,
29 				  struct kobj_attribute *attr, char *buf)
30 {
31 	return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
32 }
33 KERNEL_ATTR_RO(uevent_seqnum);
34 
35 /* uevent helper program, used during early boo */
36 static ssize_t uevent_helper_show(struct kobject *kobj,
37 				  struct kobj_attribute *attr, char *buf)
38 {
39 	return sprintf(buf, "%s\n", uevent_helper);
40 }
41 static ssize_t uevent_helper_store(struct kobject *kobj,
42 				   struct kobj_attribute *attr,
43 				   const char *buf, size_t count)
44 {
45 	if (count+1 > UEVENT_HELPER_PATH_LEN)
46 		return -ENOENT;
47 	memcpy(uevent_helper, buf, count);
48 	uevent_helper[count] = '\0';
49 	if (count && uevent_helper[count-1] == '\n')
50 		uevent_helper[count-1] = '\0';
51 	return count;
52 }
53 KERNEL_ATTR_RW(uevent_helper);
54 #endif
55 
56 #ifdef CONFIG_KEXEC
57 static ssize_t kexec_loaded_show(struct kobject *kobj,
58 				 struct kobj_attribute *attr, char *buf)
59 {
60 	return sprintf(buf, "%d\n", !!kexec_image);
61 }
62 KERNEL_ATTR_RO(kexec_loaded);
63 
64 static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
65 				       struct kobj_attribute *attr, char *buf)
66 {
67 	return sprintf(buf, "%d\n", !!kexec_crash_image);
68 }
69 KERNEL_ATTR_RO(kexec_crash_loaded);
70 
71 static ssize_t vmcoreinfo_show(struct kobject *kobj,
72 			       struct kobj_attribute *attr, char *buf)
73 {
74 	return sprintf(buf, "%lx %x\n",
75 		       paddr_vmcoreinfo_note(),
76 		       (unsigned int)vmcoreinfo_max_size);
77 }
78 KERNEL_ATTR_RO(vmcoreinfo);
79 
80 #endif /* CONFIG_KEXEC */
81 
82 /*
83  * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
84  */
85 extern const void __start_notes __attribute__((weak));
86 extern const void __stop_notes __attribute__((weak));
87 #define	notes_size (&__stop_notes - &__start_notes)
88 
89 static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
90 			  char *buf, loff_t off, size_t count)
91 {
92 	memcpy(buf, &__start_notes + off, count);
93 	return count;
94 }
95 
96 static struct bin_attribute notes_attr = {
97 	.attr = {
98 		.name = "notes",
99 		.mode = S_IRUGO,
100 	},
101 	.read = &notes_read,
102 };
103 
104 struct kobject *kernel_kobj;
105 EXPORT_SYMBOL_GPL(kernel_kobj);
106 
107 static struct attribute * kernel_attrs[] = {
108 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
109 	&uevent_seqnum_attr.attr,
110 	&uevent_helper_attr.attr,
111 #endif
112 #ifdef CONFIG_KEXEC
113 	&kexec_loaded_attr.attr,
114 	&kexec_crash_loaded_attr.attr,
115 	&vmcoreinfo_attr.attr,
116 #endif
117 	NULL
118 };
119 
120 static struct attribute_group kernel_attr_group = {
121 	.attrs = kernel_attrs,
122 };
123 
124 static int __init ksysfs_init(void)
125 {
126 	int error;
127 
128 	kernel_kobj = kobject_create_and_add("kernel", NULL);
129 	if (!kernel_kobj) {
130 		error = -ENOMEM;
131 		goto exit;
132 	}
133 	error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
134 	if (error)
135 		goto kset_exit;
136 
137 	if (notes_size > 0) {
138 		notes_attr.size = notes_size;
139 		error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
140 		if (error)
141 			goto group_exit;
142 	}
143 
144 	/* create the /sys/kernel/uids/ directory */
145 	error = uids_sysfs_init();
146 	if (error)
147 		goto notes_exit;
148 
149 	return 0;
150 
151 notes_exit:
152 	if (notes_size > 0)
153 		sysfs_remove_bin_file(kernel_kobj, &notes_attr);
154 group_exit:
155 	sysfs_remove_group(kernel_kobj, &kernel_attr_group);
156 kset_exit:
157 	kobject_put(kernel_kobj);
158 exit:
159 	return error;
160 }
161 
162 core_initcall(ksysfs_init);
163