2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software written by Ken Arnold and
6 * published in UNIX Review, Vol. 6, No. 8.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #ifdef VARIANT_DARWINEXTSN
34 #define _DARWIN_UNLIMITED_STREAMS
35 #endif /* VARIANT_DARWINEXTSN */
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/gen/popen.c,v 1.21 2009/05/27 19:28:04 ed Exp $");
43 #include "namespace.h"
44 #include <sys/param.h>
45 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <wchar.h> /* fwide() */
58 #include "un-namespace.h"
59 #include "libc_private.h"
61 #include <crt_externs.h>
62 #define environ (*_NSGetEnviron())
64 /* Our queue.h doesn't have SLIST_REMOVE_AFTER in it yet
65 * <rdar://problem/7431558> API: Add SLIST_REMOVE_AFTER to sys/queue.h (from FreeBSD)
67 #ifndef SLIST_REMOVE_AFTER
68 #define SLIST_REMOVE_AFTER(elm, field) do { \
69 SLIST_NEXT(elm, field) = \
70 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
74 /* 3516149 - store file descriptor and use that to close to prevent blocking */
76 SLIST_ENTRY(pid
) next
;
81 #define pidlist __popen_pidlist
82 #define pidlist_mutex __popen_pidlist_mutex
83 #ifndef BUILDING_VARIANT
84 __private_extern__
SLIST_HEAD(, pid
) pidlist
= SLIST_HEAD_INITIALIZER(pidlist
);
85 __private_extern__ pthread_mutex_t pidlist_mutex
= PTHREAD_MUTEX_INITIALIZER
;
86 #else /* BUILDING_VARIANT */
87 extern SLIST_HEAD(, pid
) pidlist
;
88 extern pthread_mutex_t pidlist_mutex
;
89 #endif /* !BUILDING_VARIANT */
91 #define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
92 #define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
96 const char *command
, *type
;
100 int pdes
[2], pid
, twoway
, other
;
103 posix_spawn_file_actions_t file_actions
;
110 if (strcmp(type
, "r+") == 0) {
113 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pdes
) < 0)
117 if ((*type
!= 'r' && *type
!= 'w') || type
[1]) {
125 /* fdopen can now fail */
127 iop
= fdopen(pdes
[0], type
);
130 iop
= fdopen(pdes
[1], type
);
134 (void)_close(pdes
[0]);
135 (void)_close(pdes
[1]);
139 if ((cur
= malloc(sizeof(struct pid
))) == NULL
) {
145 if ((err
= posix_spawn_file_actions_init(&file_actions
)) != 0) {
154 * The dup2() to STDIN_FILENO is repeated to avoid
155 * writing to pdes[1], which might corrupt the
156 * parent's copy. This isn't good enough in
157 * general, since the _exit() is no return, so
158 * the compiler is free to corrupt all the local
161 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[0]);
162 if (pdes
[1] != STDOUT_FILENO
) {
163 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDOUT_FILENO
);
164 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[1]);
166 (void)posix_spawn_file_actions_adddup2(&file_actions
, STDOUT_FILENO
, STDIN_FILENO
);
167 } else if (twoway
&& (pdes
[1] != STDIN_FILENO
))
168 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDIN_FILENO
);
170 if (pdes
[0] != STDIN_FILENO
) {
171 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[0], STDIN_FILENO
);
172 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[0]);
174 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[1]);
176 SLIST_FOREACH(p
, &pidlist
, next
)
177 (void)posix_spawn_file_actions_addclose(&file_actions
, p
->fd
);
181 argv
[2] = (char *)command
;
184 err
= posix_spawn(&pid
, _PATH_BSHELL
, &file_actions
, NULL
, argv
, environ
);
185 posix_spawn_file_actions_destroy(&file_actions
);
187 if (err
== ENOMEM
|| err
== EAGAIN
) { /* as if fork failed */
193 } else if (err
!= 0) { /* couldn't exec the shell */
199 (void)_close(pdes
[1]);
202 (void)_close(pdes
[0]);
205 /* Link into list of file descriptors. */
209 SLIST_INSERT_HEAD(&pidlist
, cur
, next
);
211 fwide(iop
, -1); /* byte stream */
215 #ifndef BUILDING_VARIANT
218 * Pclose returns -1 if stream is not associated with a `popened' command,
219 * if already `pclosed', or waitpid returns an error.
225 struct pid
*cur
, *last
= NULL
;
230 * Find the appropriate file pointer and remove it from the list.
233 SLIST_FOREACH(cur
, &pidlist
, next
) {
243 SLIST_REMOVE_HEAD(&pidlist
, next
);
245 SLIST_REMOVE_AFTER(last
, next
);
252 return W_EXITCODE(127, 0);
255 pid
= _wait4(cur
->pid
, &pstat
, 0, (struct rusage
*)0);
256 } while (pid
== -1 && errno
== EINTR
);
260 return (pid
== -1 ? -1 : pstat
);
262 #endif /* !BUILDING_VARIANT */