1 // Copyright (c) 2013 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. 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 10 #include <list> 11 #include <string> 12 #include <vector> 13 14 #include "base/base_export.h" 15 #include "base/basictypes.h" 16 #include "base/files/file_path.h" 17 #include "base/process/process.h" 18 #include "build/build_config.h" 19 20 #if defined(OS_WIN) 21 #include <windows.h> 22 #include <tlhelp32.h> 23 #elif defined(OS_MACOSX) || defined(OS_BSD) 24 #include <sys/sysctl.h> 25 #elif defined(OS_POSIX) 26 #include <dirent.h> 27 #endif 28 29 namespace base { 30 31 #if defined(OS_WIN) 32 struct ProcessEntry : public PROCESSENTRY32 { 33 ProcessId pid() const { return th32ProcessID; } 34 ProcessId parent_pid() const { return th32ParentProcessID; } 35 const wchar_t* exe_file() const { return szExeFile; } 36 }; 37 38 // Process access masks. These constants provide platform-independent 39 // definitions for the standard Windows access masks. 40 // See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for 41 // the specific semantics of each mask value. 42 const uint32 kProcessAccessTerminate = PROCESS_TERMINATE; 43 const uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD; 44 const uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID; 45 const uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION; 46 const uint32 kProcessAccessVMRead = PROCESS_VM_READ; 47 const uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE; 48 const uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE; 49 const uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS; 50 const uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA; 51 const uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION; 52 const uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION; 53 const uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME; 54 const uint32 kProcessAccessQueryLimitedInfomation = 55 PROCESS_QUERY_LIMITED_INFORMATION; 56 const uint32 kProcessAccessWaitForTermination = SYNCHRONIZE; 57 #elif defined(OS_POSIX) 58 struct BASE_EXPORT ProcessEntry { 59 ProcessEntry(); 60 ~ProcessEntry(); 61 62 ProcessId pid() const { return pid_; } 63 ProcessId parent_pid() const { return ppid_; } 64 ProcessId gid() const { return gid_; } 65 const char* exe_file() const { return exe_file_.c_str(); } 66 const std::vector<std::string>& cmd_line_args() const { 67 return cmd_line_args_; 68 } 69 70 ProcessId pid_; 71 ProcessId ppid_; 72 ProcessId gid_; 73 std::string exe_file_; 74 std::vector<std::string> cmd_line_args_; 75 }; 76 77 // Process access masks. They are not used on Posix because access checking 78 // does not happen during handle creation. 79 const uint32 kProcessAccessTerminate = 0; 80 const uint32 kProcessAccessCreateThread = 0; 81 const uint32 kProcessAccessSetSessionId = 0; 82 const uint32 kProcessAccessVMOperation = 0; 83 const uint32 kProcessAccessVMRead = 0; 84 const uint32 kProcessAccessVMWrite = 0; 85 const uint32 kProcessAccessDuplicateHandle = 0; 86 const uint32 kProcessAccessCreateProcess = 0; 87 const uint32 kProcessAccessSetQuota = 0; 88 const uint32 kProcessAccessSetInformation = 0; 89 const uint32 kProcessAccessQueryInformation = 0; 90 const uint32 kProcessAccessSuspendResume = 0; 91 const uint32 kProcessAccessQueryLimitedInfomation = 0; 92 const uint32 kProcessAccessWaitForTermination = 0; 93 #endif // defined(OS_POSIX) 94 95 // Used to filter processes by process ID. 96 class ProcessFilter { 97 public: 98 // Returns true to indicate set-inclusion and false otherwise. This method 99 // should not have side-effects and should be idempotent. 100 virtual bool Includes(const ProcessEntry& entry) const = 0; 101 102 protected: 103 virtual ~ProcessFilter() {} 104 }; 105 106 // This class provides a way to iterate through a list of processes on the 107 // current machine with a specified filter. 108 // To use, create an instance and then call NextProcessEntry() until it returns 109 // false. 110 class BASE_EXPORT ProcessIterator { 111 public: 112 typedef std::list<ProcessEntry> ProcessEntries; 113 114 explicit ProcessIterator(const ProcessFilter* filter); 115 virtual ~ProcessIterator(); 116 117 // If there's another process that matches the given executable name, 118 // returns a const pointer to the corresponding PROCESSENTRY32. 119 // If there are no more matching processes, returns NULL. 120 // The returned pointer will remain valid until NextProcessEntry() 121 // is called again or this NamedProcessIterator goes out of scope. 122 const ProcessEntry* NextProcessEntry(); 123 124 // Takes a snapshot of all the ProcessEntry found. 125 ProcessEntries Snapshot(); 126 127 protected: 128 virtual bool IncludeEntry(); 129 const ProcessEntry& entry() { return entry_; } 130 131 private: 132 // Determines whether there's another process (regardless of executable) 133 // left in the list of all processes. Returns true and sets entry_ to 134 // that process's info if there is one, false otherwise. 135 bool CheckForNextProcess(); 136 137 // Initializes a PROCESSENTRY32 data structure so that it's ready for 138 // use with Process32First/Process32Next. 139 void InitProcessEntry(ProcessEntry* entry); 140 141 #if defined(OS_WIN) 142 HANDLE snapshot_; 143 bool started_iteration_; 144 #elif defined(OS_MACOSX) || defined(OS_BSD) 145 std::vector<kinfo_proc> kinfo_procs_; 146 size_t index_of_kinfo_proc_; 147 #elif defined(OS_POSIX) 148 DIR* procfs_dir_; 149 #endif 150 ProcessEntry entry_; 151 const ProcessFilter* filter_; 152 153 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); 154 }; 155 156 // This class provides a way to iterate through the list of processes 157 // on the current machine that were started from the given executable 158 // name. To use, create an instance and then call NextProcessEntry() 159 // until it returns false. 160 class BASE_EXPORT NamedProcessIterator : public ProcessIterator { 161 public: 162 NamedProcessIterator(const FilePath::StringType& executable_name, 163 const ProcessFilter* filter); 164 virtual ~NamedProcessIterator(); 165 166 protected: 167 virtual bool IncludeEntry() OVERRIDE; 168 169 private: 170 FilePath::StringType executable_name_; 171 172 DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); 173 }; 174 175 // Returns the number of processes on the machine that are running from the 176 // given executable name. If filter is non-null, then only processes selected 177 // by the filter will be counted. 178 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, 179 const ProcessFilter* filter); 180 181 } // namespace base 182 183 #endif // BASE_PROCESS_PROCESS_ITERATOR_H_ 184