11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Mike Karels at Berkeley Software Design, Inc. 71541Srgrimes * 812623Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 912623Sphk * project, to make these variables more userfriendly. 1012623Sphk * 111541Srgrimes * Redistribution and use in source and binary forms, with or without 121541Srgrimes * modification, are permitted provided that the following conditions 131541Srgrimes * are met: 141541Srgrimes * 1. Redistributions of source code must retain the above copyright 151541Srgrimes * notice, this list of conditions and the following disclaimer. 161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171541Srgrimes * notice, this list of conditions and the following disclaimer in the 181541Srgrimes * documentation and/or other materials provided with the distribution. 191541Srgrimes * 4. Neither the name of the University nor the names of its contributors 201541Srgrimes * may be used to endorse or promote products derived from this software 211541Srgrimes * without specific prior written permission. 221541Srgrimes * 231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331541Srgrimes * SUCH DAMAGE. 341541Srgrimes * 351541Srgrimes * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 361541Srgrimes */ 371541Srgrimes 38116182Sobrien#include <sys/cdefs.h> 39116182Sobrien__FBSDID("$FreeBSD$"); 40116182Sobrien 41224159Srwatson#include "opt_capsicum.h" 4231778Seivind#include "opt_compat.h" 43189707Sjhb#include "opt_ktrace.h" 4431778Seivind 451541Srgrimes#include <sys/param.h> 46216060Smdf#include <sys/fail.h> 4748274Speter#include <sys/systm.h> 48224159Srwatson#include <sys/capability.h> 4948274Speter#include <sys/kernel.h> 501541Srgrimes#include <sys/sysctl.h> 5112623Sphk#include <sys/malloc.h> 52164033Srwatson#include <sys/priv.h> 5312662Sdg#include <sys/proc.h> 54194368Sbz#include <sys/jail.h> 5582746Sdillon#include <sys/lock.h> 5682746Sdillon#include <sys/mutex.h> 57212750Smdf#include <sys/sbuf.h> 5893616Salfred#include <sys/sx.h> 5915103Sphk#include <sys/sysproto.h> 60185983Skib#include <sys/uio.h> 61189707Sjhb#ifdef KTRACE 62189707Sjhb#include <sys/ktrace.h> 63189707Sjhb#endif 64163606Srwatson 65195699Srwatson#include <net/vnet.h> 66195699Srwatson 67163606Srwatson#include <security/mac/mac_framework.h> 68163606Srwatson 6912645Sbde#include <vm/vm.h> 7012662Sdg#include <vm/vm_extern.h> 7112645Sbde 7230354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 7363212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 74100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 7530309Sphk 7612429Sphk/* 77188232Sjhb * The sysctllock protects the MIB tree. It also protects sysctl 78188232Sjhb * contexts used with dynamic sysctls. The sysctl_register_oid() and 79188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already 80188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are 81188232Sjhb * provided for the few places in the kernel which need to use that 82188232Sjhb * API rather than using the dynamic API. Use of the dynamic API is 83188232Sjhb * strongly encouraged for most code. 84188232Sjhb * 85192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for 86192125Sjhb * sysctl requests. This is implemented by serializing any userland 87192125Sjhb * sysctl requests larger than a single page via an exclusive lock. 8812429Sphk */ 8993625Srwatsonstatic struct sx sysctllock; 90192125Sjhbstatic struct sx sysctlmemlock; 9112429Sphk 92188232Sjhb#define SYSCTL_XLOCK() sx_xlock(&sysctllock) 93188232Sjhb#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 94188232Sjhb#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 95112107Sjhb#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 96216060Smdf#define SYSCTL_SLEEP(ch, wmesg, timo) \ 97216060Smdf sx_sleep(ch, &sysctllock, 0, wmesg, timo) 9893616Salfred 9962573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS); 10012429Sphk 10144078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */ 10212152Sphk 103188232Sjhbstatic int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 104188232Sjhb int recurse); 105188232Sjhb 10663212Sabialstatic struct sysctl_oid * 10763212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 10863212Sabial{ 10963212Sabial struct sysctl_oid *oidp; 11063212Sabial 111216060Smdf SYSCTL_ASSERT_XLOCKED(); 11263212Sabial SLIST_FOREACH(oidp, list, oid_link) { 11363212Sabial if (strcmp(oidp->oid_name, name) == 0) { 11463212Sabial return (oidp); 11563212Sabial } 11663212Sabial } 11763212Sabial return (NULL); 11863212Sabial} 11963212Sabial 12012623Sphk/* 12112623Sphk * Initialization of the MIB tree. 12212623Sphk * 12344078Sdfr * Order by number in each list. 12412623Sphk */ 125188232Sjhbvoid 126188232Sjhbsysctl_lock(void) 127188232Sjhb{ 12812429Sphk 129188232Sjhb SYSCTL_XLOCK(); 130188232Sjhb} 131188232Sjhb 13280338Sroamvoid 133188232Sjhbsysctl_unlock(void) 134188232Sjhb{ 135188232Sjhb 136188232Sjhb SYSCTL_XUNLOCK(); 137188232Sjhb} 138188232Sjhb 139188232Sjhbvoid 14080338Sroamsysctl_register_oid(struct sysctl_oid *oidp) 14112152Sphk{ 14244078Sdfr struct sysctl_oid_list *parent = oidp->oid_parent; 14344078Sdfr struct sysctl_oid *p; 14444078Sdfr struct sysctl_oid *q; 14512197Sbde 14644078Sdfr /* 14763212Sabial * First check if another oid with the same name already 14863212Sabial * exists in the parent's list. 14963212Sabial */ 150188232Sjhb SYSCTL_ASSERT_XLOCKED(); 15163212Sabial p = sysctl_find_oidname(oidp->oid_name, parent); 15263212Sabial if (p != NULL) { 15363212Sabial if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 15463212Sabial p->oid_refcnt++; 15563212Sabial return; 15663212Sabial } else { 15763212Sabial printf("can't re-use a leaf (%s)!\n", p->oid_name); 15863212Sabial return; 15963212Sabial } 16063212Sabial } 16163212Sabial /* 16244078Sdfr * If this oid has a number OID_AUTO, give it a number which 16380339Sroam * is greater than any current oid. 16480339Sroam * NOTE: DO NOT change the starting value here, change it in 16580339Sroam * <sys/sysctl.h>, and make sure it is at least 256 to 16680339Sroam * accomodate e.g. net.inet.raw as a static sysctl node. 16744078Sdfr */ 16844078Sdfr if (oidp->oid_number == OID_AUTO) { 16980339Sroam static int newoid = CTL_AUTO_START; 17071510Smckusick 17171510Smckusick oidp->oid_number = newoid++; 17271510Smckusick if (newoid == 0x7fffffff) 17371510Smckusick panic("out of oids"); 17444078Sdfr } 17584832Sroam#if 0 17684832Sroam else if (oidp->oid_number >= CTL_AUTO_START) { 17784832Sroam /* do not panic; this happens when unregistering sysctl sets */ 17884832Sroam printf("static sysctl oid too high: %d", oidp->oid_number); 17984832Sroam } 18084832Sroam#endif 18144078Sdfr 18244078Sdfr /* 18344078Sdfr * Insert the oid into the parent's list in order. 18444078Sdfr */ 18544078Sdfr q = NULL; 18644078Sdfr SLIST_FOREACH(p, parent, oid_link) { 18744078Sdfr if (oidp->oid_number < p->oid_number) 18844078Sdfr break; 18944078Sdfr q = p; 19044078Sdfr } 19144078Sdfr if (q) 19244078Sdfr SLIST_INSERT_AFTER(q, oidp, oid_link); 19344078Sdfr else 19444078Sdfr SLIST_INSERT_HEAD(parent, oidp, oid_link); 19512152Sphk} 19612131Sphk 19780338Sroamvoid 19880338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp) 19912152Sphk{ 200115391Smux struct sysctl_oid *p; 201115391Smux int error; 202115391Smux 203188232Sjhb SYSCTL_ASSERT_XLOCKED(); 204115391Smux error = ENOENT; 205115391Smux if (oidp->oid_number == OID_AUTO) { 206115391Smux error = EINVAL; 207115391Smux } else { 208115391Smux SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 209115391Smux if (p == oidp) { 210115391Smux SLIST_REMOVE(oidp->oid_parent, oidp, 211115391Smux sysctl_oid, oid_link); 212115391Smux error = 0; 213115391Smux break; 214115391Smux } 215115391Smux } 216115391Smux } 217115391Smux 218115391Smux /* 219115391Smux * This can happen when a module fails to register and is 220115391Smux * being unloaded afterwards. It should not be a panic() 221115391Smux * for normal use. 222115391Smux */ 223115391Smux if (error) 224115391Smux printf("%s: failed to unregister sysctl\n", __func__); 22544078Sdfr} 22612152Sphk 22763212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */ 22863212Sabialint 22963212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c) 23063212Sabial{ 23163212Sabial 23263212Sabial if (c == NULL) { 23363212Sabial return (EINVAL); 23463212Sabial } 235188232Sjhb 236188232Sjhb /* 237188232Sjhb * No locking here, the caller is responsible for not adding 238188232Sjhb * new nodes to a context until after this function has 239188232Sjhb * returned. 240188232Sjhb */ 24163212Sabial TAILQ_INIT(c); 24263212Sabial return (0); 24363212Sabial} 24463212Sabial 24563212Sabial/* Free the context, and destroy all dynamic oids registered in this context */ 24663212Sabialint 24763212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist) 24863212Sabial{ 24963212Sabial struct sysctl_ctx_entry *e, *e1; 25063212Sabial int error; 25163212Sabial 25263212Sabial error = 0; 25363212Sabial /* 25463212Sabial * First perform a "dry run" to check if it's ok to remove oids. 25563212Sabial * XXX FIXME 25663212Sabial * XXX This algorithm is a hack. But I don't know any 25763212Sabial * XXX better solution for now... 25863212Sabial */ 259188232Sjhb SYSCTL_XLOCK(); 26063212Sabial TAILQ_FOREACH(e, clist, link) { 261188232Sjhb error = sysctl_remove_oid_locked(e->entry, 0, 0); 26263212Sabial if (error) 26363212Sabial break; 26463212Sabial } 26563212Sabial /* 26663212Sabial * Restore deregistered entries, either from the end, 26763212Sabial * or from the place where error occured. 26863212Sabial * e contains the entry that was not unregistered 26963212Sabial */ 27063212Sabial if (error) 27163212Sabial e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 27263212Sabial else 27363212Sabial e1 = TAILQ_LAST(clist, sysctl_ctx_list); 27463212Sabial while (e1 != NULL) { 27563212Sabial sysctl_register_oid(e1->entry); 27663212Sabial e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 27763212Sabial } 278188232Sjhb if (error) { 279188232Sjhb SYSCTL_XUNLOCK(); 28063212Sabial return(EBUSY); 281188232Sjhb } 28263212Sabial /* Now really delete the entries */ 28363212Sabial e = TAILQ_FIRST(clist); 28463212Sabial while (e != NULL) { 28563212Sabial e1 = TAILQ_NEXT(e, link); 286188232Sjhb error = sysctl_remove_oid_locked(e->entry, 1, 0); 28763212Sabial if (error) 28863212Sabial panic("sysctl_remove_oid: corrupt tree, entry: %s", 28963212Sabial e->entry->oid_name); 29063212Sabial free(e, M_SYSCTLOID); 29163212Sabial e = e1; 29263212Sabial } 293188232Sjhb SYSCTL_XUNLOCK(); 29463212Sabial return (error); 29563212Sabial} 29663212Sabial 29763212Sabial/* Add an entry to the context */ 29863212Sabialstruct sysctl_ctx_entry * 29963212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 30063212Sabial{ 30163212Sabial struct sysctl_ctx_entry *e; 30263212Sabial 303188232Sjhb SYSCTL_ASSERT_XLOCKED(); 30463212Sabial if (clist == NULL || oidp == NULL) 30563212Sabial return(NULL); 306111119Simp e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 30763212Sabial e->entry = oidp; 30863212Sabial TAILQ_INSERT_HEAD(clist, e, link); 30963212Sabial return (e); 31063212Sabial} 31163212Sabial 31263212Sabial/* Find an entry in the context */ 31363212Sabialstruct sysctl_ctx_entry * 31463212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 31563212Sabial{ 31663212Sabial struct sysctl_ctx_entry *e; 31763212Sabial 318216060Smdf SYSCTL_ASSERT_XLOCKED(); 31963212Sabial if (clist == NULL || oidp == NULL) 32063212Sabial return(NULL); 32171999Sphk TAILQ_FOREACH(e, clist, link) { 32263212Sabial if(e->entry == oidp) 32363212Sabial return(e); 32463212Sabial } 32563212Sabial return (e); 32663212Sabial} 32763212Sabial 32844078Sdfr/* 32963212Sabial * Delete an entry from the context. 33063212Sabial * NOTE: this function doesn't free oidp! You have to remove it 33163212Sabial * with sysctl_remove_oid(). 33263212Sabial */ 33363212Sabialint 33463212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 33563212Sabial{ 33663212Sabial struct sysctl_ctx_entry *e; 33763212Sabial 33863212Sabial if (clist == NULL || oidp == NULL) 33963212Sabial return (EINVAL); 340188232Sjhb SYSCTL_XLOCK(); 34163212Sabial e = sysctl_ctx_entry_find(clist, oidp); 34263212Sabial if (e != NULL) { 34363212Sabial TAILQ_REMOVE(clist, e, link); 344188232Sjhb SYSCTL_XUNLOCK(); 34563212Sabial free(e, M_SYSCTLOID); 34663212Sabial return (0); 347188232Sjhb } else { 348188232Sjhb SYSCTL_XUNLOCK(); 34963212Sabial return (ENOENT); 350188232Sjhb } 35163212Sabial} 35263212Sabial 35363212Sabial/* 35463212Sabial * Remove dynamically created sysctl trees. 35563212Sabial * oidp - top of the tree to be removed 35663212Sabial * del - if 0 - just deregister, otherwise free up entries as well 35763212Sabial * recurse - if != 0 traverse the subtree to be deleted 35863212Sabial */ 35963212Sabialint 36063212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 36163212Sabial{ 362188232Sjhb int error; 363188232Sjhb 364188232Sjhb SYSCTL_XLOCK(); 365188232Sjhb error = sysctl_remove_oid_locked(oidp, del, recurse); 366188232Sjhb SYSCTL_XUNLOCK(); 367188232Sjhb return (error); 368188232Sjhb} 369188232Sjhb 370219819Sjeffint 371219819Sjeffsysctl_remove_name(struct sysctl_oid *parent, const char *name, 372219819Sjeff int del, int recurse) 373219819Sjeff{ 374219819Sjeff struct sysctl_oid *p, *tmp; 375219819Sjeff int error; 376219819Sjeff 377219819Sjeff error = ENOENT; 378219819Sjeff SYSCTL_XLOCK(); 379219819Sjeff SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) { 380219819Sjeff if (strcmp(p->oid_name, name) == 0) { 381219819Sjeff error = sysctl_remove_oid_locked(p, del, recurse); 382219819Sjeff break; 383219819Sjeff } 384219819Sjeff } 385219819Sjeff SYSCTL_XUNLOCK(); 386219819Sjeff 387219819Sjeff return (error); 388219819Sjeff} 389219819Sjeff 390219819Sjeff 391188232Sjhbstatic int 392188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 393188232Sjhb{ 394219819Sjeff struct sysctl_oid *p, *tmp; 39563212Sabial int error; 39663212Sabial 397188232Sjhb SYSCTL_ASSERT_XLOCKED(); 39863212Sabial if (oidp == NULL) 39963212Sabial return(EINVAL); 40063212Sabial if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 40163212Sabial printf("can't remove non-dynamic nodes!\n"); 40263212Sabial return (EINVAL); 40363212Sabial } 40463212Sabial /* 40563212Sabial * WARNING: normal method to do this should be through 40663212Sabial * sysctl_ctx_free(). Use recursing as the last resort 40763212Sabial * method to purge your sysctl tree of leftovers... 40863212Sabial * However, if some other code still references these nodes, 40963212Sabial * it will panic. 41063212Sabial */ 41163212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 41263212Sabial if (oidp->oid_refcnt == 1) { 413219819Sjeff SLIST_FOREACH_SAFE(p, 414219819Sjeff SYSCTL_CHILDREN(oidp), oid_link, tmp) { 41563212Sabial if (!recurse) 41663212Sabial return (ENOTEMPTY); 417188232Sjhb error = sysctl_remove_oid_locked(p, del, 418188232Sjhb recurse); 41963212Sabial if (error) 42063212Sabial return (error); 42163212Sabial } 42263212Sabial if (del) 42363212Sabial free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 42463212Sabial } 42563212Sabial } 42663212Sabial if (oidp->oid_refcnt > 1 ) { 42763212Sabial oidp->oid_refcnt--; 42863212Sabial } else { 42963212Sabial if (oidp->oid_refcnt == 0) { 43063212Sabial printf("Warning: bad oid_refcnt=%u (%s)!\n", 43163212Sabial oidp->oid_refcnt, oidp->oid_name); 43263212Sabial return (EINVAL); 43363212Sabial } 43463212Sabial sysctl_unregister_oid(oidp); 43563212Sabial if (del) { 436216060Smdf /* 437216060Smdf * Wait for all threads running the handler to drain. 438216060Smdf * This preserves the previous behavior when the 439216060Smdf * sysctl lock was held across a handler invocation, 440216060Smdf * and is necessary for module unload correctness. 441216060Smdf */ 442216060Smdf while (oidp->oid_running > 0) { 443216060Smdf oidp->oid_kind |= CTLFLAG_DYING; 444216060Smdf SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0); 445216060Smdf } 446141433Sphk if (oidp->oid_descr) 447248034Smarius free(__DECONST(char *, oidp->oid_descr), 448248034Smarius M_SYSCTLOID); 449248034Smarius free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID); 45063212Sabial free(oidp, M_SYSCTLOID); 45163212Sabial } 45263212Sabial } 45363212Sabial return (0); 45463212Sabial} 45563212Sabial/* 45663212Sabial * Create new sysctls at run time. 45763212Sabial * clist may point to a valid context initialized with sysctl_ctx_init(). 45863212Sabial */ 45963212Sabialstruct sysctl_oid * 46063212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 461219819Sjeff int number, const char *name, int kind, void *arg1, intptr_t arg2, 46270679Sjhb int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 46363212Sabial{ 46463212Sabial struct sysctl_oid *oidp; 46563212Sabial 46663212Sabial /* You have to hook up somewhere.. */ 46763212Sabial if (parent == NULL) 46863212Sabial return(NULL); 46963212Sabial /* Check if the node already exists, otherwise create it */ 470188232Sjhb SYSCTL_XLOCK(); 47163212Sabial oidp = sysctl_find_oidname(name, parent); 47263212Sabial if (oidp != NULL) { 47363212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 47463212Sabial oidp->oid_refcnt++; 47563212Sabial /* Update the context */ 47663212Sabial if (clist != NULL) 47763212Sabial sysctl_ctx_entry_add(clist, oidp); 478188232Sjhb SYSCTL_XUNLOCK(); 47963212Sabial return (oidp); 48063212Sabial } else { 481188232Sjhb SYSCTL_XUNLOCK(); 48263212Sabial printf("can't re-use a leaf (%s)!\n", name); 48363212Sabial return (NULL); 48463212Sabial } 48563212Sabial } 486111119Simp oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 48763212Sabial oidp->oid_parent = parent; 48863212Sabial SLIST_NEXT(oidp, oid_link) = NULL; 48963212Sabial oidp->oid_number = number; 49063212Sabial oidp->oid_refcnt = 1; 491248034Smarius oidp->oid_name = strdup(name, M_SYSCTLOID); 49263212Sabial oidp->oid_handler = handler; 49363212Sabial oidp->oid_kind = CTLFLAG_DYN | kind; 49463212Sabial if ((kind & CTLTYPE) == CTLTYPE_NODE) { 49563212Sabial /* Allocate space for children */ 496132776Skan SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list), 497132776Skan M_SYSCTLOID, M_WAITOK)); 49863212Sabial SLIST_INIT(SYSCTL_CHILDREN(oidp)); 499219819Sjeff oidp->oid_arg2 = arg2; 50063212Sabial } else { 50163212Sabial oidp->oid_arg1 = arg1; 50263212Sabial oidp->oid_arg2 = arg2; 50363212Sabial } 50463212Sabial oidp->oid_fmt = fmt; 505248034Smarius if (descr) 506248034Smarius oidp->oid_descr = strdup(descr, M_SYSCTLOID); 50763212Sabial /* Update the context, if used */ 50863212Sabial if (clist != NULL) 50963212Sabial sysctl_ctx_entry_add(clist, oidp); 51063212Sabial /* Register this oid */ 51163212Sabial sysctl_register_oid(oidp); 512188232Sjhb SYSCTL_XUNLOCK(); 51363212Sabial return (oidp); 51463212Sabial} 51563212Sabial 51663212Sabial/* 517174113Speter * Rename an existing oid. 518174113Speter */ 519174113Spetervoid 520174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 521174113Speter{ 522174113Speter char *newname; 523248034Smarius char *oldname; 524174113Speter 525248034Smarius newname = strdup(name, M_SYSCTLOID); 526188232Sjhb SYSCTL_XLOCK(); 527248034Smarius oldname = __DECONST(char *, oidp->oid_name); 528174113Speter oidp->oid_name = newname; 529188232Sjhb SYSCTL_XUNLOCK(); 530174113Speter free(oldname, M_SYSCTLOID); 531174113Speter} 532174113Speter 533174113Speter/* 534126319Sdes * Reparent an existing oid. 535126319Sdes */ 536126319Sdesint 537126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 538126319Sdes{ 539126319Sdes struct sysctl_oid *oidp; 540126319Sdes 541188232Sjhb SYSCTL_XLOCK(); 542188232Sjhb if (oid->oid_parent == parent) { 543188232Sjhb SYSCTL_XUNLOCK(); 544126319Sdes return (0); 545188232Sjhb } 546126319Sdes oidp = sysctl_find_oidname(oid->oid_name, parent); 547188232Sjhb if (oidp != NULL) { 548188232Sjhb SYSCTL_XUNLOCK(); 549126319Sdes return (EEXIST); 550188232Sjhb } 551126319Sdes sysctl_unregister_oid(oid); 552126319Sdes oid->oid_parent = parent; 553126319Sdes oid->oid_number = OID_AUTO; 554126319Sdes sysctl_register_oid(oid); 555188232Sjhb SYSCTL_XUNLOCK(); 556126319Sdes return (0); 557126319Sdes} 558126319Sdes 559126319Sdes/* 56044078Sdfr * Register the kernel's oids on startup. 56144078Sdfr */ 56278161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid); 56312152Sphk 56480338Sroamstatic void 56580338Sroamsysctl_register_all(void *arg) 56638869Sbde{ 56778161Speter struct sysctl_oid **oidp; 56878161Speter 569192125Sjhb sx_init(&sysctlmemlock, "sysctl mem"); 57093625Srwatson SYSCTL_INIT(); 571188232Sjhb SYSCTL_XLOCK(); 57278161Speter SET_FOREACH(oidp, sysctl_set) 57378161Speter sysctl_register_oid(*oidp); 574188232Sjhb SYSCTL_XUNLOCK(); 57538869Sbde} 57644078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 57744078Sdfr 57812623Sphk/* 57912623Sphk * "Staff-functions" 58012623Sphk * 58112650Sphk * These functions implement a presently undocumented interface 58212650Sphk * used by the sysctl program to walk the tree, and get the type 58312650Sphk * so it can print the value. 58412650Sphk * This interface is under work and consideration, and should probably 58512650Sphk * be killed with a big axe by the first person who can find the time. 58612650Sphk * (be aware though, that the proper interface isn't as obvious as it 58712650Sphk * may seem, there are various conflicting requirements. 58812650Sphk * 58912623Sphk * {0,0} printf the entire MIB-tree. 59012623Sphk * {0,1,...} return the name of the "..." OID. 59142467Sphk * {0,2,...} return the next OID. 59212623Sphk * {0,3} return the OID of the name in "new" 59312650Sphk * {0,4,...} return the kind & format info for the "..." OID. 59488006Sluigi * {0,5,...} return the description the "..." OID. 59512623Sphk */ 59612623Sphk 597136999Srwatson#ifdef SYSCTL_DEBUG 59812152Sphkstatic void 59944078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 60012152Sphk{ 60144078Sdfr int k; 60244078Sdfr struct sysctl_oid *oidp; 60312152Sphk 604216060Smdf SYSCTL_ASSERT_XLOCKED(); 60544078Sdfr SLIST_FOREACH(oidp, l, oid_link) { 60612152Sphk 60712152Sphk for (k=0; k<i; k++) 60812152Sphk printf(" "); 60912152Sphk 61044078Sdfr printf("%d %s ", oidp->oid_number, oidp->oid_name); 61112152Sphk 61212152Sphk printf("%c%c", 61344078Sdfr oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 61444078Sdfr oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 61512152Sphk 61644078Sdfr if (oidp->oid_handler) 61715241Sphk printf(" *Handler"); 61815241Sphk 61944078Sdfr switch (oidp->oid_kind & CTLTYPE) { 62012243Sphk case CTLTYPE_NODE: 62115241Sphk printf(" Node\n"); 62244078Sdfr if (!oidp->oid_handler) { 62312152Sphk sysctl_sysctl_debug_dump_node( 62444078Sdfr oidp->oid_arg1, i+2); 62512152Sphk } 62612152Sphk break; 62712152Sphk case CTLTYPE_INT: printf(" Int\n"); break; 628217616Smdf case CTLTYPE_UINT: printf(" u_int\n"); break; 629217616Smdf case CTLTYPE_LONG: printf(" Long\n"); break; 630217616Smdf case CTLTYPE_ULONG: printf(" u_long\n"); break; 63112152Sphk case CTLTYPE_STRING: printf(" String\n"); break; 632217616Smdf case CTLTYPE_U64: printf(" uint64_t\n"); break; 633217616Smdf case CTLTYPE_S64: printf(" int64_t\n"); break; 63412152Sphk case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 63512152Sphk default: printf("\n"); 63612152Sphk } 63712152Sphk 63812152Sphk } 63912152Sphk} 64012152Sphk 64112152Sphkstatic int 64262573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 64312152Sphk{ 64487024Speter int error; 64587024Speter 646164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 64787024Speter if (error) 648139483Spjd return (error); 649216060Smdf SYSCTL_XLOCK(); 65044078Sdfr sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 651216060Smdf SYSCTL_XUNLOCK(); 652139483Spjd return (ENOENT); 65312152Sphk} 65412152Sphk 65512152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 65612623Sphk 0, 0, sysctl_sysctl_debug, "-", ""); 657136999Srwatson#endif 65812152Sphk 65912623Sphkstatic int 66062573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 66112623Sphk{ 66212623Sphk int *name = (int *) arg1; 66312623Sphk u_int namelen = arg2; 66444078Sdfr int error = 0; 66544078Sdfr struct sysctl_oid *oid; 66644972Sphk struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 66712623Sphk char buf[10]; 66812131Sphk 669216060Smdf SYSCTL_XLOCK(); 67012623Sphk while (namelen) { 67112623Sphk if (!lsp) { 67241514Sarchie snprintf(buf,sizeof(buf),"%d",*name); 67312623Sphk if (req->oldidx) 67412623Sphk error = SYSCTL_OUT(req, ".", 1); 67512623Sphk if (!error) 67612623Sphk error = SYSCTL_OUT(req, buf, strlen(buf)); 67712623Sphk if (error) 678216060Smdf goto out; 67912623Sphk namelen--; 68012623Sphk name++; 68112623Sphk continue; 68212623Sphk } 68344972Sphk lsp2 = 0; 68444078Sdfr SLIST_FOREACH(oid, lsp, oid_link) { 68544078Sdfr if (oid->oid_number != *name) 68612623Sphk continue; 68712131Sphk 68812623Sphk if (req->oldidx) 68912623Sphk error = SYSCTL_OUT(req, ".", 1); 69012623Sphk if (!error) 69144078Sdfr error = SYSCTL_OUT(req, oid->oid_name, 69244078Sdfr strlen(oid->oid_name)); 69312623Sphk if (error) 694216060Smdf goto out; 69512623Sphk 69612623Sphk namelen--; 69712623Sphk name++; 69812623Sphk 69944972Sphk if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 70012623Sphk break; 70112623Sphk 70244078Sdfr if (oid->oid_handler) 70312623Sphk break; 70412623Sphk 705216058Smdf lsp2 = SYSCTL_CHILDREN(oid); 70612623Sphk break; 70712623Sphk } 70844972Sphk lsp = lsp2; 70912623Sphk } 710216060Smdf error = SYSCTL_OUT(req, "", 1); 711216060Smdf out: 712216060Smdf SYSCTL_XUNLOCK(); 713216060Smdf return (error); 71412623Sphk} 71512623Sphk 716224159Srwatson/* 717224159Srwatson * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 718224159Srwatson * capability mode. 719224159Srwatson */ 720224159Srwatsonstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_CAPRD, 721224159Srwatson sysctl_sysctl_name, ""); 72212623Sphk 72312623Sphkstatic int 72463978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 72544078Sdfr int *next, int *len, int level, struct sysctl_oid **oidpp) 72612623Sphk{ 72744078Sdfr struct sysctl_oid *oidp; 72812623Sphk 729216060Smdf SYSCTL_ASSERT_XLOCKED(); 73012623Sphk *len = level; 73144078Sdfr SLIST_FOREACH(oidp, lsp, oid_link) { 73244078Sdfr *next = oidp->oid_number; 73344078Sdfr *oidpp = oidp; 73412623Sphk 735101650Smux if (oidp->oid_kind & CTLFLAG_SKIP) 736101650Smux continue; 737101650Smux 73812623Sphk if (!namelen) { 73944078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 740139483Spjd return (0); 74144078Sdfr if (oidp->oid_handler) 74212623Sphk /* We really should call the handler here...*/ 743139483Spjd return (0); 744216058Smdf lsp = SYSCTL_CHILDREN(oidp); 74563978Speter if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 74644078Sdfr len, level+1, oidpp)) 747139483Spjd return (0); 748111260Srwatson goto emptynode; 74912623Sphk } 75012623Sphk 75144078Sdfr if (oidp->oid_number < *name) 75212623Sphk continue; 75312623Sphk 75444078Sdfr if (oidp->oid_number > *name) { 75544078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 756139483Spjd return (0); 75744078Sdfr if (oidp->oid_handler) 758139483Spjd return (0); 759216058Smdf lsp = SYSCTL_CHILDREN(oidp); 76063978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 76144078Sdfr next+1, len, level+1, oidpp)) 76212623Sphk return (0); 76315241Sphk goto next; 76412623Sphk } 76544078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 76612623Sphk continue; 76712623Sphk 76844078Sdfr if (oidp->oid_handler) 76912623Sphk continue; 77012623Sphk 771216058Smdf lsp = SYSCTL_CHILDREN(oidp); 77263978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 77344078Sdfr len, level+1, oidpp)) 77412623Sphk return (0); 77515241Sphk next: 77612623Sphk namelen = 1; 777111260Srwatson emptynode: 77812623Sphk *len = level; 77912623Sphk } 780139483Spjd return (1); 78112623Sphk} 78212623Sphk 78312623Sphkstatic int 78462573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 78512623Sphk{ 78612623Sphk int *name = (int *) arg1; 78712623Sphk u_int namelen = arg2; 78812623Sphk int i, j, error; 78912623Sphk struct sysctl_oid *oid; 79044078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 79112623Sphk int newoid[CTL_MAXNAME]; 79212623Sphk 793216060Smdf SYSCTL_XLOCK(); 79463978Speter i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 795216060Smdf SYSCTL_XUNLOCK(); 79612623Sphk if (i) 797139483Spjd return (ENOENT); 79812650Sphk error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 79912623Sphk return (error); 80012623Sphk} 80112623Sphk 802224159Srwatson/* 803224159Srwatson * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in 804224159Srwatson * capability mode. 805224159Srwatson */ 806224159Srwatsonstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD, 807224159Srwatson sysctl_sysctl_next, ""); 80812623Sphk 80912623Sphkstatic int 810189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 81112623Sphk{ 81244078Sdfr struct sysctl_oid *oidp; 81344078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 81412623Sphk char *p; 81512623Sphk 816216060Smdf SYSCTL_ASSERT_XLOCKED(); 817186564Sed 818248034Smarius for (*len = 0; *len < CTL_MAXNAME;) { 819248034Smarius p = strsep(&name, "."); 82012623Sphk 821248034Smarius oidp = SLIST_FIRST(lsp); 822248034Smarius for (;; oidp = SLIST_NEXT(oidp, oid_link)) { 823248034Smarius if (oidp == NULL) 824248034Smarius return (ENOENT); 825248034Smarius if (strcmp(p, oidp->oid_name) == 0) 826248034Smarius break; 82712623Sphk } 82844078Sdfr *oid++ = oidp->oid_number; 82912623Sphk (*len)++; 83012623Sphk 831248034Smarius if (name == NULL || *name == '\0') { 83244078Sdfr if (oidpp) 83344078Sdfr *oidpp = oidp; 83412623Sphk return (0); 83512623Sphk } 83612623Sphk 83744078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 83812623Sphk break; 83912623Sphk 84044078Sdfr if (oidp->oid_handler) 84112623Sphk break; 84212623Sphk 843216058Smdf lsp = SYSCTL_CHILDREN(oidp); 84412623Sphk } 845139483Spjd return (ENOENT); 84612623Sphk} 84712623Sphk 84812623Sphkstatic int 84962573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 85012623Sphk{ 85112623Sphk char *p; 852216066Smdf int error, oid[CTL_MAXNAME], len = 0; 85312623Sphk struct sysctl_oid *op = 0; 85412623Sphk 85512623Sphk if (!req->newlen) 856139483Spjd return (ENOENT); 85745140Sphk if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 85845140Sphk return (ENAMETOOLONG); 85912623Sphk 860111119Simp p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 86112623Sphk 86212623Sphk error = SYSCTL_IN(req, p, req->newlen); 86312623Sphk if (error) { 86412623Sphk free(p, M_SYSCTL); 86512623Sphk return (error); 86612623Sphk } 86712623Sphk 86812623Sphk p [req->newlen] = '\0'; 86912623Sphk 870216060Smdf SYSCTL_XLOCK(); 87112623Sphk error = name2oid(p, oid, &len, &op); 872216060Smdf SYSCTL_XUNLOCK(); 87312623Sphk 87412623Sphk free(p, M_SYSCTL); 87512623Sphk 87612623Sphk if (error) 87712623Sphk return (error); 87812623Sphk 87912650Sphk error = SYSCTL_OUT(req, oid, len * sizeof *oid); 88012623Sphk return (error); 88112623Sphk} 88212623Sphk 883224159Srwatson/* 884224159Srwatson * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in 885224159Srwatson * capability mode. 886224159Srwatson */ 887217555SmdfSYSCTL_PROC(_sysctl, 3, name2oid, 888224159Srwatson CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 889224159Srwatson | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 89012623Sphk 89112623Sphkstatic int 89262573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 89312623Sphk{ 89444078Sdfr struct sysctl_oid *oid; 89553977Sgreen int error; 89612623Sphk 897216060Smdf SYSCTL_XLOCK(); 89853977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 89953977Sgreen if (error) 900216060Smdf goto out; 90112623Sphk 902216060Smdf if (oid->oid_fmt == NULL) { 903216060Smdf error = ENOENT; 904216060Smdf goto out; 905216060Smdf } 90653977Sgreen error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 90753977Sgreen if (error) 908216060Smdf goto out; 90953977Sgreen error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 910216060Smdf out: 911216060Smdf SYSCTL_XUNLOCK(); 91212650Sphk return (error); 91312623Sphk} 91412623Sphk 91542467Sphk 916224159Srwatsonstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 917187864Sed sysctl_sysctl_oidfmt, ""); 91812623Sphk 91988006Sluigistatic int 92088006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 92188006Sluigi{ 92288006Sluigi struct sysctl_oid *oid; 92388006Sluigi int error; 92488006Sluigi 925216060Smdf SYSCTL_XLOCK(); 92688006Sluigi error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 92788006Sluigi if (error) 928216060Smdf goto out; 92988006Sluigi 930216060Smdf if (oid->oid_descr == NULL) { 931216060Smdf error = ENOENT; 932216060Smdf goto out; 933216060Smdf } 934141433Sphk error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 935216060Smdf out: 936216060Smdf SYSCTL_XUNLOCK(); 93788006Sluigi return (error); 93888006Sluigi} 93988006Sluigi 940224159Srwatsonstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_CAPRD, 941224159Srwatson sysctl_sysctl_oiddescr, ""); 94288006Sluigi 94312243Sphk/* 94412623Sphk * Default "handler" functions. 94512623Sphk */ 94612623Sphk 94712623Sphk/* 94842095Sdfr * Handle an int, signed or unsigned. 94912243Sphk * Two cases: 95012243Sphk * a variable: point arg1 at it. 95112243Sphk * a constant: pass it in arg2. 95212243Sphk */ 95312243Sphk 95411865Sphkint 95562573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS) 95611863Sphk{ 957100833Struckman int tmpout, error = 0; 95811863Sphk 959100833Struckman /* 960100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 961100833Struckman */ 96212243Sphk if (arg1) 963100833Struckman tmpout = *(int *)arg1; 96420506Sbde else 965100833Struckman tmpout = arg2; 966100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 96711863Sphk 96812243Sphk if (error || !req->newptr) 96912243Sphk return (error); 97011863Sphk 97112243Sphk if (!arg1) 97212243Sphk error = EPERM; 97312243Sphk else 97412243Sphk error = SYSCTL_IN(req, arg1, sizeof(int)); 97512243Sphk return (error); 97611863Sphk} 97711863Sphk 97812243Sphk/* 979155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks. 980195699Srwatson * Note: this is used by TCP. 981155758Sandre */ 982155758Sandre 983155758Sandreint 984155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 985155758Sandre{ 986155758Sandre int error, s, tt; 987155758Sandre 988191688Szec tt = *(int *)arg1; 989155758Sandre s = (int)((int64_t)tt * 1000 / hz); 990155758Sandre 991155758Sandre error = sysctl_handle_int(oidp, &s, 0, req); 992155758Sandre if (error || !req->newptr) 993155758Sandre return (error); 994155758Sandre 995155758Sandre tt = (int)((int64_t)s * hz / 1000); 996155758Sandre if (tt < 1) 997155758Sandre return (EINVAL); 998155758Sandre 999191688Szec *(int *)arg1 = tt; 1000155758Sandre return (0); 1001155758Sandre} 1002155758Sandre 1003155758Sandre 1004155758Sandre/* 1005247656Smarius * Handle a long, signed or unsigned. 1006247656Smarius * Two cases: 1007247656Smarius * a variable: point arg1 at it. 1008247656Smarius * a constant: pass it in arg2. 100938517Sdfr */ 101038517Sdfr 101138517Sdfrint 101262573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS) 101338517Sdfr{ 101438517Sdfr int error = 0; 1015136404Speter long tmplong; 1016136404Speter#ifdef SCTL_MASK32 1017136404Speter int tmpint; 1018136404Speter#endif 101938517Sdfr 1020100833Struckman /* 1021100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 1022100833Struckman */ 1023247656Smarius if (arg1) 1024247656Smarius tmplong = *(long *)arg1; 1025247656Smarius else 1026247656Smarius tmplong = arg2; 1027136404Speter#ifdef SCTL_MASK32 1028136404Speter if (req->flags & SCTL_MASK32) { 1029136404Speter tmpint = tmplong; 1030136404Speter error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 1031136404Speter } else 1032136404Speter#endif 1033136404Speter error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 103438517Sdfr 103538517Sdfr if (error || !req->newptr) 103638517Sdfr return (error); 103738517Sdfr 1038247656Smarius if (!arg1) 1039247656Smarius error = EPERM; 1040136404Speter#ifdef SCTL_MASK32 1041247656Smarius else if (req->flags & SCTL_MASK32) { 1042136404Speter error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1043136404Speter *(long *)arg1 = (long)tmpint; 1044247656Smarius } 1045136404Speter#endif 1046247656Smarius else 1047136404Speter error = SYSCTL_IN(req, arg1, sizeof(long)); 104838517Sdfr return (error); 104938517Sdfr} 105038517Sdfr 105138517Sdfr/* 1052247656Smarius * Handle a 64 bit int, signed or unsigned. 1053247656Smarius * Two cases: 1054247656Smarius * a variable: point arg1 at it. 1055247656Smarius * a constant: pass it in arg2. 1056170288Sdwmalone */ 1057170288Sdwmaloneint 1058217616Smdfsysctl_handle_64(SYSCTL_HANDLER_ARGS) 1059170288Sdwmalone{ 1060170288Sdwmalone int error = 0; 1061170288Sdwmalone uint64_t tmpout; 1062170288Sdwmalone 1063170288Sdwmalone /* 1064170288Sdwmalone * Attempt to get a coherent snapshot by making a copy of the data. 1065170288Sdwmalone */ 1066247656Smarius if (arg1) 1067247656Smarius tmpout = *(uint64_t *)arg1; 1068247656Smarius else 1069247656Smarius tmpout = arg2; 1070170288Sdwmalone error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1071170288Sdwmalone 1072170288Sdwmalone if (error || !req->newptr) 1073170288Sdwmalone return (error); 1074170288Sdwmalone 1075247656Smarius if (!arg1) 1076247656Smarius error = EPERM; 1077247656Smarius else 1078247656Smarius error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1079170288Sdwmalone return (error); 1080170288Sdwmalone} 1081170288Sdwmalone 1082170288Sdwmalone/* 108312243Sphk * Handle our generic '\0' terminated 'C' string. 108412243Sphk * Two cases: 108512243Sphk * a variable string: point arg1 at it, arg2 is max length. 108612243Sphk * a constant string: point arg1 at it, arg2 is zero. 108712243Sphk */ 108812243Sphk 108911865Sphkint 109062573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS) 109111863Sphk{ 109212243Sphk int error=0; 1093100833Struckman char *tmparg; 1094100833Struckman size_t outlen; 109511863Sphk 1096100833Struckman /* 1097100833Struckman * Attempt to get a coherent snapshot by copying to a 1098100833Struckman * temporary kernel buffer. 1099100833Struckman */ 1100100833Struckmanretry: 1101100833Struckman outlen = strlen((char *)arg1)+1; 1102111119Simp tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 1103105354Srobert 1104105354Srobert if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 1105100833Struckman free(tmparg, M_SYSCTLTMP); 1106100833Struckman goto retry; 1107100833Struckman } 1108105354Srobert 1109100833Struckman error = SYSCTL_OUT(req, tmparg, outlen); 1110100833Struckman free(tmparg, M_SYSCTLTMP); 111111863Sphk 111245140Sphk if (error || !req->newptr) 111312243Sphk return (error); 111411863Sphk 111545140Sphk if ((req->newlen - req->newidx) >= arg2) { 111645140Sphk error = EINVAL; 111712243Sphk } else { 111812243Sphk arg2 = (req->newlen - req->newidx); 111912243Sphk error = SYSCTL_IN(req, arg1, arg2); 112012243Sphk ((char *)arg1)[arg2] = '\0'; 112111863Sphk } 112212131Sphk 112312131Sphk return (error); 112411863Sphk} 112511863Sphk 112612243Sphk/* 112712243Sphk * Handle any kind of opaque data. 112812243Sphk * arg1 points to it, arg2 is the size. 112912243Sphk */ 113012243Sphk 113111865Sphkint 113262573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 113311863Sphk{ 1134120803Sbms int error, tries; 1135120803Sbms u_int generation; 1136120813Sbms struct sysctl_req req2; 113712243Sphk 1138100833Struckman /* 1139120803Sbms * Attempt to get a coherent snapshot, by using the thread 1140120803Sbms * pre-emption counter updated from within mi_switch() to 1141120803Sbms * determine if we were pre-empted during a bcopy() or 1142120803Sbms * copyout(). Make 3 attempts at doing this before giving up. 1143120803Sbms * If we encounter an error, stop immediately. 1144100833Struckman */ 1145120803Sbms tries = 0; 1146120813Sbms req2 = *req; 1147120813Sbmsretry: 1148120813Sbms generation = curthread->td_generation; 1149120813Sbms error = SYSCTL_OUT(req, arg1, arg2); 1150120813Sbms if (error) 1151120813Sbms return (error); 1152120813Sbms tries++; 1153120813Sbms if (generation != curthread->td_generation && tries < 3) { 1154120813Sbms *req = req2; 1155120813Sbms goto retry; 1156120813Sbms } 115712243Sphk 115812243Sphk error = SYSCTL_IN(req, arg1, arg2); 115912243Sphk 116012243Sphk return (error); 116112243Sphk} 116212243Sphk 116312260Sphk/* 116412260Sphk * Transfer functions to/from kernel space. 116512260Sphk * XXX: rather untested at this point 116612260Sphk */ 116712260Sphkstatic int 116838517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 116912243Sphk{ 117038517Sdfr size_t i = 0; 117112260Sphk 117212260Sphk if (req->oldptr) { 117338517Sdfr i = l; 117473971Stmm if (req->oldlen <= req->oldidx) 117573971Stmm i = 0; 117673971Stmm else 117773971Stmm if (i > req->oldlen - req->oldidx) 117873971Stmm i = req->oldlen - req->oldidx; 117912260Sphk if (i > 0) 118017971Sbde bcopy(p, (char *)req->oldptr + req->oldidx, i); 118112243Sphk } 1182192144Skib req->oldidx += l; 118316282Snate if (req->oldptr && i != l) 118411863Sphk return (ENOMEM); 118512260Sphk return (0); 118612243Sphk} 118712243Sphk 118812260Sphkstatic int 118938517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 119012243Sphk{ 119112260Sphk if (!req->newptr) 1192139483Spjd return (0); 119312260Sphk if (req->newlen - req->newidx < l) 119411863Sphk return (EINVAL); 119517971Sbde bcopy((char *)req->newptr + req->newidx, p, l); 119612243Sphk req->newidx += l; 119712131Sphk return (0); 119811863Sphk} 119911863Sphk 120016282Snateint 120183366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1202136404Speter size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 120316282Snate{ 120416282Snate int error = 0; 120516282Snate struct sysctl_req req; 120616282Snate 120716282Snate bzero(&req, sizeof req); 120816282Snate 120986183Srwatson req.td = td; 1210136404Speter req.flags = flags; 121116282Snate 121216282Snate if (oldlenp) { 121316282Snate req.oldlen = *oldlenp; 121416282Snate } 1215127052Struckman req.validlen = req.oldlen; 121616282Snate 121716282Snate if (old) { 121816282Snate req.oldptr= old; 121916282Snate } 122016282Snate 122177646Sdd if (new != NULL) { 122216282Snate req.newlen = newlen; 122316282Snate req.newptr = new; 122416282Snate } 122516282Snate 122616282Snate req.oldfunc = sysctl_old_kernel; 122716282Snate req.newfunc = sysctl_new_kernel; 1228217915Smdf req.lock = REQ_UNWIRED; 122916282Snate 1230216060Smdf SYSCTL_XLOCK(); 123116282Snate error = sysctl_root(0, name, namelen, &req); 1232216060Smdf SYSCTL_XUNLOCK(); 1233120813Sbms 1234127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1235127052Struckman vsunlock(req.oldptr, req.validlen); 123616282Snate 123716282Snate if (error && error != ENOMEM) 123816282Snate return (error); 123916282Snate 124016282Snate if (retval) { 1241127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1242127052Struckman *retval = req.validlen; 124316282Snate else 124416282Snate *retval = req.oldidx; 124516282Snate } 124616282Snate return (error); 124716282Snate} 124816282Snate 124976834Sjlemonint 125083366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1251136404Speter void *new, size_t newlen, size_t *retval, int flags) 125276834Sjlemon{ 125376834Sjlemon int oid[CTL_MAXNAME]; 125478620Smjacob size_t oidlen, plen; 125578620Smjacob int error; 125676834Sjlemon 125776834Sjlemon oid[0] = 0; /* sysctl internal magic */ 125876834Sjlemon oid[1] = 3; /* name2oid */ 125976834Sjlemon oidlen = sizeof(oid); 126076834Sjlemon 126183366Sjulian error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1262136404Speter (void *)name, strlen(name), &plen, flags); 126376834Sjlemon if (error) 126476834Sjlemon return (error); 126576834Sjlemon 126683366Sjulian error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1267136404Speter new, newlen, retval, flags); 126876834Sjlemon return (error); 126976834Sjlemon} 127076834Sjlemon 127112260Sphk/* 127212260Sphk * Transfer function to/from user space. 127312260Sphk */ 127412260Sphkstatic int 127538517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 127612243Sphk{ 1277126253Struckman size_t i, len, origidx; 1278233647Salc int error; 127912243Sphk 1280126253Struckman origidx = req->oldidx; 1281192144Skib req->oldidx += l; 1282192144Skib if (req->oldptr == NULL) 1283126253Struckman return (0); 1284148864Scsjp /* 1285148864Scsjp * If we have not wired the user supplied buffer and we are currently 1286148864Scsjp * holding locks, drop a witness warning, as it's possible that 1287148864Scsjp * write operations to the user page can sleep. 1288148864Scsjp */ 1289148864Scsjp if (req->lock != REQ_WIRED) 1290111883Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1291111883Sjhb "sysctl_old_user()"); 1292126253Struckman i = l; 1293127052Struckman len = req->validlen; 1294126253Struckman if (len <= origidx) 1295126253Struckman i = 0; 1296126253Struckman else { 1297126253Struckman if (i > len - origidx) 1298126253Struckman i = len - origidx; 1299233647Salc if (req->lock == REQ_WIRED) { 1300233647Salc error = copyout_nofault(p, (char *)req->oldptr + 1301233647Salc origidx, i); 1302233647Salc } else 1303233647Salc error = copyout(p, (char *)req->oldptr + origidx, i); 1304233647Salc if (error != 0) 1305233647Salc return (error); 130612260Sphk } 1307126253Struckman if (i < l) 130812243Sphk return (ENOMEM); 130912260Sphk return (0); 131012243Sphk} 131112243Sphk 131212260Sphkstatic int 131338517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l) 131412243Sphk{ 131512285Sphk int error; 131612260Sphk 131712260Sphk if (!req->newptr) 1318139483Spjd return (0); 131912260Sphk if (req->newlen - req->newidx < l) 132012243Sphk return (EINVAL); 1321148873Scsjp WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1322148873Scsjp "sysctl_new_user()"); 132317971Sbde error = copyin((char *)req->newptr + req->newidx, p, l); 132412243Sphk req->newidx += l; 132512243Sphk return (error); 132612243Sphk} 132712243Sphk 1328100487Struckman/* 1329100487Struckman * Wire the user space destination buffer. If set to a value greater than 1330100487Struckman * zero, the len parameter limits the maximum amount of wired memory. 1331100487Struckman */ 1332126253Struckmanint 1333100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1334100487Struckman{ 1335126253Struckman int ret; 1336192160Sdes size_t wiredlen; 1337126253Struckman 1338126253Struckman wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1339126253Struckman ret = 0; 1340217915Smdf if (req->lock != REQ_WIRED && req->oldptr && 1341120781Sbms req->oldfunc == sysctl_old_user) { 1342127050Struckman if (wiredlen != 0) { 1343127050Struckman ret = vslock(req->oldptr, wiredlen); 1344130327Sgreen if (ret != 0) { 1345130327Sgreen if (ret != ENOMEM) 1346130327Sgreen return (ret); 1347130327Sgreen wiredlen = 0; 1348130327Sgreen } 1349126253Struckman } 1350127050Struckman req->lock = REQ_WIRED; 1351127052Struckman req->validlen = wiredlen; 1352100487Struckman } 1353127050Struckman return (0); 1354100487Struckman} 1355100487Struckman 13561541Srgrimesint 135753977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 135853977Sgreen int *nindx, struct sysctl_req *req) 135912131Sphk{ 1360216059Smdf struct sysctl_oid_list *lsp; 136144078Sdfr struct sysctl_oid *oid; 136253977Sgreen int indx; 136312131Sphk 1364216060Smdf SYSCTL_ASSERT_XLOCKED(); 1365216059Smdf lsp = &sysctl__children; 136612131Sphk indx = 0; 1367216059Smdf while (indx < CTL_MAXNAME) { 1368216059Smdf SLIST_FOREACH(oid, lsp, oid_link) { 1369216059Smdf if (oid->oid_number == name[indx]) 1370216059Smdf break; 1371216059Smdf } 1372216059Smdf if (oid == NULL) 1373216059Smdf return (ENOENT); 1374216059Smdf 1375216059Smdf indx++; 1376216059Smdf if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1377216059Smdf if (oid->oid_handler != NULL || indx == namelen) { 137853977Sgreen *noid = oid; 137953977Sgreen if (nindx != NULL) 138053977Sgreen *nindx = indx; 1381216060Smdf KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1382216060Smdf ("%s found DYING node %p", __func__, oid)); 138353977Sgreen return (0); 138412131Sphk } 1385216059Smdf lsp = SYSCTL_CHILDREN(oid); 1386216059Smdf } else if (indx == namelen) { 1387216059Smdf *noid = oid; 1388216059Smdf if (nindx != NULL) 1389216059Smdf *nindx = indx; 1390216060Smdf KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0, 1391216060Smdf ("%s found DYING node %p", __func__, oid)); 1392216059Smdf return (0); 139312131Sphk } else { 1394216059Smdf return (ENOTDIR); 139512131Sphk } 139612131Sphk } 139753977Sgreen return (ENOENT); 139853977Sgreen} 139953977Sgreen 140053977Sgreen/* 140153977Sgreen * Traverse our tree, and find the right node, execute whatever it points 140253977Sgreen * to, and return the resulting error code. 140353977Sgreen */ 140453977Sgreen 1405104094Sphkstatic int 140662573Sphksysctl_root(SYSCTL_HANDLER_ARGS) 140753977Sgreen{ 140853977Sgreen struct sysctl_oid *oid; 1409109246Sdillon int error, indx, lvl; 141053977Sgreen 1411216060Smdf SYSCTL_ASSERT_XLOCKED(); 1412186564Sed 141353977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 141453977Sgreen if (error) 141553977Sgreen return (error); 141653977Sgreen 141753977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 141853977Sgreen /* 141953977Sgreen * You can't call a sysctl when it's a node, but has 142053977Sgreen * no handler. Inform the user that it's a node. 142153977Sgreen * The indx may or may not be the same as namelen. 142253977Sgreen */ 142353977Sgreen if (oid->oid_handler == NULL) 142453977Sgreen return (EISDIR); 142553977Sgreen } 142653977Sgreen 142783968Srwatson /* Is this sysctl writable? */ 142883968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 142912131Sphk return (EPERM); 143012131Sphk 143192953Srwatson KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 143292953Srwatson 1433224159Srwatson#ifdef CAPABILITY_MODE 1434224159Srwatson /* 1435224159Srwatson * If the process is in capability mode, then don't permit reading or 1436224159Srwatson * writing unless specifically granted for the node. 1437224159Srwatson */ 1438224159Srwatson if (IN_CAPABILITY_MODE(req->td)) { 1439224159Srwatson if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) 1440224159Srwatson return (EPERM); 1441224159Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR)) 1442224159Srwatson return (EPERM); 1443224159Srwatson } 1444224159Srwatson#endif 1445224159Srwatson 144683968Srwatson /* Is this sysctl sensitive to securelevels? */ 144783968Srwatson if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1448109246Sdillon lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1449109246Sdillon error = securelevel_gt(req->td->td_ucred, lvl); 145092953Srwatson if (error) 145192953Srwatson return (error); 145283968Srwatson } 145312910Sphk 145483968Srwatson /* Is this sysctl writable by only privileged users? */ 145583968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1456196176Sbz int priv; 1457196176Sbz 145892953Srwatson if (oid->oid_kind & CTLFLAG_PRISON) 1459196176Sbz priv = PRIV_SYSCTL_WRITEJAIL; 1460196176Sbz#ifdef VIMAGE 1461196176Sbz else if ((oid->oid_kind & CTLFLAG_VNET) && 1462196176Sbz prison_owns_vnet(req->td->td_ucred)) 1463196176Sbz priv = PRIV_SYSCTL_WRITEJAIL; 1464196176Sbz#endif 146592953Srwatson else 1466196176Sbz priv = PRIV_SYSCTL_WRITE; 1467196176Sbz error = priv_check(req->td, priv); 146892953Srwatson if (error) 146992953Srwatson return (error); 147083968Srwatson } 147183968Srwatson 147244078Sdfr if (!oid->oid_handler) 1473139483Spjd return (EINVAL); 147412131Sphk 1475126121Spjd if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1476132776Skan arg1 = (int *)arg1 + indx; 1477126121Spjd arg2 -= indx; 1478126121Spjd } else { 1479126121Spjd arg1 = oid->oid_arg1; 1480126121Spjd arg2 = oid->oid_arg2; 1481126121Spjd } 1482126121Spjd#ifdef MAC 1483172930Srwatson error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1484126121Spjd req); 1485126121Spjd if (error != 0) 1486126121Spjd return (error); 1487126121Spjd#endif 1488216060Smdf oid->oid_running++; 1489216060Smdf SYSCTL_XUNLOCK(); 1490216060Smdf 1491187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1492187656Sjhb mtx_lock(&Giant); 1493126121Spjd error = oid->oid_handler(oid, arg1, arg2, req); 1494187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1495187656Sjhb mtx_unlock(&Giant); 1496126121Spjd 1497216060Smdf KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 1498216060Smdf 1499216060Smdf SYSCTL_XLOCK(); 1500216060Smdf oid->oid_running--; 1501216060Smdf if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0) 1502216060Smdf wakeup(&oid->oid_running); 150353977Sgreen return (error); 150412131Sphk} 150512131Sphk 150612221Sbde#ifndef _SYS_SYSPROTO_H_ 150712171Sphkstruct sysctl_args { 150812171Sphk int *name; 150912171Sphk u_int namelen; 151012171Sphk void *old; 151112171Sphk size_t *oldlenp; 151212171Sphk void *new; 151312171Sphk size_t newlen; 151412171Sphk}; 151512221Sbde#endif 151612131Sphkint 1517225617Skmacysys___sysctl(struct thread *td, struct sysctl_args *uap) 15181541Srgrimes{ 1519188232Sjhb int error, i, name[CTL_MAXNAME]; 152038517Sdfr size_t j; 15211541Srgrimes 15221541Srgrimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 15231541Srgrimes return (EINVAL); 152411863Sphk 15253308Sphk error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 15263308Sphk if (error) 15271541Srgrimes return (error); 15281541Srgrimes 152983366Sjulian error = userland_sysctl(td, name, uap->namelen, 153012171Sphk uap->old, uap->oldlenp, 0, 1531136404Speter uap->new, uap->newlen, &j, 0); 153212260Sphk if (error && error != ENOMEM) 1533186564Sed return (error); 1534186664Sed if (uap->oldlenp) { 1535188232Sjhb i = copyout(&j, uap->oldlenp, sizeof(j)); 1536186664Sed if (i) 1537186664Sed return (i); 1538186664Sed } 153912260Sphk return (error); 154012171Sphk} 154112171Sphk 154212171Sphk/* 154312171Sphk * This is used from various compatibility syscalls too. That's why name 154412171Sphk * must be in kernel space. 154512171Sphk */ 154612171Sphkint 154783366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1548136404Speter size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1549136404Speter int flags) 155012171Sphk{ 1551192125Sjhb int error = 0, memlocked; 1552127052Struckman struct sysctl_req req; 155312171Sphk 155412243Sphk bzero(&req, sizeof req); 155512243Sphk 155686183Srwatson req.td = td; 1557136404Speter req.flags = flags; 155812285Sphk 155912171Sphk if (oldlenp) { 156012171Sphk if (inkernel) { 156112243Sphk req.oldlen = *oldlenp; 156212171Sphk } else { 156312260Sphk error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 156412171Sphk if (error) 156512171Sphk return (error); 156612171Sphk } 156712171Sphk } 1568127052Struckman req.validlen = req.oldlen; 156912171Sphk 157012243Sphk if (old) { 157152644Sphk if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 157212243Sphk return (EFAULT); 157312243Sphk req.oldptr= old; 157412243Sphk } 157512131Sphk 157677646Sdd if (new != NULL) { 1577172038Srwatson if (!useracc(new, newlen, VM_PROT_READ)) 157812243Sphk return (EFAULT); 157912243Sphk req.newlen = newlen; 158012243Sphk req.newptr = new; 158111863Sphk } 158212131Sphk 158312243Sphk req.oldfunc = sysctl_old_user; 158412243Sphk req.newfunc = sysctl_new_user; 1585217915Smdf req.lock = REQ_UNWIRED; 158611863Sphk 1587189707Sjhb#ifdef KTRACE 1588189707Sjhb if (KTRPOINT(curthread, KTR_SYSCTL)) 1589189707Sjhb ktrsysctl(name, namelen); 1590189707Sjhb#endif 1591192125Sjhb 1592192125Sjhb if (req.oldlen > PAGE_SIZE) { 1593192125Sjhb memlocked = 1; 1594192125Sjhb sx_xlock(&sysctlmemlock); 1595192125Sjhb } else 1596192125Sjhb memlocked = 0; 1597194252Sjamie CURVNET_SET(TD_TO_VNET(td)); 159812429Sphk 1599185983Skib for (;;) { 1600127052Struckman req.oldidx = 0; 1601127052Struckman req.newidx = 0; 1602216060Smdf SYSCTL_XLOCK(); 1603127052Struckman error = sysctl_root(0, name, namelen, &req); 1604216060Smdf SYSCTL_XUNLOCK(); 1605185983Skib if (error != EAGAIN) 1606185983Skib break; 1607221829Smdf kern_yield(PRI_USER); 1608185983Skib } 160912243Sphk 1610186564Sed CURVNET_RESTORE(); 1611186564Sed 1612127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1613127052Struckman vsunlock(req.oldptr, req.validlen); 1614192125Sjhb if (memlocked) 1615192125Sjhb sx_xunlock(&sysctlmemlock); 161612429Sphk 161712260Sphk if (error && error != ENOMEM) 161812260Sphk return (error); 161912260Sphk 162012260Sphk if (retval) { 1621127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1622127052Struckman *retval = req.validlen; 162312260Sphk else 162412260Sphk *retval = req.oldidx; 162511863Sphk } 162612260Sphk return (error); 16271541Srgrimes} 1628212750Smdf 1629212750Smdf/* 1630217916Smdf * Drain into a sysctl struct. The user buffer should be wired if a page 1631217916Smdf * fault would cause issue. 1632212750Smdf */ 1633212750Smdfstatic int 1634212750Smdfsbuf_sysctl_drain(void *arg, const char *data, int len) 1635212750Smdf{ 1636212750Smdf struct sysctl_req *req = arg; 1637212750Smdf int error; 1638212750Smdf 1639212750Smdf error = SYSCTL_OUT(req, data, len); 1640212750Smdf KASSERT(error >= 0, ("Got unexpected negative value %d", error)); 1641212750Smdf return (error == 0 ? len : -error); 1642212750Smdf} 1643212750Smdf 1644212750Smdfstruct sbuf * 1645212750Smdfsbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, 1646212750Smdf struct sysctl_req *req) 1647212750Smdf{ 1648212750Smdf 1649212750Smdf s = sbuf_new(s, buf, length, SBUF_FIXEDLEN); 1650212750Smdf sbuf_set_drain(s, sbuf_sysctl_drain, req); 1651212750Smdf return (s); 1652212750Smdf} 1653