gnunet_janitor.py.in 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!@PYTHON@
  2. # This file is part of GNUnet.
  3. # (C) 2011 Christian Grothoff (and other contributing authors)
  4. #
  5. # GNUnet is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published
  7. # by the Free Software Foundation; either version 2, or (at your
  8. # 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. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with GNUnet; see the file COPYING. If not, write to the
  17. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. # Boston, MA 02110-1301, USA.
  19. #
  20. # Finds any gnunet processes still running in the system and kills them
  21. #
  22. # gnunet janitor can be used by invoking `make' like this:
  23. # TESTS_ENVIRONMENT='${top_srcdir}/contrib/gnunet_janitor.py &&' make check
  24. from __future__ import print_function
  25. import os
  26. import re
  27. import subprocess
  28. import sys
  29. import shutil
  30. import time
  31. import signal
  32. import terminate
  33. if os.name == 'nt':
  34. from win32com.client import GetObject
  35. WMI = GetObject('winmgmts:')
  36. def get_process_list ():
  37. result = []
  38. if os.name == 'nt':
  39. processes = WMI.InstancesOf('Win32_Process')
  40. for p in processes:
  41. result.append ((p.Properties_('ProcessId').Value, re.sub (r'(.+)\.exe', r'\1', p.Properties_('Name').Value)))
  42. else:
  43. pids = [pid for pid in os.listdir('/proc') if pid.isdigit ()]
  44. for pid in pids:
  45. with open (os.path.join ('/proc', pid, 'cmdline'), 'rb') as p:
  46. cmdline = p.read ().split ('\x00')
  47. if len (cmdline) > 0:
  48. result.append ((pid, cmdline[0]))
  49. return result
  50. def main ():
  51. procs = get_process_list ()
  52. gnunet_procs = []
  53. for p in procs:
  54. if re.match (r'gnunet-.+', p[1]):
  55. gnunet_procs.append (p)
  56. for p in gnunet_procs:
  57. if re.match (r'gnunet-service-arm', p[1]):
  58. print ("killing arm process {0:5} {1}".format (p[0], p[1]))
  59. try:
  60. terminate.safe_terminate_process_by_pid (int (p[0]), 1)
  61. except OSError as e:
  62. print ("failed: {0}".format (e))
  63. pass
  64. for p in gnunet_procs:
  65. if not re.match (r'gnunet-service-arm', p[1]):
  66. print ("killing non-arm process {0:5} {1}".format (p[0], p[1]))
  67. try:
  68. terminate.safe_terminate_process_by_pid (int (p[0]), 1)
  69. except OSError as e:
  70. print ("failed: {0}".format (e))
  71. pass
  72. if __name__ == '__main__':
  73. sys.exit (main ())