blob: 566dad3f419601165bc32e71f2f2a580afbc3c8c [file] [log] [blame]
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -07001/*
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/*
19 * This module provides an interface to the the proc sysctl interfaces. This
20 * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the
21 * system unless explicitly requested by name. You can also build this driver
22 * into your kernel.
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/printk.h>
31#include <linux/fs.h>
32#include <linux/miscdevice.h>
33#include <linux/slab.h>
34#include <linux/uaccess.h>
35#include <linux/async.h>
36#include <linux/delay.h>
37#include <linux/vmalloc.h>
38
39static int i_zero;
40static int i_one_hundred = 100;
41
42struct test_sysctl_data {
43 int int_0001;
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070044 int int_0002;
Luis R. Rodriguez7c43a652017-07-12 14:33:58 -070045 int int_0003[4];
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070046
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -070047 unsigned int uint_0001;
48
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070049 char string_0001[65];
Eric Sandeen2ea622b2019-05-14 15:45:10 -070050
51#define SYSCTL_TEST_BITMAP_SIZE 65536
52 unsigned long *bitmap_0001;
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070053};
54
55static struct test_sysctl_data test_data = {
56 .int_0001 = 60,
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070057 .int_0002 = 1,
58
Luis R. Rodriguez7c43a652017-07-12 14:33:58 -070059 .int_0003[0] = 0,
60 .int_0003[1] = 1,
61 .int_0003[2] = 2,
62 .int_0003[3] = 3,
63
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -070064 .uint_0001 = 314,
65
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070066 .string_0001 = "(none)",
67};
68
69/* These are all under /proc/sys/debug/test_sysctl/ */
70static struct ctl_table test_table[] = {
71 {
72 .procname = "int_0001",
73 .data = &test_data.int_0001,
74 .maxlen = sizeof(int),
75 .mode = 0644,
76 .proc_handler = proc_dointvec_minmax,
77 .extra1 = &i_zero,
78 .extra2 = &i_one_hundred,
79 },
80 {
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070081 .procname = "int_0002",
82 .data = &test_data.int_0002,
83 .maxlen = sizeof(int),
84 .mode = 0644,
85 .proc_handler = proc_dointvec,
86 },
87 {
Luis R. Rodriguez7c43a652017-07-12 14:33:58 -070088 .procname = "int_0003",
89 .data = &test_data.int_0003,
90 .maxlen = sizeof(test_data.int_0003),
91 .mode = 0644,
92 .proc_handler = proc_dointvec,
93 },
94 {
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -070095 .procname = "uint_0001",
96 .data = &test_data.uint_0001,
97 .maxlen = sizeof(unsigned int),
98 .mode = 0644,
99 .proc_handler = proc_douintvec,
100 },
101 {
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700102 .procname = "string_0001",
103 .data = &test_data.string_0001,
104 .maxlen = sizeof(test_data.string_0001),
105 .mode = 0644,
106 .proc_handler = proc_dostring,
107 },
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700108 {
109 .procname = "bitmap_0001",
110 .data = &test_data.bitmap_0001,
111 .maxlen = SYSCTL_TEST_BITMAP_SIZE,
112 .mode = 0644,
113 .proc_handler = proc_do_large_bitmap,
114 },
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700115 { }
116};
117
118static struct ctl_table test_sysctl_table[] = {
119 {
120 .procname = "test_sysctl",
121 .maxlen = 0,
122 .mode = 0555,
123 .child = test_table,
124 },
125 { }
126};
127
128static struct ctl_table test_sysctl_root_table[] = {
129 {
130 .procname = "debug",
131 .maxlen = 0,
132 .mode = 0555,
133 .child = test_sysctl_table,
134 },
135 { }
136};
137
138static struct ctl_table_header *test_sysctl_header;
139
140static int __init test_sysctl_init(void)
141{
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700142 test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
143 if (!test_data.bitmap_0001)
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700144 return -ENOMEM;
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700145 test_sysctl_header = register_sysctl_table(test_sysctl_root_table);
146 if (!test_sysctl_header) {
147 kfree(test_data.bitmap_0001);
148 return -ENOMEM;
149 }
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700150 return 0;
151}
152late_initcall(test_sysctl_init);
153
154static void __exit test_sysctl_exit(void)
155{
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700156 kfree(test_data.bitmap_0001);
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700157 if (test_sysctl_header)
158 unregister_sysctl_table(test_sysctl_header);
159}
160
161module_exit(test_sysctl_exit);
162
163MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
164MODULE_LICENSE("GPL");