Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 3d0734a828b50c6d5eb033ef943607b168865e0f [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]d3acb672013-05-31 15:38:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains methods to iterate over processes on the system.
6
7#ifndef BASE_PROCESS_PROCESS_ITERATOR_H_
8#define BASE_PROCESS_PROCESS_ITERATOR_H_
9
avibeced7c2015-12-24 06:47:5910#include <stddef.h>
11
[email protected]d3acb672013-05-31 15:38:0112#include <list>
13#include <string>
14#include <vector>
15
[email protected]d3acb672013-05-31 15:38:0116#include "base/base_export.h"
Tom Sepez04e98bf2024-10-25 18:19:3117#include "base/compiler_specific.h"
[email protected]d3acb672013-05-31 15:38:0118#include "base/files/file_path.h"
Keishi Hattori0e45c022021-11-27 09:25:5219#include "base/memory/raw_ptr.h"
[email protected]dd4b51262013-07-25 21:38:2320#include "base/process/process.h"
jdoerrie79b354c2019-05-07 16:41:0721#include "base/strings/string_util.h"
[email protected]dd4b51262013-07-25 21:38:2322#include "build/build_config.h"
[email protected]d3acb672013-05-31 15:38:0123
Xiaohan Wang37e81612022-01-15 18:27:0024#if BUILDFLAG(IS_WIN)
[email protected]d3acb672013-05-31 15:38:0125#include <windows.h>
Bruce Dawsona1e1cfcb2022-11-22 20:04:3526
[email protected]d3acb672013-05-31 15:38:0127#include <tlhelp32.h>
Xiaohan Wang37e81612022-01-15 18:27:0028#elif BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OPENBSD)
[email protected]d3acb672013-05-31 15:38:0129#include <sys/sysctl.h>
Xiaohan Wang37e81612022-01-15 18:27:0030#elif BUILDFLAG(IS_FREEBSD)
[email protected]fd0acad2014-03-11 04:11:0231#include <sys/user.h>
Xiaohan Wang37e81612022-01-15 18:27:0032#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d3acb672013-05-31 15:38:0133#include <dirent.h>
34#endif
35
36namespace base {
37
Xiaohan Wang37e81612022-01-15 18:27:0038#if BUILDFLAG(IS_WIN)
[email protected]d3acb672013-05-31 15:38:0139struct ProcessEntry : public PROCESSENTRY32 {
40 ProcessId pid() const { return th32ProcessID; }
41 ProcessId parent_pid() const { return th32ParentProcessID; }
Jan Wilken Dörrieb630aca2019-12-04 10:59:1142 const wchar_t* exe_file() const { return szExeFile; }
[email protected]d3acb672013-05-31 15:38:0143};
Xiaohan Wang37e81612022-01-15 18:27:0044#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]d3acb672013-05-31 15:38:0145struct BASE_EXPORT ProcessEntry {
46 ProcessEntry();
vmpstr7c7877062016-02-18 22:12:2447 ProcessEntry(const ProcessEntry& other);
[email protected]d3acb672013-05-31 15:38:0148 ~ProcessEntry();
49
50 ProcessId pid() const { return pid_; }
51 ProcessId parent_pid() const { return ppid_; }
52 ProcessId gid() const { return gid_; }
53 const char* exe_file() const { return exe_file_.c_str(); }
Tom Sepez04e98bf2024-10-25 18:19:3154 const std::vector<std::string>& cmd_line_args() const LIFETIME_BOUND {
[email protected]d3acb672013-05-31 15:38:0155 return cmd_line_args_;
56 }
57
58 ProcessId pid_;
59 ProcessId ppid_;
60 ProcessId gid_;
61 std::string exe_file_;
62 std::vector<std::string> cmd_line_args_;
63};
Xiaohan Wang37e81612022-01-15 18:27:0064#endif // BUILDFLAG(IS_WIN)
[email protected]d3acb672013-05-31 15:38:0165
66// Used to filter processes by process ID.
67class ProcessFilter {
68 public:
69 // Returns true to indicate set-inclusion and false otherwise. This method
70 // should not have side-effects and should be idempotent.
71 virtual bool Includes(const ProcessEntry& entry) const = 0;
72
73 protected:
Chris Watkins091d6292017-12-13 04:25:5874 virtual ~ProcessFilter() = default;
[email protected]d3acb672013-05-31 15:38:0175};
76
77// This class provides a way to iterate through a list of processes on the
78// current machine with a specified filter.
79// To use, create an instance and then call NextProcessEntry() until it returns
80// false.
81class BASE_EXPORT ProcessIterator {
82 public:
83 typedef std::list<ProcessEntry> ProcessEntries;
84
85 explicit ProcessIterator(const ProcessFilter* filter);
Peter Boström7319bbd2021-09-15 22:59:3886
87 ProcessIterator(const ProcessIterator&) = delete;
88 ProcessIterator& operator=(const ProcessIterator&) = delete;
89
[email protected]d3acb672013-05-31 15:38:0190 virtual ~ProcessIterator();
91
92 // If there's another process that matches the given executable name,
93 // returns a const pointer to the corresponding PROCESSENTRY32.
94 // If there are no more matching processes, returns NULL.
95 // The returned pointer will remain valid until NextProcessEntry()
96 // is called again or this NamedProcessIterator goes out of scope.
97 const ProcessEntry* NextProcessEntry();
98
99 // Takes a snapshot of all the ProcessEntry found.
100 ProcessEntries Snapshot();
101
102 protected:
103 virtual bool IncludeEntry();
Tom Sepez04e98bf2024-10-25 18:19:31104 const ProcessEntry& entry() const LIFETIME_BOUND { return entry_; }
[email protected]d3acb672013-05-31 15:38:01105
106 private:
107 // Determines whether there's another process (regardless of executable)
108 // left in the list of all processes. Returns true and sets entry_ to
109 // that process's info if there is one, false otherwise.
110 bool CheckForNextProcess();
111
112 // Initializes a PROCESSENTRY32 data structure so that it's ready for
113 // use with Process32First/Process32Next.
114 void InitProcessEntry(ProcessEntry* entry);
115
Xiaohan Wang37e81612022-01-15 18:27:00116#if BUILDFLAG(IS_WIN)
[email protected]d3acb672013-05-31 15:38:01117 HANDLE snapshot_;
Lei Zhang6f418e02023-02-08 21:55:43118 bool started_iteration_ = false;
Xiaohan Wang37e81612022-01-15 18:27:00119#elif BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_BSD)
[email protected]d3acb672013-05-31 15:38:01120 std::vector<kinfo_proc> kinfo_procs_;
Lei Zhang6f418e02023-02-08 21:55:43121 size_t index_of_kinfo_proc_ = 0;
Ali Hijazie8035ce42022-07-29 15:02:46122#elif BUILDFLAG(IS_POSIX)
123 struct DIRClose {
124 inline void operator()(DIR* x) const {
Peter Kasting134ef9af2024-12-28 02:30:09125 if (x) {
Ali Hijazie8035ce42022-07-29 15:02:46126 closedir(x);
Peter Kasting134ef9af2024-12-28 02:30:09127 }
Ali Hijazie8035ce42022-07-29 15:02:46128 }
129 };
130 std::unique_ptr<DIR, DIRClose> procfs_dir_;
[email protected]d3acb672013-05-31 15:38:01131#endif
132 ProcessEntry entry_;
Keishi Hattori0e45c022021-11-27 09:25:52133 raw_ptr<const ProcessFilter> filter_;
[email protected]d3acb672013-05-31 15:38:01134};
135
136// This class provides a way to iterate through the list of processes
137// on the current machine that were started from the given executable
138// name. To use, create an instance and then call NextProcessEntry()
139// until it returns false.
Leonard Grey77c540e2022-10-28 20:04:15140// If `use_prefix_match` is true, this iterates all processes that
141// begin with `executable_name`; for example, "Google Chrome Helper" would
142// match "Google Chrome Helper", "Google Chrome Helper (Renderer)" and
143// "Google Chrome Helper (GPU)" if `use_prefix_match` is true and otherwise
144// only "Google Chrome Helper". This option is only implemented on Mac.
[email protected]d3acb672013-05-31 15:38:01145class BASE_EXPORT NamedProcessIterator : public ProcessIterator {
146 public:
147 NamedProcessIterator(const FilePath::StringType& executable_name,
Leonard Grey77c540e2022-10-28 20:04:15148 const ProcessFilter* filter,
149 bool use_prefix_match = false);
Peter Boström7319bbd2021-09-15 22:59:38150
151 NamedProcessIterator(const NamedProcessIterator&) = delete;
152 NamedProcessIterator& operator=(const NamedProcessIterator&) = delete;
153
dcheng56488182014-10-21 10:54:51154 ~NamedProcessIterator() override;
[email protected]d3acb672013-05-31 15:38:01155
156 protected:
dcheng56488182014-10-21 10:54:51157 bool IncludeEntry() override;
[email protected]d3acb672013-05-31 15:38:01158
159 private:
160 FilePath::StringType executable_name_;
Leonard Grey77c540e2022-10-28 20:04:15161 const bool use_prefix_match_;
[email protected]d3acb672013-05-31 15:38:01162};
163
164// Returns the number of processes on the machine that are running from the
165// given executable name. If filter is non-null, then only processes selected
166// by the filter will be counted.
167BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name,
168 const ProcessFilter* filter);
169
170} // namespace base
171
172#endif // BASE_PROCESS_PROCESS_ITERATOR_H_