kern_sysctl.c revision 221829
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 221829 2011-05-13 05:27:58Z 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
368219819Sjeffint
369219819Sjeffsysctl_remove_name(struct sysctl_oid *parent, const char *name,
370219819Sjeff    int del, int recurse)
371219819Sjeff{
372219819Sjeff	struct sysctl_oid *p, *tmp;
373219819Sjeff	int error;
374219819Sjeff
375219819Sjeff	error = ENOENT;
376219819Sjeff	SYSCTL_XLOCK();
377219819Sjeff	SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
378219819Sjeff		if (strcmp(p->oid_name, name) == 0) {
379219819Sjeff			error = sysctl_remove_oid_locked(p, del, recurse);
380219819Sjeff			break;
381219819Sjeff		}
382219819Sjeff	}
383219819Sjeff	SYSCTL_XUNLOCK();
384219819Sjeff
385219819Sjeff	return (error);
386219819Sjeff}
387219819Sjeff
388219819Sjeff
389188232Sjhbstatic int
390188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
391188232Sjhb{
392219819Sjeff	struct sysctl_oid *p, *tmp;
39363212Sabial	int error;
39463212Sabial
395188232Sjhb	SYSCTL_ASSERT_XLOCKED();
39663212Sabial	if (oidp == NULL)
39763212Sabial		return(EINVAL);
39863212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
39963212Sabial		printf("can't remove non-dynamic nodes!\n");
40063212Sabial		return (EINVAL);
40163212Sabial	}
40263212Sabial	/*
40363212Sabial	 * WARNING: normal method to do this should be through
40463212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
40563212Sabial	 * method to purge your sysctl tree of leftovers...
40663212Sabial	 * However, if some other code still references these nodes,
40763212Sabial	 * it will panic.
40863212Sabial	 */
40963212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
41063212Sabial		if (oidp->oid_refcnt == 1) {
411219819Sjeff			SLIST_FOREACH_SAFE(p,
412219819Sjeff			    SYSCTL_CHILDREN(oidp), oid_link, tmp) {
41363212Sabial				if (!recurse)
41463212Sabial					return (ENOTEMPTY);
415188232Sjhb				error = sysctl_remove_oid_locked(p, del,
416188232Sjhb				    recurse);
41763212Sabial				if (error)
41863212Sabial					return (error);
41963212Sabial			}
42063212Sabial			if (del)
42163212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
42263212Sabial		}
42363212Sabial	}
42463212Sabial	if (oidp->oid_refcnt > 1 ) {
42563212Sabial		oidp->oid_refcnt--;
42663212Sabial	} else {
42763212Sabial		if (oidp->oid_refcnt == 0) {
42863212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
42963212Sabial				oidp->oid_refcnt, oidp->oid_name);
43063212Sabial			return (EINVAL);
43163212Sabial		}
43263212Sabial		sysctl_unregister_oid(oidp);
43363212Sabial		if (del) {
434216060Smdf			/*
435216060Smdf			 * Wait for all threads running the handler to drain.
436216060Smdf			 * This preserves the previous behavior when the
437216060Smdf			 * sysctl lock was held across a handler invocation,
438216060Smdf			 * and is necessary for module unload correctness.
439216060Smdf			 */
440216060Smdf			while (oidp->oid_running > 0) {
441216060Smdf				oidp->oid_kind |= CTLFLAG_DYING;
442216060Smdf				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
443216060Smdf			}
444141433Sphk			if (oidp->oid_descr)
445141433Sphk				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
44663978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
44763978Speter			     M_SYSCTLOID);
44863212Sabial			free(oidp, M_SYSCTLOID);
44963212Sabial		}
45063212Sabial	}
45163212Sabial	return (0);
45263212Sabial}
45363212Sabial/*
45463212Sabial * Create new sysctls at run time.
45563212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
45663212Sabial */
45763212Sabialstruct sysctl_oid *
45863212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
459219819Sjeff	int number, const char *name, int kind, void *arg1, intptr_t arg2,
46070679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
46163212Sabial{
46263212Sabial	struct sysctl_oid *oidp;
46363212Sabial	ssize_t len;
46463978Speter	char *newname;
46563212Sabial
46663212Sabial	/* You have to hook up somewhere.. */
46763212Sabial	if (parent == NULL)
46863212Sabial		return(NULL);
46963212Sabial	/* Check if the node already exists, otherwise create it */
470188232Sjhb	SYSCTL_XLOCK();
47163212Sabial	oidp = sysctl_find_oidname(name, parent);
47263212Sabial	if (oidp != NULL) {
47363212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
47463212Sabial			oidp->oid_refcnt++;
47563212Sabial			/* Update the context */
47663212Sabial			if (clist != NULL)
47763212Sabial				sysctl_ctx_entry_add(clist, oidp);
478188232Sjhb			SYSCTL_XUNLOCK();
47963212Sabial			return (oidp);
48063212Sabial		} else {
481188232Sjhb			SYSCTL_XUNLOCK();
48263212Sabial			printf("can't re-use a leaf (%s)!\n", name);
48363212Sabial			return (NULL);
48463212Sabial		}
48563212Sabial	}
486111119Simp	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
48763212Sabial	oidp->oid_parent = parent;
48863212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
48963212Sabial	oidp->oid_number = number;
49063212Sabial	oidp->oid_refcnt = 1;
49163212Sabial	len = strlen(name);
492111119Simp	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
49363978Speter	bcopy(name, newname, len + 1);
49463978Speter	newname[len] = '\0';
49563978Speter	oidp->oid_name = newname;
49663212Sabial	oidp->oid_handler = handler;
49763212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
49863212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
49963212Sabial		/* Allocate space for children */
500132776Skan		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
501132776Skan		    M_SYSCTLOID, M_WAITOK));
50263212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
503219819Sjeff		oidp->oid_arg2 = arg2;
50463212Sabial	} else {
50563212Sabial		oidp->oid_arg1 = arg1;
50663212Sabial		oidp->oid_arg2 = arg2;
50763212Sabial	}
50863212Sabial	oidp->oid_fmt = fmt;
50988006Sluigi	if (descr) {
51088006Sluigi		int len = strlen(descr) + 1;
511141433Sphk		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
512141433Sphk		if (oidp->oid_descr)
513141433Sphk			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
51488006Sluigi	}
51563212Sabial	/* Update the context, if used */
51663212Sabial	if (clist != NULL)
51763212Sabial		sysctl_ctx_entry_add(clist, oidp);
51863212Sabial	/* Register this oid */
51963212Sabial	sysctl_register_oid(oidp);
520188232Sjhb	SYSCTL_XUNLOCK();
52163212Sabial	return (oidp);
52263212Sabial}
52363212Sabial
52463212Sabial/*
525174113Speter * Rename an existing oid.
526174113Speter */
527174113Spetervoid
528174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
529174113Speter{
530174113Speter	ssize_t len;
531174113Speter	char *newname;
532174113Speter	void *oldname;
533174113Speter
534174113Speter	len = strlen(name);
535174113Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
536174113Speter	bcopy(name, newname, len + 1);
537174113Speter	newname[len] = '\0';
538188232Sjhb	SYSCTL_XLOCK();
539188232Sjhb	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
540174113Speter	oidp->oid_name = newname;
541188232Sjhb	SYSCTL_XUNLOCK();
542174113Speter	free(oldname, M_SYSCTLOID);
543174113Speter}
544174113Speter
545174113Speter/*
546126319Sdes * Reparent an existing oid.
547126319Sdes */
548126319Sdesint
549126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
550126319Sdes{
551126319Sdes	struct sysctl_oid *oidp;
552126319Sdes
553188232Sjhb	SYSCTL_XLOCK();
554188232Sjhb	if (oid->oid_parent == parent) {
555188232Sjhb		SYSCTL_XUNLOCK();
556126319Sdes		return (0);
557188232Sjhb	}
558126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
559188232Sjhb	if (oidp != NULL) {
560188232Sjhb		SYSCTL_XUNLOCK();
561126319Sdes		return (EEXIST);
562188232Sjhb	}
563126319Sdes	sysctl_unregister_oid(oid);
564126319Sdes	oid->oid_parent = parent;
565126319Sdes	oid->oid_number = OID_AUTO;
566126319Sdes	sysctl_register_oid(oid);
567188232Sjhb	SYSCTL_XUNLOCK();
568126319Sdes	return (0);
569126319Sdes}
570126319Sdes
571126319Sdes/*
57244078Sdfr * Register the kernel's oids on startup.
57344078Sdfr */
57478161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
57512152Sphk
57680338Sroamstatic void
57780338Sroamsysctl_register_all(void *arg)
57838869Sbde{
57978161Speter	struct sysctl_oid **oidp;
58078161Speter
581192125Sjhb	sx_init(&sysctlmemlock, "sysctl mem");
58293625Srwatson	SYSCTL_INIT();
583188232Sjhb	SYSCTL_XLOCK();
58478161Speter	SET_FOREACH(oidp, sysctl_set)
58578161Speter		sysctl_register_oid(*oidp);
586188232Sjhb	SYSCTL_XUNLOCK();
58738869Sbde}
58844078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
58944078Sdfr
59012623Sphk/*
59112623Sphk * "Staff-functions"
59212623Sphk *
59312650Sphk * These functions implement a presently undocumented interface
59412650Sphk * used by the sysctl program to walk the tree, and get the type
59512650Sphk * so it can print the value.
59612650Sphk * This interface is under work and consideration, and should probably
59712650Sphk * be killed with a big axe by the first person who can find the time.
59812650Sphk * (be aware though, that the proper interface isn't as obvious as it
59912650Sphk * may seem, there are various conflicting requirements.
60012650Sphk *
60112623Sphk * {0,0}	printf the entire MIB-tree.
60212623Sphk * {0,1,...}	return the name of the "..." OID.
60342467Sphk * {0,2,...}	return the next OID.
60412623Sphk * {0,3}	return the OID of the name in "new"
60512650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
60688006Sluigi * {0,5,...}	return the description the "..." OID.
60712623Sphk */
60812623Sphk
609136999Srwatson#ifdef SYSCTL_DEBUG
61012152Sphkstatic void
61144078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
61212152Sphk{
61344078Sdfr	int k;
61444078Sdfr	struct sysctl_oid *oidp;
61512152Sphk
616216060Smdf	SYSCTL_ASSERT_XLOCKED();
61744078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
61812152Sphk
61912152Sphk		for (k=0; k<i; k++)
62012152Sphk			printf(" ");
62112152Sphk
62244078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
62312152Sphk
62412152Sphk		printf("%c%c",
62544078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
62644078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
62712152Sphk
62844078Sdfr		if (oidp->oid_handler)
62915241Sphk			printf(" *Handler");
63015241Sphk
63144078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
63212243Sphk			case CTLTYPE_NODE:
63315241Sphk				printf(" Node\n");
63444078Sdfr				if (!oidp->oid_handler) {
63512152Sphk					sysctl_sysctl_debug_dump_node(
63644078Sdfr						oidp->oid_arg1, i+2);
63712152Sphk				}
63812152Sphk				break;
63912152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
640217616Smdf			case CTLTYPE_UINT:   printf(" u_int\n"); break;
641217616Smdf			case CTLTYPE_LONG:   printf(" Long\n"); break;
642217616Smdf			case CTLTYPE_ULONG:  printf(" u_long\n"); break;
64312152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
644217616Smdf			case CTLTYPE_U64:    printf(" uint64_t\n"); break;
645217616Smdf			case CTLTYPE_S64:    printf(" int64_t\n"); break;
64612152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
64712152Sphk			default:	     printf("\n");
64812152Sphk		}
64912152Sphk
65012152Sphk	}
65112152Sphk}
65212152Sphk
65312152Sphkstatic int
65462573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
65512152Sphk{
65687024Speter	int error;
65787024Speter
658164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
65987024Speter	if (error)
660139483Spjd		return (error);
661216060Smdf	SYSCTL_XLOCK();
66244078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
663216060Smdf	SYSCTL_XUNLOCK();
664139483Spjd	return (ENOENT);
66512152Sphk}
66612152Sphk
66712152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
66812623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
669136999Srwatson#endif
67012152Sphk
67112623Sphkstatic int
67262573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
67312623Sphk{
67412623Sphk	int *name = (int *) arg1;
67512623Sphk	u_int namelen = arg2;
67644078Sdfr	int error = 0;
67744078Sdfr	struct sysctl_oid *oid;
67844972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
67912623Sphk	char buf[10];
68012131Sphk
681216060Smdf	SYSCTL_XLOCK();
68212623Sphk	while (namelen) {
68312623Sphk		if (!lsp) {
68441514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
68512623Sphk			if (req->oldidx)
68612623Sphk				error = SYSCTL_OUT(req, ".", 1);
68712623Sphk			if (!error)
68812623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
68912623Sphk			if (error)
690216060Smdf				goto out;
69112623Sphk			namelen--;
69212623Sphk			name++;
69312623Sphk			continue;
69412623Sphk		}
69544972Sphk		lsp2 = 0;
69644078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
69744078Sdfr			if (oid->oid_number != *name)
69812623Sphk				continue;
69912131Sphk
70012623Sphk			if (req->oldidx)
70112623Sphk				error = SYSCTL_OUT(req, ".", 1);
70212623Sphk			if (!error)
70344078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
70444078Sdfr					strlen(oid->oid_name));
70512623Sphk			if (error)
706216060Smdf				goto out;
70712623Sphk
70812623Sphk			namelen--;
70912623Sphk			name++;
71012623Sphk
71144972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
71212623Sphk				break;
71312623Sphk
71444078Sdfr			if (oid->oid_handler)
71512623Sphk				break;
71612623Sphk
717216058Smdf			lsp2 = SYSCTL_CHILDREN(oid);
71812623Sphk			break;
71912623Sphk		}
72044972Sphk		lsp = lsp2;
72112623Sphk	}
722216060Smdf	error = SYSCTL_OUT(req, "", 1);
723216060Smdf out:
724216060Smdf	SYSCTL_XUNLOCK();
725216060Smdf	return (error);
72612623Sphk}
72712623Sphk
728141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
72912623Sphk
73012623Sphkstatic int
73163978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
73244078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
73312623Sphk{
73444078Sdfr	struct sysctl_oid *oidp;
73512623Sphk
736216060Smdf	SYSCTL_ASSERT_XLOCKED();
73712623Sphk	*len = level;
73844078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
73944078Sdfr		*next = oidp->oid_number;
74044078Sdfr		*oidpp = oidp;
74112623Sphk
742101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
743101650Smux			continue;
744101650Smux
74512623Sphk		if (!namelen) {
74644078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
747139483Spjd				return (0);
74844078Sdfr			if (oidp->oid_handler)
74912623Sphk				/* We really should call the handler here...*/
750139483Spjd				return (0);
751216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
75263978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
75344078Sdfr				len, level+1, oidpp))
754139483Spjd				return (0);
755111260Srwatson			goto emptynode;
75612623Sphk		}
75712623Sphk
75844078Sdfr		if (oidp->oid_number < *name)
75912623Sphk			continue;
76012623Sphk
76144078Sdfr		if (oidp->oid_number > *name) {
76244078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
763139483Spjd				return (0);
76444078Sdfr			if (oidp->oid_handler)
765139483Spjd				return (0);
766216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
76763978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
76844078Sdfr				next+1, len, level+1, oidpp))
76912623Sphk				return (0);
77015241Sphk			goto next;
77112623Sphk		}
77244078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
77312623Sphk			continue;
77412623Sphk
77544078Sdfr		if (oidp->oid_handler)
77612623Sphk			continue;
77712623Sphk
778216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
77963978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
78044078Sdfr			len, level+1, oidpp))
78112623Sphk			return (0);
78215241Sphk	next:
78312623Sphk		namelen = 1;
784111260Srwatson	emptynode:
78512623Sphk		*len = level;
78612623Sphk	}
787139483Spjd	return (1);
78812623Sphk}
78912623Sphk
79012623Sphkstatic int
79162573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
79212623Sphk{
79312623Sphk	int *name = (int *) arg1;
79412623Sphk	u_int namelen = arg2;
79512623Sphk	int i, j, error;
79612623Sphk	struct sysctl_oid *oid;
79744078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
79812623Sphk	int newoid[CTL_MAXNAME];
79912623Sphk
800216060Smdf	SYSCTL_XLOCK();
80163978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
802216060Smdf	SYSCTL_XUNLOCK();
80312623Sphk	if (i)
804139483Spjd		return (ENOENT);
80512650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
80612623Sphk	return (error);
80712623Sphk}
80812623Sphk
809141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
81012623Sphk
81112623Sphkstatic int
812189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
81312623Sphk{
81444078Sdfr	int i;
81544078Sdfr	struct sysctl_oid *oidp;
81644078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
81712623Sphk	char *p;
81812623Sphk
819216060Smdf	SYSCTL_ASSERT_XLOCKED();
820186564Sed
82112623Sphk	if (!*name)
822139483Spjd		return (ENOENT);
82312623Sphk
82412623Sphk	p = name + strlen(name) - 1 ;
82512623Sphk	if (*p == '.')
82612623Sphk		*p = '\0';
82712623Sphk
82812623Sphk	*len = 0;
82912623Sphk
83012623Sphk	for (p = name; *p && *p != '.'; p++)
83112623Sphk		;
83212623Sphk	i = *p;
83312623Sphk	if (i == '.')
83412623Sphk		*p = '\0';
83512623Sphk
83644078Sdfr	oidp = SLIST_FIRST(lsp);
83712623Sphk
83844078Sdfr	while (oidp && *len < CTL_MAXNAME) {
83944078Sdfr		if (strcmp(name, oidp->oid_name)) {
84044078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
84112623Sphk			continue;
84212623Sphk		}
84344078Sdfr		*oid++ = oidp->oid_number;
84412623Sphk		(*len)++;
84512623Sphk
84612623Sphk		if (!i) {
84744078Sdfr			if (oidpp)
84844078Sdfr				*oidpp = oidp;
84912623Sphk			return (0);
85012623Sphk		}
85112623Sphk
85244078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
85312623Sphk			break;
85412623Sphk
85544078Sdfr		if (oidp->oid_handler)
85612623Sphk			break;
85712623Sphk
858216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
85944078Sdfr		oidp = SLIST_FIRST(lsp);
86012623Sphk		name = p+1;
86112623Sphk		for (p = name; *p && *p != '.'; p++)
86212623Sphk				;
86312623Sphk		i = *p;
86412623Sphk		if (i == '.')
86512623Sphk			*p = '\0';
86612623Sphk	}
867139483Spjd	return (ENOENT);
86812623Sphk}
86912623Sphk
87012623Sphkstatic int
87162573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
87212623Sphk{
87312623Sphk	char *p;
874216066Smdf	int error, oid[CTL_MAXNAME], len = 0;
87512623Sphk	struct sysctl_oid *op = 0;
87612623Sphk
87712623Sphk	if (!req->newlen)
878139483Spjd		return (ENOENT);
87945140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
88045140Sphk		return (ENAMETOOLONG);
88112623Sphk
882111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
88312623Sphk
88412623Sphk	error = SYSCTL_IN(req, p, req->newlen);
88512623Sphk	if (error) {
88612623Sphk		free(p, M_SYSCTL);
88712623Sphk		return (error);
88812623Sphk	}
88912623Sphk
89012623Sphk	p [req->newlen] = '\0';
89112623Sphk
892216060Smdf	SYSCTL_XLOCK();
89312623Sphk	error = name2oid(p, oid, &len, &op);
894216060Smdf	SYSCTL_XUNLOCK();
89512623Sphk
89612623Sphk	free(p, M_SYSCTL);
89712623Sphk
89812623Sphk	if (error)
89912623Sphk		return (error);
90012623Sphk
90112650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
90212623Sphk	return (error);
90312623Sphk}
90412623Sphk
905217555SmdfSYSCTL_PROC(_sysctl, 3, name2oid,
906217555Smdf    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
907187864Sed    0, 0, sysctl_sysctl_name2oid, "I", "");
90812623Sphk
90912623Sphkstatic int
91062573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
91112623Sphk{
91244078Sdfr	struct sysctl_oid *oid;
91353977Sgreen	int error;
91412623Sphk
915216060Smdf	SYSCTL_XLOCK();
91653977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
91753977Sgreen	if (error)
918216060Smdf		goto out;
91912623Sphk
920216060Smdf	if (oid->oid_fmt == NULL) {
921216060Smdf		error = ENOENT;
922216060Smdf		goto out;
923216060Smdf	}
92453977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
92553977Sgreen	if (error)
926216060Smdf		goto out;
92753977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
928216060Smdf out:
929216060Smdf	SYSCTL_XUNLOCK();
93012650Sphk	return (error);
93112623Sphk}
93212623Sphk
93342467Sphk
934187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
935187864Sed    sysctl_sysctl_oidfmt, "");
93612623Sphk
93788006Sluigistatic int
93888006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
93988006Sluigi{
94088006Sluigi	struct sysctl_oid *oid;
94188006Sluigi	int error;
94288006Sluigi
943216060Smdf	SYSCTL_XLOCK();
94488006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
94588006Sluigi	if (error)
946216060Smdf		goto out;
94788006Sluigi
948216060Smdf	if (oid->oid_descr == NULL) {
949216060Smdf		error = ENOENT;
950216060Smdf		goto out;
951216060Smdf	}
952141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
953216060Smdf out:
954216060Smdf	SYSCTL_XUNLOCK();
95588006Sluigi	return (error);
95688006Sluigi}
95788006Sluigi
958141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
95988006Sluigi
96012243Sphk/*
96112623Sphk * Default "handler" functions.
96212623Sphk */
96312623Sphk
96412623Sphk/*
96542095Sdfr * Handle an int, signed or unsigned.
96612243Sphk * Two cases:
96712243Sphk *     a variable:  point arg1 at it.
96812243Sphk *     a constant:  pass it in arg2.
96912243Sphk */
97012243Sphk
97111865Sphkint
97262573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
97311863Sphk{
974100833Struckman	int tmpout, error = 0;
97511863Sphk
976100833Struckman	/*
977100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
978100833Struckman	 */
97912243Sphk	if (arg1)
980100833Struckman		tmpout = *(int *)arg1;
98120506Sbde	else
982100833Struckman		tmpout = arg2;
983100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
98411863Sphk
98512243Sphk	if (error || !req->newptr)
98612243Sphk		return (error);
98711863Sphk
98812243Sphk	if (!arg1)
98912243Sphk		error = EPERM;
99012243Sphk	else
99112243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
99212243Sphk	return (error);
99311863Sphk}
99411863Sphk
99512243Sphk/*
996155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
997195699Srwatson * Note: this is used by TCP.
998155758Sandre */
999155758Sandre
1000155758Sandreint
1001155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1002155758Sandre{
1003155758Sandre	int error, s, tt;
1004155758Sandre
1005191688Szec	tt = *(int *)arg1;
1006155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
1007155758Sandre
1008155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
1009155758Sandre	if (error || !req->newptr)
1010155758Sandre		return (error);
1011155758Sandre
1012155758Sandre	tt = (int)((int64_t)s * hz / 1000);
1013155758Sandre	if (tt < 1)
1014155758Sandre		return (EINVAL);
1015155758Sandre
1016191688Szec	*(int *)arg1 = tt;
1017155758Sandre	return (0);
1018155758Sandre}
1019155758Sandre
1020155758Sandre
1021155758Sandre/*
102245140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
102338517Sdfr */
102438517Sdfr
102538517Sdfrint
102662573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
102738517Sdfr{
102838517Sdfr	int error = 0;
1029136404Speter	long tmplong;
1030136404Speter#ifdef SCTL_MASK32
1031136404Speter	int tmpint;
1032136404Speter#endif
103338517Sdfr
1034100833Struckman	/*
1035100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
1036100833Struckman	 */
103745140Sphk	if (!arg1)
103845140Sphk		return (EINVAL);
1039136404Speter	tmplong = *(long *)arg1;
1040136404Speter#ifdef SCTL_MASK32
1041136404Speter	if (req->flags & SCTL_MASK32) {
1042136404Speter		tmpint = tmplong;
1043136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1044136404Speter	} else
1045136404Speter#endif
1046136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
104738517Sdfr
104838517Sdfr	if (error || !req->newptr)
104938517Sdfr		return (error);
105038517Sdfr
1051136404Speter#ifdef SCTL_MASK32
1052136404Speter	if (req->flags & SCTL_MASK32) {
1053136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1054136404Speter		*(long *)arg1 = (long)tmpint;
1055136404Speter	} else
1056136404Speter#endif
1057136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
105838517Sdfr	return (error);
105938517Sdfr}
106038517Sdfr
106138517Sdfr/*
1062170288Sdwmalone * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1063170288Sdwmalone */
1064170288Sdwmaloneint
1065217616Smdfsysctl_handle_64(SYSCTL_HANDLER_ARGS)
1066170288Sdwmalone{
1067170288Sdwmalone	int error = 0;
1068170288Sdwmalone	uint64_t tmpout;
1069170288Sdwmalone
1070170288Sdwmalone	/*
1071170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
1072170288Sdwmalone	 */
1073170288Sdwmalone	if (!arg1)
1074170288Sdwmalone		return (EINVAL);
1075170288Sdwmalone	tmpout = *(uint64_t *)arg1;
1076170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1077170288Sdwmalone
1078170288Sdwmalone	if (error || !req->newptr)
1079170288Sdwmalone		return (error);
1080170288Sdwmalone
1081170288Sdwmalone	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1082170288Sdwmalone	return (error);
1083170288Sdwmalone}
1084170288Sdwmalone
1085170288Sdwmalone/*
108612243Sphk * Handle our generic '\0' terminated 'C' string.
108712243Sphk * Two cases:
108812243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
108912243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
109012243Sphk */
109112243Sphk
109211865Sphkint
109362573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
109411863Sphk{
109512243Sphk	int error=0;
1096100833Struckman	char *tmparg;
1097100833Struckman	size_t outlen;
109811863Sphk
1099100833Struckman	/*
1100100833Struckman	 * Attempt to get a coherent snapshot by copying to a
1101100833Struckman	 * temporary kernel buffer.
1102100833Struckman	 */
1103100833Struckmanretry:
1104100833Struckman	outlen = strlen((char *)arg1)+1;
1105111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1106105354Srobert
1107105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1108100833Struckman		free(tmparg, M_SYSCTLTMP);
1109100833Struckman		goto retry;
1110100833Struckman	}
1111105354Srobert
1112100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
1113100833Struckman	free(tmparg, M_SYSCTLTMP);
111411863Sphk
111545140Sphk	if (error || !req->newptr)
111612243Sphk		return (error);
111711863Sphk
111845140Sphk	if ((req->newlen - req->newidx) >= arg2) {
111945140Sphk		error = EINVAL;
112012243Sphk	} else {
112112243Sphk		arg2 = (req->newlen - req->newidx);
112212243Sphk		error = SYSCTL_IN(req, arg1, arg2);
112312243Sphk		((char *)arg1)[arg2] = '\0';
112411863Sphk	}
112512131Sphk
112612131Sphk	return (error);
112711863Sphk}
112811863Sphk
112912243Sphk/*
113012243Sphk * Handle any kind of opaque data.
113112243Sphk * arg1 points to it, arg2 is the size.
113212243Sphk */
113312243Sphk
113411865Sphkint
113562573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
113611863Sphk{
1137120803Sbms	int error, tries;
1138120803Sbms	u_int generation;
1139120813Sbms	struct sysctl_req req2;
114012243Sphk
1141100833Struckman	/*
1142120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
1143120803Sbms	 * pre-emption counter updated from within mi_switch() to
1144120803Sbms	 * determine if we were pre-empted during a bcopy() or
1145120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
1146120803Sbms	 * If we encounter an error, stop immediately.
1147100833Struckman	 */
1148120803Sbms	tries = 0;
1149120813Sbms	req2 = *req;
1150120813Sbmsretry:
1151120813Sbms	generation = curthread->td_generation;
1152120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
1153120813Sbms	if (error)
1154120813Sbms		return (error);
1155120813Sbms	tries++;
1156120813Sbms	if (generation != curthread->td_generation && tries < 3) {
1157120813Sbms		*req = req2;
1158120813Sbms		goto retry;
1159120813Sbms	}
116012243Sphk
116112243Sphk	error = SYSCTL_IN(req, arg1, arg2);
116212243Sphk
116312243Sphk	return (error);
116412243Sphk}
116512243Sphk
116612260Sphk/*
116712260Sphk * Transfer functions to/from kernel space.
116812260Sphk * XXX: rather untested at this point
116912260Sphk */
117012260Sphkstatic int
117138517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
117212243Sphk{
117338517Sdfr	size_t i = 0;
117412260Sphk
117512260Sphk	if (req->oldptr) {
117638517Sdfr		i = l;
117773971Stmm		if (req->oldlen <= req->oldidx)
117873971Stmm			i = 0;
117973971Stmm		else
118073971Stmm			if (i > req->oldlen - req->oldidx)
118173971Stmm				i = req->oldlen - req->oldidx;
118212260Sphk		if (i > 0)
118317971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
118412243Sphk	}
1185192144Skib	req->oldidx += l;
118616282Snate	if (req->oldptr && i != l)
118711863Sphk		return (ENOMEM);
118812260Sphk	return (0);
118912243Sphk}
119012243Sphk
119112260Sphkstatic int
119238517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
119312243Sphk{
119412260Sphk	if (!req->newptr)
1195139483Spjd		return (0);
119612260Sphk	if (req->newlen - req->newidx < l)
119711863Sphk		return (EINVAL);
119817971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
119912243Sphk	req->newidx += l;
120012131Sphk	return (0);
120111863Sphk}
120211863Sphk
120316282Snateint
120483366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1205136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
120616282Snate{
120716282Snate	int error = 0;
120816282Snate	struct sysctl_req req;
120916282Snate
121016282Snate	bzero(&req, sizeof req);
121116282Snate
121286183Srwatson	req.td = td;
1213136404Speter	req.flags = flags;
121416282Snate
121516282Snate	if (oldlenp) {
121616282Snate		req.oldlen = *oldlenp;
121716282Snate	}
1218127052Struckman	req.validlen = req.oldlen;
121916282Snate
122016282Snate	if (old) {
122116282Snate		req.oldptr= old;
122216282Snate	}
122316282Snate
122477646Sdd	if (new != NULL) {
122516282Snate		req.newlen = newlen;
122616282Snate		req.newptr = new;
122716282Snate	}
122816282Snate
122916282Snate	req.oldfunc = sysctl_old_kernel;
123016282Snate	req.newfunc = sysctl_new_kernel;
1231217915Smdf	req.lock = REQ_UNWIRED;
123216282Snate
1233216060Smdf	SYSCTL_XLOCK();
123416282Snate	error = sysctl_root(0, name, namelen, &req);
1235216060Smdf	SYSCTL_XUNLOCK();
1236120813Sbms
1237127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1238127052Struckman		vsunlock(req.oldptr, req.validlen);
123916282Snate
124016282Snate	if (error && error != ENOMEM)
124116282Snate		return (error);
124216282Snate
124316282Snate	if (retval) {
1244127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1245127052Struckman			*retval = req.validlen;
124616282Snate		else
124716282Snate			*retval = req.oldidx;
124816282Snate	}
124916282Snate	return (error);
125016282Snate}
125116282Snate
125276834Sjlemonint
125383366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1254136404Speter    void *new, size_t newlen, size_t *retval, int flags)
125576834Sjlemon{
125676834Sjlemon        int oid[CTL_MAXNAME];
125778620Smjacob        size_t oidlen, plen;
125878620Smjacob	int error;
125976834Sjlemon
126076834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
126176834Sjlemon	oid[1] = 3;		/* name2oid */
126276834Sjlemon	oidlen = sizeof(oid);
126376834Sjlemon
126483366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1265136404Speter	    (void *)name, strlen(name), &plen, flags);
126676834Sjlemon	if (error)
126776834Sjlemon		return (error);
126876834Sjlemon
126983366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1270136404Speter	    new, newlen, retval, flags);
127176834Sjlemon	return (error);
127276834Sjlemon}
127376834Sjlemon
127412260Sphk/*
127512260Sphk * Transfer function to/from user space.
127612260Sphk */
127712260Sphkstatic int
127838517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
127912243Sphk{
128038517Sdfr	int error = 0;
1281126253Struckman	size_t i, len, origidx;
128212243Sphk
1283126253Struckman	origidx = req->oldidx;
1284192144Skib	req->oldidx += l;
1285192144Skib	if (req->oldptr == NULL)
1286126253Struckman		return (0);
1287148864Scsjp	/*
1288148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1289148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1290148864Scsjp	 * write operations to the user page can sleep.
1291148864Scsjp	 */
1292148864Scsjp	if (req->lock != REQ_WIRED)
1293111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1294111883Sjhb		    "sysctl_old_user()");
1295126253Struckman	i = l;
1296127052Struckman	len = req->validlen;
1297126253Struckman	if (len <= origidx)
1298126253Struckman		i = 0;
1299126253Struckman	else {
1300126253Struckman		if (i > len - origidx)
1301126253Struckman			i = len - origidx;
1302126253Struckman		error = copyout(p, (char *)req->oldptr + origidx, i);
130312260Sphk	}
130412243Sphk	if (error)
130512243Sphk		return (error);
1306126253Struckman	if (i < l)
130712243Sphk		return (ENOMEM);
130812260Sphk	return (0);
130912243Sphk}
131012243Sphk
131112260Sphkstatic int
131238517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
131312243Sphk{
131412285Sphk	int error;
131512260Sphk
131612260Sphk	if (!req->newptr)
1317139483Spjd		return (0);
131812260Sphk	if (req->newlen - req->newidx < l)
131912243Sphk		return (EINVAL);
1320148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1321148873Scsjp	    "sysctl_new_user()");
132217971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
132312243Sphk	req->newidx += l;
132412243Sphk	return (error);
132512243Sphk}
132612243Sphk
1327100487Struckman/*
1328100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1329100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1330100487Struckman */
1331126253Struckmanint
1332100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1333100487Struckman{
1334126253Struckman	int ret;
1335192160Sdes	size_t wiredlen;
1336126253Struckman
1337126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1338126253Struckman	ret = 0;
1339217915Smdf	if (req->lock != REQ_WIRED && req->oldptr &&
1340120781Sbms	    req->oldfunc == sysctl_old_user) {
1341127050Struckman		if (wiredlen != 0) {
1342127050Struckman			ret = vslock(req->oldptr, wiredlen);
1343130327Sgreen			if (ret != 0) {
1344130327Sgreen				if (ret != ENOMEM)
1345130327Sgreen					return (ret);
1346130327Sgreen				wiredlen = 0;
1347130327Sgreen			}
1348126253Struckman		}
1349127050Struckman		req->lock = REQ_WIRED;
1350127052Struckman		req->validlen = wiredlen;
1351100487Struckman	}
1352127050Struckman	return (0);
1353100487Struckman}
1354100487Struckman
13551541Srgrimesint
135653977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
135753977Sgreen    int *nindx, struct sysctl_req *req)
135812131Sphk{
1359216059Smdf	struct sysctl_oid_list *lsp;
136044078Sdfr	struct sysctl_oid *oid;
136153977Sgreen	int indx;
136212131Sphk
1363216060Smdf	SYSCTL_ASSERT_XLOCKED();
1364216059Smdf	lsp = &sysctl__children;
136512131Sphk	indx = 0;
1366216059Smdf	while (indx < CTL_MAXNAME) {
1367216059Smdf		SLIST_FOREACH(oid, lsp, oid_link) {
1368216059Smdf			if (oid->oid_number == name[indx])
1369216059Smdf				break;
1370216059Smdf		}
1371216059Smdf		if (oid == NULL)
1372216059Smdf			return (ENOENT);
1373216059Smdf
1374216059Smdf		indx++;
1375216059Smdf		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1376216059Smdf			if (oid->oid_handler != NULL || indx == namelen) {
137753977Sgreen				*noid = oid;
137853977Sgreen				if (nindx != NULL)
137953977Sgreen					*nindx = indx;
1380216060Smdf				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1381216060Smdf				    ("%s found DYING node %p", __func__, oid));
138253977Sgreen				return (0);
138312131Sphk			}
1384216059Smdf			lsp = SYSCTL_CHILDREN(oid);
1385216059Smdf		} else if (indx == namelen) {
1386216059Smdf			*noid = oid;
1387216059Smdf			if (nindx != NULL)
1388216059Smdf				*nindx = indx;
1389216060Smdf			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1390216060Smdf			    ("%s found DYING node %p", __func__, oid));
1391216059Smdf			return (0);
139212131Sphk		} else {
1393216059Smdf			return (ENOTDIR);
139412131Sphk		}
139512131Sphk	}
139653977Sgreen	return (ENOENT);
139753977Sgreen}
139853977Sgreen
139953977Sgreen/*
140053977Sgreen * Traverse our tree, and find the right node, execute whatever it points
140153977Sgreen * to, and return the resulting error code.
140253977Sgreen */
140353977Sgreen
1404104094Sphkstatic int
140562573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
140653977Sgreen{
140753977Sgreen	struct sysctl_oid *oid;
1408109246Sdillon	int error, indx, lvl;
140953977Sgreen
1410216060Smdf	SYSCTL_ASSERT_XLOCKED();
1411186564Sed
141253977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
141353977Sgreen	if (error)
141453977Sgreen		return (error);
141553977Sgreen
141653977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
141753977Sgreen		/*
141853977Sgreen		 * You can't call a sysctl when it's a node, but has
141953977Sgreen		 * no handler.  Inform the user that it's a node.
142053977Sgreen		 * The indx may or may not be the same as namelen.
142153977Sgreen		 */
142253977Sgreen		if (oid->oid_handler == NULL)
142353977Sgreen			return (EISDIR);
142453977Sgreen	}
142553977Sgreen
142683968Srwatson	/* Is this sysctl writable? */
142783968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
142812131Sphk		return (EPERM);
142912131Sphk
143092953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
143192953Srwatson
143283968Srwatson	/* Is this sysctl sensitive to securelevels? */
143383968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1434109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1435109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
143692953Srwatson		if (error)
143792953Srwatson			return (error);
143883968Srwatson	}
143912910Sphk
144083968Srwatson	/* Is this sysctl writable by only privileged users? */
144183968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1442196176Sbz		int priv;
1443196176Sbz
144492953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1445196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1446196176Sbz#ifdef VIMAGE
1447196176Sbz		else if ((oid->oid_kind & CTLFLAG_VNET) &&
1448196176Sbz		     prison_owns_vnet(req->td->td_ucred))
1449196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1450196176Sbz#endif
145192953Srwatson		else
1452196176Sbz			priv = PRIV_SYSCTL_WRITE;
1453196176Sbz		error = priv_check(req->td, priv);
145492953Srwatson		if (error)
145592953Srwatson			return (error);
145683968Srwatson	}
145783968Srwatson
145844078Sdfr	if (!oid->oid_handler)
1459139483Spjd		return (EINVAL);
146012131Sphk
1461126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1462132776Skan		arg1 = (int *)arg1 + indx;
1463126121Spjd		arg2 -= indx;
1464126121Spjd	} else {
1465126121Spjd		arg1 = oid->oid_arg1;
1466126121Spjd		arg2 = oid->oid_arg2;
1467126121Spjd	}
1468126121Spjd#ifdef MAC
1469172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1470126121Spjd	    req);
1471126121Spjd	if (error != 0)
1472126121Spjd		return (error);
1473126121Spjd#endif
1474216060Smdf	oid->oid_running++;
1475216060Smdf	SYSCTL_XUNLOCK();
1476216060Smdf
1477187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1478187656Sjhb		mtx_lock(&Giant);
1479126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1480187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1481187656Sjhb		mtx_unlock(&Giant);
1482126121Spjd
1483216060Smdf	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1484216060Smdf
1485216060Smdf	SYSCTL_XLOCK();
1486216060Smdf	oid->oid_running--;
1487216060Smdf	if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1488216060Smdf		wakeup(&oid->oid_running);
148953977Sgreen	return (error);
149012131Sphk}
149112131Sphk
149212221Sbde#ifndef _SYS_SYSPROTO_H_
149312171Sphkstruct sysctl_args {
149412171Sphk	int	*name;
149512171Sphk	u_int	namelen;
149612171Sphk	void	*old;
149712171Sphk	size_t	*oldlenp;
149812171Sphk	void	*new;
149912171Sphk	size_t	newlen;
150012171Sphk};
150112221Sbde#endif
150212131Sphkint
150383366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap)
15041541Srgrimes{
1505188232Sjhb	int error, i, name[CTL_MAXNAME];
150638517Sdfr	size_t j;
15071541Srgrimes
15081541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
15091541Srgrimes		return (EINVAL);
151011863Sphk
15113308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
15123308Sphk 	if (error)
15131541Srgrimes		return (error);
15141541Srgrimes
151583366Sjulian	error = userland_sysctl(td, name, uap->namelen,
151612171Sphk		uap->old, uap->oldlenp, 0,
1517136404Speter		uap->new, uap->newlen, &j, 0);
151812260Sphk	if (error && error != ENOMEM)
1519186564Sed		return (error);
1520186664Sed	if (uap->oldlenp) {
1521188232Sjhb		i = copyout(&j, uap->oldlenp, sizeof(j));
1522186664Sed		if (i)
1523186664Sed			return (i);
1524186664Sed	}
152512260Sphk	return (error);
152612171Sphk}
152712171Sphk
152812171Sphk/*
152912171Sphk * This is used from various compatibility syscalls too.  That's why name
153012171Sphk * must be in kernel space.
153112171Sphk */
153212171Sphkint
153383366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1534136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1535136404Speter    int flags)
153612171Sphk{
1537192125Sjhb	int error = 0, memlocked;
1538127052Struckman	struct sysctl_req req;
153912171Sphk
154012243Sphk	bzero(&req, sizeof req);
154112243Sphk
154286183Srwatson	req.td = td;
1543136404Speter	req.flags = flags;
154412285Sphk
154512171Sphk	if (oldlenp) {
154612171Sphk		if (inkernel) {
154712243Sphk			req.oldlen = *oldlenp;
154812171Sphk		} else {
154912260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
155012171Sphk			if (error)
155112171Sphk				return (error);
155212171Sphk		}
155312171Sphk	}
1554127052Struckman	req.validlen = req.oldlen;
155512171Sphk
155612243Sphk	if (old) {
155752644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
155812243Sphk			return (EFAULT);
155912243Sphk		req.oldptr= old;
156012243Sphk	}
156112131Sphk
156277646Sdd	if (new != NULL) {
1563172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
156412243Sphk			return (EFAULT);
156512243Sphk		req.newlen = newlen;
156612243Sphk		req.newptr = new;
156711863Sphk	}
156812131Sphk
156912243Sphk	req.oldfunc = sysctl_old_user;
157012243Sphk	req.newfunc = sysctl_new_user;
1571217915Smdf	req.lock = REQ_UNWIRED;
157211863Sphk
1573189707Sjhb#ifdef KTRACE
1574189707Sjhb	if (KTRPOINT(curthread, KTR_SYSCTL))
1575189707Sjhb		ktrsysctl(name, namelen);
1576189707Sjhb#endif
1577192125Sjhb
1578192125Sjhb	if (req.oldlen > PAGE_SIZE) {
1579192125Sjhb		memlocked = 1;
1580192125Sjhb		sx_xlock(&sysctlmemlock);
1581192125Sjhb	} else
1582192125Sjhb		memlocked = 0;
1583194252Sjamie	CURVNET_SET(TD_TO_VNET(td));
158412429Sphk
1585185983Skib	for (;;) {
1586127052Struckman		req.oldidx = 0;
1587127052Struckman		req.newidx = 0;
1588216060Smdf		SYSCTL_XLOCK();
1589127052Struckman		error = sysctl_root(0, name, namelen, &req);
1590216060Smdf		SYSCTL_XUNLOCK();
1591185983Skib		if (error != EAGAIN)
1592185983Skib			break;
1593221829Smdf		kern_yield(PRI_USER);
1594185983Skib	}
159512243Sphk
1596186564Sed	CURVNET_RESTORE();
1597186564Sed
1598127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1599127052Struckman		vsunlock(req.oldptr, req.validlen);
1600192125Sjhb	if (memlocked)
1601192125Sjhb		sx_xunlock(&sysctlmemlock);
160212429Sphk
160312260Sphk	if (error && error != ENOMEM)
160412260Sphk		return (error);
160512260Sphk
160612260Sphk	if (retval) {
1607127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1608127052Struckman			*retval = req.validlen;
160912260Sphk		else
161012260Sphk			*retval = req.oldidx;
161111863Sphk	}
161212260Sphk	return (error);
16131541Srgrimes}
1614212750Smdf
1615212750Smdf/*
1616217916Smdf * Drain into a sysctl struct.  The user buffer should be wired if a page
1617217916Smdf * fault would cause issue.
1618212750Smdf */
1619212750Smdfstatic int
1620212750Smdfsbuf_sysctl_drain(void *arg, const char *data, int len)
1621212750Smdf{
1622212750Smdf	struct sysctl_req *req = arg;
1623212750Smdf	int error;
1624212750Smdf
1625212750Smdf	error = SYSCTL_OUT(req, data, len);
1626212750Smdf	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1627212750Smdf	return (error == 0 ? len : -error);
1628212750Smdf}
1629212750Smdf
1630212750Smdfstruct sbuf *
1631212750Smdfsbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1632212750Smdf    struct sysctl_req *req)
1633212750Smdf{
1634212750Smdf
1635212750Smdf	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1636212750Smdf	sbuf_set_drain(s, sbuf_sysctl_drain, req);
1637212750Smdf	return (s);
1638212750Smdf}
1639