1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software written by Ken Arnold and
8 * published in UNIX Review, Vol. 6, No. 8.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include "namespace.h"
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/wait.h>
39
40 #include <signal.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <paths.h>
48 #include <pthread.h>
49 #include "un-namespace.h"
50 #include "libc_private.h"
51
52 struct pid {
53 SLIST_ENTRY(pid) next;
54 FILE *fp;
55 pid_t pid;
56 };
57 static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
58 static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
59
60 #define THREAD_LOCK() if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
61 #define THREAD_UNLOCK() if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
62
63 FILE *
popen(const char * command,const char * type)64 popen(const char *command, const char *type)
65 {
66 struct pid *cur;
67 FILE *iop;
68 int pdes[2], pid, twoway, cloexec;
69 int pdes_unused_in_parent;
70 char *argv[4];
71 struct pid *p;
72
73 cloexec = strchr(type, 'e') != NULL;
74 /*
75 * Lite2 introduced two-way popen() pipes using _socketpair().
76 * FreeBSD's pipe() is bidirectional, so we use that.
77 */
78 if (strchr(type, '+')) {
79 twoway = 1;
80 type = "r+";
81 } else {
82 twoway = 0;
83 if ((*type != 'r' && *type != 'w') ||
84 (type[1] && (type[1] != 'e' || type[2])))
85 return (NULL);
86 }
87 if (pipe2(pdes, O_CLOEXEC) < 0)
88 return (NULL);
89
90 if ((cur = malloc(sizeof(struct pid))) == NULL) {
91 (void)_close(pdes[0]);
92 (void)_close(pdes[1]);
93 return (NULL);
94 }
95
96 if (*type == 'r') {
97 iop = fdopen(pdes[0], type);
98 pdes_unused_in_parent = pdes[1];
99 } else {
100 iop = fdopen(pdes[1], type);
101 pdes_unused_in_parent = pdes[0];
102 }
103 if (iop == NULL) {
104 (void)_close(pdes[0]);
105 (void)_close(pdes[1]);
106 free(cur);
107 return (NULL);
108 }
109
110 argv[0] = "sh";
111 argv[1] = "-c";
112 argv[2] = (char *)command;
113 argv[3] = NULL;
114
115 THREAD_LOCK();
116 switch (pid = vfork()) {
117 case -1: /* Error. */
118 THREAD_UNLOCK();
119 /*
120 * The _close() closes the unused end of pdes[], while
121 * the fclose() closes the used end of pdes[], *and* cleans
122 * up iop.
123 */
124 (void)_close(pdes_unused_in_parent);
125 free(cur);
126 (void)fclose(iop);
127 return (NULL);
128 /* NOTREACHED */
129 case 0: /* Child. */
130 if (*type == 'r') {
131 /*
132 * The _dup2() to STDIN_FILENO is repeated to avoid
133 * writing to pdes[1], which might corrupt the
134 * parent's copy. This isn't good enough in
135 * general, since the _exit() is no return, so
136 * the compiler is free to corrupt all the local
137 * variables.
138 */
139 if (pdes[1] != STDOUT_FILENO) {
140 (void)_dup2(pdes[1], STDOUT_FILENO);
141 if (twoway)
142 (void)_dup2(STDOUT_FILENO, STDIN_FILENO);
143 } else if (twoway && (pdes[1] != STDIN_FILENO)) {
144 (void)_dup2(pdes[1], STDIN_FILENO);
145 (void)_fcntl(pdes[1], F_SETFD, 0);
146 } else
147 (void)_fcntl(pdes[1], F_SETFD, 0);
148 } else {
149 if (pdes[0] != STDIN_FILENO) {
150 (void)_dup2(pdes[0], STDIN_FILENO);
151 } else
152 (void)_fcntl(pdes[0], F_SETFD, 0);
153 }
154 SLIST_FOREACH(p, &pidlist, next)
155 (void)_close(fileno(p->fp));
156 _execve(_PATH_BSHELL, argv, environ);
157 _exit(127);
158 /* NOTREACHED */
159 }
160 THREAD_UNLOCK();
161
162 /* Parent. */
163 (void)_close(pdes_unused_in_parent);
164
165 /* Link into list of file descriptors. */
166 cur->fp = iop;
167 cur->pid = pid;
168 THREAD_LOCK();
169 SLIST_INSERT_HEAD(&pidlist, cur, next);
170 THREAD_UNLOCK();
171
172 /*
173 * To guard against undesired fd passing with concurrent calls,
174 * only clear the close-on-exec flag after linking the file into
175 * the list which will cause an explicit close.
176 */
177 if (!cloexec)
178 (void)_fcntl(fileno(iop), F_SETFD, 0);
179
180 return (iop);
181 }
182
183 /*
184 * pclose --
185 * Pclose returns -1 if stream is not associated with a `popened' command,
186 * if already `pclosed', or waitpid returns an error.
187 */
188 int
pclose(FILE * iop)189 pclose(FILE *iop)
190 {
191 struct pid *cur, *last = NULL;
192 int pstat;
193 pid_t pid;
194
195 /*
196 * Find the appropriate file pointer and remove it from the list.
197 */
198 THREAD_LOCK();
199 SLIST_FOREACH(cur, &pidlist, next) {
200 if (cur->fp == iop)
201 break;
202 last = cur;
203 }
204 if (cur == NULL) {
205 THREAD_UNLOCK();
206 return (-1);
207 }
208 if (last == NULL)
209 SLIST_REMOVE_HEAD(&pidlist, next);
210 else
211 SLIST_REMOVE_AFTER(last, next);
212 THREAD_UNLOCK();
213
214 (void)fclose(iop);
215
216 do {
217 pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
218 } while (pid == -1 && errno == EINTR);
219
220 free(cur);
221
222 return (pid == -1 ? -1 : pstat);
223 }
224