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>
52 #include <vm/vm_extern.h>
54 #include "opt_sysctl.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.
69 static int sysctl_root(SYSCTL_HANDLER_ARGS);
70 static void sysctl_register_oid_int(struct sysctl_oid *oipd);
71 static void sysctl_unregister_oid_int(struct sysctl_oid *oipd);
73 struct sysctl_oid_list sysctl__children; /* root list */
75 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
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)
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 * Finish initialization from sysctl_set or add.
115 lockinit(&oidp->oid_lock, "oidlk", 0, LK_CANRECURSE);
118 * First check if another oid with the same name already
119 * exists in the parent's list.
121 p = sysctl_find_oidname(oidp->oid_name, parent, 0);
123 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE)
126 kprintf("can't re-use a leaf (%s)!\n", p->oid_name);
131 * If this oid has a number OID_AUTO, give it a number which
132 * is greater than any current oid. Make sure it is at least
133 * 256 to leave space for pre-assigned oid numbers.
135 if (oidp->oid_number == OID_AUTO) {
136 int newoid = 0x100; /* minimum AUTO oid */
139 * Adjust based on highest oid in parent list
141 SLIST_FOREACH(p, parent, oid_link) {
142 if (newoid <= p->oid_number)
143 newoid = p->oid_number + 1;
145 oidp->oid_number = newoid;
149 * Insert the oid into the parent's list in order.
152 SLIST_FOREACH(p, parent, oid_link) {
153 if (oidp->oid_number < p->oid_number)
158 SLIST_INSERT_AFTER(q, oidp, oid_link);
160 SLIST_INSERT_HEAD(parent, oidp, oid_link);
164 sysctl_unregister_oid(struct sysctl_oid *oidp)
167 sysctl_unregister_oid_int(oidp);
172 sysctl_unregister_oid_int(struct sysctl_oid *oidp)
174 struct sysctl_oid *p;
176 if (oidp->oid_number == OID_AUTO)
177 panic("Trying to unregister OID_AUTO entry: %p", oidp);
179 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
182 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
187 * This can happen when a module fails to register and is
188 * being unloaded afterwards. It should not be a panic()
191 kprintf("%s: failed to unregister sysctl\n", __func__);
194 /* Initialize a new context to keep track of dynamically added sysctls. */
196 sysctl_ctx_init(struct sysctl_ctx_list *c)
204 /* Free the context, and destroy all dynamic oids registered in this context */
206 sysctl_ctx_free(struct sysctl_ctx_list *clist)
208 struct sysctl_ctx_entry *e, *e1;
213 * First perform a "dry run" to check if it's ok to remove oids.
215 * XXX This algorithm is a hack. But I don't know any
216 * XXX better solution for now...
219 TAILQ_FOREACH(e, clist, link) {
220 error = sysctl_remove_oid_locked(e->entry, 0, 0);
225 * Restore deregistered entries, either from the end,
226 * or from the place where error occured.
227 * e contains the entry that was not unregistered
230 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
232 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
234 sysctl_register_oid(e1->entry);
235 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
241 /* Now really delete the entries */
242 e = TAILQ_FIRST(clist);
244 e1 = TAILQ_NEXT(e, link);
245 error = sysctl_remove_oid_locked(e->entry, 1, 0);
247 panic("sysctl_remove_oid: corrupt tree, entry: %s",
249 kfree(e, M_SYSCTLOID);
256 /* Add an entry to the context */
257 struct sysctl_ctx_entry *
258 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
260 struct sysctl_ctx_entry *e;
262 SYSCTL_ASSERT_LOCKED();
263 if (clist == NULL || oidp == NULL)
265 e = kmalloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
267 TAILQ_INSERT_HEAD(clist, e, link);
271 /* Find an entry in the context */
272 struct sysctl_ctx_entry *
273 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
275 struct sysctl_ctx_entry *e;
277 SYSCTL_ASSERT_LOCKED();
278 if (clist == NULL || oidp == NULL)
280 TAILQ_FOREACH(e, clist, link) {
288 * Delete an entry from the context.
289 * NOTE: this function doesn't free oidp! You have to remove it
290 * with sysctl_remove_oid().
293 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
295 struct sysctl_ctx_entry *e;
297 if (clist == NULL || oidp == NULL)
300 e = sysctl_ctx_entry_find(clist, oidp);
302 TAILQ_REMOVE(clist, e, link);
304 kfree(e, M_SYSCTLOID);
313 * Remove dynamically created sysctl trees.
314 * oidp - top of the tree to be removed
315 * del - if 0 - just deregister, otherwise free up entries as well
316 * recurse - if != 0 traverse the subtree to be deleted
319 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
324 error = sysctl_remove_oid_locked(oidp, del, recurse);
330 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
332 struct sysctl_oid *p, *tmp;
335 SYSCTL_ASSERT_LOCKED();
338 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
339 kprintf("can't remove non-dynamic nodes!\n");
343 * WARNING: normal method to do this should be through
344 * sysctl_ctx_free(). Use recursing as the last resort
345 * method to purge your sysctl tree of leftovers...
346 * However, if some other code still references these nodes,
349 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
350 if (oidp->oid_refcnt == 1) {
351 SLIST_FOREACH_MUTABLE(p,
352 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
354 kprintf("Warning: failed attempt to "
355 "remove oid %s with child %s\n",
356 oidp->oid_name, p->oid_name);
359 error = sysctl_remove_oid_locked(p, del,
365 kfree(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
368 if (oidp->oid_refcnt > 1 ) {
371 if (oidp->oid_refcnt == 0) {
372 kprintf("Warning: bad oid_refcnt=%u (%s)!\n",
373 oidp->oid_refcnt, oidp->oid_name);
376 sysctl_unregister_oid(oidp);
379 * Wait for all threads running the handler to drain.
380 * This preserves the previous behavior when the
381 * sysctl lock was held across a handler invocation,
382 * and is necessary for module unload correctness.
384 while (oidp->oid_running > 0) {
385 oidp->oid_kind |= CTLFLAG_DYING;
386 tsleep_interlock(&oidp->oid_running, 0);
388 tsleep(&oidp->oid_running, PINTERLOCKED,
393 kfree(__DECONST(char *, oidp->oid_descr),
395 kfree(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
396 lockuninit(&oidp->oid_lock);
397 kfree(oidp, M_SYSCTLOID);
404 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
405 int del, int recurse)
407 struct sysctl_oid *p, *tmp;
412 SLIST_FOREACH_MUTABLE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
413 if (strcmp(p->oid_name, name) == 0) {
414 error = sysctl_remove_oid_locked(p, del, recurse);
424 * Create new sysctls at run time.
425 * clist may point to a valid context initialized with sysctl_ctx_init().
428 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
429 int number, const char *name, int kind, void *arg1, int arg2,
430 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
432 struct sysctl_oid *oidp;
436 /* You have to hook up somewhere.. */
440 /* Check if the node already exists, otherwise create it */
441 oidp = sysctl_find_oidname(name, parent, 0);
443 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
445 /* Update the context */
447 sysctl_ctx_entry_add(clist, oidp);
451 kprintf("can't re-use a leaf (%s)!\n", name);
456 oidp = kmalloc(sizeof(struct sysctl_oid), M_SYSCTLOID,
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;
523 SET_FOREACH(oidp, sysctl_set)
524 sysctl_register_oid(*oidp);
527 SYSINIT(sysctl, SI_BOOT1_POST, SI_ORDER_ANY, sysctl_register_all, 0);
533 * These functions implement a presently undocumented interface
534 * used by the sysctl program to walk the tree, and get the type
535 * so it can print the value.
536 * This interface is under work and consideration, and should probably
537 * be killed with a big axe by the first person who can find the time.
538 * (be aware though, that the proper interface isn't as obvious as it
539 * may seem, there are various conflicting requirements.
541 * {CTL_SYSCTL, CTL_SYSCTL_DEBUG} kprintf the entire MIB-tree.
542 * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...} return the name of the "..."
544 * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...} return the next OID.
545 * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID} return the OID of the name in
547 * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...} return the kind & format info
549 * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...} return the description of the
554 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
557 struct sysctl_oid *oidp;
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);
591 kprintf(" u_long\n");
594 kprintf(" String\n");
597 kprintf(" int8_t\n");
600 kprintf(" int16_t\n");
603 kprintf(" int32_t\n");
606 kprintf(" int64_t\n");
609 kprintf(" uint8_t\n");
612 kprintf(" uint16_t\n");
615 kprintf(" uint32_t\n");
618 kprintf(" uint64_t\n");
620 case CTLTYPE_BIT32(0):
623 case CTLTYPE_BIT64(0):
627 kprintf(" Opaque/struct\n");
638 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
642 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
645 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
650 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD,
651 0, 0, sysctl_sysctl_debug, "-", "");
652 #endif /* SYSCTL_DEBUG */
655 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
657 int *name = (int *) arg1;
658 u_int namelen = arg2;
660 struct sysctl_oid *oid;
661 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
666 ksnprintf(buf, sizeof(buf), "%d", *name);
668 error = SYSCTL_OUT(req, ".", 1);
670 error = SYSCTL_OUT(req, buf, strlen(buf));
678 SLIST_FOREACH(oid, lsp, oid_link) {
679 if (oid->oid_number != *name)
683 error = SYSCTL_OUT(req, ".", 1);
685 error = SYSCTL_OUT(req, oid->oid_name,
686 strlen(oid->oid_name));
693 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
696 if (oid->oid_handler)
699 lsp2 = SYSCTL_CHILDREN(oid);
704 error = SYSCTL_OUT(req, "", 1);
709 SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD | CTLFLAG_NOLOCK,
710 sysctl_sysctl_name, "");
713 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
714 int *next, int *len, int level, struct sysctl_oid **oidpp)
716 struct sysctl_oid *oidp;
719 SLIST_FOREACH(oidp, lsp, oid_link) {
720 *next = oidp->oid_number;
723 if (oidp->oid_kind & CTLFLAG_SKIP)
727 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
729 if (oidp->oid_handler)
730 /* We really should call the handler here...*/
732 lsp = SYSCTL_CHILDREN(oidp);
733 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
734 len, level+1, oidpp))
739 if (oidp->oid_number < *name)
742 if (oidp->oid_number > *name) {
743 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
745 if (oidp->oid_handler)
747 lsp = SYSCTL_CHILDREN(oidp);
748 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
749 next+1, len, level+1, oidpp))
753 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
756 if (oidp->oid_handler)
759 lsp = SYSCTL_CHILDREN(oidp);
760 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
761 len, level+1, oidpp))
772 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
774 int *name = (int *) arg1;
775 u_int namelen = arg2;
777 struct sysctl_oid *oid;
778 struct sysctl_oid_list *lsp = &sysctl__children;
779 int newoid[CTL_MAXNAME];
781 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
784 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
789 SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD | CTLFLAG_NOLOCK,
790 sysctl_sysctl_next, "");
793 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
795 struct sysctl_oid *oidp;
796 struct sysctl_oid_list *lsp = &sysctl__children;
799 SYSCTL_ASSERT_LOCKED();
801 for (*len = 0; *len < CTL_MAXNAME;) {
802 p = strsep(&name, ".");
804 oidp = SLIST_FIRST(lsp);
805 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
808 if (strcmp(p, oidp->oid_name) == 0)
811 *oid++ = oidp->oid_number;
814 if (name == NULL || *name == '\0') {
820 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
823 if (oidp->oid_handler)
826 lsp = SYSCTL_CHILDREN(oidp);
832 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
835 int error, oid[CTL_MAXNAME], len;
836 struct sysctl_oid *op = NULL;
840 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
841 return (ENAMETOOLONG);
843 p = kmalloc(req->newlen+1, M_SYSCTL, M_WAITOK);
845 error = SYSCTL_IN(req, p, req->newlen);
851 p [req->newlen] = '\0';
853 error = name2oid(p, oid, &len, &op);
860 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
864 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid,
865 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
866 0, 0, sysctl_sysctl_name2oid, "I", "");
869 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
871 struct sysctl_oid *oid;
874 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
880 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
883 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
888 SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD | CTLFLAG_NOLOCK,
889 sysctl_sysctl_oidfmt, "");
892 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
894 struct sysctl_oid *oid;
897 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
903 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
907 SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr,
908 CTLFLAG_RD | CTLFLAG_NOLOCK,
909 sysctl_sysctl_oiddescr, "");
912 * Default "handler" functions.
916 * Handle an 8-bit number, signed or unsigned. arg1 points to it.
920 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
926 error = SYSCTL_OUT(req, arg1, sizeof(int8_t));
928 if (error || !req->newptr)
931 error = SYSCTL_IN(req, arg1, sizeof(int8_t));
936 * Handle a 16-bit number, signed or unsigned. arg1 points to it.
940 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
946 error = SYSCTL_OUT(req, arg1, sizeof(int16_t));
948 if (error || !req->newptr)
951 error = SYSCTL_IN(req, arg1, sizeof(int16_t));
956 * Handle a 32-bit number, signed or unsigned. arg1 points to it.
960 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
966 error = SYSCTL_OUT(req, arg1, sizeof(int32_t));
968 if (error || !req->newptr)
971 error = SYSCTL_IN(req, arg1, sizeof(int32_t));
976 * Handle a 64-bit number, signed or unsigned. arg1 points to it.
980 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
986 error = SYSCTL_OUT(req, arg1, sizeof(int64_t));
988 if (error || !req->newptr)
991 error = SYSCTL_IN(req, arg1, sizeof(int64_t));
996 * Handle an int, signed or unsigned.
998 * a variable: point arg1 at it.
999 * a constant: pass it in arg2.
1003 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1008 error = SYSCTL_OUT(req, arg1, sizeof(int));
1010 error = SYSCTL_OUT(req, &arg2, sizeof(int));
1012 if (error || !req->newptr)
1018 error = SYSCTL_IN(req, arg1, sizeof(int));
1023 * Handle a long, signed or unsigned. arg1 points to it.
1027 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1033 if (req->oldlen == sizeof(int) &&
1034 *(long *)arg1 >= INT_MIN &&
1035 *(long *)arg1 <= INT_MAX) {
1037 * Backwards compatibility for read-only fields promoted
1038 * from int to long. Allow userland to request the field
1039 * as an integer if the value is in-range.
1041 int val = (int)*(long *)arg1;
1042 error = SYSCTL_OUT(req, &val, sizeof(int));
1045 * Normal operation fo a long
1047 error = SYSCTL_OUT(req, arg1, sizeof(long));
1050 if (error || !req->newptr)
1053 error = SYSCTL_IN(req, arg1, sizeof(long));
1059 * Handle a quad, signed or unsigned. arg1 points to it.
1063 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
1069 error = SYSCTL_OUT(req, arg1, sizeof(quad_t));
1071 if (error || !req->newptr)
1074 error = SYSCTL_IN(req, arg1, sizeof(quad_t));
1079 * Handle an bit in a 32-bit field, pass and return an 'int'
1081 * a variable: point arg1 at it.
1082 * a constant: pass it in arg2.
1086 sysctl_handle_bit32(SYSCTL_HANDLER_ARGS)
1093 bit = (oidp->oid_kind & CTLMASK_BITFLD) >> CTLSHIFT_BITFLD;
1094 mask = arg1 ? *(uint32_t *)arg1 : (uint32_t)arg2;
1095 v = (mask & (1U << bit)) ? 1 : 0;
1096 error = SYSCTL_OUT(req, &v, sizeof(int));
1098 if (error || !req->newptr)
1104 error = SYSCTL_IN(req, &v, sizeof(int));
1107 atomic_set_int((uint32_t *)arg1, 1U << bit);
1109 atomic_clear_int((uint32_t *)arg1, 1U << bit);
1116 * Handle an bit in a 64-bit field, pass and return an 'int'
1118 * a variable: point arg1 at it.
1119 * a constant: pass it in arg2. (NOTE: arg2 is only 32bits)
1123 sysctl_handle_bit64(SYSCTL_HANDLER_ARGS)
1130 bit = (oidp->oid_kind & CTLMASK_BITFLD) >> CTLSHIFT_BITFLD;
1131 mask = arg1 ? *(uint64_t *)arg1 : (uint64_t)(uint32_t)arg2;
1132 v = (mask & (1LU << bit)) ? 1 : 0;
1133 error = SYSCTL_OUT(req, &v, sizeof(int));
1135 if (error || !req->newptr)
1141 error = SYSCTL_IN(req, &v, sizeof(int));
1144 atomic_set_long((uint64_t *)arg1, 1LU << bit);
1146 atomic_clear_long((uint64_t *)arg1, 1LU << bit);
1153 * Handle our generic '\0' terminated 'C' string.
1155 * a variable string: point arg1 at it, arg2 is max length.
1156 * a constant string: point arg1 at it, arg2 is zero.
1160 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1164 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
1166 if (error || !req->newptr)
1169 if ((req->newlen - req->newidx) >= arg2) {
1172 arg2 = (req->newlen - req->newidx);
1173 error = SYSCTL_IN(req, arg1, arg2);
1174 ((char *)arg1)[arg2] = '\0';
1181 * Handle any kind of opaque data.
1182 * arg1 points to it, arg2 is the size.
1186 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1190 error = SYSCTL_OUT(req, arg1, arg2);
1192 if (error || !req->newptr)
1195 error = SYSCTL_IN(req, arg1, arg2);
1201 * Transfer functions to/from kernel space.
1202 * XXX: rather untested at this point
1205 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1211 if (i > req->oldlen - req->oldidx)
1212 i = req->oldlen - req->oldidx;
1214 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1217 if (req->oldptr && i != l)
1223 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1228 if (req->newlen - req->newidx < l)
1230 bcopy((char *)req->newptr + req->newidx, p, l);
1236 kernel_sysctl(int *name, u_int namelen,
1237 void *old, size_t *oldlenp,
1238 void *new, size_t newlen, size_t *retval)
1241 struct sysctl_req req;
1243 bzero(&req, sizeof req);
1248 req.oldlen = *oldlenp;
1250 req.validlen = req.oldlen;
1257 req.newlen = newlen;
1261 req.oldfunc = sysctl_old_kernel;
1262 req.newfunc = sysctl_new_kernel;
1264 req.lock = REQ_UNWIRED;
1268 error = sysctl_root(0, name, namelen, &req);
1272 if (req.lock == REQ_WIRED && req.validlen > 0)
1273 vsunlock(req.oldptr, req.validlen);
1276 if (error && error != ENOMEM)
1280 if (req.oldptr && req.oldidx > req.validlen)
1281 *retval = req.validlen;
1283 *retval = req.oldidx;
1289 kernel_sysctlbyname(char *name,
1290 void *old, size_t *oldlenp,
1291 void *new, size_t newlen, size_t *retval)
1293 int oid[CTL_MAXNAME];
1294 size_t oidlen, plen;
1297 oid[0] = CTL_SYSCTL;
1298 oid[1] = CTL_SYSCTL_NAME2OID;
1299 oidlen = sizeof(oid);
1301 error = kernel_sysctl(oid, 2, oid, &oidlen, name, strlen(name), &plen);
1305 error = kernel_sysctl(oid, plen / sizeof(int), old, oldlenp,
1306 new, newlen, retval);
1311 * Transfer function to/from user space.
1314 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1320 if (req->lock == 1 && req->oldptr) {
1321 vslock(req->oldptr, req->oldlen);
1327 if (i > req->oldlen - req->oldidx)
1328 i = req->oldlen - req->oldidx;
1330 error = copyout(p, (char *)req->oldptr + req->oldidx,
1336 if (req->oldptr && i < l)
1342 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1348 if (req->newlen - req->newidx < l)
1350 error = copyin((char *)req->newptr + req->newidx, p, l);
1356 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1357 int *nindx, struct sysctl_req *req)
1359 struct sysctl_oid_list *lsp;
1360 struct sysctl_oid *oid;
1363 lsp = &sysctl__children;
1365 while (indx < CTL_MAXNAME) {
1366 SLIST_FOREACH(oid, lsp, oid_link) {
1367 if (oid->oid_number == name[indx])
1374 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1375 if (oid->oid_handler != NULL || indx == namelen) {
1379 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1380 ("%s found DYING node %p", __func__, oid));
1383 lsp = SYSCTL_CHILDREN(oid);
1384 } else if (indx == namelen) {
1388 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1389 ("%s found DYING node %p", __func__, oid));
1399 * Traverse our tree, and find the right node, execute whatever it points
1400 * to, and return the resulting error code.
1403 sysctl_root(SYSCTL_HANDLER_ARGS)
1405 struct thread *td = req->td;
1406 struct proc *p = td ? td->td_proc : NULL;
1407 struct sysctl_oid *oid;
1411 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1415 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1417 * You can't call a sysctl when it's a node, but has
1418 * no handler. Inform the user that it's a node.
1419 * The indx may or may not be the same as namelen.
1421 if (oid->oid_handler == NULL)
1425 /* If writing isn't allowed */
1426 if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1427 ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1430 /* Most likely only root can write */
1431 if (!(oid->oid_kind & CTLFLAG_ANYBODY) && req->newptr && p &&
1432 (error = priv_check_cred(td->td_ucred,
1433 (oid->oid_kind & CTLFLAG_PRISON) ? PRIV_SYSCTL_WRITEJAIL :
1434 PRIV_SYSCTL_WRITE, 0)))
1437 if (oid->oid_handler == NULL)
1441 * Default oid locking is exclusive when modifying (newptr),
1442 * shared otherwise, unless overridden with a control flag.
1444 if ((oid->oid_kind & CTLFLAG_NOLOCK) == 0) {
1445 lktype = (req->newptr != NULL) ? LK_EXCLUSIVE : LK_SHARED;
1446 if (oid->oid_kind & CTLFLAG_SHLOCK)
1448 if (oid->oid_kind & CTLFLAG_EXLOCK)
1449 lktype = LK_EXCLUSIVE;
1451 lockmgr(&oid->oid_lock, lktype);
1454 if (lockmgr(&oid->oid_lock, lktype | LK_SLEEPFAIL)) {
1455 kprintf("%s\n", oid->oid_name);
1456 lockmgr(&oid->oid_lock, lktype);
1461 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1462 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1465 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1468 if ((oid->oid_kind & CTLFLAG_NOLOCK) == 0)
1469 lockmgr(&oid->oid_lock, LK_RELEASE);
1474 sys___sysctl(struct sysctl_args *uap)
1476 int error, i, name[CTL_MAXNAME];
1479 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1482 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1486 error = userland_sysctl(name, uap->namelen,
1487 uap->old, uap->oldlenp, 0,
1488 uap->new, uap->newlen, &j);
1489 if (error && error != ENOMEM)
1492 i = copyout(&j, uap->oldlenp, sizeof(j));
1500 * This is used from various compatibility syscalls too. That's why name
1501 * must be in kernel space.
1504 userland_sysctl(int *name, u_int namelen,
1505 void *old, size_t *oldlenp, int inkernel,
1506 void *new, size_t newlen, size_t *retval)
1509 struct sysctl_req req;
1511 bzero(&req, sizeof req);
1518 req.oldlen = *oldlenp;
1520 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1525 req.validlen = req.oldlen;
1528 * NOTE: User supplied buffers are not guaranteed to be good,
1529 * the sysctl copyins and copyouts can fail.
1535 req.newlen = newlen;
1539 req.oldfunc = sysctl_old_user;
1540 req.newfunc = sysctl_new_user;
1542 req.lock = REQ_UNWIRED;
1546 if (KTRPOINT(curthread, KTR_SYSCTL))
1547 ktrsysctl(name, namelen);
1554 error = sysctl_root(0, name, namelen, &req);
1556 if (error != EAGAIN)
1562 if (req.lock == REQ_WIRED && req.validlen > 0)
1563 vsunlock(req.oldptr, req.validlen);
1565 if (error && error != ENOMEM)
1569 if (req.oldptr && req.oldidx > req.validlen)
1570 *retval = req.validlen;
1572 *retval = req.oldidx;
1578 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
1582 value = *(int *)arg1;
1583 error = sysctl_handle_int(oidp, &value, 0, req);
1584 if (error || !req->newptr)
1586 if (value < low || value > high)
1588 *(int *)arg1 = value;
1593 * Drain into a sysctl struct. The user buffer should be wired if a page
1594 * fault would cause issue.
1597 sbuf_sysctl_drain(void *arg, const char *data, int len)
1599 struct sysctl_req *req = arg;
1602 error = SYSCTL_OUT(req, data, len);
1603 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
1604 return (error == 0 ? len : -error);
1608 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
1609 struct sysctl_req *req)
1612 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
1613 sbuf_set_drain(s, sbuf_sysctl_drain, req);
1618 * The exclusive sysctl lock only protects its topology, and is
1619 * very expensive, but allows us to use a pcpu shared lock for
1620 * critical path accesses.
1628 for (i = 0; i < ncpus; ++i) {
1629 gd = globaldata_find(i);
1630 lockmgr(&gd->gd_sysctllock, LK_EXCLUSIVE);
1635 _sysctl_xunlock(void)
1640 for (i = 0; i < ncpus; ++i) {
1641 gd = globaldata_find(i);
1642 lockmgr(&gd->gd_sysctllock, LK_RELEASE);