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 int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
114 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
115 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
117 static struct sysctl_oid *
118 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
120 struct sysctl_oid *oidp;
122 SYSCTL_ASSERT_LOCKED();
123 SLIST_FOREACH(oidp, list, oid_link) {
124 if (strcmp(oidp->oid_name, name) == 0) {
132 * Initialization of the MIB tree.
134 * Order by number in each list.
151 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
152 struct sysctl_req *req, struct rm_priotracker *tracker)
156 if (oid->oid_kind & CTLFLAG_DYN)
157 atomic_add_int(&oid->oid_running, 1);
160 SYSCTL_RUNLOCK(tracker);
164 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
166 error = oid->oid_handler(oid, arg1, arg2, req);
167 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
170 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
173 SYSCTL_RLOCK(tracker);
177 if (oid->oid_kind & CTLFLAG_DYN) {
178 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
179 (oid->oid_kind & CTLFLAG_DYING) != 0)
180 wakeup(&oid->oid_running);
187 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
189 struct sysctl_req req;
190 struct sysctl_oid *curr;
193 ssize_t rem = sizeof(path);
195 uint8_t data[512] __aligned(sizeof(uint64_t));
201 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
202 len = strlen(curr->oid_name);
207 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
210 memcpy(path + rem, curr->oid_name, len);
212 path[rem + len] = '.';
215 memset(&req, 0, sizeof(req));
218 req.oldfunc = sysctl_old_kernel;
219 req.newfunc = sysctl_new_kernel;
220 req.lock = REQ_UNWIRED;
222 switch (oidp->oid_kind & CTLTYPE) {
224 if (getenv_array(path + rem, data, sizeof(data), &size,
225 sizeof(int), GETENV_SIGNED) == 0)
231 if (getenv_array(path + rem, data, sizeof(data), &size,
232 sizeof(int), GETENV_UNSIGNED) == 0)
238 if (getenv_array(path + rem, data, sizeof(data), &size,
239 sizeof(long), GETENV_SIGNED) == 0)
245 if (getenv_array(path + rem, data, sizeof(data), &size,
246 sizeof(long), GETENV_UNSIGNED) == 0)
252 if (getenv_array(path + rem, data, sizeof(data), &size,
253 sizeof(int8_t), GETENV_SIGNED) == 0)
259 if (getenv_array(path + rem, data, sizeof(data), &size,
260 sizeof(int16_t), GETENV_SIGNED) == 0)
266 if (getenv_array(path + rem, data, sizeof(data), &size,
267 sizeof(int32_t), GETENV_SIGNED) == 0)
273 if (getenv_array(path + rem, data, sizeof(data), &size,
274 sizeof(int64_t), GETENV_SIGNED) == 0)
280 if (getenv_array(path + rem, data, sizeof(data), &size,
281 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
287 if (getenv_array(path + rem, data, sizeof(data), &size,
288 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
294 if (getenv_array(path + rem, data, sizeof(data), &size,
295 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
301 if (getenv_array(path + rem, data, sizeof(data), &size,
302 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
308 penv = kern_getenv(path + rem);
311 req.newlen = strlen(penv);
317 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
318 oidp->oid_arg2, &req, NULL);
320 printf("Setting sysctl %s failed: %d\n", path + rem, error);
326 sbuf_printf_drain(void *arg __unused, const char *data, int len)
329 return (printf("%.*s", len, data));
333 * Locate the path to a given oid. Returns the length of the resulting path,
334 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
335 * elements and be NULL initialized.
338 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
342 SYSCTL_ASSERT_LOCKED();
344 while (indx < CTL_MAXNAME && indx >= 0) {
345 if (nodes[indx] == NULL && indx == 0)
346 nodes[indx] = SLIST_FIRST(&sysctl__children);
347 else if (nodes[indx] == NULL)
348 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
350 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
352 if (nodes[indx] == needle)
355 if (nodes[indx] == NULL) {
360 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
369 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
371 struct sysctl_oid *nodes[CTL_MAXNAME];
376 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
377 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
379 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
381 memset(nodes, 0, sizeof(nodes));
382 rc = sysctl_search_oid(nodes, leaf);
384 for (i = 0; i < rc; i++)
385 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
388 sbuf_printf(&sb, "%s", leaf->oid_name);
390 sbuf_printf(&sb, ")!\n");
392 (void)sbuf_finish(&sb);
397 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
399 struct rm_priotracker tracker;
401 SYSCTL_RLOCK(&tracker);
402 sysctl_warn_reuse(__func__, oidp);
403 SYSCTL_RUNLOCK(&tracker);
406 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
407 0, 0, sysctl_reuse_test, "-", "");
411 sysctl_register_oid(struct sysctl_oid *oidp)
413 struct sysctl_oid_list *parent = oidp->oid_parent;
414 struct sysctl_oid *p;
415 struct sysctl_oid *q;
420 * First check if another oid with the same name already
421 * exists in the parent's list.
423 SYSCTL_ASSERT_WLOCKED();
424 p = sysctl_find_oidname(oidp->oid_name, parent);
426 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
430 sysctl_warn_reuse(__func__, p);
434 /* get current OID number */
435 oid_number = oidp->oid_number;
438 #error "OID_AUTO is expected to be a negative value"
441 * Any negative OID number qualifies as OID_AUTO. Valid OID
442 * numbers should always be positive.
444 * NOTE: DO NOT change the starting value here, change it in
445 * <sys/sysctl.h>, and make sure it is at least 256 to
446 * accommodate e.g. net.inet.raw as a static sysctl node.
448 if (oid_number < 0) {
452 * By decrementing the next OID number we spend less
453 * time inserting the OIDs into a sorted list.
455 if (--newoid < CTL_AUTO_START)
462 * Insert the OID into the parent's list sorted by OID number.
466 SLIST_FOREACH(p, parent, oid_link) {
467 /* check if the current OID number is in use */
468 if (oid_number == p->oid_number) {
469 /* get the next valid OID number */
470 if (oid_number < CTL_AUTO_START ||
471 oid_number == 0x7fffffff) {
472 /* wraparound - restart */
473 oid_number = CTL_AUTO_START;
474 /* don't loop forever */
476 panic("sysctl: Out of OID numbers\n");
481 } else if (oid_number < p->oid_number)
485 /* check for non-auto OID number collision */
486 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
487 oid_number >= CTL_AUTO_START) {
488 printf("sysctl: OID number(%d) is already in use for '%s'\n",
489 oidp->oid_number, oidp->oid_name);
491 /* update the OID number, if any */
492 oidp->oid_number = oid_number;
494 SLIST_INSERT_AFTER(q, oidp, oid_link);
496 SLIST_INSERT_HEAD(parent, oidp, oid_link);
498 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
500 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
502 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
503 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
504 /* only fetch value once */
505 oidp->oid_kind |= CTLFLAG_NOFETCH;
506 /* try to fetch value from kernel environment */
507 sysctl_load_tunable_by_oid_locked(oidp);
512 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
516 * Mark the leaf as dormant if it's not to be immediately enabled.
517 * We do not disable nodes as they can be shared between modules
518 * and it is always safe to access a node.
520 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
521 ("internal flag is set in oid_kind"));
522 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
523 oidp->oid_kind |= CTLFLAG_DORMANT;
524 sysctl_register_oid(oidp);
528 sysctl_enable_oid(struct sysctl_oid *oidp)
531 SYSCTL_ASSERT_WLOCKED();
532 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
533 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
534 ("sysctl node is marked as dormant"));
537 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
538 ("enabling already enabled sysctl oid"));
539 oidp->oid_kind &= ~CTLFLAG_DORMANT;
543 sysctl_unregister_oid(struct sysctl_oid *oidp)
545 struct sysctl_oid *p;
548 SYSCTL_ASSERT_WLOCKED();
549 if (oidp->oid_number == OID_AUTO) {
553 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
555 SLIST_REMOVE(oidp->oid_parent, oidp,
556 sysctl_oid, oid_link);
564 * This can happen when a module fails to register and is
565 * being unloaded afterwards. It should not be a panic()
569 printf("%s: failed(%d) to unregister sysctl(%s)\n",
570 __func__, error, oidp->oid_name);
574 /* Initialize a new context to keep track of dynamically added sysctls. */
576 sysctl_ctx_init(struct sysctl_ctx_list *c)
584 * No locking here, the caller is responsible for not adding
585 * new nodes to a context until after this function has
592 /* Free the context, and destroy all dynamic oids registered in this context */
594 sysctl_ctx_free(struct sysctl_ctx_list *clist)
596 struct sysctl_ctx_entry *e, *e1;
601 * First perform a "dry run" to check if it's ok to remove oids.
603 * XXX This algorithm is a hack. But I don't know any
604 * XXX better solution for now...
607 TAILQ_FOREACH(e, clist, link) {
608 error = sysctl_remove_oid_locked(e->entry, 0, 0);
613 * Restore deregistered entries, either from the end,
614 * or from the place where error occurred.
615 * e contains the entry that was not unregistered
618 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
620 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
622 sysctl_register_oid(e1->entry);
623 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
629 /* Now really delete the entries */
630 e = TAILQ_FIRST(clist);
632 e1 = TAILQ_NEXT(e, link);
633 error = sysctl_remove_oid_locked(e->entry, 1, 0);
635 panic("sysctl_remove_oid: corrupt tree, entry: %s",
637 free(e, M_SYSCTLOID);
644 /* Add an entry to the context */
645 struct sysctl_ctx_entry *
646 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
648 struct sysctl_ctx_entry *e;
650 SYSCTL_ASSERT_WLOCKED();
651 if (clist == NULL || oidp == NULL)
653 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
655 TAILQ_INSERT_HEAD(clist, e, link);
659 /* Find an entry in the context */
660 struct sysctl_ctx_entry *
661 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
663 struct sysctl_ctx_entry *e;
665 SYSCTL_ASSERT_WLOCKED();
666 if (clist == NULL || oidp == NULL)
668 TAILQ_FOREACH(e, clist, link) {
676 * Delete an entry from the context.
677 * NOTE: this function doesn't free oidp! You have to remove it
678 * with sysctl_remove_oid().
681 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
683 struct sysctl_ctx_entry *e;
685 if (clist == NULL || oidp == NULL)
688 e = sysctl_ctx_entry_find(clist, oidp);
690 TAILQ_REMOVE(clist, e, link);
692 free(e, M_SYSCTLOID);
701 * Remove dynamically created sysctl trees.
702 * oidp - top of the tree to be removed
703 * del - if 0 - just deregister, otherwise free up entries as well
704 * recurse - if != 0 traverse the subtree to be deleted
707 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
712 error = sysctl_remove_oid_locked(oidp, del, recurse);
718 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
719 int del, int recurse)
721 struct sysctl_oid *p, *tmp;
726 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
727 if (strcmp(p->oid_name, name) == 0) {
728 error = sysctl_remove_oid_locked(p, del, recurse);
739 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
741 struct sysctl_oid *p, *tmp;
744 SYSCTL_ASSERT_WLOCKED();
747 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
748 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
753 * WARNING: normal method to do this should be through
754 * sysctl_ctx_free(). Use recursing as the last resort
755 * method to purge your sysctl tree of leftovers...
756 * However, if some other code still references these nodes,
759 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
760 if (oidp->oid_refcnt == 1) {
761 SLIST_FOREACH_SAFE(p,
762 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
764 printf("Warning: failed attempt to "
765 "remove oid %s with child %s\n",
766 oidp->oid_name, p->oid_name);
769 error = sysctl_remove_oid_locked(p, del,
776 if (oidp->oid_refcnt > 1 ) {
779 if (oidp->oid_refcnt == 0) {
780 printf("Warning: bad oid_refcnt=%u (%s)!\n",
781 oidp->oid_refcnt, oidp->oid_name);
784 sysctl_unregister_oid(oidp);
787 * Wait for all threads running the handler to drain.
788 * This preserves the previous behavior when the
789 * sysctl lock was held across a handler invocation,
790 * and is necessary for module unload correctness.
792 while (oidp->oid_running > 0) {
793 oidp->oid_kind |= CTLFLAG_DYING;
794 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
797 free(__DECONST(char *, oidp->oid_descr),
800 free(__DECONST(char *, oidp->oid_label),
802 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
803 free(oidp, M_SYSCTLOID);
809 * Create new sysctls at run time.
810 * clist may point to a valid context initialized with sysctl_ctx_init().
813 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
814 int number, const char *name, int kind, void *arg1, intmax_t arg2,
815 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
818 struct sysctl_oid *oidp;
820 /* You have to hook up somewhere.. */
823 /* Check if the node already exists, otherwise create it */
825 oidp = sysctl_find_oidname(name, parent);
827 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
829 /* Update the context */
831 sysctl_ctx_entry_add(clist, oidp);
835 sysctl_warn_reuse(__func__, oidp);
840 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
841 oidp->oid_parent = parent;
842 SLIST_INIT(&oidp->oid_children);
843 oidp->oid_number = number;
844 oidp->oid_refcnt = 1;
845 oidp->oid_name = strdup(name, M_SYSCTLOID);
846 oidp->oid_handler = handler;
847 oidp->oid_kind = CTLFLAG_DYN | kind;
848 oidp->oid_arg1 = arg1;
849 oidp->oid_arg2 = arg2;
852 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
854 oidp->oid_label = strdup(label, M_SYSCTLOID);
855 /* Update the context, if used */
857 sysctl_ctx_entry_add(clist, oidp);
858 /* Register this oid */
859 sysctl_register_oid(oidp);
865 * Rename an existing oid.
868 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
873 newname = strdup(name, M_SYSCTLOID);
875 oldname = __DECONST(char *, oidp->oid_name);
876 oidp->oid_name = newname;
878 free(oldname, M_SYSCTLOID);
882 * Reparent an existing oid.
885 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
887 struct sysctl_oid *oidp;
890 if (oid->oid_parent == parent) {
894 oidp = sysctl_find_oidname(oid->oid_name, parent);
899 sysctl_unregister_oid(oid);
900 oid->oid_parent = parent;
901 oid->oid_number = OID_AUTO;
902 sysctl_register_oid(oid);
908 * Register the kernel's oids on startup.
910 SET_DECLARE(sysctl_set, struct sysctl_oid);
913 sysctl_register_all(void *arg)
915 struct sysctl_oid **oidp;
917 sx_init(&sysctlmemlock, "sysctl mem");
920 SET_FOREACH(oidp, sysctl_set)
921 sysctl_register_oid(*oidp);
924 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
929 * These functions implement a presently undocumented interface
930 * used by the sysctl program to walk the tree, and get the type
931 * so it can print the value.
932 * This interface is under work and consideration, and should probably
933 * be killed with a big axe by the first person who can find the time.
934 * (be aware though, that the proper interface isn't as obvious as it
935 * may seem, there are various conflicting requirements.
937 * {0,0} printf the entire MIB-tree.
938 * {0,1,...} return the name of the "..." OID.
939 * {0,2,...} return the next OID.
940 * {0,3} return the OID of the name in "new"
941 * {0,4,...} return the kind & format info for the "..." OID.
942 * {0,5,...} return the description of the "..." OID.
943 * {0,6,...} return the aggregation label of the "..." OID.
948 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
951 struct sysctl_oid *oidp;
953 SYSCTL_ASSERT_LOCKED();
954 SLIST_FOREACH(oidp, l, oid_link) {
959 printf("%d %s ", oidp->oid_number, oidp->oid_name);
962 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
963 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
965 if (oidp->oid_handler)
968 switch (oidp->oid_kind & CTLTYPE) {
971 if (!oidp->oid_handler) {
972 sysctl_sysctl_debug_dump_node(
973 SYSCTL_CHILDREN(oidp), i + 2);
976 case CTLTYPE_INT: printf(" Int\n"); break;
977 case CTLTYPE_UINT: printf(" u_int\n"); break;
978 case CTLTYPE_LONG: printf(" Long\n"); break;
979 case CTLTYPE_ULONG: printf(" u_long\n"); break;
980 case CTLTYPE_STRING: printf(" String\n"); break;
981 case CTLTYPE_S8: printf(" int8_t\n"); break;
982 case CTLTYPE_S16: printf(" int16_t\n"); break;
983 case CTLTYPE_S32: printf(" int32_t\n"); break;
984 case CTLTYPE_S64: printf(" int64_t\n"); break;
985 case CTLTYPE_U8: printf(" uint8_t\n"); break;
986 case CTLTYPE_U16: printf(" uint16_t\n"); break;
987 case CTLTYPE_U32: printf(" uint32_t\n"); break;
988 case CTLTYPE_U64: printf(" uint64_t\n"); break;
989 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
990 default: printf("\n");
997 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
999 struct rm_priotracker tracker;
1002 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1005 SYSCTL_RLOCK(&tracker);
1006 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1007 SYSCTL_RUNLOCK(&tracker);
1011 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
1012 0, 0, sysctl_sysctl_debug, "-", "");
1016 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1018 int *name = (int *) arg1;
1019 u_int namelen = arg2;
1021 struct sysctl_oid *oid;
1022 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1023 struct rm_priotracker tracker;
1026 SYSCTL_RLOCK(&tracker);
1029 snprintf(buf,sizeof(buf),"%d",*name);
1031 error = SYSCTL_OUT(req, ".", 1);
1033 error = SYSCTL_OUT(req, buf, strlen(buf));
1041 SLIST_FOREACH(oid, lsp, oid_link) {
1042 if (oid->oid_number != *name)
1046 error = SYSCTL_OUT(req, ".", 1);
1048 error = SYSCTL_OUT(req, oid->oid_name,
1049 strlen(oid->oid_name));
1056 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1059 if (oid->oid_handler)
1062 lsp2 = SYSCTL_CHILDREN(oid);
1067 error = SYSCTL_OUT(req, "", 1);
1069 SYSCTL_RUNLOCK(&tracker);
1074 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1077 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1078 sysctl_sysctl_name, "");
1081 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1082 int *next, int *len, int level, struct sysctl_oid **oidpp)
1084 struct sysctl_oid *oidp;
1086 SYSCTL_ASSERT_LOCKED();
1088 SLIST_FOREACH(oidp, lsp, oid_link) {
1089 *next = oidp->oid_number;
1092 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1096 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1098 if (oidp->oid_handler)
1099 /* We really should call the handler here...*/
1101 lsp = SYSCTL_CHILDREN(oidp);
1102 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1103 len, level+1, oidpp))
1108 if (oidp->oid_number < *name)
1111 if (oidp->oid_number > *name) {
1112 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1114 if (oidp->oid_handler)
1116 lsp = SYSCTL_CHILDREN(oidp);
1117 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1118 next+1, len, level+1, oidpp))
1122 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1125 if (oidp->oid_handler)
1128 lsp = SYSCTL_CHILDREN(oidp);
1129 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1130 len, level+1, oidpp))
1141 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1143 int *name = (int *) arg1;
1144 u_int namelen = arg2;
1146 struct sysctl_oid *oid;
1147 struct sysctl_oid_list *lsp = &sysctl__children;
1148 struct rm_priotracker tracker;
1149 int newoid[CTL_MAXNAME];
1151 SYSCTL_RLOCK(&tracker);
1152 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1153 SYSCTL_RUNLOCK(&tracker);
1156 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1161 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1164 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1165 sysctl_sysctl_next, "");
1168 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1170 struct sysctl_oid *oidp;
1171 struct sysctl_oid_list *lsp = &sysctl__children;
1174 SYSCTL_ASSERT_LOCKED();
1176 for (*len = 0; *len < CTL_MAXNAME;) {
1177 p = strsep(&name, ".");
1179 oidp = SLIST_FIRST(lsp);
1180 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1183 if (strcmp(p, oidp->oid_name) == 0)
1186 *oid++ = oidp->oid_number;
1189 if (name == NULL || *name == '\0') {
1195 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1198 if (oidp->oid_handler)
1201 lsp = SYSCTL_CHILDREN(oidp);
1207 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1210 int error, oid[CTL_MAXNAME], len = 0;
1211 struct sysctl_oid *op = NULL;
1212 struct rm_priotracker tracker;
1217 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1218 return (ENAMETOOLONG);
1221 if (req->newlen >= sizeof(buf))
1222 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1224 error = SYSCTL_IN(req, p, req->newlen);
1231 p [req->newlen] = '\0';
1233 SYSCTL_RLOCK(&tracker);
1234 error = name2oid(p, oid, &len, &op);
1235 SYSCTL_RUNLOCK(&tracker);
1243 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1248 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1251 SYSCTL_PROC(_sysctl, 3, name2oid,
1252 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1253 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1256 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1258 struct sysctl_oid *oid;
1259 struct rm_priotracker tracker;
1262 SYSCTL_RLOCK(&tracker);
1263 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1267 if (oid->oid_fmt == NULL) {
1271 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1274 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1276 SYSCTL_RUNLOCK(&tracker);
1281 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1282 sysctl_sysctl_oidfmt, "");
1285 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1287 struct sysctl_oid *oid;
1288 struct rm_priotracker tracker;
1291 SYSCTL_RLOCK(&tracker);
1292 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1296 if (oid->oid_descr == NULL) {
1300 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1302 SYSCTL_RUNLOCK(&tracker);
1306 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1307 sysctl_sysctl_oiddescr, "");
1310 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1312 struct sysctl_oid *oid;
1313 struct rm_priotracker tracker;
1316 SYSCTL_RLOCK(&tracker);
1317 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1321 if (oid->oid_label == NULL) {
1325 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1327 SYSCTL_RUNLOCK(&tracker);
1331 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1332 CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1335 * Default "handler" functions.
1341 * a variable: point arg1 at it.
1342 * a constant: pass it in arg2.
1346 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1352 * Attempt to get a coherent snapshot by making a copy of the data.
1355 temp = *(bool *)arg1 ? 1 : 0;
1357 temp = arg2 ? 1 : 0;
1359 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1360 if (error || !req->newptr)
1366 error = SYSCTL_IN(req, &temp, sizeof(temp));
1368 *(bool *)arg1 = temp ? 1 : 0;
1374 * Handle an int8_t, signed or unsigned.
1376 * a variable: point arg1 at it.
1377 * a constant: pass it in arg2.
1381 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1387 * Attempt to get a coherent snapshot by making a copy of the data.
1390 tmpout = *(int8_t *)arg1;
1393 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1395 if (error || !req->newptr)
1401 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1406 * Handle an int16_t, signed or unsigned.
1408 * a variable: point arg1 at it.
1409 * a constant: pass it in arg2.
1413 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1419 * Attempt to get a coherent snapshot by making a copy of the data.
1422 tmpout = *(int16_t *)arg1;
1425 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1427 if (error || !req->newptr)
1433 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1438 * Handle an int32_t, signed or unsigned.
1440 * a variable: point arg1 at it.
1441 * a constant: pass it in arg2.
1445 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1451 * Attempt to get a coherent snapshot by making a copy of the data.
1454 tmpout = *(int32_t *)arg1;
1457 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1459 if (error || !req->newptr)
1465 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1470 * Handle an int, signed or unsigned.
1472 * a variable: point arg1 at it.
1473 * a constant: pass it in arg2.
1477 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1479 int tmpout, error = 0;
1482 * Attempt to get a coherent snapshot by making a copy of the data.
1485 tmpout = *(int *)arg1;
1488 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1490 if (error || !req->newptr)
1496 error = SYSCTL_IN(req, arg1, sizeof(int));
1501 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1502 * Note: this is used by TCP.
1506 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1511 s = (int)((int64_t)tt * 1000 / hz);
1513 error = sysctl_handle_int(oidp, &s, 0, req);
1514 if (error || !req->newptr)
1517 tt = (int)((int64_t)s * hz / 1000);
1527 * Handle a long, signed or unsigned.
1529 * a variable: point arg1 at it.
1530 * a constant: pass it in arg2.
1534 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1543 * Attempt to get a coherent snapshot by making a copy of the data.
1546 tmplong = *(long *)arg1;
1550 if (req->flags & SCTL_MASK32) {
1552 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1555 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1557 if (error || !req->newptr)
1563 else if (req->flags & SCTL_MASK32) {
1564 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1565 *(long *)arg1 = (long)tmpint;
1569 error = SYSCTL_IN(req, arg1, sizeof(long));
1574 * Handle a 64 bit int, signed or unsigned.
1576 * a variable: point arg1 at it.
1577 * a constant: pass it in arg2.
1580 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1586 * Attempt to get a coherent snapshot by making a copy of the data.
1589 tmpout = *(uint64_t *)arg1;
1592 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1594 if (error || !req->newptr)
1600 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1605 * Handle our generic '\0' terminated 'C' string.
1607 * a variable string: point arg1 at it, arg2 is max length.
1608 * a constant string: point arg1 at it, arg2 is zero.
1612 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1615 int error = 0, ro_string = 0;
1618 * A zero-length buffer indicates a fixed size read-only
1622 arg2 = strlen((char *)arg1) + 1;
1626 if (req->oldptr != NULL) {
1632 /* try to make a coherent snapshot of the string */
1633 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1634 memcpy(tmparg, arg1, arg2);
1637 outlen = strnlen(tmparg, arg2 - 1) + 1;
1638 error = SYSCTL_OUT(req, tmparg, outlen);
1641 free(tmparg, M_SYSCTLTMP);
1643 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1644 error = SYSCTL_OUT(req, NULL, outlen);
1646 if (error || !req->newptr)
1649 if ((req->newlen - req->newidx) >= arg2) {
1652 arg2 = (req->newlen - req->newidx);
1653 error = SYSCTL_IN(req, arg1, arg2);
1654 ((char *)arg1)[arg2] = '\0';
1660 * Handle any kind of opaque data.
1661 * arg1 points to it, arg2 is the size.
1665 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1669 struct sysctl_req req2;
1672 * Attempt to get a coherent snapshot, by using the thread
1673 * pre-emption counter updated from within mi_switch() to
1674 * determine if we were pre-empted during a bcopy() or
1675 * copyout(). Make 3 attempts at doing this before giving up.
1676 * If we encounter an error, stop immediately.
1681 generation = curthread->td_generation;
1682 error = SYSCTL_OUT(req, arg1, arg2);
1686 if (generation != curthread->td_generation && tries < 3) {
1691 error = SYSCTL_IN(req, arg1, arg2);
1697 * Convert seconds to a struct timeval. Intended for use with
1698 * intervals and thus does not permit negative seconds.
1701 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1709 error = sysctl_handle_int(oidp, &secs, 0, req);
1710 if (error || req->newptr == NULL)
1721 * Transfer functions to/from kernel space.
1722 * XXX: rather untested at this point
1725 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1731 if (req->oldlen <= req->oldidx)
1734 if (i > req->oldlen - req->oldidx)
1735 i = req->oldlen - req->oldidx;
1737 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1740 if (req->oldptr && i != l)
1746 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1750 if (req->newlen - req->newidx < l)
1752 bcopy((char *)req->newptr + req->newidx, p, l);
1758 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1759 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1762 struct sysctl_req req;
1764 bzero(&req, sizeof req);
1770 req.oldlen = *oldlenp;
1772 req.validlen = req.oldlen;
1779 req.newlen = newlen;
1783 req.oldfunc = sysctl_old_kernel;
1784 req.newfunc = sysctl_new_kernel;
1785 req.lock = REQ_UNWIRED;
1787 error = sysctl_root(0, name, namelen, &req);
1789 if (req.lock == REQ_WIRED && req.validlen > 0)
1790 vsunlock(req.oldptr, req.validlen);
1792 if (error && error != ENOMEM)
1796 if (req.oldptr && req.oldidx > req.validlen)
1797 *retval = req.validlen;
1799 *retval = req.oldidx;
1805 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1806 void *new, size_t newlen, size_t *retval, int flags)
1808 int oid[CTL_MAXNAME];
1809 size_t oidlen, plen;
1812 oid[0] = 0; /* sysctl internal magic */
1813 oid[1] = 3; /* name2oid */
1814 oidlen = sizeof(oid);
1816 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1817 (void *)name, strlen(name), &plen, flags);
1821 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1822 new, newlen, retval, flags);
1827 * Transfer function to/from user space.
1830 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1832 size_t i, len, origidx;
1835 origidx = req->oldidx;
1837 if (req->oldptr == NULL)
1840 * If we have not wired the user supplied buffer and we are currently
1841 * holding locks, drop a witness warning, as it's possible that
1842 * write operations to the user page can sleep.
1844 if (req->lock != REQ_WIRED)
1845 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1846 "sysctl_old_user()");
1848 len = req->validlen;
1852 if (i > len - origidx)
1854 if (req->lock == REQ_WIRED) {
1855 error = copyout_nofault(p, (char *)req->oldptr +
1858 error = copyout(p, (char *)req->oldptr + origidx, i);
1868 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1874 if (req->newlen - req->newidx < l)
1876 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1877 "sysctl_new_user()");
1878 error = copyin((char *)req->newptr + req->newidx, p, l);
1884 * Wire the user space destination buffer. If set to a value greater than
1885 * zero, the len parameter limits the maximum amount of wired memory.
1888 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1893 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1895 if (req->lock != REQ_WIRED && req->oldptr &&
1896 req->oldfunc == sysctl_old_user) {
1897 if (wiredlen != 0) {
1898 ret = vslock(req->oldptr, wiredlen);
1905 req->lock = REQ_WIRED;
1906 req->validlen = wiredlen;
1912 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1913 int *nindx, struct sysctl_req *req)
1915 struct sysctl_oid_list *lsp;
1916 struct sysctl_oid *oid;
1919 SYSCTL_ASSERT_LOCKED();
1920 lsp = &sysctl__children;
1922 while (indx < CTL_MAXNAME) {
1923 SLIST_FOREACH(oid, lsp, oid_link) {
1924 if (oid->oid_number == name[indx])
1931 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1932 if (oid->oid_handler != NULL || indx == namelen) {
1936 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1937 ("%s found DYING node %p", __func__, oid));
1940 lsp = SYSCTL_CHILDREN(oid);
1941 } else if (indx == namelen) {
1942 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1947 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1948 ("%s found DYING node %p", __func__, oid));
1958 * Traverse our tree, and find the right node, execute whatever it points
1959 * to, and return the resulting error code.
1963 sysctl_root(SYSCTL_HANDLER_ARGS)
1965 struct sysctl_oid *oid;
1966 struct rm_priotracker tracker;
1967 int error, indx, lvl;
1969 SYSCTL_RLOCK(&tracker);
1971 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1975 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1977 * You can't call a sysctl when it's a node, but has
1978 * no handler. Inform the user that it's a node.
1979 * The indx may or may not be the same as namelen.
1981 if (oid->oid_handler == NULL) {
1987 /* Is this sysctl writable? */
1988 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1993 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1995 #ifdef CAPABILITY_MODE
1997 * If the process is in capability mode, then don't permit reading or
1998 * writing unless specifically granted for the node.
2000 if (IN_CAPABILITY_MODE(req->td)) {
2001 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2002 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2009 /* Is this sysctl sensitive to securelevels? */
2010 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2011 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2012 error = securelevel_gt(req->td->td_ucred, lvl);
2017 /* Is this sysctl writable by only privileged users? */
2018 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2021 if (oid->oid_kind & CTLFLAG_PRISON)
2022 priv = PRIV_SYSCTL_WRITEJAIL;
2024 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2025 prison_owns_vnet(req->td->td_ucred))
2026 priv = PRIV_SYSCTL_WRITEJAIL;
2029 priv = PRIV_SYSCTL_WRITE;
2030 error = priv_check(req->td, priv);
2035 if (!oid->oid_handler) {
2040 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2041 arg1 = (int *)arg1 + indx;
2044 arg1 = oid->oid_arg1;
2045 arg2 = oid->oid_arg2;
2048 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2054 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2055 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2057 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2060 SYSCTL_RUNLOCK(&tracker);
2064 #ifndef _SYS_SYSPROTO_H_
2065 struct sysctl_args {
2075 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2077 int error, i, name[CTL_MAXNAME];
2080 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2083 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2087 error = userland_sysctl(td, name, uap->namelen,
2088 uap->old, uap->oldlenp, 0,
2089 uap->new, uap->newlen, &j, 0);
2090 if (error && error != ENOMEM)
2093 i = copyout(&j, uap->oldlenp, sizeof(j));
2101 * This is used from various compatibility syscalls too. That's why name
2102 * must be in kernel space.
2105 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2106 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2109 int error = 0, memlocked;
2110 struct sysctl_req req;
2112 bzero(&req, sizeof req);
2119 req.oldlen = *oldlenp;
2121 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2126 req.validlen = req.oldlen;
2130 req.newlen = newlen;
2134 req.oldfunc = sysctl_old_user;
2135 req.newfunc = sysctl_new_user;
2136 req.lock = REQ_UNWIRED;
2139 if (KTRPOINT(curthread, KTR_SYSCTL))
2140 ktrsysctl(name, namelen);
2143 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2145 sx_xlock(&sysctlmemlock);
2147 CURVNET_SET(TD_TO_VNET(td));
2152 error = sysctl_root(0, name, namelen, &req);
2153 if (error != EAGAIN)
2155 kern_yield(PRI_USER);
2160 if (req.lock == REQ_WIRED && req.validlen > 0)
2161 vsunlock(req.oldptr, req.validlen);
2163 sx_xunlock(&sysctlmemlock);
2165 if (error && error != ENOMEM)
2169 if (req.oldptr && req.oldidx > req.validlen)
2170 *retval = req.validlen;
2172 *retval = req.oldidx;
2178 * Drain into a sysctl struct. The user buffer should be wired if a page
2179 * fault would cause issue.
2182 sbuf_sysctl_drain(void *arg, const char *data, int len)
2184 struct sysctl_req *req = arg;
2187 error = SYSCTL_OUT(req, data, len);
2188 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2189 return (error == 0 ? len : -error);
2193 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2194 struct sysctl_req *req)
2197 /* Supply a default buffer size if none given. */
2198 if (buf == NULL && length == 0)
2200 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2201 sbuf_set_drain(s, sbuf_sysctl_drain, req);