test_sysctl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * proc sysctl test driver
  3. *
  4. * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or at your option any
  9. * later version; or, when distributed separately from the Linux kernel or
  10. * when incorporated into other software packages, subject to the following
  11. * license:
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of copyleft-next (version 0.3.1 or later) as published
  15. * at http://copyleft-next.org/.
  16. */
  17. /*
  18. * This module provides an interface to the proc sysctl interfaces. This
  19. * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the
  20. * system unless explicitly requested by name. You can also build this driver
  21. * into your kernel.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/module.h>
  27. #include <linux/printk.h>
  28. #include <linux/fs.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/async.h>
  33. #include <linux/delay.h>
  34. #include <linux/vmalloc.h>
  35. static int i_zero;
  36. static int i_one_hundred = 100;
  37. struct test_sysctl_data {
  38. int int_0001;
  39. int int_0002;
  40. int int_0003[4];
  41. int boot_int;
  42. unsigned int uint_0001;
  43. char string_0001[65];
  44. #define SYSCTL_TEST_BITMAP_SIZE 65536
  45. unsigned long *bitmap_0001;
  46. };
  47. static struct test_sysctl_data test_data = {
  48. .int_0001 = 60,
  49. .int_0002 = 1,
  50. .int_0003[0] = 0,
  51. .int_0003[1] = 1,
  52. .int_0003[2] = 2,
  53. .int_0003[3] = 3,
  54. .boot_int = 0,
  55. .uint_0001 = 314,
  56. .string_0001 = "(none)",
  57. };
  58. /* These are all under /proc/sys/debug/test_sysctl/ */
  59. static struct ctl_table test_table[] = {
  60. {
  61. .procname = "int_0001",
  62. .data = &test_data.int_0001,
  63. .maxlen = sizeof(int),
  64. .mode = 0644,
  65. .proc_handler = proc_dointvec_minmax,
  66. .extra1 = &i_zero,
  67. .extra2 = &i_one_hundred,
  68. },
  69. {
  70. .procname = "int_0002",
  71. .data = &test_data.int_0002,
  72. .maxlen = sizeof(int),
  73. .mode = 0644,
  74. .proc_handler = proc_dointvec,
  75. },
  76. {
  77. .procname = "int_0003",
  78. .data = &test_data.int_0003,
  79. .maxlen = sizeof(test_data.int_0003),
  80. .mode = 0644,
  81. .proc_handler = proc_dointvec,
  82. },
  83. {
  84. .procname = "boot_int",
  85. .data = &test_data.boot_int,
  86. .maxlen = sizeof(test_data.boot_int),
  87. .mode = 0644,
  88. .proc_handler = proc_dointvec,
  89. .extra1 = SYSCTL_ZERO,
  90. .extra2 = SYSCTL_ONE,
  91. },
  92. {
  93. .procname = "uint_0001",
  94. .data = &test_data.uint_0001,
  95. .maxlen = sizeof(unsigned int),
  96. .mode = 0644,
  97. .proc_handler = proc_douintvec,
  98. },
  99. {
  100. .procname = "string_0001",
  101. .data = &test_data.string_0001,
  102. .maxlen = sizeof(test_data.string_0001),
  103. .mode = 0644,
  104. .proc_handler = proc_dostring,
  105. },
  106. {
  107. .procname = "bitmap_0001",
  108. .data = &test_data.bitmap_0001,
  109. .maxlen = SYSCTL_TEST_BITMAP_SIZE,
  110. .mode = 0644,
  111. .proc_handler = proc_do_large_bitmap,
  112. },
  113. { }
  114. };
  115. static struct ctl_table test_sysctl_table[] = {
  116. {
  117. .procname = "test_sysctl",
  118. .maxlen = 0,
  119. .mode = 0555,
  120. .child = test_table,
  121. },
  122. { }
  123. };
  124. static struct ctl_table test_sysctl_root_table[] = {
  125. {
  126. .procname = "debug",
  127. .maxlen = 0,
  128. .mode = 0555,
  129. .child = test_sysctl_table,
  130. },
  131. { }
  132. };
  133. static struct ctl_table_header *test_sysctl_header;
  134. static int __init test_sysctl_init(void)
  135. {
  136. test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
  137. if (!test_data.bitmap_0001)
  138. return -ENOMEM;
  139. test_sysctl_header = register_sysctl_table(test_sysctl_root_table);
  140. if (!test_sysctl_header) {
  141. kfree(test_data.bitmap_0001);
  142. return -ENOMEM;
  143. }
  144. return 0;
  145. }
  146. module_init(test_sysctl_init);
  147. static void __exit test_sysctl_exit(void)
  148. {
  149. kfree(test_data.bitmap_0001);
  150. if (test_sysctl_header)
  151. unregister_sysctl_table(test_sysctl_header);
  152. }
  153. module_exit(test_sysctl_exit);
  154. MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
  155. MODULE_LICENSE("GPL");