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>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
53 #include <sys/mutex.h>
56 #include <sys/sysproto.h>
59 #include <sys/ktrace.h>
64 #include <security/mac/mac_framework.h>
67 #include <vm/vm_extern.h>
69 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
70 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
71 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
74 * The sysctllock protects the MIB tree. It also protects sysctl
75 * contexts used with dynamic sysctls. The sysctl_register_oid() and
76 * sysctl_unregister_oid() routines require the sysctllock to already
77 * be held, so the sysctl_lock() and sysctl_unlock() routines are
78 * provided for the few places in the kernel which need to use that
79 * API rather than using the dynamic API. Use of the dynamic API is
80 * strongly encouraged for most code.
82 * The sysctlmemlock is used to limit the amount of user memory wired for
83 * sysctl requests. This is implemented by serializing any userland
84 * sysctl requests larger than a single page via an exclusive lock.
86 static struct sx sysctllock;
87 static struct sx sysctlmemlock;
89 #define SYSCTL_SLOCK() sx_slock(&sysctllock)
90 #define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock)
91 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
92 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
93 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
94 #define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED)
95 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
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_LOCKED();
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_LOCKED();
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 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
414 free((void *)(uintptr_t)(const void *)oidp->oid_name,
416 free(oidp, M_SYSCTLOID);
423 * Create new sysctls at run time.
424 * clist may point to a valid context initialized with sysctl_ctx_init().
427 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
428 int number, const char *name, int kind, void *arg1, int arg2,
429 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
431 struct sysctl_oid *oidp;
435 /* You have to hook up somewhere.. */
438 /* Check if the node already exists, otherwise create it */
440 oidp = sysctl_find_oidname(name, parent);
442 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
444 /* Update the context */
446 sysctl_ctx_entry_add(clist, oidp);
451 printf("can't re-use a leaf (%s)!\n", name);
455 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
456 oidp->oid_parent = parent;
457 SLIST_NEXT(oidp, oid_link) = NULL;
458 oidp->oid_number = number;
459 oidp->oid_refcnt = 1;
461 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
462 bcopy(name, newname, len + 1);
464 oidp->oid_name = newname;
465 oidp->oid_handler = handler;
466 oidp->oid_kind = CTLFLAG_DYN | kind;
467 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
468 /* Allocate space for children */
469 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
470 M_SYSCTLOID, M_WAITOK));
471 SLIST_INIT(SYSCTL_CHILDREN(oidp));
473 oidp->oid_arg1 = arg1;
474 oidp->oid_arg2 = arg2;
478 int len = strlen(descr) + 1;
479 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
481 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
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)
503 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
504 bcopy(name, newname, len + 1);
507 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
508 oidp->oid_name = newname;
510 free(oldname, M_SYSCTLOID);
514 * Reparent an existing oid.
517 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
519 struct sysctl_oid *oidp;
522 if (oid->oid_parent == parent) {
526 oidp = sysctl_find_oidname(oid->oid_name, parent);
531 sysctl_unregister_oid(oid);
532 oid->oid_parent = parent;
533 oid->oid_number = OID_AUTO;
534 sysctl_register_oid(oid);
540 * Register the kernel's oids on startup.
542 SET_DECLARE(sysctl_set, struct sysctl_oid);
545 sysctl_register_all(void *arg)
547 struct sysctl_oid **oidp;
549 sx_init(&sysctlmemlock, "sysctl mem");
552 SET_FOREACH(oidp, sysctl_set)
553 sysctl_register_oid(*oidp);
556 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
561 * These functions implement a presently undocumented interface
562 * used by the sysctl program to walk the tree, and get the type
563 * so it can print the value.
564 * This interface is under work and consideration, and should probably
565 * be killed with a big axe by the first person who can find the time.
566 * (be aware though, that the proper interface isn't as obvious as it
567 * may seem, there are various conflicting requirements.
569 * {0,0} printf the entire MIB-tree.
570 * {0,1,...} return the name of the "..." OID.
571 * {0,2,...} return the next OID.
572 * {0,3} return the OID of the name in "new"
573 * {0,4,...} return the kind & format info for the "..." OID.
574 * {0,5,...} return the description the "..." OID.
579 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
582 struct sysctl_oid *oidp;
584 SYSCTL_ASSERT_LOCKED();
585 SLIST_FOREACH(oidp, l, oid_link) {
590 printf("%d %s ", oidp->oid_number, oidp->oid_name);
593 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
594 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
596 if (oidp->oid_handler)
599 switch (oidp->oid_kind & CTLTYPE) {
602 if (!oidp->oid_handler) {
603 sysctl_sysctl_debug_dump_node(
604 oidp->oid_arg1, i+2);
607 case CTLTYPE_INT: printf(" Int\n"); break;
608 case CTLTYPE_STRING: printf(" String\n"); break;
609 case CTLTYPE_QUAD: printf(" Quad\n"); break;
610 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
611 default: printf("\n");
618 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
622 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
625 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
629 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
630 0, 0, sysctl_sysctl_debug, "-", "");
634 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
636 int *name = (int *) arg1;
637 u_int namelen = arg2;
639 struct sysctl_oid *oid;
640 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
643 SYSCTL_ASSERT_LOCKED();
646 snprintf(buf,sizeof(buf),"%d",*name);
648 error = SYSCTL_OUT(req, ".", 1);
650 error = SYSCTL_OUT(req, buf, strlen(buf));
658 SLIST_FOREACH(oid, lsp, oid_link) {
659 if (oid->oid_number != *name)
663 error = SYSCTL_OUT(req, ".", 1);
665 error = SYSCTL_OUT(req, oid->oid_name,
666 strlen(oid->oid_name));
673 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
676 if (oid->oid_handler)
679 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
684 return (SYSCTL_OUT(req, "", 1));
687 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
690 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
691 int *next, int *len, int level, struct sysctl_oid **oidpp)
693 struct sysctl_oid *oidp;
695 SYSCTL_ASSERT_LOCKED();
697 SLIST_FOREACH(oidp, lsp, oid_link) {
698 *next = oidp->oid_number;
701 if (oidp->oid_kind & CTLFLAG_SKIP)
705 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
707 if (oidp->oid_handler)
708 /* We really should call the handler here...*/
710 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
711 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
712 len, level+1, oidpp))
717 if (oidp->oid_number < *name)
720 if (oidp->oid_number > *name) {
721 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
723 if (oidp->oid_handler)
725 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
726 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
727 next+1, len, level+1, oidpp))
731 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
734 if (oidp->oid_handler)
737 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
738 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
739 len, level+1, oidpp))
750 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
752 int *name = (int *) arg1;
753 u_int namelen = arg2;
755 struct sysctl_oid *oid;
756 struct sysctl_oid_list *lsp = &sysctl__children;
757 int newoid[CTL_MAXNAME];
759 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
762 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
766 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
769 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
772 struct sysctl_oid *oidp;
773 struct sysctl_oid_list *lsp = &sysctl__children;
776 SYSCTL_ASSERT_LOCKED();
781 p = name + strlen(name) - 1 ;
787 for (p = name; *p && *p != '.'; p++)
793 oidp = SLIST_FIRST(lsp);
795 while (oidp && *len < CTL_MAXNAME) {
796 if (strcmp(name, oidp->oid_name)) {
797 oidp = SLIST_NEXT(oidp, oid_link);
800 *oid++ = oidp->oid_number;
809 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
812 if (oidp->oid_handler)
815 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
816 oidp = SLIST_FIRST(lsp);
818 for (p = name; *p && *p != '.'; p++)
828 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
831 int error, oid[CTL_MAXNAME], len;
832 struct sysctl_oid *op = 0;
834 SYSCTL_ASSERT_LOCKED();
838 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
839 return (ENAMETOOLONG);
841 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
843 error = SYSCTL_IN(req, p, req->newlen);
849 p [req->newlen] = '\0';
851 error = name2oid(p, oid, &len, &op);
858 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
862 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
863 0, 0, sysctl_sysctl_name2oid, "I", "");
866 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
868 struct sysctl_oid *oid;
871 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
877 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
880 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
885 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
886 sysctl_sysctl_oidfmt, "");
889 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
891 struct sysctl_oid *oid;
894 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
900 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
904 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
907 * Default "handler" functions.
911 * Handle an int, signed or unsigned.
913 * a variable: point arg1 at it.
914 * a constant: pass it in arg2.
918 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
920 int tmpout, error = 0;
923 * Attempt to get a coherent snapshot by making a copy of the data.
926 tmpout = *(int *)arg1;
929 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
931 if (error || !req->newptr)
937 error = SYSCTL_IN(req, arg1, sizeof(int));
942 * Based on on sysctl_handle_int() convert milliseconds into ticks.
943 * Note: this is used by TCP.
947 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
952 s = (int)((int64_t)tt * 1000 / hz);
954 error = sysctl_handle_int(oidp, &s, 0, req);
955 if (error || !req->newptr)
958 tt = (int)((int64_t)s * hz / 1000);
968 * Handle a long, signed or unsigned. arg1 points to it.
972 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
981 * Attempt to get a coherent snapshot by making a copy of the data.
985 tmplong = *(long *)arg1;
987 if (req->flags & SCTL_MASK32) {
989 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
992 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
994 if (error || !req->newptr)
998 if (req->flags & SCTL_MASK32) {
999 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1000 *(long *)arg1 = (long)tmpint;
1003 error = SYSCTL_IN(req, arg1, sizeof(long));
1008 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
1012 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1018 * Attempt to get a coherent snapshot by making a copy of the data.
1022 tmpout = *(uint64_t *)arg1;
1023 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1025 if (error || !req->newptr)
1028 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1033 * Handle our generic '\0' terminated 'C' string.
1035 * a variable string: point arg1 at it, arg2 is max length.
1036 * a constant string: point arg1 at it, arg2 is zero.
1040 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1047 * Attempt to get a coherent snapshot by copying to a
1048 * temporary kernel buffer.
1051 outlen = strlen((char *)arg1)+1;
1052 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1054 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1055 free(tmparg, M_SYSCTLTMP);
1059 error = SYSCTL_OUT(req, tmparg, outlen);
1060 free(tmparg, M_SYSCTLTMP);
1062 if (error || !req->newptr)
1065 if ((req->newlen - req->newidx) >= arg2) {
1068 arg2 = (req->newlen - req->newidx);
1069 error = SYSCTL_IN(req, arg1, arg2);
1070 ((char *)arg1)[arg2] = '\0';
1077 * Handle any kind of opaque data.
1078 * arg1 points to it, arg2 is the size.
1082 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1086 struct sysctl_req req2;
1089 * Attempt to get a coherent snapshot, by using the thread
1090 * pre-emption counter updated from within mi_switch() to
1091 * determine if we were pre-empted during a bcopy() or
1092 * copyout(). Make 3 attempts at doing this before giving up.
1093 * If we encounter an error, stop immediately.
1098 generation = curthread->td_generation;
1099 error = SYSCTL_OUT(req, arg1, arg2);
1103 if (generation != curthread->td_generation && tries < 3) {
1108 error = SYSCTL_IN(req, arg1, arg2);
1114 * Transfer functions to/from kernel space.
1115 * XXX: rather untested at this point
1118 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1124 if (req->oldlen <= req->oldidx)
1127 if (i > req->oldlen - req->oldidx)
1128 i = req->oldlen - req->oldidx;
1130 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1133 if (req->oldptr && i != l)
1139 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1143 if (req->newlen - req->newidx < l)
1145 bcopy((char *)req->newptr + req->newidx, p, l);
1151 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1152 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1155 struct sysctl_req req;
1157 bzero(&req, sizeof req);
1163 req.oldlen = *oldlenp;
1165 req.validlen = req.oldlen;
1172 req.newlen = newlen;
1176 req.oldfunc = sysctl_old_kernel;
1177 req.newfunc = sysctl_new_kernel;
1178 req.lock = REQ_LOCKED;
1181 error = sysctl_root(0, name, namelen, &req);
1184 if (req.lock == REQ_WIRED && req.validlen > 0)
1185 vsunlock(req.oldptr, req.validlen);
1187 if (error && error != ENOMEM)
1191 if (req.oldptr && req.oldidx > req.validlen)
1192 *retval = req.validlen;
1194 *retval = req.oldidx;
1200 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1201 void *new, size_t newlen, size_t *retval, int flags)
1203 int oid[CTL_MAXNAME];
1204 size_t oidlen, plen;
1207 oid[0] = 0; /* sysctl internal magic */
1208 oid[1] = 3; /* name2oid */
1209 oidlen = sizeof(oid);
1211 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1212 (void *)name, strlen(name), &plen, flags);
1216 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1217 new, newlen, retval, flags);
1222 * Transfer function to/from user space.
1225 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1228 size_t i, len, origidx;
1230 origidx = req->oldidx;
1232 if (req->oldptr == NULL)
1235 * If we have not wired the user supplied buffer and we are currently
1236 * holding locks, drop a witness warning, as it's possible that
1237 * write operations to the user page can sleep.
1239 if (req->lock != REQ_WIRED)
1240 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1241 "sysctl_old_user()");
1243 len = req->validlen;
1247 if (i > len - origidx)
1249 error = copyout(p, (char *)req->oldptr + origidx, i);
1259 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1265 if (req->newlen - req->newidx < l)
1267 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1268 "sysctl_new_user()");
1269 error = copyin((char *)req->newptr + req->newidx, p, l);
1275 * Wire the user space destination buffer. If set to a value greater than
1276 * zero, the len parameter limits the maximum amount of wired memory.
1279 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1284 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1286 if (req->lock == REQ_LOCKED && req->oldptr &&
1287 req->oldfunc == sysctl_old_user) {
1288 if (wiredlen != 0) {
1289 ret = vslock(req->oldptr, wiredlen);
1296 req->lock = REQ_WIRED;
1297 req->validlen = wiredlen;
1303 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1304 int *nindx, struct sysctl_req *req)
1306 struct sysctl_oid *oid;
1309 SYSCTL_ASSERT_LOCKED();
1310 oid = SLIST_FIRST(&sysctl__children);
1312 while (oid && indx < CTL_MAXNAME) {
1313 if (oid->oid_number == name[indx]) {
1315 if (oid->oid_kind & CTLFLAG_NOLOCK)
1316 req->lock = REQ_UNLOCKED;
1317 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1318 if (oid->oid_handler != NULL ||
1326 (struct sysctl_oid_list *)oid->oid_arg1);
1327 } else if (indx == namelen) {
1336 oid = SLIST_NEXT(oid, oid_link);
1343 * Traverse our tree, and find the right node, execute whatever it points
1344 * to, and return the resulting error code.
1348 sysctl_root(SYSCTL_HANDLER_ARGS)
1350 struct sysctl_oid *oid;
1351 int error, indx, lvl;
1353 SYSCTL_ASSERT_LOCKED();
1355 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1359 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1361 * You can't call a sysctl when it's a node, but has
1362 * no handler. Inform the user that it's a node.
1363 * The indx may or may not be the same as namelen.
1365 if (oid->oid_handler == NULL)
1369 /* Is this sysctl writable? */
1370 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1373 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1375 /* Is this sysctl sensitive to securelevels? */
1376 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1377 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1378 error = securelevel_gt(req->td->td_ucred, lvl);
1383 /* Is this sysctl writable by only privileged users? */
1384 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1387 if (oid->oid_kind & CTLFLAG_PRISON)
1388 priv = PRIV_SYSCTL_WRITEJAIL;
1390 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1391 prison_owns_vnet(req->td->td_ucred))
1392 priv = PRIV_SYSCTL_WRITEJAIL;
1395 priv = PRIV_SYSCTL_WRITE;
1396 error = priv_check(req->td, priv);
1401 if (!oid->oid_handler)
1404 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1405 arg1 = (int *)arg1 + indx;
1408 arg1 = oid->oid_arg1;
1409 arg2 = oid->oid_arg2;
1412 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1417 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1419 error = oid->oid_handler(oid, arg1, arg2, req);
1420 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1426 #ifndef _SYS_SYSPROTO_H_
1427 struct sysctl_args {
1437 __sysctl(struct thread *td, struct sysctl_args *uap)
1439 int error, i, name[CTL_MAXNAME];
1442 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1445 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1449 error = userland_sysctl(td, name, uap->namelen,
1450 uap->old, uap->oldlenp, 0,
1451 uap->new, uap->newlen, &j, 0);
1452 if (error && error != ENOMEM)
1455 i = copyout(&j, uap->oldlenp, sizeof(j));
1463 * This is used from various compatibility syscalls too. That's why name
1464 * must be in kernel space.
1467 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1468 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1471 int error = 0, memlocked;
1472 struct sysctl_req req;
1474 bzero(&req, sizeof req);
1481 req.oldlen = *oldlenp;
1483 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1488 req.validlen = req.oldlen;
1491 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1497 if (!useracc(new, newlen, VM_PROT_READ))
1499 req.newlen = newlen;
1503 req.oldfunc = sysctl_old_user;
1504 req.newfunc = sysctl_new_user;
1505 req.lock = REQ_LOCKED;
1508 if (KTRPOINT(curthread, KTR_SYSCTL))
1509 ktrsysctl(name, namelen);
1512 if (req.oldlen > PAGE_SIZE) {
1514 sx_xlock(&sysctlmemlock);
1517 CURVNET_SET(TD_TO_VNET(td));
1523 error = sysctl_root(0, name, namelen, &req);
1525 if (error != EAGAIN)
1532 if (req.lock == REQ_WIRED && req.validlen > 0)
1533 vsunlock(req.oldptr, req.validlen);
1535 sx_xunlock(&sysctlmemlock);
1537 if (error && error != ENOMEM)
1541 if (req.oldptr && req.oldidx > req.validlen)
1542 *retval = req.validlen;
1544 *retval = req.oldidx;
1550 * Drain into a sysctl struct. The user buffer must be wired.
1553 sbuf_sysctl_drain(void *arg, const char *data, int len)
1555 struct sysctl_req *req = arg;
1558 error = SYSCTL_OUT(req, data, len);
1559 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1560 return (error == 0 ? len : -error);
1564 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1565 struct sysctl_req *req)
1568 /* Wire the user buffer, so we can write without blocking. */
1569 sysctl_wire_old_buffer(req, 0);
1571 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1572 sbuf_set_drain(s, sbuf_sysctl_drain, req);