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 * Locate the path to a given oid. Returns the length of the resulting path,
327 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
328 * elements and be NULL initialized.
331 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
335 SYSCTL_ASSERT_LOCKED();
337 while (indx < CTL_MAXNAME && indx >= 0) {
338 if (nodes[indx] == NULL && indx == 0)
339 nodes[indx] = SLIST_FIRST(&sysctl__children);
340 else if (nodes[indx] == NULL)
341 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
343 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
345 if (nodes[indx] == needle)
348 if (nodes[indx] == NULL) {
353 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
362 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
364 struct sysctl_oid *nodes[CTL_MAXNAME];
369 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
370 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
372 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
374 memset(nodes, 0, sizeof(nodes));
375 rc = sysctl_search_oid(nodes, leaf);
377 for (i = 0; i < rc; i++)
378 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
381 sbuf_printf(&sb, "%s", leaf->oid_name);
383 sbuf_printf(&sb, ")!\n");
385 (void)sbuf_finish(&sb);
390 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
392 struct rm_priotracker tracker;
394 SYSCTL_RLOCK(&tracker);
395 sysctl_warn_reuse(__func__, oidp);
396 SYSCTL_RUNLOCK(&tracker);
399 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
400 0, 0, sysctl_reuse_test, "-", "");
404 sysctl_register_oid(struct sysctl_oid *oidp)
406 struct sysctl_oid_list *parent = oidp->oid_parent;
407 struct sysctl_oid *p;
408 struct sysctl_oid *q;
413 * First check if another oid with the same name already
414 * exists in the parent's list.
416 SYSCTL_ASSERT_WLOCKED();
417 p = sysctl_find_oidname(oidp->oid_name, parent);
419 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
423 sysctl_warn_reuse(__func__, p);
427 /* get current OID number */
428 oid_number = oidp->oid_number;
431 #error "OID_AUTO is expected to be a negative value"
434 * Any negative OID number qualifies as OID_AUTO. Valid OID
435 * numbers should always be positive.
437 * NOTE: DO NOT change the starting value here, change it in
438 * <sys/sysctl.h>, and make sure it is at least 256 to
439 * accommodate e.g. net.inet.raw as a static sysctl node.
441 if (oid_number < 0) {
445 * By decrementing the next OID number we spend less
446 * time inserting the OIDs into a sorted list.
448 if (--newoid < CTL_AUTO_START)
455 * Insert the OID into the parent's list sorted by OID number.
459 SLIST_FOREACH(p, parent, oid_link) {
460 /* check if the current OID number is in use */
461 if (oid_number == p->oid_number) {
462 /* get the next valid OID number */
463 if (oid_number < CTL_AUTO_START ||
464 oid_number == 0x7fffffff) {
465 /* wraparound - restart */
466 oid_number = CTL_AUTO_START;
467 /* don't loop forever */
469 panic("sysctl: Out of OID numbers\n");
474 } else if (oid_number < p->oid_number)
478 /* check for non-auto OID number collision */
479 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
480 oid_number >= CTL_AUTO_START) {
481 printf("sysctl: OID number(%d) is already in use for '%s'\n",
482 oidp->oid_number, oidp->oid_name);
484 /* update the OID number, if any */
485 oidp->oid_number = oid_number;
487 SLIST_INSERT_AFTER(q, oidp, oid_link);
489 SLIST_INSERT_HEAD(parent, oidp, oid_link);
491 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
493 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
495 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
496 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
497 /* only fetch value once */
498 oidp->oid_kind |= CTLFLAG_NOFETCH;
499 /* try to fetch value from kernel environment */
500 sysctl_load_tunable_by_oid_locked(oidp);
505 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
509 * Mark the leaf as dormant if it's not to be immediately enabled.
510 * We do not disable nodes as they can be shared between modules
511 * and it is always safe to access a node.
513 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
514 ("internal flag is set in oid_kind"));
515 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
516 oidp->oid_kind |= CTLFLAG_DORMANT;
517 sysctl_register_oid(oidp);
521 sysctl_enable_oid(struct sysctl_oid *oidp)
524 SYSCTL_ASSERT_WLOCKED();
525 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
526 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
527 ("sysctl node is marked as dormant"));
530 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
531 ("enabling already enabled sysctl oid"));
532 oidp->oid_kind &= ~CTLFLAG_DORMANT;
536 sysctl_unregister_oid(struct sysctl_oid *oidp)
538 struct sysctl_oid *p;
541 SYSCTL_ASSERT_WLOCKED();
542 if (oidp->oid_number == OID_AUTO) {
546 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
548 SLIST_REMOVE(oidp->oid_parent, oidp,
549 sysctl_oid, oid_link);
557 * This can happen when a module fails to register and is
558 * being unloaded afterwards. It should not be a panic()
562 printf("%s: failed(%d) to unregister sysctl(%s)\n",
563 __func__, error, oidp->oid_name);
567 /* Initialize a new context to keep track of dynamically added sysctls. */
569 sysctl_ctx_init(struct sysctl_ctx_list *c)
577 * No locking here, the caller is responsible for not adding
578 * new nodes to a context until after this function has
585 /* Free the context, and destroy all dynamic oids registered in this context */
587 sysctl_ctx_free(struct sysctl_ctx_list *clist)
589 struct sysctl_ctx_entry *e, *e1;
594 * First perform a "dry run" to check if it's ok to remove oids.
596 * XXX This algorithm is a hack. But I don't know any
597 * XXX better solution for now...
600 TAILQ_FOREACH(e, clist, link) {
601 error = sysctl_remove_oid_locked(e->entry, 0, 0);
606 * Restore deregistered entries, either from the end,
607 * or from the place where error occurred.
608 * e contains the entry that was not unregistered
611 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
613 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
615 sysctl_register_oid(e1->entry);
616 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
622 /* Now really delete the entries */
623 e = TAILQ_FIRST(clist);
625 e1 = TAILQ_NEXT(e, link);
626 error = sysctl_remove_oid_locked(e->entry, 1, 0);
628 panic("sysctl_remove_oid: corrupt tree, entry: %s",
630 free(e, M_SYSCTLOID);
637 /* Add an entry to the context */
638 struct sysctl_ctx_entry *
639 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
641 struct sysctl_ctx_entry *e;
643 SYSCTL_ASSERT_WLOCKED();
644 if (clist == NULL || oidp == NULL)
646 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
648 TAILQ_INSERT_HEAD(clist, e, link);
652 /* Find an entry in the context */
653 struct sysctl_ctx_entry *
654 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
656 struct sysctl_ctx_entry *e;
658 SYSCTL_ASSERT_WLOCKED();
659 if (clist == NULL || oidp == NULL)
661 TAILQ_FOREACH(e, clist, link) {
669 * Delete an entry from the context.
670 * NOTE: this function doesn't free oidp! You have to remove it
671 * with sysctl_remove_oid().
674 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
676 struct sysctl_ctx_entry *e;
678 if (clist == NULL || oidp == NULL)
681 e = sysctl_ctx_entry_find(clist, oidp);
683 TAILQ_REMOVE(clist, e, link);
685 free(e, M_SYSCTLOID);
694 * Remove dynamically created sysctl trees.
695 * oidp - top of the tree to be removed
696 * del - if 0 - just deregister, otherwise free up entries as well
697 * recurse - if != 0 traverse the subtree to be deleted
700 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
705 error = sysctl_remove_oid_locked(oidp, del, recurse);
711 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
712 int del, int recurse)
714 struct sysctl_oid *p, *tmp;
719 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
720 if (strcmp(p->oid_name, name) == 0) {
721 error = sysctl_remove_oid_locked(p, del, recurse);
732 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
734 struct sysctl_oid *p, *tmp;
737 SYSCTL_ASSERT_WLOCKED();
740 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
741 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
746 * WARNING: normal method to do this should be through
747 * sysctl_ctx_free(). Use recursing as the last resort
748 * method to purge your sysctl tree of leftovers...
749 * However, if some other code still references these nodes,
752 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
753 if (oidp->oid_refcnt == 1) {
754 SLIST_FOREACH_SAFE(p,
755 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
757 printf("Warning: failed attempt to "
758 "remove oid %s with child %s\n",
759 oidp->oid_name, p->oid_name);
762 error = sysctl_remove_oid_locked(p, del,
769 if (oidp->oid_refcnt > 1 ) {
772 if (oidp->oid_refcnt == 0) {
773 printf("Warning: bad oid_refcnt=%u (%s)!\n",
774 oidp->oid_refcnt, oidp->oid_name);
777 sysctl_unregister_oid(oidp);
780 * Wait for all threads running the handler to drain.
781 * This preserves the previous behavior when the
782 * sysctl lock was held across a handler invocation,
783 * and is necessary for module unload correctness.
785 while (oidp->oid_running > 0) {
786 oidp->oid_kind |= CTLFLAG_DYING;
787 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
790 free(__DECONST(char *, oidp->oid_descr),
793 free(__DECONST(char *, oidp->oid_label),
795 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
796 free(oidp, M_SYSCTLOID);
802 * Create new sysctls at run time.
803 * clist may point to a valid context initialized with sysctl_ctx_init().
806 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
807 int number, const char *name, int kind, void *arg1, intmax_t arg2,
808 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
811 struct sysctl_oid *oidp;
813 /* You have to hook up somewhere.. */
816 /* Check if the node already exists, otherwise create it */
818 oidp = sysctl_find_oidname(name, parent);
820 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
822 /* Update the context */
824 sysctl_ctx_entry_add(clist, oidp);
828 sysctl_warn_reuse(__func__, oidp);
833 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
834 oidp->oid_parent = parent;
835 SLIST_INIT(&oidp->oid_children);
836 oidp->oid_number = number;
837 oidp->oid_refcnt = 1;
838 oidp->oid_name = strdup(name, M_SYSCTLOID);
839 oidp->oid_handler = handler;
840 oidp->oid_kind = CTLFLAG_DYN | kind;
841 oidp->oid_arg1 = arg1;
842 oidp->oid_arg2 = arg2;
845 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
847 oidp->oid_label = strdup(label, M_SYSCTLOID);
848 /* Update the context, if used */
850 sysctl_ctx_entry_add(clist, oidp);
851 /* Register this oid */
852 sysctl_register_oid(oidp);
858 * Rename an existing oid.
861 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
866 newname = strdup(name, M_SYSCTLOID);
868 oldname = __DECONST(char *, oidp->oid_name);
869 oidp->oid_name = newname;
871 free(oldname, M_SYSCTLOID);
875 * Reparent an existing oid.
878 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
880 struct sysctl_oid *oidp;
883 if (oid->oid_parent == parent) {
887 oidp = sysctl_find_oidname(oid->oid_name, parent);
892 sysctl_unregister_oid(oid);
893 oid->oid_parent = parent;
894 oid->oid_number = OID_AUTO;
895 sysctl_register_oid(oid);
901 * Register the kernel's oids on startup.
903 SET_DECLARE(sysctl_set, struct sysctl_oid);
906 sysctl_register_all(void *arg)
908 struct sysctl_oid **oidp;
910 sx_init(&sysctlmemlock, "sysctl mem");
913 SET_FOREACH(oidp, sysctl_set)
914 sysctl_register_oid(*oidp);
917 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
922 * These functions implement a presently undocumented interface
923 * used by the sysctl program to walk the tree, and get the type
924 * so it can print the value.
925 * This interface is under work and consideration, and should probably
926 * be killed with a big axe by the first person who can find the time.
927 * (be aware though, that the proper interface isn't as obvious as it
928 * may seem, there are various conflicting requirements.
930 * {0,0} printf the entire MIB-tree.
931 * {0,1,...} return the name of the "..." OID.
932 * {0,2,...} return the next OID.
933 * {0,3} return the OID of the name in "new"
934 * {0,4,...} return the kind & format info for the "..." OID.
935 * {0,5,...} return the description of the "..." OID.
936 * {0,6,...} return the aggregation label of the "..." OID.
941 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
944 struct sysctl_oid *oidp;
946 SYSCTL_ASSERT_LOCKED();
947 SLIST_FOREACH(oidp, l, oid_link) {
952 printf("%d %s ", oidp->oid_number, oidp->oid_name);
955 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
956 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
958 if (oidp->oid_handler)
961 switch (oidp->oid_kind & CTLTYPE) {
964 if (!oidp->oid_handler) {
965 sysctl_sysctl_debug_dump_node(
966 SYSCTL_CHILDREN(oidp), i + 2);
969 case CTLTYPE_INT: printf(" Int\n"); break;
970 case CTLTYPE_UINT: printf(" u_int\n"); break;
971 case CTLTYPE_LONG: printf(" Long\n"); break;
972 case CTLTYPE_ULONG: printf(" u_long\n"); break;
973 case CTLTYPE_STRING: printf(" String\n"); break;
974 case CTLTYPE_S8: printf(" int8_t\n"); break;
975 case CTLTYPE_S16: printf(" int16_t\n"); break;
976 case CTLTYPE_S32: printf(" int32_t\n"); break;
977 case CTLTYPE_S64: printf(" int64_t\n"); break;
978 case CTLTYPE_U8: printf(" uint8_t\n"); break;
979 case CTLTYPE_U16: printf(" uint16_t\n"); break;
980 case CTLTYPE_U32: printf(" uint32_t\n"); break;
981 case CTLTYPE_U64: printf(" uint64_t\n"); break;
982 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
983 default: printf("\n");
990 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
992 struct rm_priotracker tracker;
995 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
998 SYSCTL_RLOCK(&tracker);
999 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1000 SYSCTL_RUNLOCK(&tracker);
1004 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
1005 0, 0, sysctl_sysctl_debug, "-", "");
1009 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1011 int *name = (int *) arg1;
1012 u_int namelen = arg2;
1014 struct sysctl_oid *oid;
1015 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1016 struct rm_priotracker tracker;
1019 SYSCTL_RLOCK(&tracker);
1022 snprintf(buf,sizeof(buf),"%d",*name);
1024 error = SYSCTL_OUT(req, ".", 1);
1026 error = SYSCTL_OUT(req, buf, strlen(buf));
1034 SLIST_FOREACH(oid, lsp, oid_link) {
1035 if (oid->oid_number != *name)
1039 error = SYSCTL_OUT(req, ".", 1);
1041 error = SYSCTL_OUT(req, oid->oid_name,
1042 strlen(oid->oid_name));
1049 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1052 if (oid->oid_handler)
1055 lsp2 = SYSCTL_CHILDREN(oid);
1060 error = SYSCTL_OUT(req, "", 1);
1062 SYSCTL_RUNLOCK(&tracker);
1067 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1070 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1071 sysctl_sysctl_name, "");
1074 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1075 int *next, int *len, int level, struct sysctl_oid **oidpp)
1077 struct sysctl_oid *oidp;
1079 SYSCTL_ASSERT_LOCKED();
1081 SLIST_FOREACH(oidp, lsp, oid_link) {
1082 *next = oidp->oid_number;
1085 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1089 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1091 if (oidp->oid_handler)
1092 /* We really should call the handler here...*/
1094 lsp = SYSCTL_CHILDREN(oidp);
1095 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1096 len, level+1, oidpp))
1101 if (oidp->oid_number < *name)
1104 if (oidp->oid_number > *name) {
1105 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1107 if (oidp->oid_handler)
1109 lsp = SYSCTL_CHILDREN(oidp);
1110 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1111 next+1, len, level+1, oidpp))
1115 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1118 if (oidp->oid_handler)
1121 lsp = SYSCTL_CHILDREN(oidp);
1122 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1123 len, level+1, oidpp))
1134 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1136 int *name = (int *) arg1;
1137 u_int namelen = arg2;
1139 struct sysctl_oid *oid;
1140 struct sysctl_oid_list *lsp = &sysctl__children;
1141 struct rm_priotracker tracker;
1142 int newoid[CTL_MAXNAME];
1144 SYSCTL_RLOCK(&tracker);
1145 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1146 SYSCTL_RUNLOCK(&tracker);
1149 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1154 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1157 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1158 sysctl_sysctl_next, "");
1161 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1163 struct sysctl_oid *oidp;
1164 struct sysctl_oid_list *lsp = &sysctl__children;
1167 SYSCTL_ASSERT_LOCKED();
1169 for (*len = 0; *len < CTL_MAXNAME;) {
1170 p = strsep(&name, ".");
1172 oidp = SLIST_FIRST(lsp);
1173 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1176 if (strcmp(p, oidp->oid_name) == 0)
1179 *oid++ = oidp->oid_number;
1182 if (name == NULL || *name == '\0') {
1188 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1191 if (oidp->oid_handler)
1194 lsp = SYSCTL_CHILDREN(oidp);
1200 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1203 int error, oid[CTL_MAXNAME], len = 0;
1204 struct sysctl_oid *op = NULL;
1205 struct rm_priotracker tracker;
1210 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1211 return (ENAMETOOLONG);
1214 if (req->newlen >= sizeof(buf))
1215 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1217 error = SYSCTL_IN(req, p, req->newlen);
1224 p [req->newlen] = '\0';
1226 SYSCTL_RLOCK(&tracker);
1227 error = name2oid(p, oid, &len, &op);
1228 SYSCTL_RUNLOCK(&tracker);
1236 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1241 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1244 SYSCTL_PROC(_sysctl, 3, name2oid,
1245 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1246 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1249 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1251 struct sysctl_oid *oid;
1252 struct rm_priotracker tracker;
1255 SYSCTL_RLOCK(&tracker);
1256 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1260 if (oid->oid_fmt == NULL) {
1264 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1267 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1269 SYSCTL_RUNLOCK(&tracker);
1274 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1275 sysctl_sysctl_oidfmt, "");
1278 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1280 struct sysctl_oid *oid;
1281 struct rm_priotracker tracker;
1284 SYSCTL_RLOCK(&tracker);
1285 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1289 if (oid->oid_descr == NULL) {
1293 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1295 SYSCTL_RUNLOCK(&tracker);
1299 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1300 sysctl_sysctl_oiddescr, "");
1303 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1305 struct sysctl_oid *oid;
1306 struct rm_priotracker tracker;
1309 SYSCTL_RLOCK(&tracker);
1310 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1314 if (oid->oid_label == NULL) {
1318 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1320 SYSCTL_RUNLOCK(&tracker);
1324 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1325 CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1328 * Default "handler" functions.
1334 * a variable: point arg1 at it.
1335 * a constant: pass it in arg2.
1339 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1345 * Attempt to get a coherent snapshot by making a copy of the data.
1348 temp = *(bool *)arg1 ? 1 : 0;
1350 temp = arg2 ? 1 : 0;
1352 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1353 if (error || !req->newptr)
1359 error = SYSCTL_IN(req, &temp, sizeof(temp));
1361 *(bool *)arg1 = temp ? 1 : 0;
1367 * Handle an int8_t, signed or unsigned.
1369 * a variable: point arg1 at it.
1370 * a constant: pass it in arg2.
1374 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1380 * Attempt to get a coherent snapshot by making a copy of the data.
1383 tmpout = *(int8_t *)arg1;
1386 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1388 if (error || !req->newptr)
1394 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1399 * Handle an int16_t, signed or unsigned.
1401 * a variable: point arg1 at it.
1402 * a constant: pass it in arg2.
1406 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1412 * Attempt to get a coherent snapshot by making a copy of the data.
1415 tmpout = *(int16_t *)arg1;
1418 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1420 if (error || !req->newptr)
1426 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1431 * Handle an int32_t, signed or unsigned.
1433 * a variable: point arg1 at it.
1434 * a constant: pass it in arg2.
1438 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1444 * Attempt to get a coherent snapshot by making a copy of the data.
1447 tmpout = *(int32_t *)arg1;
1450 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1452 if (error || !req->newptr)
1458 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1463 * Handle an int, signed or unsigned.
1465 * a variable: point arg1 at it.
1466 * a constant: pass it in arg2.
1470 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1472 int tmpout, error = 0;
1475 * Attempt to get a coherent snapshot by making a copy of the data.
1478 tmpout = *(int *)arg1;
1481 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1483 if (error || !req->newptr)
1489 error = SYSCTL_IN(req, arg1, sizeof(int));
1494 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1495 * Note: this is used by TCP.
1499 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1504 s = (int)((int64_t)tt * 1000 / hz);
1506 error = sysctl_handle_int(oidp, &s, 0, req);
1507 if (error || !req->newptr)
1510 tt = (int)((int64_t)s * hz / 1000);
1520 * Handle a long, signed or unsigned.
1522 * a variable: point arg1 at it.
1523 * a constant: pass it in arg2.
1527 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1536 * Attempt to get a coherent snapshot by making a copy of the data.
1539 tmplong = *(long *)arg1;
1543 if (req->flags & SCTL_MASK32) {
1545 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1548 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1550 if (error || !req->newptr)
1556 else if (req->flags & SCTL_MASK32) {
1557 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1558 *(long *)arg1 = (long)tmpint;
1562 error = SYSCTL_IN(req, arg1, sizeof(long));
1567 * Handle a 64 bit int, signed or unsigned.
1569 * a variable: point arg1 at it.
1570 * a constant: pass it in arg2.
1573 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1579 * Attempt to get a coherent snapshot by making a copy of the data.
1582 tmpout = *(uint64_t *)arg1;
1585 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1587 if (error || !req->newptr)
1593 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1598 * Handle our generic '\0' terminated 'C' string.
1600 * a variable string: point arg1 at it, arg2 is max length.
1601 * a constant string: point arg1 at it, arg2 is zero.
1605 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1608 int error = 0, ro_string = 0;
1611 * A zero-length buffer indicates a fixed size read-only
1615 arg2 = strlen((char *)arg1) + 1;
1619 if (req->oldptr != NULL) {
1625 /* try to make a coherent snapshot of the string */
1626 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1627 memcpy(tmparg, arg1, arg2);
1630 outlen = strnlen(tmparg, arg2 - 1) + 1;
1631 error = SYSCTL_OUT(req, tmparg, outlen);
1634 free(tmparg, M_SYSCTLTMP);
1636 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1637 error = SYSCTL_OUT(req, NULL, outlen);
1639 if (error || !req->newptr)
1642 if ((req->newlen - req->newidx) >= arg2) {
1645 arg2 = (req->newlen - req->newidx);
1646 error = SYSCTL_IN(req, arg1, arg2);
1647 ((char *)arg1)[arg2] = '\0';
1653 * Handle any kind of opaque data.
1654 * arg1 points to it, arg2 is the size.
1658 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1662 struct sysctl_req req2;
1665 * Attempt to get a coherent snapshot, by using the thread
1666 * pre-emption counter updated from within mi_switch() to
1667 * determine if we were pre-empted during a bcopy() or
1668 * copyout(). Make 3 attempts at doing this before giving up.
1669 * If we encounter an error, stop immediately.
1674 generation = curthread->td_generation;
1675 error = SYSCTL_OUT(req, arg1, arg2);
1679 if (generation != curthread->td_generation && tries < 3) {
1684 error = SYSCTL_IN(req, arg1, arg2);
1690 * Based on on sysctl_handle_int() convert microseconds to a sbintime.
1693 sysctl_usec_to_sbintime(SYSCTL_HANDLER_ARGS)
1699 tt = *(int64_t *)arg1;
1702 error = sysctl_handle_64(oidp, &sb, 0, req);
1703 if (error || !req->newptr)
1707 *(int64_t *)arg1 = tt;
1713 * Based on on sysctl_handle_int() convert milliseconds to a sbintime.
1716 sysctl_msec_to_sbintime(SYSCTL_HANDLER_ARGS)
1722 tt = *(int64_t *)arg1;
1725 error = sysctl_handle_64(oidp, &sb, 0, req);
1726 if (error || !req->newptr)
1730 *(int64_t *)arg1 = tt;
1736 * Convert seconds to a struct timeval. Intended for use with
1737 * intervals and thus does not permit negative seconds.
1740 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
1748 error = sysctl_handle_int(oidp, &secs, 0, req);
1749 if (error || req->newptr == NULL)
1760 * Transfer functions to/from kernel space.
1761 * XXX: rather untested at this point
1764 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1770 if (req->oldlen <= req->oldidx)
1773 if (i > req->oldlen - req->oldidx)
1774 i = req->oldlen - req->oldidx;
1776 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1779 if (req->oldptr && i != l)
1785 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1789 if (req->newlen - req->newidx < l)
1791 bcopy((const char *)req->newptr + req->newidx, p, l);
1797 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1798 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1801 struct sysctl_req req;
1803 bzero(&req, sizeof req);
1809 req.oldlen = *oldlenp;
1811 req.validlen = req.oldlen;
1818 req.newlen = newlen;
1822 req.oldfunc = sysctl_old_kernel;
1823 req.newfunc = sysctl_new_kernel;
1824 req.lock = REQ_UNWIRED;
1826 error = sysctl_root(0, name, namelen, &req);
1828 if (req.lock == REQ_WIRED && req.validlen > 0)
1829 vsunlock(req.oldptr, req.validlen);
1831 if (error && error != ENOMEM)
1835 if (req.oldptr && req.oldidx > req.validlen)
1836 *retval = req.validlen;
1838 *retval = req.oldidx;
1844 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1845 void *new, size_t newlen, size_t *retval, int flags)
1847 int oid[CTL_MAXNAME];
1848 size_t oidlen, plen;
1851 oid[0] = 0; /* sysctl internal magic */
1852 oid[1] = 3; /* name2oid */
1853 oidlen = sizeof(oid);
1855 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1856 (void *)name, strlen(name), &plen, flags);
1860 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1861 new, newlen, retval, flags);
1866 * Transfer function to/from user space.
1869 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1871 size_t i, len, origidx;
1874 origidx = req->oldidx;
1876 if (req->oldptr == NULL)
1879 * If we have not wired the user supplied buffer and we are currently
1880 * holding locks, drop a witness warning, as it's possible that
1881 * write operations to the user page can sleep.
1883 if (req->lock != REQ_WIRED)
1884 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1885 "sysctl_old_user()");
1887 len = req->validlen;
1891 if (i > len - origidx)
1893 if (req->lock == REQ_WIRED) {
1894 error = copyout_nofault(p, (char *)req->oldptr +
1897 error = copyout(p, (char *)req->oldptr + origidx, i);
1907 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1913 if (req->newlen - req->newidx < l)
1915 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1916 "sysctl_new_user()");
1917 error = copyin((const char *)req->newptr + req->newidx, p, l);
1923 * Wire the user space destination buffer. If set to a value greater than
1924 * zero, the len parameter limits the maximum amount of wired memory.
1927 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1932 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1934 if (req->lock != REQ_WIRED && req->oldptr &&
1935 req->oldfunc == sysctl_old_user) {
1936 if (wiredlen != 0) {
1937 ret = vslock(req->oldptr, wiredlen);
1944 req->lock = REQ_WIRED;
1945 req->validlen = wiredlen;
1951 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1952 int *nindx, struct sysctl_req *req)
1954 struct sysctl_oid_list *lsp;
1955 struct sysctl_oid *oid;
1958 SYSCTL_ASSERT_LOCKED();
1959 lsp = &sysctl__children;
1961 while (indx < CTL_MAXNAME) {
1962 SLIST_FOREACH(oid, lsp, oid_link) {
1963 if (oid->oid_number == name[indx])
1970 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1971 if (oid->oid_handler != NULL || indx == namelen) {
1975 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1976 ("%s found DYING node %p", __func__, oid));
1979 lsp = SYSCTL_CHILDREN(oid);
1980 } else if (indx == namelen) {
1981 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1986 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1987 ("%s found DYING node %p", __func__, oid));
1997 * Traverse our tree, and find the right node, execute whatever it points
1998 * to, and return the resulting error code.
2002 sysctl_root(SYSCTL_HANDLER_ARGS)
2004 struct sysctl_oid *oid;
2005 struct rm_priotracker tracker;
2006 int error, indx, lvl;
2008 SYSCTL_RLOCK(&tracker);
2010 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
2014 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2016 * You can't call a sysctl when it's a node, but has
2017 * no handler. Inform the user that it's a node.
2018 * The indx may or may not be the same as namelen.
2020 if (oid->oid_handler == NULL) {
2026 /* Is this sysctl writable? */
2027 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
2032 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
2034 #ifdef CAPABILITY_MODE
2036 * If the process is in capability mode, then don't permit reading or
2037 * writing unless specifically granted for the node.
2039 if (IN_CAPABILITY_MODE(req->td)) {
2040 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
2041 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
2048 /* Is this sysctl sensitive to securelevels? */
2049 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
2050 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
2051 error = securelevel_gt(req->td->td_ucred, lvl);
2056 /* Is this sysctl writable by only privileged users? */
2057 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
2060 if (oid->oid_kind & CTLFLAG_PRISON)
2061 priv = PRIV_SYSCTL_WRITEJAIL;
2063 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2064 prison_owns_vnet(req->td->td_ucred))
2065 priv = PRIV_SYSCTL_WRITEJAIL;
2068 priv = PRIV_SYSCTL_WRITE;
2069 error = priv_check(req->td, priv);
2074 if (!oid->oid_handler) {
2079 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2080 arg1 = (int *)arg1 + indx;
2083 arg1 = oid->oid_arg1;
2084 arg2 = oid->oid_arg2;
2087 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2093 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2094 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2096 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2099 SYSCTL_RUNLOCK(&tracker);
2103 #ifndef _SYS_SYSPROTO_H_
2104 struct sysctl_args {
2114 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2116 int error, i, name[CTL_MAXNAME];
2119 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2122 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2126 error = userland_sysctl(td, name, uap->namelen,
2127 uap->old, uap->oldlenp, 0,
2128 uap->new, uap->newlen, &j, 0);
2129 if (error && error != ENOMEM)
2132 i = copyout(&j, uap->oldlenp, sizeof(j));
2140 * This is used from various compatibility syscalls too. That's why name
2141 * must be in kernel space.
2144 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2145 size_t *oldlenp, int inkernel, const void *new, size_t newlen,
2146 size_t *retval, int flags)
2148 int error = 0, memlocked;
2149 struct sysctl_req req;
2151 bzero(&req, sizeof req);
2158 req.oldlen = *oldlenp;
2160 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2165 req.validlen = req.oldlen;
2169 req.newlen = newlen;
2173 req.oldfunc = sysctl_old_user;
2174 req.newfunc = sysctl_new_user;
2175 req.lock = REQ_UNWIRED;
2178 if (KTRPOINT(curthread, KTR_SYSCTL))
2179 ktrsysctl(name, namelen);
2182 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2184 sx_xlock(&sysctlmemlock);
2186 CURVNET_SET(TD_TO_VNET(td));
2191 error = sysctl_root(0, name, namelen, &req);
2192 if (error != EAGAIN)
2194 kern_yield(PRI_USER);
2199 if (req.lock == REQ_WIRED && req.validlen > 0)
2200 vsunlock(req.oldptr, req.validlen);
2202 sx_xunlock(&sysctlmemlock);
2204 if (error && error != ENOMEM)
2208 if (req.oldptr && req.oldidx > req.validlen)
2209 *retval = req.validlen;
2211 *retval = req.oldidx;
2217 * Drain into a sysctl struct. The user buffer should be wired if a page
2218 * fault would cause issue.
2221 sbuf_sysctl_drain(void *arg, const char *data, int len)
2223 struct sysctl_req *req = arg;
2226 error = SYSCTL_OUT(req, data, len);
2227 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2228 return (error == 0 ? len : -error);
2232 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2233 struct sysctl_req *req)
2236 /* Supply a default buffer size if none given. */
2237 if (buf == NULL && length == 0)
2239 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2240 sbuf_set_drain(s, sbuf_sysctl_drain, req);