The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sysctl.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Mike Karels at Berkeley Software Design, Inc.
    7  *
    8  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
    9  * project, to make these variables more userfriendly.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 4. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  *      @(#)kern_sysctl.c       8.4 (Berkeley) 4/14/94
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD: releng/8.3/sys/kern/kern_sysctl.c 222579 2011-06-01 18:26:59Z mdf $");
   40 
   41 #include "opt_compat.h"
   42 #include "opt_ktrace.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/fail.h>
   46 #include <sys/systm.h>
   47 #include <sys/kernel.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/malloc.h>
   50 #include <sys/priv.h>
   51 #include <sys/proc.h>
   52 #include <sys/jail.h>
   53 #include <sys/lock.h>
   54 #include <sys/mutex.h>
   55 #include <sys/sbuf.h>
   56 #include <sys/sx.h>
   57 #include <sys/sysproto.h>
   58 #include <sys/uio.h>
   59 #ifdef KTRACE
   60 #include <sys/ktrace.h>
   61 #endif
   62 
   63 #include <net/vnet.h>
   64 
   65 #include <security/mac/mac_framework.h>
   66 
   67 #include <vm/vm.h>
   68 #include <vm/vm_extern.h>
   69 
   70 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
   71 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
   72 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
   73 
   74 /*
   75  * The sysctllock protects the MIB tree.  It also protects sysctl
   76  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
   77  * sysctl_unregister_oid() routines require the sysctllock to already
   78  * be held, so the sysctl_lock() and sysctl_unlock() routines are
   79  * provided for the few places in the kernel which need to use that
   80  * API rather than using the dynamic API.  Use of the dynamic API is
   81  * strongly encouraged for most code.
   82  *
   83  * The sysctlmemlock is used to limit the amount of user memory wired for
   84  * sysctl requests.  This is implemented by serializing any userland
   85  * sysctl requests larger than a single page via an exclusive lock.
   86  */
   87 static struct sx sysctllock;
   88 static struct sx sysctlmemlock;
   89 
   90 #define SYSCTL_XLOCK()          sx_xlock(&sysctllock)
   91 #define SYSCTL_XUNLOCK()        sx_xunlock(&sysctllock)
   92 #define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
   93 #define SYSCTL_INIT()           sx_init(&sysctllock, "sysctl lock")
   94 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
   95                                 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
   96 
   97 static int sysctl_root(SYSCTL_HANDLER_ARGS);
   98 
   99 struct sysctl_oid_list sysctl__children; /* root list */
  100 
  101 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
  102                     int recurse);
  103 
  104 static struct sysctl_oid *
  105 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
  106 {
  107         struct sysctl_oid *oidp;
  108 
  109         SYSCTL_ASSERT_XLOCKED();
  110         SLIST_FOREACH(oidp, list, oid_link) {
  111                 if (strcmp(oidp->oid_name, name) == 0) {
  112                         return (oidp);
  113                 }
  114         }
  115         return (NULL);
  116 }
  117 
  118 /*
  119  * Initialization of the MIB tree.
  120  *
  121  * Order by number in each list.
  122  */
  123 void
  124 sysctl_lock(void)
  125 {
  126 
  127         SYSCTL_XLOCK();
  128 }
  129 
  130 void
  131 sysctl_unlock(void)
  132 {
  133 
  134         SYSCTL_XUNLOCK();
  135 }
  136 
  137 void
  138 sysctl_register_oid(struct sysctl_oid *oidp)
  139 {
  140         struct sysctl_oid_list *parent = oidp->oid_parent;
  141         struct sysctl_oid *p;
  142         struct sysctl_oid *q;
  143 
  144         /*
  145          * First check if another oid with the same name already
  146          * exists in the parent's list.
  147          */
  148         SYSCTL_ASSERT_XLOCKED();
  149         p = sysctl_find_oidname(oidp->oid_name, parent);
  150         if (p != NULL) {
  151                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  152                         p->oid_refcnt++;
  153                         return;
  154                 } else {
  155                         printf("can't re-use a leaf (%s)!\n", p->oid_name);
  156                         return;
  157                 }
  158         }
  159         /*
  160          * If this oid has a number OID_AUTO, give it a number which
  161          * is greater than any current oid.
  162          * NOTE: DO NOT change the starting value here, change it in
  163          * <sys/sysctl.h>, and make sure it is at least 256 to
  164          * accomodate e.g. net.inet.raw as a static sysctl node.
  165          */
  166         if (oidp->oid_number == OID_AUTO) {
  167                 static int newoid = CTL_AUTO_START;
  168 
  169                 oidp->oid_number = newoid++;
  170                 if (newoid == 0x7fffffff)
  171                         panic("out of oids");
  172         }
  173 #if 0
  174         else if (oidp->oid_number >= CTL_AUTO_START) {
  175                 /* do not panic; this happens when unregistering sysctl sets */
  176                 printf("static sysctl oid too high: %d", oidp->oid_number);
  177         }
  178 #endif
  179 
  180         /*
  181          * Insert the oid into the parent's list in order.
  182          */
  183         q = NULL;
  184         SLIST_FOREACH(p, parent, oid_link) {
  185                 if (oidp->oid_number < p->oid_number)
  186                         break;
  187                 q = p;
  188         }
  189         if (q)
  190                 SLIST_INSERT_AFTER(q, oidp, oid_link);
  191         else
  192                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
  193 }
  194 
  195 void
  196 sysctl_unregister_oid(struct sysctl_oid *oidp)
  197 {
  198         struct sysctl_oid *p;
  199         int error;
  200 
  201         SYSCTL_ASSERT_XLOCKED();
  202         error = ENOENT;
  203         if (oidp->oid_number == OID_AUTO) {
  204                 error = EINVAL;
  205         } else {
  206                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
  207                         if (p == oidp) {
  208                                 SLIST_REMOVE(oidp->oid_parent, oidp,
  209                                     sysctl_oid, oid_link);
  210                                 error = 0;
  211                                 break;
  212                         }
  213                 }
  214         }
  215 
  216         /* 
  217          * This can happen when a module fails to register and is
  218          * being unloaded afterwards.  It should not be a panic()
  219          * for normal use.
  220          */
  221         if (error)
  222                 printf("%s: failed to unregister sysctl\n", __func__);
  223 }
  224 
  225 /* Initialize a new context to keep track of dynamically added sysctls. */
  226 int
  227 sysctl_ctx_init(struct sysctl_ctx_list *c)
  228 {
  229 
  230         if (c == NULL) {
  231                 return (EINVAL);
  232         }
  233 
  234         /*
  235          * No locking here, the caller is responsible for not adding
  236          * new nodes to a context until after this function has
  237          * returned.
  238          */
  239         TAILQ_INIT(c);
  240         return (0);
  241 }
  242 
  243 /* Free the context, and destroy all dynamic oids registered in this context */
  244 int
  245 sysctl_ctx_free(struct sysctl_ctx_list *clist)
  246 {
  247         struct sysctl_ctx_entry *e, *e1;
  248         int error;
  249 
  250         error = 0;
  251         /*
  252          * First perform a "dry run" to check if it's ok to remove oids.
  253          * XXX FIXME
  254          * XXX This algorithm is a hack. But I don't know any
  255          * XXX better solution for now...
  256          */
  257         SYSCTL_XLOCK();
  258         TAILQ_FOREACH(e, clist, link) {
  259                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
  260                 if (error)
  261                         break;
  262         }
  263         /*
  264          * Restore deregistered entries, either from the end,
  265          * or from the place where error occured.
  266          * e contains the entry that was not unregistered
  267          */
  268         if (error)
  269                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
  270         else
  271                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
  272         while (e1 != NULL) {
  273                 sysctl_register_oid(e1->entry);
  274                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
  275         }
  276         if (error) {
  277                 SYSCTL_XUNLOCK();
  278                 return(EBUSY);
  279         }
  280         /* Now really delete the entries */
  281         e = TAILQ_FIRST(clist);
  282         while (e != NULL) {
  283                 e1 = TAILQ_NEXT(e, link);
  284                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
  285                 if (error)
  286                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
  287                             e->entry->oid_name);
  288                 free(e, M_SYSCTLOID);
  289                 e = e1;
  290         }
  291         SYSCTL_XUNLOCK();
  292         return (error);
  293 }
  294 
  295 /* Add an entry to the context */
  296 struct sysctl_ctx_entry *
  297 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  298 {
  299         struct sysctl_ctx_entry *e;
  300 
  301         SYSCTL_ASSERT_XLOCKED();
  302         if (clist == NULL || oidp == NULL)
  303                 return(NULL);
  304         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
  305         e->entry = oidp;
  306         TAILQ_INSERT_HEAD(clist, e, link);
  307         return (e);
  308 }
  309 
  310 /* Find an entry in the context */
  311 struct sysctl_ctx_entry *
  312 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  313 {
  314         struct sysctl_ctx_entry *e;
  315 
  316         SYSCTL_ASSERT_XLOCKED();
  317         if (clist == NULL || oidp == NULL)
  318                 return(NULL);
  319         TAILQ_FOREACH(e, clist, link) {
  320                 if(e->entry == oidp)
  321                         return(e);
  322         }
  323         return (e);
  324 }
  325 
  326 /*
  327  * Delete an entry from the context.
  328  * NOTE: this function doesn't free oidp! You have to remove it
  329  * with sysctl_remove_oid().
  330  */
  331 int
  332 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  333 {
  334         struct sysctl_ctx_entry *e;
  335 
  336         if (clist == NULL || oidp == NULL)
  337                 return (EINVAL);
  338         SYSCTL_XLOCK();
  339         e = sysctl_ctx_entry_find(clist, oidp);
  340         if (e != NULL) {
  341                 TAILQ_REMOVE(clist, e, link);
  342                 SYSCTL_XUNLOCK();
  343                 free(e, M_SYSCTLOID);
  344                 return (0);
  345         } else {
  346                 SYSCTL_XUNLOCK();
  347                 return (ENOENT);
  348         }
  349 }
  350 
  351 /*
  352  * Remove dynamically created sysctl trees.
  353  * oidp - top of the tree to be removed
  354  * del - if 0 - just deregister, otherwise free up entries as well
  355  * recurse - if != 0 traverse the subtree to be deleted
  356  */
  357 int
  358 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
  359 {
  360         int error;
  361 
  362         SYSCTL_XLOCK();
  363         error = sysctl_remove_oid_locked(oidp, del, recurse);
  364         SYSCTL_XUNLOCK();
  365         return (error);
  366 }
  367 
  368 static int
  369 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
  370 {
  371         struct sysctl_oid *p;
  372         int error;
  373 
  374         SYSCTL_ASSERT_XLOCKED();
  375         if (oidp == NULL)
  376                 return(EINVAL);
  377         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
  378                 printf("can't remove non-dynamic nodes!\n");
  379                 return (EINVAL);
  380         }
  381         /*
  382          * WARNING: normal method to do this should be through
  383          * sysctl_ctx_free(). Use recursing as the last resort
  384          * method to purge your sysctl tree of leftovers...
  385          * However, if some other code still references these nodes,
  386          * it will panic.
  387          */
  388         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  389                 if (oidp->oid_refcnt == 1) {
  390                         SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
  391                                 if (!recurse)
  392                                         return (ENOTEMPTY);
  393                                 error = sysctl_remove_oid_locked(p, del,
  394                                     recurse);
  395                                 if (error)
  396                                         return (error);
  397                         }
  398                         if (del)
  399                                 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
  400                 }
  401         }
  402         if (oidp->oid_refcnt > 1 ) {
  403                 oidp->oid_refcnt--;
  404         } else {
  405                 if (oidp->oid_refcnt == 0) {
  406                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
  407                                 oidp->oid_refcnt, oidp->oid_name);
  408                         return (EINVAL);
  409                 }
  410                 sysctl_unregister_oid(oidp);
  411                 if (del) {
  412                         /*
  413                          * Wait for all threads running the handler to drain.
  414                          * This preserves the previous behavior when the
  415                          * sysctl lock was held across a handler invocation,
  416                          * and is necessary for module unload correctness.
  417                          */
  418                         while (oidp->oid_running > 0) {
  419                                 oidp->oid_kind |= CTLFLAG_DYING;
  420                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
  421                         }
  422                         if (oidp->oid_descr)
  423                                 free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
  424                         free((void *)(uintptr_t)(const void *)oidp->oid_name,
  425                              M_SYSCTLOID);
  426                         free(oidp, M_SYSCTLOID);
  427                 }
  428         }
  429         return (0);
  430 }
  431 
  432 /*
  433  * Create new sysctls at run time.
  434  * clist may point to a valid context initialized with sysctl_ctx_init().
  435  */
  436 struct sysctl_oid *
  437 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
  438         int number, const char *name, int kind, void *arg1, int arg2,
  439         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
  440 {
  441         struct sysctl_oid *oidp;
  442         ssize_t len;
  443         char *newname;
  444 
  445         /* You have to hook up somewhere.. */
  446         if (parent == NULL)
  447                 return(NULL);
  448         /* Check if the node already exists, otherwise create it */
  449         SYSCTL_XLOCK();
  450         oidp = sysctl_find_oidname(name, parent);
  451         if (oidp != NULL) {
  452                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  453                         oidp->oid_refcnt++;
  454                         /* Update the context */
  455                         if (clist != NULL)
  456                                 sysctl_ctx_entry_add(clist, oidp);
  457                         SYSCTL_XUNLOCK();
  458                         return (oidp);
  459                 } else {
  460                         SYSCTL_XUNLOCK();
  461                         printf("can't re-use a leaf (%s)!\n", name);
  462                         return (NULL);
  463                 }
  464         }
  465         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
  466         oidp->oid_parent = parent;
  467         SLIST_NEXT(oidp, oid_link) = NULL;
  468         oidp->oid_number = number;
  469         oidp->oid_refcnt = 1;
  470         len = strlen(name);
  471         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
  472         bcopy(name, newname, len + 1);
  473         newname[len] = '\0';
  474         oidp->oid_name = newname;
  475         oidp->oid_handler = handler;
  476         oidp->oid_kind = CTLFLAG_DYN | kind;
  477         if ((kind & CTLTYPE) == CTLTYPE_NODE) {
  478                 /* Allocate space for children */
  479                 SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
  480                     M_SYSCTLOID, M_WAITOK));
  481                 SLIST_INIT(SYSCTL_CHILDREN(oidp));
  482         } else {
  483                 oidp->oid_arg1 = arg1;
  484                 oidp->oid_arg2 = arg2;
  485         }
  486         oidp->oid_fmt = fmt;
  487         if (descr) {
  488                 int len = strlen(descr) + 1;
  489                 oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
  490                 if (oidp->oid_descr)
  491                         strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
  492         }
  493         /* Update the context, if used */
  494         if (clist != NULL)
  495                 sysctl_ctx_entry_add(clist, oidp);
  496         /* Register this oid */
  497         sysctl_register_oid(oidp);
  498         SYSCTL_XUNLOCK();
  499         return (oidp);
  500 }
  501 
  502 /*
  503  * Rename an existing oid.
  504  */
  505 void
  506 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
  507 {
  508         ssize_t len;
  509         char *newname;
  510         void *oldname;
  511 
  512         len = strlen(name);
  513         newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
  514         bcopy(name, newname, len + 1);
  515         newname[len] = '\0';
  516         SYSCTL_XLOCK();
  517         oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
  518         oidp->oid_name = newname;
  519         SYSCTL_XUNLOCK();
  520         free(oldname, M_SYSCTLOID);
  521 }
  522 
  523 /*
  524  * Reparent an existing oid.
  525  */
  526 int
  527 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
  528 {
  529         struct sysctl_oid *oidp;
  530 
  531         SYSCTL_XLOCK();
  532         if (oid->oid_parent == parent) {
  533                 SYSCTL_XUNLOCK();
  534                 return (0);
  535         }
  536         oidp = sysctl_find_oidname(oid->oid_name, parent);
  537         if (oidp != NULL) {
  538                 SYSCTL_XUNLOCK();
  539                 return (EEXIST);
  540         }
  541         sysctl_unregister_oid(oid);
  542         oid->oid_parent = parent;
  543         oid->oid_number = OID_AUTO;
  544         sysctl_register_oid(oid);
  545         SYSCTL_XUNLOCK();
  546         return (0);
  547 }
  548 
  549 /*
  550  * Register the kernel's oids on startup.
  551  */
  552 SET_DECLARE(sysctl_set, struct sysctl_oid);
  553 
  554 static void
  555 sysctl_register_all(void *arg)
  556 {
  557         struct sysctl_oid **oidp;
  558 
  559         sx_init(&sysctlmemlock, "sysctl mem");
  560         SYSCTL_INIT();
  561         SYSCTL_XLOCK();
  562         SET_FOREACH(oidp, sysctl_set)
  563                 sysctl_register_oid(*oidp);
  564         SYSCTL_XUNLOCK();
  565 }
  566 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
  567 
  568 /*
  569  * "Staff-functions"
  570  *
  571  * These functions implement a presently undocumented interface 
  572  * used by the sysctl program to walk the tree, and get the type
  573  * so it can print the value.
  574  * This interface is under work and consideration, and should probably
  575  * be killed with a big axe by the first person who can find the time.
  576  * (be aware though, that the proper interface isn't as obvious as it
  577  * may seem, there are various conflicting requirements.
  578  *
  579  * {0,0}        printf the entire MIB-tree.
  580  * {0,1,...}    return the name of the "..." OID.
  581  * {0,2,...}    return the next OID.
  582  * {0,3}        return the OID of the name in "new"
  583  * {0,4,...}    return the kind & format info for the "..." OID.
  584  * {0,5,...}    return the description the "..." OID.
  585  */
  586 
  587 #ifdef SYSCTL_DEBUG
  588 static void
  589 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
  590 {
  591         int k;
  592         struct sysctl_oid *oidp;
  593 
  594         SYSCTL_ASSERT_XLOCKED();
  595         SLIST_FOREACH(oidp, l, oid_link) {
  596 
  597                 for (k=0; k<i; k++)
  598                         printf(" ");
  599 
  600                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
  601 
  602                 printf("%c%c",
  603                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
  604                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
  605 
  606                 if (oidp->oid_handler)
  607                         printf(" *Handler");
  608 
  609                 switch (oidp->oid_kind & CTLTYPE) {
  610                         case CTLTYPE_NODE:
  611                                 printf(" Node\n");
  612                                 if (!oidp->oid_handler) {
  613                                         sysctl_sysctl_debug_dump_node(
  614                                                 oidp->oid_arg1, i+2);
  615                                 }
  616                                 break;
  617                         case CTLTYPE_INT:    printf(" Int\n"); break;
  618                         case CTLTYPE_STRING: printf(" String\n"); break;
  619                         case CTLTYPE_QUAD:   printf(" Quad\n"); break;
  620                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
  621                         default:             printf("\n");
  622                 }
  623 
  624         }
  625 }
  626 
  627 static int
  628 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
  629 {
  630         int error;
  631 
  632         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
  633         if (error)
  634                 return (error);
  635         SYSCTL_XLOCK();
  636         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
  637         SYSCTL_XUNLOCK();
  638         return (ENOENT);
  639 }
  640 
  641 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
  642         0, 0, sysctl_sysctl_debug, "-", "");
  643 #endif
  644 
  645 static int
  646 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
  647 {
  648         int *name = (int *) arg1;
  649         u_int namelen = arg2;
  650         int error = 0;
  651         struct sysctl_oid *oid;
  652         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
  653         char buf[10];
  654 
  655         SYSCTL_XLOCK();
  656         while (namelen) {
  657                 if (!lsp) {
  658                         snprintf(buf,sizeof(buf),"%d",*name);
  659                         if (req->oldidx)
  660                                 error = SYSCTL_OUT(req, ".", 1);
  661                         if (!error)
  662                                 error = SYSCTL_OUT(req, buf, strlen(buf));
  663                         if (error)
  664                                 goto out;
  665                         namelen--;
  666                         name++;
  667                         continue;
  668                 }
  669                 lsp2 = 0;
  670                 SLIST_FOREACH(oid, lsp, oid_link) {
  671                         if (oid->oid_number != *name)
  672                                 continue;
  673 
  674                         if (req->oldidx)
  675                                 error = SYSCTL_OUT(req, ".", 1);
  676                         if (!error)
  677                                 error = SYSCTL_OUT(req, oid->oid_name,
  678                                         strlen(oid->oid_name));
  679                         if (error)
  680                                 goto out;
  681 
  682                         namelen--;
  683                         name++;
  684 
  685                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
  686                                 break;
  687 
  688                         if (oid->oid_handler)
  689                                 break;
  690 
  691                         lsp2 = SYSCTL_CHILDREN(oid);
  692                         break;
  693                 }
  694                 lsp = lsp2;
  695         }
  696         error = SYSCTL_OUT(req, "", 1);
  697  out:
  698         SYSCTL_XUNLOCK();
  699         return (error);
  700 }
  701 
  702 static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
  703 
  704 static int
  705 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
  706         int *next, int *len, int level, struct sysctl_oid **oidpp)
  707 {
  708         struct sysctl_oid *oidp;
  709 
  710         SYSCTL_ASSERT_XLOCKED();
  711         *len = level;
  712         SLIST_FOREACH(oidp, lsp, oid_link) {
  713                 *next = oidp->oid_number;
  714                 *oidpp = oidp;
  715 
  716                 if (oidp->oid_kind & CTLFLAG_SKIP)
  717                         continue;
  718 
  719                 if (!namelen) {
  720                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
  721                                 return (0);
  722                         if (oidp->oid_handler) 
  723                                 /* We really should call the handler here...*/
  724                                 return (0);
  725                         lsp = SYSCTL_CHILDREN(oidp);
  726                         if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 
  727                                 len, level+1, oidpp))
  728                                 return (0);
  729                         goto emptynode;
  730                 }
  731 
  732                 if (oidp->oid_number < *name)
  733                         continue;
  734 
  735                 if (oidp->oid_number > *name) {
  736                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
  737                                 return (0);
  738                         if (oidp->oid_handler)
  739                                 return (0);
  740                         lsp = SYSCTL_CHILDREN(oidp);
  741                         if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 
  742                                 next+1, len, level+1, oidpp))
  743                                 return (0);
  744                         goto next;
  745                 }
  746                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
  747                         continue;
  748 
  749                 if (oidp->oid_handler)
  750                         continue;
  751 
  752                 lsp = SYSCTL_CHILDREN(oidp);
  753                 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 
  754                         len, level+1, oidpp))
  755                         return (0);
  756         next:
  757                 namelen = 1;
  758         emptynode:
  759                 *len = level;
  760         }
  761         return (1);
  762 }
  763 
  764 static int
  765 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
  766 {
  767         int *name = (int *) arg1;
  768         u_int namelen = arg2;
  769         int i, j, error;
  770         struct sysctl_oid *oid;
  771         struct sysctl_oid_list *lsp = &sysctl__children;
  772         int newoid[CTL_MAXNAME];
  773 
  774         SYSCTL_XLOCK();
  775         i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
  776         SYSCTL_XUNLOCK();
  777         if (i)
  778                 return (ENOENT);
  779         error = SYSCTL_OUT(req, newoid, j * sizeof (int));
  780         return (error);
  781 }
  782 
  783 static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
  784 
  785 static int
  786 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
  787 {
  788         int i;
  789         struct sysctl_oid *oidp;
  790         struct sysctl_oid_list *lsp = &sysctl__children;
  791         char *p;
  792 
  793         SYSCTL_ASSERT_XLOCKED();
  794 
  795         if (!*name)
  796                 return (ENOENT);
  797 
  798         p = name + strlen(name) - 1 ;
  799         if (*p == '.')
  800                 *p = '\0';
  801 
  802         *len = 0;
  803 
  804         for (p = name; *p && *p != '.'; p++) 
  805                 ;
  806         i = *p;
  807         if (i == '.')
  808                 *p = '\0';
  809 
  810         oidp = SLIST_FIRST(lsp);
  811 
  812         while (oidp && *len < CTL_MAXNAME) {
  813                 if (strcmp(name, oidp->oid_name)) {
  814                         oidp = SLIST_NEXT(oidp, oid_link);
  815                         continue;
  816                 }
  817                 *oid++ = oidp->oid_number;
  818                 (*len)++;
  819 
  820                 if (!i) {
  821                         if (oidpp)
  822                                 *oidpp = oidp;
  823                         return (0);
  824                 }
  825 
  826                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
  827                         break;
  828 
  829                 if (oidp->oid_handler)
  830                         break;
  831 
  832                 lsp = SYSCTL_CHILDREN(oidp);
  833                 oidp = SLIST_FIRST(lsp);
  834                 name = p+1;
  835                 for (p = name; *p && *p != '.'; p++) 
  836                                 ;
  837                 i = *p;
  838                 if (i == '.')
  839                         *p = '\0';
  840         }
  841         return (ENOENT);
  842 }
  843 
  844 static int
  845 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
  846 {
  847         char *p;
  848         int error, oid[CTL_MAXNAME], len;
  849         struct sysctl_oid *op = 0;
  850 
  851         if (!req->newlen) 
  852                 return (ENOENT);
  853         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
  854                 return (ENAMETOOLONG);
  855 
  856         p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
  857 
  858         error = SYSCTL_IN(req, p, req->newlen);
  859         if (error) {
  860                 free(p, M_SYSCTL);
  861                 return (error);
  862         }
  863 
  864         p [req->newlen] = '\0';
  865         len = 0;
  866         SYSCTL_XLOCK();
  867         error = name2oid(p, oid, &len, &op);
  868         SYSCTL_XUNLOCK();
  869 
  870         free(p, M_SYSCTL);
  871 
  872         if (error)
  873                 return (error);
  874 
  875         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
  876         return (error);
  877 }
  878 
  879 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
  880     0, 0, sysctl_sysctl_name2oid, "I", "");
  881 
  882 static int
  883 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
  884 {
  885         struct sysctl_oid *oid;
  886         int error;
  887 
  888         SYSCTL_XLOCK();
  889         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
  890         if (error)
  891                 goto out;
  892 
  893         if (oid->oid_fmt == NULL) {
  894                 error = ENOENT;
  895                 goto out;
  896         }
  897         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
  898         if (error)
  899                 goto out;
  900         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
  901  out:
  902         SYSCTL_XUNLOCK();
  903         return (error);
  904 }
  905 
  906 
  907 static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE,
  908     sysctl_sysctl_oidfmt, "");
  909 
  910 static int
  911 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
  912 {
  913         struct sysctl_oid *oid;
  914         int error;
  915 
  916         SYSCTL_XLOCK();
  917         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
  918         if (error)
  919                 goto out;
  920 
  921         if (oid->oid_descr == NULL) {
  922                 error = ENOENT;
  923                 goto out;
  924         }
  925         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
  926  out:
  927         SYSCTL_XUNLOCK();
  928         return (error);
  929 }
  930 
  931 static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
  932 
  933 /*
  934  * Default "handler" functions.
  935  */
  936 
  937 /*
  938  * Handle an int, signed or unsigned.
  939  * Two cases:
  940  *     a variable:  point arg1 at it.
  941  *     a constant:  pass it in arg2.
  942  */
  943 
  944 int
  945 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
  946 {
  947         int tmpout, error = 0;
  948 
  949         /*
  950          * Attempt to get a coherent snapshot by making a copy of the data.
  951          */
  952         if (arg1)
  953                 tmpout = *(int *)arg1;
  954         else
  955                 tmpout = arg2;
  956         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
  957 
  958         if (error || !req->newptr)
  959                 return (error);
  960 
  961         if (!arg1)
  962                 error = EPERM;
  963         else
  964                 error = SYSCTL_IN(req, arg1, sizeof(int));
  965         return (error);
  966 }
  967 
  968 /*
  969  * Based on on sysctl_handle_int() convert milliseconds into ticks.
  970  * Note: this is used by TCP.
  971  */
  972 
  973 int
  974 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
  975 {
  976         int error, s, tt;
  977 
  978         tt = *(int *)arg1;
  979         s = (int)((int64_t)tt * 1000 / hz);
  980 
  981         error = sysctl_handle_int(oidp, &s, 0, req);
  982         if (error || !req->newptr)
  983                 return (error);
  984 
  985         tt = (int)((int64_t)s * hz / 1000);
  986         if (tt < 1)
  987                 return (EINVAL);
  988 
  989         *(int *)arg1 = tt;
  990         return (0);
  991 }
  992 
  993 
  994 /*
  995  * Handle a long, signed or unsigned.  arg1 points to it.
  996  */
  997 
  998 int
  999 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
 1000 {
 1001         int error = 0;
 1002         long tmplong;
 1003 #ifdef SCTL_MASK32
 1004         int tmpint;
 1005 #endif
 1006 
 1007         /*
 1008          * Attempt to get a coherent snapshot by making a copy of the data.
 1009          */
 1010         if (!arg1)
 1011                 return (EINVAL);
 1012         tmplong = *(long *)arg1;
 1013 #ifdef SCTL_MASK32
 1014         if (req->flags & SCTL_MASK32) {
 1015                 tmpint = tmplong;
 1016                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
 1017         } else
 1018 #endif
 1019                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
 1020 
 1021         if (error || !req->newptr)
 1022                 return (error);
 1023 
 1024 #ifdef SCTL_MASK32
 1025         if (req->flags & SCTL_MASK32) {
 1026                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
 1027                 *(long *)arg1 = (long)tmpint;
 1028         } else
 1029 #endif
 1030                 error = SYSCTL_IN(req, arg1, sizeof(long));
 1031         return (error);
 1032 }
 1033 
 1034 /*
 1035  * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
 1036  */
 1037 
 1038 int
 1039 sysctl_handle_quad(SYSCTL_HANDLER_ARGS)
 1040 {
 1041         int error = 0;
 1042         uint64_t tmpout;
 1043 
 1044         /*
 1045          * Attempt to get a coherent snapshot by making a copy of the data.
 1046          */
 1047         if (!arg1)
 1048                 return (EINVAL);
 1049         tmpout = *(uint64_t *)arg1;
 1050         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
 1051 
 1052         if (error || !req->newptr)
 1053                 return (error);
 1054 
 1055         error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
 1056         return (error);
 1057 }
 1058 
 1059 /*
 1060  * Handle our generic '\0' terminated 'C' string.
 1061  * Two cases:
 1062  *      a variable string:  point arg1 at it, arg2 is max length.
 1063  *      a constant string:  point arg1 at it, arg2 is zero.
 1064  */
 1065 
 1066 int
 1067 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
 1068 {
 1069         int error=0;
 1070         char *tmparg;
 1071         size_t outlen;
 1072 
 1073         /*
 1074          * Attempt to get a coherent snapshot by copying to a
 1075          * temporary kernel buffer.
 1076          */
 1077 retry:
 1078         outlen = strlen((char *)arg1)+1;
 1079         tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
 1080 
 1081         if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
 1082                 free(tmparg, M_SYSCTLTMP);
 1083                 goto retry;
 1084         }
 1085 
 1086         error = SYSCTL_OUT(req, tmparg, outlen);
 1087         free(tmparg, M_SYSCTLTMP);
 1088 
 1089         if (error || !req->newptr)
 1090                 return (error);
 1091 
 1092         if ((req->newlen - req->newidx) >= arg2) {
 1093                 error = EINVAL;
 1094         } else {
 1095                 arg2 = (req->newlen - req->newidx);
 1096                 error = SYSCTL_IN(req, arg1, arg2);
 1097                 ((char *)arg1)[arg2] = '\0';
 1098         }
 1099 
 1100         return (error);
 1101 }
 1102 
 1103 /*
 1104  * Handle any kind of opaque data.
 1105  * arg1 points to it, arg2 is the size.
 1106  */
 1107 
 1108 int
 1109 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
 1110 {
 1111         int error, tries;
 1112         u_int generation;
 1113         struct sysctl_req req2;
 1114 
 1115         /*
 1116          * Attempt to get a coherent snapshot, by using the thread
 1117          * pre-emption counter updated from within mi_switch() to
 1118          * determine if we were pre-empted during a bcopy() or
 1119          * copyout(). Make 3 attempts at doing this before giving up.
 1120          * If we encounter an error, stop immediately.
 1121          */
 1122         tries = 0;
 1123         req2 = *req;
 1124 retry:
 1125         generation = curthread->td_generation;
 1126         error = SYSCTL_OUT(req, arg1, arg2);
 1127         if (error)
 1128                 return (error);
 1129         tries++;
 1130         if (generation != curthread->td_generation && tries < 3) {
 1131                 *req = req2;
 1132                 goto retry;
 1133         }
 1134 
 1135         error = SYSCTL_IN(req, arg1, arg2);
 1136 
 1137         return (error);
 1138 }
 1139 
 1140 /*
 1141  * Transfer functions to/from kernel space.
 1142  * XXX: rather untested at this point
 1143  */
 1144 static int
 1145 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
 1146 {
 1147         size_t i = 0;
 1148 
 1149         if (req->oldptr) {
 1150                 i = l;
 1151                 if (req->oldlen <= req->oldidx)
 1152                         i = 0;
 1153                 else
 1154                         if (i > req->oldlen - req->oldidx)
 1155                                 i = req->oldlen - req->oldidx;
 1156                 if (i > 0)
 1157                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
 1158         }
 1159         req->oldidx += l;
 1160         if (req->oldptr && i != l)
 1161                 return (ENOMEM);
 1162         return (0);
 1163 }
 1164 
 1165 static int
 1166 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
 1167 {
 1168         if (!req->newptr)
 1169                 return (0);
 1170         if (req->newlen - req->newidx < l)
 1171                 return (EINVAL);
 1172         bcopy((char *)req->newptr + req->newidx, p, l);
 1173         req->newidx += l;
 1174         return (0);
 1175 }
 1176 
 1177 int
 1178 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
 1179     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
 1180 {
 1181         int error = 0;
 1182         struct sysctl_req req;
 1183 
 1184         bzero(&req, sizeof req);
 1185 
 1186         req.td = td;
 1187         req.flags = flags;
 1188 
 1189         if (oldlenp) {
 1190                 req.oldlen = *oldlenp;
 1191         }
 1192         req.validlen = req.oldlen;
 1193 
 1194         if (old) {
 1195                 req.oldptr= old;
 1196         }
 1197 
 1198         if (new != NULL) {
 1199                 req.newlen = newlen;
 1200                 req.newptr = new;
 1201         }
 1202 
 1203         req.oldfunc = sysctl_old_kernel;
 1204         req.newfunc = sysctl_new_kernel;
 1205         req.lock = REQ_LOCKED;
 1206 
 1207         SYSCTL_XLOCK();
 1208         error = sysctl_root(0, name, namelen, &req);
 1209         SYSCTL_XUNLOCK();
 1210 
 1211         if (req.lock == REQ_WIRED && req.validlen > 0)
 1212                 vsunlock(req.oldptr, req.validlen);
 1213 
 1214         if (error && error != ENOMEM)
 1215                 return (error);
 1216 
 1217         if (retval) {
 1218                 if (req.oldptr && req.oldidx > req.validlen)
 1219                         *retval = req.validlen;
 1220                 else
 1221                         *retval = req.oldidx;
 1222         }
 1223         return (error);
 1224 }
 1225 
 1226 int
 1227 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
 1228     void *new, size_t newlen, size_t *retval, int flags)
 1229 {
 1230         int oid[CTL_MAXNAME];
 1231         size_t oidlen, plen;
 1232         int error;
 1233 
 1234         oid[0] = 0;             /* sysctl internal magic */
 1235         oid[1] = 3;             /* name2oid */
 1236         oidlen = sizeof(oid);
 1237 
 1238         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
 1239             (void *)name, strlen(name), &plen, flags);
 1240         if (error)
 1241                 return (error);
 1242 
 1243         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
 1244             new, newlen, retval, flags);
 1245         return (error);
 1246 }
 1247 
 1248 /*
 1249  * Transfer function to/from user space.
 1250  */
 1251 static int
 1252 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
 1253 {
 1254         int error = 0;
 1255         size_t i, len, origidx;
 1256 
 1257         origidx = req->oldidx;
 1258         req->oldidx += l;
 1259         if (req->oldptr == NULL)
 1260                 return (0);
 1261         /*
 1262          * If we have not wired the user supplied buffer and we are currently
 1263          * holding locks, drop a witness warning, as it's possible that
 1264          * write operations to the user page can sleep.
 1265          */
 1266         if (req->lock != REQ_WIRED)
 1267                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
 1268                     "sysctl_old_user()");
 1269         i = l;
 1270         len = req->validlen;
 1271         if (len <= origidx)
 1272                 i = 0;
 1273         else {
 1274                 if (i > len - origidx)
 1275                         i = len - origidx;
 1276                 error = copyout(p, (char *)req->oldptr + origidx, i);
 1277         }
 1278         if (error)
 1279                 return (error);
 1280         if (i < l)
 1281                 return (ENOMEM);
 1282         return (0);
 1283 }
 1284 
 1285 static int
 1286 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
 1287 {
 1288         int error;
 1289 
 1290         if (!req->newptr)
 1291                 return (0);
 1292         if (req->newlen - req->newidx < l)
 1293                 return (EINVAL);
 1294         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
 1295             "sysctl_new_user()");
 1296         error = copyin((char *)req->newptr + req->newidx, p, l);
 1297         req->newidx += l;
 1298         return (error);
 1299 }
 1300 
 1301 /*
 1302  * Wire the user space destination buffer.  If set to a value greater than
 1303  * zero, the len parameter limits the maximum amount of wired memory.
 1304  */
 1305 int
 1306 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
 1307 {
 1308         int ret;
 1309         size_t wiredlen;
 1310 
 1311         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
 1312         ret = 0;
 1313         if (req->lock == REQ_LOCKED && req->oldptr &&
 1314             req->oldfunc == sysctl_old_user) {
 1315                 if (wiredlen != 0) {
 1316                         ret = vslock(req->oldptr, wiredlen);
 1317                         if (ret != 0) {
 1318                                 if (ret != ENOMEM)
 1319                                         return (ret);
 1320                                 wiredlen = 0;
 1321                         }
 1322                 }
 1323                 req->lock = REQ_WIRED;
 1324                 req->validlen = wiredlen;
 1325         }
 1326         return (0);
 1327 }
 1328 
 1329 int
 1330 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
 1331     int *nindx, struct sysctl_req *req)
 1332 {
 1333         struct sysctl_oid_list *lsp;
 1334         struct sysctl_oid *oid;
 1335         int indx;
 1336 
 1337         SYSCTL_ASSERT_XLOCKED();
 1338         lsp = &sysctl__children;
 1339         indx = 0;
 1340         while (indx < CTL_MAXNAME) {
 1341                 SLIST_FOREACH(oid, lsp, oid_link) {
 1342                         if (oid->oid_number == name[indx])
 1343                                 break;
 1344                 }
 1345                 if (oid == NULL)
 1346                         return (ENOENT);
 1347 
 1348                 indx++;
 1349                 if (oid->oid_kind & CTLFLAG_NOLOCK)
 1350                         req->lock = REQ_UNLOCKED;
 1351                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 1352                         if (oid->oid_handler != NULL || indx == namelen) {
 1353                                 *noid = oid;
 1354                                 if (nindx != NULL)
 1355                                         *nindx = indx;
 1356                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
 1357                                     ("%s found DYING node %p", __func__, oid));
 1358                                 return (0);
 1359                         }
 1360                         lsp = SYSCTL_CHILDREN(oid);
 1361                 } else if (indx == namelen) {
 1362                         *noid = oid;
 1363                         if (nindx != NULL)
 1364                                 *nindx = indx;
 1365                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
 1366                             ("%s found DYING node %p", __func__, oid));
 1367                         return (0);
 1368                 } else {
 1369                         return (ENOTDIR);
 1370                 }
 1371         }
 1372         return (ENOENT);
 1373 }
 1374 
 1375 /*
 1376  * Traverse our tree, and find the right node, execute whatever it points
 1377  * to, and return the resulting error code.
 1378  */
 1379 
 1380 static int
 1381 sysctl_root(SYSCTL_HANDLER_ARGS)
 1382 {
 1383         struct sysctl_oid *oid;
 1384         int error, indx, lvl;
 1385 
 1386         SYSCTL_ASSERT_XLOCKED();
 1387 
 1388         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
 1389         if (error)
 1390                 return (error);
 1391 
 1392         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 1393                 /*
 1394                  * You can't call a sysctl when it's a node, but has
 1395                  * no handler.  Inform the user that it's a node.
 1396                  * The indx may or may not be the same as namelen.
 1397                  */
 1398                 if (oid->oid_handler == NULL)
 1399                         return (EISDIR);
 1400         }
 1401 
 1402         /* Is this sysctl writable? */
 1403         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
 1404                 return (EPERM);
 1405 
 1406         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
 1407 
 1408         /* Is this sysctl sensitive to securelevels? */
 1409         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
 1410                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
 1411                 error = securelevel_gt(req->td->td_ucred, lvl);
 1412                 if (error)
 1413                         return (error);
 1414         }
 1415 
 1416         /* Is this sysctl writable by only privileged users? */
 1417         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
 1418                 int priv;
 1419 
 1420                 if (oid->oid_kind & CTLFLAG_PRISON)
 1421                         priv = PRIV_SYSCTL_WRITEJAIL;
 1422 #ifdef VIMAGE
 1423                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
 1424                      prison_owns_vnet(req->td->td_ucred))
 1425                         priv = PRIV_SYSCTL_WRITEJAIL;
 1426 #endif
 1427                 else
 1428                         priv = PRIV_SYSCTL_WRITE;
 1429                 error = priv_check(req->td, priv);
 1430                 if (error)
 1431                         return (error);
 1432         }
 1433 
 1434         if (!oid->oid_handler)
 1435                 return (EINVAL);
 1436 
 1437         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 1438                 arg1 = (int *)arg1 + indx;
 1439                 arg2 -= indx;
 1440         } else {
 1441                 arg1 = oid->oid_arg1;
 1442                 arg2 = oid->oid_arg2;
 1443         }
 1444 #ifdef MAC
 1445         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
 1446             req);
 1447         if (error != 0)
 1448                 return (error);
 1449 #endif
 1450         oid->oid_running++;
 1451         SYSCTL_XUNLOCK();
 1452 
 1453         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
 1454                 mtx_lock(&Giant);
 1455         error = oid->oid_handler(oid, arg1, arg2, req);
 1456         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
 1457                 mtx_unlock(&Giant);
 1458 
 1459         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
 1460 
 1461         SYSCTL_XLOCK();
 1462         oid->oid_running--;
 1463         if (oid->oid_running == 0 && (oid->oid_kind & CTLFLAG_DYING) != 0)
 1464                 wakeup(&oid->oid_running);
 1465         return (error);
 1466 }
 1467 
 1468 #ifndef _SYS_SYSPROTO_H_
 1469 struct sysctl_args {
 1470         int     *name;
 1471         u_int   namelen;
 1472         void    *old;
 1473         size_t  *oldlenp;
 1474         void    *new;
 1475         size_t  newlen;
 1476 };
 1477 #endif
 1478 int
 1479 __sysctl(struct thread *td, struct sysctl_args *uap)
 1480 {
 1481         int error, i, name[CTL_MAXNAME];
 1482         size_t j;
 1483 
 1484         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
 1485                 return (EINVAL);
 1486 
 1487         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
 1488         if (error)
 1489                 return (error);
 1490 
 1491         error = userland_sysctl(td, name, uap->namelen,
 1492                 uap->old, uap->oldlenp, 0,
 1493                 uap->new, uap->newlen, &j, 0);
 1494         if (error && error != ENOMEM)
 1495                 return (error);
 1496         if (uap->oldlenp) {
 1497                 i = copyout(&j, uap->oldlenp, sizeof(j));
 1498                 if (i)
 1499                         return (i);
 1500         }
 1501         return (error);
 1502 }
 1503 
 1504 /*
 1505  * This is used from various compatibility syscalls too.  That's why name
 1506  * must be in kernel space.
 1507  */
 1508 int
 1509 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
 1510     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
 1511     int flags)
 1512 {
 1513         int error = 0, memlocked;
 1514         struct sysctl_req req;
 1515 
 1516         bzero(&req, sizeof req);
 1517 
 1518         req.td = td;
 1519         req.flags = flags;
 1520 
 1521         if (oldlenp) {
 1522                 if (inkernel) {
 1523                         req.oldlen = *oldlenp;
 1524                 } else {
 1525                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
 1526                         if (error)
 1527                                 return (error);
 1528                 }
 1529         }
 1530         req.validlen = req.oldlen;
 1531 
 1532         if (old) {
 1533                 if (!useracc(old, req.oldlen, VM_PROT_WRITE))
 1534                         return (EFAULT);
 1535                 req.oldptr= old;
 1536         }
 1537 
 1538         if (new != NULL) {
 1539                 if (!useracc(new, newlen, VM_PROT_READ))
 1540                         return (EFAULT);
 1541                 req.newlen = newlen;
 1542                 req.newptr = new;
 1543         }
 1544 
 1545         req.oldfunc = sysctl_old_user;
 1546         req.newfunc = sysctl_new_user;
 1547         req.lock = REQ_LOCKED;
 1548 
 1549 #ifdef KTRACE
 1550         if (KTRPOINT(curthread, KTR_SYSCTL))
 1551                 ktrsysctl(name, namelen);
 1552 #endif
 1553 
 1554         if (req.oldlen > PAGE_SIZE) {
 1555                 memlocked = 1;
 1556                 sx_xlock(&sysctlmemlock);
 1557         } else
 1558                 memlocked = 0;
 1559         CURVNET_SET(TD_TO_VNET(td));
 1560 
 1561         for (;;) {
 1562                 req.oldidx = 0;
 1563                 req.newidx = 0;
 1564                 SYSCTL_XLOCK();
 1565                 error = sysctl_root(0, name, namelen, &req);
 1566                 SYSCTL_XUNLOCK();
 1567                 if (error != EAGAIN)
 1568                         break;
 1569                 uio_yield();
 1570         }
 1571 
 1572         CURVNET_RESTORE();
 1573 
 1574         if (req.lock == REQ_WIRED && req.validlen > 0)
 1575                 vsunlock(req.oldptr, req.validlen);
 1576         if (memlocked)
 1577                 sx_xunlock(&sysctlmemlock);
 1578 
 1579         if (error && error != ENOMEM)
 1580                 return (error);
 1581 
 1582         if (retval) {
 1583                 if (req.oldptr && req.oldidx > req.validlen)
 1584                         *retval = req.validlen;
 1585                 else
 1586                         *retval = req.oldidx;
 1587         }
 1588         return (error);
 1589 }
 1590 
 1591 /*
 1592  * Drain into a sysctl struct.  The user buffer should be wired if a page
 1593  * fault would cause issue.
 1594  */
 1595 static int
 1596 sbuf_sysctl_drain(void *arg, const char *data, int len)
 1597 {
 1598         struct sysctl_req *req = arg;
 1599         int error;
 1600 
 1601         error = SYSCTL_OUT(req, data, len);
 1602         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
 1603         return (error == 0 ? len : -error);
 1604 }
 1605 
 1606 struct sbuf *
 1607 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
 1608     struct sysctl_req *req)
 1609 {
 1610 
 1611         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN);
 1612         sbuf_set_drain(s, sbuf_sysctl_drain, req);
 1613         return (s);
 1614 }

Cache object: 6067d5fca69a65f207881ae1b2119816


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.