2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9 * project, to make these variables more userfriendly.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
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>
50 #include <sys/mplock2.h>
53 #include <vm/vm_extern.h>
55 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
56 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
58 static struct lock sysctl_lkp;
59 static struct lock sysctl_ctx_lkp;
61 static void sysctl_lock(int type);
62 static void sysctl_unlock(void);
63 static void sysctl_ctx_lock(int type);
64 static void sysctl_ctx_unlock(void);
66 static int sysctl_root(SYSCTL_HANDLER_ARGS);
67 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
68 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
69 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
70 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
72 struct sysctl_oid_list sysctl__children; /* root list */
74 static struct sysctl_oid *
75 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
77 struct sysctl_oid *oidp;
79 SLIST_FOREACH(oidp, list, oid_link) {
80 if (strcmp(oidp->oid_name, name) == 0) {
88 * Initialization of the MIB tree.
90 * Order by number in each list.
94 sysctl_register_oid(struct sysctl_oid *oidp)
96 sysctl_lock(LK_EXCLUSIVE);
97 sysctl_register_oid_int(oidp);
102 sysctl_register_oid_int(struct sysctl_oid *oidp)
104 struct sysctl_oid_list *parent = oidp->oid_parent;
105 struct sysctl_oid *p;
106 struct sysctl_oid *q;
109 * First check if another oid with the same name already
110 * exists in the parent's list.
112 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
114 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
117 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
122 * If this oid has a number OID_AUTO, give it a number which
123 * is greater than any current oid. Make sure it is at least
124 * 256 to leave space for pre-assigned oid numbers.
126 if (oidp->oid_number == OID_AUTO) {
127 int newoid = 0x100; /* minimum AUTO oid */
130 * Adjust based on highest oid in parent list
132 SLIST_FOREACH(p, parent, oid_link) {
133 if (newoid <= p->oid_number)
134 newoid = p->oid_number + 1;
136 oidp->oid_number = newoid;
140 * Insert the oid into the parent's list in order.
143 SLIST_FOREACH(p, parent, oid_link) {
144 if (oidp->oid_number < p->oid_number)
149 SLIST_INSERT_AFTER(q, oidp, oid_link);
151 SLIST_INSERT_HEAD(parent, oidp, oid_link);
155 sysctl_unregister_oid(struct sysctl_oid *oidp)
157 sysctl_lock(LK_EXCLUSIVE);
158 sysctl_unregister_oid_int(oidp);
163 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
165 struct sysctl_oid *p;
167 if (oidp->oid_number == OID_AUTO)
168 panic("Trying to unregister OID_AUTO entry: %p", oidp);
170 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
173 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
178 * This can happen when a module fails to register and is
179 * being unloaded afterwards. It should not be a panic()
182 kprintf("%s: failed to unregister sysctl\n", __func__);
185 /* Initialize a new context to keep track of dynamically added sysctls. */
187 sysctl_ctx_init(struct sysctl_ctx_list *c)
195 /* Free the context, and destroy all dynamic oids registered in this context */
197 sysctl_ctx_free(struct sysctl_ctx_list *clist)
199 struct sysctl_ctx_entry *e, *e1;
203 sysctl_ctx_lock(LK_EXCLUSIVE);
205 * First perform a "dry run" to check if it's ok to remove oids.
207 * XXX This algorithm is a hack. But I don't know any
208 * XXX better solution for now...
210 TAILQ_FOREACH(e, clist, link) {
211 error = sysctl_remove_oid(e->entry, 0, 0);
216 * Restore deregistered entries, either from the end,
217 * or from the place where error occured.
218 * e contains the entry that was not unregistered
221 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
223 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
225 sysctl_register_oid(e1->entry);
226 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
232 /* Now really delete the entries */
233 e = TAILQ_FIRST(clist);
235 e1 = TAILQ_NEXT(e, link);
236 error = sysctl_remove_oid(e->entry, 1, 0);
238 panic("sysctl_remove_oid: corrupt tree, entry: %s",
240 kfree(e, M_SYSCTLOID);
247 /* Add an entry to the context */
248 struct sysctl_ctx_entry *
249 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
251 struct sysctl_ctx_entry *e;
253 if (clist == NULL || oidp == NULL)
255 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
257 sysctl_ctx_lock(LK_EXCLUSIVE);
258 TAILQ_INSERT_HEAD(clist, e, link);
263 /* Find an entry in the context */
264 struct sysctl_ctx_entry *
265 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
267 struct sysctl_ctx_entry *e;
269 if (clist == NULL || oidp == NULL)
272 sysctl_ctx_lock(LK_SHARED);
273 e = sysctl_ctx_entry_find_int(clist, oidp);
279 struct sysctl_ctx_entry *
280 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
282 struct sysctl_ctx_entry *e;
284 KKASSERT(clist != NULL && oidp != NULL);
286 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, 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 sysctl_ctx_lock(LK_EXCLUSIVE);
308 e = sysctl_ctx_entry_find_int(clist, oidp);
313 TAILQ_REMOVE(clist, e, link);
314 kfree(e, M_SYSCTLOID);
321 * Remove dynamically created sysctl trees.
322 * oidp - top of the tree to be removed
323 * del - if 0 - just deregister, otherwise free up entries as well
324 * recurse - if != 0 traverse the subtree to be deleted
327 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
329 struct sysctl_oid *p;
334 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
335 kprintf("can't remove non-dynamic nodes!\n");
338 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
340 * WARNING: normal method to do this should be through
341 * sysctl_ctx_free(). Use recursing as the last resort
342 * method to purge your sysctl tree of leftovers...
343 * However, if some other code still references these nodes,
346 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
347 if (oidp->oid_refcnt == 1) {
348 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
353 error = sysctl_remove_oid(p, del, recurse);
360 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
363 if (oidp->oid_refcnt > 1 ) {
366 if (oidp->oid_refcnt == 0) {
367 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
368 oidp->oid_refcnt, oidp->oid_name);
372 sysctl_unregister_oid_int(oidp);
375 kfree(__DECONST(char *,oidp->oid_descr),
377 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
378 kfree(oidp, M_SYSCTLOID);
386 * Create new sysctls at run time.
387 * clist may point to a valid context initialized with sysctl_ctx_init().
390 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
391 int number, const char *name, int kind, void *arg1, int arg2,
392 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
394 struct sysctl_oid *oidp;
398 /* You have to hook up somewhere.. */
401 sysctl_lock(LK_EXCLUSIVE);
402 /* Check if the node already exists, otherwise create it */
403 oidp = sysctl_find_oidname(name, parent, 0);
405 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
407 /* Update the context */
409 sysctl_ctx_entry_add(clist, oidp);
413 kprintf("can't re-use a leaf (%s)!\n", name);
418 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
419 oidp->oid_parent = parent;
420 SLIST_NEXT(oidp, oid_link) = NULL;
421 oidp->oid_number = number;
422 oidp->oid_refcnt = 1;
424 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
425 bcopy(name, newname, len + 1);
427 oidp->oid_name = newname;
428 oidp->oid_handler = handler;
429 oidp->oid_kind = CTLFLAG_DYN | kind;
430 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
431 struct sysctl_oid_list *children;
433 /* Allocate space for children */
434 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
435 SYSCTL_SET_CHILDREN(oidp, children);
436 SLIST_INIT(children);
438 oidp->oid_arg1 = arg1;
439 oidp->oid_arg2 = arg2;
443 int len = strlen(descr) + 1;
444 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
445 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
447 /* Update the context, if used */
449 sysctl_ctx_entry_add(clist, oidp);
450 /* Register this oid */
451 sysctl_register_oid_int(oidp);
457 * Register the kernel's oids on startup.
459 SET_DECLARE(sysctl_set, struct sysctl_oid);
462 sysctl_register_all(void *arg)
464 struct sysctl_oid **oidp;
466 lockinit(&sysctl_lkp, "sysctl", 0, 0);
467 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0);
468 SET_FOREACH(oidp, sysctl_set)
469 sysctl_register_oid_int(*oidp);
472 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
477 * These functions implement a presently undocumented interface
478 * used by the sysctl program to walk the tree, and get the type
479 * so it can print the value.
480 * This interface is under work and consideration, and should probably
481 * be killed with a big axe by the first person who can find the time.
482 * (be aware though, that the proper interface isn't as obvious as it
483 * may seem, there are various conflicting requirements.
485 * {0,0} kprintf the entire MIB-tree.
486 * {0,1,...} return the name of the "..." OID.
487 * {0,2,...} return the next OID.
488 * {0,3} return the OID of the name in "new"
489 * {0,4,...} return the kind & format info for the "..." OID.
493 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
496 struct sysctl_oid *oidp;
498 sysctl_lock(LK_SHARED);
499 SLIST_FOREACH(oidp, l, oid_link) {
504 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
507 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
508 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
510 if (oidp->oid_handler)
511 kprintf(" *Handler");
513 switch (oidp->oid_kind & CTLTYPE) {
516 if (!oidp->oid_handler) {
517 sysctl_sysctl_debug_dump_node(
518 oidp->oid_arg1, i+2);
521 case CTLTYPE_INT: kprintf(" Int\n"); break;
522 case CTLTYPE_STRING: kprintf(" String\n"); break;
523 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
524 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
525 default: kprintf("\n");
533 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
537 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
540 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
544 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
545 0, 0, sysctl_sysctl_debug, "-", "");
548 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
550 int *name = (int *) arg1;
551 u_int namelen = arg2;
553 struct sysctl_oid *oid;
554 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
557 sysctl_lock(LK_SHARED);
560 ksnprintf(buf, sizeof(buf), "%d", *name);
562 error = SYSCTL_OUT(req, ".", 1);
564 error = SYSCTL_OUT(req, buf, strlen(buf));
574 SLIST_FOREACH(oid, lsp, oid_link) {
575 if (oid->oid_number != *name)
579 error = SYSCTL_OUT(req, ".", 1);
581 error = SYSCTL_OUT(req, oid->oid_name,
582 strlen(oid->oid_name));
591 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
594 if (oid->oid_handler)
597 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
603 return (SYSCTL_OUT(req, "", 1));
606 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
609 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
610 int *next, int *len, int level, struct sysctl_oid **oidpp)
612 struct sysctl_oid *oidp;
615 sysctl_lock(LK_SHARED);
616 SLIST_FOREACH(oidp, lsp, oid_link) {
617 *next = oidp->oid_number;
621 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
625 if (oidp->oid_handler) {
626 /* We really should call the handler here...*/
630 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
631 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
632 len, level+1, oidpp)) {
639 if (oidp->oid_number < *name)
642 if (oidp->oid_number > *name) {
643 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
647 if (oidp->oid_handler) {
651 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
652 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
653 next+1, len, level+1, oidpp)) {
659 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
662 if (oidp->oid_handler)
665 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
666 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
667 len, level+1, oidpp)) {
682 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
684 int *name = (int *) arg1;
685 u_int namelen = arg2;
687 struct sysctl_oid *oid;
688 struct sysctl_oid_list *lsp = &sysctl__children;
689 int newoid[CTL_MAXNAME];
691 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
694 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
698 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
701 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
704 struct sysctl_oid *oidp;
705 struct sysctl_oid_list *lsp = &sysctl__children;
711 p = name + strlen(name) - 1 ;
717 for (p = name; *p && *p != '.'; p++)
723 sysctl_lock(LK_SHARED);
724 oidp = SLIST_FIRST(lsp);
726 while (oidp && *len < CTL_MAXNAME) {
727 if (strcmp(name, oidp->oid_name)) {
728 oidp = SLIST_NEXT(oidp, oid_link);
731 *oid++ = oidp->oid_number;
741 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
744 if (oidp->oid_handler)
747 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
748 oidp = SLIST_FIRST(lsp);
750 for (p = name; *p && *p != '.'; p++)
761 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
764 int error, oid[CTL_MAXNAME], len;
765 struct sysctl_oid *op = NULL;
769 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
770 return (ENAMETOOLONG);
772 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
774 error = SYSCTL_IN(req, p, req->newlen);
780 p [req->newlen] = '\0';
782 error = name2oid(p, oid, &len, &op);
789 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
793 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
794 sysctl_sysctl_name2oid, "I", "");
797 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
799 struct sysctl_oid *oid;
802 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
808 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
811 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
816 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
819 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
821 struct sysctl_oid *oid;
824 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
830 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
834 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
837 * Default "handler" functions.
841 * Handle an int, signed or unsigned.
843 * a variable: point arg1 at it.
844 * a constant: pass it in arg2.
848 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
853 error = SYSCTL_OUT(req, arg1, sizeof(int));
855 error = SYSCTL_OUT(req, &arg2, sizeof(int));
857 if (error || !req->newptr)
863 error = SYSCTL_IN(req, arg1, sizeof(int));
868 * Handle a long, signed or unsigned. arg1 points to it.
872 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
878 error = SYSCTL_OUT(req, arg1, sizeof(long));
880 if (error || !req->newptr)
883 error = SYSCTL_IN(req, arg1, sizeof(long));
888 * Handle a quad, signed or unsigned. arg1 points to it.
892 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
898 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
900 if (error || !req->newptr)
903 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
908 * Handle our generic '\0' terminated 'C' string.
910 * a variable string: point arg1 at it, arg2 is max length.
911 * a constant string: point arg1 at it, arg2 is zero.
915 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
919 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
921 if (error || !req->newptr)
924 if ((req->newlen - req->newidx) >= arg2) {
927 arg2 = (req->newlen - req->newidx);
928 error = SYSCTL_IN(req, arg1, arg2);
929 ((char *)arg1)[arg2] = '\0';
936 * Handle any kind of opaque data.
937 * arg1 points to it, arg2 is the size.
941 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
945 error = SYSCTL_OUT(req, arg1, arg2);
947 if (error || !req->newptr)
950 error = SYSCTL_IN(req, arg1, arg2);
956 * Transfer functions to/from kernel space.
957 * XXX: rather untested at this point
960 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
966 if (i > req->oldlen - req->oldidx)
967 i = req->oldlen - req->oldidx;
969 bcopy(p, (char *)req->oldptr + req->oldidx, i);
972 if (req->oldptr && i != l)
978 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
983 if (req->newlen - req->newidx < l)
985 bcopy((char *)req->newptr + req->newidx, p, l);
991 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
994 struct sysctl_req req;
996 bzero(&req, sizeof req);
1001 req.oldlen = *oldlenp;
1009 req.newlen = newlen;
1013 req.oldfunc = sysctl_old_kernel;
1014 req.newfunc = sysctl_new_kernel;
1019 sysctl_lock(LK_SHARED);
1021 error = sysctl_root(0, name, namelen, &req);
1025 vsunlock(req.oldptr, req.oldlen);
1030 if (error && error != ENOMEM)
1034 if (req.oldptr && req.oldidx > req.oldlen)
1035 *retval = req.oldlen;
1037 *retval = req.oldidx;
1043 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1044 void *new, size_t newlen, size_t *retval)
1046 int oid[CTL_MAXNAME];
1047 size_t oidlen, plen;
1050 oid[0] = 0; /* sysctl internal magic */
1051 oid[1] = 3; /* name2oid */
1052 oidlen = sizeof(oid);
1054 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1058 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1059 new, newlen, retval);
1064 * Transfer function to/from user space.
1067 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1073 if (req->lock == 1 && req->oldptr) {
1074 vslock(req->oldptr, req->oldlen);
1080 if (i > req->oldlen - req->oldidx)
1081 i = req->oldlen - req->oldidx;
1083 error = copyout(p, (char *)req->oldptr + req->oldidx,
1089 if (req->oldptr && i < l)
1095 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1101 if (req->newlen - req->newidx < l)
1103 error = copyin((char *)req->newptr + req->newidx, p, l);
1109 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1110 int *nindx, struct sysctl_req *req)
1112 struct sysctl_oid *oid;
1115 sysctl_lock(LK_SHARED);
1116 oid = SLIST_FIRST(&sysctl__children);
1118 while (oid && indx < CTL_MAXNAME) {
1119 if (oid->oid_number == name[indx]) {
1121 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1122 if (oid->oid_handler != NULL ||
1131 (struct sysctl_oid_list *)oid->oid_arg1);
1132 } else if (indx == namelen) {
1143 oid = SLIST_NEXT(oid, oid_link);
1151 * Traverse our tree, and find the right node, execute whatever it points
1152 * to, and return the resulting error code.
1156 sysctl_root(SYSCTL_HANDLER_ARGS)
1158 struct thread *td = req->td;
1159 struct proc *p = td ? td->td_proc : NULL;
1160 struct sysctl_oid *oid;
1163 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1167 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1169 * You can't call a sysctl when it's a node, but has
1170 * no handler. Inform the user that it's a node.
1171 * The indx may or may not be the same as namelen.
1173 if (oid->oid_handler == NULL)
1177 /* If writing isn't allowed */
1178 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1179 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1182 /* Most likely only root can write */
1183 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1184 (error = priv_check_cred(td->td_ucred,
1185 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1186 PRIV_SYSCTL_WRITE, 0)))
1189 if (!oid->oid_handler)
1192 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1193 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1196 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1205 sys___sysctl(struct sysctl_args *uap)
1207 int error, i, name[CTL_MAXNAME];
1210 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1213 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1217 error = userland_sysctl(name, uap->namelen,
1218 uap->old, uap->oldlenp, 0,
1219 uap->new, uap->newlen, &j);
1220 if (error && error != ENOMEM)
1223 i = copyout(&j, uap->oldlenp, sizeof(j));
1231 * This is used from various compatibility syscalls too. That's why name
1232 * must be in kernel space.
1235 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1238 struct sysctl_req req, req2;
1240 bzero(&req, sizeof req);
1244 req.oldlen = *oldlenp;
1246 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1253 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1259 if (!useracc(new, newlen, VM_PROT_READ))
1261 req.newlen = newlen;
1265 req.oldfunc = sysctl_old_user;
1266 req.newfunc = sysctl_new_user;
1272 sysctl_lock(LK_SHARED);
1276 error = sysctl_root(0, name, namelen, &req2);
1277 } while (error == EAGAIN);
1282 vsunlock(req.oldptr, req.oldlen);
1287 if (error && error != ENOMEM)
1291 if (req.oldptr && req.oldidx > req.oldlen)
1292 *retval = req.oldlen;
1294 *retval = req.oldidx;
1300 sysctl_lock(int flag)
1302 lockmgr(&sysctl_lkp, flag);
1308 lockmgr(&sysctl_lkp, LK_RELEASE);
1312 sysctl_ctx_lock(int flag)
1314 lockmgr(&sysctl_ctx_lkp, flag);
1318 sysctl_ctx_unlock(void)
1320 lockmgr(&sysctl_ctx_lkp, LK_RELEASE);
1324 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1328 value = *(int *)arg1;
1329 error = sysctl_handle_int(oidp, &value, 0, req);
1330 if (error || !req->newptr)
1332 if (value < low || value > high)
1334 *(int *)arg1 = value;