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