kern_sysctl.c revision 192144
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: head/sys/kern/kern_sysctl.c 192144 2009-05-15 14:41:44Z kib $"); 40116182Sobrien 4131778Seivind#include "opt_compat.h" 42189707Sjhb#include "opt_ktrace.h" 43106025Srwatson#include "opt_mac.h" 4431778Seivind 451541Srgrimes#include <sys/param.h> 4648274Speter#include <sys/systm.h> 4748274Speter#include <sys/kernel.h> 481541Srgrimes#include <sys/sysctl.h> 4912623Sphk#include <sys/malloc.h> 50164033Srwatson#include <sys/priv.h> 5112662Sdg#include <sys/proc.h> 5282746Sdillon#include <sys/lock.h> 5382746Sdillon#include <sys/mutex.h> 5493616Salfred#include <sys/sx.h> 5515103Sphk#include <sys/sysproto.h> 56185983Skib#include <sys/uio.h> 57185348Szec#include <sys/vimage.h> 58189707Sjhb#ifdef KTRACE 59189707Sjhb#include <sys/ktrace.h> 60189707Sjhb#endif 61163606Srwatson 62163606Srwatson#include <security/mac/mac_framework.h> 63163606Srwatson 6412645Sbde#include <vm/vm.h> 6512662Sdg#include <vm/vm_extern.h> 6612645Sbde 6730354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 6863212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 69100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 7030309Sphk 7112429Sphk/* 72188232Sjhb * The sysctllock protects the MIB tree. It also protects sysctl 73188232Sjhb * contexts used with dynamic sysctls. The sysctl_register_oid() and 74188232Sjhb * sysctl_unregister_oid() routines require the sysctllock to already 75188232Sjhb * be held, so the sysctl_lock() and sysctl_unlock() routines are 76188232Sjhb * provided for the few places in the kernel which need to use that 77188232Sjhb * API rather than using the dynamic API. Use of the dynamic API is 78188232Sjhb * strongly encouraged for most code. 79188232Sjhb * 80192125Sjhb * The sysctlmemlock is used to limit the amount of user memory wired for 81192125Sjhb * sysctl requests. This is implemented by serializing any userland 82192125Sjhb * sysctl requests larger than a single page via an exclusive lock. 8312429Sphk */ 8493625Srwatsonstatic struct sx sysctllock; 85192125Sjhbstatic struct sx sysctlmemlock; 8612429Sphk 87188232Sjhb#define SYSCTL_SLOCK() sx_slock(&sysctllock) 88188232Sjhb#define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock) 89188232Sjhb#define SYSCTL_XLOCK() sx_xlock(&sysctllock) 90188232Sjhb#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 91188232Sjhb#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 92188232Sjhb#define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED) 93112107Sjhb#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 9493616Salfred 9562573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS); 9612429Sphk 9744078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */ 9812152Sphk 99188232Sjhbstatic int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 100188232Sjhb int recurse); 101188232Sjhb 10263212Sabialstatic struct sysctl_oid * 10363212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 10463212Sabial{ 10563212Sabial struct sysctl_oid *oidp; 10663212Sabial 107188232Sjhb SYSCTL_ASSERT_LOCKED(); 10863212Sabial SLIST_FOREACH(oidp, list, oid_link) { 10963212Sabial if (strcmp(oidp->oid_name, name) == 0) { 11063212Sabial return (oidp); 11163212Sabial } 11263212Sabial } 11363212Sabial return (NULL); 11463212Sabial} 11563212Sabial 11612623Sphk/* 11712623Sphk * Initialization of the MIB tree. 11812623Sphk * 11944078Sdfr * Order by number in each list. 12012623Sphk */ 121188232Sjhbvoid 122188232Sjhbsysctl_lock(void) 123188232Sjhb{ 12412429Sphk 125188232Sjhb SYSCTL_XLOCK(); 126188232Sjhb} 127188232Sjhb 12880338Sroamvoid 129188232Sjhbsysctl_unlock(void) 130188232Sjhb{ 131188232Sjhb 132188232Sjhb SYSCTL_XUNLOCK(); 133188232Sjhb} 134188232Sjhb 135188232Sjhbvoid 13680338Sroamsysctl_register_oid(struct sysctl_oid *oidp) 13712152Sphk{ 13844078Sdfr struct sysctl_oid_list *parent = oidp->oid_parent; 13944078Sdfr struct sysctl_oid *p; 14044078Sdfr struct sysctl_oid *q; 14112197Sbde 14244078Sdfr /* 14363212Sabial * First check if another oid with the same name already 14463212Sabial * exists in the parent's list. 14563212Sabial */ 146188232Sjhb SYSCTL_ASSERT_XLOCKED(); 14763212Sabial p = sysctl_find_oidname(oidp->oid_name, parent); 14863212Sabial if (p != NULL) { 14963212Sabial if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 15063212Sabial p->oid_refcnt++; 15163212Sabial return; 15263212Sabial } else { 15363212Sabial printf("can't re-use a leaf (%s)!\n", p->oid_name); 15463212Sabial return; 15563212Sabial } 15663212Sabial } 15763212Sabial /* 15844078Sdfr * If this oid has a number OID_AUTO, give it a number which 15980339Sroam * is greater than any current oid. 16080339Sroam * NOTE: DO NOT change the starting value here, change it in 16180339Sroam * <sys/sysctl.h>, and make sure it is at least 256 to 16280339Sroam * accomodate e.g. net.inet.raw as a static sysctl node. 16344078Sdfr */ 16444078Sdfr if (oidp->oid_number == OID_AUTO) { 16580339Sroam static int newoid = CTL_AUTO_START; 16671510Smckusick 16771510Smckusick oidp->oid_number = newoid++; 16871510Smckusick if (newoid == 0x7fffffff) 16971510Smckusick panic("out of oids"); 17044078Sdfr } 17184832Sroam#if 0 17284832Sroam else if (oidp->oid_number >= CTL_AUTO_START) { 17384832Sroam /* do not panic; this happens when unregistering sysctl sets */ 17484832Sroam printf("static sysctl oid too high: %d", oidp->oid_number); 17584832Sroam } 17684832Sroam#endif 17744078Sdfr 17844078Sdfr /* 17944078Sdfr * Insert the oid into the parent's list in order. 18044078Sdfr */ 18144078Sdfr q = NULL; 18244078Sdfr SLIST_FOREACH(p, parent, oid_link) { 18344078Sdfr if (oidp->oid_number < p->oid_number) 18444078Sdfr break; 18544078Sdfr q = p; 18644078Sdfr } 18744078Sdfr if (q) 18844078Sdfr SLIST_INSERT_AFTER(q, oidp, oid_link); 18944078Sdfr else 19044078Sdfr SLIST_INSERT_HEAD(parent, oidp, oid_link); 19112152Sphk} 19212131Sphk 19380338Sroamvoid 19480338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp) 19512152Sphk{ 196115391Smux struct sysctl_oid *p; 197115391Smux int error; 198115391Smux 199188232Sjhb SYSCTL_ASSERT_XLOCKED(); 200115391Smux error = ENOENT; 201115391Smux if (oidp->oid_number == OID_AUTO) { 202115391Smux error = EINVAL; 203115391Smux } else { 204115391Smux SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 205115391Smux if (p == oidp) { 206115391Smux SLIST_REMOVE(oidp->oid_parent, oidp, 207115391Smux sysctl_oid, oid_link); 208115391Smux error = 0; 209115391Smux break; 210115391Smux } 211115391Smux } 212115391Smux } 213115391Smux 214115391Smux /* 215115391Smux * This can happen when a module fails to register and is 216115391Smux * being unloaded afterwards. It should not be a panic() 217115391Smux * for normal use. 218115391Smux */ 219115391Smux if (error) 220115391Smux printf("%s: failed to unregister sysctl\n", __func__); 22144078Sdfr} 22212152Sphk 22363212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */ 22463212Sabialint 22563212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c) 22663212Sabial{ 22763212Sabial 22863212Sabial if (c == NULL) { 22963212Sabial return (EINVAL); 23063212Sabial } 231188232Sjhb 232188232Sjhb /* 233188232Sjhb * No locking here, the caller is responsible for not adding 234188232Sjhb * new nodes to a context until after this function has 235188232Sjhb * returned. 236188232Sjhb */ 23763212Sabial TAILQ_INIT(c); 23863212Sabial return (0); 23963212Sabial} 24063212Sabial 24163212Sabial/* Free the context, and destroy all dynamic oids registered in this context */ 24263212Sabialint 24363212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist) 24463212Sabial{ 24563212Sabial struct sysctl_ctx_entry *e, *e1; 24663212Sabial int error; 24763212Sabial 24863212Sabial error = 0; 24963212Sabial /* 25063212Sabial * First perform a "dry run" to check if it's ok to remove oids. 25163212Sabial * XXX FIXME 25263212Sabial * XXX This algorithm is a hack. But I don't know any 25363212Sabial * XXX better solution for now... 25463212Sabial */ 255188232Sjhb SYSCTL_XLOCK(); 25663212Sabial TAILQ_FOREACH(e, clist, link) { 257188232Sjhb error = sysctl_remove_oid_locked(e->entry, 0, 0); 25863212Sabial if (error) 25963212Sabial break; 26063212Sabial } 26163212Sabial /* 26263212Sabial * Restore deregistered entries, either from the end, 26363212Sabial * or from the place where error occured. 26463212Sabial * e contains the entry that was not unregistered 26563212Sabial */ 26663212Sabial if (error) 26763212Sabial e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 26863212Sabial else 26963212Sabial e1 = TAILQ_LAST(clist, sysctl_ctx_list); 27063212Sabial while (e1 != NULL) { 27163212Sabial sysctl_register_oid(e1->entry); 27263212Sabial e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 27363212Sabial } 274188232Sjhb if (error) { 275188232Sjhb SYSCTL_XUNLOCK(); 27663212Sabial return(EBUSY); 277188232Sjhb } 27863212Sabial /* Now really delete the entries */ 27963212Sabial e = TAILQ_FIRST(clist); 28063212Sabial while (e != NULL) { 28163212Sabial e1 = TAILQ_NEXT(e, link); 282188232Sjhb error = sysctl_remove_oid_locked(e->entry, 1, 0); 28363212Sabial if (error) 28463212Sabial panic("sysctl_remove_oid: corrupt tree, entry: %s", 28563212Sabial e->entry->oid_name); 28663212Sabial free(e, M_SYSCTLOID); 28763212Sabial e = e1; 28863212Sabial } 289188232Sjhb SYSCTL_XUNLOCK(); 29063212Sabial return (error); 29163212Sabial} 29263212Sabial 29363212Sabial/* Add an entry to the context */ 29463212Sabialstruct sysctl_ctx_entry * 29563212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 29663212Sabial{ 29763212Sabial struct sysctl_ctx_entry *e; 29863212Sabial 299188232Sjhb SYSCTL_ASSERT_XLOCKED(); 30063212Sabial if (clist == NULL || oidp == NULL) 30163212Sabial return(NULL); 302111119Simp e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 30363212Sabial e->entry = oidp; 30463212Sabial TAILQ_INSERT_HEAD(clist, e, link); 30563212Sabial return (e); 30663212Sabial} 30763212Sabial 30863212Sabial/* Find an entry in the context */ 30963212Sabialstruct sysctl_ctx_entry * 31063212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 31163212Sabial{ 31263212Sabial struct sysctl_ctx_entry *e; 31363212Sabial 314188232Sjhb SYSCTL_ASSERT_LOCKED(); 31563212Sabial if (clist == NULL || oidp == NULL) 31663212Sabial return(NULL); 31771999Sphk TAILQ_FOREACH(e, clist, link) { 31863212Sabial if(e->entry == oidp) 31963212Sabial return(e); 32063212Sabial } 32163212Sabial return (e); 32263212Sabial} 32363212Sabial 32444078Sdfr/* 32563212Sabial * Delete an entry from the context. 32663212Sabial * NOTE: this function doesn't free oidp! You have to remove it 32763212Sabial * with sysctl_remove_oid(). 32863212Sabial */ 32963212Sabialint 33063212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 33163212Sabial{ 33263212Sabial struct sysctl_ctx_entry *e; 33363212Sabial 33463212Sabial if (clist == NULL || oidp == NULL) 33563212Sabial return (EINVAL); 336188232Sjhb SYSCTL_XLOCK(); 33763212Sabial e = sysctl_ctx_entry_find(clist, oidp); 33863212Sabial if (e != NULL) { 33963212Sabial TAILQ_REMOVE(clist, e, link); 340188232Sjhb SYSCTL_XUNLOCK(); 34163212Sabial free(e, M_SYSCTLOID); 34263212Sabial return (0); 343188232Sjhb } else { 344188232Sjhb SYSCTL_XUNLOCK(); 34563212Sabial return (ENOENT); 346188232Sjhb } 34763212Sabial} 34863212Sabial 34963212Sabial/* 35063212Sabial * Remove dynamically created sysctl trees. 35163212Sabial * oidp - top of the tree to be removed 35263212Sabial * del - if 0 - just deregister, otherwise free up entries as well 35363212Sabial * recurse - if != 0 traverse the subtree to be deleted 35463212Sabial */ 35563212Sabialint 35663212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 35763212Sabial{ 358188232Sjhb int error; 359188232Sjhb 360188232Sjhb SYSCTL_XLOCK(); 361188232Sjhb error = sysctl_remove_oid_locked(oidp, del, recurse); 362188232Sjhb SYSCTL_XUNLOCK(); 363188232Sjhb return (error); 364188232Sjhb} 365188232Sjhb 366188232Sjhbstatic int 367188232Sjhbsysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 368188232Sjhb{ 36963212Sabial struct sysctl_oid *p; 37063212Sabial int error; 37163212Sabial 372188232Sjhb SYSCTL_ASSERT_XLOCKED(); 37363212Sabial if (oidp == NULL) 37463212Sabial return(EINVAL); 37563212Sabial if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 37663212Sabial printf("can't remove non-dynamic nodes!\n"); 37763212Sabial return (EINVAL); 37863212Sabial } 37963212Sabial /* 38063212Sabial * WARNING: normal method to do this should be through 38163212Sabial * sysctl_ctx_free(). Use recursing as the last resort 38263212Sabial * method to purge your sysctl tree of leftovers... 38363212Sabial * However, if some other code still references these nodes, 38463212Sabial * it will panic. 38563212Sabial */ 38663212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 38763212Sabial if (oidp->oid_refcnt == 1) { 38863212Sabial SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 38963212Sabial if (!recurse) 39063212Sabial return (ENOTEMPTY); 391188232Sjhb error = sysctl_remove_oid_locked(p, del, 392188232Sjhb recurse); 39363212Sabial if (error) 39463212Sabial return (error); 39563212Sabial } 39663212Sabial if (del) 39763212Sabial free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 39863212Sabial } 39963212Sabial } 40063212Sabial if (oidp->oid_refcnt > 1 ) { 40163212Sabial oidp->oid_refcnt--; 40263212Sabial } else { 40363212Sabial if (oidp->oid_refcnt == 0) { 40463212Sabial printf("Warning: bad oid_refcnt=%u (%s)!\n", 40563212Sabial oidp->oid_refcnt, oidp->oid_name); 40663212Sabial return (EINVAL); 40763212Sabial } 40863212Sabial sysctl_unregister_oid(oidp); 40963212Sabial if (del) { 410141433Sphk if (oidp->oid_descr) 411141433Sphk free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID); 41263978Speter free((void *)(uintptr_t)(const void *)oidp->oid_name, 41363978Speter M_SYSCTLOID); 41463212Sabial free(oidp, M_SYSCTLOID); 41563212Sabial } 41663212Sabial } 41763212Sabial return (0); 41863212Sabial} 41963212Sabial 42063212Sabial/* 42163212Sabial * Create new sysctls at run time. 42263212Sabial * clist may point to a valid context initialized with sysctl_ctx_init(). 42363212Sabial */ 42463212Sabialstruct sysctl_oid * 42563212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 42670679Sjhb int number, const char *name, int kind, void *arg1, int arg2, 42770679Sjhb int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 42863212Sabial{ 42963212Sabial struct sysctl_oid *oidp; 43063212Sabial ssize_t len; 43163978Speter char *newname; 43263212Sabial 43363212Sabial /* You have to hook up somewhere.. */ 43463212Sabial if (parent == NULL) 43563212Sabial return(NULL); 43663212Sabial /* Check if the node already exists, otherwise create it */ 437188232Sjhb SYSCTL_XLOCK(); 43863212Sabial oidp = sysctl_find_oidname(name, parent); 43963212Sabial if (oidp != NULL) { 44063212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 44163212Sabial oidp->oid_refcnt++; 44263212Sabial /* Update the context */ 44363212Sabial if (clist != NULL) 44463212Sabial sysctl_ctx_entry_add(clist, oidp); 445188232Sjhb SYSCTL_XUNLOCK(); 44663212Sabial return (oidp); 44763212Sabial } else { 448188232Sjhb SYSCTL_XUNLOCK(); 44963212Sabial printf("can't re-use a leaf (%s)!\n", name); 45063212Sabial return (NULL); 45163212Sabial } 45263212Sabial } 453111119Simp oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 45463212Sabial oidp->oid_parent = parent; 45563212Sabial SLIST_NEXT(oidp, oid_link) = NULL; 45663212Sabial oidp->oid_number = number; 45763212Sabial oidp->oid_refcnt = 1; 45863212Sabial len = strlen(name); 459111119Simp newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 46063978Speter bcopy(name, newname, len + 1); 46163978Speter newname[len] = '\0'; 46263978Speter oidp->oid_name = newname; 46363212Sabial oidp->oid_handler = handler; 46463212Sabial oidp->oid_kind = CTLFLAG_DYN | kind; 46563212Sabial if ((kind & CTLTYPE) == CTLTYPE_NODE) { 46663212Sabial /* Allocate space for children */ 467132776Skan SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list), 468132776Skan M_SYSCTLOID, M_WAITOK)); 46963212Sabial SLIST_INIT(SYSCTL_CHILDREN(oidp)); 47063212Sabial } else { 47163212Sabial oidp->oid_arg1 = arg1; 47263212Sabial oidp->oid_arg2 = arg2; 47363212Sabial } 47463212Sabial oidp->oid_fmt = fmt; 47588006Sluigi if (descr) { 47688006Sluigi int len = strlen(descr) + 1; 477141433Sphk oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK); 478141433Sphk if (oidp->oid_descr) 479141433Sphk strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr); 48088006Sluigi } 48163212Sabial /* Update the context, if used */ 48263212Sabial if (clist != NULL) 48363212Sabial sysctl_ctx_entry_add(clist, oidp); 48463212Sabial /* Register this oid */ 48563212Sabial sysctl_register_oid(oidp); 486188232Sjhb SYSCTL_XUNLOCK(); 48763212Sabial return (oidp); 48863212Sabial} 48963212Sabial 49063212Sabial/* 491174113Speter * Rename an existing oid. 492174113Speter */ 493174113Spetervoid 494174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 495174113Speter{ 496174113Speter ssize_t len; 497174113Speter char *newname; 498174113Speter void *oldname; 499174113Speter 500174113Speter len = strlen(name); 501174113Speter newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 502174113Speter bcopy(name, newname, len + 1); 503174113Speter newname[len] = '\0'; 504188232Sjhb SYSCTL_XLOCK(); 505188232Sjhb oldname = (void *)(uintptr_t)(const void *)oidp->oid_name; 506174113Speter oidp->oid_name = newname; 507188232Sjhb SYSCTL_XUNLOCK(); 508174113Speter free(oldname, M_SYSCTLOID); 509174113Speter} 510174113Speter 511174113Speter/* 512126319Sdes * Reparent an existing oid. 513126319Sdes */ 514126319Sdesint 515126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 516126319Sdes{ 517126319Sdes struct sysctl_oid *oidp; 518126319Sdes 519188232Sjhb SYSCTL_XLOCK(); 520188232Sjhb if (oid->oid_parent == parent) { 521188232Sjhb SYSCTL_XUNLOCK(); 522126319Sdes return (0); 523188232Sjhb } 524126319Sdes oidp = sysctl_find_oidname(oid->oid_name, parent); 525188232Sjhb if (oidp != NULL) { 526188232Sjhb SYSCTL_XUNLOCK(); 527126319Sdes return (EEXIST); 528188232Sjhb } 529126319Sdes sysctl_unregister_oid(oid); 530126319Sdes oid->oid_parent = parent; 531126319Sdes oid->oid_number = OID_AUTO; 532126319Sdes sysctl_register_oid(oid); 533188232Sjhb SYSCTL_XUNLOCK(); 534126319Sdes return (0); 535126319Sdes} 536126319Sdes 537126319Sdes/* 53844078Sdfr * Register the kernel's oids on startup. 53944078Sdfr */ 54078161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid); 54112152Sphk 54280338Sroamstatic void 54380338Sroamsysctl_register_all(void *arg) 54438869Sbde{ 54578161Speter struct sysctl_oid **oidp; 54678161Speter 547192125Sjhb sx_init(&sysctlmemlock, "sysctl mem"); 54893625Srwatson SYSCTL_INIT(); 549188232Sjhb SYSCTL_XLOCK(); 55078161Speter SET_FOREACH(oidp, sysctl_set) 55178161Speter sysctl_register_oid(*oidp); 552188232Sjhb SYSCTL_XUNLOCK(); 55338869Sbde} 55444078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 55544078Sdfr 55612623Sphk/* 55712623Sphk * "Staff-functions" 55812623Sphk * 55912650Sphk * These functions implement a presently undocumented interface 56012650Sphk * used by the sysctl program to walk the tree, and get the type 56112650Sphk * so it can print the value. 56212650Sphk * This interface is under work and consideration, and should probably 56312650Sphk * be killed with a big axe by the first person who can find the time. 56412650Sphk * (be aware though, that the proper interface isn't as obvious as it 56512650Sphk * may seem, there are various conflicting requirements. 56612650Sphk * 56712623Sphk * {0,0} printf the entire MIB-tree. 56812623Sphk * {0,1,...} return the name of the "..." OID. 56942467Sphk * {0,2,...} return the next OID. 57012623Sphk * {0,3} return the OID of the name in "new" 57112650Sphk * {0,4,...} return the kind & format info for the "..." OID. 57288006Sluigi * {0,5,...} return the description the "..." OID. 57312623Sphk */ 57412623Sphk 575136999Srwatson#ifdef SYSCTL_DEBUG 57612152Sphkstatic void 57744078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 57812152Sphk{ 57944078Sdfr int k; 58044078Sdfr struct sysctl_oid *oidp; 58112152Sphk 582188232Sjhb SYSCTL_ASSERT_LOCKED(); 58344078Sdfr SLIST_FOREACH(oidp, l, oid_link) { 58412152Sphk 58512152Sphk for (k=0; k<i; k++) 58612152Sphk printf(" "); 58712152Sphk 58844078Sdfr printf("%d %s ", oidp->oid_number, oidp->oid_name); 58912152Sphk 59012152Sphk printf("%c%c", 59144078Sdfr oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 59244078Sdfr oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 59312152Sphk 59444078Sdfr if (oidp->oid_handler) 59515241Sphk printf(" *Handler"); 59615241Sphk 59744078Sdfr switch (oidp->oid_kind & CTLTYPE) { 59812243Sphk case CTLTYPE_NODE: 59915241Sphk printf(" Node\n"); 60044078Sdfr if (!oidp->oid_handler) { 60112152Sphk sysctl_sysctl_debug_dump_node( 60244078Sdfr oidp->oid_arg1, i+2); 60312152Sphk } 60412152Sphk break; 60512152Sphk case CTLTYPE_INT: printf(" Int\n"); break; 60612152Sphk case CTLTYPE_STRING: printf(" String\n"); break; 60712152Sphk case CTLTYPE_QUAD: printf(" Quad\n"); break; 60812152Sphk case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 60912152Sphk default: printf("\n"); 61012152Sphk } 61112152Sphk 61212152Sphk } 61312152Sphk} 61412152Sphk 61512152Sphkstatic int 61662573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 61712152Sphk{ 61887024Speter int error; 61987024Speter 620164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 62187024Speter if (error) 622139483Spjd return (error); 62344078Sdfr sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 624139483Spjd return (ENOENT); 62512152Sphk} 62612152Sphk 62712152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 62812623Sphk 0, 0, sysctl_sysctl_debug, "-", ""); 629136999Srwatson#endif 63012152Sphk 63112623Sphkstatic int 63262573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 63312623Sphk{ 63412623Sphk int *name = (int *) arg1; 63512623Sphk u_int namelen = arg2; 63644078Sdfr int error = 0; 63744078Sdfr struct sysctl_oid *oid; 63844972Sphk struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 63912623Sphk char buf[10]; 64012131Sphk 641188232Sjhb SYSCTL_ASSERT_LOCKED(); 64212623Sphk while (namelen) { 64312623Sphk if (!lsp) { 64441514Sarchie snprintf(buf,sizeof(buf),"%d",*name); 64512623Sphk if (req->oldidx) 64612623Sphk error = SYSCTL_OUT(req, ".", 1); 64712623Sphk if (!error) 64812623Sphk error = SYSCTL_OUT(req, buf, strlen(buf)); 64912623Sphk if (error) 65012623Sphk return (error); 65112623Sphk namelen--; 65212623Sphk name++; 65312623Sphk continue; 65412623Sphk } 65544972Sphk lsp2 = 0; 65644078Sdfr SLIST_FOREACH(oid, lsp, oid_link) { 65744078Sdfr if (oid->oid_number != *name) 65812623Sphk continue; 65912131Sphk 66012623Sphk if (req->oldidx) 66112623Sphk error = SYSCTL_OUT(req, ".", 1); 66212623Sphk if (!error) 66344078Sdfr error = SYSCTL_OUT(req, oid->oid_name, 66444078Sdfr strlen(oid->oid_name)); 66512623Sphk if (error) 66612623Sphk return (error); 66712623Sphk 66812623Sphk namelen--; 66912623Sphk name++; 67012623Sphk 67144972Sphk if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 67212623Sphk break; 67312623Sphk 67444078Sdfr if (oid->oid_handler) 67512623Sphk break; 67612623Sphk 67744972Sphk lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 67812623Sphk break; 67912623Sphk } 68044972Sphk lsp = lsp2; 68112623Sphk } 68212623Sphk return (SYSCTL_OUT(req, "", 1)); 68312623Sphk} 68412623Sphk 685141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 68612623Sphk 68712623Sphkstatic int 68863978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 68944078Sdfr int *next, int *len, int level, struct sysctl_oid **oidpp) 69012623Sphk{ 69144078Sdfr struct sysctl_oid *oidp; 69212623Sphk 693188232Sjhb SYSCTL_ASSERT_LOCKED(); 69412623Sphk *len = level; 69544078Sdfr SLIST_FOREACH(oidp, lsp, oid_link) { 69644078Sdfr *next = oidp->oid_number; 69744078Sdfr *oidpp = oidp; 69812623Sphk 699101650Smux if (oidp->oid_kind & CTLFLAG_SKIP) 700101650Smux continue; 701101650Smux 70212623Sphk if (!namelen) { 70344078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 704139483Spjd return (0); 70544078Sdfr if (oidp->oid_handler) 70612623Sphk /* We really should call the handler here...*/ 707139483Spjd return (0); 70844078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 70963978Speter if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 71044078Sdfr len, level+1, oidpp)) 711139483Spjd return (0); 712111260Srwatson goto emptynode; 71312623Sphk } 71412623Sphk 71544078Sdfr if (oidp->oid_number < *name) 71612623Sphk continue; 71712623Sphk 71844078Sdfr if (oidp->oid_number > *name) { 71944078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 720139483Spjd return (0); 72144078Sdfr if (oidp->oid_handler) 722139483Spjd return (0); 72344078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 72463978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 72544078Sdfr next+1, len, level+1, oidpp)) 72612623Sphk return (0); 72715241Sphk goto next; 72812623Sphk } 72944078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 73012623Sphk continue; 73112623Sphk 73244078Sdfr if (oidp->oid_handler) 73312623Sphk continue; 73412623Sphk 73544078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 73663978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 73744078Sdfr len, level+1, oidpp)) 73812623Sphk return (0); 73915241Sphk next: 74012623Sphk namelen = 1; 741111260Srwatson emptynode: 74212623Sphk *len = level; 74312623Sphk } 744139483Spjd return (1); 74512623Sphk} 74612623Sphk 74712623Sphkstatic int 74862573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 74912623Sphk{ 75012623Sphk int *name = (int *) arg1; 75112623Sphk u_int namelen = arg2; 75212623Sphk int i, j, error; 75312623Sphk struct sysctl_oid *oid; 75444078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 75512623Sphk int newoid[CTL_MAXNAME]; 75612623Sphk 75763978Speter i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 75812623Sphk if (i) 759139483Spjd return (ENOENT); 76012650Sphk error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 76112623Sphk return (error); 76212623Sphk} 76312623Sphk 764141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 76512623Sphk 76612623Sphkstatic int 767189707Sjhbname2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) 76812623Sphk{ 76944078Sdfr int i; 77044078Sdfr struct sysctl_oid *oidp; 77144078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 77212623Sphk char *p; 77312623Sphk 774188232Sjhb SYSCTL_ASSERT_LOCKED(); 775186564Sed 77612623Sphk if (!*name) 777139483Spjd return (ENOENT); 77812623Sphk 77912623Sphk p = name + strlen(name) - 1 ; 78012623Sphk if (*p == '.') 78112623Sphk *p = '\0'; 78212623Sphk 78312623Sphk *len = 0; 78412623Sphk 78512623Sphk for (p = name; *p && *p != '.'; p++) 78612623Sphk ; 78712623Sphk i = *p; 78812623Sphk if (i == '.') 78912623Sphk *p = '\0'; 79012623Sphk 79144078Sdfr oidp = SLIST_FIRST(lsp); 79212623Sphk 79344078Sdfr while (oidp && *len < CTL_MAXNAME) { 79444078Sdfr if (strcmp(name, oidp->oid_name)) { 79544078Sdfr oidp = SLIST_NEXT(oidp, oid_link); 79612623Sphk continue; 79712623Sphk } 79844078Sdfr *oid++ = oidp->oid_number; 79912623Sphk (*len)++; 80012623Sphk 80112623Sphk if (!i) { 80244078Sdfr if (oidpp) 80344078Sdfr *oidpp = oidp; 80412623Sphk return (0); 80512623Sphk } 80612623Sphk 80744078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 80812623Sphk break; 80912623Sphk 81044078Sdfr if (oidp->oid_handler) 81112623Sphk break; 81212623Sphk 81344078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 81444078Sdfr oidp = SLIST_FIRST(lsp); 81512623Sphk name = p+1; 81612623Sphk for (p = name; *p && *p != '.'; p++) 81712623Sphk ; 81812623Sphk i = *p; 81912623Sphk if (i == '.') 82012623Sphk *p = '\0'; 82112623Sphk } 822139483Spjd return (ENOENT); 82312623Sphk} 82412623Sphk 82512623Sphkstatic int 82662573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 82712623Sphk{ 82812623Sphk char *p; 82912623Sphk int error, oid[CTL_MAXNAME], len; 83012623Sphk struct sysctl_oid *op = 0; 83112623Sphk 832188232Sjhb SYSCTL_ASSERT_LOCKED(); 833186564Sed 83412623Sphk if (!req->newlen) 835139483Spjd return (ENOENT); 83645140Sphk if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 83745140Sphk return (ENAMETOOLONG); 83812623Sphk 839111119Simp p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 84012623Sphk 84112623Sphk error = SYSCTL_IN(req, p, req->newlen); 84212623Sphk if (error) { 84312623Sphk free(p, M_SYSCTL); 84412623Sphk return (error); 84512623Sphk } 84612623Sphk 84712623Sphk p [req->newlen] = '\0'; 84812623Sphk 84912623Sphk error = name2oid(p, oid, &len, &op); 85012623Sphk 85112623Sphk free(p, M_SYSCTL); 85212623Sphk 85312623Sphk if (error) 85412623Sphk return (error); 85512623Sphk 85612650Sphk error = SYSCTL_OUT(req, oid, len * sizeof *oid); 85712623Sphk return (error); 85812623Sphk} 85912623Sphk 860187864SedSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE, 861187864Sed 0, 0, sysctl_sysctl_name2oid, "I", ""); 86212623Sphk 86312623Sphkstatic int 86462573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 86512623Sphk{ 86644078Sdfr struct sysctl_oid *oid; 86753977Sgreen int error; 86812623Sphk 86953977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 87053977Sgreen if (error) 87153977Sgreen return (error); 87212623Sphk 87344078Sdfr if (!oid->oid_fmt) 87453977Sgreen return (ENOENT); 87553977Sgreen error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 87653977Sgreen if (error) 87753977Sgreen return (error); 87853977Sgreen error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 87912650Sphk return (error); 88012623Sphk} 88112623Sphk 88242467Sphk 883187864Sedstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE, 884187864Sed sysctl_sysctl_oidfmt, ""); 88512623Sphk 88688006Sluigistatic int 88788006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 88888006Sluigi{ 88988006Sluigi struct sysctl_oid *oid; 89088006Sluigi int error; 89188006Sluigi 89288006Sluigi error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 89388006Sluigi if (error) 89488006Sluigi return (error); 89588006Sluigi 896141433Sphk if (!oid->oid_descr) 89788006Sluigi return (ENOENT); 898141433Sphk error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 89988006Sluigi return (error); 90088006Sluigi} 90188006Sluigi 902141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 90388006Sluigi 90412243Sphk/* 90512623Sphk * Default "handler" functions. 90612623Sphk */ 90712623Sphk 90812623Sphk/* 90942095Sdfr * Handle an int, signed or unsigned. 91012243Sphk * Two cases: 91112243Sphk * a variable: point arg1 at it. 91212243Sphk * a constant: pass it in arg2. 91312243Sphk */ 91412243Sphk 91511865Sphkint 91662573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS) 91711863Sphk{ 918100833Struckman int tmpout, error = 0; 91911863Sphk 920100833Struckman /* 921100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 922100833Struckman */ 92312243Sphk if (arg1) 924100833Struckman tmpout = *(int *)arg1; 92520506Sbde else 926100833Struckman tmpout = arg2; 927100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 92811863Sphk 92912243Sphk if (error || !req->newptr) 93012243Sphk return (error); 93111863Sphk 93212243Sphk if (!arg1) 93312243Sphk error = EPERM; 93412243Sphk else 93512243Sphk error = SYSCTL_IN(req, arg1, sizeof(int)); 93612243Sphk return (error); 93711863Sphk} 93811863Sphk 939191688Szec#ifdef VIMAGE 940191688Szecint 941191688Szecsysctl_handle_v_int(SYSCTL_HANDLER_ARGS) 942191688Szec{ 943191688Szec int tmpout, error = 0; 944191688Szec 945191688Szec SYSCTL_RESOLVE_V_ARG1(); 946191688Szec 947191688Szec /* 948191688Szec * Attempt to get a coherent snapshot by making a copy of the data. 949191688Szec */ 950191688Szec tmpout = *(int *)arg1; 951191688Szec error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 952155758Sandre 953191688Szec if (error || !req->newptr) 954191688Szec return (error); 955191688Szec 956191688Szec if (!arg1) 957191688Szec error = EPERM; 958191688Szec else 959191688Szec error = SYSCTL_IN(req, arg1, sizeof(int)); 960191688Szec return (error); 961191688Szec} 962191688Szec#endif 963191688Szec 96412243Sphk/* 965155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks. 966155758Sandre */ 967155758Sandre 968155758Sandreint 969155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS) 970155758Sandre{ 971155758Sandre int error, s, tt; 972155758Sandre 973191688Szec SYSCTL_RESOLVE_V_ARG1(); 974191688Szec 975191688Szec tt = *(int *)arg1; 976155758Sandre s = (int)((int64_t)tt * 1000 / hz); 977155758Sandre 978155758Sandre error = sysctl_handle_int(oidp, &s, 0, req); 979155758Sandre if (error || !req->newptr) 980155758Sandre return (error); 981155758Sandre 982155758Sandre tt = (int)((int64_t)s * hz / 1000); 983155758Sandre if (tt < 1) 984155758Sandre return (EINVAL); 985155758Sandre 986191688Szec *(int *)arg1 = tt; 987155758Sandre return (0); 988155758Sandre} 989155758Sandre 990155758Sandre 991155758Sandre/* 99245140Sphk * Handle a long, signed or unsigned. arg1 points to it. 99338517Sdfr */ 99438517Sdfr 99538517Sdfrint 99662573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS) 99738517Sdfr{ 99838517Sdfr int error = 0; 999136404Speter long tmplong; 1000136404Speter#ifdef SCTL_MASK32 1001136404Speter int tmpint; 1002136404Speter#endif 100338517Sdfr 1004100833Struckman /* 1005100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 1006100833Struckman */ 100745140Sphk if (!arg1) 100845140Sphk return (EINVAL); 1009136404Speter tmplong = *(long *)arg1; 1010136404Speter#ifdef SCTL_MASK32 1011136404Speter if (req->flags & SCTL_MASK32) { 1012136404Speter tmpint = tmplong; 1013136404Speter error = SYSCTL_OUT(req, &tmpint, sizeof(int)); 1014136404Speter } else 1015136404Speter#endif 1016136404Speter error = SYSCTL_OUT(req, &tmplong, sizeof(long)); 101738517Sdfr 101838517Sdfr if (error || !req->newptr) 101938517Sdfr return (error); 102038517Sdfr 1021136404Speter#ifdef SCTL_MASK32 1022136404Speter if (req->flags & SCTL_MASK32) { 1023136404Speter error = SYSCTL_IN(req, &tmpint, sizeof(int)); 1024136404Speter *(long *)arg1 = (long)tmpint; 1025136404Speter } else 1026136404Speter#endif 1027136404Speter error = SYSCTL_IN(req, arg1, sizeof(long)); 102838517Sdfr return (error); 102938517Sdfr} 103038517Sdfr 103138517Sdfr/* 1032170288Sdwmalone * Handle a 64 bit int, signed or unsigned. arg1 points to it. 1033170288Sdwmalone */ 1034170288Sdwmalone 1035170288Sdwmaloneint 1036170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS) 1037170288Sdwmalone{ 1038170288Sdwmalone int error = 0; 1039170288Sdwmalone uint64_t tmpout; 1040170288Sdwmalone 1041170288Sdwmalone /* 1042170288Sdwmalone * Attempt to get a coherent snapshot by making a copy of the data. 1043170288Sdwmalone */ 1044170288Sdwmalone if (!arg1) 1045170288Sdwmalone return (EINVAL); 1046170288Sdwmalone tmpout = *(uint64_t *)arg1; 1047170288Sdwmalone error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t)); 1048170288Sdwmalone 1049170288Sdwmalone if (error || !req->newptr) 1050170288Sdwmalone return (error); 1051170288Sdwmalone 1052170288Sdwmalone error = SYSCTL_IN(req, arg1, sizeof(uint64_t)); 1053170288Sdwmalone return (error); 1054170288Sdwmalone} 1055170288Sdwmalone 1056170288Sdwmalone/* 105712243Sphk * Handle our generic '\0' terminated 'C' string. 105812243Sphk * Two cases: 105912243Sphk * a variable string: point arg1 at it, arg2 is max length. 106012243Sphk * a constant string: point arg1 at it, arg2 is zero. 106112243Sphk */ 106212243Sphk 106311865Sphkint 106462573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS) 106511863Sphk{ 106612243Sphk int error=0; 1067100833Struckman char *tmparg; 1068100833Struckman size_t outlen; 106911863Sphk 1070100833Struckman /* 1071100833Struckman * Attempt to get a coherent snapshot by copying to a 1072100833Struckman * temporary kernel buffer. 1073100833Struckman */ 1074100833Struckmanretry: 1075100833Struckman outlen = strlen((char *)arg1)+1; 1076111119Simp tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 1077105354Srobert 1078105354Srobert if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 1079100833Struckman free(tmparg, M_SYSCTLTMP); 1080100833Struckman goto retry; 1081100833Struckman } 1082105354Srobert 1083100833Struckman error = SYSCTL_OUT(req, tmparg, outlen); 1084100833Struckman free(tmparg, M_SYSCTLTMP); 108511863Sphk 108645140Sphk if (error || !req->newptr) 108712243Sphk return (error); 108811863Sphk 108945140Sphk if ((req->newlen - req->newidx) >= arg2) { 109045140Sphk error = EINVAL; 109112243Sphk } else { 109212243Sphk arg2 = (req->newlen - req->newidx); 109312243Sphk error = SYSCTL_IN(req, arg1, arg2); 109412243Sphk ((char *)arg1)[arg2] = '\0'; 109511863Sphk } 109612131Sphk 109712131Sphk return (error); 109811863Sphk} 109911863Sphk 1100191688Szec#ifdef VIMAGE 1101191688Szecint 1102191688Szecsysctl_handle_v_string(SYSCTL_HANDLER_ARGS) 1103191688Szec{ 1104191688Szec int error=0; 1105191688Szec char *tmparg; 1106191688Szec size_t outlen; 1107191688Szec 1108191688Szec SYSCTL_RESOLVE_V_ARG1(); 1109191688Szec 1110191688Szec /* 1111191688Szec * Attempt to get a coherent snapshot by copying to a 1112191688Szec * temporary kernel buffer. 1113191688Szec */ 1114191688Szecretry: 1115191688Szec outlen = strlen((char *)arg1)+1; 1116191688Szec tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 1117191688Szec 1118191688Szec if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 1119191688Szec free(tmparg, M_SYSCTLTMP); 1120191688Szec goto retry; 1121191688Szec } 1122191688Szec 1123191688Szec error = SYSCTL_OUT(req, tmparg, outlen); 1124191688Szec free(tmparg, M_SYSCTLTMP); 1125191688Szec 1126191688Szec if (error || !req->newptr) 1127191688Szec return (error); 1128191688Szec 1129191688Szec if ((req->newlen - req->newidx) >= arg2) { 1130191688Szec error = EINVAL; 1131191688Szec } else { 1132191688Szec arg2 = (req->newlen - req->newidx); 1133191688Szec error = SYSCTL_IN(req, arg1, arg2); 1134191688Szec ((char *)arg1)[arg2] = '\0'; 1135191688Szec } 1136191688Szec 1137191688Szec return (error); 1138191688Szec} 1139191688Szec#endif 1140191688Szec 114112243Sphk/* 114212243Sphk * Handle any kind of opaque data. 114312243Sphk * arg1 points to it, arg2 is the size. 114412243Sphk */ 114512243Sphk 114611865Sphkint 114762573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 114811863Sphk{ 1149120803Sbms int error, tries; 1150120803Sbms u_int generation; 1151120813Sbms struct sysctl_req req2; 115212243Sphk 1153100833Struckman /* 1154120803Sbms * Attempt to get a coherent snapshot, by using the thread 1155120803Sbms * pre-emption counter updated from within mi_switch() to 1156120803Sbms * determine if we were pre-empted during a bcopy() or 1157120803Sbms * copyout(). Make 3 attempts at doing this before giving up. 1158120803Sbms * If we encounter an error, stop immediately. 1159100833Struckman */ 1160120803Sbms tries = 0; 1161120813Sbms req2 = *req; 1162120813Sbmsretry: 1163120813Sbms generation = curthread->td_generation; 1164120813Sbms error = SYSCTL_OUT(req, arg1, arg2); 1165120813Sbms if (error) 1166120813Sbms return (error); 1167120813Sbms tries++; 1168120813Sbms if (generation != curthread->td_generation && tries < 3) { 1169120813Sbms *req = req2; 1170120813Sbms goto retry; 1171120813Sbms } 117212243Sphk 117312243Sphk error = SYSCTL_IN(req, arg1, arg2); 117412243Sphk 117512243Sphk return (error); 117612243Sphk} 117712243Sphk 1178191688Szec#ifdef VIMAGE 1179191688Szecint 1180191688Szecsysctl_handle_v_opaque(SYSCTL_HANDLER_ARGS) 1181191688Szec{ 1182191688Szec int error, tries; 1183191688Szec u_int generation; 1184191688Szec struct sysctl_req req2; 1185191688Szec 1186191688Szec SYSCTL_RESOLVE_V_ARG1(); 1187191688Szec 1188191688Szec tries = 0; 1189191688Szec req2 = *req; 1190191688Szecretry: 1191191688Szec generation = curthread->td_generation; 1192191688Szec error = SYSCTL_OUT(req, arg1, arg2); 1193191688Szec if (error) 1194191688Szec return (error); 1195191688Szec tries++; 1196191688Szec if (generation != curthread->td_generation && tries < 3) { 1197191688Szec *req = req2; 1198191688Szec goto retry; 1199191688Szec } 1200191688Szec 1201191688Szec error = SYSCTL_IN(req, arg1, arg2); 1202191688Szec 1203191688Szec return (error); 1204191688Szec} 1205191688Szec#endif 1206191688Szec 120712260Sphk/* 120812260Sphk * Transfer functions to/from kernel space. 120912260Sphk * XXX: rather untested at this point 121012260Sphk */ 121112260Sphkstatic int 121238517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 121312243Sphk{ 121438517Sdfr size_t i = 0; 121512260Sphk 121612260Sphk if (req->oldptr) { 121738517Sdfr i = l; 121873971Stmm if (req->oldlen <= req->oldidx) 121973971Stmm i = 0; 122073971Stmm else 122173971Stmm if (i > req->oldlen - req->oldidx) 122273971Stmm i = req->oldlen - req->oldidx; 122312260Sphk if (i > 0) 122417971Sbde bcopy(p, (char *)req->oldptr + req->oldidx, i); 122512243Sphk } 1226192144Skib req->oldidx += l; 122716282Snate if (req->oldptr && i != l) 122811863Sphk return (ENOMEM); 122912260Sphk return (0); 123012243Sphk} 123112243Sphk 123212260Sphkstatic int 123338517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 123412243Sphk{ 123512260Sphk if (!req->newptr) 1236139483Spjd return (0); 123712260Sphk if (req->newlen - req->newidx < l) 123811863Sphk return (EINVAL); 123917971Sbde bcopy((char *)req->newptr + req->newidx, p, l); 124012243Sphk req->newidx += l; 124112131Sphk return (0); 124211863Sphk} 124311863Sphk 124416282Snateint 124583366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1246136404Speter size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags) 124716282Snate{ 124816282Snate int error = 0; 124916282Snate struct sysctl_req req; 125016282Snate 125116282Snate bzero(&req, sizeof req); 125216282Snate 125386183Srwatson req.td = td; 1254136404Speter req.flags = flags; 125516282Snate 125616282Snate if (oldlenp) { 125716282Snate req.oldlen = *oldlenp; 125816282Snate } 1259127052Struckman req.validlen = req.oldlen; 126016282Snate 126116282Snate if (old) { 126216282Snate req.oldptr= old; 126316282Snate } 126416282Snate 126577646Sdd if (new != NULL) { 126616282Snate req.newlen = newlen; 126716282Snate req.newptr = new; 126816282Snate } 126916282Snate 127016282Snate req.oldfunc = sysctl_old_kernel; 127116282Snate req.newfunc = sysctl_new_kernel; 1272120781Sbms req.lock = REQ_LOCKED; 127316282Snate 1274188232Sjhb SYSCTL_SLOCK(); 127516282Snate error = sysctl_root(0, name, namelen, &req); 1276188232Sjhb SYSCTL_SUNLOCK(); 1277120813Sbms 1278127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1279127052Struckman vsunlock(req.oldptr, req.validlen); 128016282Snate 128116282Snate if (error && error != ENOMEM) 128216282Snate return (error); 128316282Snate 128416282Snate if (retval) { 1285127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1286127052Struckman *retval = req.validlen; 128716282Snate else 128816282Snate *retval = req.oldidx; 128916282Snate } 129016282Snate return (error); 129116282Snate} 129216282Snate 129376834Sjlemonint 129483366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 1295136404Speter void *new, size_t newlen, size_t *retval, int flags) 129676834Sjlemon{ 129776834Sjlemon int oid[CTL_MAXNAME]; 129878620Smjacob size_t oidlen, plen; 129978620Smjacob int error; 130076834Sjlemon 130176834Sjlemon oid[0] = 0; /* sysctl internal magic */ 130276834Sjlemon oid[1] = 3; /* name2oid */ 130376834Sjlemon oidlen = sizeof(oid); 130476834Sjlemon 130583366Sjulian error = kernel_sysctl(td, oid, 2, oid, &oidlen, 1306136404Speter (void *)name, strlen(name), &plen, flags); 130776834Sjlemon if (error) 130876834Sjlemon return (error); 130976834Sjlemon 131083366Sjulian error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 1311136404Speter new, newlen, retval, flags); 131276834Sjlemon return (error); 131376834Sjlemon} 131476834Sjlemon 131512260Sphk/* 131612260Sphk * Transfer function to/from user space. 131712260Sphk */ 131812260Sphkstatic int 131938517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 132012243Sphk{ 132138517Sdfr int error = 0; 1322126253Struckman size_t i, len, origidx; 132312243Sphk 1324126253Struckman origidx = req->oldidx; 1325192144Skib req->oldidx += l; 1326192144Skib if (req->oldptr == NULL) 1327126253Struckman return (0); 1328148864Scsjp /* 1329148864Scsjp * If we have not wired the user supplied buffer and we are currently 1330148864Scsjp * holding locks, drop a witness warning, as it's possible that 1331148864Scsjp * write operations to the user page can sleep. 1332148864Scsjp */ 1333148864Scsjp if (req->lock != REQ_WIRED) 1334111883Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1335111883Sjhb "sysctl_old_user()"); 1336126253Struckman i = l; 1337127052Struckman len = req->validlen; 1338126253Struckman if (len <= origidx) 1339126253Struckman i = 0; 1340126253Struckman else { 1341126253Struckman if (i > len - origidx) 1342126253Struckman i = len - origidx; 1343126253Struckman error = copyout(p, (char *)req->oldptr + origidx, i); 134412260Sphk } 134512243Sphk if (error) 134612243Sphk return (error); 1347126253Struckman if (i < l) 134812243Sphk return (ENOMEM); 134912260Sphk return (0); 135012243Sphk} 135112243Sphk 135212260Sphkstatic int 135338517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l) 135412243Sphk{ 135512285Sphk int error; 135612260Sphk 135712260Sphk if (!req->newptr) 1358139483Spjd return (0); 135912260Sphk if (req->newlen - req->newidx < l) 136012243Sphk return (EINVAL); 1361148873Scsjp WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1362148873Scsjp "sysctl_new_user()"); 136317971Sbde error = copyin((char *)req->newptr + req->newidx, p, l); 136412243Sphk req->newidx += l; 136512243Sphk return (error); 136612243Sphk} 136712243Sphk 1368100487Struckman/* 1369100487Struckman * Wire the user space destination buffer. If set to a value greater than 1370100487Struckman * zero, the len parameter limits the maximum amount of wired memory. 1371100487Struckman */ 1372126253Struckmanint 1373100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1374100487Struckman{ 1375126253Struckman int ret; 1376154792Struckman size_t i, wiredlen; 1377154792Struckman char *cp, dummy; 1378126253Struckman 1379126253Struckman wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen; 1380126253Struckman ret = 0; 1381120781Sbms if (req->lock == REQ_LOCKED && req->oldptr && 1382120781Sbms req->oldfunc == sysctl_old_user) { 1383127050Struckman if (wiredlen != 0) { 1384127050Struckman ret = vslock(req->oldptr, wiredlen); 1385130327Sgreen if (ret != 0) { 1386130327Sgreen if (ret != ENOMEM) 1387130327Sgreen return (ret); 1388130327Sgreen wiredlen = 0; 1389130327Sgreen } 1390154792Struckman /* 1391154792Struckman * Touch all the wired pages to avoid PTE modified 1392154792Struckman * bit emulation traps on Alpha while holding locks 1393154792Struckman * in the sysctl handler. 1394154792Struckman */ 1395154792Struckman for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE, 1396154792Struckman cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) { 1397154792Struckman copyin(cp, &dummy, 1); 1398154792Struckman copyout(&dummy, cp, 1); 1399154792Struckman } 1400126253Struckman } 1401127050Struckman req->lock = REQ_WIRED; 1402127052Struckman req->validlen = wiredlen; 1403100487Struckman } 1404127050Struckman return (0); 1405100487Struckman} 1406100487Struckman 14071541Srgrimesint 140853977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 140953977Sgreen int *nindx, struct sysctl_req *req) 141012131Sphk{ 141144078Sdfr struct sysctl_oid *oid; 141253977Sgreen int indx; 141312131Sphk 1414188232Sjhb SYSCTL_ASSERT_LOCKED(); 141553977Sgreen oid = SLIST_FIRST(&sysctl__children); 141612131Sphk indx = 0; 141744078Sdfr while (oid && indx < CTL_MAXNAME) { 141844078Sdfr if (oid->oid_number == name[indx]) { 141912131Sphk indx++; 142044078Sdfr if (oid->oid_kind & CTLFLAG_NOLOCK) 1421120781Sbms req->lock = REQ_UNLOCKED; 142244078Sdfr if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 142353977Sgreen if (oid->oid_handler != NULL || 142453977Sgreen indx == namelen) { 142553977Sgreen *noid = oid; 142653977Sgreen if (nindx != NULL) 142753977Sgreen *nindx = indx; 142853977Sgreen return (0); 142953977Sgreen } 143053977Sgreen oid = SLIST_FIRST( 143153977Sgreen (struct sysctl_oid_list *)oid->oid_arg1); 143253977Sgreen } else if (indx == namelen) { 143353977Sgreen *noid = oid; 143453977Sgreen if (nindx != NULL) 143553977Sgreen *nindx = indx; 143653977Sgreen return (0); 143712131Sphk } else { 143853977Sgreen return (ENOTDIR); 143912131Sphk } 144012131Sphk } else { 144144078Sdfr oid = SLIST_NEXT(oid, oid_link); 144212131Sphk } 144312131Sphk } 144453977Sgreen return (ENOENT); 144553977Sgreen} 144653977Sgreen 144753977Sgreen/* 144853977Sgreen * Traverse our tree, and find the right node, execute whatever it points 144953977Sgreen * to, and return the resulting error code. 145053977Sgreen */ 145153977Sgreen 1452104094Sphkstatic int 145362573Sphksysctl_root(SYSCTL_HANDLER_ARGS) 145453977Sgreen{ 145553977Sgreen struct sysctl_oid *oid; 1456109246Sdillon int error, indx, lvl; 145753977Sgreen 1458188232Sjhb SYSCTL_ASSERT_LOCKED(); 1459186564Sed 146053977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 146153977Sgreen if (error) 146253977Sgreen return (error); 146353977Sgreen 146453977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 146553977Sgreen /* 146653977Sgreen * You can't call a sysctl when it's a node, but has 146753977Sgreen * no handler. Inform the user that it's a node. 146853977Sgreen * The indx may or may not be the same as namelen. 146953977Sgreen */ 147053977Sgreen if (oid->oid_handler == NULL) 147153977Sgreen return (EISDIR); 147253977Sgreen } 147353977Sgreen 147483968Srwatson /* Is this sysctl writable? */ 147583968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 147612131Sphk return (EPERM); 147712131Sphk 147892953Srwatson KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 147992953Srwatson 148083968Srwatson /* Is this sysctl sensitive to securelevels? */ 148183968Srwatson if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1482109246Sdillon lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1483109246Sdillon error = securelevel_gt(req->td->td_ucred, lvl); 148492953Srwatson if (error) 148592953Srwatson return (error); 148683968Srwatson } 148712910Sphk 148883968Srwatson /* Is this sysctl writable by only privileged users? */ 148983968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 149092953Srwatson if (oid->oid_kind & CTLFLAG_PRISON) 1491170587Srwatson error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL); 149292953Srwatson else 1493164033Srwatson error = priv_check(req->td, PRIV_SYSCTL_WRITE); 149492953Srwatson if (error) 149592953Srwatson return (error); 149683968Srwatson } 149783968Srwatson 149844078Sdfr if (!oid->oid_handler) 1499139483Spjd return (EINVAL); 150012131Sphk 1501126121Spjd if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1502132776Skan arg1 = (int *)arg1 + indx; 1503126121Spjd arg2 -= indx; 1504126121Spjd } else { 1505126121Spjd arg1 = oid->oid_arg1; 1506126121Spjd arg2 = oid->oid_arg2; 1507126121Spjd } 1508126121Spjd#ifdef MAC 1509172930Srwatson error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1510126121Spjd req); 1511126121Spjd if (error != 0) 1512126121Spjd return (error); 1513126121Spjd#endif 1514187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1515187656Sjhb mtx_lock(&Giant); 1516126121Spjd error = oid->oid_handler(oid, arg1, arg2, req); 1517187656Sjhb if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 1518187656Sjhb mtx_unlock(&Giant); 1519126121Spjd 152053977Sgreen return (error); 152112131Sphk} 152212131Sphk 152312221Sbde#ifndef _SYS_SYSPROTO_H_ 152412171Sphkstruct sysctl_args { 152512171Sphk int *name; 152612171Sphk u_int namelen; 152712171Sphk void *old; 152812171Sphk size_t *oldlenp; 152912171Sphk void *new; 153012171Sphk size_t newlen; 153112171Sphk}; 153212221Sbde#endif 153312131Sphkint 153483366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap) 15351541Srgrimes{ 1536188232Sjhb int error, i, name[CTL_MAXNAME]; 153738517Sdfr size_t j; 15381541Srgrimes 15391541Srgrimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 15401541Srgrimes return (EINVAL); 154111863Sphk 15423308Sphk error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 15433308Sphk if (error) 15441541Srgrimes return (error); 15451541Srgrimes 154683366Sjulian error = userland_sysctl(td, name, uap->namelen, 154712171Sphk uap->old, uap->oldlenp, 0, 1548136404Speter uap->new, uap->newlen, &j, 0); 154912260Sphk if (error && error != ENOMEM) 1550186564Sed return (error); 1551186664Sed if (uap->oldlenp) { 1552188232Sjhb i = copyout(&j, uap->oldlenp, sizeof(j)); 1553186664Sed if (i) 1554186664Sed return (i); 1555186664Sed } 155612260Sphk return (error); 155712171Sphk} 155812171Sphk 155912171Sphk/* 156012171Sphk * This is used from various compatibility syscalls too. That's why name 156112171Sphk * must be in kernel space. 156212171Sphk */ 156312171Sphkint 156483366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1565136404Speter size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval, 1566136404Speter int flags) 156712171Sphk{ 1568192125Sjhb int error = 0, memlocked; 1569127052Struckman struct sysctl_req req; 157012171Sphk 157112243Sphk bzero(&req, sizeof req); 157212243Sphk 157386183Srwatson req.td = td; 1574136404Speter req.flags = flags; 157512285Sphk 157612171Sphk if (oldlenp) { 157712171Sphk if (inkernel) { 157812243Sphk req.oldlen = *oldlenp; 157912171Sphk } else { 158012260Sphk error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 158112171Sphk if (error) 158212171Sphk return (error); 158312171Sphk } 158412171Sphk } 1585127052Struckman req.validlen = req.oldlen; 158612171Sphk 158712243Sphk if (old) { 158852644Sphk if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 158912243Sphk return (EFAULT); 159012243Sphk req.oldptr= old; 159112243Sphk } 159212131Sphk 159377646Sdd if (new != NULL) { 1594172038Srwatson if (!useracc(new, newlen, VM_PROT_READ)) 159512243Sphk return (EFAULT); 159612243Sphk req.newlen = newlen; 159712243Sphk req.newptr = new; 159811863Sphk } 159912131Sphk 160012243Sphk req.oldfunc = sysctl_old_user; 160112243Sphk req.newfunc = sysctl_new_user; 1602120781Sbms req.lock = REQ_LOCKED; 160311863Sphk 1604189707Sjhb#ifdef KTRACE 1605189707Sjhb if (KTRPOINT(curthread, KTR_SYSCTL)) 1606189707Sjhb ktrsysctl(name, namelen); 1607189707Sjhb#endif 1608192125Sjhb 1609192125Sjhb if (req.oldlen > PAGE_SIZE) { 1610192125Sjhb memlocked = 1; 1611192125Sjhb sx_xlock(&sysctlmemlock); 1612192125Sjhb } else 1613192125Sjhb memlocked = 0; 1614185348Szec CURVNET_SET(TD_TO_VNET(curthread)); 161512429Sphk 1616185983Skib for (;;) { 1617127052Struckman req.oldidx = 0; 1618127052Struckman req.newidx = 0; 1619192125Sjhb SYSCTL_SLOCK(); 1620127052Struckman error = sysctl_root(0, name, namelen, &req); 1621192125Sjhb SYSCTL_SUNLOCK(); 1622185983Skib if (error != EAGAIN) 1623185983Skib break; 1624185983Skib uio_yield(); 1625185983Skib } 162612243Sphk 1627186564Sed CURVNET_RESTORE(); 1628186564Sed 1629127052Struckman if (req.lock == REQ_WIRED && req.validlen > 0) 1630127052Struckman vsunlock(req.oldptr, req.validlen); 1631192125Sjhb if (memlocked) 1632192125Sjhb sx_xunlock(&sysctlmemlock); 163312429Sphk 163412260Sphk if (error && error != ENOMEM) 163512260Sphk return (error); 163612260Sphk 163712260Sphk if (retval) { 1638127052Struckman if (req.oldptr && req.oldidx > req.validlen) 1639127052Struckman *retval = req.validlen; 164012260Sphk else 164112260Sphk *retval = req.oldidx; 164211863Sphk } 164312260Sphk return (error); 16441541Srgrimes} 1645