2 * SPDX-License-Identifier: BSD-3-Clause
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 __SCCSID("@(#)popen.c 8.3 (Berkeley) 5/3/95");
37 __FBSDID("$FreeBSD$");
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/queue.h>
53 #include "un-namespace.h"
54 #include "libc_private.h"
56 extern char **environ;
59 SLIST_ENTRY(pid) next;
63 static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
64 static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
66 #define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
67 #define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
70 popen(const char *command, const char *type)
74 int pdes[2], pid, twoway, cloexec;
75 int pdes_unused_in_parent;
79 cloexec = strchr(type, 'e') != NULL;
81 * Lite2 introduced two-way popen() pipes using _socketpair().
82 * FreeBSD's pipe() is bidirectional, so we use that.
84 if (strchr(type, '+')) {
89 if ((*type != 'r' && *type != 'w') ||
90 (type[1] && (type[1] != 'e' || type[2])))
93 if (pipe2(pdes, O_CLOEXEC) < 0)
96 if ((cur = malloc(sizeof(struct pid))) == NULL) {
97 (void)_close(pdes[0]);
98 (void)_close(pdes[1]);
103 iop = fdopen(pdes[0], type);
104 pdes_unused_in_parent = pdes[1];
106 iop = fdopen(pdes[1], type);
107 pdes_unused_in_parent = pdes[0];
110 (void)_close(pdes[0]);
111 (void)_close(pdes[1]);
118 argv[2] = (char *)command;
122 switch (pid = vfork()) {
123 case -1: /* Error. */
126 * The _close() closes the unused end of pdes[], while
127 * the fclose() closes the used end of pdes[], *and* cleans
130 (void)_close(pdes_unused_in_parent);
138 * The _dup2() to STDIN_FILENO is repeated to avoid
139 * writing to pdes[1], which might corrupt the
140 * parent's copy. This isn't good enough in
141 * general, since the _exit() is no return, so
142 * the compiler is free to corrupt all the local
145 if (pdes[1] != STDOUT_FILENO) {
146 (void)_dup2(pdes[1], STDOUT_FILENO);
148 (void)_dup2(STDOUT_FILENO, STDIN_FILENO);
149 } else if (twoway && (pdes[1] != STDIN_FILENO)) {
150 (void)_dup2(pdes[1], STDIN_FILENO);
151 (void)_fcntl(pdes[1], F_SETFD, 0);
153 (void)_fcntl(pdes[1], F_SETFD, 0);
155 if (pdes[0] != STDIN_FILENO) {
156 (void)_dup2(pdes[0], STDIN_FILENO);
158 (void)_fcntl(pdes[0], F_SETFD, 0);
160 SLIST_FOREACH(p, &pidlist, next)
161 (void)_close(fileno(p->fp));
162 _execve(_PATH_BSHELL, argv, environ);
169 (void)_close(pdes_unused_in_parent);
171 /* Link into list of file descriptors. */
175 SLIST_INSERT_HEAD(&pidlist, cur, next);
179 * To guard against undesired fd passing with concurrent calls,
180 * only clear the close-on-exec flag after linking the file into
181 * the list which will cause an explicit close.
184 (void)_fcntl(*type == 'r' ? pdes[0] : pdes[1], F_SETFD, 0);
191 * Pclose returns -1 if stream is not associated with a `popened' command,
192 * if already `pclosed', or waitpid returns an error.
197 struct pid *cur, *last = NULL;
202 * Find the appropriate file pointer and remove it from the list.
205 SLIST_FOREACH(cur, &pidlist, next) {
215 SLIST_REMOVE_HEAD(&pidlist, next);
217 SLIST_REMOVE_AFTER(last, next);
223 pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
224 } while (pid == -1 && errno == EINTR);
228 return (pid == -1 ? -1 : pstat);