| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <time.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/wait.h> |
| 8 |
|
| 9 |
static int execute(char* argv[]) |
| 10 |
{ |
| 11 |
pid_t my_pid; |
| 12 |
int status, timeout; |
| 13 |
struct timespec duration; |
| 14 |
duration.tv_sec = 0; |
| 15 |
duration.tv_nsec = 100000000; |
| 16 |
|
| 17 |
if (0 == (my_pid = fork())) { |
| 18 |
if (-1 == execve(argv[0], (char**)argv, NULL)) { |
| 19 |
perror("child process execve failed"); |
| 20 |
return -1; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
timeout = 100; |
| 25 |
while (0 == waitpid(my_pid, &status, WNOHANG)) { |
| 26 |
if (--timeout < 0) { |
| 27 |
perror(argv[0]); |
| 28 |
return -1; |
| 29 |
} |
| 30 |
nanosleep(&duration, NULL); |
| 31 |
} |
| 32 |
|
| 33 |
if (1 != WIFEXITED(status) || 0 != WEXITSTATUS(status)) { |
| 34 |
return -1; |
| 35 |
} |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
int main(int argc, char* argv[]) |
| 41 |
{ |
| 42 |
if (argc < 2) { |
| 43 |
fprintf(stderr, "%s: require at least one argument.\n", argv[0]); |
| 44 |
return -1; |
| 45 |
} |
| 46 |
|
| 47 |
return execute(&argv[1]); |
| 48 |
} |