xref: /aosp_15_r20/external/libchrome/base/process/process_iterator.h (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 // This file contains methods to iterate over processes on the system.
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_
8*635a8641SAndroid Build Coastguard Worker #define BASE_PROCESS_PROCESS_ITERATOR_H_
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <list>
13*635a8641SAndroid Build Coastguard Worker #include <string>
14*635a8641SAndroid Build Coastguard Worker #include <vector>
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
20*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
23*635a8641SAndroid Build Coastguard Worker #include <windows.h>
24*635a8641SAndroid Build Coastguard Worker #include <tlhelp32.h>
25*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX) || defined(OS_OPENBSD)
26*635a8641SAndroid Build Coastguard Worker #include <sys/sysctl.h>
27*635a8641SAndroid Build Coastguard Worker #elif defined(OS_FREEBSD)
28*635a8641SAndroid Build Coastguard Worker #include <sys/user.h>
29*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
30*635a8641SAndroid Build Coastguard Worker #include <dirent.h>
31*635a8641SAndroid Build Coastguard Worker #endif
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker namespace base {
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
36*635a8641SAndroid Build Coastguard Worker struct ProcessEntry : public PROCESSENTRY32 {
pidProcessEntry37*635a8641SAndroid Build Coastguard Worker   ProcessId pid() const { return th32ProcessID; }
parent_pidProcessEntry38*635a8641SAndroid Build Coastguard Worker   ProcessId parent_pid() const { return th32ParentProcessID; }
exe_fileProcessEntry39*635a8641SAndroid Build Coastguard Worker   const wchar_t* exe_file() const { return szExeFile; }
40*635a8641SAndroid Build Coastguard Worker };
41*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
42*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT ProcessEntry {
43*635a8641SAndroid Build Coastguard Worker   ProcessEntry();
44*635a8641SAndroid Build Coastguard Worker   ProcessEntry(const ProcessEntry& other);
45*635a8641SAndroid Build Coastguard Worker   ~ProcessEntry();
46*635a8641SAndroid Build Coastguard Worker 
47*635a8641SAndroid Build Coastguard Worker   ProcessId pid() const { return pid_; }
48*635a8641SAndroid Build Coastguard Worker   ProcessId parent_pid() const { return ppid_; }
49*635a8641SAndroid Build Coastguard Worker   ProcessId gid() const { return gid_; }
50*635a8641SAndroid Build Coastguard Worker   const char* exe_file() const { return exe_file_.c_str(); }
51*635a8641SAndroid Build Coastguard Worker   const std::vector<std::string>& cmd_line_args() const {
52*635a8641SAndroid Build Coastguard Worker     return cmd_line_args_;
53*635a8641SAndroid Build Coastguard Worker   }
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker   ProcessId pid_;
56*635a8641SAndroid Build Coastguard Worker   ProcessId ppid_;
57*635a8641SAndroid Build Coastguard Worker   ProcessId gid_;
58*635a8641SAndroid Build Coastguard Worker   std::string exe_file_;
59*635a8641SAndroid Build Coastguard Worker   std::vector<std::string> cmd_line_args_;
60*635a8641SAndroid Build Coastguard Worker };
61*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_WIN)
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker // Used to filter processes by process ID.
64*635a8641SAndroid Build Coastguard Worker class ProcessFilter {
65*635a8641SAndroid Build Coastguard Worker  public:
66*635a8641SAndroid Build Coastguard Worker   // Returns true to indicate set-inclusion and false otherwise.  This method
67*635a8641SAndroid Build Coastguard Worker   // should not have side-effects and should be idempotent.
68*635a8641SAndroid Build Coastguard Worker   virtual bool Includes(const ProcessEntry& entry) const = 0;
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker  protected:
71*635a8641SAndroid Build Coastguard Worker   virtual ~ProcessFilter() = default;
72*635a8641SAndroid Build Coastguard Worker };
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker // This class provides a way to iterate through a list of processes on the
75*635a8641SAndroid Build Coastguard Worker // current machine with a specified filter.
76*635a8641SAndroid Build Coastguard Worker // To use, create an instance and then call NextProcessEntry() until it returns
77*635a8641SAndroid Build Coastguard Worker // false.
78*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT ProcessIterator {
79*635a8641SAndroid Build Coastguard Worker  public:
80*635a8641SAndroid Build Coastguard Worker   typedef std::list<ProcessEntry> ProcessEntries;
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker   explicit ProcessIterator(const ProcessFilter* filter);
83*635a8641SAndroid Build Coastguard Worker   virtual ~ProcessIterator();
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker   // If there's another process that matches the given executable name,
86*635a8641SAndroid Build Coastguard Worker   // returns a const pointer to the corresponding PROCESSENTRY32.
87*635a8641SAndroid Build Coastguard Worker   // If there are no more matching processes, returns NULL.
88*635a8641SAndroid Build Coastguard Worker   // The returned pointer will remain valid until NextProcessEntry()
89*635a8641SAndroid Build Coastguard Worker   // is called again or this NamedProcessIterator goes out of scope.
90*635a8641SAndroid Build Coastguard Worker   const ProcessEntry* NextProcessEntry();
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   // Takes a snapshot of all the ProcessEntry found.
93*635a8641SAndroid Build Coastguard Worker   ProcessEntries Snapshot();
94*635a8641SAndroid Build Coastguard Worker 
95*635a8641SAndroid Build Coastguard Worker  protected:
96*635a8641SAndroid Build Coastguard Worker   virtual bool IncludeEntry();
entry()97*635a8641SAndroid Build Coastguard Worker   const ProcessEntry& entry() { return entry_; }
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker  private:
100*635a8641SAndroid Build Coastguard Worker   // Determines whether there's another process (regardless of executable)
101*635a8641SAndroid Build Coastguard Worker   // left in the list of all processes.  Returns true and sets entry_ to
102*635a8641SAndroid Build Coastguard Worker   // that process's info if there is one, false otherwise.
103*635a8641SAndroid Build Coastguard Worker   bool CheckForNextProcess();
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker   // Initializes a PROCESSENTRY32 data structure so that it's ready for
106*635a8641SAndroid Build Coastguard Worker   // use with Process32First/Process32Next.
107*635a8641SAndroid Build Coastguard Worker   void InitProcessEntry(ProcessEntry* entry);
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
110*635a8641SAndroid Build Coastguard Worker   HANDLE snapshot_;
111*635a8641SAndroid Build Coastguard Worker   bool started_iteration_;
112*635a8641SAndroid Build Coastguard Worker #elif defined(OS_MACOSX) || defined(OS_BSD)
113*635a8641SAndroid Build Coastguard Worker   std::vector<kinfo_proc> kinfo_procs_;
114*635a8641SAndroid Build Coastguard Worker   size_t index_of_kinfo_proc_;
115*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
116*635a8641SAndroid Build Coastguard Worker   DIR* procfs_dir_;
117*635a8641SAndroid Build Coastguard Worker #endif
118*635a8641SAndroid Build Coastguard Worker   ProcessEntry entry_;
119*635a8641SAndroid Build Coastguard Worker   const ProcessFilter* filter_;
120*635a8641SAndroid Build Coastguard Worker 
121*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ProcessIterator);
122*635a8641SAndroid Build Coastguard Worker };
123*635a8641SAndroid Build Coastguard Worker 
124*635a8641SAndroid Build Coastguard Worker // This class provides a way to iterate through the list of processes
125*635a8641SAndroid Build Coastguard Worker // on the current machine that were started from the given executable
126*635a8641SAndroid Build Coastguard Worker // name.  To use, create an instance and then call NextProcessEntry()
127*635a8641SAndroid Build Coastguard Worker // until it returns false.
128*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT NamedProcessIterator : public ProcessIterator {
129*635a8641SAndroid Build Coastguard Worker  public:
130*635a8641SAndroid Build Coastguard Worker   NamedProcessIterator(const FilePath::StringType& executable_name,
131*635a8641SAndroid Build Coastguard Worker                        const ProcessFilter* filter);
132*635a8641SAndroid Build Coastguard Worker   ~NamedProcessIterator() override;
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker  protected:
135*635a8641SAndroid Build Coastguard Worker   bool IncludeEntry() override;
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker  private:
138*635a8641SAndroid Build Coastguard Worker   FilePath::StringType executable_name_;
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator);
141*635a8641SAndroid Build Coastguard Worker };
142*635a8641SAndroid Build Coastguard Worker 
143*635a8641SAndroid Build Coastguard Worker // Returns the number of processes on the machine that are running from the
144*635a8641SAndroid Build Coastguard Worker // given executable name.  If filter is non-null, then only processes selected
145*635a8641SAndroid Build Coastguard Worker // by the filter will be counted.
146*635a8641SAndroid Build Coastguard Worker BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name,
147*635a8641SAndroid Build Coastguard Worker                                 const ProcessFilter* filter);
148*635a8641SAndroid Build Coastguard Worker 
149*635a8641SAndroid Build Coastguard Worker }  // namespace base
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_PROCESS_ITERATOR_H_
152