Commit | Line | Data |
---|---|---|
9308f2f9 LR |
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 | /* | |
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 | ||
39 | static int i_zero; | |
40 | static int i_one_hundred = 100; | |
41 | ||
42 | struct test_sysctl_data { | |
43 | int int_0001; | |
eb965eda | 44 | int int_0002; |
7c43a657 | 45 | int int_0003[4]; |
eb965eda | 46 | |
4f2f682d VB |
47 | int boot_int; |
48 | ||
2920fad3 LR |
49 | unsigned int uint_0001; |
50 | ||
9308f2f9 | 51 | char string_0001[65]; |
2ea622b8 ES |
52 | |
53 | #define SYSCTL_TEST_BITMAP_SIZE 65536 | |
54 | unsigned long *bitmap_0001; | |
9308f2f9 LR |
55 | }; |
56 | ||
57 | static struct test_sysctl_data test_data = { | |
58 | .int_0001 = 60, | |
eb965eda LR |
59 | .int_0002 = 1, |
60 | ||
7c43a657 LR |
61 | .int_0003[0] = 0, |
62 | .int_0003[1] = 1, | |
63 | .int_0003[2] = 2, | |
64 | .int_0003[3] = 3, | |
65 | ||
4f2f682d VB |
66 | .boot_int = 0, |
67 | ||
2920fad3 LR |
68 | .uint_0001 = 314, |
69 | ||
9308f2f9 LR |
70 | .string_0001 = "(none)", |
71 | }; | |
72 | ||
73 | /* These are all under /proc/sys/debug/test_sysctl/ */ | |
74 | static struct ctl_table test_table[] = { | |
75 | { | |
76 | .procname = "int_0001", | |
77 | .data = &test_data.int_0001, | |
78 | .maxlen = sizeof(int), | |
79 | .mode = 0644, | |
80 | .proc_handler = proc_dointvec_minmax, | |
81 | .extra1 = &i_zero, | |
82 | .extra2 = &i_one_hundred, | |
83 | }, | |
eb965eda LR |
84 | { |
85 | .procname = "int_0002", | |
86 | .data = &test_data.int_0002, | |
87 | .maxlen = sizeof(int), | |
88 | .mode = 0644, | |
89 | .proc_handler = proc_dointvec, | |
90 | }, | |
7c43a657 LR |
91 | { |
92 | .procname = "int_0003", | |
93 | .data = &test_data.int_0003, | |
94 | .maxlen = sizeof(test_data.int_0003), | |
95 | .mode = 0644, | |
96 | .proc_handler = proc_dointvec, | |
97 | }, | |
4f2f682d VB |
98 | { |
99 | .procname = "boot_int", | |
100 | .data = &test_data.boot_int, | |
101 | .maxlen = sizeof(test_data.boot_int), | |
102 | .mode = 0644, | |
103 | .proc_handler = proc_dointvec, | |
104 | .extra1 = SYSCTL_ZERO, | |
105 | .extra2 = SYSCTL_ONE, | |
106 | }, | |
2920fad3 LR |
107 | { |
108 | .procname = "uint_0001", | |
109 | .data = &test_data.uint_0001, | |
110 | .maxlen = sizeof(unsigned int), | |
111 | .mode = 0644, | |
112 | .proc_handler = proc_douintvec, | |
113 | }, | |
9308f2f9 LR |
114 | { |
115 | .procname = "string_0001", | |
116 | .data = &test_data.string_0001, | |
117 | .maxlen = sizeof(test_data.string_0001), | |
118 | .mode = 0644, | |
119 | .proc_handler = proc_dostring, | |
120 | }, | |
2ea622b8 ES |
121 | { |
122 | .procname = "bitmap_0001", | |
123 | .data = &test_data.bitmap_0001, | |
124 | .maxlen = SYSCTL_TEST_BITMAP_SIZE, | |
125 | .mode = 0644, | |
126 | .proc_handler = proc_do_large_bitmap, | |
127 | }, | |
9308f2f9 LR |
128 | { } |
129 | }; | |
130 | ||
131 | static struct ctl_table test_sysctl_table[] = { | |
132 | { | |
133 | .procname = "test_sysctl", | |
134 | .maxlen = 0, | |
135 | .mode = 0555, | |
136 | .child = test_table, | |
137 | }, | |
138 | { } | |
139 | }; | |
140 | ||
141 | static struct ctl_table test_sysctl_root_table[] = { | |
142 | { | |
143 | .procname = "debug", | |
144 | .maxlen = 0, | |
145 | .mode = 0555, | |
146 | .child = test_sysctl_table, | |
147 | }, | |
148 | { } | |
149 | }; | |
150 | ||
151 | static struct ctl_table_header *test_sysctl_header; | |
152 | ||
153 | static int __init test_sysctl_init(void) | |
154 | { | |
2ea622b8 ES |
155 | test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL); |
156 | if (!test_data.bitmap_0001) | |
157 | return -ENOMEM; | |
9308f2f9 | 158 | test_sysctl_header = register_sysctl_table(test_sysctl_root_table); |
2ea622b8 ES |
159 | if (!test_sysctl_header) { |
160 | kfree(test_data.bitmap_0001); | |
9308f2f9 | 161 | return -ENOMEM; |
2ea622b8 | 162 | } |
9308f2f9 LR |
163 | return 0; |
164 | } | |
2f56f845 | 165 | module_init(test_sysctl_init); |
9308f2f9 LR |
166 | |
167 | static void __exit test_sysctl_exit(void) | |
168 | { | |
2ea622b8 | 169 | kfree(test_data.bitmap_0001); |
9308f2f9 LR |
170 | if (test_sysctl_header) |
171 | unregister_sysctl_table(test_sysctl_header); | |
172 | } | |
173 | ||
174 | module_exit(test_sysctl_exit); | |
175 | ||
176 | MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>"); | |
177 | MODULE_LICENSE("GPL"); |