summaryrefslogtreecommitdiff
path: root/source3/script/tests/timelimit.c
blob: 5b8281cb0dc6ad0b22421e5b8a807abb21470ebb (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* 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)
*/
#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 pid_t child_pid;

static void usage(void)
{
	printf("usage: timelimit <time> <command>\n");
	printf("   SIGUSR1 - passes SIGTERM to command's process group\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");
}

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)
{
	kill(-child_pid, SIGTERM);
	alarm(5);
	signal(SIGALRM, sig_alrm_kill);
}

static void sig_term(int sig)
{
	kill(-child_pid, SIGTERM);
	alarm(1);
	signal(SIGALRM, sig_alrm_kill);
}

static void sig_usr1(int sig)
{
	kill(-child_pid, SIGTERM);
}

static void new_process_group(void)
{
#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;

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

	maxtime = atoi(argv[1]);

	child_pid = fork();
	if (child_pid == 0) {
		new_process_group();
		execvp(argv[2], argv+2);
		perror(argv[2]);
		exit(1);
	}

	signal(SIGTERM, sig_term);
	signal(SIGUSR1, sig_usr1);
	signal(SIGALRM, sig_alrm_term);
	alarm(maxtime);

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

	kill(-child_pid, SIGKILL);

	exit(ret);
}