kern_sysctl.c revision 189707
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 189707 2009-03-11 21:48:36Z jhb $");
40
41#include "opt_compat.h"
42#include "opt_ktrace.h"
43#include "opt_mac.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/sysctl.h>
49#include <sys/malloc.h>
50#include <sys/priv.h>
51#include <sys/proc.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/sx.h>
55#include <sys/sysproto.h>
56#include <sys/uio.h>
57#include <sys/vimage.h>
58#ifdef KTRACE
59#include <sys/ktrace.h>
60#endif
61
62#include <security/mac/mac_framework.h>
63
64#include <vm/vm.h>
65#include <vm/vm_extern.h>
66
67static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
68static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
69static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
70
71/*
72 * The sysctllock protects the MIB tree.  It also protects sysctl
73 * contexts used with dynamic sysctls.  The sysctl_register_oid() and
74 * sysctl_unregister_oid() routines require the sysctllock to already
75 * be held, so the sysctl_lock() and sysctl_unlock() routines are
76 * provided for the few places in the kernel which need to use that
77 * API rather than using the dynamic API.  Use of the dynamic API is
78 * strongly encouraged for most code.
79 *
80 * This lock is also used to serialize userland sysctl requests.  Some
81 * sysctls wire user memory, and serializing the requests limits the
82 * amount of wired user memory in use.
83 */
84static struct sx sysctllock;
85
86#define	SYSCTL_SLOCK()		sx_slock(&sysctllock)
87#define	SYSCTL_SUNLOCK()	sx_sunlock(&sysctllock)
88#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
89#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
90#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
91#define	SYSCTL_ASSERT_LOCKED()	sx_assert(&sysctllock, SA_LOCKED)
92#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
93
94static int sysctl_root(SYSCTL_HANDLER_ARGS);
95
96struct sysctl_oid_list sysctl__children; /* root list */
97
98static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
99		    int recurse);
100
101static struct sysctl_oid *
102sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
103{
104	struct sysctl_oid *oidp;
105
106	SYSCTL_ASSERT_LOCKED();
107	SLIST_FOREACH(oidp, list, oid_link) {
108		if (strcmp(oidp->oid_name, name) == 0) {
109			return (oidp);
110		}
111	}
112	return (NULL);
113}
114
115/*
116 * Initialization of the MIB tree.
117 *
118 * Order by number in each list.
119 */
120void
121sysctl_lock(void)
122{
123
124	SYSCTL_XLOCK();
125}
126
127void
128sysctl_unlock(void)
129{
130
131	SYSCTL_XUNLOCK();
132}
133
134void
135sysctl_register_oid(struct sysctl_oid *oidp)
136{
137	struct sysctl_oid_list *parent = oidp->oid_parent;
138	struct sysctl_oid *p;
139	struct sysctl_oid *q;
140
141	/*
142	 * First check if another oid with the same name already
143	 * exists in the parent's list.
144	 */
145	SYSCTL_ASSERT_XLOCKED();
146	p = sysctl_find_oidname(oidp->oid_name, parent);
147	if (p != NULL) {
148		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
149			p->oid_refcnt++;
150			return;
151		} else {
152			printf("can't re-use a leaf (%s)!\n", p->oid_name);
153			return;
154		}
155	}
156	/*
157	 * If this oid has a number OID_AUTO, give it a number which
158	 * is greater than any current oid.
159	 * NOTE: DO NOT change the starting value here, change it in
160	 * <sys/sysctl.h>, and make sure it is at least 256 to
161	 * accomodate e.g. net.inet.raw as a static sysctl node.
162	 */
163	if (oidp->oid_number == OID_AUTO) {
164		static int newoid = CTL_AUTO_START;
165
166		oidp->oid_number = newoid++;
167		if (newoid == 0x7fffffff)
168			panic("out of oids");
169	}
170#if 0
171	else if (oidp->oid_number >= CTL_AUTO_START) {
172		/* do not panic; this happens when unregistering sysctl sets */
173		printf("static sysctl oid too high: %d", oidp->oid_number);
174	}
175#endif
176
177	/*
178	 * Insert the oid into the parent's list in order.
179	 */
180	q = NULL;
181	SLIST_FOREACH(p, parent, oid_link) {
182		if (oidp->oid_number < p->oid_number)
183			break;
184		q = p;
185	}
186	if (q)
187		SLIST_INSERT_AFTER(q, oidp, oid_link);
188	else
189		SLIST_INSERT_HEAD(parent, oidp, oid_link);
190}
191
192void
193sysctl_unregister_oid(struct sysctl_oid *oidp)
194{
195	struct sysctl_oid *p;
196	int error;
197
198	SYSCTL_ASSERT_XLOCKED();
199	error = ENOENT;
200	if (oidp->oid_number == OID_AUTO) {
201		error = EINVAL;
202	} else {
203		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
204			if (p == oidp) {
205				SLIST_REMOVE(oidp->oid_parent, oidp,
206				    sysctl_oid, oid_link);
207				error = 0;
208				break;
209			}
210		}
211	}
212
213	/*
214	 * This can happen when a module fails to register and is
215	 * being unloaded afterwards.  It should not be a panic()
216	 * for normal use.
217	 */
218	if (error)
219		printf("%s: failed to unregister sysctl\n", __func__);
220}
221
222/* Initialize a new context to keep track of dynamically added sysctls. */
223int
224sysctl_ctx_init(struct sysctl_ctx_list *c)
225{
226
227	if (c == NULL) {
228		return (EINVAL);
229	}
230
231	/*
232	 * No locking here, the caller is responsible for not adding
233	 * new nodes to a context until after this function has
234	 * returned.
235	 */
236	TAILQ_INIT(c);
237	return (0);
238}
239
240/* Free the context, and destroy all dynamic oids registered in this context */
241int
242sysctl_ctx_free(struct sysctl_ctx_list *clist)
243{
244	struct sysctl_ctx_entry *e, *e1;
245	int error;
246
247	error = 0;
248	/*
249	 * First perform a "dry run" to check if it's ok to remove oids.
250	 * XXX FIXME
251	 * XXX This algorithm is a hack. But I don't know any
252	 * XXX better solution for now...
253	 */
254	SYSCTL_XLOCK();
255	TAILQ_FOREACH(e, clist, link) {
256		error = sysctl_remove_oid_locked(e->entry, 0, 0);
257		if (error)
258			break;
259	}
260	/*
261	 * Restore deregistered entries, either from the end,
262	 * or from the place where error occured.
263	 * e contains the entry that was not unregistered
264	 */
265	if (error)
266		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
267	else
268		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
269	while (e1 != NULL) {
270		sysctl_register_oid(e1->entry);
271		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
272	}
273	if (error) {
274		SYSCTL_XUNLOCK();
275		return(EBUSY);
276	}
277	/* Now really delete the entries */
278	e = TAILQ_FIRST(clist);
279	while (e != NULL) {
280		e1 = TAILQ_NEXT(e, link);
281		error = sysctl_remove_oid_locked(e->entry, 1, 0);
282		if (error)
283			panic("sysctl_remove_oid: corrupt tree, entry: %s",
284			    e->entry->oid_name);
285		free(e, M_SYSCTLOID);
286		e = e1;
287	}
288	SYSCTL_XUNLOCK();
289	return (error);
290}
291
292/* Add an entry to the context */
293struct sysctl_ctx_entry *
294sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
295{
296	struct sysctl_ctx_entry *e;
297
298	SYSCTL_ASSERT_XLOCKED();
299	if (clist == NULL || oidp == NULL)
300		return(NULL);
301	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
302	e->entry = oidp;
303	TAILQ_INSERT_HEAD(clist, e, link);
304	return (e);
305}
306
307/* Find an entry in the context */
308struct sysctl_ctx_entry *
309sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
310{
311	struct sysctl_ctx_entry *e;
312
313	SYSCTL_ASSERT_LOCKED();
314	if (clist == NULL || oidp == NULL)
315		return(NULL);
316	TAILQ_FOREACH(e, clist, link) {
317		if(e->entry == oidp)
318			return(e);
319	}
320	return (e);
321}
322
323/*
324 * Delete an entry from the context.
325 * NOTE: this function doesn't free oidp! You have to remove it
326 * with sysctl_remove_oid().
327 */
328int
329sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
330{
331	struct sysctl_ctx_entry *e;
332
333	if (clist == NULL || oidp == NULL)
334		return (EINVAL);
335	SYSCTL_XLOCK();
336	e = sysctl_ctx_entry_find(clist, oidp);
337	if (e != NULL) {
338		TAILQ_REMOVE(clist, e, link);
339		SYSCTL_XUNLOCK();
340		free(e, M_SYSCTLOID);
341		return (0);
342	} else {
343		SYSCTL_XUNLOCK();
344		return (ENOENT);
345	}
346}
347
348/*
349 * Remove dynamically created sysctl trees.
350 * oidp - top of the tree to be removed
351 * del - if 0 - just deregister, otherwise free up entries as well
352 * recurse - if != 0 traverse the subtree to be deleted
353 */
354int
355sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
356{
357	int error;
358
359	SYSCTL_XLOCK();
360	error = sysctl_remove_oid_locked(oidp, del, recurse);
361	SYSCTL_XUNLOCK();
362	return (error);
363}
364
365static int
366sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
367{
368	struct sysctl_oid *p;
369	int error;
370
371	SYSCTL_ASSERT_XLOCKED();
372	if (oidp == NULL)
373		return(EINVAL);
374	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
375		printf("can't remove non-dynamic nodes!\n");
376		return (EINVAL);
377	}
378	/*
379	 * WARNING: normal method to do this should be through
380	 * sysctl_ctx_free(). Use recursing as the last resort
381	 * method to purge your sysctl tree of leftovers...
382	 * However, if some other code still references these nodes,
383	 * it will panic.
384	 */
385	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
386		if (oidp->oid_refcnt == 1) {
387			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
388				if (!recurse)
389					return (ENOTEMPTY);
390				error = sysctl_remove_oid_locked(p, del,
391				    recurse);
392				if (error)
393					return (error);
394			}
395			if (del)
396				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
397		}
398	}
399	if (oidp->oid_refcnt > 1 ) {
400		oidp->oid_refcnt--;
401	} else {
402		if (oidp->oid_refcnt == 0) {
403			printf("Warning: bad oid_refcnt=%u (%s)!\n",
404				oidp->oid_refcnt, oidp->oid_name);
405			return (EINVAL);
406		}
407		sysctl_unregister_oid(oidp);
408		if (del) {
409			if (oidp->oid_descr)
410				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
411			free((void *)(uintptr_t)(const void *)oidp->oid_name,
412			     M_SYSCTLOID);
413			free(oidp, M_SYSCTLOID);
414		}
415	}
416	return (0);
417}
418
419/*
420 * Create new sysctls at run time.
421 * clist may point to a valid context initialized with sysctl_ctx_init().
422 */
423struct sysctl_oid *
424sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
425	int number, const char *name, int kind, void *arg1, int arg2,
426	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
427{
428	struct sysctl_oid *oidp;
429	ssize_t len;
430	char *newname;
431
432	/* You have to hook up somewhere.. */
433	if (parent == NULL)
434		return(NULL);
435	/* Check if the node already exists, otherwise create it */
436	SYSCTL_XLOCK();
437	oidp = sysctl_find_oidname(name, parent);
438	if (oidp != NULL) {
439		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
440			oidp->oid_refcnt++;
441			/* Update the context */
442			if (clist != NULL)
443				sysctl_ctx_entry_add(clist, oidp);
444			SYSCTL_XUNLOCK();
445			return (oidp);
446		} else {
447			SYSCTL_XUNLOCK();
448			printf("can't re-use a leaf (%s)!\n", name);
449			return (NULL);
450		}
451	}
452	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
453	oidp->oid_parent = parent;
454	SLIST_NEXT(oidp, oid_link) = NULL;
455	oidp->oid_number = number;
456	oidp->oid_refcnt = 1;
457	len = strlen(name);
458	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
459	bcopy(name, newname, len + 1);
460	newname[len] = '\0';
461	oidp->oid_name = newname;
462	oidp->oid_handler = handler;
463	oidp->oid_kind = CTLFLAG_DYN | kind;
464	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
465		/* Allocate space for children */
466		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
467		    M_SYSCTLOID, M_WAITOK));
468		SLIST_INIT(SYSCTL_CHILDREN(oidp));
469	} else {
470		oidp->oid_arg1 = arg1;
471		oidp->oid_arg2 = arg2;
472	}
473	oidp->oid_fmt = fmt;
474	if (descr) {
475		int len = strlen(descr) + 1;
476		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
477		if (oidp->oid_descr)
478			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
479	}
480	/* Update the context, if used */
481	if (clist != NULL)
482		sysctl_ctx_entry_add(clist, oidp);
483	/* Register this oid */
484	sysctl_register_oid(oidp);
485	SYSCTL_XUNLOCK();
486	return (oidp);
487}
488
489/*
490 * Rename an existing oid.
491 */
492void
493sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
494{
495	ssize_t len;
496	char *newname;
497	void *oldname;
498
499	len = strlen(name);
500	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
501	bcopy(name, newname, len + 1);
502	newname[len] = '\0';
503	SYSCTL_XLOCK();
504	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
505	oidp->oid_name = newname;
506	SYSCTL_XUNLOCK();
507	free(oldname, M_SYSCTLOID);
508}
509
510/*
511 * Reparent an existing oid.
512 */
513int
514sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
515{
516	struct sysctl_oid *oidp;
517
518	SYSCTL_XLOCK();
519	if (oid->oid_parent == parent) {
520		SYSCTL_XUNLOCK();
521		return (0);
522	}
523	oidp = sysctl_find_oidname(oid->oid_name, parent);
524	if (oidp != NULL) {
525		SYSCTL_XUNLOCK();
526		return (EEXIST);
527	}
528	sysctl_unregister_oid(oid);
529	oid->oid_parent = parent;
530	oid->oid_number = OID_AUTO;
531	sysctl_register_oid(oid);
532	SYSCTL_XUNLOCK();
533	return (0);
534}
535
536/*
537 * Register the kernel's oids on startup.
538 */
539SET_DECLARE(sysctl_set, struct sysctl_oid);
540
541static void
542sysctl_register_all(void *arg)
543{
544	struct sysctl_oid **oidp;
545
546	SYSCTL_INIT();
547	SYSCTL_XLOCK();
548	SET_FOREACH(oidp, sysctl_set)
549		sysctl_register_oid(*oidp);
550	SYSCTL_XUNLOCK();
551}
552SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
553
554/*
555 * "Staff-functions"
556 *
557 * These functions implement a presently undocumented interface
558 * used by the sysctl program to walk the tree, and get the type
559 * so it can print the value.
560 * This interface is under work and consideration, and should probably
561 * be killed with a big axe by the first person who can find the time.
562 * (be aware though, that the proper interface isn't as obvious as it
563 * may seem, there are various conflicting requirements.
564 *
565 * {0,0}	printf the entire MIB-tree.
566 * {0,1,...}	return the name of the "..." OID.
567 * {0,2,...}	return the next OID.
568 * {0,3}	return the OID of the name in "new"
569 * {0,4,...}	return the kind & format info for the "..." OID.
570 * {0,5,...}	return the description the "..." OID.
571 */
572
573#ifdef SYSCTL_DEBUG
574static void
575sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
576{
577	int k;
578	struct sysctl_oid *oidp;
579
580	SYSCTL_ASSERT_LOCKED();
581	SLIST_FOREACH(oidp, l, oid_link) {
582
583		for (k=0; k<i; k++)
584			printf(" ");
585
586		printf("%d %s ", oidp->oid_number, oidp->oid_name);
587
588		printf("%c%c",
589			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
590			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
591
592		if (oidp->oid_handler)
593			printf(" *Handler");
594
595		switch (oidp->oid_kind & CTLTYPE) {
596			case CTLTYPE_NODE:
597				printf(" Node\n");
598				if (!oidp->oid_handler) {
599					sysctl_sysctl_debug_dump_node(
600						oidp->oid_arg1, i+2);
601				}
602				break;
603			case CTLTYPE_INT:    printf(" Int\n"); break;
604			case CTLTYPE_STRING: printf(" String\n"); break;
605			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
606			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
607			default:	     printf("\n");
608		}
609
610	}
611}
612
613static int
614sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
615{
616	int error;
617
618	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
619	if (error)
620		return (error);
621	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
622	return (ENOENT);
623}
624
625SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
626	0, 0, sysctl_sysctl_debug, "-", "");
627#endif
628
629static int
630sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
631{
632	int *name = (int *) arg1;
633	u_int namelen = arg2;
634	int error = 0;
635	struct sysctl_oid *oid;
636	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
637	char buf[10];
638
639	SYSCTL_ASSERT_LOCKED();
640	while (namelen) {
641		if (!lsp) {
642			snprintf(buf,sizeof(buf),"%d",*name);
643			if (req->oldidx)
644				error = SYSCTL_OUT(req, ".", 1);
645			if (!error)
646				error = SYSCTL_OUT(req, buf, strlen(buf));
647			if (error)
648				return (error);
649			namelen--;
650			name++;
651			continue;
652		}
653		lsp2 = 0;
654		SLIST_FOREACH(oid, lsp, oid_link) {
655			if (oid->oid_number != *name)
656				continue;
657
658			if (req->oldidx)
659				error = SYSCTL_OUT(req, ".", 1);
660			if (!error)
661				error = SYSCTL_OUT(req, oid->oid_name,
662					strlen(oid->oid_name));
663			if (error)
664				return (error);
665
666			namelen--;
667			name++;
668
669			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
670				break;
671
672			if (oid->oid_handler)
673				break;
674
675			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
676			break;
677		}
678		lsp = lsp2;
679	}
680	return (SYSCTL_OUT(req, "", 1));
681}
682
683static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
684
685static int
686sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
687	int *next, int *len, int level, struct sysctl_oid **oidpp)
688{
689	struct sysctl_oid *oidp;
690
691	SYSCTL_ASSERT_LOCKED();
692	*len = level;
693	SLIST_FOREACH(oidp, lsp, oid_link) {
694		*next = oidp->oid_number;
695		*oidpp = oidp;
696
697		if (oidp->oid_kind & CTLFLAG_SKIP)
698			continue;
699
700		if (!namelen) {
701			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
702				return (0);
703			if (oidp->oid_handler)
704				/* We really should call the handler here...*/
705				return (0);
706			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
707			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
708				len, level+1, oidpp))
709				return (0);
710			goto emptynode;
711		}
712
713		if (oidp->oid_number < *name)
714			continue;
715
716		if (oidp->oid_number > *name) {
717			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
718				return (0);
719			if (oidp->oid_handler)
720				return (0);
721			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
722			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
723				next+1, len, level+1, oidpp))
724				return (0);
725			goto next;
726		}
727		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
728			continue;
729
730		if (oidp->oid_handler)
731			continue;
732
733		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
734		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
735			len, level+1, oidpp))
736			return (0);
737	next:
738		namelen = 1;
739	emptynode:
740		*len = level;
741	}
742	return (1);
743}
744
745static int
746sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
747{
748	int *name = (int *) arg1;
749	u_int namelen = arg2;
750	int i, j, error;
751	struct sysctl_oid *oid;
752	struct sysctl_oid_list *lsp = &sysctl__children;
753	int newoid[CTL_MAXNAME];
754
755	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
756	if (i)
757		return (ENOENT);
758	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
759	return (error);
760}
761
762static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
763
764static int
765name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
766{
767	int i;
768	struct sysctl_oid *oidp;
769	struct sysctl_oid_list *lsp = &sysctl__children;
770	char *p;
771
772	SYSCTL_ASSERT_LOCKED();
773
774	if (!*name)
775		return (ENOENT);
776
777	p = name + strlen(name) - 1 ;
778	if (*p == '.')
779		*p = '\0';
780
781	*len = 0;
782
783	for (p = name; *p && *p != '.'; p++)
784		;
785	i = *p;
786	if (i == '.')
787		*p = '\0';
788
789	oidp = SLIST_FIRST(lsp);
790
791	while (oidp && *len < CTL_MAXNAME) {
792		if (strcmp(name, oidp->oid_name)) {
793			oidp = SLIST_NEXT(oidp, oid_link);
794			continue;
795		}
796		*oid++ = oidp->oid_number;
797		(*len)++;
798
799		if (!i) {
800			if (oidpp)
801				*oidpp = oidp;
802			return (0);
803		}
804
805		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
806			break;
807
808		if (oidp->oid_handler)
809			break;
810
811		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
812		oidp = SLIST_FIRST(lsp);
813		name = p+1;
814		for (p = name; *p && *p != '.'; p++)
815				;
816		i = *p;
817		if (i == '.')
818			*p = '\0';
819	}
820	return (ENOENT);
821}
822
823static int
824sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
825{
826	char *p;
827	int error, oid[CTL_MAXNAME], len;
828	struct sysctl_oid *op = 0;
829
830	SYSCTL_ASSERT_LOCKED();
831
832	if (!req->newlen)
833		return (ENOENT);
834	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
835		return (ENAMETOOLONG);
836
837	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
838
839	error = SYSCTL_IN(req, p, req->newlen);
840	if (error) {
841		free(p, M_SYSCTL);
842		return (error);
843	}
844
845	p [req->newlen] = '\0';
846
847	error = name2oid(p, oid, &len, &op);
848
849	free(p, M_SYSCTL);
850
851	if (error)
852		return (error);
853
854	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
855	return (error);
856}
857
858SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
859    0, 0, sysctl_sysctl_name2oid, "I", "");
860
861static int
862sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
863{
864	struct sysctl_oid *oid;
865	int error;
866
867	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
868	if (error)
869		return (error);
870
871	if (!oid->oid_fmt)
872		return (ENOENT);
873	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
874	if (error)
875		return (error);
876	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
877	return (error);
878}
879
880
881static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
882    sysctl_sysctl_oidfmt, "");
883
884static int
885sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
886{
887	struct sysctl_oid *oid;
888	int error;
889
890	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
891	if (error)
892		return (error);
893
894	if (!oid->oid_descr)
895		return (ENOENT);
896	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
897	return (error);
898}
899
900static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
901
902/*
903 * Default "handler" functions.
904 */
905
906/*
907 * Handle an int, signed or unsigned.
908 * Two cases:
909 *     a variable:  point arg1 at it.
910 *     a constant:  pass it in arg2.
911 */
912
913int
914sysctl_handle_int(SYSCTL_HANDLER_ARGS)
915{
916	int tmpout, error = 0;
917
918	/*
919	 * Attempt to get a coherent snapshot by making a copy of the data.
920	 */
921	if (arg1)
922		tmpout = *(int *)arg1;
923	else
924		tmpout = arg2;
925	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
926
927	if (error || !req->newptr)
928		return (error);
929
930	if (!arg1)
931		error = EPERM;
932	else
933		error = SYSCTL_IN(req, arg1, sizeof(int));
934	return (error);
935}
936
937
938/*
939 * Based on on sysctl_handle_int() convert milliseconds into ticks.
940 */
941
942int
943sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
944{
945	int error, s, tt;
946
947	tt = *(int *)oidp->oid_arg1;
948	s = (int)((int64_t)tt * 1000 / hz);
949
950	error = sysctl_handle_int(oidp, &s, 0, req);
951	if (error || !req->newptr)
952		return (error);
953
954	tt = (int)((int64_t)s * hz / 1000);
955	if (tt < 1)
956		return (EINVAL);
957
958	*(int *)oidp->oid_arg1 = tt;
959	return (0);
960}
961
962
963/*
964 * Handle a long, signed or unsigned.  arg1 points to it.
965 */
966
967int
968sysctl_handle_long(SYSCTL_HANDLER_ARGS)
969{
970	int error = 0;
971	long tmplong;
972#ifdef SCTL_MASK32
973	int tmpint;
974#endif
975
976	/*
977	 * Attempt to get a coherent snapshot by making a copy of the data.
978	 */
979	if (!arg1)
980		return (EINVAL);
981	tmplong = *(long *)arg1;
982#ifdef SCTL_MASK32
983	if (req->flags & SCTL_MASK32) {
984		tmpint = tmplong;
985		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
986	} else
987#endif
988		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
989
990	if (error || !req->newptr)
991		return (error);
992
993#ifdef SCTL_MASK32
994	if (req->flags & SCTL_MASK32) {
995		error = SYSCTL_IN(req, &tmpint, sizeof(int));
996		*(long *)arg1 = (long)tmpint;
997	} else
998#endif
999		error = SYSCTL_IN(req, arg1, sizeof(long));
1000	return (error);
1001}
1002
1003/*
1004 * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1005 */
1006
1007int
1008sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1009{
1010	int error = 0;
1011	uint64_t tmpout;
1012
1013	/*
1014	 * Attempt to get a coherent snapshot by making a copy of the data.
1015	 */
1016	if (!arg1)
1017		return (EINVAL);
1018	tmpout = *(uint64_t *)arg1;
1019	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1020
1021	if (error || !req->newptr)
1022		return (error);
1023
1024	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1025	return (error);
1026}
1027
1028/*
1029 * Handle our generic '\0' terminated 'C' string.
1030 * Two cases:
1031 * 	a variable string:  point arg1 at it, arg2 is max length.
1032 * 	a constant string:  point arg1 at it, arg2 is zero.
1033 */
1034
1035int
1036sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1037{
1038	int error=0;
1039	char *tmparg;
1040	size_t outlen;
1041
1042	/*
1043	 * Attempt to get a coherent snapshot by copying to a
1044	 * temporary kernel buffer.
1045	 */
1046retry:
1047	outlen = strlen((char *)arg1)+1;
1048	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1049
1050	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1051		free(tmparg, M_SYSCTLTMP);
1052		goto retry;
1053	}
1054
1055	error = SYSCTL_OUT(req, tmparg, outlen);
1056	free(tmparg, M_SYSCTLTMP);
1057
1058	if (error || !req->newptr)
1059		return (error);
1060
1061	if ((req->newlen - req->newidx) >= arg2) {
1062		error = EINVAL;
1063	} else {
1064		arg2 = (req->newlen - req->newidx);
1065		error = SYSCTL_IN(req, arg1, arg2);
1066		((char *)arg1)[arg2] = '\0';
1067	}
1068
1069	return (error);
1070}
1071
1072/*
1073 * Handle any kind of opaque data.
1074 * arg1 points to it, arg2 is the size.
1075 */
1076
1077int
1078sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1079{
1080	int error, tries;
1081	u_int generation;
1082	struct sysctl_req req2;
1083
1084	/*
1085	 * Attempt to get a coherent snapshot, by using the thread
1086	 * pre-emption counter updated from within mi_switch() to
1087	 * determine if we were pre-empted during a bcopy() or
1088	 * copyout(). Make 3 attempts at doing this before giving up.
1089	 * If we encounter an error, stop immediately.
1090	 */
1091	tries = 0;
1092	req2 = *req;
1093retry:
1094	generation = curthread->td_generation;
1095	error = SYSCTL_OUT(req, arg1, arg2);
1096	if (error)
1097		return (error);
1098	tries++;
1099	if (generation != curthread->td_generation && tries < 3) {
1100		*req = req2;
1101		goto retry;
1102	}
1103
1104	error = SYSCTL_IN(req, arg1, arg2);
1105
1106	return (error);
1107}
1108
1109/*
1110 * Transfer functions to/from kernel space.
1111 * XXX: rather untested at this point
1112 */
1113static int
1114sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1115{
1116	size_t i = 0;
1117
1118	if (req->oldptr) {
1119		i = l;
1120		if (req->oldlen <= req->oldidx)
1121			i = 0;
1122		else
1123			if (i > req->oldlen - req->oldidx)
1124				i = req->oldlen - req->oldidx;
1125		if (i > 0)
1126			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1127	}
1128	req->oldidx += l;
1129	if (req->oldptr && i != l)
1130		return (ENOMEM);
1131	return (0);
1132}
1133
1134static int
1135sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1136{
1137	if (!req->newptr)
1138		return (0);
1139	if (req->newlen - req->newidx < l)
1140		return (EINVAL);
1141	bcopy((char *)req->newptr + req->newidx, p, l);
1142	req->newidx += l;
1143	return (0);
1144}
1145
1146int
1147kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1148    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1149{
1150	int error = 0;
1151	struct sysctl_req req;
1152
1153	bzero(&req, sizeof req);
1154
1155	req.td = td;
1156	req.flags = flags;
1157
1158	if (oldlenp) {
1159		req.oldlen = *oldlenp;
1160	}
1161	req.validlen = req.oldlen;
1162
1163	if (old) {
1164		req.oldptr= old;
1165	}
1166
1167	if (new != NULL) {
1168		req.newlen = newlen;
1169		req.newptr = new;
1170	}
1171
1172	req.oldfunc = sysctl_old_kernel;
1173	req.newfunc = sysctl_new_kernel;
1174	req.lock = REQ_LOCKED;
1175
1176	SYSCTL_SLOCK();
1177	error = sysctl_root(0, name, namelen, &req);
1178	SYSCTL_SUNLOCK();
1179
1180	if (req.lock == REQ_WIRED && req.validlen > 0)
1181		vsunlock(req.oldptr, req.validlen);
1182
1183	if (error && error != ENOMEM)
1184		return (error);
1185
1186	if (retval) {
1187		if (req.oldptr && req.oldidx > req.validlen)
1188			*retval = req.validlen;
1189		else
1190			*retval = req.oldidx;
1191	}
1192	return (error);
1193}
1194
1195int
1196kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1197    void *new, size_t newlen, size_t *retval, int flags)
1198{
1199        int oid[CTL_MAXNAME];
1200        size_t oidlen, plen;
1201	int error;
1202
1203	oid[0] = 0;		/* sysctl internal magic */
1204	oid[1] = 3;		/* name2oid */
1205	oidlen = sizeof(oid);
1206
1207	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1208	    (void *)name, strlen(name), &plen, flags);
1209	if (error)
1210		return (error);
1211
1212	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1213	    new, newlen, retval, flags);
1214	return (error);
1215}
1216
1217/*
1218 * Transfer function to/from user space.
1219 */
1220static int
1221sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1222{
1223	int error = 0;
1224	size_t i, len, origidx;
1225
1226	origidx = req->oldidx;
1227	req->oldidx += l;
1228	if (req->oldptr == NULL)
1229		return (0);
1230	/*
1231	 * If we have not wired the user supplied buffer and we are currently
1232	 * holding locks, drop a witness warning, as it's possible that
1233	 * write operations to the user page can sleep.
1234	 */
1235	if (req->lock != REQ_WIRED)
1236		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1237		    "sysctl_old_user()");
1238	i = l;
1239	len = req->validlen;
1240	if (len <= origidx)
1241		i = 0;
1242	else {
1243		if (i > len - origidx)
1244			i = len - origidx;
1245		error = copyout(p, (char *)req->oldptr + origidx, i);
1246	}
1247	if (error)
1248		return (error);
1249	if (i < l)
1250		return (ENOMEM);
1251	return (0);
1252}
1253
1254static int
1255sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1256{
1257	int error;
1258
1259	if (!req->newptr)
1260		return (0);
1261	if (req->newlen - req->newidx < l)
1262		return (EINVAL);
1263	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1264	    "sysctl_new_user()");
1265	error = copyin((char *)req->newptr + req->newidx, p, l);
1266	req->newidx += l;
1267	return (error);
1268}
1269
1270/*
1271 * Wire the user space destination buffer.  If set to a value greater than
1272 * zero, the len parameter limits the maximum amount of wired memory.
1273 */
1274int
1275sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1276{
1277	int ret;
1278	size_t i, wiredlen;
1279	char *cp, dummy;
1280
1281	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1282	ret = 0;
1283	if (req->lock == REQ_LOCKED && req->oldptr &&
1284	    req->oldfunc == sysctl_old_user) {
1285		if (wiredlen != 0) {
1286			ret = vslock(req->oldptr, wiredlen);
1287			if (ret != 0) {
1288				if (ret != ENOMEM)
1289					return (ret);
1290				wiredlen = 0;
1291			}
1292			/*
1293			 * Touch all the wired pages to avoid PTE modified
1294			 * bit emulation traps on Alpha while holding locks
1295			 * in the sysctl handler.
1296			 */
1297			for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1298			    cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1299				copyin(cp, &dummy, 1);
1300				copyout(&dummy, cp, 1);
1301			}
1302		}
1303		req->lock = REQ_WIRED;
1304		req->validlen = wiredlen;
1305	}
1306	return (0);
1307}
1308
1309int
1310sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1311    int *nindx, struct sysctl_req *req)
1312{
1313	struct sysctl_oid *oid;
1314	int indx;
1315
1316	SYSCTL_ASSERT_LOCKED();
1317	oid = SLIST_FIRST(&sysctl__children);
1318	indx = 0;
1319	while (oid && indx < CTL_MAXNAME) {
1320		if (oid->oid_number == name[indx]) {
1321			indx++;
1322			if (oid->oid_kind & CTLFLAG_NOLOCK)
1323				req->lock = REQ_UNLOCKED;
1324			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1325				if (oid->oid_handler != NULL ||
1326				    indx == namelen) {
1327					*noid = oid;
1328					if (nindx != NULL)
1329						*nindx = indx;
1330					return (0);
1331				}
1332				oid = SLIST_FIRST(
1333				    (struct sysctl_oid_list *)oid->oid_arg1);
1334			} else if (indx == namelen) {
1335				*noid = oid;
1336				if (nindx != NULL)
1337					*nindx = indx;
1338				return (0);
1339			} else {
1340				return (ENOTDIR);
1341			}
1342		} else {
1343			oid = SLIST_NEXT(oid, oid_link);
1344		}
1345	}
1346	return (ENOENT);
1347}
1348
1349/*
1350 * Traverse our tree, and find the right node, execute whatever it points
1351 * to, and return the resulting error code.
1352 */
1353
1354static int
1355sysctl_root(SYSCTL_HANDLER_ARGS)
1356{
1357	struct sysctl_oid *oid;
1358	int error, indx, lvl;
1359
1360	SYSCTL_ASSERT_LOCKED();
1361
1362	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1363	if (error)
1364		return (error);
1365
1366	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1367		/*
1368		 * You can't call a sysctl when it's a node, but has
1369		 * no handler.  Inform the user that it's a node.
1370		 * The indx may or may not be the same as namelen.
1371		 */
1372		if (oid->oid_handler == NULL)
1373			return (EISDIR);
1374	}
1375
1376	/* Is this sysctl writable? */
1377	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1378		return (EPERM);
1379
1380	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1381
1382	/* Is this sysctl sensitive to securelevels? */
1383	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1384		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1385		error = securelevel_gt(req->td->td_ucred, lvl);
1386		if (error)
1387			return (error);
1388	}
1389
1390	/* Is this sysctl writable by only privileged users? */
1391	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1392		if (oid->oid_kind & CTLFLAG_PRISON)
1393			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1394		else
1395			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1396		if (error)
1397			return (error);
1398	}
1399
1400	if (!oid->oid_handler)
1401		return (EINVAL);
1402
1403	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1404		arg1 = (int *)arg1 + indx;
1405		arg2 -= indx;
1406	} else {
1407		arg1 = oid->oid_arg1;
1408		arg2 = oid->oid_arg2;
1409	}
1410#ifdef MAC
1411	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1412	    req);
1413	if (error != 0)
1414		return (error);
1415#endif
1416	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1417		mtx_lock(&Giant);
1418	error = oid->oid_handler(oid, arg1, arg2, req);
1419	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1420		mtx_unlock(&Giant);
1421
1422	return (error);
1423}
1424
1425#ifndef _SYS_SYSPROTO_H_
1426struct sysctl_args {
1427	int	*name;
1428	u_int	namelen;
1429	void	*old;
1430	size_t	*oldlenp;
1431	void	*new;
1432	size_t	newlen;
1433};
1434#endif
1435int
1436__sysctl(struct thread *td, struct sysctl_args *uap)
1437{
1438	int error, i, name[CTL_MAXNAME];
1439	size_t j;
1440
1441	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1442		return (EINVAL);
1443
1444 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1445 	if (error)
1446		return (error);
1447
1448	error = userland_sysctl(td, name, uap->namelen,
1449		uap->old, uap->oldlenp, 0,
1450		uap->new, uap->newlen, &j, 0);
1451	if (error && error != ENOMEM)
1452		return (error);
1453	if (uap->oldlenp) {
1454		i = copyout(&j, uap->oldlenp, sizeof(j));
1455		if (i)
1456			return (i);
1457	}
1458	return (error);
1459}
1460
1461/*
1462 * This is used from various compatibility syscalls too.  That's why name
1463 * must be in kernel space.
1464 */
1465int
1466userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1467    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1468    int flags)
1469{
1470	int error = 0;
1471	struct sysctl_req req;
1472
1473	bzero(&req, sizeof req);
1474
1475	req.td = td;
1476	req.flags = flags;
1477
1478	if (oldlenp) {
1479		if (inkernel) {
1480			req.oldlen = *oldlenp;
1481		} else {
1482			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1483			if (error)
1484				return (error);
1485		}
1486	}
1487	req.validlen = req.oldlen;
1488
1489	if (old) {
1490		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1491			return (EFAULT);
1492		req.oldptr= old;
1493	}
1494
1495	if (new != NULL) {
1496		if (!useracc(new, newlen, VM_PROT_READ))
1497			return (EFAULT);
1498		req.newlen = newlen;
1499		req.newptr = new;
1500	}
1501
1502	req.oldfunc = sysctl_old_user;
1503	req.newfunc = sysctl_new_user;
1504	req.lock = REQ_LOCKED;
1505
1506#ifdef KTRACE
1507	if (KTRPOINT(curthread, KTR_SYSCTL))
1508		ktrsysctl(name, namelen);
1509#endif
1510
1511	SYSCTL_XLOCK();
1512	CURVNET_SET(TD_TO_VNET(curthread));
1513
1514	for (;;) {
1515		req.oldidx = 0;
1516		req.newidx = 0;
1517		error = sysctl_root(0, name, namelen, &req);
1518		if (error != EAGAIN)
1519			break;
1520		uio_yield();
1521	}
1522
1523	CURVNET_RESTORE();
1524
1525	if (req.lock == REQ_WIRED && req.validlen > 0)
1526		vsunlock(req.oldptr, req.validlen);
1527	SYSCTL_XUNLOCK();
1528
1529	if (error && error != ENOMEM)
1530		return (error);
1531
1532	if (retval) {
1533		if (req.oldptr && req.oldidx > req.validlen)
1534			*retval = req.validlen;
1535		else
1536			*retval = req.oldidx;
1537	}
1538	return (error);
1539}
1540