1 /* run a command with a limited timeout
2 tridge@samba.org, June 2005
4 attempt to be as portable as possible (fighting posix all the way)
12 #include <sys/types.h>
15 static pid_t child_pid;
17 static void usage(void)
19 printf("usage: timelimit <time> <command>\n");
20 printf(" SIGALRM - passes SIGKILL to command's process group and exit(1)\n");
21 printf(" SIGUSR1 - passes SIGTERM to command's process group\n");
22 printf(" SIGTERM - passes SIGTERM to command's process group and exit(0)\n");
25 static void sig_alrm(int sig)
27 fprintf(stderr, "\nMaximum time expired in timelimit - killing\n");
28 kill(-child_pid, SIGKILL);
32 static void sig_term(int sig)
34 kill(-child_pid, SIGTERM);
38 static void sig_usr1(int sig)
40 kill(-child_pid, SIGTERM);
43 static void new_process_group(void)
46 if (setpgrp(0,0) == -1) {
51 if (setpgrp() == -1) {
59 int main(int argc, char *argv[])
69 maxtime = atoi(argv[1]);
74 execvp(argv[2], argv+2);
79 signal(SIGTERM, sig_term);
80 signal(SIGUSR1, sig_usr1);
81 signal(SIGALRM, sig_alrm);
86 pid_t pid = wait(&status);
88 ret = WEXITSTATUS(status);
89 } else if (errno == ECHILD) {
94 kill(-child_pid, SIGKILL);