usr.sbin/makefs/ffs: Remove m_buf::b_is_hammer2
[dragonfly.git] / sys / kern / kern_sysctl.c
blob8f4785e901e695666d5d665268c70656c0e0b7a6
1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
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.
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.
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 $
39 #include "opt_ktrace.h"
40 #include "opt_sysctl.h"
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
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
60 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
61 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
63 int sysctl_debugx = 0;
64 SYSCTL_INT(_debug, OID_AUTO, sysctl, CTLFLAG_RW, &sysctl_debugx, 0, "");
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.
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);
80 struct sysctl_oid_list sysctl__children; /* root list */
82 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
83 int recurse);
85 static struct sysctl_oid *
86 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
88 struct sysctl_oid *oidp;
90 SLIST_FOREACH(oidp, list, oid_link) {
91 if (strcmp(oidp->oid_name, name) == 0) {
92 break;
95 return (oidp);
99 * Initialization of the MIB tree.
101 * Order by number in each list.
104 void
105 sysctl_register_oid(struct sysctl_oid *oidp)
107 SYSCTL_XLOCK();
108 sysctl_register_oid_int(oidp);
109 SYSCTL_XUNLOCK();
112 static void
113 sysctl_register_oid_int(struct sysctl_oid *oidp)
115 struct sysctl_oid_list *parent = oidp->oid_parent;
116 struct sysctl_oid *p;
117 struct sysctl_oid *q;
120 * Finish initialization from sysctl_set or add.
122 lockinit(&oidp->oid_lock, "oidlk", 0, LK_CANRECURSE);
125 * First check if another oid with the same name already
126 * exists in the parent's list.
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;
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.
142 if (oidp->oid_number == OID_AUTO) {
143 int newoid = 0x100; /* minimum AUTO oid */
146 * Adjust based on highest oid in parent list
148 SLIST_FOREACH(p, parent, oid_link) {
149 if (newoid <= p->oid_number)
150 newoid = p->oid_number + 1;
152 oidp->oid_number = newoid;
156 * Insert the oid into the parent's list in order.
158 q = NULL;
159 SLIST_FOREACH(p, parent, oid_link) {
160 if (oidp->oid_number < p->oid_number)
161 break;
162 q = p;
164 if (q)
165 SLIST_INSERT_AFTER(q, oidp, oid_link);
166 else
167 SLIST_INSERT_HEAD(parent, oidp, oid_link);
170 void
171 sysctl_unregister_oid(struct sysctl_oid *oidp)
173 SYSCTL_XLOCK();
174 sysctl_unregister_oid_int(oidp);
175 SYSCTL_XUNLOCK();
178 static void
179 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
181 struct sysctl_oid *p;
183 if (oidp->oid_number == OID_AUTO)
184 panic("Trying to unregister OID_AUTO entry: %p", oidp);
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;
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.
198 kprintf("%s: failed to unregister sysctl\n", __func__);
201 /* Initialize a new context to keep track of dynamically added sysctls. */
203 sysctl_ctx_init(struct sysctl_ctx_list *c)
205 if (c == NULL)
206 return(EINVAL);
207 TAILQ_INIT(c);
208 return(0);
211 /* Free the context, and destroy all dynamic oids registered in this context */
213 sysctl_ctx_free(struct sysctl_ctx_list *clist)
215 struct sysctl_ctx_entry *e, *e1;
216 int error;
218 error = 0;
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...
225 SYSCTL_XLOCK();
226 TAILQ_FOREACH(e, clist, link) {
227 error = sysctl_remove_oid_locked(e->entry, 0, 0);
228 if (error)
229 break;
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
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);
244 if (error) {
245 SYSCTL_XUNLOCK();
246 return(EBUSY);
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;
259 SYSCTL_XUNLOCK();
260 return (error);
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)
267 struct sysctl_ctx_entry *e;
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);
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)
282 struct sysctl_ctx_entry *e;
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);
291 return (e);
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().
300 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
302 struct sysctl_ctx_entry *e;
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);
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
326 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
328 int error;
330 SYSCTL_XLOCK();
331 error = sysctl_remove_oid_locked(oidp, del, recurse);
332 SYSCTL_XUNLOCK();
333 return (error);
336 static int
337 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
339 struct sysctl_oid *p, *tmp;
340 int error;
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);
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.
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);
366 error = sysctl_remove_oid_locked(p, del,
367 recurse);
368 if (error)
369 return (error);
371 if (del)
372 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
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);
383 sysctl_unregister_oid(oidp);
384 if (del) {
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.
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();
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);
407 return (0);
411 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
412 int del, int recurse)
414 struct sysctl_oid *p, *tmp;
415 int error;
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;
425 SYSCTL_XUNLOCK();
427 return (error);
431 * Create new sysctls at run time.
432 * clist may point to a valid context initialized with sysctl_ctx_init().
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)
439 struct sysctl_oid *oidp;
440 ssize_t len;
441 char *newname;
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);
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;
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;
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);
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);
503 * Rename an existing oid.
505 void
506 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
508 char *newname;
509 char *oldname;
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);
520 * Register the kernel's oids on startup.
522 SET_DECLARE(sysctl_set, struct sysctl_oid);
524 static void
525 sysctl_register_all(void *arg)
527 struct sysctl_oid **oidp;
529 SYSCTL_XLOCK();
530 SET_FOREACH(oidp, sysctl_set)
531 sysctl_register_oid(*oidp);
532 SYSCTL_XUNLOCK();
534 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
536 #ifdef SYSCTL_DEBUG
538 * "Staff-functions"
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.
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.
560 static void
561 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
563 int k;
564 struct sysctl_oid *oidp;
566 SLIST_FOREACH(oidp, l, oid_link) {
568 for (k=0; k<i; k++)
569 kprintf(" ");
571 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
573 kprintf("%c%c",
574 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
575 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
577 if (oidp->oid_handler)
578 kprintf(" *Handler");
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);
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;
644 static int
645 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
647 int error;
649 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
650 if (error)
651 return (error);
652 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
654 return (ENOENT);
657 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD,
658 0, 0, sysctl_sysctl_debug, "-", "");
659 #endif /* SYSCTL_DEBUG */
661 static int
662 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
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];
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;
684 lsp2 = NULL;
685 SLIST_FOREACH(oid, lsp, oid_link) {
686 if (oid->oid_number != *name)
687 continue;
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;
697 namelen--;
698 name++;
700 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
701 break;
703 if (oid->oid_handler)
704 break;
706 lsp2 = SYSCTL_CHILDREN(oid);
707 break;
709 lsp = lsp2;
711 error = SYSCTL_OUT(req, "", 1);
712 out:
713 return (error);
716 SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD | CTLFLAG_NOLOCK,
717 sysctl_sysctl_name, "");
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)
723 struct sysctl_oid *oidp;
725 *len = level;
726 SLIST_FOREACH(oidp, lsp, oid_link) {
727 *next = oidp->oid_number;
728 *oidpp = oidp;
730 if (oidp->oid_kind & CTLFLAG_SKIP)
731 continue;
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;
746 if (oidp->oid_number < *name)
747 continue;
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;
760 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
761 continue;
763 if (oidp->oid_handler)
764 continue;
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;
775 return (1);
778 static int
779 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
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];
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));
793 return (error);
796 SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD | CTLFLAG_NOLOCK,
797 sysctl_sysctl_next, "");
799 static int
800 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
802 struct sysctl_oid *oidp;
803 struct sysctl_oid_list *lsp = &sysctl__children;
804 char *p;
806 SYSCTL_ASSERT_LOCKED();
808 for (*len = 0; *len < CTL_MAXNAME;) {
809 p = strsep(&name, ".");
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;
818 *oid++ = oidp->oid_number;
819 (*len)++;
821 if (name == NULL || *name == '\0') {
822 if (oidpp)
823 *oidpp = oidp;
824 return (0);
827 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
828 break;
830 if (oidp->oid_handler)
831 break;
833 lsp = SYSCTL_CHILDREN(oidp);
835 return (ENOENT);
838 static int
839 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
841 char *p;
842 int error, oid[CTL_MAXNAME], len;
843 struct sysctl_oid *op = NULL;
845 if (!req->newlen)
846 return ENOENT;
847 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
848 return (ENAMETOOLONG);
850 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
852 error = SYSCTL_IN(req, p, req->newlen);
853 if (error) {
854 kfree(p, M_SYSCTL);
855 return (error);
858 p [req->newlen] = '\0';
860 error = name2oid(p, oid, &len, &op);
862 kfree(p, M_SYSCTL);
864 if (error)
865 return (error);
867 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
868 return (error);
871 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid,
872 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
873 0, 0, sysctl_sysctl_name2oid, "I", "");
875 static int
876 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
878 struct sysctl_oid *oid;
879 int error;
881 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
882 if (error)
883 return (error);
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);
895 SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD | CTLFLAG_NOLOCK,
896 sysctl_sysctl_oidfmt, "");
898 static int
899 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
901 struct sysctl_oid *oid;
902 int error;
904 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
905 if (error)
906 return (error);
908 if (!oid->oid_descr)
909 return (ENOENT);
910 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
911 return (error);
914 SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr,
915 CTLFLAG_RD | CTLFLAG_NOLOCK,
916 sysctl_sysctl_oiddescr, "");
919 * Default "handler" functions.
923 * Handle an 8-bit number, signed or unsigned. arg1 points to it.
927 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
929 int error = 0;
931 if (!arg1)
932 return (EINVAL);
933 error = SYSCTL_OUT(req, arg1, sizeof(int8_t));
935 if (error || !req->newptr)
936 return (error);
938 error = SYSCTL_IN(req, arg1, sizeof(int8_t));
939 return (error);
943 * Handle a 16-bit number, signed or unsigned. arg1 points to it.
947 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
949 int error = 0;
951 if (!arg1)
952 return (EINVAL);
953 error = SYSCTL_OUT(req, arg1, sizeof(int16_t));
955 if (error || !req->newptr)
956 return (error);
958 error = SYSCTL_IN(req, arg1, sizeof(int16_t));
959 return (error);
963 * Handle a 32-bit number, signed or unsigned. arg1 points to it.
967 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
969 int error = 0;
971 if (!arg1)
972 return (EINVAL);
973 error = SYSCTL_OUT(req, arg1, sizeof(int32_t));
975 if (error || !req->newptr)
976 return (error);
978 error = SYSCTL_IN(req, arg1, sizeof(int32_t));
979 return (error);
983 * Handle a 64-bit number, signed or unsigned. arg1 points to it.
987 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
989 int error = 0;
991 if (!arg1)
992 return (EINVAL);
993 error = SYSCTL_OUT(req, arg1, sizeof(int64_t));
995 if (error || !req->newptr)
996 return (error);
998 error = SYSCTL_IN(req, arg1, sizeof(int64_t));
999 return (error);
1003 * Handle an int, signed or unsigned.
1004 * Two cases:
1005 * a variable: point arg1 at it.
1006 * a constant: pass it in arg2.
1010 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1012 int error = 0;
1014 if (arg1)
1015 error = SYSCTL_OUT(req, arg1, sizeof(int));
1016 else
1017 error = SYSCTL_OUT(req, &arg2, sizeof(int));
1019 if (error || !req->newptr)
1020 return (error);
1022 if (!arg1)
1023 error = EPERM;
1024 else
1025 error = SYSCTL_IN(req, arg1, sizeof(int));
1026 return (error);
1030 * Handle a long, signed or unsigned. arg1 points to it.
1034 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1036 int error = 0;
1038 if (!arg1)
1039 return (EINVAL);
1040 if (req->oldlen == sizeof(int) &&
1041 *(long *)arg1 >= INT_MIN &&
1042 *(long *)arg1 <= INT_MAX) {
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.
1048 int val = (int)*(long *)arg1;
1049 error = SYSCTL_OUT(req, &val, sizeof(int));
1050 } else {
1052 * Normal operation fo a long
1054 error = SYSCTL_OUT(req, arg1, sizeof(long));
1057 if (error || !req->newptr)
1058 return (error);
1060 error = SYSCTL_IN(req, arg1, sizeof(long));
1062 return (error);
1066 * Handle a quad, signed or unsigned. arg1 points to it.
1070 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1072 int error = 0;
1074 if (!arg1)
1075 return (EINVAL);
1076 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
1078 if (error || !req->newptr)
1079 return (error);
1081 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
1082 return (error);
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.
1093 sysctl_handle_bit32(SYSCTL_HANDLER_ARGS)
1095 int error = 0;
1096 uint32_t mask;
1097 int v;
1098 int bit;
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));
1105 if (error || !req->newptr)
1106 return (error);
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);
1119 return (error);
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)
1130 sysctl_handle_bit64(SYSCTL_HANDLER_ARGS)
1132 int error = 0;
1133 uint64_t mask;
1134 int v;
1135 int bit;
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));
1142 if (error || !req->newptr)
1143 return (error);
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);
1156 return (error);
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.
1167 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1169 int error=0;
1171 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
1173 if (error || !req->newptr)
1174 return (error);
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';
1184 return (error);
1188 * Handle any kind of opaque data.
1189 * arg1 points to it, arg2 is the size.
1193 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1195 int error;
1197 error = SYSCTL_OUT(req, arg1, arg2);
1199 if (error || !req->newptr)
1200 return (error);
1202 error = SYSCTL_IN(req, arg1, arg2);
1204 return (error);
1208 * Transfer functions to/from kernel space.
1209 * XXX: rather untested at this point
1211 static int
1212 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1214 size_t i = 0;
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);
1223 req->oldidx += l;
1224 if (req->oldptr && i != l)
1225 return (ENOMEM);
1226 return (0);
1229 static int
1230 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
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);
1243 kernel_sysctl(int *name, u_int namelen,
1244 void *old, size_t *oldlenp,
1245 void *new, size_t newlen, size_t *retval)
1247 int error = 0;
1248 struct sysctl_req req;
1250 bzero(&req, sizeof req);
1252 req.td = curthread;
1254 if (oldlenp) {
1255 req.oldlen = *oldlenp;
1257 req.validlen = req.oldlen;
1259 if (old) {
1260 req.oldptr= old;
1263 if (new != NULL) {
1264 req.newlen = newlen;
1265 req.newptr = new;
1268 req.oldfunc = sysctl_old_kernel;
1269 req.newfunc = sysctl_new_kernel;
1270 #if 0
1271 req.lock = REQ_UNWIRED;
1272 #endif
1274 SYSCTL_SLOCK();
1275 error = sysctl_root(0, name, namelen, &req);
1276 SYSCTL_SUNLOCK();
1278 #if 0
1279 if (req.lock == REQ_WIRED && req.validlen > 0)
1280 vsunlock(req.oldptr, req.validlen);
1281 #endif
1283 if (error && error != ENOMEM)
1284 return (error);
1286 if (retval) {
1287 if (req.oldptr && req.oldidx > req.validlen)
1288 *retval = req.validlen;
1289 else
1290 *retval = req.oldidx;
1292 return (error);
1296 kernel_sysctlbyname(char *name,
1297 void *old, size_t *oldlenp,
1298 void *new, size_t newlen, size_t *retval)
1300 int oid[CTL_MAXNAME];
1301 size_t oidlen, plen;
1302 int error;
1304 oid[0] = CTL_SYSCTL;
1305 oid[1] = CTL_SYSCTL_NAME2OID;
1306 oidlen = sizeof(oid);
1308 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1309 if (error)
1310 return (error);
1312 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1313 new, newlen, retval);
1314 return (error);
1318 * Transfer function to/from user space.
1320 static int
1321 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1323 int error = 0;
1324 size_t i = 0;
1326 #if 0
1327 if (req->lock == 1 && req->oldptr) {
1328 vslock(req->oldptr, req->oldlen);
1329 req->lock = 2;
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,
1340 req->oldidx += l;
1341 if (error)
1342 return (error);
1343 if (req->oldptr && i < l)
1344 return (ENOMEM);
1345 return (0);
1348 static int
1349 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1351 int error;
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);
1363 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1364 int *nindx, struct sysctl_req *req)
1366 struct sysctl_oid_list *lsp;
1367 struct sysctl_oid *oid;
1368 int indx;
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;
1377 if (oid == NULL)
1378 return (ENOENT);
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);
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);
1402 return (ENOENT);
1406 * Traverse our tree, and find the right node, execute whatever it points
1407 * to, and return the resulting error code.
1409 static int
1410 sysctl_root(SYSCTL_HANDLER_ARGS)
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;
1418 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1419 if (error)
1420 return (error);
1422 if (sysctl_debugx & 1) {
1423 kprintf("pid %d oid %p %s\n",
1424 (p ? p->p_pid : -1), oid, oid->oid_name);
1428 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
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.
1434 if (oid->oid_handler == NULL)
1435 return (EISDIR);
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);
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);
1450 if (oid->oid_handler == NULL)
1451 return EINVAL;
1454 * Default oid locking is exclusive when modifying (newptr),
1455 * shared otherwise, unless overridden with a control flag.
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);
1471 #endif
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);
1481 if ((oid->oid_kind & CTLFLAG_NOLOCK) == 0)
1482 lockmgr(&oid->oid_lock, LK_RELEASE);
1483 return (error);
1487 sys___sysctl(struct sysmsg *sysmsg, const struct sysctl_args *uap)
1489 int error, i, name[CTL_MAXNAME];
1490 size_t j;
1492 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1493 return (EINVAL);
1495 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1496 if (error)
1497 return (error);
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);
1509 return (error);
1513 * This is used from various compatibility syscalls too. That's why name
1514 * must be in kernel space.
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)
1521 struct thread *td = curthread;
1522 struct lwp *lp = td->td_lwp;
1523 int error = 0;
1524 struct sysctl_req req;
1526 bzero(&req, sizeof req);
1528 req.td = td;
1529 req.flags = 0;
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);
1540 req.validlen = req.oldlen;
1543 * NOTE: User supplied buffers are not guaranteed to be good,
1544 * the sysctl copyins and copyouts can fail.
1546 if (old)
1547 req.oldptr= old;
1549 if (new != NULL) {
1550 req.newlen = newlen;
1551 req.newptr = new;
1554 req.oldfunc = sysctl_old_user;
1555 req.newfunc = sysctl_new_user;
1556 #if 0
1557 req.lock = REQ_UNWIRED;
1558 #endif
1560 #ifdef KTRACE
1561 if (KTRPOINT(td, KTR_SYSCTL))
1562 ktrsysctl(lp, name, namelen);
1563 #endif
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();
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);
1583 if (retval) {
1584 if (req.oldptr && req.oldidx > req.validlen)
1585 *retval = req.validlen;
1586 else
1587 *retval = req.oldidx;
1589 return (error);
1593 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1595 int error, value;
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);
1608 * Drain into a sysctl struct. The user buffer should be wired if a page
1609 * fault would cause issue.
1611 static int
1612 sbuf_sysctl_drain(void *arg, const char *data, int len)
1614 struct sysctl_req *req = arg;
1615 int error;
1617 error = SYSCTL_OUT(req, data, len);
1618 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1619 return (error == 0 ? len : -error);
1622 struct sbuf *
1623 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1624 struct sysctl_req *req)
1627 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1628 sbuf_set_drain(s, sbuf_sysctl_drain, req);
1629 return (s);
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.
1637 void
1638 _sysctl_xlock(void)
1640 globaldata_t gd;
1641 int i;
1643 for (i = 0; i < ncpus; ++i) {
1644 gd = globaldata_find(i);
1645 lockmgr(&gd->gd_sysctllock, LK_EXCLUSIVE);
1649 void
1650 _sysctl_xunlock(void)
1652 globaldata_t gd;
1653 int i;
1655 for (i = 0; i < ncpus; ++i) {
1656 gd = globaldata_find(i);
1657 lockmgr(&gd->gd_sysctllock, LK_RELEASE);