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_lock() and sysctl_unlock() 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_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
95 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
96 #define SYSCTL_SLEEP(ch, wmesg, timo) \
97 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
99 static int sysctl_root(SYSCTL_HANDLER_ARGS);
101 struct sysctl_oid_list sysctl__children; /* root list */
103 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
106 static struct sysctl_oid *
107 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
109 struct sysctl_oid *oidp;
111 SYSCTL_ASSERT_XLOCKED();
112 SLIST_FOREACH(oidp, list, oid_link) {
113 if (strcmp(oidp->oid_name, name) == 0) {
121 * Initialization of the MIB tree.
123 * Order by number in each list.
140 sysctl_register_oid(struct sysctl_oid *oidp)
142 struct sysctl_oid_list *parent = oidp->oid_parent;
143 struct sysctl_oid *p;
144 struct sysctl_oid *q;
149 * First check if another oid with the same name already
150 * exists in the parent's list.
152 SYSCTL_ASSERT_XLOCKED();
153 p = sysctl_find_oidname(oidp->oid_name, parent);
155 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
159 printf("can't re-use a leaf (%s)!\n", p->oid_name);
163 /* get current OID number */
164 oid_number = oidp->oid_number;
167 #error "OID_AUTO is expected to be a negative value"
170 * Any negative OID number qualifies as OID_AUTO. Valid OID
171 * numbers should always be positive.
173 * NOTE: DO NOT change the starting value here, change it in
174 * <sys/sysctl.h>, and make sure it is at least 256 to
175 * accommodate e.g. net.inet.raw as a static sysctl node.
177 if (oid_number < 0) {
181 * By decrementing the next OID number we spend less
182 * time inserting the OIDs into a sorted list.
184 if (--newoid < CTL_AUTO_START)
191 * Insert the OID into the parent's list sorted by OID number.
195 SLIST_FOREACH(p, parent, oid_link) {
196 /* check if the current OID number is in use */
197 if (oid_number == p->oid_number) {
198 /* get the next valid OID number */
199 if (oid_number < CTL_AUTO_START ||
200 oid_number == 0x7fffffff) {
201 /* wraparound - restart */
202 oid_number = CTL_AUTO_START;
203 /* don't loop forever */
205 panic("sysctl: Out of OID numbers\n");
210 } else if (oid_number < p->oid_number)
214 /* check for non-auto OID number collision */
215 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
216 oid_number >= CTL_AUTO_START) {
217 printf("sysctl: OID number(%d) is already in use for '%s'\n",
218 oidp->oid_number, oidp->oid_name);
220 /* update the OID number, if any */
221 oidp->oid_number = oid_number;
223 SLIST_INSERT_AFTER(q, oidp, oid_link);
225 SLIST_INSERT_HEAD(parent, oidp, oid_link);
229 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
233 * Mark the leaf as dormant if it's not to be immediately enabled.
234 * We do not disable nodes as they can be shared between modules
235 * and it is always safe to access a node.
237 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
238 ("internal flag is set in oid_kind"));
239 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
240 oidp->oid_kind |= CTLFLAG_DORMANT;
241 sysctl_register_oid(oidp);
245 sysctl_enable_oid(struct sysctl_oid *oidp)
248 SYSCTL_ASSERT_XLOCKED();
249 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
250 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
251 ("sysctl node is marked as dormant"));
254 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
255 ("enabling already enabled sysctl oid"));
256 oidp->oid_kind &= ~CTLFLAG_DORMANT;
260 sysctl_unregister_oid(struct sysctl_oid *oidp)
262 struct sysctl_oid *p;
265 SYSCTL_ASSERT_XLOCKED();
267 if (oidp->oid_number == OID_AUTO) {
270 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
272 SLIST_REMOVE(oidp->oid_parent, oidp,
273 sysctl_oid, oid_link);
281 * This can happen when a module fails to register and is
282 * being unloaded afterwards. It should not be a panic()
286 printf("%s: failed to unregister sysctl\n", __func__);
289 /* Initialize a new context to keep track of dynamically added sysctls. */
291 sysctl_ctx_init(struct sysctl_ctx_list *c)
299 * No locking here, the caller is responsible for not adding
300 * new nodes to a context until after this function has
307 /* Free the context, and destroy all dynamic oids registered in this context */
309 sysctl_ctx_free(struct sysctl_ctx_list *clist)
311 struct sysctl_ctx_entry *e, *e1;
316 * First perform a "dry run" to check if it's ok to remove oids.
318 * XXX This algorithm is a hack. But I don't know any
319 * XXX better solution for now...
322 TAILQ_FOREACH(e, clist, link) {
323 error = sysctl_remove_oid_locked(e->entry, 0, 0);
328 * Restore deregistered entries, either from the end,
329 * or from the place where error occurred.
330 * e contains the entry that was not unregistered
333 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
335 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
337 sysctl_register_oid(e1->entry);
338 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
344 /* Now really delete the entries */
345 e = TAILQ_FIRST(clist);
347 e1 = TAILQ_NEXT(e, link);
348 error = sysctl_remove_oid_locked(e->entry, 1, 0);
350 panic("sysctl_remove_oid: corrupt tree, entry: %s",
352 free(e, M_SYSCTLOID);
359 /* Add an entry to the context */
360 struct sysctl_ctx_entry *
361 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
363 struct sysctl_ctx_entry *e;
365 SYSCTL_ASSERT_XLOCKED();
366 if (clist == NULL || oidp == NULL)
368 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
370 TAILQ_INSERT_HEAD(clist, e, link);
374 /* Find an entry in the context */
375 struct sysctl_ctx_entry *
376 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
378 struct sysctl_ctx_entry *e;
380 SYSCTL_ASSERT_XLOCKED();
381 if (clist == NULL || oidp == NULL)
383 TAILQ_FOREACH(e, clist, link) {
391 * Delete an entry from the context.
392 * NOTE: this function doesn't free oidp! You have to remove it
393 * with sysctl_remove_oid().
396 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
398 struct sysctl_ctx_entry *e;
400 if (clist == NULL || oidp == NULL)
403 e = sysctl_ctx_entry_find(clist, oidp);
405 TAILQ_REMOVE(clist, e, link);
407 free(e, M_SYSCTLOID);
416 * Remove dynamically created sysctl trees.
417 * oidp - top of the tree to be removed
418 * del - if 0 - just deregister, otherwise free up entries as well
419 * recurse - if != 0 traverse the subtree to be deleted
422 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
427 error = sysctl_remove_oid_locked(oidp, del, recurse);
433 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
434 int del, int recurse)
436 struct sysctl_oid *p, *tmp;
441 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
442 if (strcmp(p->oid_name, name) == 0) {
443 error = sysctl_remove_oid_locked(p, del, recurse);
454 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
456 struct sysctl_oid *p, *tmp;
459 SYSCTL_ASSERT_XLOCKED();
462 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
463 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
468 * WARNING: normal method to do this should be through
469 * sysctl_ctx_free(). Use recursing as the last resort
470 * method to purge your sysctl tree of leftovers...
471 * However, if some other code still references these nodes,
474 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
475 if (oidp->oid_refcnt == 1) {
476 SLIST_FOREACH_SAFE(p,
477 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
479 printf("Warning: failed attempt to "
480 "remove oid %s with child %s\n",
481 oidp->oid_name, p->oid_name);
484 error = sysctl_remove_oid_locked(p, del,
490 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
493 if (oidp->oid_refcnt > 1 ) {
496 if (oidp->oid_refcnt == 0) {
497 printf("Warning: bad oid_refcnt=%u (%s)!\n",
498 oidp->oid_refcnt, oidp->oid_name);
501 sysctl_unregister_oid(oidp);
504 * Wait for all threads running the handler to drain.
505 * This preserves the previous behavior when the
506 * sysctl lock was held across a handler invocation,
507 * and is necessary for module unload correctness.
509 while (oidp->oid_running > 0) {
510 oidp->oid_kind |= CTLFLAG_DYING;
511 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
514 free(__DECONST(char *, oidp->oid_descr),
516 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
517 free(oidp, M_SYSCTLOID);
523 * Create new sysctls at run time.
524 * clist may point to a valid context initialized with sysctl_ctx_init().
527 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
528 int number, const char *name, int kind, void *arg1, intptr_t arg2,
529 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
531 struct sysctl_oid *oidp;
533 /* You have to hook up somewhere.. */
536 /* Check if the node already exists, otherwise create it */
538 oidp = sysctl_find_oidname(name, parent);
540 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
542 /* Update the context */
544 sysctl_ctx_entry_add(clist, oidp);
549 printf("can't re-use a leaf (%s)!\n", name);
553 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
554 oidp->oid_parent = parent;
555 SLIST_NEXT(oidp, oid_link) = NULL;
556 oidp->oid_number = number;
557 oidp->oid_refcnt = 1;
558 oidp->oid_name = strdup(name, M_SYSCTLOID);
559 oidp->oid_handler = handler;
560 oidp->oid_kind = CTLFLAG_DYN | kind;
561 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
562 /* Allocate space for children */
563 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
564 M_SYSCTLOID, M_WAITOK));
565 SLIST_INIT(SYSCTL_CHILDREN(oidp));
566 oidp->oid_arg2 = arg2;
568 oidp->oid_arg1 = arg1;
569 oidp->oid_arg2 = arg2;
573 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
574 /* Update the context, if used */
576 sysctl_ctx_entry_add(clist, oidp);
577 /* Register this oid */
578 sysctl_register_oid(oidp);
584 * Rename an existing oid.
587 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
592 newname = strdup(name, M_SYSCTLOID);
594 oldname = __DECONST(char *, oidp->oid_name);
595 oidp->oid_name = newname;
597 free(oldname, M_SYSCTLOID);
601 * Reparent an existing oid.
604 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
606 struct sysctl_oid *oidp;
609 if (oid->oid_parent == parent) {
613 oidp = sysctl_find_oidname(oid->oid_name, parent);
618 sysctl_unregister_oid(oid);
619 oid->oid_parent = parent;
620 oid->oid_number = OID_AUTO;
621 sysctl_register_oid(oid);
627 * Register the kernel's oids on startup.
629 SET_DECLARE(sysctl_set, struct sysctl_oid);
632 sysctl_register_all(void *arg)
634 struct sysctl_oid **oidp;
636 sx_init(&sysctlmemlock, "sysctl mem");
639 SET_FOREACH(oidp, sysctl_set)
640 sysctl_register_oid(*oidp);
643 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
648 * These functions implement a presently undocumented interface
649 * used by the sysctl program to walk the tree, and get the type
650 * so it can print the value.
651 * This interface is under work and consideration, and should probably
652 * be killed with a big axe by the first person who can find the time.
653 * (be aware though, that the proper interface isn't as obvious as it
654 * may seem, there are various conflicting requirements.
656 * {0,0} printf the entire MIB-tree.
657 * {0,1,...} return the name of the "..." OID.
658 * {0,2,...} return the next OID.
659 * {0,3} return the OID of the name in "new"
660 * {0,4,...} return the kind & format info for the "..." OID.
661 * {0,5,...} return the description the "..." OID.
666 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
669 struct sysctl_oid *oidp;
671 SYSCTL_ASSERT_XLOCKED();
672 SLIST_FOREACH(oidp, l, oid_link) {
677 printf("%d %s ", oidp->oid_number, oidp->oid_name);
680 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
681 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
683 if (oidp->oid_handler)
686 switch (oidp->oid_kind & CTLTYPE) {
689 if (!oidp->oid_handler) {
690 sysctl_sysctl_debug_dump_node(
691 oidp->oid_arg1, i+2);
694 case CTLTYPE_INT: printf(" Int\n"); break;
695 case CTLTYPE_UINT: printf(" u_int\n"); break;
696 case CTLTYPE_LONG: printf(" Long\n"); break;
697 case CTLTYPE_ULONG: printf(" u_long\n"); break;
698 case CTLTYPE_STRING: printf(" String\n"); break;
699 case CTLTYPE_U64: printf(" uint64_t\n"); break;
700 case CTLTYPE_S64: printf(" int64_t\n"); break;
701 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
702 default: printf("\n");
709 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
713 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
717 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
722 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
723 0, 0, sysctl_sysctl_debug, "-", "");
727 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
729 int *name = (int *) arg1;
730 u_int namelen = arg2;
732 struct sysctl_oid *oid;
733 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
739 snprintf(buf,sizeof(buf),"%d",*name);
741 error = SYSCTL_OUT(req, ".", 1);
743 error = SYSCTL_OUT(req, buf, strlen(buf));
751 SLIST_FOREACH(oid, lsp, oid_link) {
752 if (oid->oid_number != *name)
756 error = SYSCTL_OUT(req, ".", 1);
758 error = SYSCTL_OUT(req, oid->oid_name,
759 strlen(oid->oid_name));
766 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
769 if (oid->oid_handler)
772 lsp2 = SYSCTL_CHILDREN(oid);
777 error = SYSCTL_OUT(req, "", 1);
784 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
787 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD,
788 sysctl_sysctl_name, "");
791 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
792 int *next, int *len, int level, struct sysctl_oid **oidpp)
794 struct sysctl_oid *oidp;
796 SYSCTL_ASSERT_XLOCKED();
798 SLIST_FOREACH(oidp, lsp, oid_link) {
799 *next = oidp->oid_number;
802 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
806 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
808 if (oidp->oid_handler)
809 /* We really should call the handler here...*/
811 lsp = SYSCTL_CHILDREN(oidp);
812 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
813 len, level+1, oidpp))
818 if (oidp->oid_number < *name)
821 if (oidp->oid_number > *name) {
822 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
824 if (oidp->oid_handler)
826 lsp = SYSCTL_CHILDREN(oidp);
827 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
828 next+1, len, level+1, oidpp))
832 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
835 if (oidp->oid_handler)
838 lsp = SYSCTL_CHILDREN(oidp);
839 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
840 len, level+1, oidpp))
851 sysctl_sysctl_next(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;
858 int newoid[CTL_MAXNAME];
861 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
865 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
870 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
873 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
874 sysctl_sysctl_next, "");
877 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
879 struct sysctl_oid *oidp;
880 struct sysctl_oid_list *lsp = &sysctl__children;
883 SYSCTL_ASSERT_XLOCKED();
885 for (*len = 0; *len < CTL_MAXNAME;) {
886 p = strsep(&name, ".");
888 oidp = SLIST_FIRST(lsp);
889 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
892 if (strcmp(p, oidp->oid_name) == 0)
895 *oid++ = oidp->oid_number;
898 if (name == NULL || *name == '\0') {
904 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
907 if (oidp->oid_handler)
910 lsp = SYSCTL_CHILDREN(oidp);
916 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
919 int error, oid[CTL_MAXNAME], len = 0;
920 struct sysctl_oid *op = 0;
924 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
925 return (ENAMETOOLONG);
927 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
929 error = SYSCTL_IN(req, p, req->newlen);
935 p [req->newlen] = '\0';
938 error = name2oid(p, oid, &len, &op);
946 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
951 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
954 SYSCTL_PROC(_sysctl, 3, name2oid,
955 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
956 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
959 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
961 struct sysctl_oid *oid;
965 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
969 if (oid->oid_fmt == NULL) {
973 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
976 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
983 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
984 sysctl_sysctl_oidfmt, "");
987 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
989 struct sysctl_oid *oid;
993 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
997 if (oid->oid_descr == NULL) {
1001 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1007 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD,
1008 sysctl_sysctl_oiddescr, "");
1011 * Default "handler" functions.
1015 * Handle an int, signed or unsigned.
1017 * a variable: point arg1 at it.
1018 * a constant: pass it in arg2.
1022 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1024 int tmpout, error = 0;
1027 * Attempt to get a coherent snapshot by making a copy of the data.
1030 tmpout = *(int *)arg1;
1033 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1035 if (error || !req->newptr)
1041 error = SYSCTL_IN(req, arg1, sizeof(int));
1046 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1047 * Note: this is used by TCP.
1051 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1056 s = (int)((int64_t)tt * 1000 / hz);
1058 error = sysctl_handle_int(oidp, &s, 0, req);
1059 if (error || !req->newptr)
1062 tt = (int)((int64_t)s * hz / 1000);
1072 * Handle a long, signed or unsigned.
1074 * a variable: point arg1 at it.
1075 * a constant: pass it in arg2.
1079 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1088 * Attempt to get a coherent snapshot by making a copy of the data.
1091 tmplong = *(long *)arg1;
1095 if (req->flags & SCTL_MASK32) {
1097 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1100 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1102 if (error || !req->newptr)
1108 else if (req->flags & SCTL_MASK32) {
1109 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1110 *(long *)arg1 = (long)tmpint;
1114 error = SYSCTL_IN(req, arg1, sizeof(long));
1119 * Handle a 64 bit int, signed or unsigned.
1121 * a variable: point arg1 at it.
1122 * a constant: pass it in arg2.
1125 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1131 * Attempt to get a coherent snapshot by making a copy of the data.
1134 tmpout = *(uint64_t *)arg1;
1137 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1139 if (error || !req->newptr)
1145 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1150 * Handle our generic '\0' terminated 'C' string.
1152 * a variable string: point arg1 at it, arg2 is max length.
1153 * a constant string: point arg1 at it, arg2 is zero.
1157 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1164 * Attempt to get a coherent snapshot by copying to a
1165 * temporary kernel buffer.
1168 outlen = strlen((char *)arg1)+1;
1169 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1171 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1172 free(tmparg, M_SYSCTLTMP);
1176 error = SYSCTL_OUT(req, tmparg, outlen);
1177 free(tmparg, M_SYSCTLTMP);
1179 if (error || !req->newptr)
1182 if ((req->newlen - req->newidx) >= arg2) {
1185 arg2 = (req->newlen - req->newidx);
1186 error = SYSCTL_IN(req, arg1, arg2);
1187 ((char *)arg1)[arg2] = '\0';
1194 * Handle any kind of opaque data.
1195 * arg1 points to it, arg2 is the size.
1199 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1203 struct sysctl_req req2;
1206 * Attempt to get a coherent snapshot, by using the thread
1207 * pre-emption counter updated from within mi_switch() to
1208 * determine if we were pre-empted during a bcopy() or
1209 * copyout(). Make 3 attempts at doing this before giving up.
1210 * If we encounter an error, stop immediately.
1215 generation = curthread->td_generation;
1216 error = SYSCTL_OUT(req, arg1, arg2);
1220 if (generation != curthread->td_generation && tries < 3) {
1225 error = SYSCTL_IN(req, arg1, arg2);
1231 * Transfer functions to/from kernel space.
1232 * XXX: rather untested at this point
1235 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1241 if (req->oldlen <= req->oldidx)
1244 if (i > req->oldlen - req->oldidx)
1245 i = req->oldlen - req->oldidx;
1247 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1250 if (req->oldptr && i != l)
1256 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1260 if (req->newlen - req->newidx < l)
1262 bcopy((char *)req->newptr + req->newidx, p, l);
1268 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1269 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1272 struct sysctl_req req;
1274 bzero(&req, sizeof req);
1280 req.oldlen = *oldlenp;
1282 req.validlen = req.oldlen;
1289 req.newlen = newlen;
1293 req.oldfunc = sysctl_old_kernel;
1294 req.newfunc = sysctl_new_kernel;
1295 req.lock = REQ_UNWIRED;
1298 error = sysctl_root(0, name, namelen, &req);
1301 if (req.lock == REQ_WIRED && req.validlen > 0)
1302 vsunlock(req.oldptr, req.validlen);
1304 if (error && error != ENOMEM)
1308 if (req.oldptr && req.oldidx > req.validlen)
1309 *retval = req.validlen;
1311 *retval = req.oldidx;
1317 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1318 void *new, size_t newlen, size_t *retval, int flags)
1320 int oid[CTL_MAXNAME];
1321 size_t oidlen, plen;
1324 oid[0] = 0; /* sysctl internal magic */
1325 oid[1] = 3; /* name2oid */
1326 oidlen = sizeof(oid);
1328 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1329 (void *)name, strlen(name), &plen, flags);
1333 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1334 new, newlen, retval, flags);
1339 * Transfer function to/from user space.
1342 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1344 size_t i, len, origidx;
1347 origidx = req->oldidx;
1349 if (req->oldptr == NULL)
1352 * If we have not wired the user supplied buffer and we are currently
1353 * holding locks, drop a witness warning, as it's possible that
1354 * write operations to the user page can sleep.
1356 if (req->lock != REQ_WIRED)
1357 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1358 "sysctl_old_user()");
1360 len = req->validlen;
1364 if (i > len - origidx)
1366 if (req->lock == REQ_WIRED) {
1367 error = copyout_nofault(p, (char *)req->oldptr +
1370 error = copyout(p, (char *)req->oldptr + origidx, i);
1380 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1386 if (req->newlen - req->newidx < l)
1388 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1389 "sysctl_new_user()");
1390 error = copyin((char *)req->newptr + req->newidx, p, l);
1396 * Wire the user space destination buffer. If set to a value greater than
1397 * zero, the len parameter limits the maximum amount of wired memory.
1400 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1405 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1407 if (req->lock != REQ_WIRED && req->oldptr &&
1408 req->oldfunc == sysctl_old_user) {
1409 if (wiredlen != 0) {
1410 ret = vslock(req->oldptr, wiredlen);
1417 req->lock = REQ_WIRED;
1418 req->validlen = wiredlen;
1424 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1425 int *nindx, struct sysctl_req *req)
1427 struct sysctl_oid_list *lsp;
1428 struct sysctl_oid *oid;
1431 SYSCTL_ASSERT_XLOCKED();
1432 lsp = &sysctl__children;
1434 while (indx < CTL_MAXNAME) {
1435 SLIST_FOREACH(oid, lsp, oid_link) {
1436 if (oid->oid_number == name[indx])
1443 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1444 if (oid->oid_handler != NULL || indx == namelen) {
1448 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1449 ("%s found DYING node %p", __func__, oid));
1452 lsp = SYSCTL_CHILDREN(oid);
1453 } else if (indx == namelen) {
1454 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1459 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1460 ("%s found DYING node %p", __func__, oid));
1470 * Traverse our tree, and find the right node, execute whatever it points
1471 * to, and return the resulting error code.
1475 sysctl_root(SYSCTL_HANDLER_ARGS)
1477 struct sysctl_oid *oid;
1478 int error, indx, lvl;
1480 SYSCTL_ASSERT_XLOCKED();
1482 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1486 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1488 * You can't call a sysctl when it's a node, but has
1489 * no handler. Inform the user that it's a node.
1490 * The indx may or may not be the same as namelen.
1492 if (oid->oid_handler == NULL)
1496 /* Is this sysctl writable? */
1497 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1500 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1502 #ifdef CAPABILITY_MODE
1504 * If the process is in capability mode, then don't permit reading or
1505 * writing unless specifically granted for the node.
1507 if (IN_CAPABILITY_MODE(req->td)) {
1508 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1510 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1515 /* Is this sysctl sensitive to securelevels? */
1516 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1517 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1518 error = securelevel_gt(req->td->td_ucred, lvl);
1523 /* Is this sysctl writable by only privileged users? */
1524 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1527 if (oid->oid_kind & CTLFLAG_PRISON)
1528 priv = PRIV_SYSCTL_WRITEJAIL;
1530 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1531 prison_owns_vnet(req->td->td_ucred))
1532 priv = PRIV_SYSCTL_WRITEJAIL;
1535 priv = PRIV_SYSCTL_WRITE;
1536 error = priv_check(req->td, priv);
1541 if (!oid->oid_handler)
1544 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1545 arg1 = (int *)arg1 + indx;
1548 arg1 = oid->oid_arg1;
1549 arg2 = oid->oid_arg2;
1552 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1560 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1561 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1563 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1565 error = oid->oid_handler(oid, arg1, arg2, req);
1566 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1569 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1573 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1574 wakeup(&oid->oid_running);
1578 #ifndef _SYS_SYSPROTO_H_
1579 struct sysctl_args {
1589 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1591 int error, i, name[CTL_MAXNAME];
1594 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1597 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1601 error = userland_sysctl(td, name, uap->namelen,
1602 uap->old, uap->oldlenp, 0,
1603 uap->new, uap->newlen, &j, 0);
1604 if (error && error != ENOMEM)
1607 i = copyout(&j, uap->oldlenp, sizeof(j));
1615 * This is used from various compatibility syscalls too. That's why name
1616 * must be in kernel space.
1619 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1620 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1623 int error = 0, memlocked;
1624 struct sysctl_req req;
1626 bzero(&req, sizeof req);
1633 req.oldlen = *oldlenp;
1635 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1640 req.validlen = req.oldlen;
1643 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1649 if (!useracc(new, newlen, VM_PROT_READ))
1651 req.newlen = newlen;
1655 req.oldfunc = sysctl_old_user;
1656 req.newfunc = sysctl_new_user;
1657 req.lock = REQ_UNWIRED;
1660 if (KTRPOINT(curthread, KTR_SYSCTL))
1661 ktrsysctl(name, namelen);
1664 if (req.oldlen > PAGE_SIZE) {
1666 sx_xlock(&sysctlmemlock);
1669 CURVNET_SET(TD_TO_VNET(td));
1675 error = sysctl_root(0, name, namelen, &req);
1677 if (error != EAGAIN)
1679 kern_yield(PRI_USER);
1684 if (req.lock == REQ_WIRED && req.validlen > 0)
1685 vsunlock(req.oldptr, req.validlen);
1687 sx_xunlock(&sysctlmemlock);
1689 if (error && error != ENOMEM)
1693 if (req.oldptr && req.oldidx > req.validlen)
1694 *retval = req.validlen;
1696 *retval = req.oldidx;
1702 * Drain into a sysctl struct. The user buffer should be wired if a page
1703 * fault would cause issue.
1706 sbuf_sysctl_drain(void *arg, const char *data, int len)
1708 struct sysctl_req *req = arg;
1711 error = SYSCTL_OUT(req, data, len);
1712 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1713 return (error == 0 ? len : -error);
1717 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1718 struct sysctl_req *req)
1721 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1722 sbuf_set_drain(s, sbuf_sysctl_drain, req);