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 happens.""" | |
| 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. | |
|
tikuta
2016/10/26 01:34:05
this statement can be better?
Yoshisato Yanagisawa
2016/10/26 01:46:42
Done.
| |
| 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, 10 * 10**3) | |
| 91 if ret == win32event.WAIT_TIMEOUT: | |
| 92 print('process %d running more than 10 seconds' % pid) | |
| 93 raise NotDiedError() | |
| 94 elif ret == win32event.WAIT_OBJECT_0: | |
| 95 return | |
| 96 raise Error('Unexpected return code %d for pid %d.' % (ret, pid)) | |
| 97 except pywintypes.error as e: | |
| 98 if e[0] == winerror.ERROR_INVALID_PARAMETER and e[1] == 'OpenProcess': | |
| 99 print('Can\'t open process %d. Already dead? error %d.' % (pid, e)) | |
| 100 return | |
| 101 raise | |
| 102 except OSError as e: | |
| 103 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): | |
| 104 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % | |
| 105 (pid, e.errno)) | |
| 106 return | |
| 107 raise | |
| 108 finally: | |
| 109 if handle: | |
| 110 win32api.CloseHandle(handle) | |
| 111 | |
| 112 | |
| 57 def wait_termination(pid): | 113 def wait_termination(pid): |
| 58 """Send SIGINT to pid and wait termination of pid. | 114 """Send SIGINT to pid and wait termination of pid. |
| 59 | 115 |
| 60 Args: | 116 Args: |
| 61 pid(int): pid of process which this function waits termination. | 117 pid(int): pid of process which this function waits termination. |
| 62 | 118 |
| 63 Raises: | 119 Raises: |
| 64 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. | 120 OSError: is_running_posix, os.waitpid and os.kill may throw OSError. |
| 65 NotDiedError: if cloudtail is running after 10 seconds waiting, | 121 NotDiedError: if cloudtail is running after 10 seconds waiting, |
| 66 NotDiedError is raised. | 122 NotDiedError is raised. |
| 67 """ | 123 """ |
| 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': | 124 if os.name == 'nt': |
| 125 wait_termination_win(pid) | |
| 126 else: | |
| 82 try: | 127 try: |
| 83 os.waitpid(pid, 0) | 128 os.kill(pid, signal.SIGINT) |
| 84 except OSError as e: | 129 except OSError as e: |
| 85 if e.errno == errno.ECHILD: | 130 if e.errno in (errno.ECHILD, errno.EPERM, errno.ESRCH): |
| 86 print('process %d died before waitpitd' % pid) | 131 print('Can\'t send SIGINT to process %d. Already dead? Errno %d.' % |
| 132 (pid, e.errno)) | |
| 87 return | 133 return |
| 88 raise e | |
| 89 else: | |
| 90 for _ in xrange(10): | 134 for _ in xrange(10): |
| 91 time.sleep(1) | 135 time.sleep(1) |
| 92 if not is_running_posix(pid): | 136 if not is_running_posix(pid): |
| 93 return | 137 return |
| 94 | |
| 95 print('process %d running more than 10 seconds' % pid) | 138 print('process %d running more than 10 seconds' % pid) |
| 96 raise NotDiedError() | 139 raise NotDiedError() |
| 97 | 140 |
| 98 | 141 |
| 99 def main(): | 142 def main(): |
| 100 parser = argparse.ArgumentParser( | 143 parser = argparse.ArgumentParser( |
| 101 description='cloudtail utility for goma recipe module.') | 144 description='cloudtail utility for goma recipe module.') |
| 102 | 145 |
| 103 subparsers = parser.add_subparsers(help='commands for cloudtail') | 146 subparsers = parser.add_subparsers(help='commands for cloudtail') |
| 104 | 147 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 120 | 163 |
| 121 if args.command == 'start': | 164 if args.command == 'start': |
| 122 start_cloudtail(args) | 165 start_cloudtail(args) |
| 123 elif args.command == 'stop': | 166 elif args.command == 'stop': |
| 124 with open(args.killed_pid_file) as f: | 167 with open(args.killed_pid_file) as f: |
| 125 # cloudtail flushes log and terminates | 168 # cloudtail flushes log and terminates |
| 126 # within 5 seconds when it recieves SIGINT. | 169 # within 5 seconds when it recieves SIGINT. |
| 127 pid = int(f.read()) | 170 pid = int(f.read()) |
| 128 try: | 171 try: |
| 129 wait_termination(pid) | 172 wait_termination(pid) |
| 130 except (OSError, NotDiedError) as e: | 173 except Exception as e: |
| 131 print('Going to send SIGTERM to process %d due to Error %s' % (pid, e)) | 174 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. | 175 # Since Windows does not have SIGKILL, we need to use SIGTERM. |
| 133 try: | 176 try: |
| 134 os.kill(pid, signal.SIGTERM) | 177 os.kill(pid, signal.SIGTERM) |
| 135 except OSError as e: | 178 except OSError as e: |
| 136 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) | 179 print('Failed to send SIGTERM to process %d: %s' % (pid, e)) |
| 137 # We do not reraise because I believe not suspending the process | 180 # We do not reraise because I believe not suspending the process |
| 138 # is more important than completely killing cloudtail. | 181 # is more important than completely killing cloudtail. |
| 139 | 182 |
| 140 | 183 |
| 141 if '__main__' == __name__: | 184 if '__main__' == __name__: |
| 142 sys.exit(main()) | 185 sys.exit(main()) |
| OLD | NEW |