##// END OF EJS Templates
tests: killdaemons.py for windows distinguishes access violation and terminated...
Simon Heimberg -
r20496:acbd19b9 default
parent child Browse files
Show More
@@ -1,85 +1,92 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 import os, sys, time, errno, signal
3 import os, sys, time, errno, signal
4
4
5 if os.name =='nt':
5 if os.name =='nt':
6 import ctypes
6 import ctypes
7
7
8 def _check(ret, expectederr=None):
8 def _check(ret, expectederr=None):
9 if ret == 0:
9 if ret == 0:
10 winerrno = ctypes.GetLastError()
10 winerrno = ctypes.GetLastError()
11 if winerrno == expectederr:
11 if winerrno == expectederr:
12 return True
12 return True
13 raise ctypes.WinError(winerrno)
13 raise ctypes.WinError(winerrno)
14
14
15 def kill(pid, logfn, tryhard=True):
15 def kill(pid, logfn, tryhard=True):
16 logfn('# Killing daemon process %d' % pid)
16 logfn('# Killing daemon process %d' % pid)
17 PROCESS_TERMINATE = 1
17 PROCESS_TERMINATE = 1
18 PROCESS_QUERY_INFORMATION = 0x400
18 SYNCHRONIZE = 0x00100000L
19 SYNCHRONIZE = 0x00100000L
19 WAIT_OBJECT_0 = 0
20 WAIT_OBJECT_0 = 0
20 WAIT_TIMEOUT = 258
21 WAIT_TIMEOUT = 258
21 handle = ctypes.windll.kernel32.OpenProcess(
22 handle = ctypes.windll.kernel32.OpenProcess(
22 PROCESS_TERMINATE|SYNCHRONIZE, False, pid)
23 PROCESS_TERMINATE|SYNCHRONIZE|PROCESS_QUERY_INFORMATION,
24 False, pid)
23 if handle == 0:
25 if handle == 0:
24 _check(0, 87) # err 87 when process not found
26 _check(0, 87) # err 87 when process not found
25 return # process not found, already finished
27 return # process not found, already finished
26 try:
28 try:
27 _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5)
29 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
28 # windows error 5 when process does not exist or no access TODO
30 if r == WAIT_OBJECT_0:
31 pass # terminated, but process handle still available
32 elif r == WAIT_TIMEOUT:
33 _check(ctypes.windll.kernel32.TerminateProcess(handle, -1))
34 else:
35 _check(r)
29
36
30 # TODO?: forcefully kill when timeout
37 # TODO?: forcefully kill when timeout
31 # and ?shorter waiting time? when tryhard==True
38 # and ?shorter waiting time? when tryhard==True
32 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
39 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100)
33 # timeout = 100 ms
40 # timeout = 100 ms
34 if r == WAIT_OBJECT_0:
41 if r == WAIT_OBJECT_0:
35 pass # process is terminated
42 pass # process is terminated
36 elif r == WAIT_TIMEOUT:
43 elif r == WAIT_TIMEOUT:
37 logfn('# Daemon process %d is stuck')
44 logfn('# Daemon process %d is stuck')
38 else:
45 else:
39 check(r) # any error
46 check(r) # any error
40 except: #re-raises
47 except: #re-raises
41 ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error
48 ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error
42 raise
49 raise
43 _check(ctypes.windll.kernel32.CloseHandle(handle))
50 _check(ctypes.windll.kernel32.CloseHandle(handle))
44
51
45 else:
52 else:
46 def kill(pid, logfn, tryhard=True):
53 def kill(pid, logfn, tryhard=True):
47 try:
54 try:
48 os.kill(pid, 0)
55 os.kill(pid, 0)
49 logfn('# Killing daemon process %d' % pid)
56 logfn('# Killing daemon process %d' % pid)
50 os.kill(pid, signal.SIGTERM)
57 os.kill(pid, signal.SIGTERM)
51 if tryhard:
58 if tryhard:
52 for i in range(10):
59 for i in range(10):
53 time.sleep(0.05)
60 time.sleep(0.05)
54 os.kill(pid, 0)
61 os.kill(pid, 0)
55 else:
62 else:
56 time.sleep(0.1)
63 time.sleep(0.1)
57 os.kill(pid, 0)
64 os.kill(pid, 0)
58 logfn('# Daemon process %d is stuck - really killing it' % pid)
65 logfn('# Daemon process %d is stuck - really killing it' % pid)
59 os.kill(pid, signal.SIGKILL)
66 os.kill(pid, signal.SIGKILL)
60 except OSError, err:
67 except OSError, err:
61 if err.errno != errno.ESRCH:
68 if err.errno != errno.ESRCH:
62 raise
69 raise
63
70
64 def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
71 def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
65 if not logfn:
72 if not logfn:
66 logfn = lambda s: s
73 logfn = lambda s: s
67 # Kill off any leftover daemon processes
74 # Kill off any leftover daemon processes
68 try:
75 try:
69 fp = open(pidfile)
76 fp = open(pidfile)
70 for line in fp:
77 for line in fp:
71 try:
78 try:
72 pid = int(line)
79 pid = int(line)
73 except ValueError:
80 except ValueError:
74 continue
81 continue
75 kill(pid, logfn, tryhard)
82 kill(pid, logfn, tryhard)
76 fp.close()
83 fp.close()
77 if remove:
84 if remove:
78 os.unlink(pidfile)
85 os.unlink(pidfile)
79 except IOError:
86 except IOError:
80 pass
87 pass
81
88
82 if __name__ == '__main__':
89 if __name__ == '__main__':
83 path, = sys.argv[1:]
90 path, = sys.argv[1:]
84 killdaemons(path)
91 killdaemons(path)
85
92
General Comments 0
You need to be logged in to leave comments. Login now