kern_sysctl.c revision 120803
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 * 3. All advertising materials mentioning features or use of this software 201541Srgrimes * must display the following acknowledgement: 211541Srgrimes * This product includes software developed by the University of 221541Srgrimes * California, Berkeley and its contributors. 231541Srgrimes * 4. Neither the name of the University nor the names of its contributors 241541Srgrimes * may be used to endorse or promote products derived from this software 251541Srgrimes * without specific prior written permission. 261541Srgrimes * 271541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 281541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 291541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 301541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 311541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 321541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 331541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 341541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 351541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 361541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 371541Srgrimes * SUCH DAMAGE. 381541Srgrimes * 391541Srgrimes * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 401541Srgrimes */ 411541Srgrimes 42116182Sobrien#include <sys/cdefs.h> 43116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 120803 2003-10-05 09:37:47Z bms $"); 44116182Sobrien 4531778Seivind#include "opt_compat.h" 46106025Srwatson#include "opt_mac.h" 4731778Seivind 481541Srgrimes#include <sys/param.h> 4948274Speter#include <sys/systm.h> 5048274Speter#include <sys/kernel.h> 511541Srgrimes#include <sys/sysctl.h> 52106025Srwatson#include <sys/mac.h> 5312623Sphk#include <sys/malloc.h> 5412662Sdg#include <sys/proc.h> 5582746Sdillon#include <sys/lock.h> 5682746Sdillon#include <sys/mutex.h> 5793616Salfred#include <sys/sx.h> 5815103Sphk#include <sys/sysproto.h> 5912645Sbde#include <vm/vm.h> 6012662Sdg#include <vm/vm_extern.h> 6112645Sbde 6230354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 6363212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 64100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 6530309Sphk 6612429Sphk/* 6793625Srwatson * Locking - this locks the sysctl tree in memory. 6812429Sphk */ 6993625Srwatsonstatic struct sx sysctllock; 7012429Sphk 7193625Srwatson#define SYSCTL_LOCK() sx_xlock(&sysctllock) 72105999Smux#define SYSCTL_UNLOCK() sx_xunlock(&sysctllock) 73112107Sjhb#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") 7493616Salfred 7562573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS); 7612429Sphk 7744078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */ 7812152Sphk 7963212Sabialstatic struct sysctl_oid * 8063212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 8163212Sabial{ 8263212Sabial struct sysctl_oid *oidp; 8363212Sabial 8463212Sabial SLIST_FOREACH(oidp, list, oid_link) { 8563212Sabial if (strcmp(oidp->oid_name, name) == 0) { 8663212Sabial return (oidp); 8763212Sabial } 8863212Sabial } 8963212Sabial return (NULL); 9063212Sabial} 9163212Sabial 9212623Sphk/* 9312623Sphk * Initialization of the MIB tree. 9412623Sphk * 9544078Sdfr * Order by number in each list. 9612623Sphk */ 9712429Sphk 9880338Sroamvoid 9980338Sroamsysctl_register_oid(struct sysctl_oid *oidp) 10012152Sphk{ 10144078Sdfr struct sysctl_oid_list *parent = oidp->oid_parent; 10244078Sdfr struct sysctl_oid *p; 10344078Sdfr struct sysctl_oid *q; 10412197Sbde 10544078Sdfr /* 10663212Sabial * First check if another oid with the same name already 10763212Sabial * exists in the parent's list. 10863212Sabial */ 10963212Sabial p = sysctl_find_oidname(oidp->oid_name, parent); 11063212Sabial if (p != NULL) { 11163212Sabial if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 11263212Sabial p->oid_refcnt++; 11363212Sabial return; 11463212Sabial } else { 11563212Sabial printf("can't re-use a leaf (%s)!\n", p->oid_name); 11663212Sabial return; 11763212Sabial } 11863212Sabial } 11963212Sabial /* 12044078Sdfr * If this oid has a number OID_AUTO, give it a number which 12180339Sroam * is greater than any current oid. 12280339Sroam * NOTE: DO NOT change the starting value here, change it in 12380339Sroam * <sys/sysctl.h>, and make sure it is at least 256 to 12480339Sroam * accomodate e.g. net.inet.raw as a static sysctl node. 12544078Sdfr */ 12644078Sdfr if (oidp->oid_number == OID_AUTO) { 12780339Sroam static int newoid = CTL_AUTO_START; 12871510Smckusick 12971510Smckusick oidp->oid_number = newoid++; 13071510Smckusick if (newoid == 0x7fffffff) 13171510Smckusick panic("out of oids"); 13244078Sdfr } 13384832Sroam#if 0 13484832Sroam else if (oidp->oid_number >= CTL_AUTO_START) { 13584832Sroam /* do not panic; this happens when unregistering sysctl sets */ 13684832Sroam printf("static sysctl oid too high: %d", oidp->oid_number); 13784832Sroam } 13884832Sroam#endif 13944078Sdfr 14044078Sdfr /* 14144078Sdfr * Insert the oid into the parent's list in order. 14244078Sdfr */ 14344078Sdfr q = NULL; 14444078Sdfr SLIST_FOREACH(p, parent, oid_link) { 14544078Sdfr if (oidp->oid_number < p->oid_number) 14644078Sdfr break; 14744078Sdfr q = p; 14844078Sdfr } 14944078Sdfr if (q) 15044078Sdfr SLIST_INSERT_AFTER(q, oidp, oid_link); 15144078Sdfr else 15244078Sdfr SLIST_INSERT_HEAD(parent, oidp, oid_link); 15312152Sphk} 15412131Sphk 15580338Sroamvoid 15680338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp) 15712152Sphk{ 158115391Smux struct sysctl_oid *p; 159115391Smux int error; 160115391Smux 161115391Smux error = ENOENT; 162115391Smux if (oidp->oid_number == OID_AUTO) { 163115391Smux error = EINVAL; 164115391Smux } else { 165115391Smux SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 166115391Smux if (p == oidp) { 167115391Smux SLIST_REMOVE(oidp->oid_parent, oidp, 168115391Smux sysctl_oid, oid_link); 169115391Smux error = 0; 170115391Smux break; 171115391Smux } 172115391Smux } 173115391Smux } 174115391Smux 175115391Smux /* 176115391Smux * This can happen when a module fails to register and is 177115391Smux * being unloaded afterwards. It should not be a panic() 178115391Smux * for normal use. 179115391Smux */ 180115391Smux if (error) 181115391Smux printf("%s: failed to unregister sysctl\n", __func__); 18244078Sdfr} 18312152Sphk 18463212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */ 18563212Sabialint 18663212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c) 18763212Sabial{ 18863212Sabial 18963212Sabial if (c == NULL) { 19063212Sabial return (EINVAL); 19163212Sabial } 19263212Sabial TAILQ_INIT(c); 19363212Sabial return (0); 19463212Sabial} 19563212Sabial 19663212Sabial/* Free the context, and destroy all dynamic oids registered in this context */ 19763212Sabialint 19863212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist) 19963212Sabial{ 20063212Sabial struct sysctl_ctx_entry *e, *e1; 20163212Sabial int error; 20263212Sabial 20363212Sabial error = 0; 20463212Sabial /* 20563212Sabial * First perform a "dry run" to check if it's ok to remove oids. 20663212Sabial * XXX FIXME 20763212Sabial * XXX This algorithm is a hack. But I don't know any 20863212Sabial * XXX better solution for now... 20963212Sabial */ 21063212Sabial TAILQ_FOREACH(e, clist, link) { 21163212Sabial error = sysctl_remove_oid(e->entry, 0, 0); 21263212Sabial if (error) 21363212Sabial break; 21463212Sabial } 21563212Sabial /* 21663212Sabial * Restore deregistered entries, either from the end, 21763212Sabial * or from the place where error occured. 21863212Sabial * e contains the entry that was not unregistered 21963212Sabial */ 22063212Sabial if (error) 22163212Sabial e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 22263212Sabial else 22363212Sabial e1 = TAILQ_LAST(clist, sysctl_ctx_list); 22463212Sabial while (e1 != NULL) { 22563212Sabial sysctl_register_oid(e1->entry); 22663212Sabial e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 22763212Sabial } 22863212Sabial if (error) 22963212Sabial return(EBUSY); 23063212Sabial /* Now really delete the entries */ 23163212Sabial e = TAILQ_FIRST(clist); 23263212Sabial while (e != NULL) { 23363212Sabial e1 = TAILQ_NEXT(e, link); 23463212Sabial error = sysctl_remove_oid(e->entry, 1, 0); 23563212Sabial if (error) 23663212Sabial panic("sysctl_remove_oid: corrupt tree, entry: %s", 23763212Sabial e->entry->oid_name); 23863212Sabial free(e, M_SYSCTLOID); 23963212Sabial e = e1; 24063212Sabial } 24163212Sabial return (error); 24263212Sabial} 24363212Sabial 24463212Sabial/* Add an entry to the context */ 24563212Sabialstruct sysctl_ctx_entry * 24663212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 24763212Sabial{ 24863212Sabial struct sysctl_ctx_entry *e; 24963212Sabial 25063212Sabial if (clist == NULL || oidp == NULL) 25163212Sabial return(NULL); 252111119Simp e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 25363212Sabial e->entry = oidp; 25463212Sabial TAILQ_INSERT_HEAD(clist, e, link); 25563212Sabial return (e); 25663212Sabial} 25763212Sabial 25863212Sabial/* Find an entry in the context */ 25963212Sabialstruct sysctl_ctx_entry * 26063212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 26163212Sabial{ 26263212Sabial struct sysctl_ctx_entry *e; 26363212Sabial 26463212Sabial if (clist == NULL || oidp == NULL) 26563212Sabial return(NULL); 26671999Sphk TAILQ_FOREACH(e, clist, link) { 26763212Sabial if(e->entry == oidp) 26863212Sabial return(e); 26963212Sabial } 27063212Sabial return (e); 27163212Sabial} 27263212Sabial 27344078Sdfr/* 27463212Sabial * Delete an entry from the context. 27563212Sabial * NOTE: this function doesn't free oidp! You have to remove it 27663212Sabial * with sysctl_remove_oid(). 27763212Sabial */ 27863212Sabialint 27963212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 28063212Sabial{ 28163212Sabial struct sysctl_ctx_entry *e; 28263212Sabial 28363212Sabial if (clist == NULL || oidp == NULL) 28463212Sabial return (EINVAL); 28563212Sabial e = sysctl_ctx_entry_find(clist, oidp); 28663212Sabial if (e != NULL) { 28763212Sabial TAILQ_REMOVE(clist, e, link); 28863212Sabial free(e, M_SYSCTLOID); 28963212Sabial return (0); 29063212Sabial } else 29163212Sabial return (ENOENT); 29263212Sabial} 29363212Sabial 29463212Sabial/* 29563212Sabial * Remove dynamically created sysctl trees. 29663212Sabial * oidp - top of the tree to be removed 29763212Sabial * del - if 0 - just deregister, otherwise free up entries as well 29863212Sabial * recurse - if != 0 traverse the subtree to be deleted 29963212Sabial */ 30063212Sabialint 30163212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 30263212Sabial{ 30363212Sabial struct sysctl_oid *p; 30463212Sabial int error; 30563212Sabial 30663212Sabial if (oidp == NULL) 30763212Sabial return(EINVAL); 30863212Sabial if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 30963212Sabial printf("can't remove non-dynamic nodes!\n"); 31063212Sabial return (EINVAL); 31163212Sabial } 31263212Sabial /* 31363212Sabial * WARNING: normal method to do this should be through 31463212Sabial * sysctl_ctx_free(). Use recursing as the last resort 31563212Sabial * method to purge your sysctl tree of leftovers... 31663212Sabial * However, if some other code still references these nodes, 31763212Sabial * it will panic. 31863212Sabial */ 31963212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 32063212Sabial if (oidp->oid_refcnt == 1) { 32163212Sabial SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 32263212Sabial if (!recurse) 32363212Sabial return (ENOTEMPTY); 32463212Sabial error = sysctl_remove_oid(p, del, recurse); 32563212Sabial if (error) 32663212Sabial return (error); 32763212Sabial } 32863212Sabial if (del) 32963212Sabial free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 33063212Sabial } 33163212Sabial } 33263212Sabial if (oidp->oid_refcnt > 1 ) { 33363212Sabial oidp->oid_refcnt--; 33463212Sabial } else { 33563212Sabial if (oidp->oid_refcnt == 0) { 33663212Sabial printf("Warning: bad oid_refcnt=%u (%s)!\n", 33763212Sabial oidp->oid_refcnt, oidp->oid_name); 33863212Sabial return (EINVAL); 33963212Sabial } 34063212Sabial sysctl_unregister_oid(oidp); 34163212Sabial if (del) { 34288006Sluigi if (oidp->descr) 343100113Smarkm free((void *)(uintptr_t)(const void *)oidp->descr, M_SYSCTLOID); 34463978Speter free((void *)(uintptr_t)(const void *)oidp->oid_name, 34563978Speter M_SYSCTLOID); 34663212Sabial free(oidp, M_SYSCTLOID); 34763212Sabial } 34863212Sabial } 34963212Sabial return (0); 35063212Sabial} 35163212Sabial 35263212Sabial/* 35363212Sabial * Create new sysctls at run time. 35463212Sabial * clist may point to a valid context initialized with sysctl_ctx_init(). 35563212Sabial */ 35663212Sabialstruct sysctl_oid * 35763212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 35870679Sjhb int number, const char *name, int kind, void *arg1, int arg2, 35970679Sjhb int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 36063212Sabial{ 36163212Sabial struct sysctl_oid *oidp; 36263212Sabial ssize_t len; 36363978Speter char *newname; 36463212Sabial 36563212Sabial /* You have to hook up somewhere.. */ 36663212Sabial if (parent == NULL) 36763212Sabial return(NULL); 36863212Sabial /* Check if the node already exists, otherwise create it */ 36963212Sabial oidp = sysctl_find_oidname(name, parent); 37063212Sabial if (oidp != NULL) { 37163212Sabial if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 37263212Sabial oidp->oid_refcnt++; 37363212Sabial /* Update the context */ 37463212Sabial if (clist != NULL) 37563212Sabial sysctl_ctx_entry_add(clist, oidp); 37663212Sabial return (oidp); 37763212Sabial } else { 37863212Sabial printf("can't re-use a leaf (%s)!\n", name); 37963212Sabial return (NULL); 38063212Sabial } 38163212Sabial } 382111119Simp oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 38363212Sabial oidp->oid_parent = parent; 38463212Sabial SLIST_NEXT(oidp, oid_link) = NULL; 38563212Sabial oidp->oid_number = number; 38663212Sabial oidp->oid_refcnt = 1; 38763212Sabial len = strlen(name); 388111119Simp newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 38963978Speter bcopy(name, newname, len + 1); 39063978Speter newname[len] = '\0'; 39163978Speter oidp->oid_name = newname; 39263212Sabial oidp->oid_handler = handler; 39363212Sabial oidp->oid_kind = CTLFLAG_DYN | kind; 39463212Sabial if ((kind & CTLTYPE) == CTLTYPE_NODE) { 39563212Sabial /* Allocate space for children */ 39663212Sabial SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list), 397111119Simp M_SYSCTLOID, M_WAITOK); 39863212Sabial SLIST_INIT(SYSCTL_CHILDREN(oidp)); 39963212Sabial } else { 40063212Sabial oidp->oid_arg1 = arg1; 40163212Sabial oidp->oid_arg2 = arg2; 40263212Sabial } 40363212Sabial oidp->oid_fmt = fmt; 40488006Sluigi if (descr) { 40588006Sluigi int len = strlen(descr) + 1; 406111119Simp oidp->descr = malloc(len, M_SYSCTLOID, M_WAITOK); 40788006Sluigi if (oidp->descr) 408100113Smarkm strcpy((char *)(uintptr_t)(const void *)oidp->descr, descr); 40988006Sluigi } 41063212Sabial /* Update the context, if used */ 41163212Sabial if (clist != NULL) 41263212Sabial sysctl_ctx_entry_add(clist, oidp); 41363212Sabial /* Register this oid */ 41463212Sabial sysctl_register_oid(oidp); 41563212Sabial return (oidp); 41663212Sabial} 41763212Sabial 41863212Sabial/* 41944078Sdfr * Register the kernel's oids on startup. 42044078Sdfr */ 42178161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid); 42212152Sphk 42380338Sroamstatic void 42480338Sroamsysctl_register_all(void *arg) 42538869Sbde{ 42678161Speter struct sysctl_oid **oidp; 42778161Speter 42893625Srwatson SYSCTL_INIT(); 42978161Speter SET_FOREACH(oidp, sysctl_set) 43078161Speter sysctl_register_oid(*oidp); 43138869Sbde} 43244078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 43344078Sdfr 43412623Sphk/* 43512623Sphk * "Staff-functions" 43612623Sphk * 43712650Sphk * These functions implement a presently undocumented interface 43812650Sphk * used by the sysctl program to walk the tree, and get the type 43912650Sphk * so it can print the value. 44012650Sphk * This interface is under work and consideration, and should probably 44112650Sphk * be killed with a big axe by the first person who can find the time. 44212650Sphk * (be aware though, that the proper interface isn't as obvious as it 44312650Sphk * may seem, there are various conflicting requirements. 44412650Sphk * 44512623Sphk * {0,0} printf the entire MIB-tree. 44612623Sphk * {0,1,...} return the name of the "..." OID. 44742467Sphk * {0,2,...} return the next OID. 44812623Sphk * {0,3} return the OID of the name in "new" 44912650Sphk * {0,4,...} return the kind & format info for the "..." OID. 45088006Sluigi * {0,5,...} return the description the "..." OID. 45112623Sphk */ 45212623Sphk 45312152Sphkstatic void 45444078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 45512152Sphk{ 45644078Sdfr int k; 45744078Sdfr struct sysctl_oid *oidp; 45812152Sphk 45944078Sdfr SLIST_FOREACH(oidp, l, oid_link) { 46012152Sphk 46112152Sphk for (k=0; k<i; k++) 46212152Sphk printf(" "); 46312152Sphk 46444078Sdfr printf("%d %s ", oidp->oid_number, oidp->oid_name); 46512152Sphk 46612152Sphk printf("%c%c", 46744078Sdfr oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 46844078Sdfr oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 46912152Sphk 47044078Sdfr if (oidp->oid_handler) 47115241Sphk printf(" *Handler"); 47215241Sphk 47344078Sdfr switch (oidp->oid_kind & CTLTYPE) { 47412243Sphk case CTLTYPE_NODE: 47515241Sphk printf(" Node\n"); 47644078Sdfr if (!oidp->oid_handler) { 47712152Sphk sysctl_sysctl_debug_dump_node( 47844078Sdfr oidp->oid_arg1, i+2); 47912152Sphk } 48012152Sphk break; 48112152Sphk case CTLTYPE_INT: printf(" Int\n"); break; 48212152Sphk case CTLTYPE_STRING: printf(" String\n"); break; 48312152Sphk case CTLTYPE_QUAD: printf(" Quad\n"); break; 48412152Sphk case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 48512152Sphk default: printf("\n"); 48612152Sphk } 48712152Sphk 48812152Sphk } 48912152Sphk} 49012152Sphk 49112152Sphkstatic int 49262573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 49312152Sphk{ 49487024Speter int error; 49587024Speter 49693593Sjhb error = suser(req->td); 49787024Speter if (error) 49887024Speter return error; 49944078Sdfr sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 50012152Sphk return ENOENT; 50112152Sphk} 50212152Sphk 50312152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 50412623Sphk 0, 0, sysctl_sysctl_debug, "-", ""); 50512152Sphk 50612623Sphkstatic int 50762573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 50812623Sphk{ 50912623Sphk int *name = (int *) arg1; 51012623Sphk u_int namelen = arg2; 51144078Sdfr int error = 0; 51244078Sdfr struct sysctl_oid *oid; 51344972Sphk struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 51412623Sphk char buf[10]; 51512131Sphk 51612623Sphk while (namelen) { 51712623Sphk if (!lsp) { 51841514Sarchie snprintf(buf,sizeof(buf),"%d",*name); 51912623Sphk if (req->oldidx) 52012623Sphk error = SYSCTL_OUT(req, ".", 1); 52112623Sphk if (!error) 52212623Sphk error = SYSCTL_OUT(req, buf, strlen(buf)); 52312623Sphk if (error) 52412623Sphk return (error); 52512623Sphk namelen--; 52612623Sphk name++; 52712623Sphk continue; 52812623Sphk } 52944972Sphk lsp2 = 0; 53044078Sdfr SLIST_FOREACH(oid, lsp, oid_link) { 53144078Sdfr if (oid->oid_number != *name) 53212623Sphk continue; 53312131Sphk 53412623Sphk if (req->oldidx) 53512623Sphk error = SYSCTL_OUT(req, ".", 1); 53612623Sphk if (!error) 53744078Sdfr error = SYSCTL_OUT(req, oid->oid_name, 53844078Sdfr strlen(oid->oid_name)); 53912623Sphk if (error) 54012623Sphk return (error); 54112623Sphk 54212623Sphk namelen--; 54312623Sphk name++; 54412623Sphk 54544972Sphk if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 54612623Sphk break; 54712623Sphk 54844078Sdfr if (oid->oid_handler) 54912623Sphk break; 55012623Sphk 55144972Sphk lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 55212623Sphk break; 55312623Sphk } 55444972Sphk lsp = lsp2; 55512623Sphk } 55612623Sphk return (SYSCTL_OUT(req, "", 1)); 55712623Sphk} 55812623Sphk 55912623SphkSYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 56012623Sphk 56112623Sphkstatic int 56263978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 56344078Sdfr int *next, int *len, int level, struct sysctl_oid **oidpp) 56412623Sphk{ 56544078Sdfr struct sysctl_oid *oidp; 56612623Sphk 56712623Sphk *len = level; 56844078Sdfr SLIST_FOREACH(oidp, lsp, oid_link) { 56944078Sdfr *next = oidp->oid_number; 57044078Sdfr *oidpp = oidp; 57112623Sphk 572101650Smux if (oidp->oid_kind & CTLFLAG_SKIP) 573101650Smux continue; 574101650Smux 57512623Sphk if (!namelen) { 57644078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 57712623Sphk return 0; 57844078Sdfr if (oidp->oid_handler) 57912623Sphk /* We really should call the handler here...*/ 58012623Sphk return 0; 58144078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 58263978Speter if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 58344078Sdfr len, level+1, oidpp)) 58415241Sphk return 0; 585111260Srwatson goto emptynode; 58612623Sphk } 58712623Sphk 58844078Sdfr if (oidp->oid_number < *name) 58912623Sphk continue; 59012623Sphk 59144078Sdfr if (oidp->oid_number > *name) { 59244078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 59312623Sphk return 0; 59444078Sdfr if (oidp->oid_handler) 59512623Sphk return 0; 59644078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 59763978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 59844078Sdfr next+1, len, level+1, oidpp)) 59912623Sphk return (0); 60015241Sphk goto next; 60112623Sphk } 60244078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 60312623Sphk continue; 60412623Sphk 60544078Sdfr if (oidp->oid_handler) 60612623Sphk continue; 60712623Sphk 60844078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 60963978Speter if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 61044078Sdfr len, level+1, oidpp)) 61112623Sphk return (0); 61215241Sphk next: 61312623Sphk namelen = 1; 614111260Srwatson emptynode: 61512623Sphk *len = level; 61612623Sphk } 61712623Sphk return 1; 61812623Sphk} 61912623Sphk 62012623Sphkstatic int 62162573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 62212623Sphk{ 62312623Sphk int *name = (int *) arg1; 62412623Sphk u_int namelen = arg2; 62512623Sphk int i, j, error; 62612623Sphk struct sysctl_oid *oid; 62744078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 62812623Sphk int newoid[CTL_MAXNAME]; 62912623Sphk 63063978Speter i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 63112623Sphk if (i) 63212623Sphk return ENOENT; 63312650Sphk error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 63412623Sphk return (error); 63512623Sphk} 63612623Sphk 63712623SphkSYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 63812623Sphk 63912623Sphkstatic int 64044078Sdfrname2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp) 64112623Sphk{ 64244078Sdfr int i; 64344078Sdfr struct sysctl_oid *oidp; 64444078Sdfr struct sysctl_oid_list *lsp = &sysctl__children; 64512623Sphk char *p; 64612623Sphk 64712623Sphk if (!*name) 64812623Sphk return ENOENT; 64912623Sphk 65012623Sphk p = name + strlen(name) - 1 ; 65112623Sphk if (*p == '.') 65212623Sphk *p = '\0'; 65312623Sphk 65412623Sphk *len = 0; 65512623Sphk 65612623Sphk for (p = name; *p && *p != '.'; p++) 65712623Sphk ; 65812623Sphk i = *p; 65912623Sphk if (i == '.') 66012623Sphk *p = '\0'; 66112623Sphk 66244078Sdfr oidp = SLIST_FIRST(lsp); 66312623Sphk 66444078Sdfr while (oidp && *len < CTL_MAXNAME) { 66544078Sdfr if (strcmp(name, oidp->oid_name)) { 66644078Sdfr oidp = SLIST_NEXT(oidp, oid_link); 66712623Sphk continue; 66812623Sphk } 66944078Sdfr *oid++ = oidp->oid_number; 67012623Sphk (*len)++; 67112623Sphk 67212623Sphk if (!i) { 67344078Sdfr if (oidpp) 67444078Sdfr *oidpp = oidp; 67512623Sphk return (0); 67612623Sphk } 67712623Sphk 67844078Sdfr if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 67912623Sphk break; 68012623Sphk 68144078Sdfr if (oidp->oid_handler) 68212623Sphk break; 68312623Sphk 68444078Sdfr lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 68544078Sdfr oidp = SLIST_FIRST(lsp); 68612623Sphk name = p+1; 68712623Sphk for (p = name; *p && *p != '.'; p++) 68812623Sphk ; 68912623Sphk i = *p; 69012623Sphk if (i == '.') 69112623Sphk *p = '\0'; 69212623Sphk } 69312623Sphk return ENOENT; 69412623Sphk} 69512623Sphk 69612623Sphkstatic int 69762573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 69812623Sphk{ 69912623Sphk char *p; 70012623Sphk int error, oid[CTL_MAXNAME], len; 70112623Sphk struct sysctl_oid *op = 0; 70212623Sphk 70312623Sphk if (!req->newlen) 70412623Sphk return ENOENT; 70545140Sphk if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 70645140Sphk return (ENAMETOOLONG); 70712623Sphk 708111119Simp p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 70912623Sphk 71012623Sphk error = SYSCTL_IN(req, p, req->newlen); 71112623Sphk if (error) { 71212623Sphk free(p, M_SYSCTL); 71312623Sphk return (error); 71412623Sphk } 71512623Sphk 71612623Sphk p [req->newlen] = '\0'; 71712623Sphk 71812623Sphk error = name2oid(p, oid, &len, &op); 71912623Sphk 72012623Sphk free(p, M_SYSCTL); 72112623Sphk 72212623Sphk if (error) 72312623Sphk return (error); 72412623Sphk 72512650Sphk error = SYSCTL_OUT(req, oid, len * sizeof *oid); 72612623Sphk return (error); 72712623Sphk} 72812623Sphk 72912910SphkSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 73012623Sphk sysctl_sysctl_name2oid, "I", ""); 73112623Sphk 73212623Sphkstatic int 73362573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 73412623Sphk{ 73544078Sdfr struct sysctl_oid *oid; 73653977Sgreen int error; 73712623Sphk 73853977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 73953977Sgreen if (error) 74053977Sgreen return (error); 74112623Sphk 74244078Sdfr if (!oid->oid_fmt) 74353977Sgreen return (ENOENT); 74453977Sgreen error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 74553977Sgreen if (error) 74653977Sgreen return (error); 74753977Sgreen error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 74812650Sphk return (error); 74912623Sphk} 75012623Sphk 75142467Sphk 75212623SphkSYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 75312623Sphk 75488006Sluigistatic int 75588006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 75688006Sluigi{ 75788006Sluigi struct sysctl_oid *oid; 75888006Sluigi int error; 75988006Sluigi 76088006Sluigi error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 76188006Sluigi if (error) 76288006Sluigi return (error); 76388006Sluigi 76488006Sluigi if (!oid->descr) 76588006Sluigi return (ENOENT); 76688006Sluigi error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1); 76788006Sluigi return (error); 76888006Sluigi} 76988006Sluigi 77088006SluigiSYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 77188006Sluigi 77212243Sphk/* 77312623Sphk * Default "handler" functions. 77412623Sphk */ 77512623Sphk 77612623Sphk/* 77742095Sdfr * Handle an int, signed or unsigned. 77812243Sphk * Two cases: 77912243Sphk * a variable: point arg1 at it. 78012243Sphk * a constant: pass it in arg2. 78112243Sphk */ 78212243Sphk 78311865Sphkint 78462573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS) 78511863Sphk{ 786100833Struckman int tmpout, error = 0; 78711863Sphk 788100833Struckman /* 789100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 790100833Struckman */ 79112243Sphk if (arg1) 792100833Struckman tmpout = *(int *)arg1; 79320506Sbde else 794100833Struckman tmpout = arg2; 795100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 79611863Sphk 79712243Sphk if (error || !req->newptr) 79812243Sphk return (error); 79911863Sphk 80012243Sphk if (!arg1) 80112243Sphk error = EPERM; 80212243Sphk else 80312243Sphk error = SYSCTL_IN(req, arg1, sizeof(int)); 80412243Sphk return (error); 80511863Sphk} 80611863Sphk 80712243Sphk/* 80845140Sphk * Handle a long, signed or unsigned. arg1 points to it. 80938517Sdfr */ 81038517Sdfr 81138517Sdfrint 81262573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS) 81338517Sdfr{ 81438517Sdfr int error = 0; 815100833Struckman long tmpout; 81638517Sdfr 817100833Struckman /* 818100833Struckman * Attempt to get a coherent snapshot by making a copy of the data. 819100833Struckman */ 82045140Sphk if (!arg1) 82145140Sphk return (EINVAL); 822100833Struckman tmpout = *(long *)arg1; 823100833Struckman error = SYSCTL_OUT(req, &tmpout, sizeof(long)); 82438517Sdfr 82538517Sdfr if (error || !req->newptr) 82638517Sdfr return (error); 82738517Sdfr 82845140Sphk error = SYSCTL_IN(req, arg1, sizeof(long)); 82938517Sdfr return (error); 83038517Sdfr} 83138517Sdfr 83238517Sdfr/* 83312243Sphk * Handle our generic '\0' terminated 'C' string. 83412243Sphk * Two cases: 83512243Sphk * a variable string: point arg1 at it, arg2 is max length. 83612243Sphk * a constant string: point arg1 at it, arg2 is zero. 83712243Sphk */ 83812243Sphk 83911865Sphkint 84062573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS) 84111863Sphk{ 84212243Sphk int error=0; 843100833Struckman char *tmparg; 844100833Struckman size_t outlen; 84511863Sphk 846100833Struckman /* 847100833Struckman * Attempt to get a coherent snapshot by copying to a 848100833Struckman * temporary kernel buffer. 849100833Struckman */ 850100833Struckmanretry: 851100833Struckman outlen = strlen((char *)arg1)+1; 852111119Simp tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 853105354Srobert 854105354Srobert if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 855100833Struckman free(tmparg, M_SYSCTLTMP); 856100833Struckman goto retry; 857100833Struckman } 858105354Srobert 859100833Struckman error = SYSCTL_OUT(req, tmparg, outlen); 860100833Struckman free(tmparg, M_SYSCTLTMP); 86111863Sphk 86245140Sphk if (error || !req->newptr) 86312243Sphk return (error); 86411863Sphk 86545140Sphk if ((req->newlen - req->newidx) >= arg2) { 86645140Sphk error = EINVAL; 86712243Sphk } else { 86812243Sphk arg2 = (req->newlen - req->newidx); 86912243Sphk error = SYSCTL_IN(req, arg1, arg2); 87012243Sphk ((char *)arg1)[arg2] = '\0'; 87111863Sphk } 87212131Sphk 87312131Sphk return (error); 87411863Sphk} 87511863Sphk 87612243Sphk/* 87712243Sphk * Handle any kind of opaque data. 87812243Sphk * arg1 points to it, arg2 is the size. 87912243Sphk */ 88012243Sphk 88111865Sphkint 88262573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 88311863Sphk{ 884120803Sbms int error, tries; 885120803Sbms u_int generation; 88612243Sphk 887100833Struckman /* 888120803Sbms * Attempt to get a coherent snapshot, by using the thread 889120803Sbms * pre-emption counter updated from within mi_switch() to 890120803Sbms * determine if we were pre-empted during a bcopy() or 891120803Sbms * copyout(). Make 3 attempts at doing this before giving up. 892120803Sbms * If we encounter an error, stop immediately. 893100833Struckman */ 894120803Sbms tries = 0; 895120803Sbms do { 896120803Sbms generation = curthread->td_generation; 897100833Struckman error = SYSCTL_OUT(req, arg1, arg2); 898120803Sbms if (error) 899120803Sbms return (error); 900120803Sbms tries++; 901120803Sbms } while (generation != curthread->td_generation && tries < 3); 90212243Sphk 90312243Sphk error = SYSCTL_IN(req, arg1, arg2); 90412243Sphk 90512243Sphk return (error); 90612243Sphk} 90712243Sphk 90812260Sphk/* 90912260Sphk * Transfer functions to/from kernel space. 91012260Sphk * XXX: rather untested at this point 91112260Sphk */ 91212260Sphkstatic int 91338517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 91412243Sphk{ 91538517Sdfr size_t i = 0; 91612260Sphk 91712260Sphk if (req->oldptr) { 91838517Sdfr i = l; 91973971Stmm if (req->oldlen <= req->oldidx) 92073971Stmm i = 0; 92173971Stmm else 92273971Stmm if (i > req->oldlen - req->oldidx) 92373971Stmm i = req->oldlen - req->oldidx; 92412260Sphk if (i > 0) 92517971Sbde bcopy(p, (char *)req->oldptr + req->oldidx, i); 92612243Sphk } 92712260Sphk req->oldidx += l; 92816282Snate if (req->oldptr && i != l) 92911863Sphk return (ENOMEM); 93012260Sphk return (0); 93112243Sphk} 93212243Sphk 93312260Sphkstatic int 93438517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 93512243Sphk{ 93612260Sphk if (!req->newptr) 93712260Sphk return 0; 93812260Sphk if (req->newlen - req->newidx < l) 93911863Sphk return (EINVAL); 94017971Sbde bcopy((char *)req->newptr + req->newidx, p, l); 94112243Sphk req->newidx += l; 94212131Sphk return (0); 94311863Sphk} 94411863Sphk 94516282Snateint 94683366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 94776834Sjlemon size_t *oldlenp, void *new, size_t newlen, size_t *retval) 94816282Snate{ 94916282Snate int error = 0; 95016282Snate struct sysctl_req req; 95116282Snate 95216282Snate bzero(&req, sizeof req); 95316282Snate 95486183Srwatson req.td = td; 95516282Snate 95616282Snate if (oldlenp) { 95716282Snate req.oldlen = *oldlenp; 95816282Snate } 95916282Snate 96016282Snate if (old) { 96116282Snate req.oldptr= old; 96216282Snate } 96316282Snate 96477646Sdd if (new != NULL) { 96516282Snate req.newlen = newlen; 96616282Snate req.newptr = new; 96716282Snate } 96816282Snate 96916282Snate req.oldfunc = sysctl_old_kernel; 97016282Snate req.newfunc = sysctl_new_kernel; 971120781Sbms req.lock = REQ_LOCKED; 97216282Snate 97393625Srwatson SYSCTL_LOCK(); 97416282Snate 97516282Snate error = sysctl_root(0, name, namelen, &req); 976120798Sbms#if 0 977120781Sbms if (req.lock == REQ_WIRED) 97857975Sphk vsunlock(req.oldptr, req.oldlen); 979120798Sbms#endif 98016282Snate 98193625Srwatson SYSCTL_UNLOCK(); 98216282Snate 98316282Snate if (error && error != ENOMEM) 98416282Snate return (error); 98516282Snate 98616282Snate if (retval) { 98716282Snate if (req.oldptr && req.oldidx > req.oldlen) 98816282Snate *retval = req.oldlen; 98916282Snate else 99016282Snate *retval = req.oldidx; 99116282Snate } 99216282Snate return (error); 99316282Snate} 99416282Snate 99576834Sjlemonint 99683366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 99776834Sjlemon void *new, size_t newlen, size_t *retval) 99876834Sjlemon{ 99976834Sjlemon int oid[CTL_MAXNAME]; 100078620Smjacob size_t oidlen, plen; 100178620Smjacob int error; 100276834Sjlemon 100376834Sjlemon oid[0] = 0; /* sysctl internal magic */ 100476834Sjlemon oid[1] = 3; /* name2oid */ 100576834Sjlemon oidlen = sizeof(oid); 100676834Sjlemon 100783366Sjulian error = kernel_sysctl(td, oid, 2, oid, &oidlen, 100876834Sjlemon (void *)name, strlen(name), &plen); 100976834Sjlemon if (error) 101076834Sjlemon return (error); 101176834Sjlemon 101283366Sjulian error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 101376834Sjlemon new, newlen, retval); 101476834Sjlemon return (error); 101576834Sjlemon} 101676834Sjlemon 101712260Sphk/* 101812260Sphk * Transfer function to/from user space. 101912260Sphk */ 102012260Sphkstatic int 102138517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 102212243Sphk{ 102338517Sdfr int error = 0; 102438517Sdfr size_t i = 0; 102512243Sphk 1026120781Sbms if (req->lock == REQ_LOCKED && req->oldptr) 1027111883Sjhb WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1028111883Sjhb "sysctl_old_user()"); 102912260Sphk if (req->oldptr) { 103038517Sdfr i = l; 103173971Stmm if (req->oldlen <= req->oldidx) 103273971Stmm i = 0; 103373971Stmm else 103473971Stmm if (i > req->oldlen - req->oldidx) 103573971Stmm i = req->oldlen - req->oldidx; 103612260Sphk if (i > 0) 103717971Sbde error = copyout(p, (char *)req->oldptr + req->oldidx, 103817971Sbde i); 103912260Sphk } 104012260Sphk req->oldidx += l; 104112243Sphk if (error) 104212243Sphk return (error); 104312260Sphk if (req->oldptr && i < l) 104412243Sphk return (ENOMEM); 104512260Sphk return (0); 104612243Sphk} 104712243Sphk 104812260Sphkstatic int 104938517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l) 105012243Sphk{ 105112285Sphk int error; 105212260Sphk 105312260Sphk if (!req->newptr) 105412260Sphk return 0; 105512260Sphk if (req->newlen - req->newidx < l) 105612243Sphk return (EINVAL); 105717971Sbde error = copyin((char *)req->newptr + req->newidx, p, l); 105812243Sphk req->newidx += l; 105912243Sphk return (error); 106012243Sphk} 106112243Sphk 1062100487Struckman/* 1063100487Struckman * Wire the user space destination buffer. If set to a value greater than 1064100487Struckman * zero, the len parameter limits the maximum amount of wired memory. 1065100487Struckman * 1066100487Struckman * XXX - The len parameter is currently ignored due to the lack of 1067100487Struckman * a place to save it in the sysctl_req structure so that the matching 1068100487Struckman * amount of memory can be unwired in the sysctl exit code. 1069100487Struckman */ 1070100487Struckmanvoid 1071100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1072100487Struckman{ 1073120798Sbms#if 0 1074120781Sbms if (req->lock == REQ_LOCKED && req->oldptr && 1075120781Sbms req->oldfunc == sysctl_old_user) { 1076100487Struckman vslock(req->oldptr, req->oldlen); 1077120781Sbms req->lock = REQ_WIRED; 1078100487Struckman } 1079120798Sbms#endif 1080100487Struckman} 1081100487Struckman 10821541Srgrimesint 108353977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 108453977Sgreen int *nindx, struct sysctl_req *req) 108512131Sphk{ 108644078Sdfr struct sysctl_oid *oid; 108753977Sgreen int indx; 108812131Sphk 108953977Sgreen oid = SLIST_FIRST(&sysctl__children); 109012131Sphk indx = 0; 109144078Sdfr while (oid && indx < CTL_MAXNAME) { 109244078Sdfr if (oid->oid_number == name[indx]) { 109312131Sphk indx++; 109444078Sdfr if (oid->oid_kind & CTLFLAG_NOLOCK) 1095120781Sbms req->lock = REQ_UNLOCKED; 109644078Sdfr if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 109753977Sgreen if (oid->oid_handler != NULL || 109853977Sgreen indx == namelen) { 109953977Sgreen *noid = oid; 110053977Sgreen if (nindx != NULL) 110153977Sgreen *nindx = indx; 110253977Sgreen return (0); 110353977Sgreen } 110453977Sgreen oid = SLIST_FIRST( 110553977Sgreen (struct sysctl_oid_list *)oid->oid_arg1); 110653977Sgreen } else if (indx == namelen) { 110753977Sgreen *noid = oid; 110853977Sgreen if (nindx != NULL) 110953977Sgreen *nindx = indx; 111053977Sgreen return (0); 111112131Sphk } else { 111253977Sgreen return (ENOTDIR); 111312131Sphk } 111412131Sphk } else { 111544078Sdfr oid = SLIST_NEXT(oid, oid_link); 111612131Sphk } 111712131Sphk } 111853977Sgreen return (ENOENT); 111953977Sgreen} 112053977Sgreen 112153977Sgreen/* 112253977Sgreen * Traverse our tree, and find the right node, execute whatever it points 112353977Sgreen * to, and return the resulting error code. 112453977Sgreen */ 112553977Sgreen 1126104094Sphkstatic int 112762573Sphksysctl_root(SYSCTL_HANDLER_ARGS) 112853977Sgreen{ 112953977Sgreen struct sysctl_oid *oid; 1130109246Sdillon int error, indx, lvl; 113153977Sgreen 113253977Sgreen error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 113353977Sgreen if (error) 113453977Sgreen return (error); 113553977Sgreen 113653977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 113753977Sgreen /* 113853977Sgreen * You can't call a sysctl when it's a node, but has 113953977Sgreen * no handler. Inform the user that it's a node. 114053977Sgreen * The indx may or may not be the same as namelen. 114153977Sgreen */ 114253977Sgreen if (oid->oid_handler == NULL) 114353977Sgreen return (EISDIR); 114453977Sgreen } 114553977Sgreen 114683968Srwatson /* Is this sysctl writable? */ 114783968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 114812131Sphk return (EPERM); 114912131Sphk 115092953Srwatson KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 115192953Srwatson 115283968Srwatson /* Is this sysctl sensitive to securelevels? */ 115383968Srwatson if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1154109246Sdillon lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1155109246Sdillon error = securelevel_gt(req->td->td_ucred, lvl); 115692953Srwatson if (error) 115792953Srwatson return (error); 115883968Srwatson } 115912910Sphk 116083968Srwatson /* Is this sysctl writable by only privileged users? */ 116183968Srwatson if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 116292953Srwatson int flags; 116383968Srwatson 116492953Srwatson if (oid->oid_kind & CTLFLAG_PRISON) 116592953Srwatson flags = PRISON_ROOT; 116692953Srwatson else 116792953Srwatson flags = 0; 116893593Sjhb error = suser_cred(req->td->td_ucred, flags); 116992953Srwatson if (error) 117092953Srwatson return (error); 117183968Srwatson } 117283968Srwatson 117344078Sdfr if (!oid->oid_handler) 117412131Sphk return EINVAL; 117512131Sphk 117653977Sgreen if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) 117753977Sgreen error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx, 117853977Sgreen req); 117953977Sgreen else 118053977Sgreen error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2, 118153977Sgreen req); 118253977Sgreen return (error); 118312131Sphk} 118412131Sphk 118512221Sbde#ifndef _SYS_SYSPROTO_H_ 118612171Sphkstruct sysctl_args { 118712171Sphk int *name; 118812171Sphk u_int namelen; 118912171Sphk void *old; 119012171Sphk size_t *oldlenp; 119112171Sphk void *new; 119212171Sphk size_t newlen; 119312171Sphk}; 119412221Sbde#endif 119512171Sphk 119682746Sdillon/* 119782746Sdillon * MPSAFE 119882746Sdillon */ 119912131Sphkint 120083366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap) 12011541Srgrimes{ 120282746Sdillon int error, name[CTL_MAXNAME]; 120338517Sdfr size_t j; 12041541Srgrimes 12051541Srgrimes if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 12061541Srgrimes return (EINVAL); 120711863Sphk 12083308Sphk error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 12093308Sphk if (error) 12101541Srgrimes return (error); 12111541Srgrimes 121282746Sdillon mtx_lock(&Giant); 121382746Sdillon 121483366Sjulian error = userland_sysctl(td, name, uap->namelen, 121512171Sphk uap->old, uap->oldlenp, 0, 121612260Sphk uap->new, uap->newlen, &j); 121712260Sphk if (error && error != ENOMEM) 121882746Sdillon goto done2; 121912260Sphk if (uap->oldlenp) { 122082746Sdillon int i = copyout(&j, uap->oldlenp, sizeof(j)); 122112260Sphk if (i) 122282746Sdillon error = i; 122312260Sphk } 122482746Sdillondone2: 122582746Sdillon mtx_unlock(&Giant); 122612260Sphk return (error); 122712171Sphk} 122812171Sphk 122912171Sphk/* 123012171Sphk * This is used from various compatibility syscalls too. That's why name 123112171Sphk * must be in kernel space. 123212171Sphk */ 123312171Sphkint 123483366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 123580338Sroam size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval) 123612171Sphk{ 123712429Sphk int error = 0; 123816159Sphk struct sysctl_req req, req2; 123912171Sphk 124012243Sphk bzero(&req, sizeof req); 124112243Sphk 124286183Srwatson req.td = td; 124312285Sphk 124412171Sphk if (oldlenp) { 124512171Sphk if (inkernel) { 124612243Sphk req.oldlen = *oldlenp; 124712171Sphk } else { 124812260Sphk error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 124912171Sphk if (error) 125012171Sphk return (error); 125112171Sphk } 125212171Sphk } 125312171Sphk 125412243Sphk if (old) { 125552644Sphk if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 125612243Sphk return (EFAULT); 125712243Sphk req.oldptr= old; 125812243Sphk } 125912131Sphk 126077646Sdd if (new != NULL) { 126152644Sphk if (!useracc(new, req.newlen, VM_PROT_READ)) 126212243Sphk return (EFAULT); 126312243Sphk req.newlen = newlen; 126412243Sphk req.newptr = new; 126511863Sphk } 126612131Sphk 126712243Sphk req.oldfunc = sysctl_old_user; 126812243Sphk req.newfunc = sysctl_new_user; 1269120781Sbms req.lock = REQ_LOCKED; 127011863Sphk 127193625Srwatson SYSCTL_LOCK(); 127212429Sphk 1273106025Srwatson#ifdef MAC 1274106025Srwatson error = mac_check_system_sysctl(td->td_ucred, name, namelen, old, 1275106025Srwatson oldlenp, inkernel, new, newlen); 1276106025Srwatson if (error) { 1277106025Srwatson SYSCTL_UNLOCK(); 1278106025Srwatson return (error); 1279106025Srwatson } 1280106025Srwatson#endif 1281106025Srwatson 128216159Sphk do { 128316159Sphk req2 = req; 128416159Sphk error = sysctl_root(0, name, namelen, &req2); 128516159Sphk } while (error == EAGAIN); 128612243Sphk 128716159Sphk req = req2; 1288120798Sbms#if 0 1289120781Sbms if (req.lock == REQ_WIRED) 129057975Sphk vsunlock(req.oldptr, req.oldlen); 1291120798Sbms#endif 129212429Sphk 129393625Srwatson SYSCTL_UNLOCK(); 129412429Sphk 129512260Sphk if (error && error != ENOMEM) 129612260Sphk return (error); 129712260Sphk 129812260Sphk if (retval) { 129912260Sphk if (req.oldptr && req.oldidx > req.oldlen) 130012243Sphk *retval = req.oldlen; 130112260Sphk else 130212260Sphk *retval = req.oldidx; 130311863Sphk } 130412260Sphk return (error); 13051541Srgrimes} 13061541Srgrimes 13071541Srgrimes#ifdef COMPAT_43 13081541Srgrimes#include <sys/socket.h> 130915103Sphk#include <vm/vm_param.h> 131015103Sphk 13111541Srgrimes#define KINFO_PROC (0<<8) 13121541Srgrimes#define KINFO_RT (1<<8) 13131541Srgrimes#define KINFO_VNODE (2<<8) 13141541Srgrimes#define KINFO_FILE (3<<8) 13151541Srgrimes#define KINFO_METER (4<<8) 13161541Srgrimes#define KINFO_LOADAVG (5<<8) 13171541Srgrimes#define KINFO_CLOCKRATE (6<<8) 13181541Srgrimes 13199455Speter/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 13209455Speter#define KINFO_BSDI_SYSINFO (101<<8) 13219455Speter 13229455Speter/* 13239455Speter * XXX this is bloat, but I hope it's better here than on the potentially 13249455Speter * limited kernel stack... -Peter 13259455Speter */ 13269455Speter 132712819Sphkstatic struct { 13289455Speter int bsdi_machine; /* "i386" on BSD/386 */ 13299455Speter/* ^^^ this is an offset to the string, relative to the struct start */ 13309455Speter char *pad0; 13319455Speter long pad1; 13329455Speter long pad2; 13339455Speter long pad3; 13349455Speter u_long pad4; 13359455Speter u_long pad5; 13369455Speter u_long pad6; 13379455Speter 13389455Speter int bsdi_ostype; /* "BSD/386" on BSD/386 */ 13399455Speter int bsdi_osrelease; /* "1.1" on BSD/386 */ 13409455Speter long pad7; 13419455Speter long pad8; 13429455Speter char *pad9; 13439455Speter 13449455Speter long pad10; 13459455Speter long pad11; 13469455Speter int pad12; 13479455Speter long pad13; 13489455Speter quad_t pad14; 13499455Speter long pad15; 13509455Speter 13519455Speter struct timeval pad16; 13529455Speter /* we dont set this, because BSDI's uname used gethostname() instead */ 13539455Speter int bsdi_hostname; /* hostname on BSD/386 */ 13549455Speter 13559455Speter /* the actual string data is appended here */ 13569455Speter 13579455Speter} bsdi_si; 13589455Speter/* 13599455Speter * this data is appended to the end of the bsdi_si structure during copyout. 13609455Speter * The "char *" offsets are relative to the base of the bsdi_si struct. 13619455Speter * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 13629455Speter * should not exceed the length of the buffer here... (or else!! :-) 13639455Speter */ 136412819Sphkstatic char bsdi_strings[80]; /* It had better be less than this! */ 13659455Speter 136612221Sbde#ifndef _SYS_SYSPROTO_H_ 13671541Srgrimesstruct getkerninfo_args { 13681541Srgrimes int op; 13691541Srgrimes char *where; 137038864Sbde size_t *size; 13711541Srgrimes int arg; 13721541Srgrimes}; 137312221Sbde#endif 13741541Srgrimes 137582746Sdillon/* 137682746Sdillon * MPSAFE 137782746Sdillon */ 13781549Srgrimesint 137983366Sjulianogetkerninfo(struct thread *td, struct getkerninfo_args *uap) 13801541Srgrimes{ 138112171Sphk int error, name[6]; 138238517Sdfr size_t size; 138382494Speter u_int needed = 0; 13841541Srgrimes 138582746Sdillon mtx_lock(&Giant); 138682746Sdillon 13871541Srgrimes switch (uap->op & 0xff00) { 13881541Srgrimes 13891541Srgrimes case KINFO_RT: 139012171Sphk name[0] = CTL_NET; 139112171Sphk name[1] = PF_ROUTE; 139212171Sphk name[2] = 0; 139312171Sphk name[3] = (uap->op & 0xff0000) >> 16; 139412171Sphk name[4] = uap->op & 0xff; 139512171Sphk name[5] = uap->arg; 139683366Sjulian error = userland_sysctl(td, name, 6, uap->where, uap->size, 139712429Sphk 0, 0, 0, &size); 13981541Srgrimes break; 13991541Srgrimes 14001541Srgrimes case KINFO_VNODE: 140112171Sphk name[0] = CTL_KERN; 140212171Sphk name[1] = KERN_VNODE; 140383366Sjulian error = userland_sysctl(td, name, 2, uap->where, uap->size, 140412429Sphk 0, 0, 0, &size); 14051541Srgrimes break; 14061541Srgrimes 14071541Srgrimes case KINFO_PROC: 140812171Sphk name[0] = CTL_KERN; 140912171Sphk name[1] = KERN_PROC; 141012171Sphk name[2] = uap->op & 0xff; 141112171Sphk name[3] = uap->arg; 141283366Sjulian error = userland_sysctl(td, name, 4, uap->where, uap->size, 141312429Sphk 0, 0, 0, &size); 14141541Srgrimes break; 14151541Srgrimes 14161541Srgrimes case KINFO_FILE: 141712171Sphk name[0] = CTL_KERN; 141812171Sphk name[1] = KERN_FILE; 141983366Sjulian error = userland_sysctl(td, name, 2, uap->where, uap->size, 142012429Sphk 0, 0, 0, &size); 14211541Srgrimes break; 14221541Srgrimes 14231541Srgrimes case KINFO_METER: 142412171Sphk name[0] = CTL_VM; 1425109102Smux name[1] = VM_TOTAL; 142683366Sjulian error = userland_sysctl(td, name, 2, uap->where, uap->size, 142712429Sphk 0, 0, 0, &size); 14281541Srgrimes break; 14291541Srgrimes 14301541Srgrimes case KINFO_LOADAVG: 143112171Sphk name[0] = CTL_VM; 143212171Sphk name[1] = VM_LOADAVG; 143383366Sjulian error = userland_sysctl(td, name, 2, uap->where, uap->size, 143412429Sphk 0, 0, 0, &size); 14351541Srgrimes break; 14361541Srgrimes 14371541Srgrimes case KINFO_CLOCKRATE: 143812171Sphk name[0] = CTL_KERN; 143912171Sphk name[1] = KERN_CLOCKRATE; 144083366Sjulian error = userland_sysctl(td, name, 2, uap->where, uap->size, 144112429Sphk 0, 0, 0, &size); 14421541Srgrimes break; 14431541Srgrimes 14449455Speter case KINFO_BSDI_SYSINFO: { 14459455Speter /* 14469455Speter * this is pretty crude, but it's just enough for uname() 14479455Speter * from BSDI's 1.x libc to work. 14489455Speter * 144982494Speter * *size gives the size of the buffer before the call, and 145082494Speter * the amount of data copied after a successful call. 145182494Speter * If successful, the return value is the amount of data 145282494Speter * available, which can be larger than *size. 145382494Speter * 145482494Speter * BSDI's 2.x product apparently fails with ENOMEM if *size 145582494Speter * is too small. 14569455Speter */ 14579455Speter 14589455Speter u_int left; 14599455Speter char *s; 14609455Speter 14619455Speter bzero((char *)&bsdi_si, sizeof(bsdi_si)); 14629455Speter bzero(bsdi_strings, sizeof(bsdi_strings)); 14639455Speter 14649455Speter s = bsdi_strings; 14659455Speter 14669455Speter bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 14679455Speter strcpy(s, ostype); 14689455Speter s += strlen(s) + 1; 14699455Speter 14709455Speter bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 14719455Speter strcpy(s, osrelease); 14729455Speter s += strlen(s) + 1; 14739455Speter 14749455Speter bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 14759455Speter strcpy(s, machine); 14769455Speter s += strlen(s) + 1; 14779455Speter 14789455Speter needed = sizeof(bsdi_si) + (s - bsdi_strings); 14799455Speter 148082494Speter if ((uap->where == NULL) || (uap->size == NULL)) { 14819455Speter /* process is asking how much buffer to supply.. */ 14829455Speter size = needed; 14839455Speter error = 0; 14849455Speter break; 14859455Speter } 14869455Speter 148782494Speter if ((error = copyin(uap->size, &size, sizeof(size))) != 0) 148882494Speter break; 14899455Speter 14909455Speter /* if too much buffer supplied, trim it down */ 14919455Speter if (size > needed) 14929455Speter size = needed; 14939455Speter 14949455Speter /* how much of the buffer is remaining */ 14959455Speter left = size; 14969455Speter 14979455Speter if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 14989455Speter break; 14999455Speter 15009455Speter /* is there any point in continuing? */ 15019455Speter if (left > sizeof(bsdi_si)) { 15029455Speter left -= sizeof(bsdi_si); 15039455Speter error = copyout(&bsdi_strings, 15049455Speter uap->where + sizeof(bsdi_si), left); 15059455Speter } 15069455Speter break; 15079455Speter } 15089455Speter 15091541Srgrimes default: 151082746Sdillon error = EOPNOTSUPP; 151182746Sdillon break; 15121541Srgrimes } 151382746Sdillon if (error == 0) { 151483366Sjulian td->td_retval[0] = needed ? needed : size; 151582746Sdillon if (uap->size) { 151699012Salfred error = copyout(&size, uap->size, sizeof(size)); 151782746Sdillon } 151882746Sdillon } 151982746Sdillon mtx_unlock(&Giant); 15201541Srgrimes return (error); 15211541Srgrimes} 15221541Srgrimes#endif /* COMPAT_43 */ 1523