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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
36 * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.92.2.9 2003/05/01 22:48:09 trhodes Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
47 #include <sys/sysproto.h>
51 #include <sys/mplock2.h>
54 #include <vm/vm_extern.h>
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
57 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
60 * The sysctllock protects the MIB tree. It also protects sysctl
61 * contexts used with dynamic sysctls. The sysctl_register_oid() and
62 * sysctl_unregister_oid() routines require the sysctllock to already
63 * be held, so the sysctl_lock() and sysctl_unlock() routines are
64 * provided for the few places in the kernel which need to use that
65 * API rather than using the dynamic API. Use of the dynamic API is
66 * strongly encouraged for most code.
68 * The sysctlmemlock is used to limit the amount of user memory wired for
69 * sysctl requests. This is implemented by serializing any userland
70 * sysctl requests larger than a single page via an exclusive lock.
72 static struct lock sysctllock;
73 static struct lock sysctlmemlock;
75 #define SYSCTL_XLOCK() lockmgr(&sysctllock, LK_EXCLUSIVE)
76 #define SYSCTL_XUNLOCK() lockmgr(&sysctllock, LK_RELEASE)
77 #define SYSCTL_ASSERT_XLOCKED() KKASSERT(lockstatus(&sysctllock, curthread) != 0)
78 #define SYSCTL_INIT() lockinit(&sysctllock, \
79 "sysctl lock", 0, LK_CANRECURSE)
80 #define SYSCTL_SLEEP(ch, wmesg, timo) \
81 lksleep(ch, &sysctllock, 0, wmesg, timo)
83 static int sysctl_root(SYSCTL_HANDLER_ARGS);
84 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
85 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
87 struct sysctl_oid_list sysctl__children; /* root list */
89 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
92 static struct sysctl_oid *
93 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
95 struct sysctl_oid *oidp;
97 SLIST_FOREACH(oidp, list, oid_link) {
98 if (strcmp(oidp->oid_name, name) == 0) {
106 * Initialization of the MIB tree.
108 * Order by number in each list.
112 sysctl_register_oid(struct sysctl_oid *oidp)
115 sysctl_register_oid_int(oidp);
120 sysctl_register_oid_int(struct sysctl_oid *oidp)
122 struct sysctl_oid_list *parent = oidp->oid_parent;
123 struct sysctl_oid *p;
124 struct sysctl_oid *q;
127 * First check if another oid with the same name already
128 * exists in the parent's list.
130 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
132 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
135 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
140 * If this oid has a number OID_AUTO, give it a number which
141 * is greater than any current oid. Make sure it is at least
142 * 256 to leave space for pre-assigned oid numbers.
144 if (oidp->oid_number == OID_AUTO) {
145 int newoid = 0x100; /* minimum AUTO oid */
148 * Adjust based on highest oid in parent list
150 SLIST_FOREACH(p, parent, oid_link) {
151 if (newoid <= p->oid_number)
152 newoid = p->oid_number + 1;
154 oidp->oid_number = newoid;
158 * Insert the oid into the parent's list in order.
161 SLIST_FOREACH(p, parent, oid_link) {
162 if (oidp->oid_number < p->oid_number)
167 SLIST_INSERT_AFTER(q, oidp, oid_link);
169 SLIST_INSERT_HEAD(parent, oidp, oid_link);
173 sysctl_unregister_oid(struct sysctl_oid *oidp)
176 sysctl_unregister_oid_int(oidp);
181 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
183 struct sysctl_oid *p;
185 if (oidp->oid_number == OID_AUTO)
186 panic("Trying to unregister OID_AUTO entry: %p", oidp);
188 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
191 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
196 * This can happen when a module fails to register and is
197 * being unloaded afterwards. It should not be a panic()
200 kprintf("%s: failed to unregister sysctl\n", __func__);
203 /* Initialize a new context to keep track of dynamically added sysctls. */
205 sysctl_ctx_init(struct sysctl_ctx_list *c)
213 /* Free the context, and destroy all dynamic oids registered in this context */
215 sysctl_ctx_free(struct sysctl_ctx_list *clist)
217 struct sysctl_ctx_entry *e, *e1;
222 * First perform a "dry run" to check if it's ok to remove oids.
224 * XXX This algorithm is a hack. But I don't know any
225 * XXX better solution for now...
228 TAILQ_FOREACH(e, clist, link) {
229 error = sysctl_remove_oid_locked(e->entry, 0, 0);
234 * Restore deregistered entries, either from the end,
235 * or from the place where error occured.
236 * e contains the entry that was not unregistered
239 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
241 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
243 sysctl_register_oid(e1->entry);
244 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
250 /* Now really delete the entries */
251 e = TAILQ_FIRST(clist);
253 e1 = TAILQ_NEXT(e, link);
254 error = sysctl_remove_oid_locked(e->entry, 1, 0);
256 panic("sysctl_remove_oid: corrupt tree, entry: %s",
258 kfree(e, M_SYSCTLOID);
265 /* Add an entry to the context */
266 struct sysctl_ctx_entry *
267 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
269 struct sysctl_ctx_entry *e;
271 SYSCTL_ASSERT_XLOCKED();
272 if (clist == NULL || oidp == NULL)
274 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
276 TAILQ_INSERT_HEAD(clist, e, link);
280 /* Find an entry in the context */
281 struct sysctl_ctx_entry *
282 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
284 struct sysctl_ctx_entry *e;
286 SYSCTL_ASSERT_XLOCKED();
287 if (clist == NULL || oidp == NULL)
289 TAILQ_FOREACH(e, clist, link) {
297 * Delete an entry from the context.
298 * NOTE: this function doesn't free oidp! You have to remove it
299 * with sysctl_remove_oid().
302 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
304 struct sysctl_ctx_entry *e;
306 if (clist == NULL || oidp == NULL)
309 e = sysctl_ctx_entry_find(clist, oidp);
311 TAILQ_REMOVE(clist, e, link);
313 kfree(e, M_SYSCTLOID);
322 * Remove dynamically created sysctl trees.
323 * oidp - top of the tree to be removed
324 * del - if 0 - just deregister, otherwise free up entries as well
325 * recurse - if != 0 traverse the subtree to be deleted
328 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
333 error = sysctl_remove_oid_locked(oidp, del, recurse);
339 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
341 struct sysctl_oid *p, *tmp;
344 SYSCTL_ASSERT_XLOCKED();
347 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
348 kprintf("can't remove non-dynamic nodes!\n");
352 * WARNING: normal method to do this should be through
353 * sysctl_ctx_free(). Use recursing as the last resort
354 * method to purge your sysctl tree of leftovers...
355 * However, if some other code still references these nodes,
358 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
359 if (oidp->oid_refcnt == 1) {
360 SLIST_FOREACH_MUTABLE(p,
361 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
363 kprintf("Warning: failed attempt to "
364 "remove oid %s with child %s\n",
365 oidp->oid_name, p->oid_name);
368 error = sysctl_remove_oid_locked(p, del,
374 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
377 if (oidp->oid_refcnt > 1 ) {
380 if (oidp->oid_refcnt == 0) {
381 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
382 oidp->oid_refcnt, oidp->oid_name);
385 sysctl_unregister_oid(oidp);
388 * Wait for all threads running the handler to drain.
389 * This preserves the previous behavior when the
390 * sysctl lock was held across a handler invocation,
391 * and is necessary for module unload correctness.
393 while (oidp->oid_running > 0) {
394 oidp->oid_kind |= CTLFLAG_DYING;
395 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
398 kfree(__DECONST(char *, oidp->oid_descr),
400 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
401 kfree(oidp, M_SYSCTLOID);
408 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
409 int del, int recurse)
411 struct sysctl_oid *p, *tmp;
416 SLIST_FOREACH_MUTABLE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
417 if (strcmp(p->oid_name, name) == 0) {
418 error = sysctl_remove_oid_locked(p, del, recurse);
428 * Create new sysctls at run time.
429 * clist may point to a valid context initialized with sysctl_ctx_init().
432 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
433 int number, const char *name, int kind, void *arg1, int arg2,
434 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
436 struct sysctl_oid *oidp;
440 /* You have to hook up somewhere.. */
444 /* Check if the node already exists, otherwise create it */
445 oidp = sysctl_find_oidname(name, parent, 0);
447 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
449 /* Update the context */
451 sysctl_ctx_entry_add(clist, oidp);
455 kprintf("can't re-use a leaf (%s)!\n", name);
460 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
461 oidp->oid_parent = parent;
462 SLIST_NEXT(oidp, oid_link) = NULL;
463 oidp->oid_number = number;
464 oidp->oid_refcnt = 1;
466 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
467 bcopy(name, newname, len + 1);
469 oidp->oid_name = newname;
470 oidp->oid_handler = handler;
471 oidp->oid_kind = CTLFLAG_DYN | kind;
472 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
473 struct sysctl_oid_list *children;
475 /* Allocate space for children */
476 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
477 SYSCTL_SET_CHILDREN(oidp, children);
478 SLIST_INIT(children);
480 oidp->oid_arg1 = arg1;
481 oidp->oid_arg2 = arg2;
485 int len = strlen(descr) + 1;
486 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
487 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
489 /* Update the context, if used */
491 sysctl_ctx_entry_add(clist, oidp);
492 /* Register this oid */
493 sysctl_register_oid_int(oidp);
499 * Rename an existing oid.
502 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
507 newname = kstrdup(name, M_SYSCTLOID);
509 oldname = __DECONST(char *, oidp->oid_name);
510 oidp->oid_name = newname;
512 kfree(oldname, M_SYSCTLOID);
516 * Register the kernel's oids on startup.
518 SET_DECLARE(sysctl_set, struct sysctl_oid);
521 sysctl_register_all(void *arg)
523 struct sysctl_oid **oidp;
525 lockinit(&sysctlmemlock, "sysctl mem", 0, LK_CANRECURSE);
528 SET_FOREACH(oidp, sysctl_set)
529 sysctl_register_oid(*oidp);
532 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
537 * These functions implement a presently undocumented interface
538 * used by the sysctl program to walk the tree, and get the type
539 * so it can print the value.
540 * This interface is under work and consideration, and should probably
541 * be killed with a big axe by the first person who can find the time.
542 * (be aware though, that the proper interface isn't as obvious as it
543 * may seem, there are various conflicting requirements.
545 * {0,0} kprintf the entire MIB-tree.
546 * {0,1,...} return the name of the "..." OID.
547 * {0,2,...} return the next OID.
548 * {0,3} return the OID of the name in "new"
549 * {0,4,...} return the kind & format info for the "..." OID.
553 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
556 struct sysctl_oid *oidp;
558 SYSCTL_ASSERT_XLOCKED();
559 SLIST_FOREACH(oidp, l, oid_link) {
564 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
567 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
568 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
570 if (oidp->oid_handler)
571 kprintf(" *Handler");
573 switch (oidp->oid_kind & CTLTYPE) {
576 if (!oidp->oid_handler) {
577 sysctl_sysctl_debug_dump_node(
578 oidp->oid_arg1, i+2);
581 case CTLTYPE_INT: kprintf(" Int\n"); break;
582 case CTLTYPE_STRING: kprintf(" String\n"); break;
583 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
584 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
585 default: kprintf("\n");
593 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
597 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
601 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
606 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
607 0, 0, sysctl_sysctl_debug, "-", "");
610 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
612 int *name = (int *) arg1;
613 u_int namelen = arg2;
615 struct sysctl_oid *oid;
616 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
622 ksnprintf(buf, sizeof(buf), "%d", *name);
624 error = SYSCTL_OUT(req, ".", 1);
626 error = SYSCTL_OUT(req, buf, strlen(buf));
634 SLIST_FOREACH(oid, lsp, oid_link) {
635 if (oid->oid_number != *name)
639 error = SYSCTL_OUT(req, ".", 1);
641 error = SYSCTL_OUT(req, oid->oid_name,
642 strlen(oid->oid_name));
649 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
652 if (oid->oid_handler)
655 lsp2 = SYSCTL_CHILDREN(oid);
660 error = SYSCTL_OUT(req, "", 1);
666 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
669 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
670 int *next, int *len, int level, struct sysctl_oid **oidpp)
672 struct sysctl_oid *oidp;
674 SYSCTL_ASSERT_XLOCKED();
676 SLIST_FOREACH(oidp, lsp, oid_link) {
677 *next = oidp->oid_number;
680 if (oidp->oid_kind & CTLFLAG_SKIP)
684 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
686 if (oidp->oid_handler)
687 /* We really should call the handler here...*/
689 lsp = SYSCTL_CHILDREN(oidp);
690 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
691 len, level+1, oidpp))
696 if (oidp->oid_number < *name)
699 if (oidp->oid_number > *name) {
700 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
702 if (oidp->oid_handler)
704 lsp = SYSCTL_CHILDREN(oidp);
705 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
706 next+1, len, level+1, oidpp))
710 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
713 if (oidp->oid_handler)
716 lsp = SYSCTL_CHILDREN(oidp);
717 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
718 len, level+1, oidpp))
729 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
731 int *name = (int *) arg1;
732 u_int namelen = arg2;
734 struct sysctl_oid *oid;
735 struct sysctl_oid_list *lsp = &sysctl__children;
736 int newoid[CTL_MAXNAME];
738 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
741 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
745 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
748 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
750 struct sysctl_oid *oidp;
751 struct sysctl_oid_list *lsp = &sysctl__children;
754 SYSCTL_ASSERT_XLOCKED();
756 for (*len = 0; *len < CTL_MAXNAME;) {
757 p = strsep(&name, ".");
759 oidp = SLIST_FIRST(lsp);
760 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
763 if (strcmp(p, oidp->oid_name) == 0)
766 *oid++ = oidp->oid_number;
769 if (name == NULL || *name == '\0') {
775 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
778 if (oidp->oid_handler)
781 lsp = SYSCTL_CHILDREN(oidp);
787 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
790 int error, oid[CTL_MAXNAME], len;
791 struct sysctl_oid *op = NULL;
795 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
796 return (ENAMETOOLONG);
798 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
800 error = SYSCTL_IN(req, p, req->newlen);
806 p [req->newlen] = '\0';
808 error = name2oid(p, oid, &len, &op);
815 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
819 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
820 sysctl_sysctl_name2oid, "I", "");
823 sysctl_sysctl_oidfmt(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_kind, sizeof(oid->oid_kind));
837 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
842 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
845 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
847 struct sysctl_oid *oid;
850 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
856 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
860 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
863 * Default "handler" functions.
867 * Handle an int, signed or unsigned.
869 * a variable: point arg1 at it.
870 * a constant: pass it in arg2.
874 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
879 error = SYSCTL_OUT(req, arg1, sizeof(int));
881 error = SYSCTL_OUT(req, &arg2, sizeof(int));
883 if (error || !req->newptr)
889 error = SYSCTL_IN(req, arg1, sizeof(int));
894 * Handle a long, signed or unsigned. arg1 points to it.
898 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
904 error = SYSCTL_OUT(req, arg1, sizeof(long));
906 if (error || !req->newptr)
909 error = SYSCTL_IN(req, arg1, sizeof(long));
914 * Handle a quad, signed or unsigned. arg1 points to it.
918 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
924 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
926 if (error || !req->newptr)
929 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
934 * Handle our generic '\0' terminated 'C' string.
936 * a variable string: point arg1 at it, arg2 is max length.
937 * a constant string: point arg1 at it, arg2 is zero.
941 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
945 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
947 if (error || !req->newptr)
950 if ((req->newlen - req->newidx) >= arg2) {
953 arg2 = (req->newlen - req->newidx);
954 error = SYSCTL_IN(req, arg1, arg2);
955 ((char *)arg1)[arg2] = '\0';
962 * Handle any kind of opaque data.
963 * arg1 points to it, arg2 is the size.
967 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
971 error = SYSCTL_OUT(req, arg1, arg2);
973 if (error || !req->newptr)
976 error = SYSCTL_IN(req, arg1, arg2);
982 * Transfer functions to/from kernel space.
983 * XXX: rather untested at this point
986 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
992 if (i > req->oldlen - req->oldidx)
993 i = req->oldlen - req->oldidx;
995 bcopy(p, (char *)req->oldptr + req->oldidx, i);
998 if (req->oldptr && i != l)
1004 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1009 if (req->newlen - req->newidx < l)
1011 bcopy((char *)req->newptr + req->newidx, p, l);
1017 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
1020 struct sysctl_req req;
1022 bzero(&req, sizeof req);
1027 req.oldlen = *oldlenp;
1029 req.validlen = req.oldlen;
1036 req.newlen = newlen;
1040 req.oldfunc = sysctl_old_kernel;
1041 req.newfunc = sysctl_new_kernel;
1043 req.lock = REQ_UNWIRED;
1047 error = sysctl_root(0, name, namelen, &req);
1051 if (req.lock == REQ_WIRED && req.validlen > 0)
1052 vsunlock(req.oldptr, req.validlen);
1055 if (error && error != ENOMEM)
1059 if (req.oldptr && req.oldidx > req.validlen)
1060 *retval = req.validlen;
1062 *retval = req.oldidx;
1068 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1069 void *new, size_t newlen, size_t *retval)
1071 int oid[CTL_MAXNAME];
1072 size_t oidlen, plen;
1075 oid[0] = 0; /* sysctl internal magic */
1076 oid[1] = 3; /* name2oid */
1077 oidlen = sizeof(oid);
1079 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1083 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1084 new, newlen, retval);
1089 * Transfer function to/from user space.
1092 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1098 if (req->lock == 1 && req->oldptr) {
1099 vslock(req->oldptr, req->oldlen);
1105 if (i > req->oldlen - req->oldidx)
1106 i = req->oldlen - req->oldidx;
1108 error = copyout(p, (char *)req->oldptr + req->oldidx,
1114 if (req->oldptr && i < l)
1120 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1126 if (req->newlen - req->newidx < l)
1128 error = copyin((char *)req->newptr + req->newidx, p, l);
1134 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1135 int *nindx, struct sysctl_req *req)
1137 struct sysctl_oid_list *lsp;
1138 struct sysctl_oid *oid;
1141 SYSCTL_ASSERT_XLOCKED();
1142 lsp = &sysctl__children;
1144 while (indx < CTL_MAXNAME) {
1145 SLIST_FOREACH(oid, lsp, oid_link) {
1146 if (oid->oid_number == name[indx])
1153 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1154 if (oid->oid_handler != NULL || indx == namelen) {
1158 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1159 ("%s found DYING node %p", __func__, oid));
1162 lsp = SYSCTL_CHILDREN(oid);
1163 } else if (indx == namelen) {
1167 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1168 ("%s found DYING node %p", __func__, oid));
1178 * Traverse our tree, and find the right node, execute whatever it points
1179 * to, and return the resulting error code.
1183 sysctl_root(SYSCTL_HANDLER_ARGS)
1185 struct thread *td = req->td;
1186 struct proc *p = td ? td->td_proc : NULL;
1187 struct sysctl_oid *oid;
1190 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1194 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1196 * You can't call a sysctl when it's a node, but has
1197 * no handler. Inform the user that it's a node.
1198 * The indx may or may not be the same as namelen.
1200 if (oid->oid_handler == NULL)
1204 /* If writing isn't allowed */
1205 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1206 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1209 /* Most likely only root can write */
1210 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1211 (error = priv_check_cred(td->td_ucred,
1212 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1213 PRIV_SYSCTL_WRITE, 0)))
1216 if (!oid->oid_handler)
1219 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1220 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1223 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1229 sys___sysctl(struct sysctl_args *uap)
1231 int error, i, name[CTL_MAXNAME];
1234 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1237 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1241 error = userland_sysctl(name, uap->namelen,
1242 uap->old, uap->oldlenp, 0,
1243 uap->new, uap->newlen, &j);
1244 if (error && error != ENOMEM)
1247 i = copyout(&j, uap->oldlenp, sizeof(j));
1255 * This is used from various compatibility syscalls too. That's why name
1256 * must be in kernel space.
1259 userland_sysctl(int *name, u_int namelen, void *old,
1260 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1262 int error = 0, memlocked;
1263 struct sysctl_req req;
1265 bzero(&req, sizeof req);
1272 req.oldlen = *oldlenp;
1274 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1279 req.validlen = req.oldlen;
1282 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1288 if (!useracc(new, newlen, VM_PROT_READ))
1290 req.newlen = newlen;
1294 req.oldfunc = sysctl_old_user;
1295 req.newfunc = sysctl_new_user;
1297 req.lock = REQ_UNWIRED;
1301 if (KTRPOINT(curthread, KTR_SYSCTL))
1302 ktrsysctl(name, namelen);
1305 if (req.oldlen > PAGE_SIZE) {
1307 lockmgr(&sysctlmemlock, LK_EXCLUSIVE);
1315 error = sysctl_root(0, name, namelen, &req);
1317 if (error != EAGAIN)
1323 if (req.lock == REQ_WIRED && req.validlen > 0)
1324 vsunlock(req.oldptr, req.validlen);
1327 lockmgr(&sysctlmemlock, LK_RELEASE);
1329 if (error && error != ENOMEM)
1333 if (req.oldptr && req.oldidx > req.validlen)
1334 *retval = req.validlen;
1336 *retval = req.oldidx;
1342 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1346 value = *(int *)arg1;
1347 error = sysctl_handle_int(oidp, &value, 0, req);
1348 if (error || !req->newptr)
1350 if (value < low || value > high)
1352 *(int *)arg1 = value;
1357 * Drain into a sysctl struct. The user buffer should be wired if a page
1358 * fault would cause issue.
1361 sbuf_sysctl_drain(void *arg, const char *data, int len)
1363 struct sysctl_req *req = arg;
1366 error = SYSCTL_OUT(req, data, len);
1367 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1368 return (error == 0 ? len : -error);
1372 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1373 struct sysctl_req *req)
1376 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1377 sbuf_set_drain(s, sbuf_sysctl_drain, req);