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  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1982, 1986, 1989, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from software contributed to Berkeley by
    8  * Mike Karels at Berkeley Software Design, Inc.
    9  *
   10  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
   11  * project, to make these variables more userfriendly.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  *
   37  *      @(#)kern_sysctl.c       8.4 (Berkeley) 4/14/94
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD$");
   42 
   43 #include "opt_capsicum.h"
   44 #include "opt_ktrace.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/fail.h>
   48 #include <sys/systm.h>
   49 #include <sys/capsicum.h>
   50 #include <sys/kernel.h>
   51 #include <sys/sysctl.h>
   52 #include <sys/malloc.h>
   53 #include <sys/priv.h>
   54 #include <sys/proc.h>
   55 #include <sys/jail.h>
   56 #include <sys/lock.h>
   57 #include <sys/mutex.h>
   58 #include <sys/rmlock.h>
   59 #include <sys/sbuf.h>
   60 #include <sys/sx.h>
   61 #include <sys/sysproto.h>
   62 #include <sys/uio.h>
   63 #ifdef KTRACE
   64 #include <sys/ktrace.h>
   65 #endif
   66 
   67 #include <net/vnet.h>
   68 
   69 #include <security/mac/mac_framework.h>
   70 
   71 #include <vm/vm.h>
   72 #include <vm/vm_extern.h>
   73 
   74 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
   75 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
   76 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
   77 
   78 /*
   79  * The sysctllock protects the MIB tree.  It also protects sysctl
   80  * contexts used with dynamic sysctls.  The sysctl_register_oid() and
   81  * sysctl_unregister_oid() routines require the sysctllock to already
   82  * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
   83  * provided for the few places in the kernel which need to use that
   84  * API rather than using the dynamic API.  Use of the dynamic API is
   85  * strongly encouraged for most code.
   86  *
   87  * The sysctlmemlock is used to limit the amount of user memory wired for
   88  * sysctl requests.  This is implemented by serializing any userland
   89  * sysctl requests larger than a single page via an exclusive lock.
   90  */
   91 static struct rmlock sysctllock;
   92 static struct sx __exclusive_cache_line sysctlmemlock;
   93 
   94 #define SYSCTL_WLOCK()          rm_wlock(&sysctllock)
   95 #define SYSCTL_WUNLOCK()        rm_wunlock(&sysctllock)
   96 #define SYSCTL_RLOCK(tracker)   rm_rlock(&sysctllock, (tracker))
   97 #define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
   98 #define SYSCTL_WLOCKED()        rm_wowned(&sysctllock)
   99 #define SYSCTL_ASSERT_LOCKED()  rm_assert(&sysctllock, RA_LOCKED)
  100 #define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
  101 #define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
  102 #define SYSCTL_INIT()           rm_init_flags(&sysctllock, "sysctl lock", \
  103                                     RM_SLEEPABLE)
  104 #define SYSCTL_SLEEP(ch, wmesg, timo)                                   \
  105                                 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
  106 
  107 static int sysctl_root(SYSCTL_HANDLER_ARGS);
  108 
  109 /* Root list */
  110 struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
  111 
  112 static char*    sysctl_escape_name(const char*);
  113 static int      sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
  114                     int recurse);
  115 static int      sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
  116 static int      sysctl_new_kernel(struct sysctl_req *, void *, size_t);
  117 
  118 static struct sysctl_oid *
  119 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
  120 {
  121         struct sysctl_oid *oidp;
  122 
  123         SYSCTL_ASSERT_LOCKED();
  124         SLIST_FOREACH(oidp, list, oid_link) {
  125                 if (strcmp(oidp->oid_name, name) == 0) {
  126                         return (oidp);
  127                 }
  128         }
  129         return (NULL);
  130 }
  131 
  132 /*
  133  * Initialization of the MIB tree.
  134  *
  135  * Order by number in each list.
  136  */
  137 void
  138 sysctl_wlock(void)
  139 {
  140 
  141         SYSCTL_WLOCK();
  142 }
  143 
  144 void
  145 sysctl_wunlock(void)
  146 {
  147 
  148         SYSCTL_WUNLOCK();
  149 }
  150 
  151 static int
  152 sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intmax_t arg2,
  153     struct sysctl_req *req, struct rm_priotracker *tracker)
  154 {
  155         int error;
  156 
  157         if (oid->oid_kind & CTLFLAG_DYN)
  158                 atomic_add_int(&oid->oid_running, 1);
  159 
  160         if (tracker != NULL)
  161                 SYSCTL_RUNLOCK(tracker);
  162         else
  163                 SYSCTL_WUNLOCK();
  164 
  165         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
  166                 mtx_lock(&Giant);
  167         error = oid->oid_handler(oid, arg1, arg2, req);
  168         if (!(oid->oid_kind & CTLFLAG_MPSAFE))
  169                 mtx_unlock(&Giant);
  170 
  171         KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
  172 
  173         if (tracker != NULL)
  174                 SYSCTL_RLOCK(tracker);
  175         else
  176                 SYSCTL_WLOCK();
  177 
  178         if (oid->oid_kind & CTLFLAG_DYN) {
  179                 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
  180                     (oid->oid_kind & CTLFLAG_DYING) != 0)
  181                         wakeup(&oid->oid_running);
  182         }
  183 
  184         return (error);
  185 }
  186 
  187 static void
  188 sysctl_load_tunable_by_oid_locked(struct sysctl_oid *oidp)
  189 {
  190         struct sysctl_req req;
  191         struct sysctl_oid *curr;
  192         char *penv = NULL;
  193         char path[96];
  194         ssize_t rem = sizeof(path);
  195         ssize_t len;
  196         uint8_t data[512] __aligned(sizeof(uint64_t));
  197         int size;
  198         int error;
  199 
  200         path[--rem] = 0;
  201 
  202         for (curr = oidp; curr != NULL; curr = SYSCTL_PARENT(curr)) {
  203                 len = strlen(curr->oid_name);
  204                 rem -= len;
  205                 if (curr != oidp)
  206                         rem -= 1;
  207                 if (rem < 0) {
  208                         printf("OID path exceeds %d bytes\n", (int)sizeof(path));
  209                         return;
  210                 }
  211                 memcpy(path + rem, curr->oid_name, len);
  212                 if (curr != oidp)
  213                         path[rem + len] = '.';
  214         }
  215 
  216         memset(&req, 0, sizeof(req));
  217 
  218         req.td = curthread;
  219         req.oldfunc = sysctl_old_kernel;
  220         req.newfunc = sysctl_new_kernel;
  221         req.lock = REQ_UNWIRED;
  222 
  223         switch (oidp->oid_kind & CTLTYPE) {
  224         case CTLTYPE_INT:
  225                 if (getenv_array(path + rem, data, sizeof(data), &size,
  226                     sizeof(int), GETENV_SIGNED) == 0)
  227                         return;
  228                 req.newlen = size;
  229                 req.newptr = data;
  230                 break;
  231         case CTLTYPE_UINT:
  232                 if (getenv_array(path + rem, data, sizeof(data), &size,
  233                     sizeof(int), GETENV_UNSIGNED) == 0)
  234                         return;
  235                 req.newlen = size;
  236                 req.newptr = data;
  237                 break;
  238         case CTLTYPE_LONG:
  239                 if (getenv_array(path + rem, data, sizeof(data), &size,
  240                     sizeof(long), GETENV_SIGNED) == 0)
  241                         return;
  242                 req.newlen = size;
  243                 req.newptr = data;
  244                 break;
  245         case CTLTYPE_ULONG:
  246                 if (getenv_array(path + rem, data, sizeof(data), &size,
  247                     sizeof(long), GETENV_UNSIGNED) == 0)
  248                         return;
  249                 req.newlen = size;
  250                 req.newptr = data;
  251                 break;
  252         case CTLTYPE_S8:
  253                 if (getenv_array(path + rem, data, sizeof(data), &size,
  254                     sizeof(int8_t), GETENV_SIGNED) == 0)
  255                         return;
  256                 req.newlen = size;
  257                 req.newptr = data;
  258                 break;
  259         case CTLTYPE_S16:
  260                 if (getenv_array(path + rem, data, sizeof(data), &size,
  261                     sizeof(int16_t), GETENV_SIGNED) == 0)
  262                         return;
  263                 req.newlen = size;
  264                 req.newptr = data;
  265                 break;
  266         case CTLTYPE_S32:
  267                 if (getenv_array(path + rem, data, sizeof(data), &size,
  268                     sizeof(int32_t), GETENV_SIGNED) == 0)
  269                         return;
  270                 req.newlen = size;
  271                 req.newptr = data;
  272                 break;
  273         case CTLTYPE_S64:
  274                 if (getenv_array(path + rem, data, sizeof(data), &size,
  275                     sizeof(int64_t), GETENV_SIGNED) == 0)
  276                         return;
  277                 req.newlen = size;
  278                 req.newptr = data;
  279                 break;
  280         case CTLTYPE_U8:
  281                 if (getenv_array(path + rem, data, sizeof(data), &size,
  282                     sizeof(uint8_t), GETENV_UNSIGNED) == 0)
  283                         return;
  284                 req.newlen = size;
  285                 req.newptr = data;
  286                 break;
  287         case CTLTYPE_U16:
  288                 if (getenv_array(path + rem, data, sizeof(data), &size,
  289                     sizeof(uint16_t), GETENV_UNSIGNED) == 0)
  290                         return;
  291                 req.newlen = size;
  292                 req.newptr = data;
  293                 break;
  294         case CTLTYPE_U32:
  295                 if (getenv_array(path + rem, data, sizeof(data), &size,
  296                     sizeof(uint32_t), GETENV_UNSIGNED) == 0)
  297                         return;
  298                 req.newlen = size;
  299                 req.newptr = data;
  300                 break;
  301         case CTLTYPE_U64:
  302                 if (getenv_array(path + rem, data, sizeof(data), &size,
  303                     sizeof(uint64_t), GETENV_UNSIGNED) == 0)
  304                         return;
  305                 req.newlen = size;
  306                 req.newptr = data;
  307                 break;
  308         case CTLTYPE_STRING:
  309                 penv = kern_getenv(path + rem);
  310                 if (penv == NULL)
  311                         return;
  312                 req.newlen = strlen(penv);
  313                 req.newptr = penv;
  314                 break;
  315         default:
  316                 return;
  317         }
  318         error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
  319             oidp->oid_arg2, &req, NULL);
  320         if (error != 0)
  321                 printf("Setting sysctl %s failed: %d\n", path + rem, error);
  322         if (penv != NULL)
  323                 freeenv(penv);
  324 }
  325 
  326 static int
  327 sbuf_printf_drain(void *arg __unused, const char *data, int len)
  328 {
  329 
  330         return (printf("%.*s", len, data));
  331 }
  332 
  333 /*
  334  * Locate the path to a given oid.  Returns the length of the resulting path,
  335  * or -1 if the oid was not found.  nodes must have room for CTL_MAXNAME
  336  * elements and be NULL initialized.
  337  */
  338 static int
  339 sysctl_search_oid(struct sysctl_oid **nodes, struct sysctl_oid *needle)
  340 {
  341         int indx;
  342 
  343         SYSCTL_ASSERT_LOCKED();
  344         indx = 0;
  345         while (indx < CTL_MAXNAME && indx >= 0) {
  346                 if (nodes[indx] == NULL && indx == 0)
  347                         nodes[indx] = SLIST_FIRST(&sysctl__children);
  348                 else if (nodes[indx] == NULL)
  349                         nodes[indx] = SLIST_FIRST(&nodes[indx - 1]->oid_children);
  350                 else
  351                         nodes[indx] = SLIST_NEXT(nodes[indx], oid_link);
  352 
  353                 if (nodes[indx] == needle)
  354                         return (indx + 1);
  355 
  356                 if (nodes[indx] == NULL) {
  357                         indx--;
  358                         continue;
  359                 }
  360 
  361                 if ((nodes[indx]->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  362                         indx++;
  363                         continue;
  364                 }
  365         }
  366         return (-1);
  367 }
  368 
  369 static void
  370 sysctl_warn_reuse(const char *func, struct sysctl_oid *leaf)
  371 {
  372         struct sysctl_oid *nodes[CTL_MAXNAME];
  373         char buf[128];
  374         struct sbuf sb;
  375         int rc, i;
  376 
  377         (void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
  378         sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
  379 
  380         sbuf_printf(&sb, "%s: can't re-use a leaf (", __func__);
  381 
  382         memset(nodes, 0, sizeof(nodes));
  383         rc = sysctl_search_oid(nodes, leaf);
  384         if (rc > 0) {
  385                 for (i = 0; i < rc; i++)
  386                         sbuf_printf(&sb, "%s%.*s", nodes[i]->oid_name,
  387                             i != (rc - 1), ".");
  388         } else {
  389                 sbuf_printf(&sb, "%s", leaf->oid_name);
  390         }
  391         sbuf_printf(&sb, ")!\n");
  392 
  393         (void)sbuf_finish(&sb);
  394 }
  395 
  396 #ifdef SYSCTL_DEBUG
  397 static int
  398 sysctl_reuse_test(SYSCTL_HANDLER_ARGS)
  399 {
  400         struct rm_priotracker tracker;
  401 
  402         SYSCTL_RLOCK(&tracker);
  403         sysctl_warn_reuse(__func__, oidp);
  404         SYSCTL_RUNLOCK(&tracker);
  405         return (0);
  406 }
  407 SYSCTL_PROC(_sysctl, 0, reuse_test, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
  408         0, 0, sysctl_reuse_test, "-", "");
  409 #endif
  410 
  411 void
  412 sysctl_register_oid(struct sysctl_oid *oidp)
  413 {
  414         struct sysctl_oid_list *parent = oidp->oid_parent;
  415         struct sysctl_oid *p;
  416         struct sysctl_oid *q;
  417         int oid_number;
  418         int timeout = 2;
  419 
  420         /*
  421          * First check if another oid with the same name already
  422          * exists in the parent's list.
  423          */
  424         SYSCTL_ASSERT_WLOCKED();
  425         p = sysctl_find_oidname(oidp->oid_name, parent);
  426         if (p != NULL) {
  427                 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  428                         p->oid_refcnt++;
  429                         return;
  430                 } else {
  431                         sysctl_warn_reuse(__func__, p);
  432                         return;
  433                 }
  434         }
  435         /* get current OID number */
  436         oid_number = oidp->oid_number;
  437 
  438 #if (OID_AUTO >= 0)
  439 #error "OID_AUTO is expected to be a negative value"
  440 #endif  
  441         /*
  442          * Any negative OID number qualifies as OID_AUTO. Valid OID
  443          * numbers should always be positive.
  444          *
  445          * NOTE: DO NOT change the starting value here, change it in
  446          * <sys/sysctl.h>, and make sure it is at least 256 to
  447          * accommodate e.g. net.inet.raw as a static sysctl node.
  448          */
  449         if (oid_number < 0) {
  450                 static int newoid;
  451 
  452                 /*
  453                  * By decrementing the next OID number we spend less
  454                  * time inserting the OIDs into a sorted list.
  455                  */
  456                 if (--newoid < CTL_AUTO_START)
  457                         newoid = 0x7fffffff;
  458 
  459                 oid_number = newoid;
  460         }
  461 
  462         /*
  463          * Insert the OID into the parent's list sorted by OID number.
  464          */
  465 retry:
  466         q = NULL;
  467         SLIST_FOREACH(p, parent, oid_link) {
  468                 /* check if the current OID number is in use */
  469                 if (oid_number == p->oid_number) {
  470                         /* get the next valid OID number */
  471                         if (oid_number < CTL_AUTO_START ||
  472                             oid_number == 0x7fffffff) {
  473                                 /* wraparound - restart */
  474                                 oid_number = CTL_AUTO_START;
  475                                 /* don't loop forever */
  476                                 if (!timeout--)
  477                                         panic("sysctl: Out of OID numbers\n");
  478                                 goto retry;
  479                         } else {
  480                                 oid_number++;
  481                         }
  482                 } else if (oid_number < p->oid_number)
  483                         break;
  484                 q = p;
  485         }
  486         /* check for non-auto OID number collision */
  487         if (oidp->oid_number >= 0 && oidp->oid_number < CTL_AUTO_START &&
  488             oid_number >= CTL_AUTO_START) {
  489                 printf("sysctl: OID number(%d) is already in use for '%s'\n",
  490                     oidp->oid_number, oidp->oid_name);
  491         }
  492         /* update the OID number, if any */
  493         oidp->oid_number = oid_number;
  494         if (q != NULL)
  495                 SLIST_INSERT_AFTER(q, oidp, oid_link);
  496         else
  497                 SLIST_INSERT_HEAD(parent, oidp, oid_link);
  498 
  499         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE &&
  500 #ifdef VIMAGE
  501             (oidp->oid_kind & CTLFLAG_VNET) == 0 &&
  502 #endif
  503             (oidp->oid_kind & CTLFLAG_TUN) != 0 &&
  504             (oidp->oid_kind & CTLFLAG_NOFETCH) == 0) {
  505                 /* only fetch value once */
  506                 oidp->oid_kind |= CTLFLAG_NOFETCH;
  507                 /* try to fetch value from kernel environment */
  508                 sysctl_load_tunable_by_oid_locked(oidp);
  509         }
  510 }
  511 
  512 void
  513 sysctl_register_disabled_oid(struct sysctl_oid *oidp)
  514 {
  515 
  516         /*
  517          * Mark the leaf as dormant if it's not to be immediately enabled.
  518          * We do not disable nodes as they can be shared between modules
  519          * and it is always safe to access a node.
  520          */
  521         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
  522             ("internal flag is set in oid_kind"));
  523         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
  524                 oidp->oid_kind |= CTLFLAG_DORMANT;
  525         sysctl_register_oid(oidp);
  526 }
  527 
  528 void
  529 sysctl_enable_oid(struct sysctl_oid *oidp)
  530 {
  531 
  532         SYSCTL_ASSERT_WLOCKED();
  533         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  534                 KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) == 0,
  535                     ("sysctl node is marked as dormant"));
  536                 return;
  537         }
  538         KASSERT((oidp->oid_kind & CTLFLAG_DORMANT) != 0,
  539             ("enabling already enabled sysctl oid"));
  540         oidp->oid_kind &= ~CTLFLAG_DORMANT;
  541 }
  542 
  543 void
  544 sysctl_unregister_oid(struct sysctl_oid *oidp)
  545 {
  546         struct sysctl_oid *p;
  547         int error;
  548 
  549         SYSCTL_ASSERT_WLOCKED();
  550         if (oidp->oid_number == OID_AUTO) {
  551                 error = EINVAL;
  552         } else {
  553                 error = ENOENT;
  554                 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
  555                         if (p == oidp) {
  556                                 SLIST_REMOVE(oidp->oid_parent, oidp,
  557                                     sysctl_oid, oid_link);
  558                                 error = 0;
  559                                 break;
  560                         }
  561                 }
  562         }
  563 
  564         /* 
  565          * This can happen when a module fails to register and is
  566          * being unloaded afterwards.  It should not be a panic()
  567          * for normal use.
  568          */
  569         if (error) {
  570                 printf("%s: failed(%d) to unregister sysctl(%s)\n",
  571                     __func__, error, oidp->oid_name);
  572         }
  573 }
  574 
  575 /* Initialize a new context to keep track of dynamically added sysctls. */
  576 int
  577 sysctl_ctx_init(struct sysctl_ctx_list *c)
  578 {
  579 
  580         if (c == NULL) {
  581                 return (EINVAL);
  582         }
  583 
  584         /*
  585          * No locking here, the caller is responsible for not adding
  586          * new nodes to a context until after this function has
  587          * returned.
  588          */
  589         TAILQ_INIT(c);
  590         return (0);
  591 }
  592 
  593 /* Free the context, and destroy all dynamic oids registered in this context */
  594 int
  595 sysctl_ctx_free(struct sysctl_ctx_list *clist)
  596 {
  597         struct sysctl_ctx_entry *e, *e1;
  598         int error;
  599 
  600         error = 0;
  601         /*
  602          * First perform a "dry run" to check if it's ok to remove oids.
  603          * XXX FIXME
  604          * XXX This algorithm is a hack. But I don't know any
  605          * XXX better solution for now...
  606          */
  607         SYSCTL_WLOCK();
  608         TAILQ_FOREACH(e, clist, link) {
  609                 error = sysctl_remove_oid_locked(e->entry, 0, 0);
  610                 if (error)
  611                         break;
  612         }
  613         /*
  614          * Restore deregistered entries, either from the end,
  615          * or from the place where error occurred.
  616          * e contains the entry that was not unregistered
  617          */
  618         if (error)
  619                 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
  620         else
  621                 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
  622         while (e1 != NULL) {
  623                 sysctl_register_oid(e1->entry);
  624                 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
  625         }
  626         if (error) {
  627                 SYSCTL_WUNLOCK();
  628                 return(EBUSY);
  629         }
  630         /* Now really delete the entries */
  631         e = TAILQ_FIRST(clist);
  632         while (e != NULL) {
  633                 e1 = TAILQ_NEXT(e, link);
  634                 error = sysctl_remove_oid_locked(e->entry, 1, 0);
  635                 if (error)
  636                         panic("sysctl_remove_oid: corrupt tree, entry: %s",
  637                             e->entry->oid_name);
  638                 free(e, M_SYSCTLOID);
  639                 e = e1;
  640         }
  641         SYSCTL_WUNLOCK();
  642         return (error);
  643 }
  644 
  645 /* Add an entry to the context */
  646 struct sysctl_ctx_entry *
  647 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  648 {
  649         struct sysctl_ctx_entry *e;
  650 
  651         SYSCTL_ASSERT_WLOCKED();
  652         if (clist == NULL || oidp == NULL)
  653                 return(NULL);
  654         e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
  655         e->entry = oidp;
  656         TAILQ_INSERT_HEAD(clist, e, link);
  657         return (e);
  658 }
  659 
  660 /* Find an entry in the context */
  661 struct sysctl_ctx_entry *
  662 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  663 {
  664         struct sysctl_ctx_entry *e;
  665 
  666         SYSCTL_ASSERT_WLOCKED();
  667         if (clist == NULL || oidp == NULL)
  668                 return(NULL);
  669         TAILQ_FOREACH(e, clist, link) {
  670                 if(e->entry == oidp)
  671                         return(e);
  672         }
  673         return (e);
  674 }
  675 
  676 /*
  677  * Delete an entry from the context.
  678  * NOTE: this function doesn't free oidp! You have to remove it
  679  * with sysctl_remove_oid().
  680  */
  681 int
  682 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
  683 {
  684         struct sysctl_ctx_entry *e;
  685 
  686         if (clist == NULL || oidp == NULL)
  687                 return (EINVAL);
  688         SYSCTL_WLOCK();
  689         e = sysctl_ctx_entry_find(clist, oidp);
  690         if (e != NULL) {
  691                 TAILQ_REMOVE(clist, e, link);
  692                 SYSCTL_WUNLOCK();
  693                 free(e, M_SYSCTLOID);
  694                 return (0);
  695         } else {
  696                 SYSCTL_WUNLOCK();
  697                 return (ENOENT);
  698         }
  699 }
  700 
  701 /*
  702  * Remove dynamically created sysctl trees.
  703  * oidp - top of the tree to be removed
  704  * del - if 0 - just deregister, otherwise free up entries as well
  705  * recurse - if != 0 traverse the subtree to be deleted
  706  */
  707 int
  708 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
  709 {
  710         int error;
  711 
  712         SYSCTL_WLOCK();
  713         error = sysctl_remove_oid_locked(oidp, del, recurse);
  714         SYSCTL_WUNLOCK();
  715         return (error);
  716 }
  717 
  718 int
  719 sysctl_remove_name(struct sysctl_oid *parent, const char *name,
  720     int del, int recurse)
  721 {
  722         struct sysctl_oid *p, *tmp;
  723         int error;
  724 
  725         error = ENOENT;
  726         SYSCTL_WLOCK();
  727         SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
  728                 if (strcmp(p->oid_name, name) == 0) {
  729                         error = sysctl_remove_oid_locked(p, del, recurse);
  730                         break;
  731                 }
  732         }
  733         SYSCTL_WUNLOCK();
  734 
  735         return (error);
  736 }
  737 
  738 /*
  739  * Duplicate the provided string, escaping any illegal characters.  The result
  740  * must be freed when no longer in use.
  741  *
  742  * The list of illegal characters is ".".
  743  */
  744 static char*
  745 sysctl_escape_name(const char* orig)
  746 {
  747         int i, s = 0, d = 0, nillegals = 0;
  748         char *new;
  749 
  750         /* First count the number of illegal characters */
  751         for (i = 0; orig[i] != '\0'; i++) {
  752                 if (orig[i] == '.')
  753                         nillegals++;
  754         }
  755 
  756         /* Allocate storage for new string */
  757         new = malloc(i + 2 * nillegals + 1, M_SYSCTLOID, M_WAITOK);
  758 
  759         /* Copy the name, escaping characters as we go */
  760         while (orig[s] != '\0') {
  761                 if (orig[s] == '.') {
  762                         /* %25 is the hexadecimal representation of '.' */
  763                         new[d++] = '%';
  764                         new[d++] = '2';
  765                         new[d++] = '5';
  766                         s++;
  767                 } else {
  768                         new[d++] = orig[s++];
  769                 }
  770         }
  771 
  772         /* Finally, nul-terminate */
  773         new[d] = '\0';
  774 
  775         return (new);
  776 }
  777 
  778 static int
  779 sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
  780 {
  781         struct sysctl_oid *p, *tmp;
  782         int error;
  783 
  784         SYSCTL_ASSERT_WLOCKED();
  785         if (oidp == NULL)
  786                 return(EINVAL);
  787         if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
  788                 printf("Warning: can't remove non-dynamic nodes (%s)!\n",
  789                     oidp->oid_name);
  790                 return (EINVAL);
  791         }
  792         /*
  793          * WARNING: normal method to do this should be through
  794          * sysctl_ctx_free(). Use recursing as the last resort
  795          * method to purge your sysctl tree of leftovers...
  796          * However, if some other code still references these nodes,
  797          * it will panic.
  798          */
  799         if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  800                 if (oidp->oid_refcnt == 1) {
  801                         SLIST_FOREACH_SAFE(p,
  802                             SYSCTL_CHILDREN(oidp), oid_link, tmp) {
  803                                 if (!recurse) {
  804                                         printf("Warning: failed attempt to "
  805                                             "remove oid %s with child %s\n",
  806                                             oidp->oid_name, p->oid_name);
  807                                         return (ENOTEMPTY);
  808                                 }
  809                                 error = sysctl_remove_oid_locked(p, del,
  810                                     recurse);
  811                                 if (error)
  812                                         return (error);
  813                         }
  814                 }
  815         }
  816         if (oidp->oid_refcnt > 1 ) {
  817                 oidp->oid_refcnt--;
  818         } else {
  819                 if (oidp->oid_refcnt == 0) {
  820                         printf("Warning: bad oid_refcnt=%u (%s)!\n",
  821                                 oidp->oid_refcnt, oidp->oid_name);
  822                         return (EINVAL);
  823                 }
  824                 sysctl_unregister_oid(oidp);
  825                 if (del) {
  826                         /*
  827                          * Wait for all threads running the handler to drain.
  828                          * This preserves the previous behavior when the
  829                          * sysctl lock was held across a handler invocation,
  830                          * and is necessary for module unload correctness.
  831                          */
  832                         while (oidp->oid_running > 0) {
  833                                 oidp->oid_kind |= CTLFLAG_DYING;
  834                                 SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
  835                         }
  836                         if (oidp->oid_descr)
  837                                 free(__DECONST(char *, oidp->oid_descr),
  838                                     M_SYSCTLOID);
  839                         if (oidp->oid_label)
  840                                 free(__DECONST(char *, oidp->oid_label),
  841                                     M_SYSCTLOID);
  842                         free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
  843                         free(oidp, M_SYSCTLOID);
  844                 }
  845         }
  846         return (0);
  847 }
  848 /*
  849  * Create new sysctls at run time.
  850  * clist may point to a valid context initialized with sysctl_ctx_init().
  851  */
  852 struct sysctl_oid *
  853 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
  854         int number, const char *name, int kind, void *arg1, intmax_t arg2,
  855         int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr,
  856         const char *label)
  857 {
  858         struct sysctl_oid *oidp;
  859         char *escaped;
  860 
  861         /* You have to hook up somewhere.. */
  862         if (parent == NULL)
  863                 return(NULL);
  864         escaped = sysctl_escape_name(name);
  865         /* Check if the node already exists, otherwise create it */
  866         SYSCTL_WLOCK();
  867         oidp = sysctl_find_oidname(escaped, parent);
  868         if (oidp != NULL) {
  869                 free(escaped, M_SYSCTLOID);
  870                 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
  871                         oidp->oid_refcnt++;
  872                         /* Update the context */
  873                         if (clist != NULL)
  874                                 sysctl_ctx_entry_add(clist, oidp);
  875                         SYSCTL_WUNLOCK();
  876                         return (oidp);
  877                 } else {
  878                         sysctl_warn_reuse(__func__, oidp);
  879                         SYSCTL_WUNLOCK();
  880                         return (NULL);
  881                 }
  882         }
  883         oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
  884         oidp->oid_parent = parent;
  885         SLIST_INIT(&oidp->oid_children);
  886         oidp->oid_number = number;
  887         oidp->oid_refcnt = 1;
  888         oidp->oid_name = escaped;
  889         oidp->oid_handler = handler;
  890         oidp->oid_kind = CTLFLAG_DYN | kind;
  891         oidp->oid_arg1 = arg1;
  892         oidp->oid_arg2 = arg2;
  893         oidp->oid_fmt = fmt;
  894         if (descr != NULL)
  895                 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
  896         if (label != NULL)
  897                 oidp->oid_label = strdup(label, M_SYSCTLOID);
  898         /* Update the context, if used */
  899         if (clist != NULL)
  900                 sysctl_ctx_entry_add(clist, oidp);
  901         /* Register this oid */
  902         sysctl_register_oid(oidp);
  903         SYSCTL_WUNLOCK();
  904         return (oidp);
  905 }
  906 
  907 /*
  908  * Rename an existing oid.
  909  */
  910 void
  911 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
  912 {
  913         char *newname;
  914         char *oldname;
  915 
  916         newname = strdup(name, M_SYSCTLOID);
  917         SYSCTL_WLOCK();
  918         oldname = __DECONST(char *, oidp->oid_name);
  919         oidp->oid_name = newname;
  920         SYSCTL_WUNLOCK();
  921         free(oldname, M_SYSCTLOID);
  922 }
  923 
  924 /*
  925  * Reparent an existing oid.
  926  */
  927 int
  928 sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
  929 {
  930         struct sysctl_oid *oidp;
  931 
  932         SYSCTL_WLOCK();
  933         if (oid->oid_parent == parent) {
  934                 SYSCTL_WUNLOCK();
  935                 return (0);
  936         }
  937         oidp = sysctl_find_oidname(oid->oid_name, parent);
  938         if (oidp != NULL) {
  939                 SYSCTL_WUNLOCK();
  940                 return (EEXIST);
  941         }
  942         sysctl_unregister_oid(oid);
  943         oid->oid_parent = parent;
  944         oid->oid_number = OID_AUTO;
  945         sysctl_register_oid(oid);
  946         SYSCTL_WUNLOCK();
  947         return (0);
  948 }
  949 
  950 /*
  951  * Register the kernel's oids on startup.
  952  */
  953 SET_DECLARE(sysctl_set, struct sysctl_oid);
  954 
  955 static void
  956 sysctl_register_all(void *arg)
  957 {
  958         struct sysctl_oid **oidp;
  959 
  960         sx_init(&sysctlmemlock, "sysctl mem");
  961         SYSCTL_INIT();
  962         SYSCTL_WLOCK();
  963         SET_FOREACH(oidp, sysctl_set)
  964                 sysctl_register_oid(*oidp);
  965         SYSCTL_WUNLOCK();
  966 }
  967 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, NULL);
  968 
  969 /*
  970  * "Staff-functions"
  971  *
  972  * These functions implement a presently undocumented interface 
  973  * used by the sysctl program to walk the tree, and get the type
  974  * so it can print the value.
  975  * This interface is under work and consideration, and should probably
  976  * be killed with a big axe by the first person who can find the time.
  977  * (be aware though, that the proper interface isn't as obvious as it
  978  * may seem, there are various conflicting requirements.
  979  *
  980  * {CTL_SYSCTL, CTL_SYSCTL_DEBUG}               printf the entire MIB-tree.
  981  * {CTL_SYSCTL, CTL_SYSCTL_NAME, ...}           return the name of the "..."
  982  *                                              OID.
  983  * {CTL_SYSCTL, CTL_SYSCTL_NEXT, ...}           return the next OID, honoring
  984  *                                              CTLFLAG_SKIP.
  985  * {CTL_SYSCTL, CTL_SYSCTL_NAME2OID}            return the OID of the name in
  986  *                                              "new"
  987  * {CTL_SYSCTL, CTL_SYSCTL_OIDFMT, ...}         return the kind & format info
  988  *                                              for the "..." OID.
  989  * {CTL_SYSCTL, CTL_SYSCTL_OIDDESCR, ...}       return the description of the
  990  *                                              "..." OID.
  991  * {CTL_SYSCTL, CTL_SYSCTL_OIDLABEL, ...}       return the aggregation label of
  992  *                                              the "..." OID.
  993  * {CTL_SYSCTL, CTL_SYSCTL_NEXTNOSKIP, ...}     return the next OID, ignoring
  994  *                                              CTLFLAG_SKIP.
  995  */
  996 
  997 #ifdef SYSCTL_DEBUG
  998 static void
  999 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
 1000 {
 1001         int k;
 1002         struct sysctl_oid *oidp;
 1003 
 1004         SYSCTL_ASSERT_LOCKED();
 1005         SLIST_FOREACH(oidp, l, oid_link) {
 1006 
 1007                 for (k=0; k<i; k++)
 1008                         printf(" ");
 1009 
 1010                 printf("%d %s ", oidp->oid_number, oidp->oid_name);
 1011 
 1012                 printf("%c%c",
 1013                         oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
 1014                         oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
 1015 
 1016                 if (oidp->oid_handler)
 1017                         printf(" *Handler");
 1018 
 1019                 switch (oidp->oid_kind & CTLTYPE) {
 1020                         case CTLTYPE_NODE:
 1021                                 printf(" Node\n");
 1022                                 if (!oidp->oid_handler) {
 1023                                         sysctl_sysctl_debug_dump_node(
 1024                                             SYSCTL_CHILDREN(oidp), i + 2);
 1025                                 }
 1026                                 break;
 1027                         case CTLTYPE_INT:    printf(" Int\n"); break;
 1028                         case CTLTYPE_UINT:   printf(" u_int\n"); break;
 1029                         case CTLTYPE_LONG:   printf(" Long\n"); break;
 1030                         case CTLTYPE_ULONG:  printf(" u_long\n"); break;
 1031                         case CTLTYPE_STRING: printf(" String\n"); break;
 1032                         case CTLTYPE_S8:     printf(" int8_t\n"); break;
 1033                         case CTLTYPE_S16:    printf(" int16_t\n"); break;
 1034                         case CTLTYPE_S32:    printf(" int32_t\n"); break;
 1035                         case CTLTYPE_S64:    printf(" int64_t\n"); break;
 1036                         case CTLTYPE_U8:     printf(" uint8_t\n"); break;
 1037                         case CTLTYPE_U16:    printf(" uint16_t\n"); break;
 1038                         case CTLTYPE_U32:    printf(" uint32_t\n"); break;
 1039                         case CTLTYPE_U64:    printf(" uint64_t\n"); break;
 1040                         case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
 1041                         default:             printf("\n");
 1042                 }
 1043 
 1044         }
 1045 }
 1046 
 1047 static int
 1048 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
 1049 {
 1050         struct rm_priotracker tracker;
 1051         int error;
 1052 
 1053         error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
 1054         if (error)
 1055                 return (error);
 1056         SYSCTL_RLOCK(&tracker);
 1057         sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
 1058         SYSCTL_RUNLOCK(&tracker);
 1059         return (ENOENT);
 1060 }
 1061 
 1062 SYSCTL_PROC(_sysctl, CTL_SYSCTL_DEBUG, debug, CTLTYPE_STRING | CTLFLAG_RD |
 1063     CTLFLAG_MPSAFE, 0, 0, sysctl_sysctl_debug, "-", "");
 1064 #endif
 1065 
 1066 static int
 1067 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
 1068 {
 1069         int *name = (int *) arg1;
 1070         u_int namelen = arg2;
 1071         int error;
 1072         struct sysctl_oid *oid;
 1073         struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
 1074         struct rm_priotracker tracker;
 1075         char buf[10];
 1076 
 1077         error = sysctl_wire_old_buffer(req, 0);
 1078         if (error)
 1079                 return (error);
 1080 
 1081         SYSCTL_RLOCK(&tracker);
 1082         while (namelen) {
 1083                 if (!lsp) {
 1084                         snprintf(buf,sizeof(buf),"%d",*name);
 1085                         if (req->oldidx)
 1086                                 error = SYSCTL_OUT(req, ".", 1);
 1087                         if (!error)
 1088                                 error = SYSCTL_OUT(req, buf, strlen(buf));
 1089                         if (error)
 1090                                 goto out;
 1091                         namelen--;
 1092                         name++;
 1093                         continue;
 1094                 }
 1095                 lsp2 = NULL;
 1096                 SLIST_FOREACH(oid, lsp, oid_link) {
 1097                         if (oid->oid_number != *name)
 1098                                 continue;
 1099 
 1100                         if (req->oldidx)
 1101                                 error = SYSCTL_OUT(req, ".", 1);
 1102                         if (!error)
 1103                                 error = SYSCTL_OUT(req, oid->oid_name,
 1104                                         strlen(oid->oid_name));
 1105                         if (error)
 1106                                 goto out;
 1107 
 1108                         namelen--;
 1109                         name++;
 1110 
 1111                         if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
 1112                                 break;
 1113 
 1114                         if (oid->oid_handler)
 1115                                 break;
 1116 
 1117                         lsp2 = SYSCTL_CHILDREN(oid);
 1118                         break;
 1119                 }
 1120                 lsp = lsp2;
 1121         }
 1122         error = SYSCTL_OUT(req, "", 1);
 1123  out:
 1124         SYSCTL_RUNLOCK(&tracker);
 1125         return (error);
 1126 }
 1127 
 1128 /*
 1129  * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
 1130  * capability mode.
 1131  */
 1132 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NAME, name, CTLFLAG_RD |
 1133     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_name, "");
 1134 
 1135 /*
 1136  * Walk the sysctl subtree at lsp until we find the given name,
 1137  * and return the next name in order by oid_number.
 1138  */
 1139 static int
 1140 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 
 1141     int *next, int *len, int level, bool honor_skip)
 1142 {
 1143         struct sysctl_oid *oidp;
 1144 
 1145         SYSCTL_ASSERT_LOCKED();
 1146         *len = level;
 1147         SLIST_FOREACH(oidp, lsp, oid_link) {
 1148                 *next = oidp->oid_number;
 1149 
 1150                 if ((oidp->oid_kind & CTLFLAG_DORMANT) != 0)
 1151                         continue;
 1152 
 1153                 if (honor_skip && (oidp->oid_kind & CTLFLAG_SKIP) != 0)
 1154                         continue;
 1155 
 1156                 if (namelen == 0) {
 1157                         /*
 1158                          * We have reached a node with a full name match and are
 1159                          * looking for the next oid in its children.
 1160                          *
 1161                          * For CTL_SYSCTL_NEXTNOSKIP we are done.
 1162                          *
 1163                          * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
 1164                          * has a handler) and move on to the children.
 1165                          */
 1166                         if (!honor_skip)
 1167                                 return (0);
 1168                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 
 1169                                 return (0);
 1170                         if (oidp->oid_handler) 
 1171                                 return (0);
 1172                         lsp = SYSCTL_CHILDREN(oidp);
 1173                         if (!sysctl_sysctl_next_ls(lsp, NULL, 0, next + 1, len,
 1174                             level + 1, honor_skip))
 1175                                 return (0);
 1176                         /*
 1177                          * There were no useable children in this node.
 1178                          * Continue searching for the next oid at this level.
 1179                          */
 1180                         goto emptynode;
 1181                 }
 1182 
 1183                 /*
 1184                  * No match yet. Continue seeking the given name.
 1185                  *
 1186                  * We are iterating in order by oid_number, so skip oids lower
 1187                  * than the one we are looking for.
 1188                  *
 1189                  * When the current oid_number is higher than the one we seek,
 1190                  * that means we have reached the next oid in the sequence and
 1191                  * should return it.
 1192                  *
 1193                  * If the oid_number matches the name at this level then we
 1194                  * have to find a node to continue searching at the next level.
 1195                  */
 1196                 if (oidp->oid_number < *name)
 1197                         continue;
 1198                 if (oidp->oid_number > *name) {
 1199                         /*
 1200                          * We have reached the next oid.
 1201                          *
 1202                          * For CTL_SYSCTL_NEXTNOSKIP we are done.
 1203                          *
 1204                          * For CTL_SYSCTL_NEXT we skip CTLTYPE_NODE (unless it
 1205                          * has a handler) and move on to the children.
 1206                          */
 1207                         if (!honor_skip)
 1208                                 return (0);
 1209                         if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
 1210                                 return (0);
 1211                         if (oidp->oid_handler)
 1212                                 return (0);
 1213                         lsp = SYSCTL_CHILDREN(oidp);
 1214                         if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
 1215                             next + 1, len, level + 1, honor_skip))
 1216                                 return (0);
 1217                         goto next;
 1218                 }
 1219                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
 1220                         continue;
 1221                 if (oidp->oid_handler)
 1222                         continue;
 1223                 lsp = SYSCTL_CHILDREN(oidp);
 1224                 if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
 1225                     next + 1, len, level + 1, honor_skip))
 1226                         return (0);
 1227         next:
 1228                 /*
 1229                  * There were no useable children in this node.
 1230                  * Continue searching for the next oid at the root level.
 1231                  */
 1232                 namelen = 1;
 1233         emptynode:
 1234                 /* Reset len in case a failed recursive call changed it. */
 1235                 *len = level;
 1236         }
 1237         return (ENOENT);
 1238 }
 1239 
 1240 static int
 1241 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
 1242 {
 1243         int *name = (int *) arg1;
 1244         u_int namelen = arg2;
 1245         int len, error;
 1246         struct sysctl_oid_list *lsp = &sysctl__children;
 1247         struct rm_priotracker tracker;
 1248         int next[CTL_MAXNAME];
 1249 
 1250         SYSCTL_RLOCK(&tracker);
 1251         error = sysctl_sysctl_next_ls(lsp, name, namelen, next, &len, 1,
 1252             oidp->oid_number == CTL_SYSCTL_NEXT);
 1253         SYSCTL_RUNLOCK(&tracker);
 1254         if (error)
 1255                 return (error);
 1256         error = SYSCTL_OUT(req, next, len * sizeof (int));
 1257         return (error);
 1258 }
 1259 
 1260 /*
 1261  * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in
 1262  * capability mode.
 1263  */
 1264 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXT, next, CTLFLAG_RD |
 1265     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
 1266 
 1267 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD |
 1268     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, "");
 1269 
 1270 static int
 1271 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 1272 {
 1273         struct sysctl_oid *oidp;
 1274         struct sysctl_oid_list *lsp = &sysctl__children;
 1275         char *p;
 1276 
 1277         SYSCTL_ASSERT_LOCKED();
 1278 
 1279         for (*len = 0; *len < CTL_MAXNAME;) {
 1280                 p = strsep(&name, ".");
 1281 
 1282                 oidp = SLIST_FIRST(lsp);
 1283                 for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
 1284                         if (oidp == NULL)
 1285                                 return (ENOENT);
 1286                         if (strcmp(p, oidp->oid_name) == 0)
 1287                                 break;
 1288                 }
 1289                 *oid++ = oidp->oid_number;
 1290                 (*len)++;
 1291 
 1292                 if (name == NULL || *name == '\0') {
 1293                         if (oidpp)
 1294                                 *oidpp = oidp;
 1295                         return (0);
 1296                 }
 1297 
 1298                 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
 1299                         break;
 1300 
 1301                 if (oidp->oid_handler)
 1302                         break;
 1303 
 1304                 lsp = SYSCTL_CHILDREN(oidp);
 1305         }
 1306         return (ENOENT);
 1307 }
 1308 
 1309 static int
 1310 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
 1311 {
 1312         char *p;
 1313         int error, oid[CTL_MAXNAME], len = 0;
 1314         struct sysctl_oid *op = NULL;
 1315         struct rm_priotracker tracker;
 1316         char buf[32];
 1317 
 1318         if (!req->newlen) 
 1319                 return (ENOENT);
 1320         if (req->newlen >= MAXPATHLEN)  /* XXX arbitrary, undocumented */
 1321                 return (ENAMETOOLONG);
 1322 
 1323         p = buf;
 1324         if (req->newlen >= sizeof(buf))
 1325                 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
 1326 
 1327         error = SYSCTL_IN(req, p, req->newlen);
 1328         if (error) {
 1329                 if (p != buf)
 1330                         free(p, M_SYSCTL);
 1331                 return (error);
 1332         }
 1333 
 1334         p [req->newlen] = '\0';
 1335 
 1336         SYSCTL_RLOCK(&tracker);
 1337         error = name2oid(p, oid, &len, &op);
 1338         SYSCTL_RUNLOCK(&tracker);
 1339 
 1340         if (p != buf)
 1341                 free(p, M_SYSCTL);
 1342 
 1343         if (error)
 1344                 return (error);
 1345 
 1346         error = SYSCTL_OUT(req, oid, len * sizeof *oid);
 1347         return (error);
 1348 }
 1349 
 1350 /*
 1351  * XXXRW/JA: Shouldn't return name2oid data for nodes that we don't permit in
 1352  * capability mode.
 1353  */
 1354 SYSCTL_PROC(_sysctl, CTL_SYSCTL_NAME2OID, name2oid, CTLTYPE_INT | CTLFLAG_RW |
 1355     CTLFLAG_ANYBODY | CTLFLAG_MPSAFE | CTLFLAG_CAPRW, 0, 0,
 1356     sysctl_sysctl_name2oid, "I", "");
 1357 
 1358 static int
 1359 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
 1360 {
 1361         struct sysctl_oid *oid;
 1362         struct rm_priotracker tracker;
 1363         int error;
 1364 
 1365         error = sysctl_wire_old_buffer(req, 0);
 1366         if (error)
 1367                 return (error);
 1368 
 1369         SYSCTL_RLOCK(&tracker);
 1370         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
 1371         if (error)
 1372                 goto out;
 1373 
 1374         if (oid->oid_fmt == NULL) {
 1375                 error = ENOENT;
 1376                 goto out;
 1377         }
 1378         error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
 1379         if (error)
 1380                 goto out;
 1381         error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
 1382  out:
 1383         SYSCTL_RUNLOCK(&tracker);
 1384         return (error);
 1385 }
 1386 
 1387 
 1388 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDFMT, oidfmt, CTLFLAG_RD |
 1389     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidfmt, "");
 1390 
 1391 static int
 1392 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
 1393 {
 1394         struct sysctl_oid *oid;
 1395         struct rm_priotracker tracker;
 1396         int error;
 1397 
 1398         error = sysctl_wire_old_buffer(req, 0);
 1399         if (error)
 1400                 return (error);
 1401 
 1402         SYSCTL_RLOCK(&tracker);
 1403         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
 1404         if (error)
 1405                 goto out;
 1406 
 1407         if (oid->oid_descr == NULL) {
 1408                 error = ENOENT;
 1409                 goto out;
 1410         }
 1411         error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
 1412  out:
 1413         SYSCTL_RUNLOCK(&tracker);
 1414         return (error);
 1415 }
 1416 
 1417 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDDESCR, oiddescr, CTLFLAG_RD |
 1418     CTLFLAG_MPSAFE|CTLFLAG_CAPRD, sysctl_sysctl_oiddescr, "");
 1419 
 1420 static int
 1421 sysctl_sysctl_oidlabel(SYSCTL_HANDLER_ARGS)
 1422 {
 1423         struct sysctl_oid *oid;
 1424         struct rm_priotracker tracker;
 1425         int error;
 1426 
 1427         error = sysctl_wire_old_buffer(req, 0);
 1428         if (error)
 1429                 return (error);
 1430 
 1431         SYSCTL_RLOCK(&tracker);
 1432         error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
 1433         if (error)
 1434                 goto out;
 1435 
 1436         if (oid->oid_label == NULL) {
 1437                 error = ENOENT;
 1438                 goto out;
 1439         }
 1440         error = SYSCTL_OUT(req, oid->oid_label, strlen(oid->oid_label) + 1);
 1441  out:
 1442         SYSCTL_RUNLOCK(&tracker);
 1443         return (error);
 1444 }
 1445 
 1446 static SYSCTL_NODE(_sysctl, CTL_SYSCTL_OIDLABEL, oidlabel, CTLFLAG_RD |
 1447     CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_oidlabel, "");
 1448 
 1449 /*
 1450  * Default "handler" functions.
 1451  */
 1452 
 1453 /*
 1454  * Handle a bool.
 1455  * Two cases:
 1456  *     a variable:  point arg1 at it.
 1457  *     a constant:  pass it in arg2.
 1458  */
 1459 
 1460 int
 1461 sysctl_handle_bool(SYSCTL_HANDLER_ARGS)
 1462 {
 1463         uint8_t temp;
 1464         int error;
 1465 
 1466         /*
 1467          * Attempt to get a coherent snapshot by making a copy of the data.
 1468          */
 1469         if (arg1)
 1470                 temp = *(bool *)arg1 ? 1 : 0;
 1471         else
 1472                 temp = arg2 ? 1 : 0;
 1473 
 1474         error = SYSCTL_OUT(req, &temp, sizeof(temp));
 1475         if (error || !req->newptr)
 1476                 return (error);
 1477 
 1478         if (!arg1)
 1479                 error = EPERM;
 1480         else {
 1481                 error = SYSCTL_IN(req, &temp, sizeof(temp));
 1482                 if (!error)
 1483                         *(bool *)arg1 = temp ? 1 : 0;
 1484         }
 1485         return (error);
 1486 }
 1487 
 1488 /*
 1489  * Handle an int8_t, signed or unsigned.
 1490  * Two cases:
 1491  *     a variable:  point arg1 at it.
 1492  *     a constant:  pass it in arg2.
 1493  */
 1494 
 1495 int
 1496 sysctl_handle_8(SYSCTL_HANDLER_ARGS)
 1497 {
 1498         int8_t tmpout;
 1499         int error = 0;
 1500 
 1501         /*
 1502          * Attempt to get a coherent snapshot by making a copy of the data.
 1503          */
 1504         if (arg1)
 1505                 tmpout = *(int8_t *)arg1;
 1506         else
 1507                 tmpout = arg2;
 1508         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
 1509 
 1510         if (error || !req->newptr)
 1511                 return (error);
 1512 
 1513         if (!arg1)
 1514                 error = EPERM;
 1515         else
 1516                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
 1517         return (error);
 1518 }
 1519 
 1520 /*
 1521  * Handle an int16_t, signed or unsigned.
 1522  * Two cases:
 1523  *     a variable:  point arg1 at it.
 1524  *     a constant:  pass it in arg2.
 1525  */
 1526 
 1527 int
 1528 sysctl_handle_16(SYSCTL_HANDLER_ARGS)
 1529 {
 1530         int16_t tmpout;
 1531         int error = 0;
 1532 
 1533         /*
 1534          * Attempt to get a coherent snapshot by making a copy of the data.
 1535          */
 1536         if (arg1)
 1537                 tmpout = *(int16_t *)arg1;
 1538         else
 1539                 tmpout = arg2;
 1540         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
 1541 
 1542         if (error || !req->newptr)
 1543                 return (error);
 1544 
 1545         if (!arg1)
 1546                 error = EPERM;
 1547         else
 1548                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
 1549         return (error);
 1550 }
 1551 
 1552 /*
 1553  * Handle an int32_t, signed or unsigned.
 1554  * Two cases:
 1555  *     a variable:  point arg1 at it.
 1556  *     a constant:  pass it in arg2.
 1557  */
 1558 
 1559 int
 1560 sysctl_handle_32(SYSCTL_HANDLER_ARGS)
 1561 {
 1562         int32_t tmpout;
 1563         int error = 0;
 1564 
 1565         /*
 1566          * Attempt to get a coherent snapshot by making a copy of the data.
 1567          */
 1568         if (arg1)
 1569                 tmpout = *(int32_t *)arg1;
 1570         else
 1571                 tmpout = arg2;
 1572         error = SYSCTL_OUT(req, &tmpout, sizeof(tmpout));
 1573 
 1574         if (error || !req->newptr)
 1575                 return (error);
 1576 
 1577         if (!arg1)
 1578                 error = EPERM;
 1579         else
 1580                 error = SYSCTL_IN(req, arg1, sizeof(tmpout));
 1581         return (error);
 1582 }
 1583 
 1584 /*
 1585  * Handle an int, signed or unsigned.
 1586  * Two cases:
 1587  *     a variable:  point arg1 at it.
 1588  *     a constant:  pass it in arg2.
 1589  */
 1590 
 1591 int
 1592 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
 1593 {
 1594         int tmpout, error = 0;
 1595 
 1596         /*
 1597          * Attempt to get a coherent snapshot by making a copy of the data.
 1598          */
 1599         if (arg1)
 1600                 tmpout = *(int *)arg1;
 1601         else
 1602                 tmpout = arg2;
 1603         error = SYSCTL_OUT(req, &tmpout, sizeof(int));
 1604 
 1605         if (error || !req->newptr)
 1606                 return (error);
 1607 
 1608         if (!arg1)
 1609                 error = EPERM;
 1610         else
 1611                 error = SYSCTL_IN(req, arg1, sizeof(int));
 1612         return (error);
 1613 }
 1614 
 1615 /*
 1616  * Based on on sysctl_handle_int() convert milliseconds into ticks.
 1617  * Note: this is used by TCP.
 1618  */
 1619 
 1620 int
 1621 sysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
 1622 {
 1623         int error, s, tt;
 1624 
 1625         tt = *(int *)arg1;
 1626         s = (int)((int64_t)tt * 1000 / hz);
 1627 
 1628         error = sysctl_handle_int(oidp, &s, 0, req);
 1629         if (error || !req->newptr)
 1630                 return (error);
 1631 
 1632         tt = (int)((int64_t)s * hz / 1000);
 1633         if (tt < 1)
 1634                 return (EINVAL);
 1635 
 1636         *(int *)arg1 = tt;
 1637         return (0);
 1638 }
 1639 
 1640 
 1641 /*
 1642  * Handle a long, signed or unsigned.
 1643  * Two cases:
 1644  *     a variable:  point arg1 at it.
 1645  *     a constant:  pass it in arg2.
 1646  */
 1647 
 1648 int
 1649 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
 1650 {
 1651         int error = 0;
 1652         long tmplong;
 1653 #ifdef SCTL_MASK32
 1654         int tmpint;
 1655 #endif
 1656 
 1657         /*
 1658          * Attempt to get a coherent snapshot by making a copy of the data.
 1659          */
 1660         if (arg1)
 1661                 tmplong = *(long *)arg1;
 1662         else
 1663                 tmplong = arg2;
 1664 #ifdef SCTL_MASK32
 1665         if (req->flags & SCTL_MASK32) {
 1666                 tmpint = tmplong;
 1667                 error = SYSCTL_OUT(req, &tmpint, sizeof(int));
 1668         } else
 1669 #endif
 1670                 error = SYSCTL_OUT(req, &tmplong, sizeof(long));
 1671 
 1672         if (error || !req->newptr)
 1673                 return (error);
 1674 
 1675         if (!arg1)
 1676                 error = EPERM;
 1677 #ifdef SCTL_MASK32
 1678         else if (req->flags & SCTL_MASK32) {
 1679                 error = SYSCTL_IN(req, &tmpint, sizeof(int));
 1680                 *(long *)arg1 = (long)tmpint;
 1681         }
 1682 #endif
 1683         else
 1684                 error = SYSCTL_IN(req, arg1, sizeof(long));
 1685         return (error);
 1686 }
 1687 
 1688 /*
 1689  * Handle a 64 bit int, signed or unsigned.
 1690  * Two cases:
 1691  *     a variable:  point arg1 at it.
 1692  *     a constant:  pass it in arg2.
 1693  */
 1694 int
 1695 sysctl_handle_64(SYSCTL_HANDLER_ARGS)
 1696 {
 1697         int error = 0;
 1698         uint64_t tmpout;
 1699 
 1700         /*
 1701          * Attempt to get a coherent snapshot by making a copy of the data.
 1702          */
 1703         if (arg1)
 1704                 tmpout = *(uint64_t *)arg1;
 1705         else
 1706                 tmpout = arg2;
 1707         error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
 1708 
 1709         if (error || !req->newptr)
 1710                 return (error);
 1711 
 1712         if (!arg1)
 1713                 error = EPERM;
 1714         else
 1715                 error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
 1716         return (error);
 1717 }
 1718 
 1719 /*
 1720  * Handle our generic '\0' terminated 'C' string.
 1721  * Two cases:
 1722  *      a variable string:  point arg1 at it, arg2 is max length.
 1723  *      a constant string:  point arg1 at it, arg2 is zero.
 1724  */
 1725 
 1726 int
 1727 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
 1728 {
 1729         size_t outlen;
 1730         int error = 0, ro_string = 0;
 1731 
 1732         /*
 1733          * A zero-length buffer indicates a fixed size read-only
 1734          * string:
 1735          */
 1736         if (arg2 == 0) {
 1737                 arg2 = strlen((char *)arg1) + 1;
 1738                 ro_string = 1;
 1739         }
 1740 
 1741         if (req->oldptr != NULL) {
 1742                 char *tmparg;
 1743 
 1744                 if (ro_string) {
 1745                         tmparg = arg1;
 1746                 } else {
 1747                         /* try to make a coherent snapshot of the string */
 1748                         tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
 1749                         memcpy(tmparg, arg1, arg2);
 1750                 }
 1751 
 1752                 outlen = strnlen(tmparg, arg2 - 1) + 1;
 1753                 error = SYSCTL_OUT(req, tmparg, outlen);
 1754 
 1755                 if (!ro_string)
 1756                         free(tmparg, M_SYSCTLTMP);
 1757         } else {
 1758                 outlen = strnlen((char *)arg1, arg2 - 1) + 1;
 1759                 error = SYSCTL_OUT(req, NULL, outlen);
 1760         }
 1761         if (error || !req->newptr)
 1762                 return (error);
 1763 
 1764         if ((req->newlen - req->newidx) >= arg2) {
 1765                 error = EINVAL;
 1766         } else {
 1767                 arg2 = (req->newlen - req->newidx);
 1768                 error = SYSCTL_IN(req, arg1, arg2);
 1769                 ((char *)arg1)[arg2] = '\0';
 1770         }
 1771         return (error);
 1772 }
 1773 
 1774 /*
 1775  * Handle any kind of opaque data.
 1776  * arg1 points to it, arg2 is the size.
 1777  */
 1778 
 1779 int
 1780 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
 1781 {
 1782         int error, tries;
 1783         u_int generation;
 1784         struct sysctl_req req2;
 1785 
 1786         /*
 1787          * Attempt to get a coherent snapshot, by using the thread
 1788          * pre-emption counter updated from within mi_switch() to
 1789          * determine if we were pre-empted during a bcopy() or
 1790          * copyout(). Make 3 attempts at doing this before giving up.
 1791          * If we encounter an error, stop immediately.
 1792          */
 1793         tries = 0;
 1794         req2 = *req;
 1795 retry:
 1796         generation = curthread->td_generation;
 1797         error = SYSCTL_OUT(req, arg1, arg2);
 1798         if (error)
 1799                 return (error);
 1800         tries++;
 1801         if (generation != curthread->td_generation && tries < 3) {
 1802                 *req = req2;
 1803                 goto retry;
 1804         }
 1805 
 1806         error = SYSCTL_IN(req, arg1, arg2);
 1807 
 1808         return (error);
 1809 }
 1810 
 1811 /*
 1812  * Convert seconds to a struct timeval.  Intended for use with
 1813  * intervals and thus does not permit negative seconds.
 1814  */
 1815 int
 1816 sysctl_sec_to_timeval(SYSCTL_HANDLER_ARGS)
 1817 {
 1818         struct timeval *tv;
 1819         int error, secs;
 1820 
 1821         tv = arg1;
 1822         secs = tv->tv_sec;
 1823 
 1824         error = sysctl_handle_int(oidp, &secs, 0, req);
 1825         if (error || req->newptr == NULL)
 1826                 return (error);
 1827 
 1828         if (secs < 0)
 1829                 return (EINVAL);
 1830         tv->tv_sec = secs;
 1831 
 1832         return (0);
 1833 }
 1834 
 1835 /*
 1836  * Transfer functions to/from kernel space.
 1837  * XXX: rather untested at this point
 1838  */
 1839 static int
 1840 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
 1841 {
 1842         size_t i = 0;
 1843 
 1844         if (req->oldptr) {
 1845                 i = l;
 1846                 if (req->oldlen <= req->oldidx)
 1847                         i = 0;
 1848                 else
 1849                         if (i > req->oldlen - req->oldidx)
 1850                                 i = req->oldlen - req->oldidx;
 1851                 if (i > 0)
 1852                         bcopy(p, (char *)req->oldptr + req->oldidx, i);
 1853         }
 1854         req->oldidx += l;
 1855         if (req->oldptr && i != l)
 1856                 return (ENOMEM);
 1857         return (0);
 1858 }
 1859 
 1860 static int
 1861 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
 1862 {
 1863         if (!req->newptr)
 1864                 return (0);
 1865         if (req->newlen - req->newidx < l)
 1866                 return (EINVAL);
 1867         bcopy((char *)req->newptr + req->newidx, p, l);
 1868         req->newidx += l;
 1869         return (0);
 1870 }
 1871 
 1872 int
 1873 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
 1874     size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
 1875 {
 1876         int error = 0;
 1877         struct sysctl_req req;
 1878 
 1879         bzero(&req, sizeof req);
 1880 
 1881         req.td = td;
 1882         req.flags = flags;
 1883 
 1884         if (oldlenp) {
 1885                 req.oldlen = *oldlenp;
 1886         }
 1887         req.validlen = req.oldlen;
 1888 
 1889         if (old) {
 1890                 req.oldptr= old;
 1891         }
 1892 
 1893         if (new != NULL) {
 1894                 req.newlen = newlen;
 1895                 req.newptr = new;
 1896         }
 1897 
 1898         req.oldfunc = sysctl_old_kernel;
 1899         req.newfunc = sysctl_new_kernel;
 1900         req.lock = REQ_UNWIRED;
 1901 
 1902         error = sysctl_root(0, name, namelen, &req);
 1903 
 1904         if (req.lock == REQ_WIRED && req.validlen > 0)
 1905                 vsunlock(req.oldptr, req.validlen);
 1906 
 1907         if (error && error != ENOMEM)
 1908                 return (error);
 1909 
 1910         if (retval) {
 1911                 if (req.oldptr && req.oldidx > req.validlen)
 1912                         *retval = req.validlen;
 1913                 else
 1914                         *retval = req.oldidx;
 1915         }
 1916         return (error);
 1917 }
 1918 
 1919 int
 1920 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
 1921     void *new, size_t newlen, size_t *retval, int flags)
 1922 {
 1923         int oid[CTL_MAXNAME];
 1924         size_t oidlen, plen;
 1925         int error;
 1926 
 1927         oid[0] = CTL_SYSCTL;
 1928         oid[1] = CTL_SYSCTL_NAME2OID;
 1929         oidlen = sizeof(oid);
 1930 
 1931         error = kernel_sysctl(td, oid, 2, oid, &oidlen,
 1932             (void *)name, strlen(name), &plen, flags);
 1933         if (error)
 1934                 return (error);
 1935 
 1936         error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
 1937             new, newlen, retval, flags);
 1938         return (error);
 1939 }
 1940 
 1941 /*
 1942  * Transfer function to/from user space.
 1943  */
 1944 static int
 1945 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
 1946 {
 1947         size_t i, len, origidx;
 1948         int error;
 1949 
 1950         origidx = req->oldidx;
 1951         req->oldidx += l;
 1952         if (req->oldptr == NULL)
 1953                 return (0);
 1954         /*
 1955          * If we have not wired the user supplied buffer and we are currently
 1956          * holding locks, drop a witness warning, as it's possible that
 1957          * write operations to the user page can sleep.
 1958          */
 1959         if (req->lock != REQ_WIRED)
 1960                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
 1961                     "sysctl_old_user()");
 1962         i = l;
 1963         len = req->validlen;
 1964         if (len <= origidx)
 1965                 i = 0;
 1966         else {
 1967                 if (i > len - origidx)
 1968                         i = len - origidx;
 1969                 if (req->lock == REQ_WIRED) {
 1970                         error = copyout_nofault(p, (char *)req->oldptr +
 1971                             origidx, i);
 1972                 } else
 1973                         error = copyout(p, (char *)req->oldptr + origidx, i);
 1974                 if (error != 0)
 1975                         return (error);
 1976         }
 1977         if (i < l)
 1978                 return (ENOMEM);
 1979         return (0);
 1980 }
 1981 
 1982 static int
 1983 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
 1984 {
 1985         int error;
 1986 
 1987         if (!req->newptr)
 1988                 return (0);
 1989         if (req->newlen - req->newidx < l)
 1990                 return (EINVAL);
 1991         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
 1992             "sysctl_new_user()");
 1993         error = copyin((char *)req->newptr + req->newidx, p, l);
 1994         req->newidx += l;
 1995         return (error);
 1996 }
 1997 
 1998 /*
 1999  * Wire the user space destination buffer.  If set to a value greater than
 2000  * zero, the len parameter limits the maximum amount of wired memory.
 2001  */
 2002 int
 2003 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
 2004 {
 2005         int ret;
 2006         size_t wiredlen;
 2007 
 2008         wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
 2009         ret = 0;
 2010         if (req->lock != REQ_WIRED && req->oldptr &&
 2011             req->oldfunc == sysctl_old_user) {
 2012                 if (wiredlen != 0) {
 2013                         ret = vslock(req->oldptr, wiredlen);
 2014                         if (ret != 0) {
 2015                                 if (ret != ENOMEM)
 2016                                         return (ret);
 2017                                 wiredlen = 0;
 2018                         }
 2019                 }
 2020                 req->lock = REQ_WIRED;
 2021                 req->validlen = wiredlen;
 2022         }
 2023         return (0);
 2024 }
 2025 
 2026 int
 2027 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
 2028     int *nindx, struct sysctl_req *req)
 2029 {
 2030         struct sysctl_oid_list *lsp;
 2031         struct sysctl_oid *oid;
 2032         int indx;
 2033 
 2034         SYSCTL_ASSERT_LOCKED();
 2035         lsp = &sysctl__children;
 2036         indx = 0;
 2037         while (indx < CTL_MAXNAME) {
 2038                 SLIST_FOREACH(oid, lsp, oid_link) {
 2039                         if (oid->oid_number == name[indx])
 2040                                 break;
 2041                 }
 2042                 if (oid == NULL)
 2043                         return (ENOENT);
 2044 
 2045                 indx++;
 2046                 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 2047                         if (oid->oid_handler != NULL || indx == namelen) {
 2048                                 *noid = oid;
 2049                                 if (nindx != NULL)
 2050                                         *nindx = indx;
 2051                                 KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
 2052                                     ("%s found DYING node %p", __func__, oid));
 2053                                 return (0);
 2054                         }
 2055                         lsp = SYSCTL_CHILDREN(oid);
 2056                 } else if (indx == namelen) {
 2057                         if ((oid->oid_kind & CTLFLAG_DORMANT) != 0)
 2058                                 return (ENOENT);
 2059                         *noid = oid;
 2060                         if (nindx != NULL)
 2061                                 *nindx = indx;
 2062                         KASSERT((oid->oid_kind & CTLFLAG_DYING) == 0,
 2063                             ("%s found DYING node %p", __func__, oid));
 2064                         return (0);
 2065                 } else {
 2066                         return (ENOTDIR);
 2067                 }
 2068         }
 2069         return (ENOENT);
 2070 }
 2071 
 2072 /*
 2073  * Traverse our tree, and find the right node, execute whatever it points
 2074  * to, and return the resulting error code.
 2075  */
 2076 
 2077 static int
 2078 sysctl_root(SYSCTL_HANDLER_ARGS)
 2079 {
 2080         struct sysctl_oid *oid;
 2081         struct rm_priotracker tracker;
 2082         int error, indx, lvl;
 2083 
 2084         SYSCTL_RLOCK(&tracker);
 2085 
 2086         error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
 2087         if (error)
 2088                 goto out;
 2089 
 2090         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 2091                 /*
 2092                  * You can't call a sysctl when it's a node, but has
 2093                  * no handler.  Inform the user that it's a node.
 2094                  * The indx may or may not be the same as namelen.
 2095                  */
 2096                 if (oid->oid_handler == NULL) {
 2097                         error = EISDIR;
 2098                         goto out;
 2099                 }
 2100         }
 2101 
 2102         /* Is this sysctl writable? */
 2103         if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
 2104                 error = EPERM;
 2105                 goto out;
 2106         }
 2107 
 2108         KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
 2109 
 2110 #ifdef CAPABILITY_MODE
 2111         /*
 2112          * If the process is in capability mode, then don't permit reading or
 2113          * writing unless specifically granted for the node.
 2114          */
 2115         if (IN_CAPABILITY_MODE(req->td)) {
 2116                 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
 2117                     (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
 2118                         error = EPERM;
 2119                         goto out;
 2120                 }
 2121         }
 2122 #endif
 2123 
 2124         /* Is this sysctl sensitive to securelevels? */
 2125         if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
 2126                 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
 2127                 error = securelevel_gt(req->td->td_ucred, lvl);
 2128                 if (error)
 2129                         goto out;
 2130         }
 2131 
 2132         /* Is this sysctl writable by only privileged users? */
 2133         if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
 2134                 int priv;
 2135 
 2136                 if (oid->oid_kind & CTLFLAG_PRISON)
 2137                         priv = PRIV_SYSCTL_WRITEJAIL;
 2138 #ifdef VIMAGE
 2139                 else if ((oid->oid_kind & CTLFLAG_VNET) &&
 2140                      prison_owns_vnet(req->td->td_ucred))
 2141                         priv = PRIV_SYSCTL_WRITEJAIL;
 2142 #endif
 2143                 else
 2144                         priv = PRIV_SYSCTL_WRITE;
 2145                 error = priv_check(req->td, priv);
 2146                 if (error)
 2147                         goto out;
 2148         }
 2149 
 2150         if (!oid->oid_handler) {
 2151                 error = EINVAL;
 2152                 goto out;
 2153         }
 2154 
 2155         if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
 2156                 arg1 = (int *)arg1 + indx;
 2157                 arg2 -= indx;
 2158         } else {
 2159                 arg1 = oid->oid_arg1;
 2160                 arg2 = oid->oid_arg2;
 2161         }
 2162 #ifdef MAC
 2163         error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
 2164             req);
 2165         if (error != 0)
 2166                 goto out;
 2167 #endif
 2168 #ifdef VIMAGE
 2169         if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
 2170                 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
 2171 #endif
 2172         error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
 2173 
 2174 out:
 2175         SYSCTL_RUNLOCK(&tracker);
 2176         return (error);
 2177 }
 2178 
 2179 #ifndef _SYS_SYSPROTO_H_
 2180 struct sysctl_args {
 2181         int     *name;
 2182         u_int   namelen;
 2183         void    *old;
 2184         size_t  *oldlenp;
 2185         void    *new;
 2186         size_t  newlen;
 2187 };
 2188 #endif
 2189 int
 2190 sys___sysctl(struct thread *td, struct sysctl_args *uap)
 2191 {
 2192         int error, i, name[CTL_MAXNAME];
 2193         size_t j;
 2194 
 2195         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
 2196                 return (EINVAL);
 2197 
 2198         error = copyin(uap->name, &name, uap->namelen * sizeof(int));
 2199         if (error)
 2200                 return (error);
 2201 
 2202         error = userland_sysctl(td, name, uap->namelen,
 2203                 uap->old, uap->oldlenp, 0,
 2204                 uap->new, uap->newlen, &j, 0);
 2205         if (error && error != ENOMEM)
 2206                 return (error);
 2207         if (uap->oldlenp) {
 2208                 i = copyout(&j, uap->oldlenp, sizeof(j));
 2209                 if (i)
 2210                         return (i);
 2211         }
 2212         return (error);
 2213 }
 2214 
 2215 int
 2216 kern___sysctlbyname(struct thread *td, const char *oname, size_t namelen,
 2217     void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval,
 2218     int flags, bool inkernel)
 2219 {
 2220         int oid[CTL_MAXNAME];
 2221         char namebuf[16];
 2222         char *name;
 2223         size_t oidlen;
 2224         int error;
 2225 
 2226         if (namelen > MAXPATHLEN || namelen == 0)
 2227                 return (EINVAL);
 2228         name = namebuf;
 2229         if (namelen > sizeof(namebuf))
 2230                 name = malloc(namelen, M_SYSCTL, M_WAITOK);
 2231         error = copyin(oname, name, namelen);
 2232         if (error != 0)
 2233                 goto out;
 2234 
 2235         oid[0] = CTL_SYSCTL;
 2236         oid[1] = CTL_SYSCTL_NAME2OID;
 2237         oidlen = sizeof(oid);
 2238         error = kernel_sysctl(td, oid, 2, oid, &oidlen, (void *)name, namelen,
 2239             retval, flags);
 2240         if (error != 0)
 2241                 goto out;
 2242         error = userland_sysctl(td, oid, *retval / sizeof(int), old, oldlenp,
 2243             inkernel, new, newlen, retval, flags);
 2244 
 2245 out:
 2246         if (namelen > sizeof(namebuf))
 2247                 free(name, M_SYSCTL);
 2248         return (error);
 2249 }
 2250 
 2251 #ifndef _SYS_SYSPROTO_H_
 2252 struct __sysctlbyname_args {
 2253         const char      *name;
 2254         size_t  namelen;
 2255         void    *old;
 2256         size_t  *oldlenp;
 2257         void    *new;
 2258         size_t  newlen;
 2259 };
 2260 #endif
 2261 int
 2262 sys___sysctlbyname(struct thread *td, struct __sysctlbyname_args *uap)
 2263 {
 2264         size_t rv;
 2265         int error;
 2266 
 2267         error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
 2268             uap->oldlenp, uap->new, uap->newlen, &rv, 0, 0);
 2269         if (error != 0)
 2270                 return (error);
 2271         if (uap->oldlenp != NULL)
 2272                 error = copyout(&rv, uap->oldlenp, sizeof(rv));
 2273 
 2274         return (error);
 2275 }
 2276 
 2277 /*
 2278  * This is used from various compatibility syscalls too.  That's why name
 2279  * must be in kernel space.
 2280  */
 2281 int
 2282 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
 2283     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
 2284     int flags)
 2285 {
 2286         int error = 0, memlocked;
 2287         struct sysctl_req req;
 2288 
 2289         bzero(&req, sizeof req);
 2290 
 2291         req.td = td;
 2292         req.flags = flags;
 2293 
 2294         if (oldlenp) {
 2295                 if (inkernel) {
 2296                         req.oldlen = *oldlenp;
 2297                 } else {
 2298                         error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
 2299                         if (error)
 2300                                 return (error);
 2301                 }
 2302         }
 2303         req.validlen = req.oldlen;
 2304         req.oldptr = old;
 2305 
 2306         if (new != NULL) {
 2307                 req.newlen = newlen;
 2308                 req.newptr = new;
 2309         }
 2310 
 2311         req.oldfunc = sysctl_old_user;
 2312         req.newfunc = sysctl_new_user;
 2313         req.lock = REQ_UNWIRED;
 2314 
 2315 #ifdef KTRACE
 2316         if (KTRPOINT(curthread, KTR_SYSCTL))
 2317                 ktrsysctl(name, namelen);
 2318 #endif
 2319         memlocked = 0;
 2320         if (req.oldptr && req.oldlen > 4 * PAGE_SIZE) {
 2321                 memlocked = 1;
 2322                 sx_xlock(&sysctlmemlock);
 2323         }
 2324         CURVNET_SET(TD_TO_VNET(td));
 2325 
 2326         for (;;) {
 2327                 req.oldidx = 0;
 2328                 req.newidx = 0;
 2329                 error = sysctl_root(0, name, namelen, &req);
 2330                 if (error != EAGAIN)
 2331                         break;
 2332                 kern_yield(PRI_USER);
 2333         }
 2334 
 2335         CURVNET_RESTORE();
 2336 
 2337         if (req.lock == REQ_WIRED && req.validlen > 0)
 2338                 vsunlock(req.oldptr, req.validlen);
 2339         if (memlocked)
 2340                 sx_xunlock(&sysctlmemlock);
 2341 
 2342         if (error && error != ENOMEM)
 2343                 return (error);
 2344 
 2345         if (retval) {
 2346                 if (req.oldptr && req.oldidx > req.validlen)
 2347                         *retval = req.validlen;
 2348                 else
 2349                         *retval = req.oldidx;
 2350         }
 2351         return (error);
 2352 }
 2353 
 2354 /*
 2355  * Drain into a sysctl struct.  The user buffer should be wired if a page
 2356  * fault would cause issue.
 2357  */
 2358 static int
 2359 sbuf_sysctl_drain(void *arg, const char *data, int len)
 2360 {
 2361         struct sysctl_req *req = arg;
 2362         int error;
 2363 
 2364         error = SYSCTL_OUT(req, data, len);
 2365         KASSERT(error >= 0, ("Got unexpected negative value %d", error));
 2366         return (error == 0 ? len : -error);
 2367 }
 2368 
 2369 struct sbuf *
 2370 sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
 2371     struct sysctl_req *req)
 2372 {
 2373 
 2374         /* Supply a default buffer size if none given. */
 2375         if (buf == NULL && length == 0)
 2376                 length = 64;
 2377         s = sbuf_new(s, buf, length, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
 2378         sbuf_set_drain(s, sbuf_sysctl_drain, req);
 2379         return (s);
 2380 }

Cache object: 687330fb8f3c1ca0f1230a1bae4f208b


[ 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.