Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 16 | #include <linux/kexec.h> |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 17 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #define KERNEL_ATTR_RO(_name) \ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 20 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define KERNEL_ATTR_RW(_name) \ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 23 | static struct kobj_attribute _name##_attr = \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | __ATTR(_name, 0644, _name##_show, _name##_store) |
| 25 | |
Andrew Morton | f125b56 | 2006-03-24 03:18:44 -0800 | [diff] [blame] | 26 | #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 27 | /* current uevent sequence number */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 28 | static ssize_t uevent_seqnum_show(struct kobject *kobj, |
| 29 | struct kobj_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 31 | return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 33 | KERNEL_ATTR_RO(uevent_seqnum); |
| 34 | |
| 35 | /* uevent helper program, used during early boo */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 36 | static ssize_t uevent_helper_show(struct kobject *kobj, |
| 37 | struct kobj_attribute *attr, char *buf) |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 38 | { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 39 | return sprintf(buf, "%s\n", uevent_helper); |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 40 | } |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 41 | static ssize_t uevent_helper_store(struct kobject *kobj, |
| 42 | struct kobj_attribute *attr, |
| 43 | const char *buf, size_t count) |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 44 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 45 | if (count+1 > UEVENT_HELPER_PATH_LEN) |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 46 | return -ENOENT; |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 47 | memcpy(uevent_helper, buf, count); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 48 | uevent_helper[count] = '\0'; |
| 49 | if (count && uevent_helper[count-1] == '\n') |
| 50 | uevent_helper[count-1] = '\0'; |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 51 | return count; |
| 52 | } |
| 53 | KERNEL_ATTR_RW(uevent_helper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 56 | #ifdef CONFIG_KEXEC |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 57 | static ssize_t kexec_loaded_show(struct kobject *kobj, |
| 58 | struct kobj_attribute *attr, char *buf) |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 59 | { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 60 | return sprintf(buf, "%d\n", !!kexec_image); |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 61 | } |
| 62 | KERNEL_ATTR_RO(kexec_loaded); |
| 63 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 64 | static ssize_t kexec_crash_loaded_show(struct kobject *kobj, |
| 65 | struct kobj_attribute *attr, char *buf) |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 66 | { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 67 | return sprintf(buf, "%d\n", !!kexec_crash_image); |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 68 | } |
| 69 | KERNEL_ATTR_RO(kexec_crash_loaded); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 70 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 71 | static ssize_t vmcoreinfo_show(struct kobject *kobj, |
| 72 | struct kobj_attribute *attr, char *buf) |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 73 | { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 74 | return sprintf(buf, "%lx %x\n", |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 75 | paddr_vmcoreinfo_note(), |
Ken'ichi Ohmichi | d768281 | 2007-10-16 23:27:28 -0700 | [diff] [blame] | 76 | (unsigned int)vmcoreinfo_max_size); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 77 | } |
| 78 | KERNEL_ATTR_RO(vmcoreinfo); |
| 79 | |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 80 | #endif /* CONFIG_KEXEC */ |
| 81 | |
Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Make /sys/kernel/notes give the raw contents of our kernel .notes section. |
| 84 | */ |
David Howells | 0b1937a | 2007-07-20 17:02:04 +0100 | [diff] [blame] | 85 | extern const void __start_notes __attribute__((weak)); |
| 86 | extern const void __stop_notes __attribute__((weak)); |
Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 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 = ¬es_read, |
| 102 | }; |
| 103 | |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 104 | struct kobject *kernel_kobj; |
| 105 | EXPORT_SYMBOL_GPL(kernel_kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | static struct attribute * kernel_attrs[] = { |
Andrew Morton | f125b56 | 2006-03-24 03:18:44 -0800 | [diff] [blame] | 108 | #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) |
Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 109 | &uevent_seqnum_attr.attr, |
| 110 | &uevent_helper_attr.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #endif |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_KEXEC |
| 113 | &kexec_loaded_attr.attr, |
| 114 | &kexec_crash_loaded_attr.attr, |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 115 | &vmcoreinfo_attr.attr, |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 116 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 126 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 128 | kernel_kobj = kobject_create_and_add("kernel", NULL); |
| 129 | if (!kernel_kobj) { |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 130 | error = -ENOMEM; |
| 131 | goto exit; |
| 132 | } |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 133 | error = sysfs_create_group(kernel_kobj, &kernel_attr_group); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 134 | if (error) |
| 135 | goto kset_exit; |
| 136 | |
| 137 | if (notes_size > 0) { |
Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 138 | notes_attr.size = notes_size; |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 139 | error = sysfs_create_bin_file(kernel_kobj, ¬es_attr); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 140 | if (error) |
| 141 | goto group_exit; |
Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Kay Sievers | eb41d94 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 144 | /* create the /sys/kernel/uids/ directory */ |
| 145 | error = uids_sysfs_init(); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 146 | if (error) |
| 147 | goto notes_exit; |
Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 148 | |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 149 | return 0; |
| 150 | |
| 151 | notes_exit: |
| 152 | if (notes_size > 0) |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 153 | sysfs_remove_bin_file(kernel_kobj, ¬es_attr); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 154 | group_exit: |
Greg Kroah-Hartman | 0ff21e4 | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 155 | sysfs_remove_group(kernel_kobj, &kernel_attr_group); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 156 | kset_exit: |
Greg Kroah-Hartman | 78a2d90 | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 157 | kobject_put(kernel_kobj); |
Greg Kroah-Hartman | bd35b93 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 158 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return error; |
| 160 | } |
| 161 | |
| 162 | core_initcall(ksysfs_init); |