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 struct lock sysctllock;
73 static struct lock sysctlmemlock;
75 #define SYSCTL_INIT() lockinit(&sysctllock, \
76 "sysctl lock", 0, LK_CANRECURSE)
77 #define SYSCTL_SLEEP(ch, wmesg, timo) \
78 lksleep(ch, &sysctllock, 0, wmesg, timo)
80 static int sysctl_root(SYSCTL_HANDLER_ARGS);
81 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
82 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
84 struct sysctl_oid_list sysctl__children; /* root list */
86 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
89 static struct sysctl_oid *
90 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list, int lock)
92 struct sysctl_oid *oidp;
94 SLIST_FOREACH(oidp, list, oid_link) {
95 if (strcmp(oidp->oid_name, name) == 0) {
103 * Initialization of the MIB tree.
105 * Order by number in each list.
109 sysctl_register_oid(struct sysctl_oid *oidp)
112 sysctl_register_oid_int(oidp);
117 sysctl_register_oid_int(struct sysctl_oid *oidp)
119 struct sysctl_oid_list *parent = oidp->oid_parent;
120 struct sysctl_oid *p;
121 struct sysctl_oid *q;
124 * First check if another oid with the same name already
125 * exists in the parent's list.
127 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
129 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
132 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
137 * If this oid has a number OID_AUTO, give it a number which
138 * is greater than any current oid. Make sure it is at least
139 * 256 to leave space for pre-assigned oid numbers.
141 if (oidp->oid_number == OID_AUTO) {
142 int newoid = 0x100; /* minimum AUTO oid */
145 * Adjust based on highest oid in parent list
147 SLIST_FOREACH(p, parent, oid_link) {
148 if (newoid <= p->oid_number)
149 newoid = p->oid_number + 1;
151 oidp->oid_number = newoid;
155 * Insert the oid into the parent's list in order.
158 SLIST_FOREACH(p, parent, oid_link) {
159 if (oidp->oid_number < p->oid_number)
164 SLIST_INSERT_AFTER(q, oidp, oid_link);
166 SLIST_INSERT_HEAD(parent, oidp, oid_link);
170 sysctl_unregister_oid(struct sysctl_oid *oidp)
173 sysctl_unregister_oid_int(oidp);
178 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
180 struct sysctl_oid *p;
182 if (oidp->oid_number == OID_AUTO)
183 panic("Trying to unregister OID_AUTO entry: %p", oidp);
185 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
188 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
193 * This can happen when a module fails to register and is
194 * being unloaded afterwards. It should not be a panic()
197 kprintf("%s: failed to unregister sysctl\n", __func__);
200 /* Initialize a new context to keep track of dynamically added sysctls. */
202 sysctl_ctx_init(struct sysctl_ctx_list *c)
210 /* Free the context, and destroy all dynamic oids registered in this context */
212 sysctl_ctx_free(struct sysctl_ctx_list *clist)
214 struct sysctl_ctx_entry *e, *e1;
219 * First perform a "dry run" to check if it's ok to remove oids.
221 * XXX This algorithm is a hack. But I don't know any
222 * XXX better solution for now...
225 TAILQ_FOREACH(e, clist, link) {
226 error = sysctl_remove_oid_locked(e->entry, 0, 0);
231 * Restore deregistered entries, either from the end,
232 * or from the place where error occured.
233 * e contains the entry that was not unregistered
236 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
238 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
240 sysctl_register_oid(e1->entry);
241 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
247 /* Now really delete the entries */
248 e = TAILQ_FIRST(clist);
250 e1 = TAILQ_NEXT(e, link);
251 error = sysctl_remove_oid_locked(e->entry, 1, 0);
253 panic("sysctl_remove_oid: corrupt tree, entry: %s",
255 kfree(e, M_SYSCTLOID);
262 /* Add an entry to the context */
263 struct sysctl_ctx_entry *
264 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
266 struct sysctl_ctx_entry *e;
268 SYSCTL_ASSERT_XLOCKED();
269 if (clist == NULL || oidp == NULL)
271 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
273 TAILQ_INSERT_HEAD(clist, e, link);
277 /* Find an entry in the context */
278 struct sysctl_ctx_entry *
279 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
281 struct sysctl_ctx_entry *e;
283 SYSCTL_ASSERT_XLOCKED();
284 if (clist == NULL || oidp == NULL)
286 TAILQ_FOREACH(e, clist, link) {
294 * Delete an entry from the context.
295 * NOTE: this function doesn't free oidp! You have to remove it
296 * with sysctl_remove_oid().
299 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
301 struct sysctl_ctx_entry *e;
303 if (clist == NULL || oidp == NULL)
306 e = sysctl_ctx_entry_find(clist, oidp);
308 TAILQ_REMOVE(clist, e, link);
310 kfree(e, M_SYSCTLOID);
319 * Remove dynamically created sysctl trees.
320 * oidp - top of the tree to be removed
321 * del - if 0 - just deregister, otherwise free up entries as well
322 * recurse - if != 0 traverse the subtree to be deleted
325 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
330 error = sysctl_remove_oid_locked(oidp, del, recurse);
336 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
338 struct sysctl_oid *p, *tmp;
341 SYSCTL_ASSERT_XLOCKED();
344 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
345 kprintf("can't remove non-dynamic nodes!\n");
349 * WARNING: normal method to do this should be through
350 * sysctl_ctx_free(). Use recursing as the last resort
351 * method to purge your sysctl tree of leftovers...
352 * However, if some other code still references these nodes,
355 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
356 if (oidp->oid_refcnt == 1) {
357 SLIST_FOREACH_MUTABLE(p,
358 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
360 kprintf("Warning: failed attempt to "
361 "remove oid %s with child %s\n",
362 oidp->oid_name, p->oid_name);
365 error = sysctl_remove_oid_locked(p, del,
371 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
374 if (oidp->oid_refcnt > 1 ) {
377 if (oidp->oid_refcnt == 0) {
378 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
379 oidp->oid_refcnt, oidp->oid_name);
382 sysctl_unregister_oid(oidp);
385 * Wait for all threads running the handler to drain.
386 * This preserves the previous behavior when the
387 * sysctl lock was held across a handler invocation,
388 * and is necessary for module unload correctness.
390 while (oidp->oid_running > 0) {
391 oidp->oid_kind |= CTLFLAG_DYING;
392 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
395 kfree(__DECONST(char *, oidp->oid_descr),
397 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
398 kfree(oidp, M_SYSCTLOID);
405 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
406 int del, int recurse)
408 struct sysctl_oid *p, *tmp;
413 SLIST_FOREACH_MUTABLE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
414 if (strcmp(p->oid_name, name) == 0) {
415 error = sysctl_remove_oid_locked(p, del, recurse);
425 * Create new sysctls at run time.
426 * clist may point to a valid context initialized with sysctl_ctx_init().
429 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
430 int number, const char *name, int kind, void *arg1, int arg2,
431 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
433 struct sysctl_oid *oidp;
437 /* You have to hook up somewhere.. */
441 /* Check if the node already exists, otherwise create it */
442 oidp = sysctl_find_oidname(name, parent, 0);
444 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
446 /* Update the context */
448 sysctl_ctx_entry_add(clist, oidp);
452 kprintf("can't re-use a leaf (%s)!\n", name);
457 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK | M_ZERO);
458 oidp->oid_parent = parent;
459 SLIST_NEXT(oidp, oid_link) = NULL;
460 oidp->oid_number = number;
461 oidp->oid_refcnt = 1;
463 newname = kmalloc(len + 1, M_SYSCTLOID, M_WAITOK);
464 bcopy(name, newname, len + 1);
466 oidp->oid_name = newname;
467 oidp->oid_handler = handler;
468 oidp->oid_kind = CTLFLAG_DYN | kind;
469 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
470 struct sysctl_oid_list *children;
472 /* Allocate space for children */
473 children = kmalloc(sizeof(*children), M_SYSCTLOID, M_WAITOK);
474 SYSCTL_SET_CHILDREN(oidp, children);
475 SLIST_INIT(children);
477 oidp->oid_arg1 = arg1;
478 oidp->oid_arg2 = arg2;
482 int len = strlen(descr) + 1;
483 oidp->oid_descr = kmalloc(len, M_SYSCTLOID, M_WAITOK);
484 strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
486 /* Update the context, if used */
488 sysctl_ctx_entry_add(clist, oidp);
489 /* Register this oid */
490 sysctl_register_oid_int(oidp);
496 * Rename an existing oid.
499 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
504 newname = kstrdup(name, M_SYSCTLOID);
506 oldname = __DECONST(char *, oidp->oid_name);
507 oidp->oid_name = newname;
509 kfree(oldname, M_SYSCTLOID);
513 * Register the kernel's oids on startup.
515 SET_DECLARE(sysctl_set, struct sysctl_oid);
518 sysctl_register_all(void *arg)
520 struct sysctl_oid **oidp;
522 lockinit(&sysctlmemlock, "sysctl mem", 0, LK_CANRECURSE);
525 SET_FOREACH(oidp, sysctl_set)
526 sysctl_register_oid(*oidp);
529 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
534 * These functions implement a presently undocumented interface
535 * used by the sysctl program to walk the tree, and get the type
536 * so it can print the value.
537 * This interface is under work and consideration, and should probably
538 * be killed with a big axe by the first person who can find the time.
539 * (be aware though, that the proper interface isn't as obvious as it
540 * may seem, there are various conflicting requirements.
542 * {0,0} kprintf the entire MIB-tree.
543 * {0,1,...} return the name of the "..." OID.
544 * {0,2,...} return the next OID.
545 * {0,3} return the OID of the name in "new"
546 * {0,4,...} return the kind & format info for the "..." OID.
550 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
553 struct sysctl_oid *oidp;
555 SYSCTL_ASSERT_XLOCKED();
556 SLIST_FOREACH(oidp, l, oid_link) {
561 kprintf("%d %s ", oidp->oid_number, oidp->oid_name);
564 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
565 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
567 if (oidp->oid_handler)
568 kprintf(" *Handler");
570 switch (oidp->oid_kind & CTLTYPE) {
573 if (!oidp->oid_handler) {
574 sysctl_sysctl_debug_dump_node(
575 oidp->oid_arg1, i+2);
578 case CTLTYPE_INT: kprintf(" Int\n"); break;
579 case CTLTYPE_STRING: kprintf(" String\n"); break;
580 case CTLTYPE_QUAD: kprintf(" Quad\n"); break;
581 case CTLTYPE_OPAQUE: kprintf(" Opaque/struct\n"); break;
582 default: kprintf("\n");
589 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
593 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
597 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
602 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
603 0, 0, sysctl_sysctl_debug, "-", "");
606 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
608 int *name = (int *) arg1;
609 u_int namelen = arg2;
611 struct sysctl_oid *oid;
612 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
618 ksnprintf(buf, sizeof(buf), "%d", *name);
620 error = SYSCTL_OUT(req, ".", 1);
622 error = SYSCTL_OUT(req, buf, strlen(buf));
630 SLIST_FOREACH(oid, lsp, oid_link) {
631 if (oid->oid_number != *name)
635 error = SYSCTL_OUT(req, ".", 1);
637 error = SYSCTL_OUT(req, oid->oid_name,
638 strlen(oid->oid_name));
645 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
648 if (oid->oid_handler)
651 lsp2 = SYSCTL_CHILDREN(oid);
656 error = SYSCTL_OUT(req, "", 1);
662 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
665 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
666 int *next, int *len, int level, struct sysctl_oid **oidpp)
668 struct sysctl_oid *oidp;
670 SYSCTL_ASSERT_XLOCKED();
672 SLIST_FOREACH(oidp, lsp, oid_link) {
673 *next = oidp->oid_number;
676 if (oidp->oid_kind & CTLFLAG_SKIP)
680 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
682 if (oidp->oid_handler)
683 /* We really should call the handler here...*/
685 lsp = SYSCTL_CHILDREN(oidp);
686 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
687 len, level+1, oidpp))
692 if (oidp->oid_number < *name)
695 if (oidp->oid_number > *name) {
696 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
698 if (oidp->oid_handler)
700 lsp = SYSCTL_CHILDREN(oidp);
701 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
702 next+1, len, level+1, oidpp))
706 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
709 if (oidp->oid_handler)
712 lsp = SYSCTL_CHILDREN(oidp);
713 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
714 len, level+1, oidpp))
725 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
727 int *name = (int *) arg1;
728 u_int namelen = arg2;
730 struct sysctl_oid *oid;
731 struct sysctl_oid_list *lsp = &sysctl__children;
732 int newoid[CTL_MAXNAME];
734 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
737 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
741 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
744 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
746 struct sysctl_oid *oidp;
747 struct sysctl_oid_list *lsp = &sysctl__children;
750 SYSCTL_ASSERT_XLOCKED();
752 for (*len = 0; *len < CTL_MAXNAME;) {
753 p = strsep(&name, ".");
755 oidp = SLIST_FIRST(lsp);
756 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
759 if (strcmp(p, oidp->oid_name) == 0)
762 *oid++ = oidp->oid_number;
765 if (name == NULL || *name == '\0') {
771 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
774 if (oidp->oid_handler)
777 lsp = SYSCTL_CHILDREN(oidp);
783 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
786 int error, oid[CTL_MAXNAME], len;
787 struct sysctl_oid *op = NULL;
791 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
792 return (ENAMETOOLONG);
794 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
796 error = SYSCTL_IN(req, p, req->newlen);
802 p [req->newlen] = '\0';
804 error = name2oid(p, oid, &len, &op);
811 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
815 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
816 sysctl_sysctl_name2oid, "I", "");
819 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
821 struct sysctl_oid *oid;
824 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
830 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
833 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
838 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
841 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
843 struct sysctl_oid *oid;
846 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
852 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
856 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
859 * Default "handler" functions.
863 * Handle an int, signed or unsigned.
865 * a variable: point arg1 at it.
866 * a constant: pass it in arg2.
870 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
875 error = SYSCTL_OUT(req, arg1, sizeof(int));
877 error = SYSCTL_OUT(req, &arg2, sizeof(int));
879 if (error || !req->newptr)
885 error = SYSCTL_IN(req, arg1, sizeof(int));
890 * Handle a long, signed or unsigned. arg1 points to it.
894 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
900 error = SYSCTL_OUT(req, arg1, sizeof(long));
902 if (error || !req->newptr)
905 error = SYSCTL_IN(req, arg1, sizeof(long));
910 * Handle a quad, signed or unsigned. arg1 points to it.
914 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
920 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
922 if (error || !req->newptr)
925 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
930 * Handle our generic '\0' terminated 'C' string.
932 * a variable string: point arg1 at it, arg2 is max length.
933 * a constant string: point arg1 at it, arg2 is zero.
937 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
941 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
943 if (error || !req->newptr)
946 if ((req->newlen - req->newidx) >= arg2) {
949 arg2 = (req->newlen - req->newidx);
950 error = SYSCTL_IN(req, arg1, arg2);
951 ((char *)arg1)[arg2] = '\0';
958 * Handle any kind of opaque data.
959 * arg1 points to it, arg2 is the size.
963 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
967 error = SYSCTL_OUT(req, arg1, arg2);
969 if (error || !req->newptr)
972 error = SYSCTL_IN(req, arg1, arg2);
978 * Transfer functions to/from kernel space.
979 * XXX: rather untested at this point
982 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
988 if (i > req->oldlen - req->oldidx)
989 i = req->oldlen - req->oldidx;
991 bcopy(p, (char *)req->oldptr + req->oldidx, i);
994 if (req->oldptr && i != l)
1000 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1005 if (req->newlen - req->newidx < l)
1007 bcopy((char *)req->newptr + req->newidx, p, l);
1013 kernel_sysctl(int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
1016 struct sysctl_req req;
1018 bzero(&req, sizeof req);
1023 req.oldlen = *oldlenp;
1025 req.validlen = req.oldlen;
1032 req.newlen = newlen;
1036 req.oldfunc = sysctl_old_kernel;
1037 req.newfunc = sysctl_new_kernel;
1039 req.lock = REQ_UNWIRED;
1043 error = sysctl_root(0, name, namelen, &req);
1047 if (req.lock == REQ_WIRED && req.validlen > 0)
1048 vsunlock(req.oldptr, req.validlen);
1051 if (error && error != ENOMEM)
1055 if (req.oldptr && req.oldidx > req.validlen)
1056 *retval = req.validlen;
1058 *retval = req.oldidx;
1064 kernel_sysctlbyname(char *name, void *old, size_t *oldlenp,
1065 void *new, size_t newlen, size_t *retval)
1067 int oid[CTL_MAXNAME];
1068 size_t oidlen, plen;
1071 oid[0] = 0; /* sysctl internal magic */
1072 oid[1] = 3; /* name2oid */
1073 oidlen = sizeof(oid);
1075 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1079 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1080 new, newlen, retval);
1085 * Transfer function to/from user space.
1088 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1094 if (req->lock == 1 && req->oldptr) {
1095 vslock(req->oldptr, req->oldlen);
1101 if (i > req->oldlen - req->oldidx)
1102 i = req->oldlen - req->oldidx;
1104 error = copyout(p, (char *)req->oldptr + req->oldidx,
1110 if (req->oldptr && i < l)
1116 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1122 if (req->newlen - req->newidx < l)
1124 error = copyin((char *)req->newptr + req->newidx, p, l);
1130 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1131 int *nindx, struct sysctl_req *req)
1133 struct sysctl_oid_list *lsp;
1134 struct sysctl_oid *oid;
1137 SYSCTL_ASSERT_XLOCKED();
1138 lsp = &sysctl__children;
1140 while (indx < CTL_MAXNAME) {
1141 SLIST_FOREACH(oid, lsp, oid_link) {
1142 if (oid->oid_number == name[indx])
1149 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1150 if (oid->oid_handler != NULL || indx == namelen) {
1154 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1155 ("%s found DYING node %p", __func__, oid));
1158 lsp = SYSCTL_CHILDREN(oid);
1159 } else if (indx == namelen) {
1163 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1164 ("%s found DYING node %p", __func__, oid));
1174 * Traverse our tree, and find the right node, execute whatever it points
1175 * to, and return the resulting error code.
1179 sysctl_root(SYSCTL_HANDLER_ARGS)
1181 struct thread *td = req->td;
1182 struct proc *p = td ? td->td_proc : NULL;
1183 struct sysctl_oid *oid;
1186 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1190 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1192 * You can't call a sysctl when it's a node, but has
1193 * no handler. Inform the user that it's a node.
1194 * The indx may or may not be the same as namelen.
1196 if (oid->oid_handler == NULL)
1200 /* If writing isn't allowed */
1201 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1202 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1205 /* Most likely only root can write */
1206 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1207 (error = priv_check_cred(td->td_ucred,
1208 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1209 PRIV_SYSCTL_WRITE, 0)))
1212 if (!oid->oid_handler)
1215 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1216 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1219 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1225 sys___sysctl(struct sysctl_args *uap)
1227 int error, i, name[CTL_MAXNAME];
1230 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1233 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1237 error = userland_sysctl(name, uap->namelen,
1238 uap->old, uap->oldlenp, 0,
1239 uap->new, uap->newlen, &j);
1240 if (error && error != ENOMEM)
1243 i = copyout(&j, uap->oldlenp, sizeof(j));
1251 * This is used from various compatibility syscalls too. That's why name
1252 * must be in kernel space.
1255 userland_sysctl(int *name, u_int namelen, void *old,
1256 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1258 int error = 0, memlocked;
1259 struct sysctl_req req;
1261 bzero(&req, sizeof req);
1268 req.oldlen = *oldlenp;
1270 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1275 req.validlen = req.oldlen;
1278 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1284 if (!useracc(new, newlen, VM_PROT_READ))
1286 req.newlen = newlen;
1290 req.oldfunc = sysctl_old_user;
1291 req.newfunc = sysctl_new_user;
1293 req.lock = REQ_UNWIRED;
1297 if (KTRPOINT(curthread, KTR_SYSCTL))
1298 ktrsysctl(name, namelen);
1301 if (req.oldlen > PAGE_SIZE) {
1303 lockmgr(&sysctlmemlock, LK_EXCLUSIVE);
1311 error = sysctl_root(0, name, namelen, &req);
1313 if (error != EAGAIN)
1319 if (req.lock == REQ_WIRED && req.validlen > 0)
1320 vsunlock(req.oldptr, req.validlen);
1323 lockmgr(&sysctlmemlock, LK_RELEASE);
1325 if (error && error != ENOMEM)
1329 if (req.oldptr && req.oldidx > req.validlen)
1330 *retval = req.validlen;
1332 *retval = req.oldidx;
1338 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1342 value = *(int *)arg1;
1343 error = sysctl_handle_int(oidp, &value, 0, req);
1344 if (error || !req->newptr)
1346 if (value < low || value > high)
1348 *(int *)arg1 = value;
1353 * Drain into a sysctl struct. The user buffer should be wired if a page
1354 * fault would cause issue.
1357 sbuf_sysctl_drain(void *arg, const char *data, int len)
1359 struct sysctl_req *req = arg;
1362 error = SYSCTL_OUT(req, data, len);
1363 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1364 return (error == 0 ? len : -error);
1368 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1369 struct sysctl_req *req)
1372 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1373 sbuf_set_drain(s, sbuf_sysctl_drain, req);