summaryrefslogtreecommitdiff
path: root/testsuite/timeout/timeout.py
blob: 7b4658642cb42073be33dacf160178713389fb60 (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
#!/usr/bin/python

import os
import signal
import sys

secs = int(sys.argv[1])
cmd = sys.argv[2]

pid = os.fork()
# XXX error checking
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')
        os.killpg(pid, signal.SIGKILL) # XXX Kill better like .hs
        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)