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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
40 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
41 * $DragonFly: src/sys/kern/kern_sysctl.c,v 1.30 2008/08/03 11:00:32 sephe Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
52 #include <sys/sysproto.h>
55 #include <vm/vm_extern.h>
57 static MALLOC_DEFINE(M_SYSCTL
, "sysctl", "sysctl internal magic");
58 static MALLOC_DEFINE(M_SYSCTLOID
, "sysctloid", "sysctl dynamic oids");
60 static struct lock sysctl_lkp
;
61 static struct lock sysctl_ctx_lkp
;
63 static void sysctl_lock(int type
);
64 static void sysctl_unlock(void);
65 static void sysctl_ctx_lock(int type
);
66 static void sysctl_ctx_unlock(void);
68 static int sysctl_root(SYSCTL_HANDLER_ARGS
);
69 static void sysctl_register_oid_int(struct sysctl_oid
*oipd
);
70 static void sysctl_unregister_oid_int(struct sysctl_oid
*oipd
);
71 static struct sysctl_ctx_entry
* sysctl_ctx_entry_find_int
72 (struct sysctl_ctx_list
*, struct sysctl_oid
*oidp
);
74 struct sysctl_oid_list sysctl__children
; /* root list */
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
)
98 sysctl_lock(LK_EXCLUSIVE
);
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 * First check if another oid with the same name already
112 * exists in the parent's list.
114 p
= sysctl_find_oidname(oidp
->oid_name
, parent
, 0);
116 if ((p
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
119 kprintf("can't re-use a leaf (%s)!\n", p
->oid_name
);
124 * If this oid has a number OID_AUTO, give it a number which
125 * is greater than any current oid. Make sure it is at least
126 * 256 to leave space for pre-assigned oid numbers.
128 if (oidp
->oid_number
== OID_AUTO
) {
129 int newoid
= 0x100; /* minimum AUTO oid */
132 * Adjust based on highest oid in parent list
134 SLIST_FOREACH(p
, parent
, oid_link
) {
135 if (newoid
<= p
->oid_number
)
136 newoid
= p
->oid_number
+ 1;
138 oidp
->oid_number
= newoid
;
142 * Insert the oid into the parent's list in order.
145 SLIST_FOREACH(p
, parent
, oid_link
) {
146 if (oidp
->oid_number
< p
->oid_number
)
151 SLIST_INSERT_AFTER(q
, oidp
, oid_link
);
153 SLIST_INSERT_HEAD(parent
, oidp
, oid_link
);
157 sysctl_unregister_oid(struct sysctl_oid
*oidp
)
159 sysctl_lock(LK_EXCLUSIVE
);
160 sysctl_unregister_oid_int(oidp
);
165 sysctl_unregister_oid_int(struct sysctl_oid
*oidp
)
167 struct sysctl_oid
*p
;
169 if (oidp
->oid_number
== OID_AUTO
)
170 panic("Trying to unregister OID_AUTO entry: %p", oidp
);
172 SLIST_FOREACH(p
, oidp
->oid_parent
, oid_link
) {
175 SLIST_REMOVE(oidp
->oid_parent
, oidp
, sysctl_oid
, oid_link
);
180 * This can happen when a module fails to register and is
181 * being unloaded afterwards. It should not be a panic()
184 kprintf("%s: failed to unregister sysctl\n", __func__
);
187 /* Initialize a new context to keep track of dynamically added sysctls. */
189 sysctl_ctx_init(struct sysctl_ctx_list
*c
)
197 /* Free the context, and destroy all dynamic oids registered in this context */
199 sysctl_ctx_free(struct sysctl_ctx_list
*clist
)
201 struct sysctl_ctx_entry
*e
, *e1
;
205 sysctl_ctx_lock(LK_EXCLUSIVE
);
207 * First perform a "dry run" to check if it's ok to remove oids.
209 * XXX This algorithm is a hack. But I don't know any
210 * XXX better solution for now...
212 TAILQ_FOREACH(e
, clist
, link
) {
213 error
= sysctl_remove_oid(e
->entry
, 0, 0);
218 * Restore deregistered entries, either from the end,
219 * or from the place where error occured.
220 * e contains the entry that was not unregistered
223 e1
= TAILQ_PREV(e
, sysctl_ctx_list
, link
);
225 e1
= TAILQ_LAST(clist
, sysctl_ctx_list
);
227 sysctl_register_oid(e1
->entry
);
228 e1
= TAILQ_PREV(e1
, sysctl_ctx_list
, link
);
234 /* Now really delete the entries */
235 e
= TAILQ_FIRST(clist
);
237 e1
= TAILQ_NEXT(e
, link
);
238 error
= sysctl_remove_oid(e
->entry
, 1, 0);
240 panic("sysctl_remove_oid: corrupt tree, entry: %s",
242 kfree(e
, M_SYSCTLOID
);
249 /* Add an entry to the context */
250 struct sysctl_ctx_entry
*
251 sysctl_ctx_entry_add(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
253 struct sysctl_ctx_entry
*e
;
255 if (clist
== NULL
|| oidp
== NULL
)
257 e
= kmalloc(sizeof(struct sysctl_ctx_entry
), M_SYSCTLOID
, M_WAITOK
);
259 sysctl_ctx_lock(LK_EXCLUSIVE
);
260 TAILQ_INSERT_HEAD(clist
, e
, link
);
265 /* Find an entry in the context */
266 struct sysctl_ctx_entry
*
267 sysctl_ctx_entry_find(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
269 struct sysctl_ctx_entry
*e
;
271 if (clist
== NULL
|| oidp
== NULL
)
274 sysctl_ctx_lock(LK_SHARED
);
275 e
= sysctl_ctx_entry_find_int(clist
, oidp
);
281 struct sysctl_ctx_entry
*
282 sysctl_ctx_entry_find_int(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
284 struct sysctl_ctx_entry
*e
;
286 KKASSERT(clist
!= NULL
&& oidp
!= NULL
);
288 for (e
= TAILQ_FIRST(clist
); e
!= NULL
; e
= TAILQ_NEXT(e
, link
)) {
297 * Delete an entry from the context.
298 * NOTE: this function doesn't free oidp! You have to remove it
299 * with sysctl_remove_oid().
302 sysctl_ctx_entry_del(struct sysctl_ctx_list
*clist
, struct sysctl_oid
*oidp
)
304 struct sysctl_ctx_entry
*e
;
306 if (clist
== NULL
|| oidp
== NULL
)
309 sysctl_ctx_lock(LK_EXCLUSIVE
);
310 e
= sysctl_ctx_entry_find_int(clist
, oidp
);
315 TAILQ_REMOVE(clist
, e
, link
);
316 kfree(e
, M_SYSCTLOID
);
323 * Remove dynamically created sysctl trees.
324 * oidp - top of the tree to be removed
325 * del - if 0 - just deregister, otherwise free up entries as well
326 * recurse - if != 0 traverse the subtree to be deleted
329 sysctl_remove_oid(struct sysctl_oid
*oidp
, int del
, int recurse
)
331 struct sysctl_oid
*p
;
336 if ((oidp
->oid_kind
& CTLFLAG_DYN
) == 0) {
337 kprintf("can't remove non-dynamic nodes!\n");
340 sysctl_lock(LK_EXCLUSIVE
| LK_CANRECURSE
);
342 * WARNING: normal method to do this should be through
343 * sysctl_ctx_free(). Use recursing as the last resort
344 * method to purge your sysctl tree of leftovers...
345 * However, if some other code still references these nodes,
348 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
349 if (oidp
->oid_refcnt
== 1) {
350 SLIST_FOREACH(p
, SYSCTL_CHILDREN(oidp
), oid_link
) {
355 error
= sysctl_remove_oid(p
, del
, recurse
);
362 kfree(SYSCTL_CHILDREN(oidp
), M_SYSCTLOID
);
365 if (oidp
->oid_refcnt
> 1 ) {
368 if (oidp
->oid_refcnt
== 0) {
369 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
370 oidp
->oid_refcnt
, oidp
->oid_name
);
374 sysctl_unregister_oid_int(oidp
);
377 kfree(__DECONST(char *,oidp
->oid_descr
),
379 kfree(__DECONST(char *, oidp
->oid_name
), M_SYSCTLOID
);
380 kfree(oidp
, M_SYSCTLOID
);
388 * Create new sysctls at run time.
389 * clist may point to a valid context initialized with sysctl_ctx_init().
392 sysctl_add_oid(struct sysctl_ctx_list
*clist
, struct sysctl_oid_list
*parent
,
393 int number
, const char *name
, int kind
, void *arg1
, int arg2
,
394 int (*handler
)(SYSCTL_HANDLER_ARGS
), const char *fmt
, const char *descr
)
396 struct sysctl_oid
*oidp
;
400 /* You have to hook up somewhere.. */
403 sysctl_lock(LK_EXCLUSIVE
);
404 /* Check if the node already exists, otherwise create it */
405 oidp
= sysctl_find_oidname(name
, parent
, 0);
407 if ((oidp
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
409 /* Update the context */
411 sysctl_ctx_entry_add(clist
, oidp
);
415 kprintf("can't re-use a leaf (%s)!\n", name
);
420 oidp
= kmalloc(sizeof(struct sysctl_oid
), M_SYSCTLOID
, M_WAITOK
| M_ZERO
);
421 oidp
->oid_parent
= parent
;
422 SLIST_NEXT(oidp
, oid_link
) = NULL
;
423 oidp
->oid_number
= number
;
424 oidp
->oid_refcnt
= 1;
426 newname
= kmalloc(len
+ 1, M_SYSCTLOID
, M_WAITOK
);
427 bcopy(name
, newname
, len
+ 1);
429 oidp
->oid_name
= newname
;
430 oidp
->oid_handler
= handler
;
431 oidp
->oid_kind
= CTLFLAG_DYN
| kind
;
432 if ((kind
& CTLTYPE
) == CTLTYPE_NODE
) {
433 struct sysctl_oid_list
*children
;
435 /* Allocate space for children */
436 children
= kmalloc(sizeof(*children
), M_SYSCTLOID
, M_WAITOK
);
437 SYSCTL_SET_CHILDREN(oidp
, children
);
438 SLIST_INIT(children
);
440 oidp
->oid_arg1
= arg1
;
441 oidp
->oid_arg2
= arg2
;
445 int len
= strlen(descr
) + 1;
446 oidp
->oid_descr
= kmalloc(len
, M_SYSCTLOID
, M_WAITOK
);
447 strcpy((char *)(uintptr_t)(const void *)oidp
->oid_descr
, descr
);
449 /* Update the context, if used */
451 sysctl_ctx_entry_add(clist
, oidp
);
452 /* Register this oid */
453 sysctl_register_oid_int(oidp
);
459 * Register the kernel's oids on startup.
461 SET_DECLARE(sysctl_set
, struct sysctl_oid
);
464 sysctl_register_all(void *arg
)
466 struct sysctl_oid
**oidp
;
468 lockinit(&sysctl_lkp
, "sysctl", 0, 0);
469 lockinit(&sysctl_ctx_lkp
, "sysctl ctx", 0, 0);
470 SET_FOREACH(oidp
, sysctl_set
)
471 sysctl_register_oid_int(*oidp
);
474 SYSINIT(sysctl
, SI_BOOT1_POST
, SI_ORDER_ANY
, sysctl_register_all
, 0);
479 * These functions implement a presently undocumented interface
480 * used by the sysctl program to walk the tree, and get the type
481 * so it can print the value.
482 * This interface is under work and consideration, and should probably
483 * be killed with a big axe by the first person who can find the time.
484 * (be aware though, that the proper interface isn't as obvious as it
485 * may seem, there are various conflicting requirements.
487 * {0,0} kprintf the entire MIB-tree.
488 * {0,1,...} return the name of the "..." OID.
489 * {0,2,...} return the next OID.
490 * {0,3} return the OID of the name in "new"
491 * {0,4,...} return the kind & format info for the "..." OID.
495 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list
*l
, int i
)
498 struct sysctl_oid
*oidp
;
500 sysctl_lock(LK_SHARED
);
501 SLIST_FOREACH(oidp
, l
, oid_link
) {
506 kprintf("%d %s ", oidp
->oid_number
, oidp
->oid_name
);
509 oidp
->oid_kind
& CTLFLAG_RD
? 'R':' ',
510 oidp
->oid_kind
& CTLFLAG_WR
? 'W':' ');
512 if (oidp
->oid_handler
)
513 kprintf(" *Handler");
515 switch (oidp
->oid_kind
& CTLTYPE
) {
518 if (!oidp
->oid_handler
) {
519 sysctl_sysctl_debug_dump_node(
520 oidp
->oid_arg1
, i
+2);
523 case CTLTYPE_INT
: kprintf(" Int\n"); break;
524 case CTLTYPE_STRING
: kprintf(" String\n"); break;
525 case CTLTYPE_QUAD
: kprintf(" Quad\n"); break;
526 case CTLTYPE_OPAQUE
: kprintf(" Opaque/struct\n"); break;
527 default: kprintf("\n");
535 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS
)
539 error
= priv_check(req
->td
, PRIV_ROOT
);
542 sysctl_sysctl_debug_dump_node(&sysctl__children
, 0);
546 SYSCTL_PROC(_sysctl
, 0, debug
, CTLTYPE_STRING
|CTLFLAG_RD
,
547 0, 0, sysctl_sysctl_debug
, "-", "");
550 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS
)
552 int *name
= (int *) arg1
;
553 u_int namelen
= arg2
;
555 struct sysctl_oid
*oid
;
556 struct sysctl_oid_list
*lsp
= &sysctl__children
, *lsp2
;
559 sysctl_lock(LK_SHARED
);
562 ksnprintf(buf
,sizeof(buf
),"%d",*name
);
564 error
= SYSCTL_OUT(req
, ".", 1);
566 error
= SYSCTL_OUT(req
, buf
, strlen(buf
));
576 SLIST_FOREACH(oid
, lsp
, oid_link
) {
577 if (oid
->oid_number
!= *name
)
581 error
= SYSCTL_OUT(req
, ".", 1);
583 error
= SYSCTL_OUT(req
, oid
->oid_name
,
584 strlen(oid
->oid_name
));
593 if ((oid
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
596 if (oid
->oid_handler
)
599 lsp2
= (struct sysctl_oid_list
*)oid
->oid_arg1
;
605 return (SYSCTL_OUT(req
, "", 1));
608 SYSCTL_NODE(_sysctl
, 1, name
, CTLFLAG_RD
, sysctl_sysctl_name
, "");
611 sysctl_sysctl_next_ls(struct sysctl_oid_list
*lsp
, int *name
, u_int namelen
,
612 int *next
, int *len
, int level
, struct sysctl_oid
**oidpp
)
614 struct sysctl_oid
*oidp
;
617 sysctl_lock(LK_SHARED
);
618 SLIST_FOREACH(oidp
, lsp
, oid_link
) {
619 *next
= oidp
->oid_number
;
623 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
) {
627 if (oidp
->oid_handler
) {
628 /* We really should call the handler here...*/
632 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
633 if (!sysctl_sysctl_next_ls(lsp
, 0, 0, next
+1,
634 len
, level
+1, oidpp
)) {
641 if (oidp
->oid_number
< *name
)
644 if (oidp
->oid_number
> *name
) {
645 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
) {
649 if (oidp
->oid_handler
) {
653 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
654 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1,
655 next
+1, len
, level
+1, oidpp
)) {
661 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
664 if (oidp
->oid_handler
)
667 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
668 if (!sysctl_sysctl_next_ls(lsp
, name
+1, namelen
-1, next
+1,
669 len
, level
+1, oidpp
)) {
684 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS
)
686 int *name
= (int *) arg1
;
687 u_int namelen
= arg2
;
689 struct sysctl_oid
*oid
;
690 struct sysctl_oid_list
*lsp
= &sysctl__children
;
691 int newoid
[CTL_MAXNAME
];
693 i
= sysctl_sysctl_next_ls(lsp
, name
, namelen
, newoid
, &j
, 1, &oid
);
696 error
= SYSCTL_OUT(req
, newoid
, j
* sizeof (int));
700 SYSCTL_NODE(_sysctl
, 2, next
, CTLFLAG_RD
, sysctl_sysctl_next
, "");
703 name2oid (char *name
, int *oid
, int *len
, struct sysctl_oid
**oidpp
)
706 struct sysctl_oid
*oidp
;
707 struct sysctl_oid_list
*lsp
= &sysctl__children
;
713 p
= name
+ strlen(name
) - 1 ;
719 for (p
= name
; *p
&& *p
!= '.'; p
++)
725 sysctl_lock(LK_SHARED
);
726 oidp
= SLIST_FIRST(lsp
);
728 while (oidp
&& *len
< CTL_MAXNAME
) {
729 if (strcmp(name
, oidp
->oid_name
)) {
730 oidp
= SLIST_NEXT(oidp
, oid_link
);
733 *oid
++ = oidp
->oid_number
;
743 if ((oidp
->oid_kind
& CTLTYPE
) != CTLTYPE_NODE
)
746 if (oidp
->oid_handler
)
749 lsp
= (struct sysctl_oid_list
*)oidp
->oid_arg1
;
750 oidp
= SLIST_FIRST(lsp
);
752 for (p
= name
; *p
&& *p
!= '.'; p
++)
763 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS
)
766 int error
, oid
[CTL_MAXNAME
], len
;
767 struct sysctl_oid
*op
= 0;
771 if (req
->newlen
>= MAXPATHLEN
) /* XXX arbitrary, undocumented */
772 return (ENAMETOOLONG
);
774 p
= kmalloc(req
->newlen
+1, M_SYSCTL
, M_WAITOK
);
776 error
= SYSCTL_IN(req
, p
, req
->newlen
);
782 p
[req
->newlen
] = '\0';
784 error
= name2oid(p
, oid
, &len
, &op
);
791 error
= SYSCTL_OUT(req
, oid
, len
* sizeof *oid
);
795 SYSCTL_PROC(_sysctl
, 3, name2oid
, CTLFLAG_RW
|CTLFLAG_ANYBODY
, 0, 0,
796 sysctl_sysctl_name2oid
, "I", "");
799 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS
)
801 struct sysctl_oid
*oid
;
804 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
810 error
= SYSCTL_OUT(req
, &oid
->oid_kind
, sizeof(oid
->oid_kind
));
813 error
= SYSCTL_OUT(req
, oid
->oid_fmt
, strlen(oid
->oid_fmt
) + 1);
818 SYSCTL_NODE(_sysctl
, 4, oidfmt
, CTLFLAG_RD
, sysctl_sysctl_oidfmt
, "");
821 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS
)
823 struct sysctl_oid
*oid
;
826 error
= sysctl_find_oid(arg1
, arg2
, &oid
, NULL
, req
);
832 error
= SYSCTL_OUT(req
, oid
->oid_descr
, strlen(oid
->oid_descr
) + 1);
836 SYSCTL_NODE(_sysctl
, 5, oiddescr
, CTLFLAG_RD
, sysctl_sysctl_oiddescr
, "");
839 * Default "handler" functions.
843 * Handle an int, signed or unsigned.
845 * a variable: point arg1 at it.
846 * a constant: pass it in arg2.
850 sysctl_handle_int(SYSCTL_HANDLER_ARGS
)
855 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
857 error
= SYSCTL_OUT(req
, &arg2
, sizeof(int));
859 if (error
|| !req
->newptr
)
865 error
= SYSCTL_IN(req
, arg1
, sizeof(int));
870 * Handle a long, signed or unsigned. arg1 points to it.
874 sysctl_handle_long(SYSCTL_HANDLER_ARGS
)
880 error
= SYSCTL_OUT(req
, arg1
, sizeof(long));
882 if (error
|| !req
->newptr
)
885 error
= SYSCTL_IN(req
, arg1
, sizeof(long));
890 * Handle a quad, signed or unsigned. arg1 points to it.
894 sysctl_handle_quad(SYSCTL_HANDLER_ARGS
)
900 error
= SYSCTL_OUT(req
, arg1
, sizeof(quad_t
));
902 if (error
|| !req
->newptr
)
905 error
= SYSCTL_IN(req
, arg1
, sizeof(quad_t
));
910 * Handle our generic '\0' terminated 'C' string.
912 * a variable string: point arg1 at it, arg2 is max length.
913 * a constant string: point arg1 at it, arg2 is zero.
917 sysctl_handle_string(SYSCTL_HANDLER_ARGS
)
921 error
= SYSCTL_OUT(req
, arg1
, strlen((char *)arg1
)+1);
923 if (error
|| !req
->newptr
)
926 if ((req
->newlen
- req
->newidx
) >= arg2
) {
929 arg2
= (req
->newlen
- req
->newidx
);
930 error
= SYSCTL_IN(req
, arg1
, arg2
);
931 ((char *)arg1
)[arg2
] = '\0';
938 * Handle any kind of opaque data.
939 * arg1 points to it, arg2 is the size.
943 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS
)
947 error
= SYSCTL_OUT(req
, arg1
, arg2
);
949 if (error
|| !req
->newptr
)
952 error
= SYSCTL_IN(req
, arg1
, arg2
);
958 * Transfer functions to/from kernel space.
959 * XXX: rather untested at this point
962 sysctl_old_kernel(struct sysctl_req
*req
, const void *p
, size_t l
)
968 if (i
> req
->oldlen
- req
->oldidx
)
969 i
= req
->oldlen
- req
->oldidx
;
971 bcopy(p
, (char *)req
->oldptr
+ req
->oldidx
, i
);
974 if (req
->oldptr
&& i
!= l
)
980 sysctl_new_kernel(struct sysctl_req
*req
, void *p
, size_t l
)
985 if (req
->newlen
- req
->newidx
< l
)
987 bcopy((char *)req
->newptr
+ req
->newidx
, p
, l
);
993 kernel_sysctl(int *name
, u_int namelen
, void *old
, size_t *oldlenp
, void *new, size_t newlen
, size_t *retval
)
996 struct sysctl_req req
;
998 bzero(&req
, sizeof req
);
1003 req
.oldlen
= *oldlenp
;
1011 req
.newlen
= newlen
;
1015 req
.oldfunc
= sysctl_old_kernel
;
1016 req
.newfunc
= sysctl_new_kernel
;
1019 sysctl_lock(LK_SHARED
);
1021 error
= sysctl_root(0, name
, namelen
, &req
);
1024 vsunlock(req
.oldptr
, req
.oldlen
);
1028 if (error
&& error
!= ENOMEM
)
1032 if (req
.oldptr
&& req
.oldidx
> req
.oldlen
)
1033 *retval
= req
.oldlen
;
1035 *retval
= req
.oldidx
;
1041 kernel_sysctlbyname(char *name
, void *old
, size_t *oldlenp
,
1042 void *new, size_t newlen
, size_t *retval
)
1044 int oid
[CTL_MAXNAME
];
1045 size_t oidlen
, plen
;
1048 oid
[0] = 0; /* sysctl internal magic */
1049 oid
[1] = 3; /* name2oid */
1050 oidlen
= sizeof(oid
);
1052 error
= kernel_sysctl(oid
, 2, oid
, &oidlen
, (void *)name
,
1053 strlen(name
), &plen
);
1057 error
= kernel_sysctl(oid
, plen
/ sizeof(int), old
, oldlenp
,
1058 new, newlen
, retval
);
1063 * Transfer function to/from user space.
1066 sysctl_old_user(struct sysctl_req
*req
, const void *p
, size_t l
)
1071 if (req
->lock
== 1 && req
->oldptr
) {
1072 vslock(req
->oldptr
, req
->oldlen
);
1077 if (i
> req
->oldlen
- req
->oldidx
)
1078 i
= req
->oldlen
- req
->oldidx
;
1080 error
= copyout(p
, (char *)req
->oldptr
+ req
->oldidx
,
1086 if (req
->oldptr
&& i
< l
)
1092 sysctl_new_user(struct sysctl_req
*req
, void *p
, size_t l
)
1098 if (req
->newlen
- req
->newidx
< l
)
1100 error
= copyin((char *)req
->newptr
+ req
->newidx
, p
, l
);
1106 sysctl_find_oid(int *name
, u_int namelen
, struct sysctl_oid
**noid
,
1107 int *nindx
, struct sysctl_req
*req
)
1109 struct sysctl_oid
*oid
;
1112 sysctl_lock(LK_SHARED
);
1113 oid
= SLIST_FIRST(&sysctl__children
);
1115 while (oid
&& indx
< CTL_MAXNAME
) {
1116 if (oid
->oid_number
== name
[indx
]) {
1118 if (oid
->oid_kind
& CTLFLAG_NOLOCK
)
1120 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1121 if (oid
->oid_handler
!= NULL
||
1130 (struct sysctl_oid_list
*)oid
->oid_arg1
);
1131 } else if (indx
== namelen
) {
1142 oid
= SLIST_NEXT(oid
, oid_link
);
1150 * Traverse our tree, and find the right node, execute whatever it points
1151 * to, and return the resulting error code.
1155 sysctl_root(SYSCTL_HANDLER_ARGS
)
1157 struct thread
*td
= req
->td
;
1158 struct proc
*p
= td
? td
->td_proc
: NULL
;
1159 struct sysctl_oid
*oid
;
1162 error
= sysctl_find_oid(arg1
, arg2
, &oid
, &indx
, req
);
1166 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
) {
1168 * You can't call a sysctl when it's a node, but has
1169 * no handler. Inform the user that it's a node.
1170 * The indx may or may not be the same as namelen.
1172 if (oid
->oid_handler
== NULL
)
1176 /* If writing isn't allowed */
1177 if (req
->newptr
&& (!(oid
->oid_kind
& CTLFLAG_WR
) ||
1178 ((oid
->oid_kind
& CTLFLAG_SECURE
) && securelevel
> 0)))
1181 /* Most likely only root can write */
1182 if (!(oid
->oid_kind
& CTLFLAG_ANYBODY
) && req
->newptr
&& p
&&
1183 (error
= priv_check_cred(p
->p_ucred
, PRIV_ROOT
,
1184 (oid
->oid_kind
& CTLFLAG_PRISON
) ? PRISON_ROOT
: 0)))
1187 if (!oid
->oid_handler
)
1190 if ((oid
->oid_kind
& CTLTYPE
) == CTLTYPE_NODE
)
1191 error
= oid
->oid_handler(oid
, (int *)arg1
+ indx
, arg2
- indx
,
1194 error
= oid
->oid_handler(oid
, oid
->oid_arg1
, oid
->oid_arg2
,
1200 sys___sysctl(struct sysctl_args
*uap
)
1202 int error
, i
, name
[CTL_MAXNAME
];
1205 if (uap
->namelen
> CTL_MAXNAME
|| uap
->namelen
< 2)
1208 error
= copyin(uap
->name
, &name
, uap
->namelen
* sizeof(int));
1212 error
= userland_sysctl(name
, uap
->namelen
,
1213 uap
->old
, uap
->oldlenp
, 0,
1214 uap
->new, uap
->newlen
, &j
);
1215 if (error
&& error
!= ENOMEM
)
1218 i
= copyout(&j
, uap
->oldlenp
, sizeof(j
));
1226 * This is used from various compatibility syscalls too. That's why name
1227 * must be in kernel space.
1230 userland_sysctl(int *name
, u_int namelen
, void *old
, size_t *oldlenp
, int inkernel
, void *new, size_t newlen
, size_t *retval
)
1233 struct sysctl_req req
, req2
;
1235 bzero(&req
, sizeof req
);
1239 req
.oldlen
= *oldlenp
;
1241 error
= copyin(oldlenp
, &req
.oldlen
, sizeof(*oldlenp
));
1248 if (!useracc(old
, req
.oldlen
, VM_PROT_WRITE
))
1254 if (!useracc(new, newlen
, VM_PROT_READ
))
1256 req
.newlen
= newlen
;
1260 req
.oldfunc
= sysctl_old_user
;
1261 req
.newfunc
= sysctl_new_user
;
1265 sysctl_lock(LK_SHARED
);
1269 error
= sysctl_root(0, name
, namelen
, &req2
);
1270 } while (error
== EAGAIN
);
1274 vsunlock(req
.oldptr
, req
.oldlen
);
1278 if (error
&& error
!= ENOMEM
)
1282 if (req
.oldptr
&& req
.oldidx
> req
.oldlen
)
1283 *retval
= req
.oldlen
;
1285 *retval
= req
.oldidx
;
1291 sysctl_lock(int flag
)
1293 lockmgr(&sysctl_lkp
, flag
);
1299 lockmgr(&sysctl_lkp
, LK_RELEASE
);
1303 sysctl_ctx_lock(int flag
)
1305 lockmgr(&sysctl_ctx_lkp
, flag
);
1309 sysctl_ctx_unlock(void)
1311 lockmgr(&sysctl_ctx_lkp
, LK_RELEASE
);
1315 sysctl_int_range(SYSCTL_HANDLER_ARGS
, int low
, int high
)
1319 value
= *(int *)arg1
;
1320 error
= sysctl_handle_int(oidp
, &value
, 0, req
);
1321 if (error
|| !req
->newptr
)
1323 if (value
< low
|| value
> high
)
1325 *(int *)arg1
= value
;