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");
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 = SLIST_HEAD_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);
129 static struct sysctl_oid *
130 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
132 struct sysctl_oid *oidp;
134 SYSCTL_ASSERT_LOCKED();
135 SYSCTL_FOREACH(oidp, list) {
136 if (strcmp(oidp->oid_name, name) == 0) {
144 * Initialization of the MIB tree.
146 * Order by number in each list.
163 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
164 struct sysctl_req *req, struct rm_priotracker *tracker)
168 if (oid->oid_kind & CTLFLAG_DYN)
169 atomic_add_int(&oid->oid_running, 1);
172 SYSCTL_RUNLOCK(tracker);
177 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
178 * untill we're ready to remove all traces of Giant from sysctl(9).
180 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
181 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
183 error = oid->oid_handler(oid, arg1, arg2, req);
184 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
185 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
188 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
191 SYSCTL_RLOCK(tracker);
195 if (oid->oid_kind & CTLFLAG_DYN) {
196 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
197 (oid->oid_kind & CTLFLAG_DYING) != 0)
198 wakeup(&oid->oid_running);
205 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
207 struct sysctl_req req;
208 struct sysctl_oid *curr;
211 ssize_t rem = sizeof(path);
213 uint8_t data[512] __aligned(sizeof(uint64_t));
219 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
220 len = strlen(curr->oid_name);
225 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
228 memcpy(path + rem, curr->oid_name, len);
230 path[rem + len] = '.';
233 memset(&req, 0, sizeof(req));
236 req.oldfunc = sysctl_old_kernel;
237 req.newfunc = sysctl_new_kernel;
238 req.lock = REQ_UNWIRED;
240 switch (oidp->oid_kind & CTLTYPE) {
242 if (getenv_array(path + rem, data, sizeof(data), &size,
243 sizeof(int), GETENV_SIGNED) == 0)
249 if (getenv_array(path + rem, data, sizeof(data), &size,
250 sizeof(int), GETENV_UNSIGNED) == 0)
256 if (getenv_array(path + rem, data, sizeof(data), &size,
257 sizeof(long), GETENV_SIGNED) == 0)
263 if (getenv_array(path + rem, data, sizeof(data), &size,
264 sizeof(long), GETENV_UNSIGNED) == 0)
270 if (getenv_array(path + rem, data, sizeof(data), &size,
271 sizeof(int8_t), GETENV_SIGNED) == 0)
277 if (getenv_array(path + rem, data, sizeof(data), &size,
278 sizeof(int16_t), GETENV_SIGNED) == 0)
284 if (getenv_array(path + rem, data, sizeof(data), &size,
285 sizeof(int32_t), GETENV_SIGNED) == 0)
291 if (getenv_array(path + rem, data, sizeof(data), &size,
292 sizeof(int64_t), GETENV_SIGNED) == 0)
298 if (getenv_array(path + rem, data, sizeof(data), &size,
299 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
305 if (getenv_array(path + rem, data, sizeof(data), &size,
306 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
312 if (getenv_array(path + rem, data, sizeof(data), &size,
313 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
319 if (getenv_array(path + rem, data, sizeof(data), &size,
320 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
326 penv = kern_getenv(path + rem);
329 req.newlen = strlen(penv);
335 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
336 oidp->oid_arg2, &req, NULL);
338 printf("Setting sysctl %s failed: %d\n", path + rem, error);
344 * Locate the path to a given oid. Returns the length of the resulting path,
345 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
346 * elements and be NULL initialized.
349 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
353 SYSCTL_ASSERT_LOCKED();
355 while (indx < CTL_MAXNAME && indx >= 0) {
356 if (nodes[indx] == NULL && indx == 0)
357 nodes[indx] = SLIST_FIRST(&sysctl__children);
358 else if (nodes[indx] == NULL)
359 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
361 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
363 if (nodes[indx] == needle)
366 if (nodes[indx] == NULL) {
371 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
380 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
382 struct sysctl_oid *nodes[CTL_MAXNAME];
387 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
388 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
390 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
392 memset(nodes, 0, sizeof(nodes));
393 rc = sysctl_search_oid(nodes, leaf);
395 for (i = 0; i < rc; i++)
396 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
399 sbuf_printf(&sb, "%s", leaf->oid_name);
401 sbuf_printf(&sb, ")!\n");
403 (void)sbuf_finish(&sb);
408 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
410 struct rm_priotracker tracker;
412 SYSCTL_RLOCK(&tracker);
413 sysctl_warn_reuse(__func__, oidp);
414 SYSCTL_RUNLOCK(&tracker);
417 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
418 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
423 sysctl_register_oid(struct sysctl_oid *oidp)
425 struct sysctl_oid_list *parent = oidp->oid_parent;
426 struct sysctl_oid *p;
427 struct sysctl_oid *q;
432 * First check if another oid with the same name already
433 * exists in the parent's list.
435 SYSCTL_ASSERT_WLOCKED();
436 p = sysctl_find_oidname(oidp->oid_name, parent);
438 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
442 sysctl_warn_reuse(__func__, p);
446 /* get current OID number */
447 oid_number = oidp->oid_number;
450 #error "OID_AUTO is expected to be a negative value"
453 * Any negative OID number qualifies as OID_AUTO. Valid OID
454 * numbers should always be positive.
456 * NOTE: DO NOT change the starting value here, change it in
457 * <sys/sysctl.h>, and make sure it is at least 256 to
458 * accommodate e.g. net.inet.raw as a static sysctl node.
460 if (oid_number < 0) {
464 * By decrementing the next OID number we spend less
465 * time inserting the OIDs into a sorted list.
467 if (--newoid < CTL_AUTO_START)
474 * Insert the OID into the parent's list sorted by OID number.
478 SLIST_FOREACH(p, parent, oid_link) {
479 /* check if the current OID number is in use */
480 if (oid_number == p->oid_number) {
481 /* get the next valid OID number */
482 if (oid_number < CTL_AUTO_START ||
483 oid_number == 0x7fffffff) {
484 /* wraparound - restart */
485 oid_number = CTL_AUTO_START;
486 /* don't loop forever */
488 panic("sysctl: Out of OID numbers\n");
493 } else if (oid_number < p->oid_number)
497 /* check for non-auto OID number collision */
498 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
499 oid_number >= CTL_AUTO_START) {
500 printf("sysctl: OID number(%d) is already in use for '%s'\n",
501 oidp->oid_number, oidp->oid_name);
503 /* update the OID number, if any */
504 oidp->oid_number = oid_number;
506 SLIST_INSERT_AFTER(q, oidp, oid_link);
508 SLIST_INSERT_HEAD(parent, oidp, oid_link);
510 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
512 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
514 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
515 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
516 /* only fetch value once */
517 oidp->oid_kind |= CTLFLAG_NOFETCH;
518 /* try to fetch value from kernel environment */
519 sysctl_load_tunable_by_oid_locked(oidp);
524 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
528 * Mark the leaf as dormant if it's not to be immediately enabled.
529 * We do not disable nodes as they can be shared between modules
530 * and it is always safe to access a node.
532 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
533 ("internal flag is set in oid_kind"));
534 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
535 oidp->oid_kind |= CTLFLAG_DORMANT;
536 sysctl_register_oid(oidp);
540 sysctl_enable_oid(struct sysctl_oid *oidp)
543 SYSCTL_ASSERT_WLOCKED();
544 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
545 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
546 ("sysctl node is marked as dormant"));
549 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
550 ("enabling already enabled sysctl oid"));
551 oidp->oid_kind &= ~CTLFLAG_DORMANT;
555 sysctl_unregister_oid(struct sysctl_oid *oidp)
557 struct sysctl_oid *p;
560 SYSCTL_ASSERT_WLOCKED();
561 if (oidp->oid_number == OID_AUTO) {
565 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
567 SLIST_REMOVE(oidp->oid_parent, oidp,
568 sysctl_oid, oid_link);
576 * This can happen when a module fails to register and is
577 * being unloaded afterwards. It should not be a panic()
581 printf("%s: failed(%d) to unregister sysctl(%s)\n",
582 __func__, error, oidp->oid_name);
586 /* Initialize a new context to keep track of dynamically added sysctls. */
588 sysctl_ctx_init(struct sysctl_ctx_list *c)
596 * No locking here, the caller is responsible for not adding
597 * new nodes to a context until after this function has
604 /* Free the context, and destroy all dynamic oids registered in this context */
606 sysctl_ctx_free(struct sysctl_ctx_list *clist)
608 struct sysctl_ctx_entry *e, *e1;
613 * First perform a "dry run" to check if it's ok to remove oids.
615 * XXX This algorithm is a hack. But I don't know any
616 * XXX better solution for now...
619 TAILQ_FOREACH(e, clist, link) {
620 error = sysctl_remove_oid_locked(e->entry, 0, 0);
625 * Restore deregistered entries, either from the end,
626 * or from the place where error occurred.
627 * e contains the entry that was not unregistered
630 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
632 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
634 sysctl_register_oid(e1->entry);
635 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
641 /* Now really delete the entries */
642 e = TAILQ_FIRST(clist);
644 e1 = TAILQ_NEXT(e, link);
645 error = sysctl_remove_oid_locked(e->entry, 1, 0);
647 panic("sysctl_remove_oid: corrupt tree, entry: %s",
649 free(e, M_SYSCTLOID);
656 /* Add an entry to the context */
657 struct sysctl_ctx_entry *
658 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
660 struct sysctl_ctx_entry *e;
662 SYSCTL_ASSERT_WLOCKED();
663 if (clist == NULL || oidp == NULL)
665 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
667 TAILQ_INSERT_HEAD(clist, e, link);
671 /* Find an entry in the context */
672 struct sysctl_ctx_entry *
673 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
675 struct sysctl_ctx_entry *e;
677 SYSCTL_ASSERT_WLOCKED();
678 if (clist == NULL || oidp == NULL)
680 TAILQ_FOREACH(e, clist, link) {
688 * Delete an entry from the context.
689 * NOTE: this function doesn't free oidp! You have to remove it
690 * with sysctl_remove_oid().
693 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
695 struct sysctl_ctx_entry *e;
697 if (clist == NULL || oidp == NULL)
700 e = sysctl_ctx_entry_find(clist, oidp);
702 TAILQ_REMOVE(clist, e, link);
704 free(e, M_SYSCTLOID);
713 * Remove dynamically created sysctl trees.
714 * oidp - top of the tree to be removed
715 * del - if 0 - just deregister, otherwise free up entries as well
716 * recurse - if != 0 traverse the subtree to be deleted
719 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
724 error = sysctl_remove_oid_locked(oidp, del, recurse);
730 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
731 int del, int recurse)
733 struct sysctl_oid *p, *tmp;
738 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
739 if (strcmp(p->oid_name, name) == 0) {
740 error = sysctl_remove_oid_locked(p, del, recurse);
750 * Duplicate the provided string, escaping any illegal characters. The result
751 * must be freed when no longer in use.
753 * The list of illegal characters is ".".
756 sysctl_escape_name(const char* orig)
758 int i, s = 0, d = 0, nillegals = 0;
761 /* First count the number of illegal characters */
762 for (i = 0; orig[i] != '\0'; i++) {
767 /* Allocate storage for new string */
768 new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
770 /* Copy the name, escaping characters as we go */
771 while (orig[s] != '\0') {
772 if (orig[s] == '.') {
773 /* %25 is the hexadecimal representation of '.' */
779 new[d++] = orig[s++];
783 /* Finally, nul-terminate */
790 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
792 struct sysctl_oid *p, *tmp;
795 SYSCTL_ASSERT_WLOCKED();
798 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
799 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
804 * WARNING: normal method to do this should be through
805 * sysctl_ctx_free(). Use recursing as the last resort
806 * method to purge your sysctl tree of leftovers...
807 * However, if some other code still references these nodes,
810 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
811 if (oidp->oid_refcnt == 1) {
812 SLIST_FOREACH_SAFE(p,
813 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
815 printf("Warning: failed attempt to "
816 "remove oid %s with child %s\n",
817 oidp->oid_name, p->oid_name);
820 error = sysctl_remove_oid_locked(p, del,
827 if (oidp->oid_refcnt > 1 ) {
830 if (oidp->oid_refcnt == 0) {
831 printf("Warning: bad oid_refcnt=%u (%s)!\n",
832 oidp->oid_refcnt, oidp->oid_name);
835 sysctl_unregister_oid(oidp);
838 * Wait for all threads running the handler to drain.
839 * This preserves the previous behavior when the
840 * sysctl lock was held across a handler invocation,
841 * and is necessary for module unload correctness.
843 while (oidp->oid_running > 0) {
844 oidp->oid_kind |= CTLFLAG_DYING;
845 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
848 free(__DECONST(char *, oidp->oid_descr),
851 free(__DECONST(char *, oidp->oid_label),
853 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
854 free(oidp, M_SYSCTLOID);
860 * Create new sysctls at run time.
861 * clist may point to a valid context initialized with sysctl_ctx_init().
864 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
865 int number, const char *name, int kind, void *arg1, intmax_t arg2,
866 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
869 struct sysctl_oid *oidp;
872 /* You have to hook up somewhere.. */
875 escaped = sysctl_escape_name(name);
876 /* Check if the node already exists, otherwise create it */
878 oidp = sysctl_find_oidname(escaped, parent);
880 free(escaped, M_SYSCTLOID);
881 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
883 /* Update the context */
885 sysctl_ctx_entry_add(clist, oidp);
889 sysctl_warn_reuse(__func__, oidp);
894 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
895 oidp->oid_parent = parent;
896 SLIST_INIT(&oidp->oid_children);
897 oidp->oid_number = number;
898 oidp->oid_refcnt = 1;
899 oidp->oid_name = escaped;
900 oidp->oid_handler = handler;
901 oidp->oid_kind = CTLFLAG_DYN | kind;
902 oidp->oid_arg1 = arg1;
903 oidp->oid_arg2 = arg2;
906 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
908 oidp->oid_label = strdup(label, M_SYSCTLOID);
909 /* Update the context, if used */
911 sysctl_ctx_entry_add(clist, oidp);
912 /* Register this oid */
913 sysctl_register_oid(oidp);
919 * Rename an existing oid.
922 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
927 newname = strdup(name, M_SYSCTLOID);
929 oldname = __DECONST(char *, oidp->oid_name);
930 oidp->oid_name = newname;
932 free(oldname, M_SYSCTLOID);
936 * Reparent an existing oid.
939 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
941 struct sysctl_oid *oidp;
944 if (oid->oid_parent == parent) {
948 oidp = sysctl_find_oidname(oid->oid_name, parent);
953 sysctl_unregister_oid(oid);
954 oid->oid_parent = parent;
955 oid->oid_number = OID_AUTO;
956 sysctl_register_oid(oid);
962 * Register the kernel's oids on startup.
964 SET_DECLARE(sysctl_set, struct sysctl_oid);
967 sysctl_register_all(void *arg)
969 struct sysctl_oid **oidp;
971 sx_init(&sysctlmemlock, "sysctl mem");
972 sx_init(&sysctlstringlock, "sysctl string handler");
975 SET_FOREACH(oidp, sysctl_set)
976 sysctl_register_oid(*oidp);
979 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
984 * These functions implement a presently undocumented interface
985 * used by the sysctl program to walk the tree, and get the type
986 * so it can print the value.
987 * This interface is under work and consideration, and should probably
988 * be killed with a big axe by the first person who can find the time.
989 * (be aware though, that the proper interface isn't as obvious as it
990 * may seem, there are various conflicting requirements.
992 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
993 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
995 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring
997 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
999 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
1000 * for the "..." OID.
1001 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
1003 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
1005 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring
1011 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1014 struct sysctl_oid *oidp;
1016 SYSCTL_ASSERT_LOCKED();
1017 SYSCTL_FOREACH(oidp, l) {
1021 printf("%d %s ", oidp->oid_number, oidp->oid_name);
1024 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1025 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1027 if (oidp->oid_handler)
1028 printf(" *Handler");
1030 switch (oidp->oid_kind & CTLTYPE) {
1033 if (!oidp->oid_handler) {
1034 sysctl_sysctl_debug_dump_node(
1035 SYSCTL_CHILDREN(oidp), i + 2);
1038 case CTLTYPE_INT: printf(" Int\n"); break;
1039 case CTLTYPE_UINT: printf(" u_int\n"); break;
1040 case CTLTYPE_LONG: printf(" Long\n"); break;
1041 case CTLTYPE_ULONG: printf(" u_long\n"); break;
1042 case CTLTYPE_STRING: printf(" String\n"); break;
1043 case CTLTYPE_S8: printf(" int8_t\n"); break;
1044 case CTLTYPE_S16: printf(" int16_t\n"); break;
1045 case CTLTYPE_S32: printf(" int32_t\n"); break;
1046 case CTLTYPE_S64: printf(" int64_t\n"); break;
1047 case CTLTYPE_U8: printf(" uint8_t\n"); break;
1048 case CTLTYPE_U16: printf(" uint16_t\n"); break;
1049 case CTLTYPE_U32: printf(" uint32_t\n"); break;
1050 case CTLTYPE_U64: printf(" uint64_t\n"); break;
1051 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1052 default: printf("\n");
1058 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1060 struct rm_priotracker tracker;
1063 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1066 SYSCTL_RLOCK(&tracker);
1067 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1068 SYSCTL_RUNLOCK(&tracker);
1072 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1073 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1077 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1079 int *name = (int *) arg1;
1080 u_int namelen = arg2;
1082 struct sysctl_oid *oid;
1083 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1084 struct rm_priotracker tracker;
1087 error = sysctl_wire_old_buffer(req, 0);
1091 SYSCTL_RLOCK(&tracker);
1094 snprintf(buf,sizeof(buf),"%d",*name);
1096 error = SYSCTL_OUT(req, ".", 1);
1098 error = SYSCTL_OUT(req, buf, strlen(buf));
1106 SLIST_FOREACH(oid, lsp, oid_link) {
1107 if (oid->oid_number != *name)
1111 error = SYSCTL_OUT(req, ".", 1);
1113 error = SYSCTL_OUT(req, oid->oid_name,
1114 strlen(oid->oid_name));
1121 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1124 if (oid->oid_handler)
1127 lsp2 = SYSCTL_CHILDREN(oid);
1132 error = SYSCTL_OUT(req, "", 1);
1134 SYSCTL_RUNLOCK(&tracker);
1139 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1142 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1143 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1145 enum sysctl_iter_action {
1146 ITER_SIBLINGS, /* Not matched, continue iterating siblings */
1147 ITER_CHILDREN, /* Node has children we need to iterate over them */
1148 ITER_FOUND, /* Matching node was found */
1152 * Tries to find the next node for @name and @namelen.
1154 * Returns next action to take.
1156 static enum sysctl_iter_action
1157 sysctl_sysctl_next_node(struct sysctl_oid *oidp, int *name, unsigned int namelen,
1161 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1162 return (ITER_SIBLINGS);
1164 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1165 return (ITER_SIBLINGS);
1169 * We have reached a node with a full name match and are
1170 * looking for the next oid in its children.
1172 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1174 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1175 * has a handler) and move on to the children.
1178 return (ITER_FOUND);
1179 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1180 return (ITER_FOUND);
1181 /* If node does not have an iterator, treat it as leaf */
1182 if (oidp->oid_handler)
1183 return (ITER_FOUND);
1185 /* Report oid as a node to iterate */
1186 return (ITER_CHILDREN);
1190 * No match yet. Continue seeking the given name.
1192 * We are iterating in order by oid_number, so skip oids lower
1193 * than the one we are looking for.
1195 * When the current oid_number is higher than the one we seek,
1196 * that means we have reached the next oid in the sequence and
1199 * If the oid_number matches the name at this level then we
1200 * have to find a node to continue searching at the next level.
1202 if (oidp->oid_number < *name)
1203 return (ITER_SIBLINGS);
1204 if (oidp->oid_number > *name) {
1206 * We have reached the next oid.
1208 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1210 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1211 * has a handler) and move on to the children.
1214 return (ITER_FOUND);
1215 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1216 return (ITER_FOUND);
1217 /* If node does not have an iterator, treat it as leaf */
1218 if (oidp->oid_handler)
1219 return (ITER_FOUND);
1220 return (ITER_CHILDREN);
1223 /* match at a current level */
1224 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1225 return (ITER_SIBLINGS);
1226 if (oidp->oid_handler)
1227 return (ITER_SIBLINGS);
1229 return (ITER_CHILDREN);
1233 * Recursively walk the sysctl subtree at lsp until we find the given name.
1234 * Returns true and fills in next oid data in @next and @len if oid is found.
1237 sysctl_sysctl_next_action(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1238 int *next, int *len, int level, bool honor_skip)
1240 struct sysctl_oid *oidp;
1241 bool success = false;
1242 enum sysctl_iter_action action;
1244 SYSCTL_ASSERT_LOCKED();
1245 SLIST_FOREACH(oidp, lsp, oid_link) {
1246 action = sysctl_sysctl_next_node(oidp, name, namelen, honor_skip);
1247 if (action == ITER_SIBLINGS)
1249 if (action == ITER_FOUND) {
1253 KASSERT((action== ITER_CHILDREN), ("ret(%d)!=ITER_CHILDREN", action));
1255 lsp = SYSCTL_CHILDREN(oidp);
1257 success = sysctl_sysctl_next_action(lsp, NULL, 0,
1258 next + 1, len, level + 1, honor_skip);
1260 success = sysctl_sysctl_next_action(lsp, name + 1, namelen - 1,
1261 next + 1, len, level + 1, honor_skip);
1265 * We maintain the invariant that current node oid
1266 * is >= the oid provided in @name.
1267 * As there are no usable children at this node,
1268 * current node oid is strictly > than the requested
1270 * Hence, reduce namelen to 0 to allow for picking first
1271 * nodes/leafs in the next node in list.
1281 *next = oidp->oid_number;
1290 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1292 int *name = (int *) arg1;
1293 u_int namelen = arg2;
1296 struct sysctl_oid_list *lsp = &sysctl__children;
1297 struct rm_priotracker tracker;
1298 int next[CTL_MAXNAME];
1301 SYSCTL_RLOCK(&tracker);
1302 success = sysctl_sysctl_next_action(lsp, name, namelen, next, &len, 1,
1303 oidp->oid_number == CTL_SYSCTL_NEXT);
1304 SYSCTL_RUNLOCK(&tracker);
1307 error = SYSCTL_OUT(req, next, len * sizeof (int));
1312 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1315 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1316 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1318 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1319 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1322 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1324 struct sysctl_oid *oidp;
1325 struct sysctl_oid_list *lsp = &sysctl__children;
1328 SYSCTL_ASSERT_LOCKED();
1330 for (*len = 0; *len < CTL_MAXNAME;) {
1331 p = strsep(&name, ".");
1333 SYSCTL_FOREACH(oidp, lsp) {
1334 if (strcmp(p, oidp->oid_name) == 0)
1339 *oid++ = oidp->oid_number;
1342 if (name == NULL || *name == '\0') {
1348 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1351 if (oidp->oid_handler)
1354 lsp = SYSCTL_CHILDREN(oidp);
1360 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1363 int error, oid[CTL_MAXNAME], len = 0;
1364 struct sysctl_oid *op = NULL;
1365 struct rm_priotracker tracker;
1370 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1371 return (ENAMETOOLONG);
1374 if (req->newlen >= sizeof(buf))
1375 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1377 error = SYSCTL_IN(req, p, req->newlen);
1384 p [req->newlen] = '\0';
1386 SYSCTL_RLOCK(&tracker);
1387 error = name2oid(p, oid, &len, &op);
1388 SYSCTL_RUNLOCK(&tracker);
1396 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1401 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1404 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1405 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1406 sysctl_sysctl_name2oid, "I", "");
1409 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1411 struct sysctl_oid *oid;
1412 struct rm_priotracker tracker;
1415 error = sysctl_wire_old_buffer(req, 0);
1419 SYSCTL_RLOCK(&tracker);
1420 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1424 if (oid->oid_fmt == NULL) {
1428 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1431 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1433 SYSCTL_RUNLOCK(&tracker);
1437 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1438 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1441 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1443 struct sysctl_oid *oid;
1444 struct rm_priotracker tracker;
1447 error = sysctl_wire_old_buffer(req, 0);
1451 SYSCTL_RLOCK(&tracker);
1452 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1456 if (oid->oid_descr == NULL) {
1460 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1462 SYSCTL_RUNLOCK(&tracker);
1466 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1467 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1470 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1472 struct sysctl_oid *oid;
1473 struct rm_priotracker tracker;
1476 error = sysctl_wire_old_buffer(req, 0);
1480 SYSCTL_RLOCK(&tracker);
1481 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1485 if (oid->oid_label == NULL) {
1489 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1491 SYSCTL_RUNLOCK(&tracker);
1495 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1496 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1499 * Default "handler" functions.
1505 * a variable: point arg1 at it.
1506 * a constant: pass it in arg2.
1510 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1516 * Attempt to get a coherent snapshot by making a copy of the data.
1519 temp = *(bool *)arg1 ? 1 : 0;
1521 temp = arg2 ? 1 : 0;
1523 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1524 if (error || !req->newptr)
1530 error = SYSCTL_IN(req, &temp, sizeof(temp));
1532 *(bool *)arg1 = temp ? 1 : 0;
1538 * Handle an int8_t, signed or unsigned.
1540 * a variable: point arg1 at it.
1541 * a constant: pass it in arg2.
1545 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1551 * Attempt to get a coherent snapshot by making a copy of the data.
1554 tmpout = *(int8_t *)arg1;
1557 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1559 if (error || !req->newptr)
1565 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1570 * Handle an int16_t, signed or unsigned.
1572 * a variable: point arg1 at it.
1573 * a constant: pass it in arg2.
1577 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1583 * Attempt to get a coherent snapshot by making a copy of the data.
1586 tmpout = *(int16_t *)arg1;
1589 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1591 if (error || !req->newptr)
1597 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1602 * Handle an int32_t, signed or unsigned.
1604 * a variable: point arg1 at it.
1605 * a constant: pass it in arg2.
1609 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1615 * Attempt to get a coherent snapshot by making a copy of the data.
1618 tmpout = *(int32_t *)arg1;
1621 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1623 if (error || !req->newptr)
1629 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1634 * Handle an int, signed or unsigned.
1636 * a variable: point arg1 at it.
1637 * a constant: pass it in arg2.
1641 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1643 int tmpout, error = 0;
1646 * Attempt to get a coherent snapshot by making a copy of the data.
1649 tmpout = *(int *)arg1;
1652 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1654 if (error || !req->newptr)
1660 error = SYSCTL_IN(req, arg1, sizeof(int));
1665 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1666 * Note: this is used by TCP.
1670 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1675 s = (int)((int64_t)tt * 1000 / hz);
1677 error = sysctl_handle_int(oidp, &s, 0, req);
1678 if (error || !req->newptr)
1681 tt = (int)((int64_t)s * hz / 1000);
1690 * Handle a long, signed or unsigned.
1692 * a variable: point arg1 at it.
1693 * a constant: pass it in arg2.
1697 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1706 * Attempt to get a coherent snapshot by making a copy of the data.
1709 tmplong = *(long *)arg1;
1713 if (req->flags & SCTL_MASK32) {
1715 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1718 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1720 if (error || !req->newptr)
1726 else if (req->flags & SCTL_MASK32) {
1727 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1728 *(long *)arg1 = (long)tmpint;
1732 error = SYSCTL_IN(req, arg1, sizeof(long));
1737 * Handle a 64 bit int, signed or unsigned.
1739 * a variable: point arg1 at it.
1740 * a constant: pass it in arg2.
1743 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1749 * Attempt to get a coherent snapshot by making a copy of the data.
1752 tmpout = *(uint64_t *)arg1;
1755 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1757 if (error || !req->newptr)
1763 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1768 * Handle our generic '\0' terminated 'C' string.
1770 * a variable string: point arg1 at it, arg2 is max length.
1771 * a constant string: point arg1 at it, arg2 is zero.
1775 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1779 int error = 0, ro_string = 0;
1782 * If the sysctl isn't writable and isn't a preallocated tunable that
1783 * can be modified by kenv(2), microoptimise and treat it as a
1785 * A zero-length buffer indicates a fixed size read-only
1786 * string. In ddb, don't worry about trying to make a malloced
1789 if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
1790 arg2 == 0 || kdb_active) {
1791 arg2 = strlen((char *)arg1) + 1;
1795 if (req->oldptr != NULL) {
1798 outlen = strlen(tmparg) + 1;
1800 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1801 sx_slock(&sysctlstringlock);
1802 memcpy(tmparg, arg1, arg2);
1803 sx_sunlock(&sysctlstringlock);
1804 outlen = strlen(tmparg) + 1;
1807 error = SYSCTL_OUT(req, tmparg, outlen);
1810 free(tmparg, M_SYSCTLTMP);
1813 sx_slock(&sysctlstringlock);
1814 outlen = strlen((char *)arg1) + 1;
1816 sx_sunlock(&sysctlstringlock);
1817 error = SYSCTL_OUT(req, NULL, outlen);
1819 if (error || !req->newptr)
1822 if (req->newlen - req->newidx >= arg2 ||
1823 req->newlen - req->newidx < 0) {
1825 } else if (req->newlen - req->newidx == 0) {
1826 sx_xlock(&sysctlstringlock);
1827 ((char *)arg1)[0] = '\0';
1828 sx_xunlock(&sysctlstringlock);
1829 } else if (req->newfunc == sysctl_new_kernel) {
1830 arg2 = req->newlen - req->newidx;
1831 sx_xlock(&sysctlstringlock);
1832 error = SYSCTL_IN(req, arg1, arg2);
1834 ((char *)arg1)[arg2] = '\0';
1835 req->newidx += arg2;
1837 sx_xunlock(&sysctlstringlock);
1839 arg2 = req->newlen - req->newidx;
1840 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1842 error = SYSCTL_IN(req, tmparg, arg2);
1844 free(tmparg, M_SYSCTLTMP);
1848 sx_xlock(&sysctlstringlock);
1849 memcpy(arg1, tmparg, arg2);
1850 ((char *)arg1)[arg2] = '\0';
1851 sx_xunlock(&sysctlstringlock);
1852 free(tmparg, M_SYSCTLTMP);
1853 req->newidx += arg2;
1859 * Handle any kind of opaque data.
1860 * arg1 points to it, arg2 is the size.
1864 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1868 struct sysctl_req req2;
1871 * Attempt to get a coherent snapshot, by using the thread
1872 * pre-emption counter updated from within mi_switch() to
1873 * determine if we were pre-empted during a bcopy() or
1874 * copyout(). Make 3 attempts at doing this before giving up.
1875 * If we encounter an error, stop immediately.
1880 generation = curthread->td_generation;
1881 error = SYSCTL_OUT(req, arg1, arg2);
1885 if (generation != curthread->td_generation && tries < 3) {
1890 error = SYSCTL_IN(req, arg1, arg2);
1896 * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1899 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1905 tt = *(int64_t *)arg1;
1908 error = sysctl_handle_64(oidp, &sb, 0, req);
1909 if (error || !req->newptr)
1913 *(int64_t *)arg1 = tt;
1919 * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1922 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1928 tt = *(int64_t *)arg1;
1931 error = sysctl_handle_64(oidp, &sb, 0, req);
1932 if (error || !req->newptr)
1936 *(int64_t *)arg1 = tt;
1942 * Convert seconds to a struct timeval. Intended for use with
1943 * intervals and thus does not permit negative seconds.
1946 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1954 error = sysctl_handle_int(oidp, &secs, 0, req);
1955 if (error || req->newptr == NULL)
1966 * Transfer functions to/from kernel space.
1967 * XXX: rather untested at this point
1970 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1976 if (req->oldlen <= req->oldidx)
1979 if (i > req->oldlen - req->oldidx)
1980 i = req->oldlen - req->oldidx;
1982 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1985 if (req->oldptr && i != l)
1991 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1995 if (req->newlen - req->newidx < l)
1997 bcopy((const char *)req->newptr + req->newidx, p, l);
2003 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2004 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
2007 struct sysctl_req req;
2009 bzero(&req, sizeof req);
2015 req.oldlen = *oldlenp;
2017 req.validlen = req.oldlen;
2024 req.newlen = newlen;
2028 req.oldfunc = sysctl_old_kernel;
2029 req.newfunc = sysctl_new_kernel;
2030 req.lock = REQ_UNWIRED;
2032 error = sysctl_root(0, name, namelen, &req);
2034 if (req.lock == REQ_WIRED && req.validlen > 0)
2035 vsunlock(req.oldptr, req.validlen);
2037 if (error && error != ENOMEM)
2041 if (req.oldptr && req.oldidx > req.validlen)
2042 *retval = req.validlen;
2044 *retval = req.oldidx;
2050 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
2051 void *new, size_t newlen, size_t *retval, int flags)
2053 int oid[CTL_MAXNAME];
2054 size_t oidlen, plen;
2057 oid[0] = CTL_SYSCTL;
2058 oid[1] = CTL_SYSCTL_NAME2OID;
2059 oidlen = sizeof(oid);
2061 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
2062 (void *)name, strlen(name), &plen, flags);
2066 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
2067 new, newlen, retval, flags);
2072 * Transfer function to/from user space.
2075 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
2077 size_t i, len, origidx;
2080 origidx = req->oldidx;
2082 if (req->oldptr == NULL)
2085 * If we have not wired the user supplied buffer and we are currently
2086 * holding locks, drop a witness warning, as it's possible that
2087 * write operations to the user page can sleep.
2089 if (req->lock != REQ_WIRED)
2090 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2091 "sysctl_old_user()");
2093 len = req->validlen;
2097 if (i > len - origidx)
2099 if (req->lock == REQ_WIRED) {
2100 error = copyout_nofault(p, (char *)req->oldptr +
2103 error = copyout(p, (char *)req->oldptr + origidx, i);
2113 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2119 if (req->newlen - req->newidx < l)
2121 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2122 "sysctl_new_user()");
2123 error = copyin((const char *)req->newptr + req->newidx, p, l);
2129 * Wire the user space destination buffer. If set to a value greater than
2130 * zero, the len parameter limits the maximum amount of wired memory.
2133 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2138 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2140 if (req->lock != REQ_WIRED && req->oldptr &&
2141 req->oldfunc == sysctl_old_user) {
2142 if (wiredlen != 0) {
2143 ret = vslock(req->oldptr, wiredlen);
2150 req->lock = REQ_WIRED;
2151 req->validlen = wiredlen;
2157 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2158 int *nindx, struct sysctl_req *req)
2160 struct sysctl_oid_list *lsp;
2161 struct sysctl_oid *oid;
2164 SYSCTL_ASSERT_LOCKED();
2165 lsp = &sysctl__children;
2167 while (indx < CTL_MAXNAME) {
2168 SLIST_FOREACH(oid, lsp, oid_link) {
2169 if (oid->oid_number == name[indx])
2176 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2177 if (oid->oid_handler != NULL || indx == namelen) {
2181 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2182 ("%s found DYING node %p", __func__, oid));
2185 lsp = SYSCTL_CHILDREN(oid);
2186 } else if (indx == namelen) {
2187 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2192 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2193 ("%s found DYING node %p", __func__, oid));
2203 * Traverse our tree, and find the right node, execute whatever it points
2204 * to, and return the resulting error code.
2208 sysctl_root(SYSCTL_HANDLER_ARGS)
2210 struct sysctl_oid *oid;
2211 struct rm_priotracker tracker;
2212 int error, indx, lvl;
2214 SYSCTL_RLOCK(&tracker);
2216 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2220 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2222 * You can't call a sysctl when it's a node, but has
2223 * no handler. Inform the user that it's a node.
2224 * The indx may or may not be the same as namelen.
2226 if (oid->oid_handler == NULL) {
2232 /* Is this sysctl writable? */
2233 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2238 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2240 #ifdef CAPABILITY_MODE
2242 * If the process is in capability mode, then don't permit reading or
2243 * writing unless specifically granted for the node.
2245 if (IN_CAPABILITY_MODE(req->td)) {
2246 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2247 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2254 /* Is this sysctl sensitive to securelevels? */
2255 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2256 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2257 error = securelevel_gt(req->td->td_ucred, lvl);
2262 /* Is this sysctl writable by only privileged users? */
2263 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2266 if (oid->oid_kind & CTLFLAG_PRISON)
2267 priv = PRIV_SYSCTL_WRITEJAIL;
2269 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2270 prison_owns_vnet(req->td->td_ucred))
2271 priv = PRIV_SYSCTL_WRITEJAIL;
2274 priv = PRIV_SYSCTL_WRITE;
2275 error = priv_check(req->td, priv);
2280 if (!oid->oid_handler) {
2285 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2286 arg1 = (int *)arg1 + indx;
2289 arg1 = oid->oid_arg1;
2290 arg2 = oid->oid_arg2;
2293 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2299 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2300 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2302 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2305 SYSCTL_RUNLOCK(&tracker);
2309 #ifndef _SYS_SYSPROTO_H_
2310 struct sysctl_args {
2320 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2322 int error, i, name[CTL_MAXNAME];
2325 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2328 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2332 error = userland_sysctl(td, name, uap->namelen,
2333 uap->old, uap->oldlenp, 0,
2334 uap->new, uap->newlen, &j, 0);
2335 if (error && error != ENOMEM)
2338 i = copyout(&j, uap->oldlenp, sizeof(j));
2346 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2347 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2348 int flags, bool inkernel)
2350 int oid[CTL_MAXNAME];
2356 if (namelen > MAXPATHLEN || namelen == 0)
2359 if (namelen > sizeof(namebuf))
2360 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2361 error = copyin(oname, name, namelen);
2365 oid[0] = CTL_SYSCTL;
2366 oid[1] = CTL_SYSCTL_NAME2OID;
2367 oidlen = sizeof(oid);
2368 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2372 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2373 inkernel, new, newlen, retval, flags);
2376 if (namelen > sizeof(namebuf))
2377 free(name, M_SYSCTL);
2381 #ifndef _SYS_SYSPROTO_H_
2382 struct __sysctlbyname_args {
2392 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2397 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2398 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2401 if (uap->oldlenp != NULL)
2402 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2408 * This is used from various compatibility syscalls too. That's why name
2409 * must be in kernel space.
2412 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2413 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2414 size_t *retval, int flags)
2416 int error = 0, memlocked;
2417 struct sysctl_req req;
2419 bzero(&req, sizeof req);
2426 req.oldlen = *oldlenp;
2428 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2433 req.validlen = req.oldlen;
2437 req.newlen = newlen;
2441 req.oldfunc = sysctl_old_user;
2442 req.newfunc = sysctl_new_user;
2443 req.lock = REQ_UNWIRED;
2446 if (KTRPOINT(curthread, KTR_SYSCTL))
2447 ktrsysctl(name, namelen);
2450 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2452 sx_xlock(&sysctlmemlock);
2454 CURVNET_SET(TD_TO_VNET(td));
2459 error = sysctl_root(0, name, namelen, &req);
2460 if (error != EAGAIN)
2462 kern_yield(PRI_USER);
2467 if (req.lock == REQ_WIRED && req.validlen > 0)
2468 vsunlock(req.oldptr, req.validlen);
2470 sx_xunlock(&sysctlmemlock);
2472 if (error && error != ENOMEM)
2476 if (req.oldptr && req.oldidx > req.validlen)
2477 *retval = req.validlen;
2479 *retval = req.oldidx;
2485 * Drain into a sysctl struct. The user buffer should be wired if a page
2486 * fault would cause issue.
2489 sbuf_sysctl_drain(void *arg, const char *data, int len)
2491 struct sysctl_req *req = arg;
2494 error = SYSCTL_OUT(req, data, len);
2495 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2496 return (error == 0 ? len : -error);
2500 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2501 struct sysctl_req *req)
2504 /* Supply a default buffer size if none given. */
2505 if (buf == NULL && length == 0)
2507 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2508 sbuf_set_drain(s, sbuf_sysctl_drain, req);
2514 /* The current OID the debugger is working with */
2515 static struct sysctl_oid *g_ddb_oid;
2517 /* The current flags specified by the user */
2518 static int g_ddb_sysctl_flags;
2520 /* Check to see if the last sysctl printed */
2521 static int g_ddb_sysctl_printed;
2523 static const int ctl_sign[CTLTYPE+1] = {
2532 static const int ctl_size[CTLTYPE+1] = {
2533 [CTLTYPE_INT] = sizeof(int),
2534 [CTLTYPE_UINT] = sizeof(u_int),
2535 [CTLTYPE_LONG] = sizeof(long),
2536 [CTLTYPE_ULONG] = sizeof(u_long),
2537 [CTLTYPE_S8] = sizeof(int8_t),
2538 [CTLTYPE_S16] = sizeof(int16_t),
2539 [CTLTYPE_S32] = sizeof(int32_t),
2540 [CTLTYPE_S64] = sizeof(int64_t),
2541 [CTLTYPE_U8] = sizeof(uint8_t),
2542 [CTLTYPE_U16] = sizeof(uint16_t),
2543 [CTLTYPE_U32] = sizeof(uint32_t),
2544 [CTLTYPE_U64] = sizeof(uint64_t),
2547 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */
2548 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */
2549 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */
2550 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */
2552 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */
2554 static const char db_sysctl_modifs[] = {
2558 static const int db_sysctl_modif_values[] = {
2559 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2560 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2563 /* Handlers considered safe to print while recursing */
2564 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2572 sysctl_handle_string,
2573 sysctl_handle_opaque,
2577 * Use in place of sysctl_old_kernel to print sysctl values.
2579 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2582 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2584 const u_char *val, *p;
2586 size_t intlen, slen;
2589 int sign, ctltype, hexlen, xflag, error;
2591 /* Suppress false-positive GCC uninitialized variable warnings */
2603 /* We are going to print */
2604 g_ddb_sysctl_printed = 1;
2606 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2608 ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2609 sign = ctl_sign[ctltype];
2610 intlen = ctl_size[ctltype];
2614 case CTLTYPE_STRING:
2615 db_printf("%.*s", (int) len, (const char *) p);
2631 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2633 while (len >= intlen) {
2637 umv = *(const u_int *)p;
2638 mv = *(const int *)p;
2642 umv = *(const u_long *)p;
2643 mv = *(const long *)p;
2647 umv = *(const uint8_t *)p;
2648 mv = *(const int8_t *)p;
2652 umv = *(const uint16_t *)p;
2653 mv = *(const int16_t *)p;
2657 umv = *(const uint32_t *)p;
2658 mv = *(const int32_t *)p;
2662 umv = *(const uint64_t *)p;
2663 mv = *(const int64_t *)p;
2667 db_printf("%s", sep1);
2669 db_printf("%#0*jx", hexlen, umv);
2671 db_printf("%ju", umv);
2672 else if (g_ddb_oid->oid_fmt[1] == 'K') {
2673 /* Kelvins are currently unsupported. */
2677 db_printf("%jd", mv);
2686 case CTLTYPE_OPAQUE:
2687 /* TODO: Support struct functions. */
2691 db_printf("Format:%s Length:%zu Dump:0x",
2692 g_ddb_oid->oid_fmt, len);
2693 while (len-- && (xflag || p < val + 16))
2694 db_printf("%02x", *p++);
2695 if (!xflag && len > 16)
2702 req->oldidx += slen;
2707 * Avoid setting new sysctl values from the debugger
2710 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2716 /* Changing sysctls from the debugger is currently unsupported */
2721 * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2722 * Instead of copying any output to a buffer we'll dump it right to
2726 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2727 void *old, size_t *oldlenp, size_t *retval, int flags)
2729 struct sysctl_req req;
2732 /* Setup the request */
2733 bzero(&req, sizeof req);
2734 req.td = kdb_thread;
2735 req.oldfunc = sysctl_old_ddb;
2736 req.newfunc = sysctl_new_ddb;
2737 req.lock = REQ_UNWIRED;
2739 req.oldlen = *oldlenp;
2741 req.validlen = req.oldlen;
2746 /* Setup our globals for sysctl_old_ddb */
2748 g_ddb_sysctl_flags = flags;
2749 g_ddb_sysctl_printed = 0;
2751 error = sysctl_root(0, name, namelen, &req);
2755 g_ddb_sysctl_flags = 0;
2758 if (req.oldptr && req.oldidx > req.validlen)
2759 *retval = req.validlen;
2761 *retval = req.oldidx;
2767 * Show a sysctl's name
2770 db_show_oid_name(int *oid, size_t nlen)
2772 struct sysctl_oid *oidp;
2773 int qoid[CTL_MAXNAME+2];
2777 memcpy(qoid + 2, oid, nlen * sizeof(int));
2780 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2782 db_error("sysctl name oid");
2784 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2786 db_error("sysctl name");
2790 * Check to see if an OID is safe to print from ddb.
2793 db_oid_safe(const struct sysctl_oid *oidp)
2795 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2796 if (oidp->oid_handler == db_safe_handlers[i])
2804 * Show a sysctl at a specific OID
2805 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2808 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2810 int error, xflag, oflag, Nflag, nflag;
2813 xflag = flags & DB_SYSCTL_HEX;
2814 oflag = flags & DB_SYSCTL_OPAQUE;
2815 nflag = flags & DB_SYSCTL_VALUE_ONLY;
2816 Nflag = flags & DB_SYSCTL_NAME_ONLY;
2818 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2823 db_show_oid_name(oid, nlen);
2829 db_show_oid_name(oid, nlen);
2833 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2834 db_printf("Skipping, unsafe to print while recursing.");
2839 /* Try once, and ask about the size */
2841 error = db_sysctl(oidp, oid, nlen,
2842 NULL, NULL, &len, flags);
2846 if (!g_ddb_sysctl_printed)
2847 /* Lie about the size */
2848 error = db_sysctl(oidp, oid, nlen,
2849 (void *) 1, &len, NULL, flags);
2857 * Show all sysctls under a specific OID
2858 * Compare to sysctl_all from sbin/sysctl/sysctl.c
2861 db_show_sysctl_all(int *oid, size_t len, int flags)
2863 struct sysctl_oid *oidp;
2864 int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
2867 name1[0] = CTL_SYSCTL;
2868 name1[1] = CTL_SYSCTL_NEXT;
2871 memcpy(name1 + 2, oid, len * sizeof(int));
2874 name1[2] = CTL_KERN;
2881 error = kernel_sysctl(kdb_thread, name1, l1,
2882 name2, &l2, NULL, 0, &l2, 0);
2884 if (error == ENOENT)
2887 db_error("sysctl(next)");
2892 if (l2 < (unsigned int)len)
2895 for (i = 0; i < len; i++)
2896 if (name2[i] != oid[i])
2899 /* Find the OID in question */
2900 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
2904 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
2909 memcpy(name1+2, name2, l2 * sizeof(int));
2915 * Show a sysctl by its user facing string
2918 db_sysctlbyname(char *name, int flags)
2920 struct sysctl_oid *oidp;
2921 int oid[CTL_MAXNAME];
2924 error = name2oid(name, oid, &nlen, &oidp);
2929 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2930 db_show_sysctl_all(oid, nlen, flags);
2932 error = db_show_oid(oidp, oid, nlen, flags);
2939 db_sysctl_cmd_usage(void)
2942 " sysctl [/Nnox] <sysctl> \n"
2944 " <sysctl> The name of the sysctl to show. \n"
2946 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n"
2947 " This will work for most sysctls, but should not be used \n"
2948 " with sysctls that are known to malloc. \n"
2950 " While recursing any \"unsafe\" sysctls will be skipped. \n"
2951 " Call sysctl directly on the sysctl to try printing the \n"
2952 " skipped sysctl. This is unsafe and may make the ddb \n"
2953 " session unusable. \n"
2956 " /N Display only the name of the sysctl. \n"
2957 " /n Display only the value of the sysctl. \n"
2958 " /o Display opaque values. \n"
2959 " /x Display the sysctl in hex. \n"
2962 "sysctl vm.v_free_min \n"
2963 "vn.v_free_min: 12669 \n"
2968 * Show a specific sysctl similar to sysctl (8).
2970 DB_COMMAND_FLAGS(sysctl, db_sysctl_cmd, CS_OWN)
2972 char name[TOK_STRING_SIZE];
2973 int error, i, t, flags;
2975 /* Parse the modifiers */
2976 t = db_read_token();
2977 if (t == tSLASH || t == tMINUS) {
2978 t = db_read_token();
2980 db_printf("Bad modifier\n");
2984 db_strcpy(modif, db_tok_string);
2992 for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2993 if (strchr(modif, db_sysctl_modifs[i])) {
2994 flags |= db_sysctl_modif_values[i];
2998 /* Parse the sysctl names */
2999 t = db_read_token();
3001 db_printf("Need sysctl name\n");
3006 /* Copy the name into a temporary buffer */
3007 db_strcpy(name, db_tok_string);
3009 /* Ensure there is no trailing cruft */
3010 t = db_read_token();
3012 db_printf("Unexpected sysctl argument\n");
3017 error = db_sysctlbyname(name, flags);
3018 if (error == ENOENT) {
3019 db_printf("unknown oid: '%s'\n", db_tok_string);
3022 db_printf("%s: error: %d\n", db_tok_string, error);
3027 /* Ensure we eat all of our text */
3030 if (error == EINVAL) {
3031 db_sysctl_cmd_usage();