OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains methods to iterate over processes on the system. | 5 // This file contains methods to iterate over processes on the system. |
6 | 6 |
7 #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_ | 7 #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_ |
8 #define BASE_PROCESS_PROCESS_ITERATOR_H_ | 8 #define BASE_PROCESS_PROCESS_ITERATOR_H_ |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/process/process.h" | 17 #include "base/process/process.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 | 19 |
20 #if defined(OS_WIN) | 20 #if defined(OS_MACOSX) || defined(OS_OPENBSD) |
21 #include <windows.h> | |
22 #include <tlhelp32.h> | |
23 #elif defined(OS_MACOSX) || defined(OS_OPENBSD) | |
24 #include <sys/sysctl.h> | 21 #include <sys/sysctl.h> |
25 #elif defined(OS_FREEBSD) | 22 #elif defined(OS_FREEBSD) |
26 #include <sys/user.h> | 23 #include <sys/user.h> |
27 #elif defined(OS_POSIX) | 24 #elif defined(OS_POSIX) |
28 #include <dirent.h> | 25 #include <dirent.h> |
29 #endif | 26 #endif |
30 | 27 |
31 namespace base { | 28 namespace base { |
32 | 29 |
33 #if defined(OS_WIN) | 30 #if defined(OS_POSIX) |
34 struct ProcessEntry : public PROCESSENTRY32 { | |
35 ProcessId pid() const { return th32ProcessID; } | |
36 ProcessId parent_pid() const { return th32ParentProcessID; } | |
37 const wchar_t* exe_file() const { return szExeFile; } | |
38 }; | |
39 #elif defined(OS_POSIX) | |
40 struct BASE_EXPORT ProcessEntry { | 31 struct BASE_EXPORT ProcessEntry { |
41 ProcessEntry(); | 32 ProcessEntry(); |
42 ~ProcessEntry(); | 33 ~ProcessEntry(); |
43 | 34 |
44 ProcessId pid() const { return pid_; } | 35 ProcessId pid() const { return pid_; } |
45 ProcessId parent_pid() const { return ppid_; } | 36 ProcessId parent_pid() const { return ppid_; } |
46 ProcessId gid() const { return gid_; } | 37 ProcessId gid() const { return gid_; } |
47 const char* exe_file() const { return exe_file_.c_str(); } | 38 const char* exe_file() const { return exe_file_.c_str(); } |
48 const std::vector<std::string>& cmd_line_args() const { | 39 const std::vector<std::string>& cmd_line_args() const { |
49 return cmd_line_args_; | 40 return cmd_line_args_; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 private: | 87 private: |
97 // Determines whether there's another process (regardless of executable) | 88 // Determines whether there's another process (regardless of executable) |
98 // left in the list of all processes. Returns true and sets entry_ to | 89 // left in the list of all processes. Returns true and sets entry_ to |
99 // that process's info if there is one, false otherwise. | 90 // that process's info if there is one, false otherwise. |
100 bool CheckForNextProcess(); | 91 bool CheckForNextProcess(); |
101 | 92 |
102 // Initializes a PROCESSENTRY32 data structure so that it's ready for | 93 // Initializes a PROCESSENTRY32 data structure so that it's ready for |
103 // use with Process32First/Process32Next. | 94 // use with Process32First/Process32Next. |
104 void InitProcessEntry(ProcessEntry* entry); | 95 void InitProcessEntry(ProcessEntry* entry); |
105 | 96 |
106 #if defined(OS_WIN) | 97 #if defined(OS_MACOSX) || defined(OS_BSD) |
107 HANDLE snapshot_; | |
108 bool started_iteration_; | |
109 #elif defined(OS_MACOSX) || defined(OS_BSD) | |
110 std::vector<kinfo_proc> kinfo_procs_; | 98 std::vector<kinfo_proc> kinfo_procs_; |
111 size_t index_of_kinfo_proc_; | 99 size_t index_of_kinfo_proc_; |
112 #elif defined(OS_POSIX) | 100 #elif defined(OS_POSIX) |
113 DIR* procfs_dir_; | 101 DIR* procfs_dir_; |
114 #endif | 102 #endif |
115 ProcessEntry entry_; | 103 ProcessEntry entry_; |
116 const ProcessFilter* filter_; | 104 const ProcessFilter* filter_; |
117 | 105 |
118 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); | 106 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
119 }; | 107 }; |
(...skipping 19 matching lines...) Expand all Loading... |
139 | 127 |
140 // Returns the number of processes on the machine that are running from the | 128 // Returns the number of processes on the machine that are running from the |
141 // given executable name. If filter is non-null, then only processes selected | 129 // given executable name. If filter is non-null, then only processes selected |
142 // by the filter will be counted. | 130 // by the filter will be counted. |
143 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, | 131 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, |
144 const ProcessFilter* filter); | 132 const ProcessFilter* filter); |
145 | 133 |
146 } // namespace base | 134 } // namespace base |
147 | 135 |
148 #endif // BASE_PROCESS_PROCESS_ITERATOR_H_ | 136 #endif // BASE_PROCESS_PROCESS_ITERATOR_H_ |
OLD | NEW |