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 * 3. 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
36 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
39 #include "opt_ktrace.h"
40 #include "opt_sysctl.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
50 #include <sys/sysmsg.h>
54 #include <sys/ktrace.h>
58 #include <vm/vm_extern.h>
60 static MALLOC_DEFINE(M_SYSCTL
, "sysctl", "sysctl internal magic");
61 static MALLOC_DEFINE(M_SYSCTLOID
, "sysctloid", "sysctl dynamic oids");
63 int sysctl_debugx
= 0;
64 SYSCTL_INT(_debug
, OID_AUTO
, sysctl
, CTLFLAG_RW
, &sysctl_debugx
, 0, "");
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.
76 static int sysctl_root(SYSCTL_HANDLER_ARGS
);
77 static void sysctl_register_oid_int(struct sysctl_oid
*oipd
);
78 static void sysctl_unregister_oid_int(struct sysctl_oid
*oipd
);
80 struct sysctl_oid_list sysctl__children
; /* root list */
82 static int sysctl_remove_oid_locked(struct sysctl_oid
*oidp
, int del
,
85 static struct sysctl_oid
*
86 sysctl_find_oidname(const char *name
, struct sysctl_oid_list
*list
, int lock
)
88 struct sysctl_oid
*oidp
;
90 SLIST_FOREACH(oidp
, list
, oid_link
) {
91 if (strcmp(oidp
->oid_name
, name
) == 0) {
99 * Initialization of the MIB tree.
101 * Order by number in each list.
105 sysctl_register_oid(struct sysctl_oid
*oidp
)
108 sysctl_register_oid_int(oidp
);
113 sysctl_register_oid_int(struct sysctl_oid
*oidp
)
115 struct sysctl_oid_list
*parent
= oidp
->oid_parent
;
116 struct sysctl_oid
*p
;
117 struct sysctl_oid
*q
;
120 * Finish initialization from sysctl_set or add.
122 lockinit(&oidp
->oid_lock
, "oidlk", 0, LK_CANRECURSE
);
125 * First check if another oid with the same name already
126 * exists in the parent's list.
128 p
= sysctl_find_oidname(oidp
->oid_name
, parent
, 0);
130 if ((p
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
133 kprintf("can't re-use a leaf (%s)!\n", p
->oid_name
);
138 * If this oid has a number OID_AUTO, give it a number which
139 * is greater than any current oid. Make sure it is at least
140 * 256 to leave space for pre-assigned oid numbers.
142 if (oidp
->oid_number
== OID_AUTO
) {
143 int newoid
= 0x100; /* minimum AUTO oid */
146 * Adjust based on highest oid in parent list
148 SLIST_FOREACH(p
, parent
, oid_link
) {
149 if (newoid
<= p
->oid_number
)
150 newoid
= p
->oid_number
+ 1;
152 oidp
->oid_number
= newoid
;
156 * Insert the oid into the parent's list in order.
159 SLIST_FOREACH(p
, parent
, oid_link
) {
160 if (oidp
->oid_number
< p
->oid_number
)
165 SLIST_INSERT_AFTER(q
, oidp
, oid_link
);
167 SLIST_INSERT_HEAD(parent
, oidp
, oid_link
);
171 sysctl_unregister_oid(struct sysctl_oid
*oidp
)
174 sysctl_unregister_oid_int(oidp
);
179 sysctl_unregister_oid_int(struct sysctl_oid
*oidp
)
181 struct sysctl_oid
*p
;
183 if (oidp
->oid_number
== OID_AUTO
)
184 panic("Trying to unregister OID_AUTO entry: %p", oidp
);
186 SLIST_FOREACH(p
, oidp
->oid_parent
, oid_link
) {
189 SLIST_REMOVE(oidp
->oid_parent
, oidp
, sysctl_oid
, oid_link
);
194 * This can happen when a module fails to register and is
195 * being unloaded afterwards. It should not be a panic()
198 kprintf("%s: failed to unregister sysctl\n", __func__
);
201 /* Initialize a new context to keep track of dynamically added sysctls. */
203 sysctl_ctx_init(struct sysctl_ctx_list
*c
)
211 /* Free the context, and destroy all dynamic oids registered in this context */
213 sysctl_ctx_free(struct sysctl_ctx_list
*clist
)
215 struct sysctl_ctx_entry
*e
, *e1
;
220 * First perform a "dry run" to check if it's ok to remove oids.
222 * XXX This algorithm is a hack. But I don't know any
223 * XXX better solution for now...
226 TAILQ_FOREACH(e
, clist
, link
) {
227 error
= sysctl_remove_oid_locked(e
->entry
, 0, 0);
232 * Restore deregistered entries, either from the end,
233 * or from the place where error occured.
234 * e contains the entry that was not unregistered
237 e1
= TAILQ_PREV(e
, sysctl_ctx_list
, link
);
239 e1
= TAILQ_LAST(clist
, sysctl_ctx_list
);
241 sysctl_register_oid(e1
->entry
);
242 e1
= TAILQ_PREV(e1
, sysctl_ctx_list
, link
);
248 /* Now really delete the entries */
249 e
= TAILQ_FIRST(clist
);
251 e1
= TAILQ_NEXT(e
, link
);
252 error
= sysctl_remove_oid_locked(e
->entry
, 1, 0);
254 panic("sysctl_remove_oid: corrupt tree, entry: %s",
256 kfree(e
, M_SYSCTLOID
);
263 /* Add an entry to the context */
264 struct sysctl_ctx_entry
*
265 sysctl_ctx_entry_add(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
267 struct sysctl_ctx_entry
*e
;
269 SYSCTL_ASSERT_LOCKED();
270 if (clist
== NULL
|| oidp
== NULL
)
272 e
= kmalloc(sizeof(struct sysctl_ctx_entry
), M_SYSCTLOID
, M_WAITOK
);
274 TAILQ_INSERT_HEAD(clist
, e
, link
);
278 /* Find an entry in the context */
279 struct sysctl_ctx_entry
*
280 sysctl_ctx_entry_find(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
282 struct sysctl_ctx_entry
*e
;
284 SYSCTL_ASSERT_LOCKED();
285 if (clist
== NULL
|| oidp
== NULL
)
287 TAILQ_FOREACH(e
, clist
, link
) {
295 * Delete an entry from the context.
296 * NOTE: this function doesn't free oidp! You have to remove it
297 * with sysctl_remove_oid().
300 sysctl_ctx_entry_del(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
302 struct sysctl_ctx_entry
*e
;
304 if (clist
== NULL
|| oidp
== NULL
)
307 e
= sysctl_ctx_entry_find(clist
, oidp
);
309 TAILQ_REMOVE(clist
, e
, link
);
311 kfree(e
, M_SYSCTLOID
);
320 * Remove dynamically created sysctl trees.
321 * oidp - top of the tree to be removed
322 * del - if 0 - just deregister, otherwise free up entries as well
323 * recurse - if != 0 traverse the subtree to be deleted
326 sysctl_remove_oid(struct sysctl_oid
*oidp
, int del
, int recurse
)
331 error
= sysctl_remove_oid_locked(oidp
, del
, recurse
);
337 sysctl_remove_oid_locked(struct sysctl_oid
*oidp
, int del
, int recurse
)
339 struct sysctl_oid
*p
, *tmp
;
342 SYSCTL_ASSERT_LOCKED();
345 if ((oidp
->oid_kind
& CTLFLAG_DYN
) == 0) {
346 kprintf("can't remove non-dynamic nodes!\n");
350 * WARNING: normal method to do this should be through
351 * sysctl_ctx_free(). Use recursing as the last resort
352 * method to purge your sysctl tree of leftovers...
353 * However, if some other code still references these nodes,
356 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
357 if (oidp
->oid_refcnt
== 1) {
358 SLIST_FOREACH_MUTABLE(p
,
359 SYSCTL_CHILDREN(oidp
), oid_link
, tmp
) {
361 kprintf("Warning: failed attempt to "
362 "remove oid %s with child %s\n",
363 oidp
->oid_name
, p
->oid_name
);
366 error
= sysctl_remove_oid_locked(p
, del
,
372 kfree(SYSCTL_CHILDREN(oidp
), M_SYSCTLOID
);
375 if (oidp
->oid_refcnt
> 1 ) {
378 if (oidp
->oid_refcnt
== 0) {
379 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
380 oidp
->oid_refcnt
, oidp
->oid_name
);
383 sysctl_unregister_oid(oidp
);
386 * Wait for all threads running the handler to drain.
387 * This preserves the previous behavior when the
388 * sysctl lock was held across a handler invocation,
389 * and is necessary for module unload correctness.
391 while (oidp
->oid_running
> 0) {
392 oidp
->oid_kind
|= CTLFLAG_DYING
;
393 tsleep_interlock(&oidp
->oid_running
, 0);
395 tsleep(&oidp
->oid_running
, PINTERLOCKED
,
400 kfree(__DECONST(char *, oidp
->oid_descr
),
402 kfree(__DECONST(char *, oidp
->oid_name
), M_SYSCTLOID
);
403 lockuninit(&oidp
->oid_lock
);
404 kfree(oidp
, M_SYSCTLOID
);
411 sysctl_remove_name(struct sysctl_oid
*parent
, const char *name
,
412 int del
, int recurse
)
414 struct sysctl_oid
*p
, *tmp
;
419 SLIST_FOREACH_MUTABLE(p
, SYSCTL_CHILDREN(parent
), oid_link
, tmp
) {
420 if (strcmp(p
->oid_name
, name
) == 0) {
421 error
= sysctl_remove_oid_locked(p
, del
, recurse
);
431 * Create new sysctls at run time.
432 * clist may point to a valid context initialized with sysctl_ctx_init().
435 sysctl_add_oid(struct sysctl_ctx_list
*clist
, struct sysctl_oid_list
*parent
,
436 int number
, const char *name
, int kind
, void *arg1
, int arg2
,
437 int (*handler
)(SYSCTL_HANDLER_ARGS
), const char *fmt
, const char *descr
)
439 struct sysctl_oid
*oidp
;
443 /* You have to hook up somewhere.. */
447 /* Check if the node already exists, otherwise create it */
448 oidp
= sysctl_find_oidname(name
, parent
, 0);
450 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
452 /* Update the context */
454 sysctl_ctx_entry_add(clist
, oidp
);
458 kprintf("can't re-use a leaf (%s)!\n", name
);
463 oidp
= kmalloc(sizeof(struct sysctl_oid
), M_SYSCTLOID
,
465 oidp
->oid_parent
= parent
;
466 SLIST_NEXT(oidp
, oid_link
) = NULL
;
467 oidp
->oid_number
= number
;
468 oidp
->oid_refcnt
= 1;
470 newname
= kmalloc(len
+ 1, M_SYSCTLOID
, M_WAITOK
);
471 bcopy(name
, newname
, len
+ 1);
473 oidp
->oid_name
= newname
;
474 oidp
->oid_handler
= handler
;
475 oidp
->oid_kind
= CTLFLAG_DYN
| kind
;
476 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
477 struct sysctl_oid_list
*children
;
479 /* Allocate space for children */
480 children
= kmalloc(sizeof(*children
), M_SYSCTLOID
, M_WAITOK
);
481 SYSCTL_SET_CHILDREN(oidp
, children
);
482 SLIST_INIT(children
);
484 oidp
->oid_arg1
= arg1
;
485 oidp
->oid_arg2
= arg2
;
489 int len
= strlen(descr
) + 1;
490 oidp
->oid_descr
= kmalloc(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_int(oidp
);
503 * Rename an existing oid.
506 sysctl_rename_oid(struct sysctl_oid
*oidp
, const char *name
)
511 newname
= kstrdup(name
, M_SYSCTLOID
);
513 oldname
= __DECONST(char *, oidp
->oid_name
);
514 oidp
->oid_name
= newname
;
516 kfree(oldname
, M_SYSCTLOID
);
520 * Register the kernel's oids on startup.
522 SET_DECLARE(sysctl_set
, struct sysctl_oid
);
525 sysctl_register_all(void *arg
)
527 struct sysctl_oid
**oidp
;
530 SET_FOREACH(oidp
, sysctl_set
)
531 sysctl_register_oid(*oidp
);
534 SYSINIT(sysctl
, SI_BOOT1_POST
, SI_ORDER_ANY
, sysctl_register_all
, 0);
540 * These functions implement a presently undocumented interface
541 * used by the sysctl program to walk the tree, and get the type
542 * so it can print the value.
543 * This interface is under work and consideration, and should probably
544 * be killed with a big axe by the first person who can find the time.
545 * (be aware though, that the proper interface isn't as obvious as it
546 * may seem, there are various conflicting requirements.
548 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} kprintf the entire MIB-tree.
549 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
551 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID.
552 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
554 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
556 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
561 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list
*l
, int i
)
564 struct sysctl_oid
*oidp
;
566 SLIST_FOREACH(oidp
, l
, oid_link
) {
571 kprintf("%d %s ", oidp
->oid_number
, oidp
->oid_name
);
574 oidp
->oid_kind
& CTLFLAG_RD
? 'R':' ',
575 oidp
->oid_kind
& CTLFLAG_WR
? 'W':' ');
577 if (oidp
->oid_handler
)
578 kprintf(" *Handler");
580 switch (oidp
->oid_kind
& CTLTYPE
) {
583 if (!oidp
->oid_handler
) {
584 sysctl_sysctl_debug_dump_node(
585 oidp
->oid_arg1
, i
+2);
598 kprintf(" u_long\n");
601 kprintf(" String\n");
604 kprintf(" int8_t\n");
607 kprintf(" int16_t\n");
610 kprintf(" int32_t\n");
613 kprintf(" int64_t\n");
616 kprintf(" uint8_t\n");
619 kprintf(" uint16_t\n");
622 kprintf(" uint32_t\n");
625 kprintf(" uint64_t\n");
627 case CTLTYPE_BIT32(0):
630 case CTLTYPE_BIT64(0):
634 kprintf(" Opaque/struct\n");
645 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS
)
649 error
= caps_priv_check_td(req
->td
, SYSCAP_NODEBUG_UNPRIV
);
652 sysctl_sysctl_debug_dump_node(&sysctl__children
, 0);
657 SYSCTL_PROC(_sysctl
, CTL_SYSCTL_DEBUG
, debug
, CTLTYPE_STRING
| CTLFLAG_RD
,
658 0, 0, sysctl_sysctl_debug
, "-", "");
659 #endif /* SYSCTL_DEBUG */
662 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS
)
664 int *name
= (int *) arg1
;
665 u_int namelen
= arg2
;
667 struct sysctl_oid
*oid
;
668 struct sysctl_oid_list
*lsp
= &sysctl__children
, *lsp2
;
673 ksnprintf(buf
, sizeof(buf
), "%d", *name
);
675 error
= SYSCTL_OUT(req
, ".", 1);
677 error
= SYSCTL_OUT(req
, buf
, strlen(buf
));
685 SLIST_FOREACH(oid
, lsp
, oid_link
) {
686 if (oid
->oid_number
!= *name
)
690 error
= SYSCTL_OUT(req
, ".", 1);
692 error
= SYSCTL_OUT(req
, oid
->oid_name
,
693 strlen(oid
->oid_name
));
700 if ((oid
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
703 if (oid
->oid_handler
)
706 lsp2
= SYSCTL_CHILDREN(oid
);
711 error
= SYSCTL_OUT(req
, "", 1);
716 SYSCTL_NODE(_sysctl
, CTL_SYSCTL_NAME
, name
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
717 sysctl_sysctl_name
, "");
720 sysctl_sysctl_next_ls(struct sysctl_oid_list
*lsp
, int *name
, u_int namelen
,
721 int *next
, int *len
, int level
, struct sysctl_oid
**oidpp
)
723 struct sysctl_oid
*oidp
;
726 SLIST_FOREACH(oidp
, lsp
, oid_link
) {
727 *next
= oidp
->oid_number
;
730 if (oidp
->oid_kind
& CTLFLAG_SKIP
)
734 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
736 if (oidp
->oid_handler
)
737 /* We really should call the handler here...*/
739 lsp
= SYSCTL_CHILDREN(oidp
);
740 if (!sysctl_sysctl_next_ls(lsp
, 0, 0, next
+1,
741 len
, level
+1, oidpp
))
746 if (oidp
->oid_number
< *name
)
749 if (oidp
->oid_number
> *name
) {
750 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
752 if (oidp
->oid_handler
)
754 lsp
= SYSCTL_CHILDREN(oidp
);
755 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1,
756 next
+1, len
, level
+1, oidpp
))
760 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
763 if (oidp
->oid_handler
)
766 lsp
= SYSCTL_CHILDREN(oidp
);
767 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1, next
+1,
768 len
, level
+1, oidpp
))
779 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS
)
781 int *name
= (int *) arg1
;
782 u_int namelen
= arg2
;
784 struct sysctl_oid
*oid
;
785 struct sysctl_oid_list
*lsp
= &sysctl__children
;
786 int newoid
[CTL_MAXNAME
];
788 i
= sysctl_sysctl_next_ls(lsp
, name
, namelen
, newoid
, &j
, 1, &oid
);
791 error
= SYSCTL_OUT(req
, newoid
, j
* sizeof (int));
796 SYSCTL_NODE(_sysctl
, CTL_SYSCTL_NEXT
, next
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
797 sysctl_sysctl_next
, "");
800 name2oid(char *name
, int *oid
, int *len
, struct sysctl_oid
**oidpp
)
802 struct sysctl_oid
*oidp
;
803 struct sysctl_oid_list
*lsp
= &sysctl__children
;
806 SYSCTL_ASSERT_LOCKED();
808 for (*len
= 0; *len
< CTL_MAXNAME
;) {
809 p
= strsep(&name
, ".");
811 oidp
= SLIST_FIRST(lsp
);
812 for (;; oidp
= SLIST_NEXT(oidp
, oid_link
)) {
815 if (strcmp(p
, oidp
->oid_name
) == 0)
818 *oid
++ = oidp
->oid_number
;
821 if (name
== NULL
|| *name
== '\0') {
827 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
830 if (oidp
->oid_handler
)
833 lsp
= SYSCTL_CHILDREN(oidp
);
839 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS
)
842 int error
, oid
[CTL_MAXNAME
], len
;
843 struct sysctl_oid
*op
= NULL
;
847 if (req
->newlen
>= MAXPATHLEN
) /* XXX arbitrary, undocumented */
848 return (ENAMETOOLONG
);
850 p
= kmalloc(req
->newlen
+1, M_SYSCTL
, M_WAITOK
);
852 error
= SYSCTL_IN(req
, p
, req
->newlen
);
858 p
[req
->newlen
] = '\0';
860 error
= name2oid(p
, oid
, &len
, &op
);
867 error
= SYSCTL_OUT(req
, oid
, len
* sizeof *oid
);
871 SYSCTL_PROC(_sysctl
, CTL_SYSCTL_NAME2OID
, name2oid
,
872 CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_NOLOCK
,
873 0, 0, sysctl_sysctl_name2oid
, "I", "");
876 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS
)
878 struct sysctl_oid
*oid
;
881 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
887 error
= SYSCTL_OUT(req
, &oid
->oid_kind
, sizeof(oid
->oid_kind
));
890 error
= SYSCTL_OUT(req
, oid
->oid_fmt
, strlen(oid
->oid_fmt
) + 1);
895 SYSCTL_NODE(_sysctl
, CTL_SYSCTL_OIDFMT
, oidfmt
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
896 sysctl_sysctl_oidfmt
, "");
899 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS
)
901 struct sysctl_oid
*oid
;
904 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
910 error
= SYSCTL_OUT(req
, oid
->oid_descr
, strlen(oid
->oid_descr
) + 1);
914 SYSCTL_NODE(_sysctl
, CTL_SYSCTL_OIDDESCR
, oiddescr
,
915 CTLFLAG_RD
| CTLFLAG_NOLOCK
,
916 sysctl_sysctl_oiddescr
, "");
919 * Default "handler" functions.
923 * Handle an 8-bit number, signed or unsigned. arg1 points to it.
927 sysctl_handle_8(SYSCTL_HANDLER_ARGS
)
933 error
= SYSCTL_OUT(req
, arg1
, sizeof(int8_t));
935 if (error
|| !req
->newptr
)
938 error
= SYSCTL_IN(req
, arg1
, sizeof(int8_t));
943 * Handle a 16-bit number, signed or unsigned. arg1 points to it.
947 sysctl_handle_16(SYSCTL_HANDLER_ARGS
)
953 error
= SYSCTL_OUT(req
, arg1
, sizeof(int16_t));
955 if (error
|| !req
->newptr
)
958 error
= SYSCTL_IN(req
, arg1
, sizeof(int16_t));
963 * Handle a 32-bit number, signed or unsigned. arg1 points to it.
967 sysctl_handle_32(SYSCTL_HANDLER_ARGS
)
973 error
= SYSCTL_OUT(req
, arg1
, sizeof(int32_t));
975 if (error
|| !req
->newptr
)
978 error
= SYSCTL_IN(req
, arg1
, sizeof(int32_t));
983 * Handle a 64-bit number, signed or unsigned. arg1 points to it.
987 sysctl_handle_64(SYSCTL_HANDLER_ARGS
)
993 error
= SYSCTL_OUT(req
, arg1
, sizeof(int64_t));
995 if (error
|| !req
->newptr
)
998 error
= SYSCTL_IN(req
, arg1
, sizeof(int64_t));
1003 * Handle an int, signed or unsigned.
1005 * a variable: point arg1 at it.
1006 * a constant: pass it in arg2.
1010 sysctl_handle_int(SYSCTL_HANDLER_ARGS
)
1015 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
1017 error
= SYSCTL_OUT(req
, &arg2
, sizeof(int));
1019 if (error
|| !req
->newptr
)
1025 error
= SYSCTL_IN(req
, arg1
, sizeof(int));
1030 * Handle a long, signed or unsigned. arg1 points to it.
1034 sysctl_handle_long(SYSCTL_HANDLER_ARGS
)
1040 if (req
->oldlen
== sizeof(int) &&
1041 *(long *)arg1
>= INT_MIN
&&
1042 *(long *)arg1
<= INT_MAX
) {
1044 * Backwards compatibility for read-only fields promoted
1045 * from int to long. Allow userland to request the field
1046 * as an integer if the value is in-range.
1048 int val
= (int)*(long *)arg1
;
1049 error
= SYSCTL_OUT(req
, &val
, sizeof(int));
1052 * Normal operation fo a long
1054 error
= SYSCTL_OUT(req
, arg1
, sizeof(long));
1057 if (error
|| !req
->newptr
)
1060 error
= SYSCTL_IN(req
, arg1
, sizeof(long));
1066 * Handle a quad, signed or unsigned. arg1 points to it.
1070 sysctl_handle_quad(SYSCTL_HANDLER_ARGS
)
1076 error
= SYSCTL_OUT(req
, arg1
, sizeof(quad_t
));
1078 if (error
|| !req
->newptr
)
1081 error
= SYSCTL_IN(req
, arg1
, sizeof(quad_t
));
1086 * Handle an bit in a 32-bit field, pass and return an 'int'
1088 * a variable: point arg1 at it.
1089 * a constant: pass it in arg2.
1093 sysctl_handle_bit32(SYSCTL_HANDLER_ARGS
)
1100 bit
= (oidp
->oid_kind
& CTLMASK_BITFLD
) >> CTLSHIFT_BITFLD
;
1101 mask
= arg1
? *(uint32_t *)arg1
: (uint32_t)arg2
;
1102 v
= (mask
& (1U << bit
)) ? 1 : 0;
1103 error
= SYSCTL_OUT(req
, &v
, sizeof(int));
1105 if (error
|| !req
->newptr
)
1111 error
= SYSCTL_IN(req
, &v
, sizeof(int));
1114 atomic_set_int((uint32_t *)arg1
, 1U << bit
);
1116 atomic_clear_int((uint32_t *)arg1
, 1U << bit
);
1123 * Handle an bit in a 64-bit field, pass and return an 'int'
1125 * a variable: point arg1 at it.
1126 * a constant: pass it in arg2. (NOTE: arg2 is only 32bits)
1130 sysctl_handle_bit64(SYSCTL_HANDLER_ARGS
)
1137 bit
= (oidp
->oid_kind
& CTLMASK_BITFLD
) >> CTLSHIFT_BITFLD
;
1138 mask
= arg1
? *(uint64_t *)arg1
: (uint64_t)(uint32_t)arg2
;
1139 v
= (mask
& (1LU << bit
)) ? 1 : 0;
1140 error
= SYSCTL_OUT(req
, &v
, sizeof(int));
1142 if (error
|| !req
->newptr
)
1148 error
= SYSCTL_IN(req
, &v
, sizeof(int));
1151 atomic_set_long((uint64_t *)arg1
, 1LU << bit
);
1153 atomic_clear_long((uint64_t *)arg1
, 1LU << bit
);
1160 * Handle our generic '\0' terminated 'C' string.
1162 * a variable string: point arg1 at it, arg2 is max length.
1163 * a constant string: point arg1 at it, arg2 is zero.
1167 sysctl_handle_string(SYSCTL_HANDLER_ARGS
)
1171 error
= SYSCTL_OUT(req
, arg1
, strlen((char *)arg1
)+1);
1173 if (error
|| !req
->newptr
)
1176 if ((req
->newlen
- req
->newidx
) >= arg2
) {
1179 arg2
= (req
->newlen
- req
->newidx
);
1180 error
= SYSCTL_IN(req
, arg1
, arg2
);
1181 ((char *)arg1
)[arg2
] = '\0';
1188 * Handle any kind of opaque data.
1189 * arg1 points to it, arg2 is the size.
1193 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS
)
1197 error
= SYSCTL_OUT(req
, arg1
, arg2
);
1199 if (error
|| !req
->newptr
)
1202 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 (i
> req
->oldlen
- req
->oldidx
)
1219 i
= req
->oldlen
- req
->oldidx
;
1221 bcopy(p
, (char *)req
->oldptr
+ req
->oldidx
, i
);
1224 if (req
->oldptr
&& i
!= l
)
1230 sysctl_new_kernel(struct sysctl_req
*req
, void *p
, size_t l
)
1235 if (req
->newlen
- req
->newidx
< l
)
1237 bcopy((char *)req
->newptr
+ req
->newidx
, p
, l
);
1243 kernel_sysctl(int *name
, u_int namelen
,
1244 void *old
, size_t *oldlenp
,
1245 void *new, size_t newlen
, size_t *retval
)
1248 struct sysctl_req req
;
1250 bzero(&req
, sizeof req
);
1255 req
.oldlen
= *oldlenp
;
1257 req
.validlen
= req
.oldlen
;
1264 req
.newlen
= newlen
;
1268 req
.oldfunc
= sysctl_old_kernel
;
1269 req
.newfunc
= sysctl_new_kernel
;
1271 req
.lock
= REQ_UNWIRED
;
1275 error
= sysctl_root(0, name
, namelen
, &req
);
1279 if (req
.lock
== REQ_WIRED
&& req
.validlen
> 0)
1280 vsunlock(req
.oldptr
, req
.validlen
);
1283 if (error
&& error
!= ENOMEM
)
1287 if (req
.oldptr
&& req
.oldidx
> req
.validlen
)
1288 *retval
= req
.validlen
;
1290 *retval
= req
.oldidx
;
1296 kernel_sysctlbyname(char *name
,
1297 void *old
, size_t *oldlenp
,
1298 void *new, size_t newlen
, size_t *retval
)
1300 int oid
[CTL_MAXNAME
];
1301 size_t oidlen
, plen
;
1304 oid
[0] = CTL_SYSCTL
;
1305 oid
[1] = CTL_SYSCTL_NAME2OID
;
1306 oidlen
= sizeof(oid
);
1308 error
= kernel_sysctl(oid
, 2, oid
, &oidlen
, name
, strlen(name
), &plen
);
1312 error
= kernel_sysctl(oid
, plen
/ sizeof(int), old
, oldlenp
,
1313 new, newlen
, retval
);
1318 * Transfer function to/from user space.
1321 sysctl_old_user(struct sysctl_req
*req
, const void *p
, size_t l
)
1327 if (req
->lock
== 1 && req
->oldptr
) {
1328 vslock(req
->oldptr
, req
->oldlen
);
1334 if (i
> req
->oldlen
- req
->oldidx
)
1335 i
= req
->oldlen
- req
->oldidx
;
1337 error
= copyout(p
, (char *)req
->oldptr
+ req
->oldidx
,
1343 if (req
->oldptr
&& i
< l
)
1349 sysctl_new_user(struct sysctl_req
*req
, void *p
, size_t l
)
1355 if (req
->newlen
- req
->newidx
< l
)
1357 error
= copyin((char *)req
->newptr
+ req
->newidx
, p
, l
);
1363 sysctl_find_oid(int *name
, u_int namelen
, struct sysctl_oid
**noid
,
1364 int *nindx
, struct sysctl_req
*req
)
1366 struct sysctl_oid_list
*lsp
;
1367 struct sysctl_oid
*oid
;
1370 lsp
= &sysctl__children
;
1372 while (indx
< CTL_MAXNAME
) {
1373 SLIST_FOREACH(oid
, lsp
, oid_link
) {
1374 if (oid
->oid_number
== name
[indx
])
1381 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1382 if (oid
->oid_handler
!= NULL
|| indx
== namelen
) {
1386 KASSERT((oid
->oid_kind
& CTLFLAG_DYING
) == 0,
1387 ("%s found DYING node %p", __func__
, oid
));
1390 lsp
= SYSCTL_CHILDREN(oid
);
1391 } else if (indx
== namelen
) {
1395 KASSERT((oid
->oid_kind
& CTLFLAG_DYING
) == 0,
1396 ("%s found DYING node %p", __func__
, oid
));
1406 * Traverse our tree, and find the right node, execute whatever it points
1407 * to, and return the resulting error code.
1410 sysctl_root(SYSCTL_HANDLER_ARGS
)
1412 struct thread
*td
= req
->td
;
1413 struct proc
*p
= td
? td
->td_proc
: NULL
;
1414 struct sysctl_oid
*oid
;
1418 error
= sysctl_find_oid(arg1
, arg2
, &oid
, &indx
, req
);
1422 if (sysctl_debugx
& 1) {
1423 kprintf("pid %d oid %p %s\n",
1424 (p
? p
->p_pid
: -1), oid
, oid
->oid_name
);
1428 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1430 * You can't call a sysctl when it's a node, but has
1431 * no handler. Inform the user that it's a node.
1432 * The indx may or may not be the same as namelen.
1434 if (oid
->oid_handler
== NULL
)
1438 /* If writing isn't allowed */
1439 if (req
->newptr
&& (!(oid
->oid_kind
& CTLFLAG_WR
) ||
1440 ((oid
->oid_kind
& CTLFLAG_SECURE
) && securelevel
> 0)))
1445 /* Most likely only root can write */
1446 if (!(oid
->oid_kind
& CTLFLAG_ANYBODY
) && req
->newptr
&& p
&&
1447 (error
= caps_priv_check(td
->td_ucred
, SYSCAP_NOSYSCTL_WR
)))
1452 if (oid
->oid_handler
== NULL
)
1456 * Default oid locking is exclusive when modifying (newptr),
1457 * shared otherwise, unless overridden with a control flag.
1459 if ((oid
->oid_kind
& CTLFLAG_NOLOCK
) == 0) {
1460 lktype
= (req
->newptr
!= NULL
) ? LK_EXCLUSIVE
: LK_SHARED
;
1461 if (oid
->oid_kind
& CTLFLAG_SHLOCK
)
1463 if (oid
->oid_kind
& CTLFLAG_EXLOCK
)
1464 lktype
= LK_EXCLUSIVE
;
1466 lockmgr(&oid
->oid_lock
, lktype
);
1469 if (lockmgr(&oid
->oid_lock
, lktype
| LK_SLEEPFAIL
)) {
1470 kprintf("%s\n", oid
->oid_name
);
1471 lockmgr(&oid
->oid_lock
, lktype
);
1476 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
1477 error
= oid
->oid_handler(oid
, (int *)arg1
+ indx
, arg2
- indx
,
1480 error
= oid
->oid_handler(oid
, oid
->oid_arg1
, oid
->oid_arg2
,
1483 if ((oid
->oid_kind
& CTLFLAG_NOLOCK
) == 0)
1484 lockmgr(&oid
->oid_lock
, LK_RELEASE
);
1489 sys___sysctl(struct sysmsg
*sysmsg
, const struct sysctl_args
*uap
)
1491 int error
, i
, name
[CTL_MAXNAME
];
1494 if (uap
->namelen
> CTL_MAXNAME
|| uap
->namelen
< 2)
1497 error
= copyin(uap
->name
, &name
, uap
->namelen
* sizeof(int));
1501 error
= userland_sysctl(name
, uap
->namelen
,
1502 uap
->old
, uap
->oldlenp
, 0,
1503 uap
->new, uap
->newlen
, &j
);
1504 if (error
&& error
!= ENOMEM
)
1507 i
= copyout(&j
, uap
->oldlenp
, sizeof(j
));
1515 * This is used from various compatibility syscalls too. That's why name
1516 * must be in kernel space.
1519 userland_sysctl(int *name
, u_int namelen
,
1520 void *old
, size_t *oldlenp
, int inkernel
,
1521 void *new, size_t newlen
, size_t *retval
)
1523 struct thread
*td
= curthread
;
1524 struct lwp
*lp
= td
->td_lwp
;
1526 struct sysctl_req req
;
1528 bzero(&req
, sizeof req
);
1535 req
.oldlen
= *oldlenp
;
1537 error
= copyin(oldlenp
, &req
.oldlen
, sizeof(*oldlenp
));
1542 req
.validlen
= req
.oldlen
;
1545 * NOTE: User supplied buffers are not guaranteed to be good,
1546 * the sysctl copyins and copyouts can fail.
1552 req
.newlen
= newlen
;
1556 req
.oldfunc
= sysctl_old_user
;
1557 req
.newfunc
= sysctl_new_user
;
1559 req
.lock
= REQ_UNWIRED
;
1563 if (KTRPOINT(td
, KTR_SYSCTL
))
1564 ktrsysctl(lp
, name
, namelen
);
1571 error
= sysctl_root(0, name
, namelen
, &req
);
1573 if (error
!= EAGAIN
)
1579 if (req
.lock
== REQ_WIRED
&& req
.validlen
> 0)
1580 vsunlock(req
.oldptr
, req
.validlen
);
1582 if (error
&& error
!= ENOMEM
)
1586 if (req
.oldptr
&& req
.oldidx
> req
.validlen
)
1587 *retval
= req
.validlen
;
1589 *retval
= req
.oldidx
;
1595 sysctl_int_range(SYSCTL_HANDLER_ARGS
, int low
, int high
)
1599 value
= *(int *)arg1
;
1600 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
1601 if (error
|| !req
->newptr
)
1603 if (value
< low
|| value
> high
)
1605 *(int *)arg1
= value
;
1610 * Drain into a sysctl struct. The user buffer should be wired if a page
1611 * fault would cause issue.
1614 sbuf_sysctl_drain(void *arg
, const char *data
, int len
)
1616 struct sysctl_req
*req
= arg
;
1619 error
= SYSCTL_OUT(req
, data
, len
);
1620 KASSERT(error
>= 0, ("Got unexpected negative value %d", error
));
1621 return (error
== 0 ? len
: -error
);
1625 sbuf_new_for_sysctl(struct sbuf
*s
, char *buf
, int length
,
1626 struct sysctl_req
*req
)
1629 s
= sbuf_new(s
, buf
, length
, SBUF_FIXEDLEN
);
1630 sbuf_set_drain(s
, sbuf_sysctl_drain
, req
);
1635 * The exclusive sysctl lock only protects its topology, and is
1636 * very expensive, but allows us to use a pcpu shared lock for
1637 * critical path accesses.
1645 for (i
= 0; i
< ncpus
; ++i
) {
1646 gd
= globaldata_find(i
);
1647 lockmgr(&gd
->gd_sysctllock
, LK_EXCLUSIVE
);
1652 _sysctl_xunlock(void)
1657 for (i
= 0; i
< ncpus
; ++i
) {
1658 gd
= globaldata_find(i
);
1659 lockmgr(&gd
->gd_sysctllock
, LK_RELEASE
);