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

Cache object: a57e8f02e65cd28a1974e9965fb433c7


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