Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Saves logcats from all connected devices. | 7 """Saves logcats from all connected devices. |
| 8 | 8 |
| 9 Usage: adb_logcat_monitor.py <base_dir> [<adb_binary_path>] | 9 Usage: adb_logcat_monitor.py <base_dir> [<adb_binary_path>] |
| 10 | 10 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 return [] | 89 return [] |
| 90 except (IOError, OSError): | 90 except (IOError, OSError): |
| 91 logging.exception('Exception from "adb devices"') | 91 logging.exception('Exception from "adb devices"') |
| 92 return [] | 92 return [] |
| 93 finally: | 93 finally: |
| 94 signal.alarm(0) | 94 signal.alarm(0) |
| 95 | 95 |
| 96 | 96 |
| 97 def main(base_dir, adb_cmd='adb'): | 97 def main(base_dir, adb_cmd='adb'): |
| 98 """Monitor adb forever. Expects a SIGINT (Ctrl-C) to kill.""" | 98 """Monitor adb forever. Expects a SIGINT (Ctrl-C) to kill.""" |
| 99 # Spawn a detached child process. | |
| 100 pid = os.fork() | |
| 101 if pid > 0: | |
| 102 return 0 | |
|
Isaac (away)
2013/09/26 18:31:08
yikes. I believe this needs to be os._exit(0)
Siva Chandra
2013/09/26 20:24:13
I think in principle this should be the other way:
| |
| 103 elif pid < 0: | |
| 104 sys.exit('Unable to spawn a detached child process.') | |
| 105 os.setsid() | |
|
iannucci
2013/09/26 18:15:57
How does this process get cleaned up?
Siva Chandra
2013/09/26 18:30:06
There is another step which is always executed. Th
Isaac (away)
2013/09/26 18:31:08
Another process reads PID from a file and kills it
navabi
2013/09/26 19:56:00
Where does that happen again? I don't see the PID
Siva Chandra
2013/09/26 20:29:44
See below.
| |
| 106 # The rest happens in the child process. | |
| 107 | |
| 99 # We create the directory to ensure 'run once' semantics | 108 # We create the directory to ensure 'run once' semantics |
| 100 if os.path.exists(base_dir): | 109 if os.path.exists(base_dir): |
| 101 print 'adb_logcat_monitor: %s already exists? Cleaning' % base_dir | 110 print 'adb_logcat_monitor: %s already exists? Cleaning' % base_dir |
| 102 shutil.rmtree(base_dir, ignore_errors=True) | 111 shutil.rmtree(base_dir, ignore_errors=True) |
| 103 | 112 |
| 104 os.makedirs(base_dir) | 113 os.makedirs(base_dir) |
| 105 logging.basicConfig(filename=os.path.join(base_dir, 'eventlog'), | 114 logging.basicConfig(filename=os.path.join(base_dir, 'eventlog'), |
| 106 level=logging.INFO, | 115 level=logging.INFO, |
| 107 format='%(asctime)-2s %(levelname)-8s %(message)s') | 116 format='%(asctime)-2s %(levelname)-8s %(message)s') |
| 108 | 117 |
| 109 # Set up the alarm for calling 'adb devices'. This is to ensure | 118 # Set up the alarm for calling 'adb devices'. This is to ensure |
| 110 # our script doesn't get stuck waiting for a process response | 119 # our script doesn't get stuck waiting for a process response |
| 111 def TimeoutHandler(_, unused_frame): | 120 def TimeoutHandler(_, unused_frame): |
| 112 raise TimeoutException() | 121 raise TimeoutException() |
| 113 signal.signal(signal.SIGALRM, TimeoutHandler) | 122 signal.signal(signal.SIGALRM, TimeoutHandler) |
| 114 | 123 |
| 115 # Handle SIGTERMs to ensure clean shutdown | 124 # Handle SIGTERMs to ensure clean shutdown |
| 116 def SigtermHandler(_, unused_frame): | 125 def SigtermHandler(_, unused_frame): |
| 117 raise SigtermError() | 126 raise SigtermError() |
| 118 signal.signal(signal.SIGTERM, SigtermHandler) | 127 signal.signal(signal.SIGTERM, SigtermHandler) |
| 119 | 128 |
| 120 logging.info('Started with pid %d', os.getpid()) | 129 logging.info('Started with pid %d', os.getpid()) |
| 121 pid_file_path = os.path.join(base_dir, 'LOGCAT_MONITOR_PID') | 130 pid_file_path = os.path.join(base_dir, 'LOGCAT_MONITOR_PID') |
| 122 | 131 |
| 123 try: | 132 try: |
| 124 with open(pid_file_path, 'w') as f: | 133 with open(pid_file_path, 'w') as f: |
|
Siva Chandra
2013/09/26 20:29:44
The PID gets written to a file here.
| |
| 125 f.write(str(os.getpid())) | 134 f.write(str(os.getpid())) |
| 126 while True: | 135 while True: |
| 127 for device_id in GetAttachedDevices(adb_cmd): | 136 for device_id in GetAttachedDevices(adb_cmd): |
| 128 if not device_id in devices: | 137 if not device_id in devices: |
| 129 subprocess.call([adb_cmd, '-s', device_id, 'logcat', '-c']) | 138 subprocess.call([adb_cmd, '-s', device_id, 'logcat', '-c']) |
| 130 devices[device_id] = (None, 0) | 139 devices[device_id] = (None, 0) |
| 131 | 140 |
| 132 for device in devices: | 141 for device in devices: |
| 133 # This will spawn logcat watchers for any device ever detected | 142 # This will spawn logcat watchers for any device ever detected |
| 134 StartLogcatIfNecessary(device, adb_cmd, base_dir) | 143 StartLogcatIfNecessary(device, adb_cmd, base_dir) |
| 135 | 144 |
| 136 time.sleep(5) | 145 time.sleep(5) |
| 137 except SigtermError: | 146 except SigtermError: |
| 138 logging.info('Received SIGTERM, shutting down') | 147 logging.info('Received SIGTERM, shutting down') |
|
Siva Chandra
2013/09/26 20:29:44
adb_logcat_printer.py:133 sends SIGTERM which is c
| |
| 139 except: | 148 except: |
| 140 logging.exception('Unexpected exception in main.') | 149 logging.exception('Unexpected exception in main.') |
| 141 finally: | 150 finally: |
| 142 for process, _ in devices.itervalues(): | 151 for process, _ in devices.itervalues(): |
| 143 if process: | 152 if process: |
| 144 try: | 153 try: |
| 145 process.terminate() | 154 process.terminate() |
| 146 except OSError: | 155 except OSError: |
| 147 pass | 156 pass |
| 148 os.remove(pid_file_path) | 157 os.remove(pid_file_path) |
|
Siva Chandra
2013/09/26 20:29:44
The PID file is removed here.
| |
| 149 | 158 |
| 150 | 159 |
| 151 if __name__ == '__main__': | 160 if __name__ == '__main__': |
| 152 if 2 <= len(sys.argv) <= 3: | 161 if 2 <= len(sys.argv) <= 3: |
| 153 print 'adb_logcat_monitor: Initializing' | 162 print 'adb_logcat_monitor: Initializing' |
| 154 sys.exit(main(*sys.argv[1:3])) | 163 sys.exit(main(*sys.argv[1:3])) |
| 155 | 164 |
| 156 print 'Usage: %s <base_dir> [<adb_binary_path>]' % sys.argv[0] | 165 print 'Usage: %s <base_dir> [<adb_binary_path>]' % sys.argv[0] |
| OLD | NEW |