2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_capsicum.h"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/capsicum.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/rmlock.h>
60 #include <sys/sysproto.h>
63 #include <sys/ktrace.h>
68 #include <security/mac/mac_framework.h>
71 #include <vm/vm_extern.h>
73 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
74 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
75 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
78 * The sysctllock protects the MIB tree. It also protects sysctl
79 * contexts used with dynamic sysctls. The sysctl_register_oid() and
80 * sysctl_unregister_oid() routines require the sysctllock to already
81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
82 * provided for the few places in the kernel which need to use that
83 * API rather than using the dynamic API. Use of the dynamic API is
84 * strongly encouraged for most code.
86 * The sysctlmemlock is used to limit the amount of user memory wired for
87 * sysctl requests. This is implemented by serializing any userland
88 * sysctl requests larger than a single page via an exclusive lock.
90 static struct rmlock sysctllock;
91 static struct sx sysctlmemlock;
93 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
94 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
95 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
96 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
97 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
98 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
99 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
100 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
101 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
103 #define SYSCTL_SLEEP(ch, wmesg, timo) \
104 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
106 static int sysctl_root(SYSCTL_HANDLER_ARGS);
109 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
111 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
113 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
114 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
116 static struct sysctl_oid *
117 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
119 struct sysctl_oid *oidp;
121 SYSCTL_ASSERT_LOCKED();
122 SLIST_FOREACH(oidp, list, oid_link) {
123 if (strcmp(oidp->oid_name, name) == 0) {
131 * Initialization of the MIB tree.
133 * Order by number in each list.
150 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
151 struct sysctl_req *req, struct rm_priotracker *tracker)
155 if (oid->oid_kind & CTLFLAG_DYN)
156 atomic_add_int(&oid->oid_running, 1);
159 SYSCTL_RUNLOCK(tracker);
163 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
165 error = oid->oid_handler(oid, arg1, arg2, req);
166 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
169 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
172 SYSCTL_RLOCK(tracker);
176 if (oid->oid_kind & CTLFLAG_DYN) {
177 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
178 (oid->oid_kind & CTLFLAG_DYING) != 0)
179 wakeup(&oid->oid_running);
186 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
188 struct sysctl_req req;
189 struct sysctl_oid *curr;
192 ssize_t rem = sizeof(path);
205 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
206 len = strlen(curr->oid_name);
211 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
214 memcpy(path + rem, curr->oid_name, len);
216 path[rem + len] = '.';
219 memset(&req, 0, sizeof(req));
222 req.oldfunc = sysctl_old_kernel;
223 req.newfunc = sysctl_new_kernel;
224 req.lock = REQ_UNWIRED;
226 switch (oidp->oid_kind & CTLTYPE) {
228 if (getenv_int(path + rem, &val_int) == 0)
230 req.newlen = sizeof(val_int);
231 req.newptr = &val_int;
234 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
236 req.newlen = sizeof(val_int);
237 req.newptr = &val_int;
240 if (getenv_long(path + rem, &val_long) == 0)
242 req.newlen = sizeof(val_long);
243 req.newptr = &val_long;
246 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
248 req.newlen = sizeof(val_long);
249 req.newptr = &val_long;
252 if (getenv_int(path + rem, &val_int) == 0)
255 req.newlen = sizeof(val_8);
259 if (getenv_int(path + rem, &val_int) == 0)
262 req.newlen = sizeof(val_16);
263 req.newptr = &val_16;
266 if (getenv_long(path + rem, &val_long) == 0)
269 req.newlen = sizeof(val_32);
270 req.newptr = &val_32;
273 if (getenv_quad(path + rem, &val_quad) == 0)
276 req.newlen = sizeof(val_64);
277 req.newptr = &val_64;
280 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
283 req.newlen = sizeof(val_8);
287 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
290 req.newlen = sizeof(val_16);
291 req.newptr = &val_16;
294 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
297 req.newlen = sizeof(val_32);
298 req.newptr = &val_32;
301 /* XXX there is no getenv_uquad() */
302 if (getenv_quad(path + rem, &val_quad) == 0)
305 req.newlen = sizeof(val_64);
306 req.newptr = &val_64;
309 penv = kern_getenv(path + rem);
312 req.newlen = strlen(penv);
318 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
319 oidp->oid_arg2, &req, NULL);
321 printf("Setting sysctl %s failed: %d\n", path + rem, error);
327 sbuf_printf_drain(void *arg __unused, const char *data, int len)
330 return (printf("%.*s", len, data));
334 * Locate the path to a given oid. Returns the length of the resulting path,
335 * or -1 if the oid was not found. nodes must have room for CTL_MAXNAME
336 * elements and be NULL initialized.
339 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
343 SYSCTL_ASSERT_LOCKED();
345 while (indx < CTL_MAXNAME && indx >= 0) {
346 if (nodes[indx] == NULL && indx == 0)
347 nodes[indx] = SLIST_FIRST(&sysctl__children);
348 else if (nodes[indx] == NULL)
349 nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
351 nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
353 if (nodes[indx] == needle)
356 if (nodes[indx] == NULL) {
361 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
370 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
372 struct sysctl_oid *nodes[CTL_MAXNAME];
377 (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
378 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
380 sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
382 memset(nodes, 0, sizeof(nodes));
383 rc = sysctl_search_oid(nodes, leaf);
385 for (i = 0; i < rc; i++)
386 sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
389 sbuf_printf(&sb, "%s", leaf->oid_name);
391 sbuf_printf(&sb, ")!\n");
393 (void)sbuf_finish(&sb);
398 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
400 struct rm_priotracker tracker;
402 SYSCTL_RLOCK(&tracker);
403 sysctl_warn_reuse(__func__, oidp);
404 SYSCTL_RUNLOCK(&tracker);
407 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
408 0, 0, sysctl_reuse_test, "-", "");
412 sysctl_register_oid(struct sysctl_oid *oidp)
414 struct sysctl_oid_list *parent = oidp->oid_parent;
415 struct sysctl_oid *p;
416 struct sysctl_oid *q;
421 * First check if another oid with the same name already
422 * exists in the parent's list.
424 SYSCTL_ASSERT_WLOCKED();
425 p = sysctl_find_oidname(oidp->oid_name, parent);
427 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
431 sysctl_warn_reuse(__func__, p);
435 /* get current OID number */
436 oid_number = oidp->oid_number;
439 #error "OID_AUTO is expected to be a negative value"
442 * Any negative OID number qualifies as OID_AUTO. Valid OID
443 * numbers should always be positive.
445 * NOTE: DO NOT change the starting value here, change it in
446 * <sys/sysctl.h>, and make sure it is at least 256 to
447 * accommodate e.g. net.inet.raw as a static sysctl node.
449 if (oid_number < 0) {
453 * By decrementing the next OID number we spend less
454 * time inserting the OIDs into a sorted list.
456 if (--newoid < CTL_AUTO_START)
463 * Insert the OID into the parent's list sorted by OID number.
467 SLIST_FOREACH(p, parent, oid_link) {
468 /* check if the current OID number is in use */
469 if (oid_number == p->oid_number) {
470 /* get the next valid OID number */
471 if (oid_number < CTL_AUTO_START ||
472 oid_number == 0x7fffffff) {
473 /* wraparound - restart */
474 oid_number = CTL_AUTO_START;
475 /* don't loop forever */
477 panic("sysctl: Out of OID numbers\n");
482 } else if (oid_number < p->oid_number)
486 /* check for non-auto OID number collision */
487 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
488 oid_number >= CTL_AUTO_START) {
489 printf("sysctl: OID number(%d) is already in use for '%s'\n",
490 oidp->oid_number, oidp->oid_name);
492 /* update the OID number, if any */
493 oidp->oid_number = oid_number;
495 SLIST_INSERT_AFTER(q, oidp, oid_link);
497 SLIST_INSERT_HEAD(parent, oidp, oid_link);
499 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
501 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
503 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
504 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
505 /* only fetch value once */
506 oidp->oid_kind |= CTLFLAG_NOFETCH;
507 /* try to fetch value from kernel environment */
508 sysctl_load_tunable_by_oid_locked(oidp);
513 sysctl_unregister_oid(struct sysctl_oid *oidp)
515 struct sysctl_oid *p;
518 SYSCTL_ASSERT_WLOCKED();
520 if (oidp->oid_number == OID_AUTO) {
523 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
525 SLIST_REMOVE(oidp->oid_parent, oidp,
526 sysctl_oid, oid_link);
534 * This can happen when a module fails to register and is
535 * being unloaded afterwards. It should not be a panic()
539 printf("%s: failed to unregister sysctl\n", __func__);
542 /* Initialize a new context to keep track of dynamically added sysctls. */
544 sysctl_ctx_init(struct sysctl_ctx_list *c)
552 * No locking here, the caller is responsible for not adding
553 * new nodes to a context until after this function has
560 /* Free the context, and destroy all dynamic oids registered in this context */
562 sysctl_ctx_free(struct sysctl_ctx_list *clist)
564 struct sysctl_ctx_entry *e, *e1;
569 * First perform a "dry run" to check if it's ok to remove oids.
571 * XXX This algorithm is a hack. But I don't know any
572 * XXX better solution for now...
575 TAILQ_FOREACH(e, clist, link) {
576 error = sysctl_remove_oid_locked(e->entry, 0, 0);
581 * Restore deregistered entries, either from the end,
582 * or from the place where error occurred.
583 * e contains the entry that was not unregistered
586 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
588 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
590 sysctl_register_oid(e1->entry);
591 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
597 /* Now really delete the entries */
598 e = TAILQ_FIRST(clist);
600 e1 = TAILQ_NEXT(e, link);
601 error = sysctl_remove_oid_locked(e->entry, 1, 0);
603 panic("sysctl_remove_oid: corrupt tree, entry: %s",
605 free(e, M_SYSCTLOID);
612 /* Add an entry to the context */
613 struct sysctl_ctx_entry *
614 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
616 struct sysctl_ctx_entry *e;
618 SYSCTL_ASSERT_WLOCKED();
619 if (clist == NULL || oidp == NULL)
621 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
623 TAILQ_INSERT_HEAD(clist, e, link);
627 /* Find an entry in the context */
628 struct sysctl_ctx_entry *
629 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
631 struct sysctl_ctx_entry *e;
633 SYSCTL_ASSERT_WLOCKED();
634 if (clist == NULL || oidp == NULL)
636 TAILQ_FOREACH(e, clist, link) {
644 * Delete an entry from the context.
645 * NOTE: this function doesn't free oidp! You have to remove it
646 * with sysctl_remove_oid().
649 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
651 struct sysctl_ctx_entry *e;
653 if (clist == NULL || oidp == NULL)
656 e = sysctl_ctx_entry_find(clist, oidp);
658 TAILQ_REMOVE(clist, e, link);
660 free(e, M_SYSCTLOID);
669 * Remove dynamically created sysctl trees.
670 * oidp - top of the tree to be removed
671 * del - if 0 - just deregister, otherwise free up entries as well
672 * recurse - if != 0 traverse the subtree to be deleted
675 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
680 error = sysctl_remove_oid_locked(oidp, del, recurse);
686 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
687 int del, int recurse)
689 struct sysctl_oid *p, *tmp;
694 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
695 if (strcmp(p->oid_name, name) == 0) {
696 error = sysctl_remove_oid_locked(p, del, recurse);
707 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
709 struct sysctl_oid *p, *tmp;
712 SYSCTL_ASSERT_WLOCKED();
715 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
716 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
721 * WARNING: normal method to do this should be through
722 * sysctl_ctx_free(). Use recursing as the last resort
723 * method to purge your sysctl tree of leftovers...
724 * However, if some other code still references these nodes,
727 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
728 if (oidp->oid_refcnt == 1) {
729 SLIST_FOREACH_SAFE(p,
730 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
732 printf("Warning: failed attempt to "
733 "remove oid %s with child %s\n",
734 oidp->oid_name, p->oid_name);
737 error = sysctl_remove_oid_locked(p, del,
744 if (oidp->oid_refcnt > 1 ) {
747 if (oidp->oid_refcnt == 0) {
748 printf("Warning: bad oid_refcnt=%u (%s)!\n",
749 oidp->oid_refcnt, oidp->oid_name);
752 sysctl_unregister_oid(oidp);
755 * Wait for all threads running the handler to drain.
756 * This preserves the previous behavior when the
757 * sysctl lock was held across a handler invocation,
758 * and is necessary for module unload correctness.
760 while (oidp->oid_running > 0) {
761 oidp->oid_kind |= CTLFLAG_DYING;
762 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
765 free(__DECONST(char *, oidp->oid_descr),
768 free(__DECONST(char *, oidp->oid_label),
770 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
771 free(oidp, M_SYSCTLOID);
777 * Create new sysctls at run time.
778 * clist may point to a valid context initialized with sysctl_ctx_init().
781 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
782 int number, const char *name, int kind, void *arg1, intmax_t arg2,
783 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
786 struct sysctl_oid *oidp;
788 /* You have to hook up somewhere.. */
791 /* Check if the node already exists, otherwise create it */
793 oidp = sysctl_find_oidname(name, parent);
795 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
797 /* Update the context */
799 sysctl_ctx_entry_add(clist, oidp);
803 sysctl_warn_reuse(__func__, oidp);
808 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
809 oidp->oid_parent = parent;
810 SLIST_INIT(&oidp->oid_children);
811 oidp->oid_number = number;
812 oidp->oid_refcnt = 1;
813 oidp->oid_name = strdup(name, M_SYSCTLOID);
814 oidp->oid_handler = handler;
815 oidp->oid_kind = CTLFLAG_DYN | kind;
816 oidp->oid_arg1 = arg1;
817 oidp->oid_arg2 = arg2;
820 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
822 oidp->oid_label = strdup(label, M_SYSCTLOID);
823 /* Update the context, if used */
825 sysctl_ctx_entry_add(clist, oidp);
826 /* Register this oid */
827 sysctl_register_oid(oidp);
833 * Rename an existing oid.
836 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
841 newname = strdup(name, M_SYSCTLOID);
843 oldname = __DECONST(char *, oidp->oid_name);
844 oidp->oid_name = newname;
846 free(oldname, M_SYSCTLOID);
850 * Reparent an existing oid.
853 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
855 struct sysctl_oid *oidp;
858 if (oid->oid_parent == parent) {
862 oidp = sysctl_find_oidname(oid->oid_name, parent);
867 sysctl_unregister_oid(oid);
868 oid->oid_parent = parent;
869 oid->oid_number = OID_AUTO;
870 sysctl_register_oid(oid);
876 * Register the kernel's oids on startup.
878 SET_DECLARE(sysctl_set, struct sysctl_oid);
881 sysctl_register_all(void *arg)
883 struct sysctl_oid **oidp;
885 sx_init(&sysctlmemlock, "sysctl mem");
888 SET_FOREACH(oidp, sysctl_set)
889 sysctl_register_oid(*oidp);
892 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
897 * These functions implement a presently undocumented interface
898 * used by the sysctl program to walk the tree, and get the type
899 * so it can print the value.
900 * This interface is under work and consideration, and should probably
901 * be killed with a big axe by the first person who can find the time.
902 * (be aware though, that the proper interface isn't as obvious as it
903 * may seem, there are various conflicting requirements.
905 * {0,0} printf the entire MIB-tree.
906 * {0,1,...} return the name of the "..." OID.
907 * {0,2,...} return the next OID.
908 * {0,3} return the OID of the name in "new"
909 * {0,4,...} return the kind & format info for the "..." OID.
910 * {0,5,...} return the description of the "..." OID.
911 * {0,6,...} return the aggregation label of the "..." OID.
916 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
919 struct sysctl_oid *oidp;
921 SYSCTL_ASSERT_LOCKED();
922 SLIST_FOREACH(oidp, l, oid_link) {
927 printf("%d %s ", oidp->oid_number, oidp->oid_name);
930 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
931 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
933 if (oidp->oid_handler)
936 switch (oidp->oid_kind & CTLTYPE) {
939 if (!oidp->oid_handler) {
940 sysctl_sysctl_debug_dump_node(
941 SYSCTL_CHILDREN(oidp), i + 2);
944 case CTLTYPE_INT: printf(" Int\n"); break;
945 case CTLTYPE_UINT: printf(" u_int\n"); break;
946 case CTLTYPE_LONG: printf(" Long\n"); break;
947 case CTLTYPE_ULONG: printf(" u_long\n"); break;
948 case CTLTYPE_STRING: printf(" String\n"); break;
949 case CTLTYPE_S8: printf(" int8_t\n"); break;
950 case CTLTYPE_S16: printf(" int16_t\n"); break;
951 case CTLTYPE_S32: printf(" int32_t\n"); break;
952 case CTLTYPE_S64: printf(" int64_t\n"); break;
953 case CTLTYPE_U8: printf(" uint8_t\n"); break;
954 case CTLTYPE_U16: printf(" uint16_t\n"); break;
955 case CTLTYPE_U32: printf(" uint32_t\n"); break;
956 case CTLTYPE_U64: printf(" uint64_t\n"); break;
957 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
958 default: printf("\n");
965 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
967 struct rm_priotracker tracker;
970 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
973 SYSCTL_RLOCK(&tracker);
974 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
975 SYSCTL_RUNLOCK(&tracker);
979 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
980 0, 0, sysctl_sysctl_debug, "-", "");
984 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
986 int *name = (int *) arg1;
987 u_int namelen = arg2;
989 struct sysctl_oid *oid;
990 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
991 struct rm_priotracker tracker;
994 SYSCTL_RLOCK(&tracker);
997 snprintf(buf,sizeof(buf),"%d",*name);
999 error = SYSCTL_OUT(req, ".", 1);
1001 error = SYSCTL_OUT(req, buf, strlen(buf));
1009 SLIST_FOREACH(oid, lsp, oid_link) {
1010 if (oid->oid_number != *name)
1014 error = SYSCTL_OUT(req, ".", 1);
1016 error = SYSCTL_OUT(req, oid->oid_name,
1017 strlen(oid->oid_name));
1024 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1027 if (oid->oid_handler)
1030 lsp2 = SYSCTL_CHILDREN(oid);
1035 error = SYSCTL_OUT(req, "", 1);
1037 SYSCTL_RUNLOCK(&tracker);
1042 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
1045 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1046 sysctl_sysctl_name, "");
1049 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
1050 int *next, int *len, int level, struct sysctl_oid **oidpp)
1052 struct sysctl_oid *oidp;
1054 SYSCTL_ASSERT_LOCKED();
1056 SLIST_FOREACH(oidp, lsp, oid_link) {
1057 *next = oidp->oid_number;
1060 if (oidp->oid_kind & CTLFLAG_SKIP)
1064 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1066 if (oidp->oid_handler)
1067 /* We really should call the handler here...*/
1069 lsp = SYSCTL_CHILDREN(oidp);
1070 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1071 len, level+1, oidpp))
1076 if (oidp->oid_number < *name)
1079 if (oidp->oid_number > *name) {
1080 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1082 if (oidp->oid_handler)
1084 lsp = SYSCTL_CHILDREN(oidp);
1085 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1086 next+1, len, level+1, oidpp))
1090 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1093 if (oidp->oid_handler)
1096 lsp = SYSCTL_CHILDREN(oidp);
1097 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1098 len, level+1, oidpp))
1109 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1111 int *name = (int *) arg1;
1112 u_int namelen = arg2;
1114 struct sysctl_oid *oid;
1115 struct sysctl_oid_list *lsp = &sysctl__children;
1116 struct rm_priotracker tracker;
1117 int newoid[CTL_MAXNAME];
1119 SYSCTL_RLOCK(&tracker);
1120 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1121 SYSCTL_RUNLOCK(&tracker);
1124 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1129 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1132 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1133 sysctl_sysctl_next, "");
1136 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1138 struct sysctl_oid *oidp;
1139 struct sysctl_oid_list *lsp = &sysctl__children;
1142 SYSCTL_ASSERT_LOCKED();
1144 for (*len = 0; *len < CTL_MAXNAME;) {
1145 p = strsep(&name, ".");
1147 oidp = SLIST_FIRST(lsp);
1148 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1151 if (strcmp(p, oidp->oid_name) == 0)
1154 *oid++ = oidp->oid_number;
1157 if (name == NULL || *name == '\0') {
1163 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1166 if (oidp->oid_handler)
1169 lsp = SYSCTL_CHILDREN(oidp);
1175 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1178 int error, oid[CTL_MAXNAME], len = 0;
1179 struct sysctl_oid *op = NULL;
1180 struct rm_priotracker tracker;
1184 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1185 return (ENAMETOOLONG);
1187 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1189 error = SYSCTL_IN(req, p, req->newlen);
1195 p [req->newlen] = '\0';
1197 SYSCTL_RLOCK(&tracker);
1198 error = name2oid(p, oid, &len, &op);
1199 SYSCTL_RUNLOCK(&tracker);
1206 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1211 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1214 SYSCTL_PROC(_sysctl, 3, name2oid,
1215 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1216 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1219 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1221 struct sysctl_oid *oid;
1222 struct rm_priotracker tracker;
1225 SYSCTL_RLOCK(&tracker);
1226 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1230 if (oid->oid_fmt == NULL) {
1234 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1237 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1239 SYSCTL_RUNLOCK(&tracker);
1244 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1245 sysctl_sysctl_oidfmt, "");
1248 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1250 struct sysctl_oid *oid;
1251 struct rm_priotracker tracker;
1254 SYSCTL_RLOCK(&tracker);
1255 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1259 if (oid->oid_descr == NULL) {
1263 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1265 SYSCTL_RUNLOCK(&tracker);
1269 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1270 sysctl_sysctl_oiddescr, "");
1273 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
1275 struct sysctl_oid *oid;
1276 struct rm_priotracker tracker;
1279 SYSCTL_RLOCK(&tracker);
1280 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1284 if (oid->oid_label == NULL) {
1288 error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
1290 SYSCTL_RUNLOCK(&tracker);
1294 static SYSCTL_NODE(_sysctl, 6, oidlabel,
1295 CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
1298 * Default "handler" functions.
1304 * a variable: point arg1 at it.
1305 * a constant: pass it in arg2.
1309 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1315 * Attempt to get a coherent snapshot by making a copy of the data.
1318 temp = *(bool *)arg1 ? 1 : 0;
1320 temp = arg2 ? 1 : 0;
1322 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1323 if (error || !req->newptr)
1329 error = SYSCTL_IN(req, &temp, sizeof(temp));
1331 *(bool *)arg1 = temp ? 1 : 0;
1337 * Handle an int8_t, signed or unsigned.
1339 * a variable: point arg1 at it.
1340 * a constant: pass it in arg2.
1344 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1350 * Attempt to get a coherent snapshot by making a copy of the data.
1353 tmpout = *(int8_t *)arg1;
1356 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1358 if (error || !req->newptr)
1364 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1369 * Handle an int16_t, signed or unsigned.
1371 * a variable: point arg1 at it.
1372 * a constant: pass it in arg2.
1376 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1382 * Attempt to get a coherent snapshot by making a copy of the data.
1385 tmpout = *(int16_t *)arg1;
1388 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1390 if (error || !req->newptr)
1396 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1401 * Handle an int32_t, signed or unsigned.
1403 * a variable: point arg1 at it.
1404 * a constant: pass it in arg2.
1408 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1414 * Attempt to get a coherent snapshot by making a copy of the data.
1417 tmpout = *(int32_t *)arg1;
1420 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1422 if (error || !req->newptr)
1428 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1433 * Handle an int, signed or unsigned.
1435 * a variable: point arg1 at it.
1436 * a constant: pass it in arg2.
1440 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1442 int tmpout, error = 0;
1445 * Attempt to get a coherent snapshot by making a copy of the data.
1448 tmpout = *(int *)arg1;
1451 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1453 if (error || !req->newptr)
1459 error = SYSCTL_IN(req, arg1, sizeof(int));
1464 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1465 * Note: this is used by TCP.
1469 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1474 s = (int)((int64_t)tt * 1000 / hz);
1476 error = sysctl_handle_int(oidp, &s, 0, req);
1477 if (error || !req->newptr)
1480 tt = (int)((int64_t)s * hz / 1000);
1490 * Handle a long, signed or unsigned.
1492 * a variable: point arg1 at it.
1493 * a constant: pass it in arg2.
1497 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1506 * Attempt to get a coherent snapshot by making a copy of the data.
1509 tmplong = *(long *)arg1;
1513 if (req->flags & SCTL_MASK32) {
1515 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1518 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1520 if (error || !req->newptr)
1526 else if (req->flags & SCTL_MASK32) {
1527 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1528 *(long *)arg1 = (long)tmpint;
1532 error = SYSCTL_IN(req, arg1, sizeof(long));
1537 * Handle a 64 bit int, signed or unsigned.
1539 * a variable: point arg1 at it.
1540 * a constant: pass it in arg2.
1543 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1549 * Attempt to get a coherent snapshot by making a copy of the data.
1552 tmpout = *(uint64_t *)arg1;
1555 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1557 if (error || !req->newptr)
1563 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1568 * Handle our generic '\0' terminated 'C' string.
1570 * a variable string: point arg1 at it, arg2 is max length.
1571 * a constant string: point arg1 at it, arg2 is zero.
1575 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1578 int error = 0, ro_string = 0;
1581 * A zero-length buffer indicates a fixed size read-only
1585 arg2 = strlen((char *)arg1) + 1;
1589 if (req->oldptr != NULL) {
1595 /* try to make a coherent snapshot of the string */
1596 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1597 memcpy(tmparg, arg1, arg2);
1600 outlen = strnlen(tmparg, arg2 - 1) + 1;
1601 error = SYSCTL_OUT(req, tmparg, outlen);
1604 free(tmparg, M_SYSCTLTMP);
1606 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1607 error = SYSCTL_OUT(req, NULL, outlen);
1609 if (error || !req->newptr)
1612 if ((req->newlen - req->newidx) >= arg2) {
1615 arg2 = (req->newlen - req->newidx);
1616 error = SYSCTL_IN(req, arg1, arg2);
1617 ((char *)arg1)[arg2] = '\0';
1623 * Handle any kind of opaque data.
1624 * arg1 points to it, arg2 is the size.
1628 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1632 struct sysctl_req req2;
1635 * Attempt to get a coherent snapshot, by using the thread
1636 * pre-emption counter updated from within mi_switch() to
1637 * determine if we were pre-empted during a bcopy() or
1638 * copyout(). Make 3 attempts at doing this before giving up.
1639 * If we encounter an error, stop immediately.
1644 generation = curthread->td_generation;
1645 error = SYSCTL_OUT(req, arg1, arg2);
1649 if (generation != curthread->td_generation && tries < 3) {
1654 error = SYSCTL_IN(req, arg1, arg2);
1660 * Transfer functions to/from kernel space.
1661 * XXX: rather untested at this point
1664 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1670 if (req->oldlen <= req->oldidx)
1673 if (i > req->oldlen - req->oldidx)
1674 i = req->oldlen - req->oldidx;
1676 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1679 if (req->oldptr && i != l)
1685 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1689 if (req->newlen - req->newidx < l)
1691 bcopy((char *)req->newptr + req->newidx, p, l);
1697 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1698 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1701 struct sysctl_req req;
1703 bzero(&req, sizeof req);
1709 req.oldlen = *oldlenp;
1711 req.validlen = req.oldlen;
1718 req.newlen = newlen;
1722 req.oldfunc = sysctl_old_kernel;
1723 req.newfunc = sysctl_new_kernel;
1724 req.lock = REQ_UNWIRED;
1726 error = sysctl_root(0, name, namelen, &req);
1728 if (req.lock == REQ_WIRED && req.validlen > 0)
1729 vsunlock(req.oldptr, req.validlen);
1731 if (error && error != ENOMEM)
1735 if (req.oldptr && req.oldidx > req.validlen)
1736 *retval = req.validlen;
1738 *retval = req.oldidx;
1744 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1745 void *new, size_t newlen, size_t *retval, int flags)
1747 int oid[CTL_MAXNAME];
1748 size_t oidlen, plen;
1751 oid[0] = 0; /* sysctl internal magic */
1752 oid[1] = 3; /* name2oid */
1753 oidlen = sizeof(oid);
1755 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1756 (void *)name, strlen(name), &plen, flags);
1760 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1761 new, newlen, retval, flags);
1766 * Transfer function to/from user space.
1769 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1771 size_t i, len, origidx;
1774 origidx = req->oldidx;
1776 if (req->oldptr == NULL)
1779 * If we have not wired the user supplied buffer and we are currently
1780 * holding locks, drop a witness warning, as it's possible that
1781 * write operations to the user page can sleep.
1783 if (req->lock != REQ_WIRED)
1784 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1785 "sysctl_old_user()");
1787 len = req->validlen;
1791 if (i > len - origidx)
1793 if (req->lock == REQ_WIRED) {
1794 error = copyout_nofault(p, (char *)req->oldptr +
1797 error = copyout(p, (char *)req->oldptr + origidx, i);
1807 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1813 if (req->newlen - req->newidx < l)
1815 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1816 "sysctl_new_user()");
1817 error = copyin((char *)req->newptr + req->newidx, p, l);
1823 * Wire the user space destination buffer. If set to a value greater than
1824 * zero, the len parameter limits the maximum amount of wired memory.
1827 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1832 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1834 if (req->lock != REQ_WIRED && req->oldptr &&
1835 req->oldfunc == sysctl_old_user) {
1836 if (wiredlen != 0) {
1837 ret = vslock(req->oldptr, wiredlen);
1844 req->lock = REQ_WIRED;
1845 req->validlen = wiredlen;
1851 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1852 int *nindx, struct sysctl_req *req)
1854 struct sysctl_oid_list *lsp;
1855 struct sysctl_oid *oid;
1858 SYSCTL_ASSERT_LOCKED();
1859 lsp = &sysctl__children;
1861 while (indx < CTL_MAXNAME) {
1862 SLIST_FOREACH(oid, lsp, oid_link) {
1863 if (oid->oid_number == name[indx])
1870 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1871 if (oid->oid_handler != NULL || indx == namelen) {
1875 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1876 ("%s found DYING node %p", __func__, oid));
1879 lsp = SYSCTL_CHILDREN(oid);
1880 } else if (indx == namelen) {
1884 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1885 ("%s found DYING node %p", __func__, oid));
1895 * Traverse our tree, and find the right node, execute whatever it points
1896 * to, and return the resulting error code.
1900 sysctl_root(SYSCTL_HANDLER_ARGS)
1902 struct sysctl_oid *oid;
1903 struct rm_priotracker tracker;
1904 int error, indx, lvl;
1906 SYSCTL_RLOCK(&tracker);
1908 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1912 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1914 * You can't call a sysctl when it's a node, but has
1915 * no handler. Inform the user that it's a node.
1916 * The indx may or may not be the same as namelen.
1918 if (oid->oid_handler == NULL) {
1924 /* Is this sysctl writable? */
1925 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1930 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1932 #ifdef CAPABILITY_MODE
1934 * If the process is in capability mode, then don't permit reading or
1935 * writing unless specifically granted for the node.
1937 if (IN_CAPABILITY_MODE(req->td)) {
1938 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1939 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1946 /* Is this sysctl sensitive to securelevels? */
1947 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1948 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1949 error = securelevel_gt(req->td->td_ucred, lvl);
1954 /* Is this sysctl writable by only privileged users? */
1955 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1958 if (oid->oid_kind & CTLFLAG_PRISON)
1959 priv = PRIV_SYSCTL_WRITEJAIL;
1961 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1962 prison_owns_vnet(req->td->td_ucred))
1963 priv = PRIV_SYSCTL_WRITEJAIL;
1966 priv = PRIV_SYSCTL_WRITE;
1967 error = priv_check(req->td, priv);
1972 if (!oid->oid_handler) {
1977 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1978 arg1 = (int *)arg1 + indx;
1981 arg1 = oid->oid_arg1;
1982 arg2 = oid->oid_arg2;
1985 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1991 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1992 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1994 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
1997 SYSCTL_RUNLOCK(&tracker);
2001 #ifndef _SYS_SYSPROTO_H_
2002 struct sysctl_args {
2012 sys___sysctl(struct thread *td, struct sysctl_args *uap)
2014 int error, i, name[CTL_MAXNAME];
2017 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2020 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
2024 error = userland_sysctl(td, name, uap->namelen,
2025 uap->old, uap->oldlenp, 0,
2026 uap->new, uap->newlen, &j, 0);
2027 if (error && error != ENOMEM)
2030 i = copyout(&j, uap->oldlenp, sizeof(j));
2038 * This is used from various compatibility syscalls too. That's why name
2039 * must be in kernel space.
2042 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
2043 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
2046 int error = 0, memlocked;
2047 struct sysctl_req req;
2049 bzero(&req, sizeof req);
2056 req.oldlen = *oldlenp;
2058 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
2063 req.validlen = req.oldlen;
2066 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
2072 if (!useracc(new, newlen, VM_PROT_READ))
2074 req.newlen = newlen;
2078 req.oldfunc = sysctl_old_user;
2079 req.newfunc = sysctl_new_user;
2080 req.lock = REQ_UNWIRED;
2083 if (KTRPOINT(curthread, KTR_SYSCTL))
2084 ktrsysctl(name, namelen);
2087 if (req.oldptr && req.oldlen > PAGE_SIZE) {
2089 sx_xlock(&sysctlmemlock);
2092 CURVNET_SET(TD_TO_VNET(td));
2097 error = sysctl_root(0, name, namelen, &req);
2098 if (error != EAGAIN)
2100 kern_yield(PRI_USER);
2105 if (req.lock == REQ_WIRED && req.validlen > 0)
2106 vsunlock(req.oldptr, req.validlen);
2108 sx_xunlock(&sysctlmemlock);
2110 if (error && error != ENOMEM)
2114 if (req.oldptr && req.oldidx > req.validlen)
2115 *retval = req.validlen;
2117 *retval = req.oldidx;
2123 * Drain into a sysctl struct. The user buffer should be wired if a page
2124 * fault would cause issue.
2127 sbuf_sysctl_drain(void *arg, const char *data, int len)
2129 struct sysctl_req *req = arg;
2132 error = SYSCTL_OUT(req, data, len);
2133 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2134 return (error == 0 ? len : -error);
2138 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2139 struct sysctl_req *req)
2142 /* Supply a default buffer size if none given. */
2143 if (buf == NULL && length == 0)
2145 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2146 sbuf_set_drain(s, sbuf_sysctl_drain, req);