1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/process_util.h"
10 ProcessEntry::ProcessEntry() : pid_(0), ppid_(0), gid_(0) {}
11 ProcessEntry::~ProcessEntry() {}
14 int GetProcessCount(const FilePath::StringType
& executable_name
,
15 const ProcessFilter
* filter
) {
17 NamedProcessIterator
iter(executable_name
, filter
);
18 while (iter
.NextProcessEntry())
23 bool KillProcesses(const FilePath::StringType
& executable_name
, int exit_code
,
24 const ProcessFilter
* filter
) {
26 NamedProcessIterator
iter(executable_name
, filter
);
27 while (const ProcessEntry
* entry
= iter
.NextProcessEntry()) {
29 result
&= KillProcessById(entry
->pid(), exit_code
, true);
31 result
&= KillProcess(entry
->pid(), exit_code
, true);
37 const ProcessEntry
* ProcessIterator::NextProcessEntry() {
40 result
= CheckForNextProcess();
41 } while (result
&& !IncludeEntry());
47 ProcessIterator::ProcessEntries
ProcessIterator::Snapshot() {
49 while (const ProcessEntry
* process_entry
= NextProcessEntry()) {
50 found
.push_back(*process_entry
);
55 bool ProcessIterator::IncludeEntry() {
56 return !filter_
|| filter_
->Includes(entry_
);
59 NamedProcessIterator::NamedProcessIterator(
60 const FilePath::StringType
& executable_name
,
61 const ProcessFilter
* filter
) : ProcessIterator(filter
),
62 executable_name_(executable_name
) {
63 #if defined(OS_ANDROID)
64 // On Android, the process name contains only the last 15 characters, which
65 // is in file /proc/<pid>/stat, the string between open parenthesis and close
66 // parenthesis. Please See ProcessIterator::CheckForNextProcess for details.
67 // Now if the length of input process name is greater than 15, only save the
68 // last 15 characters.
69 if (executable_name_
.size() > 15) {
70 executable_name_
= FilePath::StringType(executable_name_
,
71 executable_name_
.size() - 15, 15);
76 NamedProcessIterator::~NamedProcessIterator() {