version 1.25, 2000/01/22 22:19:11 |
version 1.30, 2010/11/14 18:11:42 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
* 3. All advertising materials mentioning features or use of this software |
* 3. Neither the name of the University nor the names of its contributors |
* must display the following acknowledgement: |
|
* This product includes software developed by the University of |
|
* California, Berkeley and its contributors. |
|
* 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 |
* may be used to endorse or promote products derived from this software |
* without specific prior written permission. |
* without specific prior written permission. |
* |
* |
Line 59 __RCSID("$NetBSD$"); |
|
Line 55 __RCSID("$NetBSD$"); |
|
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
|
#include "env.h" |
|
#include "reentrant.h" |
|
|
#ifdef __weak_alias |
#ifdef __weak_alias |
__weak_alias(popen,_popen) |
__weak_alias(popen,_popen) |
__weak_alias(pclose,_pclose) |
__weak_alias(pclose,_pclose) |
Line 67 __weak_alias(pclose,_pclose) |
|
Line 66 __weak_alias(pclose,_pclose) |
|
static struct pid { |
static struct pid { |
struct pid *next; |
struct pid *next; |
FILE *fp; |
FILE *fp; |
|
#ifdef _REENTRANT |
|
int fd; |
|
#endif |
pid_t pid; |
pid_t pid; |
} *pidlist; |
} *pidlist; |
|
|
|
#ifdef _REENTRANT |
|
static rwlock_t pidlist_lock = RWLOCK_INITIALIZER; |
|
#endif |
|
|
FILE * |
FILE * |
popen(command, type) |
popen(const char *command, const char *type) |
const char *command, *type; |
|
{ |
{ |
struct pid *cur, *old; |
struct pid *cur, *old; |
FILE *iop; |
FILE *iop; |
int pdes[2], pid, twoway, serrno; |
const char * volatile xtype = type; |
|
int pdes[2], pid, serrno; |
|
volatile int twoway; |
|
|
_DIAGASSERT(command != NULL); |
_DIAGASSERT(command != NULL); |
_DIAGASSERT(type != NULL); |
_DIAGASSERT(xtype != NULL); |
|
|
#ifdef __GNUC__ |
|
/* This outrageous construct just to shut up a GCC warning. */ |
|
(void) &cur; (void) &twoway; (void) &type; |
|
#endif |
|
|
|
if (strchr(type, '+')) { |
if (strchr(xtype, '+')) { |
twoway = 1; |
twoway = 1; |
type = "r+"; |
type = "r+"; |
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0) |
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0) |
return (NULL); |
return (NULL); |
} else { |
} else { |
twoway = 0; |
twoway = 0; |
if ((*type != 'r' && *type != 'w') || type[1] || |
if ((*xtype != 'r' && *xtype != 'w') || xtype[1] || |
(pipe(pdes) < 0)) { |
(pipe(pdes) < 0)) { |
errno = EINVAL; |
errno = EINVAL; |
return (NULL); |
return (NULL); |
Line 107 popen(command, type) |
|
Line 109 popen(command, type) |
|
return (NULL); |
return (NULL); |
} |
} |
|
|
|
(void)rwlock_rdlock(&pidlist_lock); |
|
(void)__readlockenv(); |
switch (pid = vfork()) { |
switch (pid = vfork()) { |
case -1: /* Error. */ |
case -1: /* Error. */ |
serrno = errno; |
serrno = errno; |
|
(void)__unlockenv(); |
|
(void)rwlock_unlock(&pidlist_lock); |
free(cur); |
free(cur); |
(void)close(pdes[0]); |
(void)close(pdes[0]); |
(void)close(pdes[1]); |
(void)close(pdes[1]); |
Line 121 popen(command, type) |
|
Line 127 popen(command, type) |
|
from previous popen() calls that remain open in the |
from previous popen() calls that remain open in the |
parent process are closed in the new child process. */ |
parent process are closed in the new child process. */ |
for (old = pidlist; old; old = old->next) |
for (old = pidlist; old; old = old->next) |
|
#ifdef _REENTRANT |
|
close(old->fd); /* don't allow a flush */ |
|
#else |
close(fileno(old->fp)); /* don't allow a flush */ |
close(fileno(old->fp)); /* don't allow a flush */ |
|
#endif |
|
|
if (*type == 'r') { |
if (*xtype == 'r') { |
(void)close(pdes[0]); |
(void)close(pdes[0]); |
if (pdes[1] != STDOUT_FILENO) { |
if (pdes[1] != STDOUT_FILENO) { |
(void)dup2(pdes[1], STDOUT_FILENO); |
(void)dup2(pdes[1], STDOUT_FILENO); |
Line 143 popen(command, type) |
|
Line 153 popen(command, type) |
|
_exit(127); |
_exit(127); |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
(void)__unlockenv(); |
|
|
/* Parent; assume fdopen can't fail. */ |
/* Parent; assume fdopen can't fail. */ |
if (*type == 'r') { |
if (*xtype == 'r') { |
iop = fdopen(pdes[0], type); |
iop = fdopen(pdes[0], xtype); |
|
#ifdef _REENTRANT |
|
cur->fd = pdes[0]; |
|
#endif |
(void)close(pdes[1]); |
(void)close(pdes[1]); |
} else { |
} else { |
iop = fdopen(pdes[1], type); |
iop = fdopen(pdes[1], xtype); |
|
#ifdef _REENTRANT |
|
cur->fd = pdes[1]; |
|
#endif |
(void)close(pdes[0]); |
(void)close(pdes[0]); |
} |
} |
|
|
Line 158 popen(command, type) |
|
Line 175 popen(command, type) |
|
cur->pid = pid; |
cur->pid = pid; |
cur->next = pidlist; |
cur->next = pidlist; |
pidlist = cur; |
pidlist = cur; |
|
(void)rwlock_unlock(&pidlist_lock); |
|
|
return (iop); |
return (iop); |
} |
} |
|
|
|
|
_DIAGASSERT(iop != NULL); |
_DIAGASSERT(iop != NULL); |
|
|
|
rwlock_wrlock(&pidlist_lock); |
|
|
/* Find the appropriate file pointer. */ |
/* Find the appropriate file pointer. */ |
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) |
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) |
if (cur->fp == iop) |
if (cur->fp == iop) |
break; |
break; |
if (cur == NULL) |
if (cur == NULL) { |
|
(void)rwlock_unlock(&pidlist_lock); |
return (-1); |
return (-1); |
|
} |
|
|
(void)fclose(iop); |
(void)fclose(iop); |
|
|
do { |
|
pid = waitpid(cur->pid, &pstat, 0); |
|
} while (pid == -1 && errno == EINTR); |
|
|
|
/* Remove the entry from the linked list. */ |
/* Remove the entry from the linked list. */ |
if (last == NULL) |
if (last == NULL) |
pidlist = cur->next; |
pidlist = cur->next; |
else |
else |
last->next = cur->next; |
last->next = cur->next; |
|
|
|
(void)rwlock_unlock(&pidlist_lock); |
|
|
|
do { |
|
pid = waitpid(cur->pid, &pstat, 0); |
|
} while (pid == -1 && errno == EINTR); |
|
|
free(cur); |
free(cur); |
|
|
return (pid == -1 ? -1 : pstat); |
return (pid == -1 ? -1 : pstat); |
} |
} |