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 $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
51 #include <sys/sysproto.h>
54 #include <sys/mplock2.h>
57 #include <vm/vm_extern.h>
59 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
60 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
62 static struct lock sysctl_lkp;
63 static struct lock sysctl_ctx_lkp;
65 static void sysctl_lock(int type);
66 static void sysctl_unlock(void);
67 static void sysctl_ctx_lock(int type);
68 static void sysctl_ctx_unlock(void);
70 static int sysctl_root(SYSCTL_HANDLER_ARGS);
71 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
72 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
73 static struct sysctl_ctx_entry* sysctl_ctx_entry_find_int
74 (struct sysctl_ctx_list *, struct sysctl_oid *oidp);
76 struct sysctl_oid_list sysctl__children; /* root list */
78 static struct sysctl_oid *
79 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
81 struct sysctl_oid *oidp;
83 SLIST_FOREACH(oidp, list, oid_link) {
84 if (strcmp(oidp->oid_name, name) == 0) {
92 * Initialization of the MIB tree.
94 * Order by number in each list.
98 sysctl_register_oid(struct sysctl_oid *oidp)
100 sysctl_lock(LK_EXCLUSIVE);
101 sysctl_register_oid_int(oidp);
106 sysctl_register_oid_int(struct sysctl_oid *oidp)
108 struct sysctl_oid_list *parent = oidp->oid_parent;
109 struct sysctl_oid *p;
110 struct sysctl_oid *q;
113 * First check if another oid with the same name already
114 * exists in the parent's list.
116 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
118 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
121 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
126 * If this oid has a number OID_AUTO, give it a number which
127 * is greater than any current oid. Make sure it is at least
128 * 256 to leave space for pre-assigned oid numbers.
130 if (oidp->oid_number == OID_AUTO) {
131 int newoid = 0x100; /* minimum AUTO oid */
134 * Adjust based on highest oid in parent list
136 SLIST_FOREACH(p, parent, oid_link) {
137 if (newoid <= p->oid_number)
138 newoid = p->oid_number + 1;
140 oidp->oid_number = newoid;
144 * Insert the oid into the parent's list in order.
147 SLIST_FOREACH(p, parent, oid_link) {
148 if (oidp->oid_number < p->oid_number)
153 SLIST_INSERT_AFTER(q, oidp, oid_link);
155 SLIST_INSERT_HEAD(parent, oidp, oid_link);
159 sysctl_unregister_oid(struct sysctl_oid *oidp)
161 sysctl_lock(LK_EXCLUSIVE);
162 sysctl_unregister_oid_int(oidp);
167 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
169 struct sysctl_oid *p;
171 if (oidp->oid_number == OID_AUTO)
172 panic("Trying to unregister OID_AUTO entry: %p", oidp);
174 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
177 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
182 * This can happen when a module fails to register and is
183 * being unloaded afterwards. It should not be a panic()
186 kprintf("%s: failed to unregister sysctl\n", __func__);
189 /* Initialize a new context to keep track of dynamically added sysctls. */
191 sysctl_ctx_init(struct sysctl_ctx_list *c)
199 /* Free the context, and destroy all dynamic oids registered in this context */
201 sysctl_ctx_free(struct sysctl_ctx_list *clist)
203 struct sysctl_ctx_entry *e, *e1;
207 sysctl_ctx_lock(LK_EXCLUSIVE);
209 * First perform a "dry run" to check if it's ok to remove oids.
211 * XXX This algorithm is a hack. But I don't know any
212 * XXX better solution for now...
214 TAILQ_FOREACH(e, clist, link) {
215 error = sysctl_remove_oid(e->entry, 0, 0);
220 * Restore deregistered entries, either from the end,
221 * or from the place where error occured.
222 * e contains the entry that was not unregistered
225 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
227 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
229 sysctl_register_oid(e1->entry);
230 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
236 /* Now really delete the entries */
237 e = TAILQ_FIRST(clist);
239 e1 = TAILQ_NEXT(e, link);
240 error = sysctl_remove_oid(e->entry, 1, 0);
242 panic("sysctl_remove_oid: corrupt tree, entry: %s",
244 kfree(e, M_SYSCTLOID);
251 /* Add an entry to the context */
252 struct sysctl_ctx_entry *
253 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
255 struct sysctl_ctx_entry *e;
257 if (clist == NULL || oidp == NULL)
259 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
261 sysctl_ctx_lock(LK_EXCLUSIVE);
262 TAILQ_INSERT_HEAD(clist, e, link);
267 /* Find an entry in the context */
268 struct sysctl_ctx_entry *
269 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
271 struct sysctl_ctx_entry *e;
273 if (clist == NULL || oidp == NULL)
276 sysctl_ctx_lock(LK_SHARED);
277 e = sysctl_ctx_entry_find_int(clist, oidp);
283 struct sysctl_ctx_entry *
284 sysctl_ctx_entry_find_int(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
286 struct sysctl_ctx_entry *e;
288 KKASSERT(clist != NULL && oidp != NULL);
290 for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
299 * Delete an entry from the context.
300 * NOTE: this function doesn't free oidp! You have to remove it
301 * with sysctl_remove_oid().
304 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
306 struct sysctl_ctx_entry *e;
308 if (clist == NULL || oidp == NULL)
311 sysctl_ctx_lock(LK_EXCLUSIVE);
312 e = sysctl_ctx_entry_find_int(clist, oidp);
317 TAILQ_REMOVE(clist, e, link);
318 kfree(e, M_SYSCTLOID);
325 * Remove dynamically created sysctl trees.
326 * oidp - top of the tree to be removed
327 * del - if 0 - just deregister, otherwise free up entries as well
328 * recurse - if != 0 traverse the subtree to be deleted
331 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
333 struct sysctl_oid *p;
338 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
339 kprintf("can't remove non-dynamic nodes!\n");
342 sysctl_lock(LK_EXCLUSIVE | LK_CANRECURSE);
344 * WARNING: normal method to do this should be through
345 * sysctl_ctx_free(). Use recursing as the last resort
346 * method to purge your sysctl tree of leftovers...
347 * However, if some other code still references these nodes,
350 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
351 if (oidp->oid_refcnt == 1) {
352 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
357 error = sysctl_remove_oid(p, del, recurse);
364 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
367 if (oidp->oid_refcnt > 1 ) {
370 if (oidp->oid_refcnt == 0) {
371 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
372 oidp->oid_refcnt, oidp->oid_name);
376 sysctl_unregister_oid_int(oidp);
379 kfree(__DECONST(char *,oidp->oid_descr),
381 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
382 kfree(oidp, M_SYSCTLOID);
390 * Create new sysctls at run time.
391 * clist may point to a valid context initialized with sysctl_ctx_init().
394 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
395 int number, const char *name, int kind, void *arg1, int arg2,
396 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
398 struct sysctl_oid *oidp;
402 /* You have to hook up somewhere.. */
405 sysctl_lock(LK_EXCLUSIVE);
406 /* Check if the node already exists, otherwise create it */
407 oidp = sysctl_find_oidname(name, parent, 0);
409 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
411 /* Update the context */
413 sysctl_ctx_entry_add(clist, oidp);
417 kprintf("can't re-use a leaf (%s)!\n", name);
422 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
423 oidp->oid_parent = parent;
424 SLIST_NEXT(oidp, oid_link) = NULL;
425 oidp->oid_number = number;
426 oidp->oid_refcnt = 1;
428 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
429 bcopy(name, newname, len + 1);
431 oidp->oid_name = newname;
432 oidp->oid_handler = handler;
433 oidp->oid_kind = CTLFLAG_DYN | kind;
434 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
435 struct sysctl_oid_list *children;
437 /* Allocate space for children */
438 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
439 SYSCTL_SET_CHILDREN(oidp, children);
440 SLIST_INIT(children);
442 oidp->oid_arg1 = arg1;
443 oidp->oid_arg2 = arg2;
447 int len = strlen(descr) + 1;
448 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
449 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
451 /* Update the context, if used */
453 sysctl_ctx_entry_add(clist, oidp);
454 /* Register this oid */
455 sysctl_register_oid_int(oidp);
461 * Register the kernel's oids on startup.
463 SET_DECLARE(sysctl_set, struct sysctl_oid);
466 sysctl_register_all(void *arg)
468 struct sysctl_oid **oidp;
470 lockinit(&sysctl_lkp, "sysctl", 0, 0);
471 lockinit(&sysctl_ctx_lkp, "sysctl ctx", 0, 0);
472 SET_FOREACH(oidp, sysctl_set)
473 sysctl_register_oid_int(*oidp);
476 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
481 * These functions implement a presently undocumented interface
482 * used by the sysctl program to walk the tree, and get the type
483 * so it can print the value.
484 * This interface is under work and consideration, and should probably
485 * be killed with a big axe by the first person who can find the time.
486 * (be aware though, that the proper interface isn't as obvious as it
487 * may seem, there are various conflicting requirements.
489 * {0,0} kprintf the entire MIB-tree.
490 * {0,1,...} return the name of the "..." OID.
491 * {0,2,...} return the next OID.
492 * {0,3} return the OID of the name in "new"
493 * {0,4,...} return the kind & format info for the "..." OID.
497 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
500 struct sysctl_oid *oidp;
502 sysctl_lock(LK_SHARED);
503 SLIST_FOREACH(oidp, l, oid_link) {
508 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
511 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
512 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
514 if (oidp->oid_handler)
515 kprintf(" *Handler");
517 switch (oidp->oid_kind & CTLTYPE) {
520 if (!oidp->oid_handler) {
521 sysctl_sysctl_debug_dump_node(
522 oidp->oid_arg1, i+2);
525 case CTLTYPE_INT: kprintf(" Int\n"); break;
526 case CTLTYPE_STRING: kprintf(" String\n"); break;
527 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
528 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
529 default: kprintf("\n");
537 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
541 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
544 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
548 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
549 0, 0, sysctl_sysctl_debug, "-", "");
552 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
554 int *name = (int *) arg1;
555 u_int namelen = arg2;
557 struct sysctl_oid *oid;
558 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
561 sysctl_lock(LK_SHARED);
564 ksnprintf(buf, sizeof(buf), "%d", *name);
566 error = SYSCTL_OUT(req, ".", 1);
568 error = SYSCTL_OUT(req, buf, strlen(buf));
578 SLIST_FOREACH(oid, lsp, oid_link) {
579 if (oid->oid_number != *name)
583 error = SYSCTL_OUT(req, ".", 1);
585 error = SYSCTL_OUT(req, oid->oid_name,
586 strlen(oid->oid_name));
595 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
598 if (oid->oid_handler)
601 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
607 return (SYSCTL_OUT(req, "", 1));
610 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
613 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
614 int *next, int *len, int level, struct sysctl_oid **oidpp)
616 struct sysctl_oid *oidp;
619 sysctl_lock(LK_SHARED);
620 SLIST_FOREACH(oidp, lsp, oid_link) {
621 *next = oidp->oid_number;
625 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
629 if (oidp->oid_handler) {
630 /* We really should call the handler here...*/
634 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
635 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
636 len, level+1, oidpp)) {
643 if (oidp->oid_number < *name)
646 if (oidp->oid_number > *name) {
647 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
651 if (oidp->oid_handler) {
655 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
656 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
657 next+1, len, level+1, oidpp)) {
663 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
666 if (oidp->oid_handler)
669 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
670 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
671 len, level+1, oidpp)) {
686 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
688 int *name = (int *) arg1;
689 u_int namelen = arg2;
691 struct sysctl_oid *oid;
692 struct sysctl_oid_list *lsp = &sysctl__children;
693 int newoid[CTL_MAXNAME];
695 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
698 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
702 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
705 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
708 struct sysctl_oid *oidp;
709 struct sysctl_oid_list *lsp = &sysctl__children;
715 p = name + strlen(name) - 1 ;
721 for (p = name; *p && *p != '.'; p++)
727 sysctl_lock(LK_SHARED);
728 oidp = SLIST_FIRST(lsp);
730 while (oidp && *len < CTL_MAXNAME) {
731 if (strcmp(name, oidp->oid_name)) {
732 oidp = SLIST_NEXT(oidp, oid_link);
735 *oid++ = oidp->oid_number;
745 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
748 if (oidp->oid_handler)
751 lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
752 oidp = SLIST_FIRST(lsp);
754 for (p = name; *p && *p != '.'; p++)
765 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
768 int error, oid[CTL_MAXNAME], len;
769 struct sysctl_oid *op = NULL;
773 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
774 return (ENAMETOOLONG);
776 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
778 error = SYSCTL_IN(req, p, req->newlen);
784 p [req->newlen] = '\0';
786 error = name2oid(p, oid, &len, &op);
793 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
797 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
798 sysctl_sysctl_name2oid, "I", "");
801 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
803 struct sysctl_oid *oid;
806 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
812 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
815 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
820 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
823 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
825 struct sysctl_oid *oid;
828 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
834 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
838 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
841 * Default "handler" functions.
845 * Handle an int, signed or unsigned.
847 * a variable: point arg1 at it.
848 * a constant: pass it in arg2.
852 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
857 error = SYSCTL_OUT(req, arg1, sizeof(int));
859 error = SYSCTL_OUT(req, &arg2, sizeof(int));
861 if (error || !req->newptr)
867 error = SYSCTL_IN(req, arg1, sizeof(int));
872 * Handle a long, signed or unsigned. arg1 points to it.
876 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
882 error = SYSCTL_OUT(req, arg1, sizeof(long));
884 if (error || !req->newptr)
887 error = SYSCTL_IN(req, arg1, sizeof(long));
892 * Handle a quad, signed or unsigned. arg1 points to it.
896 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
902 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
904 if (error || !req->newptr)
907 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
912 * Handle our generic '\0' terminated 'C' string.
914 * a variable string: point arg1 at it, arg2 is max length.
915 * a constant string: point arg1 at it, arg2 is zero.
919 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
923 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
925 if (error || !req->newptr)
928 if ((req->newlen - req->newidx) >= arg2) {
931 arg2 = (req->newlen - req->newidx);
932 error = SYSCTL_IN(req, arg1, arg2);
933 ((char *)arg1)[arg2] = '\0';
940 * Handle any kind of opaque data.
941 * arg1 points to it, arg2 is the size.
945 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
949 error = SYSCTL_OUT(req, arg1, arg2);
951 if (error || !req->newptr)
954 error = SYSCTL_IN(req, arg1, arg2);
960 * Transfer functions to/from kernel space.
961 * XXX: rather untested at this point
964 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
970 if (i > req->oldlen - req->oldidx)
971 i = req->oldlen - req->oldidx;
973 bcopy(p, (char *)req->oldptr + req->oldidx, i);
976 if (req->oldptr && i != l)
982 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
987 if (req->newlen - req->newidx < l)
989 bcopy((char *)req->newptr + req->newidx, p, l);
995 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
998 struct sysctl_req req;
1000 bzero(&req, sizeof req);
1005 req.oldlen = *oldlenp;
1013 req.newlen = newlen;
1017 req.oldfunc = sysctl_old_kernel;
1018 req.newfunc = sysctl_new_kernel;
1023 sysctl_lock(LK_SHARED);
1025 error = sysctl_root(0, name, namelen, &req);
1029 vsunlock(req.oldptr, req.oldlen);
1034 if (error && error != ENOMEM)
1038 if (req.oldptr && req.oldidx > req.oldlen)
1039 *retval = req.oldlen;
1041 *retval = req.oldidx;
1047 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1048 void *new, size_t newlen, size_t *retval)
1050 int oid[CTL_MAXNAME];
1051 size_t oidlen, plen;
1054 oid[0] = 0; /* sysctl internal magic */
1055 oid[1] = 3; /* name2oid */
1056 oidlen = sizeof(oid);
1058 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1062 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1063 new, newlen, retval);
1068 * Transfer function to/from user space.
1071 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1077 if (req->lock == 1 && req->oldptr) {
1078 vslock(req->oldptr, req->oldlen);
1084 if (i > req->oldlen - req->oldidx)
1085 i = req->oldlen - req->oldidx;
1087 error = copyout(p, (char *)req->oldptr + req->oldidx,
1093 if (req->oldptr && i < l)
1099 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1105 if (req->newlen - req->newidx < l)
1107 error = copyin((char *)req->newptr + req->newidx, p, l);
1113 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1114 int *nindx, struct sysctl_req *req)
1116 struct sysctl_oid *oid;
1119 sysctl_lock(LK_SHARED);
1120 oid = SLIST_FIRST(&sysctl__children);
1122 while (oid && indx < CTL_MAXNAME) {
1123 if (oid->oid_number == name[indx]) {
1125 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1126 if (oid->oid_handler != NULL ||
1135 (struct sysctl_oid_list *)oid->oid_arg1);
1136 } else if (indx == namelen) {
1147 oid = SLIST_NEXT(oid, oid_link);
1155 * Traverse our tree, and find the right node, execute whatever it points
1156 * to, and return the resulting error code.
1160 sysctl_root(SYSCTL_HANDLER_ARGS)
1162 struct thread *td = req->td;
1163 struct proc *p = td ? td->td_proc : NULL;
1164 struct sysctl_oid *oid;
1167 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1171 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1173 * You can't call a sysctl when it's a node, but has
1174 * no handler. Inform the user that it's a node.
1175 * The indx may or may not be the same as namelen.
1177 if (oid->oid_handler == NULL)
1181 /* If writing isn't allowed */
1182 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1183 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1186 /* Most likely only root can write */
1187 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1188 (error = priv_check_cred(td->td_ucred,
1189 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1190 PRIV_SYSCTL_WRITE, 0)))
1193 if (!oid->oid_handler)
1196 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1197 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1200 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1209 sys___sysctl(struct sysctl_args *uap)
1211 int error, i, name[CTL_MAXNAME];
1214 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1217 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1221 error = userland_sysctl(name, uap->namelen,
1222 uap->old, uap->oldlenp, 0,
1223 uap->new, uap->newlen, &j);
1224 if (error && error != ENOMEM)
1227 i = copyout(&j, uap->oldlenp, sizeof(j));
1235 * This is used from various compatibility syscalls too. That's why name
1236 * must be in kernel space.
1239 userland_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1242 struct sysctl_req req, req2;
1244 bzero(&req, sizeof req);
1248 req.oldlen = *oldlenp;
1250 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1257 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1263 if (!useracc(new, newlen, VM_PROT_READ))
1265 req.newlen = newlen;
1269 req.oldfunc = sysctl_old_user;
1270 req.newfunc = sysctl_new_user;
1276 sysctl_lock(LK_SHARED);
1280 error = sysctl_root(0, name, namelen, &req2);
1281 } while (error == EAGAIN);
1286 vsunlock(req.oldptr, req.oldlen);
1291 if (error && error != ENOMEM)
1295 if (req.oldptr && req.oldidx > req.oldlen)
1296 *retval = req.oldlen;
1298 *retval = req.oldidx;
1304 sysctl_lock(int flag)
1306 lockmgr(&sysctl_lkp, flag);
1312 lockmgr(&sysctl_lkp, LK_RELEASE);
1316 sysctl_ctx_lock(int flag)
1318 lockmgr(&sysctl_ctx_lkp, flag);
1322 sysctl_ctx_unlock(void)
1324 lockmgr(&sysctl_ctx_lkp, LK_RELEASE);
1328 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1332 value = *(int *)arg1;
1333 error = sysctl_handle_int(oidp, &value, 0, req);
1334 if (error || !req->newptr)
1336 if (value < low || value > high)
1338 *(int *)arg1 = value;