This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.pl
executable file
·200 lines (167 loc) · 4.89 KB
/
timer.pl
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl
use strict;
use POSIX;
sub final_reap();
sub reap_children();
sub set_reap($);
sub catch_child($);
my $timeLimit = 60 * 5; # Default 5 minute wait.
my @regexList;
my $childPid = 0;
my $childStatus = 'UNDEF';
my $retVal = -1;
$SIG{ALRM} = \&set_reap;
$SIG{KILL} = \&set_reap;
$SIG{INT} = \&set_reap;
$SIG{CHLD} = \&catch_child;
if ($#ARGV < 0) {
print(STDERR "timer.pl Usage:\n");
print(STDERR "\t./timer.pl [ -t time ] [ -r regex ] program [ arguments ]\n");
print(STDERR "\t -t time\tTime limit in seconds. Default = $timeLimit.\n");
print(STDERR "\t -r regex\tRegex of processes to consider as children.\n");
print(STDERR "\n");
print(STDERR "timer.pl Description:\n");
print(STDERR "\tForks a process which executes the given program for\n");
print(STDERR "\ta limited amount of time. If the child process does\n");
print(STDERR "\tnot terminate within the time limit, timer.pl will\n");
print(STDERR "\tsend it a SIGTERM, followed by a SIGKILL.\n");
print(STDERR "\n");
exit(-1);
}
while ($ARGV[0] =~ /^-/) {
if ($ARGV[0] eq '-t') {
shift;
$timeLimit = shift;
} elsif ($ARGV[0] eq '-r') {
shift;
push(@regexList, shift);
} else {
print(STDERR "Unknown option: $ARGV[0]\n");
exit(-1);
}
}
if (@regexList == undef) {
# Default child regex list.
# ** Should/Can this regex be more specific?
@regexList = ('^\S*test\d+.mutatee',
'^\S*dyncov',
'^\S*ijpeg',
'^\S*cc1'
);
}
$childPid = fork();
die("*** Could not fork: $!") if (!defined $childPid);
if ($childPid == 0) {
#
# Child Case
#
my $group = setsid(); # Start a new process group.
exec(@ARGV) or die("*** Could not execute '$ARGV[0]': $!\n");
} else {
#
# Parent Case
#
# select((select(FILE), $| = 1)[0]);
alarm($timeLimit);
pause() if ($childStatus eq 'UNDEF'); # Wait for signal (if needed).
# At this point, $childStatus should be one of the following:
#
# 'NORMAL': Child exited normally. $retVal holds return value.
# 'SIGNAL': Child exited via signal. $retVal holds signal number.
# 'CORE': Child dumped core.
# 'ALRM': Child exceeded time limit. Parent should reap children.
# 'KILL': Parent received SIGKILL. Parent should reap children.
# 'INT': Parent received SIGINT. Parent should reap children.
reap_children() if ($childStatus eq 'ALRM' ||
$childStatus eq 'KILL' ||
$childStatus eq 'INT');
if ($childStatus eq 'SIGNAL') {
print(STDERR "*** Child terminated abnormally via signal $retVal.\n");
$retVal = -2;
} elsif ($childStatus eq 'CORE') {
print(STDERR "*** Child terminated abnormally and dumped core.\n");
$retVal = -2;
}
final_reap();
exit($retVal);
}
######################################################
# Called if timer expires, or if interrupted by user.
#
sub set_reap($) {
return if ($childStatus ne 'UNDEF');
$childStatus = shift;
$retVal = -1;
}
######################################################
# Called if child returns normally.
#
sub catch_child($) {
my $result = -1;
my $status = -1;
return if ($childStatus ne 'UNDEF');
$result = waitpid(-1, &WNOHANG);
$status = $?;
if ($result == $childPid) {
if ($? & 127) {
$childStatus = 'SIGNAL';
$retVal = ($? & 127);
} elsif ($? & 128) {
$childStatus = 'CORE';
} else {
$childStatus = 'NORMAL';
$retVal = ($? >> 8);
}
}
}
######################################################
# Reap children via process group.
#
sub reap_children() {
my $children;
if ($childStatus eq 'ALRM') {
print(STDERR "*** Process exceeded time limit. Reaping children.\n");
} else {
print(STDERR "*** SIG$childStatus received. Reaping children.\n");
}
$children = kill(SIGTERM, -$childPid);
return if ($children == 0);
sleep(5);
foreach (1 .. 3) {
$children = kill(SIGKILL, -$childPid);
last if ($children == 0);
sleep(3);
}
}
######################################################
# Reap children via external 'ps'.
#
sub final_reap()
{
my $ps = "/bin/ps -u $<"; # "$<" == Effective UID
foreach (1 .. 5) {
my $done = 1;
my @input = `$ps`; # Run "ps".
chomp(@input); # Remove trailing '\n'.
# Find start of command column via 'ps' header
my $cmdIdx = rindex(shift(@input), 'C');
die "Cannot parse '$ps' output. Exiting." if ($cmdIdx < 0);
foreach (@input) {
# Parse 'pid' and 'command' from line.
my ($pid, $aixPid) = split(' ', $_, 3);
my $cmd = substr($_, $cmdIdx);
$pid = $aixPid if ($ENV{"PLATFORM"} =~ /^rs6000-ibm-aix/);
foreach my $pattern (@regexList) {
if ($cmd =~ /$pattern/) {
print(STDERR "*** Extra process found; Reaping $pid ($cmd).\n");
kill(SIGKILL, $pid);
kill(SIGCONT, $pid);
$done = 0;
}
}
}
# Wait 3 seconds if we sent any signals.
sleep(3) if ($done == 0);
last if ($done == 1);
}
}