2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Mike Karels at Berkeley Software Design, Inc.
10 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
11 * project, to make these variables more userfriendly.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "opt_capsicum.h"
45 #include "opt_ktrace.h"
46 #include "opt_sysctl.h"
48 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/capsicum.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/sysctl.h>
55 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/rmlock.h>
65 #include <sys/sysproto.h>
68 #include <sys/ktrace.h>
73 #include <ddb/db_lex.h>
78 #include <security/mac/mac_framework.h>
81 #include <vm/vm_extern.h>
83 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
84 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
85 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
88 * The sysctllock protects the MIB tree. It also protects sysctl
89 * contexts used with dynamic sysctls. The sysctl_register_oid() and
90 * sysctl_unregister_oid() routines require the sysctllock to already
91 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
92 * provided for the few places in the kernel which need to use that
93 * API rather than using the dynamic API. Use of the dynamic API is
94 * strongly encouraged for most code.
96 * The sysctlmemlock is used to limit the amount of user memory wired for
97 * sysctl requests. This is implemented by serializing any userland
98 * sysctl requests larger than a single page via an exclusive lock.
100 * The sysctlstringlock is used to protect concurrent access to writable
101 * string nodes in sysctl_handle_string().
103 static struct rmlock sysctllock;
104 static struct sx __exclusive_cache_line sysctlmemlock;
105 static struct sx sysctlstringlock;
107 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
108 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
109 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
110 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
111 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
112 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
113 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
114 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
115 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
117 #define SYSCTL_SLEEP(ch, wmesg, timo) \
118 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
120 static int sysctl_root(SYSCTL_HANDLER_ARGS);
123 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
125 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
127 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
128 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
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 SLIST_FOREACH(oidp, list, oid_link) {
137 if (strcmp(oidp->oid_name, name) == 0) {
145 * Initialization of the MIB tree.
147 * Order by number in each list.
164 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
165 struct sysctl_req *req, struct rm_priotracker *tracker)
169 if (oid->oid_kind & CTLFLAG_DYN)
170 atomic_add_int(&oid->oid_running, 1);
173 SYSCTL_RUNLOCK(tracker);
178 * Treat set CTLFLAG_NEEDGIANT and unset CTLFLAG_MPSAFE flags the same,
179 * untill we're ready to remove all traces of Giant from sysctl(9).
181 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
182 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
184 error = oid->oid_handler(oid, arg1, arg2, req);
185 if ((oid->oid_kind & CTLFLAG_NEEDGIANT) ||
186 (!(oid->oid_kind & CTLFLAG_MPSAFE)))
189 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
192 SYSCTL_RLOCK(tracker);
196 if (oid->oid_kind & CTLFLAG_DYN) {
197 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
198 (oid->oid_kind & CTLFLAG_DYING) != 0)
199 wakeup(&oid->oid_running);
206 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
208 struct sysctl_req req;
209 struct sysctl_oid *curr;
212 ssize_t rem = sizeof(path);
214 uint8_t data[512] __aligned(sizeof(uint64_t));
220 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
221 len = strlen(curr->oid_name);
226 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
229 memcpy(path + rem, curr->oid_name, len);
231 path[rem + len] = '.';
234 memset(&req, 0, sizeof(req));
237 req.oldfunc = sysctl_old_kernel;
238 req.newfunc = sysctl_new_kernel;
239 req.lock = REQ_UNWIRED;
241 switch (oidp->oid_kind & CTLTYPE) {
243 if (getenv_array(path + rem, data, sizeof(data), &size,
244 sizeof(int), GETENV_SIGNED) == 0)
250 if (getenv_array(path + rem, data, sizeof(data), &size,
251 sizeof(int), GETENV_UNSIGNED) == 0)
257 if (getenv_array(path + rem, data, sizeof(data), &size,
258 sizeof(long), GETENV_SIGNED) == 0)
264 if (getenv_array(path + rem, data, sizeof(data), &size,
265 sizeof(long), GETENV_UNSIGNED) == 0)
271 if (getenv_array(path + rem, data, sizeof(data), &size,
272 sizeof(int8_t), GETENV_SIGNED) == 0)
278 if (getenv_array(path + rem, data, sizeof(data), &size,
279 sizeof(int16_t), GETENV_SIGNED) == 0)
285 if (getenv_array(path + rem, data, sizeof(data), &size,
286 sizeof(int32_t), GETENV_SIGNED) == 0)
292 if (getenv_array(path + rem, data, sizeof(data), &size,
293 sizeof(int64_t), GETENV_SIGNED) == 0)
299 if (getenv_array(path + rem, data, sizeof(data), &size,
300 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
306 if (getenv_array(path + rem, data, sizeof(data), &size,
307 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
313 if (getenv_array(path + rem, data, sizeof(data), &size,
314 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
320 if (getenv_array(path + rem, data, sizeof(data), &size,
321 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
327 penv = kern_getenv(path + rem);
330 req.newlen = strlen(penv);
336 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
337 oidp->oid_arg2, &req, NULL);
339 printf("Setting sysctl %s failed: %d\n", path + rem, error);
345 * Locate the path to a given oid. Returns the length of the resulting path,
346 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
347 * elements and be NULL initialized.
350 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
354 SYSCTL_ASSERT_LOCKED();
356 while (indx < CTL_MAXNAME && indx >= 0) {
357 if (nodes[indx] == NULL && indx == 0)
358 nodes[indx] = SLIST_FIRST(&sysctl__children);
359 else if (nodes[indx] == NULL)
360 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
362 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
364 if (nodes[indx] == needle)
367 if (nodes[indx] == NULL) {
372 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
381 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
383 struct sysctl_oid *nodes[CTL_MAXNAME];
388 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
389 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
391 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
393 memset(nodes, 0, sizeof(nodes));
394 rc = sysctl_search_oid(nodes, leaf);
396 for (i = 0; i < rc; i++)
397 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
400 sbuf_printf(&sb, "%s", leaf->oid_name);
402 sbuf_printf(&sb, ")!\n");
404 (void)sbuf_finish(&sb);
409 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
411 struct rm_priotracker tracker;
413 SYSCTL_RLOCK(&tracker);
414 sysctl_warn_reuse(__func__, oidp);
415 SYSCTL_RUNLOCK(&tracker);
418 SYSCTL_PROC(_sysctl, OID_AUTO, reuse_test,
419 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0, sysctl_reuse_test, "-",
424 sysctl_register_oid(struct sysctl_oid *oidp)
426 struct sysctl_oid_list *parent = oidp->oid_parent;
427 struct sysctl_oid *p;
428 struct sysctl_oid *q;
433 * First check if another oid with the same name already
434 * exists in the parent's list.
436 SYSCTL_ASSERT_WLOCKED();
437 p = sysctl_find_oidname(oidp->oid_name, parent);
439 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
443 sysctl_warn_reuse(__func__, p);
447 /* get current OID number */
448 oid_number = oidp->oid_number;
451 #error "OID_AUTO is expected to be a negative value"
454 * Any negative OID number qualifies as OID_AUTO. Valid OID
455 * numbers should always be positive.
457 * NOTE: DO NOT change the starting value here, change it in
458 * <sys/sysctl.h>, and make sure it is at least 256 to
459 * accommodate e.g. net.inet.raw as a static sysctl node.
461 if (oid_number < 0) {
465 * By decrementing the next OID number we spend less
466 * time inserting the OIDs into a sorted list.
468 if (--newoid < CTL_AUTO_START)
475 * Insert the OID into the parent's list sorted by OID number.
479 SLIST_FOREACH(p, parent, oid_link) {
480 /* check if the current OID number is in use */
481 if (oid_number == p->oid_number) {
482 /* get the next valid OID number */
483 if (oid_number < CTL_AUTO_START ||
484 oid_number == 0x7fffffff) {
485 /* wraparound - restart */
486 oid_number = CTL_AUTO_START;
487 /* don't loop forever */
489 panic("sysctl: Out of OID numbers\n");
494 } else if (oid_number < p->oid_number)
498 /* check for non-auto OID number collision */
499 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
500 oid_number >= CTL_AUTO_START) {
501 printf("sysctl: OID number(%d) is already in use for '%s'\n",
502 oidp->oid_number, oidp->oid_name);
504 /* update the OID number, if any */
505 oidp->oid_number = oid_number;
507 SLIST_INSERT_AFTER(q, oidp, oid_link);
509 SLIST_INSERT_HEAD(parent, oidp, oid_link);
511 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
513 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
515 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
516 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
517 /* only fetch value once */
518 oidp->oid_kind |= CTLFLAG_NOFETCH;
519 /* try to fetch value from kernel environment */
520 sysctl_load_tunable_by_oid_locked(oidp);
525 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
529 * Mark the leaf as dormant if it's not to be immediately enabled.
530 * We do not disable nodes as they can be shared between modules
531 * and it is always safe to access a node.
533 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
534 ("internal flag is set in oid_kind"));
535 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
536 oidp->oid_kind |= CTLFLAG_DORMANT;
537 sysctl_register_oid(oidp);
541 sysctl_enable_oid(struct sysctl_oid *oidp)
544 SYSCTL_ASSERT_WLOCKED();
545 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
546 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
547 ("sysctl node is marked as dormant"));
550 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
551 ("enabling already enabled sysctl oid"));
552 oidp->oid_kind &= ~CTLFLAG_DORMANT;
556 sysctl_unregister_oid(struct sysctl_oid *oidp)
558 struct sysctl_oid *p;
561 SYSCTL_ASSERT_WLOCKED();
562 if (oidp->oid_number == OID_AUTO) {
566 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
568 SLIST_REMOVE(oidp->oid_parent, oidp,
569 sysctl_oid, oid_link);
577 * This can happen when a module fails to register and is
578 * being unloaded afterwards. It should not be a panic()
582 printf("%s: failed(%d) to unregister sysctl(%s)\n",
583 __func__, error, oidp->oid_name);
587 /* Initialize a new context to keep track of dynamically added sysctls. */
589 sysctl_ctx_init(struct sysctl_ctx_list *c)
597 * No locking here, the caller is responsible for not adding
598 * new nodes to a context until after this function has
605 /* Free the context, and destroy all dynamic oids registered in this context */
607 sysctl_ctx_free(struct sysctl_ctx_list *clist)
609 struct sysctl_ctx_entry *e, *e1;
614 * First perform a "dry run" to check if it's ok to remove oids.
616 * XXX This algorithm is a hack. But I don't know any
617 * XXX better solution for now...
620 TAILQ_FOREACH(e, clist, link) {
621 error = sysctl_remove_oid_locked(e->entry, 0, 0);
626 * Restore deregistered entries, either from the end,
627 * or from the place where error occurred.
628 * e contains the entry that was not unregistered
631 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
633 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
635 sysctl_register_oid(e1->entry);
636 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
642 /* Now really delete the entries */
643 e = TAILQ_FIRST(clist);
645 e1 = TAILQ_NEXT(e, link);
646 error = sysctl_remove_oid_locked(e->entry, 1, 0);
648 panic("sysctl_remove_oid: corrupt tree, entry: %s",
650 free(e, M_SYSCTLOID);
657 /* Add an entry to the context */
658 struct sysctl_ctx_entry *
659 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
661 struct sysctl_ctx_entry *e;
663 SYSCTL_ASSERT_WLOCKED();
664 if (clist == NULL || oidp == NULL)
666 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
668 TAILQ_INSERT_HEAD(clist, e, link);
672 /* Find an entry in the context */
673 struct sysctl_ctx_entry *
674 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
676 struct sysctl_ctx_entry *e;
678 SYSCTL_ASSERT_WLOCKED();
679 if (clist == NULL || oidp == NULL)
681 TAILQ_FOREACH(e, clist, link) {
689 * Delete an entry from the context.
690 * NOTE: this function doesn't free oidp! You have to remove it
691 * with sysctl_remove_oid().
694 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
696 struct sysctl_ctx_entry *e;
698 if (clist == NULL || oidp == NULL)
701 e = sysctl_ctx_entry_find(clist, oidp);
703 TAILQ_REMOVE(clist, e, link);
705 free(e, M_SYSCTLOID);
714 * Remove dynamically created sysctl trees.
715 * oidp - top of the tree to be removed
716 * del - if 0 - just deregister, otherwise free up entries as well
717 * recurse - if != 0 traverse the subtree to be deleted
720 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
725 error = sysctl_remove_oid_locked(oidp, del, recurse);
731 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
732 int del, int recurse)
734 struct sysctl_oid *p, *tmp;
739 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
740 if (strcmp(p->oid_name, name) == 0) {
741 error = sysctl_remove_oid_locked(p, del, recurse);
751 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
753 struct sysctl_oid *p, *tmp;
756 SYSCTL_ASSERT_WLOCKED();
759 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
760 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
765 * WARNING: normal method to do this should be through
766 * sysctl_ctx_free(). Use recursing as the last resort
767 * method to purge your sysctl tree of leftovers...
768 * However, if some other code still references these nodes,
771 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
772 if (oidp->oid_refcnt == 1) {
773 SLIST_FOREACH_SAFE(p,
774 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
776 printf("Warning: failed attempt to "
777 "remove oid %s with child %s\n",
778 oidp->oid_name, p->oid_name);
781 error = sysctl_remove_oid_locked(p, del,
788 if (oidp->oid_refcnt > 1 ) {
791 if (oidp->oid_refcnt == 0) {
792 printf("Warning: bad oid_refcnt=%u (%s)!\n",
793 oidp->oid_refcnt, oidp->oid_name);
796 sysctl_unregister_oid(oidp);
799 * Wait for all threads running the handler to drain.
800 * This preserves the previous behavior when the
801 * sysctl lock was held across a handler invocation,
802 * and is necessary for module unload correctness.
804 while (oidp->oid_running > 0) {
805 oidp->oid_kind |= CTLFLAG_DYING;
806 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
809 free(__DECONST(char *, oidp->oid_descr),
812 free(__DECONST(char *, oidp->oid_label),
814 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
815 free(oidp, M_SYSCTLOID);
821 * Create new sysctls at run time.
822 * clist may point to a valid context initialized with sysctl_ctx_init().
825 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
826 int number, const char *name, int kind, void *arg1, intmax_t arg2,
827 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
830 struct sysctl_oid *oidp;
832 /* You have to hook up somewhere.. */
835 /* Check if the node already exists, otherwise create it */
837 oidp = sysctl_find_oidname(name, parent);
839 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
841 /* Update the context */
843 sysctl_ctx_entry_add(clist, oidp);
847 sysctl_warn_reuse(__func__, oidp);
852 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
853 oidp->oid_parent = parent;
854 SLIST_INIT(&oidp->oid_children);
855 oidp->oid_number = number;
856 oidp->oid_refcnt = 1;
857 oidp->oid_name = strdup(name, M_SYSCTLOID);
858 oidp->oid_handler = handler;
859 oidp->oid_kind = CTLFLAG_DYN | kind;
860 oidp->oid_arg1 = arg1;
861 oidp->oid_arg2 = arg2;
864 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
866 oidp->oid_label = strdup(label, M_SYSCTLOID);
867 /* Update the context, if used */
869 sysctl_ctx_entry_add(clist, oidp);
870 /* Register this oid */
871 sysctl_register_oid(oidp);
877 * Rename an existing oid.
880 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
885 newname = strdup(name, M_SYSCTLOID);
887 oldname = __DECONST(char *, oidp->oid_name);
888 oidp->oid_name = newname;
890 free(oldname, M_SYSCTLOID);
894 * Reparent an existing oid.
897 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
899 struct sysctl_oid *oidp;
902 if (oid->oid_parent == parent) {
906 oidp = sysctl_find_oidname(oid->oid_name, parent);
911 sysctl_unregister_oid(oid);
912 oid->oid_parent = parent;
913 oid->oid_number = OID_AUTO;
914 sysctl_register_oid(oid);
920 * Register the kernel's oids on startup.
922 SET_DECLARE(sysctl_set, struct sysctl_oid);
925 sysctl_register_all(void *arg)
927 struct sysctl_oid **oidp;
929 sx_init(&sysctlmemlock, "sysctl mem");
930 sx_init(&sysctlstringlock, "sysctl string handler");
933 SET_FOREACH(oidp, sysctl_set)
934 sysctl_register_oid(*oidp);
937 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
942 * These functions implement a presently undocumented interface
943 * used by the sysctl program to walk the tree, and get the type
944 * so it can print the value.
945 * This interface is under work and consideration, and should probably
946 * be killed with a big axe by the first person who can find the time.
947 * (be aware though, that the proper interface isn't as obvious as it
948 * may seem, there are various conflicting requirements.
950 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
951 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
953 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring
955 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
957 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
959 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
961 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
963 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring
969 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
972 struct sysctl_oid *oidp;
974 SYSCTL_ASSERT_LOCKED();
975 SLIST_FOREACH(oidp, l, oid_link) {
979 printf("%d %s ", oidp->oid_number, oidp->oid_name);
982 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
983 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
985 if (oidp->oid_handler)
988 switch (oidp->oid_kind & CTLTYPE) {
991 if (!oidp->oid_handler) {
992 sysctl_sysctl_debug_dump_node(
993 SYSCTL_CHILDREN(oidp), i + 2);
996 case CTLTYPE_INT: printf(" Int\n"); break;
997 case CTLTYPE_UINT: printf(" u_int\n"); break;
998 case CTLTYPE_LONG: printf(" Long\n"); break;
999 case CTLTYPE_ULONG: printf(" u_long\n"); break;
1000 case CTLTYPE_STRING: printf(" String\n"); break;
1001 case CTLTYPE_S8: printf(" int8_t\n"); break;
1002 case CTLTYPE_S16: printf(" int16_t\n"); break;
1003 case CTLTYPE_S32: printf(" int32_t\n"); break;
1004 case CTLTYPE_S64: printf(" int64_t\n"); break;
1005 case CTLTYPE_U8: printf(" uint8_t\n"); break;
1006 case CTLTYPE_U16: printf(" uint16_t\n"); break;
1007 case CTLTYPE_U32: printf(" uint32_t\n"); break;
1008 case CTLTYPE_U64: printf(" uint64_t\n"); break;
1009 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1010 default: printf("\n");
1016 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1018 struct rm_priotracker tracker;
1021 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1024 SYSCTL_RLOCK(&tracker);
1025 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1026 SYSCTL_RUNLOCK(&tracker);
1030 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1031 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1035 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1037 int *name = (int *) arg1;
1038 u_int namelen = arg2;
1040 struct sysctl_oid *oid;
1041 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1042 struct rm_priotracker tracker;
1045 error = sysctl_wire_old_buffer(req, 0);
1049 SYSCTL_RLOCK(&tracker);
1052 snprintf(buf,sizeof(buf),"%d",*name);
1054 error = SYSCTL_OUT(req, ".", 1);
1056 error = SYSCTL_OUT(req, buf, strlen(buf));
1064 SLIST_FOREACH(oid, lsp, oid_link) {
1065 if (oid->oid_number != *name)
1069 error = SYSCTL_OUT(req, ".", 1);
1071 error = SYSCTL_OUT(req, oid->oid_name,
1072 strlen(oid->oid_name));
1079 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1082 if (oid->oid_handler)
1085 lsp2 = SYSCTL_CHILDREN(oid);
1090 error = SYSCTL_OUT(req, "", 1);
1092 SYSCTL_RUNLOCK(&tracker);
1097 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1100 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1101 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1104 * Walk the sysctl subtree at lsp until we find the given name,
1105 * and return the next name in order by oid_number.
1108 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1109 int *next, int *len, int level, bool honor_skip)
1111 struct sysctl_oid *oidp;
1113 SYSCTL_ASSERT_LOCKED();
1115 SLIST_FOREACH(oidp, lsp, oid_link) {
1116 *next = oidp->oid_number;
1118 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1121 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1126 * We have reached a node with a full name match and are
1127 * looking for the next oid in its children.
1129 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1131 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1132 * has a handler) and move on to the children.
1136 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1138 if (oidp->oid_handler)
1140 lsp = SYSCTL_CHILDREN(oidp);
1141 if (!sysctl_sysctl_next_ls(lsp, NULL, 0, next + 1, len,
1142 level + 1, honor_skip))
1145 * There were no useable children in this node.
1146 * Continue searching for the next oid at this level.
1152 * No match yet. Continue seeking the given name.
1154 * We are iterating in order by oid_number, so skip oids lower
1155 * than the one we are looking for.
1157 * When the current oid_number is higher than the one we seek,
1158 * that means we have reached the next oid in the sequence and
1161 * If the oid_number matches the name at this level then we
1162 * have to find a node to continue searching at the next level.
1164 if (oidp->oid_number < *name)
1166 if (oidp->oid_number > *name) {
1168 * We have reached the next oid.
1170 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1172 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1173 * has a handler) and move on to the children.
1177 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1179 if (oidp->oid_handler)
1181 lsp = SYSCTL_CHILDREN(oidp);
1182 if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
1183 next + 1, len, level + 1, honor_skip))
1187 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1189 if (oidp->oid_handler)
1191 lsp = SYSCTL_CHILDREN(oidp);
1192 if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
1193 next + 1, len, level + 1, honor_skip))
1197 * There were no useable children in this node.
1198 * Continue searching for the next oid at the root level.
1202 /* Reset len in case a failed recursive call changed it. */
1209 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1211 int *name = (int *) arg1;
1212 u_int namelen = arg2;
1214 struct sysctl_oid_list *lsp = &sysctl__children;
1215 struct rm_priotracker tracker;
1216 int next[CTL_MAXNAME];
1218 SYSCTL_RLOCK(&tracker);
1219 error = sysctl_sysctl_next_ls(lsp, name, namelen, next, &len, 1,
1220 oidp->oid_number == CTL_SYSCTL_NEXT);
1221 SYSCTL_RUNLOCK(&tracker);
1224 error = SYSCTL_OUT(req, next, len * sizeof (int));
1229 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1232 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1233 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1235 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1236 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1239 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1241 struct sysctl_oid *oidp;
1242 struct sysctl_oid_list *lsp = &sysctl__children;
1245 SYSCTL_ASSERT_LOCKED();
1247 for (*len = 0; *len < CTL_MAXNAME;) {
1248 p = strsep(&name, ".");
1250 oidp = SLIST_FIRST(lsp);
1251 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1254 if (strcmp(p, oidp->oid_name) == 0)
1257 *oid++ = oidp->oid_number;
1260 if (name == NULL || *name == '\0') {
1266 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1269 if (oidp->oid_handler)
1272 lsp = SYSCTL_CHILDREN(oidp);
1278 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1281 int error, oid[CTL_MAXNAME], len = 0;
1282 struct sysctl_oid *op = NULL;
1283 struct rm_priotracker tracker;
1288 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1289 return (ENAMETOOLONG);
1292 if (req->newlen >= sizeof(buf))
1293 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1295 error = SYSCTL_IN(req, p, req->newlen);
1302 p [req->newlen] = '\0';
1304 SYSCTL_RLOCK(&tracker);
1305 error = name2oid(p, oid, &len, &op);
1306 SYSCTL_RUNLOCK(&tracker);
1314 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1319 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1322 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1323 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1324 sysctl_sysctl_name2oid, "I", "");
1327 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1329 struct sysctl_oid *oid;
1330 struct rm_priotracker tracker;
1333 error = sysctl_wire_old_buffer(req, 0);
1337 SYSCTL_RLOCK(&tracker);
1338 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1342 if (oid->oid_fmt == NULL) {
1346 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1349 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1351 SYSCTL_RUNLOCK(&tracker);
1355 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1356 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1359 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1361 struct sysctl_oid *oid;
1362 struct rm_priotracker tracker;
1365 error = sysctl_wire_old_buffer(req, 0);
1369 SYSCTL_RLOCK(&tracker);
1370 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1374 if (oid->oid_descr == NULL) {
1378 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1380 SYSCTL_RUNLOCK(&tracker);
1384 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1385 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1388 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1390 struct sysctl_oid *oid;
1391 struct rm_priotracker tracker;
1394 error = sysctl_wire_old_buffer(req, 0);
1398 SYSCTL_RLOCK(&tracker);
1399 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1403 if (oid->oid_label == NULL) {
1407 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1409 SYSCTL_RUNLOCK(&tracker);
1413 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1414 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1417 * Default "handler" functions.
1423 * a variable: point arg1 at it.
1424 * a constant: pass it in arg2.
1428 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1434 * Attempt to get a coherent snapshot by making a copy of the data.
1437 temp = *(bool *)arg1 ? 1 : 0;
1439 temp = arg2 ? 1 : 0;
1441 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1442 if (error || !req->newptr)
1448 error = SYSCTL_IN(req, &temp, sizeof(temp));
1450 *(bool *)arg1 = temp ? 1 : 0;
1456 * Handle an int8_t, signed or unsigned.
1458 * a variable: point arg1 at it.
1459 * a constant: pass it in arg2.
1463 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1469 * Attempt to get a coherent snapshot by making a copy of the data.
1472 tmpout = *(int8_t *)arg1;
1475 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1477 if (error || !req->newptr)
1483 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1488 * Handle an int16_t, signed or unsigned.
1490 * a variable: point arg1 at it.
1491 * a constant: pass it in arg2.
1495 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1501 * Attempt to get a coherent snapshot by making a copy of the data.
1504 tmpout = *(int16_t *)arg1;
1507 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1509 if (error || !req->newptr)
1515 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1520 * Handle an int32_t, signed or unsigned.
1522 * a variable: point arg1 at it.
1523 * a constant: pass it in arg2.
1527 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1533 * Attempt to get a coherent snapshot by making a copy of the data.
1536 tmpout = *(int32_t *)arg1;
1539 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1541 if (error || !req->newptr)
1547 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1552 * Handle an int, signed or unsigned.
1554 * a variable: point arg1 at it.
1555 * a constant: pass it in arg2.
1559 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1561 int tmpout, error = 0;
1564 * Attempt to get a coherent snapshot by making a copy of the data.
1567 tmpout = *(int *)arg1;
1570 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1572 if (error || !req->newptr)
1578 error = SYSCTL_IN(req, arg1, sizeof(int));
1583 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1584 * Note: this is used by TCP.
1588 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1593 s = (int)((int64_t)tt * 1000 / hz);
1595 error = sysctl_handle_int(oidp, &s, 0, req);
1596 if (error || !req->newptr)
1599 tt = (int)((int64_t)s * hz / 1000);
1608 * Handle a long, signed or unsigned.
1610 * a variable: point arg1 at it.
1611 * a constant: pass it in arg2.
1615 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1624 * Attempt to get a coherent snapshot by making a copy of the data.
1627 tmplong = *(long *)arg1;
1631 if (req->flags & SCTL_MASK32) {
1633 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1636 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1638 if (error || !req->newptr)
1644 else if (req->flags & SCTL_MASK32) {
1645 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1646 *(long *)arg1 = (long)tmpint;
1650 error = SYSCTL_IN(req, arg1, sizeof(long));
1655 * Handle a 64 bit int, signed or unsigned.
1657 * a variable: point arg1 at it.
1658 * a constant: pass it in arg2.
1661 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1667 * Attempt to get a coherent snapshot by making a copy of the data.
1670 tmpout = *(uint64_t *)arg1;
1673 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1675 if (error || !req->newptr)
1681 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1686 * Handle our generic '\0' terminated 'C' string.
1688 * a variable string: point arg1 at it, arg2 is max length.
1689 * a constant string: point arg1 at it, arg2 is zero.
1693 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1697 int error = 0, ro_string = 0;
1700 * If the sysctl isn't writable and isn't a preallocated tunable that
1701 * can be modified by kenv(2), microoptimise and treat it as a
1703 * A zero-length buffer indicates a fixed size read-only
1704 * string. In ddb, don't worry about trying to make a malloced
1707 if ((oidp->oid_kind & (CTLFLAG_WR | CTLFLAG_TUN)) == 0 ||
1708 arg2 == 0 || kdb_active) {
1709 arg2 = strlen((char *)arg1) + 1;
1713 if (req->oldptr != NULL) {
1716 outlen = strlen(tmparg) + 1;
1718 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1719 sx_slock(&sysctlstringlock);
1720 memcpy(tmparg, arg1, arg2);
1721 sx_sunlock(&sysctlstringlock);
1722 outlen = strlen(tmparg) + 1;
1725 error = SYSCTL_OUT(req, tmparg, outlen);
1728 free(tmparg, M_SYSCTLTMP);
1731 sx_slock(&sysctlstringlock);
1732 outlen = strlen((char *)arg1) + 1;
1734 sx_sunlock(&sysctlstringlock);
1735 error = SYSCTL_OUT(req, NULL, outlen);
1737 if (error || !req->newptr)
1740 if (req->newlen - req->newidx >= arg2 ||
1741 req->newlen - req->newidx < 0) {
1743 } else if (req->newlen - req->newidx == 0) {
1744 sx_xlock(&sysctlstringlock);
1745 ((char *)arg1)[0] = '\0';
1746 sx_xunlock(&sysctlstringlock);
1748 arg2 = req->newlen - req->newidx;
1749 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1751 error = SYSCTL_IN(req, tmparg, arg2);
1753 free(tmparg, M_SYSCTLTMP);
1757 sx_xlock(&sysctlstringlock);
1758 memcpy(arg1, tmparg, arg2);
1759 ((char *)arg1)[arg2] = '\0';
1760 sx_xunlock(&sysctlstringlock);
1761 free(tmparg, M_SYSCTLTMP);
1762 req->newidx += arg2;
1768 * Handle any kind of opaque data.
1769 * arg1 points to it, arg2 is the size.
1773 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1777 struct sysctl_req req2;
1780 * Attempt to get a coherent snapshot, by using the thread
1781 * pre-emption counter updated from within mi_switch() to
1782 * determine if we were pre-empted during a bcopy() or
1783 * copyout(). Make 3 attempts at doing this before giving up.
1784 * If we encounter an error, stop immediately.
1789 generation = curthread->td_generation;
1790 error = SYSCTL_OUT(req, arg1, arg2);
1794 if (generation != curthread->td_generation && tries < 3) {
1799 error = SYSCTL_IN(req, arg1, arg2);
1805 * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1808 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1814 tt = *(int64_t *)arg1;
1817 error = sysctl_handle_64(oidp, &sb, 0, req);
1818 if (error || !req->newptr)
1822 *(int64_t *)arg1 = tt;
1828 * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1831 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1837 tt = *(int64_t *)arg1;
1840 error = sysctl_handle_64(oidp, &sb, 0, req);
1841 if (error || !req->newptr)
1845 *(int64_t *)arg1 = tt;
1851 * Convert seconds to a struct timeval. Intended for use with
1852 * intervals and thus does not permit negative seconds.
1855 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1863 error = sysctl_handle_int(oidp, &secs, 0, req);
1864 if (error || req->newptr == NULL)
1875 * Transfer functions to/from kernel space.
1876 * XXX: rather untested at this point
1879 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1885 if (req->oldlen <= req->oldidx)
1888 if (i > req->oldlen - req->oldidx)
1889 i = req->oldlen - req->oldidx;
1891 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1894 if (req->oldptr && i != l)
1900 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1904 if (req->newlen - req->newidx < l)
1906 bcopy((const char *)req->newptr + req->newidx, p, l);
1912 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1913 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1916 struct sysctl_req req;
1918 bzero(&req, sizeof req);
1924 req.oldlen = *oldlenp;
1926 req.validlen = req.oldlen;
1933 req.newlen = newlen;
1937 req.oldfunc = sysctl_old_kernel;
1938 req.newfunc = sysctl_new_kernel;
1939 req.lock = REQ_UNWIRED;
1941 error = sysctl_root(0, name, namelen, &req);
1943 if (req.lock == REQ_WIRED && req.validlen > 0)
1944 vsunlock(req.oldptr, req.validlen);
1946 if (error && error != ENOMEM)
1950 if (req.oldptr && req.oldidx > req.validlen)
1951 *retval = req.validlen;
1953 *retval = req.oldidx;
1959 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1960 void *new, size_t newlen, size_t *retval, int flags)
1962 int oid[CTL_MAXNAME];
1963 size_t oidlen, plen;
1966 oid[0] = CTL_SYSCTL;
1967 oid[1] = CTL_SYSCTL_NAME2OID;
1968 oidlen = sizeof(oid);
1970 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1971 (void *)name, strlen(name), &plen, flags);
1975 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1976 new, newlen, retval, flags);
1981 * Transfer function to/from user space.
1984 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1986 size_t i, len, origidx;
1989 origidx = req->oldidx;
1991 if (req->oldptr == NULL)
1994 * If we have not wired the user supplied buffer and we are currently
1995 * holding locks, drop a witness warning, as it's possible that
1996 * write operations to the user page can sleep.
1998 if (req->lock != REQ_WIRED)
1999 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2000 "sysctl_old_user()");
2002 len = req->validlen;
2006 if (i > len - origidx)
2008 if (req->lock == REQ_WIRED) {
2009 error = copyout_nofault(p, (char *)req->oldptr +
2012 error = copyout(p, (char *)req->oldptr + origidx, i);
2022 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
2028 if (req->newlen - req->newidx < l)
2030 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2031 "sysctl_new_user()");
2032 error = copyin((const char *)req->newptr + req->newidx, p, l);
2038 * Wire the user space destination buffer. If set to a value greater than
2039 * zero, the len parameter limits the maximum amount of wired memory.
2042 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2047 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2049 if (req->lock != REQ_WIRED && req->oldptr &&
2050 req->oldfunc == sysctl_old_user) {
2051 if (wiredlen != 0) {
2052 ret = vslock(req->oldptr, wiredlen);
2059 req->lock = REQ_WIRED;
2060 req->validlen = wiredlen;
2066 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2067 int *nindx, struct sysctl_req *req)
2069 struct sysctl_oid_list *lsp;
2070 struct sysctl_oid *oid;
2073 SYSCTL_ASSERT_LOCKED();
2074 lsp = &sysctl__children;
2076 while (indx < CTL_MAXNAME) {
2077 SLIST_FOREACH(oid, lsp, oid_link) {
2078 if (oid->oid_number == name[indx])
2085 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2086 if (oid->oid_handler != NULL || indx == namelen) {
2090 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2091 ("%s found DYING node %p", __func__, oid));
2094 lsp = SYSCTL_CHILDREN(oid);
2095 } else if (indx == namelen) {
2096 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2101 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2102 ("%s found DYING node %p", __func__, oid));
2112 * Traverse our tree, and find the right node, execute whatever it points
2113 * to, and return the resulting error code.
2117 sysctl_root(SYSCTL_HANDLER_ARGS)
2119 struct sysctl_oid *oid;
2120 struct rm_priotracker tracker;
2121 int error, indx, lvl;
2123 SYSCTL_RLOCK(&tracker);
2125 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2129 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2131 * You can't call a sysctl when it's a node, but has
2132 * no handler. Inform the user that it's a node.
2133 * The indx may or may not be the same as namelen.
2135 if (oid->oid_handler == NULL) {
2141 /* Is this sysctl writable? */
2142 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2147 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2149 #ifdef CAPABILITY_MODE
2151 * If the process is in capability mode, then don't permit reading or
2152 * writing unless specifically granted for the node.
2154 if (IN_CAPABILITY_MODE(req->td)) {
2155 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2156 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2163 /* Is this sysctl sensitive to securelevels? */
2164 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2165 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2166 error = securelevel_gt(req->td->td_ucred, lvl);
2171 /* Is this sysctl writable by only privileged users? */
2172 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2175 if (oid->oid_kind & CTLFLAG_PRISON)
2176 priv = PRIV_SYSCTL_WRITEJAIL;
2178 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2179 prison_owns_vnet(req->td->td_ucred))
2180 priv = PRIV_SYSCTL_WRITEJAIL;
2183 priv = PRIV_SYSCTL_WRITE;
2184 error = priv_check(req->td, priv);
2189 if (!oid->oid_handler) {
2194 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2195 arg1 = (int *)arg1 + indx;
2198 arg1 = oid->oid_arg1;
2199 arg2 = oid->oid_arg2;
2202 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2208 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2209 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2211 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2214 SYSCTL_RUNLOCK(&tracker);
2218 #ifndef _SYS_SYSPROTO_H_
2219 struct sysctl_args {
2229 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2231 int error, i, name[CTL_MAXNAME];
2234 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2237 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2241 error = userland_sysctl(td, name, uap->namelen,
2242 uap->old, uap->oldlenp, 0,
2243 uap->new, uap->newlen, &j, 0);
2244 if (error && error != ENOMEM)
2247 i = copyout(&j, uap->oldlenp, sizeof(j));
2255 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2256 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2257 int flags, bool inkernel)
2259 int oid[CTL_MAXNAME];
2265 if (namelen > MAXPATHLEN || namelen == 0)
2268 if (namelen > sizeof(namebuf))
2269 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2270 error = copyin(oname, name, namelen);
2274 oid[0] = CTL_SYSCTL;
2275 oid[1] = CTL_SYSCTL_NAME2OID;
2276 oidlen = sizeof(oid);
2277 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2281 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2282 inkernel, new, newlen, retval, flags);
2285 if (namelen > sizeof(namebuf))
2286 free(name, M_SYSCTL);
2290 #ifndef _SYS_SYSPROTO_H_
2291 struct __sysctlbyname_args {
2301 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2306 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2307 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2310 if (uap->oldlenp != NULL)
2311 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2317 * This is used from various compatibility syscalls too. That's why name
2318 * must be in kernel space.
2321 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2322 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2323 size_t *retval, int flags)
2325 int error = 0, memlocked;
2326 struct sysctl_req req;
2328 bzero(&req, sizeof req);
2335 req.oldlen = *oldlenp;
2337 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2342 req.validlen = req.oldlen;
2346 req.newlen = newlen;
2350 req.oldfunc = sysctl_old_user;
2351 req.newfunc = sysctl_new_user;
2352 req.lock = REQ_UNWIRED;
2355 if (KTRPOINT(curthread, KTR_SYSCTL))
2356 ktrsysctl(name, namelen);
2359 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2361 sx_xlock(&sysctlmemlock);
2363 CURVNET_SET(TD_TO_VNET(td));
2368 error = sysctl_root(0, name, namelen, &req);
2369 if (error != EAGAIN)
2371 kern_yield(PRI_USER);
2376 if (req.lock == REQ_WIRED && req.validlen > 0)
2377 vsunlock(req.oldptr, req.validlen);
2379 sx_xunlock(&sysctlmemlock);
2381 if (error && error != ENOMEM)
2385 if (req.oldptr && req.oldidx > req.validlen)
2386 *retval = req.validlen;
2388 *retval = req.oldidx;
2394 * Drain into a sysctl struct. The user buffer should be wired if a page
2395 * fault would cause issue.
2398 sbuf_sysctl_drain(void *arg, const char *data, int len)
2400 struct sysctl_req *req = arg;
2403 error = SYSCTL_OUT(req, data, len);
2404 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2405 return (error == 0 ? len : -error);
2409 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2410 struct sysctl_req *req)
2413 /* Supply a default buffer size if none given. */
2414 if (buf == NULL && length == 0)
2416 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2417 sbuf_set_drain(s, sbuf_sysctl_drain, req);
2423 /* The current OID the debugger is working with */
2424 static struct sysctl_oid *g_ddb_oid;
2426 /* The current flags specified by the user */
2427 static int g_ddb_sysctl_flags;
2429 /* Check to see if the last sysctl printed */
2430 static int g_ddb_sysctl_printed;
2432 static const int ctl_sign[CTLTYPE+1] = {
2441 static const int ctl_size[CTLTYPE+1] = {
2442 [CTLTYPE_INT] = sizeof(int),
2443 [CTLTYPE_UINT] = sizeof(u_int),
2444 [CTLTYPE_LONG] = sizeof(long),
2445 [CTLTYPE_ULONG] = sizeof(u_long),
2446 [CTLTYPE_S8] = sizeof(int8_t),
2447 [CTLTYPE_S16] = sizeof(int16_t),
2448 [CTLTYPE_S32] = sizeof(int32_t),
2449 [CTLTYPE_S64] = sizeof(int64_t),
2450 [CTLTYPE_U8] = sizeof(uint8_t),
2451 [CTLTYPE_U16] = sizeof(uint16_t),
2452 [CTLTYPE_U32] = sizeof(uint32_t),
2453 [CTLTYPE_U64] = sizeof(uint64_t),
2456 #define DB_SYSCTL_NAME_ONLY 0x001 /* Compare with -N */
2457 #define DB_SYSCTL_VALUE_ONLY 0x002 /* Compare with -n */
2458 #define DB_SYSCTL_OPAQUE 0x004 /* Compare with -o */
2459 #define DB_SYSCTL_HEX 0x008 /* Compare with -x */
2461 #define DB_SYSCTL_SAFE_ONLY 0x100 /* Only simple types */
2463 static const char db_sysctl_modifs[] = {
2467 static const int db_sysctl_modif_values[] = {
2468 DB_SYSCTL_NAME_ONLY, DB_SYSCTL_VALUE_ONLY,
2469 DB_SYSCTL_OPAQUE, DB_SYSCTL_HEX,
2472 /* Handlers considered safe to print while recursing */
2473 static int (* const db_safe_handlers[])(SYSCTL_HANDLER_ARGS) = {
2481 sysctl_handle_string,
2482 sysctl_handle_opaque,
2486 * Use in place of sysctl_old_kernel to print sysctl values.
2488 * Compare to the output handling in show_var from sbin/sysctl/sysctl.c
2491 sysctl_old_ddb(struct sysctl_req *req, const void *ptr, size_t len)
2493 const u_char *val, *p;
2495 size_t intlen, slen;
2498 int sign, ctltype, hexlen, xflag, error;
2500 /* Suppress false-positive GCC uninitialized variable warnings */
2512 /* We are going to print */
2513 g_ddb_sysctl_printed = 1;
2515 xflag = g_ddb_sysctl_flags & DB_SYSCTL_HEX;
2517 ctltype = (g_ddb_oid->oid_kind & CTLTYPE);
2518 sign = ctl_sign[ctltype];
2519 intlen = ctl_size[ctltype];
2523 case CTLTYPE_STRING:
2524 db_printf("%.*s", (int) len, (const char *) p);
2540 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
2542 while (len >= intlen) {
2546 umv = *(const u_int *)p;
2547 mv = *(const int *)p;
2551 umv = *(const u_long *)p;
2552 mv = *(const long *)p;
2556 umv = *(const uint8_t *)p;
2557 mv = *(const int8_t *)p;
2561 umv = *(const uint16_t *)p;
2562 mv = *(const int16_t *)p;
2566 umv = *(const uint32_t *)p;
2567 mv = *(const int32_t *)p;
2571 umv = *(const uint64_t *)p;
2572 mv = *(const int64_t *)p;
2576 db_printf("%s", sep1);
2578 db_printf("%#0*jx", hexlen, umv);
2580 db_printf("%ju", umv);
2581 else if (g_ddb_oid->oid_fmt[1] == 'K') {
2582 /* Kelvins are currently unsupported. */
2586 db_printf("%jd", mv);
2595 case CTLTYPE_OPAQUE:
2596 /* TODO: Support struct functions. */
2600 db_printf("Format:%s Length:%zu Dump:0x",
2601 g_ddb_oid->oid_fmt, len);
2602 while (len-- && (xflag || p < val + 16))
2603 db_printf("%02x", *p++);
2604 if (!xflag && len > 16)
2611 req->oldidx += slen;
2616 * Avoid setting new sysctl values from the debugger
2619 sysctl_new_ddb(struct sysctl_req *req, void *p, size_t l)
2625 /* Changing sysctls from the debugger is currently unsupported */
2630 * Run a sysctl handler with the DDB oldfunc and newfunc attached.
2631 * Instead of copying any output to a buffer we'll dump it right to
2635 db_sysctl(struct sysctl_oid *oidp, int *name, u_int namelen,
2636 void *old, size_t *oldlenp, size_t *retval, int flags)
2638 struct sysctl_req req;
2641 /* Setup the request */
2642 bzero(&req, sizeof req);
2643 req.td = kdb_thread;
2644 req.oldfunc = sysctl_old_ddb;
2645 req.newfunc = sysctl_new_ddb;
2646 req.lock = REQ_UNWIRED;
2648 req.oldlen = *oldlenp;
2650 req.validlen = req.oldlen;
2655 /* Setup our globals for sysctl_old_ddb */
2657 g_ddb_sysctl_flags = flags;
2658 g_ddb_sysctl_printed = 0;
2660 error = sysctl_root(0, name, namelen, &req);
2664 g_ddb_sysctl_flags = 0;
2667 if (req.oldptr && req.oldidx > req.validlen)
2668 *retval = req.validlen;
2670 *retval = req.oldidx;
2676 * Show a sysctl's name
2679 db_show_oid_name(int *oid, size_t nlen)
2681 struct sysctl_oid *oidp;
2682 int qoid[CTL_MAXNAME+2];
2686 memcpy(qoid + 2, oid, nlen * sizeof(int));
2689 error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
2691 db_error("sysctl name oid");
2693 error = db_sysctl(oidp, qoid, nlen + 2, NULL, NULL, NULL, 0);
2695 db_error("sysctl name");
2699 * Check to see if an OID is safe to print from ddb.
2702 db_oid_safe(const struct sysctl_oid *oidp)
2704 for (unsigned int i = 0; i < nitems(db_safe_handlers); ++i) {
2705 if (oidp->oid_handler == db_safe_handlers[i])
2713 * Show a sysctl at a specific OID
2714 * Compare to the input handling in show_var from sbin/sysctl/sysctl.c
2717 db_show_oid(struct sysctl_oid *oidp, int *oid, size_t nlen, int flags)
2719 int error, xflag, oflag, Nflag, nflag;
2722 xflag = flags & DB_SYSCTL_HEX;
2723 oflag = flags & DB_SYSCTL_OPAQUE;
2724 nflag = flags & DB_SYSCTL_VALUE_ONLY;
2725 Nflag = flags & DB_SYSCTL_NAME_ONLY;
2727 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_OPAQUE &&
2732 db_show_oid_name(oid, nlen);
2738 db_show_oid_name(oid, nlen);
2742 if ((flags & DB_SYSCTL_SAFE_ONLY) && !db_oid_safe(oidp)) {
2743 db_printf("Skipping, unsafe to print while recursing.");
2748 /* Try once, and ask about the size */
2750 error = db_sysctl(oidp, oid, nlen,
2751 NULL, NULL, &len, flags);
2755 if (!g_ddb_sysctl_printed)
2756 /* Lie about the size */
2757 error = db_sysctl(oidp, oid, nlen,
2758 (void *) 1, &len, NULL, flags);
2766 * Show all sysctls under a specific OID
2767 * Compare to sysctl_all from sbin/sysctl/sysctl.c
2770 db_show_sysctl_all(int *oid, size_t len, int flags)
2772 struct sysctl_oid *oidp;
2773 int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
2776 name1[0] = CTL_SYSCTL;
2777 name1[1] = CTL_SYSCTL_NEXT;
2780 memcpy(name1 + 2, oid, len * sizeof(int));
2783 name1[2] = CTL_KERN;
2790 error = kernel_sysctl(kdb_thread, name1, l1,
2791 name2, &l2, NULL, 0, &l2, 0);
2793 if (error == ENOENT)
2796 db_error("sysctl(next)");
2801 if (l2 < (unsigned int)len)
2804 for (i = 0; i < len; i++)
2805 if (name2[i] != oid[i])
2808 /* Find the OID in question */
2809 error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
2813 i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
2818 memcpy(name1+2, name2, l2 * sizeof(int));
2824 * Show a sysctl by its user facing string
2827 db_sysctlbyname(char *name, int flags)
2829 struct sysctl_oid *oidp;
2830 int oid[CTL_MAXNAME];
2833 error = name2oid(name, oid, &nlen, &oidp);
2838 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2839 db_show_sysctl_all(oid, nlen, flags);
2841 error = db_show_oid(oidp, oid, nlen, flags);
2848 db_sysctl_cmd_usage(void)
2851 " sysctl [/Nnox] <sysctl> \n"
2853 " <sysctl> The name of the sysctl to show. \n"
2855 " Show a sysctl by hooking into SYSCTL_IN and SYSCTL_OUT. \n"
2856 " This will work for most sysctls, but should not be used \n"
2857 " with sysctls that are known to malloc. \n"
2859 " While recursing any \"unsafe\" sysctls will be skipped. \n"
2860 " Call sysctl directly on the sysctl to try printing the \n"
2861 " skipped sysctl. This is unsafe and may make the ddb \n"
2862 " session unusable. \n"
2865 " /N Display only the name of the sysctl. \n"
2866 " /n Display only the value of the sysctl. \n"
2867 " /o Display opaque values. \n"
2868 " /x Display the sysctl in hex. \n"
2871 "sysctl vm.v_free_min \n"
2872 "vn.v_free_min: 12669 \n"
2877 * Show a specific sysctl similar to sysctl (8).
2879 DB_FUNC(sysctl, db_sysctl_cmd, db_cmd_table, CS_OWN, NULL)
2881 char name[TOK_STRING_SIZE];
2882 int error, i, t, flags;
2884 /* Parse the modifiers */
2885 t = db_read_token();
2886 if (t == tSLASH || t == tMINUS) {
2887 t = db_read_token();
2889 db_printf("Bad modifier\n");
2893 db_strcpy(modif, db_tok_string);
2901 for (i = 0; i < nitems(db_sysctl_modifs); i++) {
2902 if (strchr(modif, db_sysctl_modifs[i])) {
2903 flags |= db_sysctl_modif_values[i];
2907 /* Parse the sysctl names */
2908 t = db_read_token();
2910 db_printf("Need sysctl name\n");
2915 /* Copy the name into a temporary buffer */
2916 db_strcpy(name, db_tok_string);
2918 /* Ensure there is no trailing cruft */
2919 t = db_read_token();
2921 db_printf("Unexpected sysctl argument\n");
2926 error = db_sysctlbyname(name, flags);
2927 if (error == ENOENT) {
2928 db_printf("unknown oid: '%s'\n", db_tok_string);
2931 db_printf("%s: error: %d\n", db_tok_string, error);
2936 /* Ensure we eat all of our text */
2939 if (error == EINVAL) {
2940 db_sysctl_cmd_usage();