diff options
author | scf <scf@FreeBSD.org> | 2007-07-04 00:00:41 +0000 |
---|---|---|
committer | scf <scf@FreeBSD.org> | 2007-07-04 00:00:41 +0000 |
commit | 196b6346ba4e13a3f7679e2de3317b6aa65983df (patch) | |
tree | 423c7d016f87f6541b9ef8231a14f8b267bc5d5e /lib/libc/stdlib/getenv.c | |
parent | af5bbfbc7b6a610f0c44a56a7b2d81b96be9b1b5 (diff) | |
download | FreeBSD-src-196b6346ba4e13a3f7679e2de3317b6aa65983df.zip FreeBSD-src-196b6346ba4e13a3f7679e2de3317b6aa65983df.tar.gz |
Significantly reduce the memory leak as noted in BUGS section for
setenv(3) by tracking the size of the memory allocated instead of using
strlen() on the current value.
Convert all calls to POSIX from historic BSD API:
- unsetenv returns an int.
- putenv takes a char * instead of const char *.
- putenv no longer makes a copy of the input string.
- errno is set appropriately for POSIX. Exceptions involve bad environ
variable and internal initialization code. These both set errno to
EFAULT.
Several patches to base utilities to handle the POSIX changes from
Andrey Chernov's previous commit. A few I re-wrote to use setenv()
instead of putenv().
New regression module for tools/regression/environ to test these
functions. It also can be used to test the performance.
Bump __FreeBSD_version to 700050 due to API change.
PR: kern/99826
Approved by: wes
Approved by: re (kensmith)
Diffstat (limited to 'lib/libc/stdlib/getenv.c')
-rw-r--r-- | lib/libc/stdlib/getenv.c | 597 |
1 files changed, 538 insertions, 59 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 306b6a1..5f6f497 100644 --- a/lib/libc/stdlib/getenv.c +++ b/lib/libc/stdlib/getenv.c @@ -1,89 +1,568 @@ -/* - * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. +/*- + * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)getenv.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/types.h> +#include <err.h> +#include <errno.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + + #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); -#include <stdlib.h> -#include <stddef.h> -#include <string.h> -inline char *__findenv(const char *, int *); +/* + * Standard environ. environ variable is exposed to entire process. + * + * origEnviron: Upon cleanup on unloading of library or failure, this + * allows environ to return to as it was before. + * environSize: Number of variables environ can hold. Can only + * increase. + */ +extern char **environ; +static char **origEnviron; +static int environSize = 0; + +/* + * Array of environment variables built from environ. Each element records: + * name: Pointer to name=value string + * name length: Length of name not counting '=' character + * value: Pointer to value within same string as name + * value size: Size (not length) of space for value not counting the + * nul character + * active state: true/false value to signify whether variable is active. + * Useful since multiple variables with the same name can + * co-exist. At most, one variable can be active at any + * one time. + * putenv: Created from putenv() call. This memory must not be + * reused. + */ +static struct envVars { + size_t nameLen; + size_t valueSize; + char *name; + char *value; + bool active; + bool putenv; +} *envVars = NULL; /* - * __findenv -- - * Returns pointer to value associated with name, if any, else NULL. - * Sets offset to be the offset of the name/value combination in the - * environmental array, for use by setenv(3) and unsetenv(3). - * Explicitly removes '=' in argument name. + * Environment array information. * - * This routine *should* be a static; don't use it. + * envActive: Number of active variables in array. + * envVarsSize: Size of array. + * envVarsTotal: Number of total variables in array (active or not). + */ +static int envActive = 0; +static int envVarsSize = 0; +static int envVarsTotal = 0; + + +/* Deinitialization of new environment. */ +static void __attribute__ ((destructor)) __clean_env(void); + + +/* + * Inline strlen() for performance. Also, perform check for an equals sign. + * Cheaper here than peforming a strchr() later. + */ +static inline size_t +__strleneq(const char *str) +{ + const char *s; + + for (s = str; *s != '\0'; ++s) + if (*s == '=') + return (0); + + return (s - str); +} + + +/* + * Comparison of an environment name=value to a name. + */ +static inline bool +strncmpeq(const char *nameValue, const char *name, size_t nameLen) +{ + if (strncmp(nameValue, name, nameLen) == 0 && nameValue[nameLen] == '=') + return (true); + + return (false); +} + + +/* + * Using environment, returns pointer to value associated with name, if any, + * else NULL. If the onlyActive flag is set to true, only variables that are + * active are returned else all are. + */ +static inline char * +__findenv(const char *name, size_t nameLen, int *envNdx, bool onlyActive) +{ + int ndx; + + /* + * Find environment variable from end of array (more likely to be + * active). A variable created by putenv is always active or it is not + * tracked in the array. + */ + for (ndx = *envNdx; ndx >= 0; ndx--) + if (envVars[ndx].putenv) { + if (strncmpeq(envVars[ndx].name, name, nameLen)) { + *envNdx = ndx; + return (envVars[ndx].name + nameLen + + sizeof ("=") - 1); + } + } else if ((!onlyActive || envVars[ndx].active) && + (envVars[ndx].nameLen == nameLen && + strncmpeq(envVars[ndx].name, name, nameLen))) { + *envNdx = ndx; + return (envVars[ndx].value); + } + + return (NULL); +} + + +/* + * Using environ, returns pointer to value associated with name, if any, else + * NULL. Used on the original environ passed into the program. */ -inline char * -__findenv(name, offset) - const char *name; - int *offset; +static char * +__findenv_environ(const char *name, size_t nameLen) { - extern char **environ; - int len, i; - const char *np; - char **p, *cp; + int envNdx; - if (name == NULL || environ == NULL) + /* Check for non-existant environment. */ + if (environ == NULL) return (NULL); - for (np = name; *np && *np != '='; ++np) - continue; - len = np - name; - for (p = environ; (cp = *p) != NULL; ++p) { - for (np = name, i = len; i && *cp; i--) - if (*cp++ != *np++) - break; - if (i == 0 && *cp++ == '=') { - *offset = p - environ; - return (cp); + + /* Find variable within environ. */ + for (envNdx = 0; environ[envNdx] != NULL; envNdx++) + if (strncmpeq(environ[envNdx], name, nameLen)) + return (&(environ[envNdx][nameLen + sizeof("=") - 1])); + + return (NULL); +} + + +/* + * Using the environment, rebuild the environ array for use by other C library + * calls that depend upon it. + */ +static int +__rebuild_environ(int newEnvironSize) +{ + char **tmpEnviron; + int envNdx; + int environNdx; + int tmpEnvironSize; + + /* Resize environ. */ + if (newEnvironSize > environSize) { + tmpEnvironSize = newEnvironSize * 2; + tmpEnviron = realloc(environ, sizeof (*environ) * + (tmpEnvironSize + 1)); + if (tmpEnviron == NULL) + return (-1); + environSize = tmpEnvironSize; + environ = tmpEnviron; + } + envActive = newEnvironSize; + + /* Assign active variables to environ. */ + for (envNdx = envVarsTotal - 1, environNdx = 0; envNdx >= 0; envNdx--) + if (envVars[envNdx].active) + environ[environNdx++] = envVars[envNdx].name; + environ[environNdx] = NULL; + + return (0); +} + + +/* + * Enlarge new environment. + */ +static inline bool +__enlarge_env(void) +{ + int newEnvVarsSize; + struct envVars *tmpEnvVars; + + envVarsTotal++; + if (envVarsTotal > envVarsSize) { + newEnvVarsSize = envVarsTotal * 2; + tmpEnvVars = realloc(envVars, sizeof (*envVars) * + newEnvVarsSize); + if (tmpEnvVars == NULL) { + envVarsTotal--; + return (false); } + envVarsSize = newEnvVarsSize; + envVars = tmpEnvVars; } - return (NULL); + + return (true); +} + + +/* + * Using environ, build an environment for use by standard C library calls. + */ +static int +__build_env(void) +{ + char **env; + int activeNdx; + int envNdx; + int rtrnVal; + int savedErrno; + size_t nameLen; + + /* Check for non-existant environment. */ + if (environ == NULL) + return (0); + if (environ[0] == NULL) + goto SaveEnviron; + + /* Count environment variables. */ + for (env = environ, envVarsTotal = 0; *env != NULL; env++) + envVarsTotal++; + envVarsSize = envVarsTotal * 2; + + /* Create new environment. */ + envVars = calloc(1, sizeof (*envVars) * envVarsSize); + if (envVars == NULL) + goto Failure; + + /* Copy environ values and keep track of them. */ + for (envNdx = envVarsTotal - 1; envNdx >= 0; envNdx--) { + envVars[envNdx].putenv = false; + envVars[envNdx].name = + strdup(environ[envVarsTotal - envNdx - 1]); + if (envVars[envNdx].name == NULL) + goto Failure; + envVars[envNdx].value = strchr(envVars[envNdx].name, '='); + if (envVars[envNdx].value != NULL) { + envVars[envNdx].value++; + envVars[envNdx].valueSize = + strlen(envVars[envNdx].value); + } else { + warnx("environment corrupt; missing value for %s", + envVars[envNdx].name); + errno = EFAULT; + goto Failure; + } + + /* + * Find most current version of variable to make active. This + * will prevent multiple active variables from being created + * during this initialization phase. + */ + nameLen = envVars[envNdx].value - envVars[envNdx].name - 1; + envVars[envNdx].nameLen = nameLen; + activeNdx = envVarsTotal - 1; + if (__findenv(envVars[envNdx].name, nameLen, &activeNdx, + false) == NULL) { + warnx("environment corrupt; unable to find %.*s", + nameLen, envVars[envNdx].name); + errno = EFAULT; + goto Failure; + } + envVars[activeNdx].active = true; + } + + /* Create a new environ. */ +SaveEnviron: + origEnviron = environ; + environ = NULL; + if (envVarsTotal > 0) { + rtrnVal = __rebuild_environ(envVarsTotal); + if (rtrnVal == -1) { + savedErrno = errno; + __clean_env(); + errno = savedErrno; + } + } else + rtrnVal = 0; + + return (rtrnVal); + +Failure: + savedErrno = errno; + __clean_env(); + errno = savedErrno; + + return (-1); +} + + +/* + * Remove variable added by putenv() from variable tracking array. + */ +static void +__remove_putenv(int envNdx) +{ + memmove(&(envVars[envNdx]), &(envVars[envNdx + 1]), + (envVarsTotal - envNdx) * sizeof (*envVars)); + envVarsTotal--; + + return; +} + + +/* + * Deallocate the environment built from environ as well as environ then set + * both to NULL. Eases debugging of memory leaks. + */ +static void +__clean_env(void) +{ + int envNdx; + + /* Deallocate environment and environ if created by *env(). */ + if (envVars != NULL) { + for (envNdx = 0; envNdx < envVarsTotal; envNdx++) + if (!envVars[envNdx].putenv) + free(envVars[envNdx].name); + free(envVars); + envVars = NULL; + + /* Restore original environ. */ + if (origEnviron != NULL) { + free(environ); + environ = origEnviron; + } + } + + return; } + /* - * getenv -- - * Returns ptr to value associated with name, if any, else NULL. + * Returns the value of a variable or NULL if none are found. */ char * -getenv(name) - const char *name; +getenv(const char *name) +{ + int envNdx; + size_t nameLen; + + /* Check for malformed name. */ + if (name == NULL || (nameLen = __strleneq(name)) == 0) { + errno = EINVAL; + return (NULL); + } + + /* Find environment variable via environ or rebuilt environment. */ + if (envVars == NULL) + return (__findenv_environ(name, nameLen)); + else { + envNdx = envVarsTotal - 1; + return (__findenv(name, nameLen, &envNdx, true)); + } +} + + +/* + * Set the value of a variable. Older settings are labeled as inactive. If an + * older setting has enough room to store the new value, it will be reused. No + * previous variables are ever freed here to avoid causing a segmentation fault + * in a user's code. + */ +int +setenv(const char *name, const char *value, int overwrite) +{ + bool reuse; + char *env; + int envNdx; + int newEnvActive; + size_t nameLen; + size_t valueLen; + + /* Check for malformed name. */ + if (name == NULL || (nameLen = __strleneq(name)) == 0) { + errno = EINVAL; + return (-1); + } + + /* Initialize environment. */ + if (envVars == NULL && __build_env() == -1) + return (-1); + + /* Find existing environment variable large enough to use. */ + envNdx = envVarsTotal - 1; + newEnvActive = envActive; + valueLen = strlen(value); + reuse = false; + if (__findenv(name, nameLen, &envNdx, false) != NULL) { + /* Deactivate entry if overwrite is allowed. */ + if (envVars[envNdx].active) { + if (overwrite == 0) + return (0); + envVars[envNdx].active = false; + newEnvActive--; + } + + /* putenv() created variable cannot be reused. */ + if (envVars[envNdx].putenv) + __remove_putenv(envNdx); + + /* Entry is large enough to reuse. */ + else if (envVars[envNdx].valueSize >= valueLen) + reuse = true; + + } + + /* Create new variable if none was found of sufficient size. */ + if (! reuse) { + /* Enlarge environment. */ + envNdx = envVarsTotal; + if (!__enlarge_env()) + return (-1); + + /* Create environment entry. */ + envVars[envNdx].name = malloc(nameLen + sizeof ("=") + + valueLen); + if (envVars[envNdx].name == NULL) { + envVarsTotal--; + return (-1); + } + envVars[envNdx].nameLen = nameLen; + envVars[envNdx].valueSize = valueLen; + + /* Save name of name/value pair. */ + env = stpcpy(envVars[envNdx].name, name); + if ((envVars[envNdx].name)[nameLen] != '=') + env = stpcpy(env, "="); + } + else + env = envVars[envNdx].value; + + /* Save value of name/value pair. */ + strcpy(env, value); + envVars[envNdx].value = env; + envVars[envNdx].active = true; + newEnvActive++; + + /* No need to rebuild environ if the variable was reused. */ + if (reuse) + return (0); + else + return (__rebuild_environ(newEnvActive)); +} + + +/* + * Insert a "name=value" string into then environment. Special settings must be + * made to keep setenv() from reusing this memory block and unsetenv() from + * allowing it to be tracked. + */ +int +putenv(char *string) { - int offset; + char *equals; + int envNdx; + int newEnvActive; + size_t nameLen; + + /* Check for malformed argument. */ + if (string == NULL || (equals = strchr(string, '=')) == NULL || + (nameLen = equals - string) == 0) { + errno = EINVAL; + return (-1); + } + + /* Initialize environment. */ + if (envVars == NULL && __build_env() == -1) + return (-1); + + /* Deactivate previous environment variable. */ + envNdx = envVarsTotal - 1; + newEnvActive = envActive; + if (__findenv(string, nameLen, &envNdx, true) != NULL) { + /* Reuse previous putenv slot. */ + if (envVars[envNdx].putenv) { + envVars[envNdx].name = string; + return (__rebuild_environ(envActive)); + } else { + newEnvActive--; + envVars[envNdx].active = false; + } + } + + /* Enlarge environment. */ + envNdx = envVarsTotal; + if (!__enlarge_env()) + return (-1); + + /* Create environment entry. */ + envVars[envNdx].name = string; + envVars[envNdx].nameLen = -1; + envVars[envNdx].value = NULL; + envVars[envNdx].valueSize = -1; + envVars[envNdx].putenv = true; + envVars[envNdx].active = true; + newEnvActive++; + + return (__rebuild_environ(newEnvActive)); +} + + +/* + * Unset variable with the same name by flagging it as inactive. No variable is + * ever freed. + */ +int +unsetenv(const char *name) +{ + int envNdx; + size_t nameLen; + + /* Check for malformed name. */ + if (name == NULL || (nameLen = __strleneq(name)) == 0) { + errno = EINVAL; + return (-1); + } + + /* Initialize environment. */ + if (envVars == NULL && __build_env() == -1) + return (-1); + + /* Deactivate specified variable. */ + envNdx = envVarsTotal - 1; + if (__findenv(name, nameLen, &envNdx, true) != NULL) { + envVars[envNdx].active = false; + if (envVars[envNdx].putenv) + __remove_putenv(envNdx); + __rebuild_environ(envActive - 1); + } - return (__findenv(name, &offset)); + return (0); } |