1 /* $NetBSD: popen.c,v 1.35 2015/02/02 22:07:05 christos 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.35 2015/02/02 22:07:05 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
47 #include <sys/socket.h>
62 __weak_alias(popen
,_popen
)
63 __weak_alias(pclose
,_pclose
)
76 static rwlock_t pidlist_lock
= RWLOCK_INITIALIZER
;
80 pdes_get(int *pdes
, const char **type
)
83 int flags
= strchr(*type
, 'e') ? O_CLOEXEC
: 0;
86 if (strchr(*type
, '+')) {
87 int stype
= flags
? (SOCK_STREAM
| SOCK_CLOEXEC
) : SOCK_STREAM
;
89 if (socketpair(AF_LOCAL
, stype
, 0, pdes
) < 0)
92 *type
= strrchr(*type
, 'r') ? "r" : "w";
93 if (pipe2(pdes
, flags
) == -1)
97 if ((cur
= malloc(sizeof(*cur
))) != NULL
)
100 (void)close(pdes
[0]);
101 (void)close(pdes
[1]);
107 pdes_child(int *pdes
, const char *type
)
111 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
112 from previous popen() calls that remain open in the
113 parent process are closed in the new child process. */
114 for (old
= pidlist
; old
; old
= old
->next
)
116 (void)close(old
->fd
); /* don't allow a flush */
118 (void)close(fileno(old
->fp
)); /* don't allow a flush */
121 if (type
[0] == 'r') {
122 (void)close(pdes
[0]);
123 if (pdes
[1] != STDOUT_FILENO
) {
124 (void)dup2(pdes
[1], STDOUT_FILENO
);
125 (void)close(pdes
[1]);
128 (void)dup2(STDOUT_FILENO
, STDIN_FILENO
);
130 (void)close(pdes
[1]);
131 if (pdes
[0] != STDIN_FILENO
) {
132 (void)dup2(pdes
[0], STDIN_FILENO
);
133 (void)close(pdes
[0]);
139 pdes_parent(int *pdes
, struct pid
*cur
, pid_t pid
, const char *type
)
143 /* Parent; assume fdopen can't fail. */
145 iop
= fdopen(pdes
[0], type
);
149 (void)close(pdes
[1]);
151 iop
= fdopen(pdes
[1], type
);
155 (void)close(pdes
[0]);
158 /* Link into list of file descriptors. */
166 pdes_error(int *pdes
, struct pid
*cur
)
169 (void)close(pdes
[0]);
170 (void)close(pdes
[1]);
174 popen(const char *cmd
, const char *type
)
180 _DIAGASSERT(cmd
!= NULL
);
181 _DIAGASSERT(type
!= NULL
);
183 if ((cur
= pdes_get(pdes
, &type
)) == NULL
)
187 (void)rwlock_rdlock(&pidlist_lock
);
189 (void)__readlockenv();
190 switch (pid
= vfork()) {
191 case -1: /* Error. */
195 (void)rwlock_unlock(&pidlist_lock
);
197 pdes_error(pdes
, cur
);
202 pdes_child(pdes
, type
);
203 execl(_PATH_BSHELL
, "sh", "-c", cmd
, NULL
);
209 pdes_parent(pdes
, cur
, pid
, type
);
212 (void)rwlock_unlock(&pidlist_lock
);
219 popenve(const char *cmd
, char *const *argv
, char *const *envp
, const char *type
)
225 _DIAGASSERT(cmd
!= NULL
);
226 _DIAGASSERT(type
!= NULL
);
228 if ((cur
= pdes_get(pdes
, &type
)) == NULL
)
232 (void)rwlock_rdlock(&pidlist_lock
);
234 switch (pid
= vfork()) {
235 case -1: /* Error. */
238 (void)rwlock_unlock(&pidlist_lock
);
240 pdes_error(pdes
, cur
);
245 pdes_child(pdes
, type
);
246 execve(cmd
, argv
, envp
);
251 pdes_parent(pdes
, cur
, pid
, type
);
254 (void)rwlock_unlock(&pidlist_lock
);
262 * Pclose returns -1 if stream is not associated with a `popened' command,
263 * if already `pclosed', or waitpid returns an error.
268 struct pid
*cur
, *last
;
272 _DIAGASSERT(iop
!= NULL
);
275 rwlock_wrlock(&pidlist_lock
);
278 /* Find the appropriate file pointer. */
279 for (last
= NULL
, cur
= pidlist
; cur
; last
= cur
, cur
= cur
->next
)
284 (void)rwlock_unlock(&pidlist_lock
);
292 /* Remove the entry from the linked list. */
296 last
->next
= cur
->next
;
299 (void)rwlock_unlock(&pidlist_lock
);
303 pid
= waitpid(cur
->pid
, &pstat
, 0);
304 } while (pid
== -1 && errno
== EINTR
);
308 return pid
== -1 ? -1 : pstat
;