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_name(struct sysctl_oid *parent, const char *name,
370 int del, int recurse)
372 struct sysctl_oid *p, *tmp;
377 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
378 if (strcmp(p->oid_name, name) == 0) {
379 error = sysctl_remove_oid_locked(p, del, recurse);
390 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
392 struct sysctl_oid *p, *tmp;
395 SYSCTL_ASSERT_XLOCKED();
398 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
399 printf("can't remove non-dynamic nodes!\n");
403 * WARNING: normal method to do this should be through
404 * sysctl_ctx_free(). Use recursing as the last resort
405 * method to purge your sysctl tree of leftovers...
406 * However, if some other code still references these nodes,
409 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
410 if (oidp->oid_refcnt == 1) {
411 SLIST_FOREACH_SAFE(p,
412 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
415 error = sysctl_remove_oid_locked(p, del,
421 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
424 if (oidp->oid_refcnt > 1 ) {
427 if (oidp->oid_refcnt == 0) {
428 printf("Warning: bad oid_refcnt=%u (%s)!\n",
429 oidp->oid_refcnt, oidp->oid_name);
432 sysctl_unregister_oid(oidp);
435 * Wait for all threads running the handler to drain.
436 * This preserves the previous behavior when the
437 * sysctl lock was held across a handler invocation,
438 * and is necessary for module unload correctness.
440 while (oidp->oid_running > 0) {
441 oidp->oid_kind |= CTLFLAG_DYING;
442 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
445 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
446 free((void *)(uintptr_t)(const void *)oidp->oid_name,
448 free(oidp, M_SYSCTLOID);
454 * Create new sysctls at run time.
455 * clist may point to a valid context initialized with sysctl_ctx_init().
458 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
459 int number, const char *name, int kind, void *arg1, intptr_t arg2,
460 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
462 struct sysctl_oid *oidp;
466 /* You have to hook up somewhere.. */
469 /* Check if the node already exists, otherwise create it */
471 oidp = sysctl_find_oidname(name, parent);
473 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
475 /* Update the context */
477 sysctl_ctx_entry_add(clist, oidp);
482 printf("can't re-use a leaf (%s)!\n", name);
486 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
487 oidp->oid_parent = parent;
488 SLIST_NEXT(oidp, oid_link) = NULL;
489 oidp->oid_number = number;
490 oidp->oid_refcnt = 1;
492 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
493 bcopy(name, newname, len + 1);
495 oidp->oid_name = newname;
496 oidp->oid_handler = handler;
497 oidp->oid_kind = CTLFLAG_DYN | kind;
498 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
499 /* Allocate space for children */
500 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
501 M_SYSCTLOID, M_WAITOK));
502 SLIST_INIT(SYSCTL_CHILDREN(oidp));
503 oidp->oid_arg2 = arg2;
505 oidp->oid_arg1 = arg1;
506 oidp->oid_arg2 = arg2;
510 int len = strlen(descr) + 1;
511 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
513 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
515 /* Update the context, if used */
517 sysctl_ctx_entry_add(clist, oidp);
518 /* Register this oid */
519 sysctl_register_oid(oidp);
525 * Rename an existing oid.
528 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
535 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
536 bcopy(name, newname, len + 1);
539 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
540 oidp->oid_name = newname;
542 free(oldname, M_SYSCTLOID);
546 * Reparent an existing oid.
549 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
551 struct sysctl_oid *oidp;
554 if (oid->oid_parent == parent) {
558 oidp = sysctl_find_oidname(oid->oid_name, parent);
563 sysctl_unregister_oid(oid);
564 oid->oid_parent = parent;
565 oid->oid_number = OID_AUTO;
566 sysctl_register_oid(oid);
572 * Register the kernel's oids on startup.
574 SET_DECLARE(sysctl_set, struct sysctl_oid);
577 sysctl_register_all(void *arg)
579 struct sysctl_oid **oidp;
581 sx_init(&sysctlmemlock, "sysctl mem");
584 SET_FOREACH(oidp, sysctl_set)
585 sysctl_register_oid(*oidp);
588 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
593 * These functions implement a presently undocumented interface
594 * used by the sysctl program to walk the tree, and get the type
595 * so it can print the value.
596 * This interface is under work and consideration, and should probably
597 * be killed with a big axe by the first person who can find the time.
598 * (be aware though, that the proper interface isn't as obvious as it
599 * may seem, there are various conflicting requirements.
601 * {0,0} printf the entire MIB-tree.
602 * {0,1,...} return the name of the "..." OID.
603 * {0,2,...} return the next OID.
604 * {0,3} return the OID of the name in "new"
605 * {0,4,...} return the kind & format info for the "..." OID.
606 * {0,5,...} return the description the "..." OID.
611 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
614 struct sysctl_oid *oidp;
616 SYSCTL_ASSERT_XLOCKED();
617 SLIST_FOREACH(oidp, l, oid_link) {
622 printf("%d %s ", oidp->oid_number, oidp->oid_name);
625 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
626 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
628 if (oidp->oid_handler)
631 switch (oidp->oid_kind & CTLTYPE) {
634 if (!oidp->oid_handler) {
635 sysctl_sysctl_debug_dump_node(
636 oidp->oid_arg1, i+2);
639 case CTLTYPE_INT: printf(" Int\n"); break;
640 case CTLTYPE_UINT: printf(" u_int\n"); break;
641 case CTLTYPE_LONG: printf(" Long\n"); break;
642 case CTLTYPE_ULONG: printf(" u_long\n"); break;
643 case CTLTYPE_STRING: printf(" String\n"); break;
644 case CTLTYPE_U64: printf(" uint64_t\n"); break;
645 case CTLTYPE_S64: printf(" int64_t\n"); break;
646 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
647 default: printf("\n");
654 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
658 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
662 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
667 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
668 0, 0, sysctl_sysctl_debug, "-", "");
672 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
674 int *name = (int *) arg1;
675 u_int namelen = arg2;
677 struct sysctl_oid *oid;
678 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
684 snprintf(buf,sizeof(buf),"%d",*name);
686 error = SYSCTL_OUT(req, ".", 1);
688 error = SYSCTL_OUT(req, buf, strlen(buf));
696 SLIST_FOREACH(oid, lsp, oid_link) {
697 if (oid->oid_number != *name)
701 error = SYSCTL_OUT(req, ".", 1);
703 error = SYSCTL_OUT(req, oid->oid_name,
704 strlen(oid->oid_name));
711 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
714 if (oid->oid_handler)
717 lsp2 = SYSCTL_CHILDREN(oid);
722 error = SYSCTL_OUT(req, "", 1);
728 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
731 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
732 int *next, int *len, int level, struct sysctl_oid **oidpp)
734 struct sysctl_oid *oidp;
736 SYSCTL_ASSERT_XLOCKED();
738 SLIST_FOREACH(oidp, lsp, oid_link) {
739 *next = oidp->oid_number;
742 if (oidp->oid_kind & CTLFLAG_SKIP)
746 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
748 if (oidp->oid_handler)
749 /* We really should call the handler here...*/
751 lsp = SYSCTL_CHILDREN(oidp);
752 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
753 len, level+1, oidpp))
758 if (oidp->oid_number < *name)
761 if (oidp->oid_number > *name) {
762 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
764 if (oidp->oid_handler)
766 lsp = SYSCTL_CHILDREN(oidp);
767 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
768 next+1, len, level+1, oidpp))
772 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
775 if (oidp->oid_handler)
778 lsp = SYSCTL_CHILDREN(oidp);
779 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
780 len, level+1, oidpp))
791 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
793 int *name = (int *) arg1;
794 u_int namelen = arg2;
796 struct sysctl_oid *oid;
797 struct sysctl_oid_list *lsp = &sysctl__children;
798 int newoid[CTL_MAXNAME];
801 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
805 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
809 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
812 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
815 struct sysctl_oid *oidp;
816 struct sysctl_oid_list *lsp = &sysctl__children;
819 SYSCTL_ASSERT_XLOCKED();
824 p = name + strlen(name) - 1 ;
830 for (p = name; *p && *p != '.'; p++)
836 oidp = SLIST_FIRST(lsp);
838 while (oidp && *len < CTL_MAXNAME) {
839 if (strcmp(name, oidp->oid_name)) {
840 oidp = SLIST_NEXT(oidp, oid_link);
843 *oid++ = oidp->oid_number;
852 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
855 if (oidp->oid_handler)
858 lsp = SYSCTL_CHILDREN(oidp);
859 oidp = SLIST_FIRST(lsp);
861 for (p = name; *p && *p != '.'; p++)
871 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
874 int error, oid[CTL_MAXNAME], len = 0;
875 struct sysctl_oid *op = 0;
879 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
880 return (ENAMETOOLONG);
882 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
884 error = SYSCTL_IN(req, p, req->newlen);
890 p [req->newlen] = '\0';
893 error = name2oid(p, oid, &len, &op);
901 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
905 SYSCTL_PROC(_sysctl, 3, name2oid,
906 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
907 0, 0, sysctl_sysctl_name2oid, "I", "");
910 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
912 struct sysctl_oid *oid;
916 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
920 if (oid->oid_fmt == NULL) {
924 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
927 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
934 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
935 sysctl_sysctl_oidfmt, "");
938 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
940 struct sysctl_oid *oid;
944 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
948 if (oid->oid_descr == NULL) {
952 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
958 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
961 * Default "handler" functions.
965 * Handle an int, signed or unsigned.
967 * a variable: point arg1 at it.
968 * a constant: pass it in arg2.
972 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
974 int tmpout, error = 0;
977 * Attempt to get a coherent snapshot by making a copy of the data.
980 tmpout = *(int *)arg1;
983 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
985 if (error || !req->newptr)
991 error = SYSCTL_IN(req, arg1, sizeof(int));
996 * Based on on sysctl_handle_int() convert milliseconds into ticks.
997 * Note: this is used by TCP.
1001 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1006 s = (int)((int64_t)tt * 1000 / hz);
1008 error = sysctl_handle_int(oidp, &s, 0, req);
1009 if (error || !req->newptr)
1012 tt = (int)((int64_t)s * hz / 1000);
1022 * Handle a long, signed or unsigned. arg1 points to it.
1026 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1035 * Attempt to get a coherent snapshot by making a copy of the data.
1039 tmplong = *(long *)arg1;
1041 if (req->flags & SCTL_MASK32) {
1043 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1046 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1048 if (error || !req->newptr)
1052 if (req->flags & SCTL_MASK32) {
1053 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1054 *(long *)arg1 = (long)tmpint;
1057 error = SYSCTL_IN(req, arg1, sizeof(long));
1062 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
1065 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1071 * Attempt to get a coherent snapshot by making a copy of the data.
1075 tmpout = *(uint64_t *)arg1;
1076 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1078 if (error || !req->newptr)
1081 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1086 * Handle our generic '\0' terminated 'C' string.
1088 * a variable string: point arg1 at it, arg2 is max length.
1089 * a constant string: point arg1 at it, arg2 is zero.
1093 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1100 * Attempt to get a coherent snapshot by copying to a
1101 * temporary kernel buffer.
1104 outlen = strlen((char *)arg1)+1;
1105 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1107 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1108 free(tmparg, M_SYSCTLTMP);
1112 error = SYSCTL_OUT(req, tmparg, outlen);
1113 free(tmparg, M_SYSCTLTMP);
1115 if (error || !req->newptr)
1118 if ((req->newlen - req->newidx) >= arg2) {
1121 arg2 = (req->newlen - req->newidx);
1122 error = SYSCTL_IN(req, arg1, arg2);
1123 ((char *)arg1)[arg2] = '\0';
1130 * Handle any kind of opaque data.
1131 * arg1 points to it, arg2 is the size.
1135 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1139 struct sysctl_req req2;
1142 * Attempt to get a coherent snapshot, by using the thread
1143 * pre-emption counter updated from within mi_switch() to
1144 * determine if we were pre-empted during a bcopy() or
1145 * copyout(). Make 3 attempts at doing this before giving up.
1146 * If we encounter an error, stop immediately.
1151 generation = curthread->td_generation;
1152 error = SYSCTL_OUT(req, arg1, arg2);
1156 if (generation != curthread->td_generation && tries < 3) {
1161 error = SYSCTL_IN(req, arg1, arg2);
1167 * Transfer functions to/from kernel space.
1168 * XXX: rather untested at this point
1171 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1177 if (req->oldlen <= req->oldidx)
1180 if (i > req->oldlen - req->oldidx)
1181 i = req->oldlen - req->oldidx;
1183 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1186 if (req->oldptr && i != l)
1192 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1196 if (req->newlen - req->newidx < l)
1198 bcopy((char *)req->newptr + req->newidx, p, l);
1204 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1205 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1208 struct sysctl_req req;
1210 bzero(&req, sizeof req);
1216 req.oldlen = *oldlenp;
1218 req.validlen = req.oldlen;
1225 req.newlen = newlen;
1229 req.oldfunc = sysctl_old_kernel;
1230 req.newfunc = sysctl_new_kernel;
1231 req.lock = REQ_UNWIRED;
1234 error = sysctl_root(0, name, namelen, &req);
1237 if (req.lock == REQ_WIRED && req.validlen > 0)
1238 vsunlock(req.oldptr, req.validlen);
1240 if (error && error != ENOMEM)
1244 if (req.oldptr && req.oldidx > req.validlen)
1245 *retval = req.validlen;
1247 *retval = req.oldidx;
1253 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1254 void *new, size_t newlen, size_t *retval, int flags)
1256 int oid[CTL_MAXNAME];
1257 size_t oidlen, plen;
1260 oid[0] = 0; /* sysctl internal magic */
1261 oid[1] = 3; /* name2oid */
1262 oidlen = sizeof(oid);
1264 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1265 (void *)name, strlen(name), &plen, flags);
1269 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1270 new, newlen, retval, flags);
1275 * Transfer function to/from user space.
1278 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1281 size_t i, len, origidx;
1283 origidx = req->oldidx;
1285 if (req->oldptr == NULL)
1288 * If we have not wired the user supplied buffer and we are currently
1289 * holding locks, drop a witness warning, as it's possible that
1290 * write operations to the user page can sleep.
1292 if (req->lock != REQ_WIRED)
1293 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1294 "sysctl_old_user()");
1296 len = req->validlen;
1300 if (i > len - origidx)
1302 error = copyout(p, (char *)req->oldptr + origidx, i);
1312 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1318 if (req->newlen - req->newidx < l)
1320 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1321 "sysctl_new_user()");
1322 error = copyin((char *)req->newptr + req->newidx, p, l);
1328 * Wire the user space destination buffer. If set to a value greater than
1329 * zero, the len parameter limits the maximum amount of wired memory.
1332 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1337 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1339 if (req->lock != REQ_WIRED && req->oldptr &&
1340 req->oldfunc == sysctl_old_user) {
1341 if (wiredlen != 0) {
1342 ret = vslock(req->oldptr, wiredlen);
1349 req->lock = REQ_WIRED;
1350 req->validlen = wiredlen;
1356 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1357 int *nindx, struct sysctl_req *req)
1359 struct sysctl_oid_list *lsp;
1360 struct sysctl_oid *oid;
1363 SYSCTL_ASSERT_XLOCKED();
1364 lsp = &sysctl__children;
1366 while (indx < CTL_MAXNAME) {
1367 SLIST_FOREACH(oid, lsp, oid_link) {
1368 if (oid->oid_number == name[indx])
1375 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1376 if (oid->oid_handler != NULL || indx == namelen) {
1380 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1381 ("%s found DYING node %p", __func__, oid));
1384 lsp = SYSCTL_CHILDREN(oid);
1385 } else if (indx == namelen) {
1389 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1390 ("%s found DYING node %p", __func__, oid));
1400 * Traverse our tree, and find the right node, execute whatever it points
1401 * to, and return the resulting error code.
1405 sysctl_root(SYSCTL_HANDLER_ARGS)
1407 struct sysctl_oid *oid;
1408 int error, indx, lvl;
1410 SYSCTL_ASSERT_XLOCKED();
1412 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1416 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1418 * You can't call a sysctl when it's a node, but has
1419 * no handler. Inform the user that it's a node.
1420 * The indx may or may not be the same as namelen.
1422 if (oid->oid_handler == NULL)
1426 /* Is this sysctl writable? */
1427 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1430 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1432 /* Is this sysctl sensitive to securelevels? */
1433 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1434 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1435 error = securelevel_gt(req->td->td_ucred, lvl);
1440 /* Is this sysctl writable by only privileged users? */
1441 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1444 if (oid->oid_kind & CTLFLAG_PRISON)
1445 priv = PRIV_SYSCTL_WRITEJAIL;
1447 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1448 prison_owns_vnet(req->td->td_ucred))
1449 priv = PRIV_SYSCTL_WRITEJAIL;
1452 priv = PRIV_SYSCTL_WRITE;
1453 error = priv_check(req->td, priv);
1458 if (!oid->oid_handler)
1461 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1462 arg1 = (int *)arg1 + indx;
1465 arg1 = oid->oid_arg1;
1466 arg2 = oid->oid_arg2;
1469 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1477 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1479 error = oid->oid_handler(oid, arg1, arg2, req);
1480 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1483 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1487 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1488 wakeup(&oid->oid_running);
1492 #ifndef _SYS_SYSPROTO_H_
1493 struct sysctl_args {
1503 __sysctl(struct thread *td, struct sysctl_args *uap)
1505 int error, i, name[CTL_MAXNAME];
1508 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1511 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1515 error = userland_sysctl(td, name, uap->namelen,
1516 uap->old, uap->oldlenp, 0,
1517 uap->new, uap->newlen, &j, 0);
1518 if (error && error != ENOMEM)
1521 i = copyout(&j, uap->oldlenp, sizeof(j));
1529 * This is used from various compatibility syscalls too. That's why name
1530 * must be in kernel space.
1533 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1534 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1537 int error = 0, memlocked;
1538 struct sysctl_req req;
1540 bzero(&req, sizeof req);
1547 req.oldlen = *oldlenp;
1549 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1554 req.validlen = req.oldlen;
1557 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1563 if (!useracc(new, newlen, VM_PROT_READ))
1565 req.newlen = newlen;
1569 req.oldfunc = sysctl_old_user;
1570 req.newfunc = sysctl_new_user;
1571 req.lock = REQ_UNWIRED;
1574 if (KTRPOINT(curthread, KTR_SYSCTL))
1575 ktrsysctl(name, namelen);
1578 if (req.oldlen > PAGE_SIZE) {
1580 sx_xlock(&sysctlmemlock);
1583 CURVNET_SET(TD_TO_VNET(td));
1589 error = sysctl_root(0, name, namelen, &req);
1591 if (error != EAGAIN)
1593 kern_yield(PRI_USER);
1598 if (req.lock == REQ_WIRED && req.validlen > 0)
1599 vsunlock(req.oldptr, req.validlen);
1601 sx_xunlock(&sysctlmemlock);
1603 if (error && error != ENOMEM)
1607 if (req.oldptr && req.oldidx > req.validlen)
1608 *retval = req.validlen;
1610 *retval = req.oldidx;
1616 * Drain into a sysctl struct. The user buffer should be wired if a page
1617 * fault would cause issue.
1620 sbuf_sysctl_drain(void *arg, const char *data, int len)
1622 struct sysctl_req *req = arg;
1625 error = SYSCTL_OUT(req, data, len);
1626 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1627 return (error == 0 ? len : -error);
1631 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1632 struct sysctl_req *req)
1635 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1636 sbuf_set_drain(s, sbuf_sysctl_drain, req);