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