diff options
Diffstat (limited to 'contrib/scripts/gnunet_janitor.py.in')
-rw-r--r-- | contrib/scripts/gnunet_janitor.py.in | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/contrib/scripts/gnunet_janitor.py.in b/contrib/scripts/gnunet_janitor.py.in new file mode 100644 index 000000000..cbe96a8eb --- /dev/null +++ b/contrib/scripts/gnunet_janitor.py.in | |||
@@ -0,0 +1,79 @@ | |||
1 | #!@PYTHON@ | ||
2 | # This file is part of GNUnet. | ||
3 | # (C) 2011, 2018 Christian Grothoff (and other contributing authors) | ||
4 | # | ||
5 | # GNUnet is free software: you can redistribute it and/or modify it | ||
6 | # under the terms of the GNU Affero General Public License as published | ||
7 | # by the Free Software Foundation, either version 3 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # GNUnet is distributed in the hope that it will be useful, but | ||
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | # Affero General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU Affero General Public License | ||
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | # | ||
18 | # Finds any gnunet processes still running in the system and kills them | ||
19 | # | ||
20 | # gnunet janitor can be used by invoking `make' like this: | ||
21 | # TESTS_ENVIRONMENT='${top_srcdir}/contrib/scripts/gnunet_janitor.py &&' make check | ||
22 | |||
23 | from __future__ import print_function | ||
24 | import os | ||
25 | import re | ||
26 | import subprocess | ||
27 | import sys | ||
28 | import shutil | ||
29 | import time | ||
30 | import signal | ||
31 | import terminate | ||
32 | |||
33 | if os.name == 'nt': | ||
34 | from win32com.client import GetObject | ||
35 | WMI = GetObject('winmgmts:') | ||
36 | |||
37 | |||
38 | def get_process_list(): | ||
39 | result = [] | ||
40 | if os.name == 'nt': | ||
41 | processes = WMI.InstancesOf('Win32_Process') | ||
42 | for p in processes: | ||
43 | result.append((p.Properties_('ProcessId').Value, re.sub(r'(.+)\.exe', r'\1', p.Properties_('Name').Value))) | ||
44 | else: | ||
45 | pids = [pid for pid in os.listdir('/proc') if pid.isdigit()] | ||
46 | for pid in pids: | ||
47 | with open(os.path.join('/proc', pid, 'cmdline'), 'rb') as p: | ||
48 | cmdline = p.read().split('\x00') | ||
49 | if len(cmdline) > 0: | ||
50 | result.append((pid, cmdline[0])) | ||
51 | return result | ||
52 | |||
53 | |||
54 | def main(): | ||
55 | procs = get_process_list() | ||
56 | gnunet_procs = [] | ||
57 | for p in procs: | ||
58 | if re.match(r'gnunet-.+', p[1]): | ||
59 | gnunet_procs.append(p) | ||
60 | for p in gnunet_procs: | ||
61 | if re.match(r'gnunet-service-arm', p[1]): | ||
62 | print("killing arm process {0:5} {1}".format(p[0], p[1])) | ||
63 | try: | ||
64 | terminate.safe_terminate_process_by_pid(int(p[0]), 1) | ||
65 | except OSError as e: | ||
66 | print("failed: {0}".format(e)) | ||
67 | pass | ||
68 | for p in gnunet_procs: | ||
69 | if not re.match(r'gnunet-service-arm', p[1]): | ||
70 | print("killing non-arm process {0:5} {1}".format(p[0], p[1])) | ||
71 | try: | ||
72 | terminate.safe_terminate_process_by_pid(int(p[0]), 1) | ||
73 | except OSError as e: | ||
74 | print("failed: {0}".format(e)) | ||
75 | pass | ||
76 | |||
77 | |||
78 | if __name__ == '__main__': | ||
79 | sys.exit(main()) | ||