ksysfs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/profile.h>
  17. #include <linux/sched.h>
  18. #define KERNEL_ATTR_RO(_name) \
  19. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  20. #define KERNEL_ATTR_RW(_name) \
  21. static struct kobj_attribute _name##_attr = \
  22. __ATTR(_name, 0644, _name##_show, _name##_store)
  23. #if defined(CONFIG_HOTPLUG)
  24. /* current uevent sequence number */
  25. static ssize_t uevent_seqnum_show(struct kobject *kobj,
  26. struct kobj_attribute *attr, char *buf)
  27. {
  28. return sprintf(buf, "%llu\n", (unsigned long long)uevent_seqnum);
  29. }
  30. KERNEL_ATTR_RO(uevent_seqnum);
  31. /* uevent helper program, used during early boo */
  32. static ssize_t uevent_helper_show(struct kobject *kobj,
  33. struct kobj_attribute *attr, char *buf)
  34. {
  35. return sprintf(buf, "%s\n", uevent_helper);
  36. }
  37. static ssize_t uevent_helper_store(struct kobject *kobj,
  38. struct kobj_attribute *attr,
  39. const char *buf, size_t count)
  40. {
  41. if (count+1 > UEVENT_HELPER_PATH_LEN)
  42. return -ENOENT;
  43. memcpy(uevent_helper, buf, count);
  44. uevent_helper[count] = '\0';
  45. if (count && uevent_helper[count-1] == '\n')
  46. uevent_helper[count-1] = '\0';
  47. return count;
  48. }
  49. KERNEL_ATTR_RW(uevent_helper);
  50. #endif
  51. #ifdef CONFIG_PROFILING
  52. static ssize_t profiling_show(struct kobject *kobj,
  53. struct kobj_attribute *attr, char *buf)
  54. {
  55. return sprintf(buf, "%d\n", prof_on);
  56. }
  57. static ssize_t profiling_store(struct kobject *kobj,
  58. struct kobj_attribute *attr,
  59. const char *buf, size_t count)
  60. {
  61. int ret;
  62. if (prof_on)
  63. return -EEXIST;
  64. /*
  65. * This eventually calls into get_option() which
  66. * has a ton of callers and is not const. It is
  67. * easiest to cast it away here.
  68. */
  69. profile_setup((char *)buf);
  70. ret = profile_init();
  71. if (ret)
  72. return ret;
  73. ret = create_proc_profile();
  74. if (ret)
  75. return ret;
  76. return count;
  77. }
  78. KERNEL_ATTR_RW(profiling);
  79. #endif
  80. #ifdef CONFIG_KEXEC
  81. static ssize_t kexec_loaded_show(struct kobject *kobj,
  82. struct kobj_attribute *attr, char *buf)
  83. {
  84. return sprintf(buf, "%d\n", !!kexec_image);
  85. }
  86. KERNEL_ATTR_RO(kexec_loaded);
  87. static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
  88. struct kobj_attribute *attr, char *buf)
  89. {
  90. return sprintf(buf, "%d\n", !!kexec_crash_image);
  91. }
  92. KERNEL_ATTR_RO(kexec_crash_loaded);
  93. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  94. struct kobj_attribute *attr, char *buf)
  95. {
  96. return sprintf(buf, "%lx %x\n",
  97. paddr_vmcoreinfo_note(),
  98. (unsigned int)vmcoreinfo_max_size);
  99. }
  100. KERNEL_ATTR_RO(vmcoreinfo);
  101. #endif /* CONFIG_KEXEC */
  102. /*
  103. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  104. */
  105. extern const void __start_notes __attribute__((weak));
  106. extern const void __stop_notes __attribute__((weak));
  107. #define notes_size (&__stop_notes - &__start_notes)
  108. static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
  109. char *buf, loff_t off, size_t count)
  110. {
  111. memcpy(buf, &__start_notes + off, count);
  112. return count;
  113. }
  114. static struct bin_attribute notes_attr = {
  115. .attr = {
  116. .name = "notes",
  117. .mode = S_IRUGO,
  118. },
  119. .read = &notes_read,
  120. };
  121. struct kobject *kernel_kobj;
  122. EXPORT_SYMBOL_GPL(kernel_kobj);
  123. static struct attribute * kernel_attrs[] = {
  124. #if defined(CONFIG_HOTPLUG)
  125. &uevent_seqnum_attr.attr,
  126. &uevent_helper_attr.attr,
  127. #endif
  128. #ifdef CONFIG_PROFILING
  129. &profiling_attr.attr,
  130. #endif
  131. #ifdef CONFIG_KEXEC
  132. &kexec_loaded_attr.attr,
  133. &kexec_crash_loaded_attr.attr,
  134. &vmcoreinfo_attr.attr,
  135. #endif
  136. NULL
  137. };
  138. static struct attribute_group kernel_attr_group = {
  139. .attrs = kernel_attrs,
  140. };
  141. static int __init ksysfs_init(void)
  142. {
  143. int error;
  144. kernel_kobj = kobject_create_and_add("kernel", NULL);
  145. if (!kernel_kobj) {
  146. error = -ENOMEM;
  147. goto exit;
  148. }
  149. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  150. if (error)
  151. goto kset_exit;
  152. if (notes_size > 0) {
  153. notes_attr.size = notes_size;
  154. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  155. if (error)
  156. goto group_exit;
  157. }
  158. /* create the /sys/kernel/uids/ directory */
  159. error = uids_sysfs_init();
  160. if (error)
  161. goto notes_exit;
  162. return 0;
  163. notes_exit:
  164. if (notes_size > 0)
  165. sysfs_remove_bin_file(kernel_kobj, &notes_attr);
  166. group_exit:
  167. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  168. kset_exit:
  169. kobject_put(kernel_kobj);
  170. exit:
  171. return error;
  172. }
  173. core_initcall(ksysfs_init);