ksysfs.c 5.1 KB

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