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