1*9c5db199SXin Li# Copyright 2016 The Chromium OS Authors. All rights reserved. 2*9c5db199SXin Li# Use of this source code is governed by a BSD-style license that can be 3*9c5db199SXin Li# found in the LICENSE file. 4*9c5db199SXin Li 5*9c5db199SXin Liimport time 6*9c5db199SXin Li 7*9c5db199SXin Lifrom autotest_lib.client.common_lib import error 8*9c5db199SXin Lifrom autotest_lib.client.common_lib import utils 9*9c5db199SXin Li 10*9c5db199SXin Lidef pkill_process(process_name, is_full_name=True, 11*9c5db199SXin Li timeout_seconds=60, host=None, 12*9c5db199SXin Li ignore_status=False): 13*9c5db199SXin Li """Run pkill against a process until it dies. 14*9c5db199SXin Li 15*9c5db199SXin Li @param process_name: the name of a process. 16*9c5db199SXin Li @param is_full_name: True iff the value of |process_name| is the complete 17*9c5db199SXin Li name of the process as understood by pkill. 18*9c5db199SXin Li @param timeout_seconds: number of seconds to wait for proceess to die. 19*9c5db199SXin Li @param host: host object to kill the process on. Defaults to killing 20*9c5db199SXin Li processes on our localhost. 21*9c5db199SXin Li @param ignore_status: True iff we should ignore whether we actually 22*9c5db199SXin Li managed to kill the given process. 23*9c5db199SXin Li 24*9c5db199SXin Li """ 25*9c5db199SXin Li run = host.run if host is not None else utils.run 26*9c5db199SXin Li full_flag = '-f' if is_full_name else '' 27*9c5db199SXin Li kill_cmd = 'pkill %s "%s"' % (full_flag, process_name) 28*9c5db199SXin Li 29*9c5db199SXin Li result = run(kill_cmd, ignore_status=True) 30*9c5db199SXin Li start_time = time.time() 31*9c5db199SXin Li while (0 == result.exit_status and 32*9c5db199SXin Li time.time() - start_time < timeout_seconds): 33*9c5db199SXin Li time.sleep(0.3) 34*9c5db199SXin Li result = run(kill_cmd, ignore_status=True) 35*9c5db199SXin Li 36*9c5db199SXin Li if result.exit_status == 0 and not ignore_status: 37*9c5db199SXin Li r = run('cat /proc/`pgrep %s`/status' % process_name, 38*9c5db199SXin Li ignore_status=True) 39*9c5db199SXin Li raise error.TestError('Failed to kill proccess "%s":\n%s' % 40*9c5db199SXin Li (process_name, r.stdout)) 41