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 os | 8 import os |
| 9 import signal | 9 import signal |
| 10 import subprocess | 10 import subprocess |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 return False | 47 return False |
| 48 raise e | 48 raise e |
| 49 return True | 49 return True |
| 50 | 50 |
| 51 | 51 |
| 52 class NotDiedError(Exception): | 52 class NotDiedError(Exception): |
| 53 def __str__(self): | 53 def __str__(self): |
| 54 return "NotDiedError" | 54 return "NotDiedError" |
| 55 | 55 |
| 56 | 56 |
| 57 class Error(Exception): | |
| 58 """Raised on something unexpected situation.""" | |
| 59 | |
| 60 | |
| 61 def wait_termination_win(pid): | |
| 62 """Send CTRL_C_EVENT or SIGINT to pid and wait termination of pid. | |
| 63 | |
| 64 Args: | |
| 65 pid(int): pid of process which this function waits termination. | |
| 66 | |
| 67 Returns: | |
| 68 None: if pid has already finished or process finished succesfully. | |
| 69 instance of Exception: if raised unexpectedly. | |
| 70 """ | |
| 71 import win32api | |
| 72 import win32con | |
| 73 import win32event | |
| 74 import winerror | |
| 75 import pywintypes | |
| 76 handle = None | |
| 77 try: | |
| 78 handle = win32api.OpenProcess( | |
| 79 win32con.PROCESS_QUERY_INFORMATION | win32con.SYNCHRONIZE, | |
| 80 False, pid) | |
| 81 try: | |
| 82 os.kill(pid, signal.CTRL_C_EVENT) | |
| 83 print('CTRL_C_EVENT has been sent to process %d. ' | |
| 84 'Going to wait for the process finishes.' % pid) | |
| 85 except WindowsError as e: # pylint: disable=E0602 | |
| 86 # If a target process does not share terminal, we cannot send Ctrl-C. | |
| 87 if e[0] == winerror.ERROR_INVALID_PARAMETER: | |
| 88 os.kill(pid, signal.SIGINT) | |
| 89 print('SIGINT has been sent to process %d.' % pid) | |
| 90 ret = win32event.WaitForSingleObject(handle) | |
|
Vadim Sh.
2016/10/26 01:21:16
I think you forgot to specify timeout here.
Yoshisato Yanagisawa
2016/10/26 01:23:14
good catch.
| |
| 91 if ret == win32event.WAIT_TIMEOUT: | |
| 92 raise NotDiedError() | |
| 93 elif ret == win32event.WAIT_OBJECT_0: | |
| 94 return | |
| 95 raise Error('Unexpected return code %d for pid %d.' % (ret, pid)) | |
| 96 except pywintypes.error as e: | |
| 97 if e[0] == winerror.ERROR_INVALID_PARAMETER and e[1] == 'OpenProcess': | |
| 98 print('Can\'t open process %d. Already dead? error %d.' % (pid, e)) | |
| 99 return | |
| 100 raise | |
| 101 except OSError as e: | |
| 102 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | |
| 103 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | |
| 104 (pid, e.errno)) | |
| 105 return | |
| 106 raise | |
| 107 finally: | |
| 108 if handle: | |
| 109 win32api.CloseHandle(handle) | |
| 110 | |
| 111 | |
| 57 def wait_termination(pid): | 112 def wait_termination(pid): |
| 58 """Send SIGINT to pid and wait termination of pid. | 113 """Send SIGINT to pid and wait termination of pid. |
| 59 | 114 |
| 60 Args: | 115 Args: |
| 61 pid(int): pid of process which this function waits termination. | 116 pid(int): pid of process which this function waits termination. |
| 62 | 117 |
| 63 Raises: | 118 Raises: |
| 64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. | 119 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. |
| 65 NotDiedError: if cloudtail is running after 10 seconds waiting, | 120 NotDiedError: if cloudtail is running after 10 seconds waiting, |
| 66 NotDiedError is raised. | 121 NotDiedError is raised. |
| 67 """ | 122 """ |
| 68 | |
| 69 try: | |
| 70 os.kill(pid, signal.SIGINT) | |
| 71 except OSError as e: | |
| 72 # Already dead? | |
| 73 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | |
| 74 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | |
| 75 (pid, e.errno)) | |
| 76 return | |
| 77 raise | |
| 78 | |
| 79 print('SIGINT has been sent to process %d. ' | |
| 80 'Going to wait for the process finishes.' % pid) | |
| 81 if os.name == 'nt': | 123 if os.name == 'nt': |
| 124 wait_termination_win(pid) | |
| 125 else: | |
| 82 try: | 126 try: |
| 83 os.waitpid(pid, 0) | 127 os.kill(pid, signal.SIGINT) |
| 84 except OSError as e: | 128 except OSError as e: |
| 85 if e.errno == errno.ECHILD: | 129 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): |
| 86 print('process %d died before waitpitd' % pid) | 130 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % |
| 131 (pid, e.errno)) | |
| 87 return | 132 return |
| 88 raise e | |
| 89 else: | |
| 90 for _ in xrange(10): | 133 for _ in xrange(10): |
| 91 time.sleep(1) | 134 time.sleep(1) |
| 92 if not is_running_posix(pid): | 135 if not is_running_posix(pid): |
| 93 return | 136 return |
| 94 | |
| 95 print('process %d running more than 10 seconds' % pid) | 137 print('process %d running more than 10 seconds' % pid) |
| 96 raise NotDiedError() | 138 raise NotDiedError() |
| 97 | 139 |
| 98 | 140 |
| 99 def main(): | 141 def main(): |
| 100 parser = argparse.ArgumentParser( | 142 parser = argparse.ArgumentParser( |
| 101 description='cloudtail utility for goma recipe module.') | 143 description='cloudtail utility for goma recipe module.') |
| 102 | 144 |
| 103 subparsers = parser.add_subparsers(help='commands for cloudtail') | 145 subparsers = parser.add_subparsers(help='commands for cloudtail') |
| 104 | 146 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 120 | 162 |
| 121 if args.command == 'start': | 163 if args.command == 'start': |
| 122 start_cloudtail(args) | 164 start_cloudtail(args) |
| 123 elif args.command == 'stop': | 165 elif args.command == 'stop': |
| 124 with open(args.killed_pid_file) as f: | 166 with open(args.killed_pid_file) as f: |
| 125 # cloudtail flushes log and terminates | 167 # cloudtail flushes log and terminates |
| 126 # within 5 seconds when it recieves SIGINT. | 168 # within 5 seconds when it recieves SIGINT. |
| 127 pid = int(f.read()) | 169 pid = int(f.read()) |
| 128 try: | 170 try: |
| 129 wait_termination(pid) | 171 wait_termination(pid) |
| 130 except (OSError, NotDiedError) as e: | 172 except Exception as e: |
| 131 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) | 173 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. | 174 # Since Windows does not have SIGKILL, we need to use SIGTERM. |
| 133 try: | 175 try: |
| 134 os.kill(pid, signal.SIGTERM) | 176 os.kill(pid, signal.SIGTERM) |
| 135 except OSError as e: | 177 except OSError as e: |
| 136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) | 178 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) |
| 137 # We do not reraise because I believe not suspending the process | 179 # We do not reraise because I believe not suspending the process |
| 138 # is more important than completely killing cloudtail. | 180 # is more important than completely killing cloudtail. |
| 139 | 181 |
| 140 | 182 |
| 141 if '__main__' == __name__: | 183 if '__main__' == __name__: |
| 142 sys.exit(main()) | 184 sys.exit(main()) |
| OLD | NEW |