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"
44 #include "opt_ktrace.h"
46 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/capsicum.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/rmlock.h>
61 #include <sys/sysproto.h>
64 #include <sys/ktrace.h>
69 #include <security/mac/mac_framework.h>
72 #include <vm/vm_extern.h>
74 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
75 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
76 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
79 * The sysctllock protects the MIB tree. It also protects sysctl
80 * contexts used with dynamic sysctls. The sysctl_register_oid() and
81 * sysctl_unregister_oid() routines require the sysctllock to already
82 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
83 * provided for the few places in the kernel which need to use that
84 * API rather than using the dynamic API. Use of the dynamic API is
85 * strongly encouraged for most code.
87 * The sysctlmemlock is used to limit the amount of user memory wired for
88 * sysctl requests. This is implemented by serializing any userland
89 * sysctl requests larger than a single page via an exclusive lock.
91 static struct rmlock sysctllock;
92 static struct sx __exclusive_cache_line sysctlmemlock;
94 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
95 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
96 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
97 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
98 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
99 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
100 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
101 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
102 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
104 #define SYSCTL_SLEEP(ch, wmesg, timo) \
105 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
107 static int sysctl_root(SYSCTL_HANDLER_ARGS);
110 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
112 static char* sysctl_escape_name(const char*);
113 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
115 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
116 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
118 static struct sysctl_oid *
119 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
121 struct sysctl_oid *oidp;
123 SYSCTL_ASSERT_LOCKED();
124 SLIST_FOREACH(oidp, list, oid_link) {
125 if (strcmp(oidp->oid_name, name) == 0) {
133 * Initialization of the MIB tree.
135 * Order by number in each list.
152 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
153 struct sysctl_req *req, struct rm_priotracker *tracker)
157 if (oid->oid_kind & CTLFLAG_DYN)
158 atomic_add_int(&oid->oid_running, 1);
161 SYSCTL_RUNLOCK(tracker);
165 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
167 error = oid->oid_handler(oid, arg1, arg2, req);
168 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
171 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
174 SYSCTL_RLOCK(tracker);
178 if (oid->oid_kind & CTLFLAG_DYN) {
179 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
180 (oid->oid_kind & CTLFLAG_DYING) != 0)
181 wakeup(&oid->oid_running);
188 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
190 struct sysctl_req req;
191 struct sysctl_oid *curr;
194 ssize_t rem = sizeof(path);
196 uint8_t data[512] __aligned(sizeof(uint64_t));
202 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
203 len = strlen(curr->oid_name);
208 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
211 memcpy(path + rem, curr->oid_name, len);
213 path[rem + len] = '.';
216 memset(&req, 0, sizeof(req));
219 req.oldfunc = sysctl_old_kernel;
220 req.newfunc = sysctl_new_kernel;
221 req.lock = REQ_UNWIRED;
223 switch (oidp->oid_kind & CTLTYPE) {
225 if (getenv_array(path + rem, data, sizeof(data), &size,
226 sizeof(int), GETENV_SIGNED) == 0)
232 if (getenv_array(path + rem, data, sizeof(data), &size,
233 sizeof(int), GETENV_UNSIGNED) == 0)
239 if (getenv_array(path + rem, data, sizeof(data), &size,
240 sizeof(long), GETENV_SIGNED) == 0)
246 if (getenv_array(path + rem, data, sizeof(data), &size,
247 sizeof(long), GETENV_UNSIGNED) == 0)
253 if (getenv_array(path + rem, data, sizeof(data), &size,
254 sizeof(int8_t), GETENV_SIGNED) == 0)
260 if (getenv_array(path + rem, data, sizeof(data), &size,
261 sizeof(int16_t), GETENV_SIGNED) == 0)
267 if (getenv_array(path + rem, data, sizeof(data), &size,
268 sizeof(int32_t), GETENV_SIGNED) == 0)
274 if (getenv_array(path + rem, data, sizeof(data), &size,
275 sizeof(int64_t), GETENV_SIGNED) == 0)
281 if (getenv_array(path + rem, data, sizeof(data), &size,
282 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
288 if (getenv_array(path + rem, data, sizeof(data), &size,
289 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
295 if (getenv_array(path + rem, data, sizeof(data), &size,
296 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
302 if (getenv_array(path + rem, data, sizeof(data), &size,
303 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
309 penv = kern_getenv(path + rem);
312 req.newlen = strlen(penv);
318 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
319 oidp->oid_arg2, &req, NULL);
321 printf("Setting sysctl %s failed: %d\n", path + rem, error);
327 sbuf_printf_drain(void *arg __unused, const char *data, int len)
330 return (printf("%.*s", len, data));
334 * Locate the path to a given oid. Returns the length of the resulting path,
335 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
336 * elements and be NULL initialized.
339 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
343 SYSCTL_ASSERT_LOCKED();
345 while (indx < CTL_MAXNAME && indx >= 0) {
346 if (nodes[indx] == NULL && indx == 0)
347 nodes[indx] = SLIST_FIRST(&sysctl__children);
348 else if (nodes[indx] == NULL)
349 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
351 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
353 if (nodes[indx] == needle)
356 if (nodes[indx] == NULL) {
361 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
370 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
372 struct sysctl_oid *nodes[CTL_MAXNAME];
377 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
378 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
380 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
382 memset(nodes, 0, sizeof(nodes));
383 rc = sysctl_search_oid(nodes, leaf);
385 for (i = 0; i < rc; i++)
386 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
389 sbuf_printf(&sb, "%s", leaf->oid_name);
391 sbuf_printf(&sb, ")!\n");
393 (void)sbuf_finish(&sb);
398 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
400 struct rm_priotracker tracker;
402 SYSCTL_RLOCK(&tracker);
403 sysctl_warn_reuse(__func__, oidp);
404 SYSCTL_RUNLOCK(&tracker);
407 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
408 0, 0, sysctl_reuse_test, "-", "");
412 sysctl_register_oid(struct sysctl_oid *oidp)
414 struct sysctl_oid_list *parent = oidp->oid_parent;
415 struct sysctl_oid *p;
416 struct sysctl_oid *q;
421 * First check if another oid with the same name already
422 * exists in the parent's list.
424 SYSCTL_ASSERT_WLOCKED();
425 p = sysctl_find_oidname(oidp->oid_name, parent);
427 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
431 sysctl_warn_reuse(__func__, p);
435 /* get current OID number */
436 oid_number = oidp->oid_number;
439 #error "OID_AUTO is expected to be a negative value"
442 * Any negative OID number qualifies as OID_AUTO. Valid OID
443 * numbers should always be positive.
445 * NOTE: DO NOT change the starting value here, change it in
446 * <sys/sysctl.h>, and make sure it is at least 256 to
447 * accommodate e.g. net.inet.raw as a static sysctl node.
449 if (oid_number < 0) {
453 * By decrementing the next OID number we spend less
454 * time inserting the OIDs into a sorted list.
456 if (--newoid < CTL_AUTO_START)
463 * Insert the OID into the parent's list sorted by OID number.
467 SLIST_FOREACH(p, parent, oid_link) {
468 /* check if the current OID number is in use */
469 if (oid_number == p->oid_number) {
470 /* get the next valid OID number */
471 if (oid_number < CTL_AUTO_START ||
472 oid_number == 0x7fffffff) {
473 /* wraparound - restart */
474 oid_number = CTL_AUTO_START;
475 /* don't loop forever */
477 panic("sysctl: Out of OID numbers\n");
482 } else if (oid_number < p->oid_number)
486 /* check for non-auto OID number collision */
487 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
488 oid_number >= CTL_AUTO_START) {
489 printf("sysctl: OID number(%d) is already in use for '%s'\n",
490 oidp->oid_number, oidp->oid_name);
492 /* update the OID number, if any */
493 oidp->oid_number = oid_number;
495 SLIST_INSERT_AFTER(q, oidp, oid_link);
497 SLIST_INSERT_HEAD(parent, oidp, oid_link);
499 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
501 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
503 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
504 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
505 /* only fetch value once */
506 oidp->oid_kind |= CTLFLAG_NOFETCH;
507 /* try to fetch value from kernel environment */
508 sysctl_load_tunable_by_oid_locked(oidp);
513 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
517 * Mark the leaf as dormant if it's not to be immediately enabled.
518 * We do not disable nodes as they can be shared between modules
519 * and it is always safe to access a node.
521 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
522 ("internal flag is set in oid_kind"));
523 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
524 oidp->oid_kind |= CTLFLAG_DORMANT;
525 sysctl_register_oid(oidp);
529 sysctl_enable_oid(struct sysctl_oid *oidp)
532 SYSCTL_ASSERT_WLOCKED();
533 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
534 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
535 ("sysctl node is marked as dormant"));
538 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
539 ("enabling already enabled sysctl oid"));
540 oidp->oid_kind &= ~CTLFLAG_DORMANT;
544 sysctl_unregister_oid(struct sysctl_oid *oidp)
546 struct sysctl_oid *p;
549 SYSCTL_ASSERT_WLOCKED();
550 if (oidp->oid_number == OID_AUTO) {
554 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
556 SLIST_REMOVE(oidp->oid_parent, oidp,
557 sysctl_oid, oid_link);
565 * This can happen when a module fails to register and is
566 * being unloaded afterwards. It should not be a panic()
570 printf("%s: failed(%d) to unregister sysctl(%s)\n",
571 __func__, error, oidp->oid_name);
575 /* Initialize a new context to keep track of dynamically added sysctls. */
577 sysctl_ctx_init(struct sysctl_ctx_list *c)
585 * No locking here, the caller is responsible for not adding
586 * new nodes to a context until after this function has
593 /* Free the context, and destroy all dynamic oids registered in this context */
595 sysctl_ctx_free(struct sysctl_ctx_list *clist)
597 struct sysctl_ctx_entry *e, *e1;
602 * First perform a "dry run" to check if it's ok to remove oids.
604 * XXX This algorithm is a hack. But I don't know any
605 * XXX better solution for now...
608 TAILQ_FOREACH(e, clist, link) {
609 error = sysctl_remove_oid_locked(e->entry, 0, 0);
614 * Restore deregistered entries, either from the end,
615 * or from the place where error occurred.
616 * e contains the entry that was not unregistered
619 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
621 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
623 sysctl_register_oid(e1->entry);
624 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
630 /* Now really delete the entries */
631 e = TAILQ_FIRST(clist);
633 e1 = TAILQ_NEXT(e, link);
634 error = sysctl_remove_oid_locked(e->entry, 1, 0);
636 panic("sysctl_remove_oid: corrupt tree, entry: %s",
638 free(e, M_SYSCTLOID);
645 /* Add an entry to the context */
646 struct sysctl_ctx_entry *
647 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
649 struct sysctl_ctx_entry *e;
651 SYSCTL_ASSERT_WLOCKED();
652 if (clist == NULL || oidp == NULL)
654 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
656 TAILQ_INSERT_HEAD(clist, e, link);
660 /* Find an entry in the context */
661 struct sysctl_ctx_entry *
662 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
664 struct sysctl_ctx_entry *e;
666 SYSCTL_ASSERT_WLOCKED();
667 if (clist == NULL || oidp == NULL)
669 TAILQ_FOREACH(e, clist, link) {
677 * Delete an entry from the context.
678 * NOTE: this function doesn't free oidp! You have to remove it
679 * with sysctl_remove_oid().
682 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
684 struct sysctl_ctx_entry *e;
686 if (clist == NULL || oidp == NULL)
689 e = sysctl_ctx_entry_find(clist, oidp);
691 TAILQ_REMOVE(clist, e, link);
693 free(e, M_SYSCTLOID);
702 * Remove dynamically created sysctl trees.
703 * oidp - top of the tree to be removed
704 * del - if 0 - just deregister, otherwise free up entries as well
705 * recurse - if != 0 traverse the subtree to be deleted
708 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
713 error = sysctl_remove_oid_locked(oidp, del, recurse);
719 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
720 int del, int recurse)
722 struct sysctl_oid *p, *tmp;
727 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
728 if (strcmp(p->oid_name, name) == 0) {
729 error = sysctl_remove_oid_locked(p, del, recurse);
739 * Duplicate the provided string, escaping any illegal characters. The result
740 * must be freed when no longer in use.
742 * The list of illegal characters is ".".
745 sysctl_escape_name(const char* orig)
747 int i, s = 0, d = 0, nillegals = 0;
750 /* First count the number of illegal characters */
751 for (i = 0; orig[i] != '\0'; i++) {
756 /* Allocate storage for new string */
757 new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
759 /* Copy the name, escaping characters as we go */
760 while (orig[s] != '\0') {
761 if (orig[s] == '.') {
762 /* %25 is the hexadecimal representation of '.' */
768 new[d++] = orig[s++];
772 /* Finally, nul-terminate */
779 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
781 struct sysctl_oid *p, *tmp;
784 SYSCTL_ASSERT_WLOCKED();
787 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
788 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
793 * WARNING: normal method to do this should be through
794 * sysctl_ctx_free(). Use recursing as the last resort
795 * method to purge your sysctl tree of leftovers...
796 * However, if some other code still references these nodes,
799 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
800 if (oidp->oid_refcnt == 1) {
801 SLIST_FOREACH_SAFE(p,
802 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
804 printf("Warning: failed attempt to "
805 "remove oid %s with child %s\n",
806 oidp->oid_name, p->oid_name);
809 error = sysctl_remove_oid_locked(p, del,
816 if (oidp->oid_refcnt > 1 ) {
819 if (oidp->oid_refcnt == 0) {
820 printf("Warning: bad oid_refcnt=%u (%s)!\n",
821 oidp->oid_refcnt, oidp->oid_name);
824 sysctl_unregister_oid(oidp);
827 * Wait for all threads running the handler to drain.
828 * This preserves the previous behavior when the
829 * sysctl lock was held across a handler invocation,
830 * and is necessary for module unload correctness.
832 while (oidp->oid_running > 0) {
833 oidp->oid_kind |= CTLFLAG_DYING;
834 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
837 free(__DECONST(char *, oidp->oid_descr),
840 free(__DECONST(char *, oidp->oid_label),
842 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
843 free(oidp, M_SYSCTLOID);
849 * Create new sysctls at run time.
850 * clist may point to a valid context initialized with sysctl_ctx_init().
853 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
854 int number, const char *name, int kind, void *arg1, intmax_t arg2,
855 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
858 struct sysctl_oid *oidp;
861 /* You have to hook up somewhere.. */
864 escaped = sysctl_escape_name(name);
865 /* Check if the node already exists, otherwise create it */
867 oidp = sysctl_find_oidname(escaped, parent);
869 free(escaped, M_SYSCTLOID);
870 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
872 /* Update the context */
874 sysctl_ctx_entry_add(clist, oidp);
878 sysctl_warn_reuse(__func__, oidp);
883 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
884 oidp->oid_parent = parent;
885 SLIST_INIT(&oidp->oid_children);
886 oidp->oid_number = number;
887 oidp->oid_refcnt = 1;
888 oidp->oid_name = escaped;
889 oidp->oid_handler = handler;
890 oidp->oid_kind = CTLFLAG_DYN | kind;
891 oidp->oid_arg1 = arg1;
892 oidp->oid_arg2 = arg2;
895 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
897 oidp->oid_label = strdup(label, M_SYSCTLOID);
898 /* Update the context, if used */
900 sysctl_ctx_entry_add(clist, oidp);
901 /* Register this oid */
902 sysctl_register_oid(oidp);
908 * Rename an existing oid.
911 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
916 newname = strdup(name, M_SYSCTLOID);
918 oldname = __DECONST(char *, oidp->oid_name);
919 oidp->oid_name = newname;
921 free(oldname, M_SYSCTLOID);
925 * Reparent an existing oid.
928 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
930 struct sysctl_oid *oidp;
933 if (oid->oid_parent == parent) {
937 oidp = sysctl_find_oidname(oid->oid_name, parent);
942 sysctl_unregister_oid(oid);
943 oid->oid_parent = parent;
944 oid->oid_number = OID_AUTO;
945 sysctl_register_oid(oid);
951 * Register the kernel's oids on startup.
953 SET_DECLARE(sysctl_set, struct sysctl_oid);
956 sysctl_register_all(void *arg)
958 struct sysctl_oid **oidp;
960 sx_init(&sysctlmemlock, "sysctl mem");
963 SET_FOREACH(oidp, sysctl_set)
964 sysctl_register_oid(*oidp);
967 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
972 * These functions implement a presently undocumented interface
973 * used by the sysctl program to walk the tree, and get the type
974 * so it can print the value.
975 * This interface is under work and consideration, and should probably
976 * be killed with a big axe by the first person who can find the time.
977 * (be aware though, that the proper interface isn't as obvious as it
978 * may seem, there are various conflicting requirements.
980 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} printf the entire MIB-tree.
981 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
983 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID, honoring
985 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
987 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
989 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
991 * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...} return the aggregation label of
993 * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...} return the next OID, ignoring
999 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
1002 struct sysctl_oid *oidp;
1004 SYSCTL_ASSERT_LOCKED();
1005 SLIST_FOREACH(oidp, l, oid_link) {
1010 printf("%d %s ", oidp->oid_number, oidp->oid_name);
1013 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
1014 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
1016 if (oidp->oid_handler)
1017 printf(" *Handler");
1019 switch (oidp->oid_kind & CTLTYPE) {
1022 if (!oidp->oid_handler) {
1023 sysctl_sysctl_debug_dump_node(
1024 SYSCTL_CHILDREN(oidp), i + 2);
1027 case CTLTYPE_INT: printf(" Int\n"); break;
1028 case CTLTYPE_UINT: printf(" u_int\n"); break;
1029 case CTLTYPE_LONG: printf(" Long\n"); break;
1030 case CTLTYPE_ULONG: printf(" u_long\n"); break;
1031 case CTLTYPE_STRING: printf(" String\n"); break;
1032 case CTLTYPE_S8: printf(" int8_t\n"); break;
1033 case CTLTYPE_S16: printf(" int16_t\n"); break;
1034 case CTLTYPE_S32: printf(" int32_t\n"); break;
1035 case CTLTYPE_S64: printf(" int64_t\n"); break;
1036 case CTLTYPE_U8: printf(" uint8_t\n"); break;
1037 case CTLTYPE_U16: printf(" uint16_t\n"); break;
1038 case CTLTYPE_U32: printf(" uint32_t\n"); break;
1039 case CTLTYPE_U64: printf(" uint64_t\n"); break;
1040 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
1041 default: printf("\n");
1048 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1050 struct rm_priotracker tracker;
1053 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1056 SYSCTL_RLOCK(&tracker);
1057 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1058 SYSCTL_RUNLOCK(&tracker);
1062 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
1063 CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
1067 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1069 int *name = (int *) arg1;
1070 u_int namelen = arg2;
1072 struct sysctl_oid *oid;
1073 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1074 struct rm_priotracker tracker;
1077 error = sysctl_wire_old_buffer(req, 0);
1081 SYSCTL_RLOCK(&tracker);
1084 snprintf(buf,sizeof(buf),"%d",*name);
1086 error = SYSCTL_OUT(req, ".", 1);
1088 error = SYSCTL_OUT(req, buf, strlen(buf));
1096 SLIST_FOREACH(oid, lsp, oid_link) {
1097 if (oid->oid_number != *name)
1101 error = SYSCTL_OUT(req, ".", 1);
1103 error = SYSCTL_OUT(req, oid->oid_name,
1104 strlen(oid->oid_name));
1111 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1114 if (oid->oid_handler)
1117 lsp2 = SYSCTL_CHILDREN(oid);
1122 error = SYSCTL_OUT(req, "", 1);
1124 SYSCTL_RUNLOCK(&tracker);
1129 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1132 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
1133 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
1136 * Walk the sysctl subtree at lsp until we find the given name,
1137 * and return the next name in order by oid_number.
1140 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1141 int *next, int *len, int level, bool honor_skip)
1143 struct sysctl_oid *oidp;
1145 SYSCTL_ASSERT_LOCKED();
1147 SLIST_FOREACH(oidp, lsp, oid_link) {
1148 *next = oidp->oid_number;
1150 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
1153 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
1158 * We have reached a node with a full name match and are
1159 * looking for the next oid in its children.
1161 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1163 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1164 * has a handler) and move on to the children.
1168 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1170 if (oidp->oid_handler)
1172 lsp = SYSCTL_CHILDREN(oidp);
1173 if (!sysctl_sysctl_next_ls(lsp, NULL, 0, next + 1, len,
1174 level + 1, honor_skip))
1177 * There were no useable children in this node.
1178 * Continue searching for the next oid at this level.
1184 * No match yet. Continue seeking the given name.
1186 * We are iterating in order by oid_number, so skip oids lower
1187 * than the one we are looking for.
1189 * When the current oid_number is higher than the one we seek,
1190 * that means we have reached the next oid in the sequence and
1193 * If the oid_number matches the name at this level then we
1194 * have to find a node to continue searching at the next level.
1196 if (oidp->oid_number < *name)
1198 if (oidp->oid_number > *name) {
1200 * We have reached the next oid.
1202 * For CTL_SYSCTL_NEXTNOSKIP we are done.
1204 * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
1205 * has a handler) and move on to the children.
1209 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1211 if (oidp->oid_handler)
1213 lsp = SYSCTL_CHILDREN(oidp);
1214 if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
1215 next + 1, len, level + 1, honor_skip))
1219 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1221 if (oidp->oid_handler)
1223 lsp = SYSCTL_CHILDREN(oidp);
1224 if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
1225 next + 1, len, level + 1, honor_skip))
1229 * There were no useable children in this node.
1230 * Continue searching for the next oid at the root level.
1234 /* Reset len in case a failed recursive call changed it. */
1241 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1243 int *name = (int *) arg1;
1244 u_int namelen = arg2;
1246 struct sysctl_oid_list *lsp = &sysctl__children;
1247 struct rm_priotracker tracker;
1248 int next[CTL_MAXNAME];
1250 SYSCTL_RLOCK(&tracker);
1251 error = sysctl_sysctl_next_ls(lsp, name, namelen, next, &len, 1,
1252 oidp->oid_number == CTL_SYSCTL_NEXT);
1253 SYSCTL_RUNLOCK(&tracker);
1256 error = SYSCTL_OUT(req, next, len * sizeof (int));
1261 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1264 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
1265 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1267 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
1268 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
1271 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1273 struct sysctl_oid *oidp;
1274 struct sysctl_oid_list *lsp = &sysctl__children;
1277 SYSCTL_ASSERT_LOCKED();
1279 for (*len = 0; *len < CTL_MAXNAME;) {
1280 p = strsep(&name, ".");
1282 oidp = SLIST_FIRST(lsp);
1283 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1286 if (strcmp(p, oidp->oid_name) == 0)
1289 *oid++ = oidp->oid_number;
1292 if (name == NULL || *name == '\0') {
1298 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1301 if (oidp->oid_handler)
1304 lsp = SYSCTL_CHILDREN(oidp);
1310 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1313 int error, oid[CTL_MAXNAME], len = 0;
1314 struct sysctl_oid *op = NULL;
1315 struct rm_priotracker tracker;
1320 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1321 return (ENAMETOOLONG);
1324 if (req->newlen >= sizeof(buf))
1325 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1327 error = SYSCTL_IN(req, p, req->newlen);
1334 p [req->newlen] = '\0';
1336 SYSCTL_RLOCK(&tracker);
1337 error = name2oid(p, oid, &len, &op);
1338 SYSCTL_RUNLOCK(&tracker);
1346 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1351 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1354 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
1355 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
1356 sysctl_sysctl_name2oid, "I", "");
1359 sysctl_sysctl_oidfmt(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_fmt == NULL) {
1378 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1381 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1383 SYSCTL_RUNLOCK(&tracker);
1388 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
1389 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
1392 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1394 struct sysctl_oid *oid;
1395 struct rm_priotracker tracker;
1398 error = sysctl_wire_old_buffer(req, 0);
1402 SYSCTL_RLOCK(&tracker);
1403 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1407 if (oid->oid_descr == NULL) {
1411 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1413 SYSCTL_RUNLOCK(&tracker);
1417 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
1418 CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
1421 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1423 struct sysctl_oid *oid;
1424 struct rm_priotracker tracker;
1427 error = sysctl_wire_old_buffer(req, 0);
1431 SYSCTL_RLOCK(&tracker);
1432 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1436 if (oid->oid_label == NULL) {
1440 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1442 SYSCTL_RUNLOCK(&tracker);
1446 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
1447 CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1450 * Default "handler" functions.
1456 * a variable: point arg1 at it.
1457 * a constant: pass it in arg2.
1461 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1467 * Attempt to get a coherent snapshot by making a copy of the data.
1470 temp = *(bool *)arg1 ? 1 : 0;
1472 temp = arg2 ? 1 : 0;
1474 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1475 if (error || !req->newptr)
1481 error = SYSCTL_IN(req, &temp, sizeof(temp));
1483 *(bool *)arg1 = temp ? 1 : 0;
1489 * Handle an int8_t, signed or unsigned.
1491 * a variable: point arg1 at it.
1492 * a constant: pass it in arg2.
1496 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1502 * Attempt to get a coherent snapshot by making a copy of the data.
1505 tmpout = *(int8_t *)arg1;
1508 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1510 if (error || !req->newptr)
1516 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1521 * Handle an int16_t, signed or unsigned.
1523 * a variable: point arg1 at it.
1524 * a constant: pass it in arg2.
1528 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1534 * Attempt to get a coherent snapshot by making a copy of the data.
1537 tmpout = *(int16_t *)arg1;
1540 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1542 if (error || !req->newptr)
1548 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1553 * Handle an int32_t, signed or unsigned.
1555 * a variable: point arg1 at it.
1556 * a constant: pass it in arg2.
1560 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1566 * Attempt to get a coherent snapshot by making a copy of the data.
1569 tmpout = *(int32_t *)arg1;
1572 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1574 if (error || !req->newptr)
1580 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1585 * Handle an int, signed or unsigned.
1587 * a variable: point arg1 at it.
1588 * a constant: pass it in arg2.
1592 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1594 int tmpout, error = 0;
1597 * Attempt to get a coherent snapshot by making a copy of the data.
1600 tmpout = *(int *)arg1;
1603 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1605 if (error || !req->newptr)
1611 error = SYSCTL_IN(req, arg1, sizeof(int));
1616 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1617 * Note: this is used by TCP.
1621 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1626 s = (int)((int64_t)tt * 1000 / hz);
1628 error = sysctl_handle_int(oidp, &s, 0, req);
1629 if (error || !req->newptr)
1632 tt = (int)((int64_t)s * hz / 1000);
1642 * Handle a long, signed or unsigned.
1644 * a variable: point arg1 at it.
1645 * a constant: pass it in arg2.
1649 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1658 * Attempt to get a coherent snapshot by making a copy of the data.
1661 tmplong = *(long *)arg1;
1665 if (req->flags & SCTL_MASK32) {
1667 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1670 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1672 if (error || !req->newptr)
1678 else if (req->flags & SCTL_MASK32) {
1679 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1680 *(long *)arg1 = (long)tmpint;
1684 error = SYSCTL_IN(req, arg1, sizeof(long));
1689 * Handle a 64 bit int, signed or unsigned.
1691 * a variable: point arg1 at it.
1692 * a constant: pass it in arg2.
1695 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1701 * Attempt to get a coherent snapshot by making a copy of the data.
1704 tmpout = *(uint64_t *)arg1;
1707 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1709 if (error || !req->newptr)
1715 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1720 * Handle our generic '\0' terminated 'C' string.
1722 * a variable string: point arg1 at it, arg2 is max length.
1723 * a constant string: point arg1 at it, arg2 is zero.
1727 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1730 int error = 0, ro_string = 0;
1733 * A zero-length buffer indicates a fixed size read-only
1737 arg2 = strlen((char *)arg1) + 1;
1741 if (req->oldptr != NULL) {
1747 /* try to make a coherent snapshot of the string */
1748 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1749 memcpy(tmparg, arg1, arg2);
1752 outlen = strnlen(tmparg, arg2 - 1) + 1;
1753 error = SYSCTL_OUT(req, tmparg, outlen);
1756 free(tmparg, M_SYSCTLTMP);
1758 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1759 error = SYSCTL_OUT(req, NULL, outlen);
1761 if (error || !req->newptr)
1764 if ((req->newlen - req->newidx) >= arg2) {
1767 arg2 = (req->newlen - req->newidx);
1768 error = SYSCTL_IN(req, arg1, arg2);
1769 ((char *)arg1)[arg2] = '\0';
1775 * Handle any kind of opaque data.
1776 * arg1 points to it, arg2 is the size.
1780 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1784 struct sysctl_req req2;
1787 * Attempt to get a coherent snapshot, by using the thread
1788 * pre-emption counter updated from within mi_switch() to
1789 * determine if we were pre-empted during a bcopy() or
1790 * copyout(). Make 3 attempts at doing this before giving up.
1791 * If we encounter an error, stop immediately.
1796 generation = curthread->td_generation;
1797 error = SYSCTL_OUT(req, arg1, arg2);
1801 if (generation != curthread->td_generation && tries < 3) {
1806 error = SYSCTL_IN(req, arg1, arg2);
1812 * Convert seconds to a struct timeval. Intended for use with
1813 * intervals and thus does not permit negative seconds.
1816 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1824 error = sysctl_handle_int(oidp, &secs, 0, req);
1825 if (error || req->newptr == NULL)
1836 * Transfer functions to/from kernel space.
1837 * XXX: rather untested at this point
1840 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1846 if (req->oldlen <= req->oldidx)
1849 if (i > req->oldlen - req->oldidx)
1850 i = req->oldlen - req->oldidx;
1852 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1855 if (req->oldptr && i != l)
1861 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1865 if (req->newlen - req->newidx < l)
1867 bcopy((char *)req->newptr + req->newidx, p, l);
1873 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1874 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1877 struct sysctl_req req;
1879 bzero(&req, sizeof req);
1885 req.oldlen = *oldlenp;
1887 req.validlen = req.oldlen;
1894 req.newlen = newlen;
1898 req.oldfunc = sysctl_old_kernel;
1899 req.newfunc = sysctl_new_kernel;
1900 req.lock = REQ_UNWIRED;
1902 error = sysctl_root(0, name, namelen, &req);
1904 if (req.lock == REQ_WIRED && req.validlen > 0)
1905 vsunlock(req.oldptr, req.validlen);
1907 if (error && error != ENOMEM)
1911 if (req.oldptr && req.oldidx > req.validlen)
1912 *retval = req.validlen;
1914 *retval = req.oldidx;
1920 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1921 void *new, size_t newlen, size_t *retval, int flags)
1923 int oid[CTL_MAXNAME];
1924 size_t oidlen, plen;
1927 oid[0] = CTL_SYSCTL;
1928 oid[1] = CTL_SYSCTL_NAME2OID;
1929 oidlen = sizeof(oid);
1931 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1932 (void *)name, strlen(name), &plen, flags);
1936 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1937 new, newlen, retval, flags);
1942 * Transfer function to/from user space.
1945 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1947 size_t i, len, origidx;
1950 origidx = req->oldidx;
1952 if (req->oldptr == NULL)
1955 * If we have not wired the user supplied buffer and we are currently
1956 * holding locks, drop a witness warning, as it's possible that
1957 * write operations to the user page can sleep.
1959 if (req->lock != REQ_WIRED)
1960 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1961 "sysctl_old_user()");
1963 len = req->validlen;
1967 if (i > len - origidx)
1969 if (req->lock == REQ_WIRED) {
1970 error = copyout_nofault(p, (char *)req->oldptr +
1973 error = copyout(p, (char *)req->oldptr + origidx, i);
1983 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1989 if (req->newlen - req->newidx < l)
1991 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1992 "sysctl_new_user()");
1993 error = copyin((char *)req->newptr + req->newidx, p, l);
1999 * Wire the user space destination buffer. If set to a value greater than
2000 * zero, the len parameter limits the maximum amount of wired memory.
2003 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
2008 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
2010 if (req->lock != REQ_WIRED && req->oldptr &&
2011 req->oldfunc == sysctl_old_user) {
2012 if (wiredlen != 0) {
2013 ret = vslock(req->oldptr, wiredlen);
2020 req->lock = REQ_WIRED;
2021 req->validlen = wiredlen;
2027 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
2028 int *nindx, struct sysctl_req *req)
2030 struct sysctl_oid_list *lsp;
2031 struct sysctl_oid *oid;
2034 SYSCTL_ASSERT_LOCKED();
2035 lsp = &sysctl__children;
2037 while (indx < CTL_MAXNAME) {
2038 SLIST_FOREACH(oid, lsp, oid_link) {
2039 if (oid->oid_number == name[indx])
2046 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2047 if (oid->oid_handler != NULL || indx == namelen) {
2051 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2052 ("%s found DYING node %p", __func__, oid));
2055 lsp = SYSCTL_CHILDREN(oid);
2056 } else if (indx == namelen) {
2057 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
2062 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
2063 ("%s found DYING node %p", __func__, oid));
2073 * Traverse our tree, and find the right node, execute whatever it points
2074 * to, and return the resulting error code.
2078 sysctl_root(SYSCTL_HANDLER_ARGS)
2080 struct sysctl_oid *oid;
2081 struct rm_priotracker tracker;
2082 int error, indx, lvl;
2084 SYSCTL_RLOCK(&tracker);
2086 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2090 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2092 * You can't call a sysctl when it's a node, but has
2093 * no handler. Inform the user that it's a node.
2094 * The indx may or may not be the same as namelen.
2096 if (oid->oid_handler == NULL) {
2102 /* Is this sysctl writable? */
2103 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2108 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2110 #ifdef CAPABILITY_MODE
2112 * If the process is in capability mode, then don't permit reading or
2113 * writing unless specifically granted for the node.
2115 if (IN_CAPABILITY_MODE(req->td)) {
2116 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2117 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2124 /* Is this sysctl sensitive to securelevels? */
2125 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2126 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2127 error = securelevel_gt(req->td->td_ucred, lvl);
2132 /* Is this sysctl writable by only privileged users? */
2133 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2136 if (oid->oid_kind & CTLFLAG_PRISON)
2137 priv = PRIV_SYSCTL_WRITEJAIL;
2139 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2140 prison_owns_vnet(req->td->td_ucred))
2141 priv = PRIV_SYSCTL_WRITEJAIL;
2144 priv = PRIV_SYSCTL_WRITE;
2145 error = priv_check(req->td, priv);
2150 if (!oid->oid_handler) {
2155 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2156 arg1 = (int *)arg1 + indx;
2159 arg1 = oid->oid_arg1;
2160 arg2 = oid->oid_arg2;
2163 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2169 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2170 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2172 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2175 SYSCTL_RUNLOCK(&tracker);
2179 #ifndef _SYS_SYSPROTO_H_
2180 struct sysctl_args {
2190 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2192 int error, i, name[CTL_MAXNAME];
2195 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2198 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2202 error = userland_sysctl(td, name, uap->namelen,
2203 uap->old, uap->oldlenp, 0,
2204 uap->new, uap->newlen, &j, 0);
2205 if (error && error != ENOMEM)
2208 i = copyout(&j, uap->oldlenp, sizeof(j));
2216 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
2217 void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
2218 int flags, bool inkernel)
2220 int oid[CTL_MAXNAME];
2226 if (namelen > MAXPATHLEN || namelen == 0)
2229 if (namelen > sizeof(namebuf))
2230 name = malloc(namelen, M_SYSCTL, M_WAITOK);
2231 error = copyin(oname, name, namelen);
2235 oid[0] = CTL_SYSCTL;
2236 oid[1] = CTL_SYSCTL_NAME2OID;
2237 oidlen = sizeof(oid);
2238 error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
2242 error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
2243 inkernel, new, newlen, retval, flags);
2246 if (namelen > sizeof(namebuf))
2247 free(name, M_SYSCTL);
2251 #ifndef _SYS_SYSPROTO_H_
2252 struct __sysctlbyname_args {
2262 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
2267 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2268 uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
2271 if (uap->oldlenp != NULL)
2272 error = copyout(&rv, uap->oldlenp, sizeof(rv));
2278 * This is used from various compatibility syscalls too. That's why name
2279 * must be in kernel space.
2282 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2283 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2286 int error = 0, memlocked;
2287 struct sysctl_req req;
2289 bzero(&req, sizeof req);
2296 req.oldlen = *oldlenp;
2298 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2303 req.validlen = req.oldlen;
2307 req.newlen = newlen;
2311 req.oldfunc = sysctl_old_user;
2312 req.newfunc = sysctl_new_user;
2313 req.lock = REQ_UNWIRED;
2316 if (KTRPOINT(curthread, KTR_SYSCTL))
2317 ktrsysctl(name, namelen);
2320 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2322 sx_xlock(&sysctlmemlock);
2324 CURVNET_SET(TD_TO_VNET(td));
2329 error = sysctl_root(0, name, namelen, &req);
2330 if (error != EAGAIN)
2332 kern_yield(PRI_USER);
2337 if (req.lock == REQ_WIRED && req.validlen > 0)
2338 vsunlock(req.oldptr, req.validlen);
2340 sx_xunlock(&sysctlmemlock);
2342 if (error && error != ENOMEM)
2346 if (req.oldptr && req.oldidx > req.validlen)
2347 *retval = req.validlen;
2349 *retval = req.oldidx;
2355 * Drain into a sysctl struct. The user buffer should be wired if a page
2356 * fault would cause issue.
2359 sbuf_sysctl_drain(void *arg, const char *data, int len)
2361 struct sysctl_req *req = arg;
2364 error = SYSCTL_OUT(req, data, len);
2365 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2366 return (error == 0 ? len : -error);
2370 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2371 struct sysctl_req *req)
2374 /* Supply a default buffer size if none given. */
2375 if (buf == NULL && length == 0)
2377 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2378 sbuf_set_drain(s, sbuf_sysctl_drain, req);