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
3 # found in the LICENSE file.
5 # pylint: disable=W0212
12 from pylib import cmd_helper
13 from pylib import constants
14 from pylib import valgrind_tools
16 # TODO(jbudorick) Remove once telemetry gets switched over.
17 import pylib.android_commands
18 import pylib.device.device_utils
21 def _GetProcessStartTime(pid):
22 return psutil.Process(pid).create_time
25 class _FileLock(object):
26 """With statement-aware implementation of a file lock.
28 File locks are needed for cross-process synchronization when the
29 multiprocessing Python module is used.
31 def __init__(self, path):
36 self._fd = os.open(self._path, os.O_RDONLY | os.O_CREAT)
38 raise Exception('Could not open file %s for reading' % self._path)
39 fcntl.flock(self._fd, fcntl.LOCK_EX)
41 def __exit__(self, _exception_type, _exception_value, traceback):
42 fcntl.flock(self._fd, fcntl.LOCK_UN)
46 class Forwarder(object):
47 """Thread-safe class to manage port forwards from the device to the host."""
49 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR +
51 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR +
52 '/forwarder/device_forwarder')
53 _LOCK_PATH = '/tmp/chrome.forwarder.lock'
54 _MULTIPROCESSING_ENV_VAR = 'CHROME_FORWARDER_USE_MULTIPROCESSING'
55 # Defined in host_forwarder_main.cc
56 _HOST_FORWARDER_LOG = '/tmp/host_forwarder_log'
61 def UseMultiprocessing():
62 """Tells the forwarder that multiprocessing is used."""
63 os.environ[Forwarder._MULTIPROCESSING_ENV_VAR] = '1'
66 def Map(port_pairs, device, tool=None):
67 """Runs the forwarder.
70 port_pairs: A list of tuples (device_port, host_port) to forward. Note
71 that you can specify 0 as a device_port, in which case a
72 port will by dynamically assigned on the device. You can
73 get the number of the assigned port using the
74 DevicePortForHostPort method.
75 device: A DeviceUtils instance.
76 tool: Tool class to use to get wrapper, if necessary, for executing the
77 forwarder (see valgrind_tools.py).
80 Exception on failure to forward the port.
82 # TODO(jbudorick) Remove once telemetry gets switched over.
83 if isinstance(device, pylib.android_commands.AndroidCommands):
84 device = pylib.device.device_utils.DeviceUtils(device)
86 tool = valgrind_tools.CreateTool(None, device)
87 with _FileLock(Forwarder._LOCK_PATH):
88 instance = Forwarder._GetInstanceLocked(tool)
89 instance._InitDeviceLocked(device, tool)
91 device_serial = device.old_interface.Adb().GetSerialNumber()
92 redirection_commands = [
93 ['--serial-id=' + device_serial, '--map', str(device),
94 str(host)] for device, host in port_pairs]
95 logging.info('Forwarding using commands: %s', redirection_commands)
97 for redirection_command in redirection_commands:
99 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
100 [instance._host_forwarder_path] + redirection_command)
103 raise Exception('Unable to start host forwarder. Make sure you have'
104 ' built host_forwarder.')
107 raise Exception('%s exited with %d:\n%s' % (
108 instance._host_forwarder_path, exit_code, '\n'.join(output)))
109 tokens = output.split(':')
111 raise Exception(('Unexpected host forwarder output "%s", ' +
112 'expected "device_port:host_port"') % output)
113 device_port = int(tokens[0])
114 host_port = int(tokens[1])
115 serial_with_port = (device_serial, device_port)
116 instance._device_to_host_port_map[serial_with_port] = host_port
117 instance._host_to_device_port_map[host_port] = serial_with_port
118 logging.info('Forwarding device port: %d to host port: %d.',
119 device_port, host_port)
122 def UnmapDevicePort(device_port, device):
123 """Unmaps a previously forwarded device port.
126 device: A DeviceUtils instance.
127 device_port: A previously forwarded port (through Map()).
129 # TODO(jbudorick) Remove once telemetry gets switched over.
130 if isinstance(device, pylib.android_commands.AndroidCommands):
131 device = pylib.device.device_utils.DeviceUtils(device)
132 with _FileLock(Forwarder._LOCK_PATH):
133 Forwarder._UnmapDevicePortLocked(device_port, device)
136 def UnmapAllDevicePorts(device):
137 """Unmaps all the previously forwarded ports for the provided device.
140 device: A DeviceUtils instance.
141 port_pairs: A list of tuples (device_port, host_port) to unmap.
143 # TODO(jbudorick) Remove once telemetry gets switched over.
144 if isinstance(device, pylib.android_commands.AndroidCommands):
145 device = pylib.device.device_utils.DeviceUtils(device)
146 with _FileLock(Forwarder._LOCK_PATH):
147 if not Forwarder._instance:
149 adb_serial = device.old_interface.Adb().GetSerialNumber()
150 if adb_serial not in Forwarder._instance._initialized_devices:
152 port_map = Forwarder._GetInstanceLocked(
153 None)._device_to_host_port_map
154 for (device_serial, device_port) in port_map.keys():
155 if adb_serial == device_serial:
156 Forwarder._UnmapDevicePortLocked(device_port, device)
157 # There are no more ports mapped, kill the device_forwarder.
158 tool = valgrind_tools.CreateTool(None, device)
159 Forwarder._KillDeviceLocked(device, tool)
160 Forwarder._instance._initialized_devices.remove(adb_serial)
164 def DevicePortForHostPort(host_port):
165 """Returns the device port that corresponds to a given host port."""
166 with _FileLock(Forwarder._LOCK_PATH):
167 (_device_serial, device_port) = Forwarder._GetInstanceLocked(
168 None)._host_to_device_port_map.get(host_port)
173 if os.path.exists(Forwarder._HOST_FORWARDER_LOG):
174 os.unlink(Forwarder._HOST_FORWARDER_LOG)
178 if not os.path.exists(Forwarder._HOST_FORWARDER_LOG):
180 with file(Forwarder._HOST_FORWARDER_LOG, 'r') as f:
184 def _GetInstanceLocked(tool):
185 """Returns the singleton instance.
187 Note that the global lock must be acquired before calling this method.
190 tool: Tool class to use to get wrapper, if necessary, for executing the
191 forwarder (see valgrind_tools.py).
193 if not Forwarder._instance:
194 Forwarder._instance = Forwarder(tool)
195 return Forwarder._instance
197 def __init__(self, tool):
198 """Constructs a new instance of Forwarder.
200 Note that Forwarder is a singleton therefore this constructor should be
204 tool: Tool class to use to get wrapper, if necessary, for executing the
205 forwarder (see valgrind_tools.py).
207 assert not Forwarder._instance
209 self._initialized_devices = set()
210 self._device_to_host_port_map = dict()
211 self._host_to_device_port_map = dict()
212 self._host_forwarder_path = os.path.join(
213 constants.GetOutDirectory(), 'host_forwarder')
214 assert os.path.exists(self._host_forwarder_path), 'Please build forwarder2'
215 self._device_forwarder_path_on_host = os.path.join(
216 constants.GetOutDirectory(), 'forwarder_dist')
217 self._InitHostLocked()
220 def _UnmapDevicePortLocked(device_port, device):
221 """Internal method used by UnmapDevicePort().
223 Note that the global lock must be acquired before calling this method.
225 instance = Forwarder._GetInstanceLocked(None)
226 serial = device.old_interface.Adb().GetSerialNumber()
227 serial_with_port = (serial, device_port)
228 if not serial_with_port in instance._device_to_host_port_map:
229 logging.error('Trying to unmap non-forwarded port %d' % device_port)
231 redirection_command = ['--serial-id=' + serial, '--unmap', str(device_port)]
232 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
233 [instance._host_forwarder_path] + redirection_command)
235 logging.error('%s exited with %d:\n%s' % (
236 instance._host_forwarder_path, exit_code, '\n'.join(output)))
237 host_port = instance._device_to_host_port_map[serial_with_port]
238 del instance._device_to_host_port_map[serial_with_port]
239 del instance._host_to_device_port_map[host_port]
242 def _GetPidForLock():
243 """Returns the PID used for host_forwarder initialization.
245 In case multi-process sharding is used, the PID of the "sharder" is used.
246 The "sharder" is the initial process that forks that is the parent process.
247 By default, multi-processing is not used. In that case the PID of the
248 current process is returned.
250 use_multiprocessing = Forwarder._MULTIPROCESSING_ENV_VAR in os.environ
251 return os.getppid() if use_multiprocessing else os.getpid()
253 def _InitHostLocked(self):
254 """Initializes the host forwarder daemon.
256 Note that the global lock must be acquired before calling this method. This
257 method kills any existing host_forwarder process that could be stale.
259 # See if the host_forwarder daemon was already initialized by a concurrent
260 # process or thread (in case multi-process sharding is not used).
261 pid_for_lock = Forwarder._GetPidForLock()
262 fd = os.open(Forwarder._LOCK_PATH, os.O_RDWR | os.O_CREAT)
263 with os.fdopen(fd, 'r+') as pid_file:
264 pid_with_start_time = pid_file.readline()
265 if pid_with_start_time:
266 (pid, process_start_time) = pid_with_start_time.split(':')
267 if pid == str(pid_for_lock):
268 if process_start_time == str(_GetProcessStartTime(pid_for_lock)):
270 self._KillHostLocked()
273 '%s:%s' % (pid_for_lock, str(_GetProcessStartTime(pid_for_lock))))
275 def _InitDeviceLocked(self, device, tool):
276 """Initializes the device_forwarder daemon for a specific device (once).
278 Note that the global lock must be acquired before calling this method. This
279 method kills any existing device_forwarder daemon on the device that could
280 be stale, pushes the latest version of the daemon (to the device) and starts
284 device: A DeviceUtils instance.
285 tool: Tool class to use to get wrapper, if necessary, for executing the
286 forwarder (see valgrind_tools.py).
288 device_serial = device.old_interface.Adb().GetSerialNumber()
289 if device_serial in self._initialized_devices:
291 Forwarder._KillDeviceLocked(device, tool)
292 device.old_interface.PushIfNeeded(
293 self._device_forwarder_path_on_host,
294 Forwarder._DEVICE_FORWARDER_FOLDER)
295 cmd = '%s %s' % (tool.GetUtilWrapper(), Forwarder._DEVICE_FORWARDER_PATH)
296 (exit_code, output) = device.old_interface.GetAndroidToolStatusAndOutput(
297 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER)
300 'Failed to start device forwarder:\n%s' % '\n'.join(output))
301 self._initialized_devices.add(device_serial)
303 def _KillHostLocked(self):
304 """Kills the forwarder process running on the host.
306 Note that the global lock must be acquired before calling this method.
308 logging.info('Killing host_forwarder.')
309 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
310 [self._host_forwarder_path, '--kill-server'])
312 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
313 ['pkill', '-9', 'host_forwarder'])
315 raise Exception('%s exited with %d:\n%s' % (
316 self._host_forwarder_path, exit_code, '\n'.join(output)))
319 def _KillDeviceLocked(device, tool):
320 """Kills the forwarder process running on the device.
322 Note that the global lock must be acquired before calling this method.
325 device: Instance of DeviceUtils for talking to the device.
326 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device
327 forwarder (see valgrind_tools.py).
329 logging.info('Killing device_forwarder.')
330 if not device.old_interface.FileExistsOnDevice(
331 Forwarder._DEVICE_FORWARDER_PATH):
334 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(),
335 Forwarder._DEVICE_FORWARDER_PATH)
336 device.old_interface.GetAndroidToolStatusAndOutput(
337 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER)
339 # TODO(pliard): Remove the following call to KillAllBlocking() when we are
340 # sure that the old version of device_forwarder (not supporting
341 # 'kill-server') is not running on the bots anymore.
343 processes_killed = device.old_interface.KillAllBlocking(
344 'device_forwarder', timeout_sec)
345 if not processes_killed:
346 pids = device.old_interface.ExtractPid('device_forwarder')
348 raise Exception('Timed out while killing device_forwarder')