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.6 2003/06/30 19:50:31 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;
100 * First check if another oid with the same name already
101 * exists in the parent's list.
103 p = sysctl_find_oidname(oidp->oid_name, parent);
105 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
109 printf("can't re-use a leaf (%s)!\n", p->oid_name);
114 * If this oid has a number OID_AUTO, give it a number which
115 * is greater than any current oid. Make sure it is at least
116 * 100 to leave space for pre-assigned oid numbers.
118 if (oidp->oid_number == OID_AUTO) {
119 /* First, find the highest oid in the parent list >99 */
121 SLIST_FOREACH(p, parent, oid_link) {
122 if (p->oid_number > n)
125 oidp->oid_number = n + 1;
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)
146 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
149 /* Initialize a new context to keep track of dynamically added sysctls. */
151 sysctl_ctx_init(struct sysctl_ctx_list *c)
161 /* Free the context, and destroy all dynamic oids registered in this context */
163 sysctl_ctx_free(struct sysctl_ctx_list *clist)
165 struct sysctl_ctx_entry *e, *e1;
170 * First perform a "dry run" to check if it's ok to remove oids.
172 * XXX This algorithm is a hack. But I don't know any
173 * XXX better solution for now...
175 TAILQ_FOREACH(e, clist, link) {
176 error = sysctl_remove_oid(e->entry, 0, 0);
181 * Restore deregistered entries, either from the end,
182 * or from the place where error occured.
183 * e contains the entry that was not unregistered
186 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
188 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
190 sysctl_register_oid(e1->entry);
191 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
195 /* Now really delete the entries */
196 e = TAILQ_FIRST(clist);
198 e1 = TAILQ_NEXT(e, link);
199 error = sysctl_remove_oid(e->entry, 1, 0);
201 panic("sysctl_remove_oid: corrupt tree, entry: %s",
203 free(e, M_SYSCTLOID);
209 /* Add an entry to the context */
210 struct sysctl_ctx_entry *
211 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
213 struct sysctl_ctx_entry *e;
215 if (clist == NULL || oidp == NULL)
217 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
219 TAILQ_INSERT_HEAD(clist, e, link);
223 /* Find an entry in the context */
224 struct sysctl_ctx_entry *
225 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
227 struct sysctl_ctx_entry *e;
229 if (clist == NULL || oidp == NULL)
231 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
239 * Delete an entry from the context.
240 * NOTE: this function doesn't free oidp! You have to remove it
241 * with sysctl_remove_oid().
244 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
246 struct sysctl_ctx_entry *e;
248 if (clist == NULL || oidp == NULL)
250 e = sysctl_ctx_entry_find(clist, oidp);
252 TAILQ_REMOVE(clist, e, link);
253 free(e, M_SYSCTLOID);
260 * Remove dynamically created sysctl trees.
261 * oidp - top of the tree to be removed
262 * del - if 0 - just deregister, otherwise free up entries as well
263 * recurse - if != 0 traverse the subtree to be deleted
266 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
268 struct sysctl_oid *p;
273 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
274 printf("can't remove non-dynamic nodes!\n");
278 * WARNING: normal method to do this should be through
279 * sysctl_ctx_free(). Use recursing as the last resort
280 * method to purge your sysctl tree of leftovers...
281 * However, if some other code still references these nodes,
284 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
285 if (oidp->oid_refcnt == 1) {
286 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
289 error = sysctl_remove_oid(p, del, recurse);
294 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
297 if (oidp->oid_refcnt > 1 ) {
300 if (oidp->oid_refcnt == 0) {
301 printf("Warning: bad oid_refcnt=%u (%s)!\n",
302 oidp->oid_refcnt, oidp->oid_name);
305 sysctl_unregister_oid(oidp);
308 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
309 free((void *)(uintptr_t)(const void *)oidp->oid_name,
311 free(oidp, M_SYSCTLOID);
318 * Create new sysctls at run time.
319 * clist may point to a valid context initialized with sysctl_ctx_init().
322 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
323 int number, const char *name, int kind, void *arg1, int arg2,
324 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
326 struct sysctl_oid *oidp;
330 /* You have to hook up somewhere.. */
333 /* Check if the node already exists, otherwise create it */
334 oidp = sysctl_find_oidname(name, parent);
336 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
338 /* Update the context */
340 sysctl_ctx_entry_add(clist, oidp);
343 printf("can't re-use a leaf (%s)!\n", name);
347 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
348 bzero(oidp, sizeof(struct sysctl_oid));
349 oidp->oid_parent = parent;
350 SLIST_NEXT(oidp, oid_link) = NULL;
351 oidp->oid_number = number;
352 oidp->oid_refcnt = 1;
354 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
355 bcopy(name, newname, len + 1);
357 oidp->oid_name = newname;
358 oidp->oid_handler = handler;
359 oidp->oid_kind = CTLFLAG_DYN | kind;
360 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
361 /* Allocate space for children */
362 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
363 M_SYSCTLOID, M_WAITOK);
364 SLIST_INIT(SYSCTL_CHILDREN(oidp));
366 oidp->oid_arg1 = arg1;
367 oidp->oid_arg2 = arg2;
371 int len = strlen(descr) + 1;
372 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
374 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
376 /* Update the context, if used */
378 sysctl_ctx_entry_add(clist, oidp);
379 /* Register this oid */
380 sysctl_register_oid(oidp);
385 * Bulk-register all the oids in a linker_set.
387 void sysctl_register_set(struct linker_set *lsp)
389 int count = lsp->ls_length;
391 for (i = 0; i < count; i++)
392 sysctl_register_oid((struct sysctl_oid *) lsp->ls_items[i]);
395 void sysctl_unregister_set(struct linker_set *lsp)
397 int count = lsp->ls_length;
399 for (i = 0; i < count; i++)
400 sysctl_unregister_oid((struct sysctl_oid *) lsp->ls_items[i]);
404 * Register the kernel's oids on startup.
406 extern struct linker_set sysctl_set;
408 static void sysctl_register_all(void *arg)
411 sysctl_register_set(&sysctl_set);
414 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
419 * These functions implement a presently undocumented interface
420 * used by the sysctl program to walk the tree, and get the type
421 * so it can print the value.
422 * This interface is under work and consideration, and should probably
423 * be killed with a big axe by the first person who can find the time.
424 * (be aware though, that the proper interface isn't as obvious as it
425 * may seem, there are various conflicting requirements.
427 * {0,0} printf the entire MIB-tree.
428 * {0,1,...} return the name of the "..." OID.
429 * {0,2,...} return the next OID.
430 * {0,3} return the OID of the name in "new"
431 * {0,4,...} return the kind & format info for the "..." OID.
435 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
438 struct sysctl_oid *oidp;
440 SLIST_FOREACH(oidp, l, oid_link) {
445 printf("%d %s ", oidp->oid_number, oidp->oid_name);
448 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
449 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
451 if (oidp->oid_handler)
454 switch (oidp->oid_kind & CTLTYPE) {
457 if (!oidp->oid_handler) {
458 sysctl_sysctl_debug_dump_node(
459 oidp->oid_arg1, i+2);
462 case CTLTYPE_INT: printf(" Int\n"); break;
463 case CTLTYPE_STRING: printf(" String\n"); break;
464 case CTLTYPE_QUAD: printf(" Quad\n"); break;
465 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
466 default: printf("\n");
473 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
477 error = suser(req->td);
480 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
484 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
485 0, 0, sysctl_sysctl_debug, "-", "");
488 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
490 int *name = (int *) arg1;
491 u_int namelen = arg2;
493 struct sysctl_oid *oid;
494 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
499 snprintf(buf,sizeof(buf),"%d",*name);
501 error = SYSCTL_OUT(req, ".", 1);
503 error = SYSCTL_OUT(req, buf, strlen(buf));
511 SLIST_FOREACH(oid, lsp, oid_link) {
512 if (oid->oid_number != *name)
516 error = SYSCTL_OUT(req, ".", 1);
518 error = SYSCTL_OUT(req, oid->oid_name,
519 strlen(oid->oid_name));
526 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
529 if (oid->oid_handler)
532 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
537 return (SYSCTL_OUT(req, "", 1));
540 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
543 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
544 int *next, int *len, int level, struct sysctl_oid **oidpp)
546 struct sysctl_oid *oidp;
549 SLIST_FOREACH(oidp, lsp, oid_link) {
550 *next = oidp->oid_number;
554 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
556 if (oidp->oid_handler)
557 /* We really should call the handler here...*/
559 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
560 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
561 len, level+1, oidpp))
566 if (oidp->oid_number < *name)
569 if (oidp->oid_number > *name) {
570 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
572 if (oidp->oid_handler)
574 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
575 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
576 next+1, len, level+1, oidpp))
580 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
583 if (oidp->oid_handler)
586 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
587 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
588 len, level+1, oidpp))
598 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
600 int *name = (int *) arg1;
601 u_int namelen = arg2;
603 struct sysctl_oid *oid;
604 struct sysctl_oid_list *lsp = &sysctl__children;
605 int newoid[CTL_MAXNAME];
607 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
610 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
614 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
617 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
620 struct sysctl_oid *oidp;
621 struct sysctl_oid_list *lsp = &sysctl__children;
627 p = name + strlen(name) - 1 ;
633 for (p = name; *p && *p != '.'; p++)
639 oidp = SLIST_FIRST(lsp);
641 while (oidp && *len < CTL_MAXNAME) {
642 if (strcmp(name, oidp->oid_name)) {
643 oidp = SLIST_NEXT(oidp, oid_link);
646 *oid++ = oidp->oid_number;
655 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
658 if (oidp->oid_handler)
661 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
662 oidp = SLIST_FIRST(lsp);
664 for (p = name; *p && *p != '.'; p++)
674 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
677 int error, oid[CTL_MAXNAME], len;
678 struct sysctl_oid *op = 0;
682 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
683 return (ENAMETOOLONG);
685 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
687 error = SYSCTL_IN(req, p, req->newlen);
693 p [req->newlen] = '\0';
695 error = name2oid(p, oid, &len, &op);
702 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
706 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
707 sysctl_sysctl_name2oid, "I", "");
710 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
712 struct sysctl_oid *oid;
715 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
721 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
724 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
729 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
732 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
734 struct sysctl_oid *oid;
737 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
743 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
747 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
750 * Default "handler" functions.
754 * Handle an int, signed or unsigned.
756 * a variable: point arg1 at it.
757 * a constant: pass it in arg2.
761 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
766 error = SYSCTL_OUT(req, arg1, sizeof(int));
768 error = SYSCTL_OUT(req, &arg2, sizeof(int));
770 if (error || !req->newptr)
776 error = SYSCTL_IN(req, arg1, sizeof(int));
781 * Handle a long, signed or unsigned. arg1 points to it.
785 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
791 error = SYSCTL_OUT(req, arg1, sizeof(long));
793 if (error || !req->newptr)
796 error = SYSCTL_IN(req, arg1, sizeof(long));
801 * Handle a quad, signed or unsigned. arg1 points to it.
805 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
811 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
813 if (error || !req->newptr)
816 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
821 * Handle our generic '\0' terminated 'C' string.
823 * a variable string: point arg1 at it, arg2 is max length.
824 * a constant string: point arg1 at it, arg2 is zero.
828 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
832 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
834 if (error || !req->newptr)
837 if ((req->newlen - req->newidx) >= arg2) {
840 arg2 = (req->newlen - req->newidx);
841 error = SYSCTL_IN(req, arg1, arg2);
842 ((char *)arg1)[arg2] = '\0';
849 * Handle any kind of opaque data.
850 * arg1 points to it, arg2 is the size.
854 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
858 error = SYSCTL_OUT(req, arg1, arg2);
860 if (error || !req->newptr)
863 error = SYSCTL_IN(req, arg1, arg2);
869 * Transfer functions to/from kernel space.
870 * XXX: rather untested at this point
873 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
879 if (i > req->oldlen - req->oldidx)
880 i = req->oldlen - req->oldidx;
882 bcopy(p, (char *)req->oldptr + req->oldidx, i);
885 if (req->oldptr && i != l)
891 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
896 if (req->newlen - req->newidx < l)
898 bcopy((char *)req->newptr + req->newidx, p, l);
904 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
907 struct sysctl_req req;
909 bzero(&req, sizeof req);
914 req.oldlen = *oldlenp;
926 req.oldfunc = sysctl_old_kernel;
927 req.newfunc = sysctl_new_kernel;
930 /* XXX this should probably be done in a general way */
931 while (memlock.sl_lock) {
933 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
938 error = sysctl_root(0, name, namelen, &req);
941 vsunlock(req.oldptr, req.oldlen);
945 if (memlock.sl_want) {
947 wakeup((caddr_t)&memlock);
950 if (error && error != ENOMEM)
954 if (req.oldptr && req.oldidx > req.oldlen)
955 *retval = req.oldlen;
957 *retval = req.oldidx;
963 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
964 void *new, size_t newlen, size_t *retval)
966 int oid[CTL_MAXNAME];
970 oid[0] = 0; /* sysctl internal magic */
971 oid[1] = 3; /* name2oid */
972 oidlen = sizeof(oid);
974 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
975 strlen(name), &plen);
979 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
980 new, newlen, retval);
985 * Transfer function to/from user space.
988 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
993 if (req->lock == 1 && req->oldptr) {
994 vslock(req->oldptr, req->oldlen);
999 if (i > req->oldlen - req->oldidx)
1000 i = req->oldlen - req->oldidx;
1002 error = copyout(p, (char *)req->oldptr + req->oldidx,
1008 if (req->oldptr && i < l)
1014 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1020 if (req->newlen - req->newidx < l)
1022 error = copyin((char *)req->newptr + req->newidx, p, l);
1028 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1029 int *nindx, struct sysctl_req *req)
1031 struct sysctl_oid *oid;
1034 oid = SLIST_FIRST(&sysctl__children);
1036 while (oid && indx < CTL_MAXNAME) {
1037 if (oid->oid_number == name[indx]) {
1039 if (oid->oid_kind & CTLFLAG_NOLOCK)
1041 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1042 if (oid->oid_handler != NULL ||
1050 (struct sysctl_oid_list *)oid->oid_arg1);
1051 } else if (indx == namelen) {
1060 oid = SLIST_NEXT(oid, oid_link);
1067 * Traverse our tree, and find the right node, execute whatever it points
1068 * to, and return the resulting error code.
1072 sysctl_root(SYSCTL_HANDLER_ARGS)
1074 struct thread *td = req->td;
1075 struct proc *p = td ? td->td_proc : NULL;
1076 struct sysctl_oid *oid;
1079 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1083 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1085 * You can't call a sysctl when it's a node, but has
1086 * no handler. Inform the user that it's a node.
1087 * The indx may or may not be the same as namelen.
1089 if (oid->oid_handler == NULL)
1093 /* If writing isn't allowed */
1094 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1095 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1098 /* Most likely only root can write */
1099 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1100 (error = suser_cred(p->p_ucred,
1101 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1104 if (!oid->oid_handler)
1107 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1108 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1111 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1116 #ifndef _SYS_SYSPROTO_H_
1117 struct sysctl_args {
1128 __sysctl(struct sysctl_args *uap)
1130 int error, i, name[CTL_MAXNAME];
1133 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1136 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1140 error = userland_sysctl(name, uap->namelen,
1141 uap->old, uap->oldlenp, 0,
1142 uap->new, uap->newlen, &j);
1143 if (error && error != ENOMEM)
1146 i = copyout(&j, uap->oldlenp, sizeof(j));
1154 * This is used from various compatibility syscalls too. That's why name
1155 * must be in kernel space.
1158 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1161 struct sysctl_req req, req2;
1163 bzero(&req, sizeof req);
1167 req.oldlen = *oldlenp;
1169 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1176 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1182 if (!useracc(new, req.newlen, VM_PROT_READ))
1184 req.newlen = newlen;
1188 req.oldfunc = sysctl_old_user;
1189 req.newfunc = sysctl_new_user;
1193 /* XXX this should probably be done in a general way */
1194 while (memlock.sl_lock) {
1195 memlock.sl_want = 1;
1196 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
1197 memlock.sl_locked++;
1199 memlock.sl_lock = 1;
1203 error = sysctl_root(0, name, namelen, &req2);
1204 } while (error == EAGAIN);
1208 vsunlock(req.oldptr, req.oldlen);
1210 memlock.sl_lock = 0;
1212 if (memlock.sl_want) {
1213 memlock.sl_want = 0;
1214 wakeup((caddr_t)&memlock);
1217 if (error && error != ENOMEM)
1221 if (req.oldptr && req.oldidx > req.oldlen)
1222 *retval = req.oldlen;
1224 *retval = req.oldidx;
1230 #include <sys/socket.h>
1231 #include <vm/vm_param.h>
1233 #define KINFO_PROC (0<<8)
1234 #define KINFO_RT (1<<8)
1235 #define KINFO_VNODE (2<<8)
1236 #define KINFO_FILE (3<<8)
1237 #define KINFO_METER (4<<8)
1238 #define KINFO_LOADAVG (5<<8)
1239 #define KINFO_CLOCKRATE (6<<8)
1241 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1242 #define KINFO_BSDI_SYSINFO (101<<8)
1245 * XXX this is bloat, but I hope it's better here than on the potentially
1246 * limited kernel stack... -Peter
1250 int bsdi_machine; /* "i386" on BSD/386 */
1251 /* ^^^ this is an offset to the string, relative to the struct start */
1260 int bsdi_ostype; /* "BSD/386" on BSD/386 */
1261 int bsdi_osrelease; /* "1.1" on BSD/386 */
1273 struct timeval pad16;
1274 /* we dont set this, because BSDI's uname used gethostname() instead */
1275 int bsdi_hostname; /* hostname on BSD/386 */
1277 /* the actual string data is appended here */
1281 * this data is appended to the end of the bsdi_si structure during copyout.
1282 * The "char *" offsets are relative to the base of the bsdi_si struct.
1283 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1284 * should not exceed the length of the buffer here... (or else!! :-)
1286 static char bsdi_strings[80]; /* It had better be less than this! */
1288 #ifndef _SYS_SYSPROTO_H_
1289 struct getkerninfo_args {
1298 ogetkerninfo(struct getkerninfo_args *uap)
1303 switch (uap->op & 0xff00) {
1309 name[3] = (uap->op & 0xff0000) >> 16;
1310 name[4] = uap->op & 0xff;
1312 error = userland_sysctl(name, 6, uap->where, uap->size,
1318 name[1] = KERN_VNODE;
1319 error = userland_sysctl(name, 2, uap->where, uap->size,
1325 name[1] = KERN_PROC;
1326 name[2] = uap->op & 0xff;
1328 error = userland_sysctl(name, 4, uap->where, uap->size,
1334 name[1] = KERN_FILE;
1335 error = userland_sysctl(name, 2, uap->where, uap->size,
1342 error = userland_sysctl(name, 2, uap->where, uap->size,
1348 name[1] = VM_LOADAVG;
1349 error = userland_sysctl(name, 2, uap->where, uap->size,
1353 case KINFO_CLOCKRATE:
1355 name[1] = KERN_CLOCKRATE;
1356 error = userland_sysctl(name, 2, uap->where, uap->size,
1360 case KINFO_BSDI_SYSINFO: {
1362 * this is pretty crude, but it's just enough for uname()
1363 * from BSDI's 1.x libc to work.
1365 * In particular, it doesn't return the same results when
1366 * the supplied buffer is too small. BSDI's version apparently
1367 * will return the amount copied, and set the *size to how
1368 * much was needed. The emulation framework here isn't capable
1369 * of that, so we just set both to the amount copied.
1370 * BSDI's 2.x product apparently fails with ENOMEM in this
1378 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1379 bzero(bsdi_strings, sizeof(bsdi_strings));
1383 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1387 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1388 strcpy(s, osrelease);
1391 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1395 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1397 if (uap->where == NULL) {
1398 /* process is asking how much buffer to supply.. */
1405 /* if too much buffer supplied, trim it down */
1409 /* how much of the buffer is remaining */
1412 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1415 /* is there any point in continuing? */
1416 if (left > sizeof(bsdi_si)) {
1417 left -= sizeof(bsdi_si);
1418 error = copyout(&bsdi_strings,
1419 uap->where + sizeof(bsdi_si), left);
1425 return (EOPNOTSUPP);
1429 curproc->p_retval[0] = size;
1431 error = copyout((caddr_t)&size, (caddr_t)uap->size,
1435 #endif /* COMPAT_43 */