kern_sysctl.c revision 194252
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 194252 2009-06-15 19:01:53Z jamie $");
40
41#include "opt_compat.h"
42#include "opt_ktrace.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#ifdef KTRACE
58#include <sys/ktrace.h>
59#endif
60
61#include <security/mac/mac_framework.h>
62
63#include <vm/vm.h>
64#include <vm/vm_extern.h>
65
66static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
67static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
68static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
69
70/*
71 * The sysctllock protects the MIB tree.  It also protects sysctl
72 * contexts used with dynamic sysctls.  The sysctl_register_oid() and
73 * sysctl_unregister_oid() routines require the sysctllock to already
74 * be held, so the sysctl_lock() and sysctl_unlock() routines are
75 * provided for the few places in the kernel which need to use that
76 * API rather than using the dynamic API.  Use of the dynamic API is
77 * strongly encouraged for most code.
78 *
79 * The sysctlmemlock is used to limit the amount of user memory wired for
80 * sysctl requests.  This is implemented by serializing any userland
81 * sysctl requests larger than a single page via an exclusive lock.
82 */
83static struct sx sysctllock;
84static struct sx sysctlmemlock;
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	sx_init(&sysctlmemlock, "sysctl mem");
547	SYSCTL_INIT();
548	SYSCTL_XLOCK();
549	SET_FOREACH(oidp, sysctl_set)
550		sysctl_register_oid(*oidp);
551	SYSCTL_XUNLOCK();
552}
553SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
554
555/*
556 * "Staff-functions"
557 *
558 * These functions implement a presently undocumented interface
559 * used by the sysctl program to walk the tree, and get the type
560 * so it can print the value.
561 * This interface is under work and consideration, and should probably
562 * be killed with a big axe by the first person who can find the time.
563 * (be aware though, that the proper interface isn't as obvious as it
564 * may seem, there are various conflicting requirements.
565 *
566 * {0,0}	printf the entire MIB-tree.
567 * {0,1,...}	return the name of the "..." OID.
568 * {0,2,...}	return the next OID.
569 * {0,3}	return the OID of the name in "new"
570 * {0,4,...}	return the kind & format info for the "..." OID.
571 * {0,5,...}	return the description the "..." OID.
572 */
573
574#ifdef SYSCTL_DEBUG
575static void
576sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
577{
578	int k;
579	struct sysctl_oid *oidp;
580
581	SYSCTL_ASSERT_LOCKED();
582	SLIST_FOREACH(oidp, l, oid_link) {
583
584		for (k=0; k<i; k++)
585			printf(" ");
586
587		printf("%d %s ", oidp->oid_number, oidp->oid_name);
588
589		printf("%c%c",
590			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
591			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
592
593		if (oidp->oid_handler)
594			printf(" *Handler");
595
596		switch (oidp->oid_kind & CTLTYPE) {
597			case CTLTYPE_NODE:
598				printf(" Node\n");
599				if (!oidp->oid_handler) {
600					sysctl_sysctl_debug_dump_node(
601						oidp->oid_arg1, i+2);
602				}
603				break;
604			case CTLTYPE_INT:    printf(" Int\n"); break;
605			case CTLTYPE_STRING: printf(" String\n"); break;
606			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
607			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
608			default:	     printf("\n");
609		}
610
611	}
612}
613
614static int
615sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
616{
617	int error;
618
619	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
620	if (error)
621		return (error);
622	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
623	return (ENOENT);
624}
625
626SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
627	0, 0, sysctl_sysctl_debug, "-", "");
628#endif
629
630static int
631sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
632{
633	int *name = (int *) arg1;
634	u_int namelen = arg2;
635	int error = 0;
636	struct sysctl_oid *oid;
637	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
638	char buf[10];
639
640	SYSCTL_ASSERT_LOCKED();
641	while (namelen) {
642		if (!lsp) {
643			snprintf(buf,sizeof(buf),"%d",*name);
644			if (req->oldidx)
645				error = SYSCTL_OUT(req, ".", 1);
646			if (!error)
647				error = SYSCTL_OUT(req, buf, strlen(buf));
648			if (error)
649				return (error);
650			namelen--;
651			name++;
652			continue;
653		}
654		lsp2 = 0;
655		SLIST_FOREACH(oid, lsp, oid_link) {
656			if (oid->oid_number != *name)
657				continue;
658
659			if (req->oldidx)
660				error = SYSCTL_OUT(req, ".", 1);
661			if (!error)
662				error = SYSCTL_OUT(req, oid->oid_name,
663					strlen(oid->oid_name));
664			if (error)
665				return (error);
666
667			namelen--;
668			name++;
669
670			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
671				break;
672
673			if (oid->oid_handler)
674				break;
675
676			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
677			break;
678		}
679		lsp = lsp2;
680	}
681	return (SYSCTL_OUT(req, "", 1));
682}
683
684static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
685
686static int
687sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
688	int *next, int *len, int level, struct sysctl_oid **oidpp)
689{
690	struct sysctl_oid *oidp;
691
692	SYSCTL_ASSERT_LOCKED();
693	*len = level;
694	SLIST_FOREACH(oidp, lsp, oid_link) {
695		*next = oidp->oid_number;
696		*oidpp = oidp;
697
698		if (oidp->oid_kind & CTLFLAG_SKIP)
699			continue;
700
701		if (!namelen) {
702			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
703				return (0);
704			if (oidp->oid_handler)
705				/* We really should call the handler here...*/
706				return (0);
707			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
708			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
709				len, level+1, oidpp))
710				return (0);
711			goto emptynode;
712		}
713
714		if (oidp->oid_number < *name)
715			continue;
716
717		if (oidp->oid_number > *name) {
718			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
719				return (0);
720			if (oidp->oid_handler)
721				return (0);
722			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
723			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
724				next+1, len, level+1, oidpp))
725				return (0);
726			goto next;
727		}
728		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
729			continue;
730
731		if (oidp->oid_handler)
732			continue;
733
734		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
735		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
736			len, level+1, oidpp))
737			return (0);
738	next:
739		namelen = 1;
740	emptynode:
741		*len = level;
742	}
743	return (1);
744}
745
746static int
747sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
748{
749	int *name = (int *) arg1;
750	u_int namelen = arg2;
751	int i, j, error;
752	struct sysctl_oid *oid;
753	struct sysctl_oid_list *lsp = &sysctl__children;
754	int newoid[CTL_MAXNAME];
755
756	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
757	if (i)
758		return (ENOENT);
759	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
760	return (error);
761}
762
763static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
764
765static int
766name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
767{
768	int i;
769	struct sysctl_oid *oidp;
770	struct sysctl_oid_list *lsp = &sysctl__children;
771	char *p;
772
773	SYSCTL_ASSERT_LOCKED();
774
775	if (!*name)
776		return (ENOENT);
777
778	p = name + strlen(name) - 1 ;
779	if (*p == '.')
780		*p = '\0';
781
782	*len = 0;
783
784	for (p = name; *p && *p != '.'; p++)
785		;
786	i = *p;
787	if (i == '.')
788		*p = '\0';
789
790	oidp = SLIST_FIRST(lsp);
791
792	while (oidp && *len < CTL_MAXNAME) {
793		if (strcmp(name, oidp->oid_name)) {
794			oidp = SLIST_NEXT(oidp, oid_link);
795			continue;
796		}
797		*oid++ = oidp->oid_number;
798		(*len)++;
799
800		if (!i) {
801			if (oidpp)
802				*oidpp = oidp;
803			return (0);
804		}
805
806		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
807			break;
808
809		if (oidp->oid_handler)
810			break;
811
812		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
813		oidp = SLIST_FIRST(lsp);
814		name = p+1;
815		for (p = name; *p && *p != '.'; p++)
816				;
817		i = *p;
818		if (i == '.')
819			*p = '\0';
820	}
821	return (ENOENT);
822}
823
824static int
825sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
826{
827	char *p;
828	int error, oid[CTL_MAXNAME], len;
829	struct sysctl_oid *op = 0;
830
831	SYSCTL_ASSERT_LOCKED();
832
833	if (!req->newlen)
834		return (ENOENT);
835	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
836		return (ENAMETOOLONG);
837
838	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
839
840	error = SYSCTL_IN(req, p, req->newlen);
841	if (error) {
842		free(p, M_SYSCTL);
843		return (error);
844	}
845
846	p [req->newlen] = '\0';
847
848	error = name2oid(p, oid, &len, &op);
849
850	free(p, M_SYSCTL);
851
852	if (error)
853		return (error);
854
855	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
856	return (error);
857}
858
859SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
860    0, 0, sysctl_sysctl_name2oid, "I", "");
861
862static int
863sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
864{
865	struct sysctl_oid *oid;
866	int error;
867
868	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
869	if (error)
870		return (error);
871
872	if (!oid->oid_fmt)
873		return (ENOENT);
874	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
875	if (error)
876		return (error);
877	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
878	return (error);
879}
880
881
882static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
883    sysctl_sysctl_oidfmt, "");
884
885static int
886sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
887{
888	struct sysctl_oid *oid;
889	int error;
890
891	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
892	if (error)
893		return (error);
894
895	if (!oid->oid_descr)
896		return (ENOENT);
897	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
898	return (error);
899}
900
901static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
902
903/*
904 * Default "handler" functions.
905 */
906
907/*
908 * Handle an int, signed or unsigned.
909 * Two cases:
910 *     a variable:  point arg1 at it.
911 *     a constant:  pass it in arg2.
912 */
913
914int
915sysctl_handle_int(SYSCTL_HANDLER_ARGS)
916{
917	int tmpout, error = 0;
918
919	/*
920	 * Attempt to get a coherent snapshot by making a copy of the data.
921	 */
922	if (arg1)
923		tmpout = *(int *)arg1;
924	else
925		tmpout = arg2;
926	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
927
928	if (error || !req->newptr)
929		return (error);
930
931	if (!arg1)
932		error = EPERM;
933	else
934		error = SYSCTL_IN(req, arg1, sizeof(int));
935	return (error);
936}
937
938#ifdef VIMAGE
939int
940sysctl_handle_v_int(SYSCTL_HANDLER_ARGS)
941{
942	int tmpout, error = 0;
943
944	SYSCTL_RESOLVE_V_ARG1();
945
946	/*
947	 * Attempt to get a coherent snapshot by making a copy of the data.
948	 */
949	tmpout = *(int *)arg1;
950	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
951
952	if (error || !req->newptr)
953		return (error);
954
955	if (!arg1)
956		error = EPERM;
957	else
958		error = SYSCTL_IN(req, arg1, sizeof(int));
959	return (error);
960}
961#endif
962
963/*
964 * Based on on sysctl_handle_int() convert milliseconds into ticks.
965 */
966
967int
968sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
969{
970	int error, s, tt;
971
972	SYSCTL_RESOLVE_V_ARG1();
973
974	tt = *(int *)arg1;
975	s = (int)((int64_t)tt * 1000 / hz);
976
977	error = sysctl_handle_int(oidp, &s, 0, req);
978	if (error || !req->newptr)
979		return (error);
980
981	tt = (int)((int64_t)s * hz / 1000);
982	if (tt < 1)
983		return (EINVAL);
984
985	*(int *)arg1 = tt;
986	return (0);
987}
988
989
990/*
991 * Handle a long, signed or unsigned.  arg1 points to it.
992 */
993
994int
995sysctl_handle_long(SYSCTL_HANDLER_ARGS)
996{
997	int error = 0;
998	long tmplong;
999#ifdef SCTL_MASK32
1000	int tmpint;
1001#endif
1002
1003	/*
1004	 * Attempt to get a coherent snapshot by making a copy of the data.
1005	 */
1006	if (!arg1)
1007		return (EINVAL);
1008	tmplong = *(long *)arg1;
1009#ifdef SCTL_MASK32
1010	if (req->flags & SCTL_MASK32) {
1011		tmpint = tmplong;
1012		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1013	} else
1014#endif
1015		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1016
1017	if (error || !req->newptr)
1018		return (error);
1019
1020#ifdef SCTL_MASK32
1021	if (req->flags & SCTL_MASK32) {
1022		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1023		*(long *)arg1 = (long)tmpint;
1024	} else
1025#endif
1026		error = SYSCTL_IN(req, arg1, sizeof(long));
1027	return (error);
1028}
1029
1030/*
1031 * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1032 */
1033
1034int
1035sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1036{
1037	int error = 0;
1038	uint64_t tmpout;
1039
1040	/*
1041	 * Attempt to get a coherent snapshot by making a copy of the data.
1042	 */
1043	if (!arg1)
1044		return (EINVAL);
1045	tmpout = *(uint64_t *)arg1;
1046	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1047
1048	if (error || !req->newptr)
1049		return (error);
1050
1051	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1052	return (error);
1053}
1054
1055/*
1056 * Handle our generic '\0' terminated 'C' string.
1057 * Two cases:
1058 * 	a variable string:  point arg1 at it, arg2 is max length.
1059 * 	a constant string:  point arg1 at it, arg2 is zero.
1060 */
1061
1062int
1063sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1064{
1065	int error=0;
1066	char *tmparg;
1067	size_t outlen;
1068
1069	/*
1070	 * Attempt to get a coherent snapshot by copying to a
1071	 * temporary kernel buffer.
1072	 */
1073retry:
1074	outlen = strlen((char *)arg1)+1;
1075	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1076
1077	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1078		free(tmparg, M_SYSCTLTMP);
1079		goto retry;
1080	}
1081
1082	error = SYSCTL_OUT(req, tmparg, outlen);
1083	free(tmparg, M_SYSCTLTMP);
1084
1085	if (error || !req->newptr)
1086		return (error);
1087
1088	if ((req->newlen - req->newidx) >= arg2) {
1089		error = EINVAL;
1090	} else {
1091		arg2 = (req->newlen - req->newidx);
1092		error = SYSCTL_IN(req, arg1, arg2);
1093		((char *)arg1)[arg2] = '\0';
1094	}
1095
1096	return (error);
1097}
1098
1099#ifdef VIMAGE
1100int
1101sysctl_handle_v_string(SYSCTL_HANDLER_ARGS)
1102{
1103	int error=0;
1104	char *tmparg;
1105	size_t outlen;
1106
1107	SYSCTL_RESOLVE_V_ARG1();
1108
1109	/*
1110	 * Attempt to get a coherent snapshot by copying to a
1111	 * temporary kernel buffer.
1112	 */
1113retry:
1114	outlen = strlen((char *)arg1)+1;
1115	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1116
1117	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1118		free(tmparg, M_SYSCTLTMP);
1119		goto retry;
1120	}
1121
1122	error = SYSCTL_OUT(req, tmparg, outlen);
1123	free(tmparg, M_SYSCTLTMP);
1124
1125	if (error || !req->newptr)
1126		return (error);
1127
1128	if ((req->newlen - req->newidx) >= arg2) {
1129		error = EINVAL;
1130	} else {
1131		arg2 = (req->newlen - req->newidx);
1132		error = SYSCTL_IN(req, arg1, arg2);
1133		((char *)arg1)[arg2] = '\0';
1134	}
1135
1136	return (error);
1137}
1138#endif
1139
1140/*
1141 * Handle any kind of opaque data.
1142 * arg1 points to it, arg2 is the size.
1143 */
1144
1145int
1146sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1147{
1148	int error, tries;
1149	u_int generation;
1150	struct sysctl_req req2;
1151
1152	/*
1153	 * Attempt to get a coherent snapshot, by using the thread
1154	 * pre-emption counter updated from within mi_switch() to
1155	 * determine if we were pre-empted during a bcopy() or
1156	 * copyout(). Make 3 attempts at doing this before giving up.
1157	 * If we encounter an error, stop immediately.
1158	 */
1159	tries = 0;
1160	req2 = *req;
1161retry:
1162	generation = curthread->td_generation;
1163	error = SYSCTL_OUT(req, arg1, arg2);
1164	if (error)
1165		return (error);
1166	tries++;
1167	if (generation != curthread->td_generation && tries < 3) {
1168		*req = req2;
1169		goto retry;
1170	}
1171
1172	error = SYSCTL_IN(req, arg1, arg2);
1173
1174	return (error);
1175}
1176
1177#ifdef VIMAGE
1178int
1179sysctl_handle_v_opaque(SYSCTL_HANDLER_ARGS)
1180{
1181	int error, tries;
1182	u_int generation;
1183	struct sysctl_req req2;
1184
1185	SYSCTL_RESOLVE_V_ARG1();
1186
1187	tries = 0;
1188	req2 = *req;
1189retry:
1190	generation = curthread->td_generation;
1191	error = SYSCTL_OUT(req, arg1, arg2);
1192	if (error)
1193		return (error);
1194	tries++;
1195	if (generation != curthread->td_generation && tries < 3) {
1196		*req = req2;
1197		goto retry;
1198	}
1199
1200	error = SYSCTL_IN(req, arg1, arg2);
1201
1202	return (error);
1203}
1204#endif
1205
1206/*
1207 * Transfer functions to/from kernel space.
1208 * XXX: rather untested at this point
1209 */
1210static int
1211sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1212{
1213	size_t i = 0;
1214
1215	if (req->oldptr) {
1216		i = l;
1217		if (req->oldlen <= req->oldidx)
1218			i = 0;
1219		else
1220			if (i > req->oldlen - req->oldidx)
1221				i = req->oldlen - req->oldidx;
1222		if (i > 0)
1223			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1224	}
1225	req->oldidx += l;
1226	if (req->oldptr && i != l)
1227		return (ENOMEM);
1228	return (0);
1229}
1230
1231static int
1232sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1233{
1234	if (!req->newptr)
1235		return (0);
1236	if (req->newlen - req->newidx < l)
1237		return (EINVAL);
1238	bcopy((char *)req->newptr + req->newidx, p, l);
1239	req->newidx += l;
1240	return (0);
1241}
1242
1243int
1244kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1245    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1246{
1247	int error = 0;
1248	struct sysctl_req req;
1249
1250	bzero(&req, sizeof req);
1251
1252	req.td = td;
1253	req.flags = flags;
1254
1255	if (oldlenp) {
1256		req.oldlen = *oldlenp;
1257	}
1258	req.validlen = req.oldlen;
1259
1260	if (old) {
1261		req.oldptr= old;
1262	}
1263
1264	if (new != NULL) {
1265		req.newlen = newlen;
1266		req.newptr = new;
1267	}
1268
1269	req.oldfunc = sysctl_old_kernel;
1270	req.newfunc = sysctl_new_kernel;
1271	req.lock = REQ_LOCKED;
1272
1273	SYSCTL_SLOCK();
1274	error = sysctl_root(0, name, namelen, &req);
1275	SYSCTL_SUNLOCK();
1276
1277	if (req.lock == REQ_WIRED && req.validlen > 0)
1278		vsunlock(req.oldptr, req.validlen);
1279
1280	if (error && error != ENOMEM)
1281		return (error);
1282
1283	if (retval) {
1284		if (req.oldptr && req.oldidx > req.validlen)
1285			*retval = req.validlen;
1286		else
1287			*retval = req.oldidx;
1288	}
1289	return (error);
1290}
1291
1292int
1293kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1294    void *new, size_t newlen, size_t *retval, int flags)
1295{
1296        int oid[CTL_MAXNAME];
1297        size_t oidlen, plen;
1298	int error;
1299
1300	oid[0] = 0;		/* sysctl internal magic */
1301	oid[1] = 3;		/* name2oid */
1302	oidlen = sizeof(oid);
1303
1304	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1305	    (void *)name, strlen(name), &plen, flags);
1306	if (error)
1307		return (error);
1308
1309	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1310	    new, newlen, retval, flags);
1311	return (error);
1312}
1313
1314/*
1315 * Transfer function to/from user space.
1316 */
1317static int
1318sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1319{
1320	int error = 0;
1321	size_t i, len, origidx;
1322
1323	origidx = req->oldidx;
1324	req->oldidx += l;
1325	if (req->oldptr == NULL)
1326		return (0);
1327	/*
1328	 * If we have not wired the user supplied buffer and we are currently
1329	 * holding locks, drop a witness warning, as it's possible that
1330	 * write operations to the user page can sleep.
1331	 */
1332	if (req->lock != REQ_WIRED)
1333		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1334		    "sysctl_old_user()");
1335	i = l;
1336	len = req->validlen;
1337	if (len <= origidx)
1338		i = 0;
1339	else {
1340		if (i > len - origidx)
1341			i = len - origidx;
1342		error = copyout(p, (char *)req->oldptr + origidx, i);
1343	}
1344	if (error)
1345		return (error);
1346	if (i < l)
1347		return (ENOMEM);
1348	return (0);
1349}
1350
1351static int
1352sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1353{
1354	int error;
1355
1356	if (!req->newptr)
1357		return (0);
1358	if (req->newlen - req->newidx < l)
1359		return (EINVAL);
1360	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1361	    "sysctl_new_user()");
1362	error = copyin((char *)req->newptr + req->newidx, p, l);
1363	req->newidx += l;
1364	return (error);
1365}
1366
1367/*
1368 * Wire the user space destination buffer.  If set to a value greater than
1369 * zero, the len parameter limits the maximum amount of wired memory.
1370 */
1371int
1372sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1373{
1374	int ret;
1375	size_t wiredlen;
1376
1377	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1378	ret = 0;
1379	if (req->lock == REQ_LOCKED && req->oldptr &&
1380	    req->oldfunc == sysctl_old_user) {
1381		if (wiredlen != 0) {
1382			ret = vslock(req->oldptr, wiredlen);
1383			if (ret != 0) {
1384				if (ret != ENOMEM)
1385					return (ret);
1386				wiredlen = 0;
1387			}
1388		}
1389		req->lock = REQ_WIRED;
1390		req->validlen = wiredlen;
1391	}
1392	return (0);
1393}
1394
1395int
1396sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1397    int *nindx, struct sysctl_req *req)
1398{
1399	struct sysctl_oid *oid;
1400	int indx;
1401
1402	SYSCTL_ASSERT_LOCKED();
1403	oid = SLIST_FIRST(&sysctl__children);
1404	indx = 0;
1405	while (oid && indx < CTL_MAXNAME) {
1406		if (oid->oid_number == name[indx]) {
1407			indx++;
1408			if (oid->oid_kind & CTLFLAG_NOLOCK)
1409				req->lock = REQ_UNLOCKED;
1410			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1411				if (oid->oid_handler != NULL ||
1412				    indx == namelen) {
1413					*noid = oid;
1414					if (nindx != NULL)
1415						*nindx = indx;
1416					return (0);
1417				}
1418				oid = SLIST_FIRST(
1419				    (struct sysctl_oid_list *)oid->oid_arg1);
1420			} else if (indx == namelen) {
1421				*noid = oid;
1422				if (nindx != NULL)
1423					*nindx = indx;
1424				return (0);
1425			} else {
1426				return (ENOTDIR);
1427			}
1428		} else {
1429			oid = SLIST_NEXT(oid, oid_link);
1430		}
1431	}
1432	return (ENOENT);
1433}
1434
1435/*
1436 * Traverse our tree, and find the right node, execute whatever it points
1437 * to, and return the resulting error code.
1438 */
1439
1440static int
1441sysctl_root(SYSCTL_HANDLER_ARGS)
1442{
1443	struct sysctl_oid *oid;
1444	int error, indx, lvl;
1445
1446	SYSCTL_ASSERT_LOCKED();
1447
1448	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1449	if (error)
1450		return (error);
1451
1452	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1453		/*
1454		 * You can't call a sysctl when it's a node, but has
1455		 * no handler.  Inform the user that it's a node.
1456		 * The indx may or may not be the same as namelen.
1457		 */
1458		if (oid->oid_handler == NULL)
1459			return (EISDIR);
1460	}
1461
1462	/* Is this sysctl writable? */
1463	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1464		return (EPERM);
1465
1466	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1467
1468	/* Is this sysctl sensitive to securelevels? */
1469	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1470		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1471		error = securelevel_gt(req->td->td_ucred, lvl);
1472		if (error)
1473			return (error);
1474	}
1475
1476	/* Is this sysctl writable by only privileged users? */
1477	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1478		if (oid->oid_kind & CTLFLAG_PRISON)
1479			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1480		else
1481			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1482		if (error)
1483			return (error);
1484	}
1485
1486	if (!oid->oid_handler)
1487		return (EINVAL);
1488
1489	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1490		arg1 = (int *)arg1 + indx;
1491		arg2 -= indx;
1492	} else {
1493		arg1 = oid->oid_arg1;
1494		arg2 = oid->oid_arg2;
1495	}
1496#ifdef MAC
1497	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1498	    req);
1499	if (error != 0)
1500		return (error);
1501#endif
1502	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1503		mtx_lock(&Giant);
1504	error = oid->oid_handler(oid, arg1, arg2, req);
1505	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1506		mtx_unlock(&Giant);
1507
1508	return (error);
1509}
1510
1511#ifndef _SYS_SYSPROTO_H_
1512struct sysctl_args {
1513	int	*name;
1514	u_int	namelen;
1515	void	*old;
1516	size_t	*oldlenp;
1517	void	*new;
1518	size_t	newlen;
1519};
1520#endif
1521int
1522__sysctl(struct thread *td, struct sysctl_args *uap)
1523{
1524	int error, i, name[CTL_MAXNAME];
1525	size_t j;
1526
1527	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1528		return (EINVAL);
1529
1530 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1531 	if (error)
1532		return (error);
1533
1534	error = userland_sysctl(td, name, uap->namelen,
1535		uap->old, uap->oldlenp, 0,
1536		uap->new, uap->newlen, &j, 0);
1537	if (error && error != ENOMEM)
1538		return (error);
1539	if (uap->oldlenp) {
1540		i = copyout(&j, uap->oldlenp, sizeof(j));
1541		if (i)
1542			return (i);
1543	}
1544	return (error);
1545}
1546
1547/*
1548 * This is used from various compatibility syscalls too.  That's why name
1549 * must be in kernel space.
1550 */
1551int
1552userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1553    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1554    int flags)
1555{
1556	int error = 0, memlocked;
1557	struct sysctl_req req;
1558
1559	bzero(&req, sizeof req);
1560
1561	req.td = td;
1562	req.flags = flags;
1563
1564	if (oldlenp) {
1565		if (inkernel) {
1566			req.oldlen = *oldlenp;
1567		} else {
1568			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1569			if (error)
1570				return (error);
1571		}
1572	}
1573	req.validlen = req.oldlen;
1574
1575	if (old) {
1576		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1577			return (EFAULT);
1578		req.oldptr= old;
1579	}
1580
1581	if (new != NULL) {
1582		if (!useracc(new, newlen, VM_PROT_READ))
1583			return (EFAULT);
1584		req.newlen = newlen;
1585		req.newptr = new;
1586	}
1587
1588	req.oldfunc = sysctl_old_user;
1589	req.newfunc = sysctl_new_user;
1590	req.lock = REQ_LOCKED;
1591
1592#ifdef KTRACE
1593	if (KTRPOINT(curthread, KTR_SYSCTL))
1594		ktrsysctl(name, namelen);
1595#endif
1596
1597	if (req.oldlen > PAGE_SIZE) {
1598		memlocked = 1;
1599		sx_xlock(&sysctlmemlock);
1600	} else
1601		memlocked = 0;
1602	CURVNET_SET(TD_TO_VNET(td));
1603
1604	for (;;) {
1605		req.oldidx = 0;
1606		req.newidx = 0;
1607		SYSCTL_SLOCK();
1608		error = sysctl_root(0, name, namelen, &req);
1609		SYSCTL_SUNLOCK();
1610		if (error != EAGAIN)
1611			break;
1612		uio_yield();
1613	}
1614
1615	CURVNET_RESTORE();
1616
1617	if (req.lock == REQ_WIRED && req.validlen > 0)
1618		vsunlock(req.oldptr, req.validlen);
1619	if (memlocked)
1620		sx_xunlock(&sysctlmemlock);
1621
1622	if (error && error != ENOMEM)
1623		return (error);
1624
1625	if (retval) {
1626		if (req.oldptr && req.oldidx > req.validlen)
1627			*retval = req.validlen;
1628		else
1629			*retval = req.oldidx;
1630	}
1631	return (error);
1632}
1633