summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source3/script/tests/timelimit.c59
1 files changed, 39 insertions, 20 deletions
diff --git a/source3/script/tests/timelimit.c b/source3/script/tests/timelimit.c
index 93d7f6497e..af8ff27454 100644
--- a/source3/script/tests/timelimit.c
+++ b/source3/script/tests/timelimit.c
@@ -12,57 +12,74 @@
#include <sys/types.h>
#include <sys/wait.h>
+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");
}
static void sig_alrm(int sig)
{
- kill(0, SIGKILL);
+ fprintf(stderr, "\nMaximum time expired in timelimit - killing\n");
+ kill(-child_pid, SIGKILL);
exit(1);
}
-static void sig_term_kill(int sig)
+static void sig_term(int sig)
{
- static int c = 0;
-
- if (c > 2) {
- kill(0, SIGKILL);
- exit(0);
- }
+ kill(-child_pid, SIGTERM);
+ exit(0);
+}
- c++;
+static void sig_usr1(int sig)
+{
+ kill(-child_pid, SIGTERM);
}
-static void sig_term(int sig)
+static void new_process_group(void)
{
- kill(0, SIGTERM);
- signal(SIGTERM, sig_term_kill);
+#ifdef BSD_SETPGRP
+ if (setpgrp(0,0) == -1) {
+ perror("setpgrp");
+ exit(1);
+ }
+#else
+ if (setpgrp() == -1) {
+ perror("setpgrp");
+ exit(1);
+ }
+#endif
}
+
int main(int argc, char *argv[])
{
int maxtime, ret=1;
+ pid_t pgid;
if (argc < 3) {
usage();
exit(1);
}
- if (setpgrp() == -1) {
- perror("setpgrp");
+ maxtime = atoi(argv[1]);
+
+ child_pid = fork();
+ if (child_pid == 0) {
+ new_process_group();
+ execvp(argv[2], argv+2);
+ perror(argv[2]);
exit(1);
}
- maxtime = atoi(argv[1]);
+ signal(SIGTERM, sig_term);
+ signal(SIGUSR1, sig_usr1);
signal(SIGALRM, sig_alrm);
alarm(maxtime);
- signal(SIGTERM, sig_term);
-
- if (fork() == 0) {
- execvp(argv[2], argv+2);
- }
do {
int status;
@@ -74,5 +91,7 @@ int main(int argc, char *argv[])
}
} while (1);
+ kill(-child_pid, SIGKILL);
+
exit(ret);
}