OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import os | 6 import os |
7 import re | 7 import re |
8 import sys | 8 import sys |
9 import threading | 9 import threading |
10 import time | 10 import time |
11 | 11 |
12 import android_commands | 12 import android_commands |
13 import cmd_helper | 13 import cmd_helper |
14 import constants | 14 import constants |
15 import ports | |
16 | 15 |
17 from pylib import pexpect | 16 from pylib import pexpect |
18 | 17 |
19 | 18 |
20 def _MakeBinaryPath(build_type, binary_name): | 19 def _MakeBinaryPath(build_type, binary_name): |
21 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) | 20 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) |
22 | 21 |
23 | 22 |
24 class Forwarder(object): | 23 class Forwarder(object): |
25 """Thread-safe class to manage port forwards from the device to the host.""" | 24 """Thread-safe class to manage port forwards from the device to the host.""" |
26 | 25 |
27 # Unix Abstract socket path: | |
28 _DEVICE_ADB_CONTROL_PORT = 'chrome_device_forwarder' | |
29 _TIMEOUT_SECS = 30 | |
30 | |
31 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + | 26 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + |
32 '/forwarder/') | 27 '/forwarder/') |
33 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + | 28 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + |
34 '/forwarder/device_forwarder') | 29 '/forwarder/device_forwarder') |
35 _LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % _DEVICE_FORWARDER_FOLDER | 30 _LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % _DEVICE_FORWARDER_FOLDER |
36 | 31 |
37 def __init__(self, adb, build_type): | 32 def __init__(self, adb, build_type): |
38 """Forwards TCP ports on the device back to the host. | 33 """Forwards TCP ports on the device back to the host. |
39 | 34 |
40 Works like adb forward, but in reverse. | 35 Works like adb forward, but in reverse. |
(...skipping 22 matching lines...) Expand all Loading... |
63 get the number of the assigned port using the | 58 get the number of the assigned port using the |
64 DevicePortForHostPort method. | 59 DevicePortForHostPort method. |
65 tool: Tool class to use to get wrapper, if necessary, for executing the | 60 tool: Tool class to use to get wrapper, if necessary, for executing the |
66 forwarder (see valgrind_tools.py). | 61 forwarder (see valgrind_tools.py). |
67 | 62 |
68 Raises: | 63 Raises: |
69 Exception on failure to forward the port. | 64 Exception on failure to forward the port. |
70 """ | 65 """ |
71 with self._lock: | 66 with self._lock: |
72 self._InitDeviceLocked(tool) | 67 self._InitDeviceLocked(tool) |
73 self._InitHostLocked() | |
74 host_name = '127.0.0.1' | 68 host_name = '127.0.0.1' |
75 redirection_commands = [ | 69 redirection_commands = [ |
76 '%d:%d:%d:%s' % (self._host_adb_control_port, device, host, | 70 ['--serial-id=' + self._adb.Adb().GetSerialNumber(), '--map', |
77 host_name) for device, host in port_pairs] | 71 str(device), str(host)] for device, host in port_pairs] |
78 logging.info('Command format: <ADB port>:<Device port>' + | |
79 '[:<Forward to port>:<Forward to address>]') | |
80 logging.info('Forwarding using commands: %s', redirection_commands) | 72 logging.info('Forwarding using commands: %s', redirection_commands) |
81 | 73 |
82 for redirection_command in redirection_commands: | 74 for redirection_command in redirection_commands: |
83 try: | 75 try: |
84 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 76 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
85 [self._host_forwarder_path, redirection_command]) | 77 [self._host_forwarder_path] + redirection_command) |
86 except OSError as e: | 78 except OSError as e: |
87 if e.errno == 2: | 79 if e.errno == 2: |
88 raise Exception('Unable to start host forwarder. Make sure you have' | 80 raise Exception('Unable to start host forwarder. Make sure you have' |
89 ' built host_forwarder.') | 81 ' built host_forwarder.') |
90 else: raise | 82 else: raise |
91 if exit_code != 0: | 83 if exit_code != 0: |
92 raise Exception('%s exited with %d:\n%s' % ( | 84 raise Exception('%s exited with %d:\n%s' % ( |
93 self._host_forwarder_path, exit_code, '\n'.join(output))) | 85 self._host_forwarder_path, exit_code, '\n'.join(output))) |
94 tokens = output.split(':') | 86 tokens = output.split(':') |
95 if len(tokens) != 2: | 87 if len(tokens) != 2: |
96 raise Exception('Unexpected host forwarder output "%s", ' + | 88 raise Exception('Unexpected host forwarder output "%s", ' + |
97 'expected "device_port:host_port"' % output) | 89 'expected "device_port:host_port"' % output) |
98 device_port = int(tokens[0]) | 90 device_port = int(tokens[0]) |
99 host_port = int(tokens[1]) | 91 host_port = int(tokens[1]) |
100 self._host_to_device_port_map[host_port] = device_port | 92 self._host_to_device_port_map[host_port] = device_port |
101 logging.info('Forwarding device port: %d to host port: %d.', | 93 logging.info('Forwarding device port: %d to host port: %d.', |
102 device_port, host_port) | 94 device_port, host_port) |
103 | 95 |
104 def _InitHostLocked(self): | |
105 """Initializes the host forwarder process (only once).""" | |
106 if self._host_adb_control_port: | |
107 return | |
108 self._host_adb_control_port = ports.AllocateTestServerPort() | |
109 if not self._host_adb_control_port: | |
110 raise Exception('Failed to allocate a TCP port in the host machine.') | |
111 if cmd_helper.RunCmd( | |
112 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', | |
113 'tcp:%s' % self._host_adb_control_port, | |
114 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: | |
115 raise Exception('Error while running adb forward.') | |
116 | |
117 def _InitDeviceLocked(self, tool): | 96 def _InitDeviceLocked(self, tool): |
118 """Initializes the device forwarder process (only once).""" | 97 """Initializes the device forwarder process (only once).""" |
119 if self._device_initialized: | 98 if self._device_initialized: |
120 return | 99 return |
121 self._adb.PushIfNeeded( | 100 self._adb.PushIfNeeded( |
122 self._device_forwarder_path_on_host, | 101 self._device_forwarder_path_on_host, |
123 Forwarder._DEVICE_FORWARDER_FOLDER) | 102 Forwarder._DEVICE_FORWARDER_FOLDER) |
124 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( | 103 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( |
125 '%s %s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), | 104 '%s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), |
126 Forwarder._DEVICE_FORWARDER_PATH, | 105 Forwarder._DEVICE_FORWARDER_PATH)) |
127 Forwarder._DEVICE_ADB_CONTROL_PORT)) | |
128 if exit_code != 0: | 106 if exit_code != 0: |
129 raise Exception( | 107 raise Exception( |
130 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 108 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
131 self._device_initialized = True | 109 self._device_initialized = True |
132 | 110 |
133 def UnmapDevicePort(self, device_port): | 111 def UnmapDevicePort(self, device_port): |
134 """Unmaps a previously forwarded device port. | 112 """Unmaps a previously forwarded device port. |
135 | 113 |
136 Args: | 114 Args: |
137 device_port: A previously forwarded port (through Run()). | 115 device_port: A previously forwarded port (through Run()). |
138 """ | 116 """ |
139 with self._lock: | 117 with self._lock: |
140 # Please note the minus sign below. | 118 redirection_command = [ |
141 redirection_command = '%d:-%d' % ( | 119 '--serial-id=' + self._adb.Adb().GetSerialNumber(), '--unmap', |
142 self._host_adb_control_port, device_port) | 120 str(device_port)] |
143 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 121 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
144 [self._host_forwarder_path, redirection_command]) | 122 [self._host_forwarder_path] + redirection_command) |
145 if exit_code != 0: | 123 if exit_code != 0: |
146 raise Exception('%s exited with %d:\n%s' % ( | 124 raise Exception('%s exited with %d:\n%s' % ( |
147 self._host_forwarder_path, exit_code, '\n'.join(output))) | 125 self._host_forwarder_path, exit_code, '\n'.join(output))) |
148 | 126 |
149 @staticmethod | 127 @staticmethod |
150 def KillHost(build_type): | 128 def KillHost(build_type): |
151 """Kills the forwarder process running on the host. | 129 """Kills the forwarder process running on the host. |
152 | 130 |
153 Args: | 131 Args: |
154 build_type: 'Release' or 'Debug' | 132 build_type: 'Release' or 'Debug' |
155 """ | 133 """ |
156 logging.info('Killing host_forwarder.') | 134 logging.info('Killing host_forwarder.') |
157 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 135 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
158 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' | 136 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' |
159 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 137 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
160 [host_forwarder_path, 'kill-server']) | 138 [host_forwarder_path, '--kill-server']) |
161 if exit_code != 0: | 139 if exit_code != 0: |
162 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 140 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
163 ['pkill', 'host_forwarder']) | 141 ['pkill', 'host_forwarder']) |
164 if exit_code != 0: | 142 if exit_code != 0: |
165 raise Exception('%s exited with %d:\n%s' % ( | 143 raise Exception('%s exited with %d:\n%s' % ( |
166 host_forwarder_path, exit_code, '\n'.join(output))) | 144 host_forwarder_path, exit_code, '\n'.join(output))) |
167 | 145 |
168 @staticmethod | 146 @staticmethod |
169 def KillDevice(adb, tool): | 147 def KillDevice(adb, tool): |
170 """Kills the forwarder process running on the device. | 148 """Kills the forwarder process running on the device. |
171 | 149 |
172 Args: | 150 Args: |
173 adb: Instance of AndroidCommands for talking to the device. | 151 adb: Instance of AndroidCommands for talking to the device. |
174 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device | 152 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device |
175 forwarder (see valgrind_tools.py). | 153 forwarder (see valgrind_tools.py). |
176 """ | 154 """ |
177 logging.info('Killing device_forwarder.') | 155 logging.info('Killing device_forwarder.') |
178 if not adb.FileExistsOnDevice(Forwarder._DEVICE_FORWARDER_PATH): | 156 if not adb.FileExistsOnDevice(Forwarder._DEVICE_FORWARDER_PATH): |
179 return | 157 return |
180 (exit_code, output) = adb.GetShellCommandStatusAndOutput( | 158 (exit_code, output) = adb.GetShellCommandStatusAndOutput( |
181 '%s %s kill-server' % (tool.GetUtilWrapper(), | 159 '%s %s --kill-server' % (tool.GetUtilWrapper(), |
182 Forwarder._DEVICE_FORWARDER_PATH)) | 160 Forwarder._DEVICE_FORWARDER_PATH)) |
183 # TODO(pliard): Remove the following call to KillAllBlocking() when we are | 161 # TODO(pliard): Remove the following call to KillAllBlocking() when we are |
184 # sure that the old version of device_forwarder (not supporting | 162 # sure that the old version of device_forwarder (not supporting |
185 # 'kill-server') is not running on the bots anymore. | 163 # 'kill-server') is not running on the bots anymore. |
186 timeout_sec = 5 | 164 timeout_sec = 5 |
187 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) | 165 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) |
188 if not processes_killed: | 166 if not processes_killed: |
189 pids = adb.ExtractPid('device_forwarder') | 167 pids = adb.ExtractPid('device_forwarder') |
190 if pids: | 168 if pids: |
191 raise Exception('Timed out while killing device_forwarder') | 169 raise Exception('Timed out while killing device_forwarder') |
192 | 170 |
193 def DevicePortForHostPort(self, host_port): | 171 def DevicePortForHostPort(self, host_port): |
194 """Returns the device port that corresponds to a given host port.""" | 172 """Returns the device port that corresponds to a given host port.""" |
195 with self._lock: | 173 with self._lock: |
196 return self._host_to_device_port_map.get(host_port) | 174 return self._host_to_device_port_map.get(host_port) |
197 | 175 |
198 # Deprecated. | 176 # Deprecated. |
199 def Close(self): | 177 def Close(self): |
200 """Terminates the forwarder process.""" | 178 """Terminates the forwarder process.""" |
201 # TODO(pliard): Remove references in client code. | 179 # TODO(pliard): Remove references in client code. |
202 pass | 180 pass |
OLD | NEW |