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
38 #include <sys/cdefs.h>
39 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
42 #include "opt_sysctl.h"
44 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/capsicum.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/rmlock.h>
61 #include <sys/sysproto.h>
64 #include <sys/ktrace.h>
69 #include <ddb/db_lex.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_extern.h>
79 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
80 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
81 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
83 RB_GENERATE(sysctl_oid_list, sysctl_oid, oid_link, cmp_sysctl_oid);
86 * The sysctllock protects the MIB tree. It also protects sysctl
87 * contexts used with dynamic sysctls. The sysctl_register_oid() and
88 * sysctl_unregister_oid() routines require the sysctllock to already
89 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
90 * provided for the few places in the kernel which need to use that
91 * API rather than using the dynamic API. Use of the dynamic API is
92 * strongly encouraged for most code.
94 * The sysctlmemlock is used to limit the amount of user memory wired for
95 * sysctl requests. This is implemented by serializing any userland
96 * sysctl requests larger than a single page via an exclusive lock.
98 * The sysctlstringlock is used to protect concurrent access to writable
99 * string nodes in sysctl_handle_string().
101 static struct rmlock sysctllock;
102 static struct sx __exclusive_cache_line sysctlmemlock;
103 static struct sx sysctlstringlock;
105 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
106 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
107 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
108 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
109 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
110 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
111 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
112 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
113 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
115 #define SYSCTL_SLEEP(ch, wmesg, timo) \
116 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
118 static int sysctl_root(SYSCTL_HANDLER_ARGS);
121 struct sysctl_oid_list sysctl__children = RB_INITIALIZER(&sysctl__children);
123 static char* sysctl_escape_name(const char*);
124 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
126 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
127 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
128 static int name2oid(const char *, int *, int *, struct sysctl_oid **);
130 static struct sysctl_oid *
131 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
133 struct sysctl_oid *oidp;
135 SYSCTL_ASSERT_LOCKED();
136 SYSCTL_FOREACH(oidp, list) {
137 if (strcmp(oidp->oid_name, name) == 0) {
144 static struct sysctl_oid *
145 sysctl_find_oidnamelen(const char *name, size_t len,
146 struct sysctl_oid_list *list)
148 struct sysctl_oid *oidp;
150 SYSCTL_ASSERT_LOCKED();
151 SYSCTL_FOREACH(oidp, list) {
152 if (strncmp(oidp->oid_name, name, len) == 0 &&
153 oidp->oid_name[len] == '\0')
160 * Initialization of the MIB tree.
162 * Order by number in each list.
179 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
180 struct sysctl_req *req, struct rm_priotracker *tracker)
184 if (oid->oid_kind & CTLFLAG_DYN)
185 atomic_add_int(&oid->oid_running, 1);
188 SYSCTL_RUNLOCK(tracker);
193 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
194 * untill we're ready to remove all traces of Giant from sysctl(9).
196 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
197 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
199 error = oid->oid_handler(oid, arg1, arg2, req);
200 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
201 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
204 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
207 SYSCTL_RLOCK(tracker);
211 if (oid->oid_kind & CTLFLAG_DYN) {
212 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
213 (oid->oid_kind & CTLFLAG_DYING) != 0)
214 wakeup(&oid->oid_running);
221 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
223 struct sysctl_req req;
224 struct sysctl_oid *curr;
227 ssize_t rem = sizeof(path);
229 uint8_t data[512] __aligned(sizeof(uint64_t));
235 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
236 len = strlen(curr->oid_name);
241 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
244 memcpy(path + rem, curr->oid_name, len);
246 path[rem + len] = '.';
249 memset(&req, 0, sizeof(req));
252 req.oldfunc = sysctl_old_kernel;
253 req.newfunc = sysctl_new_kernel;
254 req.lock = REQ_UNWIRED;
256 switch (oidp->oid_kind & CTLTYPE) {
258 if (getenv_array(path + rem, data, sizeof(data), &size,
259 sizeof(int), GETENV_SIGNED) == 0)
265 if (getenv_array(path + rem, data, sizeof(data), &size,
266 sizeof(int), GETENV_UNSIGNED) == 0)
272 if (getenv_array(path + rem, data, sizeof(data), &size,
273 sizeof(long), GETENV_SIGNED) == 0)
279 if (getenv_array(path + rem, data, sizeof(data), &size,
280 sizeof(long), GETENV_UNSIGNED) == 0)
286 if (getenv_array(path + rem, data, sizeof(data), &size,
287 sizeof(int8_t), GETENV_SIGNED) == 0)
293 if (getenv_array(path + rem, data, sizeof(data), &size,
294 sizeof(int16_t), GETENV_SIGNED) == 0)
300 if (getenv_array(path + rem, data, sizeof(data), &size,
301 sizeof(int32_t), GETENV_SIGNED) == 0)
307 if (getenv_array(path + rem, data, sizeof(data), &size,
308 sizeof(int64_t), GETENV_SIGNED) == 0)
314 if (getenv_array(path + rem, data, sizeof(data), &size,
315 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
321 if (getenv_array(path + rem, data, sizeof(data), &size,
322 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
328 if (getenv_array(path + rem, data, sizeof(data), &size,
329 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
335 if (getenv_array(path + rem, data, sizeof(data), &size,
336 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
342 penv = kern_getenv(path + rem);
345 req.newlen = strlen(penv);
351 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
352 oidp->oid_arg2, &req, NULL);
354 printf("Setting sysctl %s failed: %d\n", path + rem, error);
360 * Locate the path to a given oid. Returns the length of the resulting path,
361 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
365 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
369 SYSCTL_ASSERT_LOCKED();
372 * Do a depth-first search of the oid tree, looking for 'needle'. Start
373 * with the first child of the root.
375 nodes[indx] = RB_MIN(sysctl_oid_list, &sysctl__children);
377 if (nodes[indx] == needle)
380 if (nodes[indx] == NULL) {
381 /* Node has no more siblings, so back up to parent. */
383 /* Retreat to root, so give up. */
386 } else if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
387 /* Node has children. */
388 if (++indx == CTL_MAXNAME) {
389 /* Max search depth reached, so give up. */
392 /* Start with the first child. */
393 nodes[indx] = RB_MIN(sysctl_oid_list,
394 &nodes[indx - 1]->oid_children);
397 /* Consider next sibling. */
398 nodes[indx] = RB_NEXT(sysctl_oid_list, NULL, nodes[indx]);
404 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
406 struct sysctl_oid *nodes[CTL_MAXNAME];
411 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
412 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
414 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
416 rc = sysctl_search_oid(nodes, leaf);
418 for (i = 0; i < rc; i++)
419 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
422 sbuf_cat(&sb, leaf->oid_name);
424 sbuf_cat(&sb, ")!\n");
426 (void)sbuf_finish(&sb);
431 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
433 struct rm_priotracker tracker;
435 SYSCTL_RLOCK(&tracker);
436 sysctl_warn_reuse(__func__, oidp);
437 SYSCTL_RUNLOCK(&tracker);
440 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
441 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
446 sysctl_register_oid(struct sysctl_oid *oidp)
448 struct sysctl_oid_list *parent = oidp->oid_parent;
449 struct sysctl_oid *p, key;
454 * First check if another oid with the same name already
455 * exists in the parent's list.
457 SYSCTL_ASSERT_WLOCKED();
458 p = sysctl_find_oidname(oidp->oid_name, parent);
460 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
464 sysctl_warn_reuse(__func__, p);
468 /* get current OID number */
469 oid_number = oidp->oid_number;
472 #error "OID_AUTO is expected to be a negative value"
475 * Any negative OID number qualifies as OID_AUTO. Valid OID
476 * numbers should always be positive.
478 * NOTE: DO NOT change the starting value here, change it in
479 * <sys/sysctl.h>, and make sure it is at least 256 to
480 * accommodate e.g. net.inet.raw as a static sysctl node.
482 if (oid_number < 0) {
486 * By decrementing the next OID number we spend less
487 * time inserting the OIDs into a sorted list.
489 if (--newoid < CTL_AUTO_START)
496 * Insert the OID into the parent's list sorted by OID number.
498 key.oid_number = oid_number;
499 p = RB_NFIND(sysctl_oid_list, parent, &key);
500 while (p != NULL && oid_number == p->oid_number) {
501 /* get the next valid OID number */
502 if (oid_number < CTL_AUTO_START ||
503 oid_number == 0x7fffffff) {
504 /* wraparound - restart */
505 oid_number = CTL_AUTO_START;
506 /* don't loop forever */
508 panic("sysctl: Out of OID numbers\n");
509 key.oid_number = oid_number;
510 p = RB_NFIND(sysctl_oid_list, parent, &key);
513 p = RB_NEXT(sysctl_oid_list, NULL, p);
516 /* check for non-auto OID number collision */
517 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
518 oid_number >= CTL_AUTO_START) {
519 printf("sysctl: OID number(%d) is already in use for '%s'\n",
520 oidp->oid_number, oidp->oid_name);
522 /* update the OID number, if any */
523 oidp->oid_number = oid_number;
524 RB_INSERT(sysctl_oid_list, parent, oidp);
526 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
527 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
528 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
531 * Can fetch value multiple times for VNET loader tunables.
532 * Only fetch once for non-VNET loader tunables.
534 if ((oidp->oid_kind & CTLFLAG_VNET) == 0)
536 oidp->oid_kind |= CTLFLAG_NOFETCH;
537 /* try to fetch value from kernel environment */
538 sysctl_load_tunable_by_oid_locked(oidp);
543 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
547 * Mark the leaf as dormant if it's not to be immediately enabled.
548 * We do not disable nodes as they can be shared between modules
549 * and it is always safe to access a node.
551 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
552 ("internal flag is set in oid_kind"));
553 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
554 oidp->oid_kind |= CTLFLAG_DORMANT;
555 sysctl_register_oid(oidp);
559 sysctl_enable_oid(struct sysctl_oid *oidp)
562 SYSCTL_ASSERT_WLOCKED();
563 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
564 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
565 ("sysctl node is marked as dormant"));
568 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
569 ("enabling already enabled sysctl oid"));
570 oidp->oid_kind &= ~CTLFLAG_DORMANT;
574 sysctl_unregister_oid(struct sysctl_oid *oidp)
578 SYSCTL_ASSERT_WLOCKED();
579 if (oidp->oid_number == OID_AUTO) {
583 if (RB_REMOVE(sysctl_oid_list, oidp->oid_parent, oidp))
588 * This can happen when a module fails to register and is
589 * being unloaded afterwards. It should not be a panic()
593 printf("%s: failed(%d) to unregister sysctl(%s)\n",
594 __func__, error, oidp->oid_name);
598 /* Initialize a new context to keep track of dynamically added sysctls. */
600 sysctl_ctx_init(struct sysctl_ctx_list *c)
608 * No locking here, the caller is responsible for not adding
609 * new nodes to a context until after this function has
616 /* Free the context, and destroy all dynamic oids registered in this context */
618 sysctl_ctx_free(struct sysctl_ctx_list *clist)
620 struct sysctl_ctx_entry *e, *e1;
625 * First perform a "dry run" to check if it's ok to remove oids.
627 * XXX This algorithm is a hack. But I don't know any
628 * XXX better solution for now...
631 TAILQ_FOREACH(e, clist, link) {
632 error = sysctl_remove_oid_locked(e->entry, 0, 0);
637 * Restore deregistered entries, either from the end,
638 * or from the place where error occurred.
639 * e contains the entry that was not unregistered
642 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
644 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
646 sysctl_register_oid(e1->entry);
647 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
653 /* Now really delete the entries */
654 e = TAILQ_FIRST(clist);
656 e1 = TAILQ_NEXT(e, link);
657 error = sysctl_remove_oid_locked(e->entry, 1, 0);
659 panic("sysctl_remove_oid: corrupt tree, entry: %s",
661 free(e, M_SYSCTLOID);
668 /* Add an entry to the context */
669 struct sysctl_ctx_entry *
670 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
672 struct sysctl_ctx_entry *e;
674 SYSCTL_ASSERT_WLOCKED();
675 if (clist == NULL || oidp == NULL)
677 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
679 TAILQ_INSERT_HEAD(clist, e, link);
683 /* Find an entry in the context */
684 struct sysctl_ctx_entry *
685 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
687 struct sysctl_ctx_entry *e;
689 SYSCTL_ASSERT_WLOCKED();
690 if (clist == NULL || oidp == NULL)
692 TAILQ_FOREACH(e, clist, link) {
693 if (e->entry == oidp)
700 * Delete an entry from the context.
701 * NOTE: this function doesn't free oidp! You have to remove it
702 * with sysctl_remove_oid().
705 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
707 struct sysctl_ctx_entry *e;
709 if (clist == NULL || oidp == NULL)
712 e = sysctl_ctx_entry_find(clist, oidp);
714 TAILQ_REMOVE(clist, e, link);
716 free(e, M_SYSCTLOID);
725 * Remove dynamically created sysctl trees.
726 * oidp - top of the tree to be removed
727 * del - if 0 - just deregister, otherwise free up entries as well
728 * recurse - if != 0 traverse the subtree to be deleted
731 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
736 error = sysctl_remove_oid_locked(oidp, del, recurse);
742 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
743 int del, int recurse)
745 struct sysctl_oid *p;
750 p = sysctl_find_oidname(name, &parent->oid_children);
752 error = sysctl_remove_oid_locked(p, del, recurse);
759 * Duplicate the provided string, escaping any illegal characters. The result
760 * must be freed when no longer in use.
762 * The list of illegal characters is ".".
765 sysctl_escape_name(const char* orig)
767 int i, s = 0, d = 0, nillegals = 0;
770 /* First count the number of illegal characters */
771 for (i = 0; orig[i] != '\0'; i++) {
776 /* Allocate storage for new string */
777 new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
779 /* Copy the name, escaping characters as we go */
780 while (orig[s] != '\0') {
781 if (orig[s] == '.') {
782 /* %25 is the hexadecimal representation of '.' */
788 new[d++] = orig[s++];
792 /* Finally, nul-terminate */
799 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
801 struct sysctl_oid *p, *tmp;
804 SYSCTL_ASSERT_WLOCKED();
807 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
808 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
813 * WARNING: normal method to do this should be through
814 * sysctl_ctx_free(). Use recursing as the last resort
815 * method to purge your sysctl tree of leftovers...
816 * However, if some other code still references these nodes,
819 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
820 if (oidp->oid_refcnt == 1) {
821 for(p = RB_MIN(sysctl_oid_list, &oidp->oid_children);
822 p != NULL; p = tmp) {
824 printf("Warning: failed attempt to "
825 "remove oid %s with child %s\n",
826 oidp->oid_name, p->oid_name);
829 tmp = RB_NEXT(sysctl_oid_list,
830 &oidp->oid_children, p);
831 error = sysctl_remove_oid_locked(p, del,
838 if (oidp->oid_refcnt > 1 ) {
841 if (oidp->oid_refcnt == 0) {
842 printf("Warning: bad oid_refcnt=%u (%s)!\n",
843 oidp->oid_refcnt, oidp->oid_name);
846 sysctl_unregister_oid(oidp);
849 * Wait for all threads running the handler to drain.
850 * This preserves the previous behavior when the
851 * sysctl lock was held across a handler invocation,
852 * and is necessary for module unload correctness.
854 while (oidp->oid_running > 0) {
855 oidp->oid_kind |= CTLFLAG_DYING;
856 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
859 free(__DECONST(char *, oidp->oid_descr),
862 free(__DECONST(char *, oidp->oid_label),
864 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
865 free(oidp, M_SYSCTLOID);
871 * Create new sysctls at run time.
872 * clist may point to a valid context initialized with sysctl_ctx_init().
875 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
876 int number, const char *name, int kind, void *arg1, intmax_t arg2,
877 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
880 struct sysctl_oid *oidp;
883 /* You have to hook up somewhere.. */
886 escaped = sysctl_escape_name(name);
887 /* Check if the node already exists, otherwise create it */
889 oidp = sysctl_find_oidname(escaped, parent);
891 free(escaped, M_SYSCTLOID);
892 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
894 /* Update the context */
896 sysctl_ctx_entry_add(clist, oidp);
900 sysctl_warn_reuse(__func__, oidp);
905 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
906 oidp->oid_parent = parent;
907 RB_INIT(&oidp->oid_children);
908 oidp->oid_number = number;
909 oidp->oid_refcnt = 1;
910 oidp->oid_name = escaped;
911 oidp->oid_handler = handler;
912 oidp->oid_kind = CTLFLAG_DYN | kind;
913 oidp->oid_arg1 = arg1;
914 oidp->oid_arg2 = arg2;
917 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
919 oidp->oid_label = strdup(label, M_SYSCTLOID);
920 /* Update the context, if used */
922 sysctl_ctx_entry_add(clist, oidp);
923 /* Register this oid */
924 sysctl_register_oid(oidp);
930 * Rename an existing oid.
933 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
938 newname = strdup(name, M_SYSCTLOID);
940 oldname = __DECONST(char *, oidp->oid_name);
941 oidp->oid_name = newname;
943 free(oldname, M_SYSCTLOID);
947 * Reparent an existing oid.
950 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
952 struct sysctl_oid *oidp;
955 if (oid->oid_parent == parent) {
959 oidp = sysctl_find_oidname(oid->oid_name, parent);
964 sysctl_unregister_oid(oid);
965 oid->oid_parent = parent;
966 oid->oid_number = OID_AUTO;
967 sysctl_register_oid(oid);
973 * Register the kernel's oids on startup.
975 SET_DECLARE(sysctl_set, struct sysctl_oid);
978 sysctl_register_all(void *arg)
980 struct sysctl_oid **oidp;
982 sx_init(&sysctlmemlock, "sysctl mem");
983 sx_init(&sysctlstringlock, "sysctl string handler");
986 SET_FOREACH(oidp, sysctl_set)
987 sysctl_register_oid(*oidp);
990 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
994 sysctl_setenv_vnet(void *arg __unused, const char *name)
996 struct sysctl_oid *oidp;
997 int oid[CTL_MAXNAME];
1001 error = name2oid(name, oid, &nlen, &oidp);
1005 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1006 (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1007 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1008 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1009 /* Update value from kernel environment */
1010 sysctl_load_tunable_by_oid_locked(oidp);
1017 sysctl_unsetenv_vnet(void *arg __unused, const char *name)
1019 struct sysctl_oid *oidp;
1020 int oid[CTL_MAXNAME];
1025 * The setenv / unsetenv event handlers are invoked by kern_setenv() /
1026 * kern_unsetenv() without exclusive locks. It is rare but still possible
1027 * that the invoke order of event handlers is different from that of
1028 * kern_setenv() and kern_unsetenv().
1029 * Re-check environment variable string to make sure it is unset.
1033 error = name2oid(name, oid, &nlen, &oidp);
1037 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
1038 (oidp->oid_kind & CTLFLAG_VNET) != 0 &&
1039 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
1040 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
1043 switch (oidp->oid_kind & CTLTYPE) {
1050 size = sizeof(long);
1054 size = sizeof(int8_t);
1058 size = sizeof(int16_t);
1062 size = sizeof(int32_t);
1066 size = sizeof(int64_t);
1068 case CTLTYPE_STRING:
1069 MPASS(oidp->oid_arg2 > 0);
1070 size = oidp->oid_arg2;
1075 vnet_restore_init(oidp->oid_arg1, size);
1082 * Register the kernel's setenv / unsetenv events.
1084 EVENTHANDLER_DEFINE(setenv, sysctl_setenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1085 EVENTHANDLER_DEFINE(unsetenv, sysctl_unsetenv_vnet, NULL, EVENTHANDLER_PRI_ANY);
1091 * These functions implement a presently undocumented interface
1092 * used by the sysctl program to walk the tree, and get the type
1093 * so it can print the value.
1094 * This interface is under work and consideration, and should probably
1095 * be killed with a big axe by the first person who can find the time.
1096 * (be aware though, that the proper interface isn't as obvious as it
1097 * may seem, there are various conflicting requirements.
1099 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
1100 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
1102 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring
1104 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
1106 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
1107 * for the "..." OID.
1108 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
1110 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
1112 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring
1118 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1121 struct sysctl_oid *oidp;
1123 SYSCTL_ASSERT_LOCKED();
1124 SYSCTL_FOREACH(oidp, l) {
1128 printf("%d %s ", oidp->oid_number, oidp->oid_name);
1131 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1132 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1134 if (oidp->oid_handler)
1135 printf(" *Handler");
1137 switch (oidp->oid_kind & CTLTYPE) {
1140 if (!oidp->oid_handler) {
1141 sysctl_sysctl_debug_dump_node(
1142 SYSCTL_CHILDREN(oidp), i + 2);
1145 case CTLTYPE_INT: printf(" Int\n"); break;
1146 case CTLTYPE_UINT: printf(" u_int\n"); break;
1147 case CTLTYPE_LONG: printf(" Long\n"); break;
1148 case CTLTYPE_ULONG: printf(" u_long\n"); break;
1149 case CTLTYPE_STRING: printf(" String\n"); break;
1150 case CTLTYPE_S8: printf(" int8_t\n"); break;
1151 case CTLTYPE_S16: printf(" int16_t\n"); break;
1152 case CTLTYPE_S32: printf(" int32_t\n"); break;
1153 case CTLTYPE_S64: printf(" int64_t\n"); break;
1154 case CTLTYPE_U8: printf(" uint8_t\n"); break;
1155 case CTLTYPE_U16: printf(" uint16_t\n"); break;
1156 case CTLTYPE_U32: printf(" uint32_t\n"); break;
1157 case CTLTYPE_U64: printf(" uint64_t\n"); break;
1158 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1159 default: printf("\n");
1165 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1167 struct rm_priotracker tracker;
1170 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1173 SYSCTL_RLOCK(&tracker);
1174 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1175 SYSCTL_RUNLOCK(&tracker);
1179 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1180 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1184 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1186 int *name = (int *) arg1;
1187 u_int namelen = arg2;
1189 struct sysctl_oid *oid, key;
1190 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1191 struct rm_priotracker tracker;
1194 error = sysctl_wire_old_buffer(req, 0);
1198 SYSCTL_RLOCK(&tracker);
1201 snprintf(buf,sizeof(buf),"%d",*name);
1203 error = SYSCTL_OUT(req, ".", 1);
1205 error = SYSCTL_OUT(req, buf, strlen(buf));
1213 key.oid_number = *name;
1214 oid = RB_FIND(sysctl_oid_list, lsp, &key);
1217 error = SYSCTL_OUT(req, ".", 1);
1219 error = SYSCTL_OUT(req, oid->oid_name,
1220 strlen(oid->oid_name));
1227 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE &&
1229 lsp2 = SYSCTL_CHILDREN(oid);
1233 error = SYSCTL_OUT(req, "", 1);
1235 SYSCTL_RUNLOCK(&tracker);
1240 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1243 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1244 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1246 enum sysctl_iter_action {
1247 ITER_SIBLINGS, /* Not matched, continue iterating siblings */
1248 ITER_CHILDREN, /* Node has children we need to iterate over them */
1249 ITER_FOUND, /* Matching node was found */
1253 * Tries to find the next node for @name and @namelen.
1255 * Returns next action to take.
1257 static enum sysctl_iter_action
1258 sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen,
1262 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1263 return (ITER_SIBLINGS);
1265 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1266 return (ITER_SIBLINGS);
1270 * We have reached a node with a full name match and are
1271 * looking for the next oid in its children.
1273 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1275 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1276 * has a handler) and move on to the children.
1279 return (ITER_FOUND);
1280 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1281 return (ITER_FOUND);
1282 /* If node does not have an iterator, treat it as leaf */
1283 if (oidp->oid_handler)
1284 return (ITER_FOUND);
1286 /* Report oid as a node to iterate */
1287 return (ITER_CHILDREN);
1291 * No match yet. Continue seeking the given name.
1293 * We are iterating in order by oid_number, so skip oids lower
1294 * than the one we are looking for.
1296 * When the current oid_number is higher than the one we seek,
1297 * that means we have reached the next oid in the sequence and
1300 * If the oid_number matches the name at this level then we
1301 * have to find a node to continue searching at the next level.
1303 if (oidp->oid_number < *name)
1304 return (ITER_SIBLINGS);
1305 if (oidp->oid_number > *name) {
1307 * We have reached the next oid.
1309 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1311 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1312 * has a handler) and move on to the children.
1315 return (ITER_FOUND);
1316 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1317 return (ITER_FOUND);
1318 /* If node does not have an iterator, treat it as leaf */
1319 if (oidp->oid_handler)
1320 return (ITER_FOUND);
1321 return (ITER_CHILDREN);
1324 /* match at a current level */
1325 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1326 return (ITER_SIBLINGS);
1327 if (oidp->oid_handler)
1328 return (ITER_SIBLINGS);
1330 return (ITER_CHILDREN);
1334 * Recursively walk the sysctl subtree at lsp until we find the given name.
1335 * Returns true and fills in next oid data in @next and @len if oid is found.
1338 sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1339 int *next, int *len, int level, bool honor_skip)
1341 struct sysctl_oid_list *next_lsp;
1342 struct sysctl_oid *oidp = NULL, key;
1343 bool success = false;
1344 enum sysctl_iter_action action;
1346 SYSCTL_ASSERT_LOCKED();
1348 * Start the search at the requested oid. But if not found, then scan
1349 * through all children.
1352 key.oid_number = *name;
1353 oidp = RB_FIND(sysctl_oid_list, lsp, &key);
1356 oidp = RB_MIN(sysctl_oid_list, lsp);
1357 for(; oidp != NULL; oidp = RB_NEXT(sysctl_oid_list, lsp, oidp)) {
1358 action = sysctl_sysctl_next_node(oidp, name, namelen,
1360 if (action == ITER_SIBLINGS)
1362 if (action == ITER_FOUND) {
1366 KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action));
1368 next_lsp = SYSCTL_CHILDREN(oidp);
1370 success = sysctl_sysctl_next_action(next_lsp, NULL, 0,
1371 next + 1, len, level + 1, honor_skip);
1373 success = sysctl_sysctl_next_action(next_lsp, name + 1,
1374 namelen - 1, next + 1, len, level + 1, honor_skip);
1378 * We maintain the invariant that current node oid
1379 * is >= the oid provided in @name.
1380 * As there are no usable children at this node,
1381 * current node oid is strictly > than the requested
1383 * Hence, reduce namelen to 0 to allow for picking first
1384 * nodes/leafs in the next node in list.
1394 *next = oidp->oid_number;
1403 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1405 int *name = (int *) arg1;
1406 u_int namelen = arg2;
1409 struct sysctl_oid_list *lsp = &sysctl__children;
1410 struct rm_priotracker tracker;
1411 int next[CTL_MAXNAME];
1414 SYSCTL_RLOCK(&tracker);
1415 success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1,
1416 oidp->oid_number == CTL_SYSCTL_NEXT);
1417 SYSCTL_RUNLOCK(&tracker);
1420 error = SYSCTL_OUT(req, next, len * sizeof (int));
1425 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1428 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1429 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1431 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1432 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1435 name2oid(const char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1437 struct sysctl_oid *oidp;
1438 struct sysctl_oid_list *lsp = &sysctl__children;
1441 SYSCTL_ASSERT_LOCKED();
1443 for (*len = 0; *len < CTL_MAXNAME;) {
1444 n = strchrnul(name, '.');
1445 oidp = sysctl_find_oidnamelen(name, n - name, lsp);
1448 *oid++ = oidp->oid_number;
1454 if (*name == '\0') {
1460 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1463 if (oidp->oid_handler)
1466 lsp = SYSCTL_CHILDREN(oidp);
1472 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1475 int error, oid[CTL_MAXNAME], len = 0;
1476 struct sysctl_oid *op = NULL;
1477 struct rm_priotracker tracker;
1482 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1483 return (ENAMETOOLONG);
1486 if (req->newlen >= sizeof(buf))
1487 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1489 error = SYSCTL_IN(req, p, req->newlen);
1496 p [req->newlen] = '\0';
1498 SYSCTL_RLOCK(&tracker);
1499 error = name2oid(p, oid, &len, &op);
1500 SYSCTL_RUNLOCK(&tracker);
1508 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1513 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1516 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1517 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1518 sysctl_sysctl_name2oid, "I", "");
1521 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1523 struct sysctl_oid *oid;
1524 struct rm_priotracker tracker;
1527 error = sysctl_wire_old_buffer(req, 0);
1531 SYSCTL_RLOCK(&tracker);
1532 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1536 if (oid->oid_fmt == NULL) {
1540 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1543 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1545 SYSCTL_RUNLOCK(&tracker);
1549 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1550 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1553 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1555 struct sysctl_oid *oid;
1556 struct rm_priotracker tracker;
1559 error = sysctl_wire_old_buffer(req, 0);
1563 SYSCTL_RLOCK(&tracker);
1564 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1568 if (oid->oid_descr == NULL) {
1572 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1574 SYSCTL_RUNLOCK(&tracker);
1578 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1579 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1582 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1584 struct sysctl_oid *oid;
1585 struct rm_priotracker tracker;
1588 error = sysctl_wire_old_buffer(req, 0);
1592 SYSCTL_RLOCK(&tracker);
1593 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1597 if (oid->oid_label == NULL) {
1601 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1603 SYSCTL_RUNLOCK(&tracker);
1607 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1608 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1611 * Default "handler" functions.
1617 * a variable: point arg1 at it.
1618 * a constant: pass it in arg2.
1622 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1628 * Attempt to get a coherent snapshot by making a copy of the data.
1631 temp = *(bool *)arg1 ? 1 : 0;
1633 temp = arg2 ? 1 : 0;
1635 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1636 if (error || !req->newptr)
1642 error = SYSCTL_IN(req, &temp, sizeof(temp));
1644 *(bool *)arg1 = temp ? 1 : 0;
1650 * Handle an int8_t, signed or unsigned.
1652 * a variable: point arg1 at it.
1653 * a constant: pass it in arg2.
1657 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1663 * Attempt to get a coherent snapshot by making a copy of the data.
1666 tmpout = *(int8_t *)arg1;
1669 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1671 if (error || !req->newptr)
1677 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1682 * Handle an int16_t, signed or unsigned.
1684 * a variable: point arg1 at it.
1685 * a constant: pass it in arg2.
1689 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1695 * Attempt to get a coherent snapshot by making a copy of the data.
1698 tmpout = *(int16_t *)arg1;
1701 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1703 if (error || !req->newptr)
1709 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1714 * Handle an int32_t, signed or unsigned.
1716 * a variable: point arg1 at it.
1717 * a constant: pass it in arg2.
1721 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1727 * Attempt to get a coherent snapshot by making a copy of the data.
1730 tmpout = *(int32_t *)arg1;
1733 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1735 if (error || !req->newptr)
1741 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1746 * Handle an int, signed or unsigned.
1748 * a variable: point arg1 at it.
1749 * a constant: pass it in arg2.
1753 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1755 int tmpout, error = 0;
1758 * Attempt to get a coherent snapshot by making a copy of the data.
1761 tmpout = *(int *)arg1;
1764 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1766 if (error || !req->newptr)
1772 error = SYSCTL_IN(req, arg1, sizeof(int));
1777 * Based on sysctl_handle_int() convert milliseconds into ticks.
1778 * Note: this is used by TCP.
1782 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1787 s = (int)((int64_t)tt * 1000 / hz);
1789 error = sysctl_handle_int(oidp, &s, 0, req);
1790 if (error || !req->newptr)
1793 tt = (int)((int64_t)s * hz / 1000);
1802 * Handle a long, signed or unsigned.
1804 * a variable: point arg1 at it.
1805 * a constant: pass it in arg2.
1809 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1818 * Attempt to get a coherent snapshot by making a copy of the data.
1821 tmplong = *(long *)arg1;
1825 if (req->flags & SCTL_MASK32) {
1827 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1830 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1832 if (error || !req->newptr)
1838 else if (req->flags & SCTL_MASK32) {
1839 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1840 *(long *)arg1 = (long)tmpint;
1844 error = SYSCTL_IN(req, arg1, sizeof(long));
1849 * Handle a 64 bit int, signed or unsigned.
1851 * a variable: point arg1 at it.
1852 * a constant: pass it in arg2.
1855 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1861 * Attempt to get a coherent snapshot by making a copy of the data.
1864 tmpout = *(uint64_t *)arg1;
1867 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1869 if (error || !req->newptr)
1875 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1880 * Handle our generic '\0' terminated 'C' string.
1882 * a variable string: point arg1 at it, arg2 is max length.
1883 * a constant string: point arg1 at it, arg2 is zero.
1887 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1891 int error = 0, ro_string = 0;
1894 * If the sysctl isn't writable and isn't a preallocated tunable that
1895 * can be modified by kenv(2), microoptimise and treat it as a
1897 * A zero-length buffer indicates a fixed size read-only
1898 * string. In ddb, don't worry about trying to make a malloced
1901 if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
1902 arg2 == 0 || kdb_active) {
1903 arg2 = strlen((char *)arg1) + 1;
1907 if (req->oldptr != NULL) {
1910 outlen = strlen(tmparg) + 1;
1912 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1913 sx_slock(&sysctlstringlock);
1914 memcpy(tmparg, arg1, arg2);
1915 sx_sunlock(&sysctlstringlock);
1916 outlen = strlen(tmparg) + 1;
1919 error = SYSCTL_OUT(req, tmparg, outlen);
1922 free(tmparg, M_SYSCTLTMP);
1925 sx_slock(&sysctlstringlock);
1926 outlen = strlen((char *)arg1) + 1;
1928 sx_sunlock(&sysctlstringlock);
1929 error = SYSCTL_OUT(req, NULL, outlen);
1931 if (error || !req->newptr)
1934 if (req->newlen - req->newidx >= arg2 ||
1935 req->newlen - req->newidx < 0) {
1937 } else if (req->newlen - req->newidx == 0) {
1938 sx_xlock(&sysctlstringlock);
1939 ((char *)arg1)[0] = '\0';
1940 sx_xunlock(&sysctlstringlock);
1941 } else if (req->newfunc == sysctl_new_kernel) {
1942 arg2 = req->newlen - req->newidx;
1943 sx_xlock(&sysctlstringlock);
1944 error = SYSCTL_IN(req, arg1, arg2);
1946 ((char *)arg1)[arg2] = '\0';
1947 req->newidx += arg2;
1949 sx_xunlock(&sysctlstringlock);
1951 arg2 = req->newlen - req->newidx;
1952 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1954 error = SYSCTL_IN(req, tmparg, arg2);
1956 free(tmparg, M_SYSCTLTMP);
1960 sx_xlock(&sysctlstringlock);
1961 memcpy(arg1, tmparg, arg2);
1962 ((char *)arg1)[arg2] = '\0';
1963 sx_xunlock(&sysctlstringlock);
1964 free(tmparg, M_SYSCTLTMP);
1965 req->newidx += arg2;
1971 * Handle any kind of opaque data.
1972 * arg1 points to it, arg2 is the size.
1976 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1980 struct sysctl_req req2;
1983 * Attempt to get a coherent snapshot, by using the thread
1984 * pre-emption counter updated from within mi_switch() to
1985 * determine if we were pre-empted during a bcopy() or
1986 * copyout(). Make 3 attempts at doing this before giving up.
1987 * If we encounter an error, stop immediately.
1992 generation = curthread->td_generation;
1993 error = SYSCTL_OUT(req, arg1, arg2);
1997 if (generation != curthread->td_generation && tries < 3) {
2002 error = SYSCTL_IN(req, arg1, arg2);
2008 * Based on sysctl_handle_64() convert microseconds to a sbintime.
2011 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
2016 usec = sbttous(*(sbintime_t *)arg1);
2018 error = sysctl_handle_64(oidp, &usec, 0, req);
2019 if (error || !req->newptr)
2022 *(sbintime_t *)arg1 = ustosbt(usec);
2028 * Based on sysctl_handle_64() convert milliseconds to a sbintime.
2031 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
2036 msec = sbttoms(*(sbintime_t *)arg1);
2038 error = sysctl_handle_64(oidp, &msec, 0, req);
2039 if (error || !req->newptr)
2042 *(sbintime_t *)arg1 = mstosbt(msec);
2048 * Convert seconds to a struct timeval. Intended for use with
2049 * intervals and thus does not permit negative seconds.
2052 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
2060 error = sysctl_handle_int(oidp, &secs, 0, req);
2061 if (error || req->newptr == NULL)
2072 * Transfer functions to/from kernel space.
2073 * XXX: rather untested at this point
2076 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
2082 if (req->oldlen <= req->oldidx)
2085 if (i > req->oldlen - req->oldidx)
2086 i = req->oldlen - req->oldidx;
2088 bcopy(p, (char *)req->oldptr + req->oldidx, i);
2091 if (req->oldptr && i != l)
2097 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
2101 if (req->newlen - req->newidx < l)
2103 bcopy((const char *)req->newptr + req->newidx, p, l);
2109 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2110 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
2113 struct sysctl_req req;
2115 bzero(&req, sizeof req);
2121 req.oldlen = *oldlenp;
2123 req.validlen = req.oldlen;
2130 req.newlen = newlen;
2134 req.oldfunc = sysctl_old_kernel;
2135 req.newfunc = sysctl_new_kernel;
2136 req.lock = REQ_UNWIRED;
2138 error = sysctl_root(0, name, namelen, &req);
2140 if (req.lock == REQ_WIRED && req.validlen > 0)
2141 vsunlock(req.oldptr, req.validlen);
2143 if (error && error != ENOMEM)
2147 if (req.oldptr && req.oldidx > req.validlen)
2148 *retval = req.validlen;
2150 *retval = req.oldidx;
2156 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
2157 void *new, size_t newlen, size_t *retval, int flags)
2159 int oid[CTL_MAXNAME];
2160 size_t oidlen, plen;
2163 oid[0] = CTL_SYSCTL;
2164 oid[1] = CTL_SYSCTL_NAME2OID;
2165 oidlen = sizeof(oid);
2167 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
2168 (void *)name, strlen(name), &plen, flags);
2172 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
2173 new, newlen, retval, flags);
2178 * Transfer function to/from user space.
2181 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
2183 size_t i, len, origidx;
2186 origidx = req->oldidx;
2188 if (req->oldptr == NULL)
2191 * If we have not wired the user supplied buffer and we are currently
2192 * holding locks, drop a witness warning, as it's possible that
2193 * write operations to the user page can sleep.
2195 if (req->lock != REQ_WIRED)
2196 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2197 "sysctl_old_user()");
2199 len = req->validlen;
2203 if (i > len - origidx)
2205 if (req->lock == REQ_WIRED) {
2206 error = copyout_nofault(p, (char *)req->oldptr +
2209 error = copyout(p, (char *)req->oldptr + origidx, i);
2219 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2225 if (req->newlen - req->newidx < l)
2227 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2228 "sysctl_new_user()");
2229 error = copyin((const char *)req->newptr + req->newidx, p, l);
2235 * Wire the user space destination buffer. If set to a value greater than
2236 * zero, the len parameter limits the maximum amount of wired memory.
2239 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2244 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2246 if (req->lock != REQ_WIRED && req->oldptr &&
2247 req->oldfunc == sysctl_old_user) {
2248 if (wiredlen != 0) {
2249 ret = vslock(req->oldptr, wiredlen);
2256 req->lock = REQ_WIRED;
2257 req->validlen = wiredlen;
2263 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2264 int *nindx, struct sysctl_req *req)
2266 struct sysctl_oid_list *lsp;
2267 struct sysctl_oid *oid;
2268 struct sysctl_oid key;
2271 SYSCTL_ASSERT_LOCKED();
2272 lsp = &sysctl__children;
2274 while (indx < CTL_MAXNAME) {
2275 key.oid_number = name[indx];
2276 oid = RB_FIND(sysctl_oid_list, lsp, &key);
2281 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2282 if (oid->oid_handler != NULL || indx == namelen) {
2286 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2287 ("%s found DYING node %p", __func__, oid));
2290 lsp = SYSCTL_CHILDREN(oid);
2291 } else if (indx == namelen) {
2292 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2297 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2298 ("%s found DYING node %p", __func__, oid));
2308 * Traverse our tree, and find the right node, execute whatever it points
2309 * to, and return the resulting error code.
2313 sysctl_root(SYSCTL_HANDLER_ARGS)
2315 struct sysctl_oid *oid;
2316 struct rm_priotracker tracker;
2317 int error, indx, lvl;
2319 SYSCTL_RLOCK(&tracker);
2321 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2325 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2327 * You can't call a sysctl when it's a node, but has
2328 * no handler. Inform the user that it's a node.
2329 * The indx may or may not be the same as namelen.
2331 if (oid->oid_handler == NULL) {
2337 /* Is this sysctl writable? */
2338 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2343 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2345 #ifdef CAPABILITY_MODE
2347 * If the process is in capability mode, then don't permit reading or
2348 * writing unless specifically granted for the node.
2350 if (IN_CAPABILITY_MODE(req->td)) {
2351 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2352 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2359 /* Is this sysctl sensitive to securelevels? */
2360 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2361 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2362 error = securelevel_gt(req->td->td_ucred, lvl);
2367 /* Is this sysctl writable by only privileged users? */
2368 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2371 if (oid->oid_kind & CTLFLAG_PRISON)
2372 priv = PRIV_SYSCTL_WRITEJAIL;
2374 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2375 prison_owns_vnet(req->td->td_ucred))
2376 priv = PRIV_SYSCTL_WRITEJAIL;
2379 priv = PRIV_SYSCTL_WRITE;
2380 error = priv_check(req->td, priv);
2385 if (!oid->oid_handler) {
2390 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2391 arg1 = (int *)arg1 + indx;
2394 arg1 = oid->oid_arg1;
2395 arg2 = oid->oid_arg2;
2398 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2404 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2405 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2407 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2410 SYSCTL_RUNLOCK(&tracker);
2414 #ifndef _SYS_SYSPROTO_H_
2415 struct __sysctl_args {
2425 sys___sysctl(struct thread *td, struct __sysctl_args *uap)
2427 int error, i, name[CTL_MAXNAME];
2430 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2433 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2437 error = userland_sysctl(td, name, uap->namelen,
2438 uap->old, uap->oldlenp, 0,
2439 uap->new, uap->newlen, &j, 0);
2440 if (error && error != ENOMEM)
2443 i = copyout(&j, uap->oldlenp, sizeof(j));
2451 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2452 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2453 int flags, bool inkernel)
2455 int oid[CTL_MAXNAME];
2461 if (namelen > MAXPATHLEN || namelen == 0)
2464 if (namelen > sizeof(namebuf))
2465 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2466 error = copyin(oname, name, namelen);
2470 oid[0] = CTL_SYSCTL;
2471 oid[1] = CTL_SYSCTL_NAME2OID;
2472 oidlen = sizeof(oid);
2473 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2477 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2478 inkernel, new, newlen, retval, flags);
2481 if (namelen > sizeof(namebuf))
2482 free(name, M_SYSCTL);
2486 #ifndef _SYS_SYSPROTO_H_
2487 struct __sysctlbyname_args {
2497 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2502 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2503 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2506 if (uap->oldlenp != NULL)
2507 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2513 * This is used from various compatibility syscalls too. That's why name
2514 * must be in kernel space.
2517 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2518 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2519 size_t *retval, int flags)
2521 int error = 0, memlocked;
2522 struct sysctl_req req;
2524 bzero(&req, sizeof req);
2531 req.oldlen = *oldlenp;
2533 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2538 req.validlen = req.oldlen;
2542 req.newlen = newlen;
2546 req.oldfunc = sysctl_old_user;
2547 req.newfunc = sysctl_new_user;
2548 req.lock = REQ_UNWIRED;
2551 if (KTRPOINT(curthread, KTR_SYSCTL))
2552 ktrsysctl(name, namelen);
2555 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2557 sx_xlock(&sysctlmemlock);
2559 CURVNET_SET(TD_TO_VNET(td));
2564 error = sysctl_root(0, name, namelen, &req);
2565 if (error != EAGAIN)
2567 kern_yield(PRI_USER);
2572 if (req.lock == REQ_WIRED && req.validlen > 0)
2573 vsunlock(req.oldptr, req.validlen);
2575 sx_xunlock(&sysctlmemlock);
2577 if (error && error != ENOMEM)
2581 if (req.oldptr && req.oldidx > req.validlen)
2582 *retval = req.validlen;
2584 *retval = req.oldidx;
2590 * Drain into a sysctl struct. The user buffer should be wired if a page
2591 * fault would cause issue.
2594 sbuf_sysctl_drain(void *arg, const char *data, int len)
2596 struct sysctl_req *req = arg;
2599 error = SYSCTL_OUT(req, data, len);
2600 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2601 return (error == 0 ? len : -error);
2605 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2606 struct sysctl_req *req)
2609 /* Supply a default buffer size if none given. */
2610 if (buf == NULL && length == 0)
2612 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2613 sbuf_set_drain(s, sbuf_sysctl_drain, req);
2619 /* The current OID the debugger is working with */
2620 static struct sysctl_oid *g_ddb_oid;
2622 /* The current flags specified by the user */
2623 static int g_ddb_sysctl_flags;
2625 /* Check to see if the last sysctl printed */
2626 static int g_ddb_sysctl_printed;
2628 static const int ctl_sign[CTLTYPE+1] = {
2637 static const int ctl_size[CTLTYPE+1] = {
2638 [CTLTYPE_INT] = sizeof(int),
2639 [CTLTYPE_UINT] = sizeof(u_int),
2640 [CTLTYPE_LONG] = sizeof(long),
2641 [CTLTYPE_ULONG] = sizeof(u_long),
2642 [CTLTYPE_S8] = sizeof(int8_t),
2643 [CTLTYPE_S16] = sizeof(int16_t),
2644 [CTLTYPE_S32] = sizeof(int32_t),
2645 [CTLTYPE_S64] = sizeof(int64_t),
2646 [CTLTYPE_U8] = sizeof(uint8_t),
2647 [CTLTYPE_U16] = sizeof(uint16_t),
2648 [CTLTYPE_U32] = sizeof(uint32_t),
2649 [CTLTYPE_U64] = sizeof(uint64_t),
2652 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */
2653 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */
2654 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */
2655 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */
2657 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */
2659 static const char db_sysctl_modifs[] = {
2663 static const int db_sysctl_modif_values[] = {
2664 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2665 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2668 /* Handlers considered safe to print while recursing */
2669 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2677 sysctl_handle_string,
2678 sysctl_handle_opaque,
2682 * Use in place of sysctl_old_kernel to print sysctl values.
2684 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2687 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2689 const u_char *val, *p;
2691 size_t intlen, slen;
2694 int sign, ctltype, hexlen, xflag, error;
2696 /* Suppress false-positive GCC uninitialized variable warnings */
2708 /* We are going to print */
2709 g_ddb_sysctl_printed = 1;
2711 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2713 ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2714 sign = ctl_sign[ctltype];
2715 intlen = ctl_size[ctltype];
2719 case CTLTYPE_STRING:
2720 db_printf("%.*s", (int) len, (const char *) p);
2736 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2738 while (len >= intlen) {
2742 umv = *(const u_int *)p;
2743 mv = *(const int *)p;
2747 umv = *(const u_long *)p;
2748 mv = *(const long *)p;
2752 umv = *(const uint8_t *)p;
2753 mv = *(const int8_t *)p;
2757 umv = *(const uint16_t *)p;
2758 mv = *(const int16_t *)p;
2762 umv = *(const uint32_t *)p;
2763 mv = *(const int32_t *)p;
2767 umv = *(const uint64_t *)p;
2768 mv = *(const int64_t *)p;
2772 db_printf("%s", sep1);
2774 db_printf("%#0*jx", hexlen, umv);
2776 db_printf("%ju", umv);
2777 else if (g_ddb_oid->oid_fmt[1] == 'K') {
2778 /* Kelvins are currently unsupported. */
2782 db_printf("%jd", mv);
2791 case CTLTYPE_OPAQUE:
2792 /* TODO: Support struct functions. */
2796 db_printf("Format:%s Length:%zu Dump:0x",
2797 g_ddb_oid->oid_fmt, len);
2798 while (len-- && (xflag || p < val + 16))
2799 db_printf("%02x", *p++);
2800 if (!xflag && len > 16)
2807 req->oldidx += slen;
2812 * Avoid setting new sysctl values from the debugger
2815 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2821 /* Changing sysctls from the debugger is currently unsupported */
2826 * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2827 * Instead of copying any output to a buffer we'll dump it right to
2831 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2832 void *old, size_t *oldlenp, size_t *retval, int flags)
2834 struct sysctl_req req;
2837 /* Setup the request */
2838 bzero(&req, sizeof req);
2839 req.td = kdb_thread;
2840 req.oldfunc = sysctl_old_ddb;
2841 req.newfunc = sysctl_new_ddb;
2842 req.lock = REQ_UNWIRED;
2844 req.oldlen = *oldlenp;
2846 req.validlen = req.oldlen;
2851 /* Setup our globals for sysctl_old_ddb */
2853 g_ddb_sysctl_flags = flags;
2854 g_ddb_sysctl_printed = 0;
2856 error = sysctl_root(0, name, namelen, &req);
2860 g_ddb_sysctl_flags = 0;
2863 if (req.oldptr && req.oldidx > req.validlen)
2864 *retval = req.validlen;
2866 *retval = req.oldidx;
2872 * Show a sysctl's name
2875 db_show_oid_name(int *oid, size_t nlen)
2877 struct sysctl_oid *oidp;
2878 int qoid[CTL_MAXNAME + 2];
2881 qoid[0] = CTL_SYSCTL;
2882 qoid[1] = CTL_SYSCTL_NAME;
2883 memcpy(qoid + 2, oid, nlen * sizeof(int));
2885 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2887 db_error("sysctl name oid");
2889 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2891 db_error("sysctl name");
2895 * Check to see if an OID is safe to print from ddb.
2898 db_oid_safe(const struct sysctl_oid *oidp)
2900 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2901 if (oidp->oid_handler == db_safe_handlers[i])
2909 * Show a sysctl at a specific OID
2910 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2913 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2915 int error, xflag, oflag, Nflag, nflag;
2918 xflag = flags & DB_SYSCTL_HEX;
2919 oflag = flags & DB_SYSCTL_OPAQUE;
2920 nflag = flags & DB_SYSCTL_VALUE_ONLY;
2921 Nflag = flags & DB_SYSCTL_NAME_ONLY;
2923 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2928 db_show_oid_name(oid, nlen);
2934 db_show_oid_name(oid, nlen);
2938 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2939 db_printf("Skipping, unsafe to print while recursing.");
2944 /* Try once, and ask about the size */
2946 error = db_sysctl(oidp, oid, nlen,
2947 NULL, NULL, &len, flags);
2951 if (!g_ddb_sysctl_printed)
2952 /* Lie about the size */
2953 error = db_sysctl(oidp, oid, nlen,
2954 (void *) 1, &len, NULL, flags);
2962 * Show all sysctls under a specific OID
2963 * Compare to sysctl_all from sbin/sysctl/sysctl.c
2966 db_show_sysctl_all(int *oid, size_t len, int flags)
2968 struct sysctl_oid *oidp;
2969 int qoid[CTL_MAXNAME + 2], next[CTL_MAXNAME];
2972 qoid[0] = CTL_SYSCTL;
2973 qoid[1] = CTL_SYSCTL_NEXT;
2976 memcpy(&qoid[2], oid, nlen * sizeof(int));
2983 size_t nextsize = sizeof(next);
2985 error = kernel_sysctl(kdb_thread, qoid, nlen + 2,
2986 next, &nextsize, NULL, 0, &nlen, 0);
2988 if (error == ENOENT)
2991 db_error("sysctl(next)");
2994 nlen /= sizeof(int);
2996 if (nlen < (unsigned int)len)
2999 if (memcmp(&oid[0], &next[0], len * sizeof(int)) != 0)
3002 /* Find the OID in question */
3003 error = sysctl_find_oid(next, nlen, &oidp, NULL, NULL);
3007 (void)db_show_oid(oidp, next, nlen, flags | DB_SYSCTL_SAFE_ONLY);
3012 memcpy(&qoid[2 + len], &next[len], (nlen - len) * sizeof(int));
3017 * Show a sysctl by its user facing string
3020 db_sysctlbyname(const char *name, int flags)
3022 struct sysctl_oid *oidp;
3023 int oid[CTL_MAXNAME];
3026 error = name2oid(name, oid, &nlen, &oidp);
3031 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
3032 db_show_sysctl_all(oid, nlen, flags);
3034 error = db_show_oid(oidp, oid, nlen, flags);
3041 db_sysctl_cmd_usage(void)
3044 " sysctl [/Nnox] <sysctl> \n"
3046 " <sysctl> The name of the sysctl to show. \n"
3048 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n"
3049 " This will work for most sysctls, but should not be used \n"
3050 " with sysctls that are known to malloc. \n"
3052 " While recursing any \"unsafe\" sysctls will be skipped. \n"
3053 " Call sysctl directly on the sysctl to try printing the \n"
3054 " skipped sysctl. This is unsafe and may make the ddb \n"
3055 " session unusable. \n"
3058 " /N Display only the name of the sysctl. \n"
3059 " /n Display only the value of the sysctl. \n"
3060 " /o Display opaque values. \n"
3061 " /x Display the sysctl in hex. \n"
3064 "sysctl vm.v_free_min \n"
3065 "vn.v_free_min: 12669 \n"
3070 * Show a specific sysctl similar to sysctl (8).
3072 DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN)
3074 char name[TOK_STRING_SIZE];
3075 int error, i, t, flags;
3077 /* Parse the modifiers */
3078 t = db_read_token();
3079 if (t == tSLASH || t == tMINUS) {
3080 t = db_read_token();
3082 db_printf("Bad modifier\n");
3086 db_strcpy(modif, db_tok_string);
3094 for (i = 0; i < nitems(db_sysctl_modifs); i++) {
3095 if (strchr(modif, db_sysctl_modifs[i])) {
3096 flags |= db_sysctl_modif_values[i];
3100 /* Parse the sysctl names */
3101 t = db_read_token();
3103 db_printf("Need sysctl name\n");
3108 /* Copy the name into a temporary buffer */
3109 db_strcpy(name, db_tok_string);
3111 /* Ensure there is no trailing cruft */
3112 t = db_read_token();
3114 db_printf("Unexpected sysctl argument\n");
3119 error = db_sysctlbyname(name, flags);
3120 if (error == ENOENT) {
3121 db_printf("unknown oid: '%s'\n", db_tok_string);
3124 db_printf("%s: error: %d\n", db_tok_string, error);
3129 /* Ensure we eat all of our text */
3132 if (error == EINVAL) {
3133 db_sysctl_cmd_usage();