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 <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
47 #include <sys/sysproto.h>
52 #include <vm/vm_extern.h>
54 static MALLOC_DEFINE(M_SYSCTL
, "sysctl", "sysctl internal magic");
55 static MALLOC_DEFINE(M_SYSCTLOID
, "sysctloid", "sysctl dynamic oids");
58 * The sysctllock protects the MIB tree. It also protects sysctl
59 * contexts used with dynamic sysctls. The sysctl_register_oid() and
60 * sysctl_unregister_oid() routines require the sysctllock to already
61 * be held, so the sysctl_lock() and sysctl_unlock() routines are
62 * provided for the few places in the kernel which need to use that
63 * API rather than using the dynamic API. Use of the dynamic API is
64 * strongly encouraged for most code.
67 static int sysctl_root(SYSCTL_HANDLER_ARGS
);
68 static void sysctl_register_oid_int(struct sysctl_oid
*oipd
);
69 static void sysctl_unregister_oid_int(struct sysctl_oid
*oipd
);
71 struct sysctl_oid_list sysctl__children
; /* root list */
73 static int sysctl_remove_oid_locked(struct sysctl_oid
*oidp
, int del
,
76 static struct sysctl_oid
*
77 sysctl_find_oidname(const char *name
, struct sysctl_oid_list
*list
, int lock
)
79 struct sysctl_oid
*oidp
;
81 SLIST_FOREACH(oidp
, list
, oid_link
) {
82 if (strcmp(oidp
->oid_name
, name
) == 0) {
90 * Initialization of the MIB tree.
92 * Order by number in each list.
96 sysctl_register_oid(struct sysctl_oid
*oidp
)
99 sysctl_register_oid_int(oidp
);
104 sysctl_register_oid_int(struct sysctl_oid
*oidp
)
106 struct sysctl_oid_list
*parent
= oidp
->oid_parent
;
107 struct sysctl_oid
*p
;
108 struct sysctl_oid
*q
;
111 * Finish initialization from sysctl_set or add.
113 lockinit(&oidp
->oid_lock
, "oidlk", 0, LK_CANRECURSE
);
116 * First check if another oid with the same name already
117 * exists in the parent's list.
119 p
= sysctl_find_oidname(oidp
->oid_name
, parent
, 0);
121 if ((p
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
124 kprintf("can't re-use a leaf (%s)!\n", p
->oid_name
);
129 * If this oid has a number OID_AUTO, give it a number which
130 * is greater than any current oid. Make sure it is at least
131 * 256 to leave space for pre-assigned oid numbers.
133 if (oidp
->oid_number
== OID_AUTO
) {
134 int newoid
= 0x100; /* minimum AUTO oid */
137 * Adjust based on highest oid in parent list
139 SLIST_FOREACH(p
, parent
, oid_link
) {
140 if (newoid
<= p
->oid_number
)
141 newoid
= p
->oid_number
+ 1;
143 oidp
->oid_number
= newoid
;
147 * Insert the oid into the parent's list in order.
150 SLIST_FOREACH(p
, parent
, oid_link
) {
151 if (oidp
->oid_number
< p
->oid_number
)
156 SLIST_INSERT_AFTER(q
, oidp
, oid_link
);
158 SLIST_INSERT_HEAD(parent
, oidp
, oid_link
);
162 sysctl_unregister_oid(struct sysctl_oid
*oidp
)
165 sysctl_unregister_oid_int(oidp
);
170 sysctl_unregister_oid_int(struct sysctl_oid
*oidp
)
172 struct sysctl_oid
*p
;
174 if (oidp
->oid_number
== OID_AUTO
)
175 panic("Trying to unregister OID_AUTO entry: %p", oidp
);
177 SLIST_FOREACH(p
, oidp
->oid_parent
, oid_link
) {
180 SLIST_REMOVE(oidp
->oid_parent
, oidp
, sysctl_oid
, oid_link
);
185 * This can happen when a module fails to register and is
186 * being unloaded afterwards. It should not be a panic()
189 kprintf("%s: failed to unregister sysctl\n", __func__
);
192 /* Initialize a new context to keep track of dynamically added sysctls. */
194 sysctl_ctx_init(struct sysctl_ctx_list
*c
)
202 /* Free the context, and destroy all dynamic oids registered in this context */
204 sysctl_ctx_free(struct sysctl_ctx_list
*clist
)
206 struct sysctl_ctx_entry
*e
, *e1
;
211 * First perform a "dry run" to check if it's ok to remove oids.
213 * XXX This algorithm is a hack. But I don't know any
214 * XXX better solution for now...
217 TAILQ_FOREACH(e
, clist
, link
) {
218 error
= sysctl_remove_oid_locked(e
->entry
, 0, 0);
223 * Restore deregistered entries, either from the end,
224 * or from the place where error occured.
225 * e contains the entry that was not unregistered
228 e1
= TAILQ_PREV(e
, sysctl_ctx_list
, link
);
230 e1
= TAILQ_LAST(clist
, sysctl_ctx_list
);
232 sysctl_register_oid(e1
->entry
);
233 e1
= TAILQ_PREV(e1
, sysctl_ctx_list
, link
);
239 /* Now really delete the entries */
240 e
= TAILQ_FIRST(clist
);
242 e1
= TAILQ_NEXT(e
, link
);
243 error
= sysctl_remove_oid_locked(e
->entry
, 1, 0);
245 panic("sysctl_remove_oid: corrupt tree, entry: %s",
247 kfree(e
, M_SYSCTLOID
);
254 /* Add an entry to the context */
255 struct sysctl_ctx_entry
*
256 sysctl_ctx_entry_add(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
258 struct sysctl_ctx_entry
*e
;
260 SYSCTL_ASSERT_LOCKED();
261 if (clist
== NULL
|| oidp
== NULL
)
263 e
= kmalloc(sizeof(struct sysctl_ctx_entry
), M_SYSCTLOID
, M_WAITOK
);
265 TAILQ_INSERT_HEAD(clist
, e
, link
);
269 /* Find an entry in the context */
270 struct sysctl_ctx_entry
*
271 sysctl_ctx_entry_find(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
273 struct sysctl_ctx_entry
*e
;
275 SYSCTL_ASSERT_LOCKED();
276 if (clist
== NULL
|| oidp
== NULL
)
278 TAILQ_FOREACH(e
, clist
, link
) {
286 * Delete an entry from the context.
287 * NOTE: this function doesn't free oidp! You have to remove it
288 * with sysctl_remove_oid().
291 sysctl_ctx_entry_del(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
293 struct sysctl_ctx_entry
*e
;
295 if (clist
== NULL
|| oidp
== NULL
)
298 e
= sysctl_ctx_entry_find(clist
, oidp
);
300 TAILQ_REMOVE(clist
, e
, link
);
302 kfree(e
, M_SYSCTLOID
);
311 * Remove dynamically created sysctl trees.
312 * oidp - top of the tree to be removed
313 * del - if 0 - just deregister, otherwise free up entries as well
314 * recurse - if != 0 traverse the subtree to be deleted
317 sysctl_remove_oid(struct sysctl_oid
*oidp
, int del
, int recurse
)
322 error
= sysctl_remove_oid_locked(oidp
, del
, recurse
);
328 sysctl_remove_oid_locked(struct sysctl_oid
*oidp
, int del
, int recurse
)
330 struct sysctl_oid
*p
, *tmp
;
333 SYSCTL_ASSERT_LOCKED();
336 if ((oidp
->oid_kind
& CTLFLAG_DYN
) == 0) {
337 kprintf("can't remove non-dynamic nodes!\n");
341 * WARNING: normal method to do this should be through
342 * sysctl_ctx_free(). Use recursing as the last resort
343 * method to purge your sysctl tree of leftovers...
344 * However, if some other code still references these nodes,
347 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
348 if (oidp
->oid_refcnt
== 1) {
349 SLIST_FOREACH_MUTABLE(p
,
350 SYSCTL_CHILDREN(oidp
), oid_link
, tmp
) {
352 kprintf("Warning: failed attempt to "
353 "remove oid %s with child %s\n",
354 oidp
->oid_name
, p
->oid_name
);
357 error
= sysctl_remove_oid_locked(p
, del
,
363 kfree(SYSCTL_CHILDREN(oidp
), M_SYSCTLOID
);
366 if (oidp
->oid_refcnt
> 1 ) {
369 if (oidp
->oid_refcnt
== 0) {
370 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
371 oidp
->oid_refcnt
, oidp
->oid_name
);
374 sysctl_unregister_oid(oidp
);
377 * Wait for all threads running the handler to drain.
378 * This preserves the previous behavior when the
379 * sysctl lock was held across a handler invocation,
380 * and is necessary for module unload correctness.
382 while (oidp
->oid_running
> 0) {
383 oidp
->oid_kind
|= CTLFLAG_DYING
;
384 tsleep_interlock(&oidp
->oid_running
, 0);
386 tsleep(&oidp
->oid_running
, PINTERLOCKED
,
391 kfree(__DECONST(char *, oidp
->oid_descr
),
393 kfree(__DECONST(char *, oidp
->oid_name
), M_SYSCTLOID
);
394 lockuninit(&oidp
->oid_lock
);
395 kfree(oidp
, M_SYSCTLOID
);
402 sysctl_remove_name(struct sysctl_oid
*parent
, const char *name
,
403 int del
, int recurse
)
405 struct sysctl_oid
*p
, *tmp
;
410 SLIST_FOREACH_MUTABLE(p
, SYSCTL_CHILDREN(parent
), oid_link
, tmp
) {
411 if (strcmp(p
->oid_name
, name
) == 0) {
412 error
= sysctl_remove_oid_locked(p
, del
, recurse
);
422 * Create new sysctls at run time.
423 * clist may point to a valid context initialized with sysctl_ctx_init().
426 sysctl_add_oid(struct sysctl_ctx_list
*clist
, struct sysctl_oid_list
*parent
,
427 int number
, const char *name
, int kind
, void *arg1
, int arg2
,
428 int (*handler
)(SYSCTL_HANDLER_ARGS
), const char *fmt
, const char *descr
)
430 struct sysctl_oid
*oidp
;
434 /* You have to hook up somewhere.. */
438 /* Check if the node already exists, otherwise create it */
439 oidp
= sysctl_find_oidname(name
, parent
, 0);
441 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
443 /* Update the context */
445 sysctl_ctx_entry_add(clist
, oidp
);
449 kprintf("can't re-use a leaf (%s)!\n", name
);
454 oidp
= kmalloc(sizeof(struct sysctl_oid
), M_SYSCTLOID
,
456 oidp
->oid_parent
= parent
;
457 SLIST_NEXT(oidp
, oid_link
) = NULL
;
458 oidp
->oid_number
= number
;
459 oidp
->oid_refcnt
= 1;
461 newname
= kmalloc(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 struct sysctl_oid_list
*children
;
470 /* Allocate space for children */
471 children
= kmalloc(sizeof(*children
), M_SYSCTLOID
, M_WAITOK
);
472 SYSCTL_SET_CHILDREN(oidp
, children
);
473 SLIST_INIT(children
);
475 oidp
->oid_arg1
= arg1
;
476 oidp
->oid_arg2
= arg2
;
480 int len
= strlen(descr
) + 1;
481 oidp
->oid_descr
= kmalloc(len
, M_SYSCTLOID
, M_WAITOK
);
482 strcpy((char *)(uintptr_t)(const void *)oidp
->oid_descr
, descr
);
484 /* Update the context, if used */
486 sysctl_ctx_entry_add(clist
, oidp
);
487 /* Register this oid */
488 sysctl_register_oid_int(oidp
);
494 * Rename an existing oid.
497 sysctl_rename_oid(struct sysctl_oid
*oidp
, const char *name
)
502 newname
= kstrdup(name
, M_SYSCTLOID
);
504 oldname
= __DECONST(char *, oidp
->oid_name
);
505 oidp
->oid_name
= newname
;
507 kfree(oldname
, M_SYSCTLOID
);
511 * Register the kernel's oids on startup.
513 SET_DECLARE(sysctl_set
, struct sysctl_oid
);
516 sysctl_register_all(void *arg
)
518 struct sysctl_oid
**oidp
;
521 SET_FOREACH(oidp
, sysctl_set
)
522 sysctl_register_oid(*oidp
);
525 SYSINIT(sysctl
, SI_BOOT1_POST
, SI_ORDER_ANY
, sysctl_register_all
, 0);
530 * These functions implement a presently undocumented interface
531 * used by the sysctl program to walk the tree, and get the type
532 * so it can print the value.
533 * This interface is under work and consideration, and should probably
534 * be killed with a big axe by the first person who can find the time.
535 * (be aware though, that the proper interface isn't as obvious as it
536 * may seem, there are various conflicting requirements.
538 * {0,0} kprintf the entire MIB-tree.
539 * {0,1,...} return the name of the "..." OID.
540 * {0,2,...} return the next OID.
541 * {0,3} return the OID of the name in "new"
542 * {0,4,...} return the kind & format info for the "..." OID.
546 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list
*l
, int i
)
549 struct sysctl_oid
*oidp
;
551 SLIST_FOREACH(oidp
, l
, oid_link
) {
556 kprintf("%d %s ", oidp
->oid_number
, oidp
->oid_name
);
559 oidp
->oid_kind
& CTLFLAG_RD
? 'R':' ',
560 oidp
->oid_kind
& CTLFLAG_WR
? 'W':' ');
562 if (oidp
->oid_handler
)
563 kprintf(" *Handler");
565 switch (oidp
->oid_kind
& CTLTYPE
) {
568 if (!oidp
->oid_handler
) {
569 sysctl_sysctl_debug_dump_node(
570 oidp
->oid_arg1
, i
+2);
573 case CTLTYPE_INT
: kprintf(" Int\n"); break;
574 case CTLTYPE_UINT
: kprintf(" u_int\n"); break;
575 case CTLTYPE_LONG
: kprintf(" Long\n"); break;
576 case CTLTYPE_ULONG
: kprintf(" u_long\n"); break;
577 case CTLTYPE_STRING
: kprintf(" String\n"); break;
578 case CTLTYPE_S8
: kprintf(" int8_t\n"); break;
579 case CTLTYPE_S16
: kprintf(" int16_t\n"); break;
580 case CTLTYPE_S32
: kprintf(" int32_t\n"); break;
581 case CTLTYPE_S64
: kprintf(" int64_t\n"); break;
582 case CTLTYPE_U8
: kprintf(" uint8_t\n"); break;
583 case CTLTYPE_U16
: kprintf(" uint16_t\n"); break;
584 case CTLTYPE_U32
: kprintf(" uint32_t\n"); break;
585 case CTLTYPE_U64
: kprintf(" uint64_t\n"); break;
586 case CTLTYPE_OPAQUE
: kprintf(" Opaque/struct\n"); break;
587 default: kprintf("\n");
594 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS
)
598 error
= priv_check(req
->td
, PRIV_SYSCTL_DEBUG
);
601 sysctl_sysctl_debug_dump_node(&sysctl__children
, 0);
606 SYSCTL_PROC(_sysctl
, 0, debug
, CTLTYPE_STRING
| CTLFLAG_RD
,
607 0, 0, sysctl_sysctl_debug
, "-", "");
610 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS
)
612 int *name
= (int *) arg1
;
613 u_int namelen
= arg2
;
615 struct sysctl_oid
*oid
;
616 struct sysctl_oid_list
*lsp
= &sysctl__children
, *lsp2
;
621 ksnprintf(buf
, sizeof(buf
), "%d", *name
);
623 error
= SYSCTL_OUT(req
, ".", 1);
625 error
= SYSCTL_OUT(req
, buf
, strlen(buf
));
633 SLIST_FOREACH(oid
, lsp
, oid_link
) {
634 if (oid
->oid_number
!= *name
)
638 error
= SYSCTL_OUT(req
, ".", 1);
640 error
= SYSCTL_OUT(req
, oid
->oid_name
,
641 strlen(oid
->oid_name
));
648 if ((oid
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
651 if (oid
->oid_handler
)
654 lsp2
= SYSCTL_CHILDREN(oid
);
659 error
= SYSCTL_OUT(req
, "", 1);
664 SYSCTL_NODE(_sysctl
, 1, name
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
665 sysctl_sysctl_name
, "");
668 sysctl_sysctl_next_ls(struct sysctl_oid_list
*lsp
, int *name
, u_int namelen
,
669 int *next
, int *len
, int level
, struct sysctl_oid
**oidpp
)
671 struct sysctl_oid
*oidp
;
674 SLIST_FOREACH(oidp
, lsp
, oid_link
) {
675 *next
= oidp
->oid_number
;
678 if (oidp
->oid_kind
& CTLFLAG_SKIP
)
682 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
684 if (oidp
->oid_handler
)
685 /* We really should call the handler here...*/
687 lsp
= SYSCTL_CHILDREN(oidp
);
688 if (!sysctl_sysctl_next_ls(lsp
, 0, 0, next
+1,
689 len
, level
+1, oidpp
))
694 if (oidp
->oid_number
< *name
)
697 if (oidp
->oid_number
> *name
) {
698 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
700 if (oidp
->oid_handler
)
702 lsp
= SYSCTL_CHILDREN(oidp
);
703 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1,
704 next
+1, len
, level
+1, oidpp
))
708 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
711 if (oidp
->oid_handler
)
714 lsp
= SYSCTL_CHILDREN(oidp
);
715 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1, next
+1,
716 len
, level
+1, oidpp
))
727 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS
)
729 int *name
= (int *) arg1
;
730 u_int namelen
= arg2
;
732 struct sysctl_oid
*oid
;
733 struct sysctl_oid_list
*lsp
= &sysctl__children
;
734 int newoid
[CTL_MAXNAME
];
736 i
= sysctl_sysctl_next_ls(lsp
, name
, namelen
, newoid
, &j
, 1, &oid
);
739 error
= SYSCTL_OUT(req
, newoid
, j
* sizeof (int));
744 SYSCTL_NODE(_sysctl
, 2, next
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
745 sysctl_sysctl_next
, "");
748 name2oid(char *name
, int *oid
, int *len
, struct sysctl_oid
**oidpp
)
750 struct sysctl_oid
*oidp
;
751 struct sysctl_oid_list
*lsp
= &sysctl__children
;
754 SYSCTL_ASSERT_LOCKED();
756 for (*len
= 0; *len
< CTL_MAXNAME
;) {
757 p
= strsep(&name
, ".");
759 oidp
= SLIST_FIRST(lsp
);
760 for (;; oidp
= SLIST_NEXT(oidp
, oid_link
)) {
763 if (strcmp(p
, oidp
->oid_name
) == 0)
766 *oid
++ = oidp
->oid_number
;
769 if (name
== NULL
|| *name
== '\0') {
775 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
778 if (oidp
->oid_handler
)
781 lsp
= SYSCTL_CHILDREN(oidp
);
787 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS
)
790 int error
, oid
[CTL_MAXNAME
], len
;
791 struct sysctl_oid
*op
= NULL
;
795 if (req
->newlen
>= MAXPATHLEN
) /* XXX arbitrary, undocumented */
796 return (ENAMETOOLONG
);
798 p
= kmalloc(req
->newlen
+1, M_SYSCTL
, M_WAITOK
);
800 error
= SYSCTL_IN(req
, p
, req
->newlen
);
806 p
[req
->newlen
] = '\0';
808 error
= name2oid(p
, oid
, &len
, &op
);
815 error
= SYSCTL_OUT(req
, oid
, len
* sizeof *oid
);
819 SYSCTL_PROC(_sysctl
, 3, name2oid
, CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_NOLOCK
,
820 0, 0, sysctl_sysctl_name2oid
, "I", "");
823 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS
)
825 struct sysctl_oid
*oid
;
828 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
834 error
= SYSCTL_OUT(req
, &oid
->oid_kind
, sizeof(oid
->oid_kind
));
837 error
= SYSCTL_OUT(req
, oid
->oid_fmt
, strlen(oid
->oid_fmt
) + 1);
842 SYSCTL_NODE(_sysctl
, 4, oidfmt
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
843 sysctl_sysctl_oidfmt
, "");
846 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS
)
848 struct sysctl_oid
*oid
;
851 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
857 error
= SYSCTL_OUT(req
, oid
->oid_descr
, strlen(oid
->oid_descr
) + 1);
861 SYSCTL_NODE(_sysctl
, 5, oiddescr
, CTLFLAG_RD
| CTLFLAG_NOLOCK
,
862 sysctl_sysctl_oiddescr
, "");
865 * Default "handler" functions.
869 * Handle an 8-bit number, signed or unsigned. arg1 points to it.
873 sysctl_handle_8(SYSCTL_HANDLER_ARGS
)
879 error
= SYSCTL_OUT(req
, arg1
, sizeof(int8_t));
881 if (error
|| !req
->newptr
)
884 error
= SYSCTL_IN(req
, arg1
, sizeof(int8_t));
889 * Handle a 16-bit number, signed or unsigned. arg1 points to it.
893 sysctl_handle_16(SYSCTL_HANDLER_ARGS
)
899 error
= SYSCTL_OUT(req
, arg1
, sizeof(int16_t));
901 if (error
|| !req
->newptr
)
904 error
= SYSCTL_IN(req
, arg1
, sizeof(int16_t));
909 * Handle a 32-bit number, signed or unsigned. arg1 points to it.
913 sysctl_handle_32(SYSCTL_HANDLER_ARGS
)
919 error
= SYSCTL_OUT(req
, arg1
, sizeof(int32_t));
921 if (error
|| !req
->newptr
)
924 error
= SYSCTL_IN(req
, arg1
, sizeof(int32_t));
929 * Handle a 64-bit number, signed or unsigned. arg1 points to it.
933 sysctl_handle_64(SYSCTL_HANDLER_ARGS
)
939 error
= SYSCTL_OUT(req
, arg1
, sizeof(int64_t));
941 if (error
|| !req
->newptr
)
944 error
= SYSCTL_IN(req
, arg1
, sizeof(int64_t));
949 * Handle an int, signed or unsigned.
951 * a variable: point arg1 at it.
952 * a constant: pass it in arg2.
956 sysctl_handle_int(SYSCTL_HANDLER_ARGS
)
961 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
963 error
= SYSCTL_OUT(req
, &arg2
, sizeof(int));
965 if (error
|| !req
->newptr
)
971 error
= SYSCTL_IN(req
, arg1
, sizeof(int));
976 * Handle a long, signed or unsigned. arg1 points to it.
980 sysctl_handle_long(SYSCTL_HANDLER_ARGS
)
986 if (req
->oldlen
== sizeof(int) &&
987 *(long *)arg1
>= INT_MIN
&&
988 *(long *)arg1
<= INT_MAX
) {
990 * Backwards compatibility for read-only fields promoted
991 * from int to long. Allow userland to request the field
992 * as an integer if the value is in-range.
994 int val
= (int)*(long *)arg1
;
995 error
= SYSCTL_OUT(req
, &val
, sizeof(int));
998 * Normal operation fo a long
1000 error
= SYSCTL_OUT(req
, arg1
, sizeof(long));
1003 if (error
|| !req
->newptr
)
1006 error
= SYSCTL_IN(req
, arg1
, sizeof(long));
1012 * Handle a quad, signed or unsigned. arg1 points to it.
1016 sysctl_handle_quad(SYSCTL_HANDLER_ARGS
)
1022 error
= SYSCTL_OUT(req
, arg1
, sizeof(quad_t
));
1024 if (error
|| !req
->newptr
)
1027 error
= SYSCTL_IN(req
, arg1
, sizeof(quad_t
));
1032 * Handle our generic '\0' terminated 'C' string.
1034 * a variable string: point arg1 at it, arg2 is max length.
1035 * a constant string: point arg1 at it, arg2 is zero.
1039 sysctl_handle_string(SYSCTL_HANDLER_ARGS
)
1043 error
= SYSCTL_OUT(req
, arg1
, strlen((char *)arg1
)+1);
1045 if (error
|| !req
->newptr
)
1048 if ((req
->newlen
- req
->newidx
) >= arg2
) {
1051 arg2
= (req
->newlen
- req
->newidx
);
1052 error
= SYSCTL_IN(req
, arg1
, arg2
);
1053 ((char *)arg1
)[arg2
] = '\0';
1060 * Handle any kind of opaque data.
1061 * arg1 points to it, arg2 is the size.
1065 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS
)
1069 error
= SYSCTL_OUT(req
, arg1
, arg2
);
1071 if (error
|| !req
->newptr
)
1074 error
= SYSCTL_IN(req
, arg1
, arg2
);
1080 * Transfer functions to/from kernel space.
1081 * XXX: rather untested at this point
1084 sysctl_old_kernel(struct sysctl_req
*req
, const void *p
, size_t l
)
1090 if (i
> req
->oldlen
- req
->oldidx
)
1091 i
= req
->oldlen
- req
->oldidx
;
1093 bcopy(p
, (char *)req
->oldptr
+ req
->oldidx
, i
);
1096 if (req
->oldptr
&& i
!= l
)
1102 sysctl_new_kernel(struct sysctl_req
*req
, void *p
, size_t l
)
1107 if (req
->newlen
- req
->newidx
< l
)
1109 bcopy((char *)req
->newptr
+ req
->newidx
, p
, l
);
1115 kernel_sysctl(int *name
, u_int namelen
,
1116 void *old
, size_t *oldlenp
,
1117 void *new, size_t newlen
, size_t *retval
)
1120 struct sysctl_req req
;
1122 bzero(&req
, sizeof req
);
1127 req
.oldlen
= *oldlenp
;
1129 req
.validlen
= req
.oldlen
;
1136 req
.newlen
= newlen
;
1140 req
.oldfunc
= sysctl_old_kernel
;
1141 req
.newfunc
= sysctl_new_kernel
;
1143 req
.lock
= REQ_UNWIRED
;
1147 error
= sysctl_root(0, name
, namelen
, &req
);
1151 if (req
.lock
== REQ_WIRED
&& req
.validlen
> 0)
1152 vsunlock(req
.oldptr
, req
.validlen
);
1155 if (error
&& error
!= ENOMEM
)
1159 if (req
.oldptr
&& req
.oldidx
> req
.validlen
)
1160 *retval
= req
.validlen
;
1162 *retval
= req
.oldidx
;
1168 kernel_sysctlbyname(char *name
,
1169 void *old
, size_t *oldlenp
,
1170 void *new, size_t newlen
, size_t *retval
)
1172 int oid
[CTL_MAXNAME
];
1173 size_t oidlen
, plen
;
1176 oid
[0] = 0; /* sysctl internal magic */
1177 oid
[1] = 3; /* name2oid */
1178 oidlen
= sizeof(oid
);
1180 error
= kernel_sysctl(oid
, 2, oid
, &oidlen
, name
, strlen(name
), &plen
);
1184 error
= kernel_sysctl(oid
, plen
/ sizeof(int), old
, oldlenp
,
1185 new, newlen
, retval
);
1190 * Transfer function to/from user space.
1193 sysctl_old_user(struct sysctl_req
*req
, const void *p
, size_t l
)
1199 if (req
->lock
== 1 && req
->oldptr
) {
1200 vslock(req
->oldptr
, req
->oldlen
);
1206 if (i
> req
->oldlen
- req
->oldidx
)
1207 i
= req
->oldlen
- req
->oldidx
;
1209 error
= copyout(p
, (char *)req
->oldptr
+ req
->oldidx
,
1215 if (req
->oldptr
&& i
< l
)
1221 sysctl_new_user(struct sysctl_req
*req
, void *p
, size_t l
)
1227 if (req
->newlen
- req
->newidx
< l
)
1229 error
= copyin((char *)req
->newptr
+ req
->newidx
, p
, l
);
1235 sysctl_find_oid(int *name
, u_int namelen
, struct sysctl_oid
**noid
,
1236 int *nindx
, struct sysctl_req
*req
)
1238 struct sysctl_oid_list
*lsp
;
1239 struct sysctl_oid
*oid
;
1242 lsp
= &sysctl__children
;
1244 while (indx
< CTL_MAXNAME
) {
1245 SLIST_FOREACH(oid
, lsp
, oid_link
) {
1246 if (oid
->oid_number
== name
[indx
])
1253 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1254 if (oid
->oid_handler
!= NULL
|| indx
== namelen
) {
1258 KASSERT((oid
->oid_kind
& CTLFLAG_DYING
) == 0,
1259 ("%s found DYING node %p", __func__
, oid
));
1262 lsp
= SYSCTL_CHILDREN(oid
);
1263 } else if (indx
== namelen
) {
1267 KASSERT((oid
->oid_kind
& CTLFLAG_DYING
) == 0,
1268 ("%s found DYING node %p", __func__
, oid
));
1278 * Traverse our tree, and find the right node, execute whatever it points
1279 * to, and return the resulting error code.
1282 sysctl_root(SYSCTL_HANDLER_ARGS
)
1284 struct thread
*td
= req
->td
;
1285 struct proc
*p
= td
? td
->td_proc
: NULL
;
1286 struct sysctl_oid
*oid
;
1290 error
= sysctl_find_oid(arg1
, arg2
, &oid
, &indx
, req
);
1294 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1296 * You can't call a sysctl when it's a node, but has
1297 * no handler. Inform the user that it's a node.
1298 * The indx may or may not be the same as namelen.
1300 if (oid
->oid_handler
== NULL
)
1304 /* If writing isn't allowed */
1305 if (req
->newptr
&& (!(oid
->oid_kind
& CTLFLAG_WR
) ||
1306 ((oid
->oid_kind
& CTLFLAG_SECURE
) && securelevel
> 0)))
1309 /* Most likely only root can write */
1310 if (!(oid
->oid_kind
& CTLFLAG_ANYBODY
) && req
->newptr
&& p
&&
1311 (error
= priv_check_cred(td
->td_ucred
,
1312 (oid
->oid_kind
& CTLFLAG_PRISON
) ? PRIV_SYSCTL_WRITEJAIL
:
1313 PRIV_SYSCTL_WRITE
, 0)))
1316 if (oid
->oid_handler
== NULL
)
1320 * Default oid locking is exclusive when modifying (newptr),
1321 * shared otherwise, unless overridden with a control flag.
1323 if ((oid
->oid_kind
& CTLFLAG_NOLOCK
) == 0) {
1324 lktype
= (req
->newptr
!= NULL
) ? LK_EXCLUSIVE
: LK_SHARED
;
1325 if (oid
->oid_kind
& CTLFLAG_SHLOCK
)
1327 if (oid
->oid_kind
& CTLFLAG_EXLOCK
)
1328 lktype
= LK_EXCLUSIVE
;
1330 lockmgr(&oid
->oid_lock
, lktype
);
1333 if (lockmgr(&oid
->oid_lock
, lktype
| LK_SLEEPFAIL
)) {
1334 kprintf("%s\n", oid
->oid_name
);
1335 lockmgr(&oid
->oid_lock
, lktype
);
1340 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
1341 error
= oid
->oid_handler(oid
, (int *)arg1
+ indx
, arg2
- indx
,
1344 error
= oid
->oid_handler(oid
, oid
->oid_arg1
, oid
->oid_arg2
,
1347 if ((oid
->oid_kind
& CTLFLAG_NOLOCK
) == 0)
1348 lockmgr(&oid
->oid_lock
, LK_RELEASE
);
1353 sys___sysctl(struct sysctl_args
*uap
)
1355 int error
, i
, name
[CTL_MAXNAME
];
1358 if (uap
->namelen
> CTL_MAXNAME
|| uap
->namelen
< 2)
1361 error
= copyin(uap
->name
, &name
, uap
->namelen
* sizeof(int));
1365 error
= userland_sysctl(name
, uap
->namelen
,
1366 uap
->old
, uap
->oldlenp
, 0,
1367 uap
->new, uap
->newlen
, &j
);
1368 if (error
&& error
!= ENOMEM
)
1371 i
= copyout(&j
, uap
->oldlenp
, sizeof(j
));
1379 * This is used from various compatibility syscalls too. That's why name
1380 * must be in kernel space.
1383 userland_sysctl(int *name
, u_int namelen
,
1384 void *old
, size_t *oldlenp
, int inkernel
,
1385 void *new, size_t newlen
, size_t *retval
)
1388 struct sysctl_req req
;
1390 bzero(&req
, sizeof req
);
1397 req
.oldlen
= *oldlenp
;
1399 error
= copyin(oldlenp
, &req
.oldlen
, sizeof(*oldlenp
));
1404 req
.validlen
= req
.oldlen
;
1407 * NOTE: User supplied buffers are not guaranteed to be good,
1408 * the sysctl copyins and copyouts can fail.
1414 req
.newlen
= newlen
;
1418 req
.oldfunc
= sysctl_old_user
;
1419 req
.newfunc
= sysctl_new_user
;
1421 req
.lock
= REQ_UNWIRED
;
1425 if (KTRPOINT(curthread
, KTR_SYSCTL
))
1426 ktrsysctl(name
, namelen
);
1433 error
= sysctl_root(0, name
, namelen
, &req
);
1435 if (error
!= EAGAIN
)
1441 if (req
.lock
== REQ_WIRED
&& req
.validlen
> 0)
1442 vsunlock(req
.oldptr
, req
.validlen
);
1444 if (error
&& error
!= ENOMEM
)
1448 if (req
.oldptr
&& req
.oldidx
> req
.validlen
)
1449 *retval
= req
.validlen
;
1451 *retval
= req
.oldidx
;
1457 sysctl_int_range(SYSCTL_HANDLER_ARGS
, int low
, int high
)
1461 value
= *(int *)arg1
;
1462 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
1463 if (error
|| !req
->newptr
)
1465 if (value
< low
|| value
> high
)
1467 *(int *)arg1
= value
;
1472 * Drain into a sysctl struct. The user buffer should be wired if a page
1473 * fault would cause issue.
1476 sbuf_sysctl_drain(void *arg
, const char *data
, int len
)
1478 struct sysctl_req
*req
= arg
;
1481 error
= SYSCTL_OUT(req
, data
, len
);
1482 KASSERT(error
>= 0, ("Got unexpected negative value %d", error
));
1483 return (error
== 0 ? len
: -error
);
1487 sbuf_new_for_sysctl(struct sbuf
*s
, char *buf
, int length
,
1488 struct sysctl_req
*req
)
1491 s
= sbuf_new(s
, buf
, length
, SBUF_FIXEDLEN
);
1492 sbuf_set_drain(s
, sbuf_sysctl_drain
, req
);
1497 * The exclusive sysctl lock only protects its topology, and is
1498 * very expensive, but allows us to use a pcpu shared lock for
1499 * critical path accesses.
1507 for (i
= 0; i
< ncpus
; ++i
) {
1508 gd
= globaldata_find(i
);
1509 lockmgr(&gd
->gd_sysctllock
, LK_EXCLUSIVE
);
1514 _sysctl_xunlock(void)
1519 for (i
= 0; i
< ncpus
; ++i
) {
1520 gd
= globaldata_find(i
);
1521 lockmgr(&gd
->gd_sysctllock
, LK_RELEASE
);