ksysfs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <linux/kobject.h>
  11. #include <linux/string.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kexec.h>
  16. #include <linux/sched.h>
  17. #define KERNEL_ATTR_RO(_name) \
  18. static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
  19. #define KERNEL_ATTR_RW(_name) \
  20. static struct subsys_attribute _name##_attr = \
  21. __ATTR(_name, 0644, _name##_show, _name##_store)
  22. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  23. /* current uevent sequence number */
  24. static ssize_t uevent_seqnum_show(struct kset *kset, char *page)
  25. {
  26. return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum);
  27. }
  28. KERNEL_ATTR_RO(uevent_seqnum);
  29. /* uevent helper program, used during early boo */
  30. static ssize_t uevent_helper_show(struct kset *kset, char *page)
  31. {
  32. return sprintf(page, "%s\n", uevent_helper);
  33. }
  34. static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count)
  35. {
  36. if (count+1 > UEVENT_HELPER_PATH_LEN)
  37. return -ENOENT;
  38. memcpy(uevent_helper, page, count);
  39. uevent_helper[count] = '\0';
  40. if (count && uevent_helper[count-1] == '\n')
  41. uevent_helper[count-1] = '\0';
  42. return count;
  43. }
  44. KERNEL_ATTR_RW(uevent_helper);
  45. #endif
  46. #ifdef CONFIG_KEXEC
  47. static ssize_t kexec_loaded_show(struct kset *kset, char *page)
  48. {
  49. return sprintf(page, "%d\n", !!kexec_image);
  50. }
  51. KERNEL_ATTR_RO(kexec_loaded);
  52. static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page)
  53. {
  54. return sprintf(page, "%d\n", !!kexec_crash_image);
  55. }
  56. KERNEL_ATTR_RO(kexec_crash_loaded);
  57. #endif /* CONFIG_KEXEC */
  58. /*
  59. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  60. */
  61. extern const void __start_notes __attribute__((weak));
  62. extern const void __stop_notes __attribute__((weak));
  63. #define notes_size (&__stop_notes - &__start_notes)
  64. static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  65. char *buf, loff_t off, size_t count)
  66. {
  67. memcpy(buf, &__start_notes + off, count);
  68. return count;
  69. }
  70. static struct bin_attribute notes_attr = {
  71. .attr = {
  72. .name = "notes",
  73. .mode = S_IRUGO,
  74. },
  75. .read = &notes_read,
  76. };
  77. decl_subsys(kernel, NULL, NULL);
  78. EXPORT_SYMBOL_GPL(kernel_subsys);
  79. static struct attribute * kernel_attrs[] = {
  80. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  81. &uevent_seqnum_attr.attr,
  82. &uevent_helper_attr.attr,
  83. #endif
  84. #ifdef CONFIG_KEXEC
  85. &kexec_loaded_attr.attr,
  86. &kexec_crash_loaded_attr.attr,
  87. #endif
  88. NULL
  89. };
  90. static struct attribute_group kernel_attr_group = {
  91. .attrs = kernel_attrs,
  92. };
  93. static int __init ksysfs_init(void)
  94. {
  95. int error = subsystem_register(&kernel_subsys);
  96. if (!error)
  97. error = sysfs_create_group(&kernel_subsys.kobj,
  98. &kernel_attr_group);
  99. if (!error && notes_size > 0) {
  100. notes_attr.size = notes_size;
  101. error = sysfs_create_bin_file(&kernel_subsys.kobj,
  102. &notes_attr);
  103. }
  104. /*
  105. * Create "/sys/kernel/uids" directory and corresponding root user's
  106. * directory under it.
  107. */
  108. if (!error)
  109. error = uids_kobject_init();
  110. return error;
  111. }
  112. core_initcall(ksysfs_init);