kern_sysctl.c revision 216059
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 216059 2010-11-29 18:18:00Z mdf $");
40116182Sobrien
4131778Seivind#include "opt_compat.h"
42189707Sjhb#include "opt_ktrace.h"
4331778Seivind
441541Srgrimes#include <sys/param.h>
4548274Speter#include <sys/systm.h>
4648274Speter#include <sys/kernel.h>
471541Srgrimes#include <sys/sysctl.h>
4812623Sphk#include <sys/malloc.h>
49164033Srwatson#include <sys/priv.h>
5012662Sdg#include <sys/proc.h>
51194368Sbz#include <sys/jail.h>
5282746Sdillon#include <sys/lock.h>
5382746Sdillon#include <sys/mutex.h>
54212750Smdf#include <sys/sbuf.h>
5593616Salfred#include <sys/sx.h>
5615103Sphk#include <sys/sysproto.h>
57185983Skib#include <sys/uio.h>
58189707Sjhb#ifdef KTRACE
59189707Sjhb#include <sys/ktrace.h>
60189707Sjhb#endif
61163606Srwatson
62195699Srwatson#include <net/vnet.h>
63195699Srwatson
64163606Srwatson#include <security/mac/mac_framework.h>
65163606Srwatson
6612645Sbde#include <vm/vm.h>
6712662Sdg#include <vm/vm_extern.h>
6812645Sbde
6930354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
7063212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
71100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
7230309Sphk
7312429Sphk/*
74188232Sjhb * The sysctllock protects the MIB tree.  It also protects sysctl
75188232Sjhb * contexts used with dynamic sysctls.  The sysctl_register_oid() and
76188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already
77188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are
78188232Sjhb * provided for the few places in the kernel which need to use that
79188232Sjhb * API rather than using the dynamic API.  Use of the dynamic API is
80188232Sjhb * strongly encouraged for most code.
81188232Sjhb *
82192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for
83192125Sjhb * sysctl requests.  This is implemented by serializing any userland
84192125Sjhb * sysctl requests larger than a single page via an exclusive lock.
8512429Sphk */
8693625Srwatsonstatic struct sx sysctllock;
87192125Sjhbstatic struct sx sysctlmemlock;
8812429Sphk
89188232Sjhb#define	SYSCTL_SLOCK()		sx_slock(&sysctllock)
90188232Sjhb#define	SYSCTL_SUNLOCK()	sx_sunlock(&sysctllock)
91188232Sjhb#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
92188232Sjhb#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
93188232Sjhb#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
94188232Sjhb#define	SYSCTL_ASSERT_LOCKED()	sx_assert(&sysctllock, SA_LOCKED)
95112107Sjhb#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
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
109188232Sjhb	SYSCTL_ASSERT_LOCKED();
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
316188232Sjhb	SYSCTL_ASSERT_LOCKED();
31763212Sabial	if (clist == NULL || oidp == NULL)
31863212Sabial		return(NULL);
31971999Sphk	TAILQ_FOREACH(e, clist, link) {
32063212Sabial		if(e->entry == oidp)
32163212Sabial			return(e);
32263212Sabial	}
32363212Sabial	return (e);
32463212Sabial}
32563212Sabial
32644078Sdfr/*
32763212Sabial * Delete an entry from the context.
32863212Sabial * NOTE: this function doesn't free oidp! You have to remove it
32963212Sabial * with sysctl_remove_oid().
33063212Sabial */
33163212Sabialint
33263212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
33363212Sabial{
33463212Sabial	struct sysctl_ctx_entry *e;
33563212Sabial
33663212Sabial	if (clist == NULL || oidp == NULL)
33763212Sabial		return (EINVAL);
338188232Sjhb	SYSCTL_XLOCK();
33963212Sabial	e = sysctl_ctx_entry_find(clist, oidp);
34063212Sabial	if (e != NULL) {
34163212Sabial		TAILQ_REMOVE(clist, e, link);
342188232Sjhb		SYSCTL_XUNLOCK();
34363212Sabial		free(e, M_SYSCTLOID);
34463212Sabial		return (0);
345188232Sjhb	} else {
346188232Sjhb		SYSCTL_XUNLOCK();
34763212Sabial		return (ENOENT);
348188232Sjhb	}
34963212Sabial}
35063212Sabial
35163212Sabial/*
35263212Sabial * Remove dynamically created sysctl trees.
35363212Sabial * oidp - top of the tree to be removed
35463212Sabial * del - if 0 - just deregister, otherwise free up entries as well
35563212Sabial * recurse - if != 0 traverse the subtree to be deleted
35663212Sabial */
35763212Sabialint
35863212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
35963212Sabial{
360188232Sjhb	int error;
361188232Sjhb
362188232Sjhb	SYSCTL_XLOCK();
363188232Sjhb	error = sysctl_remove_oid_locked(oidp, del, recurse);
364188232Sjhb	SYSCTL_XUNLOCK();
365188232Sjhb	return (error);
366188232Sjhb}
367188232Sjhb
368188232Sjhbstatic int
369188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
370188232Sjhb{
37163212Sabial	struct sysctl_oid *p;
37263212Sabial	int error;
37363212Sabial
374188232Sjhb	SYSCTL_ASSERT_XLOCKED();
37563212Sabial	if (oidp == NULL)
37663212Sabial		return(EINVAL);
37763212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
37863212Sabial		printf("can't remove non-dynamic nodes!\n");
37963212Sabial		return (EINVAL);
38063212Sabial	}
38163212Sabial	/*
38263212Sabial	 * WARNING: normal method to do this should be through
38363212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
38463212Sabial	 * method to purge your sysctl tree of leftovers...
38563212Sabial	 * However, if some other code still references these nodes,
38663212Sabial	 * it will panic.
38763212Sabial	 */
38863212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
38963212Sabial		if (oidp->oid_refcnt == 1) {
39063212Sabial			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
39163212Sabial				if (!recurse)
39263212Sabial					return (ENOTEMPTY);
393188232Sjhb				error = sysctl_remove_oid_locked(p, del,
394188232Sjhb				    recurse);
39563212Sabial				if (error)
39663212Sabial					return (error);
39763212Sabial			}
39863212Sabial			if (del)
39963212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
40063212Sabial		}
40163212Sabial	}
40263212Sabial	if (oidp->oid_refcnt > 1 ) {
40363212Sabial		oidp->oid_refcnt--;
40463212Sabial	} else {
40563212Sabial		if (oidp->oid_refcnt == 0) {
40663212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
40763212Sabial				oidp->oid_refcnt, oidp->oid_name);
40863212Sabial			return (EINVAL);
40963212Sabial		}
41063212Sabial		sysctl_unregister_oid(oidp);
41163212Sabial		if (del) {
412141433Sphk			if (oidp->oid_descr)
413141433Sphk				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
41463978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
41563978Speter			     M_SYSCTLOID);
41663212Sabial			free(oidp, M_SYSCTLOID);
41763212Sabial		}
41863212Sabial	}
41963212Sabial	return (0);
42063212Sabial}
42163212Sabial
42263212Sabial/*
42363212Sabial * Create new sysctls at run time.
42463212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
42563212Sabial */
42663212Sabialstruct sysctl_oid *
42763212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
42870679Sjhb	int number, const char *name, int kind, void *arg1, int arg2,
42970679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
43063212Sabial{
43163212Sabial	struct sysctl_oid *oidp;
43263212Sabial	ssize_t len;
43363978Speter	char *newname;
43463212Sabial
43563212Sabial	/* You have to hook up somewhere.. */
43663212Sabial	if (parent == NULL)
43763212Sabial		return(NULL);
43863212Sabial	/* Check if the node already exists, otherwise create it */
439188232Sjhb	SYSCTL_XLOCK();
44063212Sabial	oidp = sysctl_find_oidname(name, parent);
44163212Sabial	if (oidp != NULL) {
44263212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
44363212Sabial			oidp->oid_refcnt++;
44463212Sabial			/* Update the context */
44563212Sabial			if (clist != NULL)
44663212Sabial				sysctl_ctx_entry_add(clist, oidp);
447188232Sjhb			SYSCTL_XUNLOCK();
44863212Sabial			return (oidp);
44963212Sabial		} else {
450188232Sjhb			SYSCTL_XUNLOCK();
45163212Sabial			printf("can't re-use a leaf (%s)!\n", name);
45263212Sabial			return (NULL);
45363212Sabial		}
45463212Sabial	}
455111119Simp	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
45663212Sabial	oidp->oid_parent = parent;
45763212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
45863212Sabial	oidp->oid_number = number;
45963212Sabial	oidp->oid_refcnt = 1;
46063212Sabial	len = strlen(name);
461111119Simp	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
46263978Speter	bcopy(name, newname, len + 1);
46363978Speter	newname[len] = '\0';
46463978Speter	oidp->oid_name = newname;
46563212Sabial	oidp->oid_handler = handler;
46663212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
46763212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
46863212Sabial		/* Allocate space for children */
469132776Skan		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
470132776Skan		    M_SYSCTLOID, M_WAITOK));
47163212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
47263212Sabial	} else {
47363212Sabial		oidp->oid_arg1 = arg1;
47463212Sabial		oidp->oid_arg2 = arg2;
47563212Sabial	}
47663212Sabial	oidp->oid_fmt = fmt;
47788006Sluigi	if (descr) {
47888006Sluigi		int len = strlen(descr) + 1;
479141433Sphk		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
480141433Sphk		if (oidp->oid_descr)
481141433Sphk			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
48288006Sluigi	}
48363212Sabial	/* Update the context, if used */
48463212Sabial	if (clist != NULL)
48563212Sabial		sysctl_ctx_entry_add(clist, oidp);
48663212Sabial	/* Register this oid */
48763212Sabial	sysctl_register_oid(oidp);
488188232Sjhb	SYSCTL_XUNLOCK();
48963212Sabial	return (oidp);
49063212Sabial}
49163212Sabial
49263212Sabial/*
493174113Speter * Rename an existing oid.
494174113Speter */
495174113Spetervoid
496174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
497174113Speter{
498174113Speter	ssize_t len;
499174113Speter	char *newname;
500174113Speter	void *oldname;
501174113Speter
502174113Speter	len = strlen(name);
503174113Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
504174113Speter	bcopy(name, newname, len + 1);
505174113Speter	newname[len] = '\0';
506188232Sjhb	SYSCTL_XLOCK();
507188232Sjhb	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
508174113Speter	oidp->oid_name = newname;
509188232Sjhb	SYSCTL_XUNLOCK();
510174113Speter	free(oldname, M_SYSCTLOID);
511174113Speter}
512174113Speter
513174113Speter/*
514126319Sdes * Reparent an existing oid.
515126319Sdes */
516126319Sdesint
517126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
518126319Sdes{
519126319Sdes	struct sysctl_oid *oidp;
520126319Sdes
521188232Sjhb	SYSCTL_XLOCK();
522188232Sjhb	if (oid->oid_parent == parent) {
523188232Sjhb		SYSCTL_XUNLOCK();
524126319Sdes		return (0);
525188232Sjhb	}
526126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
527188232Sjhb	if (oidp != NULL) {
528188232Sjhb		SYSCTL_XUNLOCK();
529126319Sdes		return (EEXIST);
530188232Sjhb	}
531126319Sdes	sysctl_unregister_oid(oid);
532126319Sdes	oid->oid_parent = parent;
533126319Sdes	oid->oid_number = OID_AUTO;
534126319Sdes	sysctl_register_oid(oid);
535188232Sjhb	SYSCTL_XUNLOCK();
536126319Sdes	return (0);
537126319Sdes}
538126319Sdes
539126319Sdes/*
54044078Sdfr * Register the kernel's oids on startup.
54144078Sdfr */
54278161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
54312152Sphk
54480338Sroamstatic void
54580338Sroamsysctl_register_all(void *arg)
54638869Sbde{
54778161Speter	struct sysctl_oid **oidp;
54878161Speter
549192125Sjhb	sx_init(&sysctlmemlock, "sysctl mem");
55093625Srwatson	SYSCTL_INIT();
551188232Sjhb	SYSCTL_XLOCK();
55278161Speter	SET_FOREACH(oidp, sysctl_set)
55378161Speter		sysctl_register_oid(*oidp);
554188232Sjhb	SYSCTL_XUNLOCK();
55538869Sbde}
55644078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
55744078Sdfr
55812623Sphk/*
55912623Sphk * "Staff-functions"
56012623Sphk *
56112650Sphk * These functions implement a presently undocumented interface
56212650Sphk * used by the sysctl program to walk the tree, and get the type
56312650Sphk * so it can print the value.
56412650Sphk * This interface is under work and consideration, and should probably
56512650Sphk * be killed with a big axe by the first person who can find the time.
56612650Sphk * (be aware though, that the proper interface isn't as obvious as it
56712650Sphk * may seem, there are various conflicting requirements.
56812650Sphk *
56912623Sphk * {0,0}	printf the entire MIB-tree.
57012623Sphk * {0,1,...}	return the name of the "..." OID.
57142467Sphk * {0,2,...}	return the next OID.
57212623Sphk * {0,3}	return the OID of the name in "new"
57312650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
57488006Sluigi * {0,5,...}	return the description the "..." OID.
57512623Sphk */
57612623Sphk
577136999Srwatson#ifdef SYSCTL_DEBUG
57812152Sphkstatic void
57944078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
58012152Sphk{
58144078Sdfr	int k;
58244078Sdfr	struct sysctl_oid *oidp;
58312152Sphk
584188232Sjhb	SYSCTL_ASSERT_LOCKED();
58544078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
58612152Sphk
58712152Sphk		for (k=0; k<i; k++)
58812152Sphk			printf(" ");
58912152Sphk
59044078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
59112152Sphk
59212152Sphk		printf("%c%c",
59344078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
59444078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
59512152Sphk
59644078Sdfr		if (oidp->oid_handler)
59715241Sphk			printf(" *Handler");
59815241Sphk
59944078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
60012243Sphk			case CTLTYPE_NODE:
60115241Sphk				printf(" Node\n");
60244078Sdfr				if (!oidp->oid_handler) {
60312152Sphk					sysctl_sysctl_debug_dump_node(
60444078Sdfr						oidp->oid_arg1, i+2);
60512152Sphk				}
60612152Sphk				break;
60712152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
60812152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
60912152Sphk			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
61012152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
61112152Sphk			default:	     printf("\n");
61212152Sphk		}
61312152Sphk
61412152Sphk	}
61512152Sphk}
61612152Sphk
61712152Sphkstatic int
61862573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
61912152Sphk{
62087024Speter	int error;
62187024Speter
622164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
62387024Speter	if (error)
624139483Spjd		return (error);
62544078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
626139483Spjd	return (ENOENT);
62712152Sphk}
62812152Sphk
62912152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
63012623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
631136999Srwatson#endif
63212152Sphk
63312623Sphkstatic int
63462573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
63512623Sphk{
63612623Sphk	int *name = (int *) arg1;
63712623Sphk	u_int namelen = arg2;
63844078Sdfr	int error = 0;
63944078Sdfr	struct sysctl_oid *oid;
64044972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
64112623Sphk	char buf[10];
64212131Sphk
643188232Sjhb	SYSCTL_ASSERT_LOCKED();
64412623Sphk	while (namelen) {
64512623Sphk		if (!lsp) {
64641514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
64712623Sphk			if (req->oldidx)
64812623Sphk				error = SYSCTL_OUT(req, ".", 1);
64912623Sphk			if (!error)
65012623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
65112623Sphk			if (error)
65212623Sphk				return (error);
65312623Sphk			namelen--;
65412623Sphk			name++;
65512623Sphk			continue;
65612623Sphk		}
65744972Sphk		lsp2 = 0;
65844078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
65944078Sdfr			if (oid->oid_number != *name)
66012623Sphk				continue;
66112131Sphk
66212623Sphk			if (req->oldidx)
66312623Sphk				error = SYSCTL_OUT(req, ".", 1);
66412623Sphk			if (!error)
66544078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
66644078Sdfr					strlen(oid->oid_name));
66712623Sphk			if (error)
66812623Sphk				return (error);
66912623Sphk
67012623Sphk			namelen--;
67112623Sphk			name++;
67212623Sphk
67344972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
67412623Sphk				break;
67512623Sphk
67644078Sdfr			if (oid->oid_handler)
67712623Sphk				break;
67812623Sphk
679216058Smdf			lsp2 = SYSCTL_CHILDREN(oid);
68012623Sphk			break;
68112623Sphk		}
68244972Sphk		lsp = lsp2;
68312623Sphk	}
68412623Sphk	return (SYSCTL_OUT(req, "", 1));
68512623Sphk}
68612623Sphk
687141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
68812623Sphk
68912623Sphkstatic int
69063978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
69144078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
69212623Sphk{
69344078Sdfr	struct sysctl_oid *oidp;
69412623Sphk
695188232Sjhb	SYSCTL_ASSERT_LOCKED();
69612623Sphk	*len = level;
69744078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
69844078Sdfr		*next = oidp->oid_number;
69944078Sdfr		*oidpp = oidp;
70012623Sphk
701101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
702101650Smux			continue;
703101650Smux
70412623Sphk		if (!namelen) {
70544078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
706139483Spjd				return (0);
70744078Sdfr			if (oidp->oid_handler)
70812623Sphk				/* We really should call the handler here...*/
709139483Spjd				return (0);
710216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
71163978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
71244078Sdfr				len, level+1, oidpp))
713139483Spjd				return (0);
714111260Srwatson			goto emptynode;
71512623Sphk		}
71612623Sphk
71744078Sdfr		if (oidp->oid_number < *name)
71812623Sphk			continue;
71912623Sphk
72044078Sdfr		if (oidp->oid_number > *name) {
72144078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
722139483Spjd				return (0);
72344078Sdfr			if (oidp->oid_handler)
724139483Spjd				return (0);
725216058Smdf			lsp = SYSCTL_CHILDREN(oidp);
72663978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
72744078Sdfr				next+1, len, level+1, oidpp))
72812623Sphk				return (0);
72915241Sphk			goto next;
73012623Sphk		}
73144078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
73212623Sphk			continue;
73312623Sphk
73444078Sdfr		if (oidp->oid_handler)
73512623Sphk			continue;
73612623Sphk
737216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
73863978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
73944078Sdfr			len, level+1, oidpp))
74012623Sphk			return (0);
74115241Sphk	next:
74212623Sphk		namelen = 1;
743111260Srwatson	emptynode:
74412623Sphk		*len = level;
74512623Sphk	}
746139483Spjd	return (1);
74712623Sphk}
74812623Sphk
74912623Sphkstatic int
75062573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
75112623Sphk{
75212623Sphk	int *name = (int *) arg1;
75312623Sphk	u_int namelen = arg2;
75412623Sphk	int i, j, error;
75512623Sphk	struct sysctl_oid *oid;
75644078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
75712623Sphk	int newoid[CTL_MAXNAME];
75812623Sphk
75963978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
76012623Sphk	if (i)
761139483Spjd		return (ENOENT);
76212650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
76312623Sphk	return (error);
76412623Sphk}
76512623Sphk
766141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
76712623Sphk
76812623Sphkstatic int
769189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
77012623Sphk{
77144078Sdfr	int i;
77244078Sdfr	struct sysctl_oid *oidp;
77344078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
77412623Sphk	char *p;
77512623Sphk
776188232Sjhb	SYSCTL_ASSERT_LOCKED();
777186564Sed
77812623Sphk	if (!*name)
779139483Spjd		return (ENOENT);
78012623Sphk
78112623Sphk	p = name + strlen(name) - 1 ;
78212623Sphk	if (*p == '.')
78312623Sphk		*p = '\0';
78412623Sphk
78512623Sphk	*len = 0;
78612623Sphk
78712623Sphk	for (p = name; *p && *p != '.'; p++)
78812623Sphk		;
78912623Sphk	i = *p;
79012623Sphk	if (i == '.')
79112623Sphk		*p = '\0';
79212623Sphk
79344078Sdfr	oidp = SLIST_FIRST(lsp);
79412623Sphk
79544078Sdfr	while (oidp && *len < CTL_MAXNAME) {
79644078Sdfr		if (strcmp(name, oidp->oid_name)) {
79744078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
79812623Sphk			continue;
79912623Sphk		}
80044078Sdfr		*oid++ = oidp->oid_number;
80112623Sphk		(*len)++;
80212623Sphk
80312623Sphk		if (!i) {
80444078Sdfr			if (oidpp)
80544078Sdfr				*oidpp = oidp;
80612623Sphk			return (0);
80712623Sphk		}
80812623Sphk
80944078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
81012623Sphk			break;
81112623Sphk
81244078Sdfr		if (oidp->oid_handler)
81312623Sphk			break;
81412623Sphk
815216058Smdf		lsp = SYSCTL_CHILDREN(oidp);
81644078Sdfr		oidp = SLIST_FIRST(lsp);
81712623Sphk		name = p+1;
81812623Sphk		for (p = name; *p && *p != '.'; p++)
81912623Sphk				;
82012623Sphk		i = *p;
82112623Sphk		if (i == '.')
82212623Sphk			*p = '\0';
82312623Sphk	}
824139483Spjd	return (ENOENT);
82512623Sphk}
82612623Sphk
82712623Sphkstatic int
82862573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
82912623Sphk{
83012623Sphk	char *p;
83112623Sphk	int error, oid[CTL_MAXNAME], len;
83212623Sphk	struct sysctl_oid *op = 0;
83312623Sphk
834188232Sjhb	SYSCTL_ASSERT_LOCKED();
835186564Sed
83612623Sphk	if (!req->newlen)
837139483Spjd		return (ENOENT);
83845140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
83945140Sphk		return (ENAMETOOLONG);
84012623Sphk
841111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
84212623Sphk
84312623Sphk	error = SYSCTL_IN(req, p, req->newlen);
84412623Sphk	if (error) {
84512623Sphk		free(p, M_SYSCTL);
84612623Sphk		return (error);
84712623Sphk	}
84812623Sphk
84912623Sphk	p [req->newlen] = '\0';
85012623Sphk
85112623Sphk	error = name2oid(p, oid, &len, &op);
85212623Sphk
85312623Sphk	free(p, M_SYSCTL);
85412623Sphk
85512623Sphk	if (error)
85612623Sphk		return (error);
85712623Sphk
85812650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
85912623Sphk	return (error);
86012623Sphk}
86112623Sphk
862187864SedSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
863187864Sed    0, 0, sysctl_sysctl_name2oid, "I", "");
86412623Sphk
86512623Sphkstatic int
86662573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
86712623Sphk{
86844078Sdfr	struct sysctl_oid *oid;
86953977Sgreen	int error;
87012623Sphk
87153977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
87253977Sgreen	if (error)
87353977Sgreen		return (error);
87412623Sphk
87544078Sdfr	if (!oid->oid_fmt)
87653977Sgreen		return (ENOENT);
87753977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
87853977Sgreen	if (error)
87953977Sgreen		return (error);
88053977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
88112650Sphk	return (error);
88212623Sphk}
88312623Sphk
88442467Sphk
885187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
886187864Sed    sysctl_sysctl_oidfmt, "");
88712623Sphk
88888006Sluigistatic int
88988006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
89088006Sluigi{
89188006Sluigi	struct sysctl_oid *oid;
89288006Sluigi	int error;
89388006Sluigi
89488006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
89588006Sluigi	if (error)
89688006Sluigi		return (error);
89788006Sluigi
898141433Sphk	if (!oid->oid_descr)
89988006Sluigi		return (ENOENT);
900141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
90188006Sluigi	return (error);
90288006Sluigi}
90388006Sluigi
904141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
90588006Sluigi
90612243Sphk/*
90712623Sphk * Default "handler" functions.
90812623Sphk */
90912623Sphk
91012623Sphk/*
91142095Sdfr * Handle an int, signed or unsigned.
91212243Sphk * Two cases:
91312243Sphk *     a variable:  point arg1 at it.
91412243Sphk *     a constant:  pass it in arg2.
91512243Sphk */
91612243Sphk
91711865Sphkint
91862573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
91911863Sphk{
920100833Struckman	int tmpout, error = 0;
92111863Sphk
922100833Struckman	/*
923100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
924100833Struckman	 */
92512243Sphk	if (arg1)
926100833Struckman		tmpout = *(int *)arg1;
92720506Sbde	else
928100833Struckman		tmpout = arg2;
929100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
93011863Sphk
93112243Sphk	if (error || !req->newptr)
93212243Sphk		return (error);
93311863Sphk
93412243Sphk	if (!arg1)
93512243Sphk		error = EPERM;
93612243Sphk	else
93712243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
93812243Sphk	return (error);
93911863Sphk}
94011863Sphk
94112243Sphk/*
942155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
943195699Srwatson * Note: this is used by TCP.
944155758Sandre */
945155758Sandre
946155758Sandreint
947155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
948155758Sandre{
949155758Sandre	int error, s, tt;
950155758Sandre
951191688Szec	tt = *(int *)arg1;
952155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
953155758Sandre
954155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
955155758Sandre	if (error || !req->newptr)
956155758Sandre		return (error);
957155758Sandre
958155758Sandre	tt = (int)((int64_t)s * hz / 1000);
959155758Sandre	if (tt < 1)
960155758Sandre		return (EINVAL);
961155758Sandre
962191688Szec	*(int *)arg1 = tt;
963155758Sandre	return (0);
964155758Sandre}
965155758Sandre
966155758Sandre
967155758Sandre/*
96845140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
96938517Sdfr */
97038517Sdfr
97138517Sdfrint
97262573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
97338517Sdfr{
97438517Sdfr	int error = 0;
975136404Speter	long tmplong;
976136404Speter#ifdef SCTL_MASK32
977136404Speter	int tmpint;
978136404Speter#endif
97938517Sdfr
980100833Struckman	/*
981100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
982100833Struckman	 */
98345140Sphk	if (!arg1)
98445140Sphk		return (EINVAL);
985136404Speter	tmplong = *(long *)arg1;
986136404Speter#ifdef SCTL_MASK32
987136404Speter	if (req->flags & SCTL_MASK32) {
988136404Speter		tmpint = tmplong;
989136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
990136404Speter	} else
991136404Speter#endif
992136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
99338517Sdfr
99438517Sdfr	if (error || !req->newptr)
99538517Sdfr		return (error);
99638517Sdfr
997136404Speter#ifdef SCTL_MASK32
998136404Speter	if (req->flags & SCTL_MASK32) {
999136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1000136404Speter		*(long *)arg1 = (long)tmpint;
1001136404Speter	} else
1002136404Speter#endif
1003136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
100438517Sdfr	return (error);
100538517Sdfr}
100638517Sdfr
100738517Sdfr/*
1008170288Sdwmalone * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1009170288Sdwmalone */
1010170288Sdwmalone
1011170288Sdwmaloneint
1012170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1013170288Sdwmalone{
1014170288Sdwmalone	int error = 0;
1015170288Sdwmalone	uint64_t tmpout;
1016170288Sdwmalone
1017170288Sdwmalone	/*
1018170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
1019170288Sdwmalone	 */
1020170288Sdwmalone	if (!arg1)
1021170288Sdwmalone		return (EINVAL);
1022170288Sdwmalone	tmpout = *(uint64_t *)arg1;
1023170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1024170288Sdwmalone
1025170288Sdwmalone	if (error || !req->newptr)
1026170288Sdwmalone		return (error);
1027170288Sdwmalone
1028170288Sdwmalone	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1029170288Sdwmalone	return (error);
1030170288Sdwmalone}
1031170288Sdwmalone
1032170288Sdwmalone/*
103312243Sphk * Handle our generic '\0' terminated 'C' string.
103412243Sphk * Two cases:
103512243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
103612243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
103712243Sphk */
103812243Sphk
103911865Sphkint
104062573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
104111863Sphk{
104212243Sphk	int error=0;
1043100833Struckman	char *tmparg;
1044100833Struckman	size_t outlen;
104511863Sphk
1046100833Struckman	/*
1047100833Struckman	 * Attempt to get a coherent snapshot by copying to a
1048100833Struckman	 * temporary kernel buffer.
1049100833Struckman	 */
1050100833Struckmanretry:
1051100833Struckman	outlen = strlen((char *)arg1)+1;
1052111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1053105354Srobert
1054105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1055100833Struckman		free(tmparg, M_SYSCTLTMP);
1056100833Struckman		goto retry;
1057100833Struckman	}
1058105354Srobert
1059100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
1060100833Struckman	free(tmparg, M_SYSCTLTMP);
106111863Sphk
106245140Sphk	if (error || !req->newptr)
106312243Sphk		return (error);
106411863Sphk
106545140Sphk	if ((req->newlen - req->newidx) >= arg2) {
106645140Sphk		error = EINVAL;
106712243Sphk	} else {
106812243Sphk		arg2 = (req->newlen - req->newidx);
106912243Sphk		error = SYSCTL_IN(req, arg1, arg2);
107012243Sphk		((char *)arg1)[arg2] = '\0';
107111863Sphk	}
107212131Sphk
107312131Sphk	return (error);
107411863Sphk}
107511863Sphk
107612243Sphk/*
107712243Sphk * Handle any kind of opaque data.
107812243Sphk * arg1 points to it, arg2 is the size.
107912243Sphk */
108012243Sphk
108111865Sphkint
108262573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
108311863Sphk{
1084120803Sbms	int error, tries;
1085120803Sbms	u_int generation;
1086120813Sbms	struct sysctl_req req2;
108712243Sphk
1088100833Struckman	/*
1089120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
1090120803Sbms	 * pre-emption counter updated from within mi_switch() to
1091120803Sbms	 * determine if we were pre-empted during a bcopy() or
1092120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
1093120803Sbms	 * If we encounter an error, stop immediately.
1094100833Struckman	 */
1095120803Sbms	tries = 0;
1096120813Sbms	req2 = *req;
1097120813Sbmsretry:
1098120813Sbms	generation = curthread->td_generation;
1099120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
1100120813Sbms	if (error)
1101120813Sbms		return (error);
1102120813Sbms	tries++;
1103120813Sbms	if (generation != curthread->td_generation && tries < 3) {
1104120813Sbms		*req = req2;
1105120813Sbms		goto retry;
1106120813Sbms	}
110712243Sphk
110812243Sphk	error = SYSCTL_IN(req, arg1, arg2);
110912243Sphk
111012243Sphk	return (error);
111112243Sphk}
111212243Sphk
111312260Sphk/*
111412260Sphk * Transfer functions to/from kernel space.
111512260Sphk * XXX: rather untested at this point
111612260Sphk */
111712260Sphkstatic int
111838517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
111912243Sphk{
112038517Sdfr	size_t i = 0;
112112260Sphk
112212260Sphk	if (req->oldptr) {
112338517Sdfr		i = l;
112473971Stmm		if (req->oldlen <= req->oldidx)
112573971Stmm			i = 0;
112673971Stmm		else
112773971Stmm			if (i > req->oldlen - req->oldidx)
112873971Stmm				i = req->oldlen - req->oldidx;
112912260Sphk		if (i > 0)
113017971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
113112243Sphk	}
1132192144Skib	req->oldidx += l;
113316282Snate	if (req->oldptr && i != l)
113411863Sphk		return (ENOMEM);
113512260Sphk	return (0);
113612243Sphk}
113712243Sphk
113812260Sphkstatic int
113938517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
114012243Sphk{
114112260Sphk	if (!req->newptr)
1142139483Spjd		return (0);
114312260Sphk	if (req->newlen - req->newidx < l)
114411863Sphk		return (EINVAL);
114517971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
114612243Sphk	req->newidx += l;
114712131Sphk	return (0);
114811863Sphk}
114911863Sphk
115016282Snateint
115183366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1152136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
115316282Snate{
115416282Snate	int error = 0;
115516282Snate	struct sysctl_req req;
115616282Snate
115716282Snate	bzero(&req, sizeof req);
115816282Snate
115986183Srwatson	req.td = td;
1160136404Speter	req.flags = flags;
116116282Snate
116216282Snate	if (oldlenp) {
116316282Snate		req.oldlen = *oldlenp;
116416282Snate	}
1165127052Struckman	req.validlen = req.oldlen;
116616282Snate
116716282Snate	if (old) {
116816282Snate		req.oldptr= old;
116916282Snate	}
117016282Snate
117177646Sdd	if (new != NULL) {
117216282Snate		req.newlen = newlen;
117316282Snate		req.newptr = new;
117416282Snate	}
117516282Snate
117616282Snate	req.oldfunc = sysctl_old_kernel;
117716282Snate	req.newfunc = sysctl_new_kernel;
1178120781Sbms	req.lock = REQ_LOCKED;
117916282Snate
1180188232Sjhb	SYSCTL_SLOCK();
118116282Snate	error = sysctl_root(0, name, namelen, &req);
1182188232Sjhb	SYSCTL_SUNLOCK();
1183120813Sbms
1184127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1185127052Struckman		vsunlock(req.oldptr, req.validlen);
118616282Snate
118716282Snate	if (error && error != ENOMEM)
118816282Snate		return (error);
118916282Snate
119016282Snate	if (retval) {
1191127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1192127052Struckman			*retval = req.validlen;
119316282Snate		else
119416282Snate			*retval = req.oldidx;
119516282Snate	}
119616282Snate	return (error);
119716282Snate}
119816282Snate
119976834Sjlemonint
120083366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1201136404Speter    void *new, size_t newlen, size_t *retval, int flags)
120276834Sjlemon{
120376834Sjlemon        int oid[CTL_MAXNAME];
120478620Smjacob        size_t oidlen, plen;
120578620Smjacob	int error;
120676834Sjlemon
120776834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
120876834Sjlemon	oid[1] = 3;		/* name2oid */
120976834Sjlemon	oidlen = sizeof(oid);
121076834Sjlemon
121183366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1212136404Speter	    (void *)name, strlen(name), &plen, flags);
121376834Sjlemon	if (error)
121476834Sjlemon		return (error);
121576834Sjlemon
121683366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1217136404Speter	    new, newlen, retval, flags);
121876834Sjlemon	return (error);
121976834Sjlemon}
122076834Sjlemon
122112260Sphk/*
122212260Sphk * Transfer function to/from user space.
122312260Sphk */
122412260Sphkstatic int
122538517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
122612243Sphk{
122738517Sdfr	int error = 0;
1228126253Struckman	size_t i, len, origidx;
122912243Sphk
1230126253Struckman	origidx = req->oldidx;
1231192144Skib	req->oldidx += l;
1232192144Skib	if (req->oldptr == NULL)
1233126253Struckman		return (0);
1234148864Scsjp	/*
1235148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1236148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1237148864Scsjp	 * write operations to the user page can sleep.
1238148864Scsjp	 */
1239148864Scsjp	if (req->lock != REQ_WIRED)
1240111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1241111883Sjhb		    "sysctl_old_user()");
1242126253Struckman	i = l;
1243127052Struckman	len = req->validlen;
1244126253Struckman	if (len <= origidx)
1245126253Struckman		i = 0;
1246126253Struckman	else {
1247126253Struckman		if (i > len - origidx)
1248126253Struckman			i = len - origidx;
1249126253Struckman		error = copyout(p, (char *)req->oldptr + origidx, i);
125012260Sphk	}
125112243Sphk	if (error)
125212243Sphk		return (error);
1253126253Struckman	if (i < l)
125412243Sphk		return (ENOMEM);
125512260Sphk	return (0);
125612243Sphk}
125712243Sphk
125812260Sphkstatic int
125938517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
126012243Sphk{
126112285Sphk	int error;
126212260Sphk
126312260Sphk	if (!req->newptr)
1264139483Spjd		return (0);
126512260Sphk	if (req->newlen - req->newidx < l)
126612243Sphk		return (EINVAL);
1267148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1268148873Scsjp	    "sysctl_new_user()");
126917971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
127012243Sphk	req->newidx += l;
127112243Sphk	return (error);
127212243Sphk}
127312243Sphk
1274100487Struckman/*
1275100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1276100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1277100487Struckman */
1278126253Struckmanint
1279100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1280100487Struckman{
1281126253Struckman	int ret;
1282192160Sdes	size_t wiredlen;
1283126253Struckman
1284126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1285126253Struckman	ret = 0;
1286120781Sbms	if (req->lock == REQ_LOCKED && req->oldptr &&
1287120781Sbms	    req->oldfunc == sysctl_old_user) {
1288127050Struckman		if (wiredlen != 0) {
1289127050Struckman			ret = vslock(req->oldptr, wiredlen);
1290130327Sgreen			if (ret != 0) {
1291130327Sgreen				if (ret != ENOMEM)
1292130327Sgreen					return (ret);
1293130327Sgreen				wiredlen = 0;
1294130327Sgreen			}
1295126253Struckman		}
1296127050Struckman		req->lock = REQ_WIRED;
1297127052Struckman		req->validlen = wiredlen;
1298100487Struckman	}
1299127050Struckman	return (0);
1300100487Struckman}
1301100487Struckman
13021541Srgrimesint
130353977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
130453977Sgreen    int *nindx, struct sysctl_req *req)
130512131Sphk{
1306216059Smdf	struct sysctl_oid_list *lsp;
130744078Sdfr	struct sysctl_oid *oid;
130853977Sgreen	int indx;
130912131Sphk
1310188232Sjhb	SYSCTL_ASSERT_LOCKED();
1311216059Smdf	lsp = &sysctl__children;
131212131Sphk	indx = 0;
1313216059Smdf	while (indx < CTL_MAXNAME) {
1314216059Smdf		SLIST_FOREACH(oid, lsp, oid_link) {
1315216059Smdf			if (oid->oid_number == name[indx])
1316216059Smdf				break;
1317216059Smdf		}
1318216059Smdf		if (oid == NULL)
1319216059Smdf			return (ENOENT);
1320216059Smdf
1321216059Smdf		indx++;
1322216059Smdf		if (oid->oid_kind & CTLFLAG_NOLOCK)
1323216059Smdf			req->lock = REQ_UNLOCKED;
1324216059Smdf		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1325216059Smdf			if (oid->oid_handler != NULL || indx == namelen) {
132653977Sgreen				*noid = oid;
132753977Sgreen				if (nindx != NULL)
132853977Sgreen					*nindx = indx;
132953977Sgreen				return (0);
133012131Sphk			}
1331216059Smdf			lsp = SYSCTL_CHILDREN(oid);
1332216059Smdf		} else if (indx == namelen) {
1333216059Smdf			*noid = oid;
1334216059Smdf			if (nindx != NULL)
1335216059Smdf				*nindx = indx;
1336216059Smdf			return (0);
133712131Sphk		} else {
1338216059Smdf			return (ENOTDIR);
133912131Sphk		}
134012131Sphk	}
134153977Sgreen	return (ENOENT);
134253977Sgreen}
134353977Sgreen
134453977Sgreen/*
134553977Sgreen * Traverse our tree, and find the right node, execute whatever it points
134653977Sgreen * to, and return the resulting error code.
134753977Sgreen */
134853977Sgreen
1349104094Sphkstatic int
135062573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
135153977Sgreen{
135253977Sgreen	struct sysctl_oid *oid;
1353109246Sdillon	int error, indx, lvl;
135453977Sgreen
1355188232Sjhb	SYSCTL_ASSERT_LOCKED();
1356186564Sed
135753977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
135853977Sgreen	if (error)
135953977Sgreen		return (error);
136053977Sgreen
136153977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
136253977Sgreen		/*
136353977Sgreen		 * You can't call a sysctl when it's a node, but has
136453977Sgreen		 * no handler.  Inform the user that it's a node.
136553977Sgreen		 * The indx may or may not be the same as namelen.
136653977Sgreen		 */
136753977Sgreen		if (oid->oid_handler == NULL)
136853977Sgreen			return (EISDIR);
136953977Sgreen	}
137053977Sgreen
137183968Srwatson	/* Is this sysctl writable? */
137283968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
137312131Sphk		return (EPERM);
137412131Sphk
137592953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
137692953Srwatson
137783968Srwatson	/* Is this sysctl sensitive to securelevels? */
137883968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1379109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1380109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
138192953Srwatson		if (error)
138292953Srwatson			return (error);
138383968Srwatson	}
138412910Sphk
138583968Srwatson	/* Is this sysctl writable by only privileged users? */
138683968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1387196176Sbz		int priv;
1388196176Sbz
138992953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1390196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1391196176Sbz#ifdef VIMAGE
1392196176Sbz		else if ((oid->oid_kind & CTLFLAG_VNET) &&
1393196176Sbz		     prison_owns_vnet(req->td->td_ucred))
1394196176Sbz			priv = PRIV_SYSCTL_WRITEJAIL;
1395196176Sbz#endif
139692953Srwatson		else
1397196176Sbz			priv = PRIV_SYSCTL_WRITE;
1398196176Sbz		error = priv_check(req->td, priv);
139992953Srwatson		if (error)
140092953Srwatson			return (error);
140183968Srwatson	}
140283968Srwatson
140344078Sdfr	if (!oid->oid_handler)
1404139483Spjd		return (EINVAL);
140512131Sphk
1406126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1407132776Skan		arg1 = (int *)arg1 + indx;
1408126121Spjd		arg2 -= indx;
1409126121Spjd	} else {
1410126121Spjd		arg1 = oid->oid_arg1;
1411126121Spjd		arg2 = oid->oid_arg2;
1412126121Spjd	}
1413126121Spjd#ifdef MAC
1414172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1415126121Spjd	    req);
1416126121Spjd	if (error != 0)
1417126121Spjd		return (error);
1418126121Spjd#endif
1419187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1420187656Sjhb		mtx_lock(&Giant);
1421126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1422187656Sjhb	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1423187656Sjhb		mtx_unlock(&Giant);
1424126121Spjd
142553977Sgreen	return (error);
142612131Sphk}
142712131Sphk
142812221Sbde#ifndef _SYS_SYSPROTO_H_
142912171Sphkstruct sysctl_args {
143012171Sphk	int	*name;
143112171Sphk	u_int	namelen;
143212171Sphk	void	*old;
143312171Sphk	size_t	*oldlenp;
143412171Sphk	void	*new;
143512171Sphk	size_t	newlen;
143612171Sphk};
143712221Sbde#endif
143812131Sphkint
143983366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap)
14401541Srgrimes{
1441188232Sjhb	int error, i, name[CTL_MAXNAME];
144238517Sdfr	size_t j;
14431541Srgrimes
14441541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
14451541Srgrimes		return (EINVAL);
144611863Sphk
14473308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
14483308Sphk 	if (error)
14491541Srgrimes		return (error);
14501541Srgrimes
145183366Sjulian	error = userland_sysctl(td, name, uap->namelen,
145212171Sphk		uap->old, uap->oldlenp, 0,
1453136404Speter		uap->new, uap->newlen, &j, 0);
145412260Sphk	if (error && error != ENOMEM)
1455186564Sed		return (error);
1456186664Sed	if (uap->oldlenp) {
1457188232Sjhb		i = copyout(&j, uap->oldlenp, sizeof(j));
1458186664Sed		if (i)
1459186664Sed			return (i);
1460186664Sed	}
146112260Sphk	return (error);
146212171Sphk}
146312171Sphk
146412171Sphk/*
146512171Sphk * This is used from various compatibility syscalls too.  That's why name
146612171Sphk * must be in kernel space.
146712171Sphk */
146812171Sphkint
146983366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1470136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1471136404Speter    int flags)
147212171Sphk{
1473192125Sjhb	int error = 0, memlocked;
1474127052Struckman	struct sysctl_req req;
147512171Sphk
147612243Sphk	bzero(&req, sizeof req);
147712243Sphk
147886183Srwatson	req.td = td;
1479136404Speter	req.flags = flags;
148012285Sphk
148112171Sphk	if (oldlenp) {
148212171Sphk		if (inkernel) {
148312243Sphk			req.oldlen = *oldlenp;
148412171Sphk		} else {
148512260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
148612171Sphk			if (error)
148712171Sphk				return (error);
148812171Sphk		}
148912171Sphk	}
1490127052Struckman	req.validlen = req.oldlen;
149112171Sphk
149212243Sphk	if (old) {
149352644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
149412243Sphk			return (EFAULT);
149512243Sphk		req.oldptr= old;
149612243Sphk	}
149712131Sphk
149877646Sdd	if (new != NULL) {
1499172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
150012243Sphk			return (EFAULT);
150112243Sphk		req.newlen = newlen;
150212243Sphk		req.newptr = new;
150311863Sphk	}
150412131Sphk
150512243Sphk	req.oldfunc = sysctl_old_user;
150612243Sphk	req.newfunc = sysctl_new_user;
1507120781Sbms	req.lock = REQ_LOCKED;
150811863Sphk
1509189707Sjhb#ifdef KTRACE
1510189707Sjhb	if (KTRPOINT(curthread, KTR_SYSCTL))
1511189707Sjhb		ktrsysctl(name, namelen);
1512189707Sjhb#endif
1513192125Sjhb
1514192125Sjhb	if (req.oldlen > PAGE_SIZE) {
1515192125Sjhb		memlocked = 1;
1516192125Sjhb		sx_xlock(&sysctlmemlock);
1517192125Sjhb	} else
1518192125Sjhb		memlocked = 0;
1519194252Sjamie	CURVNET_SET(TD_TO_VNET(td));
152012429Sphk
1521185983Skib	for (;;) {
1522127052Struckman		req.oldidx = 0;
1523127052Struckman		req.newidx = 0;
1524192125Sjhb		SYSCTL_SLOCK();
1525127052Struckman		error = sysctl_root(0, name, namelen, &req);
1526192125Sjhb		SYSCTL_SUNLOCK();
1527185983Skib		if (error != EAGAIN)
1528185983Skib			break;
1529185983Skib		uio_yield();
1530185983Skib	}
153112243Sphk
1532186564Sed	CURVNET_RESTORE();
1533186564Sed
1534127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1535127052Struckman		vsunlock(req.oldptr, req.validlen);
1536192125Sjhb	if (memlocked)
1537192125Sjhb		sx_xunlock(&sysctlmemlock);
153812429Sphk
153912260Sphk	if (error && error != ENOMEM)
154012260Sphk		return (error);
154112260Sphk
154212260Sphk	if (retval) {
1543127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1544127052Struckman			*retval = req.validlen;
154512260Sphk		else
154612260Sphk			*retval = req.oldidx;
154711863Sphk	}
154812260Sphk	return (error);
15491541Srgrimes}
1550212750Smdf
1551212750Smdf/*
1552212750Smdf * Drain into a sysctl struct.  The user buffer must be wired.
1553212750Smdf */
1554212750Smdfstatic int
1555212750Smdfsbuf_sysctl_drain(void *arg, const char *data, int len)
1556212750Smdf{
1557212750Smdf	struct sysctl_req *req = arg;
1558212750Smdf	int error;
1559212750Smdf
1560212750Smdf	error = SYSCTL_OUT(req, data, len);
1561212750Smdf	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1562212750Smdf	return (error == 0 ? len : -error);
1563212750Smdf}
1564212750Smdf
1565212750Smdfstruct sbuf *
1566212750Smdfsbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1567212750Smdf    struct sysctl_req *req)
1568212750Smdf{
1569212750Smdf
1570212750Smdf	/* Wire the user buffer, so we can write without blocking. */
1571212750Smdf	sysctl_wire_old_buffer(req, 0);
1572212750Smdf
1573212750Smdf	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1574212750Smdf	sbuf_set_drain(s, sbuf_sysctl_drain, req);
1575212750Smdf	return (s);
1576212750Smdf}
1577