aboutsummaryrefslogtreecommitdiff
path: root/contrib/gnunet_janitor.py.in
blob: c11ff4f122c9924d27b6516e2807fa159ed7c60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!@PYTHON@
#    This file is part of GNUnet.
#    (C) 2011 Christian Grothoff (and other contributing authors)
#
#    GNUnet is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published
#    by the Free Software Foundation; either version 2, or (at your
#    option) any later version.
#
#    GNUnet is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GNUnet; see the file COPYING.  If not, write to the
#    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#    Boston, MA 02111-1307, USA.
#
# Finds any gnunet processes still running in the system and kills them
#
# gnunet janitor can be used by invoking `make' like this:
# TESTS_ENVIRONMENT='${top_srcdir}/contrib/gnunet_janitor.py &&' make check

from __future__ import print_function
import os
import re
import subprocess
import sys
import shutil
import time
import signal

if os.name == 'nt':
  from win32com.client import GetObject
  WMI = GetObject('winmgmts:')

def get_process_list ():
  result = []
  if os.name == 'nt':
    processes = WMI.InstancesOf('Win32_Process')
    for p in processes:
      result.append ((p.Properties_('ProcessId').Value, re.sub (r'(.+)\.exe', r'\1', p.Properties_('Name').Value)))
  else:
    pids = [pid for pid in os.listdir('/proc') if pid.isdigit ()]
    for pid in pids:
      result.append ((pid, open (os.path.join ('/proc', pid, 'comm'), 'rb').read ()))
  return result

def main ():
  procs = get_process_list ()
  gnunet_procs = []
  for p in procs:
    if re.match (r'gnunet-.+', p[1]):
      gnunet_procs.append (p)
  for p in gnunet_procs:
    if re.match (r'gnunet-service-arm', p[1]):
      print ("killing arm process {0:5} {1}".format (p[0], p[1]))
      try:
        os.kill (p[0], signal.SIGTERM)
      except OSError as e:
        print ("failed: {0}".format (e))
        pass
  for p in gnunet_procs:
    if not re.match (r'gnunet-service-arm', p[1]):
      print ("killing non-arm process {0:5} {1}".format (p[0], p[1]))
      try:
        os.kill (p[0], signal.SIGTERM)
      except OSError as e:
        print ("failed: {0}".format (e))
        pass

if __name__ == '__main__':
  sys.exit (main ())