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