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