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 |