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"
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>
52 #include <sys/mutex.h>
54 #include <sys/sysproto.h>
57 #include <security/mac/mac_framework.h>
60 #include <vm/vm_extern.h>
62 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
63 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
64 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
67 * The sysctllock protects the MIB tree. It also protects sysctl
68 * contexts used with dynamic sysctls. The sysctl_register_oid() and
69 * sysctl_unregister_oid() routines require the sysctllock to already
70 * be held, so the sysctl_lock() and sysctl_unlock() routines are
71 * provided for the few places in the kernel which need to use that
72 * API rather than using the dynamic API. Use of the dynamic API is
73 * strongly encouraged for most code.
75 * This lock is also used to serialize userland sysctl requests. Some
76 * sysctls wire user memory, and serializing the requests limits the
77 * amount of wired user memory in use.
79 static struct sx sysctllock;
81 #define SYSCTL_SLOCK() sx_slock(&sysctllock)
82 #define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock)
83 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
84 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
85 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
86 #define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED)
87 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
89 static int sysctl_root(SYSCTL_HANDLER_ARGS);
91 struct sysctl_oid_list sysctl__children; /* root list */
93 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
96 static struct sysctl_oid *
97 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
99 struct sysctl_oid *oidp;
101 SYSCTL_ASSERT_LOCKED();
102 SLIST_FOREACH(oidp, list, oid_link) {
103 if (strcmp(oidp->oid_name, name) == 0) {
111 * Initialization of the MIB tree.
113 * Order by number in each list.
130 sysctl_register_oid(struct sysctl_oid *oidp)
132 struct sysctl_oid_list *parent = oidp->oid_parent;
133 struct sysctl_oid *p;
134 struct sysctl_oid *q;
137 * First check if another oid with the same name already
138 * exists in the parent's list.
140 SYSCTL_ASSERT_XLOCKED();
141 p = sysctl_find_oidname(oidp->oid_name, parent);
143 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
147 printf("can't re-use a leaf (%s)!\n", p->oid_name);
152 * If this oid has a number OID_AUTO, give it a number which
153 * is greater than any current oid.
154 * NOTE: DO NOT change the starting value here, change it in
155 * <sys/sysctl.h>, and make sure it is at least 256 to
156 * accomodate e.g. net.inet.raw as a static sysctl node.
158 if (oidp->oid_number == OID_AUTO) {
159 static int newoid = CTL_AUTO_START;
161 oidp->oid_number = newoid++;
162 if (newoid == 0x7fffffff)
163 panic("out of oids");
166 else if (oidp->oid_number >= CTL_AUTO_START) {
167 /* do not panic; this happens when unregistering sysctl sets */
168 printf("static sysctl oid too high: %d", oidp->oid_number);
173 * Insert the oid into the parent's list in order.
176 SLIST_FOREACH(p, parent, oid_link) {
177 if (oidp->oid_number < p->oid_number)
182 SLIST_INSERT_AFTER(q, oidp, oid_link);
184 SLIST_INSERT_HEAD(parent, oidp, oid_link);
188 sysctl_unregister_oid(struct sysctl_oid *oidp)
190 struct sysctl_oid *p;
193 SYSCTL_ASSERT_XLOCKED();
195 if (oidp->oid_number == OID_AUTO) {
198 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
200 SLIST_REMOVE(oidp->oid_parent, oidp,
201 sysctl_oid, oid_link);
209 * This can happen when a module fails to register and is
210 * being unloaded afterwards. It should not be a panic()
214 printf("%s: failed to unregister sysctl\n", __func__);
217 /* Initialize a new context to keep track of dynamically added sysctls. */
219 sysctl_ctx_init(struct sysctl_ctx_list *c)
227 * No locking here, the caller is responsible for not adding
228 * new nodes to a context until after this function has
235 /* Free the context, and destroy all dynamic oids registered in this context */
237 sysctl_ctx_free(struct sysctl_ctx_list *clist)
239 struct sysctl_ctx_entry *e, *e1;
244 * First perform a "dry run" to check if it's ok to remove oids.
246 * XXX This algorithm is a hack. But I don't know any
247 * XXX better solution for now...
250 TAILQ_FOREACH(e, clist, link) {
251 error = sysctl_remove_oid_locked(e->entry, 0, 0);
256 * Restore deregistered entries, either from the end,
257 * or from the place where error occured.
258 * e contains the entry that was not unregistered
261 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
263 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
265 sysctl_register_oid(e1->entry);
266 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
272 /* Now really delete the entries */
273 e = TAILQ_FIRST(clist);
275 e1 = TAILQ_NEXT(e, link);
276 error = sysctl_remove_oid_locked(e->entry, 1, 0);
278 panic("sysctl_remove_oid: corrupt tree, entry: %s",
280 free(e, M_SYSCTLOID);
287 /* Add an entry to the context */
288 struct sysctl_ctx_entry *
289 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
291 struct sysctl_ctx_entry *e;
293 SYSCTL_ASSERT_XLOCKED();
294 if (clist == NULL || oidp == NULL)
296 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
298 TAILQ_INSERT_HEAD(clist, e, link);
302 /* Find an entry in the context */
303 struct sysctl_ctx_entry *
304 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
306 struct sysctl_ctx_entry *e;
308 SYSCTL_ASSERT_LOCKED();
309 if (clist == NULL || oidp == NULL)
311 TAILQ_FOREACH(e, clist, link) {
319 * Delete an entry from the context.
320 * NOTE: this function doesn't free oidp! You have to remove it
321 * with sysctl_remove_oid().
324 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
326 struct sysctl_ctx_entry *e;
328 if (clist == NULL || oidp == NULL)
331 e = sysctl_ctx_entry_find(clist, oidp);
333 TAILQ_REMOVE(clist, e, link);
335 free(e, M_SYSCTLOID);
344 * Remove dynamically created sysctl trees.
345 * oidp - top of the tree to be removed
346 * del - if 0 - just deregister, otherwise free up entries as well
347 * recurse - if != 0 traverse the subtree to be deleted
350 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
355 error = sysctl_remove_oid_locked(oidp, del, recurse);
361 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
363 struct sysctl_oid *p;
366 SYSCTL_ASSERT_XLOCKED();
369 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
370 printf("can't remove non-dynamic nodes!\n");
374 * WARNING: normal method to do this should be through
375 * sysctl_ctx_free(). Use recursing as the last resort
376 * method to purge your sysctl tree of leftovers...
377 * However, if some other code still references these nodes,
380 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
381 if (oidp->oid_refcnt == 1) {
382 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
385 error = sysctl_remove_oid_locked(p, del,
391 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
394 if (oidp->oid_refcnt > 1 ) {
397 if (oidp->oid_refcnt == 0) {
398 printf("Warning: bad oid_refcnt=%u (%s)!\n",
399 oidp->oid_refcnt, oidp->oid_name);
402 sysctl_unregister_oid(oidp);
405 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
406 free((void *)(uintptr_t)(const void *)oidp->oid_name,
408 free(oidp, M_SYSCTLOID);
415 * Create new sysctls at run time.
416 * clist may point to a valid context initialized with sysctl_ctx_init().
419 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
420 int number, const char *name, int kind, void *arg1, int arg2,
421 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
423 struct sysctl_oid *oidp;
427 /* You have to hook up somewhere.. */
430 /* Check if the node already exists, otherwise create it */
432 oidp = sysctl_find_oidname(name, parent);
434 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
436 /* Update the context */
438 sysctl_ctx_entry_add(clist, oidp);
443 printf("can't re-use a leaf (%s)!\n", name);
447 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
448 oidp->oid_parent = parent;
449 SLIST_NEXT(oidp, oid_link) = NULL;
450 oidp->oid_number = number;
451 oidp->oid_refcnt = 1;
453 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
454 bcopy(name, newname, len + 1);
456 oidp->oid_name = newname;
457 oidp->oid_handler = handler;
458 oidp->oid_kind = CTLFLAG_DYN | kind;
459 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
460 /* Allocate space for children */
461 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
462 M_SYSCTLOID, M_WAITOK));
463 SLIST_INIT(SYSCTL_CHILDREN(oidp));
465 oidp->oid_arg1 = arg1;
466 oidp->oid_arg2 = arg2;
470 int len = strlen(descr) + 1;
471 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
473 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
475 /* Update the context, if used */
477 sysctl_ctx_entry_add(clist, oidp);
478 /* Register this oid */
479 sysctl_register_oid(oidp);
485 * Rename an existing oid.
488 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
495 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
496 bcopy(name, newname, len + 1);
499 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
500 oidp->oid_name = newname;
502 free(oldname, M_SYSCTLOID);
506 * Reparent an existing oid.
509 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
511 struct sysctl_oid *oidp;
514 if (oid->oid_parent == parent) {
518 oidp = sysctl_find_oidname(oid->oid_name, parent);
523 sysctl_unregister_oid(oid);
524 oid->oid_parent = parent;
525 oid->oid_number = OID_AUTO;
526 sysctl_register_oid(oid);
532 * Register the kernel's oids on startup.
534 SET_DECLARE(sysctl_set, struct sysctl_oid);
537 sysctl_register_all(void *arg)
539 struct sysctl_oid **oidp;
543 SET_FOREACH(oidp, sysctl_set)
544 sysctl_register_oid(*oidp);
547 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
552 * These functions implement a presently undocumented interface
553 * used by the sysctl program to walk the tree, and get the type
554 * so it can print the value.
555 * This interface is under work and consideration, and should probably
556 * be killed with a big axe by the first person who can find the time.
557 * (be aware though, that the proper interface isn't as obvious as it
558 * may seem, there are various conflicting requirements.
560 * {0,0} printf the entire MIB-tree.
561 * {0,1,...} return the name of the "..." OID.
562 * {0,2,...} return the next OID.
563 * {0,3} return the OID of the name in "new"
564 * {0,4,...} return the kind & format info for the "..." OID.
565 * {0,5,...} return the description the "..." OID.
570 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
573 struct sysctl_oid *oidp;
575 SYSCTL_ASSERT_LOCKED();
576 SLIST_FOREACH(oidp, l, oid_link) {
581 printf("%d %s ", oidp->oid_number, oidp->oid_name);
584 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
585 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
587 if (oidp->oid_handler)
590 switch (oidp->oid_kind & CTLTYPE) {
593 if (!oidp->oid_handler) {
594 sysctl_sysctl_debug_dump_node(
595 oidp->oid_arg1, i+2);
598 case CTLTYPE_INT: printf(" Int\n"); break;
599 case CTLTYPE_STRING: printf(" String\n"); break;
600 case CTLTYPE_QUAD: printf(" Quad\n"); break;
601 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
602 default: printf("\n");
609 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
613 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
616 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
620 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
621 0, 0, sysctl_sysctl_debug, "-", "");
625 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
627 int *name = (int *) arg1;
628 u_int namelen = arg2;
630 struct sysctl_oid *oid;
631 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
634 SYSCTL_ASSERT_LOCKED();
637 snprintf(buf,sizeof(buf),"%d",*name);
639 error = SYSCTL_OUT(req, ".", 1);
641 error = SYSCTL_OUT(req, buf, strlen(buf));
649 SLIST_FOREACH(oid, lsp, oid_link) {
650 if (oid->oid_number != *name)
654 error = SYSCTL_OUT(req, ".", 1);
656 error = SYSCTL_OUT(req, oid->oid_name,
657 strlen(oid->oid_name));
664 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
667 if (oid->oid_handler)
670 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
675 return (SYSCTL_OUT(req, "", 1));
678 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
681 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
682 int *next, int *len, int level, struct sysctl_oid **oidpp)
684 struct sysctl_oid *oidp;
686 SYSCTL_ASSERT_LOCKED();
688 SLIST_FOREACH(oidp, lsp, oid_link) {
689 *next = oidp->oid_number;
692 if (oidp->oid_kind & CTLFLAG_SKIP)
696 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
698 if (oidp->oid_handler)
699 /* We really should call the handler here...*/
701 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
702 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
703 len, level+1, oidpp))
708 if (oidp->oid_number < *name)
711 if (oidp->oid_number > *name) {
712 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
714 if (oidp->oid_handler)
716 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
717 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
718 next+1, len, level+1, oidpp))
722 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
725 if (oidp->oid_handler)
728 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
729 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
730 len, level+1, oidpp))
741 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
743 int *name = (int *) arg1;
744 u_int namelen = arg2;
746 struct sysctl_oid *oid;
747 struct sysctl_oid_list *lsp = &sysctl__children;
748 int newoid[CTL_MAXNAME];
750 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
753 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
757 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
760 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
763 struct sysctl_oid *oidp;
764 struct sysctl_oid_list *lsp = &sysctl__children;
767 SYSCTL_ASSERT_LOCKED();
772 p = name + strlen(name) - 1 ;
778 for (p = name; *p && *p != '.'; p++)
784 oidp = SLIST_FIRST(lsp);
786 while (oidp && *len < CTL_MAXNAME) {
787 if (strcmp(name, oidp->oid_name)) {
788 oidp = SLIST_NEXT(oidp, oid_link);
791 *oid++ = oidp->oid_number;
800 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
803 if (oidp->oid_handler)
806 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
807 oidp = SLIST_FIRST(lsp);
809 for (p = name; *p && *p != '.'; p++)
819 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
822 int error, oid[CTL_MAXNAME], len;
823 struct sysctl_oid *op = 0;
825 SYSCTL_ASSERT_LOCKED();
829 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
830 return (ENAMETOOLONG);
832 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
834 error = SYSCTL_IN(req, p, req->newlen);
840 p [req->newlen] = '\0';
842 error = name2oid(p, oid, &len, &op);
849 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
853 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
854 0, 0, sysctl_sysctl_name2oid, "I", "");
857 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
859 struct sysctl_oid *oid;
862 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
868 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
871 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
876 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
877 sysctl_sysctl_oidfmt, "");
880 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
882 struct sysctl_oid *oid;
885 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
891 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
895 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
898 * Default "handler" functions.
902 * Handle an int, signed or unsigned.
904 * a variable: point arg1 at it.
905 * a constant: pass it in arg2.
909 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
911 int tmpout, error = 0;
914 * Attempt to get a coherent snapshot by making a copy of the data.
917 tmpout = *(int *)arg1;
920 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
922 if (error || !req->newptr)
928 error = SYSCTL_IN(req, arg1, sizeof(int));
934 * Based on on sysctl_handle_int() convert milliseconds into ticks.
938 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
942 tt = *(int *)oidp->oid_arg1;
943 s = (int)((int64_t)tt * 1000 / hz);
945 error = sysctl_handle_int(oidp, &s, 0, req);
946 if (error || !req->newptr)
949 tt = (int)((int64_t)s * hz / 1000);
953 *(int *)oidp->oid_arg1 = tt;
959 * Handle a long, signed or unsigned. arg1 points to it.
963 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
972 * Attempt to get a coherent snapshot by making a copy of the data.
976 tmplong = *(long *)arg1;
978 if (req->flags & SCTL_MASK32) {
980 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
983 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
985 if (error || !req->newptr)
989 if (req->flags & SCTL_MASK32) {
990 error = SYSCTL_IN(req, &tmpint, sizeof(int));
991 *(long *)arg1 = (long)tmpint;
994 error = SYSCTL_IN(req, arg1, sizeof(long));
999 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
1003 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1009 * Attempt to get a coherent snapshot by making a copy of the data.
1013 tmpout = *(uint64_t *)arg1;
1014 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1016 if (error || !req->newptr)
1019 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1024 * Handle our generic '\0' terminated 'C' string.
1026 * a variable string: point arg1 at it, arg2 is max length.
1027 * a constant string: point arg1 at it, arg2 is zero.
1031 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1038 * Attempt to get a coherent snapshot by copying to a
1039 * temporary kernel buffer.
1042 outlen = strlen((char *)arg1)+1;
1043 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1045 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1046 free(tmparg, M_SYSCTLTMP);
1050 error = SYSCTL_OUT(req, tmparg, outlen);
1051 free(tmparg, M_SYSCTLTMP);
1053 if (error || !req->newptr)
1056 if ((req->newlen - req->newidx) >= arg2) {
1059 arg2 = (req->newlen - req->newidx);
1060 error = SYSCTL_IN(req, arg1, arg2);
1061 ((char *)arg1)[arg2] = '\0';
1068 * Handle any kind of opaque data.
1069 * arg1 points to it, arg2 is the size.
1073 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1077 struct sysctl_req req2;
1080 * Attempt to get a coherent snapshot, by using the thread
1081 * pre-emption counter updated from within mi_switch() to
1082 * determine if we were pre-empted during a bcopy() or
1083 * copyout(). Make 3 attempts at doing this before giving up.
1084 * If we encounter an error, stop immediately.
1089 generation = curthread->td_generation;
1090 error = SYSCTL_OUT(req, arg1, arg2);
1094 if (generation != curthread->td_generation && tries < 3) {
1099 error = SYSCTL_IN(req, arg1, arg2);
1105 * Transfer functions to/from kernel space.
1106 * XXX: rather untested at this point
1109 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1115 if (req->oldlen <= req->oldidx)
1118 if (i > req->oldlen - req->oldidx)
1119 i = req->oldlen - req->oldidx;
1121 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1124 if (req->oldptr && i != l)
1130 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1134 if (req->newlen - req->newidx < l)
1136 bcopy((char *)req->newptr + req->newidx, p, l);
1142 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1143 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1146 struct sysctl_req req;
1148 bzero(&req, sizeof req);
1154 req.oldlen = *oldlenp;
1156 req.validlen = req.oldlen;
1163 req.newlen = newlen;
1167 req.oldfunc = sysctl_old_kernel;
1168 req.newfunc = sysctl_new_kernel;
1169 req.lock = REQ_LOCKED;
1172 error = sysctl_root(0, name, namelen, &req);
1175 if (req.lock == REQ_WIRED && req.validlen > 0)
1176 vsunlock(req.oldptr, req.validlen);
1178 if (error && error != ENOMEM)
1182 if (req.oldptr && req.oldidx > req.validlen)
1183 *retval = req.validlen;
1185 *retval = req.oldidx;
1191 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1192 void *new, size_t newlen, size_t *retval, int flags)
1194 int oid[CTL_MAXNAME];
1195 size_t oidlen, plen;
1198 oid[0] = 0; /* sysctl internal magic */
1199 oid[1] = 3; /* name2oid */
1200 oidlen = sizeof(oid);
1202 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1203 (void *)name, strlen(name), &plen, flags);
1207 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1208 new, newlen, retval, flags);
1213 * Transfer function to/from user space.
1216 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1219 size_t i, len, origidx;
1221 origidx = req->oldidx;
1223 if (req->oldptr == NULL)
1226 * If we have not wired the user supplied buffer and we are currently
1227 * holding locks, drop a witness warning, as it's possible that
1228 * write operations to the user page can sleep.
1230 if (req->lock != REQ_WIRED)
1231 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1232 "sysctl_old_user()");
1234 len = req->validlen;
1238 if (i > len - origidx)
1240 error = copyout(p, (char *)req->oldptr + origidx, i);
1250 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1256 if (req->newlen - req->newidx < l)
1258 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1259 "sysctl_new_user()");
1260 error = copyin((char *)req->newptr + req->newidx, p, l);
1266 * Wire the user space destination buffer. If set to a value greater than
1267 * zero, the len parameter limits the maximum amount of wired memory.
1270 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1276 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1278 if (req->lock == REQ_LOCKED && req->oldptr &&
1279 req->oldfunc == sysctl_old_user) {
1280 if (wiredlen != 0) {
1281 ret = vslock(req->oldptr, wiredlen);
1288 * Touch all the wired pages to avoid PTE modified
1289 * bit emulation traps on Alpha while holding locks
1290 * in the sysctl handler.
1292 for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1293 cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1294 copyin(cp, &dummy, 1);
1295 copyout(&dummy, cp, 1);
1298 req->lock = REQ_WIRED;
1299 req->validlen = wiredlen;
1305 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1306 int *nindx, struct sysctl_req *req)
1308 struct sysctl_oid *oid;
1311 SYSCTL_ASSERT_LOCKED();
1312 oid = SLIST_FIRST(&sysctl__children);
1314 while (oid && indx < CTL_MAXNAME) {
1315 if (oid->oid_number == name[indx]) {
1317 if (oid->oid_kind & CTLFLAG_NOLOCK)
1318 req->lock = REQ_UNLOCKED;
1319 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1320 if (oid->oid_handler != NULL ||
1328 (struct sysctl_oid_list *)oid->oid_arg1);
1329 } else if (indx == namelen) {
1338 oid = SLIST_NEXT(oid, oid_link);
1345 * Traverse our tree, and find the right node, execute whatever it points
1346 * to, and return the resulting error code.
1350 sysctl_root(SYSCTL_HANDLER_ARGS)
1352 struct sysctl_oid *oid;
1353 int error, indx, lvl;
1355 SYSCTL_ASSERT_LOCKED();
1357 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1361 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1363 * You can't call a sysctl when it's a node, but has
1364 * no handler. Inform the user that it's a node.
1365 * The indx may or may not be the same as namelen.
1367 if (oid->oid_handler == NULL)
1371 /* Is this sysctl writable? */
1372 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1375 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1377 /* Is this sysctl sensitive to securelevels? */
1378 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1379 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1380 error = securelevel_gt(req->td->td_ucred, lvl);
1385 /* Is this sysctl writable by only privileged users? */
1386 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1387 if (oid->oid_kind & CTLFLAG_PRISON)
1388 error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1390 error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1395 if (!oid->oid_handler)
1398 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1399 arg1 = (int *)arg1 + indx;
1402 arg1 = oid->oid_arg1;
1403 arg2 = oid->oid_arg2;
1406 error = mac_check_system_sysctl(req->td->td_ucred, oid, arg1, arg2,
1411 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1413 error = oid->oid_handler(oid, arg1, arg2, req);
1414 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1420 #ifndef _SYS_SYSPROTO_H_
1421 struct sysctl_args {
1431 __sysctl(struct thread *td, struct sysctl_args *uap)
1433 int error, i, name[CTL_MAXNAME];
1436 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1439 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1443 error = userland_sysctl(td, name, uap->namelen,
1444 uap->old, uap->oldlenp, 0,
1445 uap->new, uap->newlen, &j, 0);
1446 if (error && error != ENOMEM)
1449 i = copyout(&j, uap->oldlenp, sizeof(j));
1457 * This is used from various compatibility syscalls too. That's why name
1458 * must be in kernel space.
1461 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1462 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1466 struct sysctl_req req;
1468 bzero(&req, sizeof req);
1475 req.oldlen = *oldlenp;
1477 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1482 req.validlen = req.oldlen;
1485 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1491 if (!useracc(new, newlen, VM_PROT_READ))
1493 req.newlen = newlen;
1497 req.oldfunc = sysctl_old_user;
1498 req.newfunc = sysctl_new_user;
1499 req.lock = REQ_LOCKED;
1506 error = sysctl_root(0, name, namelen, &req);
1507 if (error != EAGAIN)
1513 if (req.lock == REQ_WIRED && req.validlen > 0)
1514 vsunlock(req.oldptr, req.validlen);
1517 if (error && error != ENOMEM)
1521 if (req.oldptr && req.oldidx > req.validlen)
1522 *retval = req.validlen;
1524 *retval = req.oldidx;