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_compat.h"
45 #include "opt_ktrace.h"
47 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/capsicum.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/rmlock.h>
62 #include <sys/sysproto.h>
65 #include <sys/ktrace.h>
70 #include <security/mac/mac_framework.h>
73 #include <vm/vm_extern.h>
75 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
76 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
77 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
80 * The sysctllock protects the MIB tree. It also protects sysctl
81 * contexts used with dynamic sysctls. The sysctl_register_oid() and
82 * sysctl_unregister_oid() routines require the sysctllock to already
83 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
84 * provided for the few places in the kernel which need to use that
85 * API rather than using the dynamic API. Use of the dynamic API is
86 * strongly encouraged for most code.
88 * The sysctlmemlock is used to limit the amount of user memory wired for
89 * sysctl requests. This is implemented by serializing any userland
90 * sysctl requests larger than a single page via an exclusive lock.
92 static struct rmlock sysctllock;
93 static struct sx __exclusive_cache_line sysctlmemlock;
95 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
96 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
97 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
98 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
99 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
100 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
101 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
102 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
103 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
105 #define SYSCTL_SLEEP(ch, wmesg, timo) \
106 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
108 static int sysctl_root(SYSCTL_HANDLER_ARGS);
111 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
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);
207 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
208 len = strlen(curr->oid_name);
213 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
216 memcpy(path + rem, curr->oid_name, len);
218 path[rem + len] = '.';
221 memset(&req, 0, sizeof(req));
224 req.oldfunc = sysctl_old_kernel;
225 req.newfunc = sysctl_new_kernel;
226 req.lock = REQ_UNWIRED;
228 switch (oidp->oid_kind & CTLTYPE) {
230 if (getenv_int(path + rem, &val_int) == 0)
232 req.newlen = sizeof(val_int);
233 req.newptr = &val_int;
236 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
238 req.newlen = sizeof(val_int);
239 req.newptr = &val_int;
242 if (getenv_long(path + rem, &val_long) == 0)
244 req.newlen = sizeof(val_long);
245 req.newptr = &val_long;
248 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
250 req.newlen = sizeof(val_long);
251 req.newptr = &val_long;
254 if (getenv_int(path + rem, &val_int) == 0)
257 req.newlen = sizeof(val_8);
261 if (getenv_int(path + rem, &val_int) == 0)
264 req.newlen = sizeof(val_16);
265 req.newptr = &val_16;
268 if (getenv_long(path + rem, &val_long) == 0)
271 req.newlen = sizeof(val_32);
272 req.newptr = &val_32;
275 if (getenv_quad(path + rem, &val_quad) == 0)
278 req.newlen = sizeof(val_64);
279 req.newptr = &val_64;
282 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
285 req.newlen = sizeof(val_8);
289 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
292 req.newlen = sizeof(val_16);
293 req.newptr = &val_16;
296 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
299 req.newlen = sizeof(val_32);
300 req.newptr = &val_32;
303 /* XXX there is no getenv_uquad() */
304 if (getenv_quad(path + rem, &val_quad) == 0)
307 req.newlen = sizeof(val_64);
308 req.newptr = &val_64;
311 penv = kern_getenv(path + rem);
314 req.newlen = strlen(penv);
320 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
321 oidp->oid_arg2, &req, NULL);
323 printf("Setting sysctl %s failed: %d\n", path + rem, error);
329 sbuf_printf_drain(void *arg __unused, const char *data, int len)
332 return (printf("%.*s", len, data));
336 * Locate the path to a given oid. Returns the length of the resulting path,
337 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
338 * elements and be NULL initialized.
341 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
345 SYSCTL_ASSERT_LOCKED();
347 while (indx < CTL_MAXNAME && indx >= 0) {
348 if (nodes[indx] == NULL && indx == 0)
349 nodes[indx] = SLIST_FIRST(&sysctl__children);
350 else if (nodes[indx] == NULL)
351 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
353 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
355 if (nodes[indx] == needle)
358 if (nodes[indx] == NULL) {
363 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
372 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
374 struct sysctl_oid *nodes[CTL_MAXNAME];
379 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
380 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
382 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
384 memset(nodes, 0, sizeof(nodes));
385 rc = sysctl_search_oid(nodes, leaf);
387 for (i = 0; i < rc; i++)
388 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
391 sbuf_printf(&sb, "%s", leaf->oid_name);
393 sbuf_printf(&sb, ")!\n");
395 (void)sbuf_finish(&sb);
400 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
402 struct rm_priotracker tracker;
404 SYSCTL_RLOCK(&tracker);
405 sysctl_warn_reuse(__func__, oidp);
406 SYSCTL_RUNLOCK(&tracker);
409 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
410 0, 0, sysctl_reuse_test, "-", "");
414 sysctl_register_oid(struct sysctl_oid *oidp)
416 struct sysctl_oid_list *parent = oidp->oid_parent;
417 struct sysctl_oid *p;
418 struct sysctl_oid *q;
423 * First check if another oid with the same name already
424 * exists in the parent's list.
426 SYSCTL_ASSERT_WLOCKED();
427 p = sysctl_find_oidname(oidp->oid_name, parent);
429 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
433 sysctl_warn_reuse(__func__, p);
437 /* get current OID number */
438 oid_number = oidp->oid_number;
441 #error "OID_AUTO is expected to be a negative value"
444 * Any negative OID number qualifies as OID_AUTO. Valid OID
445 * numbers should always be positive.
447 * NOTE: DO NOT change the starting value here, change it in
448 * <sys/sysctl.h>, and make sure it is at least 256 to
449 * accommodate e.g. net.inet.raw as a static sysctl node.
451 if (oid_number < 0) {
455 * By decrementing the next OID number we spend less
456 * time inserting the OIDs into a sorted list.
458 if (--newoid < CTL_AUTO_START)
465 * Insert the OID into the parent's list sorted by OID number.
469 SLIST_FOREACH(p, parent, oid_link) {
470 /* check if the current OID number is in use */
471 if (oid_number == p->oid_number) {
472 /* get the next valid OID number */
473 if (oid_number < CTL_AUTO_START ||
474 oid_number == 0x7fffffff) {
475 /* wraparound - restart */
476 oid_number = CTL_AUTO_START;
477 /* don't loop forever */
479 panic("sysctl: Out of OID numbers\n");
484 } else if (oid_number < p->oid_number)
488 /* check for non-auto OID number collision */
489 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
490 oid_number >= CTL_AUTO_START) {
491 printf("sysctl: OID number(%d) is already in use for '%s'\n",
492 oidp->oid_number, oidp->oid_name);
494 /* update the OID number, if any */
495 oidp->oid_number = oid_number;
497 SLIST_INSERT_AFTER(q, oidp, oid_link);
499 SLIST_INSERT_HEAD(parent, oidp, oid_link);
501 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
503 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
505 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
506 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
507 /* only fetch value once */
508 oidp->oid_kind |= CTLFLAG_NOFETCH;
509 /* try to fetch value from kernel environment */
510 sysctl_load_tunable_by_oid_locked(oidp);
515 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
519 * Mark the leaf as dormant if it's not to be immediately enabled.
520 * We do not disable nodes as they can be shared between modules
521 * and it is always safe to access a node.
523 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
524 ("internal flag is set in oid_kind"));
525 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
526 oidp->oid_kind |= CTLFLAG_DORMANT;
527 sysctl_register_oid(oidp);
531 sysctl_enable_oid(struct sysctl_oid *oidp)
534 SYSCTL_ASSERT_WLOCKED();
535 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
536 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
537 ("sysctl node is marked as dormant"));
540 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
541 ("enabling already enabled sysctl oid"));
542 oidp->oid_kind &= ~CTLFLAG_DORMANT;
546 sysctl_unregister_oid(struct sysctl_oid *oidp)
548 struct sysctl_oid *p;
551 SYSCTL_ASSERT_WLOCKED();
553 if (oidp->oid_number == OID_AUTO) {
556 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
558 SLIST_REMOVE(oidp->oid_parent, oidp,
559 sysctl_oid, oid_link);
567 * This can happen when a module fails to register and is
568 * being unloaded afterwards. It should not be a panic()
572 printf("%s: failed to unregister sysctl\n", __func__);
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);
740 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
742 struct sysctl_oid *p, *tmp;
745 SYSCTL_ASSERT_WLOCKED();
748 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
749 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
754 * WARNING: normal method to do this should be through
755 * sysctl_ctx_free(). Use recursing as the last resort
756 * method to purge your sysctl tree of leftovers...
757 * However, if some other code still references these nodes,
760 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
761 if (oidp->oid_refcnt == 1) {
762 SLIST_FOREACH_SAFE(p,
763 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
765 printf("Warning: failed attempt to "
766 "remove oid %s with child %s\n",
767 oidp->oid_name, p->oid_name);
770 error = sysctl_remove_oid_locked(p, del,
777 if (oidp->oid_refcnt > 1 ) {
780 if (oidp->oid_refcnt == 0) {
781 printf("Warning: bad oid_refcnt=%u (%s)!\n",
782 oidp->oid_refcnt, oidp->oid_name);
785 sysctl_unregister_oid(oidp);
788 * Wait for all threads running the handler to drain.
789 * This preserves the previous behavior when the
790 * sysctl lock was held across a handler invocation,
791 * and is necessary for module unload correctness.
793 while (oidp->oid_running > 0) {
794 oidp->oid_kind |= CTLFLAG_DYING;
795 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
798 free(__DECONST(char *, oidp->oid_descr),
801 free(__DECONST(char *, oidp->oid_label),
803 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
804 free(oidp, M_SYSCTLOID);
810 * Create new sysctls at run time.
811 * clist may point to a valid context initialized with sysctl_ctx_init().
814 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
815 int number, const char *name, int kind, void *arg1, intmax_t arg2,
816 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
819 struct sysctl_oid *oidp;
821 /* You have to hook up somewhere.. */
824 /* Check if the node already exists, otherwise create it */
826 oidp = sysctl_find_oidname(name, parent);
828 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
830 /* Update the context */
832 sysctl_ctx_entry_add(clist, oidp);
836 sysctl_warn_reuse(__func__, oidp);
841 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
842 oidp->oid_parent = parent;
843 SLIST_INIT(&oidp->oid_children);
844 oidp->oid_number = number;
845 oidp->oid_refcnt = 1;
846 oidp->oid_name = strdup(name, M_SYSCTLOID);
847 oidp->oid_handler = handler;
848 oidp->oid_kind = CTLFLAG_DYN | kind;
849 oidp->oid_arg1 = arg1;
850 oidp->oid_arg2 = arg2;
853 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
855 oidp->oid_label = strdup(label, M_SYSCTLOID);
856 /* Update the context, if used */
858 sysctl_ctx_entry_add(clist, oidp);
859 /* Register this oid */
860 sysctl_register_oid(oidp);
866 * Rename an existing oid.
869 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
874 newname = strdup(name, M_SYSCTLOID);
876 oldname = __DECONST(char *, oidp->oid_name);
877 oidp->oid_name = newname;
879 free(oldname, M_SYSCTLOID);
883 * Reparent an existing oid.
886 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
888 struct sysctl_oid *oidp;
891 if (oid->oid_parent == parent) {
895 oidp = sysctl_find_oidname(oid->oid_name, parent);
900 sysctl_unregister_oid(oid);
901 oid->oid_parent = parent;
902 oid->oid_number = OID_AUTO;
903 sysctl_register_oid(oid);
909 * Register the kernel's oids on startup.
911 SET_DECLARE(sysctl_set, struct sysctl_oid);
914 sysctl_register_all(void *arg)
916 struct sysctl_oid **oidp;
918 sx_init(&sysctlmemlock, "sysctl mem");
921 SET_FOREACH(oidp, sysctl_set)
922 sysctl_register_oid(*oidp);
925 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
930 * These functions implement a presently undocumented interface
931 * used by the sysctl program to walk the tree, and get the type
932 * so it can print the value.
933 * This interface is under work and consideration, and should probably
934 * be killed with a big axe by the first person who can find the time.
935 * (be aware though, that the proper interface isn't as obvious as it
936 * may seem, there are various conflicting requirements.
938 * {0,0} printf the entire MIB-tree.
939 * {0,1,...} return the name of the "..." OID.
940 * {0,2,...} return the next OID.
941 * {0,3} return the OID of the name in "new"
942 * {0,4,...} return the kind & format info for the "..." OID.
943 * {0,5,...} return the description of the "..." OID.
944 * {0,6,...} return the aggregation label of the "..." OID.
949 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
952 struct sysctl_oid *oidp;
954 SYSCTL_ASSERT_LOCKED();
955 SLIST_FOREACH(oidp, l, oid_link) {
960 printf("%d %s ", oidp->oid_number, oidp->oid_name);
963 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
964 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
966 if (oidp->oid_handler)
969 switch (oidp->oid_kind & CTLTYPE) {
972 if (!oidp->oid_handler) {
973 sysctl_sysctl_debug_dump_node(
974 SYSCTL_CHILDREN(oidp), i + 2);
977 case CTLTYPE_INT: printf(" Int\n"); break;
978 case CTLTYPE_UINT: printf(" u_int\n"); break;
979 case CTLTYPE_LONG: printf(" Long\n"); break;
980 case CTLTYPE_ULONG: printf(" u_long\n"); break;
981 case CTLTYPE_STRING: printf(" String\n"); break;
982 case CTLTYPE_S8: printf(" int8_t\n"); break;
983 case CTLTYPE_S16: printf(" int16_t\n"); break;
984 case CTLTYPE_S32: printf(" int32_t\n"); break;
985 case CTLTYPE_S64: printf(" int64_t\n"); break;
986 case CTLTYPE_U8: printf(" uint8_t\n"); break;
987 case CTLTYPE_U16: printf(" uint16_t\n"); break;
988 case CTLTYPE_U32: printf(" uint32_t\n"); break;
989 case CTLTYPE_U64: printf(" uint64_t\n"); break;
990 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
991 default: printf("\n");
998 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
1000 struct rm_priotracker tracker;
1003 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
1006 SYSCTL_RLOCK(&tracker);
1007 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
1008 SYSCTL_RUNLOCK(&tracker);
1012 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
1013 0, 0, sysctl_sysctl_debug, "-", "");
1017 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
1019 int *name = (int *) arg1;
1020 u_int namelen = arg2;
1022 struct sysctl_oid *oid;
1023 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
1024 struct rm_priotracker tracker;
1027 SYSCTL_RLOCK(&tracker);
1030 snprintf(buf,sizeof(buf),"%d",*name);
1032 error = SYSCTL_OUT(req, ".", 1);
1034 error = SYSCTL_OUT(req, buf, strlen(buf));
1042 SLIST_FOREACH(oid, lsp, oid_link) {
1043 if (oid->oid_number != *name)
1047 error = SYSCTL_OUT(req, ".", 1);
1049 error = SYSCTL_OUT(req, oid->oid_name,
1050 strlen(oid->oid_name));
1057 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1060 if (oid->oid_handler)
1063 lsp2 = SYSCTL_CHILDREN(oid);
1068 error = SYSCTL_OUT(req, "", 1);
1070 SYSCTL_RUNLOCK(&tracker);
1075 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1078 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1079 sysctl_sysctl_name, "");
1082 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1083 int *next, int *len, int level, struct sysctl_oid **oidpp)
1085 struct sysctl_oid *oidp;
1087 SYSCTL_ASSERT_LOCKED();
1089 SLIST_FOREACH(oidp, lsp, oid_link) {
1090 *next = oidp->oid_number;
1093 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1097 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1099 if (oidp->oid_handler)
1100 /* We really should call the handler here...*/
1102 lsp = SYSCTL_CHILDREN(oidp);
1103 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1104 len, level+1, oidpp))
1109 if (oidp->oid_number < *name)
1112 if (oidp->oid_number > *name) {
1113 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1115 if (oidp->oid_handler)
1117 lsp = SYSCTL_CHILDREN(oidp);
1118 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1119 next+1, len, level+1, oidpp))
1123 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1126 if (oidp->oid_handler)
1129 lsp = SYSCTL_CHILDREN(oidp);
1130 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1131 len, level+1, oidpp))
1142 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1144 int *name = (int *) arg1;
1145 u_int namelen = arg2;
1147 struct sysctl_oid *oid;
1148 struct sysctl_oid_list *lsp = &sysctl__children;
1149 struct rm_priotracker tracker;
1150 int newoid[CTL_MAXNAME];
1152 SYSCTL_RLOCK(&tracker);
1153 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1154 SYSCTL_RUNLOCK(&tracker);
1157 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1162 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1165 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1166 sysctl_sysctl_next, "");
1169 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1171 struct sysctl_oid *oidp;
1172 struct sysctl_oid_list *lsp = &sysctl__children;
1175 SYSCTL_ASSERT_LOCKED();
1177 for (*len = 0; *len < CTL_MAXNAME;) {
1178 p = strsep(&name, ".");
1180 oidp = SLIST_FIRST(lsp);
1181 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1184 if (strcmp(p, oidp->oid_name) == 0)
1187 *oid++ = oidp->oid_number;
1190 if (name == NULL || *name == '\0') {
1196 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1199 if (oidp->oid_handler)
1202 lsp = SYSCTL_CHILDREN(oidp);
1208 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1211 int error, oid[CTL_MAXNAME], len = 0;
1212 struct sysctl_oid *op = NULL;
1213 struct rm_priotracker tracker;
1218 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1219 return (ENAMETOOLONG);
1222 if (req->newlen >= sizeof(buf))
1223 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1225 error = SYSCTL_IN(req, p, req->newlen);
1232 p [req->newlen] = '\0';
1234 SYSCTL_RLOCK(&tracker);
1235 error = name2oid(p, oid, &len, &op);
1236 SYSCTL_RUNLOCK(&tracker);
1244 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1249 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1252 SYSCTL_PROC(_sysctl, 3, name2oid,
1253 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1254 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1257 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1259 struct sysctl_oid *oid;
1260 struct rm_priotracker tracker;
1263 SYSCTL_RLOCK(&tracker);
1264 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1268 if (oid->oid_fmt == NULL) {
1272 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1275 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1277 SYSCTL_RUNLOCK(&tracker);
1282 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1283 sysctl_sysctl_oidfmt, "");
1286 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1288 struct sysctl_oid *oid;
1289 struct rm_priotracker tracker;
1292 SYSCTL_RLOCK(&tracker);
1293 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1297 if (oid->oid_descr == NULL) {
1301 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1303 SYSCTL_RUNLOCK(&tracker);
1307 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1308 sysctl_sysctl_oiddescr, "");
1311 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1313 struct sysctl_oid *oid;
1314 struct rm_priotracker tracker;
1317 SYSCTL_RLOCK(&tracker);
1318 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1322 if (oid->oid_label == NULL) {
1326 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1328 SYSCTL_RUNLOCK(&tracker);
1332 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1333 CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1336 * Default "handler" functions.
1342 * a variable: point arg1 at it.
1343 * a constant: pass it in arg2.
1347 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1353 * Attempt to get a coherent snapshot by making a copy of the data.
1356 temp = *(bool *)arg1 ? 1 : 0;
1358 temp = arg2 ? 1 : 0;
1360 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1361 if (error || !req->newptr)
1367 error = SYSCTL_IN(req, &temp, sizeof(temp));
1369 *(bool *)arg1 = temp ? 1 : 0;
1375 * Handle an int8_t, signed or unsigned.
1377 * a variable: point arg1 at it.
1378 * a constant: pass it in arg2.
1382 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1388 * Attempt to get a coherent snapshot by making a copy of the data.
1391 tmpout = *(int8_t *)arg1;
1394 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1396 if (error || !req->newptr)
1402 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1407 * Handle an int16_t, signed or unsigned.
1409 * a variable: point arg1 at it.
1410 * a constant: pass it in arg2.
1414 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1420 * Attempt to get a coherent snapshot by making a copy of the data.
1423 tmpout = *(int16_t *)arg1;
1426 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1428 if (error || !req->newptr)
1434 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1439 * Handle an int32_t, signed or unsigned.
1441 * a variable: point arg1 at it.
1442 * a constant: pass it in arg2.
1446 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1452 * Attempt to get a coherent snapshot by making a copy of the data.
1455 tmpout = *(int32_t *)arg1;
1458 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1460 if (error || !req->newptr)
1466 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1471 * Handle an int, signed or unsigned.
1473 * a variable: point arg1 at it.
1474 * a constant: pass it in arg2.
1478 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1480 int tmpout, error = 0;
1483 * Attempt to get a coherent snapshot by making a copy of the data.
1486 tmpout = *(int *)arg1;
1489 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1491 if (error || !req->newptr)
1497 error = SYSCTL_IN(req, arg1, sizeof(int));
1502 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1503 * Note: this is used by TCP.
1507 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1512 s = (int)((int64_t)tt * 1000 / hz);
1514 error = sysctl_handle_int(oidp, &s, 0, req);
1515 if (error || !req->newptr)
1518 tt = (int)((int64_t)s * hz / 1000);
1528 * Handle a long, signed or unsigned.
1530 * a variable: point arg1 at it.
1531 * a constant: pass it in arg2.
1535 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1544 * Attempt to get a coherent snapshot by making a copy of the data.
1547 tmplong = *(long *)arg1;
1551 if (req->flags & SCTL_MASK32) {
1553 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1556 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1558 if (error || !req->newptr)
1564 else if (req->flags & SCTL_MASK32) {
1565 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1566 *(long *)arg1 = (long)tmpint;
1570 error = SYSCTL_IN(req, arg1, sizeof(long));
1575 * Handle a 64 bit int, signed or unsigned.
1577 * a variable: point arg1 at it.
1578 * a constant: pass it in arg2.
1581 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1587 * Attempt to get a coherent snapshot by making a copy of the data.
1590 tmpout = *(uint64_t *)arg1;
1593 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1595 if (error || !req->newptr)
1601 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1606 * Handle our generic '\0' terminated 'C' string.
1608 * a variable string: point arg1 at it, arg2 is max length.
1609 * a constant string: point arg1 at it, arg2 is zero.
1613 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1616 int error = 0, ro_string = 0;
1619 * A zero-length buffer indicates a fixed size read-only
1623 arg2 = strlen((char *)arg1) + 1;
1627 if (req->oldptr != NULL) {
1633 /* try to make a coherent snapshot of the string */
1634 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1635 memcpy(tmparg, arg1, arg2);
1638 outlen = strnlen(tmparg, arg2 - 1) + 1;
1639 error = SYSCTL_OUT(req, tmparg, outlen);
1642 free(tmparg, M_SYSCTLTMP);
1644 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1645 error = SYSCTL_OUT(req, NULL, outlen);
1647 if (error || !req->newptr)
1650 if ((req->newlen - req->newidx) >= arg2) {
1653 arg2 = (req->newlen - req->newidx);
1654 error = SYSCTL_IN(req, arg1, arg2);
1655 ((char *)arg1)[arg2] = '\0';
1661 * Handle any kind of opaque data.
1662 * arg1 points to it, arg2 is the size.
1666 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1670 struct sysctl_req req2;
1673 * Attempt to get a coherent snapshot, by using the thread
1674 * pre-emption counter updated from within mi_switch() to
1675 * determine if we were pre-empted during a bcopy() or
1676 * copyout(). Make 3 attempts at doing this before giving up.
1677 * If we encounter an error, stop immediately.
1682 generation = curthread->td_generation;
1683 error = SYSCTL_OUT(req, arg1, arg2);
1687 if (generation != curthread->td_generation && tries < 3) {
1692 error = SYSCTL_IN(req, arg1, arg2);
1698 * Transfer functions to/from kernel space.
1699 * XXX: rather untested at this point
1702 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1708 if (req->oldlen <= req->oldidx)
1711 if (i > req->oldlen - req->oldidx)
1712 i = req->oldlen - req->oldidx;
1714 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1717 if (req->oldptr && i != l)
1723 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1727 if (req->newlen - req->newidx < l)
1729 bcopy((char *)req->newptr + req->newidx, p, l);
1735 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1736 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1739 struct sysctl_req req;
1741 bzero(&req, sizeof req);
1747 req.oldlen = *oldlenp;
1749 req.validlen = req.oldlen;
1756 req.newlen = newlen;
1760 req.oldfunc = sysctl_old_kernel;
1761 req.newfunc = sysctl_new_kernel;
1762 req.lock = REQ_UNWIRED;
1764 error = sysctl_root(0, name, namelen, &req);
1766 if (req.lock == REQ_WIRED && req.validlen > 0)
1767 vsunlock(req.oldptr, req.validlen);
1769 if (error && error != ENOMEM)
1773 if (req.oldptr && req.oldidx > req.validlen)
1774 *retval = req.validlen;
1776 *retval = req.oldidx;
1782 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1783 void *new, size_t newlen, size_t *retval, int flags)
1785 int oid[CTL_MAXNAME];
1786 size_t oidlen, plen;
1789 oid[0] = 0; /* sysctl internal magic */
1790 oid[1] = 3; /* name2oid */
1791 oidlen = sizeof(oid);
1793 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1794 (void *)name, strlen(name), &plen, flags);
1798 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1799 new, newlen, retval, flags);
1804 * Transfer function to/from user space.
1807 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1809 size_t i, len, origidx;
1812 origidx = req->oldidx;
1814 if (req->oldptr == NULL)
1817 * If we have not wired the user supplied buffer and we are currently
1818 * holding locks, drop a witness warning, as it's possible that
1819 * write operations to the user page can sleep.
1821 if (req->lock != REQ_WIRED)
1822 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1823 "sysctl_old_user()");
1825 len = req->validlen;
1829 if (i > len - origidx)
1831 if (req->lock == REQ_WIRED) {
1832 error = copyout_nofault(p, (char *)req->oldptr +
1835 error = copyout(p, (char *)req->oldptr + origidx, i);
1845 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1851 if (req->newlen - req->newidx < l)
1853 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1854 "sysctl_new_user()");
1855 error = copyin((char *)req->newptr + req->newidx, p, l);
1861 * Wire the user space destination buffer. If set to a value greater than
1862 * zero, the len parameter limits the maximum amount of wired memory.
1865 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1870 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1872 if (req->lock != REQ_WIRED && req->oldptr &&
1873 req->oldfunc == sysctl_old_user) {
1874 if (wiredlen != 0) {
1875 ret = vslock(req->oldptr, wiredlen);
1882 req->lock = REQ_WIRED;
1883 req->validlen = wiredlen;
1889 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1890 int *nindx, struct sysctl_req *req)
1892 struct sysctl_oid_list *lsp;
1893 struct sysctl_oid *oid;
1896 SYSCTL_ASSERT_LOCKED();
1897 lsp = &sysctl__children;
1899 while (indx < CTL_MAXNAME) {
1900 SLIST_FOREACH(oid, lsp, oid_link) {
1901 if (oid->oid_number == name[indx])
1908 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1909 if (oid->oid_handler != NULL || indx == namelen) {
1913 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1914 ("%s found DYING node %p", __func__, oid));
1917 lsp = SYSCTL_CHILDREN(oid);
1918 } else if (indx == namelen) {
1919 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1924 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1925 ("%s found DYING node %p", __func__, oid));
1935 * Traverse our tree, and find the right node, execute whatever it points
1936 * to, and return the resulting error code.
1940 sysctl_root(SYSCTL_HANDLER_ARGS)
1942 struct sysctl_oid *oid;
1943 struct rm_priotracker tracker;
1944 int error, indx, lvl;
1946 SYSCTL_RLOCK(&tracker);
1948 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1952 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1954 * You can't call a sysctl when it's a node, but has
1955 * no handler. Inform the user that it's a node.
1956 * The indx may or may not be the same as namelen.
1958 if (oid->oid_handler == NULL) {
1964 /* Is this sysctl writable? */
1965 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1970 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1972 #ifdef CAPABILITY_MODE
1974 * If the process is in capability mode, then don't permit reading or
1975 * writing unless specifically granted for the node.
1977 if (IN_CAPABILITY_MODE(req->td)) {
1978 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1979 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1986 /* Is this sysctl sensitive to securelevels? */
1987 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1988 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1989 error = securelevel_gt(req->td->td_ucred, lvl);
1994 /* Is this sysctl writable by only privileged users? */
1995 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1998 if (oid->oid_kind & CTLFLAG_PRISON)
1999 priv = PRIV_SYSCTL_WRITEJAIL;
2001 else if ((oid->oid_kind & CTLFLAG_VNET) &&
2002 prison_owns_vnet(req->td->td_ucred))
2003 priv = PRIV_SYSCTL_WRITEJAIL;
2006 priv = PRIV_SYSCTL_WRITE;
2007 error = priv_check(req->td, priv);
2012 if (!oid->oid_handler) {
2017 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
2018 arg1 = (int *)arg1 + indx;
2021 arg1 = oid->oid_arg1;
2022 arg2 = oid->oid_arg2;
2025 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
2031 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
2032 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
2034 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
2037 SYSCTL_RUNLOCK(&tracker);
2041 #ifndef _SYS_SYSPROTO_H_
2042 struct sysctl_args {
2052 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2054 int error, i, name[CTL_MAXNAME];
2057 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2060 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2064 error = userland_sysctl(td, name, uap->namelen,
2065 uap->old, uap->oldlenp, 0,
2066 uap->new, uap->newlen, &j, 0);
2067 if (error && error != ENOMEM)
2070 i = copyout(&j, uap->oldlenp, sizeof(j));
2078 * This is used from various compatibility syscalls too. That's why name
2079 * must be in kernel space.
2082 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2083 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2086 int error = 0, memlocked;
2087 struct sysctl_req req;
2089 bzero(&req, sizeof req);
2096 req.oldlen = *oldlenp;
2098 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2103 req.validlen = req.oldlen;
2107 req.newlen = newlen;
2111 req.oldfunc = sysctl_old_user;
2112 req.newfunc = sysctl_new_user;
2113 req.lock = REQ_UNWIRED;
2116 if (KTRPOINT(curthread, KTR_SYSCTL))
2117 ktrsysctl(name, namelen);
2120 if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
2122 sx_xlock(&sysctlmemlock);
2124 CURVNET_SET(TD_TO_VNET(td));
2129 error = sysctl_root(0, name, namelen, &req);
2130 if (error != EAGAIN)
2132 kern_yield(PRI_USER);
2137 if (req.lock == REQ_WIRED && req.validlen > 0)
2138 vsunlock(req.oldptr, req.validlen);
2140 sx_xunlock(&sysctlmemlock);
2142 if (error && error != ENOMEM)
2146 if (req.oldptr && req.oldidx > req.validlen)
2147 *retval = req.validlen;
2149 *retval = req.oldidx;
2155 * Drain into a sysctl struct. The user buffer should be wired if a page
2156 * fault would cause issue.
2159 sbuf_sysctl_drain(void *arg, const char *data, int len)
2161 struct sysctl_req *req = arg;
2164 error = SYSCTL_OUT(req, data, len);
2165 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2166 return (error == 0 ? len : -error);
2170 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2171 struct sysctl_req *req)
2174 /* Supply a default buffer size if none given. */
2175 if (buf == NULL && length == 0)
2177 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2178 sbuf_set_drain(s, sbuf_sysctl_drain, req);