blob: e53bc30e9ba5aa8b55311fd6700191b0f21a9597 [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-Hartman0ff21e42007-11-06 10:36:58 -0800104struct kobject *kernel_kobj;
105EXPORT_SYMBOL_GPL(kernel_kobj);
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-Hartman0ff21e42007-11-06 10:36:58 -0800128 kernel_kobj = kobject_create_and_add("kernel", NULL);
129 if (!kernel_kobj) {
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100130 error = -ENOMEM;
131 goto exit;
132 }
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800133 error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100134 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-Hartman0ff21e42007-11-06 10:36:58 -0800139 error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100140 if (error)
141 goto group_exit;
Roland McGrathda1a6792007-07-19 01:48:39 -0700142 }
143
Kay Sieverseb41d942007-11-02 13:47:53 +0100144 /* create the /sys/kernel/uids/ directory */
145 error = uids_sysfs_init();
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100146 if (error)
147 goto notes_exit;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200148
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100149 return 0;
150
151notes_exit:
152 if (notes_size > 0)
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800153 sysfs_remove_bin_file(kernel_kobj, &notes_attr);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100154group_exit:
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800155 sysfs_remove_group(kernel_kobj, &kernel_attr_group);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100156kset_exit:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800157 kobject_put(kernel_kobj);
Greg Kroah-Hartmanbd35b932007-10-29 20:13:17 +0100158exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return error;
160}
161
162core_initcall(ksysfs_init);