Back to index

nagios-plugins  1.4.16
setenv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 1995-2003, 2005-2010 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    This program is free software: you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 #if !_LIBC
00018 # include <config.h>
00019 #endif
00020 
00021 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
00022    optimizes away the name == NULL test below.  */
00023 #define _GL_ARG_NONNULL(params)
00024 
00025 #include <alloca.h>
00026 
00027 /* Specification.  */
00028 #include <stdlib.h>
00029 
00030 #include <errno.h>
00031 #ifndef __set_errno
00032 # define __set_errno(ev) ((errno) = (ev))
00033 #endif
00034 
00035 #include <string.h>
00036 #if _LIBC || HAVE_UNISTD_H
00037 # include <unistd.h>
00038 #endif
00039 
00040 #if !_LIBC
00041 # include "malloca.h"
00042 #endif
00043 
00044 #if _LIBC || !HAVE_SETENV
00045 
00046 #if !_LIBC
00047 # define __environ      environ
00048 #endif
00049 
00050 #if _LIBC
00051 /* This lock protects against simultaneous modifications of `environ'.  */
00052 # include <bits/libc-lock.h>
00053 __libc_lock_define_initialized (static, envlock)
00054 # define LOCK   __libc_lock_lock (envlock)
00055 # define UNLOCK __libc_lock_unlock (envlock)
00056 #else
00057 # define LOCK
00058 # define UNLOCK
00059 #endif
00060 
00061 /* In the GNU C library we must keep the namespace clean.  */
00062 #ifdef _LIBC
00063 # define setenv __setenv
00064 # define clearenv __clearenv
00065 # define tfind __tfind
00066 # define tsearch __tsearch
00067 #else
00068 /* Use the system functions, not the gnulib overrides in this file.  */
00069 # undef malloc
00070 # undef realloc
00071 #endif
00072 
00073 /* In the GNU C library implementation we try to be more clever and
00074    allow arbitrarily many changes of the environment given that the used
00075    values are from a small set.  Outside glibc this will eat up all
00076    memory after a while.  */
00077 #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
00078                       && defined __GNUC__)
00079 # define USE_TSEARCH    1
00080 # include <search.h>
00081 typedef int (*compar_fn_t) (const void *, const void *);
00082 
00083 /* This is a pointer to the root of the search tree with the known
00084    values.  */
00085 static void *known_values;
00086 
00087 # define KNOWN_VALUE(Str) \
00088   ({                                                                          \
00089     void *value = tfind (Str, &known_values, (compar_fn_t) strcmp);           \
00090     value != NULL ? *(char **) value : NULL;                                  \
00091   })
00092 # define STORE_VALUE(Str) \
00093   tsearch (Str, &known_values, (compar_fn_t) strcmp)
00094 
00095 #else
00096 # undef USE_TSEARCH
00097 
00098 # define KNOWN_VALUE(Str) NULL
00099 # define STORE_VALUE(Str) do { } while (0)
00100 
00101 #endif
00102 
00103 
00104 /* If this variable is not a null pointer we allocated the current
00105    environment.  */
00106 static char **last_environ;
00107 
00108 
00109 /* This function is used by `setenv' and `putenv'.  The difference between
00110    the two functions is that for the former must create a new string which
00111    is then placed in the environment, while the argument of `putenv'
00112    must be used directly.  This is all complicated by the fact that we try
00113    to reuse values once generated for a `setenv' call since we can never
00114    free the strings.  */
00115 int
00116 __add_to_environ (const char *name, const char *value, const char *combined,
00117                   int replace)
00118 {
00119   char **ep;
00120   size_t size;
00121   const size_t namelen = strlen (name);
00122   const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
00123 
00124   LOCK;
00125 
00126   /* We have to get the pointer now that we have the lock and not earlier
00127      since another thread might have created a new environment.  */
00128   ep = __environ;
00129 
00130   size = 0;
00131   if (ep != NULL)
00132     {
00133       for (; *ep != NULL; ++ep)
00134         if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
00135           break;
00136         else
00137           ++size;
00138     }
00139 
00140   if (ep == NULL || *ep == NULL)
00141     {
00142       char **new_environ;
00143 #ifdef USE_TSEARCH
00144       char *new_value;
00145 #endif
00146 
00147       /* We allocated this space; we can extend it.  */
00148       new_environ =
00149         (char **) (last_environ == NULL
00150                    ? malloc ((size + 2) * sizeof (char *))
00151                    : realloc (last_environ, (size + 2) * sizeof (char *)));
00152       if (new_environ == NULL)
00153         {
00154           /* It's easier to set errno to ENOMEM than to rely on the
00155              'malloc-posix' and 'realloc-posix' gnulib modules.  */
00156           __set_errno (ENOMEM);
00157           UNLOCK;
00158           return -1;
00159         }
00160 
00161       /* If the whole entry is given add it.  */
00162       if (combined != NULL)
00163         /* We must not add the string to the search tree since it belongs
00164            to the user.  */
00165         new_environ[size] = (char *) combined;
00166       else
00167         {
00168           /* See whether the value is already known.  */
00169 #ifdef USE_TSEARCH
00170 # ifdef _LIBC
00171           new_value = (char *) alloca (namelen + 1 + vallen);
00172           __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
00173                      value, vallen);
00174 # else
00175           new_value = (char *) malloca (namelen + 1 + vallen);
00176           if (new_value == NULL)
00177             {
00178               __set_errno (ENOMEM);
00179               UNLOCK;
00180               return -1;
00181             }
00182           memcpy (new_value, name, namelen);
00183           new_value[namelen] = '=';
00184           memcpy (&new_value[namelen + 1], value, vallen);
00185 # endif
00186 
00187           new_environ[size] = KNOWN_VALUE (new_value);
00188           if (new_environ[size] == NULL)
00189 #endif
00190             {
00191               new_environ[size] = (char *) malloc (namelen + 1 + vallen);
00192               if (new_environ[size] == NULL)
00193                 {
00194 #if defined USE_TSEARCH && !defined _LIBC
00195                   freea (new_value);
00196 #endif
00197                   __set_errno (ENOMEM);
00198                   UNLOCK;
00199                   return -1;
00200                 }
00201 
00202 #ifdef USE_TSEARCH
00203               memcpy (new_environ[size], new_value, namelen + 1 + vallen);
00204 #else
00205               memcpy (new_environ[size], name, namelen);
00206               new_environ[size][namelen] = '=';
00207               memcpy (&new_environ[size][namelen + 1], value, vallen);
00208 #endif
00209               /* And save the value now.  We cannot do this when we remove
00210                  the string since then we cannot decide whether it is a
00211                  user string or not.  */
00212               STORE_VALUE (new_environ[size]);
00213             }
00214 #if defined USE_TSEARCH && !defined _LIBC
00215           freea (new_value);
00216 #endif
00217         }
00218 
00219       if (__environ != last_environ)
00220         memcpy ((char *) new_environ, (char *) __environ,
00221                 size * sizeof (char *));
00222 
00223       new_environ[size + 1] = NULL;
00224 
00225       last_environ = __environ = new_environ;
00226     }
00227   else if (replace)
00228     {
00229       char *np;
00230 
00231       /* Use the user string if given.  */
00232       if (combined != NULL)
00233         np = (char *) combined;
00234       else
00235         {
00236 #ifdef USE_TSEARCH
00237           char *new_value;
00238 # ifdef _LIBC
00239           new_value = alloca (namelen + 1 + vallen);
00240           __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
00241                      value, vallen);
00242 # else
00243           new_value = malloca (namelen + 1 + vallen);
00244           if (new_value == NULL)
00245             {
00246               __set_errno (ENOMEM);
00247               UNLOCK;
00248               return -1;
00249             }
00250           memcpy (new_value, name, namelen);
00251           new_value[namelen] = '=';
00252           memcpy (&new_value[namelen + 1], value, vallen);
00253 # endif
00254 
00255           np = KNOWN_VALUE (new_value);
00256           if (np == NULL)
00257 #endif
00258             {
00259               np = (char *) malloc (namelen + 1 + vallen);
00260               if (np == NULL)
00261                 {
00262 #if defined USE_TSEARCH && !defined _LIBC
00263                   freea (new_value);
00264 #endif
00265                   __set_errno (ENOMEM);
00266                   UNLOCK;
00267                   return -1;
00268                 }
00269 
00270 #ifdef USE_TSEARCH
00271               memcpy (np, new_value, namelen + 1 + vallen);
00272 #else
00273               memcpy (np, name, namelen);
00274               np[namelen] = '=';
00275               memcpy (&np[namelen + 1], value, vallen);
00276 #endif
00277               /* And remember the value.  */
00278               STORE_VALUE (np);
00279             }
00280 #if defined USE_TSEARCH && !defined _LIBC
00281           freea (new_value);
00282 #endif
00283         }
00284 
00285       *ep = np;
00286     }
00287 
00288   UNLOCK;
00289 
00290   return 0;
00291 }
00292 
00293 int
00294 setenv (const char *name, const char *value, int replace)
00295 {
00296   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
00297     {
00298       __set_errno (EINVAL);
00299       return -1;
00300     }
00301 
00302   return __add_to_environ (name, value, NULL, replace);
00303 }
00304 
00305 /* The `clearenv' was planned to be added to POSIX.1 but probably
00306    never made it.  Nevertheless the POSIX.9 standard (POSIX bindings
00307    for Fortran 77) requires this function.  */
00308 int
00309 clearenv (void)
00310 {
00311   LOCK;
00312 
00313   if (__environ == last_environ && __environ != NULL)
00314     {
00315       /* We allocated this environment so we can free it.  */
00316       free (__environ);
00317       last_environ = NULL;
00318     }
00319 
00320   /* Clear the environment pointer removes the whole environment.  */
00321   __environ = NULL;
00322 
00323   UNLOCK;
00324 
00325   return 0;
00326 }
00327 
00328 #ifdef _LIBC
00329 static void
00330 free_mem (void)
00331 {
00332   /* Remove all traces.  */
00333   clearenv ();
00334 
00335   /* Now remove the search tree.  */
00336   __tdestroy (known_values, free);
00337   known_values = NULL;
00338 }
00339 text_set_element (__libc_subfreeres, free_mem);
00340 
00341 
00342 # undef setenv
00343 # undef clearenv
00344 weak_alias (__setenv, setenv)
00345 weak_alias (__clearenv, clearenv)
00346 #endif
00347 
00348 #endif /* _LIBC || !HAVE_SETENV */
00349 
00350 /* The rest of this file is called into use when replacing an existing
00351    but buggy setenv.  Known bugs include failure to diagnose invalid
00352    name, and consuming a leading '=' from value.  */
00353 #if HAVE_SETENV
00354 
00355 # undef setenv
00356 # define STREQ(a, b) (strcmp (a, b) == 0)
00357 
00358 int
00359 rpl_setenv (const char *name, const char *value, int replace)
00360 {
00361   int result;
00362   if (!name || !*name || strchr (name, '='))
00363     {
00364       errno = EINVAL;
00365       return -1;
00366     }
00367   /* Call the real setenv even if replace is 0, in case implementation
00368      has underlying data to update, such as when environ changes.  */
00369   result = setenv (name, value, replace);
00370   if (result == 0 && replace && *value == '=')
00371     {
00372       char *tmp = getenv (name);
00373       if (!STREQ (tmp, value))
00374         {
00375           int saved_errno;
00376           size_t len = strlen (value);
00377           tmp = malloca (len + 2);
00378           /* Since leading '=' is eaten, double it up.  */
00379           *tmp = '=';
00380           memcpy (tmp + 1, value, len + 1);
00381           result = setenv (name, tmp, replace);
00382           saved_errno = errno;
00383           freea (tmp);
00384           errno = saved_errno;
00385         }
00386     }
00387   return result;
00388 }
00389 
00390 #endif /* HAVE_SETENV */