2 * Copyright (c) 1998 - 2001, 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * 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.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: simple_exec.c 21005 2007-06-08 01:54:35Z lha $");
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
44 #ifdef HAVE_SYS_WAIT_H
55 #define EX_NOTFOUND 127
58 -1 on `unspecified' system errors
62 0- is return value from subprocess
63 126 if the program couldn't be executed
64 127 if the program couldn't be found
65 128- is 128 + signal that killed subprocess
67 possible values `func' can return:
68 ((time_t)-2) exit loop w/o killing child and return
69 `exec timeout'/-4 from simple_exec
70 ((time_t)-1) kill child with SIGTERM and wait for child to exit
72 n seconds to next timeout
84 int ROKEN_LIB_FUNCTION
85 wait_for_process_timed(pid_t pid, time_t (*func)(void *),
86 void *ptr, time_t timeout)
88 RETSIGTYPE (*old_func)(int sig) = NULL;
89 unsigned int oldtime = 0;
95 old_func = signal(SIGALRM, sigtimeout);
96 oldtime = alarm(timeout);
102 while(waitpid(pid, &status, 0) < 0) {
103 if (errno != EINTR) {
111 timeout = (*func)(ptr);
112 if (timeout == (time_t)-1) {
115 } else if (timeout == (time_t)-2) {
121 if(WIFSTOPPED(status))
123 if(WIFEXITED(status)) {
124 ret = WEXITSTATUS(status);
127 if(WIFSIGNALED(status)) {
128 ret = WTERMSIG(status) + 128;
134 signal(SIGALRM, old_func);
140 int ROKEN_LIB_FUNCTION
141 wait_for_process(pid_t pid)
143 return wait_for_process_timed(pid, NULL, NULL, 0);
146 int ROKEN_LIB_FUNCTION
147 pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
148 const char *file, ...)
150 int in_fd[2], out_fd[2], err_fd[2];
157 if(stdout_fd != NULL)
159 if(stderr_fd != NULL)
165 argv = vstrcollect(&ap);
170 /* close pipes we're not interested in */
173 if(stdout_fd != NULL)
175 if(stderr_fd != NULL)
178 /* pipe everything caller doesn't care about to /dev/null */
180 in_fd[0] = open(_PATH_DEVNULL, O_RDONLY);
181 if(stdout_fd == NULL)
182 out_fd[1] = open(_PATH_DEVNULL, O_WRONLY);
183 if(stderr_fd == NULL)
184 err_fd[1] = open(_PATH_DEVNULL, O_WRONLY);
186 /* move to proper descriptors */
187 if(in_fd[0] != STDIN_FILENO) {
188 dup2(in_fd[0], STDIN_FILENO);
191 if(out_fd[1] != STDOUT_FILENO) {
192 dup2(out_fd[1], STDOUT_FILENO);
195 if(err_fd[1] != STDERR_FILENO) {
196 dup2(err_fd[1], STDERR_FILENO);
203 exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
205 if(stdin_fd != NULL) {
209 if(stdout_fd != NULL) {
213 if(stderr_fd != NULL) {
219 if(stdin_fd != NULL) {
221 *stdin_fd = fdopen(in_fd[1], "w");
223 if(stdout_fd != NULL) {
225 *stdout_fd = fdopen(out_fd[0], "r");
227 if(stderr_fd != NULL) {
229 *stderr_fd = fdopen(err_fd[0], "r");
235 int ROKEN_LIB_FUNCTION
236 simple_execvp_timed(const char *file, char *const args[],
237 time_t (*func)(void *), void *ptr, time_t timeout)
245 exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
247 return wait_for_process_timed(pid, func, ptr, timeout);
251 int ROKEN_LIB_FUNCTION
252 simple_execvp(const char *file, char *const args[])
254 return simple_execvp_timed(file, args, NULL, NULL, 0);
257 /* gee, I'd like a execvpe */
258 int ROKEN_LIB_FUNCTION
259 simple_execve_timed(const char *file, char *const args[], char *const envp[],
260 time_t (*func)(void *), void *ptr, time_t timeout)
267 execve(file, args, envp);
268 exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
270 return wait_for_process_timed(pid, func, ptr, timeout);
274 int ROKEN_LIB_FUNCTION
275 simple_execve(const char *file, char *const args[], char *const envp[])
277 return simple_execve_timed(file, args, envp, NULL, NULL, 0);
280 int ROKEN_LIB_FUNCTION
281 simple_execlp(const char *file, ...)
288 argv = vstrcollect(&ap);
292 ret = simple_execvp(file, argv);
297 int ROKEN_LIB_FUNCTION
298 simple_execle(const char *file, ... /* ,char *const envp[] */)
306 argv = vstrcollect(&ap);
307 envp = va_arg(ap, char **);
311 ret = simple_execve(file, argv, envp);
316 int ROKEN_LIB_FUNCTION
317 simple_execl(const char *file, ...)
324 argv = vstrcollect(&ap);
328 ret = simple_execve(file, argv, environ);