| 1 |
/* |
| 2 |
* Copyright (c) 1998 - 2001, 2004 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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 |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include <stdarg.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#ifdef HAVE_SYS_TYPES_H |
| 39 |
#include <sys/types.h> |
| 40 |
#endif |
| 41 |
#ifdef HAVE_SYS_WAIT_H |
| 42 |
#include <sys/wait.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_UNISTD_H |
| 45 |
#include <unistd.h> |
| 46 |
#endif |
| 47 |
#include <errno.h> |
| 48 |
|
| 49 |
#include "roken.h" |
| 50 |
|
| 51 |
#define EX_NOEXEC 126 |
| 52 |
#define EX_NOTFOUND 127 |
| 53 |
|
| 54 |
/* return values: |
| 55 |
SE_E_UNSPECIFIED on `unspecified' system errors |
| 56 |
SE_E_FORKFAILED on fork failures |
| 57 |
SE_E_WAITPIDFAILED on waitpid errors |
| 58 |
SE_E_EXECTIMEOUT exec timeout |
| 59 |
0- is return value from subprocess |
| 60 |
SE_E_NOEXEC if the program couldn't be executed |
| 61 |
SE_E_NOTFOUND if the program couldn't be found |
| 62 |
128- is 128 + signal that killed subprocess |
| 63 |
|
| 64 |
possible values `func' can return: |
| 65 |
((time_t)-2) exit loop w/o killing child and return |
| 66 |
`exec timeout'/-4 from simple_exec |
| 67 |
((time_t)-1) kill child with SIGTERM and wait for child to exit |
| 68 |
0 don't timeout again |
| 69 |
n seconds to next timeout |
| 70 |
*/ |
| 71 |
|
| 72 |
static int sig_alarm; |
| 73 |
|
| 74 |
static RETSIGTYPE |
| 75 |
sigtimeout(int sig) |
| 76 |
{ |
| 77 |
sig_alarm = 1; |
| 78 |
SIGRETURN(0); |
| 79 |
} |
| 80 |
|
| 81 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 82 |
wait_for_process_timed(pid_t pid, time_t (*func)(void *), |
| 83 |
void *ptr, time_t timeout) |
| 84 |
{ |
| 85 |
RETSIGTYPE (*old_func)(int sig) = NULL; |
| 86 |
unsigned int oldtime = 0; |
| 87 |
int ret; |
| 88 |
|
| 89 |
sig_alarm = 0; |
| 90 |
|
| 91 |
if (func) { |
| 92 |
old_func = signal(SIGALRM, sigtimeout); |
| 93 |
oldtime = alarm(timeout); |
| 94 |
} |
| 95 |
|
| 96 |
while(1) { |
| 97 |
int status; |
| 98 |
|
| 99 |
while(waitpid(pid, &status, 0) < 0) { |
| 100 |
if (errno != EINTR) { |
| 101 |
ret = SE_E_WAITPIDFAILED; |
| 102 |
goto out; |
| 103 |
} |
| 104 |
if (func == NULL) |
| 105 |
continue; |
| 106 |
if (sig_alarm == 0) |
| 107 |
continue; |
| 108 |
timeout = (*func)(ptr); |
| 109 |
if (timeout == (time_t)-1) { |
| 110 |
kill(pid, SIGTERM); |
| 111 |
continue; |
| 112 |
} else if (timeout == (time_t)-2) { |
| 113 |
ret = SE_E_EXECTIMEOUT; |
| 114 |
goto out; |
| 115 |
} |
| 116 |
alarm(timeout); |
| 117 |
} |
| 118 |
if(WIFSTOPPED(status)) |
| 119 |
continue; |
| 120 |
if(WIFEXITED(status)) { |
| 121 |
ret = WEXITSTATUS(status); |
| 122 |
break; |
| 123 |
} |
| 124 |
if(WIFSIGNALED(status)) { |
| 125 |
ret = WTERMSIG(status) + 128; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
out: |
| 130 |
if (func) { |
| 131 |
signal(SIGALRM, old_func); |
| 132 |
alarm(oldtime); |
| 133 |
} |
| 134 |
return ret; |
| 135 |
} |
| 136 |
|
| 137 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 138 |
wait_for_process(pid_t pid) |
| 139 |
{ |
| 140 |
return wait_for_process_timed(pid, NULL, NULL, 0); |
| 141 |
} |
| 142 |
|
| 143 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 144 |
pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd, |
| 145 |
const char *file, ...) |
| 146 |
{ |
| 147 |
int in_fd[2] = {-1, -1}; |
| 148 |
int out_fd[2] = {-1, -1}; |
| 149 |
int err_fd[2] = {-1, -1}; |
| 150 |
pid_t pid; |
| 151 |
va_list ap; |
| 152 |
char **argv; |
| 153 |
int ret = 0; |
| 154 |
|
| 155 |
if(stdin_fd != NULL) |
| 156 |
ret = pipe(in_fd); |
| 157 |
if(ret != -1 && stdout_fd != NULL) |
| 158 |
ret = pipe(out_fd); |
| 159 |
if(ret != -1 && stderr_fd != NULL) |
| 160 |
ret = pipe(err_fd); |
| 161 |
|
| 162 |
if (ret == -1) { |
| 163 |
close(in_fd[0]); |
| 164 |
close(in_fd[1]); |
| 165 |
close(out_fd[0]); |
| 166 |
close(out_fd[1]); |
| 167 |
close(err_fd[0]); |
| 168 |
close(err_fd[1]); |
| 169 |
return SE_E_UNSPECIFIED; |
| 170 |
} |
| 171 |
|
| 172 |
pid = fork(); |
| 173 |
switch(pid) { |
| 174 |
case 0: |
| 175 |
va_start(ap, file); |
| 176 |
argv = vstrcollect(&ap); |
| 177 |
va_end(ap); |
| 178 |
if(argv == NULL) |
| 179 |
exit(-1); |
| 180 |
|
| 181 |
/* close pipes we're not interested in */ |
| 182 |
if(stdin_fd != NULL) |
| 183 |
close(in_fd[1]); |
| 184 |
if(stdout_fd != NULL) |
| 185 |
close(out_fd[0]); |
| 186 |
if(stderr_fd != NULL) |
| 187 |
close(err_fd[0]); |
| 188 |
|
| 189 |
/* pipe everything caller doesn't care about to /dev/null */ |
| 190 |
if(stdin_fd == NULL) |
| 191 |
in_fd[0] = open(_PATH_DEVNULL, O_RDONLY); |
| 192 |
if(stdout_fd == NULL) |
| 193 |
out_fd[1] = open(_PATH_DEVNULL, O_WRONLY); |
| 194 |
if(stderr_fd == NULL) |
| 195 |
err_fd[1] = open(_PATH_DEVNULL, O_WRONLY); |
| 196 |
|
| 197 |
/* move to proper descriptors */ |
| 198 |
if(in_fd[0] != STDIN_FILENO) { |
| 199 |
dup2(in_fd[0], STDIN_FILENO); |
| 200 |
close(in_fd[0]); |
| 201 |
} |
| 202 |
if(out_fd[1] != STDOUT_FILENO) { |
| 203 |
dup2(out_fd[1], STDOUT_FILENO); |
| 204 |
close(out_fd[1]); |
| 205 |
} |
| 206 |
if(err_fd[1] != STDERR_FILENO) { |
| 207 |
dup2(err_fd[1], STDERR_FILENO); |
| 208 |
close(err_fd[1]); |
| 209 |
} |
| 210 |
|
| 211 |
closefrom(3); |
| 212 |
|
| 213 |
execv(file, argv); |
| 214 |
exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); |
| 215 |
case -1: |
| 216 |
if(stdin_fd != NULL) { |
| 217 |
close(in_fd[0]); |
| 218 |
close(in_fd[1]); |
| 219 |
} |
| 220 |
if(stdout_fd != NULL) { |
| 221 |
close(out_fd[0]); |
| 222 |
close(out_fd[1]); |
| 223 |
} |
| 224 |
if(stderr_fd != NULL) { |
| 225 |
close(err_fd[0]); |
| 226 |
close(err_fd[1]); |
| 227 |
} |
| 228 |
return SE_E_FORKFAILED; |
| 229 |
default: |
| 230 |
if(stdin_fd != NULL) { |
| 231 |
close(in_fd[0]); |
| 232 |
*stdin_fd = fdopen(in_fd[1], "w"); |
| 233 |
} |
| 234 |
if(stdout_fd != NULL) { |
| 235 |
close(out_fd[1]); |
| 236 |
*stdout_fd = fdopen(out_fd[0], "r"); |
| 237 |
} |
| 238 |
if(stderr_fd != NULL) { |
| 239 |
close(err_fd[1]); |
| 240 |
*stderr_fd = fdopen(err_fd[0], "r"); |
| 241 |
} |
| 242 |
} |
| 243 |
return pid; |
| 244 |
} |
| 245 |
|
| 246 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 247 |
simple_execvp_timed(const char *file, char *const args[], |
| 248 |
time_t (*func)(void *), void *ptr, time_t timeout) |
| 249 |
{ |
| 250 |
pid_t pid = fork(); |
| 251 |
switch(pid){ |
| 252 |
case -1: |
| 253 |
return SE_E_FORKFAILED; |
| 254 |
case 0: |
| 255 |
execvp(file, args); |
| 256 |
exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); |
| 257 |
default: |
| 258 |
return wait_for_process_timed(pid, func, ptr, timeout); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 263 |
simple_execvp(const char *file, char *const args[]) |
| 264 |
{ |
| 265 |
return simple_execvp_timed(file, args, NULL, NULL, 0); |
| 266 |
} |
| 267 |
|
| 268 |
/* gee, I'd like a execvpe */ |
| 269 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 270 |
simple_execve_timed(const char *file, char *const args[], char *const envp[], |
| 271 |
time_t (*func)(void *), void *ptr, time_t timeout) |
| 272 |
{ |
| 273 |
pid_t pid = fork(); |
| 274 |
switch(pid){ |
| 275 |
case -1: |
| 276 |
return SE_E_FORKFAILED; |
| 277 |
case 0: |
| 278 |
execve(file, args, envp); |
| 279 |
exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); |
| 280 |
default: |
| 281 |
return wait_for_process_timed(pid, func, ptr, timeout); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 286 |
simple_execve(const char *file, char *const args[], char *const envp[]) |
| 287 |
{ |
| 288 |
return simple_execve_timed(file, args, envp, NULL, NULL, 0); |
| 289 |
} |
| 290 |
|
| 291 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 292 |
simple_execlp(const char *file, ...) |
| 293 |
{ |
| 294 |
va_list ap; |
| 295 |
char **argv; |
| 296 |
int ret; |
| 297 |
|
| 298 |
va_start(ap, file); |
| 299 |
argv = vstrcollect(&ap); |
| 300 |
va_end(ap); |
| 301 |
if(argv == NULL) |
| 302 |
return SE_E_UNSPECIFIED; |
| 303 |
ret = simple_execvp(file, argv); |
| 304 |
free(argv); |
| 305 |
return ret; |
| 306 |
} |
| 307 |
|
| 308 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 309 |
simple_execle(const char *file, ... /* ,char *const envp[] */) |
| 310 |
{ |
| 311 |
va_list ap; |
| 312 |
char **argv; |
| 313 |
char *const* envp; |
| 314 |
int ret; |
| 315 |
|
| 316 |
va_start(ap, file); |
| 317 |
argv = vstrcollect(&ap); |
| 318 |
envp = va_arg(ap, char **); |
| 319 |
va_end(ap); |
| 320 |
if(argv == NULL) |
| 321 |
return SE_E_UNSPECIFIED; |
| 322 |
ret = simple_execve(file, argv, envp); |
| 323 |
free(argv); |
| 324 |
return ret; |
| 325 |
} |