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 * 4. 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>
59 #include <sys/sysproto.h>
62 #include <sys/ktrace.h>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_extern.h>
72 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
73 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
74 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
77 * The sysctllock protects the MIB tree. It also protects sysctl
78 * contexts used with dynamic sysctls. The sysctl_register_oid() and
79 * sysctl_unregister_oid() routines require the sysctllock to already
80 * be held, so the sysctl_xlock() and sysctl_xunlock() routines are
81 * provided for the few places in the kernel which need to use that
82 * API rather than using the dynamic API. Use of the dynamic API is
83 * strongly encouraged for most code.
85 * The sysctlmemlock is used to limit the amount of user memory wired for
86 * sysctl requests. This is implemented by serializing any userland
87 * sysctl requests larger than a single page via an exclusive lock.
89 static struct sx sysctllock;
90 static struct sx sysctlmemlock;
92 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
93 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
94 #define SYSCTL_SLOCK() sx_slock(&sysctllock)
95 #define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock)
96 #define SYSCTL_XLOCKED() sx_xlocked(&sysctllock)
97 #define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED)
98 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
99 #define SYSCTL_ASSERT_SLOCKED() sx_assert(&sysctllock, SA_SLOCKED)
100 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
101 #define SYSCTL_SLEEP(ch, wmesg, timo) \
102 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
104 static int sysctl_root(SYSCTL_HANDLER_ARGS);
107 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
109 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
111 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
112 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
115 sysctl_lock(bool xlock)
129 xlocked = SYSCTL_XLOCKED();
137 static struct sysctl_oid *
138 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
140 struct sysctl_oid *oidp;
142 SYSCTL_ASSERT_LOCKED();
143 SLIST_FOREACH(oidp, list, oid_link) {
144 if (strcmp(oidp->oid_name, name) == 0) {
152 * Initialization of the MIB tree.
154 * Order by number in each list.
171 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2,
172 struct sysctl_req *req)
177 if (oid->oid_kind & CTLFLAG_DYN)
178 atomic_add_int(&oid->oid_running, 1);
179 xlocked = sysctl_unlock();
181 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
183 error = oid->oid_handler(oid, arg1, arg2, req);
184 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
187 sysctl_lock(xlocked);
188 if (oid->oid_kind & CTLFLAG_DYN) {
189 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
190 (oid->oid_kind & CTLFLAG_DYING) != 0)
191 wakeup(&oid->oid_running);
198 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
200 struct sysctl_req req;
201 struct sysctl_oid *curr;
204 ssize_t rem = sizeof(path);
214 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
215 len = strlen(curr->oid_name);
220 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
223 memcpy(path + rem, curr->oid_name, len);
225 path[rem + len] = '.';
228 memset(&req, 0, sizeof(req));
231 req.oldfunc = sysctl_old_kernel;
232 req.newfunc = sysctl_new_kernel;
233 req.lock = REQ_UNWIRED;
235 switch (oidp->oid_kind & CTLTYPE) {
237 if (getenv_int(path + rem, &val_int) == 0)
239 req.newlen = sizeof(val_int);
240 req.newptr = &val_int;
243 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
245 req.newlen = sizeof(val_int);
246 req.newptr = &val_int;
249 if (getenv_long(path + rem, &val_long) == 0)
251 req.newlen = sizeof(val_long);
252 req.newptr = &val_long;
255 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
257 req.newlen = sizeof(val_long);
258 req.newptr = &val_long;
261 if (getenv_quad(path + rem, &val_quad) == 0)
264 req.newlen = sizeof(val_64);
265 req.newptr = &val_64;
268 /* XXX there is no getenv_uquad() */
269 if (getenv_quad(path + rem, &val_quad) == 0)
272 req.newlen = sizeof(val_64);
273 req.newptr = &val_64;
276 penv = kern_getenv(path + rem);
279 req.newlen = strlen(penv);
285 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
286 oidp->oid_arg2, &req);
288 printf("Setting sysctl %s failed: %d\n", path + rem, error);
294 sysctl_register_oid(struct sysctl_oid *oidp)
296 struct sysctl_oid_list *parent = oidp->oid_parent;
297 struct sysctl_oid *p;
298 struct sysctl_oid *q;
303 * First check if another oid with the same name already
304 * exists in the parent's list.
306 SYSCTL_ASSERT_XLOCKED();
307 p = sysctl_find_oidname(oidp->oid_name, parent);
309 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
313 printf("can't re-use a leaf (%s)!\n", p->oid_name);
317 /* get current OID number */
318 oid_number = oidp->oid_number;
321 #error "OID_AUTO is expected to be a negative value"
324 * Any negative OID number qualifies as OID_AUTO. Valid OID
325 * numbers should always be positive.
327 * NOTE: DO NOT change the starting value here, change it in
328 * <sys/sysctl.h>, and make sure it is at least 256 to
329 * accomodate e.g. net.inet.raw as a static sysctl node.
331 if (oid_number < 0) {
335 * By decrementing the next OID number we spend less
336 * time inserting the OIDs into a sorted list.
338 if (--newoid < CTL_AUTO_START)
345 * Insert the OID into the parent's list sorted by OID number.
349 SLIST_FOREACH(p, parent, oid_link) {
350 /* check if the current OID number is in use */
351 if (oid_number == p->oid_number) {
352 /* get the next valid OID number */
353 if (oid_number < CTL_AUTO_START ||
354 oid_number == 0x7fffffff) {
355 /* wraparound - restart */
356 oid_number = CTL_AUTO_START;
357 /* don't loop forever */
359 panic("sysctl: Out of OID numbers\n");
364 } else if (oid_number < p->oid_number)
368 /* check for non-auto OID number collision */
369 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
370 oid_number >= CTL_AUTO_START) {
371 printf("sysctl: OID number(%d) is already in use for '%s'\n",
372 oidp->oid_number, oidp->oid_name);
374 /* update the OID number, if any */
375 oidp->oid_number = oid_number;
377 SLIST_INSERT_AFTER(q, oidp, oid_link);
379 SLIST_INSERT_HEAD(parent, oidp, oid_link);
381 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
383 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
385 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
386 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
387 /* only fetch value once */
388 oidp->oid_kind |= CTLFLAG_NOFETCH;
389 /* try to fetch value from kernel environment */
390 sysctl_load_tunable_by_oid_locked(oidp);
395 sysctl_unregister_oid(struct sysctl_oid *oidp)
397 struct sysctl_oid *p;
400 SYSCTL_ASSERT_XLOCKED();
402 if (oidp->oid_number == OID_AUTO) {
405 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
407 SLIST_REMOVE(oidp->oid_parent, oidp,
408 sysctl_oid, oid_link);
416 * This can happen when a module fails to register and is
417 * being unloaded afterwards. It should not be a panic()
421 printf("%s: failed to unregister sysctl\n", __func__);
424 /* Initialize a new context to keep track of dynamically added sysctls. */
426 sysctl_ctx_init(struct sysctl_ctx_list *c)
434 * No locking here, the caller is responsible for not adding
435 * new nodes to a context until after this function has
442 /* Free the context, and destroy all dynamic oids registered in this context */
444 sysctl_ctx_free(struct sysctl_ctx_list *clist)
446 struct sysctl_ctx_entry *e, *e1;
451 * First perform a "dry run" to check if it's ok to remove oids.
453 * XXX This algorithm is a hack. But I don't know any
454 * XXX better solution for now...
457 TAILQ_FOREACH(e, clist, link) {
458 error = sysctl_remove_oid_locked(e->entry, 0, 0);
463 * Restore deregistered entries, either from the end,
464 * or from the place where error occured.
465 * e contains the entry that was not unregistered
468 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
470 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
472 sysctl_register_oid(e1->entry);
473 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
479 /* Now really delete the entries */
480 e = TAILQ_FIRST(clist);
482 e1 = TAILQ_NEXT(e, link);
483 error = sysctl_remove_oid_locked(e->entry, 1, 0);
485 panic("sysctl_remove_oid: corrupt tree, entry: %s",
487 free(e, M_SYSCTLOID);
494 /* Add an entry to the context */
495 struct sysctl_ctx_entry *
496 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
498 struct sysctl_ctx_entry *e;
500 SYSCTL_ASSERT_XLOCKED();
501 if (clist == NULL || oidp == NULL)
503 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
505 TAILQ_INSERT_HEAD(clist, e, link);
509 /* Find an entry in the context */
510 struct sysctl_ctx_entry *
511 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
513 struct sysctl_ctx_entry *e;
515 SYSCTL_ASSERT_XLOCKED();
516 if (clist == NULL || oidp == NULL)
518 TAILQ_FOREACH(e, clist, link) {
526 * Delete an entry from the context.
527 * NOTE: this function doesn't free oidp! You have to remove it
528 * with sysctl_remove_oid().
531 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
533 struct sysctl_ctx_entry *e;
535 if (clist == NULL || oidp == NULL)
538 e = sysctl_ctx_entry_find(clist, oidp);
540 TAILQ_REMOVE(clist, e, link);
542 free(e, M_SYSCTLOID);
551 * Remove dynamically created sysctl trees.
552 * oidp - top of the tree to be removed
553 * del - if 0 - just deregister, otherwise free up entries as well
554 * recurse - if != 0 traverse the subtree to be deleted
557 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
562 error = sysctl_remove_oid_locked(oidp, del, recurse);
568 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
569 int del, int recurse)
571 struct sysctl_oid *p, *tmp;
576 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
577 if (strcmp(p->oid_name, name) == 0) {
578 error = sysctl_remove_oid_locked(p, del, recurse);
589 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
591 struct sysctl_oid *p, *tmp;
594 SYSCTL_ASSERT_XLOCKED();
597 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
598 printf("can't remove non-dynamic nodes!\n");
602 * WARNING: normal method to do this should be through
603 * sysctl_ctx_free(). Use recursing as the last resort
604 * method to purge your sysctl tree of leftovers...
605 * However, if some other code still references these nodes,
608 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
609 if (oidp->oid_refcnt == 1) {
610 SLIST_FOREACH_SAFE(p,
611 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
613 printf("Warning: failed attempt to "
614 "remove oid %s with child %s\n",
615 oidp->oid_name, p->oid_name);
618 error = sysctl_remove_oid_locked(p, del,
625 if (oidp->oid_refcnt > 1 ) {
628 if (oidp->oid_refcnt == 0) {
629 printf("Warning: bad oid_refcnt=%u (%s)!\n",
630 oidp->oid_refcnt, oidp->oid_name);
633 sysctl_unregister_oid(oidp);
636 * Wait for all threads running the handler to drain.
637 * This preserves the previous behavior when the
638 * sysctl lock was held across a handler invocation,
639 * and is necessary for module unload correctness.
641 while (oidp->oid_running > 0) {
642 oidp->oid_kind |= CTLFLAG_DYING;
643 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
646 free(__DECONST(char *, oidp->oid_descr),
648 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
649 free(oidp, M_SYSCTLOID);
655 * Create new sysctls at run time.
656 * clist may point to a valid context initialized with sysctl_ctx_init().
659 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
660 int number, const char *name, int kind, void *arg1, intptr_t arg2,
661 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
663 struct sysctl_oid *oidp;
665 /* You have to hook up somewhere.. */
668 /* Check if the node already exists, otherwise create it */
670 oidp = sysctl_find_oidname(name, parent);
672 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
674 /* Update the context */
676 sysctl_ctx_entry_add(clist, oidp);
681 printf("can't re-use a leaf (%s)!\n", name);
685 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
686 oidp->oid_parent = parent;
687 SLIST_INIT(&oidp->oid_children);
688 oidp->oid_number = number;
689 oidp->oid_refcnt = 1;
690 oidp->oid_name = strdup(name, M_SYSCTLOID);
691 oidp->oid_handler = handler;
692 oidp->oid_kind = CTLFLAG_DYN | kind;
693 oidp->oid_arg1 = arg1;
694 oidp->oid_arg2 = arg2;
697 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
698 /* Update the context, if used */
700 sysctl_ctx_entry_add(clist, oidp);
701 /* Register this oid */
702 sysctl_register_oid(oidp);
708 * Rename an existing oid.
711 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
716 newname = strdup(name, M_SYSCTLOID);
718 oldname = __DECONST(char *, oidp->oid_name);
719 oidp->oid_name = newname;
721 free(oldname, M_SYSCTLOID);
725 * Reparent an existing oid.
728 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
730 struct sysctl_oid *oidp;
733 if (oid->oid_parent == parent) {
737 oidp = sysctl_find_oidname(oid->oid_name, parent);
742 sysctl_unregister_oid(oid);
743 oid->oid_parent = parent;
744 oid->oid_number = OID_AUTO;
745 sysctl_register_oid(oid);
751 * Register the kernel's oids on startup.
753 SET_DECLARE(sysctl_set, struct sysctl_oid);
756 sysctl_register_all(void *arg)
758 struct sysctl_oid **oidp;
760 sx_init(&sysctlmemlock, "sysctl mem");
763 SET_FOREACH(oidp, sysctl_set)
764 sysctl_register_oid(*oidp);
767 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
772 * These functions implement a presently undocumented interface
773 * used by the sysctl program to walk the tree, and get the type
774 * so it can print the value.
775 * This interface is under work and consideration, and should probably
776 * be killed with a big axe by the first person who can find the time.
777 * (be aware though, that the proper interface isn't as obvious as it
778 * may seem, there are various conflicting requirements.
780 * {0,0} printf the entire MIB-tree.
781 * {0,1,...} return the name of the "..." OID.
782 * {0,2,...} return the next OID.
783 * {0,3} return the OID of the name in "new"
784 * {0,4,...} return the kind & format info for the "..." OID.
785 * {0,5,...} return the description the "..." OID.
790 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
793 struct sysctl_oid *oidp;
795 SYSCTL_ASSERT_LOCKED();
796 SLIST_FOREACH(oidp, l, oid_link) {
801 printf("%d %s ", oidp->oid_number, oidp->oid_name);
804 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
805 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
807 if (oidp->oid_handler)
810 switch (oidp->oid_kind & CTLTYPE) {
813 if (!oidp->oid_handler) {
814 sysctl_sysctl_debug_dump_node(
815 SYSCTL_CHILDREN(oidp), i + 2);
818 case CTLTYPE_INT: printf(" Int\n"); break;
819 case CTLTYPE_UINT: printf(" u_int\n"); break;
820 case CTLTYPE_LONG: printf(" Long\n"); break;
821 case CTLTYPE_ULONG: printf(" u_long\n"); break;
822 case CTLTYPE_STRING: printf(" String\n"); break;
823 case CTLTYPE_U64: printf(" uint64_t\n"); break;
824 case CTLTYPE_S64: printf(" int64_t\n"); break;
825 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
826 default: printf("\n");
833 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
837 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
841 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
846 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
847 0, 0, sysctl_sysctl_debug, "-", "");
851 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
853 int *name = (int *) arg1;
854 u_int namelen = arg2;
856 struct sysctl_oid *oid;
857 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
863 snprintf(buf,sizeof(buf),"%d",*name);
865 error = SYSCTL_OUT(req, ".", 1);
867 error = SYSCTL_OUT(req, buf, strlen(buf));
875 SLIST_FOREACH(oid, lsp, oid_link) {
876 if (oid->oid_number != *name)
880 error = SYSCTL_OUT(req, ".", 1);
882 error = SYSCTL_OUT(req, oid->oid_name,
883 strlen(oid->oid_name));
890 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
893 if (oid->oid_handler)
896 lsp2 = SYSCTL_CHILDREN(oid);
901 error = SYSCTL_OUT(req, "", 1);
908 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
911 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
912 sysctl_sysctl_name, "");
915 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
916 int *next, int *len, int level, struct sysctl_oid **oidpp)
918 struct sysctl_oid *oidp;
920 SYSCTL_ASSERT_LOCKED();
922 SLIST_FOREACH(oidp, lsp, oid_link) {
923 *next = oidp->oid_number;
926 if (oidp->oid_kind & CTLFLAG_SKIP)
930 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
932 if (oidp->oid_handler)
933 /* We really should call the handler here...*/
935 lsp = SYSCTL_CHILDREN(oidp);
936 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
937 len, level+1, oidpp))
942 if (oidp->oid_number < *name)
945 if (oidp->oid_number > *name) {
946 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
948 if (oidp->oid_handler)
950 lsp = SYSCTL_CHILDREN(oidp);
951 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
952 next+1, len, level+1, oidpp))
956 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
959 if (oidp->oid_handler)
962 lsp = SYSCTL_CHILDREN(oidp);
963 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
964 len, level+1, oidpp))
975 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
977 int *name = (int *) arg1;
978 u_int namelen = arg2;
980 struct sysctl_oid *oid;
981 struct sysctl_oid_list *lsp = &sysctl__children;
982 int newoid[CTL_MAXNAME];
985 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
989 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
994 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
997 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
998 sysctl_sysctl_next, "");
1001 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1003 struct sysctl_oid *oidp;
1004 struct sysctl_oid_list *lsp = &sysctl__children;
1007 SYSCTL_ASSERT_LOCKED();
1009 for (*len = 0; *len < CTL_MAXNAME;) {
1010 p = strsep(&name, ".");
1012 oidp = SLIST_FIRST(lsp);
1013 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1016 if (strcmp(p, oidp->oid_name) == 0)
1019 *oid++ = oidp->oid_number;
1022 if (name == NULL || *name == '\0') {
1028 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1031 if (oidp->oid_handler)
1034 lsp = SYSCTL_CHILDREN(oidp);
1040 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1043 int error, oid[CTL_MAXNAME], len = 0;
1044 struct sysctl_oid *op = 0;
1048 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1049 return (ENAMETOOLONG);
1051 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1053 error = SYSCTL_IN(req, p, req->newlen);
1059 p [req->newlen] = '\0';
1062 error = name2oid(p, oid, &len, &op);
1070 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1075 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1078 SYSCTL_PROC(_sysctl, 3, name2oid,
1079 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1080 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1083 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1085 struct sysctl_oid *oid;
1089 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1093 if (oid->oid_fmt == NULL) {
1097 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1100 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1107 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1108 sysctl_sysctl_oidfmt, "");
1111 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1113 struct sysctl_oid *oid;
1117 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1121 if (oid->oid_descr == NULL) {
1125 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1131 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1132 sysctl_sysctl_oiddescr, "");
1135 * Default "handler" functions.
1139 * Handle an int, signed or unsigned.
1141 * a variable: point arg1 at it.
1142 * a constant: pass it in arg2.
1146 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1148 int tmpout, error = 0;
1151 * Attempt to get a coherent snapshot by making a copy of the data.
1154 tmpout = *(int *)arg1;
1157 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1159 if (error || !req->newptr)
1165 error = SYSCTL_IN(req, arg1, sizeof(int));
1170 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1171 * Note: this is used by TCP.
1175 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1180 s = (int)((int64_t)tt * 1000 / hz);
1182 error = sysctl_handle_int(oidp, &s, 0, req);
1183 if (error || !req->newptr)
1186 tt = (int)((int64_t)s * hz / 1000);
1196 * Handle a long, signed or unsigned.
1198 * a variable: point arg1 at it.
1199 * a constant: pass it in arg2.
1203 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1212 * Attempt to get a coherent snapshot by making a copy of the data.
1215 tmplong = *(long *)arg1;
1219 if (req->flags & SCTL_MASK32) {
1221 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1224 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1226 if (error || !req->newptr)
1232 else if (req->flags & SCTL_MASK32) {
1233 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1234 *(long *)arg1 = (long)tmpint;
1238 error = SYSCTL_IN(req, arg1, sizeof(long));
1243 * Handle a 64 bit int, signed or unsigned.
1245 * a variable: point arg1 at it.
1246 * a constant: pass it in arg2.
1249 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1255 * Attempt to get a coherent snapshot by making a copy of the data.
1258 tmpout = *(uint64_t *)arg1;
1261 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1263 if (error || !req->newptr)
1269 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1274 * Handle our generic '\0' terminated 'C' string.
1276 * a variable string: point arg1 at it, arg2 is max length.
1277 * a constant string: point arg1 at it, arg2 is zero.
1281 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1284 int error = 0, ro_string = 0;
1287 * A zero-length buffer indicates a fixed size read-only
1291 arg2 = strlen((char *)arg1) + 1;
1295 if (req->oldptr != NULL) {
1301 /* try to make a coherent snapshot of the string */
1302 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1303 memcpy(tmparg, arg1, arg2);
1306 outlen = strnlen(tmparg, arg2 - 1) + 1;
1307 error = SYSCTL_OUT(req, tmparg, outlen);
1310 free(tmparg, M_SYSCTLTMP);
1312 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1313 error = SYSCTL_OUT(req, NULL, outlen);
1315 if (error || !req->newptr)
1318 if ((req->newlen - req->newidx) >= arg2) {
1321 arg2 = (req->newlen - req->newidx);
1322 error = SYSCTL_IN(req, arg1, arg2);
1323 ((char *)arg1)[arg2] = '\0';
1329 * Handle any kind of opaque data.
1330 * arg1 points to it, arg2 is the size.
1334 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1338 struct sysctl_req req2;
1341 * Attempt to get a coherent snapshot, by using the thread
1342 * pre-emption counter updated from within mi_switch() to
1343 * determine if we were pre-empted during a bcopy() or
1344 * copyout(). Make 3 attempts at doing this before giving up.
1345 * If we encounter an error, stop immediately.
1350 generation = curthread->td_generation;
1351 error = SYSCTL_OUT(req, arg1, arg2);
1355 if (generation != curthread->td_generation && tries < 3) {
1360 error = SYSCTL_IN(req, arg1, arg2);
1366 * Transfer functions to/from kernel space.
1367 * XXX: rather untested at this point
1370 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1376 if (req->oldlen <= req->oldidx)
1379 if (i > req->oldlen - req->oldidx)
1380 i = req->oldlen - req->oldidx;
1382 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1385 if (req->oldptr && i != l)
1391 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1395 if (req->newlen - req->newidx < l)
1397 bcopy((char *)req->newptr + req->newidx, p, l);
1403 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1404 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1407 struct sysctl_req req;
1409 bzero(&req, sizeof req);
1415 req.oldlen = *oldlenp;
1417 req.validlen = req.oldlen;
1424 req.newlen = newlen;
1428 req.oldfunc = sysctl_old_kernel;
1429 req.newfunc = sysctl_new_kernel;
1430 req.lock = REQ_UNWIRED;
1433 error = sysctl_root(0, name, namelen, &req);
1436 if (req.lock == REQ_WIRED && req.validlen > 0)
1437 vsunlock(req.oldptr, req.validlen);
1439 if (error && error != ENOMEM)
1443 if (req.oldptr && req.oldidx > req.validlen)
1444 *retval = req.validlen;
1446 *retval = req.oldidx;
1452 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1453 void *new, size_t newlen, size_t *retval, int flags)
1455 int oid[CTL_MAXNAME];
1456 size_t oidlen, plen;
1459 oid[0] = 0; /* sysctl internal magic */
1460 oid[1] = 3; /* name2oid */
1461 oidlen = sizeof(oid);
1463 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1464 (void *)name, strlen(name), &plen, flags);
1468 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1469 new, newlen, retval, flags);
1474 * Transfer function to/from user space.
1477 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1479 size_t i, len, origidx;
1482 origidx = req->oldidx;
1484 if (req->oldptr == NULL)
1487 * If we have not wired the user supplied buffer and we are currently
1488 * holding locks, drop a witness warning, as it's possible that
1489 * write operations to the user page can sleep.
1491 if (req->lock != REQ_WIRED)
1492 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1493 "sysctl_old_user()");
1495 len = req->validlen;
1499 if (i > len - origidx)
1501 if (req->lock == REQ_WIRED) {
1502 error = copyout_nofault(p, (char *)req->oldptr +
1505 error = copyout(p, (char *)req->oldptr + origidx, i);
1515 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1521 if (req->newlen - req->newidx < l)
1523 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1524 "sysctl_new_user()");
1525 error = copyin((char *)req->newptr + req->newidx, p, l);
1531 * Wire the user space destination buffer. If set to a value greater than
1532 * zero, the len parameter limits the maximum amount of wired memory.
1535 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1540 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1542 if (req->lock != REQ_WIRED && req->oldptr &&
1543 req->oldfunc == sysctl_old_user) {
1544 if (wiredlen != 0) {
1545 ret = vslock(req->oldptr, wiredlen);
1552 req->lock = REQ_WIRED;
1553 req->validlen = wiredlen;
1559 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1560 int *nindx, struct sysctl_req *req)
1562 struct sysctl_oid_list *lsp;
1563 struct sysctl_oid *oid;
1566 SYSCTL_ASSERT_LOCKED();
1567 lsp = &sysctl__children;
1569 while (indx < CTL_MAXNAME) {
1570 SLIST_FOREACH(oid, lsp, oid_link) {
1571 if (oid->oid_number == name[indx])
1578 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1579 if (oid->oid_handler != NULL || indx == namelen) {
1583 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1584 ("%s found DYING node %p", __func__, oid));
1587 lsp = SYSCTL_CHILDREN(oid);
1588 } else if (indx == namelen) {
1592 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1593 ("%s found DYING node %p", __func__, oid));
1603 * Traverse our tree, and find the right node, execute whatever it points
1604 * to, and return the resulting error code.
1608 sysctl_root(SYSCTL_HANDLER_ARGS)
1610 struct sysctl_oid *oid;
1611 int error, indx, lvl;
1613 SYSCTL_ASSERT_SLOCKED();
1615 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1619 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1621 * You can't call a sysctl when it's a node, but has
1622 * no handler. Inform the user that it's a node.
1623 * The indx may or may not be the same as namelen.
1625 if (oid->oid_handler == NULL)
1629 /* Is this sysctl writable? */
1630 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1633 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1635 #ifdef CAPABILITY_MODE
1637 * If the process is in capability mode, then don't permit reading or
1638 * writing unless specifically granted for the node.
1640 if (IN_CAPABILITY_MODE(req->td)) {
1641 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1643 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1648 /* Is this sysctl sensitive to securelevels? */
1649 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1650 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1651 error = securelevel_gt(req->td->td_ucred, lvl);
1656 /* Is this sysctl writable by only privileged users? */
1657 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1660 if (oid->oid_kind & CTLFLAG_PRISON)
1661 priv = PRIV_SYSCTL_WRITEJAIL;
1663 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1664 prison_owns_vnet(req->td->td_ucred))
1665 priv = PRIV_SYSCTL_WRITEJAIL;
1668 priv = PRIV_SYSCTL_WRITE;
1669 error = priv_check(req->td, priv);
1674 if (!oid->oid_handler)
1677 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1678 arg1 = (int *)arg1 + indx;
1681 arg1 = oid->oid_arg1;
1682 arg2 = oid->oid_arg2;
1685 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1691 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1692 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1694 error = sysctl_root_handler_locked(oid, arg1, arg2, req);
1696 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1701 #ifndef _SYS_SYSPROTO_H_
1702 struct sysctl_args {
1712 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1714 int error, i, name[CTL_MAXNAME];
1717 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1720 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1724 error = userland_sysctl(td, name, uap->namelen,
1725 uap->old, uap->oldlenp, 0,
1726 uap->new, uap->newlen, &j, 0);
1727 if (error && error != ENOMEM)
1730 i = copyout(&j, uap->oldlenp, sizeof(j));
1738 * This is used from various compatibility syscalls too. That's why name
1739 * must be in kernel space.
1742 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1743 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1746 int error = 0, memlocked;
1747 struct sysctl_req req;
1749 bzero(&req, sizeof req);
1756 req.oldlen = *oldlenp;
1758 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1763 req.validlen = req.oldlen;
1766 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1772 if (!useracc(new, newlen, VM_PROT_READ))
1774 req.newlen = newlen;
1778 req.oldfunc = sysctl_old_user;
1779 req.newfunc = sysctl_new_user;
1780 req.lock = REQ_UNWIRED;
1783 if (KTRPOINT(curthread, KTR_SYSCTL))
1784 ktrsysctl(name, namelen);
1787 if (req.oldlen > PAGE_SIZE) {
1789 sx_xlock(&sysctlmemlock);
1792 CURVNET_SET(TD_TO_VNET(td));
1798 error = sysctl_root(0, name, namelen, &req);
1800 if (error != EAGAIN)
1802 kern_yield(PRI_USER);
1807 if (req.lock == REQ_WIRED && req.validlen > 0)
1808 vsunlock(req.oldptr, req.validlen);
1810 sx_xunlock(&sysctlmemlock);
1812 if (error && error != ENOMEM)
1816 if (req.oldptr && req.oldidx > req.validlen)
1817 *retval = req.validlen;
1819 *retval = req.oldidx;
1825 * Drain into a sysctl struct. The user buffer should be wired if a page
1826 * fault would cause issue.
1829 sbuf_sysctl_drain(void *arg, const char *data, int len)
1831 struct sysctl_req *req = arg;
1834 error = SYSCTL_OUT(req, data, len);
1835 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1836 return (error == 0 ? len : -error);
1840 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1841 struct sysctl_req *req)
1844 /* Supply a default buffer size if none given. */
1845 if (buf == NULL && length == 0)
1847 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
1848 sbuf_set_drain(s, sbuf_sysctl_drain, req);