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