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_environment.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) 1998 Michael Smith
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD$
   27  */
   28 
   29 /*
   30  * The unified bootloader passes us a pointer to a preserved copy of
   31  * bootstrap/kernel environment variables.
   32  * We make these available using sysctl for both in-kernel and
   33  * out-of-kernel consumers.
   34  *
   35  * Note that the current sysctl infrastructure doesn't allow 
   36  * dynamic insertion or traversal through handled spaces.  Grr.
   37  */
   38 
   39 #include <sys/param.h>
   40 #include <sys/kernel.h>
   41 #include <sys/systm.h>
   42 #include <sys/sysctl.h>
   43 #include <sys/libkern.h>
   44 
   45 char    *kern_envp;
   46 
   47 static char     *kernenv_next(char *cp);
   48 
   49 /*
   50  * Look up an environment variable by name.
   51  */
   52 char *
   53 getenv(const char *name)
   54 {
   55     char        *cp, *ep;
   56     int         len;
   57     
   58     for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
   59         for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
   60             ;
   61         if (*ep != '=')
   62             continue;
   63         len = ep - cp;
   64         ep++;
   65         if (!strncmp(name, cp, len) && name[len] == 0)
   66             return(ep);
   67     }
   68     return(NULL);
   69 }
   70 
   71 /*
   72  * Return a string value from an environment variable.
   73  */
   74 int
   75 getenv_string(const char *name, char *data, int size)
   76 {
   77     char *tmp;
   78 
   79     tmp = getenv(name);
   80     if (tmp != NULL) {
   81         strncpy(data, tmp, size);
   82         data[size - 1] = 0;
   83         return (1);
   84     } else
   85         return (0);
   86 }
   87 
   88 /*
   89  * Return an integer value from an environment variable.
   90  */
   91 int
   92 getenv_int(const char *name, int *data)
   93 {
   94     quad_t tmp;
   95     int rval;
   96 
   97     rval = getenv_quad(name, &tmp);
   98     if (rval) {
   99         *data = (int) tmp;
  100     }
  101     return (rval);
  102 }
  103 
  104 /*
  105  * Return a quad_t value from an environment variable.
  106  */
  107 int
  108 getenv_quad(const char *name, quad_t *data)
  109 {
  110     const char  *value;
  111     char        *vtp;
  112     quad_t      iv;
  113     
  114     if ((value = getenv(name)) == NULL)
  115         return(0);
  116     
  117     iv = strtoq(value, &vtp, 0);
  118     if ((vtp == value) || (*vtp != '\0'))
  119         return(0);
  120     
  121     *data = iv;
  122     return(1);
  123 }
  124 
  125 static int
  126 sysctl_kernenv(SYSCTL_HANDLER_ARGS)
  127 {
  128     int         *name = (int *)arg1;
  129     u_int       namelen = arg2;
  130     char        *cp;
  131     int         i, error;
  132 
  133     if (kern_envp == NULL)
  134         return(ENOENT);
  135     
  136     name++;
  137     namelen--;
  138     
  139     if (namelen != 1)
  140         return(EINVAL);
  141 
  142     cp = kern_envp;
  143     for (i = 0; i < name[0]; i++) {
  144         cp = kernenv_next(cp);
  145         if (cp == NULL)
  146             break;
  147     }
  148     
  149     if (cp == NULL)
  150         return(ENOENT);
  151     
  152     error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
  153     return (error);
  154 }
  155 
  156 SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kernenv, "kernel environment space");
  157 
  158 /*
  159  * Find the next entry after the one which (cp) falls within, return a
  160  * pointer to its start or NULL if there are no more.
  161  */
  162 static char *
  163 kernenv_next(char *cp)
  164 {
  165     if (cp != NULL) {
  166         while (*cp != 0)
  167             cp++;
  168         cp++;
  169         if (*cp == 0)
  170             cp = NULL;
  171     }
  172     return(cp);
  173 }
  174 
  175 void
  176 tunable_int_init(void *data)
  177 { 
  178         struct tunable_int *d = (struct tunable_int *)data;
  179 
  180         TUNABLE_INT_FETCH(d->path, d->var);
  181 }
  182 
  183 void
  184 tunable_quad_init(void *data)
  185 {
  186         struct tunable_quad *d = (struct tunable_quad *)data;
  187 
  188         TUNABLE_QUAD_FETCH(d->path, d->var);
  189 }
  190 
  191 void
  192 tunable_str_init(void *data)
  193 {
  194         struct tunable_str *d = (struct tunable_str *)data;
  195 
  196         TUNABLE_STR_FETCH(d->path, d->var, d->size);
  197 }
  198 

Cache object: 5a37d3a1af68f5f76a4c61edf79fabfc


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