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