2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This code is derived from software written by Ken Arnold and
8 * published in UNIX Review, Vol. 6, No. 8.
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/queue.h>
57 extern char **environ;
60 SLIST_ENTRY(pid) next;
65 static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
70 * Replacement for popen(3), without stdin (which we do not use), but with
71 * stderr, proper logging, and improved command line arguments passing.
72 * Error handling is built in - if it returns, then it succeeded.
75 auto_popen(const char *argv0, ...)
80 int error, i, nullfd, outfds[2];
81 char *arg, *argv[ARGV_LEN], *command;
83 nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
85 log_err(1, "cannot open %s", _PATH_DEVNULL);
91 cur = malloc(sizeof(struct pid));
95 argv[0] = checked_strdup(argv0);
101 log_errx(1, "too many arguments to auto_popen");
102 arg = va_arg(ap, char *);
107 command = concat(command, ' ', arg);
111 cur->command = checked_strdup(command);
113 switch (pid = fork()) {
114 case -1: /* Error. */
118 dup2(nullfd, STDIN_FILENO);
119 dup2(outfds[1], STDOUT_FILENO);
125 SLIST_FOREACH(p, &pidlist, next)
126 close(fileno(p->outfp));
127 execvp(argv[0], argv);
128 log_err(1, "failed to execute %s", argv[0]);
132 log_debugx("executing \"%s\" as pid %d", command, pid);
134 /* Parent; assume fdopen cannot fail. */
135 cur->outfp = fdopen(outfds[0], "r");
139 /* Link into list of file descriptors. */
141 SLIST_INSERT_HEAD(&pidlist, cur, next);
147 auto_pclose(FILE *iop)
149 struct pid *cur, *last = NULL;
154 * Find the appropriate file pointer and remove it from the list.
156 SLIST_FOREACH(cur, &pidlist, next) {
157 if (cur->outfp == iop)
165 SLIST_REMOVE_HEAD(&pidlist, next);
167 SLIST_REMOVE_AFTER(last, next);
172 pid = wait4(cur->pid, &status, 0, NULL);
173 } while (pid == -1 && errno == EINTR);
175 if (WIFSIGNALED(status)) {
176 log_warnx("\"%s\", pid %d, terminated with signal %d",
177 cur->command, pid, WTERMSIG(status));
181 if (WEXITSTATUS(status) != 0) {
182 log_warnx("\"%s\", pid %d, terminated with exit status %d",
183 cur->command, pid, WEXITSTATUS(status));
187 log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid);
192 return (pid == -1 ? -1 : status);