ksysfs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /* 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. #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 kexec_crash_size_show(struct kobject *kobj,
  94. struct kobj_attribute *attr, char *buf)
  95. {
  96. return sprintf(buf, "%zu\n", crash_get_memory_size());
  97. }
  98. static ssize_t kexec_crash_size_store(struct kobject *kobj,
  99. struct kobj_attribute *attr,
  100. const char *buf, size_t count)
  101. {
  102. unsigned long cnt;
  103. int ret;
  104. if (strict_strtoul(buf, 0, &cnt))
  105. return -EINVAL;
  106. ret = crash_shrink_memory(cnt);
  107. return ret < 0 ? ret : count;
  108. }
  109. KERNEL_ATTR_RW(kexec_crash_size);
  110. static ssize_t vmcoreinfo_show(struct kobject *kobj,
  111. struct kobj_attribute *attr, char *buf)
  112. {
  113. return sprintf(buf, "%lx %x\n",
  114. paddr_vmcoreinfo_note(),
  115. (unsigned int)vmcoreinfo_max_size);
  116. }
  117. KERNEL_ATTR_RO(vmcoreinfo);
  118. #endif /* CONFIG_KEXEC */
  119. /* whether file capabilities are enabled */
  120. static ssize_t fscaps_show(struct kobject *kobj,
  121. struct kobj_attribute *attr, char *buf)
  122. {
  123. return sprintf(buf, "%d\n", file_caps_enabled);
  124. }
  125. KERNEL_ATTR_RO(fscaps);
  126. /*
  127. * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
  128. */
  129. extern const void __start_notes __attribute__((weak));
  130. extern const void __stop_notes __attribute__((weak));
  131. #define notes_size (&__stop_notes - &__start_notes)
  132. static ssize_t notes_read(struct file *filp, struct kobject *kobj,
  133. struct bin_attribute *bin_attr,
  134. char *buf, loff_t off, size_t count)
  135. {
  136. memcpy(buf, &__start_notes + off, count);
  137. return count;
  138. }
  139. static struct bin_attribute notes_attr = {
  140. .attr = {
  141. .name = "notes",
  142. .mode = S_IRUGO,
  143. },
  144. .read = &notes_read,
  145. };
  146. struct kobject *kernel_kobj;
  147. EXPORT_SYMBOL_GPL(kernel_kobj);
  148. static struct attribute * kernel_attrs[] = {
  149. &fscaps_attr.attr,
  150. &uevent_seqnum_attr.attr,
  151. &uevent_helper_attr.attr,
  152. #ifdef CONFIG_PROFILING
  153. &profiling_attr.attr,
  154. #endif
  155. #ifdef CONFIG_KEXEC
  156. &kexec_loaded_attr.attr,
  157. &kexec_crash_loaded_attr.attr,
  158. &kexec_crash_size_attr.attr,
  159. &vmcoreinfo_attr.attr,
  160. #endif
  161. NULL
  162. };
  163. static struct attribute_group kernel_attr_group = {
  164. .attrs = kernel_attrs,
  165. };
  166. static int __init ksysfs_init(void)
  167. {
  168. int error;
  169. kernel_kobj = kobject_create_and_add("kernel", NULL);
  170. if (!kernel_kobj) {
  171. error = -ENOMEM;
  172. goto exit;
  173. }
  174. error = sysfs_create_group(kernel_kobj, &kernel_attr_group);
  175. if (error)
  176. goto kset_exit;
  177. if (notes_size > 0) {
  178. notes_attr.size = notes_size;
  179. error = sysfs_create_bin_file(kernel_kobj, &notes_attr);
  180. if (error)
  181. goto group_exit;
  182. }
  183. return 0;
  184. group_exit:
  185. sysfs_remove_group(kernel_kobj, &kernel_attr_group);
  186. kset_exit:
  187. kobject_put(kernel_kobj);
  188. exit:
  189. return error;
  190. }
  191. core_initcall(ksysfs_init);