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((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
424 free((void *)(uintptr_t)(const void *)oidp->oid_name,
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;
445 /* You have to hook up somewhere.. */
448 /* Check if the node already exists, otherwise create it */
450 oidp = sysctl_find_oidname(name, parent);
452 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
454 /* Update the context */
456 sysctl_ctx_entry_add(clist, oidp);
461 printf("can't re-use a leaf (%s)!\n", name);
465 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
466 oidp->oid_parent = parent;
467 SLIST_NEXT(oidp, oid_link) = NULL;
468 oidp->oid_number = number;
469 oidp->oid_refcnt = 1;
471 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
472 bcopy(name, newname, len + 1);
474 oidp->oid_name = newname;
475 oidp->oid_handler = handler;
476 oidp->oid_kind = CTLFLAG_DYN | kind;
477 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
478 /* Allocate space for children */
479 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
480 M_SYSCTLOID, M_WAITOK));
481 SLIST_INIT(SYSCTL_CHILDREN(oidp));
483 oidp->oid_arg1 = arg1;
484 oidp->oid_arg2 = arg2;
488 int len = strlen(descr) + 1;
489 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
491 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
493 /* Update the context, if used */
495 sysctl_ctx_entry_add(clist, oidp);
496 /* Register this oid */
497 sysctl_register_oid(oidp);
503 * Rename an existing oid.
506 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
513 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
514 bcopy(name, newname, len + 1);
517 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
518 oidp->oid_name = newname;
520 free(oldname, M_SYSCTLOID);
524 * Reparent an existing oid.
527 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
529 struct sysctl_oid *oidp;
532 if (oid->oid_parent == parent) {
536 oidp = sysctl_find_oidname(oid->oid_name, parent);
541 sysctl_unregister_oid(oid);
542 oid->oid_parent = parent;
543 oid->oid_number = OID_AUTO;
544 sysctl_register_oid(oid);
550 * Register the kernel's oids on startup.
552 SET_DECLARE(sysctl_set, struct sysctl_oid);
555 sysctl_register_all(void *arg)
557 struct sysctl_oid **oidp;
559 sx_init(&sysctlmemlock, "sysctl mem");
562 SET_FOREACH(oidp, sysctl_set)
563 sysctl_register_oid(*oidp);
566 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
571 * These functions implement a presently undocumented interface
572 * used by the sysctl program to walk the tree, and get the type
573 * so it can print the value.
574 * This interface is under work and consideration, and should probably
575 * be killed with a big axe by the first person who can find the time.
576 * (be aware though, that the proper interface isn't as obvious as it
577 * may seem, there are various conflicting requirements.
579 * {0,0} printf the entire MIB-tree.
580 * {0,1,...} return the name of the "..." OID.
581 * {0,2,...} return the next OID.
582 * {0,3} return the OID of the name in "new"
583 * {0,4,...} return the kind & format info for the "..." OID.
584 * {0,5,...} return the description the "..." OID.
589 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
592 struct sysctl_oid *oidp;
594 SYSCTL_ASSERT_XLOCKED();
595 SLIST_FOREACH(oidp, l, oid_link) {
600 printf("%d %s ", oidp->oid_number, oidp->oid_name);
603 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
604 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
606 if (oidp->oid_handler)
609 switch (oidp->oid_kind & CTLTYPE) {
612 if (!oidp->oid_handler) {
613 sysctl_sysctl_debug_dump_node(
614 oidp->oid_arg1, i+2);
617 case CTLTYPE_INT: printf(" Int\n"); break;
618 case CTLTYPE_UINT: printf(" u_int\n"); break;
619 case CTLTYPE_LONG: printf(" Long\n"); break;
620 case CTLTYPE_ULONG: printf(" u_long\n"); break;
621 case CTLTYPE_STRING: printf(" String\n"); break;
622 case CTLTYPE_U64: printf(" uint64_t\n"); break;
623 case CTLTYPE_S64: printf(" int64_t\n"); break;
624 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
625 default: printf("\n");
632 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
636 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
640 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
645 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
646 0, 0, sysctl_sysctl_debug, "-", "");
650 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
652 int *name = (int *) arg1;
653 u_int namelen = arg2;
655 struct sysctl_oid *oid;
656 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
662 snprintf(buf,sizeof(buf),"%d",*name);
664 error = SYSCTL_OUT(req, ".", 1);
666 error = SYSCTL_OUT(req, buf, strlen(buf));
674 SLIST_FOREACH(oid, lsp, oid_link) {
675 if (oid->oid_number != *name)
679 error = SYSCTL_OUT(req, ".", 1);
681 error = SYSCTL_OUT(req, oid->oid_name,
682 strlen(oid->oid_name));
689 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
692 if (oid->oid_handler)
695 lsp2 = SYSCTL_CHILDREN(oid);
700 error = SYSCTL_OUT(req, "", 1);
706 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
709 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
710 int *next, int *len, int level, struct sysctl_oid **oidpp)
712 struct sysctl_oid *oidp;
714 SYSCTL_ASSERT_XLOCKED();
716 SLIST_FOREACH(oidp, lsp, oid_link) {
717 *next = oidp->oid_number;
720 if (oidp->oid_kind & CTLFLAG_SKIP)
724 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
726 if (oidp->oid_handler)
727 /* We really should call the handler here...*/
729 lsp = SYSCTL_CHILDREN(oidp);
730 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
731 len, level+1, oidpp))
736 if (oidp->oid_number < *name)
739 if (oidp->oid_number > *name) {
740 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
742 if (oidp->oid_handler)
744 lsp = SYSCTL_CHILDREN(oidp);
745 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
746 next+1, len, level+1, oidpp))
750 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
753 if (oidp->oid_handler)
756 lsp = SYSCTL_CHILDREN(oidp);
757 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
758 len, level+1, oidpp))
769 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
771 int *name = (int *) arg1;
772 u_int namelen = arg2;
774 struct sysctl_oid *oid;
775 struct sysctl_oid_list *lsp = &sysctl__children;
776 int newoid[CTL_MAXNAME];
779 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
783 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
787 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
790 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
793 struct sysctl_oid *oidp;
794 struct sysctl_oid_list *lsp = &sysctl__children;
797 SYSCTL_ASSERT_XLOCKED();
802 p = name + strlen(name) - 1 ;
808 for (p = name; *p && *p != '.'; p++)
814 oidp = SLIST_FIRST(lsp);
816 while (oidp && *len < CTL_MAXNAME) {
817 if (strcmp(name, oidp->oid_name)) {
818 oidp = SLIST_NEXT(oidp, oid_link);
821 *oid++ = oidp->oid_number;
830 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
833 if (oidp->oid_handler)
836 lsp = SYSCTL_CHILDREN(oidp);
837 oidp = SLIST_FIRST(lsp);
839 for (p = name; *p && *p != '.'; p++)
849 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
852 int error, oid[CTL_MAXNAME], len = 0;
853 struct sysctl_oid *op = 0;
857 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
858 return (ENAMETOOLONG);
860 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
862 error = SYSCTL_IN(req, p, req->newlen);
868 p [req->newlen] = '\0';
871 error = name2oid(p, oid, &len, &op);
879 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
883 SYSCTL_PROC(_sysctl, 3, name2oid,
884 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
885 0, 0, sysctl_sysctl_name2oid, "I", "");
888 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
890 struct sysctl_oid *oid;
894 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
898 if (oid->oid_fmt == NULL) {
902 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
905 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
912 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
913 sysctl_sysctl_oidfmt, "");
916 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
918 struct sysctl_oid *oid;
922 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
926 if (oid->oid_descr == NULL) {
930 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
936 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
939 * Default "handler" functions.
943 * Handle an int, signed or unsigned.
945 * a variable: point arg1 at it.
946 * a constant: pass it in arg2.
950 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
952 int tmpout, error = 0;
955 * Attempt to get a coherent snapshot by making a copy of the data.
958 tmpout = *(int *)arg1;
961 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
963 if (error || !req->newptr)
969 error = SYSCTL_IN(req, arg1, sizeof(int));
974 * Based on on sysctl_handle_int() convert milliseconds into ticks.
975 * Note: this is used by TCP.
979 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
984 s = (int)((int64_t)tt * 1000 / hz);
986 error = sysctl_handle_int(oidp, &s, 0, req);
987 if (error || !req->newptr)
990 tt = (int)((int64_t)s * hz / 1000);
1000 * Handle a long, signed or unsigned. arg1 points to it.
1004 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1013 * Attempt to get a coherent snapshot by making a copy of the data.
1017 tmplong = *(long *)arg1;
1019 if (req->flags & SCTL_MASK32) {
1021 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1024 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1026 if (error || !req->newptr)
1030 if (req->flags & SCTL_MASK32) {
1031 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1032 *(long *)arg1 = (long)tmpint;
1035 error = SYSCTL_IN(req, arg1, sizeof(long));
1040 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
1043 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1049 * Attempt to get a coherent snapshot by making a copy of the data.
1053 tmpout = *(uint64_t *)arg1;
1054 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1056 if (error || !req->newptr)
1059 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1064 * Handle our generic '\0' terminated 'C' string.
1066 * a variable string: point arg1 at it, arg2 is max length.
1067 * a constant string: point arg1 at it, arg2 is zero.
1071 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1078 * Attempt to get a coherent snapshot by copying to a
1079 * temporary kernel buffer.
1082 outlen = strlen((char *)arg1)+1;
1083 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1085 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1086 free(tmparg, M_SYSCTLTMP);
1090 error = SYSCTL_OUT(req, tmparg, outlen);
1091 free(tmparg, M_SYSCTLTMP);
1093 if (error || !req->newptr)
1096 if ((req->newlen - req->newidx) >= arg2) {
1099 arg2 = (req->newlen - req->newidx);
1100 error = SYSCTL_IN(req, arg1, arg2);
1101 ((char *)arg1)[arg2] = '\0';
1108 * Handle any kind of opaque data.
1109 * arg1 points to it, arg2 is the size.
1113 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1117 struct sysctl_req req2;
1120 * Attempt to get a coherent snapshot, by using the thread
1121 * pre-emption counter updated from within mi_switch() to
1122 * determine if we were pre-empted during a bcopy() or
1123 * copyout(). Make 3 attempts at doing this before giving up.
1124 * If we encounter an error, stop immediately.
1129 generation = curthread->td_generation;
1130 error = SYSCTL_OUT(req, arg1, arg2);
1134 if (generation != curthread->td_generation && tries < 3) {
1139 error = SYSCTL_IN(req, arg1, arg2);
1145 * Transfer functions to/from kernel space.
1146 * XXX: rather untested at this point
1149 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1155 if (req->oldlen <= req->oldidx)
1158 if (i > req->oldlen - req->oldidx)
1159 i = req->oldlen - req->oldidx;
1161 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1164 if (req->oldptr && i != l)
1170 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1174 if (req->newlen - req->newidx < l)
1176 bcopy((char *)req->newptr + req->newidx, p, l);
1182 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1183 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1186 struct sysctl_req req;
1188 bzero(&req, sizeof req);
1194 req.oldlen = *oldlenp;
1196 req.validlen = req.oldlen;
1203 req.newlen = newlen;
1207 req.oldfunc = sysctl_old_kernel;
1208 req.newfunc = sysctl_new_kernel;
1209 req.lock = REQ_UNWIRED;
1212 error = sysctl_root(0, name, namelen, &req);
1215 if (req.lock == REQ_WIRED && req.validlen > 0)
1216 vsunlock(req.oldptr, req.validlen);
1218 if (error && error != ENOMEM)
1222 if (req.oldptr && req.oldidx > req.validlen)
1223 *retval = req.validlen;
1225 *retval = req.oldidx;
1231 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1232 void *new, size_t newlen, size_t *retval, int flags)
1234 int oid[CTL_MAXNAME];
1235 size_t oidlen, plen;
1238 oid[0] = 0; /* sysctl internal magic */
1239 oid[1] = 3; /* name2oid */
1240 oidlen = sizeof(oid);
1242 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1243 (void *)name, strlen(name), &plen, flags);
1247 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1248 new, newlen, retval, flags);
1253 * Transfer function to/from user space.
1256 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1259 size_t i, len, origidx;
1261 origidx = req->oldidx;
1263 if (req->oldptr == NULL)
1266 * If we have not wired the user supplied buffer and we are currently
1267 * holding locks, drop a witness warning, as it's possible that
1268 * write operations to the user page can sleep.
1270 if (req->lock != REQ_WIRED)
1271 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1272 "sysctl_old_user()");
1274 len = req->validlen;
1278 if (i > len - origidx)
1280 error = copyout(p, (char *)req->oldptr + origidx, i);
1290 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1296 if (req->newlen - req->newidx < l)
1298 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1299 "sysctl_new_user()");
1300 error = copyin((char *)req->newptr + req->newidx, p, l);
1306 * Wire the user space destination buffer. If set to a value greater than
1307 * zero, the len parameter limits the maximum amount of wired memory.
1310 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1315 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1317 if (req->lock != REQ_WIRED && req->oldptr &&
1318 req->oldfunc == sysctl_old_user) {
1319 if (wiredlen != 0) {
1320 ret = vslock(req->oldptr, wiredlen);
1327 req->lock = REQ_WIRED;
1328 req->validlen = wiredlen;
1334 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1335 int *nindx, struct sysctl_req *req)
1337 struct sysctl_oid_list *lsp;
1338 struct sysctl_oid *oid;
1341 SYSCTL_ASSERT_XLOCKED();
1342 lsp = &sysctl__children;
1344 while (indx < CTL_MAXNAME) {
1345 SLIST_FOREACH(oid, lsp, oid_link) {
1346 if (oid->oid_number == name[indx])
1353 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1354 if (oid->oid_handler != NULL || indx == namelen) {
1358 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1359 ("%s found DYING node %p", __func__, oid));
1362 lsp = SYSCTL_CHILDREN(oid);
1363 } else if (indx == namelen) {
1367 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1368 ("%s found DYING node %p", __func__, oid));
1378 * Traverse our tree, and find the right node, execute whatever it points
1379 * to, and return the resulting error code.
1383 sysctl_root(SYSCTL_HANDLER_ARGS)
1385 struct sysctl_oid *oid;
1386 int error, indx, lvl;
1388 SYSCTL_ASSERT_XLOCKED();
1390 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1394 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1396 * You can't call a sysctl when it's a node, but has
1397 * no handler. Inform the user that it's a node.
1398 * The indx may or may not be the same as namelen.
1400 if (oid->oid_handler == NULL)
1404 /* Is this sysctl writable? */
1405 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1408 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1410 /* Is this sysctl sensitive to securelevels? */
1411 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1412 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1413 error = securelevel_gt(req->td->td_ucred, lvl);
1418 /* Is this sysctl writable by only privileged users? */
1419 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1422 if (oid->oid_kind & CTLFLAG_PRISON)
1423 priv = PRIV_SYSCTL_WRITEJAIL;
1425 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1426 prison_owns_vnet(req->td->td_ucred))
1427 priv = PRIV_SYSCTL_WRITEJAIL;
1430 priv = PRIV_SYSCTL_WRITE;
1431 error = priv_check(req->td, priv);
1436 if (!oid->oid_handler)
1439 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1440 arg1 = (int *)arg1 + indx;
1443 arg1 = oid->oid_arg1;
1444 arg2 = oid->oid_arg2;
1447 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1455 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1457 error = oid->oid_handler(oid, arg1, arg2, req);
1458 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1461 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1465 if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
1466 wakeup(&oid->oid_running);
1470 #ifndef _SYS_SYSPROTO_H_
1471 struct sysctl_args {
1481 __sysctl(struct thread *td, struct sysctl_args *uap)
1483 int error, i, name[CTL_MAXNAME];
1486 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1489 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1493 error = userland_sysctl(td, name, uap->namelen,
1494 uap->old, uap->oldlenp, 0,
1495 uap->new, uap->newlen, &j, 0);
1496 if (error && error != ENOMEM)
1499 i = copyout(&j, uap->oldlenp, sizeof(j));
1507 * This is used from various compatibility syscalls too. That's why name
1508 * must be in kernel space.
1511 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1512 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1515 int error = 0, memlocked;
1516 struct sysctl_req req;
1518 bzero(&req, sizeof req);
1525 req.oldlen = *oldlenp;
1527 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1532 req.validlen = req.oldlen;
1535 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1541 if (!useracc(new, newlen, VM_PROT_READ))
1543 req.newlen = newlen;
1547 req.oldfunc = sysctl_old_user;
1548 req.newfunc = sysctl_new_user;
1549 req.lock = REQ_UNWIRED;
1552 if (KTRPOINT(curthread, KTR_SYSCTL))
1553 ktrsysctl(name, namelen);
1556 if (req.oldlen > PAGE_SIZE) {
1558 sx_xlock(&sysctlmemlock);
1561 CURVNET_SET(TD_TO_VNET(td));
1567 error = sysctl_root(0, name, namelen, &req);
1569 if (error != EAGAIN)
1571 kern_yield(curthread->td_user_pri);
1576 if (req.lock == REQ_WIRED && req.validlen > 0)
1577 vsunlock(req.oldptr, req.validlen);
1579 sx_xunlock(&sysctlmemlock);
1581 if (error && error != ENOMEM)
1585 if (req.oldptr && req.oldidx > req.validlen)
1586 *retval = req.validlen;
1588 *retval = req.oldidx;
1594 * Drain into a sysctl struct. The user buffer should be wired if a page
1595 * fault would cause issue.
1598 sbuf_sysctl_drain(void *arg, const char *data, int len)
1600 struct sysctl_req *req = arg;
1603 error = SYSCTL_OUT(req, data, len);
1604 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1605 return (error == 0 ? len : -error);
1609 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1610 struct sysctl_req *req)
1613 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1614 sbuf_set_drain(s, sbuf_sysctl_drain, req);