summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/system.c
diff options
context:
space:
mode:
authortron <tron@NetBSD.org>2010-11-14 18:11:42 +0000
committertron <tron@NetBSD.org>2010-11-14 18:11:42 +0000
commitfbf4aa1699796a9be2edf5319b864af126e3ea87 (patch)
treec3e1a0b86b8b035e29ebcf96f900cd6bb05d95be /lib/libc/stdlib/system.c
parentdff67e454b6911f93e556ee11b0f81930b1a6b22 (diff)
Improve and simplify implementation of *env(3) functions:
- Use RB tree to keep track of memory allocated via setenv(3) as suggested by Enami Tsugutomo in private e-mail. This simplifies the code a lot as we no longer need to keep the size of "environ" in sync with an array of allocated environment variables. It also makes it possible to free environment variables in unsetenv(3) if something has changed the order of the "environ" array. - Fix a bug in getenv(3) and getenv_r(3) which would return bogus results e.g. for " getenv("A=B") " if an environment variable "A" with value "B=C" exists. - Clean up the internal functions: - Don't expose the read/write lock for the environment to other parts of "libc". Provide locking functions instead. - Use "bool" to report success or failure. - Use "ssize_t" or "size_t" instead of "int" for indexes. - Provide internal functions with simpler interfaces e.g. don't combine return values and reference arguments. - Don't copy "environ" into an allocated block unless we really need to grow it. Code reviewed by Joerg Sonnenberger and Christos Zoulas, tested by Joerg Sonnenberger and me. These changes also fix problems in zsh 4.3.* and pam_ssh according to Joerg.
Diffstat (limited to 'lib/libc/stdlib/system.c')
-rw-r--r--lib/libc/stdlib/system.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c
index dd02987a5aa..f0fc1207ee8 100644
--- a/lib/libc/stdlib/system.c
+++ b/lib/libc/stdlib/system.c
@@ -1,4 +1,4 @@
-/* $NetBSD: system.c,v 1.22 2008/08/27 06:45:02 christos Exp $ */
+/* $NetBSD: system.c,v 1.23 2010/11/14 18:11:43 tron Exp $ */
/*
* Copyright (c) 1988, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: system.c,v 1.22 2008/08/27 06:45:02 christos Exp $");
+__RCSID("$NetBSD: system.c,v 1.23 2010/11/14 18:11:43 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -46,12 +46,9 @@ __RCSID("$NetBSD: system.c,v 1.22 2008/08/27 06:45:02 christos Exp $");
#include <stdlib.h>
#include <unistd.h>
#include <paths.h>
-#include "reentrant.h"
-#ifdef _REENTRANT
-extern rwlock_t __environ_lock;
-#endif
-extern char **environ;
+#include "env.h"
+#include "reentrant.h"
int
system(command)
@@ -93,10 +90,10 @@ system(command)
return -1;
}
- rwlock_rdlock(&__environ_lock);
+ (void)__readlockenv();
switch(pid = vfork()) {
case -1: /* error */
- rwlock_unlock(&__environ_lock);
+ (void)__unlockenv();
sigaction(SIGINT, &intsa, NULL);
sigaction(SIGQUIT, &quitsa, NULL);
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
@@ -108,7 +105,7 @@ system(command)
execve(_PATH_BSHELL, __UNCONST(argp), environ);
_exit(127);
}
- rwlock_unlock(&__environ_lock);
+ (void)__unlockenv();
while (waitpid(pid, &pstat, 0) == -1) {
if (errno != EINTR) {