setenv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (C) 1992-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. /* Pacify GCC; see the commentary about VALLEN below. This is needed
  18. at least through GCC 4.9.2. Pacify GCC for the entire file, as
  19. there seems to be no way to pacify GCC selectively, only for the
  20. place where it's needed. Do not use DIAG_IGNORE_NEEDS_COMMENT
  21. here, as it's not defined yet. */
  22. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  23. #include <errno.h>
  24. #if !_LIBC
  25. # if !defined errno && !defined HAVE_ERRNO_DECL
  26. extern int errno;
  27. # endif
  28. # define __set_errno(ev) ((errno) = (ev))
  29. #endif
  30. #if _LIBC || HAVE_STDLIB_H
  31. # include <stdlib.h>
  32. #endif
  33. #if _LIBC || HAVE_STRING_H
  34. # include <string.h>
  35. #endif
  36. #if _LIBC || HAVE_UNISTD_H
  37. # include <unistd.h>
  38. #endif
  39. #if !_LIBC
  40. # define __environ environ
  41. # ifndef HAVE_ENVIRON_DECL
  42. extern char **environ;
  43. # endif
  44. #endif
  45. #if _LIBC
  46. /* This lock protects against simultaneous modifications of `environ'. */
  47. # include <libc-lock.h>
  48. __libc_lock_define_initialized (static, envlock)
  49. # define LOCK __libc_lock_lock (envlock)
  50. # define UNLOCK __libc_lock_unlock (envlock)
  51. #else
  52. # define LOCK
  53. # define UNLOCK
  54. #endif
  55. /* In the GNU C library we must keep the namespace clean. */
  56. #ifdef _LIBC
  57. # define setenv __setenv
  58. # define unsetenv __unsetenv
  59. # define clearenv __clearenv
  60. # define tfind __tfind
  61. # define tsearch __tsearch
  62. #endif
  63. /* In the GNU C library implementation we try to be more clever and
  64. allow arbitrarily many changes of the environment given that the used
  65. values are from a small set. Outside glibc this will eat up all
  66. memory after a while. */
  67. #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
  68. && defined __GNUC__)
  69. # define USE_TSEARCH 1
  70. # include <search.h>
  71. /* This is a pointer to the root of the search tree with the known
  72. values. */
  73. static void *known_values;
  74. # define KNOWN_VALUE(Str) \
  75. ({ \
  76. void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \
  77. value != NULL ? *(char **) value : NULL; \
  78. })
  79. # define STORE_VALUE(Str) \
  80. tsearch (Str, &known_values, (__compar_fn_t) strcmp)
  81. #else
  82. # undef USE_TSEARCH
  83. # define KNOWN_VALUE(Str) NULL
  84. # define STORE_VALUE(Str) do { } while (0)
  85. #endif
  86. /* If this variable is not a null pointer we allocated the current
  87. environment. */
  88. static char **last_environ;
  89. /* This function is used by `setenv' and `putenv'. The difference between
  90. the two functions is that for the former must create a new string which
  91. is then placed in the environment, while the argument of `putenv'
  92. must be used directly. This is all complicated by the fact that we try
  93. to reuse values once generated for a `setenv' call since we can never
  94. free the strings. */
  95. int
  96. __add_to_environ (const char *name, const char *value, const char *combined,
  97. int replace)
  98. {
  99. char **ep;
  100. size_t size;
  101. /* Compute lengths before locking, so that the critical section is
  102. less of a performance bottleneck. VALLEN is needed only if
  103. COMBINED is null (unfortunately GCC is not smart enough to deduce
  104. this; see the #pragma at the start of this file). Testing
  105. COMBINED instead of VALUE causes setenv (..., NULL, ...) to dump
  106. core now instead of corrupting memory later. */
  107. const size_t namelen = strlen (name);
  108. size_t vallen;
  109. if (combined == NULL)
  110. vallen = strlen (value) + 1;
  111. LOCK;
  112. /* We have to get the pointer now that we have the lock and not earlier
  113. since another thread might have created a new environment. */
  114. ep = __environ;
  115. size = 0;
  116. if (ep != NULL)
  117. {
  118. for (; *ep != NULL; ++ep)
  119. if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  120. break;
  121. else
  122. ++size;
  123. }
  124. if (ep == NULL || __builtin_expect (*ep == NULL, 1))
  125. {
  126. char **new_environ;
  127. /* We allocated this space; we can extend it. */
  128. new_environ = (char **) realloc (last_environ,
  129. (size + 2) * sizeof (char *));
  130. if (new_environ == NULL)
  131. {
  132. UNLOCK;
  133. return -1;
  134. }
  135. if (__environ != last_environ)
  136. memcpy ((char *) new_environ, (char *) __environ,
  137. size * sizeof (char *));
  138. new_environ[size] = NULL;
  139. new_environ[size + 1] = NULL;
  140. ep = new_environ + size;
  141. last_environ = __environ = new_environ;
  142. }
  143. if (*ep == NULL || replace)
  144. {
  145. char *np;
  146. /* Use the user string if given. */
  147. if (combined != NULL)
  148. np = (char *) combined;
  149. else
  150. {
  151. const size_t varlen = namelen + 1 + vallen;
  152. #ifdef USE_TSEARCH
  153. char *new_value;
  154. int use_alloca = __libc_use_alloca (varlen);
  155. if (__builtin_expect (use_alloca, 1))
  156. new_value = (char *) alloca (varlen);
  157. else
  158. {
  159. new_value = malloc (varlen);
  160. if (new_value == NULL)
  161. {
  162. UNLOCK;
  163. return -1;
  164. }
  165. }
  166. # ifdef _LIBC
  167. __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
  168. value, vallen);
  169. # else
  170. memcpy (new_value, name, namelen);
  171. new_value[namelen] = '=';
  172. memcpy (&new_value[namelen + 1], value, vallen);
  173. # endif
  174. np = KNOWN_VALUE (new_value);
  175. if (__glibc_likely (np == NULL))
  176. #endif
  177. {
  178. #ifdef USE_TSEARCH
  179. if (__glibc_unlikely (! use_alloca))
  180. np = new_value;
  181. else
  182. #endif
  183. {
  184. np = malloc (varlen);
  185. if (__glibc_unlikely (np == NULL))
  186. {
  187. UNLOCK;
  188. return -1;
  189. }
  190. #ifdef USE_TSEARCH
  191. memcpy (np, new_value, varlen);
  192. #else
  193. memcpy (np, name, namelen);
  194. np[namelen] = '=';
  195. memcpy (&np[namelen + 1], value, vallen);
  196. #endif
  197. }
  198. /* And remember the value. */
  199. STORE_VALUE (np);
  200. }
  201. #ifdef USE_TSEARCH
  202. else
  203. {
  204. if (__glibc_unlikely (! use_alloca))
  205. free (new_value);
  206. }
  207. #endif
  208. }
  209. *ep = np;
  210. }
  211. UNLOCK;
  212. return 0;
  213. }
  214. int
  215. setenv (const char *name, const char *value, int replace)
  216. {
  217. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
  218. {
  219. __set_errno (EINVAL);
  220. return -1;
  221. }
  222. return __add_to_environ (name, value, NULL, replace);
  223. }
  224. int
  225. unsetenv (const char *name)
  226. {
  227. size_t len;
  228. char **ep;
  229. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
  230. {
  231. __set_errno (EINVAL);
  232. return -1;
  233. }
  234. len = strlen (name);
  235. LOCK;
  236. ep = __environ;
  237. if (ep != NULL)
  238. while (*ep != NULL)
  239. {
  240. if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
  241. {
  242. /* Found it. Remove this pointer by moving later ones back. */
  243. char **dp = ep;
  244. do
  245. dp[0] = dp[1];
  246. while (*dp++);
  247. /* Continue the loop in case NAME appears again. */
  248. }
  249. else
  250. ++ep;
  251. }
  252. UNLOCK;
  253. return 0;
  254. }
  255. /* The `clearenv' was planned to be added to POSIX.1 but probably
  256. never made it. Nevertheless the POSIX.9 standard (POSIX bindings
  257. for Fortran 77) requires this function. */
  258. int
  259. clearenv (void)
  260. {
  261. LOCK;
  262. if (__environ == last_environ && __environ != NULL)
  263. {
  264. /* We allocated this environment so we can free it. */
  265. free (__environ);
  266. last_environ = NULL;
  267. }
  268. /* Clear the environment pointer removes the whole environment. */
  269. __environ = NULL;
  270. UNLOCK;
  271. return 0;
  272. }
  273. #ifdef _LIBC
  274. libc_freeres_fn (free_mem)
  275. {
  276. /* Remove all traces. */
  277. clearenv ();
  278. /* Now remove the search tree. */
  279. __tdestroy (known_values, free);
  280. known_values = NULL;
  281. }
  282. # undef setenv
  283. # undef unsetenv
  284. # undef clearenv
  285. weak_alias (__setenv, setenv)
  286. weak_alias (__unsetenv, unsetenv)
  287. weak_alias (__clearenv, clearenv)
  288. #endif