blob: bbee81adb59980400dc333d373fc71d2b561f40f [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]b6128aa2010-04-29 17:44:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]307af212013-07-10 18:36:095#include "base/process/kill.h"
6
Avi Drissman63e1f992023-01-13 18:54:437#include "base/functional/bind.h"
[email protected]307af212013-07-10 18:36:098#include "base/process/process_iterator.h"
Gabriel Charette0bcc6ca2020-03-14 00:12:449#include "base/task/thread_pool.h"
Wezc18a57c2018-04-02 20:20:1410#include "base/time/time.h"
Xiaohan Wang37e81612022-01-15 18:27:0011#include "build/build_config.h"
[email protected]b6128aa2010-04-29 17:44:4212
13namespace base {
14
[email protected]307af212013-07-10 18:36:0915bool KillProcesses(const FilePath::StringType& executable_name,
16 int exit_code,
[email protected]b6128aa2010-04-29 17:44:4217 const ProcessFilter* filter) {
18 bool result = true;
19 NamedProcessIterator iter(executable_name, filter);
20 while (const ProcessEntry* entry = iter.NextProcessEntry()) {
rvargas4683e5e2015-03-04 22:38:2021 Process process = Process::Open(entry->pid());
Matt Mueller68086722017-11-09 19:24:5222 // Sometimes process open fails. This would cause a DCHECK in
23 // process.Terminate(). Maybe the process has killed itself between the
24 // time the process list was enumerated and the time we try to open the
25 // process?
26 if (!process.IsValid()) {
27 result = false;
28 continue;
29 }
rvargasf8d789c2015-04-01 04:10:1230 result &= process.Terminate(exit_code, true);
[email protected]b6128aa2010-04-29 17:44:4231 }
32 return result;
33}
34
Xiaohan Wang37e81612022-01-15 18:27:0035#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
Wezc18a57c2018-04-02 20:20:1436// Common implementation for platforms under which |process| is a handle to
37// the process, rather than an identifier that must be "reaped".
38void EnsureProcessTerminated(Process process) {
39 DCHECK(!process.is_current());
40
41 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
42 return;
43
Gabriel Charette0bcc6ca2020-03-14 00:12:4444 ThreadPool::PostDelayedTask(
Wezc18a57c2018-04-02 20:20:1445 FROM_HERE,
Gabriel Charette0bcc6ca2020-03-14 00:12:4446 {TaskPriority::BEST_EFFORT, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
Wezc18a57c2018-04-02 20:20:1447 BindOnce(
48 [](Process process) {
49 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
50 return;
Xiaohan Wang37e81612022-01-15 18:27:0051#if BUILDFLAG(IS_WIN)
Wezc18a57c2018-04-02 20:20:1452 process.Terminate(win::kProcessKilledExitCode, false);
53#else
54 process.Terminate(-1, false);
55#endif
56 },
57 std::move(process)),
Peter Kasting53fd6ee2021-10-05 20:40:4858 Seconds(2));
Wezc18a57c2018-04-02 20:20:1459}
Xiaohan Wang37e81612022-01-15 18:27:0060#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
Wezc18a57c2018-04-02 20:20:1461
[email protected]b6128aa2010-04-29 17:44:4262} // namespace base