1 /* $NetBSD: popen.c,v 1.28 2003/09/15 22:30:38 cl Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software written by Ken Arnold and
8 * published in UNIX Review, Vol. 6, No. 8.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
40 __RCSID("$NetBSD: popen.c,v 1.28 2003/09/15 22:30:38 cl Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
47 #include <sys/socket.h>
57 #include "reentrant.h"
60 __weak_alias(popen
,_popen
)
61 __weak_alias(pclose
,_pclose
)
65 extern rwlock_t __environ_lock
;
78 static rwlock_t pidlist_lock
= RWLOCK_INITIALIZER
;
82 popen(const char *command
, const char *type
)
84 struct pid
*cur
, *old
;
86 const char * volatile xtype
= type
;
87 int pdes
[2], pid
, serrno
;
90 _DIAGASSERT(command
!= NULL
);
91 _DIAGASSERT(xtype
!= NULL
);
93 if (strchr(xtype
, '+')) {
96 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, pdes
) < 0)
100 if ((*xtype
!= 'r' && *xtype
!= 'w') || xtype
[1] ||
107 if ((cur
= malloc(sizeof(struct pid
))) == NULL
) {
108 (void)close(pdes
[0]);
109 (void)close(pdes
[1]);
114 rwlock_rdlock(&pidlist_lock
);
115 rwlock_rdlock(&__environ_lock
);
116 switch (pid
= vfork()) {
117 case -1: /* Error. */
119 rwlock_unlock(&__environ_lock
);
120 rwlock_unlock(&pidlist_lock
);
122 (void)close(pdes
[0]);
123 (void)close(pdes
[1]);
128 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
129 from previous popen() calls that remain open in the
130 parent process are closed in the new child process. */
131 for (old
= pidlist
; old
; old
= old
->next
)
133 close(old
->fd
); /* don't allow a flush */
135 close(fileno(old
->fp
)); /* don't allow a flush */
139 (void)close(pdes
[0]);
140 if (pdes
[1] != STDOUT_FILENO
) {
141 (void)dup2(pdes
[1], STDOUT_FILENO
);
142 (void)close(pdes
[1]);
145 (void)dup2(STDOUT_FILENO
, STDIN_FILENO
);
147 (void)close(pdes
[1]);
148 if (pdes
[0] != STDIN_FILENO
) {
149 (void)dup2(pdes
[0], STDIN_FILENO
);
150 (void)close(pdes
[0]);
154 execl(_PATH_BSHELL
, "sh", "-c", command
, NULL
);
158 rwlock_unlock(&__environ_lock
);
160 /* Parent; assume fdopen can't fail. */
162 iop
= fdopen(pdes
[0], xtype
);
166 (void)close(pdes
[1]);
168 iop
= fdopen(pdes
[1], xtype
);
172 (void)close(pdes
[0]);
175 /* Link into list of file descriptors. */
180 rwlock_unlock(&pidlist_lock
);
187 * Pclose returns -1 if stream is not associated with a `popened' command,
188 * if already `pclosed', or waitpid returns an error.
194 struct pid
*cur
, *last
;
198 _DIAGASSERT(iop
!= NULL
);
200 rwlock_wrlock(&pidlist_lock
);
202 /* Find the appropriate file pointer. */
203 for (last
= NULL
, cur
= pidlist
; cur
; last
= cur
, cur
= cur
->next
)
207 rwlock_unlock(&pidlist_lock
);
213 /* Remove the entry from the linked list. */
217 last
->next
= cur
->next
;
219 rwlock_unlock(&pidlist_lock
);
222 pid
= waitpid(cur
->pid
, &pstat
, 0);
223 } while (pid
== -1 && errno
== EINTR
);
227 return (pid
== -1 ? -1 : pstat
);