xref: /dragonfly/sys/kern/kern_sysctl.c (revision e97a1dae)
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  * 3. 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  * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/buf.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/priv.h>
47 #include <sys/sysproto.h>
48 #include <sys/lock.h>
49 #include <sys/sbuf.h>
50 
51 #include <sys/mplock2.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55 
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
57 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
58 
59 /*
60  * The sysctllock protects the MIB tree.  It also protects sysctl
61  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
62  * sysctl_unregister_oid() routines require the sysctllock to already
63  * be held, so the sysctl_lock() and sysctl_unlock() routines are
64  * provided for the few places in the kernel which need to use that
65  * API rather than using the dynamic API.  Use of the dynamic API is
66  * strongly encouraged for most code.
67  *
68  * The sysctlmemlock is used to limit the amount of user memory wired for
69  * sysctl requests.  This is implemented by serializing any userland
70  * sysctl requests larger than a single page via an exclusive lock.
71  */
72 struct lock sysctllock;
73 static struct lock sysctlmemlock;
74 
75 #define	SYSCTL_INIT()		lockinit(&sysctllock,			\
76 				    "sysctl lock", 0, LK_CANRECURSE)
77 #define	SYSCTL_SLEEP(ch, wmesg, timo)					\
78 				lksleep(ch, &sysctllock, 0, wmesg, timo)
79 
80 static int	sysctl_root(SYSCTL_HANDLER_ARGS);
81 static void	sysctl_register_oid_int(struct sysctl_oid *oipd);
82 static void	sysctl_unregister_oid_int(struct sysctl_oid *oipd);
83 
84 struct sysctl_oid_list sysctl__children; /* root list */
85 
86 static int	sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
87 		    int recurse);
88 
89 static struct sysctl_oid *
90 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
91 {
92 	struct sysctl_oid *oidp;
93 
94 	SLIST_FOREACH(oidp, list, oid_link) {
95 		if (strcmp(oidp->oid_name, name) == 0) {
96 			break;
97 		}
98 	}
99 	return (oidp);
100 }
101 
102 /*
103  * Initialization of the MIB tree.
104  *
105  * Order by number in each list.
106  */
107 
108 void
109 sysctl_register_oid(struct sysctl_oid *oidp)
110 {
111 	SYSCTL_XLOCK();
112 	sysctl_register_oid_int(oidp);
113 	SYSCTL_XUNLOCK();
114 }
115 
116 static void
117 sysctl_register_oid_int(struct sysctl_oid *oidp)
118 {
119 	struct sysctl_oid_list *parent = oidp->oid_parent;
120 	struct sysctl_oid *p;
121 	struct sysctl_oid *q;
122 
123 	/*
124 	 * First check if another oid with the same name already
125 	 * exists in the parent's list.
126 	 */
127 	p = sysctl_find_oidname(oidp->oid_name, parent, 0);
128 	if (p != NULL) {
129 		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
130 			p->oid_refcnt++;
131 		else
132 			kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
133 		return;
134 	}
135 
136 	/*
137 	 * If this oid has a number OID_AUTO, give it a number which
138 	 * is greater than any current oid.  Make sure it is at least
139 	 * 256 to leave space for pre-assigned oid numbers.
140 	 */
141 	if (oidp->oid_number == OID_AUTO) {
142 		int newoid = 0x100;	/* minimum AUTO oid */
143 
144 		/*
145 		 * Adjust based on highest oid in parent list
146 		 */
147 		SLIST_FOREACH(p, parent, oid_link) {
148 			if (newoid <= p->oid_number)
149 				newoid = p->oid_number + 1;
150 		}
151 		oidp->oid_number = newoid;
152 	}
153 
154 	/*
155 	 * Insert the oid into the parent's list in order.
156 	 */
157 	q = NULL;
158 	SLIST_FOREACH(p, parent, oid_link) {
159 		if (oidp->oid_number < p->oid_number)
160 			break;
161 		q = p;
162 	}
163 	if (q)
164 		SLIST_INSERT_AFTER(q, oidp, oid_link);
165 	else
166 		SLIST_INSERT_HEAD(parent, oidp, oid_link);
167 }
168 
169 void
170 sysctl_unregister_oid(struct sysctl_oid *oidp)
171 {
172 	SYSCTL_XLOCK();
173 	sysctl_unregister_oid_int(oidp);
174 	SYSCTL_XUNLOCK();
175 }
176 
177 static void
178 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
179 {
180 	struct sysctl_oid *p;
181 
182 	if (oidp->oid_number == OID_AUTO)
183 		panic("Trying to unregister OID_AUTO entry: %p", oidp);
184 
185 	SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
186 		if (p != oidp)
187 			continue;
188 		SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
189 		return;
190 	}
191 
192 	/*
193 	 * This can happen when a module fails to register and is
194 	 * being unloaded afterwards.  It should not be a panic()
195 	 * for normal use.
196 	 */
197 	kprintf("%s: failed to unregister sysctl\n", __func__);
198 }
199 
200 /* Initialize a new context to keep track of dynamically added sysctls. */
201 int
202 sysctl_ctx_init(struct sysctl_ctx_list *c)
203 {
204 	if (c == NULL)
205 		return(EINVAL);
206 	TAILQ_INIT(c);
207 	return(0);
208 }
209 
210 /* Free the context, and destroy all dynamic oids registered in this context */
211 int
212 sysctl_ctx_free(struct sysctl_ctx_list *clist)
213 {
214 	struct sysctl_ctx_entry *e, *e1;
215 	int error;
216 
217 	error = 0;
218 	/*
219 	 * First perform a "dry run" to check if it's ok to remove oids.
220 	 * XXX FIXME
221 	 * XXX This algorithm is a hack. But I don't know any
222 	 * XXX better solution for now...
223 	 */
224 	SYSCTL_XLOCK();
225 	TAILQ_FOREACH(e, clist, link) {
226 		error = sysctl_remove_oid_locked(e->entry, 0, 0);
227 		if (error)
228 			break;
229 	}
230 	/*
231 	 * Restore deregistered entries, either from the end,
232 	 * or from the place where error occured.
233 	 * e contains the entry that was not unregistered
234 	 */
235 	if (error)
236 		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
237 	else
238 		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
239 	while (e1 != NULL) {
240 		sysctl_register_oid(e1->entry);
241 		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
242 	}
243 	if (error) {
244 		SYSCTL_XUNLOCK();
245 		return(EBUSY);
246 	}
247 	/* Now really delete the entries */
248 	e = TAILQ_FIRST(clist);
249 	while (e != NULL) {
250 		e1 = TAILQ_NEXT(e, link);
251 		error = sysctl_remove_oid_locked(e->entry, 1, 0);
252 		if (error)
253 			panic("sysctl_remove_oid: corrupt tree, entry: %s",
254 			    e->entry->oid_name);
255 		kfree(e, M_SYSCTLOID);
256 		e = e1;
257 	}
258 	SYSCTL_XUNLOCK();
259 	return (error);
260 }
261 
262 /* Add an entry to the context */
263 struct sysctl_ctx_entry *
264 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
265 {
266 	struct sysctl_ctx_entry *e;
267 
268 	SYSCTL_ASSERT_XLOCKED();
269 	if (clist == NULL || oidp == NULL)
270 		return(NULL);
271 	e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
272 	e->entry = oidp;
273 	TAILQ_INSERT_HEAD(clist, e, link);
274 	return (e);
275 }
276 
277 /* Find an entry in the context */
278 struct sysctl_ctx_entry *
279 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
280 {
281 	struct sysctl_ctx_entry *e;
282 
283 	SYSCTL_ASSERT_XLOCKED();
284 	if (clist == NULL || oidp == NULL)
285 		return(NULL);
286 	TAILQ_FOREACH(e, clist, link) {
287 		if(e->entry == oidp)
288 			return(e);
289 	}
290 	return (e);
291 }
292 
293 /*
294  * Delete an entry from the context.
295  * NOTE: this function doesn't free oidp! You have to remove it
296  * with sysctl_remove_oid().
297  */
298 int
299 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
300 {
301 	struct sysctl_ctx_entry *e;
302 
303 	if (clist == NULL || oidp == NULL)
304 		return (EINVAL);
305 	SYSCTL_XLOCK();
306 	e = sysctl_ctx_entry_find(clist, oidp);
307 	if (e != NULL) {
308 		TAILQ_REMOVE(clist, e, link);
309 		SYSCTL_XUNLOCK();
310 		kfree(e, M_SYSCTLOID);
311 		return (0);
312 	} else {
313 		SYSCTL_XUNLOCK();
314 		return (ENOENT);
315 	}
316 }
317 
318 /*
319  * Remove dynamically created sysctl trees.
320  * oidp - top of the tree to be removed
321  * del - if 0 - just deregister, otherwise free up entries as well
322  * recurse - if != 0 traverse the subtree to be deleted
323  */
324 int
325 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
326 {
327 	int error;
328 
329 	SYSCTL_XLOCK();
330 	error = sysctl_remove_oid_locked(oidp, del, recurse);
331 	SYSCTL_XUNLOCK();
332 	return (error);
333 }
334 
335 static int
336 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
337 {
338 	struct sysctl_oid *p, *tmp;
339 	int error;
340 
341 	SYSCTL_ASSERT_XLOCKED();
342 	if (oidp == NULL)
343 		return(EINVAL);
344 	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
345 		kprintf("can't remove non-dynamic nodes!\n");
346 		return (EINVAL);
347 	}
348 	/*
349 	 * WARNING: normal method to do this should be through
350 	 * sysctl_ctx_free(). Use recursing as the last resort
351 	 * method to purge your sysctl tree of leftovers...
352 	 * However, if some other code still references these nodes,
353 	 * it will panic.
354 	 */
355 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
356 		if (oidp->oid_refcnt == 1) {
357 			SLIST_FOREACH_MUTABLE(p,
358 			    SYSCTL_CHILDREN(oidp), oid_link, tmp) {
359 				if (!recurse) {
360 					kprintf("Warning: failed attempt to "
361 					    "remove oid %s with child %s\n",
362 					    oidp->oid_name, p->oid_name);
363 					return (ENOTEMPTY);
364 				}
365 				error = sysctl_remove_oid_locked(p, del,
366 				    recurse);
367 				if (error)
368 					return (error);
369 			}
370 			if (del)
371 				kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
372 		}
373 	}
374 	if (oidp->oid_refcnt > 1 ) {
375 		oidp->oid_refcnt--;
376 	} else {
377 		if (oidp->oid_refcnt == 0) {
378 			kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
379 				oidp->oid_refcnt, oidp->oid_name);
380 			return (EINVAL);
381 		}
382 		sysctl_unregister_oid(oidp);
383 		if (del) {
384 			/*
385 			 * Wait for all threads running the handler to drain.
386 			 * This preserves the previous behavior when the
387 			 * sysctl lock was held across a handler invocation,
388 			 * and is necessary for module unload correctness.
389 			 */
390 			while (oidp->oid_running > 0) {
391 				oidp->oid_kind |= CTLFLAG_DYING;
392 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
393 			}
394 			if (oidp->oid_descr)
395 				kfree(__DECONST(char *, oidp->oid_descr),
396 				    M_SYSCTLOID);
397 			kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
398 			kfree(oidp, M_SYSCTLOID);
399 		}
400 	}
401 	return (0);
402 }
403 
404 int
405 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
406     int del, int recurse)
407 {
408 	struct sysctl_oid *p, *tmp;
409 	int error;
410 
411 	error = ENOENT;
412 	SYSCTL_XLOCK();
413 	SLIST_FOREACH_MUTABLE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
414 		if (strcmp(p->oid_name, name) == 0) {
415 			error = sysctl_remove_oid_locked(p, del, recurse);
416 			break;
417 		}
418 	}
419 	SYSCTL_XUNLOCK();
420 
421 	return (error);
422 }
423 
424 /*
425  * Create new sysctls at run time.
426  * clist may point to a valid context initialized with sysctl_ctx_init().
427  */
428 struct sysctl_oid *
429 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
430 	int number, const char *name, int kind, void *arg1, int arg2,
431 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
432 {
433 	struct sysctl_oid *oidp;
434 	ssize_t len;
435 	char *newname;
436 
437 	/* You have to hook up somewhere.. */
438 	if (parent == NULL)
439 		return(NULL);
440 	SYSCTL_XLOCK();
441 	/* Check if the node already exists, otherwise create it */
442 	oidp = sysctl_find_oidname(name, parent, 0);
443 	if (oidp != NULL) {
444 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
445 			oidp->oid_refcnt++;
446 			/* Update the context */
447 			if (clist != NULL)
448 				sysctl_ctx_entry_add(clist, oidp);
449 			SYSCTL_XUNLOCK();
450 			return (oidp);
451 		} else {
452 			kprintf("can't re-use a leaf (%s)!\n", name);
453 			SYSCTL_XUNLOCK();
454 			return (NULL);
455 		}
456 	}
457 	oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
458 	oidp->oid_parent = parent;
459 	SLIST_NEXT(oidp, oid_link) = NULL;
460 	oidp->oid_number = number;
461 	oidp->oid_refcnt = 1;
462 	len = strlen(name);
463 	newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
464 	bcopy(name, newname, len + 1);
465 	newname[len] = '\0';
466 	oidp->oid_name = newname;
467 	oidp->oid_handler = handler;
468 	oidp->oid_kind = CTLFLAG_DYN | kind;
469 	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
470 		struct sysctl_oid_list *children;
471 
472 		/* Allocate space for children */
473 		children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
474 		SYSCTL_SET_CHILDREN(oidp, children);
475 		SLIST_INIT(children);
476 	} else {
477 		oidp->oid_arg1 = arg1;
478 		oidp->oid_arg2 = arg2;
479 	}
480 	oidp->oid_fmt = fmt;
481 	if (descr) {
482 		int len = strlen(descr) + 1;
483 		oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
484 		strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
485 	};
486 	/* Update the context, if used */
487 	if (clist != NULL)
488 		sysctl_ctx_entry_add(clist, oidp);
489 	/* Register this oid */
490 	sysctl_register_oid_int(oidp);
491 	SYSCTL_XUNLOCK();
492 	return (oidp);
493 }
494 
495 /*
496  * Rename an existing oid.
497  */
498 void
499 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
500 {
501 	char *newname;
502 	char *oldname;
503 
504 	newname = kstrdup(name, M_SYSCTLOID);
505 	SYSCTL_XLOCK();
506 	oldname = __DECONST(char *, oidp->oid_name);
507 	oidp->oid_name = newname;
508 	SYSCTL_XUNLOCK();
509 	kfree(oldname, M_SYSCTLOID);
510 }
511 
512 /*
513  * Register the kernel's oids on startup.
514  */
515 SET_DECLARE(sysctl_set, struct sysctl_oid);
516 
517 static void
518 sysctl_register_all(void *arg)
519 {
520 	struct sysctl_oid **oidp;
521 
522 	lockinit(&sysctlmemlock, "sysctl mem", 0, LK_CANRECURSE);
523 	SYSCTL_INIT();
524 	SYSCTL_XLOCK();
525 	SET_FOREACH(oidp, sysctl_set)
526 		sysctl_register_oid(*oidp);
527 	SYSCTL_XUNLOCK();
528 }
529 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
530 
531 /*
532  * "Staff-functions"
533  *
534  * These functions implement a presently undocumented interface
535  * used by the sysctl program to walk the tree, and get the type
536  * so it can print the value.
537  * This interface is under work and consideration, and should probably
538  * be killed with a big axe by the first person who can find the time.
539  * (be aware though, that the proper interface isn't as obvious as it
540  * may seem, there are various conflicting requirements.
541  *
542  * {0,0}	kprintf the entire MIB-tree.
543  * {0,1,...}	return the name of the "..." OID.
544  * {0,2,...}	return the next OID.
545  * {0,3}	return the OID of the name in "new"
546  * {0,4,...}	return the kind & format info for the "..." OID.
547  */
548 
549 static void
550 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
551 {
552 	int k;
553 	struct sysctl_oid *oidp;
554 
555 	SYSCTL_ASSERT_XLOCKED();
556 	SLIST_FOREACH(oidp, l, oid_link) {
557 
558 		for (k=0; k<i; k++)
559 			kprintf(" ");
560 
561 		kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
562 
563 		kprintf("%c%c",
564 			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
565 			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
566 
567 		if (oidp->oid_handler)
568 			kprintf(" *Handler");
569 
570 		switch (oidp->oid_kind & CTLTYPE) {
571 			case CTLTYPE_NODE:
572 				kprintf(" Node\n");
573 				if (!oidp->oid_handler) {
574 					sysctl_sysctl_debug_dump_node(
575 						oidp->oid_arg1, i+2);
576 				}
577 				break;
578 			case CTLTYPE_INT:    kprintf(" Int\n"); break;
579 			case CTLTYPE_STRING: kprintf(" String\n"); break;
580 			case CTLTYPE_QUAD:   kprintf(" Quad\n"); break;
581 			case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
582 			default:	     kprintf("\n");
583 		}
584 
585 	}
586 }
587 
588 static int
589 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
590 {
591 	int error;
592 
593 	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
594 	if (error)
595 		return (error);
596 	SYSCTL_XLOCK();
597 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
598 	SYSCTL_XUNLOCK();
599 	return (ENOENT);
600 }
601 
602 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
603 	0, 0, sysctl_sysctl_debug, "-", "");
604 
605 static int
606 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
607 {
608 	int *name = (int *) arg1;
609 	u_int namelen = arg2;
610 	int error = 0;
611 	struct sysctl_oid *oid;
612 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
613 	char buf[10];
614 
615 	SYSCTL_XLOCK();
616 	while (namelen) {
617 		if (!lsp) {
618 			ksnprintf(buf, sizeof(buf), "%d",  *name);
619 			if (req->oldidx)
620 				error = SYSCTL_OUT(req, ".", 1);
621 			if (!error)
622 				error = SYSCTL_OUT(req, buf, strlen(buf));
623 			if (error)
624 				goto out;
625 			namelen--;
626 			name++;
627 			continue;
628 		}
629 		lsp2 = NULL;
630 		SLIST_FOREACH(oid, lsp, oid_link) {
631 			if (oid->oid_number != *name)
632 				continue;
633 
634 			if (req->oldidx)
635 				error = SYSCTL_OUT(req, ".", 1);
636 			if (!error)
637 				error = SYSCTL_OUT(req, oid->oid_name,
638 					strlen(oid->oid_name));
639 			if (error)
640 				goto out;
641 
642 			namelen--;
643 			name++;
644 
645 			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
646 				break;
647 
648 			if (oid->oid_handler)
649 				break;
650 
651 			lsp2 = SYSCTL_CHILDREN(oid);
652 			break;
653 		}
654 		lsp = lsp2;
655 	}
656 	error = SYSCTL_OUT(req, "", 1);
657  out:
658 	SYSCTL_XUNLOCK();
659 	return (error);
660 }
661 
662 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
663 
664 static int
665 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
666 	int *next, int *len, int level, struct sysctl_oid **oidpp)
667 {
668 	struct sysctl_oid *oidp;
669 
670 	SYSCTL_ASSERT_XLOCKED();
671 	*len = level;
672 	SLIST_FOREACH(oidp, lsp, oid_link) {
673 		*next = oidp->oid_number;
674 		*oidpp = oidp;
675 
676 		if (oidp->oid_kind & CTLFLAG_SKIP)
677 			continue;
678 
679 		if (!namelen) {
680 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
681 				return (0);
682 			if (oidp->oid_handler)
683 				/* We really should call the handler here...*/
684 				return (0);
685 			lsp = SYSCTL_CHILDREN(oidp);
686 			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
687 				len, level+1, oidpp))
688 				return (0);
689 			goto emptynode;
690 		}
691 
692 		if (oidp->oid_number < *name)
693 			continue;
694 
695 		if (oidp->oid_number > *name) {
696 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
697 				return (0);
698 			if (oidp->oid_handler)
699 				return (0);
700 			lsp = SYSCTL_CHILDREN(oidp);
701 			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
702 				next+1, len, level+1, oidpp))
703 				return (0);
704 			goto next;
705 		}
706 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
707 			continue;
708 
709 		if (oidp->oid_handler)
710 			continue;
711 
712 		lsp = SYSCTL_CHILDREN(oidp);
713 		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
714 			len, level+1, oidpp))
715 			return (0);
716 	next:
717 		namelen = 1;
718 	emptynode:
719 		*len = level;
720 	}
721 	return (1);
722 }
723 
724 static int
725 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
726 {
727 	int *name = (int *) arg1;
728 	u_int namelen = arg2;
729 	int i, j, error;
730 	struct sysctl_oid *oid;
731 	struct sysctl_oid_list *lsp = &sysctl__children;
732 	int newoid[CTL_MAXNAME];
733 
734 	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
735 	if (i)
736 		return ENOENT;
737 	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
738 	return (error);
739 }
740 
741 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
742 
743 static int
744 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
745 {
746 	struct sysctl_oid *oidp;
747 	struct sysctl_oid_list *lsp = &sysctl__children;
748 	char *p;
749 
750 	SYSCTL_ASSERT_XLOCKED();
751 
752 	for (*len = 0; *len < CTL_MAXNAME;) {
753 		p = strsep(&name, ".");
754 
755 		oidp = SLIST_FIRST(lsp);
756 		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
757 			if (oidp == NULL)
758 				return (ENOENT);
759 			if (strcmp(p, oidp->oid_name) == 0)
760 				break;
761 		}
762 		*oid++ = oidp->oid_number;
763 		(*len)++;
764 
765 		if (name == NULL || *name == '\0') {
766 			if (oidpp)
767 				*oidpp = oidp;
768 			return (0);
769 		}
770 
771 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
772 			break;
773 
774 		if (oidp->oid_handler)
775 			break;
776 
777 		lsp = SYSCTL_CHILDREN(oidp);
778 	}
779 	return (ENOENT);
780 }
781 
782 static int
783 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
784 {
785 	char *p;
786 	int error, oid[CTL_MAXNAME], len;
787 	struct sysctl_oid *op = NULL;
788 
789 	if (!req->newlen)
790 		return ENOENT;
791 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
792 		return (ENAMETOOLONG);
793 
794 	p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
795 
796 	error = SYSCTL_IN(req, p, req->newlen);
797 	if (error) {
798 		kfree(p, M_SYSCTL);
799 		return (error);
800 	}
801 
802 	p [req->newlen] = '\0';
803 
804 	error = name2oid(p, oid, &len, &op);
805 
806 	kfree(p, M_SYSCTL);
807 
808 	if (error)
809 		return (error);
810 
811 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
812 	return (error);
813 }
814 
815 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
816 	sysctl_sysctl_name2oid, "I", "");
817 
818 static int
819 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
820 {
821 	struct sysctl_oid *oid;
822 	int error;
823 
824 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
825 	if (error)
826 		return (error);
827 
828 	if (!oid->oid_fmt)
829 		return (ENOENT);
830 	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
831 	if (error)
832 		return (error);
833 	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
834 	return (error);
835 }
836 
837 
838 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
839 
840 static int
841 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
842 {
843 	struct sysctl_oid *oid;
844 	int error;
845 
846 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
847 	if (error)
848 		return (error);
849 
850 	if (!oid->oid_descr)
851 		return (ENOENT);
852 	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
853 	return (error);
854 }
855 
856 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
857 
858 /*
859  * Default "handler" functions.
860  */
861 
862 /*
863  * Handle an int, signed or unsigned.
864  * Two cases:
865  *     a variable:  point arg1 at it.
866  *     a constant:  pass it in arg2.
867  */
868 
869 int
870 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
871 {
872 	int error = 0;
873 
874 	if (arg1)
875 		error = SYSCTL_OUT(req, arg1, sizeof(int));
876 	else
877 		error = SYSCTL_OUT(req, &arg2, sizeof(int));
878 
879 	if (error || !req->newptr)
880 		return (error);
881 
882 	if (!arg1)
883 		error = EPERM;
884 	else
885 		error = SYSCTL_IN(req, arg1, sizeof(int));
886 	return (error);
887 }
888 
889 /*
890  * Handle a long, signed or unsigned.  arg1 points to it.
891  */
892 
893 int
894 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
895 {
896 	int error = 0;
897 
898 	if (!arg1)
899 		return (EINVAL);
900 	error = SYSCTL_OUT(req, arg1, sizeof(long));
901 
902 	if (error || !req->newptr)
903 		return (error);
904 
905 	error = SYSCTL_IN(req, arg1, sizeof(long));
906 	return (error);
907 }
908 
909 /*
910  * Handle a quad, signed or unsigned.  arg1 points to it.
911  */
912 
913 int
914 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
915 {
916 	int error = 0;
917 
918 	if (!arg1)
919 		return (EINVAL);
920 	error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
921 
922 	if (error || !req->newptr)
923 		return (error);
924 
925 	error = SYSCTL_IN(req, arg1, sizeof(quad_t));
926 	return (error);
927 }
928 
929 /*
930  * Handle our generic '\0' terminated 'C' string.
931  * Two cases:
932  *	a variable string:  point arg1 at it, arg2 is max length.
933  *	a constant string:  point arg1 at it, arg2 is zero.
934  */
935 
936 int
937 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
938 {
939 	int error=0;
940 
941 	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
942 
943 	if (error || !req->newptr)
944 		return (error);
945 
946 	if ((req->newlen - req->newidx) >= arg2) {
947 		error = EINVAL;
948 	} else {
949 		arg2 = (req->newlen - req->newidx);
950 		error = SYSCTL_IN(req, arg1, arg2);
951 		((char *)arg1)[arg2] = '\0';
952 	}
953 
954 	return (error);
955 }
956 
957 /*
958  * Handle any kind of opaque data.
959  * arg1 points to it, arg2 is the size.
960  */
961 
962 int
963 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
964 {
965 	int error;
966 
967 	error = SYSCTL_OUT(req, arg1, arg2);
968 
969 	if (error || !req->newptr)
970 		return (error);
971 
972 	error = SYSCTL_IN(req, arg1, arg2);
973 
974 	return (error);
975 }
976 
977 /*
978  * Transfer functions to/from kernel space.
979  * XXX: rather untested at this point
980  */
981 static int
982 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
983 {
984 	size_t i = 0;
985 
986 	if (req->oldptr) {
987 		i = l;
988 		if (i > req->oldlen - req->oldidx)
989 			i = req->oldlen - req->oldidx;
990 		if (i > 0)
991 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
992 	}
993 	req->oldidx += l;
994 	if (req->oldptr && i != l)
995 		return (ENOMEM);
996 	return (0);
997 }
998 
999 static int
1000 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1001 {
1002 
1003 	if (!req->newptr)
1004 		return 0;
1005 	if (req->newlen - req->newidx < l)
1006 		return (EINVAL);
1007 	bcopy((char *)req->newptr + req->newidx, p, l);
1008 	req->newidx += l;
1009 	return (0);
1010 }
1011 
1012 int
1013 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
1014 {
1015 	int error = 0;
1016 	struct sysctl_req req;
1017 
1018 	bzero(&req, sizeof req);
1019 
1020 	req.td = curthread;
1021 
1022 	if (oldlenp) {
1023 		req.oldlen = *oldlenp;
1024 	}
1025 	req.validlen = req.oldlen;
1026 
1027 	if (old) {
1028 		req.oldptr= old;
1029 	}
1030 
1031 	if (new != NULL) {
1032 		req.newlen = newlen;
1033 		req.newptr = new;
1034 	}
1035 
1036 	req.oldfunc = sysctl_old_kernel;
1037 	req.newfunc = sysctl_new_kernel;
1038 #if 0
1039 	req.lock = REQ_UNWIRED;
1040 #endif
1041 
1042 	SYSCTL_XLOCK();
1043 	error = sysctl_root(0, name, namelen, &req);
1044 	SYSCTL_XUNLOCK();
1045 
1046 #if 0
1047 	if (req.lock == REQ_WIRED && req.validlen > 0)
1048 		vsunlock(req.oldptr, req.validlen);
1049 #endif
1050 
1051 	if (error && error != ENOMEM)
1052 		return (error);
1053 
1054 	if (retval) {
1055 		if (req.oldptr && req.oldidx > req.validlen)
1056 			*retval = req.validlen;
1057 		else
1058 			*retval = req.oldidx;
1059 	}
1060 	return (error);
1061 }
1062 
1063 int
1064 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1065     void *new, size_t newlen, size_t *retval)
1066 {
1067         int oid[CTL_MAXNAME];
1068         size_t oidlen, plen;
1069 	int error;
1070 
1071 	oid[0] = 0;		/* sysctl internal magic */
1072 	oid[1] = 3;		/* name2oid */
1073 	oidlen = sizeof(oid);
1074 
1075 	error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1076 	if (error)
1077 		return (error);
1078 
1079 	error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1080 	    new, newlen, retval);
1081 	return (error);
1082 }
1083 
1084 /*
1085  * Transfer function to/from user space.
1086  */
1087 static int
1088 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1089 {
1090 	int error = 0;
1091 	size_t i = 0;
1092 
1093 #if 0
1094 	if (req->lock == 1 && req->oldptr) {
1095 		vslock(req->oldptr, req->oldlen);
1096 		req->lock = 2;
1097 	}
1098 #endif
1099 	if (req->oldptr) {
1100 		i = l;
1101 		if (i > req->oldlen - req->oldidx)
1102 			i = req->oldlen - req->oldidx;
1103 		if (i > 0)
1104 			error = copyout(p, (char *)req->oldptr + req->oldidx,
1105 					i);
1106 	}
1107 	req->oldidx += l;
1108 	if (error)
1109 		return (error);
1110 	if (req->oldptr && i < l)
1111 		return (ENOMEM);
1112 	return (0);
1113 }
1114 
1115 static int
1116 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1117 {
1118 	int error;
1119 
1120 	if (!req->newptr)
1121 		return 0;
1122 	if (req->newlen - req->newidx < l)
1123 		return (EINVAL);
1124 	error = copyin((char *)req->newptr + req->newidx, p, l);
1125 	req->newidx += l;
1126 	return (error);
1127 }
1128 
1129 int
1130 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1131     int *nindx, struct sysctl_req *req)
1132 {
1133 	struct sysctl_oid_list *lsp;
1134 	struct sysctl_oid *oid;
1135 	int indx;
1136 
1137 	SYSCTL_ASSERT_XLOCKED();
1138 	lsp = &sysctl__children;
1139 	indx = 0;
1140 	while (indx < CTL_MAXNAME) {
1141 		SLIST_FOREACH(oid, lsp, oid_link) {
1142 			if (oid->oid_number == name[indx])
1143 				break;
1144 		}
1145 		if (oid == NULL)
1146 			return (ENOENT);
1147 
1148 		indx++;
1149 		if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1150 			if (oid->oid_handler != NULL || indx == namelen) {
1151 				*noid = oid;
1152 				if (nindx != NULL)
1153 					*nindx = indx;
1154 				KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1155 				    ("%s found DYING node %p", __func__, oid));
1156 				return (0);
1157 			}
1158 			lsp = SYSCTL_CHILDREN(oid);
1159 		} else if (indx == namelen) {
1160 			*noid = oid;
1161 			if (nindx != NULL)
1162 				*nindx = indx;
1163 			KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1164 			    ("%s found DYING node %p", __func__, oid));
1165 			return (0);
1166 		} else {
1167 			return (ENOTDIR);
1168 		}
1169 	}
1170 	return (ENOENT);
1171 }
1172 
1173 /*
1174  * Traverse our tree, and find the right node, execute whatever it points
1175  * to, and return the resulting error code.
1176  */
1177 
1178 static int
1179 sysctl_root(SYSCTL_HANDLER_ARGS)
1180 {
1181 	struct thread *td = req->td;
1182 	struct proc *p = td ? td->td_proc : NULL;
1183 	struct sysctl_oid *oid;
1184 	int error, indx;
1185 
1186 	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1187 	if (error)
1188 		return (error);
1189 
1190 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1191 		/*
1192 		 * You can't call a sysctl when it's a node, but has
1193 		 * no handler.  Inform the user that it's a node.
1194 		 * The indx may or may not be the same as namelen.
1195 		 */
1196 		if (oid->oid_handler == NULL)
1197 			return (EISDIR);
1198 	}
1199 
1200 	/* If writing isn't allowed */
1201 	if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1202 	    ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1203 		return (EPERM);
1204 
1205 	/* Most likely only root can write */
1206 	if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1207 	    (error = priv_check_cred(td->td_ucred,
1208 	     (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1209 	                                        PRIV_SYSCTL_WRITE, 0)))
1210 		return (error);
1211 
1212 	if (!oid->oid_handler)
1213 		return EINVAL;
1214 
1215 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1216 		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1217 		    req);
1218 	else
1219 		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1220 		    req);
1221 	return (error);
1222 }
1223 
1224 int
1225 sys___sysctl(struct sysctl_args *uap)
1226 {
1227 	int error, i, name[CTL_MAXNAME];
1228 	size_t j;
1229 
1230 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1231 		return (EINVAL);
1232 
1233 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1234 	if (error)
1235 		return (error);
1236 
1237 	error = userland_sysctl(name, uap->namelen,
1238 		uap->old, uap->oldlenp, 0,
1239 		uap->new, uap->newlen, &j);
1240 	if (error && error != ENOMEM)
1241 		return (error);
1242 	if (uap->oldlenp) {
1243 		i = copyout(&j, uap->oldlenp, sizeof(j));
1244 		if (i)
1245 			return (i);
1246 	}
1247 	return (error);
1248 }
1249 
1250 /*
1251  * This is used from various compatibility syscalls too.  That's why name
1252  * must be in kernel space.
1253  */
1254 int
1255 userland_sysctl(int *name, u_int namelen, void *old,
1256     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1257 {
1258 	int error = 0, memlocked;
1259 	struct sysctl_req req;
1260 
1261 	bzero(&req, sizeof req);
1262 
1263 	req.td = curthread;
1264 	req.flags = 0;
1265 
1266 	if (oldlenp) {
1267 		if (inkernel) {
1268 			req.oldlen = *oldlenp;
1269 		} else {
1270 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1271 			if (error)
1272 				return (error);
1273 		}
1274 	}
1275 	req.validlen = req.oldlen;
1276 
1277 	if (old) {
1278 		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1279 			return (EFAULT);
1280 		req.oldptr= old;
1281 	}
1282 
1283 	if (new != NULL) {
1284 		if (!useracc(new, newlen, VM_PROT_READ))
1285 			return (EFAULT);
1286 		req.newlen = newlen;
1287 		req.newptr = new;
1288 	}
1289 
1290 	req.oldfunc = sysctl_old_user;
1291 	req.newfunc = sysctl_new_user;
1292 #if 0
1293 	req.lock = REQ_UNWIRED;
1294 #endif
1295 
1296 #ifdef KTRACE
1297 	if (KTRPOINT(curthread, KTR_SYSCTL))
1298 		ktrsysctl(name, namelen);
1299 #endif
1300 
1301 	if (req.oldlen > PAGE_SIZE) {
1302 		memlocked = 1;
1303 		lockmgr(&sysctlmemlock, LK_EXCLUSIVE);
1304 	} else
1305 		memlocked = 0;
1306 
1307 	for (;;) {
1308 		req.oldidx = 0;
1309 		req.newidx = 0;
1310 		SYSCTL_XLOCK();
1311 		error = sysctl_root(0, name, namelen, &req);
1312 		SYSCTL_XUNLOCK();
1313 		if (error != EAGAIN)
1314 			break;
1315 		lwkt_yield();
1316 	}
1317 
1318 #if 0
1319 	if (req.lock == REQ_WIRED && req.validlen > 0)
1320 		vsunlock(req.oldptr, req.validlen);
1321 #endif
1322 	if (memlocked)
1323 		lockmgr(&sysctlmemlock, LK_RELEASE);
1324 
1325 	if (error && error != ENOMEM)
1326 		return (error);
1327 
1328 	if (retval) {
1329 		if (req.oldptr && req.oldidx > req.validlen)
1330 			*retval = req.validlen;
1331 		else
1332 			*retval = req.oldidx;
1333 	}
1334 	return (error);
1335 }
1336 
1337 int
1338 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1339 {
1340 	int error, value;
1341 
1342 	value = *(int *)arg1;
1343 	error = sysctl_handle_int(oidp, &value, 0, req);
1344 	if (error || !req->newptr)
1345 		return (error);
1346 	if (value < low || value > high)
1347 		return (EINVAL);
1348 	*(int *)arg1 = value;
1349 	return (0);
1350 }
1351 
1352 /*
1353  * Drain into a sysctl struct.  The user buffer should be wired if a page
1354  * fault would cause issue.
1355  */
1356 static int
1357 sbuf_sysctl_drain(void *arg, const char *data, int len)
1358 {
1359 	struct sysctl_req *req = arg;
1360 	int error;
1361 
1362 	error = SYSCTL_OUT(req, data, len);
1363 	KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1364 	return (error == 0 ? len : -error);
1365 }
1366 
1367 struct sbuf *
1368 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1369     struct sysctl_req *req)
1370 {
1371 
1372 	s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1373 	sbuf_set_drain(s, sbuf_sysctl_drain, req);
1374 	return (s);
1375 }
1376