Merge tag 'for-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/pateldipen19...
[linux-block.git] / lib / test_sysctl.c
CommitLineData
6cad1ecd 1// SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
9308f2f9
LR
2/*
3 * proc sysctl test driver
4 *
5 * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
9308f2f9
LR
6 */
7
8/*
2d046981 9 * This module provides an interface to the proc sysctl interfaces. This
9308f2f9
LR
10 * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the
11 * system unless explicitly requested by name. You can also build this driver
12 * into your kernel.
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/printk.h>
21#include <linux/fs.h>
22#include <linux/miscdevice.h>
23#include <linux/slab.h>
24#include <linux/uaccess.h>
25#include <linux/async.h>
26#include <linux/delay.h>
27#include <linux/vmalloc.h>
28
29static int i_zero;
30static int i_one_hundred = 100;
57b19468 31static int match_int_ok = 1;
9308f2f9
LR
32
33struct test_sysctl_data {
34 int int_0001;
eb965eda 35 int int_0002;
7c43a657 36 int int_0003[4];
eb965eda 37
4f2f682d
VB
38 int boot_int;
39
2920fad3
LR
40 unsigned int uint_0001;
41
9308f2f9 42 char string_0001[65];
2ea622b8
ES
43
44#define SYSCTL_TEST_BITMAP_SIZE 65536
45 unsigned long *bitmap_0001;
9308f2f9
LR
46};
47
48static struct test_sysctl_data test_data = {
49 .int_0001 = 60,
eb965eda
LR
50 .int_0002 = 1,
51
7c43a657
LR
52 .int_0003[0] = 0,
53 .int_0003[1] = 1,
54 .int_0003[2] = 2,
55 .int_0003[3] = 3,
56
4f2f682d
VB
57 .boot_int = 0,
58
2920fad3
LR
59 .uint_0001 = 314,
60
9308f2f9
LR
61 .string_0001 = "(none)",
62};
63
64/* These are all under /proc/sys/debug/test_sysctl/ */
65static struct ctl_table test_table[] = {
66 {
67 .procname = "int_0001",
68 .data = &test_data.int_0001,
69 .maxlen = sizeof(int),
70 .mode = 0644,
71 .proc_handler = proc_dointvec_minmax,
72 .extra1 = &i_zero,
73 .extra2 = &i_one_hundred,
74 },
eb965eda
LR
75 {
76 .procname = "int_0002",
77 .data = &test_data.int_0002,
78 .maxlen = sizeof(int),
79 .mode = 0644,
80 .proc_handler = proc_dointvec,
81 },
7c43a657
LR
82 {
83 .procname = "int_0003",
84 .data = &test_data.int_0003,
85 .maxlen = sizeof(test_data.int_0003),
86 .mode = 0644,
87 .proc_handler = proc_dointvec,
88 },
57b19468
TZ
89 {
90 .procname = "match_int",
91 .data = &match_int_ok,
92 .maxlen = sizeof(match_int_ok),
93 .mode = 0444,
94 .proc_handler = proc_dointvec,
95 },
4f2f682d
VB
96 {
97 .procname = "boot_int",
98 .data = &test_data.boot_int,
99 .maxlen = sizeof(test_data.boot_int),
100 .mode = 0644,
101 .proc_handler = proc_dointvec,
102 .extra1 = SYSCTL_ZERO,
103 .extra2 = SYSCTL_ONE,
104 },
2920fad3
LR
105 {
106 .procname = "uint_0001",
107 .data = &test_data.uint_0001,
108 .maxlen = sizeof(unsigned int),
109 .mode = 0644,
110 .proc_handler = proc_douintvec,
111 },
9308f2f9
LR
112 {
113 .procname = "string_0001",
114 .data = &test_data.string_0001,
115 .maxlen = sizeof(test_data.string_0001),
116 .mode = 0644,
117 .proc_handler = proc_dostring,
118 },
2ea622b8
ES
119 {
120 .procname = "bitmap_0001",
121 .data = &test_data.bitmap_0001,
122 .maxlen = SYSCTL_TEST_BITMAP_SIZE,
123 .mode = 0644,
124 .proc_handler = proc_do_large_bitmap,
125 },
9308f2f9
LR
126 { }
127};
128
9308f2f9
LR
129static struct ctl_table_header *test_sysctl_header;
130
131static int __init test_sysctl_init(void)
132{
57b19468
TZ
133 int i;
134
135 struct {
136 int defined;
137 int wanted;
138 } match_int[] = {
139 {.defined = *(int *)SYSCTL_ZERO, .wanted = 0},
140 {.defined = *(int *)SYSCTL_ONE, .wanted = 1},
141 {.defined = *(int *)SYSCTL_TWO, .wanted = 2},
142 {.defined = *(int *)SYSCTL_THREE, .wanted = 3},
143 {.defined = *(int *)SYSCTL_FOUR, .wanted = 4},
144 {.defined = *(int *)SYSCTL_ONE_HUNDRED, .wanted = 100},
145 {.defined = *(int *)SYSCTL_TWO_HUNDRED, .wanted = 200},
146 {.defined = *(int *)SYSCTL_ONE_THOUSAND, .wanted = 1000},
147 {.defined = *(int *)SYSCTL_THREE_THOUSAND, .wanted = 3000},
148 {.defined = *(int *)SYSCTL_INT_MAX, .wanted = INT_MAX},
149 {.defined = *(int *)SYSCTL_MAXOLDUID, .wanted = 65535},
150 {.defined = *(int *)SYSCTL_NEG_ONE, .wanted = -1},
151 };
152
153 for (i = 0; i < ARRAY_SIZE(match_int); i++)
154 if (match_int[i].defined != match_int[i].wanted)
155 match_int_ok = 0;
156
2ea622b8
ES
157 test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
158 if (!test_data.bitmap_0001)
159 return -ENOMEM;
04bc883c 160 test_sysctl_header = register_sysctl("debug/test_sysctl", test_table);
2ea622b8
ES
161 if (!test_sysctl_header) {
162 kfree(test_data.bitmap_0001);
9308f2f9 163 return -ENOMEM;
2ea622b8 164 }
9308f2f9
LR
165 return 0;
166}
2f56f845 167module_init(test_sysctl_init);
9308f2f9
LR
168
169static void __exit test_sysctl_exit(void)
170{
2ea622b8 171 kfree(test_data.bitmap_0001);
9308f2f9
LR
172 if (test_sysctl_header)
173 unregister_sysctl_table(test_sysctl_header);
174}
175
176module_exit(test_sysctl_exit);
177
178MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
179MODULE_LICENSE("GPL");