blob: f641d3fe03e1ec78aa790bdc5d03bda60b0b097c [file] [log] [blame]
[email protected]307af212013-07-10 18:36:091// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[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
Wezc18a57c2018-04-02 20:20:147#include "base/bind.h"
[email protected]307af212013-07-10 18:36:098#include "base/process/process_iterator.h"
Gabriel Charette44db1422018-08-06 11:19:339#include "base/task/post_task.h"
Wezc18a57c2018-04-02 20:20:1410#include "base/time/time.h"
[email protected]b6128aa2010-04-29 17:44:4211
12namespace base {
13
[email protected]307af212013-07-10 18:36:0914bool KillProcesses(const FilePath::StringType& executable_name,
15 int exit_code,
[email protected]b6128aa2010-04-29 17:44:4216 const ProcessFilter* filter) {
17 bool result = true;
18 NamedProcessIterator iter(executable_name, filter);
19 while (const ProcessEntry* entry = iter.NextProcessEntry()) {
rvargas4683e5e2015-03-04 22:38:2020 Process process = Process::Open(entry->pid());
Matt Mueller68086722017-11-09 19:24:5221 // Sometimes process open fails. This would cause a DCHECK in
22 // process.Terminate(). Maybe the process has killed itself between the
23 // time the process list was enumerated and the time we try to open the
24 // process?
25 if (!process.IsValid()) {
26 result = false;
27 continue;
28 }
rvargasf8d789c2015-04-01 04:10:1229 result &= process.Terminate(exit_code, true);
[email protected]b6128aa2010-04-29 17:44:4230 }
31 return result;
32}
33
Wezc18a57c2018-04-02 20:20:1434#if defined(OS_WIN) || defined(OS_FUCHSIA)
35// Common implementation for platforms under which |process| is a handle to
36// the process, rather than an identifier that must be "reaped".
37void EnsureProcessTerminated(Process process) {
38 DCHECK(!process.is_current());
39
40 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
41 return;
42
43 PostDelayedTaskWithTraits(
44 FROM_HERE,
Gabriel Charettef042bf22018-07-27 18:01:1645 {TaskPriority::BEST_EFFORT, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
Wezc18a57c2018-04-02 20:20:1446 BindOnce(
47 [](Process process) {
48 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
49 return;
50#if defined(OS_WIN)
51 process.Terminate(win::kProcessKilledExitCode, false);
52#else
53 process.Terminate(-1, false);
54#endif
55 },
56 std::move(process)),
57 TimeDelta::FromSeconds(2));
58}
59#endif // defined(OS_WIN) || defined(OS_FUCHSIA)
60
[email protected]b6128aa2010-04-29 17:44:4261} // namespace base