| 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) \ | 
 | 20 | static struct subsys_attribute _name##_attr = __ATTR_RO(_name) | 
 | 21 |  | 
 | 22 | #define KERNEL_ATTR_RW(_name) \ | 
 | 23 | static struct subsys_attribute _name##_attr = \ | 
 | 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 */ | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 28 | static ssize_t uevent_seqnum_show(struct kset *kset, char *page) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 30 | 	return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 32 | KERNEL_ATTR_RO(uevent_seqnum); | 
 | 33 |  | 
 | 34 | /* uevent helper program, used during early boo */ | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 35 | static ssize_t uevent_helper_show(struct kset *kset, char *page) | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 36 | { | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 37 | 	return sprintf(page, "%s\n", uevent_helper); | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 38 | } | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 39 | static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count) | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 40 | { | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 41 | 	if (count+1 > UEVENT_HELPER_PATH_LEN) | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 42 | 		return -ENOENT; | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 43 | 	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 Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 47 | 	return count; | 
 | 48 | } | 
 | 49 | KERNEL_ATTR_RW(uevent_helper); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #endif | 
 | 51 |  | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_KEXEC | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 53 | static ssize_t kexec_loaded_show(struct kset *kset, char *page) | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 54 | { | 
 | 55 | 	return sprintf(page, "%d\n", !!kexec_image); | 
 | 56 | } | 
 | 57 | KERNEL_ATTR_RO(kexec_loaded); | 
 | 58 |  | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 59 | static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page) | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 60 | { | 
 | 61 | 	return sprintf(page, "%d\n", !!kexec_crash_image); | 
 | 62 | } | 
 | 63 | KERNEL_ATTR_RO(kexec_crash_loaded); | 
| Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 64 |  | 
 | 65 | static ssize_t vmcoreinfo_show(struct kset *kset, char *page) | 
 | 66 | { | 
 | 67 | 	return sprintf(page, "%lx %x\n", | 
 | 68 | 		       paddr_vmcoreinfo_note(), | 
| Ken'ichi Ohmichi | d768281 | 2007-10-16 23:27:28 -0700 | [diff] [blame] | 69 | 		       (unsigned int)vmcoreinfo_max_size); | 
| Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 70 | } | 
 | 71 | KERNEL_ATTR_RO(vmcoreinfo); | 
 | 72 |  | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 73 | #endif /* CONFIG_KEXEC */ | 
 | 74 |  | 
| Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 75 | /* | 
 | 76 |  * Make /sys/kernel/notes give the raw contents of our kernel .notes section. | 
 | 77 |  */ | 
| David Howells | 0b1937a | 2007-07-20 17:02:04 +0100 | [diff] [blame] | 78 | extern const void __start_notes __attribute__((weak)); | 
 | 79 | extern const void __stop_notes __attribute__((weak)); | 
| Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 80 | #define	notes_size (&__stop_notes - &__start_notes) | 
 | 81 |  | 
 | 82 | static 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 |  | 
 | 89 | static struct bin_attribute notes_attr = { | 
 | 90 | 	.attr = { | 
 | 91 | 		.name = "notes", | 
 | 92 | 		.mode = S_IRUGO, | 
 | 93 | 	}, | 
 | 94 | 	.read = ¬es_read, | 
 | 95 | }; | 
 | 96 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | decl_subsys(kernel, NULL, NULL); | 
 | 98 | EXPORT_SYMBOL_GPL(kernel_subsys); | 
 | 99 |  | 
 | 100 | static struct attribute * kernel_attrs[] = { | 
| Andrew Morton | f125b56 | 2006-03-24 03:18:44 -0800 | [diff] [blame] | 101 | #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) | 
| Kay Sievers | 0f76e5a | 2005-11-11 04:58:04 +0100 | [diff] [blame] | 102 | 	&uevent_seqnum_attr.attr, | 
 | 103 | 	&uevent_helper_attr.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #endif | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 105 | #ifdef CONFIG_KEXEC | 
 | 106 | 	&kexec_loaded_attr.attr, | 
 | 107 | 	&kexec_crash_loaded_attr.attr, | 
| Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 108 | 	&vmcoreinfo_attr.attr, | 
| Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 109 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | 	NULL | 
 | 111 | }; | 
 | 112 |  | 
 | 113 | static struct attribute_group kernel_attr_group = { | 
 | 114 | 	.attrs = kernel_attrs, | 
 | 115 | }; | 
 | 116 |  | 
 | 117 | static int __init ksysfs_init(void) | 
 | 118 | { | 
 | 119 | 	int error = subsystem_register(&kernel_subsys); | 
 | 120 | 	if (!error) | 
| Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 121 | 		error = sysfs_create_group(&kernel_subsys.kobj, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 					   &kernel_attr_group); | 
 | 123 |  | 
| Roland McGrath | da1a679 | 2007-07-19 01:48:39 -0700 | [diff] [blame] | 124 | 	if (!error && notes_size > 0) { | 
 | 125 | 		notes_attr.size = notes_size; | 
 | 126 | 		error = sysfs_create_bin_file(&kernel_subsys.kobj, | 
 | 127 | 					      ¬es_attr); | 
 | 128 | 	} | 
 | 129 |  | 
| Dhaval Giani | 5cb350b | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 130 | 	/* | 
 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 	return error; | 
 | 138 | } | 
 | 139 |  | 
 | 140 | core_initcall(ksysfs_init); |