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 
   27 /*
   28  * The unified bootloader passes us a pointer to a preserved copy of
   29  * bootstrap/kernel environment variables.  We convert them to a
   30  * dynamic array of strings later when the VM subsystem is up.
   31  *
   32  * We make these available through the kenv(2) syscall for userland
   33  * and through getenv()/freeenv() setenv() unsetenv() testenv() for
   34  * the kernel.
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD$");
   39 
   40 #include "opt_mac.h"
   41 
   42 #include <sys/types.h>
   43 #include <sys/param.h>
   44 #include <sys/proc.h>
   45 #include <sys/queue.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mutex.h>
   50 #include <sys/kernel.h>
   51 #include <sys/sx.h>
   52 #include <sys/systm.h>
   53 #include <sys/sysent.h>
   54 #include <sys/sysproto.h>
   55 #include <sys/libkern.h>
   56 #include <sys/kenv.h>
   57 
   58 MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
   59 
   60 #define KENV_SIZE       512     /* Maximum number of environment strings */
   61 
   62 /* pointer to the static environment */
   63 char            *kern_envp;
   64 static char     *kernenv_next(char *);
   65 
   66 /* dynamic environment variables */
   67 char            **kenvp;
   68 struct sx       kenv_lock;
   69 
   70 /*
   71  * No need to protect this with a mutex
   72  * since SYSINITS are single threaded.
   73  */
   74 int     dynamic_kenv = 0;
   75 
   76 #define KENV_CHECK      if (!dynamic_kenv) \
   77                             panic("%s: called before SI_SUB_KMEM", __func__)
   78 
   79 int
   80 kenv(td, uap)
   81         struct thread *td;
   82         struct kenv_args /* {
   83                 int what;
   84                 const char *name;
   85                 char *value;
   86                 int len;
   87         } */ *uap;
   88 {
   89         char *name, *value;
   90         size_t len, done, needed;
   91         int error, i;
   92 
   93         KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
   94 
   95         error = 0;
   96         if (uap->what == KENV_DUMP) {
   97 #ifdef MAC
   98                 error = mac_check_kenv_dump(td->td_ucred);
   99                 if (error)
  100                         return (error);
  101 #endif
  102                 done = needed = 0;
  103                 sx_slock(&kenv_lock);
  104                 for (i = 0; kenvp[i] != NULL; i++) {
  105                         len = strlen(kenvp[i]) + 1;
  106                         needed += len;
  107                         len = min(len, uap->len - done);
  108                         /*
  109                          * If called with a NULL or insufficiently large
  110                          * buffer, just keep computing the required size.
  111                          */
  112                         if (uap->value != NULL && len > 0) {
  113                                 error = copyout(kenvp[i], uap->value + done,
  114                                     len);
  115                                 if (error)
  116                                         break;
  117                                 done += len;
  118                         }
  119                 }
  120                 sx_sunlock(&kenv_lock);
  121                 td->td_retval[0] = ((done == needed) ? 0 : needed);
  122                 return (error);
  123         }
  124 
  125         if ((uap->what == KENV_SET) ||
  126             (uap->what == KENV_UNSET)) {
  127                 error = suser(td);
  128                 if (error)
  129                         return (error);
  130         }
  131 
  132         name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
  133 
  134         error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
  135         if (error)
  136                 goto done;
  137 
  138         switch (uap->what) {
  139         case KENV_GET:
  140 #ifdef MAC
  141                 error = mac_check_kenv_get(td->td_ucred, name);
  142                 if (error)
  143                         goto done;
  144 #endif
  145                 value = getenv(name);
  146                 if (value == NULL) {
  147                         error = ENOENT;
  148                         goto done;
  149                 }
  150                 len = strlen(value) + 1;
  151                 if (len > uap->len)
  152                         len = uap->len;
  153                 error = copyout(value, uap->value, len);
  154                 freeenv(value);
  155                 if (error)
  156                         goto done;
  157                 td->td_retval[0] = len;
  158                 break;
  159         case KENV_SET:
  160                 len = uap->len;
  161                 if (len < 1) {
  162                         error = EINVAL;
  163                         goto done;
  164                 }
  165                 if (len > KENV_MVALLEN)
  166                         len = KENV_MVALLEN;
  167                 value = malloc(len, M_TEMP, M_WAITOK);
  168                 error = copyinstr(uap->value, value, len, NULL);
  169                 if (error) {
  170                         free(value, M_TEMP);
  171                         goto done;
  172                 }
  173 #ifdef MAC
  174                 error = mac_check_kenv_set(td->td_ucred, name, value);
  175                 if (error == 0)
  176 #endif
  177                         setenv(name, value);
  178                 free(value, M_TEMP);
  179                 break;
  180         case KENV_UNSET:
  181 #ifdef MAC
  182                 error = mac_check_kenv_unset(td->td_ucred, name);
  183                 if (error)
  184                         goto done;
  185 #endif
  186                 error = unsetenv(name);
  187                 if (error)
  188                         error = ENOENT;
  189                 break;
  190         default:
  191                 error = EINVAL;
  192                 break;
  193         }
  194 done:
  195         free(name, M_TEMP);
  196         return (error);
  197 }
  198 
  199 /*
  200  * Setup the dynamic kernel environment.
  201  */
  202 static void
  203 init_dynamic_kenv(void *data __unused)
  204 {
  205         char *cp;
  206         int len, i;
  207 
  208         kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
  209                 M_WAITOK | M_ZERO);
  210         i = 0;
  211         for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
  212                 len = strlen(cp) + 1;
  213                 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
  214                 strcpy(kenvp[i++], cp);
  215         }
  216         kenvp[i] = NULL;
  217 
  218         sx_init(&kenv_lock, "kernel environment");
  219         dynamic_kenv = 1;
  220 }
  221 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
  222 
  223 void
  224 freeenv(char *env)
  225 {
  226 
  227         if (dynamic_kenv)
  228                 free(env, M_KENV);
  229 }
  230 
  231 /*
  232  * Internal functions for string lookup.
  233  */
  234 static char *
  235 _getenv_dynamic(const char *name, int *idx)
  236 {
  237         char *cp;
  238         int len, i;
  239 
  240         sx_assert(&kenv_lock, SX_LOCKED);
  241         len = strlen(name);
  242         for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
  243                 if ((strncmp(cp, name, len) == 0) &&
  244                     (cp[len] == '=')) {
  245                         if (idx != NULL)
  246                                 *idx = i;
  247                         return (cp + len + 1);
  248                 }
  249         }
  250         return (NULL);
  251 }
  252 
  253 static char *
  254 _getenv_static(const char *name)
  255 {
  256         char *cp, *ep;
  257         int len;
  258 
  259         for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
  260                 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
  261                         ;
  262                 if (*ep != '=')
  263                         continue;
  264                 len = ep - cp;
  265                 ep++;
  266                 if (!strncmp(name, cp, len) && name[len] == 0)
  267                         return (ep);
  268         }
  269         return (NULL);
  270 }
  271 
  272 /*
  273  * Look up an environment variable by name.
  274  * Return a pointer to the string if found.
  275  * The pointer has to be freed with freeenv()
  276  * after use.
  277  */
  278 char *
  279 getenv(const char *name)
  280 {
  281         char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
  282         char *ret, *cp;
  283         int len;
  284 
  285         if (dynamic_kenv) {
  286                 sx_slock(&kenv_lock);
  287                 cp = _getenv_dynamic(name, NULL);
  288                 if (cp != NULL) {
  289                         strcpy(buf, cp);
  290                         sx_sunlock(&kenv_lock);
  291                         len = strlen(buf) + 1;
  292                         ret = malloc(len, M_KENV, M_WAITOK);
  293                         strcpy(ret, buf);
  294                 } else {
  295                         sx_sunlock(&kenv_lock);
  296                         ret = NULL;
  297                 }
  298         } else
  299                 ret = _getenv_static(name);
  300         return (ret);
  301 }
  302 
  303 /*
  304  * Test if an environment variable is defined.
  305  */
  306 int
  307 testenv(const char *name)
  308 {
  309         char *cp;
  310 
  311         if (dynamic_kenv) {
  312                 sx_slock(&kenv_lock);
  313                 cp = _getenv_dynamic(name, NULL);
  314                 sx_sunlock(&kenv_lock);
  315         } else
  316                 cp = _getenv_static(name);
  317         if (cp != NULL)
  318                 return (1);
  319         return (0);
  320 }
  321 
  322 /*
  323  * Set an environment variable by name.
  324  */
  325 int
  326 setenv(const char *name, const char *value)
  327 {
  328         char *buf, *cp, *oldenv;
  329         int namelen, vallen, i;
  330 
  331         KENV_CHECK;
  332 
  333         namelen = strlen(name) + 1;
  334         if (namelen > KENV_MNAMELEN)
  335                 return (-1);
  336         vallen = strlen(value) + 1;
  337         if (vallen > KENV_MVALLEN)
  338                 return (-1);
  339         buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
  340         sprintf(buf, "%s=%s", name, value);
  341 
  342         sx_xlock(&kenv_lock);
  343         cp = _getenv_dynamic(name, &i);
  344         if (cp != NULL) {
  345                 oldenv = kenvp[i];
  346                 kenvp[i] = buf;
  347                 sx_xunlock(&kenv_lock);
  348                 free(oldenv, M_KENV);
  349         } else {
  350                 /* We add the option if it wasn't found */
  351                 for (i = 0; (cp = kenvp[i]) != NULL; i++)
  352                         ;
  353                 /* Bounds checking */
  354                 if (i < 0 || i >= KENV_SIZE) {
  355                         free(buf, M_KENV);
  356                         sx_xunlock(&kenv_lock);
  357                         return (-1);
  358                 }
  359                 kenvp[i] = buf;
  360                 kenvp[i + 1] = NULL;
  361                 sx_xunlock(&kenv_lock);
  362         }
  363         return (0);
  364 }
  365 
  366 /*
  367  * Unset an environment variable string.
  368  */
  369 int
  370 unsetenv(const char *name)
  371 {
  372         char *cp, *oldenv;
  373         int i, j;
  374 
  375         KENV_CHECK;
  376 
  377         sx_xlock(&kenv_lock);
  378         cp = _getenv_dynamic(name, &i);
  379         if (cp != NULL) {
  380                 oldenv = kenvp[i];
  381                 for (j = i + 1; kenvp[j] != NULL; j++)
  382                         kenvp[i++] = kenvp[j];
  383                 kenvp[i] = NULL;
  384                 sx_xunlock(&kenv_lock);
  385                 free(oldenv, M_KENV);
  386                 return (0);
  387         }
  388         sx_xunlock(&kenv_lock);
  389         return (-1);
  390 }
  391 
  392 /*
  393  * Return a string value from an environment variable.
  394  */
  395 int
  396 getenv_string(const char *name, char *data, int size)
  397 {
  398         char *tmp;
  399 
  400         tmp = getenv(name);
  401         if (tmp != NULL) {
  402                 strlcpy(data, tmp, size);
  403                 freeenv(tmp);
  404                 return (1);
  405         } else
  406                 return (0);
  407 }
  408 
  409 /*
  410  * Return an integer value from an environment variable.
  411  */
  412 int
  413 getenv_int(const char *name, int *data)
  414 {
  415         quad_t tmp;
  416         int rval;
  417 
  418         rval = getenv_quad(name, &tmp);
  419         if (rval)
  420                 *data = (int) tmp;
  421         return (rval);
  422 }
  423 
  424 /*
  425  * Return a long value from an environment variable.
  426  */
  427 long
  428 getenv_long(const char *name, long *data)
  429 {
  430         quad_t tmp;
  431         long rval;
  432 
  433         rval = getenv_quad(name, &tmp);
  434         if (rval)
  435                 *data = (long) tmp;
  436         return (rval);
  437 }
  438 
  439 /*
  440  * Return an unsigned long value from an environment variable.
  441  */
  442 unsigned long
  443 getenv_ulong(const char *name, unsigned long *data)
  444 {
  445         quad_t tmp;
  446         long rval;
  447 
  448         rval = getenv_quad(name, &tmp);
  449         if (rval)
  450                 *data = (unsigned long) tmp;
  451         return (rval);
  452 }
  453 
  454 /*
  455  * Return a quad_t value from an environment variable.
  456  */
  457 int
  458 getenv_quad(const char *name, quad_t *data)
  459 {
  460         char    *value;
  461         char    *vtp;
  462         quad_t  iv;
  463 
  464         value = getenv(name);
  465         if (value == NULL)
  466                 return (0);
  467         iv = strtoq(value, &vtp, 0);
  468         if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
  469                 freeenv(value);
  470                 return (0);
  471         }
  472         switch (vtp[0]) {
  473         case 't': case 'T':
  474                 iv *= 1024;
  475         case 'g': case 'G':
  476                 iv *= 1024;
  477         case 'm': case 'M':
  478                 iv *= 1024;
  479         case 'k': case 'K':
  480                 iv *= 1024;
  481         case '\0':
  482                 break;
  483         default:
  484                 freeenv(value);
  485                 return (0);
  486         }
  487         *data = iv;
  488         freeenv(value);
  489         return (1);
  490 }
  491 
  492 /*
  493  * Find the next entry after the one which (cp) falls within, return a
  494  * pointer to its start or NULL if there are no more.
  495  */
  496 static char *
  497 kernenv_next(char *cp)
  498 {
  499 
  500         if (cp != NULL) {
  501                 while (*cp != 0)
  502                         cp++;
  503                 cp++;
  504                 if (*cp == 0)
  505                         cp = NULL;
  506         }
  507         return (cp);
  508 }
  509 
  510 void
  511 tunable_int_init(void *data)
  512 {
  513         struct tunable_int *d = (struct tunable_int *)data;
  514 
  515         TUNABLE_INT_FETCH(d->path, d->var);
  516 }
  517 
  518 void
  519 tunable_long_init(void *data)
  520 {
  521         struct tunable_long *d = (struct tunable_long *)data;
  522 
  523         TUNABLE_LONG_FETCH(d->path, d->var);
  524 }
  525 
  526 void
  527 tunable_ulong_init(void *data)
  528 {
  529         struct tunable_ulong *d = (struct tunable_ulong *)data;
  530 
  531         TUNABLE_ULONG_FETCH(d->path, d->var);
  532 }
  533 
  534 void
  535 tunable_quad_init(void *data)
  536 {
  537         struct tunable_quad *d = (struct tunable_quad *)data;
  538 
  539         TUNABLE_QUAD_FETCH(d->path, d->var);
  540 }
  541 
  542 void
  543 tunable_str_init(void *data)
  544 {
  545         struct tunable_str *d = (struct tunable_str *)data;
  546 
  547         TUNABLE_STR_FETCH(d->path, d->var, d->size);
  548 }

Cache object: fe14fa7e4302661e43916b5dc6c343e8


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