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.13 2003/11/14 02:54:52 daver 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 * 100 to leave space for pre-assigned oid numbers.
115 if (oidp->oid_number == OID_AUTO) {
116 static int newoid = 100;
117 oidp->oid_number = newoid++;
118 if (newoid == 0x7fffffff)
119 panic("out of oids");
123 * Insert the oid into the parent's list in order.
126 SLIST_FOREACH(p, parent, oid_link) {
127 if (oidp->oid_number < p->oid_number)
132 SLIST_INSERT_AFTER(q, oidp, oid_link);
134 SLIST_INSERT_HEAD(parent, oidp, oid_link);
137 void sysctl_unregister_oid(struct sysctl_oid *oidp)
139 struct sysctl_oid *p;
143 if (oidp->oid_number == OID_AUTO) {
146 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
148 SLIST_REMOVE(oidp->oid_parent, oidp,
149 sysctl_oid, oid_link);
157 * This can happen when a module fails to register and is
158 * being unloaded afterwards. It should not be a panic()
162 printf("%s: failed to unregister sysctl\n", __func__);
165 /* Initialize a new context to keep track of dynamically added sysctls. */
167 sysctl_ctx_init(struct sysctl_ctx_list *c)
177 /* Free the context, and destroy all dynamic oids registered in this context */
179 sysctl_ctx_free(struct sysctl_ctx_list *clist)
181 struct sysctl_ctx_entry *e, *e1;
186 * First perform a "dry run" to check if it's ok to remove oids.
188 * XXX This algorithm is a hack. But I don't know any
189 * XXX better solution for now...
191 TAILQ_FOREACH(e, clist, link) {
192 error = sysctl_remove_oid(e->entry, 0, 0);
197 * Restore deregistered entries, either from the end,
198 * or from the place where error occured.
199 * e contains the entry that was not unregistered
202 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
204 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
206 sysctl_register_oid(e1->entry);
207 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
211 /* Now really delete the entries */
212 e = TAILQ_FIRST(clist);
214 e1 = TAILQ_NEXT(e, link);
215 error = sysctl_remove_oid(e->entry, 1, 0);
217 panic("sysctl_remove_oid: corrupt tree, entry: %s",
219 free(e, M_SYSCTLOID);
225 /* Add an entry to the context */
226 struct sysctl_ctx_entry *
227 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
229 struct sysctl_ctx_entry *e;
231 if (clist == NULL || oidp == NULL)
233 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
235 TAILQ_INSERT_HEAD(clist, e, link);
239 /* Find an entry in the context */
240 struct sysctl_ctx_entry *
241 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
243 struct sysctl_ctx_entry *e;
245 if (clist == NULL || oidp == NULL)
247 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
255 * Delete an entry from the context.
256 * NOTE: this function doesn't free oidp! You have to remove it
257 * with sysctl_remove_oid().
260 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
262 struct sysctl_ctx_entry *e;
264 if (clist == NULL || oidp == NULL)
266 e = sysctl_ctx_entry_find(clist, oidp);
268 TAILQ_REMOVE(clist, e, link);
269 free(e, M_SYSCTLOID);
276 * Remove dynamically created sysctl trees.
277 * oidp - top of the tree to be removed
278 * del - if 0 - just deregister, otherwise free up entries as well
279 * recurse - if != 0 traverse the subtree to be deleted
282 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
284 struct sysctl_oid *p;
289 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
290 printf("can't remove non-dynamic nodes!\n");
294 * WARNING: normal method to do this should be through
295 * sysctl_ctx_free(). Use recursing as the last resort
296 * method to purge your sysctl tree of leftovers...
297 * However, if some other code still references these nodes,
300 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
301 if (oidp->oid_refcnt == 1) {
302 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
305 error = sysctl_remove_oid(p, del, recurse);
310 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
313 if (oidp->oid_refcnt > 1 ) {
316 if (oidp->oid_refcnt == 0) {
317 printf("Warning: bad oid_refcnt=%u (%s)!\n",
318 oidp->oid_refcnt, oidp->oid_name);
321 sysctl_unregister_oid(oidp);
324 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
325 free((void *)(uintptr_t)(const void *)oidp->oid_name,
327 free(oidp, M_SYSCTLOID);
334 * Create new sysctls at run time.
335 * clist may point to a valid context initialized with sysctl_ctx_init().
338 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
339 int number, const char *name, int kind, void *arg1, int arg2,
340 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
342 struct sysctl_oid *oidp;
346 /* You have to hook up somewhere.. */
349 /* Check if the node already exists, otherwise create it */
350 oidp = sysctl_find_oidname(name, parent);
352 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
354 /* Update the context */
356 sysctl_ctx_entry_add(clist, oidp);
359 printf("can't re-use a leaf (%s)!\n", name);
363 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
364 bzero(oidp, sizeof(struct sysctl_oid));
365 oidp->oid_parent = parent;
366 SLIST_NEXT(oidp, oid_link) = NULL;
367 oidp->oid_number = number;
368 oidp->oid_refcnt = 1;
370 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
371 bcopy(name, newname, len + 1);
373 oidp->oid_name = newname;
374 oidp->oid_handler = handler;
375 oidp->oid_kind = CTLFLAG_DYN | kind;
376 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
377 /* Allocate space for children */
378 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
379 M_SYSCTLOID, M_WAITOK);
380 SLIST_INIT(SYSCTL_CHILDREN(oidp));
382 oidp->oid_arg1 = arg1;
383 oidp->oid_arg2 = arg2;
387 int len = strlen(descr) + 1;
388 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
390 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
392 /* Update the context, if used */
394 sysctl_ctx_entry_add(clist, oidp);
395 /* Register this oid */
396 sysctl_register_oid(oidp);
401 * Register the kernel's oids on startup.
403 SET_DECLARE(sysctl_set, struct sysctl_oid);
405 static void sysctl_register_all(void *arg)
407 struct sysctl_oid **oidp;
409 SET_FOREACH(oidp, sysctl_set)
410 sysctl_register_oid(*oidp);
413 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
418 * These functions implement a presently undocumented interface
419 * used by the sysctl program to walk the tree, and get the type
420 * so it can print the value.
421 * This interface is under work and consideration, and should probably
422 * be killed with a big axe by the first person who can find the time.
423 * (be aware though, that the proper interface isn't as obvious as it
424 * may seem, there are various conflicting requirements.
426 * {0,0} printf the entire MIB-tree.
427 * {0,1,...} return the name of the "..." OID.
428 * {0,2,...} return the next OID.
429 * {0,3} return the OID of the name in "new"
430 * {0,4,...} return the kind & format info for the "..." OID.
434 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
437 struct sysctl_oid *oidp;
439 SLIST_FOREACH(oidp, l, oid_link) {
444 printf("%d %s ", oidp->oid_number, oidp->oid_name);
447 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
448 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
450 if (oidp->oid_handler)
453 switch (oidp->oid_kind & CTLTYPE) {
456 if (!oidp->oid_handler) {
457 sysctl_sysctl_debug_dump_node(
458 oidp->oid_arg1, i+2);
461 case CTLTYPE_INT: printf(" Int\n"); break;
462 case CTLTYPE_STRING: printf(" String\n"); break;
463 case CTLTYPE_QUAD: printf(" Quad\n"); break;
464 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
465 default: printf("\n");
472 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
476 error = suser(req->td);
479 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
483 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
484 0, 0, sysctl_sysctl_debug, "-", "");
487 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
489 int *name = (int *) arg1;
490 u_int namelen = arg2;
492 struct sysctl_oid *oid;
493 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
498 snprintf(buf,sizeof(buf),"%d",*name);
500 error = SYSCTL_OUT(req, ".", 1);
502 error = SYSCTL_OUT(req, buf, strlen(buf));
510 SLIST_FOREACH(oid, lsp, oid_link) {
511 if (oid->oid_number != *name)
515 error = SYSCTL_OUT(req, ".", 1);
517 error = SYSCTL_OUT(req, oid->oid_name,
518 strlen(oid->oid_name));
525 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
528 if (oid->oid_handler)
531 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
536 return (SYSCTL_OUT(req, "", 1));
539 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
542 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
543 int *next, int *len, int level, struct sysctl_oid **oidpp)
545 struct sysctl_oid *oidp;
548 SLIST_FOREACH(oidp, lsp, oid_link) {
549 *next = oidp->oid_number;
553 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
555 if (oidp->oid_handler)
556 /* We really should call the handler here...*/
558 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
559 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
560 len, level+1, oidpp))
565 if (oidp->oid_number < *name)
568 if (oidp->oid_number > *name) {
569 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
571 if (oidp->oid_handler)
573 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
574 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
575 next+1, len, level+1, oidpp))
579 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
582 if (oidp->oid_handler)
585 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
586 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
587 len, level+1, oidpp))
599 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
601 int *name = (int *) arg1;
602 u_int namelen = arg2;
604 struct sysctl_oid *oid;
605 struct sysctl_oid_list *lsp = &sysctl__children;
606 int newoid[CTL_MAXNAME];
608 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
611 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
615 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
618 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
621 struct sysctl_oid *oidp;
622 struct sysctl_oid_list *lsp = &sysctl__children;
628 p = name + strlen(name) - 1 ;
634 for (p = name; *p && *p != '.'; p++)
640 oidp = SLIST_FIRST(lsp);
642 while (oidp && *len < CTL_MAXNAME) {
643 if (strcmp(name, oidp->oid_name)) {
644 oidp = SLIST_NEXT(oidp, oid_link);
647 *oid++ = oidp->oid_number;
656 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
659 if (oidp->oid_handler)
662 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
663 oidp = SLIST_FIRST(lsp);
665 for (p = name; *p && *p != '.'; p++)
675 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
678 int error, oid[CTL_MAXNAME], len;
679 struct sysctl_oid *op = 0;
683 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
684 return (ENAMETOOLONG);
686 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
688 error = SYSCTL_IN(req, p, req->newlen);
694 p [req->newlen] = '\0';
696 error = name2oid(p, oid, &len, &op);
703 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
707 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
708 sysctl_sysctl_name2oid, "I", "");
711 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
713 struct sysctl_oid *oid;
716 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
722 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
725 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
730 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
733 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
735 struct sysctl_oid *oid;
738 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
744 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
748 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
751 * Default "handler" functions.
755 * Handle an int, signed or unsigned.
757 * a variable: point arg1 at it.
758 * a constant: pass it in arg2.
762 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
767 error = SYSCTL_OUT(req, arg1, sizeof(int));
769 error = SYSCTL_OUT(req, &arg2, sizeof(int));
771 if (error || !req->newptr)
777 error = SYSCTL_IN(req, arg1, sizeof(int));
782 * Handle a long, signed or unsigned. arg1 points to it.
786 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
792 error = SYSCTL_OUT(req, arg1, sizeof(long));
794 if (error || !req->newptr)
797 error = SYSCTL_IN(req, arg1, sizeof(long));
802 * Handle a quad, signed or unsigned. arg1 points to it.
806 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
812 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
814 if (error || !req->newptr)
817 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
822 * Handle our generic '\0' terminated 'C' string.
824 * a variable string: point arg1 at it, arg2 is max length.
825 * a constant string: point arg1 at it, arg2 is zero.
829 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
833 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
835 if (error || !req->newptr)
838 if ((req->newlen - req->newidx) >= arg2) {
841 arg2 = (req->newlen - req->newidx);
842 error = SYSCTL_IN(req, arg1, arg2);
843 ((char *)arg1)[arg2] = '\0';
850 * Handle any kind of opaque data.
851 * arg1 points to it, arg2 is the size.
855 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
859 error = SYSCTL_OUT(req, arg1, arg2);
861 if (error || !req->newptr)
864 error = SYSCTL_IN(req, arg1, arg2);
870 * Transfer functions to/from kernel space.
871 * XXX: rather untested at this point
874 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
880 if (i > req->oldlen - req->oldidx)
881 i = req->oldlen - req->oldidx;
883 bcopy(p, (char *)req->oldptr + req->oldidx, i);
886 if (req->oldptr && i != l)
892 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
897 if (req->newlen - req->newidx < l)
899 bcopy((char *)req->newptr + req->newidx, p, l);
905 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
908 struct sysctl_req req;
910 bzero(&req, sizeof req);
915 req.oldlen = *oldlenp;
927 req.oldfunc = sysctl_old_kernel;
928 req.newfunc = sysctl_new_kernel;
931 /* XXX this should probably be done in a general way */
932 while (memlock.sl_lock) {
934 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
939 error = sysctl_root(0, name, namelen, &req);
942 vsunlock(req.oldptr, req.oldlen);
946 if (memlock.sl_want) {
948 wakeup((caddr_t)&memlock);
951 if (error && error != ENOMEM)
955 if (req.oldptr && req.oldidx > req.oldlen)
956 *retval = req.oldlen;
958 *retval = req.oldidx;
964 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
965 void *new, size_t newlen, size_t *retval)
967 int oid[CTL_MAXNAME];
971 oid[0] = 0; /* sysctl internal magic */
972 oid[1] = 3; /* name2oid */
973 oidlen = sizeof(oid);
975 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
976 strlen(name), &plen);
980 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
981 new, newlen, retval);
986 * Transfer function to/from user space.
989 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
994 if (req->lock == 1 && req->oldptr) {
995 vslock(req->oldptr, req->oldlen);
1000 if (i > req->oldlen - req->oldidx)
1001 i = req->oldlen - req->oldidx;
1003 error = copyout(p, (char *)req->oldptr + req->oldidx,
1009 if (req->oldptr && i < l)
1015 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1021 if (req->newlen - req->newidx < l)
1023 error = copyin((char *)req->newptr + req->newidx, p, l);
1029 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1030 int *nindx, struct sysctl_req *req)
1032 struct sysctl_oid *oid;
1035 oid = SLIST_FIRST(&sysctl__children);
1037 while (oid && indx < CTL_MAXNAME) {
1038 if (oid->oid_number == name[indx]) {
1040 if (oid->oid_kind & CTLFLAG_NOLOCK)
1042 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1043 if (oid->oid_handler != NULL ||
1051 (struct sysctl_oid_list *)oid->oid_arg1);
1052 } else if (indx == namelen) {
1061 oid = SLIST_NEXT(oid, oid_link);
1068 * Traverse our tree, and find the right node, execute whatever it points
1069 * to, and return the resulting error code.
1073 sysctl_root(SYSCTL_HANDLER_ARGS)
1075 struct thread *td = req->td;
1076 struct proc *p = td ? td->td_proc : NULL;
1077 struct sysctl_oid *oid;
1080 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1084 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1086 * You can't call a sysctl when it's a node, but has
1087 * no handler. Inform the user that it's a node.
1088 * The indx may or may not be the same as namelen.
1090 if (oid->oid_handler == NULL)
1094 /* If writing isn't allowed */
1095 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1096 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1099 /* Most likely only root can write */
1100 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1101 (error = suser_cred(p->p_ucred,
1102 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1105 if (!oid->oid_handler)
1108 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1109 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1112 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1118 __sysctl(struct sysctl_args *uap)
1120 int error, i, name[CTL_MAXNAME];
1123 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1126 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1130 error = userland_sysctl(name, uap->namelen,
1131 uap->old, uap->oldlenp, 0,
1132 uap->new, uap->newlen, &j);
1133 if (error && error != ENOMEM)
1136 i = copyout(&j, uap->oldlenp, sizeof(j));
1144 * This is used from various compatibility syscalls too. That's why name
1145 * must be in kernel space.
1148 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1151 struct sysctl_req req, req2;
1153 bzero(&req, sizeof req);
1157 req.oldlen = *oldlenp;
1159 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1166 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1172 if (!useracc(new, req.newlen, VM_PROT_READ))
1174 req.newlen = newlen;
1178 req.oldfunc = sysctl_old_user;
1179 req.newfunc = sysctl_new_user;
1183 /* XXX this should probably be done in a general way */
1184 while (memlock.sl_lock) {
1185 memlock.sl_want = 1;
1186 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
1187 memlock.sl_locked++;
1189 memlock.sl_lock = 1;
1193 error = sysctl_root(0, name, namelen, &req2);
1194 } while (error == EAGAIN);
1198 vsunlock(req.oldptr, req.oldlen);
1200 memlock.sl_lock = 0;
1202 if (memlock.sl_want) {
1203 memlock.sl_want = 0;
1204 wakeup((caddr_t)&memlock);
1207 if (error && error != ENOMEM)
1211 if (req.oldptr && req.oldidx > req.oldlen)
1212 *retval = req.oldlen;
1214 *retval = req.oldidx;