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 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
44 #include "opt_sysctl.h"
46 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/capsicum.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/rmlock.h>
63 #include <sys/sysproto.h>
66 #include <sys/ktrace.h>
71 #include <ddb/db_lex.h>
76 #include <security/mac/mac_framework.h>
79 #include <vm/vm_extern.h>
81 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
82 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
83 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
85 RB_GENERATE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid);
88 * The sysctllock protects the MIB tree. It also protects sysctl
89 * contexts used with dynamic sysctls. The sysctl_register_oid() and
90 * sysctl_unregister_oid() routines require the sysctllock to already
91 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
92 * provided for the few places in the kernel which need to use that
93 * API rather than using the dynamic API. Use of the dynamic API is
94 * strongly encouraged for most code.
96 * The sysctlmemlock is used to limit the amount of user memory wired for
97 * sysctl requests. This is implemented by serializing any userland
98 * sysctl requests larger than a single page via an exclusive lock.
100 * The sysctlstringlock is used to protect concurrent access to writable
101 * string nodes in sysctl_handle_string().
103 static struct rmlock sysctllock;
104 static struct sx __exclusive_cache_line sysctlmemlock;
105 static struct sx sysctlstringlock;
107 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
108 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
109 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
110 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
111 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
112 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
113 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
114 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
115 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
117 #define SYSCTL_SLEEP(ch, wmesg, timo) \
118 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
120 static int sysctl_root(SYSCTL_HANDLER_ARGS);
123 struct sysctl_oid_list sysctl__children = RB_INITIALIZER(&sysctl__children);
125 static char* sysctl_escape_name(const char*);
126 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
128 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
129 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
130 static int name2oid(const char *, int *, int *, struct sysctl_oid **);
132 static struct sysctl_oid *
133 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
135 struct sysctl_oid *oidp;
137 SYSCTL_ASSERT_LOCKED();
138 SYSCTL_FOREACH(oidp, list) {
139 if (strcmp(oidp->oid_name, name) == 0) {
146 static struct sysctl_oid *
147 sysctl_find_oidnamelen(const char *name, size_t len,
148 struct sysctl_oid_list *list)
150 struct sysctl_oid *oidp;
152 SYSCTL_ASSERT_LOCKED();
153 SYSCTL_FOREACH(oidp, list) {
154 if (strncmp(oidp->oid_name, name, len) == 0 &&
155 oidp->oid_name[len] == '\0')
162 * Initialization of the MIB tree.
164 * Order by number in each list.
181 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
182 struct sysctl_req *req, struct rm_priotracker *tracker)
186 if (oid->oid_kind & CTLFLAG_DYN)
187 atomic_add_int(&oid->oid_running, 1);
190 SYSCTL_RUNLOCK(tracker);
195 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
196 * untill we're ready to remove all traces of Giant from sysctl(9).
198 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
199 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
201 error = oid->oid_handler(oid, arg1, arg2, req);
202 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
203 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
206 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
209 SYSCTL_RLOCK(tracker);
213 if (oid->oid_kind & CTLFLAG_DYN) {
214 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
215 (oid->oid_kind & CTLFLAG_DYING) != 0)
216 wakeup(&oid->oid_running);
223 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
225 struct sysctl_req req;
226 struct sysctl_oid *curr;
229 ssize_t rem = sizeof(path);
231 uint8_t data[512] __aligned(sizeof(uint64_t));
237 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
238 len = strlen(curr->oid_name);
243 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
246 memcpy(path + rem, curr->oid_name, len);
248 path[rem + len] = '.';
251 memset(&req, 0, sizeof(req));
254 req.oldfunc = sysctl_old_kernel;
255 req.newfunc = sysctl_new_kernel;
256 req.lock = REQ_UNWIRED;
258 switch (oidp->oid_kind & CTLTYPE) {
260 if (getenv_array(path + rem, data, sizeof(data), &size,
261 sizeof(int), GETENV_SIGNED) == 0)
267 if (getenv_array(path + rem, data, sizeof(data), &size,
268 sizeof(int), GETENV_UNSIGNED) == 0)
274 if (getenv_array(path + rem, data, sizeof(data), &size,
275 sizeof(long), GETENV_SIGNED) == 0)
281 if (getenv_array(path + rem, data, sizeof(data), &size,
282 sizeof(long), GETENV_UNSIGNED) == 0)
288 if (getenv_array(path + rem, data, sizeof(data), &size,
289 sizeof(int8_t), GETENV_SIGNED) == 0)
295 if (getenv_array(path + rem, data, sizeof(data), &size,
296 sizeof(int16_t), GETENV_SIGNED) == 0)
302 if (getenv_array(path + rem, data, sizeof(data), &size,
303 sizeof(int32_t), GETENV_SIGNED) == 0)
309 if (getenv_array(path + rem, data, sizeof(data), &size,
310 sizeof(int64_t), GETENV_SIGNED) == 0)
316 if (getenv_array(path + rem, data, sizeof(data), &size,
317 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
323 if (getenv_array(path + rem, data, sizeof(data), &size,
324 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
330 if (getenv_array(path + rem, data, sizeof(data), &size,
331 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
337 if (getenv_array(path + rem, data, sizeof(data), &size,
338 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
344 penv = kern_getenv(path + rem);
347 req.newlen = strlen(penv);
353 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
354 oidp->oid_arg2, &req, NULL);
356 printf("Setting sysctl %s failed: %d\n", path + rem, error);
362 * Locate the path to a given oid. Returns the length of the resulting path,
363 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
367 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
371 SYSCTL_ASSERT_LOCKED();
374 * Do a depth-first search of the oid tree, looking for 'needle'. Start
375 * with the first child of the root.
377 nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children);
379 if (nodes[indx] == needle)
382 if (nodes[indx] == NULL) {
383 /* Node has no more siblings, so back up to parent. */
385 /* Retreat to root, so give up. */
388 } else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
389 /* Node has children. */
390 if (++indx == CTL_MAXNAME) {
391 /* Max search depth reached, so give up. */
394 /* Start with the first child. */
395 nodes[indx] = RB_MIN(sysctl_oid_list,
396 &nodes[indx - 1]->oid_children);
399 /* Consider next sibling. */
400 nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]);
406 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
408 struct sysctl_oid *nodes[CTL_MAXNAME];
413 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
414 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
416 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
418 rc = sysctl_search_oid(nodes, leaf);
420 for (i = 0; i < rc; i++)
421 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
424 sbuf_printf(&sb, "%s", leaf->oid_name);
426 sbuf_printf(&sb, ")!\n");
428 (void)sbuf_finish(&sb);
433 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
435 struct rm_priotracker tracker;
437 SYSCTL_RLOCK(&tracker);
438 sysctl_warn_reuse(__func__, oidp);
439 SYSCTL_RUNLOCK(&tracker);
442 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
443 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
448 sysctl_register_oid(struct sysctl_oid *oidp)
450 struct sysctl_oid_list *parent = oidp->oid_parent;
451 struct sysctl_oid *p, key;
456 * First check if another oid with the same name already
457 * exists in the parent's list.
459 SYSCTL_ASSERT_WLOCKED();
460 p = sysctl_find_oidname(oidp->oid_name, parent);
462 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
466 sysctl_warn_reuse(__func__, p);
470 /* get current OID number */
471 oid_number = oidp->oid_number;
474 #error "OID_AUTO is expected to be a negative value"
477 * Any negative OID number qualifies as OID_AUTO. Valid OID
478 * numbers should always be positive.
480 * NOTE: DO NOT change the starting value here, change it in
481 * <sys/sysctl.h>, and make sure it is at least 256 to
482 * accommodate e.g. net.inet.raw as a static sysctl node.
484 if (oid_number < 0) {
488 * By decrementing the next OID number we spend less
489 * time inserting the OIDs into a sorted list.
491 if (--newoid < CTL_AUTO_START)
498 * Insert the OID into the parent's list sorted by OID number.
500 key.oid_number = oid_number;
501 p = RB_NFIND(sysctl_oid_list, parent, &key);
502 while (p != NULL && oid_number == p->oid_number) {
503 /* get the next valid OID number */
504 if (oid_number < CTL_AUTO_START ||
505 oid_number == 0x7fffffff) {
506 /* wraparound - restart */
507 oid_number = CTL_AUTO_START;
508 /* don't loop forever */
510 panic("sysctl: Out of OID numbers\n");
511 key.oid_number = oid_number;
512 p = RB_NFIND(sysctl_oid_list, parent, &key);
515 p = RB_NEXT(sysctl_oid_list, NULL, p);
518 /* check for non-auto OID number collision */
519 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
520 oid_number >= CTL_AUTO_START) {
521 printf("sysctl: OID number(%d) is already in use for '%s'\n",
522 oidp->oid_number, oidp->oid_name);
524 /* update the OID number, if any */
525 oidp->oid_number = oid_number;
526 RB_INSERT(sysctl_oid_list, parent, oidp);
528 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
529 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
530 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
533 * Can fetch value multiple times for VNET loader tunables.
534 * Only fetch once for non-VNET loader tunables.
536 if ((oidp->oid_kind & CTLFLAG_VNET) == 0)
538 oidp->oid_kind |= CTLFLAG_NOFETCH;
539 /* try to fetch value from kernel environment */
540 sysctl_load_tunable_by_oid_locked(oidp);
545 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
549 * Mark the leaf as dormant if it's not to be immediately enabled.
550 * We do not disable nodes as they can be shared between modules
551 * and it is always safe to access a node.
553 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
554 ("internal flag is set in oid_kind"));
555 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
556 oidp->oid_kind |= CTLFLAG_DORMANT;
557 sysctl_register_oid(oidp);
561 sysctl_enable_oid(struct sysctl_oid *oidp)
564 SYSCTL_ASSERT_WLOCKED();
565 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
566 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
567 ("sysctl node is marked as dormant"));
570 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
571 ("enabling already enabled sysctl oid"));
572 oidp->oid_kind &= ~CTLFLAG_DORMANT;
576 sysctl_unregister_oid(struct sysctl_oid *oidp)
580 SYSCTL_ASSERT_WLOCKED();
581 if (oidp->oid_number == OID_AUTO) {
585 if (RB_REMOVE(sysctl_oid_list, oidp->oid_parent, oidp))
590 * This can happen when a module fails to register and is
591 * being unloaded afterwards. It should not be a panic()
595 printf("%s: failed(%d) to unregister sysctl(%s)\n",
596 __func__, error, oidp->oid_name);
600 /* Initialize a new context to keep track of dynamically added sysctls. */
602 sysctl_ctx_init(struct sysctl_ctx_list *c)
610 * No locking here, the caller is responsible for not adding
611 * new nodes to a context until after this function has
618 /* Free the context, and destroy all dynamic oids registered in this context */
620 sysctl_ctx_free(struct sysctl_ctx_list *clist)
622 struct sysctl_ctx_entry *e, *e1;
627 * First perform a "dry run" to check if it's ok to remove oids.
629 * XXX This algorithm is a hack. But I don't know any
630 * XXX better solution for now...
633 TAILQ_FOREACH(e, clist, link) {
634 error = sysctl_remove_oid_locked(e->entry, 0, 0);
639 * Restore deregistered entries, either from the end,
640 * or from the place where error occurred.
641 * e contains the entry that was not unregistered
644 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
646 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
648 sysctl_register_oid(e1->entry);
649 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
655 /* Now really delete the entries */
656 e = TAILQ_FIRST(clist);
658 e1 = TAILQ_NEXT(e, link);
659 error = sysctl_remove_oid_locked(e->entry, 1, 0);
661 panic("sysctl_remove_oid: corrupt tree, entry: %s",
663 free(e, M_SYSCTLOID);
670 /* Add an entry to the context */
671 struct sysctl_ctx_entry *
672 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
674 struct sysctl_ctx_entry *e;
676 SYSCTL_ASSERT_WLOCKED();
677 if (clist == NULL || oidp == NULL)
679 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
681 TAILQ_INSERT_HEAD(clist, e, link);
685 /* Find an entry in the context */
686 struct sysctl_ctx_entry *
687 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
689 struct sysctl_ctx_entry *e;
691 SYSCTL_ASSERT_WLOCKED();
692 if (clist == NULL || oidp == NULL)
694 TAILQ_FOREACH(e, clist, link) {
695 if (e->entry == oidp)
702 * Delete an entry from the context.
703 * NOTE: this function doesn't free oidp! You have to remove it
704 * with sysctl_remove_oid().
707 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
709 struct sysctl_ctx_entry *e;
711 if (clist == NULL || oidp == NULL)
714 e = sysctl_ctx_entry_find(clist, oidp);
716 TAILQ_REMOVE(clist, e, link);
718 free(e, M_SYSCTLOID);
727 * Remove dynamically created sysctl trees.
728 * oidp - top of the tree to be removed
729 * del - if 0 - just deregister, otherwise free up entries as well
730 * recurse - if != 0 traverse the subtree to be deleted
733 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
738 error = sysctl_remove_oid_locked(oidp, del, recurse);
744 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
745 int del, int recurse)
747 struct sysctl_oid *p;
752 p = sysctl_find_oidname(name, &parent->oid_children);
754 error = sysctl_remove_oid_locked(p, del, recurse);
761 * Duplicate the provided string, escaping any illegal characters. The result
762 * must be freed when no longer in use.
764 * The list of illegal characters is ".".
767 sysctl_escape_name(const char* orig)
769 int i, s = 0, d = 0, nillegals = 0;
772 /* First count the number of illegal characters */
773 for (i = 0; orig[i] != '\0'; i++) {
778 /* Allocate storage for new string */
779 new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
781 /* Copy the name, escaping characters as we go */
782 while (orig[s] != '\0') {
783 if (orig[s] == '.') {
784 /* %25 is the hexadecimal representation of '.' */
790 new[d++] = orig[s++];
794 /* Finally, nul-terminate */
801 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
803 struct sysctl_oid *p, *tmp;
806 SYSCTL_ASSERT_WLOCKED();
809 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
810 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
815 * WARNING: normal method to do this should be through
816 * sysctl_ctx_free(). Use recursing as the last resort
817 * method to purge your sysctl tree of leftovers...
818 * However, if some other code still references these nodes,
821 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
822 if (oidp->oid_refcnt == 1) {
823 for(p = RB_MIN(sysctl_oid_list, &oidp->oid_children);
824 p != NULL; p = tmp) {
826 printf("Warning: failed attempt to "
827 "remove oid %s with child %s\n",
828 oidp->oid_name, p->oid_name);
831 tmp = RB_NEXT(sysctl_oid_list,
832 &oidp->oid_children, p);
833 error = sysctl_remove_oid_locked(p, del,
840 if (oidp->oid_refcnt > 1 ) {
843 if (oidp->oid_refcnt == 0) {
844 printf("Warning: bad oid_refcnt=%u (%s)!\n",
845 oidp->oid_refcnt, oidp->oid_name);
848 sysctl_unregister_oid(oidp);
851 * Wait for all threads running the handler to drain.
852 * This preserves the previous behavior when the
853 * sysctl lock was held across a handler invocation,
854 * and is necessary for module unload correctness.
856 while (oidp->oid_running > 0) {
857 oidp->oid_kind |= CTLFLAG_DYING;
858 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
861 free(__DECONST(char *, oidp->oid_descr),
864 free(__DECONST(char *, oidp->oid_label),
866 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
867 free(oidp, M_SYSCTLOID);
873 * Create new sysctls at run time.
874 * clist may point to a valid context initialized with sysctl_ctx_init().
877 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
878 int number, const char *name, int kind, void *arg1, intmax_t arg2,
879 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
882 struct sysctl_oid *oidp;
885 /* You have to hook up somewhere.. */
888 escaped = sysctl_escape_name(name);
889 /* Check if the node already exists, otherwise create it */
891 oidp = sysctl_find_oidname(escaped, parent);
893 free(escaped, M_SYSCTLOID);
894 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
896 /* Update the context */
898 sysctl_ctx_entry_add(clist, oidp);
902 sysctl_warn_reuse(__func__, oidp);
907 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
908 oidp->oid_parent = parent;
909 RB_INIT(&oidp->oid_children);
910 oidp->oid_number = number;
911 oidp->oid_refcnt = 1;
912 oidp->oid_name = escaped;
913 oidp->oid_handler = handler;
914 oidp->oid_kind = CTLFLAG_DYN | kind;
915 oidp->oid_arg1 = arg1;
916 oidp->oid_arg2 = arg2;
919 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
921 oidp->oid_label = strdup(label, M_SYSCTLOID);
922 /* Update the context, if used */
924 sysctl_ctx_entry_add(clist, oidp);
925 /* Register this oid */
926 sysctl_register_oid(oidp);
932 * Rename an existing oid.
935 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
940 newname = strdup(name, M_SYSCTLOID);
942 oldname = __DECONST(char *, oidp->oid_name);
943 oidp->oid_name = newname;
945 free(oldname, M_SYSCTLOID);
949 * Reparent an existing oid.
952 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
954 struct sysctl_oid *oidp;
957 if (oid->oid_parent == parent) {
961 oidp = sysctl_find_oidname(oid->oid_name, parent);
966 sysctl_unregister_oid(oid);
967 oid->oid_parent = parent;
968 oid->oid_number = OID_AUTO;
969 sysctl_register_oid(oid);
975 * Register the kernel's oids on startup.
977 SET_DECLARE(sysctl_set, struct sysctl_oid);
980 sysctl_register_all(void *arg)
982 struct sysctl_oid **oidp;
984 sx_init(&sysctlmemlock, "sysctl mem");
985 sx_init(&sysctlstringlock, "sysctl string handler");
988 SET_FOREACH(oidp, sysctl_set)
989 sysctl_register_oid(*oidp);
992 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
996 sysctl_setenv_vnet(void *arg __unused, const char *name)
998 struct sysctl_oid *oidp;
999 int oid[CTL_MAXNAME];
1003 error = name2oid(name, oid, &nlen, &oidp);
1007 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1008 (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1009 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1010 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1011 /* Update value from kernel environment */
1012 sysctl_load_tunable_by_oid_locked(oidp);
1019 sysctl_unsetenv_vnet(void *arg __unused, const char *name)
1021 struct sysctl_oid *oidp;
1022 int oid[CTL_MAXNAME];
1027 * The setenv / unsetenv event handlers are invoked by kern_setenv() /
1028 * kern_unsetenv() without exclusive locks. It is rare but still possible
1029 * that the invoke order of event handlers is different from that of
1030 * kern_setenv() and kern_unsetenv().
1031 * Re-check environment variable string to make sure it is unset.
1035 error = name2oid(name, oid, &nlen, &oidp);
1039 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1040 (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1041 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1042 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1045 switch (oidp->oid_kind & CTLTYPE) {
1052 size = sizeof(long);
1056 size = sizeof(int8_t);
1060 size = sizeof(int16_t);
1064 size = sizeof(int32_t);
1068 size = sizeof(int64_t);
1070 case CTLTYPE_STRING:
1071 MPASS(oidp->oid_arg2 > 0);
1072 size = oidp->oid_arg2;
1077 vnet_restore_init(oidp->oid_arg1, size);
1084 * Register the kernel's setenv / unsetenv events.
1086 EVENTHANDLER_DEFINE(setenv, sysctl_setenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1087 EVENTHANDLER_DEFINE(unsetenv, sysctl_unsetenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1093 * These functions implement a presently undocumented interface
1094 * used by the sysctl program to walk the tree, and get the type
1095 * so it can print the value.
1096 * This interface is under work and consideration, and should probably
1097 * be killed with a big axe by the first person who can find the time.
1098 * (be aware though, that the proper interface isn't as obvious as it
1099 * may seem, there are various conflicting requirements.
1101 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
1102 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
1104 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring
1106 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
1108 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
1109 * for the "..." OID.
1110 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
1112 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
1114 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring
1120 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1123 struct sysctl_oid *oidp;
1125 SYSCTL_ASSERT_LOCKED();
1126 SYSCTL_FOREACH(oidp, l) {
1130 printf("%d %s ", oidp->oid_number, oidp->oid_name);
1133 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1134 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1136 if (oidp->oid_handler)
1137 printf(" *Handler");
1139 switch (oidp->oid_kind & CTLTYPE) {
1142 if (!oidp->oid_handler) {
1143 sysctl_sysctl_debug_dump_node(
1144 SYSCTL_CHILDREN(oidp), i + 2);
1147 case CTLTYPE_INT: printf(" Int\n"); break;
1148 case CTLTYPE_UINT: printf(" u_int\n"); break;
1149 case CTLTYPE_LONG: printf(" Long\n"); break;
1150 case CTLTYPE_ULONG: printf(" u_long\n"); break;
1151 case CTLTYPE_STRING: printf(" String\n"); break;
1152 case CTLTYPE_S8: printf(" int8_t\n"); break;
1153 case CTLTYPE_S16: printf(" int16_t\n"); break;
1154 case CTLTYPE_S32: printf(" int32_t\n"); break;
1155 case CTLTYPE_S64: printf(" int64_t\n"); break;
1156 case CTLTYPE_U8: printf(" uint8_t\n"); break;
1157 case CTLTYPE_U16: printf(" uint16_t\n"); break;
1158 case CTLTYPE_U32: printf(" uint32_t\n"); break;
1159 case CTLTYPE_U64: printf(" uint64_t\n"); break;
1160 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1161 default: printf("\n");
1167 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1169 struct rm_priotracker tracker;
1172 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1175 SYSCTL_RLOCK(&tracker);
1176 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1177 SYSCTL_RUNLOCK(&tracker);
1181 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1182 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1186 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1188 int *name = (int *) arg1;
1189 u_int namelen = arg2;
1191 struct sysctl_oid *oid, key;
1192 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1193 struct rm_priotracker tracker;
1196 error = sysctl_wire_old_buffer(req, 0);
1200 SYSCTL_RLOCK(&tracker);
1203 snprintf(buf,sizeof(buf),"%d",*name);
1205 error = SYSCTL_OUT(req, ".", 1);
1207 error = SYSCTL_OUT(req, buf, strlen(buf));
1215 key.oid_number = *name;
1216 oid = RB_FIND(sysctl_oid_list, lsp, &key);
1219 error = SYSCTL_OUT(req, ".", 1);
1221 error = SYSCTL_OUT(req, oid->oid_name,
1222 strlen(oid->oid_name));
1229 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE &&
1231 lsp2 = SYSCTL_CHILDREN(oid);
1235 error = SYSCTL_OUT(req, "", 1);
1237 SYSCTL_RUNLOCK(&tracker);
1242 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1245 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1246 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1248 enum sysctl_iter_action {
1249 ITER_SIBLINGS, /* Not matched, continue iterating siblings */
1250 ITER_CHILDREN, /* Node has children we need to iterate over them */
1251 ITER_FOUND, /* Matching node was found */
1255 * Tries to find the next node for @name and @namelen.
1257 * Returns next action to take.
1259 static enum sysctl_iter_action
1260 sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen,
1264 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1265 return (ITER_SIBLINGS);
1267 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1268 return (ITER_SIBLINGS);
1272 * We have reached a node with a full name match and are
1273 * looking for the next oid in its children.
1275 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1277 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1278 * has a handler) and move on to the children.
1281 return (ITER_FOUND);
1282 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1283 return (ITER_FOUND);
1284 /* If node does not have an iterator, treat it as leaf */
1285 if (oidp->oid_handler)
1286 return (ITER_FOUND);
1288 /* Report oid as a node to iterate */
1289 return (ITER_CHILDREN);
1293 * No match yet. Continue seeking the given name.
1295 * We are iterating in order by oid_number, so skip oids lower
1296 * than the one we are looking for.
1298 * When the current oid_number is higher than the one we seek,
1299 * that means we have reached the next oid in the sequence and
1302 * If the oid_number matches the name at this level then we
1303 * have to find a node to continue searching at the next level.
1305 if (oidp->oid_number < *name)
1306 return (ITER_SIBLINGS);
1307 if (oidp->oid_number > *name) {
1309 * We have reached the next oid.
1311 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1313 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1314 * has a handler) and move on to the children.
1317 return (ITER_FOUND);
1318 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1319 return (ITER_FOUND);
1320 /* If node does not have an iterator, treat it as leaf */
1321 if (oidp->oid_handler)
1322 return (ITER_FOUND);
1323 return (ITER_CHILDREN);
1326 /* match at a current level */
1327 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1328 return (ITER_SIBLINGS);
1329 if (oidp->oid_handler)
1330 return (ITER_SIBLINGS);
1332 return (ITER_CHILDREN);
1336 * Recursively walk the sysctl subtree at lsp until we find the given name.
1337 * Returns true and fills in next oid data in @next and @len if oid is found.
1340 sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1341 int *next, int *len, int level, bool honor_skip)
1343 struct sysctl_oid_list *next_lsp;
1344 struct sysctl_oid *oidp = NULL, key;
1345 bool success = false;
1346 enum sysctl_iter_action action;
1348 SYSCTL_ASSERT_LOCKED();
1350 * Start the search at the requested oid. But if not found, then scan
1351 * through all children.
1354 key.oid_number = *name;
1355 oidp = RB_FIND(sysctl_oid_list, lsp, &key);
1358 oidp = RB_MIN(sysctl_oid_list, lsp);
1359 for(; oidp != NULL; oidp = RB_NEXT(sysctl_oid_list, lsp, oidp)) {
1360 action = sysctl_sysctl_next_node(oidp, name, namelen,
1362 if (action == ITER_SIBLINGS)
1364 if (action == ITER_FOUND) {
1368 KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action));
1370 next_lsp = SYSCTL_CHILDREN(oidp);
1372 success = sysctl_sysctl_next_action(next_lsp, NULL, 0,
1373 next + 1, len, level + 1, honor_skip);
1375 success = sysctl_sysctl_next_action(next_lsp, name + 1,
1376 namelen - 1, next + 1, len, level + 1, honor_skip);
1380 * We maintain the invariant that current node oid
1381 * is >= the oid provided in @name.
1382 * As there are no usable children at this node,
1383 * current node oid is strictly > than the requested
1385 * Hence, reduce namelen to 0 to allow for picking first
1386 * nodes/leafs in the next node in list.
1396 *next = oidp->oid_number;
1405 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1407 int *name = (int *) arg1;
1408 u_int namelen = arg2;
1411 struct sysctl_oid_list *lsp = &sysctl__children;
1412 struct rm_priotracker tracker;
1413 int next[CTL_MAXNAME];
1416 SYSCTL_RLOCK(&tracker);
1417 success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1,
1418 oidp->oid_number == CTL_SYSCTL_NEXT);
1419 SYSCTL_RUNLOCK(&tracker);
1422 error = SYSCTL_OUT(req, next, len * sizeof (int));
1427 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1430 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1431 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1433 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1434 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1437 name2oid(const char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1439 struct sysctl_oid *oidp;
1440 struct sysctl_oid_list *lsp = &sysctl__children;
1443 SYSCTL_ASSERT_LOCKED();
1445 for (*len = 0; *len < CTL_MAXNAME;) {
1446 n = strchrnul(name, '.');
1447 oidp = sysctl_find_oidnamelen(name, n - name, lsp);
1450 *oid++ = oidp->oid_number;
1456 if (*name == '\0') {
1462 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1465 if (oidp->oid_handler)
1468 lsp = SYSCTL_CHILDREN(oidp);
1474 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1477 int error, oid[CTL_MAXNAME], len = 0;
1478 struct sysctl_oid *op = NULL;
1479 struct rm_priotracker tracker;
1484 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1485 return (ENAMETOOLONG);
1488 if (req->newlen >= sizeof(buf))
1489 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1491 error = SYSCTL_IN(req, p, req->newlen);
1498 p [req->newlen] = '\0';
1500 SYSCTL_RLOCK(&tracker);
1501 error = name2oid(p, oid, &len, &op);
1502 SYSCTL_RUNLOCK(&tracker);
1510 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1515 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1518 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1519 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1520 sysctl_sysctl_name2oid, "I", "");
1523 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1525 struct sysctl_oid *oid;
1526 struct rm_priotracker tracker;
1529 error = sysctl_wire_old_buffer(req, 0);
1533 SYSCTL_RLOCK(&tracker);
1534 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1538 if (oid->oid_fmt == NULL) {
1542 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1545 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1547 SYSCTL_RUNLOCK(&tracker);
1551 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1552 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1555 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1557 struct sysctl_oid *oid;
1558 struct rm_priotracker tracker;
1561 error = sysctl_wire_old_buffer(req, 0);
1565 SYSCTL_RLOCK(&tracker);
1566 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1570 if (oid->oid_descr == NULL) {
1574 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1576 SYSCTL_RUNLOCK(&tracker);
1580 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1581 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1584 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1586 struct sysctl_oid *oid;
1587 struct rm_priotracker tracker;
1590 error = sysctl_wire_old_buffer(req, 0);
1594 SYSCTL_RLOCK(&tracker);
1595 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1599 if (oid->oid_label == NULL) {
1603 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1605 SYSCTL_RUNLOCK(&tracker);
1609 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1610 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1613 * Default "handler" functions.
1619 * a variable: point arg1 at it.
1620 * a constant: pass it in arg2.
1624 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1630 * Attempt to get a coherent snapshot by making a copy of the data.
1633 temp = *(bool *)arg1 ? 1 : 0;
1635 temp = arg2 ? 1 : 0;
1637 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1638 if (error || !req->newptr)
1644 error = SYSCTL_IN(req, &temp, sizeof(temp));
1646 *(bool *)arg1 = temp ? 1 : 0;
1652 * Handle an int8_t, signed or unsigned.
1654 * a variable: point arg1 at it.
1655 * a constant: pass it in arg2.
1659 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1665 * Attempt to get a coherent snapshot by making a copy of the data.
1668 tmpout = *(int8_t *)arg1;
1671 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1673 if (error || !req->newptr)
1679 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1684 * Handle an int16_t, signed or unsigned.
1686 * a variable: point arg1 at it.
1687 * a constant: pass it in arg2.
1691 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1697 * Attempt to get a coherent snapshot by making a copy of the data.
1700 tmpout = *(int16_t *)arg1;
1703 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1705 if (error || !req->newptr)
1711 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1716 * Handle an int32_t, signed or unsigned.
1718 * a variable: point arg1 at it.
1719 * a constant: pass it in arg2.
1723 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1729 * Attempt to get a coherent snapshot by making a copy of the data.
1732 tmpout = *(int32_t *)arg1;
1735 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1737 if (error || !req->newptr)
1743 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1748 * Handle an int, signed or unsigned.
1750 * a variable: point arg1 at it.
1751 * a constant: pass it in arg2.
1755 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1757 int tmpout, error = 0;
1760 * Attempt to get a coherent snapshot by making a copy of the data.
1763 tmpout = *(int *)arg1;
1766 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1768 if (error || !req->newptr)
1774 error = SYSCTL_IN(req, arg1, sizeof(int));
1779 * Based on sysctl_handle_int() convert milliseconds into ticks.
1780 * Note: this is used by TCP.
1784 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1789 s = (int)((int64_t)tt * 1000 / hz);
1791 error = sysctl_handle_int(oidp, &s, 0, req);
1792 if (error || !req->newptr)
1795 tt = (int)((int64_t)s * hz / 1000);
1804 * Handle a long, signed or unsigned.
1806 * a variable: point arg1 at it.
1807 * a constant: pass it in arg2.
1811 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1820 * Attempt to get a coherent snapshot by making a copy of the data.
1823 tmplong = *(long *)arg1;
1827 if (req->flags & SCTL_MASK32) {
1829 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1832 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1834 if (error || !req->newptr)
1840 else if (req->flags & SCTL_MASK32) {
1841 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1842 *(long *)arg1 = (long)tmpint;
1846 error = SYSCTL_IN(req, arg1, sizeof(long));
1851 * Handle a 64 bit int, signed or unsigned.
1853 * a variable: point arg1 at it.
1854 * a constant: pass it in arg2.
1857 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1863 * Attempt to get a coherent snapshot by making a copy of the data.
1866 tmpout = *(uint64_t *)arg1;
1869 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1871 if (error || !req->newptr)
1877 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1882 * Handle our generic '\0' terminated 'C' string.
1884 * a variable string: point arg1 at it, arg2 is max length.
1885 * a constant string: point arg1 at it, arg2 is zero.
1889 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1893 int error = 0, ro_string = 0;
1896 * If the sysctl isn't writable and isn't a preallocated tunable that
1897 * can be modified by kenv(2), microoptimise and treat it as a
1899 * A zero-length buffer indicates a fixed size read-only
1900 * string. In ddb, don't worry about trying to make a malloced
1903 if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
1904 arg2 == 0 || kdb_active) {
1905 arg2 = strlen((char *)arg1) + 1;
1909 if (req->oldptr != NULL) {
1912 outlen = strlen(tmparg) + 1;
1914 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1915 sx_slock(&sysctlstringlock);
1916 memcpy(tmparg, arg1, arg2);
1917 sx_sunlock(&sysctlstringlock);
1918 outlen = strlen(tmparg) + 1;
1921 error = SYSCTL_OUT(req, tmparg, outlen);
1924 free(tmparg, M_SYSCTLTMP);
1927 sx_slock(&sysctlstringlock);
1928 outlen = strlen((char *)arg1) + 1;
1930 sx_sunlock(&sysctlstringlock);
1931 error = SYSCTL_OUT(req, NULL, outlen);
1933 if (error || !req->newptr)
1936 if (req->newlen - req->newidx >= arg2 ||
1937 req->newlen - req->newidx < 0) {
1939 } else if (req->newlen - req->newidx == 0) {
1940 sx_xlock(&sysctlstringlock);
1941 ((char *)arg1)[0] = '\0';
1942 sx_xunlock(&sysctlstringlock);
1943 } else if (req->newfunc == sysctl_new_kernel) {
1944 arg2 = req->newlen - req->newidx;
1945 sx_xlock(&sysctlstringlock);
1946 error = SYSCTL_IN(req, arg1, arg2);
1948 ((char *)arg1)[arg2] = '\0';
1949 req->newidx += arg2;
1951 sx_xunlock(&sysctlstringlock);
1953 arg2 = req->newlen - req->newidx;
1954 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1956 error = SYSCTL_IN(req, tmparg, arg2);
1958 free(tmparg, M_SYSCTLTMP);
1962 sx_xlock(&sysctlstringlock);
1963 memcpy(arg1, tmparg, arg2);
1964 ((char *)arg1)[arg2] = '\0';
1965 sx_xunlock(&sysctlstringlock);
1966 free(tmparg, M_SYSCTLTMP);
1967 req->newidx += arg2;
1973 * Handle any kind of opaque data.
1974 * arg1 points to it, arg2 is the size.
1978 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1982 struct sysctl_req req2;
1985 * Attempt to get a coherent snapshot, by using the thread
1986 * pre-emption counter updated from within mi_switch() to
1987 * determine if we were pre-empted during a bcopy() or
1988 * copyout(). Make 3 attempts at doing this before giving up.
1989 * If we encounter an error, stop immediately.
1994 generation = curthread->td_generation;
1995 error = SYSCTL_OUT(req, arg1, arg2);
1999 if (generation != curthread->td_generation && tries < 3) {
2004 error = SYSCTL_IN(req, arg1, arg2);
2010 * Based on sysctl_handle_64() convert microseconds to a sbintime.
2013 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
2018 usec = sbttous(*(sbintime_t *)arg1);
2020 error = sysctl_handle_64(oidp, &usec, 0, req);
2021 if (error || !req->newptr)
2024 *(sbintime_t *)arg1 = ustosbt(usec);
2030 * Based on sysctl_handle_64() convert milliseconds to a sbintime.
2033 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
2038 msec = sbttoms(*(sbintime_t *)arg1);
2040 error = sysctl_handle_64(oidp, &msec, 0, req);
2041 if (error || !req->newptr)
2044 *(sbintime_t *)arg1 = mstosbt(msec);
2050 * Convert seconds to a struct timeval. Intended for use with
2051 * intervals and thus does not permit negative seconds.
2054 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
2062 error = sysctl_handle_int(oidp, &secs, 0, req);
2063 if (error || req->newptr == NULL)
2074 * Transfer functions to/from kernel space.
2075 * XXX: rather untested at this point
2078 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
2084 if (req->oldlen <= req->oldidx)
2087 if (i > req->oldlen - req->oldidx)
2088 i = req->oldlen - req->oldidx;
2090 bcopy(p, (char *)req->oldptr + req->oldidx, i);
2093 if (req->oldptr && i != l)
2099 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
2103 if (req->newlen - req->newidx < l)
2105 bcopy((const char *)req->newptr + req->newidx, p, l);
2111 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2112 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
2115 struct sysctl_req req;
2117 bzero(&req, sizeof req);
2123 req.oldlen = *oldlenp;
2125 req.validlen = req.oldlen;
2132 req.newlen = newlen;
2136 req.oldfunc = sysctl_old_kernel;
2137 req.newfunc = sysctl_new_kernel;
2138 req.lock = REQ_UNWIRED;
2140 error = sysctl_root(0, name, namelen, &req);
2142 if (req.lock == REQ_WIRED && req.validlen > 0)
2143 vsunlock(req.oldptr, req.validlen);
2145 if (error && error != ENOMEM)
2149 if (req.oldptr && req.oldidx > req.validlen)
2150 *retval = req.validlen;
2152 *retval = req.oldidx;
2158 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
2159 void *new, size_t newlen, size_t *retval, int flags)
2161 int oid[CTL_MAXNAME];
2162 size_t oidlen, plen;
2165 oid[0] = CTL_SYSCTL;
2166 oid[1] = CTL_SYSCTL_NAME2OID;
2167 oidlen = sizeof(oid);
2169 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
2170 (void *)name, strlen(name), &plen, flags);
2174 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
2175 new, newlen, retval, flags);
2180 * Transfer function to/from user space.
2183 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
2185 size_t i, len, origidx;
2188 origidx = req->oldidx;
2190 if (req->oldptr == NULL)
2193 * If we have not wired the user supplied buffer and we are currently
2194 * holding locks, drop a witness warning, as it's possible that
2195 * write operations to the user page can sleep.
2197 if (req->lock != REQ_WIRED)
2198 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2199 "sysctl_old_user()");
2201 len = req->validlen;
2205 if (i > len - origidx)
2207 if (req->lock == REQ_WIRED) {
2208 error = copyout_nofault(p, (char *)req->oldptr +
2211 error = copyout(p, (char *)req->oldptr + origidx, i);
2221 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2227 if (req->newlen - req->newidx < l)
2229 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2230 "sysctl_new_user()");
2231 error = copyin((const char *)req->newptr + req->newidx, p, l);
2237 * Wire the user space destination buffer. If set to a value greater than
2238 * zero, the len parameter limits the maximum amount of wired memory.
2241 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2246 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2248 if (req->lock != REQ_WIRED && req->oldptr &&
2249 req->oldfunc == sysctl_old_user) {
2250 if (wiredlen != 0) {
2251 ret = vslock(req->oldptr, wiredlen);
2258 req->lock = REQ_WIRED;
2259 req->validlen = wiredlen;
2265 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2266 int *nindx, struct sysctl_req *req)
2268 struct sysctl_oid_list *lsp;
2269 struct sysctl_oid *oid;
2270 struct sysctl_oid key;
2273 SYSCTL_ASSERT_LOCKED();
2274 lsp = &sysctl__children;
2276 while (indx < CTL_MAXNAME) {
2277 key.oid_number = name[indx];
2278 oid = RB_FIND(sysctl_oid_list, lsp, &key);
2283 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2284 if (oid->oid_handler != NULL || indx == namelen) {
2288 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2289 ("%s found DYING node %p", __func__, oid));
2292 lsp = SYSCTL_CHILDREN(oid);
2293 } else if (indx == namelen) {
2294 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2299 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2300 ("%s found DYING node %p", __func__, oid));
2310 * Traverse our tree, and find the right node, execute whatever it points
2311 * to, and return the resulting error code.
2315 sysctl_root(SYSCTL_HANDLER_ARGS)
2317 struct sysctl_oid *oid;
2318 struct rm_priotracker tracker;
2319 int error, indx, lvl;
2321 SYSCTL_RLOCK(&tracker);
2323 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2327 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2329 * You can't call a sysctl when it's a node, but has
2330 * no handler. Inform the user that it's a node.
2331 * The indx may or may not be the same as namelen.
2333 if (oid->oid_handler == NULL) {
2339 /* Is this sysctl writable? */
2340 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2345 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2347 #ifdef CAPABILITY_MODE
2349 * If the process is in capability mode, then don't permit reading or
2350 * writing unless specifically granted for the node.
2352 if (IN_CAPABILITY_MODE(req->td)) {
2353 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2354 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2361 /* Is this sysctl sensitive to securelevels? */
2362 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2363 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2364 error = securelevel_gt(req->td->td_ucred, lvl);
2369 /* Is this sysctl writable by only privileged users? */
2370 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2373 if (oid->oid_kind & CTLFLAG_PRISON)
2374 priv = PRIV_SYSCTL_WRITEJAIL;
2376 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2377 prison_owns_vnet(req->td->td_ucred))
2378 priv = PRIV_SYSCTL_WRITEJAIL;
2381 priv = PRIV_SYSCTL_WRITE;
2382 error = priv_check(req->td, priv);
2387 if (!oid->oid_handler) {
2392 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2393 arg1 = (int *)arg1 + indx;
2396 arg1 = oid->oid_arg1;
2397 arg2 = oid->oid_arg2;
2400 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2406 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2407 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2409 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2412 SYSCTL_RUNLOCK(&tracker);
2416 #ifndef _SYS_SYSPROTO_H_
2417 struct __sysctl_args {
2427 sys___sysctl(struct thread *td, struct __sysctl_args *uap)
2429 int error, i, name[CTL_MAXNAME];
2432 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2435 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2439 error = userland_sysctl(td, name, uap->namelen,
2440 uap->old, uap->oldlenp, 0,
2441 uap->new, uap->newlen, &j, 0);
2442 if (error && error != ENOMEM)
2445 i = copyout(&j, uap->oldlenp, sizeof(j));
2453 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2454 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2455 int flags, bool inkernel)
2457 int oid[CTL_MAXNAME];
2463 if (namelen > MAXPATHLEN || namelen == 0)
2466 if (namelen > sizeof(namebuf))
2467 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2468 error = copyin(oname, name, namelen);
2472 oid[0] = CTL_SYSCTL;
2473 oid[1] = CTL_SYSCTL_NAME2OID;
2474 oidlen = sizeof(oid);
2475 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2479 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2480 inkernel, new, newlen, retval, flags);
2483 if (namelen > sizeof(namebuf))
2484 free(name, M_SYSCTL);
2488 #ifndef _SYS_SYSPROTO_H_
2489 struct __sysctlbyname_args {
2499 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2504 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2505 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2508 if (uap->oldlenp != NULL)
2509 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2515 * This is used from various compatibility syscalls too. That's why name
2516 * must be in kernel space.
2519 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2520 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2521 size_t *retval, int flags)
2523 int error = 0, memlocked;
2524 struct sysctl_req req;
2526 bzero(&req, sizeof req);
2533 req.oldlen = *oldlenp;
2535 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2540 req.validlen = req.oldlen;
2544 req.newlen = newlen;
2548 req.oldfunc = sysctl_old_user;
2549 req.newfunc = sysctl_new_user;
2550 req.lock = REQ_UNWIRED;
2553 if (KTRPOINT(curthread, KTR_SYSCTL))
2554 ktrsysctl(name, namelen);
2557 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2559 sx_xlock(&sysctlmemlock);
2561 CURVNET_SET(TD_TO_VNET(td));
2566 error = sysctl_root(0, name, namelen, &req);
2567 if (error != EAGAIN)
2569 kern_yield(PRI_USER);
2574 if (req.lock == REQ_WIRED && req.validlen > 0)
2575 vsunlock(req.oldptr, req.validlen);
2577 sx_xunlock(&sysctlmemlock);
2579 if (error && error != ENOMEM)
2583 if (req.oldptr && req.oldidx > req.validlen)
2584 *retval = req.validlen;
2586 *retval = req.oldidx;
2592 * Drain into a sysctl struct. The user buffer should be wired if a page
2593 * fault would cause issue.
2596 sbuf_sysctl_drain(void *arg, const char *data, int len)
2598 struct sysctl_req *req = arg;
2601 error = SYSCTL_OUT(req, data, len);
2602 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2603 return (error == 0 ? len : -error);
2607 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2608 struct sysctl_req *req)
2611 /* Supply a default buffer size if none given. */
2612 if (buf == NULL && length == 0)
2614 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2615 sbuf_set_drain(s, sbuf_sysctl_drain, req);
2621 /* The current OID the debugger is working with */
2622 static struct sysctl_oid *g_ddb_oid;
2624 /* The current flags specified by the user */
2625 static int g_ddb_sysctl_flags;
2627 /* Check to see if the last sysctl printed */
2628 static int g_ddb_sysctl_printed;
2630 static const int ctl_sign[CTLTYPE+1] = {
2639 static const int ctl_size[CTLTYPE+1] = {
2640 [CTLTYPE_INT] = sizeof(int),
2641 [CTLTYPE_UINT] = sizeof(u_int),
2642 [CTLTYPE_LONG] = sizeof(long),
2643 [CTLTYPE_ULONG] = sizeof(u_long),
2644 [CTLTYPE_S8] = sizeof(int8_t),
2645 [CTLTYPE_S16] = sizeof(int16_t),
2646 [CTLTYPE_S32] = sizeof(int32_t),
2647 [CTLTYPE_S64] = sizeof(int64_t),
2648 [CTLTYPE_U8] = sizeof(uint8_t),
2649 [CTLTYPE_U16] = sizeof(uint16_t),
2650 [CTLTYPE_U32] = sizeof(uint32_t),
2651 [CTLTYPE_U64] = sizeof(uint64_t),
2654 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */
2655 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */
2656 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */
2657 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */
2659 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */
2661 static const char db_sysctl_modifs[] = {
2665 static const int db_sysctl_modif_values[] = {
2666 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2667 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2670 /* Handlers considered safe to print while recursing */
2671 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2679 sysctl_handle_string,
2680 sysctl_handle_opaque,
2684 * Use in place of sysctl_old_kernel to print sysctl values.
2686 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2689 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2691 const u_char *val, *p;
2693 size_t intlen, slen;
2696 int sign, ctltype, hexlen, xflag, error;
2698 /* Suppress false-positive GCC uninitialized variable warnings */
2710 /* We are going to print */
2711 g_ddb_sysctl_printed = 1;
2713 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2715 ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2716 sign = ctl_sign[ctltype];
2717 intlen = ctl_size[ctltype];
2721 case CTLTYPE_STRING:
2722 db_printf("%.*s", (int) len, (const char *) p);
2738 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2740 while (len >= intlen) {
2744 umv = *(const u_int *)p;
2745 mv = *(const int *)p;
2749 umv = *(const u_long *)p;
2750 mv = *(const long *)p;
2754 umv = *(const uint8_t *)p;
2755 mv = *(const int8_t *)p;
2759 umv = *(const uint16_t *)p;
2760 mv = *(const int16_t *)p;
2764 umv = *(const uint32_t *)p;
2765 mv = *(const int32_t *)p;
2769 umv = *(const uint64_t *)p;
2770 mv = *(const int64_t *)p;
2774 db_printf("%s", sep1);
2776 db_printf("%#0*jx", hexlen, umv);
2778 db_printf("%ju", umv);
2779 else if (g_ddb_oid->oid_fmt[1] == 'K') {
2780 /* Kelvins are currently unsupported. */
2784 db_printf("%jd", mv);
2793 case CTLTYPE_OPAQUE:
2794 /* TODO: Support struct functions. */
2798 db_printf("Format:%s Length:%zu Dump:0x",
2799 g_ddb_oid->oid_fmt, len);
2800 while (len-- && (xflag || p < val + 16))
2801 db_printf("%02x", *p++);
2802 if (!xflag && len > 16)
2809 req->oldidx += slen;
2814 * Avoid setting new sysctl values from the debugger
2817 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2823 /* Changing sysctls from the debugger is currently unsupported */
2828 * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2829 * Instead of copying any output to a buffer we'll dump it right to
2833 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2834 void *old, size_t *oldlenp, size_t *retval, int flags)
2836 struct sysctl_req req;
2839 /* Setup the request */
2840 bzero(&req, sizeof req);
2841 req.td = kdb_thread;
2842 req.oldfunc = sysctl_old_ddb;
2843 req.newfunc = sysctl_new_ddb;
2844 req.lock = REQ_UNWIRED;
2846 req.oldlen = *oldlenp;
2848 req.validlen = req.oldlen;
2853 /* Setup our globals for sysctl_old_ddb */
2855 g_ddb_sysctl_flags = flags;
2856 g_ddb_sysctl_printed = 0;
2858 error = sysctl_root(0, name, namelen, &req);
2862 g_ddb_sysctl_flags = 0;
2865 if (req.oldptr && req.oldidx > req.validlen)
2866 *retval = req.validlen;
2868 *retval = req.oldidx;
2874 * Show a sysctl's name
2877 db_show_oid_name(int *oid, size_t nlen)
2879 struct sysctl_oid *oidp;
2880 int qoid[CTL_MAXNAME + 2];
2883 qoid[0] = CTL_SYSCTL;
2884 qoid[1] = CTL_SYSCTL_NAME;
2885 memcpy(qoid + 2, oid, nlen * sizeof(int));
2887 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2889 db_error("sysctl name oid");
2891 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2893 db_error("sysctl name");
2897 * Check to see if an OID is safe to print from ddb.
2900 db_oid_safe(const struct sysctl_oid *oidp)
2902 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2903 if (oidp->oid_handler == db_safe_handlers[i])
2911 * Show a sysctl at a specific OID
2912 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2915 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2917 int error, xflag, oflag, Nflag, nflag;
2920 xflag = flags & DB_SYSCTL_HEX;
2921 oflag = flags & DB_SYSCTL_OPAQUE;
2922 nflag = flags & DB_SYSCTL_VALUE_ONLY;
2923 Nflag = flags & DB_SYSCTL_NAME_ONLY;
2925 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2930 db_show_oid_name(oid, nlen);
2936 db_show_oid_name(oid, nlen);
2940 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2941 db_printf("Skipping, unsafe to print while recursing.");
2946 /* Try once, and ask about the size */
2948 error = db_sysctl(oidp, oid, nlen,
2949 NULL, NULL, &len, flags);
2953 if (!g_ddb_sysctl_printed)
2954 /* Lie about the size */
2955 error = db_sysctl(oidp, oid, nlen,
2956 (void *) 1, &len, NULL, flags);
2964 * Show all sysctls under a specific OID
2965 * Compare to sysctl_all from sbin/sysctl/sysctl.c
2968 db_show_sysctl_all(int *oid, size_t len, int flags)
2970 struct sysctl_oid *oidp;
2971 int qoid[CTL_MAXNAME + 2], next[CTL_MAXNAME];
2974 qoid[0] = CTL_SYSCTL;
2975 qoid[1] = CTL_SYSCTL_NEXT;
2978 memcpy(&qoid[2], oid, nlen * sizeof(int));
2985 size_t nextsize = sizeof(next);
2987 error = kernel_sysctl(kdb_thread, qoid, nlen + 2,
2988 next, &nextsize, NULL, 0, &nlen, 0);
2990 if (error == ENOENT)
2993 db_error("sysctl(next)");
2996 nlen /= sizeof(int);
2998 if (nlen < (unsigned int)len)
3001 if (memcmp(&oid[0], &next[0], len * sizeof(int)) != 0)
3004 /* Find the OID in question */
3005 error = sysctl_find_oid(next, nlen, &oidp, NULL, NULL);
3009 (void)db_show_oid(oidp, next, nlen, flags | DB_SYSCTL_SAFE_ONLY);
3014 memcpy(&qoid[2 + len], &next[len], (nlen - len) * sizeof(int));
3019 * Show a sysctl by its user facing string
3022 db_sysctlbyname(const char *name, int flags)
3024 struct sysctl_oid *oidp;
3025 int oid[CTL_MAXNAME];
3028 error = name2oid(name, oid, &nlen, &oidp);
3033 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
3034 db_show_sysctl_all(oid, nlen, flags);
3036 error = db_show_oid(oidp, oid, nlen, flags);
3043 db_sysctl_cmd_usage(void)
3046 " sysctl [/Nnox] <sysctl> \n"
3048 " <sysctl> The name of the sysctl to show. \n"
3050 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n"
3051 " This will work for most sysctls, but should not be used \n"
3052 " with sysctls that are known to malloc. \n"
3054 " While recursing any \"unsafe\" sysctls will be skipped. \n"
3055 " Call sysctl directly on the sysctl to try printing the \n"
3056 " skipped sysctl. This is unsafe and may make the ddb \n"
3057 " session unusable. \n"
3060 " /N Display only the name of the sysctl. \n"
3061 " /n Display only the value of the sysctl. \n"
3062 " /o Display opaque values. \n"
3063 " /x Display the sysctl in hex. \n"
3066 "sysctl vm.v_free_min \n"
3067 "vn.v_free_min: 12669 \n"
3072 * Show a specific sysctl similar to sysctl (8).
3074 DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN)
3076 char name[TOK_STRING_SIZE];
3077 int error, i, t, flags;
3079 /* Parse the modifiers */
3080 t = db_read_token();
3081 if (t == tSLASH || t == tMINUS) {
3082 t = db_read_token();
3084 db_printf("Bad modifier\n");
3088 db_strcpy(modif, db_tok_string);
3096 for (i = 0; i < nitems(db_sysctl_modifs); i++) {
3097 if (strchr(modif, db_sysctl_modifs[i])) {
3098 flags |= db_sysctl_modif_values[i];
3102 /* Parse the sysctl names */
3103 t = db_read_token();
3105 db_printf("Need sysctl name\n");
3110 /* Copy the name into a temporary buffer */
3111 db_strcpy(name, db_tok_string);
3113 /* Ensure there is no trailing cruft */
3114 t = db_read_token();
3116 db_printf("Unexpected sysctl argument\n");
3121 error = db_sysctlbyname(name, flags);
3122 if (error == ENOENT) {
3123 db_printf("unknown oid: '%s'\n", db_tok_string);
3126 db_printf("%s: error: %d\n", db_tok_string, error);
3131 /* Ensure we eat all of our text */
3134 if (error == EINVAL) {
3135 db_sysctl_cmd_usage();