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.12 2003/11/10 06:12:13 dillon Exp $
44 #include "opt_compat.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
53 #include <sys/sysproto.h>
55 #include <vm/vm_extern.h>
57 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
58 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
63 static struct sysctl_lock {
69 static int sysctl_root(SYSCTL_HANDLER_ARGS);
71 struct sysctl_oid_list sysctl__children; /* root list */
73 static struct sysctl_oid *
74 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
76 struct sysctl_oid *oidp;
78 SLIST_FOREACH(oidp, list, oid_link) {
79 if (strcmp(oidp->oid_name, name) == 0) {
87 * Initialization of the MIB tree.
89 * Order by number in each list.
92 void sysctl_register_oid(struct sysctl_oid *oidp)
94 struct sysctl_oid_list *parent = oidp->oid_parent;
99 * First check if another oid with the same name already
100 * exists in the parent's list.
102 p = sysctl_find_oidname(oidp->oid_name, parent);
104 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
108 printf("can't re-use a leaf (%s)!\n", p->oid_name);
113 * If this oid has a number OID_AUTO, give it a number which
114 * is greater than any current oid. Make sure it is at least
115 * 100 to leave space for pre-assigned oid numbers.
117 if (oidp->oid_number == OID_AUTO) {
118 static int newoid = 100;
119 oidp->oid_number = newoid++;
120 if (newoid == 0x7fffffff)
121 panic("out of oids");
125 * Insert the oid into the parent's list in order.
128 SLIST_FOREACH(p, parent, oid_link) {
129 if (oidp->oid_number < p->oid_number)
134 SLIST_INSERT_AFTER(q, oidp, oid_link);
136 SLIST_INSERT_HEAD(parent, oidp, oid_link);
139 void sysctl_unregister_oid(struct sysctl_oid *oidp)
141 struct sysctl_oid *p;
145 if (oidp->oid_number == OID_AUTO) {
148 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
150 SLIST_REMOVE(oidp->oid_parent, oidp,
151 sysctl_oid, oid_link);
159 * This can happen when a module fails to register and is
160 * being unloaded afterwards. It should not be a panic()
164 printf("%s: failed to unregister sysctl\n", __func__);
167 /* Initialize a new context to keep track of dynamically added sysctls. */
169 sysctl_ctx_init(struct sysctl_ctx_list *c)
179 /* Free the context, and destroy all dynamic oids registered in this context */
181 sysctl_ctx_free(struct sysctl_ctx_list *clist)
183 struct sysctl_ctx_entry *e, *e1;
188 * First perform a "dry run" to check if it's ok to remove oids.
190 * XXX This algorithm is a hack. But I don't know any
191 * XXX better solution for now...
193 TAILQ_FOREACH(e, clist, link) {
194 error = sysctl_remove_oid(e->entry, 0, 0);
199 * Restore deregistered entries, either from the end,
200 * or from the place where error occured.
201 * e contains the entry that was not unregistered
204 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
206 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
208 sysctl_register_oid(e1->entry);
209 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
213 /* Now really delete the entries */
214 e = TAILQ_FIRST(clist);
216 e1 = TAILQ_NEXT(e, link);
217 error = sysctl_remove_oid(e->entry, 1, 0);
219 panic("sysctl_remove_oid: corrupt tree, entry: %s",
221 free(e, M_SYSCTLOID);
227 /* Add an entry to the context */
228 struct sysctl_ctx_entry *
229 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
231 struct sysctl_ctx_entry *e;
233 if (clist == NULL || oidp == NULL)
235 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
237 TAILQ_INSERT_HEAD(clist, e, link);
241 /* Find an entry in the context */
242 struct sysctl_ctx_entry *
243 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
245 struct sysctl_ctx_entry *e;
247 if (clist == NULL || oidp == NULL)
249 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
257 * Delete an entry from the context.
258 * NOTE: this function doesn't free oidp! You have to remove it
259 * with sysctl_remove_oid().
262 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
264 struct sysctl_ctx_entry *e;
266 if (clist == NULL || oidp == NULL)
268 e = sysctl_ctx_entry_find(clist, oidp);
270 TAILQ_REMOVE(clist, e, link);
271 free(e, M_SYSCTLOID);
278 * Remove dynamically created sysctl trees.
279 * oidp - top of the tree to be removed
280 * del - if 0 - just deregister, otherwise free up entries as well
281 * recurse - if != 0 traverse the subtree to be deleted
284 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
286 struct sysctl_oid *p;
291 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
292 printf("can't remove non-dynamic nodes!\n");
296 * WARNING: normal method to do this should be through
297 * sysctl_ctx_free(). Use recursing as the last resort
298 * method to purge your sysctl tree of leftovers...
299 * However, if some other code still references these nodes,
302 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
303 if (oidp->oid_refcnt == 1) {
304 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
307 error = sysctl_remove_oid(p, del, recurse);
312 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
315 if (oidp->oid_refcnt > 1 ) {
318 if (oidp->oid_refcnt == 0) {
319 printf("Warning: bad oid_refcnt=%u (%s)!\n",
320 oidp->oid_refcnt, oidp->oid_name);
323 sysctl_unregister_oid(oidp);
326 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
327 free((void *)(uintptr_t)(const void *)oidp->oid_name,
329 free(oidp, M_SYSCTLOID);
336 * Create new sysctls at run time.
337 * clist may point to a valid context initialized with sysctl_ctx_init().
340 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
341 int number, const char *name, int kind, void *arg1, int arg2,
342 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
344 struct sysctl_oid *oidp;
348 /* You have to hook up somewhere.. */
351 /* Check if the node already exists, otherwise create it */
352 oidp = sysctl_find_oidname(name, parent);
354 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
356 /* Update the context */
358 sysctl_ctx_entry_add(clist, oidp);
361 printf("can't re-use a leaf (%s)!\n", name);
365 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
366 bzero(oidp, sizeof(struct sysctl_oid));
367 oidp->oid_parent = parent;
368 SLIST_NEXT(oidp, oid_link) = NULL;
369 oidp->oid_number = number;
370 oidp->oid_refcnt = 1;
372 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
373 bcopy(name, newname, len + 1);
375 oidp->oid_name = newname;
376 oidp->oid_handler = handler;
377 oidp->oid_kind = CTLFLAG_DYN | kind;
378 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
379 /* Allocate space for children */
380 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
381 M_SYSCTLOID, M_WAITOK);
382 SLIST_INIT(SYSCTL_CHILDREN(oidp));
384 oidp->oid_arg1 = arg1;
385 oidp->oid_arg2 = arg2;
389 int len = strlen(descr) + 1;
390 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
392 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
394 /* Update the context, if used */
396 sysctl_ctx_entry_add(clist, oidp);
397 /* Register this oid */
398 sysctl_register_oid(oidp);
403 * Register the kernel's oids on startup.
405 SET_DECLARE(sysctl_set, struct sysctl_oid);
407 static void sysctl_register_all(void *arg)
409 struct sysctl_oid **oidp;
411 SET_FOREACH(oidp, sysctl_set)
412 sysctl_register_oid(*oidp);
415 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
420 * These functions implement a presently undocumented interface
421 * used by the sysctl program to walk the tree, and get the type
422 * so it can print the value.
423 * This interface is under work and consideration, and should probably
424 * be killed with a big axe by the first person who can find the time.
425 * (be aware though, that the proper interface isn't as obvious as it
426 * may seem, there are various conflicting requirements.
428 * {0,0} printf the entire MIB-tree.
429 * {0,1,...} return the name of the "..." OID.
430 * {0,2,...} return the next OID.
431 * {0,3} return the OID of the name in "new"
432 * {0,4,...} return the kind & format info for the "..." OID.
436 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
439 struct sysctl_oid *oidp;
441 SLIST_FOREACH(oidp, l, oid_link) {
446 printf("%d %s ", oidp->oid_number, oidp->oid_name);
449 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
450 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
452 if (oidp->oid_handler)
455 switch (oidp->oid_kind & CTLTYPE) {
458 if (!oidp->oid_handler) {
459 sysctl_sysctl_debug_dump_node(
460 oidp->oid_arg1, i+2);
463 case CTLTYPE_INT: printf(" Int\n"); break;
464 case CTLTYPE_STRING: printf(" String\n"); break;
465 case CTLTYPE_QUAD: printf(" Quad\n"); break;
466 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
467 default: printf("\n");
474 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
478 error = suser(req->td);
481 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
485 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
486 0, 0, sysctl_sysctl_debug, "-", "");
489 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
491 int *name = (int *) arg1;
492 u_int namelen = arg2;
494 struct sysctl_oid *oid;
495 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
500 snprintf(buf,sizeof(buf),"%d",*name);
502 error = SYSCTL_OUT(req, ".", 1);
504 error = SYSCTL_OUT(req, buf, strlen(buf));
512 SLIST_FOREACH(oid, lsp, oid_link) {
513 if (oid->oid_number != *name)
517 error = SYSCTL_OUT(req, ".", 1);
519 error = SYSCTL_OUT(req, oid->oid_name,
520 strlen(oid->oid_name));
527 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
530 if (oid->oid_handler)
533 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
538 return (SYSCTL_OUT(req, "", 1));
541 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
544 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
545 int *next, int *len, int level, struct sysctl_oid **oidpp)
547 struct sysctl_oid *oidp;
550 SLIST_FOREACH(oidp, lsp, oid_link) {
551 *next = oidp->oid_number;
555 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
557 if (oidp->oid_handler)
558 /* We really should call the handler here...*/
560 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
561 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
562 len, level+1, oidpp))
567 if (oidp->oid_number < *name)
570 if (oidp->oid_number > *name) {
571 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
573 if (oidp->oid_handler)
575 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
576 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
577 next+1, len, level+1, oidpp))
581 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
584 if (oidp->oid_handler)
587 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
588 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
589 len, level+1, oidpp))
601 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
603 int *name = (int *) arg1;
604 u_int namelen = arg2;
606 struct sysctl_oid *oid;
607 struct sysctl_oid_list *lsp = &sysctl__children;
608 int newoid[CTL_MAXNAME];
610 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
613 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
617 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
620 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
623 struct sysctl_oid *oidp;
624 struct sysctl_oid_list *lsp = &sysctl__children;
630 p = name + strlen(name) - 1 ;
636 for (p = name; *p && *p != '.'; p++)
642 oidp = SLIST_FIRST(lsp);
644 while (oidp && *len < CTL_MAXNAME) {
645 if (strcmp(name, oidp->oid_name)) {
646 oidp = SLIST_NEXT(oidp, oid_link);
649 *oid++ = oidp->oid_number;
658 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
661 if (oidp->oid_handler)
664 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
665 oidp = SLIST_FIRST(lsp);
667 for (p = name; *p && *p != '.'; p++)
677 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
680 int error, oid[CTL_MAXNAME], len;
681 struct sysctl_oid *op = 0;
685 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
686 return (ENAMETOOLONG);
688 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
690 error = SYSCTL_IN(req, p, req->newlen);
696 p [req->newlen] = '\0';
698 error = name2oid(p, oid, &len, &op);
705 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
709 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
710 sysctl_sysctl_name2oid, "I", "");
713 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
715 struct sysctl_oid *oid;
718 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
724 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
727 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
732 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
735 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
737 struct sysctl_oid *oid;
740 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
746 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
750 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
753 * Default "handler" functions.
757 * Handle an int, signed or unsigned.
759 * a variable: point arg1 at it.
760 * a constant: pass it in arg2.
764 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
769 error = SYSCTL_OUT(req, arg1, sizeof(int));
771 error = SYSCTL_OUT(req, &arg2, sizeof(int));
773 if (error || !req->newptr)
779 error = SYSCTL_IN(req, arg1, sizeof(int));
784 * Handle a long, signed or unsigned. arg1 points to it.
788 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
794 error = SYSCTL_OUT(req, arg1, sizeof(long));
796 if (error || !req->newptr)
799 error = SYSCTL_IN(req, arg1, sizeof(long));
804 * Handle a quad, signed or unsigned. arg1 points to it.
808 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
814 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
816 if (error || !req->newptr)
819 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
824 * Handle our generic '\0' terminated 'C' string.
826 * a variable string: point arg1 at it, arg2 is max length.
827 * a constant string: point arg1 at it, arg2 is zero.
831 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
835 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
837 if (error || !req->newptr)
840 if ((req->newlen - req->newidx) >= arg2) {
843 arg2 = (req->newlen - req->newidx);
844 error = SYSCTL_IN(req, arg1, arg2);
845 ((char *)arg1)[arg2] = '\0';
852 * Handle any kind of opaque data.
853 * arg1 points to it, arg2 is the size.
857 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
861 error = SYSCTL_OUT(req, arg1, arg2);
863 if (error || !req->newptr)
866 error = SYSCTL_IN(req, arg1, arg2);
872 * Transfer functions to/from kernel space.
873 * XXX: rather untested at this point
876 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
882 if (i > req->oldlen - req->oldidx)
883 i = req->oldlen - req->oldidx;
885 bcopy(p, (char *)req->oldptr + req->oldidx, i);
888 if (req->oldptr && i != l)
894 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
899 if (req->newlen - req->newidx < l)
901 bcopy((char *)req->newptr + req->newidx, p, l);
907 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
910 struct sysctl_req req;
912 bzero(&req, sizeof req);
917 req.oldlen = *oldlenp;
929 req.oldfunc = sysctl_old_kernel;
930 req.newfunc = sysctl_new_kernel;
933 /* XXX this should probably be done in a general way */
934 while (memlock.sl_lock) {
936 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
941 error = sysctl_root(0, name, namelen, &req);
944 vsunlock(req.oldptr, req.oldlen);
948 if (memlock.sl_want) {
950 wakeup((caddr_t)&memlock);
953 if (error && error != ENOMEM)
957 if (req.oldptr && req.oldidx > req.oldlen)
958 *retval = req.oldlen;
960 *retval = req.oldidx;
966 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
967 void *new, size_t newlen, size_t *retval)
969 int oid[CTL_MAXNAME];
973 oid[0] = 0; /* sysctl internal magic */
974 oid[1] = 3; /* name2oid */
975 oidlen = sizeof(oid);
977 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
978 strlen(name), &plen);
982 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
983 new, newlen, retval);
988 * Transfer function to/from user space.
991 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
996 if (req->lock == 1 && req->oldptr) {
997 vslock(req->oldptr, req->oldlen);
1002 if (i > req->oldlen - req->oldidx)
1003 i = req->oldlen - req->oldidx;
1005 error = copyout(p, (char *)req->oldptr + req->oldidx,
1011 if (req->oldptr && i < l)
1017 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1023 if (req->newlen - req->newidx < l)
1025 error = copyin((char *)req->newptr + req->newidx, p, l);
1031 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1032 int *nindx, struct sysctl_req *req)
1034 struct sysctl_oid *oid;
1037 oid = SLIST_FIRST(&sysctl__children);
1039 while (oid && indx < CTL_MAXNAME) {
1040 if (oid->oid_number == name[indx]) {
1042 if (oid->oid_kind & CTLFLAG_NOLOCK)
1044 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1045 if (oid->oid_handler != NULL ||
1053 (struct sysctl_oid_list *)oid->oid_arg1);
1054 } else if (indx == namelen) {
1063 oid = SLIST_NEXT(oid, oid_link);
1070 * Traverse our tree, and find the right node, execute whatever it points
1071 * to, and return the resulting error code.
1075 sysctl_root(SYSCTL_HANDLER_ARGS)
1077 struct thread *td = req->td;
1078 struct proc *p = td ? td->td_proc : NULL;
1079 struct sysctl_oid *oid;
1082 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1086 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1088 * You can't call a sysctl when it's a node, but has
1089 * no handler. Inform the user that it's a node.
1090 * The indx may or may not be the same as namelen.
1092 if (oid->oid_handler == NULL)
1096 /* If writing isn't allowed */
1097 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1098 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1101 /* Most likely only root can write */
1102 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1103 (error = suser_cred(p->p_ucred,
1104 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1107 if (!oid->oid_handler)
1110 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1111 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1114 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1120 __sysctl(struct sysctl_args *uap)
1122 int error, i, name[CTL_MAXNAME];
1125 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1128 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1132 error = userland_sysctl(name, uap->namelen,
1133 uap->old, uap->oldlenp, 0,
1134 uap->new, uap->newlen, &j);
1135 if (error && error != ENOMEM)
1138 i = copyout(&j, uap->oldlenp, sizeof(j));
1146 * This is used from various compatibility syscalls too. That's why name
1147 * must be in kernel space.
1150 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1153 struct sysctl_req req, req2;
1155 bzero(&req, sizeof req);
1159 req.oldlen = *oldlenp;
1161 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1168 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1174 if (!useracc(new, req.newlen, VM_PROT_READ))
1176 req.newlen = newlen;
1180 req.oldfunc = sysctl_old_user;
1181 req.newfunc = sysctl_new_user;
1185 /* XXX this should probably be done in a general way */
1186 while (memlock.sl_lock) {
1187 memlock.sl_want = 1;
1188 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
1189 memlock.sl_locked++;
1191 memlock.sl_lock = 1;
1195 error = sysctl_root(0, name, namelen, &req2);
1196 } while (error == EAGAIN);
1200 vsunlock(req.oldptr, req.oldlen);
1202 memlock.sl_lock = 0;
1204 if (memlock.sl_want) {
1205 memlock.sl_want = 0;
1206 wakeup((caddr_t)&memlock);
1209 if (error && error != ENOMEM)
1213 if (req.oldptr && req.oldidx > req.oldlen)
1214 *retval = req.oldlen;
1216 *retval = req.oldidx;
1222 #include <sys/socket.h>
1223 #include <vm/vm_param.h>
1225 #define KINFO_PROC (0<<8)
1226 #define KINFO_RT (1<<8)
1227 #define KINFO_VNODE (2<<8)
1228 #define KINFO_FILE (3<<8)
1229 #define KINFO_METER (4<<8)
1230 #define KINFO_LOADAVG (5<<8)
1231 #define KINFO_CLOCKRATE (6<<8)
1233 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1234 #define KINFO_BSDI_SYSINFO (101<<8)
1237 * XXX this is bloat, but I hope it's better here than on the potentially
1238 * limited kernel stack... -Peter
1242 int bsdi_machine; /* "i386" on BSD/386 */
1243 /* ^^^ this is an offset to the string, relative to the struct start */
1252 int bsdi_ostype; /* "BSD/386" on BSD/386 */
1253 int bsdi_osrelease; /* "1.1" on BSD/386 */
1265 struct timeval pad16;
1266 /* we dont set this, because BSDI's uname used gethostname() instead */
1267 int bsdi_hostname; /* hostname on BSD/386 */
1269 /* the actual string data is appended here */
1273 * this data is appended to the end of the bsdi_si structure during copyout.
1274 * The "char *" offsets are relative to the base of the bsdi_si struct.
1275 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1276 * should not exceed the length of the buffer here... (or else!! :-)
1278 static char bsdi_strings[80]; /* It had better be less than this! */
1281 ogetkerninfo(struct getkerninfo_args *uap)
1287 switch (uap->op & 0xff00) {
1293 name[3] = (uap->op & 0xff0000) >> 16;
1294 name[4] = uap->op & 0xff;
1296 error = userland_sysctl(name, 6, uap->where, uap->size,
1302 name[1] = KERN_VNODE;
1303 error = userland_sysctl(name, 2, uap->where, uap->size,
1309 name[1] = KERN_PROC;
1310 name[2] = uap->op & 0xff;
1312 error = userland_sysctl(name, 4, uap->where, uap->size,
1318 name[1] = KERN_FILE;
1319 error = userland_sysctl(name, 2, uap->where, uap->size,
1326 error = userland_sysctl(name, 2, uap->where, uap->size,
1332 name[1] = VM_LOADAVG;
1333 error = userland_sysctl(name, 2, uap->where, uap->size,
1337 case KINFO_CLOCKRATE:
1339 name[1] = KERN_CLOCKRATE;
1340 error = userland_sysctl(name, 2, uap->where, uap->size,
1344 case KINFO_BSDI_SYSINFO: {
1346 * this is pretty crude, but it's just enough for uname()
1347 * from BSDI's 1.x libc to work.
1348 * *size gives the size of the buffer before the call, and
1349 * the amount of data copied after a successful call.
1350 * If successful, the return value is the amount of data
1351 * available, which can be larger than *size.
1353 * BSDI's 2.x product apparently fails with ENOMEM if
1354 * *size is too small.
1360 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1361 bzero(bsdi_strings, sizeof(bsdi_strings));
1365 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1369 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1370 strcpy(s, osrelease);
1373 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1377 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1379 if (uap->where == NULL || (uap->size == NULL)) {
1380 /* process is asking how much buffer to supply.. */
1386 if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1389 /* if too much buffer supplied, trim it down */
1393 /* how much of the buffer is remaining */
1396 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1399 /* is there any point in continuing? */
1400 if (left > sizeof(bsdi_si)) {
1401 left -= sizeof(bsdi_si);
1402 error = copyout(&bsdi_strings,
1403 uap->where + sizeof(bsdi_si), left);
1409 return (EOPNOTSUPP);
1413 uap->sysmsg_result = size;
1415 error = copyout((caddr_t)&size, (caddr_t)uap->size,
1419 #endif /* COMPAT_43 */