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.18 2005/02/05 23:04:28 joerg 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>
51 #include <sys/sysproto.h>
54 #include <vm/vm_extern.h>
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
57 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
59 static struct lock sysctl_lkp;
60 static struct lock sysctl_ctx_lkp;
62 static void sysctl_lock(int type);
63 static void sysctl_unlock(void);
64 static void sysctl_ctx_lock(int type);
65 static void sysctl_ctx_unlock(void);
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);
70 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
71 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
73 struct sysctl_oid_list sysctl__children; /* root list */
75 static struct sysctl_oid *
76 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
78 struct sysctl_oid *oidp;
80 SLIST_FOREACH(oidp, list, oid_link) {
81 if (strcmp(oidp->oid_name, name) == 0) {
89 * Initialization of the MIB tree.
91 * Order by number in each list.
95 sysctl_register_oid(struct sysctl_oid *oidp)
97 sysctl_lock(LK_EXCLUSIVE);
98 sysctl_register_oid_int(oidp);
103 sysctl_register_oid_int(struct sysctl_oid *oidp)
105 struct sysctl_oid_list *parent = oidp->oid_parent;
106 struct sysctl_oid *p;
107 struct sysctl_oid *q;
110 * First check if another oid with the same name already
111 * exists in the parent's list.
113 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
115 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
118 printf("can't re-use a leaf (%s)!\n", p->oid_name);
123 * If this oid has a number OID_AUTO, give it a number which
124 * is greater than any current oid. Make sure it is at least
125 * 256 to leave space for pre-assigned oid numbers.
127 if (oidp->oid_number == OID_AUTO) {
128 int newoid = 0x100; /* minimum AUTO oid */
131 * Adjust based on highest oid in parent list
133 SLIST_FOREACH(p, parent, oid_link) {
134 if (newoid <= p->oid_number)
135 newoid = p->oid_number + 1;
137 oidp->oid_number = newoid;
141 * Insert the oid into the parent's list in order.
144 SLIST_FOREACH(p, parent, oid_link) {
145 if (oidp->oid_number < p->oid_number)
150 SLIST_INSERT_AFTER(q, oidp, oid_link);
152 SLIST_INSERT_HEAD(parent, oidp, oid_link);
156 sysctl_unregister_oid(struct sysctl_oid *oidp)
158 sysctl_lock(LK_EXCLUSIVE);
159 sysctl_unregister_oid_int(oidp);
164 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
166 struct sysctl_oid *p;
168 if (oidp->oid_number == OID_AUTO)
169 panic("Trying to unregister OID_AUTO entry: %p", oidp);
171 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
174 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
179 * This can happen when a module fails to register and is
180 * being unloaded afterwards. It should not be a panic()
183 printf("%s: failed to unregister sysctl\n", __func__);
186 /* Initialize a new context to keep track of dynamically added sysctls. */
188 sysctl_ctx_init(struct sysctl_ctx_list *c)
196 /* Free the context, and destroy all dynamic oids registered in this context */
198 sysctl_ctx_free(struct sysctl_ctx_list *clist)
200 struct sysctl_ctx_entry *e, *e1;
204 sysctl_ctx_lock(LK_EXCLUSIVE);
206 * First perform a "dry run" to check if it's ok to remove oids.
208 * XXX This algorithm is a hack. But I don't know any
209 * XXX better solution for now...
211 TAILQ_FOREACH(e, clist, link) {
212 error = sysctl_remove_oid(e->entry, 0, 0);
217 * Restore deregistered entries, either from the end,
218 * or from the place where error occured.
219 * e contains the entry that was not unregistered
222 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
224 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
226 sysctl_register_oid(e1->entry);
227 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
233 /* Now really delete the entries */
234 e = TAILQ_FIRST(clist);
236 e1 = TAILQ_NEXT(e, link);
237 error = sysctl_remove_oid(e->entry, 1, 0);
239 panic("sysctl_remove_oid: corrupt tree, entry: %s",
241 free(e, M_SYSCTLOID);
248 /* Add an entry to the context */
249 struct sysctl_ctx_entry *
250 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
252 struct sysctl_ctx_entry *e;
254 if (clist == NULL || oidp == NULL)
256 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
258 sysctl_ctx_lock(LK_EXCLUSIVE);
259 TAILQ_INSERT_HEAD(clist, e, link);
264 /* Find an entry in the context */
265 struct sysctl_ctx_entry *
266 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
268 struct sysctl_ctx_entry *e;
270 if (clist == NULL || oidp == NULL)
273 sysctl_ctx_lock(LK_SHARED);
274 e = sysctl_ctx_entry_find_int(clist, oidp);
280 struct sysctl_ctx_entry *
281 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
283 struct sysctl_ctx_entry *e;
285 KKASSERT(clist != NULL && oidp != NULL);
287 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
296 * Delete an entry from the context.
297 * NOTE: this function doesn't free oidp! You have to remove it
298 * with sysctl_remove_oid().
301 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
303 struct sysctl_ctx_entry *e;
305 if (clist == NULL || oidp == NULL)
308 sysctl_ctx_lock(LK_EXCLUSIVE);
309 e = sysctl_ctx_entry_find_int(clist, oidp);
314 TAILQ_REMOVE(clist, e, link);
315 free(e, M_SYSCTLOID);
322 * Remove dynamically created sysctl trees.
323 * oidp - top of the tree to be removed
324 * del - if 0 - just deregister, otherwise free up entries as well
325 * recurse - if != 0 traverse the subtree to be deleted
328 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
330 struct sysctl_oid *p;
335 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
336 printf("can't remove non-dynamic nodes!\n");
339 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
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(p, SYSCTL_CHILDREN(oidp), oid_link) {
354 error = sysctl_remove_oid(p, del, recurse);
361 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
364 if (oidp->oid_refcnt > 1 ) {
367 if (oidp->oid_refcnt == 0) {
368 printf("Warning: bad oid_refcnt=%u (%s)!\n",
369 oidp->oid_refcnt, oidp->oid_name);
373 sysctl_unregister_oid_int(oidp);
376 free(__DECONST(char *,oidp->oid_descr),
378 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
379 free(oidp, M_SYSCTLOID);
387 * Create new sysctls at run time.
388 * clist may point to a valid context initialized with sysctl_ctx_init().
391 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
392 int number, const char *name, int kind, void *arg1, int arg2,
393 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
395 struct sysctl_oid *oidp;
399 /* You have to hook up somewhere.. */
402 sysctl_lock(LK_EXCLUSIVE);
403 /* Check if the node already exists, otherwise create it */
404 oidp = sysctl_find_oidname(name, parent, 0);
406 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
408 /* Update the context */
410 sysctl_ctx_entry_add(clist, oidp);
414 printf("can't re-use a leaf (%s)!\n", name);
419 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
420 oidp->oid_parent = parent;
421 SLIST_NEXT(oidp, oid_link) = NULL;
422 oidp->oid_number = number;
423 oidp->oid_refcnt = 1;
425 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
426 bcopy(name, newname, len + 1);
428 oidp->oid_name = newname;
429 oidp->oid_handler = handler;
430 oidp->oid_kind = CTLFLAG_DYN | kind;
431 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
432 struct sysctl_oid_list *children;
434 /* Allocate space for children */
435 children = malloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
436 SYSCTL_SET_CHILDREN(oidp, children);
437 SLIST_INIT(children);
439 oidp->oid_arg1 = arg1;
440 oidp->oid_arg2 = arg2;
444 int len = strlen(descr) + 1;
445 oidp->oid_descr = malloc(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);
463 static void sysctl_register_all(void *arg)
465 struct sysctl_oid **oidp;
467 lockinit(&sysctl_lkp, 0, "sysctl", 0, 0);
468 lockinit(&sysctl_ctx_lkp, 0, "sysctl ctx", 0, 0);
469 SET_FOREACH(oidp, sysctl_set)
470 sysctl_register_oid_int(*oidp);
473 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
478 * These functions implement a presently undocumented interface
479 * used by the sysctl program to walk the tree, and get the type
480 * so it can print the value.
481 * This interface is under work and consideration, and should probably
482 * be killed with a big axe by the first person who can find the time.
483 * (be aware though, that the proper interface isn't as obvious as it
484 * may seem, there are various conflicting requirements.
486 * {0,0} printf the entire MIB-tree.
487 * {0,1,...} return the name of the "..." OID.
488 * {0,2,...} return the next OID.
489 * {0,3} return the OID of the name in "new"
490 * {0,4,...} return the kind & format info for the "..." OID.
494 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
497 struct sysctl_oid *oidp;
499 sysctl_lock(LK_SHARED);
500 SLIST_FOREACH(oidp, l, oid_link) {
505 printf("%d %s ", oidp->oid_number, oidp->oid_name);
508 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
509 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
511 if (oidp->oid_handler)
514 switch (oidp->oid_kind & CTLTYPE) {
517 if (!oidp->oid_handler) {
518 sysctl_sysctl_debug_dump_node(
519 oidp->oid_arg1, i+2);
522 case CTLTYPE_INT: printf(" Int\n"); break;
523 case CTLTYPE_STRING: printf(" String\n"); break;
524 case CTLTYPE_QUAD: printf(" Quad\n"); break;
525 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
526 default: printf("\n");
534 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
538 error = suser(req->td);
541 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
545 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
546 0, 0, sysctl_sysctl_debug, "-", "");
549 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
551 int *name = (int *) arg1;
552 u_int namelen = arg2;
554 struct sysctl_oid *oid;
555 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
558 sysctl_lock(LK_SHARED);
561 snprintf(buf,sizeof(buf),"%d",*name);
563 error = SYSCTL_OUT(req, ".", 1);
565 error = SYSCTL_OUT(req, buf, strlen(buf));
575 SLIST_FOREACH(oid, lsp, oid_link) {
576 if (oid->oid_number != *name)
580 error = SYSCTL_OUT(req, ".", 1);
582 error = SYSCTL_OUT(req, oid->oid_name,
583 strlen(oid->oid_name));
592 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
595 if (oid->oid_handler)
598 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
604 return (SYSCTL_OUT(req, "", 1));
607 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
610 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
611 int *next, int *len, int level, struct sysctl_oid **oidpp)
613 struct sysctl_oid *oidp;
616 sysctl_lock(LK_SHARED);
617 SLIST_FOREACH(oidp, lsp, oid_link) {
618 *next = oidp->oid_number;
622 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
626 if (oidp->oid_handler) {
627 /* We really should call the handler here...*/
631 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
632 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
633 len, level+1, oidpp)) {
640 if (oidp->oid_number < *name)
643 if (oidp->oid_number > *name) {
644 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
648 if (oidp->oid_handler) {
652 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
653 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
654 next+1, len, level+1, oidpp)) {
660 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
663 if (oidp->oid_handler)
666 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
667 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
668 len, level+1, oidpp)) {
683 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
685 int *name = (int *) arg1;
686 u_int namelen = arg2;
688 struct sysctl_oid *oid;
689 struct sysctl_oid_list *lsp = &sysctl__children;
690 int newoid[CTL_MAXNAME];
692 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
695 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
699 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
702 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
705 struct sysctl_oid *oidp;
706 struct sysctl_oid_list *lsp = &sysctl__children;
712 p = name + strlen(name) - 1 ;
718 for (p = name; *p && *p != '.'; p++)
724 sysctl_lock(LK_SHARED);
725 oidp = SLIST_FIRST(lsp);
727 while (oidp && *len < CTL_MAXNAME) {
728 if (strcmp(name, oidp->oid_name)) {
729 oidp = SLIST_NEXT(oidp, oid_link);
732 *oid++ = oidp->oid_number;
742 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
745 if (oidp->oid_handler)
748 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
749 oidp = SLIST_FIRST(lsp);
751 for (p = name; *p && *p != '.'; p++)
762 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
765 int error, oid[CTL_MAXNAME], len;
766 struct sysctl_oid *op = 0;
770 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
771 return (ENAMETOOLONG);
773 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
775 error = SYSCTL_IN(req, p, req->newlen);
781 p [req->newlen] = '\0';
783 error = name2oid(p, oid, &len, &op);
790 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
794 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
795 sysctl_sysctl_name2oid, "I", "");
798 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
800 struct sysctl_oid *oid;
803 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
809 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
812 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
817 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
820 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
822 struct sysctl_oid *oid;
825 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
831 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
835 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
838 * Default "handler" functions.
842 * Handle an int, signed or unsigned.
844 * a variable: point arg1 at it.
845 * a constant: pass it in arg2.
849 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
854 error = SYSCTL_OUT(req, arg1, sizeof(int));
856 error = SYSCTL_OUT(req, &arg2, sizeof(int));
858 if (error || !req->newptr)
864 error = SYSCTL_IN(req, arg1, sizeof(int));
869 * Handle a long, signed or unsigned. arg1 points to it.
873 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
879 error = SYSCTL_OUT(req, arg1, sizeof(long));
881 if (error || !req->newptr)
884 error = SYSCTL_IN(req, arg1, sizeof(long));
889 * Handle a quad, signed or unsigned. arg1 points to it.
893 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
899 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
901 if (error || !req->newptr)
904 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
909 * Handle our generic '\0' terminated 'C' string.
911 * a variable string: point arg1 at it, arg2 is max length.
912 * a constant string: point arg1 at it, arg2 is zero.
916 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
920 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
922 if (error || !req->newptr)
925 if ((req->newlen - req->newidx) >= arg2) {
928 arg2 = (req->newlen - req->newidx);
929 error = SYSCTL_IN(req, arg1, arg2);
930 ((char *)arg1)[arg2] = '\0';
937 * Handle any kind of opaque data.
938 * arg1 points to it, arg2 is the size.
942 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
946 error = SYSCTL_OUT(req, arg1, arg2);
948 if (error || !req->newptr)
951 error = SYSCTL_IN(req, arg1, arg2);
957 * Transfer functions to/from kernel space.
958 * XXX: rather untested at this point
961 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
967 if (i > req->oldlen - req->oldidx)
968 i = req->oldlen - req->oldidx;
970 bcopy(p, (char *)req->oldptr + req->oldidx, i);
973 if (req->oldptr && i != l)
979 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
984 if (req->newlen - req->newidx < l)
986 bcopy((char *)req->newptr + req->newidx, p, l);
992 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
995 struct sysctl_req req;
997 bzero(&req, sizeof req);
1002 req.oldlen = *oldlenp;
1010 req.newlen = newlen;
1014 req.oldfunc = sysctl_old_kernel;
1015 req.newfunc = sysctl_new_kernel;
1018 sysctl_lock(LK_SHARED);
1020 error = sysctl_root(0, name, namelen, &req);
1023 vsunlock(req.oldptr, req.oldlen);
1027 if (error && error != ENOMEM)
1031 if (req.oldptr && req.oldidx > req.oldlen)
1032 *retval = req.oldlen;
1034 *retval = req.oldidx;
1040 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1041 void *new, size_t newlen, size_t *retval)
1043 int oid[CTL_MAXNAME];
1044 size_t oidlen, plen;
1047 oid[0] = 0; /* sysctl internal magic */
1048 oid[1] = 3; /* name2oid */
1049 oidlen = sizeof(oid);
1051 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
1052 strlen(name), &plen);
1056 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1057 new, newlen, retval);
1062 * Transfer function to/from user space.
1065 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1070 if (req->lock == 1 && req->oldptr) {
1071 vslock(req->oldptr, req->oldlen);
1076 if (i > req->oldlen - req->oldidx)
1077 i = req->oldlen - req->oldidx;
1079 error = copyout(p, (char *)req->oldptr + req->oldidx,
1085 if (req->oldptr && i < l)
1091 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1097 if (req->newlen - req->newidx < l)
1099 error = copyin((char *)req->newptr + req->newidx, p, l);
1105 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1106 int *nindx, struct sysctl_req *req)
1108 struct sysctl_oid *oid;
1111 sysctl_lock(LK_SHARED);
1112 oid = SLIST_FIRST(&sysctl__children);
1114 while (oid && indx < CTL_MAXNAME) {
1115 if (oid->oid_number == name[indx]) {
1117 if (oid->oid_kind & CTLFLAG_NOLOCK)
1119 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1120 if (oid->oid_handler != NULL ||
1129 (struct sysctl_oid_list *)oid->oid_arg1);
1130 } else if (indx == namelen) {
1141 oid = SLIST_NEXT(oid, oid_link);
1149 * Traverse our tree, and find the right node, execute whatever it points
1150 * to, and return the resulting error code.
1154 sysctl_root(SYSCTL_HANDLER_ARGS)
1156 struct thread *td = req->td;
1157 struct proc *p = td ? td->td_proc : NULL;
1158 struct sysctl_oid *oid;
1161 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1165 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1167 * You can't call a sysctl when it's a node, but has
1168 * no handler. Inform the user that it's a node.
1169 * The indx may or may not be the same as namelen.
1171 if (oid->oid_handler == NULL)
1175 /* If writing isn't allowed */
1176 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1177 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1180 /* Most likely only root can write */
1181 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1182 (error = suser_cred(p->p_ucred,
1183 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1186 if (!oid->oid_handler)
1189 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1190 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1193 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1199 __sysctl(struct sysctl_args *uap)
1201 int error, i, name[CTL_MAXNAME];
1204 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1207 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1211 error = userland_sysctl(name, uap->namelen,
1212 uap->old, uap->oldlenp, 0,
1213 uap->new, uap->newlen, &j);
1214 if (error && error != ENOMEM)
1217 i = copyout(&j, uap->oldlenp, sizeof(j));
1225 * This is used from various compatibility syscalls too. That's why name
1226 * must be in kernel space.
1229 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1232 struct sysctl_req req, req2;
1234 bzero(&req, sizeof req);
1238 req.oldlen = *oldlenp;
1240 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1247 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1253 if (!useracc(new, req.newlen, VM_PROT_READ))
1255 req.newlen = newlen;
1259 req.oldfunc = sysctl_old_user;
1260 req.newfunc = sysctl_new_user;
1264 sysctl_lock(LK_SHARED);
1268 error = sysctl_root(0, name, namelen, &req2);
1269 } while (error == EAGAIN);
1273 vsunlock(req.oldptr, req.oldlen);
1277 if (error && error != ENOMEM)
1281 if (req.oldptr && req.oldidx > req.oldlen)
1282 *retval = req.oldlen;
1284 *retval = req.oldidx;
1290 sysctl_lock(int flag)
1292 lockmgr(&sysctl_lkp, flag, NULL, curthread);
1298 lockmgr(&sysctl_lkp, LK_RELEASE, NULL, curthread);
1302 sysctl_ctx_lock(int flag)
1304 lockmgr(&sysctl_ctx_lkp, flag, NULL, curthread);
1308 sysctl_ctx_unlock(void)
1310 lockmgr(&sysctl_ctx_lkp, LK_RELEASE, NULL, curthread);