kern_sysctl.c revision 195699
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 195699 2009-07-14 22:48:30Z rwatson $");
40
41#include "opt_compat.h"
42#include "opt_ktrace.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/malloc.h>
49#include <sys/priv.h>
50#include <sys/proc.h>
51#include <sys/jail.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/sx.h>
55#include <sys/sysproto.h>
56#include <sys/uio.h>
57#include <sys/vimage.h>
58#ifdef KTRACE
59#include <sys/ktrace.h>
60#endif
61
62#include <net/vnet.h>
63
64#include <security/mac/mac_framework.h>
65
66#include <vm/vm.h>
67#include <vm/vm_extern.h>
68
69static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
70static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
71static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
72
73/*
74 * The sysctllock protects the MIB tree.  It also protects sysctl
75 * contexts used with dynamic sysctls.  The sysctl_register_oid() and
76 * sysctl_unregister_oid() routines require the sysctllock to already
77 * be held, so the sysctl_lock() and sysctl_unlock() routines are
78 * provided for the few places in the kernel which need to use that
79 * API rather than using the dynamic API.  Use of the dynamic API is
80 * strongly encouraged for most code.
81 *
82 * The sysctlmemlock is used to limit the amount of user memory wired for
83 * sysctl requests.  This is implemented by serializing any userland
84 * sysctl requests larger than a single page via an exclusive lock.
85 */
86static struct sx sysctllock;
87static struct sx sysctlmemlock;
88
89#define	SYSCTL_SLOCK()		sx_slock(&sysctllock)
90#define	SYSCTL_SUNLOCK()	sx_sunlock(&sysctllock)
91#define	SYSCTL_XLOCK()		sx_xlock(&sysctllock)
92#define	SYSCTL_XUNLOCK()	sx_xunlock(&sysctllock)
93#define	SYSCTL_ASSERT_XLOCKED()	sx_assert(&sysctllock, SA_XLOCKED)
94#define	SYSCTL_ASSERT_LOCKED()	sx_assert(&sysctllock, SA_LOCKED)
95#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
96
97static int sysctl_root(SYSCTL_HANDLER_ARGS);
98
99struct sysctl_oid_list sysctl__children; /* root list */
100
101static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
102		    int recurse);
103
104static struct sysctl_oid *
105sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
106{
107	struct sysctl_oid *oidp;
108
109	SYSCTL_ASSERT_LOCKED();
110	SLIST_FOREACH(oidp, list, oid_link) {
111		if (strcmp(oidp->oid_name, name) == 0) {
112			return (oidp);
113		}
114	}
115	return (NULL);
116}
117
118/*
119 * Initialization of the MIB tree.
120 *
121 * Order by number in each list.
122 */
123void
124sysctl_lock(void)
125{
126
127	SYSCTL_XLOCK();
128}
129
130void
131sysctl_unlock(void)
132{
133
134	SYSCTL_XUNLOCK();
135}
136
137void
138sysctl_register_oid(struct sysctl_oid *oidp)
139{
140	struct sysctl_oid_list *parent = oidp->oid_parent;
141	struct sysctl_oid *p;
142	struct sysctl_oid *q;
143
144	/*
145	 * First check if another oid with the same name already
146	 * exists in the parent's list.
147	 */
148	SYSCTL_ASSERT_XLOCKED();
149	p = sysctl_find_oidname(oidp->oid_name, parent);
150	if (p != NULL) {
151		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
152			p->oid_refcnt++;
153			return;
154		} else {
155			printf("can't re-use a leaf (%s)!\n", p->oid_name);
156			return;
157		}
158	}
159	/*
160	 * If this oid has a number OID_AUTO, give it a number which
161	 * is greater than any current oid.
162	 * NOTE: DO NOT change the starting value here, change it in
163	 * <sys/sysctl.h>, and make sure it is at least 256 to
164	 * accomodate e.g. net.inet.raw as a static sysctl node.
165	 */
166	if (oidp->oid_number == OID_AUTO) {
167		static int newoid = CTL_AUTO_START;
168
169		oidp->oid_number = newoid++;
170		if (newoid == 0x7fffffff)
171			panic("out of oids");
172	}
173#if 0
174	else if (oidp->oid_number >= CTL_AUTO_START) {
175		/* do not panic; this happens when unregistering sysctl sets */
176		printf("static sysctl oid too high: %d", oidp->oid_number);
177	}
178#endif
179
180	/*
181	 * Insert the oid into the parent's list in order.
182	 */
183	q = NULL;
184	SLIST_FOREACH(p, parent, oid_link) {
185		if (oidp->oid_number < p->oid_number)
186			break;
187		q = p;
188	}
189	if (q)
190		SLIST_INSERT_AFTER(q, oidp, oid_link);
191	else
192		SLIST_INSERT_HEAD(parent, oidp, oid_link);
193}
194
195void
196sysctl_unregister_oid(struct sysctl_oid *oidp)
197{
198	struct sysctl_oid *p;
199	int error;
200
201	SYSCTL_ASSERT_XLOCKED();
202	error = ENOENT;
203	if (oidp->oid_number == OID_AUTO) {
204		error = EINVAL;
205	} else {
206		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
207			if (p == oidp) {
208				SLIST_REMOVE(oidp->oid_parent, oidp,
209				    sysctl_oid, oid_link);
210				error = 0;
211				break;
212			}
213		}
214	}
215
216	/*
217	 * This can happen when a module fails to register and is
218	 * being unloaded afterwards.  It should not be a panic()
219	 * for normal use.
220	 */
221	if (error)
222		printf("%s: failed to unregister sysctl\n", __func__);
223}
224
225/* Initialize a new context to keep track of dynamically added sysctls. */
226int
227sysctl_ctx_init(struct sysctl_ctx_list *c)
228{
229
230	if (c == NULL) {
231		return (EINVAL);
232	}
233
234	/*
235	 * No locking here, the caller is responsible for not adding
236	 * new nodes to a context until after this function has
237	 * returned.
238	 */
239	TAILQ_INIT(c);
240	return (0);
241}
242
243/* Free the context, and destroy all dynamic oids registered in this context */
244int
245sysctl_ctx_free(struct sysctl_ctx_list *clist)
246{
247	struct sysctl_ctx_entry *e, *e1;
248	int error;
249
250	error = 0;
251	/*
252	 * First perform a "dry run" to check if it's ok to remove oids.
253	 * XXX FIXME
254	 * XXX This algorithm is a hack. But I don't know any
255	 * XXX better solution for now...
256	 */
257	SYSCTL_XLOCK();
258	TAILQ_FOREACH(e, clist, link) {
259		error = sysctl_remove_oid_locked(e->entry, 0, 0);
260		if (error)
261			break;
262	}
263	/*
264	 * Restore deregistered entries, either from the end,
265	 * or from the place where error occured.
266	 * e contains the entry that was not unregistered
267	 */
268	if (error)
269		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
270	else
271		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
272	while (e1 != NULL) {
273		sysctl_register_oid(e1->entry);
274		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
275	}
276	if (error) {
277		SYSCTL_XUNLOCK();
278		return(EBUSY);
279	}
280	/* Now really delete the entries */
281	e = TAILQ_FIRST(clist);
282	while (e != NULL) {
283		e1 = TAILQ_NEXT(e, link);
284		error = sysctl_remove_oid_locked(e->entry, 1, 0);
285		if (error)
286			panic("sysctl_remove_oid: corrupt tree, entry: %s",
287			    e->entry->oid_name);
288		free(e, M_SYSCTLOID);
289		e = e1;
290	}
291	SYSCTL_XUNLOCK();
292	return (error);
293}
294
295/* Add an entry to the context */
296struct sysctl_ctx_entry *
297sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
298{
299	struct sysctl_ctx_entry *e;
300
301	SYSCTL_ASSERT_XLOCKED();
302	if (clist == NULL || oidp == NULL)
303		return(NULL);
304	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
305	e->entry = oidp;
306	TAILQ_INSERT_HEAD(clist, e, link);
307	return (e);
308}
309
310/* Find an entry in the context */
311struct sysctl_ctx_entry *
312sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
313{
314	struct sysctl_ctx_entry *e;
315
316	SYSCTL_ASSERT_LOCKED();
317	if (clist == NULL || oidp == NULL)
318		return(NULL);
319	TAILQ_FOREACH(e, clist, link) {
320		if(e->entry == oidp)
321			return(e);
322	}
323	return (e);
324}
325
326/*
327 * Delete an entry from the context.
328 * NOTE: this function doesn't free oidp! You have to remove it
329 * with sysctl_remove_oid().
330 */
331int
332sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
333{
334	struct sysctl_ctx_entry *e;
335
336	if (clist == NULL || oidp == NULL)
337		return (EINVAL);
338	SYSCTL_XLOCK();
339	e = sysctl_ctx_entry_find(clist, oidp);
340	if (e != NULL) {
341		TAILQ_REMOVE(clist, e, link);
342		SYSCTL_XUNLOCK();
343		free(e, M_SYSCTLOID);
344		return (0);
345	} else {
346		SYSCTL_XUNLOCK();
347		return (ENOENT);
348	}
349}
350
351/*
352 * Remove dynamically created sysctl trees.
353 * oidp - top of the tree to be removed
354 * del - if 0 - just deregister, otherwise free up entries as well
355 * recurse - if != 0 traverse the subtree to be deleted
356 */
357int
358sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
359{
360	int error;
361
362	SYSCTL_XLOCK();
363	error = sysctl_remove_oid_locked(oidp, del, recurse);
364	SYSCTL_XUNLOCK();
365	return (error);
366}
367
368static int
369sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
370{
371	struct sysctl_oid *p;
372	int error;
373
374	SYSCTL_ASSERT_XLOCKED();
375	if (oidp == NULL)
376		return(EINVAL);
377	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
378		printf("can't remove non-dynamic nodes!\n");
379		return (EINVAL);
380	}
381	/*
382	 * WARNING: normal method to do this should be through
383	 * sysctl_ctx_free(). Use recursing as the last resort
384	 * method to purge your sysctl tree of leftovers...
385	 * However, if some other code still references these nodes,
386	 * it will panic.
387	 */
388	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
389		if (oidp->oid_refcnt == 1) {
390			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
391				if (!recurse)
392					return (ENOTEMPTY);
393				error = sysctl_remove_oid_locked(p, del,
394				    recurse);
395				if (error)
396					return (error);
397			}
398			if (del)
399				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
400		}
401	}
402	if (oidp->oid_refcnt > 1 ) {
403		oidp->oid_refcnt--;
404	} else {
405		if (oidp->oid_refcnt == 0) {
406			printf("Warning: bad oid_refcnt=%u (%s)!\n",
407				oidp->oid_refcnt, oidp->oid_name);
408			return (EINVAL);
409		}
410		sysctl_unregister_oid(oidp);
411		if (del) {
412			if (oidp->oid_descr)
413				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
414			free((void *)(uintptr_t)(const void *)oidp->oid_name,
415			     M_SYSCTLOID);
416			free(oidp, M_SYSCTLOID);
417		}
418	}
419	return (0);
420}
421
422/*
423 * Create new sysctls at run time.
424 * clist may point to a valid context initialized with sysctl_ctx_init().
425 */
426struct sysctl_oid *
427sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
428	int number, const char *name, int kind, void *arg1, int arg2,
429	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
430{
431	struct sysctl_oid *oidp;
432	ssize_t len;
433	char *newname;
434
435	/* You have to hook up somewhere.. */
436	if (parent == NULL)
437		return(NULL);
438	/* Check if the node already exists, otherwise create it */
439	SYSCTL_XLOCK();
440	oidp = sysctl_find_oidname(name, parent);
441	if (oidp != NULL) {
442		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
443			oidp->oid_refcnt++;
444			/* Update the context */
445			if (clist != NULL)
446				sysctl_ctx_entry_add(clist, oidp);
447			SYSCTL_XUNLOCK();
448			return (oidp);
449		} else {
450			SYSCTL_XUNLOCK();
451			printf("can't re-use a leaf (%s)!\n", name);
452			return (NULL);
453		}
454	}
455	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
456	oidp->oid_parent = parent;
457	SLIST_NEXT(oidp, oid_link) = NULL;
458	oidp->oid_number = number;
459	oidp->oid_refcnt = 1;
460	len = strlen(name);
461	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
462	bcopy(name, newname, len + 1);
463	newname[len] = '\0';
464	oidp->oid_name = newname;
465	oidp->oid_handler = handler;
466	oidp->oid_kind = CTLFLAG_DYN | kind;
467	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
468		/* Allocate space for children */
469		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
470		    M_SYSCTLOID, M_WAITOK));
471		SLIST_INIT(SYSCTL_CHILDREN(oidp));
472	} else {
473		oidp->oid_arg1 = arg1;
474		oidp->oid_arg2 = arg2;
475	}
476	oidp->oid_fmt = fmt;
477	if (descr) {
478		int len = strlen(descr) + 1;
479		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
480		if (oidp->oid_descr)
481			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
482	}
483	/* Update the context, if used */
484	if (clist != NULL)
485		sysctl_ctx_entry_add(clist, oidp);
486	/* Register this oid */
487	sysctl_register_oid(oidp);
488	SYSCTL_XUNLOCK();
489	return (oidp);
490}
491
492/*
493 * Rename an existing oid.
494 */
495void
496sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
497{
498	ssize_t len;
499	char *newname;
500	void *oldname;
501
502	len = strlen(name);
503	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
504	bcopy(name, newname, len + 1);
505	newname[len] = '\0';
506	SYSCTL_XLOCK();
507	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
508	oidp->oid_name = newname;
509	SYSCTL_XUNLOCK();
510	free(oldname, M_SYSCTLOID);
511}
512
513/*
514 * Reparent an existing oid.
515 */
516int
517sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
518{
519	struct sysctl_oid *oidp;
520
521	SYSCTL_XLOCK();
522	if (oid->oid_parent == parent) {
523		SYSCTL_XUNLOCK();
524		return (0);
525	}
526	oidp = sysctl_find_oidname(oid->oid_name, parent);
527	if (oidp != NULL) {
528		SYSCTL_XUNLOCK();
529		return (EEXIST);
530	}
531	sysctl_unregister_oid(oid);
532	oid->oid_parent = parent;
533	oid->oid_number = OID_AUTO;
534	sysctl_register_oid(oid);
535	SYSCTL_XUNLOCK();
536	return (0);
537}
538
539/*
540 * Register the kernel's oids on startup.
541 */
542SET_DECLARE(sysctl_set, struct sysctl_oid);
543
544static void
545sysctl_register_all(void *arg)
546{
547	struct sysctl_oid **oidp;
548
549	sx_init(&sysctlmemlock, "sysctl mem");
550	SYSCTL_INIT();
551	SYSCTL_XLOCK();
552	SET_FOREACH(oidp, sysctl_set)
553		sysctl_register_oid(*oidp);
554	SYSCTL_XUNLOCK();
555}
556SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
557
558/*
559 * "Staff-functions"
560 *
561 * These functions implement a presently undocumented interface
562 * used by the sysctl program to walk the tree, and get the type
563 * so it can print the value.
564 * This interface is under work and consideration, and should probably
565 * be killed with a big axe by the first person who can find the time.
566 * (be aware though, that the proper interface isn't as obvious as it
567 * may seem, there are various conflicting requirements.
568 *
569 * {0,0}	printf the entire MIB-tree.
570 * {0,1,...}	return the name of the "..." OID.
571 * {0,2,...}	return the next OID.
572 * {0,3}	return the OID of the name in "new"
573 * {0,4,...}	return the kind & format info for the "..." OID.
574 * {0,5,...}	return the description the "..." OID.
575 */
576
577#ifdef SYSCTL_DEBUG
578static void
579sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
580{
581	int k;
582	struct sysctl_oid *oidp;
583
584	SYSCTL_ASSERT_LOCKED();
585	SLIST_FOREACH(oidp, l, oid_link) {
586
587		for (k=0; k<i; k++)
588			printf(" ");
589
590		printf("%d %s ", oidp->oid_number, oidp->oid_name);
591
592		printf("%c%c",
593			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
594			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
595
596		if (oidp->oid_handler)
597			printf(" *Handler");
598
599		switch (oidp->oid_kind & CTLTYPE) {
600			case CTLTYPE_NODE:
601				printf(" Node\n");
602				if (!oidp->oid_handler) {
603					sysctl_sysctl_debug_dump_node(
604						oidp->oid_arg1, i+2);
605				}
606				break;
607			case CTLTYPE_INT:    printf(" Int\n"); break;
608			case CTLTYPE_STRING: printf(" String\n"); break;
609			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
610			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
611			default:	     printf("\n");
612		}
613
614	}
615}
616
617static int
618sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
619{
620	int error;
621
622	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
623	if (error)
624		return (error);
625	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
626	return (ENOENT);
627}
628
629SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
630	0, 0, sysctl_sysctl_debug, "-", "");
631#endif
632
633static int
634sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
635{
636	int *name = (int *) arg1;
637	u_int namelen = arg2;
638	int error = 0;
639	struct sysctl_oid *oid;
640	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
641	char buf[10];
642
643	SYSCTL_ASSERT_LOCKED();
644	while (namelen) {
645		if (!lsp) {
646			snprintf(buf,sizeof(buf),"%d",*name);
647			if (req->oldidx)
648				error = SYSCTL_OUT(req, ".", 1);
649			if (!error)
650				error = SYSCTL_OUT(req, buf, strlen(buf));
651			if (error)
652				return (error);
653			namelen--;
654			name++;
655			continue;
656		}
657		lsp2 = 0;
658		SLIST_FOREACH(oid, lsp, oid_link) {
659			if (oid->oid_number != *name)
660				continue;
661
662			if (req->oldidx)
663				error = SYSCTL_OUT(req, ".", 1);
664			if (!error)
665				error = SYSCTL_OUT(req, oid->oid_name,
666					strlen(oid->oid_name));
667			if (error)
668				return (error);
669
670			namelen--;
671			name++;
672
673			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
674				break;
675
676			if (oid->oid_handler)
677				break;
678
679			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
680			break;
681		}
682		lsp = lsp2;
683	}
684	return (SYSCTL_OUT(req, "", 1));
685}
686
687static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
688
689static int
690sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
691	int *next, int *len, int level, struct sysctl_oid **oidpp)
692{
693	struct sysctl_oid *oidp;
694
695	SYSCTL_ASSERT_LOCKED();
696	*len = level;
697	SLIST_FOREACH(oidp, lsp, oid_link) {
698		*next = oidp->oid_number;
699		*oidpp = oidp;
700
701		if (oidp->oid_kind & CTLFLAG_SKIP)
702			continue;
703
704		if (!namelen) {
705			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
706				return (0);
707			if (oidp->oid_handler)
708				/* We really should call the handler here...*/
709				return (0);
710			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
711			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
712				len, level+1, oidpp))
713				return (0);
714			goto emptynode;
715		}
716
717		if (oidp->oid_number < *name)
718			continue;
719
720		if (oidp->oid_number > *name) {
721			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
722				return (0);
723			if (oidp->oid_handler)
724				return (0);
725			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
726			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
727				next+1, len, level+1, oidpp))
728				return (0);
729			goto next;
730		}
731		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
732			continue;
733
734		if (oidp->oid_handler)
735			continue;
736
737		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
738		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
739			len, level+1, oidpp))
740			return (0);
741	next:
742		namelen = 1;
743	emptynode:
744		*len = level;
745	}
746	return (1);
747}
748
749static int
750sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
751{
752	int *name = (int *) arg1;
753	u_int namelen = arg2;
754	int i, j, error;
755	struct sysctl_oid *oid;
756	struct sysctl_oid_list *lsp = &sysctl__children;
757	int newoid[CTL_MAXNAME];
758
759	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
760	if (i)
761		return (ENOENT);
762	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
763	return (error);
764}
765
766static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
767
768static int
769name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
770{
771	int i;
772	struct sysctl_oid *oidp;
773	struct sysctl_oid_list *lsp = &sysctl__children;
774	char *p;
775
776	SYSCTL_ASSERT_LOCKED();
777
778	if (!*name)
779		return (ENOENT);
780
781	p = name + strlen(name) - 1 ;
782	if (*p == '.')
783		*p = '\0';
784
785	*len = 0;
786
787	for (p = name; *p && *p != '.'; p++)
788		;
789	i = *p;
790	if (i == '.')
791		*p = '\0';
792
793	oidp = SLIST_FIRST(lsp);
794
795	while (oidp && *len < CTL_MAXNAME) {
796		if (strcmp(name, oidp->oid_name)) {
797			oidp = SLIST_NEXT(oidp, oid_link);
798			continue;
799		}
800		*oid++ = oidp->oid_number;
801		(*len)++;
802
803		if (!i) {
804			if (oidpp)
805				*oidpp = oidp;
806			return (0);
807		}
808
809		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
810			break;
811
812		if (oidp->oid_handler)
813			break;
814
815		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
816		oidp = SLIST_FIRST(lsp);
817		name = p+1;
818		for (p = name; *p && *p != '.'; p++)
819				;
820		i = *p;
821		if (i == '.')
822			*p = '\0';
823	}
824	return (ENOENT);
825}
826
827static int
828sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
829{
830	char *p;
831	int error, oid[CTL_MAXNAME], len;
832	struct sysctl_oid *op = 0;
833
834	SYSCTL_ASSERT_LOCKED();
835
836	if (!req->newlen)
837		return (ENOENT);
838	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
839		return (ENAMETOOLONG);
840
841	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
842
843	error = SYSCTL_IN(req, p, req->newlen);
844	if (error) {
845		free(p, M_SYSCTL);
846		return (error);
847	}
848
849	p [req->newlen] = '\0';
850
851	error = name2oid(p, oid, &len, &op);
852
853	free(p, M_SYSCTL);
854
855	if (error)
856		return (error);
857
858	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
859	return (error);
860}
861
862SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
863    0, 0, sysctl_sysctl_name2oid, "I", "");
864
865static int
866sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
867{
868	struct sysctl_oid *oid;
869	int error;
870
871	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
872	if (error)
873		return (error);
874
875	if (!oid->oid_fmt)
876		return (ENOENT);
877	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
878	if (error)
879		return (error);
880	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
881	return (error);
882}
883
884
885static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
886    sysctl_sysctl_oidfmt, "");
887
888static int
889sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
890{
891	struct sysctl_oid *oid;
892	int error;
893
894	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
895	if (error)
896		return (error);
897
898	if (!oid->oid_descr)
899		return (ENOENT);
900	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
901	return (error);
902}
903
904static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
905
906/*
907 * Default "handler" functions.
908 */
909
910/*
911 * Handle an int, signed or unsigned.
912 * Two cases:
913 *     a variable:  point arg1 at it.
914 *     a constant:  pass it in arg2.
915 */
916
917int
918sysctl_handle_int(SYSCTL_HANDLER_ARGS)
919{
920	int tmpout, error = 0;
921
922	/*
923	 * Attempt to get a coherent snapshot by making a copy of the data.
924	 */
925	if (arg1)
926		tmpout = *(int *)arg1;
927	else
928		tmpout = arg2;
929	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
930
931	if (error || !req->newptr)
932		return (error);
933
934	if (!arg1)
935		error = EPERM;
936	else
937		error = SYSCTL_IN(req, arg1, sizeof(int));
938	return (error);
939}
940
941/*
942 * Based on on sysctl_handle_int() convert milliseconds into ticks.
943 * Note: this is used by TCP.
944 */
945
946int
947sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
948{
949	int error, s, tt;
950
951#ifdef VIMAGE
952	if (arg1 != NULL)
953		arg1 = (void *)(TD_TO_VNET(req->td)->vnet_data_base +
954		    (uintptr_t)arg1);
955#endif
956	tt = *(int *)arg1;
957	s = (int)((int64_t)tt * 1000 / hz);
958
959	error = sysctl_handle_int(oidp, &s, 0, req);
960	if (error || !req->newptr)
961		return (error);
962
963	tt = (int)((int64_t)s * hz / 1000);
964	if (tt < 1)
965		return (EINVAL);
966
967	*(int *)arg1 = tt;
968	return (0);
969}
970
971
972/*
973 * Handle a long, signed or unsigned.  arg1 points to it.
974 */
975
976int
977sysctl_handle_long(SYSCTL_HANDLER_ARGS)
978{
979	int error = 0;
980	long tmplong;
981#ifdef SCTL_MASK32
982	int tmpint;
983#endif
984
985	/*
986	 * Attempt to get a coherent snapshot by making a copy of the data.
987	 */
988	if (!arg1)
989		return (EINVAL);
990	tmplong = *(long *)arg1;
991#ifdef SCTL_MASK32
992	if (req->flags & SCTL_MASK32) {
993		tmpint = tmplong;
994		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
995	} else
996#endif
997		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
998
999	if (error || !req->newptr)
1000		return (error);
1001
1002#ifdef SCTL_MASK32
1003	if (req->flags & SCTL_MASK32) {
1004		error = SYSCTL_IN(req, &tmpint, sizeof(int));
1005		*(long *)arg1 = (long)tmpint;
1006	} else
1007#endif
1008		error = SYSCTL_IN(req, arg1, sizeof(long));
1009	return (error);
1010}
1011
1012/*
1013 * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
1014 */
1015
1016int
1017sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1018{
1019	int error = 0;
1020	uint64_t tmpout;
1021
1022	/*
1023	 * Attempt to get a coherent snapshot by making a copy of the data.
1024	 */
1025	if (!arg1)
1026		return (EINVAL);
1027	tmpout = *(uint64_t *)arg1;
1028	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1029
1030	if (error || !req->newptr)
1031		return (error);
1032
1033	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1034	return (error);
1035}
1036
1037/*
1038 * Handle our generic '\0' terminated 'C' string.
1039 * Two cases:
1040 * 	a variable string:  point arg1 at it, arg2 is max length.
1041 * 	a constant string:  point arg1 at it, arg2 is zero.
1042 */
1043
1044int
1045sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1046{
1047	int error=0;
1048	char *tmparg;
1049	size_t outlen;
1050
1051	/*
1052	 * Attempt to get a coherent snapshot by copying to a
1053	 * temporary kernel buffer.
1054	 */
1055retry:
1056	outlen = strlen((char *)arg1)+1;
1057	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1058
1059	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1060		free(tmparg, M_SYSCTLTMP);
1061		goto retry;
1062	}
1063
1064	error = SYSCTL_OUT(req, tmparg, outlen);
1065	free(tmparg, M_SYSCTLTMP);
1066
1067	if (error || !req->newptr)
1068		return (error);
1069
1070	if ((req->newlen - req->newidx) >= arg2) {
1071		error = EINVAL;
1072	} else {
1073		arg2 = (req->newlen - req->newidx);
1074		error = SYSCTL_IN(req, arg1, arg2);
1075		((char *)arg1)[arg2] = '\0';
1076	}
1077
1078	return (error);
1079}
1080
1081/*
1082 * Handle any kind of opaque data.
1083 * arg1 points to it, arg2 is the size.
1084 */
1085
1086int
1087sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1088{
1089	int error, tries;
1090	u_int generation;
1091	struct sysctl_req req2;
1092
1093	/*
1094	 * Attempt to get a coherent snapshot, by using the thread
1095	 * pre-emption counter updated from within mi_switch() to
1096	 * determine if we were pre-empted during a bcopy() or
1097	 * copyout(). Make 3 attempts at doing this before giving up.
1098	 * If we encounter an error, stop immediately.
1099	 */
1100	tries = 0;
1101	req2 = *req;
1102retry:
1103	generation = curthread->td_generation;
1104	error = SYSCTL_OUT(req, arg1, arg2);
1105	if (error)
1106		return (error);
1107	tries++;
1108	if (generation != curthread->td_generation && tries < 3) {
1109		*req = req2;
1110		goto retry;
1111	}
1112
1113	error = SYSCTL_IN(req, arg1, arg2);
1114
1115	return (error);
1116}
1117
1118/*
1119 * Transfer functions to/from kernel space.
1120 * XXX: rather untested at this point
1121 */
1122static int
1123sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1124{
1125	size_t i = 0;
1126
1127	if (req->oldptr) {
1128		i = l;
1129		if (req->oldlen <= req->oldidx)
1130			i = 0;
1131		else
1132			if (i > req->oldlen - req->oldidx)
1133				i = req->oldlen - req->oldidx;
1134		if (i > 0)
1135			bcopy(p, (char *)req->oldptr + req->oldidx, i);
1136	}
1137	req->oldidx += l;
1138	if (req->oldptr && i != l)
1139		return (ENOMEM);
1140	return (0);
1141}
1142
1143static int
1144sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1145{
1146	if (!req->newptr)
1147		return (0);
1148	if (req->newlen - req->newidx < l)
1149		return (EINVAL);
1150	bcopy((char *)req->newptr + req->newidx, p, l);
1151	req->newidx += l;
1152	return (0);
1153}
1154
1155int
1156kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1157    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1158{
1159	int error = 0;
1160	struct sysctl_req req;
1161
1162	bzero(&req, sizeof req);
1163
1164	req.td = td;
1165	req.flags = flags;
1166
1167	if (oldlenp) {
1168		req.oldlen = *oldlenp;
1169	}
1170	req.validlen = req.oldlen;
1171
1172	if (old) {
1173		req.oldptr= old;
1174	}
1175
1176	if (new != NULL) {
1177		req.newlen = newlen;
1178		req.newptr = new;
1179	}
1180
1181	req.oldfunc = sysctl_old_kernel;
1182	req.newfunc = sysctl_new_kernel;
1183	req.lock = REQ_LOCKED;
1184
1185	SYSCTL_SLOCK();
1186	error = sysctl_root(0, name, namelen, &req);
1187	SYSCTL_SUNLOCK();
1188
1189	if (req.lock == REQ_WIRED && req.validlen > 0)
1190		vsunlock(req.oldptr, req.validlen);
1191
1192	if (error && error != ENOMEM)
1193		return (error);
1194
1195	if (retval) {
1196		if (req.oldptr && req.oldidx > req.validlen)
1197			*retval = req.validlen;
1198		else
1199			*retval = req.oldidx;
1200	}
1201	return (error);
1202}
1203
1204int
1205kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1206    void *new, size_t newlen, size_t *retval, int flags)
1207{
1208        int oid[CTL_MAXNAME];
1209        size_t oidlen, plen;
1210	int error;
1211
1212	oid[0] = 0;		/* sysctl internal magic */
1213	oid[1] = 3;		/* name2oid */
1214	oidlen = sizeof(oid);
1215
1216	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1217	    (void *)name, strlen(name), &plen, flags);
1218	if (error)
1219		return (error);
1220
1221	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1222	    new, newlen, retval, flags);
1223	return (error);
1224}
1225
1226/*
1227 * Transfer function to/from user space.
1228 */
1229static int
1230sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1231{
1232	int error = 0;
1233	size_t i, len, origidx;
1234
1235	origidx = req->oldidx;
1236	req->oldidx += l;
1237	if (req->oldptr == NULL)
1238		return (0);
1239	/*
1240	 * If we have not wired the user supplied buffer and we are currently
1241	 * holding locks, drop a witness warning, as it's possible that
1242	 * write operations to the user page can sleep.
1243	 */
1244	if (req->lock != REQ_WIRED)
1245		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1246		    "sysctl_old_user()");
1247	i = l;
1248	len = req->validlen;
1249	if (len <= origidx)
1250		i = 0;
1251	else {
1252		if (i > len - origidx)
1253			i = len - origidx;
1254		error = copyout(p, (char *)req->oldptr + origidx, i);
1255	}
1256	if (error)
1257		return (error);
1258	if (i < l)
1259		return (ENOMEM);
1260	return (0);
1261}
1262
1263static int
1264sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1265{
1266	int error;
1267
1268	if (!req->newptr)
1269		return (0);
1270	if (req->newlen - req->newidx < l)
1271		return (EINVAL);
1272	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1273	    "sysctl_new_user()");
1274	error = copyin((char *)req->newptr + req->newidx, p, l);
1275	req->newidx += l;
1276	return (error);
1277}
1278
1279/*
1280 * Wire the user space destination buffer.  If set to a value greater than
1281 * zero, the len parameter limits the maximum amount of wired memory.
1282 */
1283int
1284sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1285{
1286	int ret;
1287	size_t wiredlen;
1288
1289	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1290	ret = 0;
1291	if (req->lock == REQ_LOCKED && req->oldptr &&
1292	    req->oldfunc == sysctl_old_user) {
1293		if (wiredlen != 0) {
1294			ret = vslock(req->oldptr, wiredlen);
1295			if (ret != 0) {
1296				if (ret != ENOMEM)
1297					return (ret);
1298				wiredlen = 0;
1299			}
1300		}
1301		req->lock = REQ_WIRED;
1302		req->validlen = wiredlen;
1303	}
1304	return (0);
1305}
1306
1307int
1308sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1309    int *nindx, struct sysctl_req *req)
1310{
1311	struct sysctl_oid *oid;
1312	int indx;
1313
1314	SYSCTL_ASSERT_LOCKED();
1315	oid = SLIST_FIRST(&sysctl__children);
1316	indx = 0;
1317	while (oid && indx < CTL_MAXNAME) {
1318		if (oid->oid_number == name[indx]) {
1319			indx++;
1320			if (oid->oid_kind & CTLFLAG_NOLOCK)
1321				req->lock = REQ_UNLOCKED;
1322			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1323				if (oid->oid_handler != NULL ||
1324				    indx == namelen) {
1325					*noid = oid;
1326					if (nindx != NULL)
1327						*nindx = indx;
1328					return (0);
1329				}
1330				oid = SLIST_FIRST(
1331				    (struct sysctl_oid_list *)oid->oid_arg1);
1332			} else if (indx == namelen) {
1333				*noid = oid;
1334				if (nindx != NULL)
1335					*nindx = indx;
1336				return (0);
1337			} else {
1338				return (ENOTDIR);
1339			}
1340		} else {
1341			oid = SLIST_NEXT(oid, oid_link);
1342		}
1343	}
1344	return (ENOENT);
1345}
1346
1347/*
1348 * Traverse our tree, and find the right node, execute whatever it points
1349 * to, and return the resulting error code.
1350 */
1351
1352static int
1353sysctl_root(SYSCTL_HANDLER_ARGS)
1354{
1355	struct sysctl_oid *oid;
1356	int error, indx, lvl;
1357
1358	SYSCTL_ASSERT_LOCKED();
1359
1360	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1361	if (error)
1362		return (error);
1363
1364	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1365		/*
1366		 * You can't call a sysctl when it's a node, but has
1367		 * no handler.  Inform the user that it's a node.
1368		 * The indx may or may not be the same as namelen.
1369		 */
1370		if (oid->oid_handler == NULL)
1371			return (EISDIR);
1372	}
1373
1374	/* Is this sysctl writable? */
1375	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1376		return (EPERM);
1377
1378	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1379
1380	/* Is this sysctl sensitive to securelevels? */
1381	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1382		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1383		error = securelevel_gt(req->td->td_ucred, lvl);
1384		if (error)
1385			return (error);
1386	}
1387
1388	/* Is this sysctl writable by only privileged users? */
1389	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1390		if (oid->oid_kind & CTLFLAG_PRISON)
1391			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1392		else
1393			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1394		if (error)
1395			return (error);
1396	}
1397
1398	if (!oid->oid_handler)
1399		return (EINVAL);
1400
1401	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1402		arg1 = (int *)arg1 + indx;
1403		arg2 -= indx;
1404	} else {
1405		arg1 = oid->oid_arg1;
1406		arg2 = oid->oid_arg2;
1407	}
1408#ifdef MAC
1409	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1410	    req);
1411	if (error != 0)
1412		return (error);
1413#endif
1414	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1415		mtx_lock(&Giant);
1416	error = oid->oid_handler(oid, arg1, arg2, req);
1417	if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1418		mtx_unlock(&Giant);
1419
1420	return (error);
1421}
1422
1423#ifndef _SYS_SYSPROTO_H_
1424struct sysctl_args {
1425	int	*name;
1426	u_int	namelen;
1427	void	*old;
1428	size_t	*oldlenp;
1429	void	*new;
1430	size_t	newlen;
1431};
1432#endif
1433int
1434__sysctl(struct thread *td, struct sysctl_args *uap)
1435{
1436	int error, i, name[CTL_MAXNAME];
1437	size_t j;
1438
1439	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1440		return (EINVAL);
1441
1442 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1443 	if (error)
1444		return (error);
1445
1446	error = userland_sysctl(td, name, uap->namelen,
1447		uap->old, uap->oldlenp, 0,
1448		uap->new, uap->newlen, &j, 0);
1449	if (error && error != ENOMEM)
1450		return (error);
1451	if (uap->oldlenp) {
1452		i = copyout(&j, uap->oldlenp, sizeof(j));
1453		if (i)
1454			return (i);
1455	}
1456	return (error);
1457}
1458
1459/*
1460 * This is used from various compatibility syscalls too.  That's why name
1461 * must be in kernel space.
1462 */
1463int
1464userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1465    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1466    int flags)
1467{
1468	int error = 0, memlocked;
1469	struct sysctl_req req;
1470
1471	bzero(&req, sizeof req);
1472
1473	req.td = td;
1474	req.flags = flags;
1475
1476	if (oldlenp) {
1477		if (inkernel) {
1478			req.oldlen = *oldlenp;
1479		} else {
1480			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1481			if (error)
1482				return (error);
1483		}
1484	}
1485	req.validlen = req.oldlen;
1486
1487	if (old) {
1488		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1489			return (EFAULT);
1490		req.oldptr= old;
1491	}
1492
1493	if (new != NULL) {
1494		if (!useracc(new, newlen, VM_PROT_READ))
1495			return (EFAULT);
1496		req.newlen = newlen;
1497		req.newptr = new;
1498	}
1499
1500	req.oldfunc = sysctl_old_user;
1501	req.newfunc = sysctl_new_user;
1502	req.lock = REQ_LOCKED;
1503
1504#ifdef KTRACE
1505	if (KTRPOINT(curthread, KTR_SYSCTL))
1506		ktrsysctl(name, namelen);
1507#endif
1508
1509	if (req.oldlen > PAGE_SIZE) {
1510		memlocked = 1;
1511		sx_xlock(&sysctlmemlock);
1512	} else
1513		memlocked = 0;
1514	CURVNET_SET(TD_TO_VNET(td));
1515
1516	for (;;) {
1517		req.oldidx = 0;
1518		req.newidx = 0;
1519		SYSCTL_SLOCK();
1520		error = sysctl_root(0, name, namelen, &req);
1521		SYSCTL_SUNLOCK();
1522		if (error != EAGAIN)
1523			break;
1524		uio_yield();
1525	}
1526
1527	CURVNET_RESTORE();
1528
1529	if (req.lock == REQ_WIRED && req.validlen > 0)
1530		vsunlock(req.oldptr, req.validlen);
1531	if (memlocked)
1532		sx_xunlock(&sysctlmemlock);
1533
1534	if (error && error != ENOMEM)
1535		return (error);
1536
1537	if (retval) {
1538		if (req.oldptr && req.oldidx > req.validlen)
1539			*retval = req.validlen;
1540		else
1541			*retval = req.oldidx;
1542	}
1543	return (error);
1544}
1545