Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import errno | 7 import errno |
| 8 import multiprocessing | |
| 8 import os | 9 import os |
| 9 import signal | 10 import signal |
| 10 import subprocess | 11 import subprocess |
| 11 import sys | 12 import sys |
| 12 import time | 13 import time |
| 13 | 14 |
| 14 from slave import goma_utils | 15 from slave import goma_utils |
| 15 | 16 |
| 16 | 17 |
| 17 def start_cloudtail(args): | 18 def start_cloudtail(args): |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 47 return False | 48 return False |
| 48 raise e | 49 raise e |
| 49 return True | 50 return True |
| 50 | 51 |
| 51 | 52 |
| 52 class NotDiedError(Exception): | 53 class NotDiedError(Exception): |
| 53 def __str__(self): | 54 def __str__(self): |
| 54 return "NotDiedError" | 55 return "NotDiedError" |
| 55 | 56 |
| 56 | 57 |
| 58 def wait_termination_win(pid): | |
|
ukai
2016/10/25 07:30:24
doc string?
Yoshisato Yanagisawa
2016/10/25 07:48:20
Done.
| |
| 59 import win32api | |
| 60 import win32con | |
| 61 import pywintypes | |
| 62 try: | |
| 63 handle = win32api.OpenProcess( | |
|
ukai
2016/10/25 07:30:24
no need to close the handle?
Yoshisato Yanagisawa
2016/10/25 07:48:20
Done.
| |
| 64 win32con.PROCESS_QUERY_INFORMATION | win32con.SYNCHRONIZE, | |
| 65 False, pid) | |
| 66 try: | |
| 67 os.kill(pid, signal.CTRL_C_EVENT) | |
| 68 print('CTRL_C_EVENT has been sent to process %d. ' | |
| 69 'Going to wait for the process finishes.' % pid) | |
| 70 except WindowsError as e: # pylint: disable=E0602 | |
| 71 # If a target process does not share terminal, we cannot send Ctrl-C. | |
| 72 if e.errno == 87: # 87 == invalid parameter | |
| 73 os.kill(pid, signal.SIGINT) | |
| 74 os.waitpid(handle, 0) | |
| 75 return None | |
| 76 except pywintypes.error as e: | |
| 77 if e[0] == 87 and e[1] == 'OpenProcess': | |
| 78 print('Can\'t open process %d. Already dead? error %d.' % (pid, e)) | |
| 79 return None | |
| 80 raise | |
| 81 except OSError as e: | |
| 82 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | |
| 83 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | |
| 84 (pid, e.errno)) | |
| 85 return None | |
| 86 raise | |
| 87 except Exception as e: | |
| 88 return e | |
| 89 | |
| 90 | |
| 57 def wait_termination(pid): | 91 def wait_termination(pid): |
| 58 """Send SIGINT to pid and wait termination of pid. | 92 """Send SIGINT to pid and wait termination of pid. |
| 59 | 93 |
| 60 Args: | 94 Args: |
| 61 pid(int): pid of process which this function waits termination. | 95 pid(int): pid of process which this function waits termination. |
| 62 | 96 |
| 63 Raises: | 97 Raises: |
| 64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. | 98 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. |
| 65 NotDiedError: if cloudtail is running after 10 seconds waiting, | 99 NotDiedError: if cloudtail is running after 10 seconds waiting, |
| 66 NotDiedError is raised. | 100 NotDiedError is raised. |
| 67 """ | 101 """ |
| 68 | 102 if os.name == 'nt': |
| 69 try: | 103 pool = multiprocessing.Pool(1) |
| 70 os.kill(pid, signal.SIGINT) | 104 res = pool.apply_async(wait_termination_win, [pid]) |
| 71 except OSError as e: | 105 try: |
| 72 # Already dead? | 106 e = res.get(10) |
| 73 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | 107 except multiprocessing.TimeoutError: |
| 74 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | 108 print('process %d running more than 10 seconds.' % pid) |
| 75 (pid, e.errno)) | 109 raise NotDiedError() |
| 110 if e is None: | |
| 76 return | 111 return |
| 77 raise | 112 raise e |
| 78 | 113 else: |
| 79 print('SIGINT has been sent to process %d. ' | |
| 80 'Going to wait for the process finishes.' % pid) | |
| 81 if os.name == 'nt': | |
| 82 try: | 114 try: |
| 83 os.waitpid(pid, 0) | 115 os.kill(pid, signal.SIGINT) |
| 84 except OSError as e: | 116 except OSError as e: |
| 85 if e.errno == errno.ECHILD: | 117 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): |
| 86 print('process %d died before waitpitd' % pid) | 118 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % |
| 119 (pid, e.errno)) | |
| 87 return | 120 return |
| 88 raise e | |
| 89 else: | |
| 90 for _ in xrange(10): | 121 for _ in xrange(10): |
| 91 time.sleep(1) | 122 time.sleep(1) |
| 92 if not is_running_posix(pid): | 123 if not is_running_posix(pid): |
| 93 return | 124 return |
| 94 | |
| 95 print('process %d running more than 10 seconds' % pid) | 125 print('process %d running more than 10 seconds' % pid) |
| 96 raise NotDiedError() | 126 raise NotDiedError() |
| 97 | 127 |
| 98 | 128 |
| 99 def main(): | 129 def main(): |
| 100 parser = argparse.ArgumentParser( | 130 parser = argparse.ArgumentParser( |
| 101 description='cloudtail utility for goma recipe module.') | 131 description='cloudtail utility for goma recipe module.') |
| 102 | 132 |
| 103 subparsers = parser.add_subparsers(help='commands for cloudtail') | 133 subparsers = parser.add_subparsers(help='commands for cloudtail') |
| 104 | 134 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 120 | 150 |
| 121 if args.command == 'start': | 151 if args.command == 'start': |
| 122 start_cloudtail(args) | 152 start_cloudtail(args) |
| 123 elif args.command == 'stop': | 153 elif args.command == 'stop': |
| 124 with open(args.killed_pid_file) as f: | 154 with open(args.killed_pid_file) as f: |
| 125 # cloudtail flushes log and terminates | 155 # cloudtail flushes log and terminates |
| 126 # within 5 seconds when it recieves SIGINT. | 156 # within 5 seconds when it recieves SIGINT. |
| 127 pid = int(f.read()) | 157 pid = int(f.read()) |
| 128 try: | 158 try: |
| 129 wait_termination(pid) | 159 wait_termination(pid) |
| 130 except (OSError, NotDiedError) as e: | 160 except (OSError, NotDiedError) as e: |
|
ukai
2016/10/25 07:30:24
no need to catch pywintypes.error ?
Yoshisato Yanagisawa
2016/10/25 07:48:20
Since pywintypes.error might not be defined in Pos
| |
| 131 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) | 161 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) |
| 132 # Since Windows does not have SIGKILL, we need to use SIGTERM. | 162 # Since Windows does not have SIGKILL, we need to use SIGTERM. |
| 133 try: | 163 try: |
| 134 os.kill(pid, signal.SIGTERM) | 164 os.kill(pid, signal.SIGTERM) |
| 135 except OSError as e: | 165 except OSError as e: |
| 136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) | 166 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) |
| 137 # We do not reraise because I believe not suspending the process | 167 # We do not reraise because I believe not suspending the process |
| 138 # is more important than completely killing cloudtail. | 168 # is more important than completely killing cloudtail. |
| 139 | 169 |
| 140 | 170 |
| 141 if '__main__' == __name__: | 171 if '__main__' == __name__: |
| 142 sys.exit(main()) | 172 sys.exit(main()) |
| OLD | NEW |