blob: a5a3d6c27e1faf8f892ed65ec21b5e27277870ad [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/*
Randy Dunlap2d046982020-10-15 20:11:10 -070019 * This module provides an interface to the proc sysctl interfaces. This
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070020 * 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
Vlastimil Babka4f2f6822020-06-07 21:40:38 -070047 int boot_int;
48
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -070049 unsigned int uint_0001;
50
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070051 char string_0001[65];
Eric Sandeen2ea622b2019-05-14 15:45:10 -070052
53#define SYSCTL_TEST_BITMAP_SIZE 65536
54 unsigned long *bitmap_0001;
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070055};
56
57static struct test_sysctl_data test_data = {
58 .int_0001 = 60,
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070059 .int_0002 = 1,
60
Luis R. Rodriguez7c43a652017-07-12 14:33:58 -070061 .int_0003[0] = 0,
62 .int_0003[1] = 1,
63 .int_0003[2] = 2,
64 .int_0003[3] = 3,
65
Vlastimil Babka4f2f6822020-06-07 21:40:38 -070066 .boot_int = 0,
67
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -070068 .uint_0001 = 314,
69
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -070070 .string_0001 = "(none)",
71};
72
73/* These are all under /proc/sys/debug/test_sysctl/ */
74static 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 },
84 {
Luis R. Rodriguezeb965ed2017-07-12 14:33:52 -070085 .procname = "int_0002",
86 .data = &test_data.int_0002,
87 .maxlen = sizeof(int),
88 .mode = 0644,
89 .proc_handler = proc_dointvec,
90 },
91 {
Luis R. Rodriguez7c43a652017-07-12 14:33:58 -070092 .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 },
98 {
Vlastimil Babka4f2f6822020-06-07 21:40:38 -070099 .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 },
107 {
Luis R. Rodriguez2920fad2017-07-12 14:33:55 -0700108 .procname = "uint_0001",
109 .data = &test_data.uint_0001,
110 .maxlen = sizeof(unsigned int),
111 .mode = 0644,
112 .proc_handler = proc_douintvec,
113 },
114 {
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700115 .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 },
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700121 {
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 },
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700128 { }
129};
130
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700131static struct ctl_table_header *test_sysctl_header;
132
133static int __init test_sysctl_init(void)
134{
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700135 test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
136 if (!test_data.bitmap_0001)
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700137 return -ENOMEM;
Luis Chamberlain04bc8832022-01-21 22:11:54 -0800138 test_sysctl_header = register_sysctl("debug/test_sysctl", test_table);
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700139 if (!test_sysctl_header) {
140 kfree(test_data.bitmap_0001);
141 return -ENOMEM;
142 }
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700143 return 0;
144}
Masami Hiramatsu2f56f842020-05-28 23:52:16 +0900145module_init(test_sysctl_init);
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700146
147static void __exit test_sysctl_exit(void)
148{
Eric Sandeen2ea622b2019-05-14 15:45:10 -0700149 kfree(test_data.bitmap_0001);
Luis R. Rodriguez9308f2f2017-07-12 14:33:43 -0700150 if (test_sysctl_header)
151 unregister_sysctl_table(test_sysctl_header);
152}
153
154module_exit(test_sysctl_exit);
155
156MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
157MODULE_LICENSE("GPL");