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 <sys/mplock2.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 static struct lock sysctl_lkp;
64 static struct lock sysctl_ctx_lkp;
66 static void sysctl_lock(int type);
67 static void sysctl_unlock(void);
68 static void sysctl_ctx_lock(int type);
69 static void sysctl_ctx_unlock(void);
71 static int sysctl_root(SYSCTL_HANDLER_ARGS);
72 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
73 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
74 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
75 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
77 struct sysctl_oid_list sysctl__children; /* root list */
79 static struct sysctl_oid *
80 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
82 struct sysctl_oid *oidp;
84 SLIST_FOREACH(oidp, list, oid_link) {
85 if (strcmp(oidp->oid_name, name) == 0) {
93 * Initialization of the MIB tree.
95 * Order by number in each list.
99 sysctl_register_oid(struct sysctl_oid *oidp)
101 sysctl_lock(LK_EXCLUSIVE);
102 sysctl_register_oid_int(oidp);
107 sysctl_register_oid_int(struct sysctl_oid *oidp)
109 struct sysctl_oid_list *parent = oidp->oid_parent;
110 struct sysctl_oid *p;
111 struct sysctl_oid *q;
114 * First check if another oid with the same name already
115 * exists in the parent's list.
117 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
119 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
122 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
127 * If this oid has a number OID_AUTO, give it a number which
128 * is greater than any current oid. Make sure it is at least
129 * 256 to leave space for pre-assigned oid numbers.
131 if (oidp->oid_number == OID_AUTO) {
132 int newoid = 0x100; /* minimum AUTO oid */
135 * Adjust based on highest oid in parent list
137 SLIST_FOREACH(p, parent, oid_link) {
138 if (newoid <= p->oid_number)
139 newoid = p->oid_number + 1;
141 oidp->oid_number = newoid;
145 * Insert the oid into the parent's list in order.
148 SLIST_FOREACH(p, parent, oid_link) {
149 if (oidp->oid_number < p->oid_number)
154 SLIST_INSERT_AFTER(q, oidp, oid_link);
156 SLIST_INSERT_HEAD(parent, oidp, oid_link);
160 sysctl_unregister_oid(struct sysctl_oid *oidp)
162 sysctl_lock(LK_EXCLUSIVE);
163 sysctl_unregister_oid_int(oidp);
168 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
170 struct sysctl_oid *p;
172 if (oidp->oid_number == OID_AUTO)
173 panic("Trying to unregister OID_AUTO entry: %p", oidp);
175 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
178 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
183 * This can happen when a module fails to register and is
184 * being unloaded afterwards. It should not be a panic()
187 kprintf("%s: failed to unregister sysctl\n", __func__);
190 /* Initialize a new context to keep track of dynamically added sysctls. */
192 sysctl_ctx_init(struct sysctl_ctx_list *c)
200 /* Free the context, and destroy all dynamic oids registered in this context */
202 sysctl_ctx_free(struct sysctl_ctx_list *clist)
204 struct sysctl_ctx_entry *e, *e1;
208 sysctl_ctx_lock(LK_EXCLUSIVE);
210 * First perform a "dry run" to check if it's ok to remove oids.
212 * XXX This algorithm is a hack. But I don't know any
213 * XXX better solution for now...
215 TAILQ_FOREACH(e, clist, link) {
216 error = sysctl_remove_oid(e->entry, 0, 0);
221 * Restore deregistered entries, either from the end,
222 * or from the place where error occured.
223 * e contains the entry that was not unregistered
226 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
228 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
230 sysctl_register_oid(e1->entry);
231 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
237 /* Now really delete the entries */
238 e = TAILQ_FIRST(clist);
240 e1 = TAILQ_NEXT(e, link);
241 error = sysctl_remove_oid(e->entry, 1, 0);
243 panic("sysctl_remove_oid: corrupt tree, entry: %s",
245 kfree(e, M_SYSCTLOID);
252 /* Add an entry to the context */
253 struct sysctl_ctx_entry *
254 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
256 struct sysctl_ctx_entry *e;
258 if (clist == NULL || oidp == NULL)
260 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
262 sysctl_ctx_lock(LK_EXCLUSIVE);
263 TAILQ_INSERT_HEAD(clist, e, link);
268 /* Find an entry in the context */
269 struct sysctl_ctx_entry *
270 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
272 struct sysctl_ctx_entry *e;
274 if (clist == NULL || oidp == NULL)
277 sysctl_ctx_lock(LK_SHARED);
278 e = sysctl_ctx_entry_find_int(clist, oidp);
284 struct sysctl_ctx_entry *
285 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
287 struct sysctl_ctx_entry *e;
289 KKASSERT(clist != NULL && oidp != NULL);
291 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
300 * Delete an entry from the context.
301 * NOTE: this function doesn't free oidp! You have to remove it
302 * with sysctl_remove_oid().
305 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
307 struct sysctl_ctx_entry *e;
309 if (clist == NULL || oidp == NULL)
312 sysctl_ctx_lock(LK_EXCLUSIVE);
313 e = sysctl_ctx_entry_find_int(clist, oidp);
318 TAILQ_REMOVE(clist, e, link);
319 kfree(e, M_SYSCTLOID);
326 * Remove dynamically created sysctl trees.
327 * oidp - top of the tree to be removed
328 * del - if 0 - just deregister, otherwise free up entries as well
329 * recurse - if != 0 traverse the subtree to be deleted
332 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
334 struct sysctl_oid *p;
339 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
340 kprintf("can't remove non-dynamic nodes!\n");
343 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
345 * WARNING: normal method to do this should be through
346 * sysctl_ctx_free(). Use recursing as the last resort
347 * method to purge your sysctl tree of leftovers...
348 * However, if some other code still references these nodes,
351 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
352 if (oidp->oid_refcnt == 1) {
353 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
358 error = sysctl_remove_oid(p, del, recurse);
365 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
368 if (oidp->oid_refcnt > 1 ) {
371 if (oidp->oid_refcnt == 0) {
372 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
373 oidp->oid_refcnt, oidp->oid_name);
377 sysctl_unregister_oid_int(oidp);
380 kfree(__DECONST(char *,oidp->oid_descr),
382 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
383 kfree(oidp, M_SYSCTLOID);
391 * Create new sysctls at run time.
392 * clist may point to a valid context initialized with sysctl_ctx_init().
395 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
396 int number, const char *name, int kind, void *arg1, int arg2,
397 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
399 struct sysctl_oid *oidp;
403 /* You have to hook up somewhere.. */
406 sysctl_lock(LK_EXCLUSIVE);
407 /* Check if the node already exists, otherwise create it */
408 oidp = sysctl_find_oidname(name, parent, 0);
410 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
412 /* Update the context */
414 sysctl_ctx_entry_add(clist, oidp);
418 kprintf("can't re-use a leaf (%s)!\n", name);
423 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
424 oidp->oid_parent = parent;
425 SLIST_NEXT(oidp, oid_link) = NULL;
426 oidp->oid_number = number;
427 oidp->oid_refcnt = 1;
429 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
430 bcopy(name, newname, len + 1);
432 oidp->oid_name = newname;
433 oidp->oid_handler = handler;
434 oidp->oid_kind = CTLFLAG_DYN | kind;
435 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
436 struct sysctl_oid_list *children;
438 /* Allocate space for children */
439 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
440 SYSCTL_SET_CHILDREN(oidp, children);
441 SLIST_INIT(children);
443 oidp->oid_arg1 = arg1;
444 oidp->oid_arg2 = arg2;
448 int len = strlen(descr) + 1;
449 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
450 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
452 /* Update the context, if used */
454 sysctl_ctx_entry_add(clist, oidp);
455 /* Register this oid */
456 sysctl_register_oid_int(oidp);
462 * Register the kernel's oids on startup.
464 SET_DECLARE(sysctl_set, struct sysctl_oid);
467 sysctl_register_all(void *arg)
469 struct sysctl_oid **oidp;
471 lockinit(&sysctl_lkp, "sysctl", 0, 0);
472 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0);
473 SET_FOREACH(oidp, sysctl_set)
474 sysctl_register_oid_int(*oidp);
477 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
482 * These functions implement a presently undocumented interface
483 * used by the sysctl program to walk the tree, and get the type
484 * so it can print the value.
485 * This interface is under work and consideration, and should probably
486 * be killed with a big axe by the first person who can find the time.
487 * (be aware though, that the proper interface isn't as obvious as it
488 * may seem, there are various conflicting requirements.
490 * {0,0} kprintf the entire MIB-tree.
491 * {0,1,...} return the name of the "..." OID.
492 * {0,2,...} return the next OID.
493 * {0,3} return the OID of the name in "new"
494 * {0,4,...} return the kind & format info for the "..." OID.
498 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
501 struct sysctl_oid *oidp;
503 sysctl_lock(LK_SHARED);
504 SLIST_FOREACH(oidp, l, oid_link) {
509 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
512 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
513 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
515 if (oidp->oid_handler)
516 kprintf(" *Handler");
518 switch (oidp->oid_kind & CTLTYPE) {
521 if (!oidp->oid_handler) {
522 sysctl_sysctl_debug_dump_node(
523 oidp->oid_arg1, i+2);
526 case CTLTYPE_INT: kprintf(" Int\n"); break;
527 case CTLTYPE_STRING: kprintf(" String\n"); break;
528 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
529 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
530 default: kprintf("\n");
538 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
542 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
545 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
549 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
550 0, 0, sysctl_sysctl_debug, "-", "");
553 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
555 int *name = (int *) arg1;
556 u_int namelen = arg2;
558 struct sysctl_oid *oid;
559 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
562 sysctl_lock(LK_SHARED);
565 ksnprintf(buf,sizeof(buf),"%d",*name);
567 error = SYSCTL_OUT(req, ".", 1);
569 error = SYSCTL_OUT(req, buf, strlen(buf));
579 SLIST_FOREACH(oid, lsp, oid_link) {
580 if (oid->oid_number != *name)
584 error = SYSCTL_OUT(req, ".", 1);
586 error = SYSCTL_OUT(req, oid->oid_name,
587 strlen(oid->oid_name));
596 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
599 if (oid->oid_handler)
602 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
608 return (SYSCTL_OUT(req, "", 1));
611 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
614 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
615 int *next, int *len, int level, struct sysctl_oid **oidpp)
617 struct sysctl_oid *oidp;
620 sysctl_lock(LK_SHARED);
621 SLIST_FOREACH(oidp, lsp, oid_link) {
622 *next = oidp->oid_number;
626 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
630 if (oidp->oid_handler) {
631 /* We really should call the handler here...*/
635 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
636 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
637 len, level+1, oidpp)) {
644 if (oidp->oid_number < *name)
647 if (oidp->oid_number > *name) {
648 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
652 if (oidp->oid_handler) {
656 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
657 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
658 next+1, len, level+1, oidpp)) {
664 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
667 if (oidp->oid_handler)
670 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
671 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
672 len, level+1, oidpp)) {
687 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
689 int *name = (int *) arg1;
690 u_int namelen = arg2;
692 struct sysctl_oid *oid;
693 struct sysctl_oid_list *lsp = &sysctl__children;
694 int newoid[CTL_MAXNAME];
696 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
699 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
703 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
706 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
709 struct sysctl_oid *oidp;
710 struct sysctl_oid_list *lsp = &sysctl__children;
716 p = name + strlen(name) - 1 ;
722 for (p = name; *p && *p != '.'; p++)
728 sysctl_lock(LK_SHARED);
729 oidp = SLIST_FIRST(lsp);
731 while (oidp && *len < CTL_MAXNAME) {
732 if (strcmp(name, oidp->oid_name)) {
733 oidp = SLIST_NEXT(oidp, oid_link);
736 *oid++ = oidp->oid_number;
746 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
749 if (oidp->oid_handler)
752 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
753 oidp = SLIST_FIRST(lsp);
755 for (p = name; *p && *p != '.'; p++)
766 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
769 int error, oid[CTL_MAXNAME], len;
770 struct sysctl_oid *op = 0;
774 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
775 return (ENAMETOOLONG);
777 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
779 error = SYSCTL_IN(req, p, req->newlen);
785 p [req->newlen] = '\0';
787 error = name2oid(p, oid, &len, &op);
794 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
798 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
799 sysctl_sysctl_name2oid, "I", "");
802 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
804 struct sysctl_oid *oid;
807 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
813 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
816 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
821 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
824 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
826 struct sysctl_oid *oid;
829 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
835 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
839 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
842 * Default "handler" functions.
846 * Handle an int, signed or unsigned.
848 * a variable: point arg1 at it.
849 * a constant: pass it in arg2.
853 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
858 error = SYSCTL_OUT(req, arg1, sizeof(int));
860 error = SYSCTL_OUT(req, &arg2, sizeof(int));
862 if (error || !req->newptr)
868 error = SYSCTL_IN(req, arg1, sizeof(int));
873 * Handle a long, signed or unsigned. arg1 points to it.
877 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
883 error = SYSCTL_OUT(req, arg1, sizeof(long));
885 if (error || !req->newptr)
888 error = SYSCTL_IN(req, arg1, sizeof(long));
893 * Handle a quad, signed or unsigned. arg1 points to it.
897 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
903 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
905 if (error || !req->newptr)
908 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
913 * Handle our generic '\0' terminated 'C' string.
915 * a variable string: point arg1 at it, arg2 is max length.
916 * a constant string: point arg1 at it, arg2 is zero.
920 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
924 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
926 if (error || !req->newptr)
929 if ((req->newlen - req->newidx) >= arg2) {
932 arg2 = (req->newlen - req->newidx);
933 error = SYSCTL_IN(req, arg1, arg2);
934 ((char *)arg1)[arg2] = '\0';
941 * Handle any kind of opaque data.
942 * arg1 points to it, arg2 is the size.
946 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
950 error = SYSCTL_OUT(req, arg1, arg2);
952 if (error || !req->newptr)
955 error = SYSCTL_IN(req, arg1, arg2);
961 * Transfer functions to/from kernel space.
962 * XXX: rather untested at this point
965 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
971 if (i > req->oldlen - req->oldidx)
972 i = req->oldlen - req->oldidx;
974 bcopy(p, (char *)req->oldptr + req->oldidx, i);
977 if (req->oldptr && i != l)
983 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
988 if (req->newlen - req->newidx < l)
990 bcopy((char *)req->newptr + req->newidx, p, l);
996 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
999 struct sysctl_req req;
1001 bzero(&req, sizeof req);
1006 req.oldlen = *oldlenp;
1014 req.newlen = newlen;
1018 req.oldfunc = sysctl_old_kernel;
1019 req.newfunc = sysctl_new_kernel;
1024 sysctl_lock(LK_SHARED);
1026 error = sysctl_root(0, name, namelen, &req);
1030 vsunlock(req.oldptr, req.oldlen);
1035 if (error && error != ENOMEM)
1039 if (req.oldptr && req.oldidx > req.oldlen)
1040 *retval = req.oldlen;
1042 *retval = req.oldidx;
1048 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1049 void *new, size_t newlen, size_t *retval)
1051 int oid[CTL_MAXNAME];
1052 size_t oidlen, plen;
1055 oid[0] = 0; /* sysctl internal magic */
1056 oid[1] = 3; /* name2oid */
1057 oidlen = sizeof(oid);
1059 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
1060 strlen(name), &plen);
1064 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1065 new, newlen, retval);
1070 * Transfer function to/from user space.
1073 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1079 if (req->lock == 1 && req->oldptr) {
1080 vslock(req->oldptr, req->oldlen);
1086 if (i > req->oldlen - req->oldidx)
1087 i = req->oldlen - req->oldidx;
1089 error = copyout(p, (char *)req->oldptr + req->oldidx,
1095 if (req->oldptr && i < l)
1101 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1107 if (req->newlen - req->newidx < l)
1109 error = copyin((char *)req->newptr + req->newidx, p, l);
1115 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1116 int *nindx, struct sysctl_req *req)
1118 struct sysctl_oid *oid;
1121 sysctl_lock(LK_SHARED);
1122 oid = SLIST_FIRST(&sysctl__children);
1124 while (oid && indx < CTL_MAXNAME) {
1125 if (oid->oid_number == name[indx]) {
1127 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1128 if (oid->oid_handler != NULL ||
1137 (struct sysctl_oid_list *)oid->oid_arg1);
1138 } else if (indx == namelen) {
1149 oid = SLIST_NEXT(oid, oid_link);
1157 * Traverse our tree, and find the right node, execute whatever it points
1158 * to, and return the resulting error code.
1162 sysctl_root(SYSCTL_HANDLER_ARGS)
1164 struct thread *td = req->td;
1165 struct proc *p = td ? td->td_proc : NULL;
1166 struct sysctl_oid *oid;
1169 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1173 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1175 * You can't call a sysctl when it's a node, but has
1176 * no handler. Inform the user that it's a node.
1177 * The indx may or may not be the same as namelen.
1179 if (oid->oid_handler == NULL)
1183 /* If writing isn't allowed */
1184 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1185 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1188 /* Most likely only root can write */
1189 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1190 (error = priv_check_cred(td->td_ucred,
1191 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1192 PRIV_SYSCTL_WRITE, 0)))
1195 if (!oid->oid_handler)
1198 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1199 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1202 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1211 sys___sysctl(struct sysctl_args *uap)
1213 int error, i, name[CTL_MAXNAME];
1216 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1219 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1224 error = userland_sysctl(name, uap->namelen,
1225 uap->old, uap->oldlenp, 0,
1226 uap->new, uap->newlen, &j);
1228 if (error && error != ENOMEM)
1231 i = copyout(&j, uap->oldlenp, sizeof(j));
1239 * This is used from various compatibility syscalls too. That's why name
1240 * must be in kernel space.
1243 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1246 struct sysctl_req req, req2;
1248 bzero(&req, sizeof req);
1252 req.oldlen = *oldlenp;
1254 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1261 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1267 if (!useracc(new, newlen, VM_PROT_READ))
1269 req.newlen = newlen;
1273 req.oldfunc = sysctl_old_user;
1274 req.newfunc = sysctl_new_user;
1280 sysctl_lock(LK_SHARED);
1284 error = sysctl_root(0, name, namelen, &req2);
1285 } while (error == EAGAIN);
1290 vsunlock(req.oldptr, req.oldlen);
1295 if (error && error != ENOMEM)
1299 if (req.oldptr && req.oldidx > req.oldlen)
1300 *retval = req.oldlen;
1302 *retval = req.oldidx;
1308 sysctl_lock(int flag)
1310 lockmgr(&sysctl_lkp, flag);
1316 lockmgr(&sysctl_lkp, LK_RELEASE);
1320 sysctl_ctx_lock(int flag)
1322 lockmgr(&sysctl_ctx_lkp, flag);
1326 sysctl_ctx_unlock(void)
1328 lockmgr(&sysctl_ctx_lkp, LK_RELEASE);
1332 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1336 value = *(int *)arg1;
1337 error = sysctl_handle_int(oidp, &value, 0, req);
1338 if (error || !req->newptr)
1340 if (value < low || value > high)
1342 *(int *)arg1 = value;