2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Mike Karels at Berkeley Software Design, Inc.
10 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
11 * project, to make these variables more userfriendly.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "opt_capsicum.h"
45 #include "opt_ktrace.h"
47 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/capsicum.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/sysctl.h>
54 #include <sys/malloc.h>
60 #include <sys/mutex.h>
61 #include <sys/rmlock.h>
64 #include <sys/sysproto.h>
67 #include <sys/ktrace.h>
72 #include <ddb/db_lex.h>
77 #include <security/mac/mac_framework.h>
80 #include <vm/vm_extern.h>
82 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
83 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
84 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
87 * The sysctllock protects the MIB tree. It also protects sysctl
88 * contexts used with dynamic sysctls. The sysctl_register_oid() and
89 * sysctl_unregister_oid() routines require the sysctllock to already
90 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
91 * provided for the few places in the kernel which need to use that
92 * API rather than using the dynamic API. Use of the dynamic API is
93 * strongly encouraged for most code.
95 * The sysctlmemlock is used to limit the amount of user memory wired for
96 * sysctl requests. This is implemented by serializing any userland
97 * sysctl requests larger than a single page via an exclusive lock.
99 static struct rmlock sysctllock;
100 static struct sx __exclusive_cache_line sysctlmemlock;
102 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
103 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
104 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
105 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
106 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
107 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
108 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
109 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
110 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
112 #define SYSCTL_SLEEP(ch, wmesg, timo) \
113 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
115 static int sysctl_root(SYSCTL_HANDLER_ARGS);
118 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
120 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
122 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
123 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
125 static struct sysctl_oid *
126 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
128 struct sysctl_oid *oidp;
130 SYSCTL_ASSERT_LOCKED();
131 SLIST_FOREACH(oidp, list, oid_link) {
132 if (strcmp(oidp->oid_name, name) == 0) {
140 * Initialization of the MIB tree.
142 * Order by number in each list.
159 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
160 struct sysctl_req *req, struct rm_priotracker *tracker)
164 if (oid->oid_kind & CTLFLAG_DYN)
165 atomic_add_int(&oid->oid_running, 1);
168 SYSCTL_RUNLOCK(tracker);
172 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
174 error = oid->oid_handler(oid, arg1, arg2, req);
175 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
178 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
181 SYSCTL_RLOCK(tracker);
185 if (oid->oid_kind & CTLFLAG_DYN) {
186 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
187 (oid->oid_kind & CTLFLAG_DYING) != 0)
188 wakeup(&oid->oid_running);
195 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
197 struct sysctl_req req;
198 struct sysctl_oid *curr;
201 ssize_t rem = sizeof(path);
203 uint8_t data[512] __aligned(sizeof(uint64_t));
209 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
210 len = strlen(curr->oid_name);
215 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
218 memcpy(path + rem, curr->oid_name, len);
220 path[rem + len] = '.';
223 memset(&req, 0, sizeof(req));
226 req.oldfunc = sysctl_old_kernel;
227 req.newfunc = sysctl_new_kernel;
228 req.lock = REQ_UNWIRED;
230 switch (oidp->oid_kind & CTLTYPE) {
232 if (getenv_array(path + rem, data, sizeof(data), &size,
233 sizeof(int), GETENV_SIGNED) == 0)
239 if (getenv_array(path + rem, data, sizeof(data), &size,
240 sizeof(int), GETENV_UNSIGNED) == 0)
246 if (getenv_array(path + rem, data, sizeof(data), &size,
247 sizeof(long), GETENV_SIGNED) == 0)
253 if (getenv_array(path + rem, data, sizeof(data), &size,
254 sizeof(long), GETENV_UNSIGNED) == 0)
260 if (getenv_array(path + rem, data, sizeof(data), &size,
261 sizeof(int8_t), GETENV_SIGNED) == 0)
267 if (getenv_array(path + rem, data, sizeof(data), &size,
268 sizeof(int16_t), GETENV_SIGNED) == 0)
274 if (getenv_array(path + rem, data, sizeof(data), &size,
275 sizeof(int32_t), GETENV_SIGNED) == 0)
281 if (getenv_array(path + rem, data, sizeof(data), &size,
282 sizeof(int64_t), GETENV_SIGNED) == 0)
288 if (getenv_array(path + rem, data, sizeof(data), &size,
289 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
295 if (getenv_array(path + rem, data, sizeof(data), &size,
296 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
302 if (getenv_array(path + rem, data, sizeof(data), &size,
303 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
309 if (getenv_array(path + rem, data, sizeof(data), &size,
310 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
316 penv = kern_getenv(path + rem);
319 req.newlen = strlen(penv);
325 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
326 oidp->oid_arg2, &req, NULL);
328 printf("Setting sysctl %s failed: %d\n", path + rem, error);
334 * Locate the path to a given oid. Returns the length of the resulting path,
335 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
336 * elements and be NULL initialized.
339 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
343 SYSCTL_ASSERT_LOCKED();
345 while (indx < CTL_MAXNAME && indx >= 0) {
346 if (nodes[indx] == NULL && indx == 0)
347 nodes[indx] = SLIST_FIRST(&sysctl__children);
348 else if (nodes[indx] == NULL)
349 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
351 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
353 if (nodes[indx] == needle)
356 if (nodes[indx] == NULL) {
361 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
370 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
372 struct sysctl_oid *nodes[CTL_MAXNAME];
377 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
378 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
380 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
382 memset(nodes, 0, sizeof(nodes));
383 rc = sysctl_search_oid(nodes, leaf);
385 for (i = 0; i < rc; i++)
386 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
389 sbuf_printf(&sb, "%s", leaf->oid_name);
391 sbuf_printf(&sb, ")!\n");
393 (void)sbuf_finish(&sb);
398 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
400 struct rm_priotracker tracker;
402 SYSCTL_RLOCK(&tracker);
403 sysctl_warn_reuse(__func__, oidp);
404 SYSCTL_RUNLOCK(&tracker);
407 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
408 0, 0, sysctl_reuse_test, "-", "");
412 sysctl_register_oid(struct sysctl_oid *oidp)
414 struct sysctl_oid_list *parent = oidp->oid_parent;
415 struct sysctl_oid *p;
416 struct sysctl_oid *q;
421 * First check if another oid with the same name already
422 * exists in the parent's list.
424 SYSCTL_ASSERT_WLOCKED();
425 p = sysctl_find_oidname(oidp->oid_name, parent);
427 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
431 sysctl_warn_reuse(__func__, p);
435 /* get current OID number */
436 oid_number = oidp->oid_number;
439 #error "OID_AUTO is expected to be a negative value"
442 * Any negative OID number qualifies as OID_AUTO. Valid OID
443 * numbers should always be positive.
445 * NOTE: DO NOT change the starting value here, change it in
446 * <sys/sysctl.h>, and make sure it is at least 256 to
447 * accommodate e.g. net.inet.raw as a static sysctl node.
449 if (oid_number < 0) {
453 * By decrementing the next OID number we spend less
454 * time inserting the OIDs into a sorted list.
456 if (--newoid < CTL_AUTO_START)
463 * Insert the OID into the parent's list sorted by OID number.
467 SLIST_FOREACH(p, parent, oid_link) {
468 /* check if the current OID number is in use */
469 if (oid_number == p->oid_number) {
470 /* get the next valid OID number */
471 if (oid_number < CTL_AUTO_START ||
472 oid_number == 0x7fffffff) {
473 /* wraparound - restart */
474 oid_number = CTL_AUTO_START;
475 /* don't loop forever */
477 panic("sysctl: Out of OID numbers\n");
482 } else if (oid_number < p->oid_number)
486 /* check for non-auto OID number collision */
487 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
488 oid_number >= CTL_AUTO_START) {
489 printf("sysctl: OID number(%d) is already in use for '%s'\n",
490 oidp->oid_number, oidp->oid_name);
492 /* update the OID number, if any */
493 oidp->oid_number = oid_number;
495 SLIST_INSERT_AFTER(q, oidp, oid_link);
497 SLIST_INSERT_HEAD(parent, oidp, oid_link);
499 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
501 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
503 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
504 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
505 /* only fetch value once */
506 oidp->oid_kind |= CTLFLAG_NOFETCH;
507 /* try to fetch value from kernel environment */
508 sysctl_load_tunable_by_oid_locked(oidp);
513 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
517 * Mark the leaf as dormant if it's not to be immediately enabled.
518 * We do not disable nodes as they can be shared between modules
519 * and it is always safe to access a node.
521 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
522 ("internal flag is set in oid_kind"));
523 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
524 oidp->oid_kind |= CTLFLAG_DORMANT;
525 sysctl_register_oid(oidp);
529 sysctl_enable_oid(struct sysctl_oid *oidp)
532 SYSCTL_ASSERT_WLOCKED();
533 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
534 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
535 ("sysctl node is marked as dormant"));
538 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
539 ("enabling already enabled sysctl oid"));
540 oidp->oid_kind &= ~CTLFLAG_DORMANT;
544 sysctl_unregister_oid(struct sysctl_oid *oidp)
546 struct sysctl_oid *p;
549 SYSCTL_ASSERT_WLOCKED();
550 if (oidp->oid_number == OID_AUTO) {
554 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
556 SLIST_REMOVE(oidp->oid_parent, oidp,
557 sysctl_oid, oid_link);
565 * This can happen when a module fails to register and is
566 * being unloaded afterwards. It should not be a panic()
570 printf("%s: failed(%d) to unregister sysctl(%s)\n",
571 __func__, error, oidp->oid_name);
575 /* Initialize a new context to keep track of dynamically added sysctls. */
577 sysctl_ctx_init(struct sysctl_ctx_list *c)
585 * No locking here, the caller is responsible for not adding
586 * new nodes to a context until after this function has
593 /* Free the context, and destroy all dynamic oids registered in this context */
595 sysctl_ctx_free(struct sysctl_ctx_list *clist)
597 struct sysctl_ctx_entry *e, *e1;
602 * First perform a "dry run" to check if it's ok to remove oids.
604 * XXX This algorithm is a hack. But I don't know any
605 * XXX better solution for now...
608 TAILQ_FOREACH(e, clist, link) {
609 error = sysctl_remove_oid_locked(e->entry, 0, 0);
614 * Restore deregistered entries, either from the end,
615 * or from the place where error occurred.
616 * e contains the entry that was not unregistered
619 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
621 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
623 sysctl_register_oid(e1->entry);
624 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
630 /* Now really delete the entries */
631 e = TAILQ_FIRST(clist);
633 e1 = TAILQ_NEXT(e, link);
634 error = sysctl_remove_oid_locked(e->entry, 1, 0);
636 panic("sysctl_remove_oid: corrupt tree, entry: %s",
638 free(e, M_SYSCTLOID);
645 /* Add an entry to the context */
646 struct sysctl_ctx_entry *
647 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
649 struct sysctl_ctx_entry *e;
651 SYSCTL_ASSERT_WLOCKED();
652 if (clist == NULL || oidp == NULL)
654 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
656 TAILQ_INSERT_HEAD(clist, e, link);
660 /* Find an entry in the context */
661 struct sysctl_ctx_entry *
662 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
664 struct sysctl_ctx_entry *e;
666 SYSCTL_ASSERT_WLOCKED();
667 if (clist == NULL || oidp == NULL)
669 TAILQ_FOREACH(e, clist, link) {
677 * Delete an entry from the context.
678 * NOTE: this function doesn't free oidp! You have to remove it
679 * with sysctl_remove_oid().
682 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
684 struct sysctl_ctx_entry *e;
686 if (clist == NULL || oidp == NULL)
689 e = sysctl_ctx_entry_find(clist, oidp);
691 TAILQ_REMOVE(clist, e, link);
693 free(e, M_SYSCTLOID);
702 * Remove dynamically created sysctl trees.
703 * oidp - top of the tree to be removed
704 * del - if 0 - just deregister, otherwise free up entries as well
705 * recurse - if != 0 traverse the subtree to be deleted
708 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
713 error = sysctl_remove_oid_locked(oidp, del, recurse);
719 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
720 int del, int recurse)
722 struct sysctl_oid *p, *tmp;
727 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
728 if (strcmp(p->oid_name, name) == 0) {
729 error = sysctl_remove_oid_locked(p, del, recurse);
740 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
742 struct sysctl_oid *p, *tmp;
745 SYSCTL_ASSERT_WLOCKED();
748 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
749 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
754 * WARNING: normal method to do this should be through
755 * sysctl_ctx_free(). Use recursing as the last resort
756 * method to purge your sysctl tree of leftovers...
757 * However, if some other code still references these nodes,
760 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
761 if (oidp->oid_refcnt == 1) {
762 SLIST_FOREACH_SAFE(p,
763 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
765 printf("Warning: failed attempt to "
766 "remove oid %s with child %s\n",
767 oidp->oid_name, p->oid_name);
770 error = sysctl_remove_oid_locked(p, del,
777 if (oidp->oid_refcnt > 1 ) {
780 if (oidp->oid_refcnt == 0) {
781 printf("Warning: bad oid_refcnt=%u (%s)!\n",
782 oidp->oid_refcnt, oidp->oid_name);
785 sysctl_unregister_oid(oidp);
788 * Wait for all threads running the handler to drain.
789 * This preserves the previous behavior when the
790 * sysctl lock was held across a handler invocation,
791 * and is necessary for module unload correctness.
793 while (oidp->oid_running > 0) {
794 oidp->oid_kind |= CTLFLAG_DYING;
795 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
798 free(__DECONST(char *, oidp->oid_descr),
801 free(__DECONST(char *, oidp->oid_label),
803 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
804 free(oidp, M_SYSCTLOID);
810 * Create new sysctls at run time.
811 * clist may point to a valid context initialized with sysctl_ctx_init().
814 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
815 int number, const char *name, int kind, void *arg1, intmax_t arg2,
816 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
819 struct sysctl_oid *oidp;
821 /* You have to hook up somewhere.. */
824 /* Check if the node already exists, otherwise create it */
826 oidp = sysctl_find_oidname(name, parent);
828 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
830 /* Update the context */
832 sysctl_ctx_entry_add(clist, oidp);
836 sysctl_warn_reuse(__func__, oidp);
841 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
842 oidp->oid_parent = parent;
843 SLIST_INIT(&oidp->oid_children);
844 oidp->oid_number = number;
845 oidp->oid_refcnt = 1;
846 oidp->oid_name = strdup(name, M_SYSCTLOID);
847 oidp->oid_handler = handler;
848 oidp->oid_kind = CTLFLAG_DYN | kind;
849 oidp->oid_arg1 = arg1;
850 oidp->oid_arg2 = arg2;
853 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
855 oidp->oid_label = strdup(label, M_SYSCTLOID);
856 /* Update the context, if used */
858 sysctl_ctx_entry_add(clist, oidp);
859 /* Register this oid */
860 sysctl_register_oid(oidp);
866 * Rename an existing oid.
869 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
874 newname = strdup(name, M_SYSCTLOID);
876 oldname = __DECONST(char *, oidp->oid_name);
877 oidp->oid_name = newname;
879 free(oldname, M_SYSCTLOID);
883 * Reparent an existing oid.
886 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
888 struct sysctl_oid *oidp;
891 if (oid->oid_parent == parent) {
895 oidp = sysctl_find_oidname(oid->oid_name, parent);
900 sysctl_unregister_oid(oid);
901 oid->oid_parent = parent;
902 oid->oid_number = OID_AUTO;
903 sysctl_register_oid(oid);
909 * Register the kernel's oids on startup.
911 SET_DECLARE(sysctl_set, struct sysctl_oid);
914 sysctl_register_all(void *arg)
916 struct sysctl_oid **oidp;
918 sx_init(&sysctlmemlock, "sysctl mem");
921 SET_FOREACH(oidp, sysctl_set)
922 sysctl_register_oid(*oidp);
925 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
930 * These functions implement a presently undocumented interface
931 * used by the sysctl program to walk the tree, and get the type
932 * so it can print the value.
933 * This interface is under work and consideration, and should probably
934 * be killed with a big axe by the first person who can find the time.
935 * (be aware though, that the proper interface isn't as obvious as it
936 * may seem, there are various conflicting requirements.
938 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
939 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
941 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID.
942 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
944 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
946 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
948 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
954 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
957 struct sysctl_oid *oidp;
959 SYSCTL_ASSERT_LOCKED();
960 SLIST_FOREACH(oidp, l, oid_link) {
965 printf("%d %s ", oidp->oid_number, oidp->oid_name);
968 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
969 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
971 if (oidp->oid_handler)
974 switch (oidp->oid_kind & CTLTYPE) {
977 if (!oidp->oid_handler) {
978 sysctl_sysctl_debug_dump_node(
979 SYSCTL_CHILDREN(oidp), i + 2);
982 case CTLTYPE_INT: printf(" Int\n"); break;
983 case CTLTYPE_UINT: printf(" u_int\n"); break;
984 case CTLTYPE_LONG: printf(" Long\n"); break;
985 case CTLTYPE_ULONG: printf(" u_long\n"); break;
986 case CTLTYPE_STRING: printf(" String\n"); break;
987 case CTLTYPE_S8: printf(" int8_t\n"); break;
988 case CTLTYPE_S16: printf(" int16_t\n"); break;
989 case CTLTYPE_S32: printf(" int32_t\n"); break;
990 case CTLTYPE_S64: printf(" int64_t\n"); break;
991 case CTLTYPE_U8: printf(" uint8_t\n"); break;
992 case CTLTYPE_U16: printf(" uint16_t\n"); break;
993 case CTLTYPE_U32: printf(" uint32_t\n"); break;
994 case CTLTYPE_U64: printf(" uint64_t\n"); break;
995 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
996 default: printf("\n");
1003 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1005 struct rm_priotracker tracker;
1008 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1011 SYSCTL_RLOCK(&tracker);
1012 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1013 SYSCTL_RUNLOCK(&tracker);
1017 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1018 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1022 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1024 int *name = (int *) arg1;
1025 u_int namelen = arg2;
1027 struct sysctl_oid *oid;
1028 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1029 struct rm_priotracker tracker;
1032 error = sysctl_wire_old_buffer(req, 0);
1036 SYSCTL_RLOCK(&tracker);
1039 snprintf(buf,sizeof(buf),"%d",*name);
1041 error = SYSCTL_OUT(req, ".", 1);
1043 error = SYSCTL_OUT(req, buf, strlen(buf));
1051 SLIST_FOREACH(oid, lsp, oid_link) {
1052 if (oid->oid_number != *name)
1056 error = SYSCTL_OUT(req, ".", 1);
1058 error = SYSCTL_OUT(req, oid->oid_name,
1059 strlen(oid->oid_name));
1066 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1069 if (oid->oid_handler)
1072 lsp2 = SYSCTL_CHILDREN(oid);
1077 error = SYSCTL_OUT(req, "", 1);
1079 SYSCTL_RUNLOCK(&tracker);
1084 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1087 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1088 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1091 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1092 int *next, int *len, int level, struct sysctl_oid **oidpp)
1094 struct sysctl_oid *oidp;
1096 SYSCTL_ASSERT_LOCKED();
1098 SLIST_FOREACH(oidp, lsp, oid_link) {
1099 *next = oidp->oid_number;
1102 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1106 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1108 if (oidp->oid_handler)
1109 /* We really should call the handler here...*/
1111 lsp = SYSCTL_CHILDREN(oidp);
1112 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1113 len, level+1, oidpp))
1118 if (oidp->oid_number < *name)
1121 if (oidp->oid_number > *name) {
1122 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1124 if (oidp->oid_handler)
1126 lsp = SYSCTL_CHILDREN(oidp);
1127 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1128 next+1, len, level+1, oidpp))
1132 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1135 if (oidp->oid_handler)
1138 lsp = SYSCTL_CHILDREN(oidp);
1139 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1140 len, level+1, oidpp))
1151 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1153 int *name = (int *) arg1;
1154 u_int namelen = arg2;
1156 struct sysctl_oid *oid;
1157 struct sysctl_oid_list *lsp = &sysctl__children;
1158 struct rm_priotracker tracker;
1159 int newoid[CTL_MAXNAME];
1161 SYSCTL_RLOCK(&tracker);
1162 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1163 SYSCTL_RUNLOCK(&tracker);
1166 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1171 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1174 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1175 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1178 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1180 struct sysctl_oid *oidp;
1181 struct sysctl_oid_list *lsp = &sysctl__children;
1184 SYSCTL_ASSERT_LOCKED();
1186 for (*len = 0; *len < CTL_MAXNAME;) {
1187 p = strsep(&name, ".");
1189 oidp = SLIST_FIRST(lsp);
1190 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1193 if (strcmp(p, oidp->oid_name) == 0)
1196 *oid++ = oidp->oid_number;
1199 if (name == NULL || *name == '\0') {
1205 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1208 if (oidp->oid_handler)
1211 lsp = SYSCTL_CHILDREN(oidp);
1217 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1220 int error, oid[CTL_MAXNAME], len = 0;
1221 struct sysctl_oid *op = NULL;
1222 struct rm_priotracker tracker;
1227 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1228 return (ENAMETOOLONG);
1231 if (req->newlen >= sizeof(buf))
1232 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1234 error = SYSCTL_IN(req, p, req->newlen);
1241 p [req->newlen] = '\0';
1243 SYSCTL_RLOCK(&tracker);
1244 error = name2oid(p, oid, &len, &op);
1245 SYSCTL_RUNLOCK(&tracker);
1253 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1258 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1261 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1262 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1263 sysctl_sysctl_name2oid, "I", "");
1266 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1268 struct sysctl_oid *oid;
1269 struct rm_priotracker tracker;
1272 error = sysctl_wire_old_buffer(req, 0);
1276 SYSCTL_RLOCK(&tracker);
1277 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1281 if (oid->oid_fmt == NULL) {
1285 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1288 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1290 SYSCTL_RUNLOCK(&tracker);
1295 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1296 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1299 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1301 struct sysctl_oid *oid;
1302 struct rm_priotracker tracker;
1305 error = sysctl_wire_old_buffer(req, 0);
1309 SYSCTL_RLOCK(&tracker);
1310 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1314 if (oid->oid_descr == NULL) {
1318 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1320 SYSCTL_RUNLOCK(&tracker);
1324 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1325 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1328 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1330 struct sysctl_oid *oid;
1331 struct rm_priotracker tracker;
1334 error = sysctl_wire_old_buffer(req, 0);
1338 SYSCTL_RLOCK(&tracker);
1339 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1343 if (oid->oid_label == NULL) {
1347 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1349 SYSCTL_RUNLOCK(&tracker);
1353 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1354 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1357 * Default "handler" functions.
1363 * a variable: point arg1 at it.
1364 * a constant: pass it in arg2.
1368 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1374 * Attempt to get a coherent snapshot by making a copy of the data.
1377 temp = *(bool *)arg1 ? 1 : 0;
1379 temp = arg2 ? 1 : 0;
1381 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1382 if (error || !req->newptr)
1388 error = SYSCTL_IN(req, &temp, sizeof(temp));
1390 *(bool *)arg1 = temp ? 1 : 0;
1396 * Handle an int8_t, signed or unsigned.
1398 * a variable: point arg1 at it.
1399 * a constant: pass it in arg2.
1403 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1409 * Attempt to get a coherent snapshot by making a copy of the data.
1412 tmpout = *(int8_t *)arg1;
1415 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1417 if (error || !req->newptr)
1423 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1428 * Handle an int16_t, signed or unsigned.
1430 * a variable: point arg1 at it.
1431 * a constant: pass it in arg2.
1435 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1441 * Attempt to get a coherent snapshot by making a copy of the data.
1444 tmpout = *(int16_t *)arg1;
1447 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1449 if (error || !req->newptr)
1455 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1460 * Handle an int32_t, signed or unsigned.
1462 * a variable: point arg1 at it.
1463 * a constant: pass it in arg2.
1467 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1473 * Attempt to get a coherent snapshot by making a copy of the data.
1476 tmpout = *(int32_t *)arg1;
1479 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1481 if (error || !req->newptr)
1487 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1492 * Handle an int, signed or unsigned.
1494 * a variable: point arg1 at it.
1495 * a constant: pass it in arg2.
1499 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1501 int tmpout, error = 0;
1504 * Attempt to get a coherent snapshot by making a copy of the data.
1507 tmpout = *(int *)arg1;
1510 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1512 if (error || !req->newptr)
1518 error = SYSCTL_IN(req, arg1, sizeof(int));
1523 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1524 * Note: this is used by TCP.
1528 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1533 s = (int)((int64_t)tt * 1000 / hz);
1535 error = sysctl_handle_int(oidp, &s, 0, req);
1536 if (error || !req->newptr)
1539 tt = (int)((int64_t)s * hz / 1000);
1549 * Handle a long, signed or unsigned.
1551 * a variable: point arg1 at it.
1552 * a constant: pass it in arg2.
1556 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1565 * Attempt to get a coherent snapshot by making a copy of the data.
1568 tmplong = *(long *)arg1;
1572 if (req->flags & SCTL_MASK32) {
1574 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1577 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1579 if (error || !req->newptr)
1585 else if (req->flags & SCTL_MASK32) {
1586 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1587 *(long *)arg1 = (long)tmpint;
1591 error = SYSCTL_IN(req, arg1, sizeof(long));
1596 * Handle a 64 bit int, signed or unsigned.
1598 * a variable: point arg1 at it.
1599 * a constant: pass it in arg2.
1602 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1608 * Attempt to get a coherent snapshot by making a copy of the data.
1611 tmpout = *(uint64_t *)arg1;
1614 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1616 if (error || !req->newptr)
1622 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1627 * Handle our generic '\0' terminated 'C' string.
1629 * a variable string: point arg1 at it, arg2 is max length.
1630 * a constant string: point arg1 at it, arg2 is zero.
1634 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1637 int error = 0, ro_string = 0;
1640 * A zero-length buffer indicates a fixed size read-only
1641 * string. In ddb, don't worry about trying to make a malloced
1644 if (arg2 == 0 || kdb_active) {
1645 arg2 = strlen((char *)arg1) + 1;
1649 if (req->oldptr != NULL) {
1655 /* try to make a coherent snapshot of the string */
1656 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1657 memcpy(tmparg, arg1, arg2);
1660 outlen = strnlen(tmparg, arg2 - 1) + 1;
1661 error = SYSCTL_OUT(req, tmparg, outlen);
1664 free(tmparg, M_SYSCTLTMP);
1666 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1667 error = SYSCTL_OUT(req, NULL, outlen);
1669 if (error || !req->newptr)
1672 if ((req->newlen - req->newidx) >= arg2) {
1675 arg2 = (req->newlen - req->newidx);
1676 error = SYSCTL_IN(req, arg1, arg2);
1677 ((char *)arg1)[arg2] = '\0';
1683 * Handle any kind of opaque data.
1684 * arg1 points to it, arg2 is the size.
1688 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1692 struct sysctl_req req2;
1695 * Attempt to get a coherent snapshot, by using the thread
1696 * pre-emption counter updated from within mi_switch() to
1697 * determine if we were pre-empted during a bcopy() or
1698 * copyout(). Make 3 attempts at doing this before giving up.
1699 * If we encounter an error, stop immediately.
1704 generation = curthread->td_generation;
1705 error = SYSCTL_OUT(req, arg1, arg2);
1709 if (generation != curthread->td_generation && tries < 3) {
1714 error = SYSCTL_IN(req, arg1, arg2);
1720 * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1723 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1729 tt = *(int64_t *)arg1;
1732 error = sysctl_handle_64(oidp, &sb, 0, req);
1733 if (error || !req->newptr)
1737 *(int64_t *)arg1 = tt;
1743 * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1746 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1752 tt = *(int64_t *)arg1;
1755 error = sysctl_handle_64(oidp, &sb, 0, req);
1756 if (error || !req->newptr)
1760 *(int64_t *)arg1 = tt;
1766 * Convert seconds to a struct timeval. Intended for use with
1767 * intervals and thus does not permit negative seconds.
1770 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1778 error = sysctl_handle_int(oidp, &secs, 0, req);
1779 if (error || req->newptr == NULL)
1790 * Transfer functions to/from kernel space.
1791 * XXX: rather untested at this point
1794 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1800 if (req->oldlen <= req->oldidx)
1803 if (i > req->oldlen - req->oldidx)
1804 i = req->oldlen - req->oldidx;
1806 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1809 if (req->oldptr && i != l)
1815 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1819 if (req->newlen - req->newidx < l)
1821 bcopy((const char *)req->newptr + req->newidx, p, l);
1827 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1828 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1831 struct sysctl_req req;
1833 bzero(&req, sizeof req);
1839 req.oldlen = *oldlenp;
1841 req.validlen = req.oldlen;
1848 req.newlen = newlen;
1852 req.oldfunc = sysctl_old_kernel;
1853 req.newfunc = sysctl_new_kernel;
1854 req.lock = REQ_UNWIRED;
1856 error = sysctl_root(0, name, namelen, &req);
1858 if (req.lock == REQ_WIRED && req.validlen > 0)
1859 vsunlock(req.oldptr, req.validlen);
1861 if (error && error != ENOMEM)
1865 if (req.oldptr && req.oldidx > req.validlen)
1866 *retval = req.validlen;
1868 *retval = req.oldidx;
1874 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1875 void *new, size_t newlen, size_t *retval, int flags)
1877 int oid[CTL_MAXNAME];
1878 size_t oidlen, plen;
1881 oid[0] = CTL_SYSCTL;
1882 oid[1] = CTL_SYSCTL_NAME2OID;
1883 oidlen = sizeof(oid);
1885 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1886 (void *)name, strlen(name), &plen, flags);
1890 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1891 new, newlen, retval, flags);
1896 * Transfer function to/from user space.
1899 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1901 size_t i, len, origidx;
1904 origidx = req->oldidx;
1906 if (req->oldptr == NULL)
1909 * If we have not wired the user supplied buffer and we are currently
1910 * holding locks, drop a witness warning, as it's possible that
1911 * write operations to the user page can sleep.
1913 if (req->lock != REQ_WIRED)
1914 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1915 "sysctl_old_user()");
1917 len = req->validlen;
1921 if (i > len - origidx)
1923 if (req->lock == REQ_WIRED) {
1924 error = copyout_nofault(p, (char *)req->oldptr +
1927 error = copyout(p, (char *)req->oldptr + origidx, i);
1937 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1943 if (req->newlen - req->newidx < l)
1945 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1946 "sysctl_new_user()");
1947 error = copyin((const char *)req->newptr + req->newidx, p, l);
1953 * Wire the user space destination buffer. If set to a value greater than
1954 * zero, the len parameter limits the maximum amount of wired memory.
1957 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1962 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1964 if (req->lock != REQ_WIRED && req->oldptr &&
1965 req->oldfunc == sysctl_old_user) {
1966 if (wiredlen != 0) {
1967 ret = vslock(req->oldptr, wiredlen);
1974 req->lock = REQ_WIRED;
1975 req->validlen = wiredlen;
1981 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1982 int *nindx, struct sysctl_req *req)
1984 struct sysctl_oid_list *lsp;
1985 struct sysctl_oid *oid;
1988 SYSCTL_ASSERT_LOCKED();
1989 lsp = &sysctl__children;
1991 while (indx < CTL_MAXNAME) {
1992 SLIST_FOREACH(oid, lsp, oid_link) {
1993 if (oid->oid_number == name[indx])
2000 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2001 if (oid->oid_handler != NULL || indx == namelen) {
2005 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2006 ("%s found DYING node %p", __func__, oid));
2009 lsp = SYSCTL_CHILDREN(oid);
2010 } else if (indx == namelen) {
2011 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2016 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2017 ("%s found DYING node %p", __func__, oid));
2027 * Traverse our tree, and find the right node, execute whatever it points
2028 * to, and return the resulting error code.
2032 sysctl_root(SYSCTL_HANDLER_ARGS)
2034 struct sysctl_oid *oid;
2035 struct rm_priotracker tracker;
2036 int error, indx, lvl;
2038 SYSCTL_RLOCK(&tracker);
2040 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2044 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2046 * You can't call a sysctl when it's a node, but has
2047 * no handler. Inform the user that it's a node.
2048 * The indx may or may not be the same as namelen.
2050 if (oid->oid_handler == NULL) {
2056 /* Is this sysctl writable? */
2057 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2062 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2064 #ifdef CAPABILITY_MODE
2066 * If the process is in capability mode, then don't permit reading or
2067 * writing unless specifically granted for the node.
2069 if (IN_CAPABILITY_MODE(req->td)) {
2070 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2071 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2078 /* Is this sysctl sensitive to securelevels? */
2079 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2080 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2081 error = securelevel_gt(req->td->td_ucred, lvl);
2086 /* Is this sysctl writable by only privileged users? */
2087 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2090 if (oid->oid_kind & CTLFLAG_PRISON)
2091 priv = PRIV_SYSCTL_WRITEJAIL;
2093 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2094 prison_owns_vnet(req->td->td_ucred))
2095 priv = PRIV_SYSCTL_WRITEJAIL;
2098 priv = PRIV_SYSCTL_WRITE;
2099 error = priv_check(req->td, priv);
2104 if (!oid->oid_handler) {
2109 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2110 arg1 = (int *)arg1 + indx;
2113 arg1 = oid->oid_arg1;
2114 arg2 = oid->oid_arg2;
2117 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2123 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2124 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2126 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2129 SYSCTL_RUNLOCK(&tracker);
2133 #ifndef _SYS_SYSPROTO_H_
2134 struct sysctl_args {
2144 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2146 int error, i, name[CTL_MAXNAME];
2149 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2152 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2156 error = userland_sysctl(td, name, uap->namelen,
2157 uap->old, uap->oldlenp, 0,
2158 uap->new, uap->newlen, &j, 0);
2159 if (error && error != ENOMEM)
2162 i = copyout(&j, uap->oldlenp, sizeof(j));
2170 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2171 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2172 int flags, bool inkernel)
2174 int oid[CTL_MAXNAME];
2180 if (namelen > MAXPATHLEN || namelen == 0)
2183 if (namelen > sizeof(namebuf))
2184 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2185 error = copyin(oname, name, namelen);
2189 oid[0] = CTL_SYSCTL;
2190 oid[1] = CTL_SYSCTL_NAME2OID;
2191 oidlen = sizeof(oid);
2192 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2196 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2197 inkernel, new, newlen, retval, flags);
2200 if (namelen > sizeof(namebuf))
2201 free(name, M_SYSCTL);
2205 #ifndef _SYS_SYSPROTO_H_
2206 struct __sysctlbyname_args {
2216 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2221 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2222 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2225 if (uap->oldlenp != NULL)
2226 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2232 * This is used from various compatibility syscalls too. That's why name
2233 * must be in kernel space.
2236 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2237 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2238 size_t *retval, int flags)
2240 int error = 0, memlocked;
2241 struct sysctl_req req;
2243 bzero(&req, sizeof req);
2250 req.oldlen = *oldlenp;
2252 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2257 req.validlen = req.oldlen;
2261 req.newlen = newlen;
2265 req.oldfunc = sysctl_old_user;
2266 req.newfunc = sysctl_new_user;
2267 req.lock = REQ_UNWIRED;
2270 if (KTRPOINT(curthread, KTR_SYSCTL))
2271 ktrsysctl(name, namelen);
2274 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2276 sx_xlock(&sysctlmemlock);
2278 CURVNET_SET(TD_TO_VNET(td));
2283 error = sysctl_root(0, name, namelen, &req);
2284 if (error != EAGAIN)
2286 kern_yield(PRI_USER);
2291 if (req.lock == REQ_WIRED && req.validlen > 0)
2292 vsunlock(req.oldptr, req.validlen);
2294 sx_xunlock(&sysctlmemlock);
2296 if (error && error != ENOMEM)
2300 if (req.oldptr && req.oldidx > req.validlen)
2301 *retval = req.validlen;
2303 *retval = req.oldidx;
2309 * Drain into a sysctl struct. The user buffer should be wired if a page
2310 * fault would cause issue.
2313 sbuf_sysctl_drain(void *arg, const char *data, int len)
2315 struct sysctl_req *req = arg;
2318 error = SYSCTL_OUT(req, data, len);
2319 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2320 return (error == 0 ? len : -error);
2324 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2325 struct sysctl_req *req)
2328 /* Supply a default buffer size if none given. */
2329 if (buf == NULL && length == 0)
2331 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2332 sbuf_set_drain(s, sbuf_sysctl_drain, req);
2338 /* The current OID the debugger is working with */
2339 static struct sysctl_oid *g_ddb_oid;
2341 /* The current flags specified by the user */
2342 static int g_ddb_sysctl_flags;
2344 /* Check to see if the last sysctl printed */
2345 static int g_ddb_sysctl_printed;
2347 static const int ctl_sign[CTLTYPE+1] = {
2356 static const int ctl_size[CTLTYPE+1] = {
2357 [CTLTYPE_INT] = sizeof(int),
2358 [CTLTYPE_UINT] = sizeof(u_int),
2359 [CTLTYPE_LONG] = sizeof(long),
2360 [CTLTYPE_ULONG] = sizeof(u_long),
2361 [CTLTYPE_S8] = sizeof(int8_t),
2362 [CTLTYPE_S16] = sizeof(int16_t),
2363 [CTLTYPE_S32] = sizeof(int32_t),
2364 [CTLTYPE_S64] = sizeof(int64_t),
2365 [CTLTYPE_U8] = sizeof(uint8_t),
2366 [CTLTYPE_U16] = sizeof(uint16_t),
2367 [CTLTYPE_U32] = sizeof(uint32_t),
2368 [CTLTYPE_U64] = sizeof(uint64_t),
2371 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */
2372 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */
2373 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */
2374 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */
2376 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */
2378 static const char db_sysctl_modifs[] = {
2382 static const int db_sysctl_modif_values[] = {
2383 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2384 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2387 /* Handlers considered safe to print while recursing */
2388 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2396 sysctl_handle_string,
2397 sysctl_handle_opaque,
2401 * Use in place of sysctl_old_kernel to print sysctl values.
2403 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2406 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2408 const u_char *val, *p;
2410 size_t intlen, slen;
2413 int sign, ctltype, hexlen, xflag, error;
2415 /* Suppress false-positive GCC uninitialized variable warnings */
2427 /* We are going to print */
2428 g_ddb_sysctl_printed = 1;
2430 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2432 ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2433 sign = ctl_sign[ctltype];
2434 intlen = ctl_size[ctltype];
2438 case CTLTYPE_STRING:
2439 db_printf("%.*s", (int) len, (const char *) p);
2455 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2457 while (len >= intlen) {
2461 umv = *(const u_int *)p;
2462 mv = *(const int *)p;
2466 umv = *(const u_long *)p;
2467 mv = *(const long *)p;
2471 umv = *(const uint8_t *)p;
2472 mv = *(const int8_t *)p;
2476 umv = *(const uint16_t *)p;
2477 mv = *(const int16_t *)p;
2481 umv = *(const uint32_t *)p;
2482 mv = *(const int32_t *)p;
2486 umv = *(const uint64_t *)p;
2487 mv = *(const int64_t *)p;
2491 db_printf("%s", sep1);
2493 db_printf("%#0*jx", hexlen, umv);
2495 db_printf("%ju", umv);
2496 else if (g_ddb_oid->oid_fmt[1] == 'K') {
2497 /* Kelvins are currently unsupported. */
2501 db_printf("%jd", mv);
2510 case CTLTYPE_OPAQUE:
2511 /* TODO: Support struct functions. */
2515 db_printf("Format:%s Length:%zu Dump:0x",
2516 g_ddb_oid->oid_fmt, len);
2517 while (len-- && (xflag || p < val + 16))
2518 db_printf("%02x", *p++);
2519 if (!xflag && len > 16)
2526 req->oldidx += slen;
2531 * Avoid setting new sysctl values from the debugger
2534 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2540 /* Changing sysctls from the debugger is currently unsupported */
2545 * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2546 * Instead of copying any output to a buffer we'll dump it right to
2550 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2551 void *old, size_t *oldlenp, size_t *retval, int flags)
2553 struct sysctl_req req;
2556 /* Setup the request */
2557 bzero(&req, sizeof req);
2558 req.td = kdb_thread;
2559 req.oldfunc = sysctl_old_ddb;
2560 req.newfunc = sysctl_new_ddb;
2561 req.lock = REQ_UNWIRED;
2563 req.oldlen = *oldlenp;
2565 req.validlen = req.oldlen;
2570 /* Setup our globals for sysctl_old_ddb */
2572 g_ddb_sysctl_flags = flags;
2573 g_ddb_sysctl_printed = 0;
2575 error = sysctl_root(0, name, namelen, &req);
2579 g_ddb_sysctl_flags = 0;
2582 if (req.oldptr && req.oldidx > req.validlen)
2583 *retval = req.validlen;
2585 *retval = req.oldidx;
2591 * Show a sysctl's name
2594 db_show_oid_name(int *oid, size_t nlen)
2596 struct sysctl_oid *oidp;
2597 int qoid[CTL_MAXNAME+2];
2601 memcpy(qoid + 2, oid, nlen * sizeof(int));
2604 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2606 db_error("sysctl name oid");
2608 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2610 db_error("sysctl name");
2614 * Check to see if an OID is safe to print from ddb.
2617 db_oid_safe(const struct sysctl_oid *oidp)
2619 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2620 if (oidp->oid_handler == db_safe_handlers[i])
2628 * Show a sysctl at a specific OID
2629 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2632 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2634 int error, xflag, oflag, Nflag, nflag;
2637 xflag = flags & DB_SYSCTL_HEX;
2638 oflag = flags & DB_SYSCTL_OPAQUE;
2639 nflag = flags & DB_SYSCTL_VALUE_ONLY;
2640 Nflag = flags & DB_SYSCTL_NAME_ONLY;
2642 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2647 db_show_oid_name(oid, nlen);
2653 db_show_oid_name(oid, nlen);
2657 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2658 db_printf("Skipping, unsafe to print while recursing.");
2663 /* Try once, and ask about the size */
2665 error = db_sysctl(oidp, oid, nlen,
2666 NULL, NULL, &len, flags);
2670 if (!g_ddb_sysctl_printed)
2671 /* Lie about the size */
2672 error = db_sysctl(oidp, oid, nlen,
2673 (void *) 1, &len, NULL, flags);
2681 * Show all sysctls under a specific OID
2682 * Compare to sysctl_all from sbin/sysctl/sysctl.c
2685 db_show_sysctl_all(int *oid, size_t len, int flags)
2687 struct sysctl_oid *oidp;
2688 int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
2691 name1[0] = CTL_SYSCTL;
2692 name1[1] = CTL_SYSCTL_NEXT;
2695 memcpy(name1+2, oid, len * sizeof(int));
2705 error = kernel_sysctl(kdb_thread, name1, l1,
2706 name2, &l2, NULL, 0, &l2, 0);
2708 if (error == ENOENT)
2711 db_error("sysctl(getnext)");
2716 if (l2 < (unsigned int)len)
2719 for (i = 0; i < len; i++)
2720 if (name2[i] != oid[i])
2723 /* Find the OID in question */
2724 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
2728 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
2733 memcpy(name1+2, name2, l2 * sizeof(int));
2739 * Show a sysctl by its user facing string
2742 db_sysctlbyname(char *name, int flags)
2744 struct sysctl_oid *oidp;
2745 int oid[CTL_MAXNAME];
2748 error = name2oid(name, oid, &nlen, &oidp);
2753 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2754 db_show_sysctl_all(oid, nlen, flags);
2756 error = db_show_oid(oidp, oid, nlen, flags);
2763 db_sysctl_cmd_usage(void)
2766 " sysctl [/Nnox] <sysctl> \n"
2768 " <sysctl> The name of the sysctl to show. \n"
2770 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n"
2771 " This will work for most sysctls, but should not be used \n"
2772 " with sysctls that are known to malloc. \n"
2774 " While recursing any \"unsafe\" sysctls will be skipped. \n"
2775 " Call sysctl directly on the sysctl to try printing the \n"
2776 " skipped sysctl. This is unsafe and may make the ddb \n"
2777 " session unusable. \n"
2780 " /N Display only the name of the sysctl. \n"
2781 " /n Display only the value of the sysctl. \n"
2782 " /o Display opaque values. \n"
2783 " /x Display the sysctl in hex. \n"
2786 "sysctl vm.v_free_min \n"
2787 "vn.v_free_min: 12669 \n"
2792 * Show a specific sysctl similar to sysctl (8).
2794 DB_FUNC(sysctl, db_sysctl_cmd, db_cmd_table, CS_OWN, NULL)
2796 char name[TOK_STRING_SIZE];
2797 int error, i, t, flags;
2799 /* Parse the modifiers */
2800 t = db_read_token();
2801 if (t == tSLASH || t == tMINUS) {
2802 t = db_read_token();
2804 db_printf("Bad modifier\n");
2808 db_strcpy(modif, db_tok_string);
2816 for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2817 if (strchr(modif, db_sysctl_modifs[i])) {
2818 flags |= db_sysctl_modif_values[i];
2822 /* Parse the sysctl names */
2823 t = db_read_token();
2825 db_printf("Need sysctl name\n");
2830 /* Copy the name into a temporary buffer */
2831 db_strcpy(name, db_tok_string);
2833 /* Ensure there is no trailing cruft */
2834 t = db_read_token();
2836 db_printf("Unexpected sysctl argument\n");
2841 error = db_sysctlbyname(name, flags);
2842 if (error == ENOENT) {
2843 db_printf("unknown oid: '%s'\n", db_tok_string);
2846 db_printf("%s: error: %d\n", db_tok_string, error);
2851 /* Ensure we eat all of our text */
2854 if (error == EINVAL) {
2855 db_sysctl_cmd_usage();