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 SYNCHRONIZE = 0x00100000L |
|
19 WAIT_OBJECT_0 = 0 |
|
20 WAIT_TIMEOUT = 258 |
18 handle = ctypes.windll.kernel32.OpenProcess( |
21 handle = ctypes.windll.kernel32.OpenProcess( |
19 PROCESS_TERMINATE, False, pid) |
22 PROCESS_TERMINATE|SYNCHRONIZE, False, pid) |
20 if handle == 0: |
23 if handle == 0: |
21 # TODO: call _check(0, expected) to check if "process not found" |
24 # TODO: call _check(0, expected) to check if "process not found" |
22 return # process not found, already finished |
25 return # process not found, already finished |
23 try: |
26 try: |
24 _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5) |
27 _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5) |
25 # windows error 5 when process does not exist or no access TODO |
28 # windows error 5 when process does not exist or no access TODO |
|
29 |
|
30 # TODO?: forcefully kill when timeout |
|
31 # and ?shorter waiting time? when tryhard==True |
|
32 r = ctypes.windll.kernel32.WaitForSingleObject(handle, 100) |
|
33 # timeout = 100 ms |
|
34 if r == WAIT_OBJECT_0: |
|
35 pass # process is terminated |
|
36 elif r == WAIT_TIMEOUT: |
|
37 logfn('# Daemon process %d is stuck') |
|
38 else: |
|
39 check(r) # any error |
26 except: #re-raises |
40 except: #re-raises |
27 ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error |
41 ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error |
28 raise |
42 raise |
29 _check(ctypes.windll.kernel32.CloseHandle(handle)) |
43 _check(ctypes.windll.kernel32.CloseHandle(handle)) |
30 |
44 |