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 atomic_add_int(&oid->oid_running, 1);
178 xlocked = sysctl_unlock();
180 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
182 error = oid->oid_handler(oid, arg1, arg2, req);
183 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
186 sysctl_lock(xlocked);
187 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
188 (oid->oid_kind & CTLFLAG_DYING) != 0)
189 wakeup(&oid->oid_running);
195 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
197 struct sysctl_req req;
198 struct sysctl_oid *curr;
201 ssize_t rem = sizeof(path);
211 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
212 len = strlen(curr->oid_name);
217 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
220 memcpy(path + rem, curr->oid_name, len);
222 path[rem + len] = '.';
225 memset(&req, 0, sizeof(req));
228 req.oldfunc = sysctl_old_kernel;
229 req.newfunc = sysctl_new_kernel;
230 req.lock = REQ_UNWIRED;
232 switch (oidp->oid_kind & CTLTYPE) {
234 if (getenv_int(path + rem, &val_int) == 0)
236 req.newlen = sizeof(val_int);
237 req.newptr = &val_int;
240 if (getenv_uint(path + rem, (unsigned int *)&val_int) == 0)
242 req.newlen = sizeof(val_int);
243 req.newptr = &val_int;
246 if (getenv_long(path + rem, &val_long) == 0)
248 req.newlen = sizeof(val_long);
249 req.newptr = &val_long;
252 if (getenv_ulong(path + rem, (unsigned long *)&val_long) == 0)
254 req.newlen = sizeof(val_long);
255 req.newptr = &val_long;
258 if (getenv_quad(path + rem, &val_quad) == 0)
261 req.newlen = sizeof(val_64);
262 req.newptr = &val_64;
265 /* XXX there is no getenv_uquad() */
266 if (getenv_quad(path + rem, &val_quad) == 0)
269 req.newlen = sizeof(val_64);
270 req.newptr = &val_64;
273 penv = kern_getenv(path + rem);
276 req.newlen = strlen(penv);
282 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
283 oidp->oid_arg2, &req);
285 printf("Setting sysctl %s failed: %d\n", path, error);
291 sysctl_register_oid(struct sysctl_oid *oidp)
293 struct sysctl_oid_list *parent = oidp->oid_parent;
294 struct sysctl_oid *p;
295 struct sysctl_oid *q;
298 * First check if another oid with the same name already
299 * exists in the parent's list.
301 SYSCTL_ASSERT_XLOCKED();
302 p = sysctl_find_oidname(oidp->oid_name, parent);
304 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
308 printf("can't re-use a leaf (%s)!\n", p->oid_name);
313 * If this oid has a number OID_AUTO, give it a number which
314 * is greater than any current oid.
315 * NOTE: DO NOT change the starting value here, change it in
316 * <sys/sysctl.h>, and make sure it is at least 256 to
317 * accomodate e.g. net.inet.raw as a static sysctl node.
319 if (oidp->oid_number == OID_AUTO) {
320 static int newoid = CTL_AUTO_START;
322 oidp->oid_number = newoid++;
323 if (newoid == 0x7fffffff)
324 panic("out of oids");
327 else if (oidp->oid_number >= CTL_AUTO_START) {
328 /* do not panic; this happens when unregistering sysctl sets */
329 printf("static sysctl oid too high: %d", oidp->oid_number);
334 * Insert the oid into the parent's list in order.
337 SLIST_FOREACH(p, parent, oid_link) {
338 if (oidp->oid_number < p->oid_number)
343 SLIST_INSERT_AFTER(q, oidp, oid_link);
345 SLIST_INSERT_HEAD(parent, oidp, oid_link);
347 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
349 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
351 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
352 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
353 sysctl_load_tunable_by_oid_locked(oidp);
358 sysctl_unregister_oid(struct sysctl_oid *oidp)
360 struct sysctl_oid *p;
363 SYSCTL_ASSERT_XLOCKED();
365 if (oidp->oid_number == OID_AUTO) {
368 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
370 SLIST_REMOVE(oidp->oid_parent, oidp,
371 sysctl_oid, oid_link);
379 * This can happen when a module fails to register and is
380 * being unloaded afterwards. It should not be a panic()
384 printf("%s: failed to unregister sysctl\n", __func__);
387 /* Initialize a new context to keep track of dynamically added sysctls. */
389 sysctl_ctx_init(struct sysctl_ctx_list *c)
397 * No locking here, the caller is responsible for not adding
398 * new nodes to a context until after this function has
405 /* Free the context, and destroy all dynamic oids registered in this context */
407 sysctl_ctx_free(struct sysctl_ctx_list *clist)
409 struct sysctl_ctx_entry *e, *e1;
414 * First perform a "dry run" to check if it's ok to remove oids.
416 * XXX This algorithm is a hack. But I don't know any
417 * XXX better solution for now...
420 TAILQ_FOREACH(e, clist, link) {
421 error = sysctl_remove_oid_locked(e->entry, 0, 0);
426 * Restore deregistered entries, either from the end,
427 * or from the place where error occured.
428 * e contains the entry that was not unregistered
431 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
433 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
435 sysctl_register_oid(e1->entry);
436 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
442 /* Now really delete the entries */
443 e = TAILQ_FIRST(clist);
445 e1 = TAILQ_NEXT(e, link);
446 error = sysctl_remove_oid_locked(e->entry, 1, 0);
448 panic("sysctl_remove_oid: corrupt tree, entry: %s",
450 free(e, M_SYSCTLOID);
457 /* Add an entry to the context */
458 struct sysctl_ctx_entry *
459 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
461 struct sysctl_ctx_entry *e;
463 SYSCTL_ASSERT_XLOCKED();
464 if (clist == NULL || oidp == NULL)
466 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
468 TAILQ_INSERT_HEAD(clist, e, link);
472 /* Find an entry in the context */
473 struct sysctl_ctx_entry *
474 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
476 struct sysctl_ctx_entry *e;
478 SYSCTL_ASSERT_XLOCKED();
479 if (clist == NULL || oidp == NULL)
481 TAILQ_FOREACH(e, clist, link) {
489 * Delete an entry from the context.
490 * NOTE: this function doesn't free oidp! You have to remove it
491 * with sysctl_remove_oid().
494 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
496 struct sysctl_ctx_entry *e;
498 if (clist == NULL || oidp == NULL)
501 e = sysctl_ctx_entry_find(clist, oidp);
503 TAILQ_REMOVE(clist, e, link);
505 free(e, M_SYSCTLOID);
514 * Remove dynamically created sysctl trees.
515 * oidp - top of the tree to be removed
516 * del - if 0 - just deregister, otherwise free up entries as well
517 * recurse - if != 0 traverse the subtree to be deleted
520 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
525 error = sysctl_remove_oid_locked(oidp, del, recurse);
531 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
532 int del, int recurse)
534 struct sysctl_oid *p, *tmp;
539 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
540 if (strcmp(p->oid_name, name) == 0) {
541 error = sysctl_remove_oid_locked(p, del, recurse);
552 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
554 struct sysctl_oid *p, *tmp;
557 SYSCTL_ASSERT_XLOCKED();
560 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
561 printf("can't remove non-dynamic nodes!\n");
565 * WARNING: normal method to do this should be through
566 * sysctl_ctx_free(). Use recursing as the last resort
567 * method to purge your sysctl tree of leftovers...
568 * However, if some other code still references these nodes,
571 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
572 if (oidp->oid_refcnt == 1) {
573 SLIST_FOREACH_SAFE(p,
574 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
576 printf("Warning: failed attempt to "
577 "remove oid %s with child %s\n",
578 oidp->oid_name, p->oid_name);
581 error = sysctl_remove_oid_locked(p, del,
588 if (oidp->oid_refcnt > 1 ) {
591 if (oidp->oid_refcnt == 0) {
592 printf("Warning: bad oid_refcnt=%u (%s)!\n",
593 oidp->oid_refcnt, oidp->oid_name);
596 sysctl_unregister_oid(oidp);
599 * Wait for all threads running the handler to drain.
600 * This preserves the previous behavior when the
601 * sysctl lock was held across a handler invocation,
602 * and is necessary for module unload correctness.
604 while (oidp->oid_running > 0) {
605 oidp->oid_kind |= CTLFLAG_DYING;
606 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
609 free(__DECONST(char *, oidp->oid_descr),
611 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
612 free(oidp, M_SYSCTLOID);
618 * Create new sysctls at run time.
619 * clist may point to a valid context initialized with sysctl_ctx_init().
622 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
623 int number, const char *name, int kind, void *arg1, intptr_t arg2,
624 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
626 struct sysctl_oid *oidp;
628 /* You have to hook up somewhere.. */
631 /* Check if the node already exists, otherwise create it */
633 oidp = sysctl_find_oidname(name, parent);
635 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
637 /* Update the context */
639 sysctl_ctx_entry_add(clist, oidp);
644 printf("can't re-use a leaf (%s)!\n", name);
648 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
649 oidp->oid_parent = parent;
650 SLIST_INIT(&oidp->oid_children);
651 oidp->oid_number = number;
652 oidp->oid_refcnt = 1;
653 oidp->oid_name = strdup(name, M_SYSCTLOID);
654 oidp->oid_handler = handler;
655 oidp->oid_kind = CTLFLAG_DYN | kind;
656 oidp->oid_arg1 = arg1;
657 oidp->oid_arg2 = arg2;
660 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
661 /* Update the context, if used */
663 sysctl_ctx_entry_add(clist, oidp);
664 /* Register this oid */
665 sysctl_register_oid(oidp);
671 * Rename an existing oid.
674 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
679 newname = strdup(name, M_SYSCTLOID);
681 oldname = __DECONST(char *, oidp->oid_name);
682 oidp->oid_name = newname;
684 free(oldname, M_SYSCTLOID);
688 * Reparent an existing oid.
691 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
693 struct sysctl_oid *oidp;
696 if (oid->oid_parent == parent) {
700 oidp = sysctl_find_oidname(oid->oid_name, parent);
705 sysctl_unregister_oid(oid);
706 oid->oid_parent = parent;
707 oid->oid_number = OID_AUTO;
708 sysctl_register_oid(oid);
714 * Register the kernel's oids on startup.
716 SET_DECLARE(sysctl_set, struct sysctl_oid);
719 sysctl_register_all(void *arg)
721 struct sysctl_oid **oidp;
723 sx_init(&sysctlmemlock, "sysctl mem");
726 SET_FOREACH(oidp, sysctl_set)
727 sysctl_register_oid(*oidp);
730 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
735 * These functions implement a presently undocumented interface
736 * used by the sysctl program to walk the tree, and get the type
737 * so it can print the value.
738 * This interface is under work and consideration, and should probably
739 * be killed with a big axe by the first person who can find the time.
740 * (be aware though, that the proper interface isn't as obvious as it
741 * may seem, there are various conflicting requirements.
743 * {0,0} printf the entire MIB-tree.
744 * {0,1,...} return the name of the "..." OID.
745 * {0,2,...} return the next OID.
746 * {0,3} return the OID of the name in "new"
747 * {0,4,...} return the kind & format info for the "..." OID.
748 * {0,5,...} return the description the "..." OID.
753 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
756 struct sysctl_oid *oidp;
758 SYSCTL_ASSERT_LOCKED();
759 SLIST_FOREACH(oidp, l, oid_link) {
764 printf("%d %s ", oidp->oid_number, oidp->oid_name);
767 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
768 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
770 if (oidp->oid_handler)
773 switch (oidp->oid_kind & CTLTYPE) {
776 if (!oidp->oid_handler) {
777 sysctl_sysctl_debug_dump_node(
778 SYSCTL_CHILDREN(oidp), i + 2);
781 case CTLTYPE_INT: printf(" Int\n"); break;
782 case CTLTYPE_UINT: printf(" u_int\n"); break;
783 case CTLTYPE_LONG: printf(" Long\n"); break;
784 case CTLTYPE_ULONG: printf(" u_long\n"); break;
785 case CTLTYPE_STRING: printf(" String\n"); break;
786 case CTLTYPE_U64: printf(" uint64_t\n"); break;
787 case CTLTYPE_S64: printf(" int64_t\n"); break;
788 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
789 default: printf("\n");
796 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
800 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
804 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
809 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
810 0, 0, sysctl_sysctl_debug, "-", "");
814 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
816 int *name = (int *) arg1;
817 u_int namelen = arg2;
819 struct sysctl_oid *oid;
820 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
826 snprintf(buf,sizeof(buf),"%d",*name);
828 error = SYSCTL_OUT(req, ".", 1);
830 error = SYSCTL_OUT(req, buf, strlen(buf));
838 SLIST_FOREACH(oid, lsp, oid_link) {
839 if (oid->oid_number != *name)
843 error = SYSCTL_OUT(req, ".", 1);
845 error = SYSCTL_OUT(req, oid->oid_name,
846 strlen(oid->oid_name));
853 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
856 if (oid->oid_handler)
859 lsp2 = SYSCTL_CHILDREN(oid);
864 error = SYSCTL_OUT(req, "", 1);
871 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
874 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
875 sysctl_sysctl_name, "");
878 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
879 int *next, int *len, int level, struct sysctl_oid **oidpp)
881 struct sysctl_oid *oidp;
883 SYSCTL_ASSERT_LOCKED();
885 SLIST_FOREACH(oidp, lsp, oid_link) {
886 *next = oidp->oid_number;
889 if (oidp->oid_kind & CTLFLAG_SKIP)
893 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
895 if (oidp->oid_handler)
896 /* We really should call the handler here...*/
898 lsp = SYSCTL_CHILDREN(oidp);
899 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
900 len, level+1, oidpp))
905 if (oidp->oid_number < *name)
908 if (oidp->oid_number > *name) {
909 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
911 if (oidp->oid_handler)
913 lsp = SYSCTL_CHILDREN(oidp);
914 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
915 next+1, len, level+1, oidpp))
919 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
922 if (oidp->oid_handler)
925 lsp = SYSCTL_CHILDREN(oidp);
926 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
927 len, level+1, oidpp))
938 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
940 int *name = (int *) arg1;
941 u_int namelen = arg2;
943 struct sysctl_oid *oid;
944 struct sysctl_oid_list *lsp = &sysctl__children;
945 int newoid[CTL_MAXNAME];
948 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
952 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
957 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
960 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
961 sysctl_sysctl_next, "");
964 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
966 struct sysctl_oid *oidp;
967 struct sysctl_oid_list *lsp = &sysctl__children;
970 SYSCTL_ASSERT_LOCKED();
972 for (*len = 0; *len < CTL_MAXNAME;) {
973 p = strsep(&name, ".");
975 oidp = SLIST_FIRST(lsp);
976 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
979 if (strcmp(p, oidp->oid_name) == 0)
982 *oid++ = oidp->oid_number;
985 if (name == NULL || *name == '\0') {
991 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
994 if (oidp->oid_handler)
997 lsp = SYSCTL_CHILDREN(oidp);
1003 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1006 int error, oid[CTL_MAXNAME], len = 0;
1007 struct sysctl_oid *op = 0;
1011 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1012 return (ENAMETOOLONG);
1014 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1016 error = SYSCTL_IN(req, p, req->newlen);
1022 p [req->newlen] = '\0';
1025 error = name2oid(p, oid, &len, &op);
1033 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1038 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1041 SYSCTL_PROC(_sysctl, 3, name2oid,
1042 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1043 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1046 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1048 struct sysctl_oid *oid;
1052 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1056 if (oid->oid_fmt == NULL) {
1060 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1063 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1070 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1071 sysctl_sysctl_oidfmt, "");
1074 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1076 struct sysctl_oid *oid;
1080 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1084 if (oid->oid_descr == NULL) {
1088 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1094 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1095 sysctl_sysctl_oiddescr, "");
1098 * Default "handler" functions.
1102 * Handle an int, signed or unsigned.
1104 * a variable: point arg1 at it.
1105 * a constant: pass it in arg2.
1109 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1111 int tmpout, error = 0;
1114 * Attempt to get a coherent snapshot by making a copy of the data.
1117 tmpout = *(int *)arg1;
1120 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1122 if (error || !req->newptr)
1128 error = SYSCTL_IN(req, arg1, sizeof(int));
1133 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1134 * Note: this is used by TCP.
1138 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1143 s = (int)((int64_t)tt * 1000 / hz);
1145 error = sysctl_handle_int(oidp, &s, 0, req);
1146 if (error || !req->newptr)
1149 tt = (int)((int64_t)s * hz / 1000);
1159 * Handle a long, signed or unsigned.
1161 * a variable: point arg1 at it.
1162 * a constant: pass it in arg2.
1166 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1175 * Attempt to get a coherent snapshot by making a copy of the data.
1178 tmplong = *(long *)arg1;
1182 if (req->flags & SCTL_MASK32) {
1184 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1187 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1189 if (error || !req->newptr)
1195 else if (req->flags & SCTL_MASK32) {
1196 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1197 *(long *)arg1 = (long)tmpint;
1201 error = SYSCTL_IN(req, arg1, sizeof(long));
1206 * Handle a 64 bit int, signed or unsigned.
1208 * a variable: point arg1 at it.
1209 * a constant: pass it in arg2.
1212 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1218 * Attempt to get a coherent snapshot by making a copy of the data.
1221 tmpout = *(uint64_t *)arg1;
1224 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1226 if (error || !req->newptr)
1232 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1237 * Handle our generic '\0' terminated 'C' string.
1239 * a variable string: point arg1 at it, arg2 is max length.
1240 * a constant string: point arg1 at it, arg2 is zero.
1244 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1247 int error = 0, ro_string = 0;
1250 * A zero-length buffer indicates a fixed size read-only
1254 arg2 = strlen((char *)arg1) + 1;
1258 if (req->oldptr != NULL) {
1264 /* try to make a coherent snapshot of the string */
1265 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1266 memcpy(tmparg, arg1, arg2);
1269 outlen = strnlen(tmparg, arg2 - 1) + 1;
1270 error = SYSCTL_OUT(req, tmparg, outlen);
1273 free(tmparg, M_SYSCTLTMP);
1275 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1276 error = SYSCTL_OUT(req, NULL, outlen);
1278 if (error || !req->newptr)
1281 if ((req->newlen - req->newidx) >= arg2) {
1284 arg2 = (req->newlen - req->newidx);
1285 error = SYSCTL_IN(req, arg1, arg2);
1286 ((char *)arg1)[arg2] = '\0';
1292 * Handle any kind of opaque data.
1293 * arg1 points to it, arg2 is the size.
1297 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1301 struct sysctl_req req2;
1304 * Attempt to get a coherent snapshot, by using the thread
1305 * pre-emption counter updated from within mi_switch() to
1306 * determine if we were pre-empted during a bcopy() or
1307 * copyout(). Make 3 attempts at doing this before giving up.
1308 * If we encounter an error, stop immediately.
1313 generation = curthread->td_generation;
1314 error = SYSCTL_OUT(req, arg1, arg2);
1318 if (generation != curthread->td_generation && tries < 3) {
1323 error = SYSCTL_IN(req, arg1, arg2);
1329 * Transfer functions to/from kernel space.
1330 * XXX: rather untested at this point
1333 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1339 if (req->oldlen <= req->oldidx)
1342 if (i > req->oldlen - req->oldidx)
1343 i = req->oldlen - req->oldidx;
1345 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1348 if (req->oldptr && i != l)
1354 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1358 if (req->newlen - req->newidx < l)
1360 bcopy((char *)req->newptr + req->newidx, p, l);
1366 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1367 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1370 struct sysctl_req req;
1372 bzero(&req, sizeof req);
1378 req.oldlen = *oldlenp;
1380 req.validlen = req.oldlen;
1387 req.newlen = newlen;
1391 req.oldfunc = sysctl_old_kernel;
1392 req.newfunc = sysctl_new_kernel;
1393 req.lock = REQ_UNWIRED;
1396 error = sysctl_root(0, name, namelen, &req);
1399 if (req.lock == REQ_WIRED && req.validlen > 0)
1400 vsunlock(req.oldptr, req.validlen);
1402 if (error && error != ENOMEM)
1406 if (req.oldptr && req.oldidx > req.validlen)
1407 *retval = req.validlen;
1409 *retval = req.oldidx;
1415 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1416 void *new, size_t newlen, size_t *retval, int flags)
1418 int oid[CTL_MAXNAME];
1419 size_t oidlen, plen;
1422 oid[0] = 0; /* sysctl internal magic */
1423 oid[1] = 3; /* name2oid */
1424 oidlen = sizeof(oid);
1426 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1427 (void *)name, strlen(name), &plen, flags);
1431 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1432 new, newlen, retval, flags);
1437 * Transfer function to/from user space.
1440 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1442 size_t i, len, origidx;
1445 origidx = req->oldidx;
1447 if (req->oldptr == NULL)
1450 * If we have not wired the user supplied buffer and we are currently
1451 * holding locks, drop a witness warning, as it's possible that
1452 * write operations to the user page can sleep.
1454 if (req->lock != REQ_WIRED)
1455 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1456 "sysctl_old_user()");
1458 len = req->validlen;
1462 if (i > len - origidx)
1464 if (req->lock == REQ_WIRED) {
1465 error = copyout_nofault(p, (char *)req->oldptr +
1468 error = copyout(p, (char *)req->oldptr + origidx, i);
1478 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1484 if (req->newlen - req->newidx < l)
1486 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1487 "sysctl_new_user()");
1488 error = copyin((char *)req->newptr + req->newidx, p, l);
1494 * Wire the user space destination buffer. If set to a value greater than
1495 * zero, the len parameter limits the maximum amount of wired memory.
1498 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1503 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1505 if (req->lock != REQ_WIRED && req->oldptr &&
1506 req->oldfunc == sysctl_old_user) {
1507 if (wiredlen != 0) {
1508 ret = vslock(req->oldptr, wiredlen);
1515 req->lock = REQ_WIRED;
1516 req->validlen = wiredlen;
1522 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1523 int *nindx, struct sysctl_req *req)
1525 struct sysctl_oid_list *lsp;
1526 struct sysctl_oid *oid;
1529 SYSCTL_ASSERT_LOCKED();
1530 lsp = &sysctl__children;
1532 while (indx < CTL_MAXNAME) {
1533 SLIST_FOREACH(oid, lsp, oid_link) {
1534 if (oid->oid_number == name[indx])
1541 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1542 if (oid->oid_handler != NULL || indx == namelen) {
1546 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1547 ("%s found DYING node %p", __func__, oid));
1550 lsp = SYSCTL_CHILDREN(oid);
1551 } else if (indx == namelen) {
1555 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1556 ("%s found DYING node %p", __func__, oid));
1566 * Traverse our tree, and find the right node, execute whatever it points
1567 * to, and return the resulting error code.
1571 sysctl_root(SYSCTL_HANDLER_ARGS)
1573 struct sysctl_oid *oid;
1574 int error, indx, lvl;
1576 SYSCTL_ASSERT_SLOCKED();
1578 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1582 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1584 * You can't call a sysctl when it's a node, but has
1585 * no handler. Inform the user that it's a node.
1586 * The indx may or may not be the same as namelen.
1588 if (oid->oid_handler == NULL)
1592 /* Is this sysctl writable? */
1593 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1596 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1598 #ifdef CAPABILITY_MODE
1600 * If the process is in capability mode, then don't permit reading or
1601 * writing unless specifically granted for the node.
1603 if (IN_CAPABILITY_MODE(req->td)) {
1604 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1606 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1611 /* Is this sysctl sensitive to securelevels? */
1612 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1613 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1614 error = securelevel_gt(req->td->td_ucred, lvl);
1619 /* Is this sysctl writable by only privileged users? */
1620 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1623 if (oid->oid_kind & CTLFLAG_PRISON)
1624 priv = PRIV_SYSCTL_WRITEJAIL;
1626 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1627 prison_owns_vnet(req->td->td_ucred))
1628 priv = PRIV_SYSCTL_WRITEJAIL;
1631 priv = PRIV_SYSCTL_WRITE;
1632 error = priv_check(req->td, priv);
1637 if (!oid->oid_handler)
1640 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1641 arg1 = (int *)arg1 + indx;
1644 arg1 = oid->oid_arg1;
1645 arg2 = oid->oid_arg2;
1648 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1654 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1655 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1657 error = sysctl_root_handler_locked(oid, arg1, arg2, req);
1659 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1664 #ifndef _SYS_SYSPROTO_H_
1665 struct sysctl_args {
1675 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1677 int error, i, name[CTL_MAXNAME];
1680 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1683 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1687 error = userland_sysctl(td, name, uap->namelen,
1688 uap->old, uap->oldlenp, 0,
1689 uap->new, uap->newlen, &j, 0);
1690 if (error && error != ENOMEM)
1693 i = copyout(&j, uap->oldlenp, sizeof(j));
1701 * This is used from various compatibility syscalls too. That's why name
1702 * must be in kernel space.
1705 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1706 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1709 int error = 0, memlocked;
1710 struct sysctl_req req;
1712 bzero(&req, sizeof req);
1719 req.oldlen = *oldlenp;
1721 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1726 req.validlen = req.oldlen;
1729 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1735 if (!useracc(new, newlen, VM_PROT_READ))
1737 req.newlen = newlen;
1741 req.oldfunc = sysctl_old_user;
1742 req.newfunc = sysctl_new_user;
1743 req.lock = REQ_UNWIRED;
1746 if (KTRPOINT(curthread, KTR_SYSCTL))
1747 ktrsysctl(name, namelen);
1750 if (req.oldlen > PAGE_SIZE) {
1752 sx_xlock(&sysctlmemlock);
1755 CURVNET_SET(TD_TO_VNET(td));
1761 error = sysctl_root(0, name, namelen, &req);
1763 if (error != EAGAIN)
1765 kern_yield(PRI_USER);
1770 if (req.lock == REQ_WIRED && req.validlen > 0)
1771 vsunlock(req.oldptr, req.validlen);
1773 sx_xunlock(&sysctlmemlock);
1775 if (error && error != ENOMEM)
1779 if (req.oldptr && req.oldidx > req.validlen)
1780 *retval = req.validlen;
1782 *retval = req.oldidx;
1788 * Drain into a sysctl struct. The user buffer should be wired if a page
1789 * fault would cause issue.
1792 sbuf_sysctl_drain(void *arg, const char *data, int len)
1794 struct sysctl_req *req = arg;
1797 error = SYSCTL_OUT(req, data, len);
1798 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1799 return (error == 0 ? len : -error);
1803 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1804 struct sysctl_req *req)
1807 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1808 sbuf_set_drain(s, sbuf_sysctl_drain, req);