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

Cache object: 570aef8afb47f95dc4631afea55908e8


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