blob: 6a57ac2f8212c2327a3a44ef17e2a53b1d0d190d (
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
|
#!/usr/bin/env python
try:
import errno
import os
import signal
import sys
import time
secs = int(sys.argv[1])
cmd = sys.argv[2]
def killProcess(pid):
os.killpg(pid, signal.SIGKILL)
for x in range(10):
try:
time.sleep(0.3)
r = os.waitpid(pid, os.WNOHANG)
if r == (0, 0):
os.killpg(pid, signal.SIGKILL)
else:
return
except OSError, e:
if e.errno == errno.ECHILD:
return
else:
raise e
pid = os.fork()
if pid == 0:
# child
os.setpgrp()
os.execvp('/bin/sh', ['/bin/sh', '-c', cmd])
else:
# parent
def handler(signum, frame):
sys.stderr.write('Timeout happened...killing process...\n')
killProcess(pid)
sys.exit(99)
old = signal.signal(signal.SIGALRM, handler)
signal.alarm(secs)
(pid2, res) = os.waitpid(pid, 0)
if (os.WIFEXITED(res)):
sys.exit(os.WEXITSTATUS(res))
else:
sys.exit(res)
except KeyboardInterrupt:
sys.exit(98)
except:
raise
|