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 * 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) {
447 printf("Warning: failed attempt to "
448 "remove oid %s with child %s\n",
449 oidp->oid_name, p->oid_name);
452 error = sysctl_remove_oid_locked(p, del,
458 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
461 if (oidp->oid_refcnt > 1 ) {
464 if (oidp->oid_refcnt == 0) {
465 printf("Warning: bad oid_refcnt=%u (%s)!\n",
466 oidp->oid_refcnt, oidp->oid_name);
469 sysctl_unregister_oid(oidp);
472 * Wait for all threads running the handler to drain.
473 * This preserves the previous behavior when the
474 * sysctl lock was held across a handler invocation,
475 * and is necessary for module unload correctness.
477 while (oidp->oid_running > 0) {
478 oidp->oid_kind |= CTLFLAG_DYING;
479 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
482 free(__DECONST(char *, oidp->oid_descr),
484 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
485 free(oidp, M_SYSCTLOID);
491 * Create new sysctls at run time.
492 * clist may point to a valid context initialized with sysctl_ctx_init().
495 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
496 int number, const char *name, int kind, void *arg1, intptr_t arg2,
497 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
499 struct sysctl_oid *oidp;
501 /* You have to hook up somewhere.. */
504 /* Check if the node already exists, otherwise create it */
506 oidp = sysctl_find_oidname(name, parent);
508 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
510 /* Update the context */
512 sysctl_ctx_entry_add(clist, oidp);
517 printf("can't re-use a leaf (%s)!\n", name);
521 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
522 oidp->oid_parent = parent;
523 SLIST_NEXT(oidp, oid_link) = NULL;
524 oidp->oid_number = number;
525 oidp->oid_refcnt = 1;
526 oidp->oid_name = strdup(name, M_SYSCTLOID);
527 oidp->oid_handler = handler;
528 oidp->oid_kind = CTLFLAG_DYN | kind;
529 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
530 /* Allocate space for children */
531 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
532 M_SYSCTLOID, M_WAITOK));
533 SLIST_INIT(SYSCTL_CHILDREN(oidp));
534 oidp->oid_arg2 = arg2;
536 oidp->oid_arg1 = arg1;
537 oidp->oid_arg2 = arg2;
541 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
542 /* Update the context, if used */
544 sysctl_ctx_entry_add(clist, oidp);
545 /* Register this oid */
546 sysctl_register_oid(oidp);
552 * Rename an existing oid.
555 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
560 newname = strdup(name, M_SYSCTLOID);
562 oldname = __DECONST(char *, oidp->oid_name);
563 oidp->oid_name = newname;
565 free(oldname, M_SYSCTLOID);
569 * Reparent an existing oid.
572 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
574 struct sysctl_oid *oidp;
577 if (oid->oid_parent == parent) {
581 oidp = sysctl_find_oidname(oid->oid_name, parent);
586 sysctl_unregister_oid(oid);
587 oid->oid_parent = parent;
588 oid->oid_number = OID_AUTO;
589 sysctl_register_oid(oid);
595 * Register the kernel's oids on startup.
597 SET_DECLARE(sysctl_set, struct sysctl_oid);
600 sysctl_register_all(void *arg)
602 struct sysctl_oid **oidp;
604 sx_init(&sysctlmemlock, "sysctl mem");
607 SET_FOREACH(oidp, sysctl_set)
608 sysctl_register_oid(*oidp);
611 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
616 * These functions implement a presently undocumented interface
617 * used by the sysctl program to walk the tree, and get the type
618 * so it can print the value.
619 * This interface is under work and consideration, and should probably
620 * be killed with a big axe by the first person who can find the time.
621 * (be aware though, that the proper interface isn't as obvious as it
622 * may seem, there are various conflicting requirements.
624 * {0,0} printf the entire MIB-tree.
625 * {0,1,...} return the name of the "..." OID.
626 * {0,2,...} return the next OID.
627 * {0,3} return the OID of the name in "new"
628 * {0,4,...} return the kind & format info for the "..." OID.
629 * {0,5,...} return the description the "..." OID.
634 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
637 struct sysctl_oid *oidp;
639 SYSCTL_ASSERT_XLOCKED();
640 SLIST_FOREACH(oidp, l, oid_link) {
645 printf("%d %s ", oidp->oid_number, oidp->oid_name);
648 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
649 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
651 if (oidp->oid_handler)
654 switch (oidp->oid_kind & CTLTYPE) {
657 if (!oidp->oid_handler) {
658 sysctl_sysctl_debug_dump_node(
659 oidp->oid_arg1, i+2);
662 case CTLTYPE_INT: printf(" Int\n"); break;
663 case CTLTYPE_UINT: printf(" u_int\n"); break;
664 case CTLTYPE_LONG: printf(" Long\n"); break;
665 case CTLTYPE_ULONG: printf(" u_long\n"); break;
666 case CTLTYPE_STRING: printf(" String\n"); break;
667 case CTLTYPE_U64: printf(" uint64_t\n"); break;
668 case CTLTYPE_S64: printf(" int64_t\n"); break;
669 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
670 default: printf("\n");
677 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
681 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
685 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
690 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
691 0, 0, sysctl_sysctl_debug, "-", "");
695 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
697 int *name = (int *) arg1;
698 u_int namelen = arg2;
700 struct sysctl_oid *oid;
701 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
707 snprintf(buf,sizeof(buf),"%d",*name);
709 error = SYSCTL_OUT(req, ".", 1);
711 error = SYSCTL_OUT(req, buf, strlen(buf));
719 SLIST_FOREACH(oid, lsp, oid_link) {
720 if (oid->oid_number != *name)
724 error = SYSCTL_OUT(req, ".", 1);
726 error = SYSCTL_OUT(req, oid->oid_name,
727 strlen(oid->oid_name));
734 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
737 if (oid->oid_handler)
740 lsp2 = SYSCTL_CHILDREN(oid);
745 error = SYSCTL_OUT(req, "", 1);
752 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
755 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD,
756 sysctl_sysctl_name, "");
759 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
760 int *next, int *len, int level, struct sysctl_oid **oidpp)
762 struct sysctl_oid *oidp;
764 SYSCTL_ASSERT_XLOCKED();
766 SLIST_FOREACH(oidp, lsp, oid_link) {
767 *next = oidp->oid_number;
770 if (oidp->oid_kind & CTLFLAG_SKIP)
774 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
776 if (oidp->oid_handler)
777 /* We really should call the handler here...*/
779 lsp = SYSCTL_CHILDREN(oidp);
780 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
781 len, level+1, oidpp))
786 if (oidp->oid_number < *name)
789 if (oidp->oid_number > *name) {
790 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
792 if (oidp->oid_handler)
794 lsp = SYSCTL_CHILDREN(oidp);
795 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
796 next+1, len, level+1, oidpp))
800 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
803 if (oidp->oid_handler)
806 lsp = SYSCTL_CHILDREN(oidp);
807 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
808 len, level+1, oidpp))
819 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
821 int *name = (int *) arg1;
822 u_int namelen = arg2;
824 struct sysctl_oid *oid;
825 struct sysctl_oid_list *lsp = &sysctl__children;
826 int newoid[CTL_MAXNAME];
829 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
833 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
838 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
841 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
842 sysctl_sysctl_next, "");
845 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
847 struct sysctl_oid *oidp;
848 struct sysctl_oid_list *lsp = &sysctl__children;
851 SYSCTL_ASSERT_XLOCKED();
853 for (*len = 0; *len < CTL_MAXNAME;) {
854 p = strsep(&name, ".");
856 oidp = SLIST_FIRST(lsp);
857 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
860 if (strcmp(p, oidp->oid_name) == 0)
863 *oid++ = oidp->oid_number;
866 if (name == NULL || *name == '\0') {
872 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
875 if (oidp->oid_handler)
878 lsp = SYSCTL_CHILDREN(oidp);
884 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
887 int error, oid[CTL_MAXNAME], len = 0;
888 struct sysctl_oid *op = 0;
892 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
893 return (ENAMETOOLONG);
895 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
897 error = SYSCTL_IN(req, p, req->newlen);
903 p [req->newlen] = '\0';
906 error = name2oid(p, oid, &len, &op);
914 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
919 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
922 SYSCTL_PROC(_sysctl, 3, name2oid,
923 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
924 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
927 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
929 struct sysctl_oid *oid;
933 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
937 if (oid->oid_fmt == NULL) {
941 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
944 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
951 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
952 sysctl_sysctl_oidfmt, "");
955 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
957 struct sysctl_oid *oid;
961 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
965 if (oid->oid_descr == NULL) {
969 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
975 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD,
976 sysctl_sysctl_oiddescr, "");
979 * Default "handler" functions.
983 * Handle an int, signed or unsigned.
985 * a variable: point arg1 at it.
986 * a constant: pass it in arg2.
990 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
992 int tmpout, error = 0;
995 * Attempt to get a coherent snapshot by making a copy of the data.
998 tmpout = *(int *)arg1;
1001 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1003 if (error || !req->newptr)
1009 error = SYSCTL_IN(req, arg1, sizeof(int));
1014 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1015 * Note: this is used by TCP.
1019 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1024 s = (int)((int64_t)tt * 1000 / hz);
1026 error = sysctl_handle_int(oidp, &s, 0, req);
1027 if (error || !req->newptr)
1030 tt = (int)((int64_t)s * hz / 1000);
1040 * Handle a long, signed or unsigned.
1042 * a variable: point arg1 at it.
1043 * a constant: pass it in arg2.
1047 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1056 * Attempt to get a coherent snapshot by making a copy of the data.
1059 tmplong = *(long *)arg1;
1063 if (req->flags & SCTL_MASK32) {
1065 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1068 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1070 if (error || !req->newptr)
1076 else if (req->flags & SCTL_MASK32) {
1077 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1078 *(long *)arg1 = (long)tmpint;
1082 error = SYSCTL_IN(req, arg1, sizeof(long));
1087 * Handle a 64 bit int, signed or unsigned.
1089 * a variable: point arg1 at it.
1090 * a constant: pass it in arg2.
1093 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1099 * Attempt to get a coherent snapshot by making a copy of the data.
1102 tmpout = *(uint64_t *)arg1;
1105 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1107 if (error || !req->newptr)
1113 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1118 * Handle our generic '\0' terminated 'C' string.
1120 * a variable string: point arg1 at it, arg2 is max length.
1121 * a constant string: point arg1 at it, arg2 is zero.
1125 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1132 * Attempt to get a coherent snapshot by copying to a
1133 * temporary kernel buffer.
1136 outlen = strlen((char *)arg1)+1;
1137 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1139 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1140 free(tmparg, M_SYSCTLTMP);
1144 error = SYSCTL_OUT(req, tmparg, outlen);
1145 free(tmparg, M_SYSCTLTMP);
1147 if (error || !req->newptr)
1150 if ((req->newlen - req->newidx) >= arg2) {
1153 arg2 = (req->newlen - req->newidx);
1154 error = SYSCTL_IN(req, arg1, arg2);
1155 ((char *)arg1)[arg2] = '\0';
1162 * Handle any kind of opaque data.
1163 * arg1 points to it, arg2 is the size.
1167 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1171 struct sysctl_req req2;
1174 * Attempt to get a coherent snapshot, by using the thread
1175 * pre-emption counter updated from within mi_switch() to
1176 * determine if we were pre-empted during a bcopy() or
1177 * copyout(). Make 3 attempts at doing this before giving up.
1178 * If we encounter an error, stop immediately.
1183 generation = curthread->td_generation;
1184 error = SYSCTL_OUT(req, arg1, arg2);
1188 if (generation != curthread->td_generation && tries < 3) {
1193 error = SYSCTL_IN(req, arg1, arg2);
1199 * Transfer functions to/from kernel space.
1200 * XXX: rather untested at this point
1203 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1209 if (req->oldlen <= req->oldidx)
1212 if (i > req->oldlen - req->oldidx)
1213 i = req->oldlen - req->oldidx;
1215 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1218 if (req->oldptr && i != l)
1224 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1228 if (req->newlen - req->newidx < l)
1230 bcopy((char *)req->newptr + req->newidx, p, l);
1236 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1237 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1240 struct sysctl_req req;
1242 bzero(&req, sizeof req);
1248 req.oldlen = *oldlenp;
1250 req.validlen = req.oldlen;
1257 req.newlen = newlen;
1261 req.oldfunc = sysctl_old_kernel;
1262 req.newfunc = sysctl_new_kernel;
1263 req.lock = REQ_UNWIRED;
1266 error = sysctl_root(0, name, namelen, &req);
1269 if (req.lock == REQ_WIRED && req.validlen > 0)
1270 vsunlock(req.oldptr, req.validlen);
1272 if (error && error != ENOMEM)
1276 if (req.oldptr && req.oldidx > req.validlen)
1277 *retval = req.validlen;
1279 *retval = req.oldidx;
1285 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1286 void *new, size_t newlen, size_t *retval, int flags)
1288 int oid[CTL_MAXNAME];
1289 size_t oidlen, plen;
1292 oid[0] = 0; /* sysctl internal magic */
1293 oid[1] = 3; /* name2oid */
1294 oidlen = sizeof(oid);
1296 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1297 (void *)name, strlen(name), &plen, flags);
1301 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1302 new, newlen, retval, flags);
1307 * Transfer function to/from user space.
1310 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1312 size_t i, len, origidx;
1315 origidx = req->oldidx;
1317 if (req->oldptr == NULL)
1320 * If we have not wired the user supplied buffer and we are currently
1321 * holding locks, drop a witness warning, as it's possible that
1322 * write operations to the user page can sleep.
1324 if (req->lock != REQ_WIRED)
1325 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1326 "sysctl_old_user()");
1328 len = req->validlen;
1332 if (i > len - origidx)
1334 if (req->lock == REQ_WIRED) {
1335 error = copyout_nofault(p, (char *)req->oldptr +
1338 error = copyout(p, (char *)req->oldptr + origidx, i);
1348 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1354 if (req->newlen - req->newidx < l)
1356 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1357 "sysctl_new_user()");
1358 error = copyin((char *)req->newptr + req->newidx, p, l);
1364 * Wire the user space destination buffer. If set to a value greater than
1365 * zero, the len parameter limits the maximum amount of wired memory.
1368 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1373 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1375 if (req->lock != REQ_WIRED && req->oldptr &&
1376 req->oldfunc == sysctl_old_user) {
1377 if (wiredlen != 0) {
1378 ret = vslock(req->oldptr, wiredlen);
1385 req->lock = REQ_WIRED;
1386 req->validlen = wiredlen;
1392 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1393 int *nindx, struct sysctl_req *req)
1395 struct sysctl_oid_list *lsp;
1396 struct sysctl_oid *oid;
1399 SYSCTL_ASSERT_XLOCKED();
1400 lsp = &sysctl__children;
1402 while (indx < CTL_MAXNAME) {
1403 SLIST_FOREACH(oid, lsp, oid_link) {
1404 if (oid->oid_number == name[indx])
1411 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1412 if (oid->oid_handler != NULL || indx == namelen) {
1416 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1417 ("%s found DYING node %p", __func__, oid));
1420 lsp = SYSCTL_CHILDREN(oid);
1421 } else if (indx == namelen) {
1425 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1426 ("%s found DYING node %p", __func__, oid));
1436 * Traverse our tree, and find the right node, execute whatever it points
1437 * to, and return the resulting error code.
1441 sysctl_root(SYSCTL_HANDLER_ARGS)
1443 struct sysctl_oid *oid;
1444 int error, indx, lvl;
1446 SYSCTL_ASSERT_XLOCKED();
1448 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1452 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1454 * You can't call a sysctl when it's a node, but has
1455 * no handler. Inform the user that it's a node.
1456 * The indx may or may not be the same as namelen.
1458 if (oid->oid_handler == NULL)
1462 /* Is this sysctl writable? */
1463 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1466 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1468 #ifdef CAPABILITY_MODE
1470 * If the process is in capability mode, then don't permit reading or
1471 * writing unless specifically granted for the node.
1473 if (IN_CAPABILITY_MODE(req->td)) {
1474 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1476 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1481 /* Is this sysctl sensitive to securelevels? */
1482 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1483 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1484 error = securelevel_gt(req->td->td_ucred, lvl);
1489 /* Is this sysctl writable by only privileged users? */
1490 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1493 if (oid->oid_kind & CTLFLAG_PRISON)
1494 priv = PRIV_SYSCTL_WRITEJAIL;
1496 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1497 prison_owns_vnet(req->td->td_ucred))
1498 priv = PRIV_SYSCTL_WRITEJAIL;
1501 priv = PRIV_SYSCTL_WRITE;
1502 error = priv_check(req->td, priv);
1507 if (!oid->oid_handler)
1510 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1511 arg1 = (int *)arg1 + indx;
1514 arg1 = oid->oid_arg1;
1515 arg2 = oid->oid_arg2;
1518 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1526 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1527 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1529 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1531 error = oid->oid_handler(oid, arg1, arg2, req);
1532 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1535 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1539 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1540 wakeup(&oid->oid_running);
1544 #ifndef _SYS_SYSPROTO_H_
1545 struct sysctl_args {
1555 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1557 int error, i, name[CTL_MAXNAME];
1560 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1563 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1567 error = userland_sysctl(td, name, uap->namelen,
1568 uap->old, uap->oldlenp, 0,
1569 uap->new, uap->newlen, &j, 0);
1570 if (error && error != ENOMEM)
1573 i = copyout(&j, uap->oldlenp, sizeof(j));
1581 * This is used from various compatibility syscalls too. That's why name
1582 * must be in kernel space.
1585 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1586 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1589 int error = 0, memlocked;
1590 struct sysctl_req req;
1592 bzero(&req, sizeof req);
1599 req.oldlen = *oldlenp;
1601 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1606 req.validlen = req.oldlen;
1609 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1615 if (!useracc(new, newlen, VM_PROT_READ))
1617 req.newlen = newlen;
1621 req.oldfunc = sysctl_old_user;
1622 req.newfunc = sysctl_new_user;
1623 req.lock = REQ_UNWIRED;
1626 if (KTRPOINT(curthread, KTR_SYSCTL))
1627 ktrsysctl(name, namelen);
1630 if (req.oldlen > PAGE_SIZE) {
1632 sx_xlock(&sysctlmemlock);
1635 CURVNET_SET(TD_TO_VNET(td));
1641 error = sysctl_root(0, name, namelen, &req);
1643 if (error != EAGAIN)
1645 kern_yield(PRI_USER);
1650 if (req.lock == REQ_WIRED && req.validlen > 0)
1651 vsunlock(req.oldptr, req.validlen);
1653 sx_xunlock(&sysctlmemlock);
1655 if (error && error != ENOMEM)
1659 if (req.oldptr && req.oldidx > req.validlen)
1660 *retval = req.validlen;
1662 *retval = req.oldidx;
1668 * Drain into a sysctl struct. The user buffer should be wired if a page
1669 * fault would cause issue.
1672 sbuf_sysctl_drain(void *arg, const char *data, int len)
1674 struct sysctl_req *req = arg;
1677 error = SYSCTL_OUT(req, data, len);
1678 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1679 return (error == 0 ? len : -error);
1683 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1684 struct sysctl_req *req)
1687 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1688 sbuf_set_drain(s, sbuf_sysctl_drain, req);