Show More
@@ -1,54 +1,71 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 | ||||
|
8 | def _check(ret, expectederr=None): | |||
|
9 | if ret == 0: | |||
|
10 | winerrno = ctypes.GetLastError() | |||
|
11 | if winerrno == expectederr: | |||
|
12 | return True | |||
|
13 | raise ctypes.WinError(winerrno) | |||
|
14 | ||||
7 | def kill(pid, logfn, tryhard=True): |
|
15 | def kill(pid, logfn, tryhard=True): | |
8 | logfn('# Killing daemon process %d' % pid) |
|
16 | logfn('# Killing daemon process %d' % pid) | |
9 | PROCESS_TERMINATE = 1 |
|
17 | PROCESS_TERMINATE = 1 | |
10 | handle = ctypes.windll.kernel32.OpenProcess( |
|
18 | handle = ctypes.windll.kernel32.OpenProcess( | |
11 | PROCESS_TERMINATE, False, pid) |
|
19 | PROCESS_TERMINATE, False, pid) | |
12 | ctypes.windll.kernel32.TerminateProcess(handle, -1) |
|
20 | if handle == 0: | |
13 | ctypes.windll.kernel32.CloseHandle(handle) |
|
21 | # TODO: call _check(0, expected) to check if "process not found" | |
|
22 | return # process not found, already finished | |||
|
23 | try: | |||
|
24 | _check(ctypes.windll.kernel32.TerminateProcess(handle, -1), 5) | |||
|
25 | # windows error 5 when process does not exist or no access TODO | |||
|
26 | except: #re-raises | |||
|
27 | ctypes.windll.kernel32.CloseHandle(handle) # no _check, keep error | |||
|
28 | raise | |||
|
29 | _check(ctypes.windll.kernel32.CloseHandle(handle)) | |||
|
30 | ||||
14 | else: |
|
31 | else: | |
15 | def kill(pid, logfn, tryhard=True): |
|
32 | def kill(pid, logfn, tryhard=True): | |
16 | try: |
|
33 | try: | |
17 | os.kill(pid, 0) |
|
34 | os.kill(pid, 0) | |
18 | logfn('# Killing daemon process %d' % pid) |
|
35 | logfn('# Killing daemon process %d' % pid) | |
19 | os.kill(pid, signal.SIGTERM) |
|
36 | os.kill(pid, signal.SIGTERM) | |
20 | if tryhard: |
|
37 | if tryhard: | |
21 | for i in range(10): |
|
38 | for i in range(10): | |
22 | time.sleep(0.05) |
|
39 | time.sleep(0.05) | |
23 | os.kill(pid, 0) |
|
40 | os.kill(pid, 0) | |
24 | else: |
|
41 | else: | |
25 | time.sleep(0.1) |
|
42 | time.sleep(0.1) | |
26 | os.kill(pid, 0) |
|
43 | os.kill(pid, 0) | |
27 | logfn('# Daemon process %d is stuck - really killing it' % pid) |
|
44 | logfn('# Daemon process %d is stuck - really killing it' % pid) | |
28 | os.kill(pid, signal.SIGKILL) |
|
45 | os.kill(pid, signal.SIGKILL) | |
29 | except OSError, err: |
|
46 | except OSError, err: | |
30 | if err.errno != errno.ESRCH: |
|
47 | if err.errno != errno.ESRCH: | |
31 | raise |
|
48 | raise | |
32 |
|
49 | |||
33 | def killdaemons(pidfile, tryhard=True, remove=False, logfn=None): |
|
50 | def killdaemons(pidfile, tryhard=True, remove=False, logfn=None): | |
34 | if not logfn: |
|
51 | if not logfn: | |
35 | logfn = lambda s: s |
|
52 | logfn = lambda s: s | |
36 | # Kill off any leftover daemon processes |
|
53 | # Kill off any leftover daemon processes | |
37 | try: |
|
54 | try: | |
38 | fp = open(pidfile) |
|
55 | fp = open(pidfile) | |
39 | for line in fp: |
|
56 | for line in fp: | |
40 | try: |
|
57 | try: | |
41 | pid = int(line) |
|
58 | pid = int(line) | |
42 | except ValueError: |
|
59 | except ValueError: | |
43 | continue |
|
60 | continue | |
44 | kill(pid, logfn, tryhard) |
|
61 | kill(pid, logfn, tryhard) | |
45 | fp.close() |
|
62 | fp.close() | |
46 | if remove: |
|
63 | if remove: | |
47 | os.unlink(pidfile) |
|
64 | os.unlink(pidfile) | |
48 | except IOError: |
|
65 | except IOError: | |
49 | pass |
|
66 | pass | |
50 |
|
67 | |||
51 | if __name__ == '__main__': |
|
68 | if __name__ == '__main__': | |
52 | path, = sys.argv[1:] |
|
69 | path, = sys.argv[1:] | |
53 | killdaemons(path) |
|
70 | killdaemons(path) | |
54 |
|
71 |
General Comments 0
You need to be logged in to leave comments.
Login now