1# Copyright 2013 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. 4 5import logging 6import os 7import psutil 8import signal 9 10from pylib import android_commands 11 12 13def _KillWebServers(): 14 for s in [signal.SIGTERM, signal.SIGINT, signal.SIGQUIT, signal.SIGKILL]: 15 signalled = [] 16 for server in ['lighttpd', 'webpagereplay']: 17 for p in psutil.process_iter(): 18 try: 19 if not server in ' '.join(p.cmdline): 20 continue 21 logging.info('Killing %s %s %s', s, server, p.pid) 22 p.send_signal(s) 23 signalled.append(p) 24 except Exception as e: 25 logging.warning('Failed killing %s %s %s', server, p.pid, e) 26 for p in signalled: 27 try: 28 p.wait(1) 29 except Exception as e: 30 logging.warning('Failed waiting for %s to die. %s', p.pid, e) 31 32 33 34def CleanupLeftoverProcesses(): 35 """Clean up the test environment, restarting fresh adb and HTTP daemons.""" 36 _KillWebServers() 37 did_restart_host_adb = False 38 for device in android_commands.GetAttachedDevices(): 39 adb = android_commands.AndroidCommands(device, api_strict_mode=True) 40 # Make sure we restart the host adb server only once. 41 if not did_restart_host_adb: 42 adb.RestartAdbServer() 43 did_restart_host_adb = True 44 adb.RestartAdbdOnDevice() 45 adb.EnableAdbRoot() 46 adb.WaitForDevicePm() 47