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_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
54 #include <sys/mutex.h>
57 #include <sys/sysproto.h>
60 #include <sys/ktrace.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_extern.h>
70 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
71 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
72 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
75 * The sysctllock protects the MIB tree. It also protects sysctl
76 * contexts used with dynamic sysctls. The sysctl_register_oid() and
77 * sysctl_unregister_oid() routines require the sysctllock to already
78 * be held, so the sysctl_lock() and sysctl_unlock() routines are
79 * provided for the few places in the kernel which need to use that
80 * API rather than using the dynamic API. Use of the dynamic API is
81 * strongly encouraged for most code.
83 * The sysctlmemlock is used to limit the amount of user memory wired for
84 * sysctl requests. This is implemented by serializing any userland
85 * sysctl requests larger than a single page via an exclusive lock.
87 static struct sx sysctllock;
88 static struct sx sysctlmemlock;
90 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
91 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
92 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
93 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
94 #define SYSCTL_SLEEP(ch, wmesg, timo) \
95 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
97 static int sysctl_root(SYSCTL_HANDLER_ARGS);
99 struct sysctl_oid_list sysctl__children; /* root list */
101 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
104 static struct sysctl_oid *
105 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
107 struct sysctl_oid *oidp;
109 SYSCTL_ASSERT_XLOCKED();
110 SLIST_FOREACH(oidp, list, oid_link) {
111 if (strcmp(oidp->oid_name, name) == 0) {
119 * Initialization of the MIB tree.
121 * Order by number in each list.
138 sysctl_register_oid(struct sysctl_oid *oidp)
140 struct sysctl_oid_list *parent = oidp->oid_parent;
141 struct sysctl_oid *p;
142 struct sysctl_oid *q;
145 * First check if another oid with the same name already
146 * exists in the parent's list.
148 SYSCTL_ASSERT_XLOCKED();
149 p = sysctl_find_oidname(oidp->oid_name, parent);
151 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
155 printf("can't re-use a leaf (%s)!\n", p->oid_name);
160 * If this oid has a number OID_AUTO, give it a number which
161 * is greater than any current oid.
162 * NOTE: DO NOT change the starting value here, change it in
163 * <sys/sysctl.h>, and make sure it is at least 256 to
164 * accomodate e.g. net.inet.raw as a static sysctl node.
166 if (oidp->oid_number == OID_AUTO) {
167 static int newoid = CTL_AUTO_START;
169 oidp->oid_number = newoid++;
170 if (newoid == 0x7fffffff)
171 panic("out of oids");
174 else if (oidp->oid_number >= CTL_AUTO_START) {
175 /* do not panic; this happens when unregistering sysctl sets */
176 printf("static sysctl oid too high: %d", oidp->oid_number);
181 * Insert the oid into the parent's list in order.
184 SLIST_FOREACH(p, parent, oid_link) {
185 if (oidp->oid_number < p->oid_number)
190 SLIST_INSERT_AFTER(q, oidp, oid_link);
192 SLIST_INSERT_HEAD(parent, oidp, oid_link);
196 sysctl_unregister_oid(struct sysctl_oid *oidp)
198 struct sysctl_oid *p;
201 SYSCTL_ASSERT_XLOCKED();
203 if (oidp->oid_number == OID_AUTO) {
206 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
208 SLIST_REMOVE(oidp->oid_parent, oidp,
209 sysctl_oid, oid_link);
217 * This can happen when a module fails to register and is
218 * being unloaded afterwards. It should not be a panic()
222 printf("%s: failed to unregister sysctl\n", __func__);
225 /* Initialize a new context to keep track of dynamically added sysctls. */
227 sysctl_ctx_init(struct sysctl_ctx_list *c)
235 * No locking here, the caller is responsible for not adding
236 * new nodes to a context until after this function has
243 /* Free the context, and destroy all dynamic oids registered in this context */
245 sysctl_ctx_free(struct sysctl_ctx_list *clist)
247 struct sysctl_ctx_entry *e, *e1;
252 * First perform a "dry run" to check if it's ok to remove oids.
254 * XXX This algorithm is a hack. But I don't know any
255 * XXX better solution for now...
258 TAILQ_FOREACH(e, clist, link) {
259 error = sysctl_remove_oid_locked(e->entry, 0, 0);
264 * Restore deregistered entries, either from the end,
265 * or from the place where error occured.
266 * e contains the entry that was not unregistered
269 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
271 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
273 sysctl_register_oid(e1->entry);
274 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
280 /* Now really delete the entries */
281 e = TAILQ_FIRST(clist);
283 e1 = TAILQ_NEXT(e, link);
284 error = sysctl_remove_oid_locked(e->entry, 1, 0);
286 panic("sysctl_remove_oid: corrupt tree, entry: %s",
288 free(e, M_SYSCTLOID);
295 /* Add an entry to the context */
296 struct sysctl_ctx_entry *
297 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
299 struct sysctl_ctx_entry *e;
301 SYSCTL_ASSERT_XLOCKED();
302 if (clist == NULL || oidp == NULL)
304 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
306 TAILQ_INSERT_HEAD(clist, e, link);
310 /* Find an entry in the context */
311 struct sysctl_ctx_entry *
312 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
314 struct sysctl_ctx_entry *e;
316 SYSCTL_ASSERT_XLOCKED();
317 if (clist == NULL || oidp == NULL)
319 TAILQ_FOREACH(e, clist, link) {
327 * Delete an entry from the context.
328 * NOTE: this function doesn't free oidp! You have to remove it
329 * with sysctl_remove_oid().
332 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
334 struct sysctl_ctx_entry *e;
336 if (clist == NULL || oidp == NULL)
339 e = sysctl_ctx_entry_find(clist, oidp);
341 TAILQ_REMOVE(clist, e, link);
343 free(e, M_SYSCTLOID);
352 * Remove dynamically created sysctl trees.
353 * oidp - top of the tree to be removed
354 * del - if 0 - just deregister, otherwise free up entries as well
355 * recurse - if != 0 traverse the subtree to be deleted
358 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
363 error = sysctl_remove_oid_locked(oidp, del, recurse);
369 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
371 struct sysctl_oid *p;
374 SYSCTL_ASSERT_XLOCKED();
377 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
378 printf("can't remove non-dynamic nodes!\n");
382 * WARNING: normal method to do this should be through
383 * sysctl_ctx_free(). Use recursing as the last resort
384 * method to purge your sysctl tree of leftovers...
385 * However, if some other code still references these nodes,
388 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
389 if (oidp->oid_refcnt == 1) {
390 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
393 error = sysctl_remove_oid_locked(p, del,
399 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
402 if (oidp->oid_refcnt > 1 ) {
405 if (oidp->oid_refcnt == 0) {
406 printf("Warning: bad oid_refcnt=%u (%s)!\n",
407 oidp->oid_refcnt, oidp->oid_name);
410 sysctl_unregister_oid(oidp);
413 * Wait for all threads running the handler to drain.
414 * This preserves the previous behavior when the
415 * sysctl lock was held across a handler invocation,
416 * and is necessary for module unload correctness.
418 while (oidp->oid_running > 0) {
419 oidp->oid_kind |= CTLFLAG_DYING;
420 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
423 free(__DECONST(char *, oidp->oid_descr),
425 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
426 free(oidp, M_SYSCTLOID);
433 * Create new sysctls at run time.
434 * clist may point to a valid context initialized with sysctl_ctx_init().
437 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
438 int number, const char *name, int kind, void *arg1, int arg2,
439 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
441 struct sysctl_oid *oidp;
443 /* You have to hook up somewhere.. */
446 /* Check if the node already exists, otherwise create it */
448 oidp = sysctl_find_oidname(name, parent);
450 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
452 /* Update the context */
454 sysctl_ctx_entry_add(clist, oidp);
459 printf("can't re-use a leaf (%s)!\n", name);
463 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
464 oidp->oid_parent = parent;
465 SLIST_NEXT(oidp, oid_link) = NULL;
466 oidp->oid_number = number;
467 oidp->oid_refcnt = 1;
468 oidp->oid_name = strdup(name, M_SYSCTLOID);
469 oidp->oid_handler = handler;
470 oidp->oid_kind = CTLFLAG_DYN | kind;
471 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
472 /* Allocate space for children */
473 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
474 M_SYSCTLOID, M_WAITOK));
475 SLIST_INIT(SYSCTL_CHILDREN(oidp));
477 oidp->oid_arg1 = arg1;
478 oidp->oid_arg2 = arg2;
482 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
483 /* Update the context, if used */
485 sysctl_ctx_entry_add(clist, oidp);
486 /* Register this oid */
487 sysctl_register_oid(oidp);
493 * Rename an existing oid.
496 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
501 newname = strdup(name, M_SYSCTLOID);
503 oldname = __DECONST(char *, oidp->oid_name);
504 oidp->oid_name = newname;
506 free(oldname, M_SYSCTLOID);
510 * Reparent an existing oid.
513 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
515 struct sysctl_oid *oidp;
518 if (oid->oid_parent == parent) {
522 oidp = sysctl_find_oidname(oid->oid_name, parent);
527 sysctl_unregister_oid(oid);
528 oid->oid_parent = parent;
529 oid->oid_number = OID_AUTO;
530 sysctl_register_oid(oid);
536 * Register the kernel's oids on startup.
538 SET_DECLARE(sysctl_set, struct sysctl_oid);
541 sysctl_register_all(void *arg)
543 struct sysctl_oid **oidp;
545 sx_init(&sysctlmemlock, "sysctl mem");
548 SET_FOREACH(oidp, sysctl_set)
549 sysctl_register_oid(*oidp);
552 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
557 * These functions implement a presently undocumented interface
558 * used by the sysctl program to walk the tree, and get the type
559 * so it can print the value.
560 * This interface is under work and consideration, and should probably
561 * be killed with a big axe by the first person who can find the time.
562 * (be aware though, that the proper interface isn't as obvious as it
563 * may seem, there are various conflicting requirements.
565 * {0,0} printf the entire MIB-tree.
566 * {0,1,...} return the name of the "..." OID.
567 * {0,2,...} return the next OID.
568 * {0,3} return the OID of the name in "new"
569 * {0,4,...} return the kind & format info for the "..." OID.
570 * {0,5,...} return the description the "..." OID.
575 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
578 struct sysctl_oid *oidp;
580 SYSCTL_ASSERT_XLOCKED();
581 SLIST_FOREACH(oidp, l, oid_link) {
586 printf("%d %s ", oidp->oid_number, oidp->oid_name);
589 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
590 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
592 if (oidp->oid_handler)
595 switch (oidp->oid_kind & CTLTYPE) {
598 if (!oidp->oid_handler) {
599 sysctl_sysctl_debug_dump_node(
600 oidp->oid_arg1, i+2);
603 case CTLTYPE_INT: printf(" Int\n"); break;
604 case CTLTYPE_STRING: printf(" String\n"); break;
605 case CTLTYPE_QUAD: printf(" Quad\n"); break;
606 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
607 default: printf("\n");
614 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
618 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
622 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
627 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
628 0, 0, sysctl_sysctl_debug, "-", "");
632 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
634 int *name = (int *) arg1;
635 u_int namelen = arg2;
637 struct sysctl_oid *oid;
638 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
644 snprintf(buf,sizeof(buf),"%d",*name);
646 error = SYSCTL_OUT(req, ".", 1);
648 error = SYSCTL_OUT(req, buf, strlen(buf));
656 SLIST_FOREACH(oid, lsp, oid_link) {
657 if (oid->oid_number != *name)
661 error = SYSCTL_OUT(req, ".", 1);
663 error = SYSCTL_OUT(req, oid->oid_name,
664 strlen(oid->oid_name));
671 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
674 if (oid->oid_handler)
677 lsp2 = SYSCTL_CHILDREN(oid);
682 error = SYSCTL_OUT(req, "", 1);
688 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
691 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
692 int *next, int *len, int level, struct sysctl_oid **oidpp)
694 struct sysctl_oid *oidp;
696 SYSCTL_ASSERT_XLOCKED();
698 SLIST_FOREACH(oidp, lsp, oid_link) {
699 *next = oidp->oid_number;
702 if (oidp->oid_kind & CTLFLAG_SKIP)
706 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
708 if (oidp->oid_handler)
709 /* We really should call the handler here...*/
711 lsp = SYSCTL_CHILDREN(oidp);
712 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
713 len, level+1, oidpp))
718 if (oidp->oid_number < *name)
721 if (oidp->oid_number > *name) {
722 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
724 if (oidp->oid_handler)
726 lsp = SYSCTL_CHILDREN(oidp);
727 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
728 next+1, len, level+1, oidpp))
732 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
735 if (oidp->oid_handler)
738 lsp = SYSCTL_CHILDREN(oidp);
739 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
740 len, level+1, oidpp))
751 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
753 int *name = (int *) arg1;
754 u_int namelen = arg2;
756 struct sysctl_oid *oid;
757 struct sysctl_oid_list *lsp = &sysctl__children;
758 int newoid[CTL_MAXNAME];
761 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
765 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
769 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
772 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
774 struct sysctl_oid *oidp;
775 struct sysctl_oid_list *lsp = &sysctl__children;
778 SYSCTL_ASSERT_XLOCKED();
780 for (*len = 0; *len < CTL_MAXNAME;) {
781 p = strsep(&name, ".");
783 oidp = SLIST_FIRST(lsp);
784 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
787 if (strcmp(p, oidp->oid_name) == 0)
790 *oid++ = oidp->oid_number;
793 if (name == NULL || *name == '\0') {
799 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
802 if (oidp->oid_handler)
805 lsp = SYSCTL_CHILDREN(oidp);
811 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
814 int error, oid[CTL_MAXNAME], len;
815 struct sysctl_oid *op = 0;
819 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
820 return (ENAMETOOLONG);
822 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
824 error = SYSCTL_IN(req, p, req->newlen);
830 p [req->newlen] = '\0';
833 error = name2oid(p, oid, &len, &op);
841 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
845 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
846 0, 0, sysctl_sysctl_name2oid, "I", "");
849 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
851 struct sysctl_oid *oid;
855 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
859 if (oid->oid_fmt == NULL) {
863 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
866 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
873 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
874 sysctl_sysctl_oidfmt, "");
877 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
879 struct sysctl_oid *oid;
883 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
887 if (oid->oid_descr == NULL) {
891 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
897 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
900 * Default "handler" functions.
904 * Handle an int, signed or unsigned.
906 * a variable: point arg1 at it.
907 * a constant: pass it in arg2.
911 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
913 int tmpout, error = 0;
916 * Attempt to get a coherent snapshot by making a copy of the data.
919 tmpout = *(int *)arg1;
922 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
924 if (error || !req->newptr)
930 error = SYSCTL_IN(req, arg1, sizeof(int));
935 * Based on on sysctl_handle_int() convert milliseconds into ticks.
936 * Note: this is used by TCP.
940 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
945 s = (int)((int64_t)tt * 1000 / hz);
947 error = sysctl_handle_int(oidp, &s, 0, req);
948 if (error || !req->newptr)
951 tt = (int)((int64_t)s * hz / 1000);
961 * Handle a long, signed or unsigned.
963 * a variable: point arg1 at it.
964 * a constant: pass it in arg2.
968 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
977 * Attempt to get a coherent snapshot by making a copy of the data.
980 tmplong = *(long *)arg1;
984 if (req->flags & SCTL_MASK32) {
986 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
989 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
991 if (error || !req->newptr)
997 else if (req->flags & SCTL_MASK32) {
998 error = SYSCTL_IN(req, &tmpint, sizeof(int));
999 *(long *)arg1 = (long)tmpint;
1003 error = SYSCTL_IN(req, arg1, sizeof(long));
1008 * Handle a 64 bit int, signed or unsigned.
1010 * a variable: point arg1 at it.
1011 * a constant: pass it in arg2.
1015 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1021 * Attempt to get a coherent snapshot by making a copy of the data.
1024 tmpout = *(uint64_t *)arg1;
1027 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1029 if (error || !req->newptr)
1035 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1040 * Handle our generic '\0' terminated 'C' string.
1042 * a variable string: point arg1 at it, arg2 is max length.
1043 * a constant string: point arg1 at it, arg2 is zero.
1047 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1054 * Attempt to get a coherent snapshot by copying to a
1055 * temporary kernel buffer.
1058 outlen = strlen((char *)arg1)+1;
1059 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1061 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1062 free(tmparg, M_SYSCTLTMP);
1066 error = SYSCTL_OUT(req, tmparg, outlen);
1067 free(tmparg, M_SYSCTLTMP);
1069 if (error || !req->newptr)
1072 if ((req->newlen - req->newidx) >= arg2) {
1075 arg2 = (req->newlen - req->newidx);
1076 error = SYSCTL_IN(req, arg1, arg2);
1077 ((char *)arg1)[arg2] = '\0';
1084 * Handle any kind of opaque data.
1085 * arg1 points to it, arg2 is the size.
1089 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1093 struct sysctl_req req2;
1096 * Attempt to get a coherent snapshot, by using the thread
1097 * pre-emption counter updated from within mi_switch() to
1098 * determine if we were pre-empted during a bcopy() or
1099 * copyout(). Make 3 attempts at doing this before giving up.
1100 * If we encounter an error, stop immediately.
1105 generation = curthread->td_generation;
1106 error = SYSCTL_OUT(req, arg1, arg2);
1110 if (generation != curthread->td_generation && tries < 3) {
1115 error = SYSCTL_IN(req, arg1, arg2);
1121 * Transfer functions to/from kernel space.
1122 * XXX: rather untested at this point
1125 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1131 if (req->oldlen <= req->oldidx)
1134 if (i > req->oldlen - req->oldidx)
1135 i = req->oldlen - req->oldidx;
1137 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1140 if (req->oldptr && i != l)
1146 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1150 if (req->newlen - req->newidx < l)
1152 bcopy((char *)req->newptr + req->newidx, p, l);
1158 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1159 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1162 struct sysctl_req req;
1164 bzero(&req, sizeof req);
1170 req.oldlen = *oldlenp;
1172 req.validlen = req.oldlen;
1179 req.newlen = newlen;
1183 req.oldfunc = sysctl_old_kernel;
1184 req.newfunc = sysctl_new_kernel;
1185 req.lock = REQ_LOCKED;
1188 error = sysctl_root(0, name, namelen, &req);
1191 if (req.lock == REQ_WIRED && req.validlen > 0)
1192 vsunlock(req.oldptr, req.validlen);
1194 if (error && error != ENOMEM)
1198 if (req.oldptr && req.oldidx > req.validlen)
1199 *retval = req.validlen;
1201 *retval = req.oldidx;
1207 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1208 void *new, size_t newlen, size_t *retval, int flags)
1210 int oid[CTL_MAXNAME];
1211 size_t oidlen, plen;
1214 oid[0] = 0; /* sysctl internal magic */
1215 oid[1] = 3; /* name2oid */
1216 oidlen = sizeof(oid);
1218 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1219 (void *)name, strlen(name), &plen, flags);
1223 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1224 new, newlen, retval, flags);
1229 * Transfer function to/from user space.
1232 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1234 size_t i, len, origidx;
1237 origidx = req->oldidx;
1239 if (req->oldptr == NULL)
1242 * If we have not wired the user supplied buffer and we are currently
1243 * holding locks, drop a witness warning, as it's possible that
1244 * write operations to the user page can sleep.
1246 if (req->lock != REQ_WIRED)
1247 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1248 "sysctl_old_user()");
1250 len = req->validlen;
1254 if (i > len - origidx)
1256 if (req->lock == REQ_WIRED) {
1257 error = copyout_nofault(p, (char *)req->oldptr +
1260 error = copyout(p, (char *)req->oldptr + origidx, i);
1270 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1276 if (req->newlen - req->newidx < l)
1278 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1279 "sysctl_new_user()");
1280 error = copyin((char *)req->newptr + req->newidx, p, l);
1286 * Wire the user space destination buffer. If set to a value greater than
1287 * zero, the len parameter limits the maximum amount of wired memory.
1290 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1295 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1297 if (req->lock == REQ_LOCKED && req->oldptr &&
1298 req->oldfunc == sysctl_old_user) {
1299 if (wiredlen != 0) {
1300 ret = vslock(req->oldptr, wiredlen);
1307 req->lock = REQ_WIRED;
1308 req->validlen = wiredlen;
1314 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1315 int *nindx, struct sysctl_req *req)
1317 struct sysctl_oid_list *lsp;
1318 struct sysctl_oid *oid;
1321 SYSCTL_ASSERT_XLOCKED();
1322 lsp = &sysctl__children;
1324 while (indx < CTL_MAXNAME) {
1325 SLIST_FOREACH(oid, lsp, oid_link) {
1326 if (oid->oid_number == name[indx])
1333 if (oid->oid_kind & CTLFLAG_NOLOCK)
1334 req->lock = REQ_UNLOCKED;
1335 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1336 if (oid->oid_handler != NULL || indx == namelen) {
1340 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1341 ("%s found DYING node %p", __func__, oid));
1344 lsp = SYSCTL_CHILDREN(oid);
1345 } else if (indx == namelen) {
1349 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1350 ("%s found DYING node %p", __func__, oid));
1360 * Traverse our tree, and find the right node, execute whatever it points
1361 * to, and return the resulting error code.
1365 sysctl_root(SYSCTL_HANDLER_ARGS)
1367 struct sysctl_oid *oid;
1368 int error, indx, lvl;
1370 SYSCTL_ASSERT_XLOCKED();
1372 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1376 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1378 * You can't call a sysctl when it's a node, but has
1379 * no handler. Inform the user that it's a node.
1380 * The indx may or may not be the same as namelen.
1382 if (oid->oid_handler == NULL)
1386 /* Is this sysctl writable? */
1387 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1390 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1392 /* Is this sysctl sensitive to securelevels? */
1393 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1394 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1395 error = securelevel_gt(req->td->td_ucred, lvl);
1400 /* Is this sysctl writable by only privileged users? */
1401 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1404 if (oid->oid_kind & CTLFLAG_PRISON)
1405 priv = PRIV_SYSCTL_WRITEJAIL;
1407 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1408 prison_owns_vnet(req->td->td_ucred))
1409 priv = PRIV_SYSCTL_WRITEJAIL;
1412 priv = PRIV_SYSCTL_WRITE;
1413 error = priv_check(req->td, priv);
1418 if (!oid->oid_handler)
1421 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1422 arg1 = (int *)arg1 + indx;
1425 arg1 = oid->oid_arg1;
1426 arg2 = oid->oid_arg2;
1429 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1437 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1439 error = oid->oid_handler(oid, arg1, arg2, req);
1440 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1443 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1447 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1448 wakeup(&oid->oid_running);
1452 #ifndef _SYS_SYSPROTO_H_
1453 struct sysctl_args {
1463 __sysctl(struct thread *td, struct sysctl_args *uap)
1465 int error, i, name[CTL_MAXNAME];
1468 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1471 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1475 error = userland_sysctl(td, name, uap->namelen,
1476 uap->old, uap->oldlenp, 0,
1477 uap->new, uap->newlen, &j, 0);
1478 if (error && error != ENOMEM)
1481 i = copyout(&j, uap->oldlenp, sizeof(j));
1489 * This is used from various compatibility syscalls too. That's why name
1490 * must be in kernel space.
1493 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1494 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1497 int error = 0, memlocked;
1498 struct sysctl_req req;
1500 bzero(&req, sizeof req);
1507 req.oldlen = *oldlenp;
1509 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1514 req.validlen = req.oldlen;
1517 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1523 if (!useracc(new, newlen, VM_PROT_READ))
1525 req.newlen = newlen;
1529 req.oldfunc = sysctl_old_user;
1530 req.newfunc = sysctl_new_user;
1531 req.lock = REQ_LOCKED;
1534 if (KTRPOINT(curthread, KTR_SYSCTL))
1535 ktrsysctl(name, namelen);
1538 if (req.oldlen > PAGE_SIZE) {
1540 sx_xlock(&sysctlmemlock);
1543 CURVNET_SET(TD_TO_VNET(td));
1549 error = sysctl_root(0, name, namelen, &req);
1551 if (error != EAGAIN)
1558 if (req.lock == REQ_WIRED && req.validlen > 0)
1559 vsunlock(req.oldptr, req.validlen);
1561 sx_xunlock(&sysctlmemlock);
1563 if (error && error != ENOMEM)
1567 if (req.oldptr && req.oldidx > req.validlen)
1568 *retval = req.validlen;
1570 *retval = req.oldidx;
1576 * Drain into a sysctl struct. The user buffer should be wired if a page
1577 * fault would cause issue.
1580 sbuf_sysctl_drain(void *arg, const char *data, int len)
1582 struct sysctl_req *req = arg;
1585 error = SYSCTL_OUT(req, data, len);
1586 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1587 return (error == 0 ? len : -error);
1591 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1592 struct sysctl_req *req)
1595 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1596 sbuf_set_drain(s, sbuf_sysctl_drain, req);