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
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
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 <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
47 #include <sys/sysproto.h>
52 #include <vm/vm_extern.h>
54 #include "opt_sysctl.h"
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
57 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
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.
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);
73 struct sysctl_oid_list sysctl__children; /* root list */
75 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
78 static struct sysctl_oid *
79 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
81 struct sysctl_oid *oidp;
83 SLIST_FOREACH(oidp, list, oid_link) {
84 if (strcmp(oidp->oid_name, name) == 0) {
92 * Initialization of the MIB tree.
94 * Order by number in each list.
98 sysctl_register_oid(struct sysctl_oid *oidp)
101 sysctl_register_oid_int(oidp);
106 sysctl_register_oid_int(struct sysctl_oid *oidp)
108 struct sysctl_oid_list *parent = oidp->oid_parent;
109 struct sysctl_oid *p;
110 struct sysctl_oid *q;
113 * Finish initialization from sysctl_set or add.
115 lockinit(&oidp->oid_lock, "oidlk", 0, LK_CANRECURSE);
118 * First check if another oid with the same name already
119 * exists in the parent's list.
121 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
123 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
126 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
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.
135 if (oidp->oid_number == OID_AUTO) {
136 int newoid = 0x100; /* minimum AUTO oid */
139 * Adjust based on highest oid in parent list
141 SLIST_FOREACH(p, parent, oid_link) {
142 if (newoid <= p->oid_number)
143 newoid = p->oid_number + 1;
145 oidp->oid_number = newoid;
149 * Insert the oid into the parent's list in order.
152 SLIST_FOREACH(p, parent, oid_link) {
153 if (oidp->oid_number < p->oid_number)
158 SLIST_INSERT_AFTER(q, oidp, oid_link);
160 SLIST_INSERT_HEAD(parent, oidp, oid_link);
164 sysctl_unregister_oid(struct sysctl_oid *oidp)
167 sysctl_unregister_oid_int(oidp);
172 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
174 struct sysctl_oid *p;
176 if (oidp->oid_number == OID_AUTO)
177 panic("Trying to unregister OID_AUTO entry: %p", oidp);
179 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
182 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
187 * This can happen when a module fails to register and is
188 * being unloaded afterwards. It should not be a panic()
191 kprintf("%s: failed to unregister sysctl\n", __func__);
194 /* Initialize a new context to keep track of dynamically added sysctls. */
196 sysctl_ctx_init(struct sysctl_ctx_list *c)
204 /* Free the context, and destroy all dynamic oids registered in this context */
206 sysctl_ctx_free(struct sysctl_ctx_list *clist)
208 struct sysctl_ctx_entry *e, *e1;
213 * First perform a "dry run" to check if it's ok to remove oids.
215 * XXX This algorithm is a hack. But I don't know any
216 * XXX better solution for now...
219 TAILQ_FOREACH(e, clist, link) {
220 error = sysctl_remove_oid_locked(e->entry, 0, 0);
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
230 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
232 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
234 sysctl_register_oid(e1->entry);
235 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
241 /* Now really delete the entries */
242 e = TAILQ_FIRST(clist);
244 e1 = TAILQ_NEXT(e, link);
245 error = sysctl_remove_oid_locked(e->entry, 1, 0);
247 panic("sysctl_remove_oid: corrupt tree, entry: %s",
249 kfree(e, M_SYSCTLOID);
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)
260 struct sysctl_ctx_entry *e;
262 SYSCTL_ASSERT_LOCKED();
263 if (clist == NULL || oidp == NULL)
265 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
267 TAILQ_INSERT_HEAD(clist, e, link);
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)
275 struct sysctl_ctx_entry *e;
277 SYSCTL_ASSERT_LOCKED();
278 if (clist == NULL || oidp == NULL)
280 TAILQ_FOREACH(e, clist, link) {
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().
293 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
295 struct sysctl_ctx_entry *e;
297 if (clist == NULL || oidp == NULL)
300 e = sysctl_ctx_entry_find(clist, oidp);
302 TAILQ_REMOVE(clist, e, link);
304 kfree(e, M_SYSCTLOID);
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
319 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
324 error = sysctl_remove_oid_locked(oidp, del, recurse);
330 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
332 struct sysctl_oid *p, *tmp;
335 SYSCTL_ASSERT_LOCKED();
338 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
339 kprintf("can't remove non-dynamic nodes!\n");
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,
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) {
354 kprintf("Warning: failed attempt to "
355 "remove oid %s with child %s\n",
356 oidp->oid_name, p->oid_name);
359 error = sysctl_remove_oid_locked(p, del,
365 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
368 if (oidp->oid_refcnt > 1 ) {
371 if (oidp->oid_refcnt == 0) {
372 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
373 oidp->oid_refcnt, oidp->oid_name);
376 sysctl_unregister_oid(oidp);
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.
384 while (oidp->oid_running > 0) {
385 oidp->oid_kind |= CTLFLAG_DYING;
386 tsleep_interlock(&oidp->oid_running, 0);
388 tsleep(&oidp->oid_running, PINTERLOCKED,
393 kfree(__DECONST(char *, oidp->oid_descr),
395 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
396 lockuninit(&oidp->oid_lock);
397 kfree(oidp, M_SYSCTLOID);
404 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
405 int del, int recurse)
407 struct sysctl_oid *p, *tmp;
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);
424 * Create new sysctls at run time.
425 * clist may point to a valid context initialized with sysctl_ctx_init().
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)
432 struct sysctl_oid *oidp;
436 /* You have to hook up somewhere.. */
440 /* Check if the node already exists, otherwise create it */
441 oidp = sysctl_find_oidname(name, parent, 0);
443 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
445 /* Update the context */
447 sysctl_ctx_entry_add(clist, oidp);
451 kprintf("can't re-use a leaf (%s)!\n", name);
456 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID,
458 oidp->oid_parent = parent;
459 SLIST_NEXT(oidp, oid_link) = NULL;
460 oidp->oid_number = number;
461 oidp->oid_refcnt = 1;
463 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
464 bcopy(name, newname, len + 1);
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;
472 /* Allocate space for children */
473 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
474 SYSCTL_SET_CHILDREN(oidp, children);
475 SLIST_INIT(children);
477 oidp->oid_arg1 = arg1;
478 oidp->oid_arg2 = arg2;
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);
486 /* Update the context, if used */
488 sysctl_ctx_entry_add(clist, oidp);
489 /* Register this oid */
490 sysctl_register_oid_int(oidp);
496 * Rename an existing oid.
499 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
504 newname = kstrdup(name, M_SYSCTLOID);
506 oldname = __DECONST(char *, oidp->oid_name);
507 oidp->oid_name = newname;
509 kfree(oldname, M_SYSCTLOID);
513 * Register the kernel's oids on startup.
515 SET_DECLARE(sysctl_set, struct sysctl_oid);
518 sysctl_register_all(void *arg)
520 struct sysctl_oid **oidp;
523 SET_FOREACH(oidp, sysctl_set)
524 sysctl_register_oid(*oidp);
527 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
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.
541 * {0,0} kprintf the entire MIB-tree.
542 * {0,1,...} return the name of the "..." OID.
543 * {0,2,...} return the next OID.
544 * {0,3} return the OID of the name in "new"
545 * {0,4,...} return the kind & format info for the "..." OID.
549 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
552 struct sysctl_oid *oidp;
554 SLIST_FOREACH(oidp, l, oid_link) {
559 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
562 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
563 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
565 if (oidp->oid_handler)
566 kprintf(" *Handler");
568 switch (oidp->oid_kind & CTLTYPE) {
571 if (!oidp->oid_handler) {
572 sysctl_sysctl_debug_dump_node(
573 oidp->oid_arg1, i+2);
576 case CTLTYPE_INT: kprintf(" Int\n"); break;
577 case CTLTYPE_UINT: kprintf(" u_int\n"); break;
578 case CTLTYPE_LONG: kprintf(" Long\n"); break;
579 case CTLTYPE_ULONG: kprintf(" u_long\n"); break;
580 case CTLTYPE_STRING: kprintf(" String\n"); break;
581 case CTLTYPE_S8: kprintf(" int8_t\n"); break;
582 case CTLTYPE_S16: kprintf(" int16_t\n"); break;
583 case CTLTYPE_S32: kprintf(" int32_t\n"); break;
584 case CTLTYPE_S64: kprintf(" int64_t\n"); break;
585 case CTLTYPE_U8: kprintf(" uint8_t\n"); break;
586 case CTLTYPE_U16: kprintf(" uint16_t\n"); break;
587 case CTLTYPE_U32: kprintf(" uint32_t\n"); break;
588 case CTLTYPE_U64: kprintf(" uint64_t\n"); break;
589 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
590 default: kprintf("\n");
597 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
601 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
604 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
609 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING | CTLFLAG_RD,
610 0, 0, sysctl_sysctl_debug, "-", "");
611 #endif /* SYSCTL_DEBUG */
614 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
616 int *name = (int *) arg1;
617 u_int namelen = arg2;
619 struct sysctl_oid *oid;
620 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
625 ksnprintf(buf, sizeof(buf), "%d", *name);
627 error = SYSCTL_OUT(req, ".", 1);
629 error = SYSCTL_OUT(req, buf, strlen(buf));
637 SLIST_FOREACH(oid, lsp, oid_link) {
638 if (oid->oid_number != *name)
642 error = SYSCTL_OUT(req, ".", 1);
644 error = SYSCTL_OUT(req, oid->oid_name,
645 strlen(oid->oid_name));
652 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
655 if (oid->oid_handler)
658 lsp2 = SYSCTL_CHILDREN(oid);
663 error = SYSCTL_OUT(req, "", 1);
668 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_NOLOCK,
669 sysctl_sysctl_name, "");
672 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
673 int *next, int *len, int level, struct sysctl_oid **oidpp)
675 struct sysctl_oid *oidp;
678 SLIST_FOREACH(oidp, lsp, oid_link) {
679 *next = oidp->oid_number;
682 if (oidp->oid_kind & CTLFLAG_SKIP)
686 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
688 if (oidp->oid_handler)
689 /* We really should call the handler here...*/
691 lsp = SYSCTL_CHILDREN(oidp);
692 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
693 len, level+1, oidpp))
698 if (oidp->oid_number < *name)
701 if (oidp->oid_number > *name) {
702 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
704 if (oidp->oid_handler)
706 lsp = SYSCTL_CHILDREN(oidp);
707 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
708 next+1, len, level+1, oidpp))
712 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
715 if (oidp->oid_handler)
718 lsp = SYSCTL_CHILDREN(oidp);
719 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
720 len, level+1, oidpp))
731 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
733 int *name = (int *) arg1;
734 u_int namelen = arg2;
736 struct sysctl_oid *oid;
737 struct sysctl_oid_list *lsp = &sysctl__children;
738 int newoid[CTL_MAXNAME];
740 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
743 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
748 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_NOLOCK,
749 sysctl_sysctl_next, "");
752 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
754 struct sysctl_oid *oidp;
755 struct sysctl_oid_list *lsp = &sysctl__children;
758 SYSCTL_ASSERT_LOCKED();
760 for (*len = 0; *len < CTL_MAXNAME;) {
761 p = strsep(&name, ".");
763 oidp = SLIST_FIRST(lsp);
764 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
767 if (strcmp(p, oidp->oid_name) == 0)
770 *oid++ = oidp->oid_number;
773 if (name == NULL || *name == '\0') {
779 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
782 if (oidp->oid_handler)
785 lsp = SYSCTL_CHILDREN(oidp);
791 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
794 int error, oid[CTL_MAXNAME], len;
795 struct sysctl_oid *op = NULL;
799 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
800 return (ENAMETOOLONG);
802 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
804 error = SYSCTL_IN(req, p, req->newlen);
810 p [req->newlen] = '\0';
812 error = name2oid(p, oid, &len, &op);
819 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
823 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
824 0, 0, sysctl_sysctl_name2oid, "I", "");
827 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
829 struct sysctl_oid *oid;
832 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
838 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
841 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
846 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD | CTLFLAG_NOLOCK,
847 sysctl_sysctl_oidfmt, "");
850 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
852 struct sysctl_oid *oid;
855 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
861 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
865 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD | CTLFLAG_NOLOCK,
866 sysctl_sysctl_oiddescr, "");
869 * Default "handler" functions.
873 * Handle an 8-bit number, signed or unsigned. arg1 points to it.
877 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
883 error = SYSCTL_OUT(req, arg1, sizeof(int8_t));
885 if (error || !req->newptr)
888 error = SYSCTL_IN(req, arg1, sizeof(int8_t));
893 * Handle a 16-bit number, signed or unsigned. arg1 points to it.
897 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
903 error = SYSCTL_OUT(req, arg1, sizeof(int16_t));
905 if (error || !req->newptr)
908 error = SYSCTL_IN(req, arg1, sizeof(int16_t));
913 * Handle a 32-bit number, signed or unsigned. arg1 points to it.
917 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
923 error = SYSCTL_OUT(req, arg1, sizeof(int32_t));
925 if (error || !req->newptr)
928 error = SYSCTL_IN(req, arg1, sizeof(int32_t));
933 * Handle a 64-bit number, signed or unsigned. arg1 points to it.
937 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
943 error = SYSCTL_OUT(req, arg1, sizeof(int64_t));
945 if (error || !req->newptr)
948 error = SYSCTL_IN(req, arg1, sizeof(int64_t));
953 * Handle an int, signed or unsigned.
955 * a variable: point arg1 at it.
956 * a constant: pass it in arg2.
960 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
965 error = SYSCTL_OUT(req, arg1, sizeof(int));
967 error = SYSCTL_OUT(req, &arg2, sizeof(int));
969 if (error || !req->newptr)
975 error = SYSCTL_IN(req, arg1, sizeof(int));
980 * Handle a long, signed or unsigned. arg1 points to it.
984 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
990 if (req->oldlen == sizeof(int) &&
991 *(long *)arg1 >= INT_MIN &&
992 *(long *)arg1 <= INT_MAX) {
994 * Backwards compatibility for read-only fields promoted
995 * from int to long. Allow userland to request the field
996 * as an integer if the value is in-range.
998 int val = (int)*(long *)arg1;
999 error = SYSCTL_OUT(req, &val, sizeof(int));
1002 * Normal operation fo a long
1004 error = SYSCTL_OUT(req, arg1, sizeof(long));
1007 if (error || !req->newptr)
1010 error = SYSCTL_IN(req, arg1, sizeof(long));
1016 * Handle a quad, signed or unsigned. arg1 points to it.
1020 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1026 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
1028 if (error || !req->newptr)
1031 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
1036 * Handle our generic '\0' terminated 'C' string.
1038 * a variable string: point arg1 at it, arg2 is max length.
1039 * a constant string: point arg1 at it, arg2 is zero.
1043 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1047 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
1049 if (error || !req->newptr)
1052 if ((req->newlen - req->newidx) >= arg2) {
1055 arg2 = (req->newlen - req->newidx);
1056 error = SYSCTL_IN(req, arg1, arg2);
1057 ((char *)arg1)[arg2] = '\0';
1064 * Handle any kind of opaque data.
1065 * arg1 points to it, arg2 is the size.
1069 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1073 error = SYSCTL_OUT(req, arg1, arg2);
1075 if (error || !req->newptr)
1078 error = SYSCTL_IN(req, arg1, arg2);
1084 * Transfer functions to/from kernel space.
1085 * XXX: rather untested at this point
1088 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1094 if (i > req->oldlen - req->oldidx)
1095 i = req->oldlen - req->oldidx;
1097 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1100 if (req->oldptr && i != l)
1106 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1111 if (req->newlen - req->newidx < l)
1113 bcopy((char *)req->newptr + req->newidx, p, l);
1119 kernel_sysctl(int *name, u_int namelen,
1120 void *old, size_t *oldlenp,
1121 void *new, size_t newlen, size_t *retval)
1124 struct sysctl_req req;
1126 bzero(&req, sizeof req);
1131 req.oldlen = *oldlenp;
1133 req.validlen = req.oldlen;
1140 req.newlen = newlen;
1144 req.oldfunc = sysctl_old_kernel;
1145 req.newfunc = sysctl_new_kernel;
1147 req.lock = REQ_UNWIRED;
1151 error = sysctl_root(0, name, namelen, &req);
1155 if (req.lock == REQ_WIRED && req.validlen > 0)
1156 vsunlock(req.oldptr, req.validlen);
1159 if (error && error != ENOMEM)
1163 if (req.oldptr && req.oldidx > req.validlen)
1164 *retval = req.validlen;
1166 *retval = req.oldidx;
1172 kernel_sysctlbyname(char *name,
1173 void *old, size_t *oldlenp,
1174 void *new, size_t newlen, size_t *retval)
1176 int oid[CTL_MAXNAME];
1177 size_t oidlen, plen;
1180 oid[0] = 0; /* sysctl internal magic */
1181 oid[1] = 3; /* name2oid */
1182 oidlen = sizeof(oid);
1184 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1188 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1189 new, newlen, retval);
1194 * Transfer function to/from user space.
1197 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1203 if (req->lock == 1 && req->oldptr) {
1204 vslock(req->oldptr, req->oldlen);
1210 if (i > req->oldlen - req->oldidx)
1211 i = req->oldlen - req->oldidx;
1213 error = copyout(p, (char *)req->oldptr + req->oldidx,
1219 if (req->oldptr && i < l)
1225 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1231 if (req->newlen - req->newidx < l)
1233 error = copyin((char *)req->newptr + req->newidx, p, l);
1239 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1240 int *nindx, struct sysctl_req *req)
1242 struct sysctl_oid_list *lsp;
1243 struct sysctl_oid *oid;
1246 lsp = &sysctl__children;
1248 while (indx < CTL_MAXNAME) {
1249 SLIST_FOREACH(oid, lsp, oid_link) {
1250 if (oid->oid_number == name[indx])
1257 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1258 if (oid->oid_handler != NULL || indx == namelen) {
1262 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1263 ("%s found DYING node %p", __func__, oid));
1266 lsp = SYSCTL_CHILDREN(oid);
1267 } else if (indx == namelen) {
1271 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1272 ("%s found DYING node %p", __func__, oid));
1282 * Traverse our tree, and find the right node, execute whatever it points
1283 * to, and return the resulting error code.
1286 sysctl_root(SYSCTL_HANDLER_ARGS)
1288 struct thread *td = req->td;
1289 struct proc *p = td ? td->td_proc : NULL;
1290 struct sysctl_oid *oid;
1294 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1298 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1300 * You can't call a sysctl when it's a node, but has
1301 * no handler. Inform the user that it's a node.
1302 * The indx may or may not be the same as namelen.
1304 if (oid->oid_handler == NULL)
1308 /* If writing isn't allowed */
1309 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1310 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1313 /* Most likely only root can write */
1314 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1315 (error = priv_check_cred(td->td_ucred,
1316 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1317 PRIV_SYSCTL_WRITE, 0)))
1320 if (oid->oid_handler == NULL)
1324 * Default oid locking is exclusive when modifying (newptr),
1325 * shared otherwise, unless overridden with a control flag.
1327 if ((oid->oid_kind & CTLFLAG_NOLOCK) == 0) {
1328 lktype = (req->newptr != NULL) ? LK_EXCLUSIVE : LK_SHARED;
1329 if (oid->oid_kind & CTLFLAG_SHLOCK)
1331 if (oid->oid_kind & CTLFLAG_EXLOCK)
1332 lktype = LK_EXCLUSIVE;
1334 lockmgr(&oid->oid_lock, lktype);
1337 if (lockmgr(&oid->oid_lock, lktype | LK_SLEEPFAIL)) {
1338 kprintf("%s\n", oid->oid_name);
1339 lockmgr(&oid->oid_lock, lktype);
1344 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1345 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1348 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1351 if ((oid->oid_kind & CTLFLAG_NOLOCK) == 0)
1352 lockmgr(&oid->oid_lock, LK_RELEASE);
1357 sys___sysctl(struct sysctl_args *uap)
1359 int error, i, name[CTL_MAXNAME];
1362 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1365 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1369 error = userland_sysctl(name, uap->namelen,
1370 uap->old, uap->oldlenp, 0,
1371 uap->new, uap->newlen, &j);
1372 if (error && error != ENOMEM)
1375 i = copyout(&j, uap->oldlenp, sizeof(j));
1383 * This is used from various compatibility syscalls too. That's why name
1384 * must be in kernel space.
1387 userland_sysctl(int *name, u_int namelen,
1388 void *old, size_t *oldlenp, int inkernel,
1389 void *new, size_t newlen, size_t *retval)
1392 struct sysctl_req req;
1394 bzero(&req, sizeof req);
1401 req.oldlen = *oldlenp;
1403 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1408 req.validlen = req.oldlen;
1411 * NOTE: User supplied buffers are not guaranteed to be good,
1412 * the sysctl copyins and copyouts can fail.
1418 req.newlen = newlen;
1422 req.oldfunc = sysctl_old_user;
1423 req.newfunc = sysctl_new_user;
1425 req.lock = REQ_UNWIRED;
1429 if (KTRPOINT(curthread, KTR_SYSCTL))
1430 ktrsysctl(name, namelen);
1437 error = sysctl_root(0, name, namelen, &req);
1439 if (error != EAGAIN)
1445 if (req.lock == REQ_WIRED && req.validlen > 0)
1446 vsunlock(req.oldptr, req.validlen);
1448 if (error && error != ENOMEM)
1452 if (req.oldptr && req.oldidx > req.validlen)
1453 *retval = req.validlen;
1455 *retval = req.oldidx;
1461 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1465 value = *(int *)arg1;
1466 error = sysctl_handle_int(oidp, &value, 0, req);
1467 if (error || !req->newptr)
1469 if (value < low || value > high)
1471 *(int *)arg1 = value;
1476 * Drain into a sysctl struct. The user buffer should be wired if a page
1477 * fault would cause issue.
1480 sbuf_sysctl_drain(void *arg, const char *data, int len)
1482 struct sysctl_req *req = arg;
1485 error = SYSCTL_OUT(req, data, len);
1486 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1487 return (error == 0 ? len : -error);
1491 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1492 struct sysctl_req *req)
1495 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1496 sbuf_set_drain(s, sbuf_sysctl_drain, req);
1501 * The exclusive sysctl lock only protects its topology, and is
1502 * very expensive, but allows us to use a pcpu shared lock for
1503 * critical path accesses.
1511 for (i = 0; i < ncpus; ++i) {
1512 gd = globaldata_find(i);
1513 lockmgr(&gd->gd_sysctllock, LK_EXCLUSIVE);
1518 _sysctl_xunlock(void)
1523 for (i = 0; i < ncpus; ++i) {
1524 gd = globaldata_find(i);
1525 lockmgr(&gd->gd_sysctllock, LK_RELEASE);