ksysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 kobj_attribute _name##_attr = __ATTR_RO(_name)
  19. #define KERNEL_ATTR_RW(_name) \
  20. static struct kobj_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 kobject *kobj,
  25. struct kobj_attribute *attr, char *buf)
  26. {
  27. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  28. }
  29. KERNEL_ATTR_RO(uevent_seqnum);
  30. /* uevent helper program, used during early boo */
  31. static ssize_t uevent_helper_show(struct kobject *kobj,
  32. struct kobj_attribute *attr, char *buf)
  33. {
  34. return sprintf(buf, "%s\n", uevent_helper);
  35. }
  36. static ssize_t uevent_helper_store(struct kobject *kobj,
  37. struct kobj_attribute *attr,
  38. const char *buf, size_t count)
  39. {
  40. if (count+1 > UEVENT_HELPER_PATH_LEN)
  41. return -ENOENT;
  42. memcpy(uevent_helper, buf, count);
  43. uevent_helper[count] = '\0';
  44. if (count && uevent_helper[count-1] == '\n')
  45. uevent_helper[count-1] = '\0';
  46. return count;
  47. }
  48. KERNEL_ATTR_RW(uevent_helper);
  49. #endif
  50. #ifdef CONFIG_KEXEC
  51. static ssize_t kexec_loaded_show(struct kobject *kobj,
  52. struct kobj_attribute *attr, char *buf)
  53. {
  54. return sprintf(buf, "%d\n", !!kexec_image);
  55. }
  56. KERNEL_ATTR_RO(kexec_loaded);
  57. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  58. struct kobj_attribute *attr, char *buf)
  59. {
  60. return sprintf(buf, "%d\n", !!kexec_crash_image);
  61. }
  62. KERNEL_ATTR_RO(kexec_crash_loaded);
  63. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  64. struct kobj_attribute *attr, char *buf)
  65. {
  66. return sprintf(buf, "%lx %x\n",
  67. paddr_vmcoreinfo_note(),
  68. (unsigned int)vmcoreinfo_max_size);
  69. }
  70. KERNEL_ATTR_RO(vmcoreinfo);
  71. #endif /* CONFIG_KEXEC */
  72. /*
  73. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  74. */
  75. extern const void __start_notes __attribute__((weak));
  76. extern const void __stop_notes __attribute__((weak));
  77. #define notes_size (&__stop_notes - &__start_notes)
  78. static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  79. char *buf, loff_t off, size_t count)
  80. {
  81. memcpy(buf, &__start_notes + off, count);
  82. return count;
  83. }
  84. static struct bin_attribute notes_attr = {
  85. .attr = {
  86. .name = "notes",
  87. .mode = S_IRUGO,
  88. },
  89. .read = &notes_read,
  90. };
  91. struct kobject *kernel_kobj;
  92. EXPORT_SYMBOL_GPL(kernel_kobj);
  93. static struct attribute * kernel_attrs[] = {
  94. #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
  95. &uevent_seqnum_attr.attr,
  96. &uevent_helper_attr.attr,
  97. #endif
  98. #ifdef CONFIG_KEXEC
  99. &kexec_loaded_attr.attr,
  100. &kexec_crash_loaded_attr.attr,
  101. &vmcoreinfo_attr.attr,
  102. #endif
  103. NULL
  104. };
  105. static struct attribute_group kernel_attr_group = {
  106. .attrs = kernel_attrs,
  107. };
  108. static int __init ksysfs_init(void)
  109. {
  110. int error;
  111. kernel_kobj = kobject_create_and_add("kernel", NULL);
  112. if (!kernel_kobj) {
  113. error = -ENOMEM;
  114. goto exit;
  115. }
  116. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  117. if (error)
  118. goto kset_exit;
  119. if (notes_size > 0) {
  120. notes_attr.size = notes_size;
  121. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  122. if (error)
  123. goto group_exit;
  124. }
  125. /* create the /sys/kernel/uids/ directory */
  126. error = uids_sysfs_init();
  127. if (error)
  128. goto notes_exit;
  129. return 0;
  130. notes_exit:
  131. if (notes_size > 0)
  132. sysfs_remove_bin_file(kernel_kobj, &notes_attr);
  133. group_exit:
  134. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  135. kset_exit:
  136. kobject_put(kernel_kobj);
  137. exit:
  138. return error;
  139. }
  140. core_initcall(ksysfs_init);