kern_sysctl.c revision 216060
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Mike Karels at Berkeley Software Design, Inc.
71541Srgrimes *
812623Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
912623Sphk * project, to make these variables more userfriendly.
1012623Sphk *
111541Srgrimes * Redistribution and use in source and binary forms, with or without
121541Srgrimes * modification, are permitted provided that the following conditions
131541Srgrimes * are met:
141541Srgrimes * 1. Redistributions of source code must retain the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer.
161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171541Srgrimes *    notice, this list of conditions and the following disclaimer in the
181541Srgrimes *    documentation and/or other materials provided with the distribution.
191541Srgrimes * 4. Neither the name of the University nor the names of its contributors
201541Srgrimes *    may be used to endorse or promote products derived from this software
211541Srgrimes *    without specific prior written permission.
221541Srgrimes *
231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331541Srgrimes * SUCH DAMAGE.
341541Srgrimes *
351541Srgrimes *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
361541Srgrimes */
371541Srgrimes
38116182Sobrien#include <sys/cdefs.h>
39116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 216060 2010-11-29 18:18:07Z mdf $");
40116182Sobrien
4131778Seivind#include "opt_compat.h"
42189707Sjhb#include "opt_ktrace.h"
4331778Seivind
441541Srgrimes#include <sys/param.h>
45216060Smdf#include <sys/fail.h>
4648274Speter#include <sys/systm.h>
4748274Speter#include <sys/kernel.h>
481541Srgrimes#include <sys/sysctl.h>
4912623Sphk#include <sys/malloc.h>
50164033Srwatson#include <sys/priv.h>
5112662Sdg#include <sys/proc.h>
52194368Sbz#include <sys/jail.h>
5382746Sdillon#include <sys/lock.h>
5482746Sdillon#include <sys/mutex.h>
55212750Smdf#include <sys/sbuf.h>
5693616Salfred#include <sys/sx.h>
5715103Sphk#include <sys/sysproto.h>
58185983Skib#include <sys/uio.h>
59189707Sjhb#ifdef KTRACE
60189707Sjhb#include <sys/ktrace.h>
61189707Sjhb#endif
62163606Srwatson
63195699Srwatson#include <net/vnet.h>
64195699Srwatson
65163606Srwatson#include <security/mac/mac_framework.h>
66163606Srwatson
6712645Sbde#include <vm/vm.h>
6812662Sdg#include <vm/vm_extern.h>
6912645Sbde
7030354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
7163212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
72100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
7330309Sphk
7412429Sphk/*
75188232Sjhb * The sysctllock protects the MIB tree.  It also protects sysctl
76188232Sjhb * contexts used with dynamic sysctls.  The sysctl_register_oid() and
77188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already
78188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are
79188232Sjhb * provided for the few places in the kernel which need to use that
80188232Sjhb * API rather than using the dynamic API.  Use of the dynamic API is
81188232Sjhb * strongly encouraged for most code.
82188232Sjhb *
83192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for
84192125Sjhb * sysctl requests.  This is implemented by serializing any userland
85192125Sjhb * sysctl requests larger than a single page via an exclusive lock.
8612429Sphk */
8793625Srwatsonstatic struct sx sysctllock;
88192125Sjhbstatic struct sx sysctlmemlock;
8912429Sphk
90188232Sjhb#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
91188232Sjhb#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
92188232Sjhb#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
93112107Sjhb#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
94216060Smdf#define	SYSCTL_SLEEP(ch, wmesg, timo)					\
95216060Smdf				sx_sleep(ch, &sysctllock, 0, wmesg, timo)
9693616Salfred
9762573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS);
9812429Sphk
9944078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */
10012152Sphk
101188232Sjhbstatic int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
102188232Sjhb		    int recurse);
103188232Sjhb
10463212Sabialstatic struct sysctl_oid *
10563212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
10663212Sabial{
10763212Sabial	struct sysctl_oid *oidp;
10863212Sabial
109216060Smdf	SYSCTL_ASSERT_XLOCKED();
11063212Sabial	SLIST_FOREACH(oidp, list, oid_link) {
11163212Sabial		if (strcmp(oidp->oid_name, name) == 0) {
11263212Sabial			return (oidp);
11363212Sabial		}
11463212Sabial	}
11563212Sabial	return (NULL);
11663212Sabial}
11763212Sabial
11812623Sphk/*
11912623Sphk * Initialization of the MIB tree.
12012623Sphk *
12144078Sdfr * Order by number in each list.
12212623Sphk */
123188232Sjhbvoid
124188232Sjhbsysctl_lock(void)
125188232Sjhb{
12612429Sphk
127188232Sjhb	SYSCTL_XLOCK();
128188232Sjhb}
129188232Sjhb
13080338Sroamvoid
131188232Sjhbsysctl_unlock(void)
132188232Sjhb{
133188232Sjhb
134188232Sjhb	SYSCTL_XUNLOCK();
135188232Sjhb}
136188232Sjhb
137188232Sjhbvoid
13880338Sroamsysctl_register_oid(struct sysctl_oid *oidp)
13912152Sphk{
14044078Sdfr	struct sysctl_oid_list *parent = oidp->oid_parent;
14144078Sdfr	struct sysctl_oid *p;
14244078Sdfr	struct sysctl_oid *q;
14312197Sbde
14444078Sdfr	/*
14563212Sabial	 * First check if another oid with the same name already
14663212Sabial	 * exists in the parent's list.
14763212Sabial	 */
148188232Sjhb	SYSCTL_ASSERT_XLOCKED();
14963212Sabial	p = sysctl_find_oidname(oidp->oid_name, parent);
15063212Sabial	if (p != NULL) {
15163212Sabial		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
15263212Sabial			p->oid_refcnt++;
15363212Sabial			return;
15463212Sabial		} else {
15563212Sabial			printf("can't re-use a leaf (%s)!\n", p->oid_name);
15663212Sabial			return;
15763212Sabial		}
15863212Sabial	}
15963212Sabial	/*
16044078Sdfr	 * If this oid has a number OID_AUTO, give it a number which
16180339Sroam	 * is greater than any current oid.
16280339Sroam	 * NOTE: DO NOT change the starting value here, change it in
16380339Sroam	 * <sys/sysctl.h>, and make sure it is at least 256 to
16480339Sroam	 * accomodate e.g. net.inet.raw as a static sysctl node.
16544078Sdfr	 */
16644078Sdfr	if (oidp->oid_number == OID_AUTO) {
16780339Sroam		static int newoid = CTL_AUTO_START;
16871510Smckusick
16971510Smckusick		oidp->oid_number = newoid++;
17071510Smckusick		if (newoid == 0x7fffffff)
17171510Smckusick			panic("out of oids");
17244078Sdfr	}
17384832Sroam#if 0
17484832Sroam	else if (oidp->oid_number >= CTL_AUTO_START) {
17584832Sroam		/* do not panic; this happens when unregistering sysctl sets */
17684832Sroam		printf("static sysctl oid too high: %d", oidp->oid_number);
17784832Sroam	}
17884832Sroam#endif
17944078Sdfr
18044078Sdfr	/*
18144078Sdfr	 * Insert the oid into the parent's list in order.
18244078Sdfr	 */
18344078Sdfr	q = NULL;
18444078Sdfr	SLIST_FOREACH(p, parent, oid_link) {
18544078Sdfr		if (oidp->oid_number < p->oid_number)
18644078Sdfr			break;
18744078Sdfr		q = p;
18844078Sdfr	}
18944078Sdfr	if (q)
19044078Sdfr		SLIST_INSERT_AFTER(q, oidp, oid_link);
19144078Sdfr	else
19244078Sdfr		SLIST_INSERT_HEAD(parent, oidp, oid_link);
19312152Sphk}
19412131Sphk
19580338Sroamvoid
19680338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp)
19712152Sphk{
198115391Smux	struct sysctl_oid *p;
199115391Smux	int error;
200115391Smux
201188232Sjhb	SYSCTL_ASSERT_XLOCKED();
202115391Smux	error = ENOENT;
203115391Smux	if (oidp->oid_number == OID_AUTO) {
204115391Smux		error = EINVAL;
205115391Smux	} else {
206115391Smux		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
207115391Smux			if (p == oidp) {
208115391Smux				SLIST_REMOVE(oidp->oid_parent, oidp,
209115391Smux				    sysctl_oid, oid_link);
210115391Smux				error = 0;
211115391Smux				break;
212115391Smux			}
213115391Smux		}
214115391Smux	}
215115391Smux
216115391Smux	/*
217115391Smux	 * This can happen when a module fails to register and is
218115391Smux	 * being unloaded afterwards.  It should not be a panic()
219115391Smux	 * for normal use.
220115391Smux	 */
221115391Smux	if (error)
222115391Smux		printf("%s: failed to unregister sysctl\n", __func__);
22344078Sdfr}
22412152Sphk
22563212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */
22663212Sabialint
22763212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c)
22863212Sabial{
22963212Sabial
23063212Sabial	if (c == NULL) {
23163212Sabial		return (EINVAL);
23263212Sabial	}
233188232Sjhb
234188232Sjhb	/*
235188232Sjhb	 * No locking here, the caller is responsible for not adding
236188232Sjhb	 * new nodes to a context until after this function has
237188232Sjhb	 * returned.
238188232Sjhb	 */
23963212Sabial	TAILQ_INIT(c);
24063212Sabial	return (0);
24163212Sabial}
24263212Sabial
24363212Sabial/* Free the context, and destroy all dynamic oids registered in this context */
24463212Sabialint
24563212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist)
24663212Sabial{
24763212Sabial	struct sysctl_ctx_entry *e, *e1;
24863212Sabial	int error;
24963212Sabial
25063212Sabial	error = 0;
25163212Sabial	/*
25263212Sabial	 * First perform a "dry run" to check if it's ok to remove oids.
25363212Sabial	 * XXX FIXME
25463212Sabial	 * XXX This algorithm is a hack. But I don't know any
25563212Sabial	 * XXX better solution for now...
25663212Sabial	 */
257188232Sjhb	SYSCTL_XLOCK();
25863212Sabial	TAILQ_FOREACH(e, clist, link) {
259188232Sjhb		error = sysctl_remove_oid_locked(e->entry, 0, 0);
26063212Sabial		if (error)
26163212Sabial			break;
26263212Sabial	}
26363212Sabial	/*
26463212Sabial	 * Restore deregistered entries, either from the end,
26563212Sabial	 * or from the place where error occured.
26663212Sabial	 * e contains the entry that was not unregistered
26763212Sabial	 */
26863212Sabial	if (error)
26963212Sabial		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
27063212Sabial	else
27163212Sabial		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
27263212Sabial	while (e1 != NULL) {
27363212Sabial		sysctl_register_oid(e1->entry);
27463212Sabial		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
27563212Sabial	}
276188232Sjhb	if (error) {
277188232Sjhb		SYSCTL_XUNLOCK();
27863212Sabial		return(EBUSY);
279188232Sjhb	}
28063212Sabial	/* Now really delete the entries */
28163212Sabial	e = TAILQ_FIRST(clist);
28263212Sabial	while (e != NULL) {
28363212Sabial		e1 = TAILQ_NEXT(e, link);
284188232Sjhb		error = sysctl_remove_oid_locked(e->entry, 1, 0);
28563212Sabial		if (error)
28663212Sabial			panic("sysctl_remove_oid: corrupt tree, entry: %s",
28763212Sabial			    e->entry->oid_name);
28863212Sabial		free(e, M_SYSCTLOID);
28963212Sabial		e = e1;
29063212Sabial	}
291188232Sjhb	SYSCTL_XUNLOCK();
29263212Sabial	return (error);
29363212Sabial}
29463212Sabial
29563212Sabial/* Add an entry to the context */
29663212Sabialstruct sysctl_ctx_entry *
29763212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
29863212Sabial{
29963212Sabial	struct sysctl_ctx_entry *e;
30063212Sabial
301188232Sjhb	SYSCTL_ASSERT_XLOCKED();
30263212Sabial	if (clist == NULL || oidp == NULL)
30363212Sabial		return(NULL);
304111119Simp	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
30563212Sabial	e->entry = oidp;
30663212Sabial	TAILQ_INSERT_HEAD(clist, e, link);
30763212Sabial	return (e);
30863212Sabial}
30963212Sabial
31063212Sabial/* Find an entry in the context */
31163212Sabialstruct sysctl_ctx_entry *
31263212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
31363212Sabial{
31463212Sabial	struct sysctl_ctx_entry *e;
31563212Sabial
316216060Smdf	SYSCTL_ASSERT_XLOCKED();
31763212Sabial	if (clist == NULL || oidp == NULL)
31863212Sabial		return(NULL);
31971999Sphk	TAILQ_FOREACH(e, clist, link) {
32063212Sabial		if(e->entry == oidp)
32163212Sabial			return(e);
32263212Sabial	}
32363212Sabial	return (e);
32463212Sabial}
32563212Sabial
32644078Sdfr/*
32763212Sabial * Delete an entry from the context.
32863212Sabial * NOTE: this function doesn't free oidp! You have to remove it
32963212Sabial * with sysctl_remove_oid().
33063212Sabial */
33163212Sabialint
33263212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
33363212Sabial{
33463212Sabial	struct sysctl_ctx_entry *e;
33563212Sabial
33663212Sabial	if (clist == NULL || oidp == NULL)
33763212Sabial		return (EINVAL);
338188232Sjhb	SYSCTL_XLOCK();
33963212Sabial	e = sysctl_ctx_entry_find(clist, oidp);
34063212Sabial	if (e != NULL) {
34163212Sabial		TAILQ_REMOVE(clist, e, link);
342188232Sjhb		SYSCTL_XUNLOCK();
34363212Sabial		free(e, M_SYSCTLOID);
34463212Sabial		return (0);
345188232Sjhb	} else {
346188232Sjhb		SYSCTL_XUNLOCK();
34763212Sabial		return (ENOENT);
348188232Sjhb	}
34963212Sabial}
35063212Sabial
35163212Sabial/*
35263212Sabial * Remove dynamically created sysctl trees.
35363212Sabial * oidp - top of the tree to be removed
35463212Sabial * del - if 0 - just deregister, otherwise free up entries as well
35563212Sabial * recurse - if != 0 traverse the subtree to be deleted
35663212Sabial */
35763212Sabialint
35863212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
35963212Sabial{
360188232Sjhb	int error;
361188232Sjhb
362188232Sjhb	SYSCTL_XLOCK();
363188232Sjhb	error = sysctl_remove_oid_locked(oidp, del, recurse);
364188232Sjhb	SYSCTL_XUNLOCK();
365188232Sjhb	return (error);
366188232Sjhb}
367188232Sjhb
368188232Sjhbstatic int
369188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
370188232Sjhb{
37163212Sabial	struct sysctl_oid *p;
37263212Sabial	int error;
37363212Sabial
374188232Sjhb	SYSCTL_ASSERT_XLOCKED();
37563212Sabial	if (oidp == NULL)
37663212Sabial		return(EINVAL);
37763212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
37863212Sabial		printf("can't remove non-dynamic nodes!\n");
37963212Sabial		return (EINVAL);
38063212Sabial	}
38163212Sabial	/*
38263212Sabial	 * WARNING: normal method to do this should be through
38363212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
38463212Sabial	 * method to purge your sysctl tree of leftovers...
38563212Sabial	 * However, if some other code still references these nodes,
38663212Sabial	 * it will panic.
38763212Sabial	 */
38863212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
38963212Sabial		if (oidp->oid_refcnt == 1) {
39063212Sabial			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
39163212Sabial				if (!recurse)
39263212Sabial					return (ENOTEMPTY);
393188232Sjhb				error = sysctl_remove_oid_locked(p, del,
394188232Sjhb				    recurse);
39563212Sabial				if (error)
39663212Sabial					return (error);
39763212Sabial			}
39863212Sabial			if (del)
39963212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
40063212Sabial		}
40163212Sabial	}
40263212Sabial	if (oidp->oid_refcnt > 1 ) {
40363212Sabial		oidp->oid_refcnt--;
40463212Sabial	} else {
40563212Sabial		if (oidp->oid_refcnt == 0) {
40663212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
40763212Sabial				oidp->oid_refcnt, oidp->oid_name);
40863212Sabial			return (EINVAL);
40963212Sabial		}
41063212Sabial		sysctl_unregister_oid(oidp);
41163212Sabial		if (del) {
412216060Smdf			/*
413216060Smdf			 * Wait for all threads running the handler to drain.
414216060Smdf			 * This preserves the previous behavior when the
415216060Smdf			 * sysctl lock was held across a handler invocation,
416216060Smdf			 * and is necessary for module unload correctness.
417216060Smdf			 */
418216060Smdf			while (oidp->oid_running > 0) {
419216060Smdf				oidp->oid_kind |= CTLFLAG_DYING;
420216060Smdf				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
421216060Smdf			}
422141433Sphk			if (oidp->oid_descr)
423141433Sphk				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
42463978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
42563978Speter			     M_SYSCTLOID);
42663212Sabial			free(oidp, M_SYSCTLOID);
42763212Sabial		}
42863212Sabial	}
42963212Sabial	return (0);
43063212Sabial}
43163212Sabial
43263212Sabial/*
43363212Sabial * Create new sysctls at run time.
43463212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
43563212Sabial */
43663212Sabialstruct sysctl_oid *
43763212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
43870679Sjhb	int number, const char *name, int kind, void *arg1, int arg2,
43970679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
44063212Sabial{
44163212Sabial	struct sysctl_oid *oidp;
44263212Sabial	ssize_t len;
44363978Speter	char *newname;
44463212Sabial
44563212Sabial	/* You have to hook up somewhere.. */
44663212Sabial	if (parent == NULL)
44763212Sabial		return(NULL);
44863212Sabial	/* Check if the node already exists, otherwise create it */
449188232Sjhb	SYSCTL_XLOCK();
45063212Sabial	oidp = sysctl_find_oidname(name, parent);
45163212Sabial	if (oidp != NULL) {
45263212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
45363212Sabial			oidp->oid_refcnt++;
45463212Sabial			/* Update the context */
45563212Sabial			if (clist != NULL)
45663212Sabial				sysctl_ctx_entry_add(clist, oidp);
457188232Sjhb			SYSCTL_XUNLOCK();
45863212Sabial			return (oidp);
45963212Sabial		} else {
460188232Sjhb			SYSCTL_XUNLOCK();
46163212Sabial			printf("can't re-use a leaf (%s)!\n", name);
46263212Sabial			return (NULL);
46363212Sabial		}
46463212Sabial	}
465111119Simp	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
46663212Sabial	oidp->oid_parent = parent;
46763212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
46863212Sabial	oidp->oid_number = number;
46963212Sabial	oidp->oid_refcnt = 1;
47063212Sabial	len = strlen(name);
471111119Simp	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
47263978Speter	bcopy(name, newname, len + 1);
47363978Speter	newname[len] = '\0';
47463978Speter	oidp->oid_name = newname;
47563212Sabial	oidp->oid_handler = handler;
47663212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
47763212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
47863212Sabial		/* Allocate space for children */
479132776Skan		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
480132776Skan		    M_SYSCTLOID, M_WAITOK));
48163212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
48263212Sabial	} else {
48363212Sabial		oidp->oid_arg1 = arg1;
48463212Sabial		oidp->oid_arg2 = arg2;
48563212Sabial	}
48663212Sabial	oidp->oid_fmt = fmt;
48788006Sluigi	if (descr) {
48888006Sluigi		int len = strlen(descr) + 1;
489141433Sphk		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
490141433Sphk		if (oidp->oid_descr)
491141433Sphk			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
49288006Sluigi	}
49363212Sabial	/* Update the context, if used */
49463212Sabial	if (clist != NULL)
49563212Sabial		sysctl_ctx_entry_add(clist, oidp);
49663212Sabial	/* Register this oid */
49763212Sabial	sysctl_register_oid(oidp);
498188232Sjhb	SYSCTL_XUNLOCK();
49963212Sabial	return (oidp);
50063212Sabial}
50163212Sabial
50263212Sabial/*
503174113Speter * Rename an existing oid.
504174113Speter */
505174113Spetervoid
506174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
507174113Speter{
508174113Speter	ssize_t len;
509174113Speter	char *newname;
510174113Speter	void *oldname;
511174113Speter
512174113Speter	len = strlen(name);
513174113Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
514174113Speter	bcopy(name, newname, len + 1);
515174113Speter	newname[len] = '\0';
516188232Sjhb	SYSCTL_XLOCK();
517188232Sjhb	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
518174113Speter	oidp->oid_name = newname;
519188232Sjhb	SYSCTL_XUNLOCK();
520174113Speter	free(oldname, M_SYSCTLOID);
521174113Speter}
522174113Speter
523174113Speter/*
524126319Sdes * Reparent an existing oid.
525126319Sdes */
526126319Sdesint
527126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
528126319Sdes{
529126319Sdes	struct sysctl_oid *oidp;
530126319Sdes
531188232Sjhb	SYSCTL_XLOCK();
532188232Sjhb	if (oid->oid_parent == parent) {
533188232Sjhb		SYSCTL_XUNLOCK();
534126319Sdes		return (0);
535188232Sjhb	}
536126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
537188232Sjhb	if (oidp != NULL) {
538188232Sjhb		SYSCTL_XUNLOCK();
539126319Sdes		return (EEXIST);
540188232Sjhb	}
541126319Sdes	sysctl_unregister_oid(oid);
542126319Sdes	oid->oid_parent = parent;
543126319Sdes	oid->oid_number = OID_AUTO;
544126319Sdes	sysctl_register_oid(oid);
545188232Sjhb	SYSCTL_XUNLOCK();
546126319Sdes	return (0);
547126319Sdes}
548126319Sdes
549126319Sdes/*
55044078Sdfr * Register the kernel's oids on startup.
55144078Sdfr */
55278161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
55312152Sphk
55480338Sroamstatic void
55580338Sroamsysctl_register_all(void *arg)
55638869Sbde{
55778161Speter	struct sysctl_oid **oidp;
55878161Speter
559192125Sjhb	sx_init(&sysctlmemlock, "sysctl mem");
56093625Srwatson	SYSCTL_INIT();
561188232Sjhb	SYSCTL_XLOCK();
56278161Speter	SET_FOREACH(oidp, sysctl_set)
56378161Speter		sysctl_register_oid(*oidp);
564188232Sjhb	SYSCTL_XUNLOCK();
56538869Sbde}
56644078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
56744078Sdfr
56812623Sphk/*
56912623Sphk * "Staff-functions"
57012623Sphk *
57112650Sphk * These functions implement a presently undocumented interface
57212650Sphk * used by the sysctl program to walk the tree, and get the type
57312650Sphk * so it can print the value.
57412650Sphk * This interface is under work and consideration, and should probably
57512650Sphk * be killed with a big axe by the first person who can find the time.
57612650Sphk * (be aware though, that the proper interface isn't as obvious as it
57712650Sphk * may seem, there are various conflicting requirements.
57812650Sphk *
57912623Sphk * {0,0}	printf the entire MIB-tree.
58012623Sphk * {0,1,...}	return the name of the "..." OID.
58142467Sphk * {0,2,...}	return the next OID.
58212623Sphk * {0,3}	return the OID of the name in "new"
58312650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
58488006Sluigi * {0,5,...}	return the description the "..." OID.
58512623Sphk */
58612623Sphk
587136999Srwatson#ifdef SYSCTL_DEBUG
58812152Sphkstatic void
58944078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
59012152Sphk{
59144078Sdfr	int k;
59244078Sdfr	struct sysctl_oid *oidp;
59312152Sphk
594216060Smdf	SYSCTL_ASSERT_XLOCKED();
59544078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
59612152Sphk
59712152Sphk		for (k=0; k<i; k++)
59812152Sphk			printf(" ");
59912152Sphk
60044078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
60112152Sphk
60212152Sphk		printf("%c%c",
60344078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
60444078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
60512152Sphk
60644078Sdfr		if (oidp->oid_handler)
60715241Sphk			printf(" *Handler");
60815241Sphk
60944078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
61012243Sphk			case CTLTYPE_NODE:
61115241Sphk				printf(" Node\n");
61244078Sdfr				if (!oidp->oid_handler) {
61312152Sphk					sysctl_sysctl_debug_dump_node(
61444078Sdfr						oidp->oid_arg1, i+2);
61512152Sphk				}
61612152Sphk				break;
61712152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
61812152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
61912152Sphk			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
62012152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
62112152Sphk			default:	     printf("\n");
62212152Sphk		}
62312152Sphk
62412152Sphk	}
62512152Sphk}
62612152Sphk
62712152Sphkstatic int
62862573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
62912152Sphk{
63087024Speter	int error;
63187024Speter
632164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
63387024Speter	if (error)
634139483Spjd		return (error);
635216060Smdf	SYSCTL_XLOCK();
63644078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
637216060Smdf	SYSCTL_XUNLOCK();
638139483Spjd	return (ENOENT);
63912152Sphk}
64012152Sphk
64112152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
64212623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
643136999Srwatson#endif
64412152Sphk
64512623Sphkstatic int
64662573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
64712623Sphk{
64812623Sphk	int *name = (int *) arg1;
64912623Sphk	u_int namelen = arg2;
65044078Sdfr	int error = 0;
65144078Sdfr	struct sysctl_oid *oid;
65244972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
65312623Sphk	char buf[10];
65412131Sphk
655216060Smdf	SYSCTL_XLOCK();
65612623Sphk	while (namelen) {
65712623Sphk		if (!lsp) {
65841514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
65912623Sphk			if (req->oldidx)
66012623Sphk				error = SYSCTL_OUT(req, ".", 1);
66112623Sphk			if (!error)
66212623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
66312623Sphk			if (error)
664216060Smdf				goto out;
66512623Sphk			namelen--;
66612623Sphk			name++;
66712623Sphk			continue;
66812623Sphk		}
66944972Sphk		lsp2 = 0;
67044078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
67144078Sdfr			if (oid->oid_number != *name)
67212623Sphk				continue;
67312131Sphk
67412623Sphk			if (req->oldidx)
67512623Sphk				error = SYSCTL_OUT(req, ".", 1);
67612623Sphk			if (!error)
67744078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
67844078Sdfr					strlen(oid->oid_name));
67912623Sphk			if (error)
680216060Smdf				goto out;
68112623Sphk
68212623Sphk			namelen--;
68312623Sphk			name++;
68412623Sphk
68544972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
68612623Sphk				break;
68712623Sphk
68844078Sdfr			if (oid->oid_handler)
68912623Sphk				break;
69012623Sphk
691216058Smdf			lsp2 = SYSCTL_CHILDREN(oid);
69212623Sphk			break;
69312623Sphk		}
69444972Sphk		lsp = lsp2;
69512623Sphk	}
696216060Smdf	error = SYSCTL_OUT(req, "", 1);
697216060Smdf out:
698216060Smdf	SYSCTL_XUNLOCK();
699216060Smdf	return (error);
70012623Sphk}
70112623Sphk
702141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
70312623Sphk
70412623Sphkstatic int
70563978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
70644078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
70712623Sphk{
70844078Sdfr	struct sysctl_oid *oidp;
70912623Sphk
710216060Smdf	SYSCTL_ASSERT_XLOCKED();
71112623Sphk	*len = level;
71244078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
71344078Sdfr		*next = oidp->oid_number;
71444078Sdfr		*oidpp = oidp;
71512623Sphk
716101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
717101650Smux			continue;
718101650Smux
71912623Sphk		if (!namelen) {
72044078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
721139483Spjd				return (0);
72244078Sdfr			if (oidp->oid_handler)
72312623Sphk				/* We really should call the handler here...*/
724139483Spjd				return (0);
725216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
72663978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
72744078Sdfr				len, level+1, oidpp))
728139483Spjd				return (0);
729111260Srwatson			goto emptynode;
73012623Sphk		}
73112623Sphk
73244078Sdfr		if (oidp->oid_number < *name)
73312623Sphk			continue;
73412623Sphk
73544078Sdfr		if (oidp->oid_number > *name) {
73644078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
737139483Spjd				return (0);
73844078Sdfr			if (oidp->oid_handler)
739139483Spjd				return (0);
740216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
74163978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
74244078Sdfr				next+1, len, level+1, oidpp))
74312623Sphk				return (0);
74415241Sphk			goto next;
74512623Sphk		}
74644078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
74712623Sphk			continue;
74812623Sphk
74944078Sdfr		if (oidp->oid_handler)
75012623Sphk			continue;
75112623Sphk
752216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
75363978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
75444078Sdfr			len, level+1, oidpp))
75512623Sphk			return (0);
75615241Sphk	next:
75712623Sphk		namelen = 1;
758111260Srwatson	emptynode:
75912623Sphk		*len = level;
76012623Sphk	}
761139483Spjd	return (1);
76212623Sphk}
76312623Sphk
76412623Sphkstatic int
76562573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
76612623Sphk{
76712623Sphk	int *name = (int *) arg1;
76812623Sphk	u_int namelen = arg2;
76912623Sphk	int i, j, error;
77012623Sphk	struct sysctl_oid *oid;
77144078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
77212623Sphk	int newoid[CTL_MAXNAME];
77312623Sphk
774216060Smdf	SYSCTL_XLOCK();
77563978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
776216060Smdf	SYSCTL_XUNLOCK();
77712623Sphk	if (i)
778139483Spjd		return (ENOENT);
77912650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
78012623Sphk	return (error);
78112623Sphk}
78212623Sphk
783141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
78412623Sphk
78512623Sphkstatic int
786189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
78712623Sphk{
78844078Sdfr	int i;
78944078Sdfr	struct sysctl_oid *oidp;
79044078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
79112623Sphk	char *p;
79212623Sphk
793216060Smdf	SYSCTL_ASSERT_XLOCKED();
794186564Sed
79512623Sphk	if (!*name)
796139483Spjd		return (ENOENT);
79712623Sphk
79812623Sphk	p = name + strlen(name) - 1 ;
79912623Sphk	if (*p == '.')
80012623Sphk		*p = '\0';
80112623Sphk
80212623Sphk	*len = 0;
80312623Sphk
80412623Sphk	for (p = name; *p && *p != '.'; p++)
80512623Sphk		;
80612623Sphk	i = *p;
80712623Sphk	if (i == '.')
80812623Sphk		*p = '\0';
80912623Sphk
81044078Sdfr	oidp = SLIST_FIRST(lsp);
81112623Sphk
81244078Sdfr	while (oidp && *len < CTL_MAXNAME) {
81344078Sdfr		if (strcmp(name, oidp->oid_name)) {
81444078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
81512623Sphk			continue;
81612623Sphk		}
81744078Sdfr		*oid++ = oidp->oid_number;
81812623Sphk		(*len)++;
81912623Sphk
82012623Sphk		if (!i) {
82144078Sdfr			if (oidpp)
82244078Sdfr				*oidpp = oidp;
82312623Sphk			return (0);
82412623Sphk		}
82512623Sphk
82644078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
82712623Sphk			break;
82812623Sphk
82944078Sdfr		if (oidp->oid_handler)
83012623Sphk			break;
83112623Sphk
832216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
83344078Sdfr		oidp = SLIST_FIRST(lsp);
83412623Sphk		name = p+1;
83512623Sphk		for (p = name; *p && *p != '.'; p++)
83612623Sphk				;
83712623Sphk		i = *p;
83812623Sphk		if (i == '.')
83912623Sphk			*p = '\0';
84012623Sphk	}
841139483Spjd	return (ENOENT);
84212623Sphk}
84312623Sphk
84412623Sphkstatic int
84562573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
84612623Sphk{
84712623Sphk	char *p;
84812623Sphk	int error, oid[CTL_MAXNAME], len;
84912623Sphk	struct sysctl_oid *op = 0;
85012623Sphk
85112623Sphk	if (!req->newlen)
852139483Spjd		return (ENOENT);
85345140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
85445140Sphk		return (ENAMETOOLONG);
85512623Sphk
856111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
85712623Sphk
85812623Sphk	error = SYSCTL_IN(req, p, req->newlen);
85912623Sphk	if (error) {
86012623Sphk		free(p, M_SYSCTL);
86112623Sphk		return (error);
86212623Sphk	}
86312623Sphk
86412623Sphk	p [req->newlen] = '\0';
86512623Sphk
866216060Smdf	SYSCTL_XLOCK();
86712623Sphk	error = name2oid(p, oid, &len, &op);
868216060Smdf	SYSCTL_XUNLOCK();
86912623Sphk
87012623Sphk	free(p, M_SYSCTL);
87112623Sphk
87212623Sphk	if (error)
87312623Sphk		return (error);
87412623Sphk
87512650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
87612623Sphk	return (error);
87712623Sphk}
87812623Sphk
879187864SedSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
880187864Sed    0, 0, sysctl_sysctl_name2oid, "I", "");
88112623Sphk
88212623Sphkstatic int
88362573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
88412623Sphk{
88544078Sdfr	struct sysctl_oid *oid;
88653977Sgreen	int error;
88712623Sphk
888216060Smdf	SYSCTL_XLOCK();
88953977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
89053977Sgreen	if (error)
891216060Smdf		goto out;
89212623Sphk
893216060Smdf	if (oid->oid_fmt == NULL) {
894216060Smdf		error = ENOENT;
895216060Smdf		goto out;
896216060Smdf	}
89753977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
89853977Sgreen	if (error)
899216060Smdf		goto out;
90053977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
901216060Smdf out:
902216060Smdf	SYSCTL_XUNLOCK();
90312650Sphk	return (error);
90412623Sphk}
90512623Sphk
90642467Sphk
907187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
908187864Sed    sysctl_sysctl_oidfmt, "");
90912623Sphk
91088006Sluigistatic int
91188006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
91288006Sluigi{
91388006Sluigi	struct sysctl_oid *oid;
91488006Sluigi	int error;
91588006Sluigi
916216060Smdf	SYSCTL_XLOCK();
91788006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
91888006Sluigi	if (error)
919216060Smdf		goto out;
92088006Sluigi
921216060Smdf	if (oid->oid_descr == NULL) {
922216060Smdf		error = ENOENT;
923216060Smdf		goto out;
924216060Smdf	}
925141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
926216060Smdf out:
927216060Smdf	SYSCTL_XUNLOCK();
92888006Sluigi	return (error);
92988006Sluigi}
93088006Sluigi
931141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
93288006Sluigi
93312243Sphk/*
93412623Sphk * Default "handler" functions.
93512623Sphk */
93612623Sphk
93712623Sphk/*
93842095Sdfr * Handle an int, signed or unsigned.
93912243Sphk * Two cases:
94012243Sphk *     a variable:  point arg1 at it.
94112243Sphk *     a constant:  pass it in arg2.
94212243Sphk */
94312243Sphk
94411865Sphkint
94562573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
94611863Sphk{
947100833Struckman	int tmpout, error = 0;
94811863Sphk
949100833Struckman	/*
950100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
951100833Struckman	 */
95212243Sphk	if (arg1)
953100833Struckman		tmpout = *(int *)arg1;
95420506Sbde	else
955100833Struckman		tmpout = arg2;
956100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
95711863Sphk
95812243Sphk	if (error || !req->newptr)
95912243Sphk		return (error);
96011863Sphk
96112243Sphk	if (!arg1)
96212243Sphk		error = EPERM;
96312243Sphk	else
96412243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
96512243Sphk	return (error);
96611863Sphk}
96711863Sphk
96812243Sphk/*
969155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
970195699Srwatson * Note: this is used by TCP.
971155758Sandre */
972155758Sandre
973155758Sandreint
974155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
975155758Sandre{
976155758Sandre	int error, s, tt;
977155758Sandre
978191688Szec	tt = *(int *)arg1;
979155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
980155758Sandre
981155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
982155758Sandre	if (error || !req->newptr)
983155758Sandre		return (error);
984155758Sandre
985155758Sandre	tt = (int)((int64_t)s * hz / 1000);
986155758Sandre	if (tt < 1)
987155758Sandre		return (EINVAL);
988155758Sandre
989191688Szec	*(int *)arg1 = tt;
990155758Sandre	return (0);
991155758Sandre}
992155758Sandre
993155758Sandre
994155758Sandre/*
99545140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
99638517Sdfr */
99738517Sdfr
99838517Sdfrint
99962573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
100038517Sdfr{
100138517Sdfr	int error = 0;
1002136404Speter	long tmplong;
1003136404Speter#ifdef SCTL_MASK32
1004136404Speter	int tmpint;
1005136404Speter#endif
100638517Sdfr
1007100833Struckman	/*
1008100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
1009100833Struckman	 */
101045140Sphk	if (!arg1)
101145140Sphk		return (EINVAL);
1012136404Speter	tmplong = *(long *)arg1;
1013136404Speter#ifdef SCTL_MASK32
1014136404Speter	if (req->flags & SCTL_MASK32) {
1015136404Speter		tmpint = tmplong;
1016136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1017136404Speter	} else
1018136404Speter#endif
1019136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
102038517Sdfr
102138517Sdfr	if (error || !req->newptr)
102238517Sdfr		return (error);
102338517Sdfr
1024136404Speter#ifdef SCTL_MASK32
1025136404Speter	if (req->flags & SCTL_MASK32) {
1026136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1027136404Speter		*(long *)arg1 = (long)tmpint;
1028136404Speter	} else
1029136404Speter#endif
1030136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
103138517Sdfr	return (error);
103238517Sdfr}
103338517Sdfr
103438517Sdfr/*
1035170288Sdwmalone * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1036170288Sdwmalone */
1037170288Sdwmalone
1038170288Sdwmaloneint
1039170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1040170288Sdwmalone{
1041170288Sdwmalone	int error = 0;
1042170288Sdwmalone	uint64_t tmpout;
1043170288Sdwmalone
1044170288Sdwmalone	/*
1045170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
1046170288Sdwmalone	 */
1047170288Sdwmalone	if (!arg1)
1048170288Sdwmalone		return (EINVAL);
1049170288Sdwmalone	tmpout = *(uint64_t *)arg1;
1050170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1051170288Sdwmalone
1052170288Sdwmalone	if (error || !req->newptr)
1053170288Sdwmalone		return (error);
1054170288Sdwmalone
1055170288Sdwmalone	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1056170288Sdwmalone	return (error);
1057170288Sdwmalone}
1058170288Sdwmalone
1059170288Sdwmalone/*
106012243Sphk * Handle our generic '\0' terminated 'C' string.
106112243Sphk * Two cases:
106212243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
106312243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
106412243Sphk */
106512243Sphk
106611865Sphkint
106762573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
106811863Sphk{
106912243Sphk	int error=0;
1070100833Struckman	char *tmparg;
1071100833Struckman	size_t outlen;
107211863Sphk
1073100833Struckman	/*
1074100833Struckman	 * Attempt to get a coherent snapshot by copying to a
1075100833Struckman	 * temporary kernel buffer.
1076100833Struckman	 */
1077100833Struckmanretry:
1078100833Struckman	outlen = strlen((char *)arg1)+1;
1079111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1080105354Srobert
1081105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1082100833Struckman		free(tmparg, M_SYSCTLTMP);
1083100833Struckman		goto retry;
1084100833Struckman	}
1085105354Srobert
1086100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
1087100833Struckman	free(tmparg, M_SYSCTLTMP);
108811863Sphk
108945140Sphk	if (error || !req->newptr)
109012243Sphk		return (error);
109111863Sphk
109245140Sphk	if ((req->newlen - req->newidx) >= arg2) {
109345140Sphk		error = EINVAL;
109412243Sphk	} else {
109512243Sphk		arg2 = (req->newlen - req->newidx);
109612243Sphk		error = SYSCTL_IN(req, arg1, arg2);
109712243Sphk		((char *)arg1)[arg2] = '\0';
109811863Sphk	}
109912131Sphk
110012131Sphk	return (error);
110111863Sphk}
110211863Sphk
110312243Sphk/*
110412243Sphk * Handle any kind of opaque data.
110512243Sphk * arg1 points to it, arg2 is the size.
110612243Sphk */
110712243Sphk
110811865Sphkint
110962573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
111011863Sphk{
1111120803Sbms	int error, tries;
1112120803Sbms	u_int generation;
1113120813Sbms	struct sysctl_req req2;
111412243Sphk
1115100833Struckman	/*
1116120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
1117120803Sbms	 * pre-emption counter updated from within mi_switch() to
1118120803Sbms	 * determine if we were pre-empted during a bcopy() or
1119120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
1120120803Sbms	 * If we encounter an error, stop immediately.
1121100833Struckman	 */
1122120803Sbms	tries = 0;
1123120813Sbms	req2 = *req;
1124120813Sbmsretry:
1125120813Sbms	generation = curthread->td_generation;
1126120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
1127120813Sbms	if (error)
1128120813Sbms		return (error);
1129120813Sbms	tries++;
1130120813Sbms	if (generation != curthread->td_generation && tries < 3) {
1131120813Sbms		*req = req2;
1132120813Sbms		goto retry;
1133120813Sbms	}
113412243Sphk
113512243Sphk	error = SYSCTL_IN(req, arg1, arg2);
113612243Sphk
113712243Sphk	return (error);
113812243Sphk}
113912243Sphk
114012260Sphk/*
114112260Sphk * Transfer functions to/from kernel space.
114212260Sphk * XXX: rather untested at this point
114312260Sphk */
114412260Sphkstatic int
114538517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
114612243Sphk{
114738517Sdfr	size_t i = 0;
114812260Sphk
114912260Sphk	if (req->oldptr) {
115038517Sdfr		i = l;
115173971Stmm		if (req->oldlen <= req->oldidx)
115273971Stmm			i = 0;
115373971Stmm		else
115473971Stmm			if (i > req->oldlen - req->oldidx)
115573971Stmm				i = req->oldlen - req->oldidx;
115612260Sphk		if (i > 0)
115717971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
115812243Sphk	}
1159192144Skib	req->oldidx += l;
116016282Snate	if (req->oldptr && i != l)
116111863Sphk		return (ENOMEM);
116212260Sphk	return (0);
116312243Sphk}
116412243Sphk
116512260Sphkstatic int
116638517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
116712243Sphk{
116812260Sphk	if (!req->newptr)
1169139483Spjd		return (0);
117012260Sphk	if (req->newlen - req->newidx < l)
117111863Sphk		return (EINVAL);
117217971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
117312243Sphk	req->newidx += l;
117412131Sphk	return (0);
117511863Sphk}
117611863Sphk
117716282Snateint
117883366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1179136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
118016282Snate{
118116282Snate	int error = 0;
118216282Snate	struct sysctl_req req;
118316282Snate
118416282Snate	bzero(&req, sizeof req);
118516282Snate
118686183Srwatson	req.td = td;
1187136404Speter	req.flags = flags;
118816282Snate
118916282Snate	if (oldlenp) {
119016282Snate		req.oldlen = *oldlenp;
119116282Snate	}
1192127052Struckman	req.validlen = req.oldlen;
119316282Snate
119416282Snate	if (old) {
119516282Snate		req.oldptr= old;
119616282Snate	}
119716282Snate
119877646Sdd	if (new != NULL) {
119916282Snate		req.newlen = newlen;
120016282Snate		req.newptr = new;
120116282Snate	}
120216282Snate
120316282Snate	req.oldfunc = sysctl_old_kernel;
120416282Snate	req.newfunc = sysctl_new_kernel;
1205120781Sbms	req.lock = REQ_LOCKED;
120616282Snate
1207216060Smdf	SYSCTL_XLOCK();
120816282Snate	error = sysctl_root(0, name, namelen, &req);
1209216060Smdf	SYSCTL_XUNLOCK();
1210120813Sbms
1211127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1212127052Struckman		vsunlock(req.oldptr, req.validlen);
121316282Snate
121416282Snate	if (error && error != ENOMEM)
121516282Snate		return (error);
121616282Snate
121716282Snate	if (retval) {
1218127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1219127052Struckman			*retval = req.validlen;
122016282Snate		else
122116282Snate			*retval = req.oldidx;
122216282Snate	}
122316282Snate	return (error);
122416282Snate}
122516282Snate
122676834Sjlemonint
122783366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1228136404Speter    void *new, size_t newlen, size_t *retval, int flags)
122976834Sjlemon{
123076834Sjlemon        int oid[CTL_MAXNAME];
123178620Smjacob        size_t oidlen, plen;
123278620Smjacob	int error;
123376834Sjlemon
123476834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
123576834Sjlemon	oid[1] = 3;		/* name2oid */
123676834Sjlemon	oidlen = sizeof(oid);
123776834Sjlemon
123883366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1239136404Speter	    (void *)name, strlen(name), &plen, flags);
124076834Sjlemon	if (error)
124176834Sjlemon		return (error);
124276834Sjlemon
124383366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1244136404Speter	    new, newlen, retval, flags);
124576834Sjlemon	return (error);
124676834Sjlemon}
124776834Sjlemon
124812260Sphk/*
124912260Sphk * Transfer function to/from user space.
125012260Sphk */
125112260Sphkstatic int
125238517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
125312243Sphk{
125438517Sdfr	int error = 0;
1255126253Struckman	size_t i, len, origidx;
125612243Sphk
1257126253Struckman	origidx = req->oldidx;
1258192144Skib	req->oldidx += l;
1259192144Skib	if (req->oldptr == NULL)
1260126253Struckman		return (0);
1261148864Scsjp	/*
1262148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1263148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1264148864Scsjp	 * write operations to the user page can sleep.
1265148864Scsjp	 */
1266148864Scsjp	if (req->lock != REQ_WIRED)
1267111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1268111883Sjhb		    "sysctl_old_user()");
1269126253Struckman	i = l;
1270127052Struckman	len = req->validlen;
1271126253Struckman	if (len <= origidx)
1272126253Struckman		i = 0;
1273126253Struckman	else {
1274126253Struckman		if (i > len - origidx)
1275126253Struckman			i = len - origidx;
1276126253Struckman		error = copyout(p, (char *)req->oldptr + origidx, i);
127712260Sphk	}
127812243Sphk	if (error)
127912243Sphk		return (error);
1280126253Struckman	if (i < l)
128112243Sphk		return (ENOMEM);
128212260Sphk	return (0);
128312243Sphk}
128412243Sphk
128512260Sphkstatic int
128638517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
128712243Sphk{
128812285Sphk	int error;
128912260Sphk
129012260Sphk	if (!req->newptr)
1291139483Spjd		return (0);
129212260Sphk	if (req->newlen - req->newidx < l)
129312243Sphk		return (EINVAL);
1294148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1295148873Scsjp	    "sysctl_new_user()");
129617971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
129712243Sphk	req->newidx += l;
129812243Sphk	return (error);
129912243Sphk}
130012243Sphk
1301100487Struckman/*
1302100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1303100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1304100487Struckman */
1305126253Struckmanint
1306100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1307100487Struckman{
1308126253Struckman	int ret;
1309192160Sdes	size_t wiredlen;
1310126253Struckman
1311126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1312126253Struckman	ret = 0;
1313120781Sbms	if (req->lock == REQ_LOCKED && req->oldptr &&
1314120781Sbms	    req->oldfunc == sysctl_old_user) {
1315127050Struckman		if (wiredlen != 0) {
1316127050Struckman			ret = vslock(req->oldptr, wiredlen);
1317130327Sgreen			if (ret != 0) {
1318130327Sgreen				if (ret != ENOMEM)
1319130327Sgreen					return (ret);
1320130327Sgreen				wiredlen = 0;
1321130327Sgreen			}
1322126253Struckman		}
1323127050Struckman		req->lock = REQ_WIRED;
1324127052Struckman		req->validlen = wiredlen;
1325100487Struckman	}
1326127050Struckman	return (0);
1327100487Struckman}
1328100487Struckman
13291541Srgrimesint
133053977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
133153977Sgreen    int *nindx, struct sysctl_req *req)
133212131Sphk{
1333216059Smdf	struct sysctl_oid_list *lsp;
133444078Sdfr	struct sysctl_oid *oid;
133553977Sgreen	int indx;
133612131Sphk
1337216060Smdf	SYSCTL_ASSERT_XLOCKED();
1338216059Smdf	lsp = &sysctl__children;
133912131Sphk	indx = 0;
1340216059Smdf	while (indx < CTL_MAXNAME) {
1341216059Smdf		SLIST_FOREACH(oid, lsp, oid_link) {
1342216059Smdf			if (oid->oid_number == name[indx])
1343216059Smdf				break;
1344216059Smdf		}
1345216059Smdf		if (oid == NULL)
1346216059Smdf			return (ENOENT);
1347216059Smdf
1348216059Smdf		indx++;
1349216059Smdf		if (oid->oid_kind & CTLFLAG_NOLOCK)
1350216059Smdf			req->lock = REQ_UNLOCKED;
1351216059Smdf		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1352216059Smdf			if (oid->oid_handler != NULL || indx == namelen) {
135353977Sgreen				*noid = oid;
135453977Sgreen				if (nindx != NULL)
135553977Sgreen					*nindx = indx;
1356216060Smdf				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1357216060Smdf				    ("%s found DYING node %p", __func__, oid));
135853977Sgreen				return (0);
135912131Sphk			}
1360216059Smdf			lsp = SYSCTL_CHILDREN(oid);
1361216059Smdf		} else if (indx == namelen) {
1362216059Smdf			*noid = oid;
1363216059Smdf			if (nindx != NULL)
1364216059Smdf				*nindx = indx;
1365216060Smdf			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1366216060Smdf			    ("%s found DYING node %p", __func__, oid));
1367216059Smdf			return (0);
136812131Sphk		} else {
1369216059Smdf			return (ENOTDIR);
137012131Sphk		}
137112131Sphk	}
137253977Sgreen	return (ENOENT);
137353977Sgreen}
137453977Sgreen
137553977Sgreen/*
137653977Sgreen * Traverse our tree, and find the right node, execute whatever it points
137753977Sgreen * to, and return the resulting error code.
137853977Sgreen */
137953977Sgreen
1380104094Sphkstatic int
138162573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
138253977Sgreen{
138353977Sgreen	struct sysctl_oid *oid;
1384109246Sdillon	int error, indx, lvl;
138553977Sgreen
1386216060Smdf	SYSCTL_ASSERT_XLOCKED();
1387186564Sed
138853977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
138953977Sgreen	if (error)
139053977Sgreen		return (error);
139153977Sgreen
139253977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
139353977Sgreen		/*
139453977Sgreen		 * You can't call a sysctl when it's a node, but has
139553977Sgreen		 * no handler.  Inform the user that it's a node.
139653977Sgreen		 * The indx may or may not be the same as namelen.
139753977Sgreen		 */
139853977Sgreen		if (oid->oid_handler == NULL)
139953977Sgreen			return (EISDIR);
140053977Sgreen	}
140153977Sgreen
140283968Srwatson	/* Is this sysctl writable? */
140383968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
140412131Sphk		return (EPERM);
140512131Sphk
140692953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
140792953Srwatson
140883968Srwatson	/* Is this sysctl sensitive to securelevels? */
140983968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1410109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1411109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
141292953Srwatson		if (error)
141392953Srwatson			return (error);
141483968Srwatson	}
141512910Sphk
141683968Srwatson	/* Is this sysctl writable by only privileged users? */
141783968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1418196176Sbz		int priv;
1419196176Sbz
142092953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1421196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1422196176Sbz#ifdef VIMAGE
1423196176Sbz		else if ((oid->oid_kind & CTLFLAG_VNET) &&
1424196176Sbz		     prison_owns_vnet(req->td->td_ucred))
1425196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1426196176Sbz#endif
142792953Srwatson		else
1428196176Sbz			priv = PRIV_SYSCTL_WRITE;
1429196176Sbz		error = priv_check(req->td, priv);
143092953Srwatson		if (error)
143192953Srwatson			return (error);
143283968Srwatson	}
143383968Srwatson
143444078Sdfr	if (!oid->oid_handler)
1435139483Spjd		return (EINVAL);
143612131Sphk
1437126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1438132776Skan		arg1 = (int *)arg1 + indx;
1439126121Spjd		arg2 -= indx;
1440126121Spjd	} else {
1441126121Spjd		arg1 = oid->oid_arg1;
1442126121Spjd		arg2 = oid->oid_arg2;
1443126121Spjd	}
1444126121Spjd#ifdef MAC
1445172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1446126121Spjd	    req);
1447126121Spjd	if (error != 0)
1448126121Spjd		return (error);
1449126121Spjd#endif
1450216060Smdf	oid->oid_running++;
1451216060Smdf	SYSCTL_XUNLOCK();
1452216060Smdf
1453187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1454187656Sjhb		mtx_lock(&Giant);
1455126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1456187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1457187656Sjhb		mtx_unlock(&Giant);
1458126121Spjd
1459216060Smdf	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1460216060Smdf
1461216060Smdf	SYSCTL_XLOCK();
1462216060Smdf	oid->oid_running--;
1463216060Smdf	if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1464216060Smdf		wakeup(&oid->oid_running);
146553977Sgreen	return (error);
146612131Sphk}
146712131Sphk
146812221Sbde#ifndef _SYS_SYSPROTO_H_
146912171Sphkstruct sysctl_args {
147012171Sphk	int	*name;
147112171Sphk	u_int	namelen;
147212171Sphk	void	*old;
147312171Sphk	size_t	*oldlenp;
147412171Sphk	void	*new;
147512171Sphk	size_t	newlen;
147612171Sphk};
147712221Sbde#endif
147812131Sphkint
147983366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap)
14801541Srgrimes{
1481188232Sjhb	int error, i, name[CTL_MAXNAME];
148238517Sdfr	size_t j;
14831541Srgrimes
14841541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
14851541Srgrimes		return (EINVAL);
148611863Sphk
14873308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
14883308Sphk 	if (error)
14891541Srgrimes		return (error);
14901541Srgrimes
149183366Sjulian	error = userland_sysctl(td, name, uap->namelen,
149212171Sphk		uap->old, uap->oldlenp, 0,
1493136404Speter		uap->new, uap->newlen, &j, 0);
149412260Sphk	if (error && error != ENOMEM)
1495186564Sed		return (error);
1496186664Sed	if (uap->oldlenp) {
1497188232Sjhb		i = copyout(&j, uap->oldlenp, sizeof(j));
1498186664Sed		if (i)
1499186664Sed			return (i);
1500186664Sed	}
150112260Sphk	return (error);
150212171Sphk}
150312171Sphk
150412171Sphk/*
150512171Sphk * This is used from various compatibility syscalls too.  That's why name
150612171Sphk * must be in kernel space.
150712171Sphk */
150812171Sphkint
150983366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1510136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1511136404Speter    int flags)
151212171Sphk{
1513192125Sjhb	int error = 0, memlocked;
1514127052Struckman	struct sysctl_req req;
151512171Sphk
151612243Sphk	bzero(&req, sizeof req);
151712243Sphk
151886183Srwatson	req.td = td;
1519136404Speter	req.flags = flags;
152012285Sphk
152112171Sphk	if (oldlenp) {
152212171Sphk		if (inkernel) {
152312243Sphk			req.oldlen = *oldlenp;
152412171Sphk		} else {
152512260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
152612171Sphk			if (error)
152712171Sphk				return (error);
152812171Sphk		}
152912171Sphk	}
1530127052Struckman	req.validlen = req.oldlen;
153112171Sphk
153212243Sphk	if (old) {
153352644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
153412243Sphk			return (EFAULT);
153512243Sphk		req.oldptr= old;
153612243Sphk	}
153712131Sphk
153877646Sdd	if (new != NULL) {
1539172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
154012243Sphk			return (EFAULT);
154112243Sphk		req.newlen = newlen;
154212243Sphk		req.newptr = new;
154311863Sphk	}
154412131Sphk
154512243Sphk	req.oldfunc = sysctl_old_user;
154612243Sphk	req.newfunc = sysctl_new_user;
1547120781Sbms	req.lock = REQ_LOCKED;
154811863Sphk
1549189707Sjhb#ifdef KTRACE
1550189707Sjhb	if (KTRPOINT(curthread, KTR_SYSCTL))
1551189707Sjhb		ktrsysctl(name, namelen);
1552189707Sjhb#endif
1553192125Sjhb
1554192125Sjhb	if (req.oldlen > PAGE_SIZE) {
1555192125Sjhb		memlocked = 1;
1556192125Sjhb		sx_xlock(&sysctlmemlock);
1557192125Sjhb	} else
1558192125Sjhb		memlocked = 0;
1559194252Sjamie	CURVNET_SET(TD_TO_VNET(td));
156012429Sphk
1561185983Skib	for (;;) {
1562127052Struckman		req.oldidx = 0;
1563127052Struckman		req.newidx = 0;
1564216060Smdf		SYSCTL_XLOCK();
1565127052Struckman		error = sysctl_root(0, name, namelen, &req);
1566216060Smdf		SYSCTL_XUNLOCK();
1567185983Skib		if (error != EAGAIN)
1568185983Skib			break;
1569185983Skib		uio_yield();
1570185983Skib	}
157112243Sphk
1572186564Sed	CURVNET_RESTORE();
1573186564Sed
1574127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1575127052Struckman		vsunlock(req.oldptr, req.validlen);
1576192125Sjhb	if (memlocked)
1577192125Sjhb		sx_xunlock(&sysctlmemlock);
157812429Sphk
157912260Sphk	if (error && error != ENOMEM)
158012260Sphk		return (error);
158112260Sphk
158212260Sphk	if (retval) {
1583127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1584127052Struckman			*retval = req.validlen;
158512260Sphk		else
158612260Sphk			*retval = req.oldidx;
158711863Sphk	}
158812260Sphk	return (error);
15891541Srgrimes}
1590212750Smdf
1591212750Smdf/*
1592212750Smdf * Drain into a sysctl struct.  The user buffer must be wired.
1593212750Smdf */
1594212750Smdfstatic int
1595212750Smdfsbuf_sysctl_drain(void *arg, const char *data, int len)
1596212750Smdf{
1597212750Smdf	struct sysctl_req *req = arg;
1598212750Smdf	int error;
1599212750Smdf
1600212750Smdf	error = SYSCTL_OUT(req, data, len);
1601212750Smdf	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1602212750Smdf	return (error == 0 ? len : -error);
1603212750Smdf}
1604212750Smdf
1605212750Smdfstruct sbuf *
1606212750Smdfsbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1607212750Smdf    struct sysctl_req *req)
1608212750Smdf{
1609212750Smdf
1610212750Smdf	/* Wire the user buffer, so we can write without blocking. */
1611212750Smdf	sysctl_wire_old_buffer(req, 0);
1612212750Smdf
1613212750Smdf	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1614212750Smdf	sbuf_set_drain(s, sbuf_sysctl_drain, req);
1615212750Smdf	return (s);
1616212750Smdf}
1617