am 3884bfe9: libc: popen: work around data corruption

* commit '3884bfe9661955543ce203c60f9225bbdf33f6bb':
  libc: popen: work around data corruption
This commit is contained in:
JP Abgrall 2011-09-17 16:32:00 -07:00 committed by Android Git Automerger
commit 771600415f

View File

@ -48,6 +48,8 @@ static struct pid {
pid_t pid; pid_t pid;
} *pidlist; } *pidlist;
extern char **environ;
FILE * FILE *
popen(const char *program, const char *type) popen(const char *program, const char *type)
{ {
@ -55,6 +57,7 @@ popen(const char *program, const char *type)
FILE *iop; FILE *iop;
int pdes[2]; int pdes[2];
pid_t pid; pid_t pid;
char *argp[] = {"sh", "-c", NULL, NULL};
if ((*type != 'r' && *type != 'w') || type[1] != '\0') { if ((*type != 'r' && *type != 'w') || type[1] != '\0') {
errno = EINVAL; errno = EINVAL;
@ -69,7 +72,7 @@ popen(const char *program, const char *type)
return (NULL); return (NULL);
} }
switch (pid = vfork()) { switch (pid = fork()) {
case -1: /* Error. */ case -1: /* Error. */
(void)close(pdes[0]); (void)close(pdes[0]);
(void)close(pdes[1]); (void)close(pdes[1]);
@ -80,24 +83,17 @@ popen(const char *program, const char *type)
{ {
struct pid *pcur; struct pid *pcur;
/* /*
* because vfork() instead of fork(), must leak FILE *, * We fork()'d, we got our own copy of the list, no
* but luckily we are terminally headed for an execl() * contention.
*/ */
for (pcur = pidlist; pcur; pcur = pcur->next) for (pcur = pidlist; pcur; pcur = pcur->next)
close(fileno(pcur->fp)); close(fileno(pcur->fp));
if (*type == 'r') { if (*type == 'r') {
int tpdes1 = pdes[1];
(void) close(pdes[0]); (void) close(pdes[0]);
/* if (pdes[1] != STDOUT_FILENO) {
* We must NOT modify pdes, due to the (void)dup2(pdes[1], STDOUT_FILENO);
* semantics of vfork. (void)close(pdes[1]);
*/
if (tpdes1 != STDOUT_FILENO) {
(void)dup2(tpdes1, STDOUT_FILENO);
(void)close(tpdes1);
tpdes1 = STDOUT_FILENO;
} }
} else { } else {
(void)close(pdes[1]); (void)close(pdes[1]);
@ -106,7 +102,8 @@ popen(const char *program, const char *type)
(void)close(pdes[0]); (void)close(pdes[0]);
} }
} }
execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL); argp[2] = (char *)program;
execve(_PATH_BSHELL, argp, environ);
_exit(127); _exit(127);
/* NOTREACHED */ /* NOTREACHED */
} }