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/capability.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 * accomodate 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_unregister_oid(struct sysctl_oid *oidp)
231 struct sysctl_oid *p;
234 SYSCTL_ASSERT_XLOCKED();
236 if (oidp->oid_number == OID_AUTO) {
239 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
241 SLIST_REMOVE(oidp->oid_parent, oidp,
242 sysctl_oid, oid_link);
250 * This can happen when a module fails to register and is
251 * being unloaded afterwards. It should not be a panic()
255 printf("%s: failed to unregister sysctl\n", __func__);
258 /* Initialize a new context to keep track of dynamically added sysctls. */
260 sysctl_ctx_init(struct sysctl_ctx_list *c)
268 * No locking here, the caller is responsible for not adding
269 * new nodes to a context until after this function has
276 /* Free the context, and destroy all dynamic oids registered in this context */
278 sysctl_ctx_free(struct sysctl_ctx_list *clist)
280 struct sysctl_ctx_entry *e, *e1;
285 * First perform a "dry run" to check if it's ok to remove oids.
287 * XXX This algorithm is a hack. But I don't know any
288 * XXX better solution for now...
291 TAILQ_FOREACH(e, clist, link) {
292 error = sysctl_remove_oid_locked(e->entry, 0, 0);
297 * Restore deregistered entries, either from the end,
298 * or from the place where error occured.
299 * e contains the entry that was not unregistered
302 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
304 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
306 sysctl_register_oid(e1->entry);
307 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
313 /* Now really delete the entries */
314 e = TAILQ_FIRST(clist);
316 e1 = TAILQ_NEXT(e, link);
317 error = sysctl_remove_oid_locked(e->entry, 1, 0);
319 panic("sysctl_remove_oid: corrupt tree, entry: %s",
321 free(e, M_SYSCTLOID);
328 /* Add an entry to the context */
329 struct sysctl_ctx_entry *
330 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
332 struct sysctl_ctx_entry *e;
334 SYSCTL_ASSERT_XLOCKED();
335 if (clist == NULL || oidp == NULL)
337 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
339 TAILQ_INSERT_HEAD(clist, e, link);
343 /* Find an entry in the context */
344 struct sysctl_ctx_entry *
345 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
347 struct sysctl_ctx_entry *e;
349 SYSCTL_ASSERT_XLOCKED();
350 if (clist == NULL || oidp == NULL)
352 TAILQ_FOREACH(e, clist, link) {
360 * Delete an entry from the context.
361 * NOTE: this function doesn't free oidp! You have to remove it
362 * with sysctl_remove_oid().
365 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
367 struct sysctl_ctx_entry *e;
369 if (clist == NULL || oidp == NULL)
372 e = sysctl_ctx_entry_find(clist, oidp);
374 TAILQ_REMOVE(clist, e, link);
376 free(e, M_SYSCTLOID);
385 * Remove dynamically created sysctl trees.
386 * oidp - top of the tree to be removed
387 * del - if 0 - just deregister, otherwise free up entries as well
388 * recurse - if != 0 traverse the subtree to be deleted
391 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
396 error = sysctl_remove_oid_locked(oidp, del, recurse);
402 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
403 int del, int recurse)
405 struct sysctl_oid *p, *tmp;
410 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
411 if (strcmp(p->oid_name, name) == 0) {
412 error = sysctl_remove_oid_locked(p, del, recurse);
423 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
425 struct sysctl_oid *p, *tmp;
428 SYSCTL_ASSERT_XLOCKED();
431 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
432 printf("can't remove non-dynamic nodes!\n");
436 * WARNING: normal method to do this should be through
437 * sysctl_ctx_free(). Use recursing as the last resort
438 * method to purge your sysctl tree of leftovers...
439 * However, if some other code still references these nodes,
442 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
443 if (oidp->oid_refcnt == 1) {
444 SLIST_FOREACH_SAFE(p,
445 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
448 error = sysctl_remove_oid_locked(p, del,
454 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
457 if (oidp->oid_refcnt > 1 ) {
460 if (oidp->oid_refcnt == 0) {
461 printf("Warning: bad oid_refcnt=%u (%s)!\n",
462 oidp->oid_refcnt, oidp->oid_name);
465 sysctl_unregister_oid(oidp);
468 * Wait for all threads running the handler to drain.
469 * This preserves the previous behavior when the
470 * sysctl lock was held across a handler invocation,
471 * and is necessary for module unload correctness.
473 while (oidp->oid_running > 0) {
474 oidp->oid_kind |= CTLFLAG_DYING;
475 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
478 free(__DECONST(char *, oidp->oid_descr),
480 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
481 free(oidp, M_SYSCTLOID);
487 * Create new sysctls at run time.
488 * clist may point to a valid context initialized with sysctl_ctx_init().
491 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
492 int number, const char *name, int kind, void *arg1, intptr_t arg2,
493 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
495 struct sysctl_oid *oidp;
497 /* You have to hook up somewhere.. */
500 /* Check if the node already exists, otherwise create it */
502 oidp = sysctl_find_oidname(name, parent);
504 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
506 /* Update the context */
508 sysctl_ctx_entry_add(clist, oidp);
513 printf("can't re-use a leaf (%s)!\n", name);
517 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
518 oidp->oid_parent = parent;
519 SLIST_NEXT(oidp, oid_link) = NULL;
520 oidp->oid_number = number;
521 oidp->oid_refcnt = 1;
522 oidp->oid_name = strdup(name, M_SYSCTLOID);
523 oidp->oid_handler = handler;
524 oidp->oid_kind = CTLFLAG_DYN | kind;
525 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
526 /* Allocate space for children */
527 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
528 M_SYSCTLOID, M_WAITOK));
529 SLIST_INIT(SYSCTL_CHILDREN(oidp));
530 oidp->oid_arg2 = arg2;
532 oidp->oid_arg1 = arg1;
533 oidp->oid_arg2 = arg2;
537 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
538 /* Update the context, if used */
540 sysctl_ctx_entry_add(clist, oidp);
541 /* Register this oid */
542 sysctl_register_oid(oidp);
548 * Rename an existing oid.
551 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
556 newname = strdup(name, M_SYSCTLOID);
558 oldname = __DECONST(char *, oidp->oid_name);
559 oidp->oid_name = newname;
561 free(oldname, M_SYSCTLOID);
565 * Reparent an existing oid.
568 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
570 struct sysctl_oid *oidp;
573 if (oid->oid_parent == parent) {
577 oidp = sysctl_find_oidname(oid->oid_name, parent);
582 sysctl_unregister_oid(oid);
583 oid->oid_parent = parent;
584 oid->oid_number = OID_AUTO;
585 sysctl_register_oid(oid);
591 * Register the kernel's oids on startup.
593 SET_DECLARE(sysctl_set, struct sysctl_oid);
596 sysctl_register_all(void *arg)
598 struct sysctl_oid **oidp;
600 sx_init(&sysctlmemlock, "sysctl mem");
603 SET_FOREACH(oidp, sysctl_set)
604 sysctl_register_oid(*oidp);
607 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
612 * These functions implement a presently undocumented interface
613 * used by the sysctl program to walk the tree, and get the type
614 * so it can print the value.
615 * This interface is under work and consideration, and should probably
616 * be killed with a big axe by the first person who can find the time.
617 * (be aware though, that the proper interface isn't as obvious as it
618 * may seem, there are various conflicting requirements.
620 * {0,0} printf the entire MIB-tree.
621 * {0,1,...} return the name of the "..." OID.
622 * {0,2,...} return the next OID.
623 * {0,3} return the OID of the name in "new"
624 * {0,4,...} return the kind & format info for the "..." OID.
625 * {0,5,...} return the description the "..." OID.
630 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
633 struct sysctl_oid *oidp;
635 SYSCTL_ASSERT_XLOCKED();
636 SLIST_FOREACH(oidp, l, oid_link) {
641 printf("%d %s ", oidp->oid_number, oidp->oid_name);
644 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
645 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
647 if (oidp->oid_handler)
650 switch (oidp->oid_kind & CTLTYPE) {
653 if (!oidp->oid_handler) {
654 sysctl_sysctl_debug_dump_node(
655 oidp->oid_arg1, i+2);
658 case CTLTYPE_INT: printf(" Int\n"); break;
659 case CTLTYPE_UINT: printf(" u_int\n"); break;
660 case CTLTYPE_LONG: printf(" Long\n"); break;
661 case CTLTYPE_ULONG: printf(" u_long\n"); break;
662 case CTLTYPE_STRING: printf(" String\n"); break;
663 case CTLTYPE_U64: printf(" uint64_t\n"); break;
664 case CTLTYPE_S64: printf(" int64_t\n"); break;
665 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
666 default: printf("\n");
673 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
677 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
681 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
686 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
687 0, 0, sysctl_sysctl_debug, "-", "");
691 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
693 int *name = (int *) arg1;
694 u_int namelen = arg2;
696 struct sysctl_oid *oid;
697 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
703 snprintf(buf,sizeof(buf),"%d",*name);
705 error = SYSCTL_OUT(req, ".", 1);
707 error = SYSCTL_OUT(req, buf, strlen(buf));
715 SLIST_FOREACH(oid, lsp, oid_link) {
716 if (oid->oid_number != *name)
720 error = SYSCTL_OUT(req, ".", 1);
722 error = SYSCTL_OUT(req, oid->oid_name,
723 strlen(oid->oid_name));
730 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
733 if (oid->oid_handler)
736 lsp2 = SYSCTL_CHILDREN(oid);
741 error = SYSCTL_OUT(req, "", 1);
748 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
751 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD,
752 sysctl_sysctl_name, "");
755 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
756 int *next, int *len, int level, struct sysctl_oid **oidpp)
758 struct sysctl_oid *oidp;
760 SYSCTL_ASSERT_XLOCKED();
762 SLIST_FOREACH(oidp, lsp, oid_link) {
763 *next = oidp->oid_number;
766 if (oidp->oid_kind & CTLFLAG_SKIP)
770 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
772 if (oidp->oid_handler)
773 /* We really should call the handler here...*/
775 lsp = SYSCTL_CHILDREN(oidp);
776 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
777 len, level+1, oidpp))
782 if (oidp->oid_number < *name)
785 if (oidp->oid_number > *name) {
786 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
788 if (oidp->oid_handler)
790 lsp = SYSCTL_CHILDREN(oidp);
791 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
792 next+1, len, level+1, oidpp))
796 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
799 if (oidp->oid_handler)
802 lsp = SYSCTL_CHILDREN(oidp);
803 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
804 len, level+1, oidpp))
815 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
817 int *name = (int *) arg1;
818 u_int namelen = arg2;
820 struct sysctl_oid *oid;
821 struct sysctl_oid_list *lsp = &sysctl__children;
822 int newoid[CTL_MAXNAME];
825 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
829 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
834 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
837 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
838 sysctl_sysctl_next, "");
841 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
843 struct sysctl_oid *oidp;
844 struct sysctl_oid_list *lsp = &sysctl__children;
847 SYSCTL_ASSERT_XLOCKED();
849 for (*len = 0; *len < CTL_MAXNAME;) {
850 p = strsep(&name, ".");
852 oidp = SLIST_FIRST(lsp);
853 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
856 if (strcmp(p, oidp->oid_name) == 0)
859 *oid++ = oidp->oid_number;
862 if (name == NULL || *name == '\0') {
868 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
871 if (oidp->oid_handler)
874 lsp = SYSCTL_CHILDREN(oidp);
880 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
883 int error, oid[CTL_MAXNAME], len = 0;
884 struct sysctl_oid *op = 0;
888 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
889 return (ENAMETOOLONG);
891 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
893 error = SYSCTL_IN(req, p, req->newlen);
899 p [req->newlen] = '\0';
902 error = name2oid(p, oid, &len, &op);
910 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
915 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
918 SYSCTL_PROC(_sysctl, 3, name2oid,
919 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
920 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
923 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
925 struct sysctl_oid *oid;
929 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
933 if (oid->oid_fmt == NULL) {
937 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
940 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
947 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
948 sysctl_sysctl_oidfmt, "");
951 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
953 struct sysctl_oid *oid;
957 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
961 if (oid->oid_descr == NULL) {
965 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
971 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD,
972 sysctl_sysctl_oiddescr, "");
975 * Default "handler" functions.
979 * Handle an int, signed or unsigned.
981 * a variable: point arg1 at it.
982 * a constant: pass it in arg2.
986 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
988 int tmpout, error = 0;
991 * Attempt to get a coherent snapshot by making a copy of the data.
994 tmpout = *(int *)arg1;
997 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
999 if (error || !req->newptr)
1005 error = SYSCTL_IN(req, arg1, sizeof(int));
1010 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1011 * Note: this is used by TCP.
1015 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1020 s = (int)((int64_t)tt * 1000 / hz);
1022 error = sysctl_handle_int(oidp, &s, 0, req);
1023 if (error || !req->newptr)
1026 tt = (int)((int64_t)s * hz / 1000);
1036 * Handle a long, signed or unsigned.
1038 * a variable: point arg1 at it.
1039 * a constant: pass it in arg2.
1043 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1052 * Attempt to get a coherent snapshot by making a copy of the data.
1055 tmplong = *(long *)arg1;
1059 if (req->flags & SCTL_MASK32) {
1061 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1064 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1066 if (error || !req->newptr)
1072 else if (req->flags & SCTL_MASK32) {
1073 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1074 *(long *)arg1 = (long)tmpint;
1078 error = SYSCTL_IN(req, arg1, sizeof(long));
1083 * Handle a 64 bit int, signed or unsigned.
1085 * a variable: point arg1 at it.
1086 * a constant: pass it in arg2.
1089 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1095 * Attempt to get a coherent snapshot by making a copy of the data.
1098 tmpout = *(uint64_t *)arg1;
1101 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1103 if (error || !req->newptr)
1109 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1114 * Handle our generic '\0' terminated 'C' string.
1116 * a variable string: point arg1 at it, arg2 is max length.
1117 * a constant string: point arg1 at it, arg2 is zero.
1121 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1128 * Attempt to get a coherent snapshot by copying to a
1129 * temporary kernel buffer.
1132 outlen = strlen((char *)arg1)+1;
1133 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1135 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1136 free(tmparg, M_SYSCTLTMP);
1140 error = SYSCTL_OUT(req, tmparg, outlen);
1141 free(tmparg, M_SYSCTLTMP);
1143 if (error || !req->newptr)
1146 if ((req->newlen - req->newidx) >= arg2) {
1149 arg2 = (req->newlen - req->newidx);
1150 error = SYSCTL_IN(req, arg1, arg2);
1151 ((char *)arg1)[arg2] = '\0';
1158 * Handle any kind of opaque data.
1159 * arg1 points to it, arg2 is the size.
1163 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1167 struct sysctl_req req2;
1170 * Attempt to get a coherent snapshot, by using the thread
1171 * pre-emption counter updated from within mi_switch() to
1172 * determine if we were pre-empted during a bcopy() or
1173 * copyout(). Make 3 attempts at doing this before giving up.
1174 * If we encounter an error, stop immediately.
1179 generation = curthread->td_generation;
1180 error = SYSCTL_OUT(req, arg1, arg2);
1184 if (generation != curthread->td_generation && tries < 3) {
1189 error = SYSCTL_IN(req, arg1, arg2);
1195 * Transfer functions to/from kernel space.
1196 * XXX: rather untested at this point
1199 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1205 if (req->oldlen <= req->oldidx)
1208 if (i > req->oldlen - req->oldidx)
1209 i = req->oldlen - req->oldidx;
1211 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1214 if (req->oldptr && i != l)
1220 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1224 if (req->newlen - req->newidx < l)
1226 bcopy((char *)req->newptr + req->newidx, p, l);
1232 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1233 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1236 struct sysctl_req req;
1238 bzero(&req, sizeof req);
1244 req.oldlen = *oldlenp;
1246 req.validlen = req.oldlen;
1253 req.newlen = newlen;
1257 req.oldfunc = sysctl_old_kernel;
1258 req.newfunc = sysctl_new_kernel;
1259 req.lock = REQ_UNWIRED;
1262 error = sysctl_root(0, name, namelen, &req);
1265 if (req.lock == REQ_WIRED && req.validlen > 0)
1266 vsunlock(req.oldptr, req.validlen);
1268 if (error && error != ENOMEM)
1272 if (req.oldptr && req.oldidx > req.validlen)
1273 *retval = req.validlen;
1275 *retval = req.oldidx;
1281 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1282 void *new, size_t newlen, size_t *retval, int flags)
1284 int oid[CTL_MAXNAME];
1285 size_t oidlen, plen;
1288 oid[0] = 0; /* sysctl internal magic */
1289 oid[1] = 3; /* name2oid */
1290 oidlen = sizeof(oid);
1292 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1293 (void *)name, strlen(name), &plen, flags);
1297 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1298 new, newlen, retval, flags);
1303 * Transfer function to/from user space.
1306 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1308 size_t i, len, origidx;
1311 origidx = req->oldidx;
1313 if (req->oldptr == NULL)
1316 * If we have not wired the user supplied buffer and we are currently
1317 * holding locks, drop a witness warning, as it's possible that
1318 * write operations to the user page can sleep.
1320 if (req->lock != REQ_WIRED)
1321 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1322 "sysctl_old_user()");
1324 len = req->validlen;
1328 if (i > len - origidx)
1330 if (req->lock == REQ_WIRED) {
1331 error = copyout_nofault(p, (char *)req->oldptr +
1334 error = copyout(p, (char *)req->oldptr + origidx, i);
1344 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1350 if (req->newlen - req->newidx < l)
1352 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1353 "sysctl_new_user()");
1354 error = copyin((char *)req->newptr + req->newidx, p, l);
1360 * Wire the user space destination buffer. If set to a value greater than
1361 * zero, the len parameter limits the maximum amount of wired memory.
1364 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1369 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1371 if (req->lock != REQ_WIRED && req->oldptr &&
1372 req->oldfunc == sysctl_old_user) {
1373 if (wiredlen != 0) {
1374 ret = vslock(req->oldptr, wiredlen);
1381 req->lock = REQ_WIRED;
1382 req->validlen = wiredlen;
1388 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1389 int *nindx, struct sysctl_req *req)
1391 struct sysctl_oid_list *lsp;
1392 struct sysctl_oid *oid;
1395 SYSCTL_ASSERT_XLOCKED();
1396 lsp = &sysctl__children;
1398 while (indx < CTL_MAXNAME) {
1399 SLIST_FOREACH(oid, lsp, oid_link) {
1400 if (oid->oid_number == name[indx])
1407 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1408 if (oid->oid_handler != NULL || indx == namelen) {
1412 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1413 ("%s found DYING node %p", __func__, oid));
1416 lsp = SYSCTL_CHILDREN(oid);
1417 } else if (indx == namelen) {
1421 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1422 ("%s found DYING node %p", __func__, oid));
1432 * Traverse our tree, and find the right node, execute whatever it points
1433 * to, and return the resulting error code.
1437 sysctl_root(SYSCTL_HANDLER_ARGS)
1439 struct sysctl_oid *oid;
1440 int error, indx, lvl;
1442 SYSCTL_ASSERT_XLOCKED();
1444 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1448 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1450 * You can't call a sysctl when it's a node, but has
1451 * no handler. Inform the user that it's a node.
1452 * The indx may or may not be the same as namelen.
1454 if (oid->oid_handler == NULL)
1458 /* Is this sysctl writable? */
1459 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1462 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1464 #ifdef CAPABILITY_MODE
1466 * If the process is in capability mode, then don't permit reading or
1467 * writing unless specifically granted for the node.
1469 if (IN_CAPABILITY_MODE(req->td)) {
1470 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1472 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1477 /* Is this sysctl sensitive to securelevels? */
1478 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1479 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1480 error = securelevel_gt(req->td->td_ucred, lvl);
1485 /* Is this sysctl writable by only privileged users? */
1486 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1489 if (oid->oid_kind & CTLFLAG_PRISON)
1490 priv = PRIV_SYSCTL_WRITEJAIL;
1492 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1493 prison_owns_vnet(req->td->td_ucred))
1494 priv = PRIV_SYSCTL_WRITEJAIL;
1497 priv = PRIV_SYSCTL_WRITE;
1498 error = priv_check(req->td, priv);
1503 if (!oid->oid_handler)
1506 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1507 arg1 = (int *)arg1 + indx;
1510 arg1 = oid->oid_arg1;
1511 arg2 = oid->oid_arg2;
1514 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1522 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1524 error = oid->oid_handler(oid, arg1, arg2, req);
1525 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1528 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1532 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1533 wakeup(&oid->oid_running);
1537 #ifndef _SYS_SYSPROTO_H_
1538 struct sysctl_args {
1548 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1550 int error, i, name[CTL_MAXNAME];
1553 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1556 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1560 error = userland_sysctl(td, name, uap->namelen,
1561 uap->old, uap->oldlenp, 0,
1562 uap->new, uap->newlen, &j, 0);
1563 if (error && error != ENOMEM)
1566 i = copyout(&j, uap->oldlenp, sizeof(j));
1574 * This is used from various compatibility syscalls too. That's why name
1575 * must be in kernel space.
1578 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1579 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1582 int error = 0, memlocked;
1583 struct sysctl_req req;
1585 bzero(&req, sizeof req);
1592 req.oldlen = *oldlenp;
1594 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1599 req.validlen = req.oldlen;
1602 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1608 if (!useracc(new, newlen, VM_PROT_READ))
1610 req.newlen = newlen;
1614 req.oldfunc = sysctl_old_user;
1615 req.newfunc = sysctl_new_user;
1616 req.lock = REQ_UNWIRED;
1619 if (KTRPOINT(curthread, KTR_SYSCTL))
1620 ktrsysctl(name, namelen);
1623 if (req.oldlen > PAGE_SIZE) {
1625 sx_xlock(&sysctlmemlock);
1628 CURVNET_SET(TD_TO_VNET(td));
1634 error = sysctl_root(0, name, namelen, &req);
1636 if (error != EAGAIN)
1638 kern_yield(PRI_USER);
1643 if (req.lock == REQ_WIRED && req.validlen > 0)
1644 vsunlock(req.oldptr, req.validlen);
1646 sx_xunlock(&sysctlmemlock);
1648 if (error && error != ENOMEM)
1652 if (req.oldptr && req.oldidx > req.validlen)
1653 *retval = req.validlen;
1655 *retval = req.oldidx;
1661 * Drain into a sysctl struct. The user buffer should be wired if a page
1662 * fault would cause issue.
1665 sbuf_sysctl_drain(void *arg, const char *data, int len)
1667 struct sysctl_req *req = arg;
1670 error = SYSCTL_OUT(req, data, len);
1671 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1672 return (error == 0 ? len : -error);
1676 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1677 struct sysctl_req *req)
1680 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1681 sbuf_set_drain(s, sbuf_sysctl_drain, req);