[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 5 | #include "base/process/kill.h" |
| 6 | |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 8 | #include "base/process/process_iterator.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 9 | #include "base/task/post_task.h" |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 10 | #include "base/time/time.h" |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 14 | bool KillProcesses(const FilePath::StringType& executable_name, |
| 15 | int exit_code, |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 16 | const ProcessFilter* filter) { |
| 17 | bool result = true; |
| 18 | NamedProcessIterator iter(executable_name, filter); |
| 19 | while (const ProcessEntry* entry = iter.NextProcessEntry()) { |
rvargas | 4683e5e | 2015-03-04 22:38:20 | [diff] [blame] | 20 | Process process = Process::Open(entry->pid()); |
Matt Mueller | 6808672 | 2017-11-09 19:24:52 | [diff] [blame] | 21 | // 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 | } |
rvargas | f8d789c | 2015-04-01 04:10:12 | [diff] [blame] | 29 | result &= process.Terminate(exit_code, true); |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 30 | } |
| 31 | return result; |
| 32 | } |
| 33 | |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 34 | #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". |
| 37 | void EnsureProcessTerminated(Process process) { |
| 38 | DCHECK(!process.is_current()); |
| 39 | |
| 40 | if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) |
| 41 | return; |
| 42 | |
| 43 | PostDelayedTaskWithTraits( |
| 44 | FROM_HERE, |
Gabriel Charette | f042bf2 | 2018-07-27 18:01:16 | [diff] [blame] | 45 | {TaskPriority::BEST_EFFORT, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 46 | 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] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 61 | } // namespace base |