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.14 2003/11/23 22:15:22 dillon 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>
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");
61 static struct sysctl_lock {
67 static int sysctl_root(SYSCTL_HANDLER_ARGS);
69 struct sysctl_oid_list sysctl__children; /* root list */
71 static struct sysctl_oid *
72 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
74 struct sysctl_oid *oidp;
76 SLIST_FOREACH(oidp, list, oid_link) {
77 if (strcmp(oidp->oid_name, name) == 0) {
85 * Initialization of the MIB tree.
87 * Order by number in each list.
90 void sysctl_register_oid(struct sysctl_oid *oidp)
92 struct sysctl_oid_list *parent = oidp->oid_parent;
97 * First check if another oid with the same name already
98 * exists in the parent's list.
100 p = sysctl_find_oidname(oidp->oid_name, parent);
102 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
106 printf("can't re-use a leaf (%s)!\n", p->oid_name);
111 * If this oid has a number OID_AUTO, give it a number which
112 * is greater than any current oid. Make sure it is at least
113 * 256 to leave space for pre-assigned oid numbers.
115 if (oidp->oid_number == OID_AUTO) {
116 int newoid = 0x100; /* minimum AUTO oid */
119 * Adjust based on highest oid in parent list
121 SLIST_FOREACH(p, parent, oid_link) {
122 if (newoid <= p->oid_number)
123 newoid = p->oid_number + 1;
125 oidp->oid_number = newoid;
129 * Insert the oid into the parent's list in order.
132 SLIST_FOREACH(p, parent, oid_link) {
133 if (oidp->oid_number < p->oid_number)
138 SLIST_INSERT_AFTER(q, oidp, oid_link);
140 SLIST_INSERT_HEAD(parent, oidp, oid_link);
143 void sysctl_unregister_oid(struct sysctl_oid *oidp)
145 struct sysctl_oid *p;
149 if (oidp->oid_number == OID_AUTO) {
152 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
154 SLIST_REMOVE(oidp->oid_parent, oidp,
155 sysctl_oid, oid_link);
163 * This can happen when a module fails to register and is
164 * being unloaded afterwards. It should not be a panic()
168 printf("%s: failed to unregister sysctl\n", __func__);
171 /* Initialize a new context to keep track of dynamically added sysctls. */
173 sysctl_ctx_init(struct sysctl_ctx_list *c)
183 /* Free the context, and destroy all dynamic oids registered in this context */
185 sysctl_ctx_free(struct sysctl_ctx_list *clist)
187 struct sysctl_ctx_entry *e, *e1;
192 * First perform a "dry run" to check if it's ok to remove oids.
194 * XXX This algorithm is a hack. But I don't know any
195 * XXX better solution for now...
197 TAILQ_FOREACH(e, clist, link) {
198 error = sysctl_remove_oid(e->entry, 0, 0);
203 * Restore deregistered entries, either from the end,
204 * or from the place where error occured.
205 * e contains the entry that was not unregistered
208 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
210 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
212 sysctl_register_oid(e1->entry);
213 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
217 /* Now really delete the entries */
218 e = TAILQ_FIRST(clist);
220 e1 = TAILQ_NEXT(e, link);
221 error = sysctl_remove_oid(e->entry, 1, 0);
223 panic("sysctl_remove_oid: corrupt tree, entry: %s",
225 free(e, M_SYSCTLOID);
231 /* Add an entry to the context */
232 struct sysctl_ctx_entry *
233 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
235 struct sysctl_ctx_entry *e;
237 if (clist == NULL || oidp == NULL)
239 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
241 TAILQ_INSERT_HEAD(clist, e, link);
245 /* Find an entry in the context */
246 struct sysctl_ctx_entry *
247 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
249 struct sysctl_ctx_entry *e;
251 if (clist == NULL || oidp == NULL)
253 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
261 * Delete an entry from the context.
262 * NOTE: this function doesn't free oidp! You have to remove it
263 * with sysctl_remove_oid().
266 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
268 struct sysctl_ctx_entry *e;
270 if (clist == NULL || oidp == NULL)
272 e = sysctl_ctx_entry_find(clist, oidp);
274 TAILQ_REMOVE(clist, e, link);
275 free(e, M_SYSCTLOID);
282 * Remove dynamically created sysctl trees.
283 * oidp - top of the tree to be removed
284 * del - if 0 - just deregister, otherwise free up entries as well
285 * recurse - if != 0 traverse the subtree to be deleted
288 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
290 struct sysctl_oid *p;
295 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
296 printf("can't remove non-dynamic nodes!\n");
300 * WARNING: normal method to do this should be through
301 * sysctl_ctx_free(). Use recursing as the last resort
302 * method to purge your sysctl tree of leftovers...
303 * However, if some other code still references these nodes,
306 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
307 if (oidp->oid_refcnt == 1) {
308 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
311 error = sysctl_remove_oid(p, del, recurse);
316 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
319 if (oidp->oid_refcnt > 1 ) {
322 if (oidp->oid_refcnt == 0) {
323 printf("Warning: bad oid_refcnt=%u (%s)!\n",
324 oidp->oid_refcnt, oidp->oid_name);
327 sysctl_unregister_oid(oidp);
330 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
331 free((void *)(uintptr_t)(const void *)oidp->oid_name,
333 free(oidp, M_SYSCTLOID);
340 * Create new sysctls at run time.
341 * clist may point to a valid context initialized with sysctl_ctx_init().
344 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
345 int number, const char *name, int kind, void *arg1, int arg2,
346 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
348 struct sysctl_oid *oidp;
352 /* You have to hook up somewhere.. */
355 /* Check if the node already exists, otherwise create it */
356 oidp = sysctl_find_oidname(name, parent);
358 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
360 /* Update the context */
362 sysctl_ctx_entry_add(clist, oidp);
365 printf("can't re-use a leaf (%s)!\n", name);
369 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
370 bzero(oidp, sizeof(struct sysctl_oid));
371 oidp->oid_parent = parent;
372 SLIST_NEXT(oidp, oid_link) = NULL;
373 oidp->oid_number = number;
374 oidp->oid_refcnt = 1;
376 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
377 bcopy(name, newname, len + 1);
379 oidp->oid_name = newname;
380 oidp->oid_handler = handler;
381 oidp->oid_kind = CTLFLAG_DYN | kind;
382 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
383 /* Allocate space for children */
384 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
385 M_SYSCTLOID, M_WAITOK);
386 SLIST_INIT(SYSCTL_CHILDREN(oidp));
388 oidp->oid_arg1 = arg1;
389 oidp->oid_arg2 = arg2;
393 int len = strlen(descr) + 1;
394 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
396 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
398 /* Update the context, if used */
400 sysctl_ctx_entry_add(clist, oidp);
401 /* Register this oid */
402 sysctl_register_oid(oidp);
407 * Register the kernel's oids on startup.
409 SET_DECLARE(sysctl_set, struct sysctl_oid);
411 static void sysctl_register_all(void *arg)
413 struct sysctl_oid **oidp;
415 SET_FOREACH(oidp, sysctl_set)
416 sysctl_register_oid(*oidp);
419 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
424 * These functions implement a presently undocumented interface
425 * used by the sysctl program to walk the tree, and get the type
426 * so it can print the value.
427 * This interface is under work and consideration, and should probably
428 * be killed with a big axe by the first person who can find the time.
429 * (be aware though, that the proper interface isn't as obvious as it
430 * may seem, there are various conflicting requirements.
432 * {0,0} printf the entire MIB-tree.
433 * {0,1,...} return the name of the "..." OID.
434 * {0,2,...} return the next OID.
435 * {0,3} return the OID of the name in "new"
436 * {0,4,...} return the kind & format info for the "..." OID.
440 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
443 struct sysctl_oid *oidp;
445 SLIST_FOREACH(oidp, l, oid_link) {
450 printf("%d %s ", oidp->oid_number, oidp->oid_name);
453 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
454 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
456 if (oidp->oid_handler)
459 switch (oidp->oid_kind & CTLTYPE) {
462 if (!oidp->oid_handler) {
463 sysctl_sysctl_debug_dump_node(
464 oidp->oid_arg1, i+2);
467 case CTLTYPE_INT: printf(" Int\n"); break;
468 case CTLTYPE_STRING: printf(" String\n"); break;
469 case CTLTYPE_QUAD: printf(" Quad\n"); break;
470 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
471 default: printf("\n");
478 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
482 error = suser(req->td);
485 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
489 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
490 0, 0, sysctl_sysctl_debug, "-", "");
493 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
495 int *name = (int *) arg1;
496 u_int namelen = arg2;
498 struct sysctl_oid *oid;
499 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
504 snprintf(buf,sizeof(buf),"%d",*name);
506 error = SYSCTL_OUT(req, ".", 1);
508 error = SYSCTL_OUT(req, buf, strlen(buf));
516 SLIST_FOREACH(oid, lsp, oid_link) {
517 if (oid->oid_number != *name)
521 error = SYSCTL_OUT(req, ".", 1);
523 error = SYSCTL_OUT(req, oid->oid_name,
524 strlen(oid->oid_name));
531 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
534 if (oid->oid_handler)
537 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
542 return (SYSCTL_OUT(req, "", 1));
545 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
548 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
549 int *next, int *len, int level, struct sysctl_oid **oidpp)
551 struct sysctl_oid *oidp;
554 SLIST_FOREACH(oidp, lsp, oid_link) {
555 *next = oidp->oid_number;
559 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
561 if (oidp->oid_handler)
562 /* We really should call the handler here...*/
564 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
565 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
566 len, level+1, oidpp))
571 if (oidp->oid_number < *name)
574 if (oidp->oid_number > *name) {
575 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
577 if (oidp->oid_handler)
579 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
580 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
581 next+1, len, level+1, oidpp))
585 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
588 if (oidp->oid_handler)
591 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
592 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
593 len, level+1, oidpp))
605 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
607 int *name = (int *) arg1;
608 u_int namelen = arg2;
610 struct sysctl_oid *oid;
611 struct sysctl_oid_list *lsp = &sysctl__children;
612 int newoid[CTL_MAXNAME];
614 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
617 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
621 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
624 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
627 struct sysctl_oid *oidp;
628 struct sysctl_oid_list *lsp = &sysctl__children;
634 p = name + strlen(name) - 1 ;
640 for (p = name; *p && *p != '.'; p++)
646 oidp = SLIST_FIRST(lsp);
648 while (oidp && *len < CTL_MAXNAME) {
649 if (strcmp(name, oidp->oid_name)) {
650 oidp = SLIST_NEXT(oidp, oid_link);
653 *oid++ = oidp->oid_number;
662 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
665 if (oidp->oid_handler)
668 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
669 oidp = SLIST_FIRST(lsp);
671 for (p = name; *p && *p != '.'; p++)
681 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
684 int error, oid[CTL_MAXNAME], len;
685 struct sysctl_oid *op = 0;
689 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
690 return (ENAMETOOLONG);
692 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
694 error = SYSCTL_IN(req, p, req->newlen);
700 p [req->newlen] = '\0';
702 error = name2oid(p, oid, &len, &op);
709 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
713 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
714 sysctl_sysctl_name2oid, "I", "");
717 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
719 struct sysctl_oid *oid;
722 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
728 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
731 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
736 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
739 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
741 struct sysctl_oid *oid;
744 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
750 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
754 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
757 * Default "handler" functions.
761 * Handle an int, signed or unsigned.
763 * a variable: point arg1 at it.
764 * a constant: pass it in arg2.
768 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
773 error = SYSCTL_OUT(req, arg1, sizeof(int));
775 error = SYSCTL_OUT(req, &arg2, sizeof(int));
777 if (error || !req->newptr)
783 error = SYSCTL_IN(req, arg1, sizeof(int));
788 * Handle a long, signed or unsigned. arg1 points to it.
792 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
798 error = SYSCTL_OUT(req, arg1, sizeof(long));
800 if (error || !req->newptr)
803 error = SYSCTL_IN(req, arg1, sizeof(long));
808 * Handle a quad, signed or unsigned. arg1 points to it.
812 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
818 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
820 if (error || !req->newptr)
823 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
828 * Handle our generic '\0' terminated 'C' string.
830 * a variable string: point arg1 at it, arg2 is max length.
831 * a constant string: point arg1 at it, arg2 is zero.
835 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
839 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
841 if (error || !req->newptr)
844 if ((req->newlen - req->newidx) >= arg2) {
847 arg2 = (req->newlen - req->newidx);
848 error = SYSCTL_IN(req, arg1, arg2);
849 ((char *)arg1)[arg2] = '\0';
856 * Handle any kind of opaque data.
857 * arg1 points to it, arg2 is the size.
861 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
865 error = SYSCTL_OUT(req, arg1, arg2);
867 if (error || !req->newptr)
870 error = SYSCTL_IN(req, arg1, arg2);
876 * Transfer functions to/from kernel space.
877 * XXX: rather untested at this point
880 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
886 if (i > req->oldlen - req->oldidx)
887 i = req->oldlen - req->oldidx;
889 bcopy(p, (char *)req->oldptr + req->oldidx, i);
892 if (req->oldptr && i != l)
898 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
903 if (req->newlen - req->newidx < l)
905 bcopy((char *)req->newptr + req->newidx, p, l);
911 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
914 struct sysctl_req req;
916 bzero(&req, sizeof req);
921 req.oldlen = *oldlenp;
933 req.oldfunc = sysctl_old_kernel;
934 req.newfunc = sysctl_new_kernel;
937 /* XXX this should probably be done in a general way */
938 while (memlock.sl_lock) {
940 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
945 error = sysctl_root(0, name, namelen, &req);
948 vsunlock(req.oldptr, req.oldlen);
952 if (memlock.sl_want) {
954 wakeup((caddr_t)&memlock);
957 if (error && error != ENOMEM)
961 if (req.oldptr && req.oldidx > req.oldlen)
962 *retval = req.oldlen;
964 *retval = req.oldidx;
970 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
971 void *new, size_t newlen, size_t *retval)
973 int oid[CTL_MAXNAME];
977 oid[0] = 0; /* sysctl internal magic */
978 oid[1] = 3; /* name2oid */
979 oidlen = sizeof(oid);
981 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
982 strlen(name), &plen);
986 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
987 new, newlen, retval);
992 * Transfer function to/from user space.
995 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1000 if (req->lock == 1 && req->oldptr) {
1001 vslock(req->oldptr, req->oldlen);
1006 if (i > req->oldlen - req->oldidx)
1007 i = req->oldlen - req->oldidx;
1009 error = copyout(p, (char *)req->oldptr + req->oldidx,
1015 if (req->oldptr && i < l)
1021 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1027 if (req->newlen - req->newidx < l)
1029 error = copyin((char *)req->newptr + req->newidx, p, l);
1035 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1036 int *nindx, struct sysctl_req *req)
1038 struct sysctl_oid *oid;
1041 oid = SLIST_FIRST(&sysctl__children);
1043 while (oid && indx < CTL_MAXNAME) {
1044 if (oid->oid_number == name[indx]) {
1046 if (oid->oid_kind & CTLFLAG_NOLOCK)
1048 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1049 if (oid->oid_handler != NULL ||
1057 (struct sysctl_oid_list *)oid->oid_arg1);
1058 } else if (indx == namelen) {
1067 oid = SLIST_NEXT(oid, oid_link);
1074 * Traverse our tree, and find the right node, execute whatever it points
1075 * to, and return the resulting error code.
1079 sysctl_root(SYSCTL_HANDLER_ARGS)
1081 struct thread *td = req->td;
1082 struct proc *p = td ? td->td_proc : NULL;
1083 struct sysctl_oid *oid;
1086 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1090 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1092 * You can't call a sysctl when it's a node, but has
1093 * no handler. Inform the user that it's a node.
1094 * The indx may or may not be the same as namelen.
1096 if (oid->oid_handler == NULL)
1100 /* If writing isn't allowed */
1101 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1102 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1105 /* Most likely only root can write */
1106 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1107 (error = suser_cred(p->p_ucred,
1108 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1111 if (!oid->oid_handler)
1114 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1115 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1118 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1124 __sysctl(struct sysctl_args *uap)
1126 int error, i, name[CTL_MAXNAME];
1129 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1132 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1136 error = userland_sysctl(name, uap->namelen,
1137 uap->old, uap->oldlenp, 0,
1138 uap->new, uap->newlen, &j);
1139 if (error && error != ENOMEM)
1142 i = copyout(&j, uap->oldlenp, sizeof(j));
1150 * This is used from various compatibility syscalls too. That's why name
1151 * must be in kernel space.
1154 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1157 struct sysctl_req req, req2;
1159 bzero(&req, sizeof req);
1163 req.oldlen = *oldlenp;
1165 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1172 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1178 if (!useracc(new, req.newlen, VM_PROT_READ))
1180 req.newlen = newlen;
1184 req.oldfunc = sysctl_old_user;
1185 req.newfunc = sysctl_new_user;
1189 /* XXX this should probably be done in a general way */
1190 while (memlock.sl_lock) {
1191 memlock.sl_want = 1;
1192 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
1193 memlock.sl_locked++;
1195 memlock.sl_lock = 1;
1199 error = sysctl_root(0, name, namelen, &req2);
1200 } while (error == EAGAIN);
1204 vsunlock(req.oldptr, req.oldlen);
1206 memlock.sl_lock = 0;
1208 if (memlock.sl_want) {
1209 memlock.sl_want = 0;
1210 wakeup((caddr_t)&memlock);
1213 if (error && error != ENOMEM)
1217 if (req.oldptr && req.oldidx > req.oldlen)
1218 *retval = req.oldlen;
1220 *retval = req.oldidx;