blob: dd0f9e7f34145766ebc09bd88d8cb9105a7eeaeb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kobject.h>
12#include <linux/string.h>
13#include <linux/sysfs.h>
14#include <linux/module.h>
15#include <linux/init.h>
Jeff Moyerc330dda2006-06-23 02:05:07 -070016#include <linux/kexec.h>
Dhaval Giani5cb350b2007-10-15 17:00:14 +020017#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#define KERNEL_ATTR_RO(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010020static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define KERNEL_ATTR_RW(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010023static struct kobj_attribute _name##_attr = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 __ATTR(_name, 0644, _name##_show, _name##_store)
25
Andrew Mortonf125b562006-03-24 03:18:44 -080026#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010027/* current uevent sequence number */
Kay Sievers386f2752007-11-02 13:47:53 +010028static ssize_t uevent_seqnum_show(struct kobject *kobj,
29 struct kobj_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Kay Sievers386f2752007-11-02 13:47:53 +010031 return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
Kay Sievers0f76e5a2005-11-11 04:58:04 +010033KERNEL_ATTR_RO(uevent_seqnum);
34
35/* uevent helper program, used during early boo */
Kay Sievers386f2752007-11-02 13:47:53 +010036static ssize_t uevent_helper_show(struct kobject *kobj,
37 struct kobj_attribute *attr, char *buf)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010038{
Kay Sievers386f2752007-11-02 13:47:53 +010039 return sprintf(buf, "%s\n", uevent_helper);
Kay Sievers0f76e5a2005-11-11 04:58:04 +010040}
Kay Sievers386f2752007-11-02 13:47:53 +010041static ssize_t uevent_helper_store(struct kobject *kobj,
42 struct kobj_attribute *attr,
43 const char *buf, size_t count)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010044{
Kay Sievers312c0042005-11-16 09:00:00 +010045 if (count+1 > UEVENT_HELPER_PATH_LEN)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010046 return -ENOENT;
Kay Sievers386f2752007-11-02 13:47:53 +010047 memcpy(uevent_helper, buf, count);
Kay Sievers312c0042005-11-16 09:00:00 +010048 uevent_helper[count] = '\0';
49 if (count && uevent_helper[count-1] == '\n')
50 uevent_helper[count-1] = '\0';
Kay Sievers0f76e5a2005-11-11 04:58:04 +010051 return count;
52}
53KERNEL_ATTR_RW(uevent_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
55
Jeff Moyerc330dda2006-06-23 02:05:07 -070056#ifdef CONFIG_KEXEC
Kay Sievers386f2752007-11-02 13:47:53 +010057static ssize_t kexec_loaded_show(struct kobject *kobj,
58 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -070059{
Kay Sievers386f2752007-11-02 13:47:53 +010060 return sprintf(buf, "%d\n", !!kexec_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -070061}
62KERNEL_ATTR_RO(kexec_loaded);
63
Kay Sievers386f2752007-11-02 13:47:53 +010064static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
65 struct kobj_attribute *attr, char *buf)
Jeff Moyerc330dda2006-06-23 02:05:07 -070066{
Kay Sievers386f2752007-11-02 13:47:53 +010067 return sprintf(buf, "%d\n", !!kexec_crash_image);
Jeff Moyerc330dda2006-06-23 02:05:07 -070068}
69KERNEL_ATTR_RO(kexec_crash_loaded);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070070
Kay Sievers386f2752007-11-02 13:47:53 +010071static ssize_t vmcoreinfo_show(struct kobject *kobj,
72 struct kobj_attribute *attr, char *buf)
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070073{
Kay Sievers386f2752007-11-02 13:47:53 +010074 return sprintf(buf, "%lx %x\n",
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070075 paddr_vmcoreinfo_note(),
Ken'ichi Ohmichid7682812007-10-16 23:27:28 -070076 (unsigned int)vmcoreinfo_max_size);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070077}
78KERNEL_ATTR_RO(vmcoreinfo);
79
Jeff Moyerc330dda2006-06-23 02:05:07 -070080#endif /* CONFIG_KEXEC */
81
Roland McGrathda1a6792007-07-19 01:48:39 -070082/*
83 * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
84 */
David Howells0b1937a2007-07-20 17:02:04 +010085extern const void __start_notes __attribute__((weak));
86extern const void __stop_notes __attribute__((weak));
Roland McGrathda1a6792007-07-19 01:48:39 -070087#define notes_size (&__stop_notes - &__start_notes)
88
89static 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
96static struct bin_attribute notes_attr = {
97 .attr = {
98 .name = "notes",
99 .mode = S_IRUGO,
100 },
101 .read = &notes_read,
102};
103
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100104struct kset *kernel_kset;
105EXPORT_SYMBOL_GPL(kernel_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static struct attribute * kernel_attrs[] = {
Andrew Mortonf125b562006-03-24 03:18:44 -0800108#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
Kay Sievers0f76e5a2005-11-11 04:58:04 +0100109 &uevent_seqnum_attr.attr,
110 &uevent_helper_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
Jeff Moyerc330dda2006-06-23 02:05:07 -0700112#ifdef CONFIG_KEXEC
113 &kexec_loaded_attr.attr,
114 &kexec_crash_loaded_attr.attr,
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700115 &vmcoreinfo_attr.attr,
Jeff Moyerc330dda2006-06-23 02:05:07 -0700116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 NULL
118};
119
120static struct attribute_group kernel_attr_group = {
121 .attrs = kernel_attrs,
122};
123
124static int __init ksysfs_init(void)
125{
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100126 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100128 kernel_kset = kset_create_and_add("kernel", NULL, NULL);
129 if (!kernel_kset) {
130 error = -ENOMEM;
131 goto exit;
132 }
133 error = sysfs_create_group(&kernel_kset->kobj, &kernel_attr_group);
134 if (error)
135 goto kset_exit;
136
137 if (notes_size > 0) {
Roland McGrathda1a6792007-07-19 01:48:39 -0700138 notes_attr.size = notes_size;
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100139 error = sysfs_create_bin_file(&kernel_kset->kobj, &notes_attr);
140 if (error)
141 goto group_exit;
Roland McGrathda1a6792007-07-19 01:48:39 -0700142 }
143
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200144 /*
145 * Create "/sys/kernel/uids" directory and corresponding root user's
146 * directory under it.
147 */
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100148 error = uids_kobject_init();
149 if (error)
150 goto notes_exit;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200151
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100152 return 0;
153
154notes_exit:
155 if (notes_size > 0)
156 sysfs_remove_bin_file(&kernel_kset->kobj, &notes_attr);
157group_exit:
158 sysfs_remove_group(&kernel_kset->kobj, &kernel_attr_group);
159kset_exit:
160 kset_unregister(kernel_kset);
161exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return error;
163}
164
165core_initcall(ksysfs_init);