xref: /aosp_15_r20/external/libchrome/base/process/process_iterator.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/process_iterator.h"
6*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker namespace base {
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
ProcessEntry()11*635a8641SAndroid Build Coastguard Worker ProcessEntry::ProcessEntry() : pid_(0), ppid_(0), gid_(0) {}
12*635a8641SAndroid Build Coastguard Worker ProcessEntry::ProcessEntry(const ProcessEntry& other) = default;
13*635a8641SAndroid Build Coastguard Worker ProcessEntry::~ProcessEntry() = default;
14*635a8641SAndroid Build Coastguard Worker #endif
15*635a8641SAndroid Build Coastguard Worker 
NextProcessEntry()16*635a8641SAndroid Build Coastguard Worker const ProcessEntry* ProcessIterator::NextProcessEntry() {
17*635a8641SAndroid Build Coastguard Worker   bool result = false;
18*635a8641SAndroid Build Coastguard Worker   do {
19*635a8641SAndroid Build Coastguard Worker     result = CheckForNextProcess();
20*635a8641SAndroid Build Coastguard Worker   } while (result && !IncludeEntry());
21*635a8641SAndroid Build Coastguard Worker   if (result)
22*635a8641SAndroid Build Coastguard Worker     return &entry_;
23*635a8641SAndroid Build Coastguard Worker   return nullptr;
24*635a8641SAndroid Build Coastguard Worker }
25*635a8641SAndroid Build Coastguard Worker 
Snapshot()26*635a8641SAndroid Build Coastguard Worker ProcessIterator::ProcessEntries ProcessIterator::Snapshot() {
27*635a8641SAndroid Build Coastguard Worker   ProcessEntries found;
28*635a8641SAndroid Build Coastguard Worker   while (const ProcessEntry* process_entry = NextProcessEntry()) {
29*635a8641SAndroid Build Coastguard Worker     found.push_back(*process_entry);
30*635a8641SAndroid Build Coastguard Worker   }
31*635a8641SAndroid Build Coastguard Worker   return found;
32*635a8641SAndroid Build Coastguard Worker }
33*635a8641SAndroid Build Coastguard Worker 
IncludeEntry()34*635a8641SAndroid Build Coastguard Worker bool ProcessIterator::IncludeEntry() {
35*635a8641SAndroid Build Coastguard Worker   return !filter_ || filter_->Includes(entry_);
36*635a8641SAndroid Build Coastguard Worker }
37*635a8641SAndroid Build Coastguard Worker 
NamedProcessIterator(const FilePath::StringType & executable_name,const ProcessFilter * filter)38*635a8641SAndroid Build Coastguard Worker NamedProcessIterator::NamedProcessIterator(
39*635a8641SAndroid Build Coastguard Worker     const FilePath::StringType& executable_name,
40*635a8641SAndroid Build Coastguard Worker     const ProcessFilter* filter) : ProcessIterator(filter),
41*635a8641SAndroid Build Coastguard Worker                                    executable_name_(executable_name) {
42*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
43*635a8641SAndroid Build Coastguard Worker   // On Android, the process name contains only the last 15 characters, which
44*635a8641SAndroid Build Coastguard Worker   // is in file /proc/<pid>/stat, the string between open parenthesis and close
45*635a8641SAndroid Build Coastguard Worker   // parenthesis. Please See ProcessIterator::CheckForNextProcess for details.
46*635a8641SAndroid Build Coastguard Worker   // Now if the length of input process name is greater than 15, only save the
47*635a8641SAndroid Build Coastguard Worker   // last 15 characters.
48*635a8641SAndroid Build Coastguard Worker   if (executable_name_.size() > 15) {
49*635a8641SAndroid Build Coastguard Worker     executable_name_ = FilePath::StringType(executable_name_,
50*635a8641SAndroid Build Coastguard Worker                                             executable_name_.size() - 15, 15);
51*635a8641SAndroid Build Coastguard Worker   }
52*635a8641SAndroid Build Coastguard Worker #endif
53*635a8641SAndroid Build Coastguard Worker }
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker NamedProcessIterator::~NamedProcessIterator() = default;
56*635a8641SAndroid Build Coastguard Worker 
GetProcessCount(const FilePath::StringType & executable_name,const ProcessFilter * filter)57*635a8641SAndroid Build Coastguard Worker int GetProcessCount(const FilePath::StringType& executable_name,
58*635a8641SAndroid Build Coastguard Worker                     const ProcessFilter* filter) {
59*635a8641SAndroid Build Coastguard Worker   int count = 0;
60*635a8641SAndroid Build Coastguard Worker   NamedProcessIterator iter(executable_name, filter);
61*635a8641SAndroid Build Coastguard Worker   while (iter.NextProcessEntry())
62*635a8641SAndroid Build Coastguard Worker     ++count;
63*635a8641SAndroid Build Coastguard Worker   return count;
64*635a8641SAndroid Build Coastguard Worker }
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker }  // namespace base
67