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