Merge "Fix environment variable assignment in init"

This commit is contained in:
Elliott Hughes 2014-07-10 18:25:52 +00:00 committed by Gerrit Code Review
commit 796fccb80d
2 changed files with 22 additions and 8 deletions

View file

@ -48,7 +48,7 @@
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
void add_environment(const char *name, const char *value); int add_environment(const char *name, const char *value);
extern int init_module(void *, unsigned long, const char *); extern int init_module(void *, unsigned long, const char *);
@ -261,8 +261,7 @@ int do_exec(int nargs, char **args)
int do_export(int nargs, char **args) int do_export(int nargs, char **args)
{ {
add_environment(args[1], args[2]); return add_environment(args[1], args[2]);
return 0;
} }
int do_hostname(int nargs, char **args) int do_hostname(int nargs, char **args)

View file

@ -96,11 +96,24 @@ static const char *ENV[32];
/* add_environment - add "key=value" to the current environment */ /* add_environment - add "key=value" to the current environment */
int add_environment(const char *key, const char *val) int add_environment(const char *key, const char *val)
{ {
int n; size_t n;
size_t key_len = strlen(key);
for (n = 0; n < 31; n++) { /* The last environment entry is reserved to terminate the list */
if (!ENV[n]) { for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
size_t len = strlen(key) + strlen(val) + 2;
/* Delete any existing entry for this key */
if (ENV[n] != NULL) {
size_t entry_key_len = strcspn(ENV[n], "=");
if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
free((char*)ENV[n]);
ENV[n] = NULL;
}
}
/* Add entry if a free slot is available */
if (ENV[n] == NULL) {
size_t len = key_len + strlen(val) + 2;
char *entry = malloc(len); char *entry = malloc(len);
snprintf(entry, len, "%s=%s", key, val); snprintf(entry, len, "%s=%s", key, val);
ENV[n] = entry; ENV[n] = entry;
@ -108,7 +121,9 @@ int add_environment(const char *key, const char *val)
} }
} }
return 1; ERROR("No env. room to store: '%s':'%s'\n", key, val);
return -1;
} }
static void zap_stdio(void) static void zap_stdio(void)