ksysfs.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #define KERNEL_ATTR_RO(_name) \
  17. static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
  18. #define KERNEL_ATTR_RW(_name) \
  19. static struct subsys_attribute _name##_attr = \
  20. __ATTR(_name, 0644, _name##_show, _name##_store)
  21. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  22. /* current uevent sequence number */
  23. static ssize_t uevent_seqnum_show(struct subsystem *subsys, char *page)
  24. {
  25. return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum);
  26. }
  27. KERNEL_ATTR_RO(uevent_seqnum);
  28. /* uevent helper program, used during early boo */
  29. static ssize_t uevent_helper_show(struct subsystem *subsys, char *page)
  30. {
  31. return sprintf(page, "%s\n", uevent_helper);
  32. }
  33. static ssize_t uevent_helper_store(struct subsystem *subsys, const char *page, size_t count)
  34. {
  35. if (count+1 > UEVENT_HELPER_PATH_LEN)
  36. return -ENOENT;
  37. memcpy(uevent_helper, page, count);
  38. uevent_helper[count] = '\0';
  39. if (count && uevent_helper[count-1] == '\n')
  40. uevent_helper[count-1] = '\0';
  41. return count;
  42. }
  43. KERNEL_ATTR_RW(uevent_helper);
  44. #endif
  45. #ifdef CONFIG_KEXEC
  46. static ssize_t kexec_loaded_show(struct subsystem *subsys, char *page)
  47. {
  48. return sprintf(page, "%d\n", !!kexec_image);
  49. }
  50. KERNEL_ATTR_RO(kexec_loaded);
  51. static ssize_t kexec_crash_loaded_show(struct subsystem *subsys, char *page)
  52. {
  53. return sprintf(page, "%d\n", !!kexec_crash_image);
  54. }
  55. KERNEL_ATTR_RO(kexec_crash_loaded);
  56. #endif /* CONFIG_KEXEC */
  57. decl_subsys(kernel, NULL, NULL);
  58. EXPORT_SYMBOL_GPL(kernel_subsys);
  59. static struct attribute * kernel_attrs[] = {
  60. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  61. &uevent_seqnum_attr.attr,
  62. &uevent_helper_attr.attr,
  63. #endif
  64. #ifdef CONFIG_KEXEC
  65. &kexec_loaded_attr.attr,
  66. &kexec_crash_loaded_attr.attr,
  67. #endif
  68. NULL
  69. };
  70. static struct attribute_group kernel_attr_group = {
  71. .attrs = kernel_attrs,
  72. };
  73. static int __init ksysfs_init(void)
  74. {
  75. int error = subsystem_register(&kernel_subsys);
  76. if (!error)
  77. error = sysfs_create_group(&kernel_subsys.kset.kobj,
  78. &kernel_attr_group);
  79. return error;
  80. }
  81. core_initcall(ksysfs_init);