2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
56 static struct fp *fp_head;
65 static struct child *child, *child_freelist = NULL;
67 static void delchild(struct child *);
68 static pid_t file_pid(FILE *);
69 static pid_t start_commandv(char *, sigset_t *, int, int, va_list);
72 Fopen(const char *path, const char *mode)
76 if ((fp = fopen(path, mode)) != NULL) {
77 register_file(fp, 0, 0);
78 (void)fcntl(fileno(fp), F_SETFD, 1);
84 Fdopen(int fd, const char *mode)
88 if ((fp = fdopen(fd, mode)) != NULL) {
89 register_file(fp, 0, 0);
90 (void)fcntl(fileno(fp), F_SETFD, 1);
104 Popen(char *cmd, const char *mode)
107 int myside, hisside, fd0, fd1;
114 (void)fcntl(p[READ], F_SETFD, 1);
115 (void)fcntl(p[WRITE], F_SETFD, 1);
118 hisside = fd0 = fd1 = p[WRITE];
121 hisside = fd0 = p[READ];
124 (void)sigemptyset(&nset);
125 pid = start_command(value("SHELL"), &nset, fd0, fd1, "-c", cmd, NULL);
127 (void)close(p[READ]);
128 (void)close(p[WRITE]);
131 (void)close(hisside);
132 if ((fp = fdopen(myside, mode)) != NULL)
133 register_file(fp, 1, pid);
144 unregister_file(ptr);
146 (void)sigemptyset(&nset);
147 (void)sigaddset(&nset, SIGINT);
148 (void)sigaddset(&nset, SIGHUP);
149 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
151 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
156 close_all_files(void)
159 while (fp_head != NULL)
161 (void)Pclose(fp_head->fp);
163 (void)Fclose(fp_head->fp);
167 register_file(FILE *fp, int pipe, pid_t pid)
171 if ((fpp = malloc(sizeof(*fpp))) == NULL)
172 err(1, "Out of memory");
181 unregister_file(FILE *fp)
185 for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
191 errx(1, "Invalid file pointer");
200 for (p = fp_head; p != NULL; p = p->link)
203 errx(1, "Invalid file pointer");
208 * Run a command without a shell, with optional arguments and splicing
209 * of stdin (-1 means none) and stdout. The command name can be a sequence
211 * Signals must be handled by the caller.
212 * "nset" contains the signals to ignore in the new process.
213 * SIGINT is enabled unless it's in "nset".
216 start_commandv(char *cmd, sigset_t *nset, int infd, int outfd, va_list args)
220 if ((pid = fork()) < 0) {
226 int i = getrawlist(cmd, argv, sizeof(argv) / sizeof(*argv));
228 while ((argv[i++] = va_arg(args, char *)))
231 prepare_child(nset, infd, outfd);
232 execvp(argv[0], argv);
240 run_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
245 va_start(args, outfd);
246 pid = start_commandv(cmd, nset, infd, outfd, args);
250 return wait_command(pid);
254 start_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
259 va_start(args, outfd);
260 r = start_commandv(cmd, nset, infd, outfd, args);
266 prepare_child(sigset_t *nset, int infd, int outfd)
272 * All file descriptors other than 0, 1, and 2 are supposed to be
279 for (i = 1; i < NSIG; i++)
280 if (nset != NULL && sigismember(nset, i))
281 (void)signal(i, SIG_IGN);
282 if (nset == NULL || !sigismember(nset, SIGINT))
283 (void)signal(SIGINT, SIG_DFL);
284 (void)sigemptyset(&eset);
285 (void)sigprocmask(SIG_SETMASK, &eset, NULL);
289 wait_command(pid_t pid)
292 if (wait_child(pid) < 0) {
293 printf("Fatal error in process.\n");
299 static struct child *
300 findchild(pid_t pid, int dont_alloc)
304 for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
310 if (child_freelist) {
311 *cpp = child_freelist;
312 child_freelist = (*cpp)->link;
314 *cpp = malloc(sizeof(struct child));
319 (*cpp)->done = (*cpp)->free = 0;
326 delchild(struct child *cp)
330 for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
333 cp->link = child_freelist;
339 sigchild(int signo __unused)
347 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
348 cp = findchild(pid, 1);
364 * Wait for a specific child to die.
367 wait_child(pid_t pid)
373 (void)sigemptyset(&nset);
374 (void)sigaddset(&nset, SIGCHLD);
375 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
377 * If we have not already waited on the pid (via sigchild)
378 * wait on it now. Otherwise, use the wait status stashed
381 cp = findchild(pid, 1);
382 if (cp == NULL || !cp->done)
383 rv = waitpid(pid, &wait_status, 0);
385 wait_status = cp->status;
388 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
389 if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
396 * Mark a child as don't care.
399 free_child(pid_t pid)
404 (void)sigemptyset(&nset);
405 (void)sigaddset(&nset, SIGCHLD);
406 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
407 if ((cp = findchild(pid, 0)) != NULL) {
413 (void)sigprocmask(SIG_SETMASK, &oset, NULL);