xref: /aosp_15_r20/external/libchrome/base/process/kill.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/process/kill.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/process/process_iterator.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/post_task.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker namespace base {
13*635a8641SAndroid Build Coastguard Worker 
KillProcesses(const FilePath::StringType & executable_name,int exit_code,const ProcessFilter * filter)14*635a8641SAndroid Build Coastguard Worker bool KillProcesses(const FilePath::StringType& executable_name,
15*635a8641SAndroid Build Coastguard Worker                    int exit_code,
16*635a8641SAndroid Build Coastguard Worker                    const ProcessFilter* filter) {
17*635a8641SAndroid Build Coastguard Worker   bool result = true;
18*635a8641SAndroid Build Coastguard Worker   NamedProcessIterator iter(executable_name, filter);
19*635a8641SAndroid Build Coastguard Worker   while (const ProcessEntry* entry = iter.NextProcessEntry()) {
20*635a8641SAndroid Build Coastguard Worker     Process process = Process::Open(entry->pid());
21*635a8641SAndroid Build Coastguard Worker     // Sometimes process open fails. This would cause a DCHECK in
22*635a8641SAndroid Build Coastguard Worker     // process.Terminate(). Maybe the process has killed itself between the
23*635a8641SAndroid Build Coastguard Worker     // time the process list was enumerated and the time we try to open the
24*635a8641SAndroid Build Coastguard Worker     // process?
25*635a8641SAndroid Build Coastguard Worker     if (!process.IsValid()) {
26*635a8641SAndroid Build Coastguard Worker       result = false;
27*635a8641SAndroid Build Coastguard Worker       continue;
28*635a8641SAndroid Build Coastguard Worker     }
29*635a8641SAndroid Build Coastguard Worker     result &= process.Terminate(exit_code, true);
30*635a8641SAndroid Build Coastguard Worker   }
31*635a8641SAndroid Build Coastguard Worker   return result;
32*635a8641SAndroid Build Coastguard Worker }
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) || defined(OS_FUCHSIA)
35*635a8641SAndroid Build Coastguard Worker // Common implementation for platforms under which |process| is a handle to
36*635a8641SAndroid Build Coastguard Worker // the process, rather than an identifier that must be "reaped".
EnsureProcessTerminated(Process process)37*635a8641SAndroid Build Coastguard Worker void EnsureProcessTerminated(Process process) {
38*635a8641SAndroid Build Coastguard Worker   DCHECK(!process.is_current());
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker   if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
41*635a8641SAndroid Build Coastguard Worker     return;
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker   PostDelayedTaskWithTraits(
44*635a8641SAndroid Build Coastguard Worker       FROM_HERE,
45*635a8641SAndroid Build Coastguard Worker       {TaskPriority::BACKGROUND, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
46*635a8641SAndroid Build Coastguard Worker       BindOnce(
47*635a8641SAndroid Build Coastguard Worker           [](Process process) {
48*635a8641SAndroid Build Coastguard Worker             if (process.WaitForExitWithTimeout(TimeDelta(), nullptr))
49*635a8641SAndroid Build Coastguard Worker               return;
50*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
51*635a8641SAndroid Build Coastguard Worker             process.Terminate(win::kProcessKilledExitCode, false);
52*635a8641SAndroid Build Coastguard Worker #else
53*635a8641SAndroid Build Coastguard Worker             process.Terminate(-1, false);
54*635a8641SAndroid Build Coastguard Worker #endif
55*635a8641SAndroid Build Coastguard Worker           },
56*635a8641SAndroid Build Coastguard Worker           std::move(process)),
57*635a8641SAndroid Build Coastguard Worker       TimeDelta::FromSeconds(2));
58*635a8641SAndroid Build Coastguard Worker }
59*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN) || defined(OS_FUCHSIA)
60*635a8641SAndroid Build Coastguard Worker 
61*635a8641SAndroid Build Coastguard Worker }  // namespace base
62