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.10 2003/07/26 18:12:44 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 * Bulk-register all the oids in a linker_set.
405 void sysctl_register_set(struct linker_set *lsp)
407 int count = lsp->ls_length;
409 for (i = 0; i < count; i++)
410 sysctl_register_oid((struct sysctl_oid *) lsp->ls_items[i]);
413 void sysctl_unregister_set(struct linker_set *lsp)
415 int count = lsp->ls_length;
417 for (i = 0; i < count; i++)
418 sysctl_unregister_oid((struct sysctl_oid *) lsp->ls_items[i]);
422 * Register the kernel's oids on startup.
424 extern struct linker_set sysctl_set;
426 static void sysctl_register_all(void *arg)
429 sysctl_register_set(&sysctl_set);
432 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
437 * These functions implement a presently undocumented interface
438 * used by the sysctl program to walk the tree, and get the type
439 * so it can print the value.
440 * This interface is under work and consideration, and should probably
441 * be killed with a big axe by the first person who can find the time.
442 * (be aware though, that the proper interface isn't as obvious as it
443 * may seem, there are various conflicting requirements.
445 * {0,0} printf the entire MIB-tree.
446 * {0,1,...} return the name of the "..." OID.
447 * {0,2,...} return the next OID.
448 * {0,3} return the OID of the name in "new"
449 * {0,4,...} return the kind & format info for the "..." OID.
453 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
456 struct sysctl_oid *oidp;
458 SLIST_FOREACH(oidp, l, oid_link) {
463 printf("%d %s ", oidp->oid_number, oidp->oid_name);
466 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
467 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
469 if (oidp->oid_handler)
472 switch (oidp->oid_kind & CTLTYPE) {
475 if (!oidp->oid_handler) {
476 sysctl_sysctl_debug_dump_node(
477 oidp->oid_arg1, i+2);
480 case CTLTYPE_INT: printf(" Int\n"); break;
481 case CTLTYPE_STRING: printf(" String\n"); break;
482 case CTLTYPE_QUAD: printf(" Quad\n"); break;
483 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
484 default: printf("\n");
491 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
495 error = suser(req->td);
498 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
502 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
503 0, 0, sysctl_sysctl_debug, "-", "");
506 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
508 int *name = (int *) arg1;
509 u_int namelen = arg2;
511 struct sysctl_oid *oid;
512 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
517 snprintf(buf,sizeof(buf),"%d",*name);
519 error = SYSCTL_OUT(req, ".", 1);
521 error = SYSCTL_OUT(req, buf, strlen(buf));
529 SLIST_FOREACH(oid, lsp, oid_link) {
530 if (oid->oid_number != *name)
534 error = SYSCTL_OUT(req, ".", 1);
536 error = SYSCTL_OUT(req, oid->oid_name,
537 strlen(oid->oid_name));
544 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
547 if (oid->oid_handler)
550 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
555 return (SYSCTL_OUT(req, "", 1));
558 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
561 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
562 int *next, int *len, int level, struct sysctl_oid **oidpp)
564 struct sysctl_oid *oidp;
567 SLIST_FOREACH(oidp, lsp, oid_link) {
568 *next = oidp->oid_number;
572 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
574 if (oidp->oid_handler)
575 /* We really should call the handler here...*/
577 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
578 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
579 len, level+1, oidpp))
584 if (oidp->oid_number < *name)
587 if (oidp->oid_number > *name) {
588 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
590 if (oidp->oid_handler)
592 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
593 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
594 next+1, len, level+1, oidpp))
598 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
601 if (oidp->oid_handler)
604 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
605 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
606 len, level+1, oidpp))
618 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
620 int *name = (int *) arg1;
621 u_int namelen = arg2;
623 struct sysctl_oid *oid;
624 struct sysctl_oid_list *lsp = &sysctl__children;
625 int newoid[CTL_MAXNAME];
627 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
630 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
634 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
637 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
640 struct sysctl_oid *oidp;
641 struct sysctl_oid_list *lsp = &sysctl__children;
647 p = name + strlen(name) - 1 ;
653 for (p = name; *p && *p != '.'; p++)
659 oidp = SLIST_FIRST(lsp);
661 while (oidp && *len < CTL_MAXNAME) {
662 if (strcmp(name, oidp->oid_name)) {
663 oidp = SLIST_NEXT(oidp, oid_link);
666 *oid++ = oidp->oid_number;
675 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
678 if (oidp->oid_handler)
681 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
682 oidp = SLIST_FIRST(lsp);
684 for (p = name; *p && *p != '.'; p++)
694 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
697 int error, oid[CTL_MAXNAME], len;
698 struct sysctl_oid *op = 0;
702 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
703 return (ENAMETOOLONG);
705 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
707 error = SYSCTL_IN(req, p, req->newlen);
713 p [req->newlen] = '\0';
715 error = name2oid(p, oid, &len, &op);
722 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
726 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
727 sysctl_sysctl_name2oid, "I", "");
730 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
732 struct sysctl_oid *oid;
735 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
741 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
744 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
749 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
752 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
754 struct sysctl_oid *oid;
757 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
763 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
767 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
770 * Default "handler" functions.
774 * Handle an int, signed or unsigned.
776 * a variable: point arg1 at it.
777 * a constant: pass it in arg2.
781 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
786 error = SYSCTL_OUT(req, arg1, sizeof(int));
788 error = SYSCTL_OUT(req, &arg2, sizeof(int));
790 if (error || !req->newptr)
796 error = SYSCTL_IN(req, arg1, sizeof(int));
801 * Handle a long, signed or unsigned. arg1 points to it.
805 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
811 error = SYSCTL_OUT(req, arg1, sizeof(long));
813 if (error || !req->newptr)
816 error = SYSCTL_IN(req, arg1, sizeof(long));
821 * Handle a quad, signed or unsigned. arg1 points to it.
825 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
831 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
833 if (error || !req->newptr)
836 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
841 * Handle our generic '\0' terminated 'C' string.
843 * a variable string: point arg1 at it, arg2 is max length.
844 * a constant string: point arg1 at it, arg2 is zero.
848 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
852 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
854 if (error || !req->newptr)
857 if ((req->newlen - req->newidx) >= arg2) {
860 arg2 = (req->newlen - req->newidx);
861 error = SYSCTL_IN(req, arg1, arg2);
862 ((char *)arg1)[arg2] = '\0';
869 * Handle any kind of opaque data.
870 * arg1 points to it, arg2 is the size.
874 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
878 error = SYSCTL_OUT(req, arg1, arg2);
880 if (error || !req->newptr)
883 error = SYSCTL_IN(req, arg1, arg2);
889 * Transfer functions to/from kernel space.
890 * XXX: rather untested at this point
893 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
899 if (i > req->oldlen - req->oldidx)
900 i = req->oldlen - req->oldidx;
902 bcopy(p, (char *)req->oldptr + req->oldidx, i);
905 if (req->oldptr && i != l)
911 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
916 if (req->newlen - req->newidx < l)
918 bcopy((char *)req->newptr + req->newidx, p, l);
924 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
927 struct sysctl_req req;
929 bzero(&req, sizeof req);
934 req.oldlen = *oldlenp;
946 req.oldfunc = sysctl_old_kernel;
947 req.newfunc = sysctl_new_kernel;
950 /* XXX this should probably be done in a general way */
951 while (memlock.sl_lock) {
953 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
958 error = sysctl_root(0, name, namelen, &req);
961 vsunlock(req.oldptr, req.oldlen);
965 if (memlock.sl_want) {
967 wakeup((caddr_t)&memlock);
970 if (error && error != ENOMEM)
974 if (req.oldptr && req.oldidx > req.oldlen)
975 *retval = req.oldlen;
977 *retval = req.oldidx;
983 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
984 void *new, size_t newlen, size_t *retval)
986 int oid[CTL_MAXNAME];
990 oid[0] = 0; /* sysctl internal magic */
991 oid[1] = 3; /* name2oid */
992 oidlen = sizeof(oid);
994 error = kernel_sysctl(oid, 2, oid, &oidlen, (void *)name,
995 strlen(name), &plen);
999 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1000 new, newlen, retval);
1005 * Transfer function to/from user space.
1008 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1013 if (req->lock == 1 && req->oldptr) {
1014 vslock(req->oldptr, req->oldlen);
1019 if (i > req->oldlen - req->oldidx)
1020 i = req->oldlen - req->oldidx;
1022 error = copyout(p, (char *)req->oldptr + req->oldidx,
1028 if (req->oldptr && i < l)
1034 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1040 if (req->newlen - req->newidx < l)
1042 error = copyin((char *)req->newptr + req->newidx, p, l);
1048 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1049 int *nindx, struct sysctl_req *req)
1051 struct sysctl_oid *oid;
1054 oid = SLIST_FIRST(&sysctl__children);
1056 while (oid && indx < CTL_MAXNAME) {
1057 if (oid->oid_number == name[indx]) {
1059 if (oid->oid_kind & CTLFLAG_NOLOCK)
1061 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1062 if (oid->oid_handler != NULL ||
1070 (struct sysctl_oid_list *)oid->oid_arg1);
1071 } else if (indx == namelen) {
1080 oid = SLIST_NEXT(oid, oid_link);
1087 * Traverse our tree, and find the right node, execute whatever it points
1088 * to, and return the resulting error code.
1092 sysctl_root(SYSCTL_HANDLER_ARGS)
1094 struct thread *td = req->td;
1095 struct proc *p = td ? td->td_proc : NULL;
1096 struct sysctl_oid *oid;
1099 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1103 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1105 * You can't call a sysctl when it's a node, but has
1106 * no handler. Inform the user that it's a node.
1107 * The indx may or may not be the same as namelen.
1109 if (oid->oid_handler == NULL)
1113 /* If writing isn't allowed */
1114 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1115 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1118 /* Most likely only root can write */
1119 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1120 (error = suser_cred(p->p_ucred,
1121 (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1124 if (!oid->oid_handler)
1127 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1128 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1131 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1137 __sysctl(struct sysctl_args *uap)
1139 int error, i, name[CTL_MAXNAME];
1142 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1145 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1149 error = userland_sysctl(name, uap->namelen,
1150 uap->old, uap->oldlenp, 0,
1151 uap->new, uap->newlen, &j);
1152 if (error && error != ENOMEM)
1155 i = copyout(&j, uap->oldlenp, sizeof(j));
1163 * This is used from various compatibility syscalls too. That's why name
1164 * must be in kernel space.
1167 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1170 struct sysctl_req req, req2;
1172 bzero(&req, sizeof req);
1176 req.oldlen = *oldlenp;
1178 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1185 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1191 if (!useracc(new, req.newlen, VM_PROT_READ))
1193 req.newlen = newlen;
1197 req.oldfunc = sysctl_old_user;
1198 req.newfunc = sysctl_new_user;
1202 /* XXX this should probably be done in a general way */
1203 while (memlock.sl_lock) {
1204 memlock.sl_want = 1;
1205 (void) tsleep((caddr_t)&memlock, 0, "sysctl", 0);
1206 memlock.sl_locked++;
1208 memlock.sl_lock = 1;
1212 error = sysctl_root(0, name, namelen, &req2);
1213 } while (error == EAGAIN);
1217 vsunlock(req.oldptr, req.oldlen);
1219 memlock.sl_lock = 0;
1221 if (memlock.sl_want) {
1222 memlock.sl_want = 0;
1223 wakeup((caddr_t)&memlock);
1226 if (error && error != ENOMEM)
1230 if (req.oldptr && req.oldidx > req.oldlen)
1231 *retval = req.oldlen;
1233 *retval = req.oldidx;
1239 #include <sys/socket.h>
1240 #include <vm/vm_param.h>
1242 #define KINFO_PROC (0<<8)
1243 #define KINFO_RT (1<<8)
1244 #define KINFO_VNODE (2<<8)
1245 #define KINFO_FILE (3<<8)
1246 #define KINFO_METER (4<<8)
1247 #define KINFO_LOADAVG (5<<8)
1248 #define KINFO_CLOCKRATE (6<<8)
1250 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1251 #define KINFO_BSDI_SYSINFO (101<<8)
1254 * XXX this is bloat, but I hope it's better here than on the potentially
1255 * limited kernel stack... -Peter
1259 int bsdi_machine; /* "i386" on BSD/386 */
1260 /* ^^^ this is an offset to the string, relative to the struct start */
1269 int bsdi_ostype; /* "BSD/386" on BSD/386 */
1270 int bsdi_osrelease; /* "1.1" on BSD/386 */
1282 struct timeval pad16;
1283 /* we dont set this, because BSDI's uname used gethostname() instead */
1284 int bsdi_hostname; /* hostname on BSD/386 */
1286 /* the actual string data is appended here */
1290 * this data is appended to the end of the bsdi_si structure during copyout.
1291 * The "char *" offsets are relative to the base of the bsdi_si struct.
1292 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1293 * should not exceed the length of the buffer here... (or else!! :-)
1295 static char bsdi_strings[80]; /* It had better be less than this! */
1298 ogetkerninfo(struct getkerninfo_args *uap)
1304 switch (uap->op & 0xff00) {
1310 name[3] = (uap->op & 0xff0000) >> 16;
1311 name[4] = uap->op & 0xff;
1313 error = userland_sysctl(name, 6, uap->where, uap->size,
1319 name[1] = KERN_VNODE;
1320 error = userland_sysctl(name, 2, uap->where, uap->size,
1326 name[1] = KERN_PROC;
1327 name[2] = uap->op & 0xff;
1329 error = userland_sysctl(name, 4, uap->where, uap->size,
1335 name[1] = KERN_FILE;
1336 error = userland_sysctl(name, 2, uap->where, uap->size,
1343 error = userland_sysctl(name, 2, uap->where, uap->size,
1349 name[1] = VM_LOADAVG;
1350 error = userland_sysctl(name, 2, uap->where, uap->size,
1354 case KINFO_CLOCKRATE:
1356 name[1] = KERN_CLOCKRATE;
1357 error = userland_sysctl(name, 2, uap->where, uap->size,
1361 case KINFO_BSDI_SYSINFO: {
1363 * this is pretty crude, but it's just enough for uname()
1364 * from BSDI's 1.x libc to work.
1365 * *size gives the size of the buffer before the call, and
1366 * the amount of data copied after a successful call.
1367 * If successful, the return value is the amount of data
1368 * available, which can be larger than *size.
1370 * BSDI's 2.x product apparently fails with ENOMEM if
1371 * *size is too small.
1377 bzero((char *)&bsdi_si, sizeof(bsdi_si));
1378 bzero(bsdi_strings, sizeof(bsdi_strings));
1382 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1386 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1387 strcpy(s, osrelease);
1390 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1394 needed = sizeof(bsdi_si) + (s - bsdi_strings);
1396 if (uap->where == NULL || (uap->size == NULL)) {
1397 /* process is asking how much buffer to supply.. */
1403 if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1406 /* if too much buffer supplied, trim it down */
1410 /* how much of the buffer is remaining */
1413 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1416 /* is there any point in continuing? */
1417 if (left > sizeof(bsdi_si)) {
1418 left -= sizeof(bsdi_si);
1419 error = copyout(&bsdi_strings,
1420 uap->where + sizeof(bsdi_si), left);
1426 return (EOPNOTSUPP);
1430 uap->lmsg.u.ms_result = size;
1432 error = copyout((caddr_t)&size, (caddr_t)uap->size,
1436 #endif /* COMPAT_43 */