Add constant for maximum build age.
[build-farm.git] / timelimit.c
index af8ff274545ad04cb1fdcfde22879e57909a4499..7232ecf1610efbd5155759f3a61c24003149aa47 100644 (file)
@@ -1,5 +1,6 @@
 /* run a command with a limited timeout
    tridge@samba.org, June 2005
+   metze@samba.org, March 2006
 
    attempt to be as portable as possible (fighting posix all the way)
 */
@@ -17,22 +18,36 @@ static pid_t child_pid;
 static void usage(void)
 {
        printf("usage: timelimit <time> <command>\n");
-       printf("   SIGALRM - passes SIGKILL to command's process group and exit(1)\n");
        printf("   SIGUSR1 - passes SIGTERM to command's process group\n");
-       printf("   SIGTERM - passes SIGTERM to command's process group and exit(0)\n");
+       printf("   SIGALRM - passes SIGTERM to command's process group\n");
+       printf("             after 5s SIGKILL will be passed and exit(1)\n");
+       printf("   SIGTERM - passes SIGTERM to command's process group\n");
+       printf("             after 1s SIGKILL will be passed and exit(1)\n");
+       printf("   SIGINT  - handled like SIGTERM\n");
+       printf("   SIGQUIT - handled like SIGTERM\n");
 }
 
-static void sig_alrm(int sig)
+static void sig_alrm_kill(int sig)
 {
        fprintf(stderr, "\nMaximum time expired in timelimit - killing\n");
        kill(-child_pid, SIGKILL);
        exit(1);
 }
 
+static void sig_alrm_term(int sig)
+{
+       fprintf(stderr, "\nMaximum time expired in timelimit - sending TERM To child\n");
+       kill(-child_pid, SIGTERM);
+       signal(SIGALRM, sig_alrm_kill);
+       alarm(5);
+}
+
 static void sig_term(int sig)
 {
+       fprintf(stderr, "\nReceived TERM/INT/QUIT signal\n");
        kill(-child_pid, SIGTERM);
-       exit(0);
+       signal(SIGALRM, sig_alrm_kill);
+       alarm(1);
 }
 
 static void sig_usr1(int sig)
@@ -42,24 +57,16 @@ static void sig_usr1(int sig)
 
 static void new_process_group(void)
 {
-#ifdef BSD_SETPGRP
-       if (setpgrp(0,0) == -1) {
-               perror("setpgrp");
-               exit(1);
-       }
-#else
-       if (setpgrp() == -1) {
-               perror("setpgrp");
+       if (setpgid(0,0) == -1) {
+               perror("setpgid");
                exit(1);
        }
-#endif
 }
 
 
 int main(int argc, char *argv[])
 {
        int maxtime, ret=1;
-       pid_t pgid;
 
        if (argc < 3) {
                usage();
@@ -77,15 +84,19 @@ int main(int argc, char *argv[])
        }
 
        signal(SIGTERM, sig_term);
+       signal(SIGINT,  sig_term);
+       signal(SIGQUIT, sig_term);
        signal(SIGUSR1, sig_usr1);
-       signal(SIGALRM, sig_alrm);
+       signal(SIGALRM, sig_alrm_term);
        alarm(maxtime);
 
        do {
                int status;
                pid_t pid = wait(&status);
                if (pid != -1) {
-                       ret = WEXITSTATUS(status);
+                       if (WIFEXITED(status)) {
+                               ret = WEXITSTATUS(status);
+                       }
                } else if (errno == ECHILD) {
                        break;
                }