xref: /freebsd/sys/kern/kern_sysctl.c (revision d35c4cfad5800b15ab730af5246df0366f2be558)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * Mike Karels at Berkeley Software Design, Inc.
9df8bae1dSRodney W. Grimes  *
10946bb7a2SPoul-Henning Kamp  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
11946bb7a2SPoul-Henning Kamp  * project, to make these variables more userfriendly.
12946bb7a2SPoul-Henning Kamp  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2169a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
38677b542eSDavid E. O'Brien #include <sys/cdefs.h>
39ff66f6a4SRobert Watson #include "opt_capsicum.h"
40088c17b4SConrad Meyer #include "opt_ddb.h"
41a56be37eSJohn Baldwin #include "opt_ktrace.h"
4291c4b68fSPawel Biernacki #include "opt_sysctl.h"
435591b823SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45ccecef29SMatthew D Fleming #include <sys/fail.h>
46e96c1fdcSPeter Wemm #include <sys/systm.h>
474a144410SRobert Watson #include <sys/capsicum.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
49088c17b4SConrad Meyer #include <sys/limits.h>
50df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
51946bb7a2SPoul-Henning Kamp #include <sys/malloc.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53efeaf95aSDavid Greenman #include <sys/proc.h>
54ebd8672cSBjoern A. Zeeb #include <sys/jail.h>
557d0658adSConrad Meyer #include <sys/kdb.h>
56fb99ab88SMatthew Dillon #include <sys/lock.h>
57fb99ab88SMatthew Dillon #include <sys/mutex.h>
587665e341SMateusz Guzik #include <sys/rmlock.h>
594e657159SMatthew D Fleming #include <sys/sbuf.h>
6029a2c0cdSAlfred Perlstein #include <sys/sx.h>
6145ec3b38SPoul-Henning Kamp #include <sys/sysproto.h>
62af80b2c9SKonstantin Belousov #include <sys/uio.h>
63a56be37eSJohn Baldwin #ifdef KTRACE
64a56be37eSJohn Baldwin #include <sys/ktrace.h>
65a56be37eSJohn Baldwin #endif
66aed55708SRobert Watson 
67088c17b4SConrad Meyer #ifdef DDB
68088c17b4SConrad Meyer #include <ddb/ddb.h>
69088c17b4SConrad Meyer #include <ddb/db_lex.h>
70088c17b4SConrad Meyer #endif
71088c17b4SConrad Meyer 
72eddfbb76SRobert Watson #include <net/vnet.h>
73eddfbb76SRobert Watson 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson 
764cb03b1bSBruce Evans #include <vm/vm.h>
77efeaf95aSDavid Greenman #include <vm/vm_extern.h>
784cb03b1bSBruce Evans 
79a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
80bd3cdc31SAndrzej Bialecki static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
819e74cba3SDon Lewis static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
8255166637SPoul-Henning Kamp 
83d3f96f66SAlan Somers RB_GENERATE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid);
84d3f96f66SAlan Somers 
854b2af45fSPoul-Henning Kamp /*
86875b66a0SJohn Baldwin  * The sysctllock protects the MIB tree.  It also protects sysctl
87875b66a0SJohn Baldwin  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
88875b66a0SJohn Baldwin  * sysctl_unregister_oid() routines require the sysctllock to already
897665e341SMateusz Guzik  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
90875b66a0SJohn Baldwin  * provided for the few places in the kernel which need to use that
91875b66a0SJohn Baldwin  * API rather than using the dynamic API.  Use of the dynamic API is
92875b66a0SJohn Baldwin  * strongly encouraged for most code.
93875b66a0SJohn Baldwin  *
943e829b18SJohn Baldwin  * The sysctlmemlock is used to limit the amount of user memory wired for
953e829b18SJohn Baldwin  * sysctl requests.  This is implemented by serializing any userland
963e829b18SJohn Baldwin  * sysctl requests larger than a single page via an exclusive lock.
97210176adSPawel Biernacki  *
98210176adSPawel Biernacki  * The sysctlstringlock is used to protect concurrent access to writable
99210176adSPawel Biernacki  * string nodes in sysctl_handle_string().
1004b2af45fSPoul-Henning Kamp  */
1017665e341SMateusz Guzik static struct rmlock sysctllock;
102956713cbSMateusz Guzik static struct sx __exclusive_cache_line sysctlmemlock;
103210176adSPawel Biernacki static struct sx sysctlstringlock;
10429a2c0cdSAlfred Perlstein 
1057665e341SMateusz Guzik #define	SYSCTL_WLOCK()		rm_wlock(&sysctllock)
1067665e341SMateusz Guzik #define	SYSCTL_WUNLOCK()	rm_wunlock(&sysctllock)
1077665e341SMateusz Guzik #define	SYSCTL_RLOCK(tracker)	rm_rlock(&sysctllock, (tracker))
1087665e341SMateusz Guzik #define	SYSCTL_RUNLOCK(tracker)	rm_runlock(&sysctllock, (tracker))
1097665e341SMateusz Guzik #define	SYSCTL_WLOCKED()	rm_wowned(&sysctllock)
1107665e341SMateusz Guzik #define	SYSCTL_ASSERT_LOCKED()	rm_assert(&sysctllock, RA_LOCKED)
1117665e341SMateusz Guzik #define	SYSCTL_ASSERT_WLOCKED()	rm_assert(&sysctllock, RA_WLOCKED)
1127665e341SMateusz Guzik #define	SYSCTL_ASSERT_RLOCKED()	rm_assert(&sysctllock, RA_RLOCKED)
1137665e341SMateusz Guzik #define	SYSCTL_INIT()		rm_init_flags(&sysctllock, "sysctl lock", \
1147665e341SMateusz Guzik 				    RM_SLEEPABLE)
115ccecef29SMatthew D Fleming #define	SYSCTL_SLEEP(ch, wmesg, timo)					\
1167665e341SMateusz Guzik 				rm_sleep(ch, &sysctllock, 0, wmesg, timo)
1174b2af45fSPoul-Henning Kamp 
11882d9ae4eSPoul-Henning Kamp static int sysctl_root(SYSCTL_HANDLER_ARGS);
1194b2af45fSPoul-Henning Kamp 
120af3b2549SHans Petter Selasky /* Root list */
121d3f96f66SAlan Somers struct sysctl_oid_list sysctl__children = RB_INITIALIZER(&sysctl__children);
122787d58f2SPoul-Henning Kamp 
1236c950655SAlan Somers static char*	sysctl_escape_name(const char*);
124875b66a0SJohn Baldwin static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
125875b66a0SJohn Baldwin 		    int recurse);
126af3b2549SHans Petter Selasky static int	sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
127af3b2549SHans Petter Selasky static int	sysctl_new_kernel(struct sysctl_req *, void *, size_t);
128f80babf9SAlexander Motin static int	name2oid(const char *, int *, int *, struct sysctl_oid **);
129875b66a0SJohn Baldwin 
130bd3cdc31SAndrzej Bialecki static struct sysctl_oid *
sysctl_find_oidname(const char * name,struct sysctl_oid_list * list)131bd3cdc31SAndrzej Bialecki sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
132bd3cdc31SAndrzej Bialecki {
133bd3cdc31SAndrzej Bialecki 	struct sysctl_oid *oidp;
134bd3cdc31SAndrzej Bialecki 
135efe0abddSMateusz Guzik 	SYSCTL_ASSERT_LOCKED();
136c075ea46SHans Petter Selasky 	SYSCTL_FOREACH(oidp, list) {
137bd3cdc31SAndrzej Bialecki 		if (strcmp(oidp->oid_name, name) == 0) {
138bd3cdc31SAndrzej Bialecki 			return (oidp);
139bd3cdc31SAndrzej Bialecki 		}
140bd3cdc31SAndrzej Bialecki 	}
141bd3cdc31SAndrzej Bialecki 	return (NULL);
142bd3cdc31SAndrzej Bialecki }
143bd3cdc31SAndrzej Bialecki 
144f80babf9SAlexander Motin static struct sysctl_oid *
sysctl_find_oidnamelen(const char * name,size_t len,struct sysctl_oid_list * list)145f80babf9SAlexander Motin sysctl_find_oidnamelen(const char *name, size_t len,
146f80babf9SAlexander Motin     struct sysctl_oid_list *list)
147f80babf9SAlexander Motin {
148f80babf9SAlexander Motin 	struct sysctl_oid *oidp;
149f80babf9SAlexander Motin 
150f80babf9SAlexander Motin 	SYSCTL_ASSERT_LOCKED();
151f80babf9SAlexander Motin 	SYSCTL_FOREACH(oidp, list) {
152f80babf9SAlexander Motin 		if (strncmp(oidp->oid_name, name, len) == 0 &&
153f80babf9SAlexander Motin 		    oidp->oid_name[len] == '\0')
154f80babf9SAlexander Motin 			return (oidp);
155f80babf9SAlexander Motin 	}
156f80babf9SAlexander Motin 	return (NULL);
157f80babf9SAlexander Motin }
158f80babf9SAlexander Motin 
159946bb7a2SPoul-Henning Kamp /*
160946bb7a2SPoul-Henning Kamp  * Initialization of the MIB tree.
161946bb7a2SPoul-Henning Kamp  *
162ce02431fSDoug Rabson  * Order by number in each list.
163946bb7a2SPoul-Henning Kamp  */
164875b66a0SJohn Baldwin void
sysctl_wlock(void)1657665e341SMateusz Guzik sysctl_wlock(void)
166875b66a0SJohn Baldwin {
167875b66a0SJohn Baldwin 
1687665e341SMateusz Guzik 	SYSCTL_WLOCK();
169875b66a0SJohn Baldwin }
170875b66a0SJohn Baldwin 
171875b66a0SJohn Baldwin void
sysctl_wunlock(void)1727665e341SMateusz Guzik sysctl_wunlock(void)
173875b66a0SJohn Baldwin {
174875b66a0SJohn Baldwin 
1757665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
176875b66a0SJohn Baldwin }
1774b2af45fSPoul-Henning Kamp 
178af3b2549SHans Petter Selasky static int
sysctl_root_handler_locked(struct sysctl_oid * oid,void * arg1,intmax_t arg2,struct sysctl_req * req,struct rm_priotracker * tracker)179e072f955SConrad Meyer sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
1807665e341SMateusz Guzik     struct sysctl_req *req, struct rm_priotracker *tracker)
181af3b2549SHans Petter Selasky {
182af3b2549SHans Petter Selasky 	int error;
183af3b2549SHans Petter Selasky 
18484267cacSMateusz Guzik 	if (oid->oid_kind & CTLFLAG_DYN)
185efe0abddSMateusz Guzik 		atomic_add_int(&oid->oid_running, 1);
1867665e341SMateusz Guzik 
1877665e341SMateusz Guzik 	if (tracker != NULL)
1887665e341SMateusz Guzik 		SYSCTL_RUNLOCK(tracker);
1897665e341SMateusz Guzik 	else
1907665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
191af3b2549SHans Petter Selasky 
192210176adSPawel Biernacki 	/*
193210176adSPawel Biernacki 	 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
194210176adSPawel Biernacki 	 * untill we're ready to remove all traces of Giant from sysctl(9).
195210176adSPawel Biernacki 	 */
196210176adSPawel Biernacki 	if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
197210176adSPawel Biernacki 	    (!(oid->oid_kind & CTLFLAG_MPSAFE)))
198af3b2549SHans Petter Selasky 		mtx_lock(&Giant);
199af3b2549SHans Petter Selasky 	error = oid->oid_handler(oid, arg1, arg2, req);
200210176adSPawel Biernacki 	if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
201210176adSPawel Biernacki 	    (!(oid->oid_kind & CTLFLAG_MPSAFE)))
202af3b2549SHans Petter Selasky 		mtx_unlock(&Giant);
203af3b2549SHans Petter Selasky 
20487b87bb8SMark Johnston 	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
20587b87bb8SMark Johnston 
2067665e341SMateusz Guzik 	if (tracker != NULL)
2077665e341SMateusz Guzik 		SYSCTL_RLOCK(tracker);
2087665e341SMateusz Guzik 	else
2097665e341SMateusz Guzik 		SYSCTL_WLOCK();
2107665e341SMateusz Guzik 
21184267cacSMateusz Guzik 	if (oid->oid_kind & CTLFLAG_DYN) {
212efe0abddSMateusz Guzik 		if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
213efe0abddSMateusz Guzik 		    (oid->oid_kind & CTLFLAG_DYING) != 0)
214af3b2549SHans Petter Selasky 			wakeup(&oid->oid_running);
21584267cacSMateusz Guzik 	}
216af3b2549SHans Petter Selasky 
217af3b2549SHans Petter Selasky 	return (error);
218af3b2549SHans Petter Selasky }
219af3b2549SHans Petter Selasky 
220af3b2549SHans Petter Selasky static void
sysctl_load_tunable_by_oid_locked(struct sysctl_oid * oidp)221af3b2549SHans Petter Selasky sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
222af3b2549SHans Petter Selasky {
223af3b2549SHans Petter Selasky 	struct sysctl_req req;
224af3b2549SHans Petter Selasky 	struct sysctl_oid *curr;
225604bf9d3SHans Petter Selasky 	char *penv = NULL;
22664282a12SHans Petter Selasky 	char path[96];
227af3b2549SHans Petter Selasky 	ssize_t rem = sizeof(path);
228af3b2549SHans Petter Selasky 	ssize_t len;
229ce70c572SHans Petter Selasky 	uint8_t data[512] __aligned(sizeof(uint64_t));
230ce70c572SHans Petter Selasky 	int size;
231af3b2549SHans Petter Selasky 	int error;
232af3b2549SHans Petter Selasky 
233af3b2549SHans Petter Selasky 	path[--rem] = 0;
234af3b2549SHans Petter Selasky 
235af3b2549SHans Petter Selasky 	for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
236af3b2549SHans Petter Selasky 		len = strlen(curr->oid_name);
237af3b2549SHans Petter Selasky 		rem -= len;
238af3b2549SHans Petter Selasky 		if (curr != oidp)
239af3b2549SHans Petter Selasky 			rem -= 1;
240af3b2549SHans Petter Selasky 		if (rem < 0) {
241af3b2549SHans Petter Selasky 			printf("OID path exceeds %d bytes\n", (int)sizeof(path));
242af3b2549SHans Petter Selasky 			return;
243af3b2549SHans Petter Selasky 		}
244af3b2549SHans Petter Selasky 		memcpy(path + rem, curr->oid_name, len);
245af3b2549SHans Petter Selasky 		if (curr != oidp)
246af3b2549SHans Petter Selasky 			path[rem + len] = '.';
247af3b2549SHans Petter Selasky 	}
248af3b2549SHans Petter Selasky 
249af3b2549SHans Petter Selasky 	memset(&req, 0, sizeof(req));
250af3b2549SHans Petter Selasky 
251af3b2549SHans Petter Selasky 	req.td = curthread;
252af3b2549SHans Petter Selasky 	req.oldfunc = sysctl_old_kernel;
253af3b2549SHans Petter Selasky 	req.newfunc = sysctl_new_kernel;
254af3b2549SHans Petter Selasky 	req.lock = REQ_UNWIRED;
255af3b2549SHans Petter Selasky 
256af3b2549SHans Petter Selasky 	switch (oidp->oid_kind & CTLTYPE) {
257af3b2549SHans Petter Selasky 	case CTLTYPE_INT:
258ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
259ce70c572SHans Petter Selasky 		    sizeof(int), GETENV_SIGNED) == 0)
260604bf9d3SHans Petter Selasky 			return;
261ce70c572SHans Petter Selasky 		req.newlen = size;
262ce70c572SHans Petter Selasky 		req.newptr = data;
263af3b2549SHans Petter Selasky 		break;
264af3b2549SHans Petter Selasky 	case CTLTYPE_UINT:
265ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
266ce70c572SHans Petter Selasky 		    sizeof(int), GETENV_UNSIGNED) == 0)
267604bf9d3SHans Petter Selasky 			return;
268ce70c572SHans Petter Selasky 		req.newlen = size;
269ce70c572SHans Petter Selasky 		req.newptr = data;
270af3b2549SHans Petter Selasky 		break;
271af3b2549SHans Petter Selasky 	case CTLTYPE_LONG:
272ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
273ce70c572SHans Petter Selasky 		    sizeof(long), GETENV_SIGNED) == 0)
274604bf9d3SHans Petter Selasky 			return;
275ce70c572SHans Petter Selasky 		req.newlen = size;
276ce70c572SHans Petter Selasky 		req.newptr = data;
277af3b2549SHans Petter Selasky 		break;
278af3b2549SHans Petter Selasky 	case CTLTYPE_ULONG:
279ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
280ce70c572SHans Petter Selasky 		    sizeof(long), GETENV_UNSIGNED) == 0)
281604bf9d3SHans Petter Selasky 			return;
282ce70c572SHans Petter Selasky 		req.newlen = size;
283ce70c572SHans Petter Selasky 		req.newptr = data;
284af3b2549SHans Petter Selasky 		break;
285e072f955SConrad Meyer 	case CTLTYPE_S8:
286ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
287ce70c572SHans Petter Selasky 		    sizeof(int8_t), GETENV_SIGNED) == 0)
288e072f955SConrad Meyer 			return;
289ce70c572SHans Petter Selasky 		req.newlen = size;
290ce70c572SHans Petter Selasky 		req.newptr = data;
291e072f955SConrad Meyer 		break;
292e072f955SConrad Meyer 	case CTLTYPE_S16:
293ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
294ce70c572SHans Petter Selasky 		    sizeof(int16_t), GETENV_SIGNED) == 0)
295e072f955SConrad Meyer 			return;
296ce70c572SHans Petter Selasky 		req.newlen = size;
297ce70c572SHans Petter Selasky 		req.newptr = data;
298e072f955SConrad Meyer 		break;
299e072f955SConrad Meyer 	case CTLTYPE_S32:
300ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
301ce70c572SHans Petter Selasky 		    sizeof(int32_t), GETENV_SIGNED) == 0)
302e072f955SConrad Meyer 			return;
303ce70c572SHans Petter Selasky 		req.newlen = size;
304ce70c572SHans Petter Selasky 		req.newptr = data;
305e072f955SConrad Meyer 		break;
306af3b2549SHans Petter Selasky 	case CTLTYPE_S64:
307ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
308ce70c572SHans Petter Selasky 		    sizeof(int64_t), GETENV_SIGNED) == 0)
309604bf9d3SHans Petter Selasky 			return;
310ce70c572SHans Petter Selasky 		req.newlen = size;
311ce70c572SHans Petter Selasky 		req.newptr = data;
312af3b2549SHans Petter Selasky 		break;
313e072f955SConrad Meyer 	case CTLTYPE_U8:
314ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
315ce70c572SHans Petter Selasky 		    sizeof(uint8_t), GETENV_UNSIGNED) == 0)
316e072f955SConrad Meyer 			return;
317ce70c572SHans Petter Selasky 		req.newlen = size;
318ce70c572SHans Petter Selasky 		req.newptr = data;
319e072f955SConrad Meyer 		break;
320e072f955SConrad Meyer 	case CTLTYPE_U16:
321ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
322ce70c572SHans Petter Selasky 		    sizeof(uint16_t), GETENV_UNSIGNED) == 0)
323e072f955SConrad Meyer 			return;
324ce70c572SHans Petter Selasky 		req.newlen = size;
325ce70c572SHans Petter Selasky 		req.newptr = data;
326e072f955SConrad Meyer 		break;
327e072f955SConrad Meyer 	case CTLTYPE_U32:
328ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
329ce70c572SHans Petter Selasky 		    sizeof(uint32_t), GETENV_UNSIGNED) == 0)
330e072f955SConrad Meyer 			return;
331ce70c572SHans Petter Selasky 		req.newlen = size;
332ce70c572SHans Petter Selasky 		req.newptr = data;
333e072f955SConrad Meyer 		break;
334af3b2549SHans Petter Selasky 	case CTLTYPE_U64:
335ce70c572SHans Petter Selasky 		if (getenv_array(path + rem, data, sizeof(data), &size,
336ce70c572SHans Petter Selasky 		    sizeof(uint64_t), GETENV_UNSIGNED) == 0)
337604bf9d3SHans Petter Selasky 			return;
338ce70c572SHans Petter Selasky 		req.newlen = size;
339ce70c572SHans Petter Selasky 		req.newptr = data;
340af3b2549SHans Petter Selasky 		break;
341af3b2549SHans Petter Selasky 	case CTLTYPE_STRING:
3422be111bfSDavide Italiano 		penv = kern_getenv(path + rem);
343604bf9d3SHans Petter Selasky 		if (penv == NULL)
344604bf9d3SHans Petter Selasky 			return;
345af3b2549SHans Petter Selasky 		req.newlen = strlen(penv);
346af3b2549SHans Petter Selasky 		req.newptr = penv;
347af3b2549SHans Petter Selasky 		break;
348af3b2549SHans Petter Selasky 	default:
349af3b2549SHans Petter Selasky 		return;
350af3b2549SHans Petter Selasky 	}
351af3b2549SHans Petter Selasky 	error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
3527665e341SMateusz Guzik 	    oidp->oid_arg2, &req, NULL);
353b0d69dfaSDag-Erling Smørgrav 	if (error != 0)
354ab91c9a7SHans Petter Selasky 		printf("Setting sysctl %s failed: %d\n", path + rem, error);
355b0d69dfaSDag-Erling Smørgrav 	if (penv != NULL)
356af3b2549SHans Petter Selasky 		freeenv(penv);
357af3b2549SHans Petter Selasky }
358af3b2549SHans Petter Selasky 
3594ae2ade1SConrad Meyer /*
3604ae2ade1SConrad Meyer  * Locate the path to a given oid.  Returns the length of the resulting path,
3614ae2ade1SConrad Meyer  * or -1 if the oid was not found.  nodes must have room for CTL_MAXNAME
3625294bfa7SDoug Moore  * elements.
3634ae2ade1SConrad Meyer  */
3644ae2ade1SConrad Meyer static int
sysctl_search_oid(struct sysctl_oid ** nodes,struct sysctl_oid * needle)3654ae2ade1SConrad Meyer sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
3664ae2ade1SConrad Meyer {
3674ae2ade1SConrad Meyer 	int indx;
3684ae2ade1SConrad Meyer 
3694ae2ade1SConrad Meyer 	SYSCTL_ASSERT_LOCKED();
3704ae2ade1SConrad Meyer 	indx = 0;
371e96ae5cbSDoug Moore 	/*
372e96ae5cbSDoug Moore 	 * Do a depth-first search of the oid tree, looking for 'needle'. Start
373e96ae5cbSDoug Moore 	 * with the first child of the root.
374e96ae5cbSDoug Moore 	 */
375e96ae5cbSDoug Moore 	nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children);
376e96ae5cbSDoug Moore 	for (;;) {
3774ae2ade1SConrad Meyer 		if (nodes[indx] == needle)
3784ae2ade1SConrad Meyer 			return (indx + 1);
3794ae2ade1SConrad Meyer 
3804ae2ade1SConrad Meyer 		if (nodes[indx] == NULL) {
381e96ae5cbSDoug Moore 			/* Node has no more siblings, so back up to parent. */
382e96ae5cbSDoug Moore 			if (indx-- == 0) {
383e96ae5cbSDoug Moore 				/* Retreat to root, so give up. */
384e96ae5cbSDoug Moore 				break;
385e96ae5cbSDoug Moore 			}
386e96ae5cbSDoug Moore 		} else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
387e96ae5cbSDoug Moore 			/* Node has children. */
388e96ae5cbSDoug Moore 			if (++indx == CTL_MAXNAME) {
389e96ae5cbSDoug Moore 				/* Max search depth reached, so give up. */
390e96ae5cbSDoug Moore 				break;
391e96ae5cbSDoug Moore 			}
392e96ae5cbSDoug Moore 			/* Start with the first child. */
393e96ae5cbSDoug Moore 			nodes[indx] = RB_MIN(sysctl_oid_list,
394e96ae5cbSDoug Moore 			    &nodes[indx - 1]->oid_children);
3954ae2ade1SConrad Meyer 			continue;
3964ae2ade1SConrad Meyer 		}
397e96ae5cbSDoug Moore 		/* Consider next sibling. */
398e96ae5cbSDoug Moore 		nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]);
3994ae2ade1SConrad Meyer 	}
4004ae2ade1SConrad Meyer 	return (-1);
4014ae2ade1SConrad Meyer }
4024ae2ade1SConrad Meyer 
4034ae2ade1SConrad Meyer static void
sysctl_warn_reuse(const char * func,struct sysctl_oid * leaf)4044ae2ade1SConrad Meyer sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
4054ae2ade1SConrad Meyer {
4064ae2ade1SConrad Meyer 	struct sysctl_oid *nodes[CTL_MAXNAME];
4074ae2ade1SConrad Meyer 	char buf[128];
4084ae2ade1SConrad Meyer 	struct sbuf sb;
4094ae2ade1SConrad Meyer 	int rc, i;
4104ae2ade1SConrad Meyer 
4114ae2ade1SConrad Meyer 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
4124ae2ade1SConrad Meyer 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
4134ae2ade1SConrad Meyer 
414faa845aaSZhenlei Huang 	sbuf_printf(&sb, "%s: can't re-use a leaf (", func);
4154ae2ade1SConrad Meyer 
4164ae2ade1SConrad Meyer 	rc = sysctl_search_oid(nodes, leaf);
4174ae2ade1SConrad Meyer 	if (rc > 0) {
4184ae2ade1SConrad Meyer 		for (i = 0; i < rc; i++)
4194ae2ade1SConrad Meyer 			sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
4204ae2ade1SConrad Meyer 			    i != (rc - 1), ".");
4214ae2ade1SConrad Meyer 	} else {
4220a713948SAlexander Motin 		sbuf_cat(&sb, leaf->oid_name);
4234ae2ade1SConrad Meyer 	}
4240a713948SAlexander Motin 	sbuf_cat(&sb, ")!\n");
4254ae2ade1SConrad Meyer 
4264ae2ade1SConrad Meyer 	(void)sbuf_finish(&sb);
4274ae2ade1SConrad Meyer }
4284ae2ade1SConrad Meyer 
4294ae2ade1SConrad Meyer #ifdef SYSCTL_DEBUG
4304ae2ade1SConrad Meyer static int
sysctl_reuse_test(SYSCTL_HANDLER_ARGS)4314ae2ade1SConrad Meyer sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
4324ae2ade1SConrad Meyer {
4334ae2ade1SConrad Meyer 	struct rm_priotracker tracker;
4344ae2ade1SConrad Meyer 
4354ae2ade1SConrad Meyer 	SYSCTL_RLOCK(&tracker);
4364ae2ade1SConrad Meyer 	sysctl_warn_reuse(__func__, oidp);
4374ae2ade1SConrad Meyer 	SYSCTL_RUNLOCK(&tracker);
4384ae2ade1SConrad Meyer 	return (0);
4394ae2ade1SConrad Meyer }
44091c4b68fSPawel Biernacki SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
44191c4b68fSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
44291c4b68fSPawel Biernacki     "");
4434ae2ade1SConrad Meyer #endif
4444ae2ade1SConrad Meyer 
445550374efSAndriy Gapon void
sysctl_register_oid(struct sysctl_oid * oidp)446550374efSAndriy Gapon sysctl_register_oid(struct sysctl_oid *oidp)
447787d58f2SPoul-Henning Kamp {
448ce02431fSDoug Rabson 	struct sysctl_oid_list *parent = oidp->oid_parent;
449d3f96f66SAlan Somers 	struct sysctl_oid *p, key;
45038668c60SHans Petter Selasky 	int oid_number;
45138668c60SHans Petter Selasky 	int timeout = 2;
4523c8e79ddSBruce Evans 
453ce02431fSDoug Rabson 	/*
454bd3cdc31SAndrzej Bialecki 	 * First check if another oid with the same name already
455bd3cdc31SAndrzej Bialecki 	 * exists in the parent's list.
456bd3cdc31SAndrzej Bialecki 	 */
4577665e341SMateusz Guzik 	SYSCTL_ASSERT_WLOCKED();
458bd3cdc31SAndrzej Bialecki 	p = sysctl_find_oidname(oidp->oid_name, parent);
459bd3cdc31SAndrzej Bialecki 	if (p != NULL) {
460bd3cdc31SAndrzej Bialecki 		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
461bd3cdc31SAndrzej Bialecki 			p->oid_refcnt++;
462bd3cdc31SAndrzej Bialecki 			return;
463bd3cdc31SAndrzej Bialecki 		} else {
4644ae2ade1SConrad Meyer 			sysctl_warn_reuse(__func__, p);
465bd3cdc31SAndrzej Bialecki 			return;
466bd3cdc31SAndrzej Bialecki 		}
467bd3cdc31SAndrzej Bialecki 	}
46838668c60SHans Petter Selasky 	/* get current OID number */
46938668c60SHans Petter Selasky 	oid_number = oidp->oid_number;
47038668c60SHans Petter Selasky 
47138668c60SHans Petter Selasky #if (OID_AUTO >= 0)
47238668c60SHans Petter Selasky #error "OID_AUTO is expected to be a negative value"
47338668c60SHans Petter Selasky #endif
474bd3cdc31SAndrzej Bialecki 	/*
47538668c60SHans Petter Selasky 	 * Any negative OID number qualifies as OID_AUTO. Valid OID
47638668c60SHans Petter Selasky 	 * numbers should always be positive.
47738668c60SHans Petter Selasky 	 *
4787ca4d05fSPeter Pentchev 	 * NOTE: DO NOT change the starting value here, change it in
4797ca4d05fSPeter Pentchev 	 * <sys/sysctl.h>, and make sure it is at least 256 to
480e3043798SPedro F. Giffuni 	 * accommodate e.g. net.inet.raw as a static sysctl node.
481ce02431fSDoug Rabson 	 */
48238668c60SHans Petter Selasky 	if (oid_number < 0) {
48338668c60SHans Petter Selasky 		static int newoid;
484787d58f2SPoul-Henning Kamp 
485ce02431fSDoug Rabson 		/*
48638668c60SHans Petter Selasky 		 * By decrementing the next OID number we spend less
48738668c60SHans Petter Selasky 		 * time inserting the OIDs into a sorted list.
488ce02431fSDoug Rabson 		 */
48938668c60SHans Petter Selasky 		if (--newoid < CTL_AUTO_START)
49038668c60SHans Petter Selasky 			newoid = 0x7fffffff;
49138668c60SHans Petter Selasky 
49238668c60SHans Petter Selasky 		oid_number = newoid;
49338668c60SHans Petter Selasky 	}
49438668c60SHans Petter Selasky 
49538668c60SHans Petter Selasky 	/*
49638668c60SHans Petter Selasky 	 * Insert the OID into the parent's list sorted by OID number.
49738668c60SHans Petter Selasky 	 */
498d3f96f66SAlan Somers 	key.oid_number = oid_number;
499ed518345SDoug Moore 	p = RB_NFIND(sysctl_oid_list, parent, &key);
500ed518345SDoug Moore 	while (p != NULL && oid_number == p->oid_number) {
50138668c60SHans Petter Selasky 		/* get the next valid OID number */
50238668c60SHans Petter Selasky 		if (oid_number < CTL_AUTO_START ||
50338668c60SHans Petter Selasky 		    oid_number == 0x7fffffff) {
50438668c60SHans Petter Selasky 			/* wraparound - restart */
50538668c60SHans Petter Selasky 			oid_number = CTL_AUTO_START;
50638668c60SHans Petter Selasky 			/* don't loop forever */
50738668c60SHans Petter Selasky 			if (!timeout--)
50838668c60SHans Petter Selasky 				panic("sysctl: Out of OID numbers\n");
509ed518345SDoug Moore 			key.oid_number = oid_number;
510ed518345SDoug Moore 			p = RB_NFIND(sysctl_oid_list, parent, &key);
511ed518345SDoug Moore 			continue;
51238668c60SHans Petter Selasky 		}
513ed518345SDoug Moore 		p = RB_NEXT(sysctl_oid_list, NULL, p);
514ed518345SDoug Moore 		oid_number++;
515ce02431fSDoug Rabson 	}
51638668c60SHans Petter Selasky 	/* check for non-auto OID number collision */
51738668c60SHans Petter Selasky 	if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
51838668c60SHans Petter Selasky 	    oid_number >= CTL_AUTO_START) {
519*d35c4cfaSMark Johnston 		panic("sysctl: OID number(%d) is already in use for '%s'\n",
52038668c60SHans Petter Selasky 		    oidp->oid_number, oidp->oid_name);
52138668c60SHans Petter Selasky 	}
52238668c60SHans Petter Selasky 	/* update the OID number, if any */
52338668c60SHans Petter Selasky 	oidp->oid_number = oid_number;
524d3f96f66SAlan Somers 	RB_INSERT(sysctl_oid_list, parent, oidp);
525af3b2549SHans Petter Selasky 
526af3b2549SHans Petter Selasky 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
527af3b2549SHans Petter Selasky 	    (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
528af3b2549SHans Petter Selasky 	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
529cf7974fdSZhenlei Huang #ifdef VIMAGE
530cf7974fdSZhenlei Huang 		/*
531cf7974fdSZhenlei Huang 		 * Can fetch value multiple times for VNET loader tunables.
532cf7974fdSZhenlei Huang 		 * Only fetch once for non-VNET loader tunables.
533cf7974fdSZhenlei Huang 		 */
534cf7974fdSZhenlei Huang 		if ((oidp->oid_kind & CTLFLAG_VNET) == 0)
535cf7974fdSZhenlei Huang #endif
536502702c6SHans Petter Selasky 			oidp->oid_kind |= CTLFLAG_NOFETCH;
537502702c6SHans Petter Selasky 		/* try to fetch value from kernel environment */
538af3b2549SHans Petter Selasky 		sysctl_load_tunable_by_oid_locked(oidp);
539af3b2549SHans Petter Selasky 	}
540ce02431fSDoug Rabson }
541ce02431fSDoug Rabson 
542107e7dc5SPeter Pentchev void
sysctl_register_disabled_oid(struct sysctl_oid * oidp)543693593b6SAndriy Gapon sysctl_register_disabled_oid(struct sysctl_oid *oidp)
544693593b6SAndriy Gapon {
545693593b6SAndriy Gapon 
546693593b6SAndriy Gapon 	/*
547693593b6SAndriy Gapon 	 * Mark the leaf as dormant if it's not to be immediately enabled.
548693593b6SAndriy Gapon 	 * We do not disable nodes as they can be shared between modules
549693593b6SAndriy Gapon 	 * and it is always safe to access a node.
550693593b6SAndriy Gapon 	 */
551693593b6SAndriy Gapon 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
552693593b6SAndriy Gapon 	    ("internal flag is set in oid_kind"));
553693593b6SAndriy Gapon 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
554693593b6SAndriy Gapon 		oidp->oid_kind |= CTLFLAG_DORMANT;
555693593b6SAndriy Gapon 	sysctl_register_oid(oidp);
556693593b6SAndriy Gapon }
557693593b6SAndriy Gapon 
558693593b6SAndriy Gapon void
sysctl_enable_oid(struct sysctl_oid * oidp)559693593b6SAndriy Gapon sysctl_enable_oid(struct sysctl_oid *oidp)
560693593b6SAndriy Gapon {
561693593b6SAndriy Gapon 
562693593b6SAndriy Gapon 	SYSCTL_ASSERT_WLOCKED();
563693593b6SAndriy Gapon 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
564693593b6SAndriy Gapon 		KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
565693593b6SAndriy Gapon 		    ("sysctl node is marked as dormant"));
566693593b6SAndriy Gapon 		return;
567693593b6SAndriy Gapon 	}
568693593b6SAndriy Gapon 	KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
569693593b6SAndriy Gapon 	    ("enabling already enabled sysctl oid"));
570693593b6SAndriy Gapon 	oidp->oid_kind &= ~CTLFLAG_DORMANT;
571693593b6SAndriy Gapon }
572693593b6SAndriy Gapon 
573693593b6SAndriy Gapon void
sysctl_unregister_oid(struct sysctl_oid * oidp)574107e7dc5SPeter Pentchev sysctl_unregister_oid(struct sysctl_oid *oidp)
575787d58f2SPoul-Henning Kamp {
576193f2edbSMaxime Henrion 	int error;
577193f2edbSMaxime Henrion 
5787665e341SMateusz Guzik 	SYSCTL_ASSERT_WLOCKED();
579193f2edbSMaxime Henrion 	if (oidp->oid_number == OID_AUTO) {
580193f2edbSMaxime Henrion 		error = EINVAL;
581193f2edbSMaxime Henrion 	} else {
58290acd1d1SHans Petter Selasky 		error = ENOENT;
583d3f96f66SAlan Somers 		if (RB_REMOVE(sysctl_oid_list, oidp->oid_parent, oidp))
584193f2edbSMaxime Henrion 			error = 0;
585193f2edbSMaxime Henrion 	}
586193f2edbSMaxime Henrion 
587193f2edbSMaxime Henrion 	/*
588193f2edbSMaxime Henrion 	 * This can happen when a module fails to register and is
589193f2edbSMaxime Henrion 	 * being unloaded afterwards.  It should not be a panic()
590193f2edbSMaxime Henrion 	 * for normal use.
591193f2edbSMaxime Henrion 	 */
5922205f61aSHans Petter Selasky 	if (error) {
5932205f61aSHans Petter Selasky 		printf("%s: failed(%d) to unregister sysctl(%s)\n",
5942205f61aSHans Petter Selasky 		    __func__, error, oidp->oid_name);
5952205f61aSHans Petter Selasky 	}
59661220614SPoul-Henning Kamp }
597946bb7a2SPoul-Henning Kamp 
598bd3cdc31SAndrzej Bialecki /* Initialize a new context to keep track of dynamically added sysctls. */
599bd3cdc31SAndrzej Bialecki int
sysctl_ctx_init(struct sysctl_ctx_list * c)600bd3cdc31SAndrzej Bialecki sysctl_ctx_init(struct sysctl_ctx_list *c)
601bd3cdc31SAndrzej Bialecki {
602bd3cdc31SAndrzej Bialecki 
603bd3cdc31SAndrzej Bialecki 	if (c == NULL) {
604bd3cdc31SAndrzej Bialecki 		return (EINVAL);
605bd3cdc31SAndrzej Bialecki 	}
606875b66a0SJohn Baldwin 
607875b66a0SJohn Baldwin 	/*
608875b66a0SJohn Baldwin 	 * No locking here, the caller is responsible for not adding
609875b66a0SJohn Baldwin 	 * new nodes to a context until after this function has
610875b66a0SJohn Baldwin 	 * returned.
611875b66a0SJohn Baldwin 	 */
612bd3cdc31SAndrzej Bialecki 	TAILQ_INIT(c);
613bd3cdc31SAndrzej Bialecki 	return (0);
614bd3cdc31SAndrzej Bialecki }
615bd3cdc31SAndrzej Bialecki 
616bd3cdc31SAndrzej Bialecki /* Free the context, and destroy all dynamic oids registered in this context */
617bd3cdc31SAndrzej Bialecki int
sysctl_ctx_free(struct sysctl_ctx_list * clist)618bd3cdc31SAndrzej Bialecki sysctl_ctx_free(struct sysctl_ctx_list *clist)
619bd3cdc31SAndrzej Bialecki {
620bd3cdc31SAndrzej Bialecki 	struct sysctl_ctx_entry *e, *e1;
621bd3cdc31SAndrzej Bialecki 	int error;
622bd3cdc31SAndrzej Bialecki 
623bd3cdc31SAndrzej Bialecki 	error = 0;
624bd3cdc31SAndrzej Bialecki 	/*
625bd3cdc31SAndrzej Bialecki 	 * First perform a "dry run" to check if it's ok to remove oids.
626bd3cdc31SAndrzej Bialecki 	 * XXX FIXME
627bd3cdc31SAndrzej Bialecki 	 * XXX This algorithm is a hack. But I don't know any
628bd3cdc31SAndrzej Bialecki 	 * XXX better solution for now...
629bd3cdc31SAndrzej Bialecki 	 */
6307665e341SMateusz Guzik 	SYSCTL_WLOCK();
631bd3cdc31SAndrzej Bialecki 	TAILQ_FOREACH(e, clist, link) {
632875b66a0SJohn Baldwin 		error = sysctl_remove_oid_locked(e->entry, 0, 0);
633bd3cdc31SAndrzej Bialecki 		if (error)
634bd3cdc31SAndrzej Bialecki 			break;
635bd3cdc31SAndrzej Bialecki 	}
636bd3cdc31SAndrzej Bialecki 	/*
637bd3cdc31SAndrzej Bialecki 	 * Restore deregistered entries, either from the end,
638e3043798SPedro F. Giffuni 	 * or from the place where error occurred.
639bd3cdc31SAndrzej Bialecki 	 * e contains the entry that was not unregistered
640bd3cdc31SAndrzej Bialecki 	 */
641bd3cdc31SAndrzej Bialecki 	if (error)
642bd3cdc31SAndrzej Bialecki 		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
643bd3cdc31SAndrzej Bialecki 	else
644bd3cdc31SAndrzej Bialecki 		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
645bd3cdc31SAndrzej Bialecki 	while (e1 != NULL) {
646bd3cdc31SAndrzej Bialecki 		sysctl_register_oid(e1->entry);
647bd3cdc31SAndrzej Bialecki 		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
648bd3cdc31SAndrzej Bialecki 	}
649875b66a0SJohn Baldwin 	if (error) {
6507665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
651bd3cdc31SAndrzej Bialecki 		return(EBUSY);
652875b66a0SJohn Baldwin 	}
653bd3cdc31SAndrzej Bialecki 	/* Now really delete the entries */
654d5eae570SMark Johnston 	TAILQ_FOREACH_SAFE(e, clist, link, e1) {
655875b66a0SJohn Baldwin 		error = sysctl_remove_oid_locked(e->entry, 1, 0);
656bd3cdc31SAndrzej Bialecki 		if (error)
657bd3cdc31SAndrzej Bialecki 			panic("sysctl_remove_oid: corrupt tree, entry: %s",
658bd3cdc31SAndrzej Bialecki 			    e->entry->oid_name);
659bd3cdc31SAndrzej Bialecki 		free(e, M_SYSCTLOID);
660bd3cdc31SAndrzej Bialecki 	}
6617665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
662d5eae570SMark Johnston 	TAILQ_INIT(clist);
663bd3cdc31SAndrzej Bialecki 	return (error);
664bd3cdc31SAndrzej Bialecki }
665bd3cdc31SAndrzej Bialecki 
666bd3cdc31SAndrzej Bialecki /* Add an entry to the context */
667bd3cdc31SAndrzej Bialecki struct sysctl_ctx_entry *
sysctl_ctx_entry_add(struct sysctl_ctx_list * clist,struct sysctl_oid * oidp)668bd3cdc31SAndrzej Bialecki sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
669bd3cdc31SAndrzej Bialecki {
670bd3cdc31SAndrzej Bialecki 	struct sysctl_ctx_entry *e;
671bd3cdc31SAndrzej Bialecki 
6727665e341SMateusz Guzik 	SYSCTL_ASSERT_WLOCKED();
673bd3cdc31SAndrzej Bialecki 	if (clist == NULL || oidp == NULL)
674bd3cdc31SAndrzej Bialecki 		return(NULL);
675a163d034SWarner Losh 	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
676bd3cdc31SAndrzej Bialecki 	e->entry = oidp;
677bd3cdc31SAndrzej Bialecki 	TAILQ_INSERT_HEAD(clist, e, link);
678bd3cdc31SAndrzej Bialecki 	return (e);
679bd3cdc31SAndrzej Bialecki }
680bd3cdc31SAndrzej Bialecki 
681bd3cdc31SAndrzej Bialecki /* Find an entry in the context */
682bd3cdc31SAndrzej Bialecki struct sysctl_ctx_entry *
sysctl_ctx_entry_find(struct sysctl_ctx_list * clist,struct sysctl_oid * oidp)683bd3cdc31SAndrzej Bialecki sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
684bd3cdc31SAndrzej Bialecki {
685bd3cdc31SAndrzej Bialecki 	struct sysctl_ctx_entry *e;
686bd3cdc31SAndrzej Bialecki 
6877665e341SMateusz Guzik 	SYSCTL_ASSERT_WLOCKED();
688bd3cdc31SAndrzej Bialecki 	if (clist == NULL || oidp == NULL)
689bd3cdc31SAndrzej Bialecki 		return(NULL);
690fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(e, clist, link) {
691bd3cdc31SAndrzej Bialecki 		if (e->entry == oidp)
692bd3cdc31SAndrzej Bialecki 			return(e);
693bd3cdc31SAndrzej Bialecki 	}
694bd3cdc31SAndrzej Bialecki 	return (e);
695bd3cdc31SAndrzej Bialecki }
696bd3cdc31SAndrzej Bialecki 
697bd3cdc31SAndrzej Bialecki /*
698bd3cdc31SAndrzej Bialecki  * Delete an entry from the context.
699bd3cdc31SAndrzej Bialecki  * NOTE: this function doesn't free oidp! You have to remove it
700bd3cdc31SAndrzej Bialecki  * with sysctl_remove_oid().
701bd3cdc31SAndrzej Bialecki  */
702bd3cdc31SAndrzej Bialecki int
sysctl_ctx_entry_del(struct sysctl_ctx_list * clist,struct sysctl_oid * oidp)703bd3cdc31SAndrzej Bialecki sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
704bd3cdc31SAndrzej Bialecki {
705bd3cdc31SAndrzej Bialecki 	struct sysctl_ctx_entry *e;
706bd3cdc31SAndrzej Bialecki 
707bd3cdc31SAndrzej Bialecki 	if (clist == NULL || oidp == NULL)
708bd3cdc31SAndrzej Bialecki 		return (EINVAL);
7097665e341SMateusz Guzik 	SYSCTL_WLOCK();
710bd3cdc31SAndrzej Bialecki 	e = sysctl_ctx_entry_find(clist, oidp);
711bd3cdc31SAndrzej Bialecki 	if (e != NULL) {
712bd3cdc31SAndrzej Bialecki 		TAILQ_REMOVE(clist, e, link);
7137665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
714bd3cdc31SAndrzej Bialecki 		free(e, M_SYSCTLOID);
715bd3cdc31SAndrzej Bialecki 		return (0);
716875b66a0SJohn Baldwin 	} else {
7177665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
718bd3cdc31SAndrzej Bialecki 		return (ENOENT);
719bd3cdc31SAndrzej Bialecki 	}
720875b66a0SJohn Baldwin }
721bd3cdc31SAndrzej Bialecki 
722bd3cdc31SAndrzej Bialecki /*
723bd3cdc31SAndrzej Bialecki  * Remove dynamically created sysctl trees.
724bd3cdc31SAndrzej Bialecki  * oidp - top of the tree to be removed
725bd3cdc31SAndrzej Bialecki  * del - if 0 - just deregister, otherwise free up entries as well
726bd3cdc31SAndrzej Bialecki  * recurse - if != 0 traverse the subtree to be deleted
727bd3cdc31SAndrzej Bialecki  */
728bd3cdc31SAndrzej Bialecki int
sysctl_remove_oid(struct sysctl_oid * oidp,int del,int recurse)729bd3cdc31SAndrzej Bialecki sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
730bd3cdc31SAndrzej Bialecki {
731875b66a0SJohn Baldwin 	int error;
732875b66a0SJohn Baldwin 
7337665e341SMateusz Guzik 	SYSCTL_WLOCK();
734875b66a0SJohn Baldwin 	error = sysctl_remove_oid_locked(oidp, del, recurse);
7357665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
736875b66a0SJohn Baldwin 	return (error);
737875b66a0SJohn Baldwin }
738875b66a0SJohn Baldwin 
739e4cd31ddSJeff Roberson int
sysctl_remove_name(struct sysctl_oid * parent,const char * name,int del,int recurse)740e4cd31ddSJeff Roberson sysctl_remove_name(struct sysctl_oid *parent, const char *name,
741e4cd31ddSJeff Roberson     int del, int recurse)
742e4cd31ddSJeff Roberson {
743d3f96f66SAlan Somers 	struct sysctl_oid *p;
744e4cd31ddSJeff Roberson 	int error;
745e4cd31ddSJeff Roberson 
746e4cd31ddSJeff Roberson 	error = ENOENT;
7477665e341SMateusz Guzik 	SYSCTL_WLOCK();
748d3f96f66SAlan Somers 	p = sysctl_find_oidname(name, &parent->oid_children);
749d3f96f66SAlan Somers 	if (p)
750e4cd31ddSJeff Roberson 		error = sysctl_remove_oid_locked(p, del, recurse);
7517665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
752e4cd31ddSJeff Roberson 
753e4cd31ddSJeff Roberson 	return (error);
754e4cd31ddSJeff Roberson }
755e4cd31ddSJeff Roberson 
7566c950655SAlan Somers /*
7576c950655SAlan Somers  * Duplicate the provided string, escaping any illegal characters.  The result
7586c950655SAlan Somers  * must be freed when no longer in use.
7596c950655SAlan Somers  *
7606c950655SAlan Somers  * The list of illegal characters is ".".
7616c950655SAlan Somers  */
7626c950655SAlan Somers static char*
sysctl_escape_name(const char * orig)7636c950655SAlan Somers sysctl_escape_name(const char* orig)
7646c950655SAlan Somers {
7656c950655SAlan Somers 	int i, s = 0, d = 0, nillegals = 0;
7666c950655SAlan Somers 	char *new;
7676c950655SAlan Somers 
7686c950655SAlan Somers 	/* First count the number of illegal characters */
7696c950655SAlan Somers 	for (i = 0; orig[i] != '\0'; i++) {
7706c950655SAlan Somers 		if (orig[i] == '.')
7716c950655SAlan Somers 			nillegals++;
7726c950655SAlan Somers 	}
7736c950655SAlan Somers 
7746c950655SAlan Somers 	/* Allocate storage for new string */
7756c950655SAlan Somers 	new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
7766c950655SAlan Somers 
7776c950655SAlan Somers 	/* Copy the name, escaping characters as we go */
7786c950655SAlan Somers 	while (orig[s] != '\0') {
7796c950655SAlan Somers 		if (orig[s] == '.') {
7806c950655SAlan Somers 			/* %25 is the hexadecimal representation of '.' */
7816c950655SAlan Somers 			new[d++] = '%';
7826c950655SAlan Somers 			new[d++] = '2';
7836c950655SAlan Somers 			new[d++] = '5';
7846c950655SAlan Somers 			s++;
7856c950655SAlan Somers 		} else {
7866c950655SAlan Somers 			new[d++] = orig[s++];
7876c950655SAlan Somers 		}
7886c950655SAlan Somers 	}
7896c950655SAlan Somers 
7906c950655SAlan Somers 	/* Finally, nul-terminate */
7916c950655SAlan Somers 	new[d] = '\0';
7926c950655SAlan Somers 
7936c950655SAlan Somers 	return (new);
7946c950655SAlan Somers }
7956c950655SAlan Somers 
796875b66a0SJohn Baldwin static int
sysctl_remove_oid_locked(struct sysctl_oid * oidp,int del,int recurse)797875b66a0SJohn Baldwin sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
798875b66a0SJohn Baldwin {
799e4cd31ddSJeff Roberson 	struct sysctl_oid *p, *tmp;
800bd3cdc31SAndrzej Bialecki 	int error;
801bd3cdc31SAndrzej Bialecki 
8027665e341SMateusz Guzik 	SYSCTL_ASSERT_WLOCKED();
803bd3cdc31SAndrzej Bialecki 	if (oidp == NULL)
804bd3cdc31SAndrzej Bialecki 		return(EINVAL);
805bd3cdc31SAndrzej Bialecki 	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
8068b69c3e7SEnji Cooper 		printf("Warning: can't remove non-dynamic nodes (%s)!\n",
8078b69c3e7SEnji Cooper 		    oidp->oid_name);
808bd3cdc31SAndrzej Bialecki 		return (EINVAL);
809bd3cdc31SAndrzej Bialecki 	}
810bd3cdc31SAndrzej Bialecki 	/*
811bd3cdc31SAndrzej Bialecki 	 * WARNING: normal method to do this should be through
812bd3cdc31SAndrzej Bialecki 	 * sysctl_ctx_free(). Use recursing as the last resort
813bd3cdc31SAndrzej Bialecki 	 * method to purge your sysctl tree of leftovers...
814bd3cdc31SAndrzej Bialecki 	 * However, if some other code still references these nodes,
815bd3cdc31SAndrzej Bialecki 	 * it will panic.
816bd3cdc31SAndrzej Bialecki 	 */
817bd3cdc31SAndrzej Bialecki 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
818bd3cdc31SAndrzej Bialecki 		if (oidp->oid_refcnt == 1) {
819d3f96f66SAlan Somers 			for(p = RB_MIN(sysctl_oid_list, &oidp->oid_children);
820d3f96f66SAlan Somers 			    p != NULL; p = tmp) {
821f510415dSScott Long 				if (!recurse) {
822f510415dSScott Long 					printf("Warning: failed attempt to "
823f510415dSScott Long 					    "remove oid %s with child %s\n",
824f510415dSScott Long 					    oidp->oid_name, p->oid_name);
825bd3cdc31SAndrzej Bialecki 					return (ENOTEMPTY);
826f510415dSScott Long 				}
827d3f96f66SAlan Somers 				tmp = RB_NEXT(sysctl_oid_list,
828d3f96f66SAlan Somers 				    &oidp->oid_children, p);
829875b66a0SJohn Baldwin 				error = sysctl_remove_oid_locked(p, del,
830875b66a0SJohn Baldwin 				    recurse);
831bd3cdc31SAndrzej Bialecki 				if (error)
832bd3cdc31SAndrzej Bialecki 					return (error);
833bd3cdc31SAndrzej Bialecki 			}
834bd3cdc31SAndrzej Bialecki 		}
835bd3cdc31SAndrzej Bialecki 	}
836bd3cdc31SAndrzej Bialecki 	if (oidp->oid_refcnt > 1 ) {
837bd3cdc31SAndrzej Bialecki 		oidp->oid_refcnt--;
838bd3cdc31SAndrzej Bialecki 	} else {
839bd3cdc31SAndrzej Bialecki 		if (oidp->oid_refcnt == 0) {
840bd3cdc31SAndrzej Bialecki 			printf("Warning: bad oid_refcnt=%u (%s)!\n",
841bd3cdc31SAndrzej Bialecki 				oidp->oid_refcnt, oidp->oid_name);
842bd3cdc31SAndrzej Bialecki 			return (EINVAL);
843bd3cdc31SAndrzej Bialecki 		}
844bd3cdc31SAndrzej Bialecki 		sysctl_unregister_oid(oidp);
845bd3cdc31SAndrzej Bialecki 		if (del) {
846ccecef29SMatthew D Fleming 			/*
847ccecef29SMatthew D Fleming 			 * Wait for all threads running the handler to drain.
848ccecef29SMatthew D Fleming 			 * This preserves the previous behavior when the
849ccecef29SMatthew D Fleming 			 * sysctl lock was held across a handler invocation,
850ccecef29SMatthew D Fleming 			 * and is necessary for module unload correctness.
851ccecef29SMatthew D Fleming 			 */
852ccecef29SMatthew D Fleming 			while (oidp->oid_running > 0) {
853ccecef29SMatthew D Fleming 				oidp->oid_kind |= CTLFLAG_DYING;
854ccecef29SMatthew D Fleming 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
855ccecef29SMatthew D Fleming 			}
8565937226dSPoul-Henning Kamp 			if (oidp->oid_descr)
857db9066f7SMarius Strobl 				free(__DECONST(char *, oidp->oid_descr),
85893e8459aSPeter Wemm 				    M_SYSCTLOID);
8591e1f3941SEd Schouten 			if (oidp->oid_label)
8601e1f3941SEd Schouten 				free(__DECONST(char *, oidp->oid_label),
8611e1f3941SEd Schouten 				    M_SYSCTLOID);
862db9066f7SMarius Strobl 			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
863bd3cdc31SAndrzej Bialecki 			free(oidp, M_SYSCTLOID);
864bd3cdc31SAndrzej Bialecki 		}
865bd3cdc31SAndrzej Bialecki 	}
866bd3cdc31SAndrzej Bialecki 	return (0);
867bd3cdc31SAndrzej Bialecki }
868bd3cdc31SAndrzej Bialecki /*
869bd3cdc31SAndrzej Bialecki  * Create new sysctls at run time.
870bd3cdc31SAndrzej Bialecki  * clist may point to a valid context initialized with sysctl_ctx_init().
871bd3cdc31SAndrzej Bialecki  */
872bd3cdc31SAndrzej Bialecki struct sysctl_oid *
sysctl_add_oid(struct sysctl_ctx_list * clist,struct sysctl_oid_list * parent,int number,const char * name,int kind,void * arg1,intmax_t arg2,int (* handler)(SYSCTL_HANDLER_ARGS),const char * fmt,const char * descr,const char * label)873bd3cdc31SAndrzej Bialecki sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
874be87839eSConrad Meyer 	int number, const char *name, int kind, void *arg1, intmax_t arg2,
8751e1f3941SEd Schouten 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
8761e1f3941SEd Schouten 	const char *label)
877bd3cdc31SAndrzej Bialecki {
878bd3cdc31SAndrzej Bialecki 	struct sysctl_oid *oidp;
8796c950655SAlan Somers 	char *escaped;
880bd3cdc31SAndrzej Bialecki 
881bd3cdc31SAndrzej Bialecki 	/* You have to hook up somewhere.. */
882bd3cdc31SAndrzej Bialecki 	if (parent == NULL)
883bd3cdc31SAndrzej Bialecki 		return(NULL);
8846c950655SAlan Somers 	escaped = sysctl_escape_name(name);
885bd3cdc31SAndrzej Bialecki 	/* Check if the node already exists, otherwise create it */
8867665e341SMateusz Guzik 	SYSCTL_WLOCK();
8876c950655SAlan Somers 	oidp = sysctl_find_oidname(escaped, parent);
888bd3cdc31SAndrzej Bialecki 	if (oidp != NULL) {
8896c950655SAlan Somers 		free(escaped, M_SYSCTLOID);
890bd3cdc31SAndrzej Bialecki 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
891bd3cdc31SAndrzej Bialecki 			oidp->oid_refcnt++;
892bd3cdc31SAndrzej Bialecki 			/* Update the context */
893bd3cdc31SAndrzej Bialecki 			if (clist != NULL)
894bd3cdc31SAndrzej Bialecki 				sysctl_ctx_entry_add(clist, oidp);
8957665e341SMateusz Guzik 			SYSCTL_WUNLOCK();
896bd3cdc31SAndrzej Bialecki 			return (oidp);
897bd3cdc31SAndrzej Bialecki 		} else {
8984ae2ade1SConrad Meyer 			sysctl_warn_reuse(__func__, oidp);
8997665e341SMateusz Guzik 			SYSCTL_WUNLOCK();
900bd3cdc31SAndrzej Bialecki 			return (NULL);
901bd3cdc31SAndrzej Bialecki 		}
902bd3cdc31SAndrzej Bialecki 	}
903a163d034SWarner Losh 	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
904bd3cdc31SAndrzej Bialecki 	oidp->oid_parent = parent;
905d3f96f66SAlan Somers 	RB_INIT(&oidp->oid_children);
906bd3cdc31SAndrzej Bialecki 	oidp->oid_number = number;
907bd3cdc31SAndrzej Bialecki 	oidp->oid_refcnt = 1;
9086c950655SAlan Somers 	oidp->oid_name = escaped;
909bd3cdc31SAndrzej Bialecki 	oidp->oid_handler = handler;
910bd3cdc31SAndrzej Bialecki 	oidp->oid_kind = CTLFLAG_DYN | kind;
911bd3cdc31SAndrzej Bialecki 	oidp->oid_arg1 = arg1;
912bd3cdc31SAndrzej Bialecki 	oidp->oid_arg2 = arg2;
913bd3cdc31SAndrzej Bialecki 	oidp->oid_fmt = fmt;
914af3b2549SHans Petter Selasky 	if (descr != NULL)
915db9066f7SMarius Strobl 		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
9161e1f3941SEd Schouten 	if (label != NULL)
9171e1f3941SEd Schouten 		oidp->oid_label = strdup(label, M_SYSCTLOID);
918bd3cdc31SAndrzej Bialecki 	/* Update the context, if used */
919bd3cdc31SAndrzej Bialecki 	if (clist != NULL)
920bd3cdc31SAndrzej Bialecki 		sysctl_ctx_entry_add(clist, oidp);
921bd3cdc31SAndrzej Bialecki 	/* Register this oid */
922bd3cdc31SAndrzej Bialecki 	sysctl_register_oid(oidp);
9237665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
924bd3cdc31SAndrzej Bialecki 	return (oidp);
925bd3cdc31SAndrzej Bialecki }
926bd3cdc31SAndrzej Bialecki 
927ce02431fSDoug Rabson /*
928cd17ceaaSPeter Wemm  * Rename an existing oid.
929cd17ceaaSPeter Wemm  */
930cd17ceaaSPeter Wemm void
sysctl_rename_oid(struct sysctl_oid * oidp,const char * name)931cd17ceaaSPeter Wemm sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
932cd17ceaaSPeter Wemm {
933cd17ceaaSPeter Wemm 	char *newname;
934db9066f7SMarius Strobl 	char *oldname;
935cd17ceaaSPeter Wemm 
936db9066f7SMarius Strobl 	newname = strdup(name, M_SYSCTLOID);
9377665e341SMateusz Guzik 	SYSCTL_WLOCK();
938db9066f7SMarius Strobl 	oldname = __DECONST(char *, oidp->oid_name);
939cd17ceaaSPeter Wemm 	oidp->oid_name = newname;
9407665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
941cd17ceaaSPeter Wemm 	free(oldname, M_SYSCTLOID);
942cd17ceaaSPeter Wemm }
943cd17ceaaSPeter Wemm 
944cd17ceaaSPeter Wemm /*
94521885af5SDag-Erling Smørgrav  * Reparent an existing oid.
94621885af5SDag-Erling Smørgrav  */
94721885af5SDag-Erling Smørgrav int
sysctl_move_oid(struct sysctl_oid * oid,struct sysctl_oid_list * parent)94821885af5SDag-Erling Smørgrav sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
94921885af5SDag-Erling Smørgrav {
95021885af5SDag-Erling Smørgrav 	struct sysctl_oid *oidp;
95121885af5SDag-Erling Smørgrav 
9527665e341SMateusz Guzik 	SYSCTL_WLOCK();
953875b66a0SJohn Baldwin 	if (oid->oid_parent == parent) {
9547665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
95521885af5SDag-Erling Smørgrav 		return (0);
956875b66a0SJohn Baldwin 	}
95721885af5SDag-Erling Smørgrav 	oidp = sysctl_find_oidname(oid->oid_name, parent);
958875b66a0SJohn Baldwin 	if (oidp != NULL) {
9597665e341SMateusz Guzik 		SYSCTL_WUNLOCK();
96021885af5SDag-Erling Smørgrav 		return (EEXIST);
961875b66a0SJohn Baldwin 	}
96221885af5SDag-Erling Smørgrav 	sysctl_unregister_oid(oid);
96321885af5SDag-Erling Smørgrav 	oid->oid_parent = parent;
96421885af5SDag-Erling Smørgrav 	oid->oid_number = OID_AUTO;
96521885af5SDag-Erling Smørgrav 	sysctl_register_oid(oid);
9667665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
96721885af5SDag-Erling Smørgrav 	return (0);
96821885af5SDag-Erling Smørgrav }
96921885af5SDag-Erling Smørgrav 
97021885af5SDag-Erling Smørgrav /*
971ce02431fSDoug Rabson  * Register the kernel's oids on startup.
972ce02431fSDoug Rabson  */
973f41325dbSPeter Wemm SET_DECLARE(sysctl_set, struct sysctl_oid);
974ce02431fSDoug Rabson 
975107e7dc5SPeter Pentchev static void
sysctl_register_all(void * arg)976107e7dc5SPeter Pentchev sysctl_register_all(void *arg)
977ce02431fSDoug Rabson {
978f41325dbSPeter Wemm 	struct sysctl_oid **oidp;
979ce02431fSDoug Rabson 
9803e829b18SJohn Baldwin 	sx_init(&sysctlmemlock, "sysctl mem");
981210176adSPawel Biernacki 	sx_init(&sysctlstringlock, "sysctl string handler");
9823bd1da29SRobert Watson 	SYSCTL_INIT();
9837665e341SMateusz Guzik 	SYSCTL_WLOCK();
984f41325dbSPeter Wemm 	SET_FOREACH(oidp, sysctl_set)
985f41325dbSPeter Wemm 		sysctl_register_oid(*oidp);
9867665e341SMateusz Guzik 	SYSCTL_WUNLOCK();
987f41325dbSPeter Wemm }
988891cf3edSEd Maste SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
989ce02431fSDoug Rabson 
990cf7974fdSZhenlei Huang #ifdef VIMAGE
991cf7974fdSZhenlei Huang static void
sysctl_setenv_vnet(void * arg __unused,const char * name)992f80babf9SAlexander Motin sysctl_setenv_vnet(void *arg __unused, const char *name)
993cf7974fdSZhenlei Huang {
994cf7974fdSZhenlei Huang 	struct sysctl_oid *oidp;
995cf7974fdSZhenlei Huang 	int oid[CTL_MAXNAME];
996cf7974fdSZhenlei Huang 	int error, nlen;
997cf7974fdSZhenlei Huang 
998cf7974fdSZhenlei Huang 	SYSCTL_WLOCK();
999cf7974fdSZhenlei Huang 	error = name2oid(name, oid, &nlen, &oidp);
1000cf7974fdSZhenlei Huang 	if (error)
1001cf7974fdSZhenlei Huang 		goto out;
1002cf7974fdSZhenlei Huang 
1003cf7974fdSZhenlei Huang 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1004cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1005cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1006cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1007cf7974fdSZhenlei Huang 		/* Update value from kernel environment */
1008cf7974fdSZhenlei Huang 		sysctl_load_tunable_by_oid_locked(oidp);
1009cf7974fdSZhenlei Huang 	}
1010cf7974fdSZhenlei Huang out:
1011cf7974fdSZhenlei Huang 	SYSCTL_WUNLOCK();
1012cf7974fdSZhenlei Huang }
1013cf7974fdSZhenlei Huang 
1014cf7974fdSZhenlei Huang static void
sysctl_unsetenv_vnet(void * arg __unused,const char * name)1015f80babf9SAlexander Motin sysctl_unsetenv_vnet(void *arg __unused, const char *name)
1016cf7974fdSZhenlei Huang {
1017cf7974fdSZhenlei Huang 	struct sysctl_oid *oidp;
1018cf7974fdSZhenlei Huang 	int oid[CTL_MAXNAME];
1019cf7974fdSZhenlei Huang 	int error, nlen;
1020cf7974fdSZhenlei Huang 
1021cf7974fdSZhenlei Huang 	SYSCTL_WLOCK();
1022cf7974fdSZhenlei Huang 	/*
1023cf7974fdSZhenlei Huang 	 * The setenv / unsetenv event handlers are invoked by kern_setenv() /
1024cf7974fdSZhenlei Huang 	 * kern_unsetenv() without exclusive locks. It is rare but still possible
1025cf7974fdSZhenlei Huang 	 * that the invoke order of event handlers is different from that of
1026cf7974fdSZhenlei Huang 	 * kern_setenv() and kern_unsetenv().
1027cf7974fdSZhenlei Huang 	 * Re-check environment variable string to make sure it is unset.
1028cf7974fdSZhenlei Huang 	 */
1029cf7974fdSZhenlei Huang 	if (testenv(name))
1030cf7974fdSZhenlei Huang 		goto out;
1031cf7974fdSZhenlei Huang 	error = name2oid(name, oid, &nlen, &oidp);
1032cf7974fdSZhenlei Huang 	if (error)
1033cf7974fdSZhenlei Huang 		goto out;
1034cf7974fdSZhenlei Huang 
1035cf7974fdSZhenlei Huang 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1036cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1037cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1038cf7974fdSZhenlei Huang 	    (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1039cf7974fdSZhenlei Huang 		size_t size;
1040cf7974fdSZhenlei Huang 
1041cf7974fdSZhenlei Huang 		switch (oidp->oid_kind & CTLTYPE) {
1042cf7974fdSZhenlei Huang 		case CTLTYPE_INT:
1043cf7974fdSZhenlei Huang 		case CTLTYPE_UINT:
1044cf7974fdSZhenlei Huang 			size = sizeof(int);
1045cf7974fdSZhenlei Huang 			break;
1046cf7974fdSZhenlei Huang 		case CTLTYPE_LONG:
1047cf7974fdSZhenlei Huang 		case CTLTYPE_ULONG:
1048cf7974fdSZhenlei Huang 			size = sizeof(long);
1049cf7974fdSZhenlei Huang 			break;
1050cf7974fdSZhenlei Huang 		case CTLTYPE_S8:
1051cf7974fdSZhenlei Huang 		case CTLTYPE_U8:
1052cf7974fdSZhenlei Huang 			size = sizeof(int8_t);
1053cf7974fdSZhenlei Huang 			break;
1054cf7974fdSZhenlei Huang 		case CTLTYPE_S16:
1055cf7974fdSZhenlei Huang 		case CTLTYPE_U16:
1056cf7974fdSZhenlei Huang 			size = sizeof(int16_t);
1057cf7974fdSZhenlei Huang 			break;
1058cf7974fdSZhenlei Huang 		case CTLTYPE_S32:
1059cf7974fdSZhenlei Huang 		case CTLTYPE_U32:
1060cf7974fdSZhenlei Huang 			size = sizeof(int32_t);
1061cf7974fdSZhenlei Huang 			break;
1062cf7974fdSZhenlei Huang 		case CTLTYPE_S64:
1063cf7974fdSZhenlei Huang 		case CTLTYPE_U64:
1064cf7974fdSZhenlei Huang 			size = sizeof(int64_t);
1065cf7974fdSZhenlei Huang 			break;
1066cf7974fdSZhenlei Huang 		case CTLTYPE_STRING:
1067cf7974fdSZhenlei Huang 			MPASS(oidp->oid_arg2 > 0);
1068cf7974fdSZhenlei Huang 			size = oidp->oid_arg2;
1069cf7974fdSZhenlei Huang 			break;
1070cf7974fdSZhenlei Huang 		default:
1071cf7974fdSZhenlei Huang 			goto out;
1072cf7974fdSZhenlei Huang 		}
1073cf7974fdSZhenlei Huang 		vnet_restore_init(oidp->oid_arg1, size);
1074cf7974fdSZhenlei Huang 	}
1075cf7974fdSZhenlei Huang out:
1076cf7974fdSZhenlei Huang 	SYSCTL_WUNLOCK();
1077cf7974fdSZhenlei Huang }
1078cf7974fdSZhenlei Huang 
1079cf7974fdSZhenlei Huang /*
1080cf7974fdSZhenlei Huang  * Register the kernel's setenv / unsetenv events.
1081cf7974fdSZhenlei Huang  */
1082cf7974fdSZhenlei Huang EVENTHANDLER_DEFINE(setenv, sysctl_setenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1083cf7974fdSZhenlei Huang EVENTHANDLER_DEFINE(unsetenv, sysctl_unsetenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1084cf7974fdSZhenlei Huang #endif
1085cf7974fdSZhenlei Huang 
1086946bb7a2SPoul-Henning Kamp /*
1087946bb7a2SPoul-Henning Kamp  * "Staff-functions"
1088946bb7a2SPoul-Henning Kamp  *
108965d0bc13SPoul-Henning Kamp  * These functions implement a presently undocumented interface
109065d0bc13SPoul-Henning Kamp  * used by the sysctl program to walk the tree, and get the type
109165d0bc13SPoul-Henning Kamp  * so it can print the value.
109265d0bc13SPoul-Henning Kamp  * This interface is under work and consideration, and should probably
109365d0bc13SPoul-Henning Kamp  * be killed with a big axe by the first person who can find the time.
109465d0bc13SPoul-Henning Kamp  * (be aware though, that the proper interface isn't as obvious as it
109565d0bc13SPoul-Henning Kamp  * may seem, there are various conflicting requirements.
109665d0bc13SPoul-Henning Kamp  *
1097382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_DEBUG}		printf the entire MIB-tree.
1098382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...}		return the name of the "..."
1099382e01c8SKonstantin Belousov  *						OID.
110092e17803SRyan Moeller  * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...}		return the next OID, honoring
110192e17803SRyan Moeller  *						CTLFLAG_SKIP.
1102382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID}		return the OID of the name in
1103382e01c8SKonstantin Belousov  *						"new"
1104382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...}		return the kind & format info
1105382e01c8SKonstantin Belousov  *						for the "..." OID.
1106382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...}	return the description of the
1107382e01c8SKonstantin Belousov  *						"..." OID.
1108382e01c8SKonstantin Belousov  * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...}	return the aggregation label of
1109382e01c8SKonstantin Belousov  *						the "..." OID.
111092e17803SRyan Moeller  * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...}	return the next OID, ignoring
111192e17803SRyan Moeller  *						CTLFLAG_SKIP.
1112946bb7a2SPoul-Henning Kamp  */
1113946bb7a2SPoul-Henning Kamp 
1114df970488SRobert Watson #ifdef SYSCTL_DEBUG
1115787d58f2SPoul-Henning Kamp static void
sysctl_sysctl_debug_dump_node(struct sysctl_oid_list * l,int i)1116ce02431fSDoug Rabson sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1117787d58f2SPoul-Henning Kamp {
1118ce02431fSDoug Rabson 	int k;
1119ce02431fSDoug Rabson 	struct sysctl_oid *oidp;
1120787d58f2SPoul-Henning Kamp 
1121fca77320SMateusz Guzik 	SYSCTL_ASSERT_LOCKED();
1122c075ea46SHans Petter Selasky 	SYSCTL_FOREACH(oidp, l) {
1123787d58f2SPoul-Henning Kamp 		for (k=0; k<i; k++)
1124787d58f2SPoul-Henning Kamp 			printf(" ");
1125787d58f2SPoul-Henning Kamp 
1126ce02431fSDoug Rabson 		printf("%d %s ", oidp->oid_number, oidp->oid_name);
1127787d58f2SPoul-Henning Kamp 
1128787d58f2SPoul-Henning Kamp 		printf("%c%c",
1129ce02431fSDoug Rabson 			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1130ce02431fSDoug Rabson 			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1131787d58f2SPoul-Henning Kamp 
1132ce02431fSDoug Rabson 		if (oidp->oid_handler)
113361220614SPoul-Henning Kamp 			printf(" *Handler");
113461220614SPoul-Henning Kamp 
1135ce02431fSDoug Rabson 		switch (oidp->oid_kind & CTLTYPE) {
1136787d58f2SPoul-Henning Kamp 			case CTLTYPE_NODE:
1137787d58f2SPoul-Henning Kamp 				printf(" Node\n");
1138ce02431fSDoug Rabson 				if (!oidp->oid_handler) {
1139787d58f2SPoul-Henning Kamp 					sysctl_sysctl_debug_dump_node(
1140af3b2549SHans Petter Selasky 					    SYSCTL_CHILDREN(oidp), i + 2);
1141787d58f2SPoul-Henning Kamp 				}
1142787d58f2SPoul-Henning Kamp 				break;
1143787d58f2SPoul-Henning Kamp 			case CTLTYPE_INT:    printf(" Int\n"); break;
1144cbc134adSMatthew D Fleming 			case CTLTYPE_UINT:   printf(" u_int\n"); break;
1145cbc134adSMatthew D Fleming 			case CTLTYPE_LONG:   printf(" Long\n"); break;
1146cbc134adSMatthew D Fleming 			case CTLTYPE_ULONG:  printf(" u_long\n"); break;
1147787d58f2SPoul-Henning Kamp 			case CTLTYPE_STRING: printf(" String\n"); break;
1148e072f955SConrad Meyer 			case CTLTYPE_S8:     printf(" int8_t\n"); break;
1149e072f955SConrad Meyer 			case CTLTYPE_S16:    printf(" int16_t\n"); break;
1150e072f955SConrad Meyer 			case CTLTYPE_S32:    printf(" int32_t\n"); break;
1151cbc134adSMatthew D Fleming 			case CTLTYPE_S64:    printf(" int64_t\n"); break;
1152e072f955SConrad Meyer 			case CTLTYPE_U8:     printf(" uint8_t\n"); break;
1153e072f955SConrad Meyer 			case CTLTYPE_U16:    printf(" uint16_t\n"); break;
1154e072f955SConrad Meyer 			case CTLTYPE_U32:    printf(" uint32_t\n"); break;
1155e072f955SConrad Meyer 			case CTLTYPE_U64:    printf(" uint64_t\n"); break;
1156787d58f2SPoul-Henning Kamp 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1157787d58f2SPoul-Henning Kamp 			default:	     printf("\n");
1158787d58f2SPoul-Henning Kamp 		}
1159787d58f2SPoul-Henning Kamp 	}
1160787d58f2SPoul-Henning Kamp }
1161787d58f2SPoul-Henning Kamp 
1162787d58f2SPoul-Henning Kamp static int
sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)116382d9ae4eSPoul-Henning Kamp sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1164787d58f2SPoul-Henning Kamp {
11657665e341SMateusz Guzik 	struct rm_priotracker tracker;
1166023a0e61SPeter Wemm 	int error;
1167023a0e61SPeter Wemm 
1168acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1169023a0e61SPeter Wemm 	if (error)
117046003fb3SPawel Jakub Dawidek 		return (error);
11717665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
1172ce02431fSDoug Rabson 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
11737665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
117446003fb3SPawel Jakub Dawidek 	return (ENOENT);
1175787d58f2SPoul-Henning Kamp }
1176787d58f2SPoul-Henning Kamp 
1177382e01c8SKonstantin Belousov SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1178382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1179df970488SRobert Watson #endif
11802e210993SPoul-Henning Kamp 
1181946bb7a2SPoul-Henning Kamp static int
sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)118282d9ae4eSPoul-Henning Kamp sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1183946bb7a2SPoul-Henning Kamp {
1184946bb7a2SPoul-Henning Kamp 	int *name = (int *) arg1;
1185946bb7a2SPoul-Henning Kamp 	u_int namelen = arg2;
118643cefe8bSRyan Libby 	int error;
1187d3f96f66SAlan Somers 	struct sysctl_oid *oid, key;
11887f4173ccSPoul-Henning Kamp 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
11897665e341SMateusz Guzik 	struct rm_priotracker tracker;
1190946bb7a2SPoul-Henning Kamp 	char buf[10];
11912e210993SPoul-Henning Kamp 
119243cefe8bSRyan Libby 	error = sysctl_wire_old_buffer(req, 0);
119343cefe8bSRyan Libby 	if (error)
119443cefe8bSRyan Libby 		return (error);
119543cefe8bSRyan Libby 
11967665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
1197946bb7a2SPoul-Henning Kamp 	while (namelen) {
1198946bb7a2SPoul-Henning Kamp 		if (!lsp) {
11992127f260SArchie Cobbs 			snprintf(buf,sizeof(buf),"%d",*name);
1200946bb7a2SPoul-Henning Kamp 			if (req->oldidx)
1201946bb7a2SPoul-Henning Kamp 				error = SYSCTL_OUT(req, ".", 1);
1202946bb7a2SPoul-Henning Kamp 			if (!error)
1203946bb7a2SPoul-Henning Kamp 				error = SYSCTL_OUT(req, buf, strlen(buf));
1204946bb7a2SPoul-Henning Kamp 			if (error)
1205ccecef29SMatthew D Fleming 				goto out;
1206946bb7a2SPoul-Henning Kamp 			namelen--;
1207946bb7a2SPoul-Henning Kamp 			name++;
1208946bb7a2SPoul-Henning Kamp 			continue;
1209946bb7a2SPoul-Henning Kamp 		}
1210b85f65afSPedro F. Giffuni 		lsp2 = NULL;
1211d3f96f66SAlan Somers 		key.oid_number = *name;
1212d3f96f66SAlan Somers 		oid = RB_FIND(sysctl_oid_list, lsp, &key);
1213d3f96f66SAlan Somers 		if (oid) {
1214946bb7a2SPoul-Henning Kamp 			if (req->oldidx)
1215946bb7a2SPoul-Henning Kamp 				error = SYSCTL_OUT(req, ".", 1);
1216946bb7a2SPoul-Henning Kamp 			if (!error)
1217ce02431fSDoug Rabson 				error = SYSCTL_OUT(req, oid->oid_name,
1218ce02431fSDoug Rabson 					strlen(oid->oid_name));
1219946bb7a2SPoul-Henning Kamp 			if (error)
1220ccecef29SMatthew D Fleming 				goto out;
1221946bb7a2SPoul-Henning Kamp 
1222946bb7a2SPoul-Henning Kamp 			namelen--;
1223946bb7a2SPoul-Henning Kamp 			name++;
1224946bb7a2SPoul-Henning Kamp 
1225d3f96f66SAlan Somers 			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE &&
1226d3f96f66SAlan Somers 				!oid->oid_handler)
12275127ecb8SMatthew D Fleming 				lsp2 = SYSCTL_CHILDREN(oid);
1228946bb7a2SPoul-Henning Kamp 		}
12297f4173ccSPoul-Henning Kamp 		lsp = lsp2;
1230946bb7a2SPoul-Henning Kamp 	}
1231ccecef29SMatthew D Fleming 	error = SYSCTL_OUT(req, "", 1);
1232ccecef29SMatthew D Fleming  out:
12337665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
1234ccecef29SMatthew D Fleming 	return (error);
1235946bb7a2SPoul-Henning Kamp }
1236946bb7a2SPoul-Henning Kamp 
1237ff66f6a4SRobert Watson /*
1238ff66f6a4SRobert Watson  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1239ff66f6a4SRobert Watson  * capability mode.
1240ff66f6a4SRobert Watson  */
1241382e01c8SKonstantin Belousov static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1242382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1243946bb7a2SPoul-Henning Kamp 
12448db8bebfSAlexander V. Chernikov enum sysctl_iter_action {
12458db8bebfSAlexander V. Chernikov 	ITER_SIBLINGS,	/* Not matched, continue iterating siblings */
12468db8bebfSAlexander V. Chernikov 	ITER_CHILDREN,	/* Node has children we need to iterate over them */
12478db8bebfSAlexander V. Chernikov 	ITER_FOUND,	/* Matching node was found */
12488db8bebfSAlexander V. Chernikov };
1249946bb7a2SPoul-Henning Kamp 
12508db8bebfSAlexander V. Chernikov /*
12518db8bebfSAlexander V. Chernikov  * Tries to find the next node for @name and @namelen.
12528db8bebfSAlexander V. Chernikov  *
12538db8bebfSAlexander V. Chernikov  * Returns next action to take.
12548db8bebfSAlexander V. Chernikov  */
12558db8bebfSAlexander V. Chernikov static enum sysctl_iter_action
sysctl_sysctl_next_node(struct sysctl_oid * oidp,int * name,unsigned int namelen,bool honor_skip)12568db8bebfSAlexander V. Chernikov sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen,
12578db8bebfSAlexander V. Chernikov     bool honor_skip)
12588db8bebfSAlexander V. Chernikov {
1259946bb7a2SPoul-Henning Kamp 
126092e17803SRyan Moeller 	if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
12618db8bebfSAlexander V. Chernikov 		return (ITER_SIBLINGS);
126292e17803SRyan Moeller 
126392e17803SRyan Moeller 	if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
12648db8bebfSAlexander V. Chernikov 		return (ITER_SIBLINGS);
1265306e6b83SMaxime Henrion 
12660595c124SRyan Moeller 	if (namelen == 0) {
12670595c124SRyan Moeller 		/*
12680595c124SRyan Moeller 		 * We have reached a node with a full name match and are
12690595c124SRyan Moeller 		 * looking for the next oid in its children.
12700595c124SRyan Moeller 		 *
1271e58483c4SRyan Moeller 		 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1272e58483c4SRyan Moeller 		 *
12730595c124SRyan Moeller 		 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
12740595c124SRyan Moeller 		 * has a handler) and move on to the children.
12750595c124SRyan Moeller 		 */
1276e58483c4SRyan Moeller 		if (!honor_skip)
12778db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
1278ce02431fSDoug Rabson 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
12798db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
12808db8bebfSAlexander V. Chernikov 		/* If node does not have an iterator, treat it as leaf */
1281ce02431fSDoug Rabson 		if (oidp->oid_handler)
12828db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
12838db8bebfSAlexander V. Chernikov 
12848db8bebfSAlexander V. Chernikov 		/* Report oid as a node to iterate */
12858db8bebfSAlexander V. Chernikov 		return (ITER_CHILDREN);
1286946bb7a2SPoul-Henning Kamp 	}
1287946bb7a2SPoul-Henning Kamp 
12880595c124SRyan Moeller 	/*
12890595c124SRyan Moeller 	 * No match yet. Continue seeking the given name.
12900595c124SRyan Moeller 	 *
12910595c124SRyan Moeller 	 * We are iterating in order by oid_number, so skip oids lower
12920595c124SRyan Moeller 	 * than the one we are looking for.
12930595c124SRyan Moeller 	 *
12940595c124SRyan Moeller 	 * When the current oid_number is higher than the one we seek,
12950595c124SRyan Moeller 	 * that means we have reached the next oid in the sequence and
12960595c124SRyan Moeller 	 * should return it.
12970595c124SRyan Moeller 	 *
12980595c124SRyan Moeller 	 * If the oid_number matches the name at this level then we
12990595c124SRyan Moeller 	 * have to find a node to continue searching at the next level.
13000595c124SRyan Moeller 	 */
1301ce02431fSDoug Rabson 	if (oidp->oid_number < *name)
13028db8bebfSAlexander V. Chernikov 		return (ITER_SIBLINGS);
1303ce02431fSDoug Rabson 	if (oidp->oid_number > *name) {
13040595c124SRyan Moeller 		/*
13050595c124SRyan Moeller 		 * We have reached the next oid.
13060595c124SRyan Moeller 		 *
1307e58483c4SRyan Moeller 		 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1308e58483c4SRyan Moeller 		 *
13090595c124SRyan Moeller 		 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
13100595c124SRyan Moeller 		 * has a handler) and move on to the children.
13110595c124SRyan Moeller 		 */
1312e58483c4SRyan Moeller 		if (!honor_skip)
13138db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
1314ce02431fSDoug Rabson 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
13158db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
13168db8bebfSAlexander V. Chernikov 		/* If node does not have an iterator, treat it as leaf */
1317ce02431fSDoug Rabson 		if (oidp->oid_handler)
13188db8bebfSAlexander V. Chernikov 			return (ITER_FOUND);
13198db8bebfSAlexander V. Chernikov 		return (ITER_CHILDREN);
1320946bb7a2SPoul-Henning Kamp 	}
13218db8bebfSAlexander V. Chernikov 
13228db8bebfSAlexander V. Chernikov 	/* match at a current level */
1323ce02431fSDoug Rabson 	if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
13248db8bebfSAlexander V. Chernikov 		return (ITER_SIBLINGS);
1325ce02431fSDoug Rabson 	if (oidp->oid_handler)
13268db8bebfSAlexander V. Chernikov 		return (ITER_SIBLINGS);
13278db8bebfSAlexander V. Chernikov 
13288db8bebfSAlexander V. Chernikov 	return (ITER_CHILDREN);
13298db8bebfSAlexander V. Chernikov }
13308db8bebfSAlexander V. Chernikov 
13310595c124SRyan Moeller /*
13328db8bebfSAlexander V. Chernikov  * Recursively walk the sysctl subtree at lsp until we find the given name.
13338db8bebfSAlexander V. Chernikov  * Returns true and fills in next oid data in @next and @len if oid is found.
13340595c124SRyan Moeller  */
13358db8bebfSAlexander V. Chernikov static bool
sysctl_sysctl_next_action(struct sysctl_oid_list * lsp,int * name,u_int namelen,int * next,int * len,int level,bool honor_skip)13368db8bebfSAlexander V. Chernikov sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen,
13378db8bebfSAlexander V. Chernikov     int *next, int *len, int level, bool honor_skip)
13388db8bebfSAlexander V. Chernikov {
1339d3f96f66SAlan Somers 	struct sysctl_oid_list *next_lsp;
1340d3f96f66SAlan Somers 	struct sysctl_oid *oidp = NULL, key;
13418db8bebfSAlexander V. Chernikov 	bool success = false;
13428db8bebfSAlexander V. Chernikov 	enum sysctl_iter_action action;
13438db8bebfSAlexander V. Chernikov 
13448db8bebfSAlexander V. Chernikov 	SYSCTL_ASSERT_LOCKED();
1345d3f96f66SAlan Somers 	/*
1346d3f96f66SAlan Somers 	 * Start the search at the requested oid.  But if not found, then scan
1347d3f96f66SAlan Somers 	 * through all children.
1348d3f96f66SAlan Somers 	 */
1349d3f96f66SAlan Somers 	if (namelen > 0) {
1350d3f96f66SAlan Somers 		key.oid_number = *name;
1351d3f96f66SAlan Somers 		oidp = RB_FIND(sysctl_oid_list, lsp, &key);
1352d3f96f66SAlan Somers 	}
1353d3f96f66SAlan Somers 	if (!oidp)
1354d3f96f66SAlan Somers 		oidp = RB_MIN(sysctl_oid_list, lsp);
1355d3f96f66SAlan Somers 	for(; oidp != NULL; oidp = RB_NEXT(sysctl_oid_list, lsp, oidp)) {
1356d3f96f66SAlan Somers 		action = sysctl_sysctl_next_node(oidp, name, namelen,
1357d3f96f66SAlan Somers 		    honor_skip);
13588db8bebfSAlexander V. Chernikov 		if (action == ITER_SIBLINGS)
13598db8bebfSAlexander V. Chernikov 			continue;
13608db8bebfSAlexander V. Chernikov 		if (action == ITER_FOUND) {
13618db8bebfSAlexander V. Chernikov 			success = true;
13628db8bebfSAlexander V. Chernikov 			break;
13638db8bebfSAlexander V. Chernikov 		}
13648db8bebfSAlexander V. Chernikov 		KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action));
13658db8bebfSAlexander V. Chernikov 
1366d3f96f66SAlan Somers 		next_lsp = SYSCTL_CHILDREN(oidp);
13678db8bebfSAlexander V. Chernikov 		if (namelen == 0) {
1368d3f96f66SAlan Somers 			success = sysctl_sysctl_next_action(next_lsp, NULL, 0,
13698db8bebfSAlexander V. Chernikov 			    next + 1, len, level + 1, honor_skip);
13708db8bebfSAlexander V. Chernikov 		} else {
1371d3f96f66SAlan Somers 			success = sysctl_sysctl_next_action(next_lsp, name + 1,
1372d3f96f66SAlan Somers 			    namelen - 1, next + 1, len, level + 1, honor_skip);
13738db8bebfSAlexander V. Chernikov 			if (!success) {
13748db8bebfSAlexander V. Chernikov 
13758db8bebfSAlexander V. Chernikov 				/*
13768db8bebfSAlexander V. Chernikov 				 * We maintain the invariant that current node oid
13778db8bebfSAlexander V. Chernikov 				 * is >= the oid provided in @name.
13788db8bebfSAlexander V. Chernikov 				 * As there are no usable children at this node,
13798db8bebfSAlexander V. Chernikov 				 *  current node oid is strictly > than the requested
13808db8bebfSAlexander V. Chernikov 				 *  oid.
13818db8bebfSAlexander V. Chernikov 				 * Hence, reduce namelen to 0 to allow for picking first
13828db8bebfSAlexander V. Chernikov 				 *  nodes/leafs in the next node in list.
13838db8bebfSAlexander V. Chernikov 				 */
13848db8bebfSAlexander V. Chernikov 				namelen = 0;
13858db8bebfSAlexander V. Chernikov 			}
13868db8bebfSAlexander V. Chernikov 		}
13878db8bebfSAlexander V. Chernikov 		if (success)
13888db8bebfSAlexander V. Chernikov 			break;
13898db8bebfSAlexander V. Chernikov 	}
13908db8bebfSAlexander V. Chernikov 
13918db8bebfSAlexander V. Chernikov 	if (success) {
13928db8bebfSAlexander V. Chernikov 		*next = oidp->oid_number;
13938db8bebfSAlexander V. Chernikov 		if (level > *len)
1394946bb7a2SPoul-Henning Kamp 			*len = level;
1395946bb7a2SPoul-Henning Kamp 	}
13968db8bebfSAlexander V. Chernikov 
13978db8bebfSAlexander V. Chernikov 	return (success);
1398946bb7a2SPoul-Henning Kamp }
1399946bb7a2SPoul-Henning Kamp 
1400946bb7a2SPoul-Henning Kamp static int
sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)140182d9ae4eSPoul-Henning Kamp sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1402946bb7a2SPoul-Henning Kamp {
1403946bb7a2SPoul-Henning Kamp 	int *name = (int *) arg1;
1404946bb7a2SPoul-Henning Kamp 	u_int namelen = arg2;
140592e17803SRyan Moeller 	int len, error;
14068db8bebfSAlexander V. Chernikov 	bool success;
1407ce02431fSDoug Rabson 	struct sysctl_oid_list *lsp = &sysctl__children;
14087665e341SMateusz Guzik 	struct rm_priotracker tracker;
140992e17803SRyan Moeller 	int next[CTL_MAXNAME];
1410946bb7a2SPoul-Henning Kamp 
14118db8bebfSAlexander V. Chernikov 	len = 0;
14127665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
14138db8bebfSAlexander V. Chernikov 	success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1,
141492e17803SRyan Moeller 	    oidp->oid_number == CTL_SYSCTL_NEXT);
14157665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
14168db8bebfSAlexander V. Chernikov 	if (!success)
14178db8bebfSAlexander V. Chernikov 		return (ENOENT);
141892e17803SRyan Moeller 	error = SYSCTL_OUT(req, next, len * sizeof (int));
1419946bb7a2SPoul-Henning Kamp 	return (error);
1420946bb7a2SPoul-Henning Kamp }
1421946bb7a2SPoul-Henning Kamp 
1422ff66f6a4SRobert Watson /*
1423ff66f6a4SRobert Watson  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1424ff66f6a4SRobert Watson  * capability mode.
1425ff66f6a4SRobert Watson  */
1426382e01c8SKonstantin Belousov static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1427382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1428946bb7a2SPoul-Henning Kamp 
142992e17803SRyan Moeller static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
143092e17803SRyan Moeller     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
143192e17803SRyan Moeller 
1432946bb7a2SPoul-Henning Kamp static int
name2oid(const char * name,int * oid,int * len,struct sysctl_oid ** oidpp)1433f80babf9SAlexander Motin name2oid(const char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1434946bb7a2SPoul-Henning Kamp {
1435ce02431fSDoug Rabson 	struct sysctl_oid *oidp;
1436ce02431fSDoug Rabson 	struct sysctl_oid_list *lsp = &sysctl__children;
1437f80babf9SAlexander Motin 	const char *n;
1438946bb7a2SPoul-Henning Kamp 
1439efe0abddSMateusz Guzik 	SYSCTL_ASSERT_LOCKED();
1440ddf9d243SEd Schouten 
1441db9066f7SMarius Strobl 	for (*len = 0; *len < CTL_MAXNAME;) {
1442f80babf9SAlexander Motin 		n = strchrnul(name, '.');
1443f80babf9SAlexander Motin 		oidp = sysctl_find_oidnamelen(name, n - name, lsp);
1444d3f96f66SAlan Somers 		if (oidp == NULL)
1445d3f96f66SAlan Somers 			return (ENOENT);
1446ce02431fSDoug Rabson 		*oid++ = oidp->oid_number;
1447946bb7a2SPoul-Henning Kamp 		(*len)++;
1448946bb7a2SPoul-Henning Kamp 
1449f80babf9SAlexander Motin 		name = n;
1450f80babf9SAlexander Motin 		if (*name == '.')
1451f80babf9SAlexander Motin 			name++;
1452f80babf9SAlexander Motin 		if (*name == '\0') {
1453ce02431fSDoug Rabson 			if (oidpp)
1454ce02431fSDoug Rabson 				*oidpp = oidp;
1455946bb7a2SPoul-Henning Kamp 			return (0);
1456946bb7a2SPoul-Henning Kamp 		}
1457946bb7a2SPoul-Henning Kamp 
1458ce02431fSDoug Rabson 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1459946bb7a2SPoul-Henning Kamp 			break;
1460946bb7a2SPoul-Henning Kamp 
1461ce02431fSDoug Rabson 		if (oidp->oid_handler)
1462946bb7a2SPoul-Henning Kamp 			break;
1463946bb7a2SPoul-Henning Kamp 
14645127ecb8SMatthew D Fleming 		lsp = SYSCTL_CHILDREN(oidp);
1465946bb7a2SPoul-Henning Kamp 	}
146646003fb3SPawel Jakub Dawidek 	return (ENOENT);
1467946bb7a2SPoul-Henning Kamp }
1468946bb7a2SPoul-Henning Kamp 
1469946bb7a2SPoul-Henning Kamp static int
sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)147082d9ae4eSPoul-Henning Kamp sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1471946bb7a2SPoul-Henning Kamp {
1472946bb7a2SPoul-Henning Kamp 	char *p;
1473dd6312a7SMatthew D Fleming 	int error, oid[CTL_MAXNAME], len = 0;
1474b85f65afSPedro F. Giffuni 	struct sysctl_oid *op = NULL;
14757665e341SMateusz Guzik 	struct rm_priotracker tracker;
14768b9817a4SMateusz Guzik 	char buf[32];
1477946bb7a2SPoul-Henning Kamp 
1478946bb7a2SPoul-Henning Kamp 	if (!req->newlen)
147946003fb3SPawel Jakub Dawidek 		return (ENOENT);
14806a5d592aSPoul-Henning Kamp 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
14816a5d592aSPoul-Henning Kamp 		return (ENAMETOOLONG);
1482946bb7a2SPoul-Henning Kamp 
14838b9817a4SMateusz Guzik 	p = buf;
14848b9817a4SMateusz Guzik 	if (req->newlen >= sizeof(buf))
1485a163d034SWarner Losh 		p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1486946bb7a2SPoul-Henning Kamp 
1487946bb7a2SPoul-Henning Kamp 	error = SYSCTL_IN(req, p, req->newlen);
1488946bb7a2SPoul-Henning Kamp 	if (error) {
14898b9817a4SMateusz Guzik 		if (p != buf)
1490946bb7a2SPoul-Henning Kamp 			free(p, M_SYSCTL);
1491946bb7a2SPoul-Henning Kamp 		return (error);
1492946bb7a2SPoul-Henning Kamp 	}
1493946bb7a2SPoul-Henning Kamp 
1494946bb7a2SPoul-Henning Kamp 	p [req->newlen] = '\0';
1495946bb7a2SPoul-Henning Kamp 
14967665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
1497946bb7a2SPoul-Henning Kamp 	error = name2oid(p, oid, &len, &op);
14987665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
1499946bb7a2SPoul-Henning Kamp 
15008b9817a4SMateusz Guzik 	if (p != buf)
1501946bb7a2SPoul-Henning Kamp 		free(p, M_SYSCTL);
1502946bb7a2SPoul-Henning Kamp 
1503946bb7a2SPoul-Henning Kamp 	if (error)
1504946bb7a2SPoul-Henning Kamp 		return (error);
1505946bb7a2SPoul-Henning Kamp 
1506946bb7a2SPoul-Henning Kamp 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1507946bb7a2SPoul-Henning Kamp 	return (error);
1508946bb7a2SPoul-Henning Kamp }
1509946bb7a2SPoul-Henning Kamp 
1510ff66f6a4SRobert Watson /*
1511ff66f6a4SRobert Watson  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1512ff66f6a4SRobert Watson  * capability mode.
1513ff66f6a4SRobert Watson  */
1514382e01c8SKonstantin Belousov SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1515382e01c8SKonstantin Belousov     CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1516382e01c8SKonstantin Belousov     sysctl_sysctl_name2oid, "I", "");
1517946bb7a2SPoul-Henning Kamp 
1518946bb7a2SPoul-Henning Kamp static int
sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)151982d9ae4eSPoul-Henning Kamp sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1520946bb7a2SPoul-Henning Kamp {
1521ce02431fSDoug Rabson 	struct sysctl_oid *oid;
15227665e341SMateusz Guzik 	struct rm_priotracker tracker;
1523226420a4SBrian Feldman 	int error;
1524946bb7a2SPoul-Henning Kamp 
152543cefe8bSRyan Libby 	error = sysctl_wire_old_buffer(req, 0);
152643cefe8bSRyan Libby 	if (error)
152743cefe8bSRyan Libby 		return (error);
152843cefe8bSRyan Libby 
15297665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
1530226420a4SBrian Feldman 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1531226420a4SBrian Feldman 	if (error)
1532ccecef29SMatthew D Fleming 		goto out;
1533946bb7a2SPoul-Henning Kamp 
1534ccecef29SMatthew D Fleming 	if (oid->oid_fmt == NULL) {
1535ccecef29SMatthew D Fleming 		error = ENOENT;
1536ccecef29SMatthew D Fleming 		goto out;
1537ccecef29SMatthew D Fleming 	}
1538226420a4SBrian Feldman 	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1539226420a4SBrian Feldman 	if (error)
1540ccecef29SMatthew D Fleming 		goto out;
1541226420a4SBrian Feldman 	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1542ccecef29SMatthew D Fleming  out:
15437665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
154465d0bc13SPoul-Henning Kamp 	return (error);
1545946bb7a2SPoul-Henning Kamp }
1546946bb7a2SPoul-Henning Kamp 
1547382e01c8SKonstantin Belousov static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1548382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1549946bb7a2SPoul-Henning Kamp 
15506105f815SLuigi Rizzo static int
sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)15516105f815SLuigi Rizzo sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
15526105f815SLuigi Rizzo {
15536105f815SLuigi Rizzo 	struct sysctl_oid *oid;
15547665e341SMateusz Guzik 	struct rm_priotracker tracker;
15556105f815SLuigi Rizzo 	int error;
15566105f815SLuigi Rizzo 
155743cefe8bSRyan Libby 	error = sysctl_wire_old_buffer(req, 0);
155843cefe8bSRyan Libby 	if (error)
155943cefe8bSRyan Libby 		return (error);
156043cefe8bSRyan Libby 
15617665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
15626105f815SLuigi Rizzo 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
15636105f815SLuigi Rizzo 	if (error)
1564ccecef29SMatthew D Fleming 		goto out;
15656105f815SLuigi Rizzo 
1566ccecef29SMatthew D Fleming 	if (oid->oid_descr == NULL) {
1567ccecef29SMatthew D Fleming 		error = ENOENT;
1568ccecef29SMatthew D Fleming 		goto out;
1569ccecef29SMatthew D Fleming 	}
15705937226dSPoul-Henning Kamp 	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1571ccecef29SMatthew D Fleming  out:
15727665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
15736105f815SLuigi Rizzo 	return (error);
15746105f815SLuigi Rizzo }
15756105f815SLuigi Rizzo 
1576382e01c8SKonstantin Belousov static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1577382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
15786105f815SLuigi Rizzo 
15791e1f3941SEd Schouten static int
sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)15801e1f3941SEd Schouten sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
15811e1f3941SEd Schouten {
15821e1f3941SEd Schouten 	struct sysctl_oid *oid;
15831e1f3941SEd Schouten 	struct rm_priotracker tracker;
15841e1f3941SEd Schouten 	int error;
15851e1f3941SEd Schouten 
158643cefe8bSRyan Libby 	error = sysctl_wire_old_buffer(req, 0);
158743cefe8bSRyan Libby 	if (error)
158843cefe8bSRyan Libby 		return (error);
158943cefe8bSRyan Libby 
15901e1f3941SEd Schouten 	SYSCTL_RLOCK(&tracker);
15911e1f3941SEd Schouten 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
15921e1f3941SEd Schouten 	if (error)
15931e1f3941SEd Schouten 		goto out;
15941e1f3941SEd Schouten 
15951e1f3941SEd Schouten 	if (oid->oid_label == NULL) {
15961e1f3941SEd Schouten 		error = ENOENT;
15971e1f3941SEd Schouten 		goto out;
15981e1f3941SEd Schouten 	}
15991e1f3941SEd Schouten 	error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
16001e1f3941SEd Schouten  out:
16011e1f3941SEd Schouten 	SYSCTL_RUNLOCK(&tracker);
16021e1f3941SEd Schouten 	return (error);
16031e1f3941SEd Schouten }
16041e1f3941SEd Schouten 
1605382e01c8SKonstantin Belousov static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1606382e01c8SKonstantin Belousov     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
16071e1f3941SEd Schouten 
1608946bb7a2SPoul-Henning Kamp /*
1609946bb7a2SPoul-Henning Kamp  * Default "handler" functions.
1610946bb7a2SPoul-Henning Kamp  */
16112e210993SPoul-Henning Kamp 
1612ae0eb976SPoul-Henning Kamp /*
161384e717c4SHans Petter Selasky  * Handle a bool.
161484e717c4SHans Petter Selasky  * Two cases:
161584e717c4SHans Petter Selasky  *     a variable:  point arg1 at it.
161684e717c4SHans Petter Selasky  *     a constant:  pass it in arg2.
161784e717c4SHans Petter Selasky  */
161884e717c4SHans Petter Selasky 
161984e717c4SHans Petter Selasky int
sysctl_handle_bool(SYSCTL_HANDLER_ARGS)162084e717c4SHans Petter Selasky sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
162184e717c4SHans Petter Selasky {
162284e717c4SHans Petter Selasky 	uint8_t temp;
162384e717c4SHans Petter Selasky 	int error;
162484e717c4SHans Petter Selasky 
162584e717c4SHans Petter Selasky 	/*
162684e717c4SHans Petter Selasky 	 * Attempt to get a coherent snapshot by making a copy of the data.
162784e717c4SHans Petter Selasky 	 */
162884e717c4SHans Petter Selasky 	if (arg1)
162984e717c4SHans Petter Selasky 		temp = *(bool *)arg1 ? 1 : 0;
163084e717c4SHans Petter Selasky 	else
163184e717c4SHans Petter Selasky 		temp = arg2 ? 1 : 0;
163284e717c4SHans Petter Selasky 
163384e717c4SHans Petter Selasky 	error = SYSCTL_OUT(req, &temp, sizeof(temp));
163484e717c4SHans Petter Selasky 	if (error || !req->newptr)
163584e717c4SHans Petter Selasky 		return (error);
163684e717c4SHans Petter Selasky 
163784e717c4SHans Petter Selasky 	if (!arg1)
163884e717c4SHans Petter Selasky 		error = EPERM;
163984e717c4SHans Petter Selasky 	else {
164084e717c4SHans Petter Selasky 		error = SYSCTL_IN(req, &temp, sizeof(temp));
164184e717c4SHans Petter Selasky 		if (!error)
164284e717c4SHans Petter Selasky 			*(bool *)arg1 = temp ? 1 : 0;
164384e717c4SHans Petter Selasky 	}
164484e717c4SHans Petter Selasky 	return (error);
164584e717c4SHans Petter Selasky }
164684e717c4SHans Petter Selasky 
164784e717c4SHans Petter Selasky /*
1648c3220d0bSConrad Meyer  * Handle an int8_t, signed or unsigned.
1649c3220d0bSConrad Meyer  * Two cases:
1650c3220d0bSConrad Meyer  *     a variable:  point arg1 at it.
1651c3220d0bSConrad Meyer  *     a constant:  pass it in arg2.
1652c3220d0bSConrad Meyer  */
1653c3220d0bSConrad Meyer 
1654c3220d0bSConrad Meyer int
sysctl_handle_8(SYSCTL_HANDLER_ARGS)1655c3220d0bSConrad Meyer sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1656c3220d0bSConrad Meyer {
1657c3220d0bSConrad Meyer 	int8_t tmpout;
1658c3220d0bSConrad Meyer 	int error = 0;
1659c3220d0bSConrad Meyer 
1660c3220d0bSConrad Meyer 	/*
1661c3220d0bSConrad Meyer 	 * Attempt to get a coherent snapshot by making a copy of the data.
1662c3220d0bSConrad Meyer 	 */
1663c3220d0bSConrad Meyer 	if (arg1)
1664c3220d0bSConrad Meyer 		tmpout = *(int8_t *)arg1;
1665c3220d0bSConrad Meyer 	else
1666c3220d0bSConrad Meyer 		tmpout = arg2;
1667c3220d0bSConrad Meyer 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1668c3220d0bSConrad Meyer 
1669c3220d0bSConrad Meyer 	if (error || !req->newptr)
1670c3220d0bSConrad Meyer 		return (error);
1671c3220d0bSConrad Meyer 
1672c3220d0bSConrad Meyer 	if (!arg1)
1673c3220d0bSConrad Meyer 		error = EPERM;
1674c3220d0bSConrad Meyer 	else
1675c3220d0bSConrad Meyer 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1676c3220d0bSConrad Meyer 	return (error);
1677c3220d0bSConrad Meyer }
1678c3220d0bSConrad Meyer 
1679c3220d0bSConrad Meyer /*
1680c3220d0bSConrad Meyer  * Handle an int16_t, signed or unsigned.
1681c3220d0bSConrad Meyer  * Two cases:
1682c3220d0bSConrad Meyer  *     a variable:  point arg1 at it.
1683c3220d0bSConrad Meyer  *     a constant:  pass it in arg2.
1684c3220d0bSConrad Meyer  */
1685c3220d0bSConrad Meyer 
1686c3220d0bSConrad Meyer int
sysctl_handle_16(SYSCTL_HANDLER_ARGS)1687c3220d0bSConrad Meyer sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1688c3220d0bSConrad Meyer {
1689c3220d0bSConrad Meyer 	int16_t tmpout;
1690c3220d0bSConrad Meyer 	int error = 0;
1691c3220d0bSConrad Meyer 
1692c3220d0bSConrad Meyer 	/*
1693c3220d0bSConrad Meyer 	 * Attempt to get a coherent snapshot by making a copy of the data.
1694c3220d0bSConrad Meyer 	 */
1695c3220d0bSConrad Meyer 	if (arg1)
1696c3220d0bSConrad Meyer 		tmpout = *(int16_t *)arg1;
1697c3220d0bSConrad Meyer 	else
1698c3220d0bSConrad Meyer 		tmpout = arg2;
1699c3220d0bSConrad Meyer 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1700c3220d0bSConrad Meyer 
1701c3220d0bSConrad Meyer 	if (error || !req->newptr)
1702c3220d0bSConrad Meyer 		return (error);
1703c3220d0bSConrad Meyer 
1704c3220d0bSConrad Meyer 	if (!arg1)
1705c3220d0bSConrad Meyer 		error = EPERM;
1706c3220d0bSConrad Meyer 	else
1707c3220d0bSConrad Meyer 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1708c3220d0bSConrad Meyer 	return (error);
1709c3220d0bSConrad Meyer }
1710c3220d0bSConrad Meyer 
1711c3220d0bSConrad Meyer /*
1712be87839eSConrad Meyer  * Handle an int32_t, signed or unsigned.
1713be87839eSConrad Meyer  * Two cases:
1714be87839eSConrad Meyer  *     a variable:  point arg1 at it.
1715be87839eSConrad Meyer  *     a constant:  pass it in arg2.
1716be87839eSConrad Meyer  */
1717be87839eSConrad Meyer 
1718be87839eSConrad Meyer int
sysctl_handle_32(SYSCTL_HANDLER_ARGS)1719be87839eSConrad Meyer sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1720be87839eSConrad Meyer {
1721be87839eSConrad Meyer 	int32_t tmpout;
1722be87839eSConrad Meyer 	int error = 0;
1723be87839eSConrad Meyer 
1724be87839eSConrad Meyer 	/*
1725be87839eSConrad Meyer 	 * Attempt to get a coherent snapshot by making a copy of the data.
1726be87839eSConrad Meyer 	 */
1727be87839eSConrad Meyer 	if (arg1)
1728be87839eSConrad Meyer 		tmpout = *(int32_t *)arg1;
1729be87839eSConrad Meyer 	else
1730be87839eSConrad Meyer 		tmpout = arg2;
1731be87839eSConrad Meyer 	error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1732be87839eSConrad Meyer 
1733be87839eSConrad Meyer 	if (error || !req->newptr)
1734be87839eSConrad Meyer 		return (error);
1735be87839eSConrad Meyer 
1736be87839eSConrad Meyer 	if (!arg1)
1737be87839eSConrad Meyer 		error = EPERM;
1738be87839eSConrad Meyer 	else
1739be87839eSConrad Meyer 		error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1740be87839eSConrad Meyer 	return (error);
1741be87839eSConrad Meyer }
1742be87839eSConrad Meyer 
1743be87839eSConrad Meyer /*
1744486bddb0SDoug Rabson  * Handle an int, signed or unsigned.
1745ae0eb976SPoul-Henning Kamp  * Two cases:
1746ae0eb976SPoul-Henning Kamp  *     a variable:  point arg1 at it.
1747ae0eb976SPoul-Henning Kamp  *     a constant:  pass it in arg2.
1748ae0eb976SPoul-Henning Kamp  */
1749ae0eb976SPoul-Henning Kamp 
17503a34a5c3SPoul-Henning Kamp int
sysctl_handle_int(SYSCTL_HANDLER_ARGS)175182d9ae4eSPoul-Henning Kamp sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1752b396cd83SPoul-Henning Kamp {
17539e74cba3SDon Lewis 	int tmpout, error = 0;
1754b396cd83SPoul-Henning Kamp 
17559e74cba3SDon Lewis 	/*
17569e74cba3SDon Lewis 	 * Attempt to get a coherent snapshot by making a copy of the data.
17579e74cba3SDon Lewis 	 */
1758ae0eb976SPoul-Henning Kamp 	if (arg1)
17599e74cba3SDon Lewis 		tmpout = *(int *)arg1;
17601fbf1f71SBruce Evans 	else
17619e74cba3SDon Lewis 		tmpout = arg2;
17629e74cba3SDon Lewis 	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1763b396cd83SPoul-Henning Kamp 
1764ae0eb976SPoul-Henning Kamp 	if (error || !req->newptr)
1765ae0eb976SPoul-Henning Kamp 		return (error);
1766b396cd83SPoul-Henning Kamp 
1767ae0eb976SPoul-Henning Kamp 	if (!arg1)
1768ae0eb976SPoul-Henning Kamp 		error = EPERM;
1769ae0eb976SPoul-Henning Kamp 	else
1770ae0eb976SPoul-Henning Kamp 		error = SYSCTL_IN(req, arg1, sizeof(int));
1771ae0eb976SPoul-Henning Kamp 	return (error);
1772b396cd83SPoul-Henning Kamp }
1773b396cd83SPoul-Henning Kamp 
1774a4684d74SAndre Oppermann /*
1775105e397eSGordon Bergling  * Based on sysctl_handle_int() convert milliseconds into ticks.
1776eddfbb76SRobert Watson  * Note: this is used by TCP.
1777a4684d74SAndre Oppermann  */
1778a4684d74SAndre Oppermann 
1779a4684d74SAndre Oppermann int
sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)1780a4684d74SAndre Oppermann sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1781a4684d74SAndre Oppermann {
1782a4684d74SAndre Oppermann 	int error, s, tt;
1783a4684d74SAndre Oppermann 
1784f6dfe47aSMarko Zec 	tt = *(int *)arg1;
1785a4684d74SAndre Oppermann 	s = (int)((int64_t)tt * 1000 / hz);
1786a4684d74SAndre Oppermann 
1787a4684d74SAndre Oppermann 	error = sysctl_handle_int(oidp, &s, 0, req);
1788a4684d74SAndre Oppermann 	if (error || !req->newptr)
1789a4684d74SAndre Oppermann 		return (error);
1790a4684d74SAndre Oppermann 
1791a4684d74SAndre Oppermann 	tt = (int)((int64_t)s * hz / 1000);
1792a4684d74SAndre Oppermann 	if (tt < 1)
1793a4684d74SAndre Oppermann 		return (EINVAL);
1794a4684d74SAndre Oppermann 
1795f6dfe47aSMarko Zec 	*(int *)arg1 = tt;
1796a4684d74SAndre Oppermann 	return (0);
1797a4684d74SAndre Oppermann }
1798a4684d74SAndre Oppermann 
1799ae0eb976SPoul-Henning Kamp /*
180018716f9fSMarius Strobl  * Handle a long, signed or unsigned.
180118716f9fSMarius Strobl  * Two cases:
180218716f9fSMarius Strobl  *     a variable:  point arg1 at it.
180318716f9fSMarius Strobl  *     a constant:  pass it in arg2.
1804069e9bc1SDoug Rabson  */
1805069e9bc1SDoug Rabson 
1806069e9bc1SDoug Rabson int
sysctl_handle_long(SYSCTL_HANDLER_ARGS)180782d9ae4eSPoul-Henning Kamp sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1808069e9bc1SDoug Rabson {
1809069e9bc1SDoug Rabson 	int error = 0;
1810a7bc3102SPeter Wemm 	long tmplong;
1811a7bc3102SPeter Wemm #ifdef SCTL_MASK32
1812a7bc3102SPeter Wemm 	int tmpint;
1813a7bc3102SPeter Wemm #endif
1814069e9bc1SDoug Rabson 
18159e74cba3SDon Lewis 	/*
18169e74cba3SDon Lewis 	 * Attempt to get a coherent snapshot by making a copy of the data.
18179e74cba3SDon Lewis 	 */
1818bdc5f017SMarius Strobl 	if (arg1)
1819a7bc3102SPeter Wemm 		tmplong = *(long *)arg1;
1820bdc5f017SMarius Strobl 	else
1821bdc5f017SMarius Strobl 		tmplong = arg2;
1822a7bc3102SPeter Wemm #ifdef SCTL_MASK32
1823a7bc3102SPeter Wemm 	if (req->flags & SCTL_MASK32) {
1824a7bc3102SPeter Wemm 		tmpint = tmplong;
1825a7bc3102SPeter Wemm 		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1826a7bc3102SPeter Wemm 	} else
1827a7bc3102SPeter Wemm #endif
1828a7bc3102SPeter Wemm 		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1829069e9bc1SDoug Rabson 
1830069e9bc1SDoug Rabson 	if (error || !req->newptr)
1831069e9bc1SDoug Rabson 		return (error);
1832069e9bc1SDoug Rabson 
1833bdc5f017SMarius Strobl 	if (!arg1)
1834bdc5f017SMarius Strobl 		error = EPERM;
1835a7bc3102SPeter Wemm #ifdef SCTL_MASK32
1836bdc5f017SMarius Strobl 	else if (req->flags & SCTL_MASK32) {
1837a7bc3102SPeter Wemm 		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1838a7bc3102SPeter Wemm 		*(long *)arg1 = (long)tmpint;
1839bdc5f017SMarius Strobl 	}
1840a7bc3102SPeter Wemm #endif
1841bdc5f017SMarius Strobl 	else
1842069e9bc1SDoug Rabson 		error = SYSCTL_IN(req, arg1, sizeof(long));
1843069e9bc1SDoug Rabson 	return (error);
1844069e9bc1SDoug Rabson }
1845069e9bc1SDoug Rabson 
1846069e9bc1SDoug Rabson /*
184718716f9fSMarius Strobl  * Handle a 64 bit int, signed or unsigned.
184818716f9fSMarius Strobl  * Two cases:
184918716f9fSMarius Strobl  *     a variable:  point arg1 at it.
185018716f9fSMarius Strobl  *     a constant:  pass it in arg2.
1851df82ff50SDavid Malone  */
1852df82ff50SDavid Malone int
sysctl_handle_64(SYSCTL_HANDLER_ARGS)1853cbc134adSMatthew D Fleming sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1854df82ff50SDavid Malone {
1855df82ff50SDavid Malone 	int error = 0;
1856df82ff50SDavid Malone 	uint64_t tmpout;
1857df82ff50SDavid Malone 
1858df82ff50SDavid Malone 	/*
1859df82ff50SDavid Malone 	 * Attempt to get a coherent snapshot by making a copy of the data.
1860df82ff50SDavid Malone 	 */
1861bdc5f017SMarius Strobl 	if (arg1)
1862df82ff50SDavid Malone 		tmpout = *(uint64_t *)arg1;
1863bdc5f017SMarius Strobl 	else
1864bdc5f017SMarius Strobl 		tmpout = arg2;
1865df82ff50SDavid Malone 	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1866df82ff50SDavid Malone 
1867df82ff50SDavid Malone 	if (error || !req->newptr)
1868df82ff50SDavid Malone 		return (error);
1869df82ff50SDavid Malone 
1870bdc5f017SMarius Strobl 	if (!arg1)
1871bdc5f017SMarius Strobl 		error = EPERM;
1872bdc5f017SMarius Strobl 	else
1873df82ff50SDavid Malone 		error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1874df82ff50SDavid Malone 	return (error);
1875df82ff50SDavid Malone }
1876df82ff50SDavid Malone 
1877df82ff50SDavid Malone /*
1878ae0eb976SPoul-Henning Kamp  * Handle our generic '\0' terminated 'C' string.
1879ae0eb976SPoul-Henning Kamp  * Two cases:
1880ae0eb976SPoul-Henning Kamp  * 	a variable string:  point arg1 at it, arg2 is max length.
1881ae0eb976SPoul-Henning Kamp  * 	a constant string:  point arg1 at it, arg2 is zero.
1882ae0eb976SPoul-Henning Kamp  */
1883ae0eb976SPoul-Henning Kamp 
18843a34a5c3SPoul-Henning Kamp int
sysctl_handle_string(SYSCTL_HANDLER_ARGS)188582d9ae4eSPoul-Henning Kamp sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1886b396cd83SPoul-Henning Kamp {
1887210176adSPawel Biernacki 	char *tmparg;
188819512355SZhenlei Huang 	int error = 0;
1889e80fb434SRobert Drehmel 
18906a3287f8SHans Petter Selasky 	/*
18911627b1fdSPawel Biernacki 	 * If the sysctl isn't writable and isn't a preallocated tunable that
18921627b1fdSPawel Biernacki 	 * can be modified by kenv(2), microoptimise and treat it as a
18931627b1fdSPawel Biernacki 	 * read-only string.
18946a3287f8SHans Petter Selasky 	 * A zero-length buffer indicates a fixed size read-only
1895088c17b4SConrad Meyer 	 * string.  In ddb, don't worry about trying to make a malloced
1896088c17b4SConrad Meyer 	 * snapshot.
18976a3287f8SHans Petter Selasky 	 */
189819112958SBrooks Davis 	if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
189919112958SBrooks Davis 	    arg2 == 0 || kdb_active) {
190019512355SZhenlei Huang 		size_t outlen;
190119512355SZhenlei Huang 
190219512355SZhenlei Huang 		if (arg2 == 0)
190319512355SZhenlei Huang 			outlen = arg2 = strlen(arg1) + 1;
190419512355SZhenlei Huang 		else
190519512355SZhenlei Huang 			outlen = strnlen(arg1, arg2 - 1) + 1;
190619512355SZhenlei Huang 
190719512355SZhenlei Huang 		tmparg = req->oldptr != NULL ? arg1 : NULL;
190819512355SZhenlei Huang 		error = SYSCTL_OUT(req, tmparg, outlen);
190919512355SZhenlei Huang 	} else {
191019512355SZhenlei Huang 		size_t outlen;
19113da1cf1eSHans Petter Selasky 
1912af3b2549SHans Petter Selasky 		if (req->oldptr != NULL) {
1913af3b2549SHans Petter Selasky 			tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1914210176adSPawel Biernacki 			sx_slock(&sysctlstringlock);
1915af3b2549SHans Petter Selasky 			memcpy(tmparg, arg1, arg2);
1916210176adSPawel Biernacki 			sx_sunlock(&sysctlstringlock);
191719512355SZhenlei Huang 			outlen = strnlen(tmparg, arg2 - 1) + 1;
1918af3b2549SHans Petter Selasky 		} else {
191919512355SZhenlei Huang 			tmparg = NULL;
1920210176adSPawel Biernacki 			sx_slock(&sysctlstringlock);
192119512355SZhenlei Huang 			outlen = strnlen(arg1, arg2 - 1) + 1;
1922210176adSPawel Biernacki 			sx_sunlock(&sysctlstringlock);
192319512355SZhenlei Huang 		}
192419512355SZhenlei Huang 		error = SYSCTL_OUT(req, tmparg, outlen);
192519512355SZhenlei Huang 		free(tmparg, M_SYSCTLTMP);
1926af3b2549SHans Petter Selasky 	}
19276a5d592aSPoul-Henning Kamp 	if (error || !req->newptr)
1928ae0eb976SPoul-Henning Kamp 		return (error);
1929ae0eb976SPoul-Henning Kamp 
1930210176adSPawel Biernacki 	if (req->newlen - req->newidx >= arg2 ||
193137d4ece7SLi-Wen Hsu 	    req->newlen - req->newidx < 0) {
19326a5d592aSPoul-Henning Kamp 		error = EINVAL;
193337d4ece7SLi-Wen Hsu 	} else if (req->newlen - req->newidx == 0) {
193437d4ece7SLi-Wen Hsu 		sx_xlock(&sysctlstringlock);
193537d4ece7SLi-Wen Hsu 		((char *)arg1)[0] = '\0';
193637d4ece7SLi-Wen Hsu 		sx_xunlock(&sysctlstringlock);
19374342ba18SKonstantin Belousov 	} else if (req->newfunc == sysctl_new_kernel) {
19384342ba18SKonstantin Belousov 		arg2 = req->newlen - req->newidx;
19394342ba18SKonstantin Belousov 		sx_xlock(&sysctlstringlock);
19404342ba18SKonstantin Belousov 		error = SYSCTL_IN(req, arg1, arg2);
19414342ba18SKonstantin Belousov 		if (error == 0) {
19424342ba18SKonstantin Belousov 			((char *)arg1)[arg2] = '\0';
19434342ba18SKonstantin Belousov 			req->newidx += arg2;
19444342ba18SKonstantin Belousov 		}
19454342ba18SKonstantin Belousov 		sx_xunlock(&sysctlstringlock);
1946ae0eb976SPoul-Henning Kamp 	} else {
1947210176adSPawel Biernacki 		arg2 = req->newlen - req->newidx;
1948210176adSPawel Biernacki 		tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1949210176adSPawel Biernacki 
19501627b1fdSPawel Biernacki 		error = SYSCTL_IN(req, tmparg, arg2);
1951210176adSPawel Biernacki 		if (error) {
1952210176adSPawel Biernacki 			free(tmparg, M_SYSCTLTMP);
1953210176adSPawel Biernacki 			return (error);
1954210176adSPawel Biernacki 		}
1955210176adSPawel Biernacki 
1956210176adSPawel Biernacki 		sx_xlock(&sysctlstringlock);
1957210176adSPawel Biernacki 		memcpy(arg1, tmparg, arg2);
1958ae0eb976SPoul-Henning Kamp 		((char *)arg1)[arg2] = '\0';
1959210176adSPawel Biernacki 		sx_xunlock(&sysctlstringlock);
1960210176adSPawel Biernacki 		free(tmparg, M_SYSCTLTMP);
1961210176adSPawel Biernacki 		req->newidx += arg2;
1962b396cd83SPoul-Henning Kamp 	}
19632e210993SPoul-Henning Kamp 	return (error);
1964b396cd83SPoul-Henning Kamp }
1965b396cd83SPoul-Henning Kamp 
1966ae0eb976SPoul-Henning Kamp /*
1967ae0eb976SPoul-Henning Kamp  * Handle any kind of opaque data.
1968ae0eb976SPoul-Henning Kamp  * arg1 points to it, arg2 is the size.
1969ae0eb976SPoul-Henning Kamp  */
1970ae0eb976SPoul-Henning Kamp 
19713a34a5c3SPoul-Henning Kamp int
sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)197282d9ae4eSPoul-Henning Kamp sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1973b396cd83SPoul-Henning Kamp {
1974dcf59a59SBruce M Simpson 	int error, tries;
1975dcf59a59SBruce M Simpson 	u_int generation;
1976f0597024SBruce M Simpson 	struct sysctl_req req2;
1977b396cd83SPoul-Henning Kamp 
19789e74cba3SDon Lewis 	/*
1979dcf59a59SBruce M Simpson 	 * Attempt to get a coherent snapshot, by using the thread
1980dcf59a59SBruce M Simpson 	 * pre-emption counter updated from within mi_switch() to
1981dcf59a59SBruce M Simpson 	 * determine if we were pre-empted during a bcopy() or
1982dcf59a59SBruce M Simpson 	 * copyout(). Make 3 attempts at doing this before giving up.
1983dcf59a59SBruce M Simpson 	 * If we encounter an error, stop immediately.
19849e74cba3SDon Lewis 	 */
1985dcf59a59SBruce M Simpson 	tries = 0;
1986f0597024SBruce M Simpson 	req2 = *req;
1987f0597024SBruce M Simpson retry:
1988dcf59a59SBruce M Simpson 	generation = curthread->td_generation;
1989ae0eb976SPoul-Henning Kamp 	error = SYSCTL_OUT(req, arg1, arg2);
1990dcf59a59SBruce M Simpson 	if (error)
1991ae0eb976SPoul-Henning Kamp 		return (error);
1992dcf59a59SBruce M Simpson 	tries++;
1993f0597024SBruce M Simpson 	if (generation != curthread->td_generation && tries < 3) {
1994f0597024SBruce M Simpson 		*req = req2;
1995f0597024SBruce M Simpson 		goto retry;
1996f0597024SBruce M Simpson 	}
1997ae0eb976SPoul-Henning Kamp 
1998ae0eb976SPoul-Henning Kamp 	error = SYSCTL_IN(req, arg1, arg2);
1999ae0eb976SPoul-Henning Kamp 
2000ae0eb976SPoul-Henning Kamp 	return (error);
2001b396cd83SPoul-Henning Kamp }
2002ae0eb976SPoul-Henning Kamp 
2003deae269aSPoul-Henning Kamp /*
2004105e397eSGordon Bergling  * Based on sysctl_handle_64() convert microseconds to a sbintime.
2005003ffd57SWarner Losh  */
2006003ffd57SWarner Losh int
sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)2007003ffd57SWarner Losh sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
2008003ffd57SWarner Losh {
2009003ffd57SWarner Losh 	int error;
2010f394d9c0SGleb Smirnoff 	int64_t usec;
2011003ffd57SWarner Losh 
2012f394d9c0SGleb Smirnoff 	usec = sbttous(*(sbintime_t *)arg1);
2013003ffd57SWarner Losh 
2014f394d9c0SGleb Smirnoff 	error = sysctl_handle_64(oidp, &usec, 0, req);
2015003ffd57SWarner Losh 	if (error || !req->newptr)
2016003ffd57SWarner Losh 		return (error);
2017003ffd57SWarner Losh 
2018f394d9c0SGleb Smirnoff 	*(sbintime_t *)arg1 = ustosbt(usec);
2019003ffd57SWarner Losh 
2020003ffd57SWarner Losh 	return (0);
2021003ffd57SWarner Losh }
2022003ffd57SWarner Losh 
2023003ffd57SWarner Losh /*
2024105e397eSGordon Bergling  * Based on sysctl_handle_64() convert milliseconds to a sbintime.
2025003ffd57SWarner Losh  */
2026003ffd57SWarner Losh int
sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)2027003ffd57SWarner Losh sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
2028003ffd57SWarner Losh {
2029003ffd57SWarner Losh 	int error;
2030f394d9c0SGleb Smirnoff 	int64_t msec;
2031003ffd57SWarner Losh 
2032f394d9c0SGleb Smirnoff 	msec = sbttoms(*(sbintime_t *)arg1);
2033003ffd57SWarner Losh 
2034f394d9c0SGleb Smirnoff 	error = sysctl_handle_64(oidp, &msec, 0, req);
2035003ffd57SWarner Losh 	if (error || !req->newptr)
2036003ffd57SWarner Losh 		return (error);
2037003ffd57SWarner Losh 
2038f394d9c0SGleb Smirnoff 	*(sbintime_t *)arg1 = mstosbt(msec);
2039003ffd57SWarner Losh 
2040003ffd57SWarner Losh 	return (0);
2041003ffd57SWarner Losh }
2042003ffd57SWarner Losh 
20430f702183SJohn Baldwin /*
20440f702183SJohn Baldwin  * Convert seconds to a struct timeval.  Intended for use with
20450f702183SJohn Baldwin  * intervals and thus does not permit negative seconds.
20460f702183SJohn Baldwin  */
20470f702183SJohn Baldwin int
sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)20480f702183SJohn Baldwin sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
20490f702183SJohn Baldwin {
20500f702183SJohn Baldwin 	struct timeval *tv;
20510f702183SJohn Baldwin 	int error, secs;
20520f702183SJohn Baldwin 
20530f702183SJohn Baldwin 	tv = arg1;
20540f702183SJohn Baldwin 	secs = tv->tv_sec;
20550f702183SJohn Baldwin 
20560f702183SJohn Baldwin 	error = sysctl_handle_int(oidp, &secs, 0, req);
20570f702183SJohn Baldwin 	if (error || req->newptr == NULL)
20580f702183SJohn Baldwin 		return (error);
20590f702183SJohn Baldwin 
20600f702183SJohn Baldwin 	if (secs < 0)
20610f702183SJohn Baldwin 		return (EINVAL);
20620f702183SJohn Baldwin 	tv->tv_sec = secs;
20630f702183SJohn Baldwin 
20640f702183SJohn Baldwin 	return (0);
20650f702183SJohn Baldwin }
2066003ffd57SWarner Losh 
2067003ffd57SWarner Losh /*
2068deae269aSPoul-Henning Kamp  * Transfer functions to/from kernel space.
2069deae269aSPoul-Henning Kamp  * XXX: rather untested at this point
2070deae269aSPoul-Henning Kamp  */
2071deae269aSPoul-Henning Kamp static int
sysctl_old_kernel(struct sysctl_req * req,const void * p,size_t l)2072069e9bc1SDoug Rabson sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
2073ae0eb976SPoul-Henning Kamp {
2074069e9bc1SDoug Rabson 	size_t i = 0;
2075deae269aSPoul-Henning Kamp 
2076deae269aSPoul-Henning Kamp 	if (req->oldptr) {
2077069e9bc1SDoug Rabson 		i = l;
20783a515572SThomas Moestl 		if (req->oldlen <= req->oldidx)
20793a515572SThomas Moestl 			i = 0;
20803a515572SThomas Moestl 		else
2081069e9bc1SDoug Rabson 			if (i > req->oldlen - req->oldidx)
2082069e9bc1SDoug Rabson 				i = req->oldlen - req->oldidx;
2083deae269aSPoul-Henning Kamp 		if (i > 0)
208409a8dfa2SBruce Evans 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
2085ae0eb976SPoul-Henning Kamp 	}
20866b72d8dbSKonstantin Belousov 	req->oldidx += l;
20871c346c70SNate Williams 	if (req->oldptr && i != l)
2088ae0eb976SPoul-Henning Kamp 		return (ENOMEM);
20892e210993SPoul-Henning Kamp 	return (0);
2090ae0eb976SPoul-Henning Kamp }
2091ae0eb976SPoul-Henning Kamp 
2092deae269aSPoul-Henning Kamp static int
sysctl_new_kernel(struct sysctl_req * req,void * p,size_t l)2093069e9bc1SDoug Rabson sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
2094ae0eb976SPoul-Henning Kamp {
2095deae269aSPoul-Henning Kamp 	if (!req->newptr)
209646003fb3SPawel Jakub Dawidek 		return (0);
2097deae269aSPoul-Henning Kamp 	if (req->newlen - req->newidx < l)
2098ae0eb976SPoul-Henning Kamp 		return (EINVAL);
209910f7b12cSBrooks Davis 	bcopy((const char *)req->newptr + req->newidx, p, l);
2100ae0eb976SPoul-Henning Kamp 	req->newidx += l;
2101ae0eb976SPoul-Henning Kamp 	return (0);
2102ae0eb976SPoul-Henning Kamp }
2103ae0eb976SPoul-Henning Kamp 
21041c346c70SNate Williams int
kernel_sysctl(struct thread * td,int * name,u_int namelen,void * old,size_t * oldlenp,void * new,size_t newlen,size_t * retval,int flags)2105b40ce416SJulian Elischer kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2106a7bc3102SPeter Wemm     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
21071c346c70SNate Williams {
21081c346c70SNate Williams 	int error = 0;
21091c346c70SNate Williams 	struct sysctl_req req;
21101c346c70SNate Williams 
21111c346c70SNate Williams 	bzero(&req, sizeof req);
21121c346c70SNate Williams 
2113ce178806SRobert Watson 	req.td = td;
2114a7bc3102SPeter Wemm 	req.flags = flags;
21151c346c70SNate Williams 
21161c346c70SNate Williams 	if (oldlenp) {
21171c346c70SNate Williams 		req.oldlen = *oldlenp;
21181c346c70SNate Williams 	}
2119a961520cSDon Lewis 	req.validlen = req.oldlen;
21201c346c70SNate Williams 
21211c346c70SNate Williams 	if (old) {
21221c346c70SNate Williams 		req.oldptr= old;
21231c346c70SNate Williams 	}
21241c346c70SNate Williams 
2125b8edb44cSDima Dorfman 	if (new != NULL) {
21261c346c70SNate Williams 		req.newlen = newlen;
21271c346c70SNate Williams 		req.newptr = new;
21281c346c70SNate Williams 	}
21291c346c70SNate Williams 
21301c346c70SNate Williams 	req.oldfunc = sysctl_old_kernel;
21311c346c70SNate Williams 	req.newfunc = sysctl_new_kernel;
213273d6f851SMatthew D Fleming 	req.lock = REQ_UNWIRED;
21331c346c70SNate Williams 
21341c346c70SNate Williams 	error = sysctl_root(0, name, namelen, &req);
2135f0597024SBruce M Simpson 
2136a961520cSDon Lewis 	if (req.lock == REQ_WIRED && req.validlen > 0)
2137a961520cSDon Lewis 		vsunlock(req.oldptr, req.validlen);
21381c346c70SNate Williams 
21391c346c70SNate Williams 	if (error && error != ENOMEM)
21401c346c70SNate Williams 		return (error);
21411c346c70SNate Williams 
21421c346c70SNate Williams 	if (retval) {
2143a961520cSDon Lewis 		if (req.oldptr && req.oldidx > req.validlen)
2144a961520cSDon Lewis 			*retval = req.validlen;
21451c346c70SNate Williams 		else
21461c346c70SNate Williams 			*retval = req.oldidx;
21471c346c70SNate Williams 	}
21481c346c70SNate Williams 	return (error);
21491c346c70SNate Williams }
21501c346c70SNate Williams 
21511890520aSJonathan Lemon int
kernel_sysctlbyname(struct thread * td,char * name,void * old,size_t * oldlenp,void * new,size_t newlen,size_t * retval,int flags)2152b40ce416SJulian Elischer kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
2153a7bc3102SPeter Wemm     void *new, size_t newlen, size_t *retval, int flags)
21541890520aSJonathan Lemon {
21551890520aSJonathan Lemon         int oid[CTL_MAXNAME];
21562f7f966cSMatt Jacob         size_t oidlen, plen;
21572f7f966cSMatt Jacob 	int error;
21581890520aSJonathan Lemon 
2159382e01c8SKonstantin Belousov 	oid[0] = CTL_SYSCTL;
2160382e01c8SKonstantin Belousov 	oid[1] = CTL_SYSCTL_NAME2OID;
21611890520aSJonathan Lemon 	oidlen = sizeof(oid);
21621890520aSJonathan Lemon 
2163b40ce416SJulian Elischer 	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
2164a7bc3102SPeter Wemm 	    (void *)name, strlen(name), &plen, flags);
21651890520aSJonathan Lemon 	if (error)
21661890520aSJonathan Lemon 		return (error);
21671890520aSJonathan Lemon 
2168b40ce416SJulian Elischer 	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
2169a7bc3102SPeter Wemm 	    new, newlen, retval, flags);
21701890520aSJonathan Lemon 	return (error);
21711890520aSJonathan Lemon }
21721890520aSJonathan Lemon 
2173deae269aSPoul-Henning Kamp /*
2174deae269aSPoul-Henning Kamp  * Transfer function to/from user space.
2175deae269aSPoul-Henning Kamp  */
2176deae269aSPoul-Henning Kamp static int
sysctl_old_user(struct sysctl_req * req,const void * p,size_t l)2177069e9bc1SDoug Rabson sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
2178ae0eb976SPoul-Henning Kamp {
217947934cefSDon Lewis 	size_t i, len, origidx;
21805730afc9SAlan Cox 	int error;
2181ae0eb976SPoul-Henning Kamp 
218247934cefSDon Lewis 	origidx = req->oldidx;
218347934cefSDon Lewis 	req->oldidx += l;
21846b72d8dbSKonstantin Belousov 	if (req->oldptr == NULL)
218547934cefSDon Lewis 		return (0);
2186417ab24fSChristian S.J. Peron 	/*
2187417ab24fSChristian S.J. Peron 	 * If we have not wired the user supplied buffer and we are currently
2188417ab24fSChristian S.J. Peron 	 * holding locks, drop a witness warning, as it's possible that
2189417ab24fSChristian S.J. Peron 	 * write operations to the user page can sleep.
2190417ab24fSChristian S.J. Peron 	 */
2191417ab24fSChristian S.J. Peron 	if (req->lock != REQ_WIRED)
219226306795SJohn Baldwin 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
219326306795SJohn Baldwin 		    "sysctl_old_user()");
2194069e9bc1SDoug Rabson 	i = l;
2195a961520cSDon Lewis 	len = req->validlen;
219647934cefSDon Lewis 	if (len <= origidx)
21973a515572SThomas Moestl 		i = 0;
219847934cefSDon Lewis 	else {
219947934cefSDon Lewis 		if (i > len - origidx)
220047934cefSDon Lewis 			i = len - origidx;
22015730afc9SAlan Cox 		if (req->lock == REQ_WIRED) {
22025730afc9SAlan Cox 			error = copyout_nofault(p, (char *)req->oldptr +
22035730afc9SAlan Cox 			    origidx, i);
22045730afc9SAlan Cox 		} else
220547934cefSDon Lewis 			error = copyout(p, (char *)req->oldptr + origidx, i);
22065730afc9SAlan Cox 		if (error != 0)
2207ae0eb976SPoul-Henning Kamp 			return (error);
22085730afc9SAlan Cox 	}
220947934cefSDon Lewis 	if (i < l)
2210ae0eb976SPoul-Henning Kamp 		return (ENOMEM);
2211deae269aSPoul-Henning Kamp 	return (0);
2212ae0eb976SPoul-Henning Kamp }
2213ae0eb976SPoul-Henning Kamp 
2214deae269aSPoul-Henning Kamp static int
sysctl_new_user(struct sysctl_req * req,void * p,size_t l)2215069e9bc1SDoug Rabson sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2216ae0eb976SPoul-Henning Kamp {
221716cd04a3SPoul-Henning Kamp 	int error;
2218deae269aSPoul-Henning Kamp 
2219deae269aSPoul-Henning Kamp 	if (!req->newptr)
222046003fb3SPawel Jakub Dawidek 		return (0);
2221deae269aSPoul-Henning Kamp 	if (req->newlen - req->newidx < l)
2222ae0eb976SPoul-Henning Kamp 		return (EINVAL);
2223d8339a26SChristian S.J. Peron 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2224d8339a26SChristian S.J. Peron 	    "sysctl_new_user()");
222510f7b12cSBrooks Davis 	error = copyin((const char *)req->newptr + req->newidx, p, l);
2226ae0eb976SPoul-Henning Kamp 	req->newidx += l;
2227ae0eb976SPoul-Henning Kamp 	return (error);
2228b396cd83SPoul-Henning Kamp }
2229b396cd83SPoul-Henning Kamp 
22300600730dSDon Lewis /*
22310600730dSDon Lewis  * Wire the user space destination buffer.  If set to a value greater than
22320600730dSDon Lewis  * zero, the len parameter limits the maximum amount of wired memory.
22330600730dSDon Lewis  */
223447934cefSDon Lewis int
sysctl_wire_old_buffer(struct sysctl_req * req,size_t len)22350600730dSDon Lewis sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
22360600730dSDon Lewis {
223747934cefSDon Lewis 	int ret;
2238433e2f47SDag-Erling Smørgrav 	size_t wiredlen;
223947934cefSDon Lewis 
224047934cefSDon Lewis 	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
224147934cefSDon Lewis 	ret = 0;
224273d6f851SMatthew D Fleming 	if (req->lock != REQ_WIRED && req->oldptr &&
22435be99846SBruce M Simpson 	    req->oldfunc == sysctl_old_user) {
22448ac3e8e9SDon Lewis 		if (wiredlen != 0) {
2245ce8660e3SDon Lewis 			ret = vslock(req->oldptr, wiredlen);
2246b4adfcf2SBrian Feldman 			if (ret != 0) {
2247b4adfcf2SBrian Feldman 				if (ret != ENOMEM)
22488ac3e8e9SDon Lewis 					return (ret);
2249b4adfcf2SBrian Feldman 				wiredlen = 0;
2250b4adfcf2SBrian Feldman 			}
22518ac3e8e9SDon Lewis 		}
22525be99846SBruce M Simpson 		req->lock = REQ_WIRED;
2253a961520cSDon Lewis 		req->validlen = wiredlen;
22540600730dSDon Lewis 	}
22558ac3e8e9SDon Lewis 	return (0);
225647934cefSDon Lewis }
22570600730dSDon Lewis 
22582e210993SPoul-Henning Kamp int
sysctl_find_oid(int * name,u_int namelen,struct sysctl_oid ** noid,int * nindx,struct sysctl_req * req)2259226420a4SBrian Feldman sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2260226420a4SBrian Feldman     int *nindx, struct sysctl_req *req)
22612e210993SPoul-Henning Kamp {
2262d0bb6f25SMatthew D Fleming 	struct sysctl_oid_list *lsp;
2263ce02431fSDoug Rabson 	struct sysctl_oid *oid;
2264d3f96f66SAlan Somers 	struct sysctl_oid key;
2265226420a4SBrian Feldman 	int indx;
22662e210993SPoul-Henning Kamp 
2267efe0abddSMateusz Guzik 	SYSCTL_ASSERT_LOCKED();
2268d0bb6f25SMatthew D Fleming 	lsp = &sysctl__children;
22692e210993SPoul-Henning Kamp 	indx = 0;
2270d0bb6f25SMatthew D Fleming 	while (indx < CTL_MAXNAME) {
2271d3f96f66SAlan Somers 		key.oid_number = name[indx];
2272d3f96f66SAlan Somers 		oid = RB_FIND(sysctl_oid_list, lsp, &key);
2273d0bb6f25SMatthew D Fleming 		if (oid == NULL)
2274d0bb6f25SMatthew D Fleming 			return (ENOENT);
2275d0bb6f25SMatthew D Fleming 
22762e210993SPoul-Henning Kamp 		indx++;
2277ce02431fSDoug Rabson 		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2278d0bb6f25SMatthew D Fleming 			if (oid->oid_handler != NULL || indx == namelen) {
2279226420a4SBrian Feldman 				*noid = oid;
2280226420a4SBrian Feldman 				if (nindx != NULL)
2281226420a4SBrian Feldman 					*nindx = indx;
2282ccecef29SMatthew D Fleming 				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2283ccecef29SMatthew D Fleming 				    ("%s found DYING node %p", __func__, oid));
2284226420a4SBrian Feldman 				return (0);
2285226420a4SBrian Feldman 			}
2286d0bb6f25SMatthew D Fleming 			lsp = SYSCTL_CHILDREN(oid);
2287226420a4SBrian Feldman 		} else if (indx == namelen) {
2288693593b6SAndriy Gapon 			if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2289693593b6SAndriy Gapon 				return (ENOENT);
2290226420a4SBrian Feldman 			*noid = oid;
2291226420a4SBrian Feldman 			if (nindx != NULL)
2292226420a4SBrian Feldman 				*nindx = indx;
2293ccecef29SMatthew D Fleming 			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2294ccecef29SMatthew D Fleming 			    ("%s found DYING node %p", __func__, oid));
2295226420a4SBrian Feldman 			return (0);
22962e210993SPoul-Henning Kamp 		} else {
2297226420a4SBrian Feldman 			return (ENOTDIR);
22982e210993SPoul-Henning Kamp 		}
22992e210993SPoul-Henning Kamp 	}
2300226420a4SBrian Feldman 	return (ENOENT);
2301226420a4SBrian Feldman }
2302226420a4SBrian Feldman 
2303226420a4SBrian Feldman /*
2304226420a4SBrian Feldman  * Traverse our tree, and find the right node, execute whatever it points
2305226420a4SBrian Feldman  * to, and return the resulting error code.
2306226420a4SBrian Feldman  */
2307226420a4SBrian Feldman 
230837c84183SPoul-Henning Kamp static int
sysctl_root(SYSCTL_HANDLER_ARGS)230982d9ae4eSPoul-Henning Kamp sysctl_root(SYSCTL_HANDLER_ARGS)
2310226420a4SBrian Feldman {
2311226420a4SBrian Feldman 	struct sysctl_oid *oid;
23127665e341SMateusz Guzik 	struct rm_priotracker tracker;
2313fe41ca53SMatthew Dillon 	int error, indx, lvl;
2314226420a4SBrian Feldman 
23157665e341SMateusz Guzik 	SYSCTL_RLOCK(&tracker);
2316ddf9d243SEd Schouten 
2317226420a4SBrian Feldman 	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2318226420a4SBrian Feldman 	if (error)
23197665e341SMateusz Guzik 		goto out;
2320226420a4SBrian Feldman 
2321226420a4SBrian Feldman 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2322226420a4SBrian Feldman 		/*
2323226420a4SBrian Feldman 		 * You can't call a sysctl when it's a node, but has
2324226420a4SBrian Feldman 		 * no handler.  Inform the user that it's a node.
2325226420a4SBrian Feldman 		 * The indx may or may not be the same as namelen.
2326226420a4SBrian Feldman 		 */
23277665e341SMateusz Guzik 		if (oid->oid_handler == NULL) {
23287665e341SMateusz Guzik 			error = EISDIR;
23297665e341SMateusz Guzik 			goto out;
23307665e341SMateusz Guzik 		}
2331226420a4SBrian Feldman 	}
2332226420a4SBrian Feldman 
2333c2f413afSRobert Watson 	/* Is this sysctl writable? */
23347665e341SMateusz Guzik 	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
23357665e341SMateusz Guzik 		error = EPERM;
23367665e341SMateusz Guzik 		goto out;
23377665e341SMateusz Guzik 	}
23382e210993SPoul-Henning Kamp 
23397906271fSRobert Watson 	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
23407906271fSRobert Watson 
2341ff66f6a4SRobert Watson #ifdef CAPABILITY_MODE
2342ff66f6a4SRobert Watson 	/*
2343ff66f6a4SRobert Watson 	 * If the process is in capability mode, then don't permit reading or
2344ff66f6a4SRobert Watson 	 * writing unless specifically granted for the node.
2345ff66f6a4SRobert Watson 	 */
2346ff66f6a4SRobert Watson 	if (IN_CAPABILITY_MODE(req->td)) {
23477665e341SMateusz Guzik 		if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
23487665e341SMateusz Guzik 		    (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
23497665e341SMateusz Guzik 			error = EPERM;
23507665e341SMateusz Guzik 			goto out;
23517665e341SMateusz Guzik 		}
2352ff66f6a4SRobert Watson 	}
2353ff66f6a4SRobert Watson #endif
2354ff66f6a4SRobert Watson 
2355c2f413afSRobert Watson 	/* Is this sysctl sensitive to securelevels? */
2356c2f413afSRobert Watson 	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2357fe41ca53SMatthew Dillon 		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2358fe41ca53SMatthew Dillon 		error = securelevel_gt(req->td->td_ucred, lvl);
2359c2f413afSRobert Watson 		if (error)
23607665e341SMateusz Guzik 			goto out;
2361c2f413afSRobert Watson 	}
2362c2f413afSRobert Watson 
2363c2f413afSRobert Watson 	/* Is this sysctl writable by only privileged users? */
2364c2f413afSRobert Watson 	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2365eb79e1c7SBjoern A. Zeeb 		int priv;
2366eb79e1c7SBjoern A. Zeeb 
2367c2f413afSRobert Watson 		if (oid->oid_kind & CTLFLAG_PRISON)
2368eb79e1c7SBjoern A. Zeeb 			priv = PRIV_SYSCTL_WRITEJAIL;
2369eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
2370eb79e1c7SBjoern A. Zeeb 		else if ((oid->oid_kind & CTLFLAG_VNET) &&
2371eb79e1c7SBjoern A. Zeeb 		     prison_owns_vnet(req->td->td_ucred))
2372eb79e1c7SBjoern A. Zeeb 			priv = PRIV_SYSCTL_WRITEJAIL;
2373eb79e1c7SBjoern A. Zeeb #endif
2374c2f413afSRobert Watson 		else
2375eb79e1c7SBjoern A. Zeeb 			priv = PRIV_SYSCTL_WRITE;
2376eb79e1c7SBjoern A. Zeeb 		error = priv_check(req->td, priv);
2377c2f413afSRobert Watson 		if (error)
23787665e341SMateusz Guzik 			goto out;
2379c2f413afSRobert Watson 	}
23803ac9f819SPoul-Henning Kamp 
23817665e341SMateusz Guzik 	if (!oid->oid_handler) {
23827665e341SMateusz Guzik 		error = EINVAL;
23837665e341SMateusz Guzik 		goto out;
23847665e341SMateusz Guzik 	}
23852e210993SPoul-Henning Kamp 
238663dba32bSPawel Jakub Dawidek 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
238700fbcda8SAlexander Kabaev 		arg1 = (int *)arg1 + indx;
238863dba32bSPawel Jakub Dawidek 		arg2 -= indx;
238963dba32bSPawel Jakub Dawidek 	} else {
239063dba32bSPawel Jakub Dawidek 		arg1 = oid->oid_arg1;
239163dba32bSPawel Jakub Dawidek 		arg2 = oid->oid_arg2;
239263dba32bSPawel Jakub Dawidek 	}
239363dba32bSPawel Jakub Dawidek #ifdef MAC
239430d239bcSRobert Watson 	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2395ae0eb976SPoul-Henning Kamp 	    req);
239663dba32bSPawel Jakub Dawidek 	if (error != 0)
23977665e341SMateusz Guzik 		goto out;
239863dba32bSPawel Jakub Dawidek #endif
2399b5c32cf4SGleb Smirnoff #ifdef VIMAGE
2400b5c32cf4SGleb Smirnoff 	if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2401b5c32cf4SGleb Smirnoff 		arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2402b5c32cf4SGleb Smirnoff #endif
24037665e341SMateusz Guzik 	error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
240463dba32bSPawel Jakub Dawidek 
24057665e341SMateusz Guzik out:
24067665e341SMateusz Guzik 	SYSCTL_RUNLOCK(&tracker);
2407226420a4SBrian Feldman 	return (error);
24082e210993SPoul-Henning Kamp }
24092e210993SPoul-Henning Kamp 
2410d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
2411d8bd949bSBrooks Davis struct __sysctl_args {
2412b8da2396SPoul-Henning Kamp 	int	*name;
2413b8da2396SPoul-Henning Kamp 	u_int	namelen;
2414b8da2396SPoul-Henning Kamp 	void	*old;
2415b8da2396SPoul-Henning Kamp 	size_t	*oldlenp;
2416b8da2396SPoul-Henning Kamp 	void	*new;
2417b8da2396SPoul-Henning Kamp 	size_t	newlen;
2418b8da2396SPoul-Henning Kamp };
2419d2d3e875SBruce Evans #endif
2420df8bae1dSRodney W. Grimes int
sys___sysctl(struct thread * td,struct __sysctl_args * uap)2421d8bd949bSBrooks Davis sys___sysctl(struct thread *td, struct __sysctl_args *uap)
2422df8bae1dSRodney W. Grimes {
2423875b66a0SJohn Baldwin 	int error, i, name[CTL_MAXNAME];
2424069e9bc1SDoug Rabson 	size_t j;
2425b396cd83SPoul-Henning Kamp 
2426df8bae1dSRodney W. Grimes 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2427df8bae1dSRodney W. Grimes 		return (EINVAL);
2428b396cd83SPoul-Henning Kamp 
2429797f2d22SPoul-Henning Kamp  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2430797f2d22SPoul-Henning Kamp  	if (error)
2431df8bae1dSRodney W. Grimes 		return (error);
2432df8bae1dSRodney W. Grimes 
2433b40ce416SJulian Elischer 	error = userland_sysctl(td, name, uap->namelen,
2434b8da2396SPoul-Henning Kamp 		uap->old, uap->oldlenp, 0,
2435a7bc3102SPeter Wemm 		uap->new, uap->newlen, &j, 0);
2436deae269aSPoul-Henning Kamp 	if (error && error != ENOMEM)
2437ddf9d243SEd Schouten 		return (error);
24384cfe4790SEd Schouten 	if (uap->oldlenp) {
2439875b66a0SJohn Baldwin 		i = copyout(&j, uap->oldlenp, sizeof(j));
24404cfe4790SEd Schouten 		if (i)
24414cfe4790SEd Schouten 			return (i);
24424cfe4790SEd Schouten 	}
2443deae269aSPoul-Henning Kamp 	return (error);
2444b8da2396SPoul-Henning Kamp }
2445b8da2396SPoul-Henning Kamp 
2446d05b53e0SMateusz Guzik int
kern___sysctlbyname(struct thread * td,const char * oname,size_t namelen,void * old,size_t * oldlenp,void * new,size_t newlen,size_t * retval,int flags,bool inkernel)2447d05b53e0SMateusz Guzik kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2448d05b53e0SMateusz Guzik     void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2449d05b53e0SMateusz Guzik     int flags, bool inkernel)
2450d05b53e0SMateusz Guzik {
2451d05b53e0SMateusz Guzik 	int oid[CTL_MAXNAME];
2452d05b53e0SMateusz Guzik 	char namebuf[16];
2453d05b53e0SMateusz Guzik 	char *name;
2454d05b53e0SMateusz Guzik 	size_t oidlen;
2455d05b53e0SMateusz Guzik 	int error;
2456d05b53e0SMateusz Guzik 
2457d05b53e0SMateusz Guzik 	if (namelen > MAXPATHLEN || namelen == 0)
2458d05b53e0SMateusz Guzik 		return (EINVAL);
2459d05b53e0SMateusz Guzik 	name = namebuf;
2460d05b53e0SMateusz Guzik 	if (namelen > sizeof(namebuf))
2461d05b53e0SMateusz Guzik 		name = malloc(namelen, M_SYSCTL, M_WAITOK);
2462d05b53e0SMateusz Guzik 	error = copyin(oname, name, namelen);
2463d05b53e0SMateusz Guzik 	if (error != 0)
2464d05b53e0SMateusz Guzik 		goto out;
2465d05b53e0SMateusz Guzik 
2466382e01c8SKonstantin Belousov 	oid[0] = CTL_SYSCTL;
2467382e01c8SKonstantin Belousov 	oid[1] = CTL_SYSCTL_NAME2OID;
2468d05b53e0SMateusz Guzik 	oidlen = sizeof(oid);
2469d05b53e0SMateusz Guzik 	error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2470d05b53e0SMateusz Guzik 	    retval, flags);
2471d05b53e0SMateusz Guzik 	if (error != 0)
2472d05b53e0SMateusz Guzik 		goto out;
2473d05b53e0SMateusz Guzik 	error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2474d05b53e0SMateusz Guzik 	    inkernel, new, newlen, retval, flags);
2475d05b53e0SMateusz Guzik 
2476d05b53e0SMateusz Guzik out:
2477d05b53e0SMateusz Guzik 	if (namelen > sizeof(namebuf))
2478d05b53e0SMateusz Guzik 		free(name, M_SYSCTL);
2479d05b53e0SMateusz Guzik 	return (error);
2480d05b53e0SMateusz Guzik }
2481d05b53e0SMateusz Guzik 
2482d05b53e0SMateusz Guzik #ifndef	_SYS_SYSPROTO_H_
2483d05b53e0SMateusz Guzik struct __sysctlbyname_args {
2484d05b53e0SMateusz Guzik 	const char	*name;
2485d05b53e0SMateusz Guzik 	size_t	namelen;
2486d05b53e0SMateusz Guzik 	void	*old;
2487d05b53e0SMateusz Guzik 	size_t	*oldlenp;
2488d05b53e0SMateusz Guzik 	void	*new;
2489d05b53e0SMateusz Guzik 	size_t	newlen;
2490d05b53e0SMateusz Guzik };
2491d05b53e0SMateusz Guzik #endif
2492d05b53e0SMateusz Guzik int
sys___sysctlbyname(struct thread * td,struct __sysctlbyname_args * uap)2493d05b53e0SMateusz Guzik sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2494d05b53e0SMateusz Guzik {
2495d05b53e0SMateusz Guzik 	size_t rv;
2496d05b53e0SMateusz Guzik 	int error;
2497d05b53e0SMateusz Guzik 
2498d05b53e0SMateusz Guzik 	error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2499d05b53e0SMateusz Guzik 	    uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2500d05b53e0SMateusz Guzik 	if (error != 0)
2501d05b53e0SMateusz Guzik 		return (error);
2502d05b53e0SMateusz Guzik 	if (uap->oldlenp != NULL)
2503d05b53e0SMateusz Guzik 		error = copyout(&rv, uap->oldlenp, sizeof(rv));
2504d05b53e0SMateusz Guzik 
2505d05b53e0SMateusz Guzik 	return (error);
2506d05b53e0SMateusz Guzik }
2507d05b53e0SMateusz Guzik 
2508b8da2396SPoul-Henning Kamp /*
2509b8da2396SPoul-Henning Kamp  * This is used from various compatibility syscalls too.  That's why name
2510b8da2396SPoul-Henning Kamp  * must be in kernel space.
2511b8da2396SPoul-Henning Kamp  */
2512b8da2396SPoul-Henning Kamp int
userland_sysctl(struct thread * td,int * name,u_int namelen,void * old,size_t * oldlenp,int inkernel,const void * new,size_t newlen,size_t * retval,int flags)2513b40ce416SJulian Elischer userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
251410f7b12cSBrooks Davis     size_t *oldlenp, int inkernel, const void *new, size_t newlen,
251510f7b12cSBrooks Davis     size_t *retval, int flags)
2516b8da2396SPoul-Henning Kamp {
2517a961520cSDon Lewis 	struct sysctl_req req;
25187d1d9cc4SMark Johnston 	int error = 0;
25197d1d9cc4SMark Johnston 	bool memlocked;
2520ae0eb976SPoul-Henning Kamp 
2521ae0eb976SPoul-Henning Kamp 	bzero(&req, sizeof req);
2522b8da2396SPoul-Henning Kamp 
2523ce178806SRobert Watson 	req.td = td;
2524a7bc3102SPeter Wemm 	req.flags = flags;
252516cd04a3SPoul-Henning Kamp 
2526b8da2396SPoul-Henning Kamp 	if (oldlenp) {
2527b8da2396SPoul-Henning Kamp 		if (inkernel) {
2528ae0eb976SPoul-Henning Kamp 			req.oldlen = *oldlenp;
2529b8da2396SPoul-Henning Kamp 		} else {
2530deae269aSPoul-Henning Kamp 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2531b8da2396SPoul-Henning Kamp 			if (error)
2532b8da2396SPoul-Henning Kamp 				return (error);
2533b8da2396SPoul-Henning Kamp 		}
2534b8da2396SPoul-Henning Kamp 	}
2535a961520cSDon Lewis 	req.validlen = req.oldlen;
2536ae0eb976SPoul-Henning Kamp 	req.oldptr = old;
25372e210993SPoul-Henning Kamp 
2538b8edb44cSDima Dorfman 	if (new != NULL) {
2539ae0eb976SPoul-Henning Kamp 		req.newlen = newlen;
2540ae0eb976SPoul-Henning Kamp 		req.newptr = new;
2541b396cd83SPoul-Henning Kamp 	}
2542b396cd83SPoul-Henning Kamp 
2543ae0eb976SPoul-Henning Kamp 	req.oldfunc = sysctl_old_user;
2544ae0eb976SPoul-Henning Kamp 	req.newfunc = sysctl_new_user;
254573d6f851SMatthew D Fleming 	req.lock = REQ_UNWIRED;
25464b2af45fSPoul-Henning Kamp 
2547a56be37eSJohn Baldwin #ifdef KTRACE
2548a56be37eSJohn Baldwin 	if (KTRPOINT(curthread, KTR_SYSCTL))
2549a56be37eSJohn Baldwin 		ktrsysctl(name, namelen);
2550a56be37eSJohn Baldwin #endif
25517d1d9cc4SMark Johnston 	memlocked = false;
25527d1d9cc4SMark Johnston 	if (priv_check(td, PRIV_SYSCTL_MEMLOCK) != 0 &&
25537d1d9cc4SMark Johnston 	    req.oldptr != NULL && req.oldlen > 4 * PAGE_SIZE) {
25547d1d9cc4SMark Johnston 		memlocked = true;
25553e829b18SJohn Baldwin 		sx_xlock(&sysctlmemlock);
25569b8de76bSMateusz Guzik 	}
25579ed47d01SJamie Gritton 	CURVNET_SET(TD_TO_VNET(td));
2558ae0eb976SPoul-Henning Kamp 
2559af80b2c9SKonstantin Belousov 	for (;;) {
2560a961520cSDon Lewis 		req.oldidx = 0;
2561a961520cSDon Lewis 		req.newidx = 0;
2562a961520cSDon Lewis 		error = sysctl_root(0, name, namelen, &req);
2563af80b2c9SKonstantin Belousov 		if (error != EAGAIN)
2564af80b2c9SKonstantin Belousov 			break;
25653d08a76bSMatthew D Fleming 		kern_yield(PRI_USER);
2566af80b2c9SKonstantin Belousov 	}
2567b396cd83SPoul-Henning Kamp 
256897021c24SMarko Zec 	CURVNET_RESTORE();
25694b2af45fSPoul-Henning Kamp 
2570ddf9d243SEd Schouten 	if (req.lock == REQ_WIRED && req.validlen > 0)
2571ddf9d243SEd Schouten 		vsunlock(req.oldptr, req.validlen);
25723e829b18SJohn Baldwin 	if (memlocked)
25733e829b18SJohn Baldwin 		sx_xunlock(&sysctlmemlock);
2574ddf9d243SEd Schouten 
2575deae269aSPoul-Henning Kamp 	if (error && error != ENOMEM)
2576deae269aSPoul-Henning Kamp 		return (error);
2577deae269aSPoul-Henning Kamp 
2578deae269aSPoul-Henning Kamp 	if (retval) {
2579a961520cSDon Lewis 		if (req.oldptr && req.oldidx > req.validlen)
2580a961520cSDon Lewis 			*retval = req.validlen;
2581deae269aSPoul-Henning Kamp 		else
2582deae269aSPoul-Henning Kamp 			*retval = req.oldidx;
2583b8da2396SPoul-Henning Kamp 	}
25842e210993SPoul-Henning Kamp 	return (error);
2585df8bae1dSRodney W. Grimes }
25864e657159SMatthew D Fleming 
25874e657159SMatthew D Fleming /*
258800f0e671SMatthew D Fleming  * Drain into a sysctl struct.  The user buffer should be wired if a page
258900f0e671SMatthew D Fleming  * fault would cause issue.
25904e657159SMatthew D Fleming  */
25914e657159SMatthew D Fleming static int
sbuf_sysctl_drain(void * arg,const char * data,int len)25924e657159SMatthew D Fleming sbuf_sysctl_drain(void *arg, const char *data, int len)
25934e657159SMatthew D Fleming {
25944e657159SMatthew D Fleming 	struct sysctl_req *req = arg;
25954e657159SMatthew D Fleming 	int error;
25964e657159SMatthew D Fleming 
25974e657159SMatthew D Fleming 	error = SYSCTL_OUT(req, data, len);
25984e657159SMatthew D Fleming 	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
25994e657159SMatthew D Fleming 	return (error == 0 ? len : -error);
26004e657159SMatthew D Fleming }
26014e657159SMatthew D Fleming 
26024e657159SMatthew D Fleming struct sbuf *
sbuf_new_for_sysctl(struct sbuf * s,char * buf,int length,struct sysctl_req * req)26034e657159SMatthew D Fleming sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
26044e657159SMatthew D Fleming     struct sysctl_req *req)
26054e657159SMatthew D Fleming {
26064e657159SMatthew D Fleming 
260728349245SIan Lepore 	/* Supply a default buffer size if none given. */
260828349245SIan Lepore 	if (buf == NULL && length == 0)
260928349245SIan Lepore 		length = 64;
26101eafc078SIan Lepore 	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
26114e657159SMatthew D Fleming 	sbuf_set_drain(s, sbuf_sysctl_drain, req);
26124e657159SMatthew D Fleming 	return (s);
26134e657159SMatthew D Fleming }
2614088c17b4SConrad Meyer 
2615088c17b4SConrad Meyer #ifdef DDB
2616088c17b4SConrad Meyer 
2617088c17b4SConrad Meyer /* The current OID the debugger is working with */
2618088c17b4SConrad Meyer static struct sysctl_oid *g_ddb_oid;
2619088c17b4SConrad Meyer 
2620088c17b4SConrad Meyer /* The current flags specified by the user */
2621088c17b4SConrad Meyer static int g_ddb_sysctl_flags;
2622088c17b4SConrad Meyer 
2623088c17b4SConrad Meyer /* Check to see if the last sysctl printed */
2624088c17b4SConrad Meyer static int g_ddb_sysctl_printed;
2625088c17b4SConrad Meyer 
2626088c17b4SConrad Meyer static const int ctl_sign[CTLTYPE+1] = {
2627088c17b4SConrad Meyer 	[CTLTYPE_INT] = 1,
2628088c17b4SConrad Meyer 	[CTLTYPE_LONG] = 1,
2629088c17b4SConrad Meyer 	[CTLTYPE_S8] = 1,
2630088c17b4SConrad Meyer 	[CTLTYPE_S16] = 1,
2631088c17b4SConrad Meyer 	[CTLTYPE_S32] = 1,
2632088c17b4SConrad Meyer 	[CTLTYPE_S64] = 1,
2633088c17b4SConrad Meyer };
2634088c17b4SConrad Meyer 
2635088c17b4SConrad Meyer static const int ctl_size[CTLTYPE+1] = {
2636088c17b4SConrad Meyer 	[CTLTYPE_INT] = sizeof(int),
2637088c17b4SConrad Meyer 	[CTLTYPE_UINT] = sizeof(u_int),
2638088c17b4SConrad Meyer 	[CTLTYPE_LONG] = sizeof(long),
2639088c17b4SConrad Meyer 	[CTLTYPE_ULONG] = sizeof(u_long),
2640088c17b4SConrad Meyer 	[CTLTYPE_S8] = sizeof(int8_t),
2641088c17b4SConrad Meyer 	[CTLTYPE_S16] = sizeof(int16_t),
2642088c17b4SConrad Meyer 	[CTLTYPE_S32] = sizeof(int32_t),
2643088c17b4SConrad Meyer 	[CTLTYPE_S64] = sizeof(int64_t),
2644088c17b4SConrad Meyer 	[CTLTYPE_U8] = sizeof(uint8_t),
2645088c17b4SConrad Meyer 	[CTLTYPE_U16] = sizeof(uint16_t),
2646088c17b4SConrad Meyer 	[CTLTYPE_U32] = sizeof(uint32_t),
2647088c17b4SConrad Meyer 	[CTLTYPE_U64] = sizeof(uint64_t),
2648088c17b4SConrad Meyer };
2649088c17b4SConrad Meyer 
2650088c17b4SConrad Meyer #define DB_SYSCTL_NAME_ONLY	0x001	/* Compare with -N */
2651088c17b4SConrad Meyer #define DB_SYSCTL_VALUE_ONLY	0x002	/* Compare with -n */
2652088c17b4SConrad Meyer #define DB_SYSCTL_OPAQUE	0x004	/* Compare with -o */
2653088c17b4SConrad Meyer #define DB_SYSCTL_HEX		0x008	/* Compare with -x */
2654088c17b4SConrad Meyer 
2655088c17b4SConrad Meyer #define DB_SYSCTL_SAFE_ONLY	0x100	/* Only simple types */
2656088c17b4SConrad Meyer 
2657088c17b4SConrad Meyer static const char db_sysctl_modifs[] = {
2658088c17b4SConrad Meyer 	'N', 'n', 'o', 'x',
2659088c17b4SConrad Meyer };
2660088c17b4SConrad Meyer 
2661088c17b4SConrad Meyer static const int db_sysctl_modif_values[] = {
2662088c17b4SConrad Meyer 	DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2663088c17b4SConrad Meyer 	DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2664088c17b4SConrad Meyer };
2665088c17b4SConrad Meyer 
2666088c17b4SConrad Meyer /* Handlers considered safe to print while recursing */
2667088c17b4SConrad Meyer static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2668088c17b4SConrad Meyer 	sysctl_handle_bool,
2669088c17b4SConrad Meyer 	sysctl_handle_8,
2670088c17b4SConrad Meyer 	sysctl_handle_16,
2671088c17b4SConrad Meyer 	sysctl_handle_32,
2672088c17b4SConrad Meyer 	sysctl_handle_64,
2673088c17b4SConrad Meyer 	sysctl_handle_int,
2674088c17b4SConrad Meyer 	sysctl_handle_long,
2675088c17b4SConrad Meyer 	sysctl_handle_string,
2676088c17b4SConrad Meyer 	sysctl_handle_opaque,
2677088c17b4SConrad Meyer };
2678088c17b4SConrad Meyer 
2679088c17b4SConrad Meyer /*
2680088c17b4SConrad Meyer  * Use in place of sysctl_old_kernel to print sysctl values.
2681088c17b4SConrad Meyer  *
2682088c17b4SConrad Meyer  * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2683088c17b4SConrad Meyer  */
2684088c17b4SConrad Meyer static int
sysctl_old_ddb(struct sysctl_req * req,const void * ptr,size_t len)2685088c17b4SConrad Meyer sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2686088c17b4SConrad Meyer {
2687088c17b4SConrad Meyer 	const u_char *val, *p;
2688088c17b4SConrad Meyer 	const char *sep1;
2689088c17b4SConrad Meyer 	size_t intlen, slen;
2690088c17b4SConrad Meyer 	uintmax_t umv;
2691088c17b4SConrad Meyer 	intmax_t mv;
2692088c17b4SConrad Meyer 	int sign, ctltype, hexlen, xflag, error;
2693088c17b4SConrad Meyer 
2694088c17b4SConrad Meyer 	/* Suppress false-positive GCC uninitialized variable warnings */
2695088c17b4SConrad Meyer 	mv = 0;
2696088c17b4SConrad Meyer 	umv = 0;
2697088c17b4SConrad Meyer 
2698088c17b4SConrad Meyer 	slen = len;
2699088c17b4SConrad Meyer 	val = p = ptr;
2700088c17b4SConrad Meyer 
2701088c17b4SConrad Meyer 	if (ptr == NULL) {
2702088c17b4SConrad Meyer 		error = 0;
2703088c17b4SConrad Meyer 		goto out;
2704088c17b4SConrad Meyer 	}
2705088c17b4SConrad Meyer 
2706088c17b4SConrad Meyer 	/* We are going to print */
2707088c17b4SConrad Meyer 	g_ddb_sysctl_printed = 1;
2708088c17b4SConrad Meyer 
2709088c17b4SConrad Meyer 	xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2710088c17b4SConrad Meyer 
2711088c17b4SConrad Meyer 	ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2712088c17b4SConrad Meyer 	sign = ctl_sign[ctltype];
2713088c17b4SConrad Meyer 	intlen = ctl_size[ctltype];
2714088c17b4SConrad Meyer 
2715088c17b4SConrad Meyer 	switch (ctltype) {
2716088c17b4SConrad Meyer 	case CTLTYPE_NODE:
2717088c17b4SConrad Meyer 	case CTLTYPE_STRING:
2718088c17b4SConrad Meyer 		db_printf("%.*s", (int) len, (const char *) p);
2719088c17b4SConrad Meyer 		error = 0;
2720088c17b4SConrad Meyer 		goto out;
2721088c17b4SConrad Meyer 
2722088c17b4SConrad Meyer 	case CTLTYPE_INT:
2723088c17b4SConrad Meyer 	case CTLTYPE_UINT:
2724088c17b4SConrad Meyer 	case CTLTYPE_LONG:
2725088c17b4SConrad Meyer 	case CTLTYPE_ULONG:
2726088c17b4SConrad Meyer 	case CTLTYPE_S8:
2727088c17b4SConrad Meyer 	case CTLTYPE_S16:
2728088c17b4SConrad Meyer 	case CTLTYPE_S32:
2729088c17b4SConrad Meyer 	case CTLTYPE_S64:
2730088c17b4SConrad Meyer 	case CTLTYPE_U8:
2731088c17b4SConrad Meyer 	case CTLTYPE_U16:
2732088c17b4SConrad Meyer 	case CTLTYPE_U32:
2733088c17b4SConrad Meyer 	case CTLTYPE_U64:
2734088c17b4SConrad Meyer 		hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2735088c17b4SConrad Meyer 		sep1 = "";
2736088c17b4SConrad Meyer 		while (len >= intlen) {
2737088c17b4SConrad Meyer 			switch (ctltype) {
2738088c17b4SConrad Meyer 			case CTLTYPE_INT:
2739088c17b4SConrad Meyer 			case CTLTYPE_UINT:
2740088c17b4SConrad Meyer 				umv = *(const u_int *)p;
2741088c17b4SConrad Meyer 				mv = *(const int *)p;
2742088c17b4SConrad Meyer 				break;
2743088c17b4SConrad Meyer 			case CTLTYPE_LONG:
2744088c17b4SConrad Meyer 			case CTLTYPE_ULONG:
2745088c17b4SConrad Meyer 				umv = *(const u_long *)p;
2746088c17b4SConrad Meyer 				mv = *(const long *)p;
2747088c17b4SConrad Meyer 				break;
2748088c17b4SConrad Meyer 			case CTLTYPE_S8:
2749088c17b4SConrad Meyer 			case CTLTYPE_U8:
2750088c17b4SConrad Meyer 				umv = *(const uint8_t *)p;
2751088c17b4SConrad Meyer 				mv = *(const int8_t *)p;
2752088c17b4SConrad Meyer 				break;
2753088c17b4SConrad Meyer 			case CTLTYPE_S16:
2754088c17b4SConrad Meyer 			case CTLTYPE_U16:
2755088c17b4SConrad Meyer 				umv = *(const uint16_t *)p;
2756088c17b4SConrad Meyer 				mv = *(const int16_t *)p;
2757088c17b4SConrad Meyer 				break;
2758088c17b4SConrad Meyer 			case CTLTYPE_S32:
2759088c17b4SConrad Meyer 			case CTLTYPE_U32:
2760088c17b4SConrad Meyer 				umv = *(const uint32_t *)p;
2761088c17b4SConrad Meyer 				mv = *(const int32_t *)p;
2762088c17b4SConrad Meyer 				break;
2763088c17b4SConrad Meyer 			case CTLTYPE_S64:
2764088c17b4SConrad Meyer 			case CTLTYPE_U64:
2765088c17b4SConrad Meyer 				umv = *(const uint64_t *)p;
2766088c17b4SConrad Meyer 				mv = *(const int64_t *)p;
2767088c17b4SConrad Meyer 				break;
2768088c17b4SConrad Meyer 			}
2769088c17b4SConrad Meyer 
2770088c17b4SConrad Meyer 			db_printf("%s", sep1);
2771088c17b4SConrad Meyer 			if (xflag)
2772088c17b4SConrad Meyer 				db_printf("%#0*jx", hexlen, umv);
2773088c17b4SConrad Meyer 			else if (!sign)
2774088c17b4SConrad Meyer 				db_printf("%ju", umv);
2775088c17b4SConrad Meyer 			else if (g_ddb_oid->oid_fmt[1] == 'K') {
2776088c17b4SConrad Meyer 				/* Kelvins are currently unsupported. */
2777088c17b4SConrad Meyer 				error = EOPNOTSUPP;
2778088c17b4SConrad Meyer 				goto out;
2779088c17b4SConrad Meyer 			} else
2780088c17b4SConrad Meyer 				db_printf("%jd", mv);
2781088c17b4SConrad Meyer 
2782088c17b4SConrad Meyer 			sep1 = " ";
2783088c17b4SConrad Meyer 			len -= intlen;
2784088c17b4SConrad Meyer 			p += intlen;
2785088c17b4SConrad Meyer 		}
2786088c17b4SConrad Meyer 		error = 0;
2787088c17b4SConrad Meyer 		goto out;
2788088c17b4SConrad Meyer 
2789088c17b4SConrad Meyer 	case CTLTYPE_OPAQUE:
2790088c17b4SConrad Meyer 		/* TODO: Support struct functions. */
2791088c17b4SConrad Meyer 
2792088c17b4SConrad Meyer 		/* FALLTHROUGH */
2793088c17b4SConrad Meyer 	default:
2794088c17b4SConrad Meyer 		db_printf("Format:%s Length:%zu Dump:0x",
2795088c17b4SConrad Meyer 		    g_ddb_oid->oid_fmt, len);
2796088c17b4SConrad Meyer 		while (len-- && (xflag || p < val + 16))
2797088c17b4SConrad Meyer 			db_printf("%02x", *p++);
2798088c17b4SConrad Meyer 		if (!xflag && len > 16)
2799088c17b4SConrad Meyer 			db_printf("...");
2800088c17b4SConrad Meyer 		error = 0;
2801088c17b4SConrad Meyer 		goto out;
2802088c17b4SConrad Meyer 	}
2803088c17b4SConrad Meyer 
2804088c17b4SConrad Meyer out:
2805088c17b4SConrad Meyer 	req->oldidx += slen;
2806088c17b4SConrad Meyer 	return (error);
2807088c17b4SConrad Meyer }
2808088c17b4SConrad Meyer 
2809088c17b4SConrad Meyer /*
2810088c17b4SConrad Meyer  * Avoid setting new sysctl values from the debugger
2811088c17b4SConrad Meyer  */
2812088c17b4SConrad Meyer static int
sysctl_new_ddb(struct sysctl_req * req,void * p,size_t l)2813088c17b4SConrad Meyer sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2814088c17b4SConrad Meyer {
2815088c17b4SConrad Meyer 
2816088c17b4SConrad Meyer 	if (!req->newptr)
2817088c17b4SConrad Meyer 		return (0);
2818088c17b4SConrad Meyer 
2819088c17b4SConrad Meyer 	/* Changing sysctls from the debugger is currently unsupported */
2820088c17b4SConrad Meyer 	return (EPERM);
2821088c17b4SConrad Meyer }
2822088c17b4SConrad Meyer 
2823088c17b4SConrad Meyer /*
2824088c17b4SConrad Meyer  * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2825088c17b4SConrad Meyer  * Instead of copying any output to a buffer we'll dump it right to
2826088c17b4SConrad Meyer  * the console.
2827088c17b4SConrad Meyer  */
2828088c17b4SConrad Meyer static int
db_sysctl(struct sysctl_oid * oidp,int * name,u_int namelen,void * old,size_t * oldlenp,size_t * retval,int flags)2829088c17b4SConrad Meyer db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2830088c17b4SConrad Meyer     void *old, size_t *oldlenp, size_t *retval, int flags)
2831088c17b4SConrad Meyer {
2832088c17b4SConrad Meyer 	struct sysctl_req req;
2833088c17b4SConrad Meyer 	int error;
2834088c17b4SConrad Meyer 
2835088c17b4SConrad Meyer 	/* Setup the request */
2836088c17b4SConrad Meyer 	bzero(&req, sizeof req);
2837088c17b4SConrad Meyer 	req.td = kdb_thread;
2838088c17b4SConrad Meyer 	req.oldfunc = sysctl_old_ddb;
2839088c17b4SConrad Meyer 	req.newfunc = sysctl_new_ddb;
2840088c17b4SConrad Meyer 	req.lock = REQ_UNWIRED;
2841088c17b4SConrad Meyer 	if (oldlenp) {
2842088c17b4SConrad Meyer 		req.oldlen = *oldlenp;
2843088c17b4SConrad Meyer 	}
2844088c17b4SConrad Meyer 	req.validlen = req.oldlen;
2845088c17b4SConrad Meyer 	if (old) {
2846088c17b4SConrad Meyer 		req.oldptr = old;
2847088c17b4SConrad Meyer 	}
2848088c17b4SConrad Meyer 
2849088c17b4SConrad Meyer 	/* Setup our globals for sysctl_old_ddb */
2850088c17b4SConrad Meyer 	g_ddb_oid = oidp;
2851088c17b4SConrad Meyer 	g_ddb_sysctl_flags = flags;
2852088c17b4SConrad Meyer 	g_ddb_sysctl_printed = 0;
2853088c17b4SConrad Meyer 
2854088c17b4SConrad Meyer 	error = sysctl_root(0, name, namelen, &req);
2855088c17b4SConrad Meyer 
2856088c17b4SConrad Meyer 	/* Reset globals */
2857088c17b4SConrad Meyer 	g_ddb_oid = NULL;
2858088c17b4SConrad Meyer 	g_ddb_sysctl_flags = 0;
2859088c17b4SConrad Meyer 
2860088c17b4SConrad Meyer 	if (retval) {
2861088c17b4SConrad Meyer 		if (req.oldptr && req.oldidx > req.validlen)
2862088c17b4SConrad Meyer 			*retval = req.validlen;
2863088c17b4SConrad Meyer 		else
2864088c17b4SConrad Meyer 			*retval = req.oldidx;
2865088c17b4SConrad Meyer 	}
2866088c17b4SConrad Meyer 	return (error);
2867088c17b4SConrad Meyer }
2868088c17b4SConrad Meyer 
2869088c17b4SConrad Meyer /*
2870088c17b4SConrad Meyer  * Show a sysctl's name
2871088c17b4SConrad Meyer  */
2872088c17b4SConrad Meyer static void
db_show_oid_name(int * oid,size_t nlen)2873088c17b4SConrad Meyer db_show_oid_name(int *oid, size_t nlen)
2874088c17b4SConrad Meyer {
2875088c17b4SConrad Meyer 	struct sysctl_oid *oidp;
2876088c17b4SConrad Meyer 	int qoid[CTL_MAXNAME + 2];
2877088c17b4SConrad Meyer 	int error;
2878088c17b4SConrad Meyer 
2879e5f93d10SDoug Moore 	qoid[0] = CTL_SYSCTL;
2880e5f93d10SDoug Moore 	qoid[1] = CTL_SYSCTL_NAME;
2881088c17b4SConrad Meyer 	memcpy(qoid + 2, oid, nlen * sizeof(int));
2882088c17b4SConrad Meyer 
2883088c17b4SConrad Meyer 	error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2884088c17b4SConrad Meyer 	if (error)
2885088c17b4SConrad Meyer 		db_error("sysctl name oid");
2886088c17b4SConrad Meyer 
2887088c17b4SConrad Meyer 	error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2888088c17b4SConrad Meyer 	if (error)
2889088c17b4SConrad Meyer 		db_error("sysctl name");
2890088c17b4SConrad Meyer }
2891088c17b4SConrad Meyer 
2892088c17b4SConrad Meyer /*
2893088c17b4SConrad Meyer  * Check to see if an OID is safe to print from ddb.
2894088c17b4SConrad Meyer  */
2895088c17b4SConrad Meyer static bool
db_oid_safe(const struct sysctl_oid * oidp)2896088c17b4SConrad Meyer db_oid_safe(const struct sysctl_oid *oidp)
2897088c17b4SConrad Meyer {
2898088c17b4SConrad Meyer 	for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2899088c17b4SConrad Meyer 		if (oidp->oid_handler == db_safe_handlers[i])
2900088c17b4SConrad Meyer 			return (true);
2901088c17b4SConrad Meyer 	}
2902088c17b4SConrad Meyer 
2903088c17b4SConrad Meyer 	return (false);
2904088c17b4SConrad Meyer }
2905088c17b4SConrad Meyer 
2906088c17b4SConrad Meyer /*
2907088c17b4SConrad Meyer  * Show a sysctl at a specific OID
2908088c17b4SConrad Meyer  * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2909088c17b4SConrad Meyer  */
2910088c17b4SConrad Meyer static int
db_show_oid(struct sysctl_oid * oidp,int * oid,size_t nlen,int flags)2911088c17b4SConrad Meyer db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2912088c17b4SConrad Meyer {
2913088c17b4SConrad Meyer 	int error, xflag, oflag, Nflag, nflag;
2914088c17b4SConrad Meyer 	size_t len;
2915088c17b4SConrad Meyer 
2916088c17b4SConrad Meyer 	xflag = flags & DB_SYSCTL_HEX;
2917088c17b4SConrad Meyer 	oflag = flags & DB_SYSCTL_OPAQUE;
2918088c17b4SConrad Meyer 	nflag = flags & DB_SYSCTL_VALUE_ONLY;
2919088c17b4SConrad Meyer 	Nflag = flags & DB_SYSCTL_NAME_ONLY;
2920088c17b4SConrad Meyer 
2921088c17b4SConrad Meyer 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2922088c17b4SConrad Meyer 	    (!xflag && !oflag))
2923088c17b4SConrad Meyer 		return (0);
2924088c17b4SConrad Meyer 
2925088c17b4SConrad Meyer 	if (Nflag) {
2926088c17b4SConrad Meyer 		db_show_oid_name(oid, nlen);
2927088c17b4SConrad Meyer 		error = 0;
2928088c17b4SConrad Meyer 		goto out;
2929088c17b4SConrad Meyer 	}
2930088c17b4SConrad Meyer 
2931088c17b4SConrad Meyer 	if (!nflag) {
2932088c17b4SConrad Meyer 		db_show_oid_name(oid, nlen);
2933088c17b4SConrad Meyer 		db_printf(": ");
2934088c17b4SConrad Meyer 	}
2935088c17b4SConrad Meyer 
2936088c17b4SConrad Meyer 	if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2937088c17b4SConrad Meyer 		db_printf("Skipping, unsafe to print while recursing.");
2938088c17b4SConrad Meyer 		error = 0;
2939088c17b4SConrad Meyer 		goto out;
2940088c17b4SConrad Meyer 	}
2941088c17b4SConrad Meyer 
2942088c17b4SConrad Meyer 	/* Try once, and ask about the size */
2943088c17b4SConrad Meyer 	len = 0;
2944088c17b4SConrad Meyer 	error = db_sysctl(oidp, oid, nlen,
2945088c17b4SConrad Meyer 	    NULL, NULL, &len, flags);
2946088c17b4SConrad Meyer 	if (error)
2947088c17b4SConrad Meyer 		goto out;
2948088c17b4SConrad Meyer 
2949088c17b4SConrad Meyer 	if (!g_ddb_sysctl_printed)
2950088c17b4SConrad Meyer 		/* Lie about the size */
2951088c17b4SConrad Meyer 		error = db_sysctl(oidp, oid, nlen,
2952088c17b4SConrad Meyer 		    (void *) 1, &len, NULL, flags);
2953088c17b4SConrad Meyer 
2954088c17b4SConrad Meyer out:
2955088c17b4SConrad Meyer 	db_printf("\n");
2956088c17b4SConrad Meyer 	return (error);
2957088c17b4SConrad Meyer }
2958088c17b4SConrad Meyer 
2959088c17b4SConrad Meyer /*
2960088c17b4SConrad Meyer  * Show all sysctls under a specific OID
2961088c17b4SConrad Meyer  * Compare to sysctl_all from sbin/sysctl/sysctl.c
2962088c17b4SConrad Meyer  */
2963088c17b4SConrad Meyer static int
db_show_sysctl_all(int * oid,size_t len,int flags)2964088c17b4SConrad Meyer db_show_sysctl_all(int *oid, size_t len, int flags)
2965088c17b4SConrad Meyer {
2966088c17b4SConrad Meyer 	struct sysctl_oid *oidp;
2967e5f93d10SDoug Moore 	int qoid[CTL_MAXNAME + 2], next[CTL_MAXNAME];
2968e5f93d10SDoug Moore 	size_t nlen;
2969088c17b4SConrad Meyer 
2970e5f93d10SDoug Moore 	qoid[0] = CTL_SYSCTL;
2971e5f93d10SDoug Moore 	qoid[1] = CTL_SYSCTL_NEXT;
2972088c17b4SConrad Meyer 	if (len) {
2973e5f93d10SDoug Moore 		nlen = len;
2974e5f93d10SDoug Moore 		memcpy(&qoid[2], oid, nlen * sizeof(int));
2975088c17b4SConrad Meyer 	} else {
2976e5f93d10SDoug Moore 		nlen = 1;
2977e5f93d10SDoug Moore 		qoid[2] = CTL_KERN;
2978088c17b4SConrad Meyer 	}
2979088c17b4SConrad Meyer 	for (;;) {
2980e5f93d10SDoug Moore 		int error;
2981e5f93d10SDoug Moore 		size_t nextsize = sizeof(next);
2982088c17b4SConrad Meyer 
2983e5f93d10SDoug Moore 		error = kernel_sysctl(kdb_thread, qoid, nlen + 2,
2984e5f93d10SDoug Moore 		    next, &nextsize, NULL, 0, &nlen, 0);
2985088c17b4SConrad Meyer 		if (error != 0) {
2986088c17b4SConrad Meyer 			if (error == ENOENT)
2987088c17b4SConrad Meyer 				return (0);
2988088c17b4SConrad Meyer 			else
298992e17803SRyan Moeller 				db_error("sysctl(next)");
2990088c17b4SConrad Meyer 		}
2991088c17b4SConrad Meyer 
2992e5f93d10SDoug Moore 		nlen /= sizeof(int);
2993088c17b4SConrad Meyer 
2994e5f93d10SDoug Moore 		if (nlen < (unsigned int)len)
2995088c17b4SConrad Meyer 			return (0);
2996088c17b4SConrad Meyer 
2997e5f93d10SDoug Moore 		if (memcmp(&oid[0], &next[0], len * sizeof(int)) != 0)
2998088c17b4SConrad Meyer 			return (0);
2999088c17b4SConrad Meyer 
3000088c17b4SConrad Meyer 		/* Find the OID in question */
3001e5f93d10SDoug Moore 		error = sysctl_find_oid(next, nlen, &oidp, NULL, NULL);
3002088c17b4SConrad Meyer 		if (error)
3003088c17b4SConrad Meyer 			return (error);
3004088c17b4SConrad Meyer 
3005e5f93d10SDoug Moore 		(void)db_show_oid(oidp, next, nlen, flags | DB_SYSCTL_SAFE_ONLY);
3006088c17b4SConrad Meyer 
3007088c17b4SConrad Meyer 		if (db_pager_quit)
3008088c17b4SConrad Meyer 			return (0);
3009088c17b4SConrad Meyer 
3010e5f93d10SDoug Moore 		memcpy(&qoid[2 + len], &next[len], (nlen - len) * sizeof(int));
3011088c17b4SConrad Meyer 	}
3012088c17b4SConrad Meyer }
3013088c17b4SConrad Meyer 
3014088c17b4SConrad Meyer /*
3015088c17b4SConrad Meyer  * Show a sysctl by its user facing string
3016088c17b4SConrad Meyer  */
3017088c17b4SConrad Meyer static int
db_sysctlbyname(const char * name,int flags)3018f80babf9SAlexander Motin db_sysctlbyname(const char *name, int flags)
3019088c17b4SConrad Meyer {
3020088c17b4SConrad Meyer 	struct sysctl_oid *oidp;
3021088c17b4SConrad Meyer 	int oid[CTL_MAXNAME];
3022088c17b4SConrad Meyer 	int error, nlen;
3023088c17b4SConrad Meyer 
3024088c17b4SConrad Meyer 	error = name2oid(name, oid, &nlen, &oidp);
3025088c17b4SConrad Meyer 	if (error) {
3026088c17b4SConrad Meyer 		return (error);
3027088c17b4SConrad Meyer 	}
3028088c17b4SConrad Meyer 
3029088c17b4SConrad Meyer 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
3030088c17b4SConrad Meyer 		db_show_sysctl_all(oid, nlen, flags);
3031088c17b4SConrad Meyer 	} else {
3032088c17b4SConrad Meyer 		error = db_show_oid(oidp, oid, nlen, flags);
3033088c17b4SConrad Meyer 	}
3034088c17b4SConrad Meyer 
3035088c17b4SConrad Meyer 	return (error);
3036088c17b4SConrad Meyer }
3037088c17b4SConrad Meyer 
3038088c17b4SConrad Meyer static void
db_sysctl_cmd_usage(void)3039088c17b4SConrad Meyer db_sysctl_cmd_usage(void)
3040088c17b4SConrad Meyer {
3041088c17b4SConrad Meyer 	db_printf(
3042088c17b4SConrad Meyer 	    " sysctl [/Nnox] <sysctl>					    \n"
3043088c17b4SConrad Meyer 	    "								    \n"
3044088c17b4SConrad Meyer 	    " <sysctl> The name of the sysctl to show.			    \n"
3045088c17b4SConrad Meyer 	    "								    \n"
3046088c17b4SConrad Meyer 	    " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT.	    \n"
3047088c17b4SConrad Meyer 	    " This will work for most sysctls, but should not be used	    \n"
3048088c17b4SConrad Meyer 	    " with sysctls that are known to malloc.			    \n"
3049088c17b4SConrad Meyer 	    "								    \n"
3050088c17b4SConrad Meyer 	    " While recursing any \"unsafe\" sysctls will be skipped.	    \n"
3051088c17b4SConrad Meyer 	    " Call sysctl directly on the sysctl to try printing the	    \n"
3052088c17b4SConrad Meyer 	    " skipped sysctl. This is unsafe and may make the ddb	    \n"
3053088c17b4SConrad Meyer 	    " session unusable.						    \n"
3054088c17b4SConrad Meyer 	    "								    \n"
3055088c17b4SConrad Meyer 	    " Arguments:						    \n"
3056088c17b4SConrad Meyer 	    "	/N	Display only the name of the sysctl.		    \n"
3057088c17b4SConrad Meyer 	    "	/n	Display only the value of the sysctl.		    \n"
3058088c17b4SConrad Meyer 	    "	/o	Display opaque values.				    \n"
3059088c17b4SConrad Meyer 	    "	/x	Display the sysctl in hex.			    \n"
3060088c17b4SConrad Meyer 	    "								    \n"
3061088c17b4SConrad Meyer 	    "For example:						    \n"
3062088c17b4SConrad Meyer 	    "sysctl vm.v_free_min					    \n"
3063088c17b4SConrad Meyer 	    "vn.v_free_min: 12669					    \n"
3064088c17b4SConrad Meyer 	    );
3065088c17b4SConrad Meyer }
3066088c17b4SConrad Meyer 
3067088c17b4SConrad Meyer /*
3068088c17b4SConrad Meyer  * Show a specific sysctl similar to sysctl (8).
3069088c17b4SConrad Meyer  */
DB_COMMAND_FLAGS(sysctl,db_sysctl_cmd,CS_OWN)3070258958b3SMitchell Horne DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN)
3071088c17b4SConrad Meyer {
3072088c17b4SConrad Meyer 	char name[TOK_STRING_SIZE];
3073088c17b4SConrad Meyer 	int error, i, t, flags;
3074088c17b4SConrad Meyer 
3075088c17b4SConrad Meyer 	/* Parse the modifiers */
3076088c17b4SConrad Meyer 	t = db_read_token();
3077088c17b4SConrad Meyer 	if (t == tSLASH || t == tMINUS) {
3078088c17b4SConrad Meyer 		t = db_read_token();
3079088c17b4SConrad Meyer 		if (t != tIDENT) {
3080088c17b4SConrad Meyer 			db_printf("Bad modifier\n");
3081088c17b4SConrad Meyer 			error = EINVAL;
3082088c17b4SConrad Meyer 			goto out;
3083088c17b4SConrad Meyer 		}
3084088c17b4SConrad Meyer 		db_strcpy(modif, db_tok_string);
3085088c17b4SConrad Meyer 	}
3086088c17b4SConrad Meyer 	else {
3087088c17b4SConrad Meyer 		db_unread_token(t);
3088088c17b4SConrad Meyer 		modif[0] = '\0';
3089088c17b4SConrad Meyer 	}
3090088c17b4SConrad Meyer 
3091088c17b4SConrad Meyer 	flags = 0;
3092088c17b4SConrad Meyer 	for (i = 0; i < nitems(db_sysctl_modifs); i++) {
3093088c17b4SConrad Meyer 		if (strchr(modif, db_sysctl_modifs[i])) {
3094088c17b4SConrad Meyer 			flags |= db_sysctl_modif_values[i];
3095088c17b4SConrad Meyer 		}
3096088c17b4SConrad Meyer 	}
3097088c17b4SConrad Meyer 
3098088c17b4SConrad Meyer 	/* Parse the sysctl names */
3099088c17b4SConrad Meyer 	t = db_read_token();
3100088c17b4SConrad Meyer 	if (t != tIDENT) {
3101088c17b4SConrad Meyer 		db_printf("Need sysctl name\n");
3102088c17b4SConrad Meyer 		error = EINVAL;
3103088c17b4SConrad Meyer 		goto out;
3104088c17b4SConrad Meyer 	}
3105088c17b4SConrad Meyer 
3106088c17b4SConrad Meyer 	/* Copy the name into a temporary buffer */
3107088c17b4SConrad Meyer 	db_strcpy(name, db_tok_string);
3108088c17b4SConrad Meyer 
3109088c17b4SConrad Meyer 	/* Ensure there is no trailing cruft */
3110088c17b4SConrad Meyer 	t = db_read_token();
3111088c17b4SConrad Meyer 	if (t != tEOL) {
3112088c17b4SConrad Meyer 		db_printf("Unexpected sysctl argument\n");
3113088c17b4SConrad Meyer 		error = EINVAL;
3114088c17b4SConrad Meyer 		goto out;
3115088c17b4SConrad Meyer 	}
3116088c17b4SConrad Meyer 
3117088c17b4SConrad Meyer 	error = db_sysctlbyname(name, flags);
3118088c17b4SConrad Meyer 	if (error == ENOENT) {
3119088c17b4SConrad Meyer 		db_printf("unknown oid: '%s'\n", db_tok_string);
3120088c17b4SConrad Meyer 		goto out;
3121088c17b4SConrad Meyer 	} else if (error) {
3122088c17b4SConrad Meyer 		db_printf("%s: error: %d\n", db_tok_string, error);
3123088c17b4SConrad Meyer 		goto out;
3124088c17b4SConrad Meyer 	}
3125088c17b4SConrad Meyer 
3126088c17b4SConrad Meyer out:
3127088c17b4SConrad Meyer 	/* Ensure we eat all of our text */
3128088c17b4SConrad Meyer 	db_flush_lex();
3129088c17b4SConrad Meyer 
3130088c17b4SConrad Meyer 	if (error == EINVAL) {
3131088c17b4SConrad Meyer 		db_sysctl_cmd_usage();
3132088c17b4SConrad Meyer 	}
3133088c17b4SConrad Meyer }
3134088c17b4SConrad Meyer 
3135088c17b4SConrad Meyer #endif /* DDB */
3136