summaryrefslogtreecommitdiff
path: root/source3/script/tests/timelimit.c
blob: 93d7f6497e36fc0f6c23412baf03c5b3b8adafe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* run a command with a limited timeout
   tridge@samba.org, June 2005

   attempt to be as portable as possible (fighting posix all the way)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

static void usage(void)
{
	printf("usage: timelimit <time> <command>\n");
}

static void sig_alrm(int sig)
{
	kill(0, SIGKILL);
	exit(1);
}

static void sig_term_kill(int sig)
{
	static int c = 0;

	if (c > 2) {
		kill(0, SIGKILL);
		exit(0);
	}

	c++;
}

static void sig_term(int sig)
{
	kill(0, SIGTERM);
	signal(SIGTERM, sig_term_kill);
}

int main(int argc, char *argv[])
{
	int maxtime, ret=1;

	if (argc < 3) {
		usage();
		exit(1);
	}

	if (setpgrp() == -1) {
		perror("setpgrp");
		exit(1);
	}

	maxtime = atoi(argv[1]);
	signal(SIGALRM, sig_alrm);
	alarm(maxtime);
	signal(SIGTERM, sig_term);

	if (fork() == 0) {
		execvp(argv[2], argv+2);
	}

	do {
		int status;
		pid_t pid = wait(&status);
		if (pid != -1) {
			ret = WEXITSTATUS(status);
		} else if (errno == ECHILD) {
			break;
		}
	} while (1);

	exit(ret);
}