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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #ifdef VARIANT_DARWINEXTSN
38 #define _DARWIN_UNLIMITED_STREAMS
39 #endif /* VARIANT_DARWINEXTSN */
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD: src/lib/libc/gen/popen.c,v 1.18 2003/01/04 00:15:15 tjr Exp $");
47 #include "namespace.h"
48 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <wchar.h> /* fwide() */
61 #include "un-namespace.h"
62 #include "libc_private.h"
64 #include <crt_externs.h>
65 #define environ (*_NSGetEnviron())
67 /* 3516149 - store file descriptor and use that to close to prevent blocking */
74 #define pidlist __popen_pidlist
75 #define pidlist_mutex __popen_pidlist_mutex
76 #ifndef BUILDING_VARIANT
77 __private_extern__
struct pid
*pidlist
= NULL
;
78 __private_extern__ pthread_mutex_t pidlist_mutex
= PTHREAD_MUTEX_INITIALIZER
;
79 #else /* BUILDING_VARIANT */
80 extern struct pid
*pidlist
;
81 extern pthread_mutex_t pidlist_mutex
;
82 #endif /* !BUILDING_VARIANT */
84 #define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
85 #define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
89 const char *command
, *type
;
93 int pdes
[2], pid
, twoway
, other
;
96 posix_spawn_file_actions_t file_actions
;
103 if (strcmp(type
, "r+") == 0) {
106 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pdes
) < 0)
110 if ((*type
!= 'r' && *type
!= 'w') || type
[1]) {
118 /* fdopen can now fail */
120 iop
= fdopen(pdes
[0], type
);
123 iop
= fdopen(pdes
[1], type
);
127 (void)_close(pdes
[0]);
128 (void)_close(pdes
[1]);
132 if ((cur
= malloc(sizeof(struct pid
))) == NULL
) {
138 if ((err
= posix_spawn_file_actions_init(&file_actions
)) != 0) {
147 * The dup2() to STDIN_FILENO is repeated to avoid
148 * writing to pdes[1], which might corrupt the
149 * parent's copy. This isn't good enough in
150 * general, since the _exit() is no return, so
151 * the compiler is free to corrupt all the local
154 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[0]);
155 if (pdes
[1] != STDOUT_FILENO
) {
156 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDOUT_FILENO
);
157 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[1]);
159 (void)posix_spawn_file_actions_adddup2(&file_actions
, STDOUT_FILENO
, STDIN_FILENO
);
160 } else if (twoway
&& (pdes
[1] != STDIN_FILENO
))
161 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDIN_FILENO
);
163 if (pdes
[0] != STDIN_FILENO
) {
164 (void)posix_spawn_file_actions_adddup2(&file_actions
, pdes
[0], STDIN_FILENO
);
165 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[0]);
167 (void)posix_spawn_file_actions_addclose(&file_actions
, pdes
[1]);
169 for (p
= pidlist
; p
; p
= p
->next
) {
170 (void)posix_spawn_file_actions_addclose(&file_actions
, p
->fd
);
175 argv
[2] = (char *)command
;
178 err
= posix_spawn(&pid
, _PATH_BSHELL
, &file_actions
, NULL
, argv
, environ
);
179 posix_spawn_file_actions_destroy(&file_actions
);
181 if (err
== ENOMEM
|| err
== EAGAIN
) { /* as if fork failed */
187 } else if (err
!= 0) { /* couldn't exec the shell */
193 (void)_close(pdes
[1]);
196 (void)_close(pdes
[0]);
199 /* Link into list of file descriptors. */
206 fwide(iop
, -1); /* byte stream */
210 #ifndef BUILDING_VARIANT
213 * Pclose returns -1 if stream is not associated with a `popened' command,
214 * if already `pclosed', or waitpid returns an error.
220 struct pid
*cur
, *last
;
225 * Find the appropriate file pointer and remove it from the list.
228 for (last
= NULL
, cur
= pidlist
; cur
; last
= cur
, cur
= cur
->next
)
238 last
->next
= cur
->next
;
245 return W_EXITCODE(127, 0);
248 pid
= _wait4(cur
->pid
, &pstat
, 0, (struct rusage
*)0);
249 } while (pid
== -1 && errno
== EINTR
);
253 return (pid
== -1 ? -1 : pstat
);
255 #endif /* !BUILDING_VARIANT */