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>
55 #include <sys/sysproto.h>
57 #include <sys/vimage.h>
59 #include <sys/ktrace.h>
62 #include <security/mac/mac_framework.h>
65 #include <vm/vm_extern.h>
67 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
68 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
69 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
72 * The sysctllock protects the MIB tree. It also protects sysctl
73 * contexts used with dynamic sysctls. The sysctl_register_oid() and
74 * sysctl_unregister_oid() routines require the sysctllock to already
75 * be held, so the sysctl_lock() and sysctl_unlock() routines are
76 * provided for the few places in the kernel which need to use that
77 * API rather than using the dynamic API. Use of the dynamic API is
78 * strongly encouraged for most code.
80 * The sysctlmemlock is used to limit the amount of user memory wired for
81 * sysctl requests. This is implemented by serializing any userland
82 * sysctl requests larger than a single page via an exclusive lock.
84 static struct sx sysctllock;
85 static struct sx sysctlmemlock;
87 #define SYSCTL_SLOCK() sx_slock(&sysctllock)
88 #define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock)
89 #define SYSCTL_XLOCK() sx_xlock(&sysctllock)
90 #define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
91 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
92 #define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED)
93 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
95 static int sysctl_root(SYSCTL_HANDLER_ARGS);
97 struct sysctl_oid_list sysctl__children; /* root list */
99 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
102 static struct sysctl_oid *
103 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
105 struct sysctl_oid *oidp;
107 SYSCTL_ASSERT_LOCKED();
108 SLIST_FOREACH(oidp, list, oid_link) {
109 if (strcmp(oidp->oid_name, name) == 0) {
117 * Initialization of the MIB tree.
119 * Order by number in each list.
136 sysctl_register_oid(struct sysctl_oid *oidp)
138 struct sysctl_oid_list *parent = oidp->oid_parent;
139 struct sysctl_oid *p;
140 struct sysctl_oid *q;
143 * First check if another oid with the same name already
144 * exists in the parent's list.
146 SYSCTL_ASSERT_XLOCKED();
147 p = sysctl_find_oidname(oidp->oid_name, parent);
149 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
153 printf("can't re-use a leaf (%s)!\n", p->oid_name);
158 * If this oid has a number OID_AUTO, give it a number which
159 * is greater than any current oid.
160 * NOTE: DO NOT change the starting value here, change it in
161 * <sys/sysctl.h>, and make sure it is at least 256 to
162 * accomodate e.g. net.inet.raw as a static sysctl node.
164 if (oidp->oid_number == OID_AUTO) {
165 static int newoid = CTL_AUTO_START;
167 oidp->oid_number = newoid++;
168 if (newoid == 0x7fffffff)
169 panic("out of oids");
172 else if (oidp->oid_number >= CTL_AUTO_START) {
173 /* do not panic; this happens when unregistering sysctl sets */
174 printf("static sysctl oid too high: %d", oidp->oid_number);
179 * Insert the oid into the parent's list in order.
182 SLIST_FOREACH(p, parent, oid_link) {
183 if (oidp->oid_number < p->oid_number)
188 SLIST_INSERT_AFTER(q, oidp, oid_link);
190 SLIST_INSERT_HEAD(parent, oidp, oid_link);
194 sysctl_unregister_oid(struct sysctl_oid *oidp)
196 struct sysctl_oid *p;
199 SYSCTL_ASSERT_XLOCKED();
201 if (oidp->oid_number == OID_AUTO) {
204 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
206 SLIST_REMOVE(oidp->oid_parent, oidp,
207 sysctl_oid, oid_link);
215 * This can happen when a module fails to register and is
216 * being unloaded afterwards. It should not be a panic()
220 printf("%s: failed to unregister sysctl\n", __func__);
223 /* Initialize a new context to keep track of dynamically added sysctls. */
225 sysctl_ctx_init(struct sysctl_ctx_list *c)
233 * No locking here, the caller is responsible for not adding
234 * new nodes to a context until after this function has
241 /* Free the context, and destroy all dynamic oids registered in this context */
243 sysctl_ctx_free(struct sysctl_ctx_list *clist)
245 struct sysctl_ctx_entry *e, *e1;
250 * First perform a "dry run" to check if it's ok to remove oids.
252 * XXX This algorithm is a hack. But I don't know any
253 * XXX better solution for now...
256 TAILQ_FOREACH(e, clist, link) {
257 error = sysctl_remove_oid_locked(e->entry, 0, 0);
262 * Restore deregistered entries, either from the end,
263 * or from the place where error occured.
264 * e contains the entry that was not unregistered
267 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
269 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
271 sysctl_register_oid(e1->entry);
272 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
278 /* Now really delete the entries */
279 e = TAILQ_FIRST(clist);
281 e1 = TAILQ_NEXT(e, link);
282 error = sysctl_remove_oid_locked(e->entry, 1, 0);
284 panic("sysctl_remove_oid: corrupt tree, entry: %s",
286 free(e, M_SYSCTLOID);
293 /* Add an entry to the context */
294 struct sysctl_ctx_entry *
295 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
297 struct sysctl_ctx_entry *e;
299 SYSCTL_ASSERT_XLOCKED();
300 if (clist == NULL || oidp == NULL)
302 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
304 TAILQ_INSERT_HEAD(clist, e, link);
308 /* Find an entry in the context */
309 struct sysctl_ctx_entry *
310 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
312 struct sysctl_ctx_entry *e;
314 SYSCTL_ASSERT_LOCKED();
315 if (clist == NULL || oidp == NULL)
317 TAILQ_FOREACH(e, clist, link) {
325 * Delete an entry from the context.
326 * NOTE: this function doesn't free oidp! You have to remove it
327 * with sysctl_remove_oid().
330 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
332 struct sysctl_ctx_entry *e;
334 if (clist == NULL || oidp == NULL)
337 e = sysctl_ctx_entry_find(clist, oidp);
339 TAILQ_REMOVE(clist, e, link);
341 free(e, M_SYSCTLOID);
350 * Remove dynamically created sysctl trees.
351 * oidp - top of the tree to be removed
352 * del - if 0 - just deregister, otherwise free up entries as well
353 * recurse - if != 0 traverse the subtree to be deleted
356 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
361 error = sysctl_remove_oid_locked(oidp, del, recurse);
367 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
369 struct sysctl_oid *p;
372 SYSCTL_ASSERT_XLOCKED();
375 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
376 printf("can't remove non-dynamic nodes!\n");
380 * WARNING: normal method to do this should be through
381 * sysctl_ctx_free(). Use recursing as the last resort
382 * method to purge your sysctl tree of leftovers...
383 * However, if some other code still references these nodes,
386 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
387 if (oidp->oid_refcnt == 1) {
388 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
391 error = sysctl_remove_oid_locked(p, del,
397 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
400 if (oidp->oid_refcnt > 1 ) {
403 if (oidp->oid_refcnt == 0) {
404 printf("Warning: bad oid_refcnt=%u (%s)!\n",
405 oidp->oid_refcnt, oidp->oid_name);
408 sysctl_unregister_oid(oidp);
411 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
412 free((void *)(uintptr_t)(const void *)oidp->oid_name,
414 free(oidp, M_SYSCTLOID);
421 * Create new sysctls at run time.
422 * clist may point to a valid context initialized with sysctl_ctx_init().
425 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
426 int number, const char *name, int kind, void *arg1, int arg2,
427 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
429 struct sysctl_oid *oidp;
433 /* You have to hook up somewhere.. */
436 /* Check if the node already exists, otherwise create it */
438 oidp = sysctl_find_oidname(name, parent);
440 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
442 /* Update the context */
444 sysctl_ctx_entry_add(clist, oidp);
449 printf("can't re-use a leaf (%s)!\n", name);
453 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
454 oidp->oid_parent = parent;
455 SLIST_NEXT(oidp, oid_link) = NULL;
456 oidp->oid_number = number;
457 oidp->oid_refcnt = 1;
459 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
460 bcopy(name, newname, len + 1);
462 oidp->oid_name = newname;
463 oidp->oid_handler = handler;
464 oidp->oid_kind = CTLFLAG_DYN | kind;
465 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
466 /* Allocate space for children */
467 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
468 M_SYSCTLOID, M_WAITOK));
469 SLIST_INIT(SYSCTL_CHILDREN(oidp));
471 oidp->oid_arg1 = arg1;
472 oidp->oid_arg2 = arg2;
476 int len = strlen(descr) + 1;
477 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
479 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
481 /* Update the context, if used */
483 sysctl_ctx_entry_add(clist, oidp);
484 /* Register this oid */
485 sysctl_register_oid(oidp);
491 * Rename an existing oid.
494 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
501 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
502 bcopy(name, newname, len + 1);
505 oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
506 oidp->oid_name = newname;
508 free(oldname, M_SYSCTLOID);
512 * Reparent an existing oid.
515 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
517 struct sysctl_oid *oidp;
520 if (oid->oid_parent == parent) {
524 oidp = sysctl_find_oidname(oid->oid_name, parent);
529 sysctl_unregister_oid(oid);
530 oid->oid_parent = parent;
531 oid->oid_number = OID_AUTO;
532 sysctl_register_oid(oid);
538 * Register the kernel's oids on startup.
540 SET_DECLARE(sysctl_set, struct sysctl_oid);
543 sysctl_register_all(void *arg)
545 struct sysctl_oid **oidp;
547 sx_init(&sysctlmemlock, "sysctl mem");
550 SET_FOREACH(oidp, sysctl_set)
551 sysctl_register_oid(*oidp);
554 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
559 * These functions implement a presently undocumented interface
560 * used by the sysctl program to walk the tree, and get the type
561 * so it can print the value.
562 * This interface is under work and consideration, and should probably
563 * be killed with a big axe by the first person who can find the time.
564 * (be aware though, that the proper interface isn't as obvious as it
565 * may seem, there are various conflicting requirements.
567 * {0,0} printf the entire MIB-tree.
568 * {0,1,...} return the name of the "..." OID.
569 * {0,2,...} return the next OID.
570 * {0,3} return the OID of the name in "new"
571 * {0,4,...} return the kind & format info for the "..." OID.
572 * {0,5,...} return the description the "..." OID.
577 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
580 struct sysctl_oid *oidp;
582 SYSCTL_ASSERT_LOCKED();
583 SLIST_FOREACH(oidp, l, oid_link) {
588 printf("%d %s ", oidp->oid_number, oidp->oid_name);
591 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
592 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
594 if (oidp->oid_handler)
597 switch (oidp->oid_kind & CTLTYPE) {
600 if (!oidp->oid_handler) {
601 sysctl_sysctl_debug_dump_node(
602 oidp->oid_arg1, i+2);
605 case CTLTYPE_INT: printf(" Int\n"); break;
606 case CTLTYPE_STRING: printf(" String\n"); break;
607 case CTLTYPE_QUAD: printf(" Quad\n"); break;
608 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
609 default: printf("\n");
616 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
620 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
623 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
627 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
628 0, 0, sysctl_sysctl_debug, "-", "");
632 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
634 int *name = (int *) arg1;
635 u_int namelen = arg2;
637 struct sysctl_oid *oid;
638 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
641 SYSCTL_ASSERT_LOCKED();
644 snprintf(buf,sizeof(buf),"%d",*name);
646 error = SYSCTL_OUT(req, ".", 1);
648 error = SYSCTL_OUT(req, buf, strlen(buf));
656 SLIST_FOREACH(oid, lsp, oid_link) {
657 if (oid->oid_number != *name)
661 error = SYSCTL_OUT(req, ".", 1);
663 error = SYSCTL_OUT(req, oid->oid_name,
664 strlen(oid->oid_name));
671 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
674 if (oid->oid_handler)
677 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
682 return (SYSCTL_OUT(req, "", 1));
685 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
688 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
689 int *next, int *len, int level, struct sysctl_oid **oidpp)
691 struct sysctl_oid *oidp;
693 SYSCTL_ASSERT_LOCKED();
695 SLIST_FOREACH(oidp, lsp, oid_link) {
696 *next = oidp->oid_number;
699 if (oidp->oid_kind & CTLFLAG_SKIP)
703 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
705 if (oidp->oid_handler)
706 /* We really should call the handler here...*/
708 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
709 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
710 len, level+1, oidpp))
715 if (oidp->oid_number < *name)
718 if (oidp->oid_number > *name) {
719 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
721 if (oidp->oid_handler)
723 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
724 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
725 next+1, len, level+1, oidpp))
729 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
732 if (oidp->oid_handler)
735 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
736 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
737 len, level+1, oidpp))
748 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
750 int *name = (int *) arg1;
751 u_int namelen = arg2;
753 struct sysctl_oid *oid;
754 struct sysctl_oid_list *lsp = &sysctl__children;
755 int newoid[CTL_MAXNAME];
757 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
760 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
764 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
767 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
770 struct sysctl_oid *oidp;
771 struct sysctl_oid_list *lsp = &sysctl__children;
774 SYSCTL_ASSERT_LOCKED();
779 p = name + strlen(name) - 1 ;
785 for (p = name; *p && *p != '.'; p++)
791 oidp = SLIST_FIRST(lsp);
793 while (oidp && *len < CTL_MAXNAME) {
794 if (strcmp(name, oidp->oid_name)) {
795 oidp = SLIST_NEXT(oidp, oid_link);
798 *oid++ = oidp->oid_number;
807 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
810 if (oidp->oid_handler)
813 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
814 oidp = SLIST_FIRST(lsp);
816 for (p = name; *p && *p != '.'; p++)
826 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
829 int error, oid[CTL_MAXNAME], len;
830 struct sysctl_oid *op = 0;
832 SYSCTL_ASSERT_LOCKED();
836 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
837 return (ENAMETOOLONG);
839 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
841 error = SYSCTL_IN(req, p, req->newlen);
847 p [req->newlen] = '\0';
849 error = name2oid(p, oid, &len, &op);
856 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
860 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
861 0, 0, sysctl_sysctl_name2oid, "I", "");
864 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
866 struct sysctl_oid *oid;
869 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
875 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
878 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
883 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
884 sysctl_sysctl_oidfmt, "");
887 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
889 struct sysctl_oid *oid;
892 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
898 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
902 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
905 * Default "handler" functions.
909 * Handle an int, signed or unsigned.
911 * a variable: point arg1 at it.
912 * a constant: pass it in arg2.
916 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
918 int tmpout, error = 0;
921 * Attempt to get a coherent snapshot by making a copy of the data.
924 tmpout = *(int *)arg1;
927 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
929 if (error || !req->newptr)
935 error = SYSCTL_IN(req, arg1, sizeof(int));
941 sysctl_handle_v_int(SYSCTL_HANDLER_ARGS)
943 int tmpout, error = 0;
945 SYSCTL_RESOLVE_V_ARG1();
948 * Attempt to get a coherent snapshot by making a copy of the data.
950 tmpout = *(int *)arg1;
951 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
953 if (error || !req->newptr)
959 error = SYSCTL_IN(req, arg1, sizeof(int));
965 * Based on on sysctl_handle_int() convert milliseconds into ticks.
969 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
973 SYSCTL_RESOLVE_V_ARG1();
976 s = (int)((int64_t)tt * 1000 / hz);
978 error = sysctl_handle_int(oidp, &s, 0, req);
979 if (error || !req->newptr)
982 tt = (int)((int64_t)s * hz / 1000);
992 * Handle a long, signed or unsigned. arg1 points to it.
996 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1005 * Attempt to get a coherent snapshot by making a copy of the data.
1009 tmplong = *(long *)arg1;
1011 if (req->flags & SCTL_MASK32) {
1013 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1016 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1018 if (error || !req->newptr)
1022 if (req->flags & SCTL_MASK32) {
1023 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1024 *(long *)arg1 = (long)tmpint;
1027 error = SYSCTL_IN(req, arg1, sizeof(long));
1032 * Handle a 64 bit int, signed or unsigned. arg1 points to it.
1036 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1042 * Attempt to get a coherent snapshot by making a copy of the data.
1046 tmpout = *(uint64_t *)arg1;
1047 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1049 if (error || !req->newptr)
1052 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1057 * Handle our generic '\0' terminated 'C' string.
1059 * a variable string: point arg1 at it, arg2 is max length.
1060 * a constant string: point arg1 at it, arg2 is zero.
1064 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1071 * Attempt to get a coherent snapshot by copying to a
1072 * temporary kernel buffer.
1075 outlen = strlen((char *)arg1)+1;
1076 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1078 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1079 free(tmparg, M_SYSCTLTMP);
1083 error = SYSCTL_OUT(req, tmparg, outlen);
1084 free(tmparg, M_SYSCTLTMP);
1086 if (error || !req->newptr)
1089 if ((req->newlen - req->newidx) >= arg2) {
1092 arg2 = (req->newlen - req->newidx);
1093 error = SYSCTL_IN(req, arg1, arg2);
1094 ((char *)arg1)[arg2] = '\0';
1102 sysctl_handle_v_string(SYSCTL_HANDLER_ARGS)
1108 SYSCTL_RESOLVE_V_ARG1();
1111 * Attempt to get a coherent snapshot by copying to a
1112 * temporary kernel buffer.
1115 outlen = strlen((char *)arg1)+1;
1116 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
1118 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
1119 free(tmparg, M_SYSCTLTMP);
1123 error = SYSCTL_OUT(req, tmparg, outlen);
1124 free(tmparg, M_SYSCTLTMP);
1126 if (error || !req->newptr)
1129 if ((req->newlen - req->newidx) >= arg2) {
1132 arg2 = (req->newlen - req->newidx);
1133 error = SYSCTL_IN(req, arg1, arg2);
1134 ((char *)arg1)[arg2] = '\0';
1142 * Handle any kind of opaque data.
1143 * arg1 points to it, arg2 is the size.
1147 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1151 struct sysctl_req req2;
1154 * Attempt to get a coherent snapshot, by using the thread
1155 * pre-emption counter updated from within mi_switch() to
1156 * determine if we were pre-empted during a bcopy() or
1157 * copyout(). Make 3 attempts at doing this before giving up.
1158 * If we encounter an error, stop immediately.
1163 generation = curthread->td_generation;
1164 error = SYSCTL_OUT(req, arg1, arg2);
1168 if (generation != curthread->td_generation && tries < 3) {
1173 error = SYSCTL_IN(req, arg1, arg2);
1180 sysctl_handle_v_opaque(SYSCTL_HANDLER_ARGS)
1184 struct sysctl_req req2;
1186 SYSCTL_RESOLVE_V_ARG1();
1191 generation = curthread->td_generation;
1192 error = SYSCTL_OUT(req, arg1, arg2);
1196 if (generation != curthread->td_generation && tries < 3) {
1201 error = SYSCTL_IN(req, arg1, arg2);
1208 * Transfer functions to/from kernel space.
1209 * XXX: rather untested at this point
1212 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1218 if (req->oldlen <= req->oldidx)
1221 if (i > req->oldlen - req->oldidx)
1222 i = req->oldlen - req->oldidx;
1224 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1227 if (req->oldptr && i != l)
1233 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1237 if (req->newlen - req->newidx < l)
1239 bcopy((char *)req->newptr + req->newidx, p, l);
1245 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1246 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1249 struct sysctl_req req;
1251 bzero(&req, sizeof req);
1257 req.oldlen = *oldlenp;
1259 req.validlen = req.oldlen;
1266 req.newlen = newlen;
1270 req.oldfunc = sysctl_old_kernel;
1271 req.newfunc = sysctl_new_kernel;
1272 req.lock = REQ_LOCKED;
1275 error = sysctl_root(0, name, namelen, &req);
1278 if (req.lock == REQ_WIRED && req.validlen > 0)
1279 vsunlock(req.oldptr, req.validlen);
1281 if (error && error != ENOMEM)
1285 if (req.oldptr && req.oldidx > req.validlen)
1286 *retval = req.validlen;
1288 *retval = req.oldidx;
1294 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1295 void *new, size_t newlen, size_t *retval, int flags)
1297 int oid[CTL_MAXNAME];
1298 size_t oidlen, plen;
1301 oid[0] = 0; /* sysctl internal magic */
1302 oid[1] = 3; /* name2oid */
1303 oidlen = sizeof(oid);
1305 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1306 (void *)name, strlen(name), &plen, flags);
1310 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1311 new, newlen, retval, flags);
1316 * Transfer function to/from user space.
1319 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1322 size_t i, len, origidx;
1324 origidx = req->oldidx;
1326 if (req->oldptr == NULL)
1329 * If we have not wired the user supplied buffer and we are currently
1330 * holding locks, drop a witness warning, as it's possible that
1331 * write operations to the user page can sleep.
1333 if (req->lock != REQ_WIRED)
1334 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1335 "sysctl_old_user()");
1337 len = req->validlen;
1341 if (i > len - origidx)
1343 error = copyout(p, (char *)req->oldptr + origidx, i);
1353 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1359 if (req->newlen - req->newidx < l)
1361 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1362 "sysctl_new_user()");
1363 error = copyin((char *)req->newptr + req->newidx, p, l);
1369 * Wire the user space destination buffer. If set to a value greater than
1370 * zero, the len parameter limits the maximum amount of wired memory.
1373 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1378 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1380 if (req->lock == REQ_LOCKED && req->oldptr &&
1381 req->oldfunc == sysctl_old_user) {
1382 if (wiredlen != 0) {
1383 ret = vslock(req->oldptr, wiredlen);
1390 req->lock = REQ_WIRED;
1391 req->validlen = wiredlen;
1397 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1398 int *nindx, struct sysctl_req *req)
1400 struct sysctl_oid *oid;
1403 SYSCTL_ASSERT_LOCKED();
1404 oid = SLIST_FIRST(&sysctl__children);
1406 while (oid && indx < CTL_MAXNAME) {
1407 if (oid->oid_number == name[indx]) {
1409 if (oid->oid_kind & CTLFLAG_NOLOCK)
1410 req->lock = REQ_UNLOCKED;
1411 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1412 if (oid->oid_handler != NULL ||
1420 (struct sysctl_oid_list *)oid->oid_arg1);
1421 } else if (indx == namelen) {
1430 oid = SLIST_NEXT(oid, oid_link);
1437 * Traverse our tree, and find the right node, execute whatever it points
1438 * to, and return the resulting error code.
1442 sysctl_root(SYSCTL_HANDLER_ARGS)
1444 struct sysctl_oid *oid;
1445 int error, indx, lvl;
1447 SYSCTL_ASSERT_LOCKED();
1449 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1453 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1455 * You can't call a sysctl when it's a node, but has
1456 * no handler. Inform the user that it's a node.
1457 * The indx may or may not be the same as namelen.
1459 if (oid->oid_handler == NULL)
1463 /* Is this sysctl writable? */
1464 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1467 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1469 /* Is this sysctl sensitive to securelevels? */
1470 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1471 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1472 error = securelevel_gt(req->td->td_ucred, lvl);
1477 /* Is this sysctl writable by only privileged users? */
1478 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1479 if (oid->oid_kind & CTLFLAG_PRISON)
1480 error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
1482 error = priv_check(req->td, PRIV_SYSCTL_WRITE);
1487 if (!oid->oid_handler)
1490 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1491 arg1 = (int *)arg1 + indx;
1494 arg1 = oid->oid_arg1;
1495 arg2 = oid->oid_arg2;
1498 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1503 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1505 error = oid->oid_handler(oid, arg1, arg2, req);
1506 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
1512 #ifndef _SYS_SYSPROTO_H_
1513 struct sysctl_args {
1523 __sysctl(struct thread *td, struct sysctl_args *uap)
1525 int error, i, name[CTL_MAXNAME];
1528 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1531 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1535 error = userland_sysctl(td, name, uap->namelen,
1536 uap->old, uap->oldlenp, 0,
1537 uap->new, uap->newlen, &j, 0);
1538 if (error && error != ENOMEM)
1541 i = copyout(&j, uap->oldlenp, sizeof(j));
1549 * This is used from various compatibility syscalls too. That's why name
1550 * must be in kernel space.
1553 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1554 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1557 int error = 0, memlocked;
1558 struct sysctl_req req;
1560 bzero(&req, sizeof req);
1567 req.oldlen = *oldlenp;
1569 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1574 req.validlen = req.oldlen;
1577 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1583 if (!useracc(new, newlen, VM_PROT_READ))
1585 req.newlen = newlen;
1589 req.oldfunc = sysctl_old_user;
1590 req.newfunc = sysctl_new_user;
1591 req.lock = REQ_LOCKED;
1594 if (KTRPOINT(curthread, KTR_SYSCTL))
1595 ktrsysctl(name, namelen);
1598 if (req.oldlen > PAGE_SIZE) {
1600 sx_xlock(&sysctlmemlock);
1603 CURVNET_SET(TD_TO_VNET(td));
1609 error = sysctl_root(0, name, namelen, &req);
1611 if (error != EAGAIN)
1618 if (req.lock == REQ_WIRED && req.validlen > 0)
1619 vsunlock(req.oldptr, req.validlen);
1621 sx_xunlock(&sysctlmemlock);
1623 if (error && error != ENOMEM)
1627 if (req.oldptr && req.oldidx > req.validlen)
1628 *retval = req.validlen;
1630 *retval = req.oldidx;