Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[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 | |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 8 | #include "base/process/process_iterator.h" |
Gabriel Charette | 0bcc6ca | 2020-03-14 00:12:44 | [diff] [blame] | 9 | #include "base/task/thread_pool.h" |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 10 | #include "base/time/time.h" |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
[email protected] | 307af21 | 2013-07-10 18:36:09 | [diff] [blame] | 15 | bool KillProcesses(const FilePath::StringType& executable_name, |
| 16 | int exit_code, |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 17 | const ProcessFilter* filter) { |
| 18 | bool result = true; |
| 19 | NamedProcessIterator iter(executable_name, filter); |
| 20 | while (const ProcessEntry* entry = iter.NextProcessEntry()) { |
rvargas | 4683e5e | 2015-03-04 22:38:20 | [diff] [blame] | 21 | Process process = Process::Open(entry->pid()); |
Matt Mueller | 6808672 | 2017-11-09 19:24:52 | [diff] [blame] | 22 | // 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 | } |
rvargas | f8d789c | 2015-04-01 04:10:12 | [diff] [blame] | 30 | result &= process.Terminate(exit_code, true); |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 31 | } |
| 32 | return result; |
| 33 | } |
| 34 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 35 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA) |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 36 | // Common implementation for platforms under which |process| is a handle to |
| 37 | // the process, rather than an identifier that must be "reaped". |
| 38 | void EnsureProcessTerminated(Process process) { |
| 39 | DCHECK(!process.is_current()); |
| 40 | |
| 41 | if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) |
| 42 | return; |
| 43 | |
Gabriel Charette | 0bcc6ca | 2020-03-14 00:12:44 | [diff] [blame] | 44 | ThreadPool::PostDelayedTask( |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 45 | FROM_HERE, |
Gabriel Charette | 0bcc6ca | 2020-03-14 00:12:44 | [diff] [blame] | 46 | {TaskPriority::BEST_EFFORT, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 47 | BindOnce( |
| 48 | [](Process process) { |
| 49 | if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) |
| 50 | return; |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 51 | #if BUILDFLAG(IS_WIN) |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 52 | process.Terminate(win::kProcessKilledExitCode, false); |
| 53 | #else |
| 54 | process.Terminate(-1, false); |
| 55 | #endif |
| 56 | }, |
| 57 | std::move(process)), |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 58 | Seconds(2)); |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 59 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 60 | #endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA) |
Wez | c18a57c | 2018-04-02 20:20:14 | [diff] [blame] | 61 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 62 | } // namespace base |