test_sysctl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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. unsigned int uint_0001;
  42. char string_0001[65];
  43. };
  44. static struct test_sysctl_data test_data = {
  45. .int_0001 = 60,
  46. .int_0002 = 1,
  47. .int_0003[0] = 0,
  48. .int_0003[1] = 1,
  49. .int_0003[2] = 2,
  50. .int_0003[3] = 3,
  51. .uint_0001 = 314,
  52. .string_0001 = "(none)",
  53. };
  54. /* These are all under /proc/sys/debug/test_sysctl/ */
  55. static struct ctl_table test_table[] = {
  56. {
  57. .procname = "int_0001",
  58. .data = &test_data.int_0001,
  59. .maxlen = sizeof(int),
  60. .mode = 0644,
  61. .proc_handler = proc_dointvec_minmax,
  62. .extra1 = &i_zero,
  63. .extra2 = &i_one_hundred,
  64. },
  65. {
  66. .procname = "int_0002",
  67. .data = &test_data.int_0002,
  68. .maxlen = sizeof(int),
  69. .mode = 0644,
  70. .proc_handler = proc_dointvec,
  71. },
  72. {
  73. .procname = "int_0003",
  74. .data = &test_data.int_0003,
  75. .maxlen = sizeof(test_data.int_0003),
  76. .mode = 0644,
  77. .proc_handler = proc_dointvec,
  78. },
  79. {
  80. .procname = "uint_0001",
  81. .data = &test_data.uint_0001,
  82. .maxlen = sizeof(unsigned int),
  83. .mode = 0644,
  84. .proc_handler = proc_douintvec,
  85. },
  86. {
  87. .procname = "string_0001",
  88. .data = &test_data.string_0001,
  89. .maxlen = sizeof(test_data.string_0001),
  90. .mode = 0644,
  91. .proc_handler = proc_dostring,
  92. },
  93. { }
  94. };
  95. static struct ctl_table test_sysctl_table[] = {
  96. {
  97. .procname = "test_sysctl",
  98. .maxlen = 0,
  99. .mode = 0555,
  100. .child = test_table,
  101. },
  102. { }
  103. };
  104. static struct ctl_table test_sysctl_root_table[] = {
  105. {
  106. .procname = "debug",
  107. .maxlen = 0,
  108. .mode = 0555,
  109. .child = test_sysctl_table,
  110. },
  111. { }
  112. };
  113. static struct ctl_table_header *test_sysctl_header;
  114. static int __init test_sysctl_init(void)
  115. {
  116. test_sysctl_header = register_sysctl_table(test_sysctl_root_table);
  117. if (!test_sysctl_header)
  118. return -ENOMEM;
  119. return 0;
  120. }
  121. late_initcall(test_sysctl_init);
  122. static void __exit test_sysctl_exit(void)
  123. {
  124. if (test_sysctl_header)
  125. unregister_sysctl_table(test_sysctl_header);
  126. }
  127. module_exit(test_sysctl_exit);
  128. MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
  129. MODULE_LICENSE("GPL");