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