2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
54 static struct fp *fp_head;
63 static struct child *child, *child_freelist = NULL;
65 static void delchild(struct child *);
66 static pid_t file_pid(FILE *);
67 static pid_t start_commandv(char *, sigset_t *, int, int, va_list);
70 Fopen(const char *path, const char *mode)
74 if ((fp = fopen(path, mode)) != NULL) {
75 register_file(fp, 0, 0);
76 (void)fcntl(fileno(fp), F_SETFD, 1);
82 Fdopen(int fd, const char *mode)
86 if ((fp = fdopen(fd, mode)) != NULL) {
87 register_file(fp, 0, 0);
88 (void)fcntl(fileno(fp), F_SETFD, 1);
102 Popen(char *cmd, const char *mode)
105 int myside, hisside, fd0, fd1;
112 (void)fcntl(p[READ], F_SETFD, 1);
113 (void)fcntl(p[WRITE], F_SETFD, 1);
116 hisside = fd0 = fd1 = p[WRITE];
119 hisside = fd0 = p[READ];
122 (void)sigemptyset(&nset);
123 pid = start_command(value("SHELL"), &nset, fd0, fd1, "-c", cmd, NULL);
125 (void)close(p[READ]);
126 (void)close(p[WRITE]);
129 (void)close(hisside);
130 if ((fp = fdopen(myside, mode)) != NULL)
131 register_file(fp, 1, pid);
142 unregister_file(ptr);
144 (void)sigemptyset(&nset);
145 (void)sigaddset(&nset, SIGINT);
146 (void)sigaddset(&nset, SIGHUP);
147 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
149 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
154 close_all_files(void)
157 while (fp_head != NULL)
159 (void)Pclose(fp_head->fp);
161 (void)Fclose(fp_head->fp);
165 register_file(FILE *fp, int pipe, pid_t pid)
169 if ((fpp = malloc(sizeof(*fpp))) == NULL)
170 err(1, "Out of memory");
179 unregister_file(FILE *fp)
183 for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
189 errx(1, "Invalid file pointer");
198 for (p = fp_head; p != NULL; p = p->link)
201 errx(1, "Invalid file pointer");
206 * Run a command without a shell, with optional arguments and splicing
207 * of stdin (-1 means none) and stdout. The command name can be a sequence
209 * Signals must be handled by the caller.
210 * "nset" contains the signals to ignore in the new process.
211 * SIGINT is enabled unless it's in "nset".
214 start_commandv(char *cmd, sigset_t *nset, int infd, int outfd, va_list args)
218 if ((pid = fork()) < 0) {
224 int i = getrawlist(cmd, argv, sizeof(argv) / sizeof(*argv));
226 while ((argv[i++] = va_arg(args, char *)))
229 prepare_child(nset, infd, outfd);
230 execvp(argv[0], argv);
238 run_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
243 va_start(args, outfd);
244 pid = start_commandv(cmd, nset, infd, outfd, args);
248 return wait_command(pid);
252 start_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
257 va_start(args, outfd);
258 r = start_commandv(cmd, nset, infd, outfd, args);
264 prepare_child(sigset_t *nset, int infd, int outfd)
270 * All file descriptors other than 0, 1, and 2 are supposed to be
277 for (i = 1; i < NSIG; i++)
278 if (nset != NULL && sigismember(nset, i))
279 (void)signal(i, SIG_IGN);
280 if (nset == NULL || !sigismember(nset, SIGINT))
281 (void)signal(SIGINT, SIG_DFL);
282 (void)sigemptyset(&eset);
283 (void)sigprocmask(SIG_SETMASK, &eset, NULL);
287 wait_command(pid_t pid)
290 if (wait_child(pid) < 0) {
291 printf("Fatal error in process.\n");
297 static struct child *
298 findchild(pid_t pid, int dont_alloc)
302 for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
308 if (child_freelist) {
309 *cpp = child_freelist;
310 child_freelist = (*cpp)->link;
312 *cpp = malloc(sizeof(struct child));
317 (*cpp)->done = (*cpp)->free = 0;
324 delchild(struct child *cp)
328 for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
331 cp->link = child_freelist;
337 sigchild(int signo __unused)
345 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
346 cp = findchild(pid, 1);
362 * Wait for a specific child to die.
365 wait_child(pid_t pid)
371 (void)sigemptyset(&nset);
372 (void)sigaddset(&nset, SIGCHLD);
373 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
375 * If we have not already waited on the pid (via sigchild)
376 * wait on it now. Otherwise, use the wait status stashed
379 cp = findchild(pid, 1);
380 if (cp == NULL || !cp->done)
381 rv = waitpid(pid, &wait_status, 0);
383 wait_status = cp->status;
386 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
387 if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
394 * Mark a child as don't care.
397 free_child(pid_t pid)
402 (void)sigemptyset(&nset);
403 (void)sigaddset(&nset, SIGCHLD);
404 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
405 if ((cp = findchild(pid, 0)) != NULL) {
411 (void)sigprocmask(SIG_SETMASK, &oset, NULL);