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

Cache object: 0e351eb06922f3440e8d4c7b4e20ea17


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