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 * 4. 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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_capsicum.h"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/capsicum.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/rmlock.h>
60 #include <sys/sysproto.h>
63 #include <sys/ktrace.h>
68 #include <security/mac/mac_framework.h>
71 #include <vm/vm_extern.h>
73 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
74 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
75 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
78 * The sysctllock protects the MIB tree. It also protects sysctl
79 * contexts used with dynamic sysctls. The sysctl_register_oid() and
80 * sysctl_unregister_oid() routines require the sysctllock to already
81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
82 * provided for the few places in the kernel which need to use that
83 * API rather than using the dynamic API. Use of the dynamic API is
84 * strongly encouraged for most code.
86 * The sysctlmemlock is used to limit the amount of user memory wired for
87 * sysctl requests. This is implemented by serializing any userland
88 * sysctl requests larger than a single page via an exclusive lock.
90 static struct rmlock sysctllock;
91 static struct sx __exclusive_cache_line sysctlmemlock;
93 #define SYSCTL_WLOCK() rm_wlock(&sysctllock)
94 #define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
95 #define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
96 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
97 #define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
98 #define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
99 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
100 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
101 #define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
103 #define SYSCTL_SLEEP(ch, wmesg, timo) \
104 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
106 static int sysctl_root(SYSCTL_HANDLER_ARGS);
109 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
111 static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
113 static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
114 static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
116 static struct sysctl_oid *
117 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
119 struct sysctl_oid *oidp;
121 SYSCTL_ASSERT_LOCKED();
122 SLIST_FOREACH(oidp, list, oid_link) {
123 if (strcmp(oidp->oid_name, name) == 0) {
131 * Initialization of the MIB tree.
133 * Order by number in each list.
150 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
151 struct sysctl_req *req, struct rm_priotracker *tracker)
155 if (oid->oid_kind & CTLFLAG_DYN)
156 atomic_add_int(&oid->oid_running, 1);
159 SYSCTL_RUNLOCK(tracker);
163 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
165 error = oid->oid_handler(oid, arg1, arg2, req);
166 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
169 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
172 SYSCTL_RLOCK(tracker);
176 if (oid->oid_kind & CTLFLAG_DYN) {
177 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
178 (oid->oid_kind & CTLFLAG_DYING) != 0)
179 wakeup(&oid->oid_running);
186 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
188 struct sysctl_req req;
189 struct sysctl_oid *curr;
192 ssize_t rem = sizeof(path);
194 uint8_t data[512] __aligned(sizeof(uint64_t));
200 for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
201 len = strlen(curr->oid_name);
206 printf("OID path exceeds %d bytes\n", (int)sizeof(path));
209 memcpy(path + rem, curr->oid_name, len);
211 path[rem + len] = '.';
214 memset(&req, 0, sizeof(req));
217 req.oldfunc = sysctl_old_kernel;
218 req.newfunc = sysctl_new_kernel;
219 req.lock = REQ_UNWIRED;
221 switch (oidp->oid_kind & CTLTYPE) {
223 if (getenv_array(path + rem, data, sizeof(data), &size,
224 sizeof(int), GETENV_SIGNED) == 0)
230 if (getenv_array(path + rem, data, sizeof(data), &size,
231 sizeof(int), GETENV_UNSIGNED) == 0)
237 if (getenv_array(path + rem, data, sizeof(data), &size,
238 sizeof(long), GETENV_SIGNED) == 0)
244 if (getenv_array(path + rem, data, sizeof(data), &size,
245 sizeof(long), GETENV_UNSIGNED) == 0)
251 if (getenv_array(path + rem, data, sizeof(data), &size,
252 sizeof(int8_t), GETENV_SIGNED) == 0)
258 if (getenv_array(path + rem, data, sizeof(data), &size,
259 sizeof(int16_t), GETENV_SIGNED) == 0)
265 if (getenv_array(path + rem, data, sizeof(data), &size,
266 sizeof(int32_t), GETENV_SIGNED) == 0)
272 if (getenv_array(path + rem, data, sizeof(data), &size,
273 sizeof(int64_t), GETENV_SIGNED) == 0)
279 if (getenv_array(path + rem, data, sizeof(data), &size,
280 sizeof(uint8_t), GETENV_UNSIGNED) == 0)
286 if (getenv_array(path + rem, data, sizeof(data), &size,
287 sizeof(uint16_t), GETENV_UNSIGNED) == 0)
293 if (getenv_array(path + rem, data, sizeof(data), &size,
294 sizeof(uint32_t), GETENV_UNSIGNED) == 0)
300 if (getenv_array(path + rem, data, sizeof(data), &size,
301 sizeof(uint64_t), GETENV_UNSIGNED) == 0)
307 penv = kern_getenv(path + rem);
310 req.newlen = strlen(penv);
316 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
317 oidp->oid_arg2, &req, NULL);
319 printf("Setting sysctl %s failed: %d\n", path + rem, error);
325 sysctl_register_oid(struct sysctl_oid *oidp)
327 struct sysctl_oid_list *parent = oidp->oid_parent;
328 struct sysctl_oid *p;
329 struct sysctl_oid *q;
334 * First check if another oid with the same name already
335 * exists in the parent's list.
337 SYSCTL_ASSERT_WLOCKED();
338 p = sysctl_find_oidname(oidp->oid_name, parent);
340 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
344 printf("can't re-use a leaf (%s)!\n", p->oid_name);
348 /* get current OID number */
349 oid_number = oidp->oid_number;
352 #error "OID_AUTO is expected to be a negative value"
355 * Any negative OID number qualifies as OID_AUTO. Valid OID
356 * numbers should always be positive.
358 * NOTE: DO NOT change the starting value here, change it in
359 * <sys/sysctl.h>, and make sure it is at least 256 to
360 * accommodate e.g. net.inet.raw as a static sysctl node.
362 if (oid_number < 0) {
366 * By decrementing the next OID number we spend less
367 * time inserting the OIDs into a sorted list.
369 if (--newoid < CTL_AUTO_START)
376 * Insert the OID into the parent's list sorted by OID number.
380 SLIST_FOREACH(p, parent, oid_link) {
381 /* check if the current OID number is in use */
382 if (oid_number == p->oid_number) {
383 /* get the next valid OID number */
384 if (oid_number < CTL_AUTO_START ||
385 oid_number == 0x7fffffff) {
386 /* wraparound - restart */
387 oid_number = CTL_AUTO_START;
388 /* don't loop forever */
390 panic("sysctl: Out of OID numbers\n");
395 } else if (oid_number < p->oid_number)
399 /* check for non-auto OID number collision */
400 if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
401 oid_number >= CTL_AUTO_START) {
402 printf("sysctl: OID number(%d) is already in use for '%s'\n",
403 oidp->oid_number, oidp->oid_name);
405 /* update the OID number, if any */
406 oidp->oid_number = oid_number;
408 SLIST_INSERT_AFTER(q, oidp, oid_link);
410 SLIST_INSERT_HEAD(parent, oidp, oid_link);
412 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
414 (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
416 (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
417 (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
418 /* only fetch value once */
419 oidp->oid_kind |= CTLFLAG_NOFETCH;
420 /* try to fetch value from kernel environment */
421 sysctl_load_tunable_by_oid_locked(oidp);
426 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
430 * Mark the leaf as dormant if it's not to be immediately enabled.
431 * We do not disable nodes as they can be shared between modules
432 * and it is always safe to access a node.
434 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
435 ("internal flag is set in oid_kind"));
436 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
437 oidp->oid_kind |= CTLFLAG_DORMANT;
438 sysctl_register_oid(oidp);
442 sysctl_enable_oid(struct sysctl_oid *oidp)
445 SYSCTL_ASSERT_WLOCKED();
446 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
447 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
448 ("sysctl node is marked as dormant"));
451 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
452 ("enabling already enabled sysctl oid"));
453 oidp->oid_kind &= ~CTLFLAG_DORMANT;
457 sysctl_unregister_oid(struct sysctl_oid *oidp)
459 struct sysctl_oid *p;
462 SYSCTL_ASSERT_WLOCKED();
463 if (oidp->oid_number == OID_AUTO) {
467 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
469 SLIST_REMOVE(oidp->oid_parent, oidp,
470 sysctl_oid, oid_link);
478 * This can happen when a module fails to register and is
479 * being unloaded afterwards. It should not be a panic()
483 printf("%s: failed(%d) to unregister sysctl(%s)\n",
484 __func__, error, oidp->oid_name);
488 /* Initialize a new context to keep track of dynamically added sysctls. */
490 sysctl_ctx_init(struct sysctl_ctx_list *c)
498 * No locking here, the caller is responsible for not adding
499 * new nodes to a context until after this function has
506 /* Free the context, and destroy all dynamic oids registered in this context */
508 sysctl_ctx_free(struct sysctl_ctx_list *clist)
510 struct sysctl_ctx_entry *e, *e1;
515 * First perform a "dry run" to check if it's ok to remove oids.
517 * XXX This algorithm is a hack. But I don't know any
518 * XXX better solution for now...
521 TAILQ_FOREACH(e, clist, link) {
522 error = sysctl_remove_oid_locked(e->entry, 0, 0);
527 * Restore deregistered entries, either from the end,
528 * or from the place where error occurred.
529 * e contains the entry that was not unregistered
532 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
534 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
536 sysctl_register_oid(e1->entry);
537 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
543 /* Now really delete the entries */
544 e = TAILQ_FIRST(clist);
546 e1 = TAILQ_NEXT(e, link);
547 error = sysctl_remove_oid_locked(e->entry, 1, 0);
549 panic("sysctl_remove_oid: corrupt tree, entry: %s",
551 free(e, M_SYSCTLOID);
558 /* Add an entry to the context */
559 struct sysctl_ctx_entry *
560 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
562 struct sysctl_ctx_entry *e;
564 SYSCTL_ASSERT_WLOCKED();
565 if (clist == NULL || oidp == NULL)
567 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
569 TAILQ_INSERT_HEAD(clist, e, link);
573 /* Find an entry in the context */
574 struct sysctl_ctx_entry *
575 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
577 struct sysctl_ctx_entry *e;
579 SYSCTL_ASSERT_WLOCKED();
580 if (clist == NULL || oidp == NULL)
582 TAILQ_FOREACH(e, clist, link) {
590 * Delete an entry from the context.
591 * NOTE: this function doesn't free oidp! You have to remove it
592 * with sysctl_remove_oid().
595 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
597 struct sysctl_ctx_entry *e;
599 if (clist == NULL || oidp == NULL)
602 e = sysctl_ctx_entry_find(clist, oidp);
604 TAILQ_REMOVE(clist, e, link);
606 free(e, M_SYSCTLOID);
615 * Remove dynamically created sysctl trees.
616 * oidp - top of the tree to be removed
617 * del - if 0 - just deregister, otherwise free up entries as well
618 * recurse - if != 0 traverse the subtree to be deleted
621 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
626 error = sysctl_remove_oid_locked(oidp, del, recurse);
632 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
633 int del, int recurse)
635 struct sysctl_oid *p, *tmp;
640 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
641 if (strcmp(p->oid_name, name) == 0) {
642 error = sysctl_remove_oid_locked(p, del, recurse);
653 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
655 struct sysctl_oid *p, *tmp;
658 SYSCTL_ASSERT_WLOCKED();
661 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
662 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
667 * WARNING: normal method to do this should be through
668 * sysctl_ctx_free(). Use recursing as the last resort
669 * method to purge your sysctl tree of leftovers...
670 * However, if some other code still references these nodes,
673 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
674 if (oidp->oid_refcnt == 1) {
675 SLIST_FOREACH_SAFE(p,
676 SYSCTL_CHILDREN(oidp), oid_link, tmp) {
678 printf("Warning: failed attempt to "
679 "remove oid %s with child %s\n",
680 oidp->oid_name, p->oid_name);
683 error = sysctl_remove_oid_locked(p, del,
690 if (oidp->oid_refcnt > 1 ) {
693 if (oidp->oid_refcnt == 0) {
694 printf("Warning: bad oid_refcnt=%u (%s)!\n",
695 oidp->oid_refcnt, oidp->oid_name);
698 sysctl_unregister_oid(oidp);
701 * Wait for all threads running the handler to drain.
702 * This preserves the previous behavior when the
703 * sysctl lock was held across a handler invocation,
704 * and is necessary for module unload correctness.
706 while (oidp->oid_running > 0) {
707 oidp->oid_kind |= CTLFLAG_DYING;
708 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
711 free(__DECONST(char *, oidp->oid_descr),
713 free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
714 free(oidp, M_SYSCTLOID);
720 * Create new sysctls at run time.
721 * clist may point to a valid context initialized with sysctl_ctx_init().
724 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
725 int number, const char *name, int kind, void *arg1, intmax_t arg2,
726 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
728 struct sysctl_oid *oidp;
730 /* You have to hook up somewhere.. */
733 /* Check if the node already exists, otherwise create it */
735 oidp = sysctl_find_oidname(name, parent);
737 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
739 /* Update the context */
741 sysctl_ctx_entry_add(clist, oidp);
746 printf("can't re-use a leaf (%s)!\n", name);
750 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
751 oidp->oid_parent = parent;
752 SLIST_INIT(&oidp->oid_children);
753 oidp->oid_number = number;
754 oidp->oid_refcnt = 1;
755 oidp->oid_name = strdup(name, M_SYSCTLOID);
756 oidp->oid_handler = handler;
757 oidp->oid_kind = CTLFLAG_DYN | kind;
758 oidp->oid_arg1 = arg1;
759 oidp->oid_arg2 = arg2;
762 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
763 /* Update the context, if used */
765 sysctl_ctx_entry_add(clist, oidp);
766 /* Register this oid */
767 sysctl_register_oid(oidp);
773 * Rename an existing oid.
776 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
781 newname = strdup(name, M_SYSCTLOID);
783 oldname = __DECONST(char *, oidp->oid_name);
784 oidp->oid_name = newname;
786 free(oldname, M_SYSCTLOID);
790 * Reparent an existing oid.
793 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
795 struct sysctl_oid *oidp;
798 if (oid->oid_parent == parent) {
802 oidp = sysctl_find_oidname(oid->oid_name, parent);
807 sysctl_unregister_oid(oid);
808 oid->oid_parent = parent;
809 oid->oid_number = OID_AUTO;
810 sysctl_register_oid(oid);
816 * Register the kernel's oids on startup.
818 SET_DECLARE(sysctl_set, struct sysctl_oid);
821 sysctl_register_all(void *arg)
823 struct sysctl_oid **oidp;
825 sx_init(&sysctlmemlock, "sysctl mem");
828 SET_FOREACH(oidp, sysctl_set)
829 sysctl_register_oid(*oidp);
832 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
837 * These functions implement a presently undocumented interface
838 * used by the sysctl program to walk the tree, and get the type
839 * so it can print the value.
840 * This interface is under work and consideration, and should probably
841 * be killed with a big axe by the first person who can find the time.
842 * (be aware though, that the proper interface isn't as obvious as it
843 * may seem, there are various conflicting requirements.
845 * {0,0} printf the entire MIB-tree.
846 * {0,1,...} return the name of the "..." OID.
847 * {0,2,...} return the next OID.
848 * {0,3} return the OID of the name in "new"
849 * {0,4,...} return the kind & format info for the "..." OID.
850 * {0,5,...} return the description the "..." OID.
855 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
858 struct sysctl_oid *oidp;
860 SYSCTL_ASSERT_LOCKED();
861 SLIST_FOREACH(oidp, l, oid_link) {
866 printf("%d %s ", oidp->oid_number, oidp->oid_name);
869 oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
870 oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
872 if (oidp->oid_handler)
875 switch (oidp->oid_kind & CTLTYPE) {
878 if (!oidp->oid_handler) {
879 sysctl_sysctl_debug_dump_node(
880 SYSCTL_CHILDREN(oidp), i + 2);
883 case CTLTYPE_INT: printf(" Int\n"); break;
884 case CTLTYPE_UINT: printf(" u_int\n"); break;
885 case CTLTYPE_LONG: printf(" Long\n"); break;
886 case CTLTYPE_ULONG: printf(" u_long\n"); break;
887 case CTLTYPE_STRING: printf(" String\n"); break;
888 case CTLTYPE_S8: printf(" int8_t\n"); break;
889 case CTLTYPE_S16: printf(" int16_t\n"); break;
890 case CTLTYPE_S32: printf(" int32_t\n"); break;
891 case CTLTYPE_S64: printf(" int64_t\n"); break;
892 case CTLTYPE_U8: printf(" uint8_t\n"); break;
893 case CTLTYPE_U16: printf(" uint16_t\n"); break;
894 case CTLTYPE_U32: printf(" uint32_t\n"); break;
895 case CTLTYPE_U64: printf(" uint64_t\n"); break;
896 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
897 default: printf("\n");
904 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
906 struct rm_priotracker tracker;
909 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
912 SYSCTL_RLOCK(&tracker);
913 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
914 SYSCTL_RUNLOCK(&tracker);
918 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
919 0, 0, sysctl_sysctl_debug, "-", "");
923 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
925 int *name = (int *) arg1;
926 u_int namelen = arg2;
928 struct sysctl_oid *oid;
929 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
930 struct rm_priotracker tracker;
933 SYSCTL_RLOCK(&tracker);
936 snprintf(buf,sizeof(buf),"%d",*name);
938 error = SYSCTL_OUT(req, ".", 1);
940 error = SYSCTL_OUT(req, buf, strlen(buf));
948 SLIST_FOREACH(oid, lsp, oid_link) {
949 if (oid->oid_number != *name)
953 error = SYSCTL_OUT(req, ".", 1);
955 error = SYSCTL_OUT(req, oid->oid_name,
956 strlen(oid->oid_name));
963 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
966 if (oid->oid_handler)
969 lsp2 = SYSCTL_CHILDREN(oid);
974 error = SYSCTL_OUT(req, "", 1);
976 SYSCTL_RUNLOCK(&tracker);
981 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
984 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
985 sysctl_sysctl_name, "");
988 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
989 int *next, int *len, int level, struct sysctl_oid **oidpp)
991 struct sysctl_oid *oidp;
993 SYSCTL_ASSERT_LOCKED();
995 SLIST_FOREACH(oidp, lsp, oid_link) {
996 *next = oidp->oid_number;
999 if ((oidp->oid_kind & (CTLFLAG_SKIP | CTLFLAG_DORMANT)) != 0)
1003 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1005 if (oidp->oid_handler)
1006 /* We really should call the handler here...*/
1008 lsp = SYSCTL_CHILDREN(oidp);
1009 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
1010 len, level+1, oidpp))
1015 if (oidp->oid_number < *name)
1018 if (oidp->oid_number > *name) {
1019 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1021 if (oidp->oid_handler)
1023 lsp = SYSCTL_CHILDREN(oidp);
1024 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
1025 next+1, len, level+1, oidpp))
1029 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1032 if (oidp->oid_handler)
1035 lsp = SYSCTL_CHILDREN(oidp);
1036 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
1037 len, level+1, oidpp))
1048 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
1050 int *name = (int *) arg1;
1051 u_int namelen = arg2;
1053 struct sysctl_oid *oid;
1054 struct sysctl_oid_list *lsp = &sysctl__children;
1055 struct rm_priotracker tracker;
1056 int newoid[CTL_MAXNAME];
1058 SYSCTL_RLOCK(&tracker);
1059 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1060 SYSCTL_RUNLOCK(&tracker);
1063 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
1068 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
1071 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,
1072 sysctl_sysctl_next, "");
1075 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
1077 struct sysctl_oid *oidp;
1078 struct sysctl_oid_list *lsp = &sysctl__children;
1081 SYSCTL_ASSERT_LOCKED();
1083 for (*len = 0; *len < CTL_MAXNAME;) {
1084 p = strsep(&name, ".");
1086 oidp = SLIST_FIRST(lsp);
1087 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
1090 if (strcmp(p, oidp->oid_name) == 0)
1093 *oid++ = oidp->oid_number;
1096 if (name == NULL || *name == '\0') {
1102 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
1105 if (oidp->oid_handler)
1108 lsp = SYSCTL_CHILDREN(oidp);
1114 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1117 int error, oid[CTL_MAXNAME], len = 0;
1118 struct sysctl_oid *op = NULL;
1119 struct rm_priotracker tracker;
1124 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1125 return (ENAMETOOLONG);
1128 if (req->newlen >= sizeof(buf))
1129 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1131 error = SYSCTL_IN(req, p, req->newlen);
1138 p [req->newlen] = '\0';
1140 SYSCTL_RLOCK(&tracker);
1141 error = name2oid(p, oid, &len, &op);
1142 SYSCTL_RUNLOCK(&tracker);
1150 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1155 * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
1158 SYSCTL_PROC(_sysctl, 3, name2oid,
1159 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1160 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1163 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1165 struct sysctl_oid *oid;
1166 struct rm_priotracker tracker;
1169 SYSCTL_RLOCK(&tracker);
1170 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1174 if (oid->oid_fmt == NULL) {
1178 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1181 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1183 SYSCTL_RUNLOCK(&tracker);
1188 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1189 sysctl_sysctl_oidfmt, "");
1192 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1194 struct sysctl_oid *oid;
1195 struct rm_priotracker tracker;
1198 SYSCTL_RLOCK(&tracker);
1199 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1203 if (oid->oid_descr == NULL) {
1207 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1209 SYSCTL_RUNLOCK(&tracker);
1213 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1214 sysctl_sysctl_oiddescr, "");
1217 * Default "handler" functions.
1223 * a variable: point arg1 at it.
1224 * a constant: pass it in arg2.
1228 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
1234 * Attempt to get a coherent snapshot by making a copy of the data.
1237 temp = *(bool *)arg1 ? 1 : 0;
1239 temp = arg2 ? 1 : 0;
1241 error = SYSCTL_OUT(req, &temp, sizeof(temp));
1242 if (error || !req->newptr)
1248 error = SYSCTL_IN(req, &temp, sizeof(temp));
1250 *(bool *)arg1 = temp ? 1 : 0;
1256 * Handle an int8_t, signed or unsigned.
1258 * a variable: point arg1 at it.
1259 * a constant: pass it in arg2.
1263 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
1269 * Attempt to get a coherent snapshot by making a copy of the data.
1272 tmpout = *(int8_t *)arg1;
1275 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1277 if (error || !req->newptr)
1283 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1288 * Handle an int16_t, signed or unsigned.
1290 * a variable: point arg1 at it.
1291 * a constant: pass it in arg2.
1295 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
1301 * Attempt to get a coherent snapshot by making a copy of the data.
1304 tmpout = *(int16_t *)arg1;
1307 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1309 if (error || !req->newptr)
1315 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1320 * Handle an int32_t, signed or unsigned.
1322 * a variable: point arg1 at it.
1323 * a constant: pass it in arg2.
1327 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
1333 * Attempt to get a coherent snapshot by making a copy of the data.
1336 tmpout = *(int32_t *)arg1;
1339 error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
1341 if (error || !req->newptr)
1347 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
1352 * Handle an int, signed or unsigned.
1354 * a variable: point arg1 at it.
1355 * a constant: pass it in arg2.
1359 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
1361 int tmpout, error = 0;
1364 * Attempt to get a coherent snapshot by making a copy of the data.
1367 tmpout = *(int *)arg1;
1370 error = SYSCTL_OUT(req, &tmpout, sizeof(int));
1372 if (error || !req->newptr)
1378 error = SYSCTL_IN(req, arg1, sizeof(int));
1383 * Based on on sysctl_handle_int() convert milliseconds into ticks.
1384 * Note: this is used by TCP.
1388 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
1393 s = (int)((int64_t)tt * 1000 / hz);
1395 error = sysctl_handle_int(oidp, &s, 0, req);
1396 if (error || !req->newptr)
1399 tt = (int)((int64_t)s * hz / 1000);
1409 * Handle a long, signed or unsigned.
1411 * a variable: point arg1 at it.
1412 * a constant: pass it in arg2.
1416 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
1425 * Attempt to get a coherent snapshot by making a copy of the data.
1428 tmplong = *(long *)arg1;
1432 if (req->flags & SCTL_MASK32) {
1434 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
1437 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
1439 if (error || !req->newptr)
1445 else if (req->flags & SCTL_MASK32) {
1446 error = SYSCTL_IN(req, &tmpint, sizeof(int));
1447 *(long *)arg1 = (long)tmpint;
1451 error = SYSCTL_IN(req, arg1, sizeof(long));
1456 * Handle a 64 bit int, signed or unsigned.
1458 * a variable: point arg1 at it.
1459 * a constant: pass it in arg2.
1462 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
1468 * Attempt to get a coherent snapshot by making a copy of the data.
1471 tmpout = *(uint64_t *)arg1;
1474 error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
1476 if (error || !req->newptr)
1482 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
1487 * Handle our generic '\0' terminated 'C' string.
1489 * a variable string: point arg1 at it, arg2 is max length.
1490 * a constant string: point arg1 at it, arg2 is zero.
1494 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
1497 int error = 0, ro_string = 0;
1500 * A zero-length buffer indicates a fixed size read-only
1504 arg2 = strlen((char *)arg1) + 1;
1508 if (req->oldptr != NULL) {
1514 /* try to make a coherent snapshot of the string */
1515 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
1516 memcpy(tmparg, arg1, arg2);
1519 outlen = strnlen(tmparg, arg2 - 1) + 1;
1520 error = SYSCTL_OUT(req, tmparg, outlen);
1523 free(tmparg, M_SYSCTLTMP);
1525 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
1526 error = SYSCTL_OUT(req, NULL, outlen);
1528 if (error || !req->newptr)
1531 if ((req->newlen - req->newidx) >= arg2) {
1534 arg2 = (req->newlen - req->newidx);
1535 error = SYSCTL_IN(req, arg1, arg2);
1536 ((char *)arg1)[arg2] = '\0';
1542 * Handle any kind of opaque data.
1543 * arg1 points to it, arg2 is the size.
1547 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
1551 struct sysctl_req req2;
1554 * Attempt to get a coherent snapshot, by using the thread
1555 * pre-emption counter updated from within mi_switch() to
1556 * determine if we were pre-empted during a bcopy() or
1557 * copyout(). Make 3 attempts at doing this before giving up.
1558 * If we encounter an error, stop immediately.
1563 generation = curthread->td_generation;
1564 error = SYSCTL_OUT(req, arg1, arg2);
1568 if (generation != curthread->td_generation && tries < 3) {
1573 error = SYSCTL_IN(req, arg1, arg2);
1579 * Transfer functions to/from kernel space.
1580 * XXX: rather untested at this point
1583 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1589 if (req->oldlen <= req->oldidx)
1592 if (i > req->oldlen - req->oldidx)
1593 i = req->oldlen - req->oldidx;
1595 bcopy(p, (char *)req->oldptr + req->oldidx, i);
1598 if (req->oldptr && i != l)
1604 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1608 if (req->newlen - req->newidx < l)
1610 bcopy((char *)req->newptr + req->newidx, p, l);
1616 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1617 size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
1620 struct sysctl_req req;
1622 bzero(&req, sizeof req);
1628 req.oldlen = *oldlenp;
1630 req.validlen = req.oldlen;
1637 req.newlen = newlen;
1641 req.oldfunc = sysctl_old_kernel;
1642 req.newfunc = sysctl_new_kernel;
1643 req.lock = REQ_UNWIRED;
1645 error = sysctl_root(0, name, namelen, &req);
1647 if (req.lock == REQ_WIRED && req.validlen > 0)
1648 vsunlock(req.oldptr, req.validlen);
1650 if (error && error != ENOMEM)
1654 if (req.oldptr && req.oldidx > req.validlen)
1655 *retval = req.validlen;
1657 *retval = req.oldidx;
1663 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1664 void *new, size_t newlen, size_t *retval, int flags)
1666 int oid[CTL_MAXNAME];
1667 size_t oidlen, plen;
1670 oid[0] = 0; /* sysctl internal magic */
1671 oid[1] = 3; /* name2oid */
1672 oidlen = sizeof(oid);
1674 error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1675 (void *)name, strlen(name), &plen, flags);
1679 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1680 new, newlen, retval, flags);
1685 * Transfer function to/from user space.
1688 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1690 size_t i, len, origidx;
1693 origidx = req->oldidx;
1695 if (req->oldptr == NULL)
1698 * If we have not wired the user supplied buffer and we are currently
1699 * holding locks, drop a witness warning, as it's possible that
1700 * write operations to the user page can sleep.
1702 if (req->lock != REQ_WIRED)
1703 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1704 "sysctl_old_user()");
1706 len = req->validlen;
1710 if (i > len - origidx)
1712 if (req->lock == REQ_WIRED) {
1713 error = copyout_nofault(p, (char *)req->oldptr +
1716 error = copyout(p, (char *)req->oldptr + origidx, i);
1726 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1732 if (req->newlen - req->newidx < l)
1734 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1735 "sysctl_new_user()");
1736 error = copyin((char *)req->newptr + req->newidx, p, l);
1742 * Wire the user space destination buffer. If set to a value greater than
1743 * zero, the len parameter limits the maximum amount of wired memory.
1746 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1751 wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1753 if (req->lock != REQ_WIRED && req->oldptr &&
1754 req->oldfunc == sysctl_old_user) {
1755 if (wiredlen != 0) {
1756 ret = vslock(req->oldptr, wiredlen);
1763 req->lock = REQ_WIRED;
1764 req->validlen = wiredlen;
1770 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1771 int *nindx, struct sysctl_req *req)
1773 struct sysctl_oid_list *lsp;
1774 struct sysctl_oid *oid;
1777 SYSCTL_ASSERT_LOCKED();
1778 lsp = &sysctl__children;
1780 while (indx < CTL_MAXNAME) {
1781 SLIST_FOREACH(oid, lsp, oid_link) {
1782 if (oid->oid_number == name[indx])
1789 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1790 if (oid->oid_handler != NULL || indx == namelen) {
1794 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1795 ("%s found DYING node %p", __func__, oid));
1798 lsp = SYSCTL_CHILDREN(oid);
1799 } else if (indx == namelen) {
1800 if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
1805 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
1806 ("%s found DYING node %p", __func__, oid));
1816 * Traverse our tree, and find the right node, execute whatever it points
1817 * to, and return the resulting error code.
1821 sysctl_root(SYSCTL_HANDLER_ARGS)
1823 struct sysctl_oid *oid;
1824 struct rm_priotracker tracker;
1825 int error, indx, lvl;
1827 SYSCTL_RLOCK(&tracker);
1829 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1833 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1835 * You can't call a sysctl when it's a node, but has
1836 * no handler. Inform the user that it's a node.
1837 * The indx may or may not be the same as namelen.
1839 if (oid->oid_handler == NULL) {
1845 /* Is this sysctl writable? */
1846 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1851 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1853 #ifdef CAPABILITY_MODE
1855 * If the process is in capability mode, then don't permit reading or
1856 * writing unless specifically granted for the node.
1858 if (IN_CAPABILITY_MODE(req->td)) {
1859 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1860 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1867 /* Is this sysctl sensitive to securelevels? */
1868 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1869 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1870 error = securelevel_gt(req->td->td_ucred, lvl);
1875 /* Is this sysctl writable by only privileged users? */
1876 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1879 if (oid->oid_kind & CTLFLAG_PRISON)
1880 priv = PRIV_SYSCTL_WRITEJAIL;
1882 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1883 prison_owns_vnet(req->td->td_ucred))
1884 priv = PRIV_SYSCTL_WRITEJAIL;
1887 priv = PRIV_SYSCTL_WRITE;
1888 error = priv_check(req->td, priv);
1893 if (!oid->oid_handler) {
1898 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1899 arg1 = (int *)arg1 + indx;
1902 arg1 = oid->oid_arg1;
1903 arg2 = oid->oid_arg2;
1906 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1912 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1913 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1915 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
1918 SYSCTL_RUNLOCK(&tracker);
1922 #ifndef _SYS_SYSPROTO_H_
1923 struct sysctl_args {
1933 sys___sysctl(struct thread *td, struct sysctl_args *uap)
1935 int error, i, name[CTL_MAXNAME];
1938 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1941 error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1945 error = userland_sysctl(td, name, uap->namelen,
1946 uap->old, uap->oldlenp, 0,
1947 uap->new, uap->newlen, &j, 0);
1948 if (error && error != ENOMEM)
1951 i = copyout(&j, uap->oldlenp, sizeof(j));
1959 * This is used from various compatibility syscalls too. That's why name
1960 * must be in kernel space.
1963 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1964 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1967 int error = 0, memlocked;
1968 struct sysctl_req req;
1970 bzero(&req, sizeof req);
1977 req.oldlen = *oldlenp;
1979 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1984 req.validlen = req.oldlen;
1988 req.newlen = newlen;
1992 req.oldfunc = sysctl_old_user;
1993 req.newfunc = sysctl_new_user;
1994 req.lock = REQ_UNWIRED;
1997 if (KTRPOINT(curthread, KTR_SYSCTL))
1998 ktrsysctl(name, namelen);
2001 if (req.oldptr && req.oldlen > PAGE_SIZE) {
2003 sx_xlock(&sysctlmemlock);
2006 CURVNET_SET(TD_TO_VNET(td));
2011 error = sysctl_root(0, name, namelen, &req);
2012 if (error != EAGAIN)
2014 kern_yield(PRI_USER);
2019 if (req.lock == REQ_WIRED && req.validlen > 0)
2020 vsunlock(req.oldptr, req.validlen);
2022 sx_xunlock(&sysctlmemlock);
2024 if (error && error != ENOMEM)
2028 if (req.oldptr && req.oldidx > req.validlen)
2029 *retval = req.validlen;
2031 *retval = req.oldidx;
2037 * Drain into a sysctl struct. The user buffer should be wired if a page
2038 * fault would cause issue.
2041 sbuf_sysctl_drain(void *arg, const char *data, int len)
2043 struct sysctl_req *req = arg;
2046 error = SYSCTL_OUT(req, data, len);
2047 KASSERT(error >= 0, ("Got unexpected negative value %d", error));
2048 return (error == 0 ? len : -error);
2052 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
2053 struct sysctl_req *req)
2056 /* Supply a default buffer size if none given. */
2057 if (buf == NULL && length == 0)
2059 s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
2060 sbuf_set_drain(s, sbuf_sysctl_drain, req);