blob: 38b38f957ef0747fdbe6c4ab751ffb7037314d4f [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) \
20static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
21
22#define KERNEL_ATTR_RW(_name) \
23static struct subsys_attribute _name##_attr = \
24 __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 */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070028static ssize_t uevent_seqnum_show(struct kset *kset, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Kay Sievers312c0042005-11-16 09:00:00 +010030 return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
Kay Sievers0f76e5a2005-11-11 04:58:04 +010032KERNEL_ATTR_RO(uevent_seqnum);
33
34/* uevent helper program, used during early boo */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070035static ssize_t uevent_helper_show(struct kset *kset, char *page)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010036{
Kay Sievers312c0042005-11-16 09:00:00 +010037 return sprintf(page, "%s\n", uevent_helper);
Kay Sievers0f76e5a2005-11-11 04:58:04 +010038}
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070039static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010040{
Kay Sievers312c0042005-11-16 09:00:00 +010041 if (count+1 > UEVENT_HELPER_PATH_LEN)
Kay Sievers0f76e5a2005-11-11 04:58:04 +010042 return -ENOENT;
Kay Sievers312c0042005-11-16 09:00:00 +010043 memcpy(uevent_helper, page, count);
44 uevent_helper[count] = '\0';
45 if (count && uevent_helper[count-1] == '\n')
46 uevent_helper[count-1] = '\0';
Kay Sievers0f76e5a2005-11-11 04:58:04 +010047 return count;
48}
49KERNEL_ATTR_RW(uevent_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
51
Jeff Moyerc330dda2006-06-23 02:05:07 -070052#ifdef CONFIG_KEXEC
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070053static ssize_t kexec_loaded_show(struct kset *kset, char *page)
Jeff Moyerc330dda2006-06-23 02:05:07 -070054{
55 return sprintf(page, "%d\n", !!kexec_image);
56}
57KERNEL_ATTR_RO(kexec_loaded);
58
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070059static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page)
Jeff Moyerc330dda2006-06-23 02:05:07 -070060{
61 return sprintf(page, "%d\n", !!kexec_crash_image);
62}
63KERNEL_ATTR_RO(kexec_crash_loaded);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070064
65static ssize_t vmcoreinfo_show(struct kset *kset, char *page)
66{
67 return sprintf(page, "%lx %x\n",
68 paddr_vmcoreinfo_note(),
69 vmcoreinfo_max_size);
70}
71KERNEL_ATTR_RO(vmcoreinfo);
72
Jeff Moyerc330dda2006-06-23 02:05:07 -070073#endif /* CONFIG_KEXEC */
74
Roland McGrathda1a6792007-07-19 01:48:39 -070075/*
76 * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
77 */
David Howells0b1937a2007-07-20 17:02:04 +010078extern const void __start_notes __attribute__((weak));
79extern const void __stop_notes __attribute__((weak));
Roland McGrathda1a6792007-07-19 01:48:39 -070080#define notes_size (&__stop_notes - &__start_notes)
81
82static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
83 char *buf, loff_t off, size_t count)
84{
85 memcpy(buf, &__start_notes + off, count);
86 return count;
87}
88
89static struct bin_attribute notes_attr = {
90 .attr = {
91 .name = "notes",
92 .mode = S_IRUGO,
93 },
94 .read = &notes_read,
95};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097decl_subsys(kernel, NULL, NULL);
98EXPORT_SYMBOL_GPL(kernel_subsys);
99
100static struct attribute * kernel_attrs[] = {
Andrew Mortonf125b562006-03-24 03:18:44 -0800101#if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
Kay Sievers0f76e5a2005-11-11 04:58:04 +0100102 &uevent_seqnum_attr.attr,
103 &uevent_helper_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
Jeff Moyerc330dda2006-06-23 02:05:07 -0700105#ifdef CONFIG_KEXEC
106 &kexec_loaded_attr.attr,
107 &kexec_crash_loaded_attr.attr,
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700108 &vmcoreinfo_attr.attr,
Jeff Moyerc330dda2006-06-23 02:05:07 -0700109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 NULL
111};
112
113static struct attribute_group kernel_attr_group = {
114 .attrs = kernel_attrs,
115};
116
117static int __init ksysfs_init(void)
118{
119 int error = subsystem_register(&kernel_subsys);
120 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700121 error = sysfs_create_group(&kernel_subsys.kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 &kernel_attr_group);
123
Roland McGrathda1a6792007-07-19 01:48:39 -0700124 if (!error && notes_size > 0) {
125 notes_attr.size = notes_size;
126 error = sysfs_create_bin_file(&kernel_subsys.kobj,
127 &notes_attr);
128 }
129
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200130 /*
131 * Create "/sys/kernel/uids" directory and corresponding root user's
132 * directory under it.
133 */
134 if (!error)
135 error = uids_kobject_init();
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return error;
138}
139
140core_initcall(ksysfs_init);