kern_sysctl.c revision 247656
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: stable/9/sys/kern/kern_sysctl.c 247656 2013-03-02 17:39:11Z marius $");
40116182Sobrien
41224159Srwatson#include "opt_capsicum.h"
4231778Seivind#include "opt_compat.h"
43189707Sjhb#include "opt_ktrace.h"
4431778Seivind
451541Srgrimes#include <sys/param.h>
46216060Smdf#include <sys/fail.h>
4748274Speter#include <sys/systm.h>
48224159Srwatson#include <sys/capability.h>
4948274Speter#include <sys/kernel.h>
501541Srgrimes#include <sys/sysctl.h>
5112623Sphk#include <sys/malloc.h>
52164033Srwatson#include <sys/priv.h>
5312662Sdg#include <sys/proc.h>
54194368Sbz#include <sys/jail.h>
5582746Sdillon#include <sys/lock.h>
5682746Sdillon#include <sys/mutex.h>
57212750Smdf#include <sys/sbuf.h>
5893616Salfred#include <sys/sx.h>
5915103Sphk#include <sys/sysproto.h>
60185983Skib#include <sys/uio.h>
61189707Sjhb#ifdef KTRACE
62189707Sjhb#include <sys/ktrace.h>
63189707Sjhb#endif
64163606Srwatson
65195699Srwatson#include <net/vnet.h>
66195699Srwatson
67163606Srwatson#include <security/mac/mac_framework.h>
68163606Srwatson
6912645Sbde#include <vm/vm.h>
7012662Sdg#include <vm/vm_extern.h>
7112645Sbde
7230354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
7363212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
74100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
7530309Sphk
7612429Sphk/*
77188232Sjhb * The sysctllock protects the MIB tree.  It also protects sysctl
78188232Sjhb * contexts used with dynamic sysctls.  The sysctl_register_oid() and
79188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already
80188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are
81188232Sjhb * provided for the few places in the kernel which need to use that
82188232Sjhb * API rather than using the dynamic API.  Use of the dynamic API is
83188232Sjhb * strongly encouraged for most code.
84188232Sjhb *
85192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for
86192125Sjhb * sysctl requests.  This is implemented by serializing any userland
87192125Sjhb * sysctl requests larger than a single page via an exclusive lock.
8812429Sphk */
8993625Srwatsonstatic struct sx sysctllock;
90192125Sjhbstatic struct sx sysctlmemlock;
9112429Sphk
92188232Sjhb#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
93188232Sjhb#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
94188232Sjhb#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
95112107Sjhb#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
96216060Smdf#define	SYSCTL_SLEEP(ch, wmesg, timo)					\
97216060Smdf				sx_sleep(ch, &sysctllock, 0, wmesg, timo)
9893616Salfred
9962573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS);
10012429Sphk
10144078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */
10212152Sphk
103188232Sjhbstatic int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
104188232Sjhb		    int recurse);
105188232Sjhb
10663212Sabialstatic struct sysctl_oid *
10763212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
10863212Sabial{
10963212Sabial	struct sysctl_oid *oidp;
11063212Sabial
111216060Smdf	SYSCTL_ASSERT_XLOCKED();
11263212Sabial	SLIST_FOREACH(oidp, list, oid_link) {
11363212Sabial		if (strcmp(oidp->oid_name, name) == 0) {
11463212Sabial			return (oidp);
11563212Sabial		}
11663212Sabial	}
11763212Sabial	return (NULL);
11863212Sabial}
11963212Sabial
12012623Sphk/*
12112623Sphk * Initialization of the MIB tree.
12212623Sphk *
12344078Sdfr * Order by number in each list.
12412623Sphk */
125188232Sjhbvoid
126188232Sjhbsysctl_lock(void)
127188232Sjhb{
12812429Sphk
129188232Sjhb	SYSCTL_XLOCK();
130188232Sjhb}
131188232Sjhb
13280338Sroamvoid
133188232Sjhbsysctl_unlock(void)
134188232Sjhb{
135188232Sjhb
136188232Sjhb	SYSCTL_XUNLOCK();
137188232Sjhb}
138188232Sjhb
139188232Sjhbvoid
14080338Sroamsysctl_register_oid(struct sysctl_oid *oidp)
14112152Sphk{
14244078Sdfr	struct sysctl_oid_list *parent = oidp->oid_parent;
14344078Sdfr	struct sysctl_oid *p;
14444078Sdfr	struct sysctl_oid *q;
14512197Sbde
14644078Sdfr	/*
14763212Sabial	 * First check if another oid with the same name already
14863212Sabial	 * exists in the parent's list.
14963212Sabial	 */
150188232Sjhb	SYSCTL_ASSERT_XLOCKED();
15163212Sabial	p = sysctl_find_oidname(oidp->oid_name, parent);
15263212Sabial	if (p != NULL) {
15363212Sabial		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
15463212Sabial			p->oid_refcnt++;
15563212Sabial			return;
15663212Sabial		} else {
15763212Sabial			printf("can't re-use a leaf (%s)!\n", p->oid_name);
15863212Sabial			return;
15963212Sabial		}
16063212Sabial	}
16163212Sabial	/*
16244078Sdfr	 * If this oid has a number OID_AUTO, give it a number which
16380339Sroam	 * is greater than any current oid.
16480339Sroam	 * NOTE: DO NOT change the starting value here, change it in
16580339Sroam	 * <sys/sysctl.h>, and make sure it is at least 256 to
16680339Sroam	 * accomodate e.g. net.inet.raw as a static sysctl node.
16744078Sdfr	 */
16844078Sdfr	if (oidp->oid_number == OID_AUTO) {
16980339Sroam		static int newoid = CTL_AUTO_START;
17071510Smckusick
17171510Smckusick		oidp->oid_number = newoid++;
17271510Smckusick		if (newoid == 0x7fffffff)
17371510Smckusick			panic("out of oids");
17444078Sdfr	}
17584832Sroam#if 0
17684832Sroam	else if (oidp->oid_number >= CTL_AUTO_START) {
17784832Sroam		/* do not panic; this happens when unregistering sysctl sets */
17884832Sroam		printf("static sysctl oid too high: %d", oidp->oid_number);
17984832Sroam	}
18084832Sroam#endif
18144078Sdfr
18244078Sdfr	/*
18344078Sdfr	 * Insert the oid into the parent's list in order.
18444078Sdfr	 */
18544078Sdfr	q = NULL;
18644078Sdfr	SLIST_FOREACH(p, parent, oid_link) {
18744078Sdfr		if (oidp->oid_number < p->oid_number)
18844078Sdfr			break;
18944078Sdfr		q = p;
19044078Sdfr	}
19144078Sdfr	if (q)
19244078Sdfr		SLIST_INSERT_AFTER(q, oidp, oid_link);
19344078Sdfr	else
19444078Sdfr		SLIST_INSERT_HEAD(parent, oidp, oid_link);
19512152Sphk}
19612131Sphk
19780338Sroamvoid
19880338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp)
19912152Sphk{
200115391Smux	struct sysctl_oid *p;
201115391Smux	int error;
202115391Smux
203188232Sjhb	SYSCTL_ASSERT_XLOCKED();
204115391Smux	error = ENOENT;
205115391Smux	if (oidp->oid_number == OID_AUTO) {
206115391Smux		error = EINVAL;
207115391Smux	} else {
208115391Smux		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
209115391Smux			if (p == oidp) {
210115391Smux				SLIST_REMOVE(oidp->oid_parent, oidp,
211115391Smux				    sysctl_oid, oid_link);
212115391Smux				error = 0;
213115391Smux				break;
214115391Smux			}
215115391Smux		}
216115391Smux	}
217115391Smux
218115391Smux	/*
219115391Smux	 * This can happen when a module fails to register and is
220115391Smux	 * being unloaded afterwards.  It should not be a panic()
221115391Smux	 * for normal use.
222115391Smux	 */
223115391Smux	if (error)
224115391Smux		printf("%s: failed to unregister sysctl\n", __func__);
22544078Sdfr}
22612152Sphk
22763212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */
22863212Sabialint
22963212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c)
23063212Sabial{
23163212Sabial
23263212Sabial	if (c == NULL) {
23363212Sabial		return (EINVAL);
23463212Sabial	}
235188232Sjhb
236188232Sjhb	/*
237188232Sjhb	 * No locking here, the caller is responsible for not adding
238188232Sjhb	 * new nodes to a context until after this function has
239188232Sjhb	 * returned.
240188232Sjhb	 */
24163212Sabial	TAILQ_INIT(c);
24263212Sabial	return (0);
24363212Sabial}
24463212Sabial
24563212Sabial/* Free the context, and destroy all dynamic oids registered in this context */
24663212Sabialint
24763212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist)
24863212Sabial{
24963212Sabial	struct sysctl_ctx_entry *e, *e1;
25063212Sabial	int error;
25163212Sabial
25263212Sabial	error = 0;
25363212Sabial	/*
25463212Sabial	 * First perform a "dry run" to check if it's ok to remove oids.
25563212Sabial	 * XXX FIXME
25663212Sabial	 * XXX This algorithm is a hack. But I don't know any
25763212Sabial	 * XXX better solution for now...
25863212Sabial	 */
259188232Sjhb	SYSCTL_XLOCK();
26063212Sabial	TAILQ_FOREACH(e, clist, link) {
261188232Sjhb		error = sysctl_remove_oid_locked(e->entry, 0, 0);
26263212Sabial		if (error)
26363212Sabial			break;
26463212Sabial	}
26563212Sabial	/*
26663212Sabial	 * Restore deregistered entries, either from the end,
26763212Sabial	 * or from the place where error occured.
26863212Sabial	 * e contains the entry that was not unregistered
26963212Sabial	 */
27063212Sabial	if (error)
27163212Sabial		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
27263212Sabial	else
27363212Sabial		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
27463212Sabial	while (e1 != NULL) {
27563212Sabial		sysctl_register_oid(e1->entry);
27663212Sabial		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
27763212Sabial	}
278188232Sjhb	if (error) {
279188232Sjhb		SYSCTL_XUNLOCK();
28063212Sabial		return(EBUSY);
281188232Sjhb	}
28263212Sabial	/* Now really delete the entries */
28363212Sabial	e = TAILQ_FIRST(clist);
28463212Sabial	while (e != NULL) {
28563212Sabial		e1 = TAILQ_NEXT(e, link);
286188232Sjhb		error = sysctl_remove_oid_locked(e->entry, 1, 0);
28763212Sabial		if (error)
28863212Sabial			panic("sysctl_remove_oid: corrupt tree, entry: %s",
28963212Sabial			    e->entry->oid_name);
29063212Sabial		free(e, M_SYSCTLOID);
29163212Sabial		e = e1;
29263212Sabial	}
293188232Sjhb	SYSCTL_XUNLOCK();
29463212Sabial	return (error);
29563212Sabial}
29663212Sabial
29763212Sabial/* Add an entry to the context */
29863212Sabialstruct sysctl_ctx_entry *
29963212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
30063212Sabial{
30163212Sabial	struct sysctl_ctx_entry *e;
30263212Sabial
303188232Sjhb	SYSCTL_ASSERT_XLOCKED();
30463212Sabial	if (clist == NULL || oidp == NULL)
30563212Sabial		return(NULL);
306111119Simp	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
30763212Sabial	e->entry = oidp;
30863212Sabial	TAILQ_INSERT_HEAD(clist, e, link);
30963212Sabial	return (e);
31063212Sabial}
31163212Sabial
31263212Sabial/* Find an entry in the context */
31363212Sabialstruct sysctl_ctx_entry *
31463212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
31563212Sabial{
31663212Sabial	struct sysctl_ctx_entry *e;
31763212Sabial
318216060Smdf	SYSCTL_ASSERT_XLOCKED();
31963212Sabial	if (clist == NULL || oidp == NULL)
32063212Sabial		return(NULL);
32171999Sphk	TAILQ_FOREACH(e, clist, link) {
32263212Sabial		if(e->entry == oidp)
32363212Sabial			return(e);
32463212Sabial	}
32563212Sabial	return (e);
32663212Sabial}
32763212Sabial
32844078Sdfr/*
32963212Sabial * Delete an entry from the context.
33063212Sabial * NOTE: this function doesn't free oidp! You have to remove it
33163212Sabial * with sysctl_remove_oid().
33263212Sabial */
33363212Sabialint
33463212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
33563212Sabial{
33663212Sabial	struct sysctl_ctx_entry *e;
33763212Sabial
33863212Sabial	if (clist == NULL || oidp == NULL)
33963212Sabial		return (EINVAL);
340188232Sjhb	SYSCTL_XLOCK();
34163212Sabial	e = sysctl_ctx_entry_find(clist, oidp);
34263212Sabial	if (e != NULL) {
34363212Sabial		TAILQ_REMOVE(clist, e, link);
344188232Sjhb		SYSCTL_XUNLOCK();
34563212Sabial		free(e, M_SYSCTLOID);
34663212Sabial		return (0);
347188232Sjhb	} else {
348188232Sjhb		SYSCTL_XUNLOCK();
34963212Sabial		return (ENOENT);
350188232Sjhb	}
35163212Sabial}
35263212Sabial
35363212Sabial/*
35463212Sabial * Remove dynamically created sysctl trees.
35563212Sabial * oidp - top of the tree to be removed
35663212Sabial * del - if 0 - just deregister, otherwise free up entries as well
35763212Sabial * recurse - if != 0 traverse the subtree to be deleted
35863212Sabial */
35963212Sabialint
36063212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
36163212Sabial{
362188232Sjhb	int error;
363188232Sjhb
364188232Sjhb	SYSCTL_XLOCK();
365188232Sjhb	error = sysctl_remove_oid_locked(oidp, del, recurse);
366188232Sjhb	SYSCTL_XUNLOCK();
367188232Sjhb	return (error);
368188232Sjhb}
369188232Sjhb
370219819Sjeffint
371219819Sjeffsysctl_remove_name(struct sysctl_oid *parent, const char *name,
372219819Sjeff    int del, int recurse)
373219819Sjeff{
374219819Sjeff	struct sysctl_oid *p, *tmp;
375219819Sjeff	int error;
376219819Sjeff
377219819Sjeff	error = ENOENT;
378219819Sjeff	SYSCTL_XLOCK();
379219819Sjeff	SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
380219819Sjeff		if (strcmp(p->oid_name, name) == 0) {
381219819Sjeff			error = sysctl_remove_oid_locked(p, del, recurse);
382219819Sjeff			break;
383219819Sjeff		}
384219819Sjeff	}
385219819Sjeff	SYSCTL_XUNLOCK();
386219819Sjeff
387219819Sjeff	return (error);
388219819Sjeff}
389219819Sjeff
390219819Sjeff
391188232Sjhbstatic int
392188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
393188232Sjhb{
394219819Sjeff	struct sysctl_oid *p, *tmp;
39563212Sabial	int error;
39663212Sabial
397188232Sjhb	SYSCTL_ASSERT_XLOCKED();
39863212Sabial	if (oidp == NULL)
39963212Sabial		return(EINVAL);
40063212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
40163212Sabial		printf("can't remove non-dynamic nodes!\n");
40263212Sabial		return (EINVAL);
40363212Sabial	}
40463212Sabial	/*
40563212Sabial	 * WARNING: normal method to do this should be through
40663212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
40763212Sabial	 * method to purge your sysctl tree of leftovers...
40863212Sabial	 * However, if some other code still references these nodes,
40963212Sabial	 * it will panic.
41063212Sabial	 */
41163212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
41263212Sabial		if (oidp->oid_refcnt == 1) {
413219819Sjeff			SLIST_FOREACH_SAFE(p,
414219819Sjeff			    SYSCTL_CHILDREN(oidp), oid_link, tmp) {
41563212Sabial				if (!recurse)
41663212Sabial					return (ENOTEMPTY);
417188232Sjhb				error = sysctl_remove_oid_locked(p, del,
418188232Sjhb				    recurse);
41963212Sabial				if (error)
42063212Sabial					return (error);
42163212Sabial			}
42263212Sabial			if (del)
42363212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
42463212Sabial		}
42563212Sabial	}
42663212Sabial	if (oidp->oid_refcnt > 1 ) {
42763212Sabial		oidp->oid_refcnt--;
42863212Sabial	} else {
42963212Sabial		if (oidp->oid_refcnt == 0) {
43063212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
43163212Sabial				oidp->oid_refcnt, oidp->oid_name);
43263212Sabial			return (EINVAL);
43363212Sabial		}
43463212Sabial		sysctl_unregister_oid(oidp);
43563212Sabial		if (del) {
436216060Smdf			/*
437216060Smdf			 * Wait for all threads running the handler to drain.
438216060Smdf			 * This preserves the previous behavior when the
439216060Smdf			 * sysctl lock was held across a handler invocation,
440216060Smdf			 * and is necessary for module unload correctness.
441216060Smdf			 */
442216060Smdf			while (oidp->oid_running > 0) {
443216060Smdf				oidp->oid_kind |= CTLFLAG_DYING;
444216060Smdf				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
445216060Smdf			}
446141433Sphk			if (oidp->oid_descr)
447141433Sphk				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
44863978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
44963978Speter			     M_SYSCTLOID);
45063212Sabial			free(oidp, M_SYSCTLOID);
45163212Sabial		}
45263212Sabial	}
45363212Sabial	return (0);
45463212Sabial}
45563212Sabial/*
45663212Sabial * Create new sysctls at run time.
45763212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
45863212Sabial */
45963212Sabialstruct sysctl_oid *
46063212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
461219819Sjeff	int number, const char *name, int kind, void *arg1, intptr_t arg2,
46270679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
46363212Sabial{
46463212Sabial	struct sysctl_oid *oidp;
46563212Sabial	ssize_t len;
46663978Speter	char *newname;
46763212Sabial
46863212Sabial	/* You have to hook up somewhere.. */
46963212Sabial	if (parent == NULL)
47063212Sabial		return(NULL);
47163212Sabial	/* Check if the node already exists, otherwise create it */
472188232Sjhb	SYSCTL_XLOCK();
47363212Sabial	oidp = sysctl_find_oidname(name, parent);
47463212Sabial	if (oidp != NULL) {
47563212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
47663212Sabial			oidp->oid_refcnt++;
47763212Sabial			/* Update the context */
47863212Sabial			if (clist != NULL)
47963212Sabial				sysctl_ctx_entry_add(clist, oidp);
480188232Sjhb			SYSCTL_XUNLOCK();
48163212Sabial			return (oidp);
48263212Sabial		} else {
483188232Sjhb			SYSCTL_XUNLOCK();
48463212Sabial			printf("can't re-use a leaf (%s)!\n", name);
48563212Sabial			return (NULL);
48663212Sabial		}
48763212Sabial	}
488111119Simp	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
48963212Sabial	oidp->oid_parent = parent;
49063212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
49163212Sabial	oidp->oid_number = number;
49263212Sabial	oidp->oid_refcnt = 1;
49363212Sabial	len = strlen(name);
494111119Simp	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
49563978Speter	bcopy(name, newname, len + 1);
49663978Speter	newname[len] = '\0';
49763978Speter	oidp->oid_name = newname;
49863212Sabial	oidp->oid_handler = handler;
49963212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
50063212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
50163212Sabial		/* Allocate space for children */
502132776Skan		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
503132776Skan		    M_SYSCTLOID, M_WAITOK));
50463212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
505219819Sjeff		oidp->oid_arg2 = arg2;
50663212Sabial	} else {
50763212Sabial		oidp->oid_arg1 = arg1;
50863212Sabial		oidp->oid_arg2 = arg2;
50963212Sabial	}
51063212Sabial	oidp->oid_fmt = fmt;
51188006Sluigi	if (descr) {
51288006Sluigi		int len = strlen(descr) + 1;
513141433Sphk		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
514141433Sphk		if (oidp->oid_descr)
515141433Sphk			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
51688006Sluigi	}
51763212Sabial	/* Update the context, if used */
51863212Sabial	if (clist != NULL)
51963212Sabial		sysctl_ctx_entry_add(clist, oidp);
52063212Sabial	/* Register this oid */
52163212Sabial	sysctl_register_oid(oidp);
522188232Sjhb	SYSCTL_XUNLOCK();
52363212Sabial	return (oidp);
52463212Sabial}
52563212Sabial
52663212Sabial/*
527174113Speter * Rename an existing oid.
528174113Speter */
529174113Spetervoid
530174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
531174113Speter{
532174113Speter	ssize_t len;
533174113Speter	char *newname;
534174113Speter	void *oldname;
535174113Speter
536174113Speter	len = strlen(name);
537174113Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
538174113Speter	bcopy(name, newname, len + 1);
539174113Speter	newname[len] = '\0';
540188232Sjhb	SYSCTL_XLOCK();
541188232Sjhb	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
542174113Speter	oidp->oid_name = newname;
543188232Sjhb	SYSCTL_XUNLOCK();
544174113Speter	free(oldname, M_SYSCTLOID);
545174113Speter}
546174113Speter
547174113Speter/*
548126319Sdes * Reparent an existing oid.
549126319Sdes */
550126319Sdesint
551126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
552126319Sdes{
553126319Sdes	struct sysctl_oid *oidp;
554126319Sdes
555188232Sjhb	SYSCTL_XLOCK();
556188232Sjhb	if (oid->oid_parent == parent) {
557188232Sjhb		SYSCTL_XUNLOCK();
558126319Sdes		return (0);
559188232Sjhb	}
560126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
561188232Sjhb	if (oidp != NULL) {
562188232Sjhb		SYSCTL_XUNLOCK();
563126319Sdes		return (EEXIST);
564188232Sjhb	}
565126319Sdes	sysctl_unregister_oid(oid);
566126319Sdes	oid->oid_parent = parent;
567126319Sdes	oid->oid_number = OID_AUTO;
568126319Sdes	sysctl_register_oid(oid);
569188232Sjhb	SYSCTL_XUNLOCK();
570126319Sdes	return (0);
571126319Sdes}
572126319Sdes
573126319Sdes/*
57444078Sdfr * Register the kernel's oids on startup.
57544078Sdfr */
57678161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
57712152Sphk
57880338Sroamstatic void
57980338Sroamsysctl_register_all(void *arg)
58038869Sbde{
58178161Speter	struct sysctl_oid **oidp;
58278161Speter
583192125Sjhb	sx_init(&sysctlmemlock, "sysctl mem");
58493625Srwatson	SYSCTL_INIT();
585188232Sjhb	SYSCTL_XLOCK();
58678161Speter	SET_FOREACH(oidp, sysctl_set)
58778161Speter		sysctl_register_oid(*oidp);
588188232Sjhb	SYSCTL_XUNLOCK();
58938869Sbde}
59044078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
59144078Sdfr
59212623Sphk/*
59312623Sphk * "Staff-functions"
59412623Sphk *
59512650Sphk * These functions implement a presently undocumented interface
59612650Sphk * used by the sysctl program to walk the tree, and get the type
59712650Sphk * so it can print the value.
59812650Sphk * This interface is under work and consideration, and should probably
59912650Sphk * be killed with a big axe by the first person who can find the time.
60012650Sphk * (be aware though, that the proper interface isn't as obvious as it
60112650Sphk * may seem, there are various conflicting requirements.
60212650Sphk *
60312623Sphk * {0,0}	printf the entire MIB-tree.
60412623Sphk * {0,1,...}	return the name of the "..." OID.
60542467Sphk * {0,2,...}	return the next OID.
60612623Sphk * {0,3}	return the OID of the name in "new"
60712650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
60888006Sluigi * {0,5,...}	return the description the "..." OID.
60912623Sphk */
61012623Sphk
611136999Srwatson#ifdef SYSCTL_DEBUG
61212152Sphkstatic void
61344078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
61412152Sphk{
61544078Sdfr	int k;
61644078Sdfr	struct sysctl_oid *oidp;
61712152Sphk
618216060Smdf	SYSCTL_ASSERT_XLOCKED();
61944078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
62012152Sphk
62112152Sphk		for (k=0; k<i; k++)
62212152Sphk			printf(" ");
62312152Sphk
62444078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
62512152Sphk
62612152Sphk		printf("%c%c",
62744078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
62844078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
62912152Sphk
63044078Sdfr		if (oidp->oid_handler)
63115241Sphk			printf(" *Handler");
63215241Sphk
63344078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
63412243Sphk			case CTLTYPE_NODE:
63515241Sphk				printf(" Node\n");
63644078Sdfr				if (!oidp->oid_handler) {
63712152Sphk					sysctl_sysctl_debug_dump_node(
63844078Sdfr						oidp->oid_arg1, i+2);
63912152Sphk				}
64012152Sphk				break;
64112152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
642217616Smdf			case CTLTYPE_UINT:   printf(" u_int\n"); break;
643217616Smdf			case CTLTYPE_LONG:   printf(" Long\n"); break;
644217616Smdf			case CTLTYPE_ULONG:  printf(" u_long\n"); break;
64512152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
646217616Smdf			case CTLTYPE_U64:    printf(" uint64_t\n"); break;
647217616Smdf			case CTLTYPE_S64:    printf(" int64_t\n"); break;
64812152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
64912152Sphk			default:	     printf("\n");
65012152Sphk		}
65112152Sphk
65212152Sphk	}
65312152Sphk}
65412152Sphk
65512152Sphkstatic int
65662573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
65712152Sphk{
65887024Speter	int error;
65987024Speter
660164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
66187024Speter	if (error)
662139483Spjd		return (error);
663216060Smdf	SYSCTL_XLOCK();
66444078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
665216060Smdf	SYSCTL_XUNLOCK();
666139483Spjd	return (ENOENT);
66712152Sphk}
66812152Sphk
66912152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
67012623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
671136999Srwatson#endif
67212152Sphk
67312623Sphkstatic int
67462573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
67512623Sphk{
67612623Sphk	int *name = (int *) arg1;
67712623Sphk	u_int namelen = arg2;
67844078Sdfr	int error = 0;
67944078Sdfr	struct sysctl_oid *oid;
68044972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
68112623Sphk	char buf[10];
68212131Sphk
683216060Smdf	SYSCTL_XLOCK();
68412623Sphk	while (namelen) {
68512623Sphk		if (!lsp) {
68641514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
68712623Sphk			if (req->oldidx)
68812623Sphk				error = SYSCTL_OUT(req, ".", 1);
68912623Sphk			if (!error)
69012623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
69112623Sphk			if (error)
692216060Smdf				goto out;
69312623Sphk			namelen--;
69412623Sphk			name++;
69512623Sphk			continue;
69612623Sphk		}
69744972Sphk		lsp2 = 0;
69844078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
69944078Sdfr			if (oid->oid_number != *name)
70012623Sphk				continue;
70112131Sphk
70212623Sphk			if (req->oldidx)
70312623Sphk				error = SYSCTL_OUT(req, ".", 1);
70412623Sphk			if (!error)
70544078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
70644078Sdfr					strlen(oid->oid_name));
70712623Sphk			if (error)
708216060Smdf				goto out;
70912623Sphk
71012623Sphk			namelen--;
71112623Sphk			name++;
71212623Sphk
71344972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
71412623Sphk				break;
71512623Sphk
71644078Sdfr			if (oid->oid_handler)
71712623Sphk				break;
71812623Sphk
719216058Smdf			lsp2 = SYSCTL_CHILDREN(oid);
72012623Sphk			break;
72112623Sphk		}
72244972Sphk		lsp = lsp2;
72312623Sphk	}
724216060Smdf	error = SYSCTL_OUT(req, "", 1);
725216060Smdf out:
726216060Smdf	SYSCTL_XUNLOCK();
727216060Smdf	return (error);
72812623Sphk}
72912623Sphk
730224159Srwatson/*
731224159Srwatson * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
732224159Srwatson * capability mode.
733224159Srwatson */
734224159Srwatsonstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD,
735224159Srwatson    sysctl_sysctl_name, "");
73612623Sphk
73712623Sphkstatic int
73863978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
73944078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
74012623Sphk{
74144078Sdfr	struct sysctl_oid *oidp;
74212623Sphk
743216060Smdf	SYSCTL_ASSERT_XLOCKED();
74412623Sphk	*len = level;
74544078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
74644078Sdfr		*next = oidp->oid_number;
74744078Sdfr		*oidpp = oidp;
74812623Sphk
749101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
750101650Smux			continue;
751101650Smux
75212623Sphk		if (!namelen) {
75344078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
754139483Spjd				return (0);
75544078Sdfr			if (oidp->oid_handler)
75612623Sphk				/* We really should call the handler here...*/
757139483Spjd				return (0);
758216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
75963978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
76044078Sdfr				len, level+1, oidpp))
761139483Spjd				return (0);
762111260Srwatson			goto emptynode;
76312623Sphk		}
76412623Sphk
76544078Sdfr		if (oidp->oid_number < *name)
76612623Sphk			continue;
76712623Sphk
76844078Sdfr		if (oidp->oid_number > *name) {
76944078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
770139483Spjd				return (0);
77144078Sdfr			if (oidp->oid_handler)
772139483Spjd				return (0);
773216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
77463978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
77544078Sdfr				next+1, len, level+1, oidpp))
77612623Sphk				return (0);
77715241Sphk			goto next;
77812623Sphk		}
77944078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
78012623Sphk			continue;
78112623Sphk
78244078Sdfr		if (oidp->oid_handler)
78312623Sphk			continue;
78412623Sphk
785216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
78663978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
78744078Sdfr			len, level+1, oidpp))
78812623Sphk			return (0);
78915241Sphk	next:
79012623Sphk		namelen = 1;
791111260Srwatson	emptynode:
79212623Sphk		*len = level;
79312623Sphk	}
794139483Spjd	return (1);
79512623Sphk}
79612623Sphk
79712623Sphkstatic int
79862573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
79912623Sphk{
80012623Sphk	int *name = (int *) arg1;
80112623Sphk	u_int namelen = arg2;
80212623Sphk	int i, j, error;
80312623Sphk	struct sysctl_oid *oid;
80444078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
80512623Sphk	int newoid[CTL_MAXNAME];
80612623Sphk
807216060Smdf	SYSCTL_XLOCK();
80863978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
809216060Smdf	SYSCTL_XUNLOCK();
81012623Sphk	if (i)
811139483Spjd		return (ENOENT);
81212650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
81312623Sphk	return (error);
81412623Sphk}
81512623Sphk
816224159Srwatson/*
817224159Srwatson * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
818224159Srwatson * capability mode.
819224159Srwatson */
820224159Srwatsonstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
821224159Srwatson    sysctl_sysctl_next, "");
82212623Sphk
82312623Sphkstatic int
824189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
82512623Sphk{
82644078Sdfr	int i;
82744078Sdfr	struct sysctl_oid *oidp;
82844078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
82912623Sphk	char *p;
83012623Sphk
831216060Smdf	SYSCTL_ASSERT_XLOCKED();
832186564Sed
83312623Sphk	if (!*name)
834139483Spjd		return (ENOENT);
83512623Sphk
83612623Sphk	p = name + strlen(name) - 1 ;
83712623Sphk	if (*p == '.')
83812623Sphk		*p = '\0';
83912623Sphk
84012623Sphk	*len = 0;
84112623Sphk
84212623Sphk	for (p = name; *p && *p != '.'; p++)
84312623Sphk		;
84412623Sphk	i = *p;
84512623Sphk	if (i == '.')
84612623Sphk		*p = '\0';
84712623Sphk
84844078Sdfr	oidp = SLIST_FIRST(lsp);
84912623Sphk
85044078Sdfr	while (oidp && *len < CTL_MAXNAME) {
85144078Sdfr		if (strcmp(name, oidp->oid_name)) {
85244078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
85312623Sphk			continue;
85412623Sphk		}
85544078Sdfr		*oid++ = oidp->oid_number;
85612623Sphk		(*len)++;
85712623Sphk
85812623Sphk		if (!i) {
85944078Sdfr			if (oidpp)
86044078Sdfr				*oidpp = oidp;
86112623Sphk			return (0);
86212623Sphk		}
86312623Sphk
86444078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
86512623Sphk			break;
86612623Sphk
86744078Sdfr		if (oidp->oid_handler)
86812623Sphk			break;
86912623Sphk
870216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
87144078Sdfr		oidp = SLIST_FIRST(lsp);
87212623Sphk		name = p+1;
87312623Sphk		for (p = name; *p && *p != '.'; p++)
87412623Sphk				;
87512623Sphk		i = *p;
87612623Sphk		if (i == '.')
87712623Sphk			*p = '\0';
87812623Sphk	}
879139483Spjd	return (ENOENT);
88012623Sphk}
88112623Sphk
88212623Sphkstatic int
88362573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
88412623Sphk{
88512623Sphk	char *p;
886216066Smdf	int error, oid[CTL_MAXNAME], len = 0;
88712623Sphk	struct sysctl_oid *op = 0;
88812623Sphk
88912623Sphk	if (!req->newlen)
890139483Spjd		return (ENOENT);
89145140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
89245140Sphk		return (ENAMETOOLONG);
89312623Sphk
894111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
89512623Sphk
89612623Sphk	error = SYSCTL_IN(req, p, req->newlen);
89712623Sphk	if (error) {
89812623Sphk		free(p, M_SYSCTL);
89912623Sphk		return (error);
90012623Sphk	}
90112623Sphk
90212623Sphk	p [req->newlen] = '\0';
90312623Sphk
904216060Smdf	SYSCTL_XLOCK();
90512623Sphk	error = name2oid(p, oid, &len, &op);
906216060Smdf	SYSCTL_XUNLOCK();
90712623Sphk
90812623Sphk	free(p, M_SYSCTL);
90912623Sphk
91012623Sphk	if (error)
91112623Sphk		return (error);
91212623Sphk
91312650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
91412623Sphk	return (error);
91512623Sphk}
91612623Sphk
917224159Srwatson/*
918224159Srwatson * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
919224159Srwatson * capability mode.
920224159Srwatson */
921217555SmdfSYSCTL_PROC(_sysctl, 3, name2oid,
922224159Srwatson    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
923224159Srwatson    | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
92412623Sphk
92512623Sphkstatic int
92662573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
92712623Sphk{
92844078Sdfr	struct sysctl_oid *oid;
92953977Sgreen	int error;
93012623Sphk
931216060Smdf	SYSCTL_XLOCK();
93253977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
93353977Sgreen	if (error)
934216060Smdf		goto out;
93512623Sphk
936216060Smdf	if (oid->oid_fmt == NULL) {
937216060Smdf		error = ENOENT;
938216060Smdf		goto out;
939216060Smdf	}
94053977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
94153977Sgreen	if (error)
942216060Smdf		goto out;
94353977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
944216060Smdf out:
945216060Smdf	SYSCTL_XUNLOCK();
94612650Sphk	return (error);
94712623Sphk}
94812623Sphk
94942467Sphk
950224159Srwatsonstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
951187864Sed    sysctl_sysctl_oidfmt, "");
95212623Sphk
95388006Sluigistatic int
95488006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
95588006Sluigi{
95688006Sluigi	struct sysctl_oid *oid;
95788006Sluigi	int error;
95888006Sluigi
959216060Smdf	SYSCTL_XLOCK();
96088006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
96188006Sluigi	if (error)
962216060Smdf		goto out;
96388006Sluigi
964216060Smdf	if (oid->oid_descr == NULL) {
965216060Smdf		error = ENOENT;
966216060Smdf		goto out;
967216060Smdf	}
968141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
969216060Smdf out:
970216060Smdf	SYSCTL_XUNLOCK();
97188006Sluigi	return (error);
97288006Sluigi}
97388006Sluigi
974224159Srwatsonstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD,
975224159Srwatson    sysctl_sysctl_oiddescr, "");
97688006Sluigi
97712243Sphk/*
97812623Sphk * Default "handler" functions.
97912623Sphk */
98012623Sphk
98112623Sphk/*
98242095Sdfr * Handle an int, signed or unsigned.
98312243Sphk * Two cases:
98412243Sphk *     a variable:  point arg1 at it.
98512243Sphk *     a constant:  pass it in arg2.
98612243Sphk */
98712243Sphk
98811865Sphkint
98962573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
99011863Sphk{
991100833Struckman	int tmpout, error = 0;
99211863Sphk
993100833Struckman	/*
994100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
995100833Struckman	 */
99612243Sphk	if (arg1)
997100833Struckman		tmpout = *(int *)arg1;
99820506Sbde	else
999100833Struckman		tmpout = arg2;
1000100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
100111863Sphk
100212243Sphk	if (error || !req->newptr)
100312243Sphk		return (error);
100411863Sphk
100512243Sphk	if (!arg1)
100612243Sphk		error = EPERM;
100712243Sphk	else
100812243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
100912243Sphk	return (error);
101011863Sphk}
101111863Sphk
101212243Sphk/*
1013155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
1014195699Srwatson * Note: this is used by TCP.
1015155758Sandre */
1016155758Sandre
1017155758Sandreint
1018155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1019155758Sandre{
1020155758Sandre	int error, s, tt;
1021155758Sandre
1022191688Szec	tt = *(int *)arg1;
1023155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
1024155758Sandre
1025155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
1026155758Sandre	if (error || !req->newptr)
1027155758Sandre		return (error);
1028155758Sandre
1029155758Sandre	tt = (int)((int64_t)s * hz / 1000);
1030155758Sandre	if (tt < 1)
1031155758Sandre		return (EINVAL);
1032155758Sandre
1033191688Szec	*(int *)arg1 = tt;
1034155758Sandre	return (0);
1035155758Sandre}
1036155758Sandre
1037155758Sandre
1038155758Sandre/*
1039247656Smarius * Handle a long, signed or unsigned.
1040247656Smarius * Two cases:
1041247656Smarius *     a variable:  point arg1 at it.
1042247656Smarius *     a constant:  pass it in arg2.
104338517Sdfr */
104438517Sdfr
104538517Sdfrint
104662573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
104738517Sdfr{
104838517Sdfr	int error = 0;
1049136404Speter	long tmplong;
1050136404Speter#ifdef SCTL_MASK32
1051136404Speter	int tmpint;
1052136404Speter#endif
105338517Sdfr
1054100833Struckman	/*
1055100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
1056100833Struckman	 */
1057247656Smarius	if (arg1)
1058247656Smarius		tmplong = *(long *)arg1;
1059247656Smarius	else
1060247656Smarius		tmplong = arg2;
1061136404Speter#ifdef SCTL_MASK32
1062136404Speter	if (req->flags & SCTL_MASK32) {
1063136404Speter		tmpint = tmplong;
1064136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1065136404Speter	} else
1066136404Speter#endif
1067136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
106838517Sdfr
106938517Sdfr	if (error || !req->newptr)
107038517Sdfr		return (error);
107138517Sdfr
1072247656Smarius	if (!arg1)
1073247656Smarius		error = EPERM;
1074136404Speter#ifdef SCTL_MASK32
1075247656Smarius	else if (req->flags & SCTL_MASK32) {
1076136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1077136404Speter		*(long *)arg1 = (long)tmpint;
1078247656Smarius	}
1079136404Speter#endif
1080247656Smarius	else
1081136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
108238517Sdfr	return (error);
108338517Sdfr}
108438517Sdfr
108538517Sdfr/*
1086247656Smarius * Handle a 64 bit int, signed or unsigned.
1087247656Smarius * Two cases:
1088247656Smarius *     a variable:  point arg1 at it.
1089247656Smarius *     a constant:  pass it in arg2.
1090170288Sdwmalone */
1091170288Sdwmaloneint
1092217616Smdfsysctl_handle_64(SYSCTL_HANDLER_ARGS)
1093170288Sdwmalone{
1094170288Sdwmalone	int error = 0;
1095170288Sdwmalone	uint64_t tmpout;
1096170288Sdwmalone
1097170288Sdwmalone	/*
1098170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
1099170288Sdwmalone	 */
1100247656Smarius	if (arg1)
1101247656Smarius		tmpout = *(uint64_t *)arg1;
1102247656Smarius	else
1103247656Smarius		tmpout = arg2;
1104170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1105170288Sdwmalone
1106170288Sdwmalone	if (error || !req->newptr)
1107170288Sdwmalone		return (error);
1108170288Sdwmalone
1109247656Smarius	if (!arg1)
1110247656Smarius		error = EPERM;
1111247656Smarius	else
1112247656Smarius		error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1113170288Sdwmalone	return (error);
1114170288Sdwmalone}
1115170288Sdwmalone
1116170288Sdwmalone/*
111712243Sphk * Handle our generic '\0' terminated 'C' string.
111812243Sphk * Two cases:
111912243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
112012243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
112112243Sphk */
112212243Sphk
112311865Sphkint
112462573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
112511863Sphk{
112612243Sphk	int error=0;
1127100833Struckman	char *tmparg;
1128100833Struckman	size_t outlen;
112911863Sphk
1130100833Struckman	/*
1131100833Struckman	 * Attempt to get a coherent snapshot by copying to a
1132100833Struckman	 * temporary kernel buffer.
1133100833Struckman	 */
1134100833Struckmanretry:
1135100833Struckman	outlen = strlen((char *)arg1)+1;
1136111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1137105354Srobert
1138105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1139100833Struckman		free(tmparg, M_SYSCTLTMP);
1140100833Struckman		goto retry;
1141100833Struckman	}
1142105354Srobert
1143100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
1144100833Struckman	free(tmparg, M_SYSCTLTMP);
114511863Sphk
114645140Sphk	if (error || !req->newptr)
114712243Sphk		return (error);
114811863Sphk
114945140Sphk	if ((req->newlen - req->newidx) >= arg2) {
115045140Sphk		error = EINVAL;
115112243Sphk	} else {
115212243Sphk		arg2 = (req->newlen - req->newidx);
115312243Sphk		error = SYSCTL_IN(req, arg1, arg2);
115412243Sphk		((char *)arg1)[arg2] = '\0';
115511863Sphk	}
115612131Sphk
115712131Sphk	return (error);
115811863Sphk}
115911863Sphk
116012243Sphk/*
116112243Sphk * Handle any kind of opaque data.
116212243Sphk * arg1 points to it, arg2 is the size.
116312243Sphk */
116412243Sphk
116511865Sphkint
116662573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
116711863Sphk{
1168120803Sbms	int error, tries;
1169120803Sbms	u_int generation;
1170120813Sbms	struct sysctl_req req2;
117112243Sphk
1172100833Struckman	/*
1173120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
1174120803Sbms	 * pre-emption counter updated from within mi_switch() to
1175120803Sbms	 * determine if we were pre-empted during a bcopy() or
1176120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
1177120803Sbms	 * If we encounter an error, stop immediately.
1178100833Struckman	 */
1179120803Sbms	tries = 0;
1180120813Sbms	req2 = *req;
1181120813Sbmsretry:
1182120813Sbms	generation = curthread->td_generation;
1183120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
1184120813Sbms	if (error)
1185120813Sbms		return (error);
1186120813Sbms	tries++;
1187120813Sbms	if (generation != curthread->td_generation && tries < 3) {
1188120813Sbms		*req = req2;
1189120813Sbms		goto retry;
1190120813Sbms	}
119112243Sphk
119212243Sphk	error = SYSCTL_IN(req, arg1, arg2);
119312243Sphk
119412243Sphk	return (error);
119512243Sphk}
119612243Sphk
119712260Sphk/*
119812260Sphk * Transfer functions to/from kernel space.
119912260Sphk * XXX: rather untested at this point
120012260Sphk */
120112260Sphkstatic int
120238517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
120312243Sphk{
120438517Sdfr	size_t i = 0;
120512260Sphk
120612260Sphk	if (req->oldptr) {
120738517Sdfr		i = l;
120873971Stmm		if (req->oldlen <= req->oldidx)
120973971Stmm			i = 0;
121073971Stmm		else
121173971Stmm			if (i > req->oldlen - req->oldidx)
121273971Stmm				i = req->oldlen - req->oldidx;
121312260Sphk		if (i > 0)
121417971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
121512243Sphk	}
1216192144Skib	req->oldidx += l;
121716282Snate	if (req->oldptr && i != l)
121811863Sphk		return (ENOMEM);
121912260Sphk	return (0);
122012243Sphk}
122112243Sphk
122212260Sphkstatic int
122338517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
122412243Sphk{
122512260Sphk	if (!req->newptr)
1226139483Spjd		return (0);
122712260Sphk	if (req->newlen - req->newidx < l)
122811863Sphk		return (EINVAL);
122917971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
123012243Sphk	req->newidx += l;
123112131Sphk	return (0);
123211863Sphk}
123311863Sphk
123416282Snateint
123583366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1236136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
123716282Snate{
123816282Snate	int error = 0;
123916282Snate	struct sysctl_req req;
124016282Snate
124116282Snate	bzero(&req, sizeof req);
124216282Snate
124386183Srwatson	req.td = td;
1244136404Speter	req.flags = flags;
124516282Snate
124616282Snate	if (oldlenp) {
124716282Snate		req.oldlen = *oldlenp;
124816282Snate	}
1249127052Struckman	req.validlen = req.oldlen;
125016282Snate
125116282Snate	if (old) {
125216282Snate		req.oldptr= old;
125316282Snate	}
125416282Snate
125577646Sdd	if (new != NULL) {
125616282Snate		req.newlen = newlen;
125716282Snate		req.newptr = new;
125816282Snate	}
125916282Snate
126016282Snate	req.oldfunc = sysctl_old_kernel;
126116282Snate	req.newfunc = sysctl_new_kernel;
1262217915Smdf	req.lock = REQ_UNWIRED;
126316282Snate
1264216060Smdf	SYSCTL_XLOCK();
126516282Snate	error = sysctl_root(0, name, namelen, &req);
1266216060Smdf	SYSCTL_XUNLOCK();
1267120813Sbms
1268127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1269127052Struckman		vsunlock(req.oldptr, req.validlen);
127016282Snate
127116282Snate	if (error && error != ENOMEM)
127216282Snate		return (error);
127316282Snate
127416282Snate	if (retval) {
1275127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1276127052Struckman			*retval = req.validlen;
127716282Snate		else
127816282Snate			*retval = req.oldidx;
127916282Snate	}
128016282Snate	return (error);
128116282Snate}
128216282Snate
128376834Sjlemonint
128483366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1285136404Speter    void *new, size_t newlen, size_t *retval, int flags)
128676834Sjlemon{
128776834Sjlemon        int oid[CTL_MAXNAME];
128878620Smjacob        size_t oidlen, plen;
128978620Smjacob	int error;
129076834Sjlemon
129176834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
129276834Sjlemon	oid[1] = 3;		/* name2oid */
129376834Sjlemon	oidlen = sizeof(oid);
129476834Sjlemon
129583366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1296136404Speter	    (void *)name, strlen(name), &plen, flags);
129776834Sjlemon	if (error)
129876834Sjlemon		return (error);
129976834Sjlemon
130083366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1301136404Speter	    new, newlen, retval, flags);
130276834Sjlemon	return (error);
130376834Sjlemon}
130476834Sjlemon
130512260Sphk/*
130612260Sphk * Transfer function to/from user space.
130712260Sphk */
130812260Sphkstatic int
130938517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
131012243Sphk{
1311126253Struckman	size_t i, len, origidx;
1312233647Salc	int error;
131312243Sphk
1314126253Struckman	origidx = req->oldidx;
1315192144Skib	req->oldidx += l;
1316192144Skib	if (req->oldptr == NULL)
1317126253Struckman		return (0);
1318148864Scsjp	/*
1319148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1320148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1321148864Scsjp	 * write operations to the user page can sleep.
1322148864Scsjp	 */
1323148864Scsjp	if (req->lock != REQ_WIRED)
1324111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1325111883Sjhb		    "sysctl_old_user()");
1326126253Struckman	i = l;
1327127052Struckman	len = req->validlen;
1328126253Struckman	if (len <= origidx)
1329126253Struckman		i = 0;
1330126253Struckman	else {
1331126253Struckman		if (i > len - origidx)
1332126253Struckman			i = len - origidx;
1333233647Salc		if (req->lock == REQ_WIRED) {
1334233647Salc			error = copyout_nofault(p, (char *)req->oldptr +
1335233647Salc			    origidx, i);
1336233647Salc		} else
1337233647Salc			error = copyout(p, (char *)req->oldptr + origidx, i);
1338233647Salc		if (error != 0)
1339233647Salc			return (error);
134012260Sphk	}
1341126253Struckman	if (i < l)
134212243Sphk		return (ENOMEM);
134312260Sphk	return (0);
134412243Sphk}
134512243Sphk
134612260Sphkstatic int
134738517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
134812243Sphk{
134912285Sphk	int error;
135012260Sphk
135112260Sphk	if (!req->newptr)
1352139483Spjd		return (0);
135312260Sphk	if (req->newlen - req->newidx < l)
135412243Sphk		return (EINVAL);
1355148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1356148873Scsjp	    "sysctl_new_user()");
135717971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
135812243Sphk	req->newidx += l;
135912243Sphk	return (error);
136012243Sphk}
136112243Sphk
1362100487Struckman/*
1363100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1364100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1365100487Struckman */
1366126253Struckmanint
1367100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1368100487Struckman{
1369126253Struckman	int ret;
1370192160Sdes	size_t wiredlen;
1371126253Struckman
1372126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1373126253Struckman	ret = 0;
1374217915Smdf	if (req->lock != REQ_WIRED && req->oldptr &&
1375120781Sbms	    req->oldfunc == sysctl_old_user) {
1376127050Struckman		if (wiredlen != 0) {
1377127050Struckman			ret = vslock(req->oldptr, wiredlen);
1378130327Sgreen			if (ret != 0) {
1379130327Sgreen				if (ret != ENOMEM)
1380130327Sgreen					return (ret);
1381130327Sgreen				wiredlen = 0;
1382130327Sgreen			}
1383126253Struckman		}
1384127050Struckman		req->lock = REQ_WIRED;
1385127052Struckman		req->validlen = wiredlen;
1386100487Struckman	}
1387127050Struckman	return (0);
1388100487Struckman}
1389100487Struckman
13901541Srgrimesint
139153977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
139253977Sgreen    int *nindx, struct sysctl_req *req)
139312131Sphk{
1394216059Smdf	struct sysctl_oid_list *lsp;
139544078Sdfr	struct sysctl_oid *oid;
139653977Sgreen	int indx;
139712131Sphk
1398216060Smdf	SYSCTL_ASSERT_XLOCKED();
1399216059Smdf	lsp = &sysctl__children;
140012131Sphk	indx = 0;
1401216059Smdf	while (indx < CTL_MAXNAME) {
1402216059Smdf		SLIST_FOREACH(oid, lsp, oid_link) {
1403216059Smdf			if (oid->oid_number == name[indx])
1404216059Smdf				break;
1405216059Smdf		}
1406216059Smdf		if (oid == NULL)
1407216059Smdf			return (ENOENT);
1408216059Smdf
1409216059Smdf		indx++;
1410216059Smdf		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1411216059Smdf			if (oid->oid_handler != NULL || indx == namelen) {
141253977Sgreen				*noid = oid;
141353977Sgreen				if (nindx != NULL)
141453977Sgreen					*nindx = indx;
1415216060Smdf				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1416216060Smdf				    ("%s found DYING node %p", __func__, oid));
141753977Sgreen				return (0);
141812131Sphk			}
1419216059Smdf			lsp = SYSCTL_CHILDREN(oid);
1420216059Smdf		} else if (indx == namelen) {
1421216059Smdf			*noid = oid;
1422216059Smdf			if (nindx != NULL)
1423216059Smdf				*nindx = indx;
1424216060Smdf			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1425216060Smdf			    ("%s found DYING node %p", __func__, oid));
1426216059Smdf			return (0);
142712131Sphk		} else {
1428216059Smdf			return (ENOTDIR);
142912131Sphk		}
143012131Sphk	}
143153977Sgreen	return (ENOENT);
143253977Sgreen}
143353977Sgreen
143453977Sgreen/*
143553977Sgreen * Traverse our tree, and find the right node, execute whatever it points
143653977Sgreen * to, and return the resulting error code.
143753977Sgreen */
143853977Sgreen
1439104094Sphkstatic int
144062573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
144153977Sgreen{
144253977Sgreen	struct sysctl_oid *oid;
1443109246Sdillon	int error, indx, lvl;
144453977Sgreen
1445216060Smdf	SYSCTL_ASSERT_XLOCKED();
1446186564Sed
144753977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
144853977Sgreen	if (error)
144953977Sgreen		return (error);
145053977Sgreen
145153977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
145253977Sgreen		/*
145353977Sgreen		 * You can't call a sysctl when it's a node, but has
145453977Sgreen		 * no handler.  Inform the user that it's a node.
145553977Sgreen		 * The indx may or may not be the same as namelen.
145653977Sgreen		 */
145753977Sgreen		if (oid->oid_handler == NULL)
145853977Sgreen			return (EISDIR);
145953977Sgreen	}
146053977Sgreen
146183968Srwatson	/* Is this sysctl writable? */
146283968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
146312131Sphk		return (EPERM);
146412131Sphk
146592953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
146692953Srwatson
1467224159Srwatson#ifdef CAPABILITY_MODE
1468224159Srwatson	/*
1469224159Srwatson	 * If the process is in capability mode, then don't permit reading or
1470224159Srwatson	 * writing unless specifically granted for the node.
1471224159Srwatson	 */
1472224159Srwatson	if (IN_CAPABILITY_MODE(req->td)) {
1473224159Srwatson		if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1474224159Srwatson			return (EPERM);
1475224159Srwatson		if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1476224159Srwatson			return (EPERM);
1477224159Srwatson	}
1478224159Srwatson#endif
1479224159Srwatson
148083968Srwatson	/* Is this sysctl sensitive to securelevels? */
148183968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1482109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1483109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
148492953Srwatson		if (error)
148592953Srwatson			return (error);
148683968Srwatson	}
148712910Sphk
148883968Srwatson	/* Is this sysctl writable by only privileged users? */
148983968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1490196176Sbz		int priv;
1491196176Sbz
149292953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1493196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1494196176Sbz#ifdef VIMAGE
1495196176Sbz		else if ((oid->oid_kind & CTLFLAG_VNET) &&
1496196176Sbz		     prison_owns_vnet(req->td->td_ucred))
1497196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1498196176Sbz#endif
149992953Srwatson		else
1500196176Sbz			priv = PRIV_SYSCTL_WRITE;
1501196176Sbz		error = priv_check(req->td, priv);
150292953Srwatson		if (error)
150392953Srwatson			return (error);
150483968Srwatson	}
150583968Srwatson
150644078Sdfr	if (!oid->oid_handler)
1507139483Spjd		return (EINVAL);
150812131Sphk
1509126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1510132776Skan		arg1 = (int *)arg1 + indx;
1511126121Spjd		arg2 -= indx;
1512126121Spjd	} else {
1513126121Spjd		arg1 = oid->oid_arg1;
1514126121Spjd		arg2 = oid->oid_arg2;
1515126121Spjd	}
1516126121Spjd#ifdef MAC
1517172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1518126121Spjd	    req);
1519126121Spjd	if (error != 0)
1520126121Spjd		return (error);
1521126121Spjd#endif
1522216060Smdf	oid->oid_running++;
1523216060Smdf	SYSCTL_XUNLOCK();
1524216060Smdf
1525187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1526187656Sjhb		mtx_lock(&Giant);
1527126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1528187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1529187656Sjhb		mtx_unlock(&Giant);
1530126121Spjd
1531216060Smdf	KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1532216060Smdf
1533216060Smdf	SYSCTL_XLOCK();
1534216060Smdf	oid->oid_running--;
1535216060Smdf	if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1536216060Smdf		wakeup(&oid->oid_running);
153753977Sgreen	return (error);
153812131Sphk}
153912131Sphk
154012221Sbde#ifndef _SYS_SYSPROTO_H_
154112171Sphkstruct sysctl_args {
154212171Sphk	int	*name;
154312171Sphk	u_int	namelen;
154412171Sphk	void	*old;
154512171Sphk	size_t	*oldlenp;
154612171Sphk	void	*new;
154712171Sphk	size_t	newlen;
154812171Sphk};
154912221Sbde#endif
155012131Sphkint
1551225617Skmacysys___sysctl(struct thread *td, struct sysctl_args *uap)
15521541Srgrimes{
1553188232Sjhb	int error, i, name[CTL_MAXNAME];
155438517Sdfr	size_t j;
15551541Srgrimes
15561541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
15571541Srgrimes		return (EINVAL);
155811863Sphk
15593308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
15603308Sphk 	if (error)
15611541Srgrimes		return (error);
15621541Srgrimes
156383366Sjulian	error = userland_sysctl(td, name, uap->namelen,
156412171Sphk		uap->old, uap->oldlenp, 0,
1565136404Speter		uap->new, uap->newlen, &j, 0);
156612260Sphk	if (error && error != ENOMEM)
1567186564Sed		return (error);
1568186664Sed	if (uap->oldlenp) {
1569188232Sjhb		i = copyout(&j, uap->oldlenp, sizeof(j));
1570186664Sed		if (i)
1571186664Sed			return (i);
1572186664Sed	}
157312260Sphk	return (error);
157412171Sphk}
157512171Sphk
157612171Sphk/*
157712171Sphk * This is used from various compatibility syscalls too.  That's why name
157812171Sphk * must be in kernel space.
157912171Sphk */
158012171Sphkint
158183366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1582136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1583136404Speter    int flags)
158412171Sphk{
1585192125Sjhb	int error = 0, memlocked;
1586127052Struckman	struct sysctl_req req;
158712171Sphk
158812243Sphk	bzero(&req, sizeof req);
158912243Sphk
159086183Srwatson	req.td = td;
1591136404Speter	req.flags = flags;
159212285Sphk
159312171Sphk	if (oldlenp) {
159412171Sphk		if (inkernel) {
159512243Sphk			req.oldlen = *oldlenp;
159612171Sphk		} else {
159712260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
159812171Sphk			if (error)
159912171Sphk				return (error);
160012171Sphk		}
160112171Sphk	}
1602127052Struckman	req.validlen = req.oldlen;
160312171Sphk
160412243Sphk	if (old) {
160552644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
160612243Sphk			return (EFAULT);
160712243Sphk		req.oldptr= old;
160812243Sphk	}
160912131Sphk
161077646Sdd	if (new != NULL) {
1611172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
161212243Sphk			return (EFAULT);
161312243Sphk		req.newlen = newlen;
161412243Sphk		req.newptr = new;
161511863Sphk	}
161612131Sphk
161712243Sphk	req.oldfunc = sysctl_old_user;
161812243Sphk	req.newfunc = sysctl_new_user;
1619217915Smdf	req.lock = REQ_UNWIRED;
162011863Sphk
1621189707Sjhb#ifdef KTRACE
1622189707Sjhb	if (KTRPOINT(curthread, KTR_SYSCTL))
1623189707Sjhb		ktrsysctl(name, namelen);
1624189707Sjhb#endif
1625192125Sjhb
1626192125Sjhb	if (req.oldlen > PAGE_SIZE) {
1627192125Sjhb		memlocked = 1;
1628192125Sjhb		sx_xlock(&sysctlmemlock);
1629192125Sjhb	} else
1630192125Sjhb		memlocked = 0;
1631194252Sjamie	CURVNET_SET(TD_TO_VNET(td));
163212429Sphk
1633185983Skib	for (;;) {
1634127052Struckman		req.oldidx = 0;
1635127052Struckman		req.newidx = 0;
1636216060Smdf		SYSCTL_XLOCK();
1637127052Struckman		error = sysctl_root(0, name, namelen, &req);
1638216060Smdf		SYSCTL_XUNLOCK();
1639185983Skib		if (error != EAGAIN)
1640185983Skib			break;
1641221829Smdf		kern_yield(PRI_USER);
1642185983Skib	}
164312243Sphk
1644186564Sed	CURVNET_RESTORE();
1645186564Sed
1646127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1647127052Struckman		vsunlock(req.oldptr, req.validlen);
1648192125Sjhb	if (memlocked)
1649192125Sjhb		sx_xunlock(&sysctlmemlock);
165012429Sphk
165112260Sphk	if (error && error != ENOMEM)
165212260Sphk		return (error);
165312260Sphk
165412260Sphk	if (retval) {
1655127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1656127052Struckman			*retval = req.validlen;
165712260Sphk		else
165812260Sphk			*retval = req.oldidx;
165911863Sphk	}
166012260Sphk	return (error);
16611541Srgrimes}
1662212750Smdf
1663212750Smdf/*
1664217916Smdf * Drain into a sysctl struct.  The user buffer should be wired if a page
1665217916Smdf * fault would cause issue.
1666212750Smdf */
1667212750Smdfstatic int
1668212750Smdfsbuf_sysctl_drain(void *arg, const char *data, int len)
1669212750Smdf{
1670212750Smdf	struct sysctl_req *req = arg;
1671212750Smdf	int error;
1672212750Smdf
1673212750Smdf	error = SYSCTL_OUT(req, data, len);
1674212750Smdf	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1675212750Smdf	return (error == 0 ? len : -error);
1676212750Smdf}
1677212750Smdf
1678212750Smdfstruct sbuf *
1679212750Smdfsbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1680212750Smdf    struct sysctl_req *req)
1681212750Smdf{
1682212750Smdf
1683212750Smdf	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1684212750Smdf	sbuf_set_drain(s, sbuf_sysctl_drain, req);
1685212750Smdf	return (s);
1686212750Smdf}
1687