testsuite: Try accepting new output for memcpy test
[ghc.git] / testsuite / timeout / timeout.py
1 #!/usr/bin/env python
2
3 try:
4
5 import errno
6 import os
7 import signal
8 import sys
9 import time
10
11 secs = int(sys.argv[1])
12 cmd = sys.argv[2]
13
14 def killProcess(pid):
15 os.killpg(pid, signal.SIGKILL)
16 for x in range(10):
17 try:
18 time.sleep(0.3)
19 r = os.waitpid(pid, os.WNOHANG)
20 if r == (0, 0):
21 os.killpg(pid, signal.SIGKILL)
22 else:
23 return
24 except OSError as e:
25 if e.errno == errno.ECHILD:
26 return
27 else:
28 raise e
29
30 pid = os.fork()
31 if pid == 0:
32 # child
33 os.setpgrp()
34 os.execvp('/bin/sh', ['/bin/sh', '-c', cmd])
35 else:
36 # parent
37 def handler(signum, frame):
38 killProcess(pid)
39 sys.exit(99)
40 old = signal.signal(signal.SIGALRM, handler)
41 signal.alarm(secs)
42 (pid2, res) = os.waitpid(pid, 0)
43 if (os.WIFEXITED(res)):
44 sys.exit(os.WEXITSTATUS(res))
45 elif os.WIFSIGNALED(res):
46 # represent signals using the Bourne shell convention
47 sys.exit(128 + os.WTERMSIG(res))
48 else: # WIFCONTINUED or WIFSTOPPED
49 killProcess(pid)
50 sys.exit(99) # unexpected
51
52 except KeyboardInterrupt:
53 sys.exit(98)
54 except:
55 raise
56