1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * sysctl.h: General linux system control interface
4 *
5 * Begun 24 March 1995, Stephen Tweedie
6 *
7 ****************************************************************
8 ****************************************************************
9 **
10 **  WARNING:
11 **  The values in this file are exported to user space via
12 **  the sysctl() binary interface.  Do *NOT* change the
13 **  numbering of any existing values here, and do not change
14 **  any numbers within any one set of values.  If you have to
15 **  redefine an existing interface, use a new number for it.
16 **  The kernel will then return -ENOTDIR to any application using
17 **  the old binary interface.
18 **
19 ****************************************************************
20 ****************************************************************
21 */
22#ifndef _LINUX_SYSCTL_H
23#define _LINUX_SYSCTL_H
24
25#include <linux/list.h>
26#include <linux/rcupdate.h>
27#include <linux/wait.h>
28#include <linux/rbtree.h>
29#include <linux/uidgid.h>
30#include <uapi/linux/sysctl.h>
31
32/* For the /proc/sys support */
33struct completion;
34struct ctl_table;
35struct nsproxy;
36struct ctl_table_root;
37struct ctl_table_header;
38struct ctl_dir;
39
40/* Keep the same order as in fs/proc/proc_sysctl.c */
41#define SYSCTL_ZERO			((void *)&sysctl_vals[0])
42#define SYSCTL_ONE			((void *)&sysctl_vals[1])
43#define SYSCTL_TWO			((void *)&sysctl_vals[2])
44#define SYSCTL_THREE			((void *)&sysctl_vals[3])
45#define SYSCTL_FOUR			((void *)&sysctl_vals[4])
46#define SYSCTL_ONE_HUNDRED		((void *)&sysctl_vals[5])
47#define SYSCTL_TWO_HUNDRED		((void *)&sysctl_vals[6])
48#define SYSCTL_ONE_THOUSAND		((void *)&sysctl_vals[7])
49#define SYSCTL_THREE_THOUSAND		((void *)&sysctl_vals[8])
50#define SYSCTL_INT_MAX			((void *)&sysctl_vals[9])
51
52/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
53#define SYSCTL_MAXOLDUID		((void *)&sysctl_vals[10])
54#define SYSCTL_NEG_ONE			((void *)&sysctl_vals[11])
55
56extern const int sysctl_vals[];
57
58#define SYSCTL_LONG_ZERO	((void *)&sysctl_long_vals[0])
59#define SYSCTL_LONG_ONE		((void *)&sysctl_long_vals[1])
60#define SYSCTL_LONG_MAX		((void *)&sysctl_long_vals[2])
61
62extern const unsigned long sysctl_long_vals[];
63
64typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
65		size_t *lenp, loff_t *ppos);
66
67int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
68int proc_dobool(struct ctl_table *table, int write, void *buffer,
69		size_t *lenp, loff_t *ppos);
70int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
71int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
72int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
73int proc_douintvec_minmax(struct ctl_table *table, int write, void *buffer,
74		size_t *lenp, loff_t *ppos);
75int proc_dou8vec_minmax(struct ctl_table *table, int write, void *buffer,
76			size_t *lenp, loff_t *ppos);
77int proc_dointvec_jiffies(struct ctl_table *, int, void *, size_t *, loff_t *);
78int proc_dointvec_userhz_jiffies(struct ctl_table *, int, void *, size_t *,
79		loff_t *);
80int proc_dointvec_ms_jiffies(struct ctl_table *, int, void *, size_t *,
81		loff_t *);
82int proc_doulongvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
83int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void *,
84		size_t *, loff_t *);
85int proc_do_large_bitmap(struct ctl_table *, int, void *, size_t *, loff_t *);
86int proc_do_static_key(struct ctl_table *table, int write, void *buffer,
87		size_t *lenp, loff_t *ppos);
88
89/*
90 * Register a set of sysctl names by calling register_sysctl_table
91 * with an initialised array of struct ctl_table's.  An entry with
92 * NULL procname terminates the table.  table->de will be
93 * set up by the registration and need not be initialised in advance.
94 *
95 * sysctl names can be mirrored automatically under /proc/sys.  The
96 * procname supplied controls /proc naming.
97 *
98 * The table's mode will be honoured for proc-fs access.
99 *
100 * Leaf nodes in the sysctl tree will be represented by a single file
101 * under /proc; non-leaf nodes will be represented by directories.  A
102 * null procname disables /proc mirroring at this node.
103 *
104 * The data and maxlen fields of the ctl_table
105 * struct enable minimal validation of the values being written to be
106 * performed, and the mode field allows minimal authentication.
107 *
108 * There must be a proc_handler routine for any terminal nodes
109 * mirrored under /proc/sys (non-terminals are handled by a built-in
110 * directory handler).  Several default handlers are available to
111 * cover common cases.
112 */
113
114/* Support for userspace poll() to watch for changes */
115struct ctl_table_poll {
116	atomic_t event;
117	wait_queue_head_t wait;
118};
119
120static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
121{
122	return (void *)(unsigned long)atomic_read(&poll->event);
123}
124
125#define __CTL_TABLE_POLL_INITIALIZER(name) {				\
126	.event = ATOMIC_INIT(0),					\
127	.wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
128
129#define DEFINE_CTL_TABLE_POLL(name)					\
130	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
131
132/* A sysctl table is an array of struct ctl_table: */
133struct ctl_table {
134	const char *procname;		/* Text ID for /proc/sys, or zero */
135	void *data;
136	int maxlen;
137	umode_t mode;
138	struct ctl_table *child;	/* Deprecated */
139	proc_handler *proc_handler;	/* Callback for text formatting */
140	struct ctl_table_poll *poll;
141	void *extra1;
142	void *extra2;
143} __randomize_layout;
144
145struct ctl_node {
146	struct rb_node node;
147	struct ctl_table_header *header;
148};
149
150/* struct ctl_table_header is used to maintain dynamic lists of
151   struct ctl_table trees. */
152struct ctl_table_header {
153	union {
154		struct {
155			struct ctl_table *ctl_table;
156			int used;
157			int count;
158			int nreg;
159		};
160		struct rcu_head rcu;
161	};
162	struct completion *unregistering;
163	struct ctl_table *ctl_table_arg;
164	struct ctl_table_root *root;
165	struct ctl_table_set *set;
166	struct ctl_dir *parent;
167	struct ctl_node *node;
168	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
169};
170
171struct ctl_dir {
172	/* Header must be at the start of ctl_dir */
173	struct ctl_table_header header;
174	struct rb_root root;
175};
176
177struct ctl_table_set {
178	int (*is_seen)(struct ctl_table_set *);
179	struct ctl_dir dir;
180};
181
182struct ctl_table_root {
183	struct ctl_table_set default_set;
184	struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
185	void (*set_ownership)(struct ctl_table_header *head,
186			      struct ctl_table *table,
187			      kuid_t *uid, kgid_t *gid);
188	int (*permissions)(struct ctl_table_header *head, struct ctl_table *table);
189};
190
191/* struct ctl_path describes where in the hierarchy a table is added */
192struct ctl_path {
193	const char *procname;
194};
195
196#ifdef CONFIG_SYSCTL
197
198#define DECLARE_SYSCTL_BASE(_name, _table)				\
199static struct ctl_table _name##_base_table[] = {			\
200	{								\
201		.procname	= #_name,				\
202		.mode		= 0555,					\
203		.child		= _table,				\
204	},								\
205	{ },								\
206}
207
208extern int __register_sysctl_base(struct ctl_table *base_table);
209
210#define register_sysctl_base(_name) __register_sysctl_base(_name##_base_table)
211
212void proc_sys_poll_notify(struct ctl_table_poll *poll);
213
214extern void setup_sysctl_set(struct ctl_table_set *p,
215	struct ctl_table_root *root,
216	int (*is_seen)(struct ctl_table_set *));
217extern void retire_sysctl_set(struct ctl_table_set *set);
218
219struct ctl_table_header *__register_sysctl_table(
220	struct ctl_table_set *set,
221	const char *path, struct ctl_table *table);
222struct ctl_table_header *__register_sysctl_paths(
223	struct ctl_table_set *set,
224	const struct ctl_path *path, struct ctl_table *table);
225struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table);
226struct ctl_table_header *register_sysctl_table(struct ctl_table * table);
227struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
228						struct ctl_table *table);
229
230void unregister_sysctl_table(struct ctl_table_header * table);
231
232extern int sysctl_init_bases(void);
233extern void __register_sysctl_init(const char *path, struct ctl_table *table,
234				 const char *table_name);
235#define register_sysctl_init(path, table) __register_sysctl_init(path, table, #table)
236extern struct ctl_table_header *register_sysctl_mount_point(const char *path);
237
238void do_sysctl_args(void);
239int do_proc_douintvec(struct ctl_table *table, int write,
240		      void *buffer, size_t *lenp, loff_t *ppos,
241		      int (*conv)(unsigned long *lvalp,
242				  unsigned int *valp,
243				  int write, void *data),
244		      void *data);
245
246extern int pwrsw_enabled;
247extern int unaligned_enabled;
248extern int unaligned_dump_stack;
249extern int no_unaligned_warning;
250
251extern struct ctl_table sysctl_mount_point[];
252
253#else /* CONFIG_SYSCTL */
254
255#define DECLARE_SYSCTL_BASE(_name, _table)
256
257static inline int __register_sysctl_base(struct ctl_table *base_table)
258{
259	return 0;
260}
261
262#define register_sysctl_base(table) __register_sysctl_base(table)
263
264static inline struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
265{
266	return NULL;
267}
268
269static inline struct ctl_table_header *register_sysctl_mount_point(const char *path)
270{
271	return NULL;
272}
273
274static inline struct ctl_table_header *register_sysctl_paths(
275			const struct ctl_path *path, struct ctl_table *table)
276{
277	return NULL;
278}
279
280static inline struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
281{
282	return NULL;
283}
284
285static inline void unregister_sysctl_table(struct ctl_table_header * table)
286{
287}
288
289static inline void setup_sysctl_set(struct ctl_table_set *p,
290	struct ctl_table_root *root,
291	int (*is_seen)(struct ctl_table_set *))
292{
293}
294
295static inline void do_sysctl_args(void)
296{
297}
298#endif /* CONFIG_SYSCTL */
299
300int sysctl_max_threads(struct ctl_table *table, int write, void *buffer,
301		size_t *lenp, loff_t *ppos);
302
303#endif /* _LINUX_SYSCTL_H */
304