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 * 4. 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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_capsicum.h"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/capsicum.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/rmlock.h>
60 #include <sys/sysproto.h>
63 #include <sys/ktrace.h>
68 #include <security/mac/mac_framework.h>
71 #include <vm/vm_extern.h>
73 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
74 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
75 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
78 * The sysctllock protects the MIB tree. It also protects sysctl
79 * contexts used with dynamic sysctls. The sysctl_register_oid() and
80 * sysctl_unregister_oid() routines require the sysctllock to already
81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
82 * provided for the few places in the kernel which need to use that
83 * API rather than using the dynamic API. Use of the dynamic API is
84 * strongly encouraged for most code.
86 * The sysctlmemlock is used to limit the amount of user memory wired for
87 * sysctl requests. This is implemented by serializing any userland
88 * sysctl requests larger than a single page via an exclusive lock.
90 static struct rmlock sysctllock;
91 static struct sx sysctlmemlock;
93 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
94 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
95 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
96 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
97 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
98 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
99 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
100 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
101 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
103 #define SYSCTL_SLEEP(ch, wmesg, timo) \
104 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
106 static int sysctl_root(SYSCTL_HANDLER_ARGS);
109 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
111 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
113 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
114 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
116 static struct sysctl_oid *
117 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
119 struct sysctl_oid *oidp;
121 SYSCTL_ASSERT_LOCKED();
122 SLIST_FOREACH(oidp, list, oid_link) {
123 if (strcmp(oidp->oid_name, name) == 0) {
131 * Initialization of the MIB tree.
133 * Order by number in each list.
150 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2,
151 struct sysctl_req *req, struct rm_priotracker *tracker)
155 if (oid->oid_kind & CTLFLAG_DYN)
156 atomic_add_int(&oid->oid_running, 1);
159 SYSCTL_RUNLOCK(tracker);
163 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
165 error = oid->oid_handler(oid, arg1, arg2, req);
166 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
170 SYSCTL_RLOCK(tracker);
174 if (oid->oid_kind & CTLFLAG_DYN) {
175 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
176 (oid->oid_kind & CTLFLAG_DYING) != 0)
177 wakeup(&oid->oid_running);
184 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
186 struct sysctl_req req;
187 struct sysctl_oid *curr;
190 ssize_t rem = sizeof(path);
200 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
201 len = strlen(curr->oid_name);
206 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
209 memcpy(path + rem, curr->oid_name, len);
211 path[rem + len] = '.';
214 memset(&req, 0, sizeof(req));
217 req.oldfunc = sysctl_old_kernel;
218 req.newfunc = sysctl_new_kernel;
219 req.lock = REQ_UNWIRED;
221 switch (oidp->oid_kind & CTLTYPE) {
223 if (getenv_int(path + rem, &val_int) == 0)
225 req.newlen = sizeof(val_int);
226 req.newptr = &val_int;
229 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
231 req.newlen = sizeof(val_int);
232 req.newptr = &val_int;
235 if (getenv_long(path + rem, &val_long) == 0)
237 req.newlen = sizeof(val_long);
238 req.newptr = &val_long;
241 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
243 req.newlen = sizeof(val_long);
244 req.newptr = &val_long;
247 if (getenv_quad(path + rem, &val_quad) == 0)
250 req.newlen = sizeof(val_64);
251 req.newptr = &val_64;
254 /* XXX there is no getenv_uquad() */
255 if (getenv_quad(path + rem, &val_quad) == 0)
258 req.newlen = sizeof(val_64);
259 req.newptr = &val_64;
262 penv = kern_getenv(path + rem);
265 req.newlen = strlen(penv);
271 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
272 oidp->oid_arg2, &req, NULL);
274 printf("Setting sysctl %s failed: %d\n", path + rem, error);
280 sysctl_register_oid(struct sysctl_oid *oidp)
282 struct sysctl_oid_list *parent = oidp->oid_parent;
283 struct sysctl_oid *p;
284 struct sysctl_oid *q;
289 * First check if another oid with the same name already
290 * exists in the parent's list.
292 SYSCTL_ASSERT_WLOCKED();
293 p = sysctl_find_oidname(oidp->oid_name, parent);
295 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
299 printf("can't re-use a leaf (%s)!\n", p->oid_name);
303 /* get current OID number */
304 oid_number = oidp->oid_number;
307 #error "OID_AUTO is expected to be a negative value"
310 * Any negative OID number qualifies as OID_AUTO. Valid OID
311 * numbers should always be positive.
313 * NOTE: DO NOT change the starting value here, change it in
314 * <sys/sysctl.h>, and make sure it is at least 256 to
315 * accomodate e.g. net.inet.raw as a static sysctl node.
317 if (oid_number < 0) {
321 * By decrementing the next OID number we spend less
322 * time inserting the OIDs into a sorted list.
324 if (--newoid < CTL_AUTO_START)
331 * Insert the OID into the parent's list sorted by OID number.
335 SLIST_FOREACH(p, parent, oid_link) {
336 /* check if the current OID number is in use */
337 if (oid_number == p->oid_number) {
338 /* get the next valid OID number */
339 if (oid_number < CTL_AUTO_START ||
340 oid_number == 0x7fffffff) {
341 /* wraparound - restart */
342 oid_number = CTL_AUTO_START;
343 /* don't loop forever */
345 panic("sysctl: Out of OID numbers\n");
350 } else if (oid_number < p->oid_number)
354 /* check for non-auto OID number collision */
355 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
356 oid_number >= CTL_AUTO_START) {
357 printf("sysctl: OID number(%d) is already in use for '%s'\n",
358 oidp->oid_number, oidp->oid_name);
360 /* update the OID number, if any */
361 oidp->oid_number = oid_number;
363 SLIST_INSERT_AFTER(q, oidp, oid_link);
365 SLIST_INSERT_HEAD(parent, oidp, oid_link);
367 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
369 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
371 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
372 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
373 /* only fetch value once */
374 oidp->oid_kind |= CTLFLAG_NOFETCH;
375 /* try to fetch value from kernel environment */
376 sysctl_load_tunable_by_oid_locked(oidp);
381 sysctl_unregister_oid(struct sysctl_oid *oidp)
383 struct sysctl_oid *p;
386 SYSCTL_ASSERT_WLOCKED();
388 if (oidp->oid_number == OID_AUTO) {
391 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
393 SLIST_REMOVE(oidp->oid_parent, oidp,
394 sysctl_oid, oid_link);
402 * This can happen when a module fails to register and is
403 * being unloaded afterwards. It should not be a panic()
407 printf("%s: failed to unregister sysctl\n", __func__);
410 /* Initialize a new context to keep track of dynamically added sysctls. */
412 sysctl_ctx_init(struct sysctl_ctx_list *c)
420 * No locking here, the caller is responsible for not adding
421 * new nodes to a context until after this function has
428 /* Free the context, and destroy all dynamic oids registered in this context */
430 sysctl_ctx_free(struct sysctl_ctx_list *clist)
432 struct sysctl_ctx_entry *e, *e1;
437 * First perform a "dry run" to check if it's ok to remove oids.
439 * XXX This algorithm is a hack. But I don't know any
440 * XXX better solution for now...
443 TAILQ_FOREACH(e, clist, link) {
444 error = sysctl_remove_oid_locked(e->entry, 0, 0);
449 * Restore deregistered entries, either from the end,
450 * or from the place where error occured.
451 * e contains the entry that was not unregistered
454 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
456 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
458 sysctl_register_oid(e1->entry);
459 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
465 /* Now really delete the entries */
466 e = TAILQ_FIRST(clist);
468 e1 = TAILQ_NEXT(e, link);
469 error = sysctl_remove_oid_locked(e->entry, 1, 0);
471 panic("sysctl_remove_oid: corrupt tree, entry: %s",
473 free(e, M_SYSCTLOID);
480 /* Add an entry to the context */
481 struct sysctl_ctx_entry *
482 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
484 struct sysctl_ctx_entry *e;
486 SYSCTL_ASSERT_WLOCKED();
487 if (clist == NULL || oidp == NULL)
489 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
491 TAILQ_INSERT_HEAD(clist, e, link);
495 /* Find an entry in the context */
496 struct sysctl_ctx_entry *
497 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
499 struct sysctl_ctx_entry *e;
501 SYSCTL_ASSERT_WLOCKED();
502 if (clist == NULL || oidp == NULL)
504 TAILQ_FOREACH(e, clist, link) {
512 * Delete an entry from the context.
513 * NOTE: this function doesn't free oidp! You have to remove it
514 * with sysctl_remove_oid().
517 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
519 struct sysctl_ctx_entry *e;
521 if (clist == NULL || oidp == NULL)
524 e = sysctl_ctx_entry_find(clist, oidp);
526 TAILQ_REMOVE(clist, e, link);
528 free(e, M_SYSCTLOID);
537 * Remove dynamically created sysctl trees.
538 * oidp - top of the tree to be removed
539 * del - if 0 - just deregister, otherwise free up entries as well
540 * recurse - if != 0 traverse the subtree to be deleted
543 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
548 error = sysctl_remove_oid_locked(oidp, del, recurse);
554 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
555 int del, int recurse)
557 struct sysctl_oid *p, *tmp;
562 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
563 if (strcmp(p->oid_name, name) == 0) {
564 error = sysctl_remove_oid_locked(p, del, recurse);
575 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
577 struct sysctl_oid *p, *tmp;
580 SYSCTL_ASSERT_WLOCKED();
583 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
584 printf("can't remove non-dynamic nodes!\n");
588 * WARNING: normal method to do this should be through
589 * sysctl_ctx_free(). Use recursing as the last resort
590 * method to purge your sysctl tree of leftovers...
591 * However, if some other code still references these nodes,
594 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
595 if (oidp->oid_refcnt == 1) {
596 SLIST_FOREACH_SAFE(p,
597 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
599 printf("Warning: failed attempt to "
600 "remove oid %s with child %s\n",
601 oidp->oid_name, p->oid_name);
604 error = sysctl_remove_oid_locked(p, del,
611 if (oidp->oid_refcnt > 1 ) {
614 if (oidp->oid_refcnt == 0) {
615 printf("Warning: bad oid_refcnt=%u (%s)!\n",
616 oidp->oid_refcnt, oidp->oid_name);
619 sysctl_unregister_oid(oidp);
622 * Wait for all threads running the handler to drain.
623 * This preserves the previous behavior when the
624 * sysctl lock was held across a handler invocation,
625 * and is necessary for module unload correctness.
627 while (oidp->oid_running > 0) {
628 oidp->oid_kind |= CTLFLAG_DYING;
629 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
632 free(__DECONST(char *, oidp->oid_descr),
634 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
635 free(oidp, M_SYSCTLOID);
641 * Create new sysctls at run time.
642 * clist may point to a valid context initialized with sysctl_ctx_init().
645 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
646 int number, const char *name, int kind, void *arg1, intptr_t arg2,
647 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
649 struct sysctl_oid *oidp;
651 /* You have to hook up somewhere.. */
654 /* Check if the node already exists, otherwise create it */
656 oidp = sysctl_find_oidname(name, parent);
658 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
660 /* Update the context */
662 sysctl_ctx_entry_add(clist, oidp);
667 printf("can't re-use a leaf (%s)!\n", name);
671 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
672 oidp->oid_parent = parent;
673 SLIST_INIT(&oidp->oid_children);
674 oidp->oid_number = number;
675 oidp->oid_refcnt = 1;
676 oidp->oid_name = strdup(name, M_SYSCTLOID);
677 oidp->oid_handler = handler;
678 oidp->oid_kind = CTLFLAG_DYN | kind;
679 oidp->oid_arg1 = arg1;
680 oidp->oid_arg2 = arg2;
683 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
684 /* Update the context, if used */
686 sysctl_ctx_entry_add(clist, oidp);
687 /* Register this oid */
688 sysctl_register_oid(oidp);
694 * Rename an existing oid.
697 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
702 newname = strdup(name, M_SYSCTLOID);
704 oldname = __DECONST(char *, oidp->oid_name);
705 oidp->oid_name = newname;
707 free(oldname, M_SYSCTLOID);
711 * Reparent an existing oid.
714 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
716 struct sysctl_oid *oidp;
719 if (oid->oid_parent == parent) {
723 oidp = sysctl_find_oidname(oid->oid_name, parent);
728 sysctl_unregister_oid(oid);
729 oid->oid_parent = parent;
730 oid->oid_number = OID_AUTO;
731 sysctl_register_oid(oid);
737 * Register the kernel's oids on startup.
739 SET_DECLARE(sysctl_set, struct sysctl_oid);
742 sysctl_register_all(void *arg)
744 struct sysctl_oid **oidp;
746 sx_init(&sysctlmemlock, "sysctl mem");
749 SET_FOREACH(oidp, sysctl_set)
750 sysctl_register_oid(*oidp);
753 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
758 * These functions implement a presently undocumented interface
759 * used by the sysctl program to walk the tree, and get the type
760 * so it can print the value.
761 * This interface is under work and consideration, and should probably
762 * be killed with a big axe by the first person who can find the time.
763 * (be aware though, that the proper interface isn't as obvious as it
764 * may seem, there are various conflicting requirements.
766 * {0,0} printf the entire MIB-tree.
767 * {0,1,...} return the name of the "..." OID.
768 * {0,2,...} return the next OID.
769 * {0,3} return the OID of the name in "new"
770 * {0,4,...} return the kind & format info for the "..." OID.
771 * {0,5,...} return the description the "..." OID.
776 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
779 struct sysctl_oid *oidp;
781 SYSCTL_ASSERT_LOCKED();
782 SLIST_FOREACH(oidp, l, oid_link) {
787 printf("%d %s ", oidp->oid_number, oidp->oid_name);
790 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
791 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
793 if (oidp->oid_handler)
796 switch (oidp->oid_kind & CTLTYPE) {
799 if (!oidp->oid_handler) {
800 sysctl_sysctl_debug_dump_node(
801 SYSCTL_CHILDREN(oidp), i + 2);
804 case CTLTYPE_INT: printf(" Int\n"); break;
805 case CTLTYPE_UINT: printf(" u_int\n"); break;
806 case CTLTYPE_LONG: printf(" Long\n"); break;
807 case CTLTYPE_ULONG: printf(" u_long\n"); break;
808 case CTLTYPE_STRING: printf(" String\n"); break;
809 case CTLTYPE_U64: printf(" uint64_t\n"); break;
810 case CTLTYPE_S64: printf(" int64_t\n"); break;
811 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
812 default: printf("\n");
819 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
821 struct rm_priotracker tracker;
824 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
827 SYSCTL_RLOCK(&tracker);
828 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
829 SYSCTL_RUNLOCK(&tracker);
833 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
834 0, 0, sysctl_sysctl_debug, "-", "");
838 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
840 int *name = (int *) arg1;
841 u_int namelen = arg2;
843 struct sysctl_oid *oid;
844 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
845 struct rm_priotracker tracker;
848 SYSCTL_RLOCK(&tracker);
851 snprintf(buf,sizeof(buf),"%d",*name);
853 error = SYSCTL_OUT(req, ".", 1);
855 error = SYSCTL_OUT(req, buf, strlen(buf));
863 SLIST_FOREACH(oid, lsp, oid_link) {
864 if (oid->oid_number != *name)
868 error = SYSCTL_OUT(req, ".", 1);
870 error = SYSCTL_OUT(req, oid->oid_name,
871 strlen(oid->oid_name));
878 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
881 if (oid->oid_handler)
884 lsp2 = SYSCTL_CHILDREN(oid);
889 error = SYSCTL_OUT(req, "", 1);
891 SYSCTL_RUNLOCK(&tracker);
896 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
899 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
900 sysctl_sysctl_name, "");
903 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
904 int *next, int *len, int level, struct sysctl_oid **oidpp)
906 struct sysctl_oid *oidp;
908 SYSCTL_ASSERT_LOCKED();
910 SLIST_FOREACH(oidp, lsp, oid_link) {
911 *next = oidp->oid_number;
914 if (oidp->oid_kind & CTLFLAG_SKIP)
918 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
920 if (oidp->oid_handler)
921 /* We really should call the handler here...*/
923 lsp = SYSCTL_CHILDREN(oidp);
924 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
925 len, level+1, oidpp))
930 if (oidp->oid_number < *name)
933 if (oidp->oid_number > *name) {
934 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
936 if (oidp->oid_handler)
938 lsp = SYSCTL_CHILDREN(oidp);
939 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
940 next+1, len, level+1, oidpp))
944 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
947 if (oidp->oid_handler)
950 lsp = SYSCTL_CHILDREN(oidp);
951 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
952 len, level+1, oidpp))
963 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
965 int *name = (int *) arg1;
966 u_int namelen = arg2;
968 struct sysctl_oid *oid;
969 struct sysctl_oid_list *lsp = &sysctl__children;
970 struct rm_priotracker tracker;
971 int newoid[CTL_MAXNAME];
973 SYSCTL_RLOCK(&tracker);
974 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
975 SYSCTL_RUNLOCK(&tracker);
978 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
983 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
986 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
987 sysctl_sysctl_next, "");
990 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
992 struct sysctl_oid *oidp;
993 struct sysctl_oid_list *lsp = &sysctl__children;
996 SYSCTL_ASSERT_LOCKED();
998 for (*len = 0; *len < CTL_MAXNAME;) {
999 p = strsep(&name, ".");
1001 oidp = SLIST_FIRST(lsp);
1002 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1005 if (strcmp(p, oidp->oid_name) == 0)
1008 *oid++ = oidp->oid_number;
1011 if (name == NULL || *name == '\0') {
1017 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1020 if (oidp->oid_handler)
1023 lsp = SYSCTL_CHILDREN(oidp);
1029 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1032 int error, oid[CTL_MAXNAME], len = 0;
1033 struct sysctl_oid *op = 0;
1034 struct rm_priotracker tracker;
1038 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1039 return (ENAMETOOLONG);
1041 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1043 error = SYSCTL_IN(req, p, req->newlen);
1049 p [req->newlen] = '\0';
1051 SYSCTL_RLOCK(&tracker);
1052 error = name2oid(p, oid, &len, &op);
1053 SYSCTL_RUNLOCK(&tracker);
1060 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1065 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1068 SYSCTL_PROC(_sysctl, 3, name2oid,
1069 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1070 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1073 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1075 struct sysctl_oid *oid;
1076 struct rm_priotracker tracker;
1079 SYSCTL_RLOCK(&tracker);
1080 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1084 if (oid->oid_fmt == NULL) {
1088 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1091 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1093 SYSCTL_RUNLOCK(&tracker);
1098 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1099 sysctl_sysctl_oidfmt, "");
1102 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1104 struct sysctl_oid *oid;
1105 struct rm_priotracker tracker;
1108 SYSCTL_RLOCK(&tracker);
1109 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1113 if (oid->oid_descr == NULL) {
1117 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1119 SYSCTL_RUNLOCK(&tracker);
1123 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1124 sysctl_sysctl_oiddescr, "");
1127 * Default "handler" functions.
1131 * Handle an int, signed or unsigned.
1133 * a variable: point arg1 at it.
1134 * a constant: pass it in arg2.
1138 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1140 int tmpout, error = 0;
1143 * Attempt to get a coherent snapshot by making a copy of the data.
1146 tmpout = *(int *)arg1;
1149 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1151 if (error || !req->newptr)
1157 error = SYSCTL_IN(req, arg1, sizeof(int));
1162 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1163 * Note: this is used by TCP.
1167 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1172 s = (int)((int64_t)tt * 1000 / hz);
1174 error = sysctl_handle_int(oidp, &s, 0, req);
1175 if (error || !req->newptr)
1178 tt = (int)((int64_t)s * hz / 1000);
1188 * Handle a long, signed or unsigned.
1190 * a variable: point arg1 at it.
1191 * a constant: pass it in arg2.
1195 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1204 * Attempt to get a coherent snapshot by making a copy of the data.
1207 tmplong = *(long *)arg1;
1211 if (req->flags & SCTL_MASK32) {
1213 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1216 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1218 if (error || !req->newptr)
1224 else if (req->flags & SCTL_MASK32) {
1225 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1226 *(long *)arg1 = (long)tmpint;
1230 error = SYSCTL_IN(req, arg1, sizeof(long));
1235 * Handle a 64 bit int, signed or unsigned.
1237 * a variable: point arg1 at it.
1238 * a constant: pass it in arg2.
1241 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1247 * Attempt to get a coherent snapshot by making a copy of the data.
1250 tmpout = *(uint64_t *)arg1;
1253 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1255 if (error || !req->newptr)
1261 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1266 * Handle our generic '\0' terminated 'C' string.
1268 * a variable string: point arg1 at it, arg2 is max length.
1269 * a constant string: point arg1 at it, arg2 is zero.
1273 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1276 int error = 0, ro_string = 0;
1279 * A zero-length buffer indicates a fixed size read-only
1283 arg2 = strlen((char *)arg1) + 1;
1287 if (req->oldptr != NULL) {
1293 /* try to make a coherent snapshot of the string */
1294 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1295 memcpy(tmparg, arg1, arg2);
1298 outlen = strnlen(tmparg, arg2 - 1) + 1;
1299 error = SYSCTL_OUT(req, tmparg, outlen);
1302 free(tmparg, M_SYSCTLTMP);
1304 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1305 error = SYSCTL_OUT(req, NULL, outlen);
1307 if (error || !req->newptr)
1310 if ((req->newlen - req->newidx) >= arg2) {
1313 arg2 = (req->newlen - req->newidx);
1314 error = SYSCTL_IN(req, arg1, arg2);
1315 ((char *)arg1)[arg2] = '\0';
1321 * Handle any kind of opaque data.
1322 * arg1 points to it, arg2 is the size.
1326 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1330 struct sysctl_req req2;
1333 * Attempt to get a coherent snapshot, by using the thread
1334 * pre-emption counter updated from within mi_switch() to
1335 * determine if we were pre-empted during a bcopy() or
1336 * copyout(). Make 3 attempts at doing this before giving up.
1337 * If we encounter an error, stop immediately.
1342 generation = curthread->td_generation;
1343 error = SYSCTL_OUT(req, arg1, arg2);
1347 if (generation != curthread->td_generation && tries < 3) {
1352 error = SYSCTL_IN(req, arg1, arg2);
1358 * Transfer functions to/from kernel space.
1359 * XXX: rather untested at this point
1362 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1368 if (req->oldlen <= req->oldidx)
1371 if (i > req->oldlen - req->oldidx)
1372 i = req->oldlen - req->oldidx;
1374 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1377 if (req->oldptr && i != l)
1383 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1387 if (req->newlen - req->newidx < l)
1389 bcopy((char *)req->newptr + req->newidx, p, l);
1395 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1396 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1399 struct sysctl_req req;
1401 bzero(&req, sizeof req);
1407 req.oldlen = *oldlenp;
1409 req.validlen = req.oldlen;
1416 req.newlen = newlen;
1420 req.oldfunc = sysctl_old_kernel;
1421 req.newfunc = sysctl_new_kernel;
1422 req.lock = REQ_UNWIRED;
1424 error = sysctl_root(0, name, namelen, &req);
1426 if (req.lock == REQ_WIRED && req.validlen > 0)
1427 vsunlock(req.oldptr, req.validlen);
1429 if (error && error != ENOMEM)
1433 if (req.oldptr && req.oldidx > req.validlen)
1434 *retval = req.validlen;
1436 *retval = req.oldidx;
1442 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1443 void *new, size_t newlen, size_t *retval, int flags)
1445 int oid[CTL_MAXNAME];
1446 size_t oidlen, plen;
1449 oid[0] = 0; /* sysctl internal magic */
1450 oid[1] = 3; /* name2oid */
1451 oidlen = sizeof(oid);
1453 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1454 (void *)name, strlen(name), &plen, flags);
1458 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1459 new, newlen, retval, flags);
1464 * Transfer function to/from user space.
1467 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1469 size_t i, len, origidx;
1472 origidx = req->oldidx;
1474 if (req->oldptr == NULL)
1477 * If we have not wired the user supplied buffer and we are currently
1478 * holding locks, drop a witness warning, as it's possible that
1479 * write operations to the user page can sleep.
1481 if (req->lock != REQ_WIRED)
1482 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1483 "sysctl_old_user()");
1485 len = req->validlen;
1489 if (i > len - origidx)
1491 if (req->lock == REQ_WIRED) {
1492 error = copyout_nofault(p, (char *)req->oldptr +
1495 error = copyout(p, (char *)req->oldptr + origidx, i);
1505 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1511 if (req->newlen - req->newidx < l)
1513 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1514 "sysctl_new_user()");
1515 error = copyin((char *)req->newptr + req->newidx, p, l);
1521 * Wire the user space destination buffer. If set to a value greater than
1522 * zero, the len parameter limits the maximum amount of wired memory.
1525 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1530 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1532 if (req->lock != REQ_WIRED && req->oldptr &&
1533 req->oldfunc == sysctl_old_user) {
1534 if (wiredlen != 0) {
1535 ret = vslock(req->oldptr, wiredlen);
1542 req->lock = REQ_WIRED;
1543 req->validlen = wiredlen;
1549 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1550 int *nindx, struct sysctl_req *req)
1552 struct sysctl_oid_list *lsp;
1553 struct sysctl_oid *oid;
1556 SYSCTL_ASSERT_LOCKED();
1557 lsp = &sysctl__children;
1559 while (indx < CTL_MAXNAME) {
1560 SLIST_FOREACH(oid, lsp, oid_link) {
1561 if (oid->oid_number == name[indx])
1568 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1569 if (oid->oid_handler != NULL || indx == namelen) {
1573 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1574 ("%s found DYING node %p", __func__, oid));
1577 lsp = SYSCTL_CHILDREN(oid);
1578 } else if (indx == namelen) {
1582 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1583 ("%s found DYING node %p", __func__, oid));
1593 * Traverse our tree, and find the right node, execute whatever it points
1594 * to, and return the resulting error code.
1598 sysctl_root(SYSCTL_HANDLER_ARGS)
1600 struct sysctl_oid *oid;
1601 struct rm_priotracker tracker;
1602 int error, indx, lvl;
1604 SYSCTL_RLOCK(&tracker);
1606 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1610 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1612 * You can't call a sysctl when it's a node, but has
1613 * no handler. Inform the user that it's a node.
1614 * The indx may or may not be the same as namelen.
1616 if (oid->oid_handler == NULL) {
1622 /* Is this sysctl writable? */
1623 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1628 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1630 #ifdef CAPABILITY_MODE
1632 * If the process is in capability mode, then don't permit reading or
1633 * writing unless specifically granted for the node.
1635 if (IN_CAPABILITY_MODE(req->td)) {
1636 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1637 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1644 /* Is this sysctl sensitive to securelevels? */
1645 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1646 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1647 error = securelevel_gt(req->td->td_ucred, lvl);
1652 /* Is this sysctl writable by only privileged users? */
1653 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1656 if (oid->oid_kind & CTLFLAG_PRISON)
1657 priv = PRIV_SYSCTL_WRITEJAIL;
1659 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1660 prison_owns_vnet(req->td->td_ucred))
1661 priv = PRIV_SYSCTL_WRITEJAIL;
1664 priv = PRIV_SYSCTL_WRITE;
1665 error = priv_check(req->td, priv);
1670 if (!oid->oid_handler) {
1675 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1676 arg1 = (int *)arg1 + indx;
1679 arg1 = oid->oid_arg1;
1680 arg2 = oid->oid_arg2;
1683 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1689 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1690 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1692 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
1694 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1697 SYSCTL_RUNLOCK(&tracker);
1701 #ifndef _SYS_SYSPROTO_H_
1702 struct sysctl_args {
1712 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1714 int error, i, name[CTL_MAXNAME];
1717 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1720 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1724 error = userland_sysctl(td, name, uap->namelen,
1725 uap->old, uap->oldlenp, 0,
1726 uap->new, uap->newlen, &j, 0);
1727 if (error && error != ENOMEM)
1730 i = copyout(&j, uap->oldlenp, sizeof(j));
1738 * This is used from various compatibility syscalls too. That's why name
1739 * must be in kernel space.
1742 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1743 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1746 int error = 0, memlocked;
1747 struct sysctl_req req;
1749 bzero(&req, sizeof req);
1756 req.oldlen = *oldlenp;
1758 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1763 req.validlen = req.oldlen;
1766 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1772 if (!useracc(new, newlen, VM_PROT_READ))
1774 req.newlen = newlen;
1778 req.oldfunc = sysctl_old_user;
1779 req.newfunc = sysctl_new_user;
1780 req.lock = REQ_UNWIRED;
1783 if (KTRPOINT(curthread, KTR_SYSCTL))
1784 ktrsysctl(name, namelen);
1787 if (req.oldptr && req.oldlen > PAGE_SIZE) {
1789 sx_xlock(&sysctlmemlock);
1792 CURVNET_SET(TD_TO_VNET(td));
1797 error = sysctl_root(0, name, namelen, &req);
1798 if (error != EAGAIN)
1800 kern_yield(PRI_USER);
1805 if (req.lock == REQ_WIRED && req.validlen > 0)
1806 vsunlock(req.oldptr, req.validlen);
1808 sx_xunlock(&sysctlmemlock);
1810 if (error && error != ENOMEM)
1814 if (req.oldptr && req.oldidx > req.validlen)
1815 *retval = req.validlen;
1817 *retval = req.oldidx;
1823 * Drain into a sysctl struct. The user buffer should be wired if a page
1824 * fault would cause issue.
1827 sbuf_sysctl_drain(void *arg, const char *data, int len)
1829 struct sysctl_req *req = arg;
1832 error = SYSCTL_OUT(req, data, len);
1833 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1834 return (error == 0 ? len : -error);
1838 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1839 struct sysctl_req *req)
1842 /* Supply a default buffer size if none given. */
1843 if (buf == NULL && length == 0)
1845 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
1846 sbuf_set_drain(s, sbuf_sysctl_drain, req);