erg@google.com | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | // This file/namespace contains utility functions for enumerating, ending and |
| 6 | // computing statistics of processes. |
| 7 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 8 | #ifndef BASE_PROCESS_UTIL_H_ |
| 9 | #define BASE_PROCESS_UTIL_H_ |
thakis@chromium.org | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 10 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 14 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include <windows.h> |
| 16 | #include <tlhelp32.h> |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 17 | #elif defined(OS_MACOSX) |
| 18 | // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration |
| 19 | // is sufficient for the vector<kinfo_proc> below. |
| 20 | struct kinfo_proc; |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 21 | // malloc_zone_t is defined in <malloc/malloc.h>, but this forward declaration |
| 22 | // is sufficient for GetPurgeableZone() below. |
| 23 | typedef struct _malloc_zone_t malloc_zone_t; |
thakis@chromium.org | c002879 | 2010-01-12 00:39:15 | [diff] [blame] | 24 | #include <mach/mach.h> |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 25 | #elif defined(OS_POSIX) |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 26 | #include <dirent.h> |
| 27 | #include <limits.h> |
| 28 | #include <sys/types.h> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 29 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 31 | #include <list> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 32 | #include <string> |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 33 | #include <utility> |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 34 | #include <vector> |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 35 | |
rvargas@google.com | f5661ca | 2011-03-24 19:00:20 | [diff] [blame] | 36 | #include "base/base_api.h" |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 37 | #include "base/file_descriptor_shuffle.h" |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 38 | #include "base/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | #include "base/process.h" |
| 40 | |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 41 | class CommandLine; |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 42 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 43 | namespace base { |
| 44 | |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 45 | #if defined(OS_WIN) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 46 | struct ProcessEntry : public PROCESSENTRY32 { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 47 | ProcessId pid() const { return th32ProcessID; } |
| 48 | ProcessId parent_pid() const { return th32ParentProcessID; } |
| 49 | const wchar_t* exe_file() const { return szExeFile; } |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 50 | }; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 51 | |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 52 | struct IoCounters : public IO_COUNTERS { |
| 53 | }; |
| 54 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 55 | // Process access masks. These constants provide platform-independent |
| 56 | // definitions for the standard Windows access masks. |
| 57 | // See http://msdn.microsoft.com/en-us/library/ms684880(VS.85).aspx for |
| 58 | // the specific semantics of each mask value. |
| 59 | const uint32 kProcessAccessTerminate = PROCESS_TERMINATE; |
| 60 | const uint32 kProcessAccessCreateThread = PROCESS_CREATE_THREAD; |
| 61 | const uint32 kProcessAccessSetSessionId = PROCESS_SET_SESSIONID; |
| 62 | const uint32 kProcessAccessVMOperation = PROCESS_VM_OPERATION; |
| 63 | const uint32 kProcessAccessVMRead = PROCESS_VM_READ; |
| 64 | const uint32 kProcessAccessVMWrite = PROCESS_VM_WRITE; |
| 65 | const uint32 kProcessAccessDuplicateHandle = PROCESS_DUP_HANDLE; |
| 66 | const uint32 kProcessAccessCreateProcess = PROCESS_CREATE_PROCESS; |
| 67 | const uint32 kProcessAccessSetQuota = PROCESS_SET_QUOTA; |
| 68 | const uint32 kProcessAccessSetInformation = PROCESS_SET_INFORMATION; |
| 69 | const uint32 kProcessAccessQueryInformation = PROCESS_QUERY_INFORMATION; |
| 70 | const uint32 kProcessAccessSuspendResume = PROCESS_SUSPEND_RESUME; |
| 71 | const uint32 kProcessAccessQueryLimitedInfomation = |
| 72 | PROCESS_QUERY_LIMITED_INFORMATION; |
| 73 | const uint32 kProcessAccessWaitForTermination = SYNCHRONIZE; |
paulg@google.com | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 74 | #elif defined(OS_POSIX) |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 75 | |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 76 | struct ProcessEntry { |
erg@google.com | bbf94a3 | 2010-10-13 17:44:15 | [diff] [blame] | 77 | ProcessEntry(); |
| 78 | ~ProcessEntry(); |
| 79 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 80 | ProcessId pid() const { return pid_; } |
| 81 | ProcessId parent_pid() const { return ppid_; } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 82 | ProcessId gid() const { return gid_; } |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 83 | const char* exe_file() const { return exe_file_.c_str(); } |
rsimha@chromium.org | c47d81d | 2010-10-05 23:41:04 | [diff] [blame] | 84 | const std::vector<std::string>& cmd_line_args() const { |
| 85 | return cmd_line_args_; |
| 86 | } |
erg@google.com | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 87 | |
| 88 | ProcessId pid_; |
| 89 | ProcessId ppid_; |
| 90 | ProcessId gid_; |
| 91 | std::string exe_file_; |
| 92 | std::vector<std::string> cmd_line_args_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 93 | }; |
| 94 | |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 95 | struct IoCounters { |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 96 | uint64_t ReadOperationCount; |
| 97 | uint64_t WriteOperationCount; |
| 98 | uint64_t OtherOperationCount; |
| 99 | uint64_t ReadTransferCount; |
| 100 | uint64_t WriteTransferCount; |
| 101 | uint64_t OtherTransferCount; |
evanm@google.com | 0b100bc8 | 2008-10-14 20:49:16 | [diff] [blame] | 102 | }; |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 103 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 104 | // Process access masks. They are not used on Posix because access checking |
| 105 | // does not happen during handle creation. |
| 106 | const uint32 kProcessAccessTerminate = 0; |
| 107 | const uint32 kProcessAccessCreateThread = 0; |
| 108 | const uint32 kProcessAccessSetSessionId = 0; |
| 109 | const uint32 kProcessAccessVMOperation = 0; |
| 110 | const uint32 kProcessAccessVMRead = 0; |
| 111 | const uint32 kProcessAccessVMWrite = 0; |
| 112 | const uint32 kProcessAccessDuplicateHandle = 0; |
| 113 | const uint32 kProcessAccessCreateProcess = 0; |
| 114 | const uint32 kProcessAccessSetQuota = 0; |
| 115 | const uint32 kProcessAccessSetInformation = 0; |
| 116 | const uint32 kProcessAccessQueryInformation = 0; |
| 117 | const uint32 kProcessAccessSuspendResume = 0; |
| 118 | const uint32 kProcessAccessQueryLimitedInfomation = 0; |
| 119 | const uint32 kProcessAccessWaitForTermination = 0; |
maruel@chromium.org | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 120 | #endif // defined(OS_POSIX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 122 | // Return status values from GetTerminationStatus. Don't use these as |
| 123 | // exit code arguments to KillProcess*(), use platform/application |
| 124 | // specific values instead. |
| 125 | enum TerminationStatus { |
| 126 | TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status |
| 127 | TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-zero exit status |
| 128 | TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill |
| 129 | TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault |
scheib@chromium.org | bcb2c77 | 2011-04-01 20:20:53 | [diff] [blame] | 130 | TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet |
| 131 | TERMINATION_STATUS_MAX_ENUM |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 132 | }; |
| 133 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | // Returns the id of the current process. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 135 | BASE_API ProcessId GetCurrentProcId(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 137 | // Returns the ProcessHandle of the current process. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 138 | BASE_API ProcessHandle GetCurrentProcessHandle(); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 139 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 140 | // Converts a PID to a process handle. This handle must be closed by |
phajdan.jr@chromium.org | 6c6cc80 | 2009-04-03 17:01:36 | [diff] [blame] | 141 | // CloseProcessHandle when you are done with it. Returns true on success. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 142 | BASE_API bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 143 | |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 144 | // Converts a PID to a process handle. On Windows the handle is opened |
| 145 | // with more access rights and must only be used by trusted code. |
| 146 | // You have to close returned handle using CloseProcessHandle. Returns true |
| 147 | // on success. |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 148 | // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
| 149 | // more specific OpenProcessHandleWithAccess method and delete this. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 150 | BASE_API bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle); |
phajdan.jr@chromium.org | 5d438dbad | 2009-04-30 08:59:39 | [diff] [blame] | 151 | |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 152 | // Converts a PID to a process handle using the desired access flags. Use a |
| 153 | // combination of the kProcessAccess* flags defined above for |access_flags|. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 154 | BASE_API bool OpenProcessHandleWithAccess(ProcessId pid, |
| 155 | uint32 access_flags, |
| 156 | ProcessHandle* handle); |
sanjeevr@chromium.org | 7d11f6d5 | 2010-10-12 21:44:23 | [diff] [blame] | 157 | |
brettw@google.com | 5986ed2 | 2009-02-06 00:19:17 | [diff] [blame] | 158 | // Closes the process handle opened by OpenProcessHandle. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 159 | BASE_API void CloseProcessHandle(ProcessHandle process); |
erikkay@google.com | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 160 | |
cpu@google.com | eef576f | 2008-11-03 23:28:06 | [diff] [blame] | 161 | // Returns the unique ID for the specified process. This is functionally the |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | // same as Windows' GetProcessId(), but works on versions of Windows before |
| 163 | // Win XP SP1 as well. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 164 | BASE_API ProcessId GetProcId(ProcessHandle process); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 166 | #if defined(OS_LINUX) |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 167 | // Returns the path to the executable of the given process. |
evan@chromium.org | 68a008e | 2011-05-02 17:54:14 | [diff] [blame] | 168 | BASE_API FilePath GetProcessExecutablePath(ProcessHandle process); |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 169 | |
| 170 | // Parse the data found in /proc/<pid>/stat and return the sum of the |
| 171 | // CPU-related ticks. Returns -1 on parse error. |
| 172 | // Exposed for testing. |
evan@chromium.org | 91d91fa | 2011-04-29 20:45:22 | [diff] [blame] | 173 | BASE_API int ParseProcStatCPU(const std::string& input); |
thestig@chromium.org | e5856a7a | 2009-12-10 02:08:10 | [diff] [blame] | 174 | |
| 175 | static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; |
| 176 | |
| 177 | // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer |
| 178 | // certain process types over others. The range for the adjustment is |
| 179 | // [-17,15], with [0,15] being user accessible. |
evan@chromium.org | 68a008e | 2011-05-02 17:54:14 | [diff] [blame] | 180 | BASE_API bool AdjustOOMScore(ProcessId process, int score); |
dkegel@google.com | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 181 | #endif |
| 182 | |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 183 | #if defined(OS_POSIX) |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 184 | // Returns the ID for the parent of the given process. |
evan@chromium.org | 91d91fa | 2011-04-29 20:45:22 | [diff] [blame] | 185 | BASE_API ProcessId GetParentProcessId(ProcessHandle process); |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 186 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 187 | // Close all file descriptors, except those which are a destination in the |
agl@chromium.org | 3f04f2b | 2009-04-30 19:40:03 | [diff] [blame] | 188 | // given multimap. Only call this function in a child process where you know |
| 189 | // that there aren't any other threads. |
evan@chromium.org | 68a008e | 2011-05-02 17:54:14 | [diff] [blame] | 190 | BASE_API void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
jeremy@chromium.org | fa3097a6a5 | 2008-12-17 22:41:50 | [diff] [blame] | 191 | #endif |
| 192 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 193 | // TODO(evan): rename these to use StudlyCaps. |
| 194 | typedef std::vector<std::pair<std::string, std::string> > environment_vector; |
| 195 | typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
| 196 | |
| 197 | // Options for launching a subprocess that are passed to LaunchApp(). |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 198 | // The default constructor constructs the object with default options. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 199 | struct LaunchOptions { |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 200 | LaunchOptions() : wait(false), |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 201 | #if defined(OS_WIN) |
| 202 | start_hidden(false), inherit_handles(false), as_user(NULL), |
| 203 | empty_desktop_name(false) |
| 204 | #else |
| 205 | environ(NULL), fds_to_remap(NULL), new_process_group(false) |
| 206 | #endif |
| 207 | {} |
| 208 | |
| 209 | // If true, wait for the process to complete. |
| 210 | bool wait; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 211 | |
| 212 | #if defined(OS_WIN) |
| 213 | bool start_hidden; |
| 214 | |
| 215 | // If true, the new process inherits handles from the parent. |
| 216 | bool inherit_handles; |
| 217 | |
| 218 | // If non-NULL, runs as if the user represented by the token had launched it. |
| 219 | // Whether the application is visible on the interactive desktop depends on |
| 220 | // the token belonging to an interactive logon session. |
| 221 | // |
| 222 | // To avoid hard to diagnose problems, when specified this loads the |
| 223 | // environment variables associated with the user and if this operation fails |
| 224 | // the entire call fails as well. |
| 225 | UserTokenHandle as_user; |
| 226 | |
| 227 | // If true, use an empty string for the desktop name. |
| 228 | bool empty_desktop_name; |
| 229 | #else |
| 230 | // If non-NULL, set/unset environment variables. |
| 231 | // See documentation of AlterEnvironment(). |
| 232 | // This pointer is owned by the caller and must live through the |
| 233 | // call to LaunchProcess(). |
| 234 | const environment_vector* environ; |
| 235 | |
| 236 | // If non-NULL, remap file descriptors according to the mapping of |
| 237 | // src fd->dest fd to propagate FDs into the child process. |
| 238 | // This pointer is owned by the caller and must live through the |
| 239 | // call to LaunchProcess(). |
| 240 | const file_handle_mapping_vector* fds_to_remap; |
| 241 | |
| 242 | // If true, start the process in a new process group, instead of |
| 243 | // inheriting the parent's process group. The pgid of the child process |
| 244 | // will be the same as its pid. |
| 245 | bool new_process_group; |
| 246 | #endif |
| 247 | }; |
| 248 | |
| 249 | // Launch a process via the command line |cmdline|. |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 250 | // See the documentation of LaunchOptions for details on |options|. |
| 251 | // |
| 252 | // If |process_handle| is non-NULL, it will be filled in with the |
| 253 | // handle of the launched process. NOTE: In this case, the caller is |
| 254 | // responsible for closing the handle so that it doesn't leak! |
| 255 | // Otherwise, the process handle will be implicitly closed. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 256 | // |
| 257 | // Unix-specific notes: |
| 258 | // - Before launching, all FDs open in the parent process will be marked as |
| 259 | // close-on-exec. |
| 260 | // - If the first argument on the command line does not contain a slash, |
| 261 | // PATH will be searched. (See man execvp.) |
| 262 | BASE_API bool LaunchProcess(const CommandLine& cmdline, |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 263 | const LaunchOptions& options, |
| 264 | ProcessHandle* process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 265 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 266 | #if defined(OS_WIN) |
tommi@chromium.org | 48dc9e1 | 2010-08-26 19:49:57 | [diff] [blame] | 267 | |
| 268 | enum IntegrityLevel { |
| 269 | INTEGRITY_UNKNOWN, |
| 270 | LOW_INTEGRITY, |
| 271 | MEDIUM_INTEGRITY, |
| 272 | HIGH_INTEGRITY, |
| 273 | }; |
| 274 | // Determine the integrity level of the specified process. Returns false |
| 275 | // if the system does not support integrity levels (pre-Vista) or in the case |
| 276 | // of an underlying system failure. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 277 | BASE_API bool GetProcessIntegrityLevel(ProcessHandle process, |
| 278 | IntegrityLevel *level); |
tommi@chromium.org | 48dc9e1 | 2010-08-26 19:49:57 | [diff] [blame] | 279 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 280 | // Windows-specific LaunchProcess that takes the command line as a |
| 281 | // string. Useful for situations where you need to control the |
| 282 | // command line arguments directly, but prefer the CommandLine version |
| 283 | // if launching Chrome itself. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 284 | // |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 285 | // The first command line argument should be the path to the process, |
| 286 | // and don't forget to quote it. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 287 | // |
| 288 | // Example (including literal quotes) |
| 289 | // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 290 | BASE_API bool LaunchProcess(const string16& cmdline, |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 291 | const LaunchOptions& options, |
| 292 | ProcessHandle* process_handle); |
cpu@chromium.org | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 293 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 294 | // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 295 | inline bool LaunchApp(const std::wstring& cmdline, |
| 296 | bool wait, bool start_hidden, |
| 297 | ProcessHandle* process_handle) { |
| 298 | LaunchOptions options; |
| 299 | options.wait = wait; |
| 300 | options.start_hidden = start_hidden; |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 301 | return LaunchProcess(cmdline, options, process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 302 | } |
finnur@chromium.org | 81e3260 | 2010-09-07 14:01:31 | [diff] [blame] | 303 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 304 | // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 305 | inline bool LaunchAppWithHandleInheritance(const std::wstring& cmdline, |
| 306 | bool wait, bool start_hidden, |
| 307 | ProcessHandle* process_handle) { |
| 308 | LaunchOptions options; |
| 309 | options.wait = wait; |
| 310 | options.start_hidden = start_hidden; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 311 | options.inherit_handles = true; |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 312 | return LaunchProcess(cmdline, options, process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 313 | } |
cpu@chromium.org | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 314 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 315 | // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 316 | inline bool LaunchAppAsUser(UserTokenHandle token, |
| 317 | const std::wstring& cmdline, |
| 318 | bool start_hidden, |
| 319 | ProcessHandle* process_handle) { |
| 320 | LaunchOptions options; |
| 321 | options.start_hidden = start_hidden; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 322 | options.as_user = token; |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 323 | return LaunchProcess(cmdline, options, process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 324 | } |
gwilson@google.com | c020ddc | 2010-02-18 23:01:52 | [diff] [blame] | 325 | |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 326 | // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
| 327 | inline bool LaunchAppAsUser(UserTokenHandle token, |
| 328 | const std::wstring& cmdline, |
| 329 | bool start_hidden, ProcessHandle* process_handle, |
| 330 | bool empty_desktop_name, bool inherit_handles) { |
| 331 | LaunchOptions options; |
| 332 | options.start_hidden = start_hidden; |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 333 | options.as_user = token; |
| 334 | options.empty_desktop_name = empty_desktop_name; |
| 335 | options.inherit_handles = inherit_handles; |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 336 | return LaunchProcess(cmdline, options, process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 337 | } |
gwilson@google.com | c020ddc | 2010-02-18 23:01:52 | [diff] [blame] | 338 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 339 | #elif defined(OS_POSIX) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 340 | // A POSIX-specific version of LaunchProcess that takes an argv array |
| 341 | // instead of a CommandLine. Useful for situations where you need to |
| 342 | // control the command line arguments directly, but prefer the |
| 343 | // CommandLine version if launching Chrome itself. |
| 344 | BASE_API bool LaunchProcess(const std::vector<std::string>& argv, |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 345 | const LaunchOptions& options, |
| 346 | ProcessHandle* process_handle); |
stuartmorgan@google.com | 2aea9e09 | 2009-08-06 20:03:01 | [diff] [blame] | 347 | |
agl@chromium.org | ef73044e | 2010-03-11 15:25:54 | [diff] [blame] | 348 | // AlterEnvironment returns a modified environment vector, constructed from the |
| 349 | // given environment and the list of changes given in |changes|. Each key in |
| 350 | // the environment is matched against the first element of the pairs. In the |
| 351 | // event of a match, the value is replaced by the second of the pair, unless |
| 352 | // the second is empty, in which case the key-value is removed. |
| 353 | // |
| 354 | // The returned array is allocated using new[] and must be freed by the caller. |
evan@chromium.org | 91d91fa | 2011-04-29 20:45:22 | [diff] [blame] | 355 | BASE_API char** AlterEnvironment(const environment_vector& changes, |
| 356 | const char* const* const env); |
thestig@chromium.org | 9ec8db0 | 2009-07-21 07:00:13 | [diff] [blame] | 357 | #endif // defined(OS_POSIX) |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 358 | |
evan@chromium.org | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 359 | #if defined(OS_WIN) |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 360 | // TODO(evan): deprecated; change callers to use LaunchProcess, remove. |
evan@chromium.org | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 361 | // Successfully deprecated on non-Windows. |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 362 | inline bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, |
| 363 | ProcessHandle* process_handle) { |
| 364 | LaunchOptions options; |
| 365 | options.wait = wait; |
evan@chromium.org | e599218 | 2011-07-15 16:47:02 | [diff] [blame^] | 366 | options.start_hidden = start_hidden; |
| 367 | return LaunchProcess(cl, options, process_handle); |
evan@chromium.org | 898a81a | 2011-06-30 22:56:15 | [diff] [blame] | 368 | } |
evan@chromium.org | adda869 | 2011-07-14 20:55:30 | [diff] [blame] | 369 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 370 | |
jcampan@chromium.org | 1e31211 | 2009-04-21 21:44:12 | [diff] [blame] | 371 | // Executes the application specified by |cl| and wait for it to exit. Stores |
phajdan.jr@chromium.org | 1912cfe | 2009-04-21 08:09:30 | [diff] [blame] | 372 | // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 373 | // on success (application launched and exited cleanly, with exit code |
maruel@chromium.org | 96878a21 | 2010-06-10 18:26:33 | [diff] [blame] | 374 | // indicating success). |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 375 | BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); |
phajdan.jr@chromium.org | c0b210ee | 2009-04-17 09:57:52 | [diff] [blame] | 376 | |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 377 | #if defined(OS_POSIX) |
| 378 | // A restricted version of |GetAppOutput()| which (a) clears the environment, |
| 379 | // and (b) stores at most |max_output| bytes; also, it doesn't search the path |
| 380 | // for the command. |
evan@chromium.org | 91d91fa | 2011-04-29 20:45:22 | [diff] [blame] | 381 | BASE_API bool GetAppOutputRestricted(const CommandLine& cl, |
| 382 | std::string* output, size_t max_output); |
benwells@chromium.org | d03b05c | 2011-07-06 06:14:48 | [diff] [blame] | 383 | |
| 384 | // A version of |GetAppOutput()| which also returns the exit code of the |
| 385 | // executed command. Returns true if the application runs and exits cleanly. If |
| 386 | // this is the case the exit code of the application is available in |
| 387 | // |*exit_code|. |
| 388 | BASE_API bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 389 | std::string* output, int* exit_code); |
viettrungluu@chromium.org | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 390 | #endif |
| 391 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 392 | // Used to filter processes by process ID. |
| 393 | class ProcessFilter { |
| 394 | public: |
| 395 | // Returns true to indicate set-inclusion and false otherwise. This method |
| 396 | // should not have side-effects and should be idempotent. |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 397 | virtual bool Includes(const ProcessEntry& entry) const = 0; |
ziadh@chromium.org | 695092f | 2010-08-02 16:34:16 | [diff] [blame] | 398 | |
| 399 | protected: |
| 400 | virtual ~ProcessFilter() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | // Returns the number of processes on the machine that are running from the |
| 404 | // given executable name. If filter is non-null, then only processes selected |
| 405 | // by the filter will be counted. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 406 | BASE_API int GetProcessCount(const FilePath::StringType& executable_name, |
| 407 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | |
| 409 | // Attempts to kill all the processes on the current machine that were launched |
| 410 | // from the given executable name, ending them with the given exit code. If |
| 411 | // filter is non-null, then only processes selected by the filter are killed. |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 412 | // Returns true if all processes were able to be killed off, false if at least |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 413 | // one couldn't be killed. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 414 | BASE_API bool KillProcesses(const FilePath::StringType& executable_name, |
| 415 | int exit_code, const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 416 | |
| 417 | // Attempts to kill the process identified by the given process |
| 418 | // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 419 | // for the process to be actually terminated before returning. |
| 420 | // Returns true if this is successful, false otherwise. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 421 | BASE_API bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 422 | |
| 423 | #if defined(OS_POSIX) |
| 424 | // Attempts to kill the process group identified by |process_group_id|. Returns |
| 425 | // true on success. |
evan@chromium.org | c48d860 | 2011-05-09 21:46:08 | [diff] [blame] | 426 | BASE_API bool KillProcessGroup(ProcessHandle process_group_id); |
rsimha@chromium.org | 61b93f88f | 2010-09-22 17:28:30 | [diff] [blame] | 427 | #endif |
| 428 | |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 429 | #if defined(OS_WIN) |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 430 | BASE_API bool KillProcessById(ProcessId process_id, int exit_code, bool wait); |
agl@chromium.org | dfe1486 | 2009-01-22 01:23:11 | [diff] [blame] | 431 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 432 | |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 433 | // Get the termination status of the process by interpreting the |
| 434 | // circumstances of the child process' death. |exit_code| is set to |
| 435 | // the status returned by waitpid() on POSIX, and from |
| 436 | // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the |
| 437 | // caller is not interested in it. Note that on Linux, this function |
| 438 | // will only return a useful result the first time it is called after |
| 439 | // the child exits (because it will reap the child and the information |
| 440 | // will no longer be available). |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 441 | BASE_API TerminationStatus GetTerminationStatus(ProcessHandle handle, |
| 442 | int* exit_code); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 443 | |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 444 | // Waits for process to exit. On POSIX systems, if the process hasn't been |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 445 | // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 446 | // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 447 | // and closes |handle| in any case. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 448 | BASE_API bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
phajdan.jr@chromium.org | c785663 | 2009-01-13 17:38:49 | [diff] [blame] | 449 | |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 450 | // Waits for process to exit. If it did exit within |timeout_milliseconds|, |
phajdan.jr@chromium.org | 40bbe59 | 2011-04-06 12:18:20 | [diff] [blame] | 451 | // then puts the exit code in |exit_code|, and returns true. |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 452 | // In POSIX systems, if the process has been signaled then |exit_code| is set |
| 453 | // to -1. Returns false on failure (the caller is then responsible for closing |
| 454 | // |handle|). |
phajdan.jr@chromium.org | 40bbe59 | 2011-04-06 12:18:20 | [diff] [blame] | 455 | // The caller is always responsible for closing the |handle|. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 456 | BASE_API bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, |
| 457 | int64 timeout_milliseconds); |
phajdan.jr@chromium.org | 8004e68 | 2010-03-16 07:41:22 | [diff] [blame] | 458 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 459 | // Wait for all the processes based on the named executable to exit. If filter |
| 460 | // is non-null, then only processes selected by the filter are waited on. |
| 461 | // Returns after all processes have exited or wait_milliseconds have expired. |
| 462 | // Returns true if all the processes exited, false otherwise. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 463 | BASE_API bool WaitForProcessesToExit( |
| 464 | const FilePath::StringType& executable_name, |
| 465 | int64 wait_milliseconds, |
| 466 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 467 | |
estade@chromium.org | fb7f9be | 2008-10-22 01:15:47 | [diff] [blame] | 468 | // Wait for a single process to exit. Return true if it exited cleanly within |
dmaclach@chromium.org | 56f0f26 | 2011-02-24 17:14:36 | [diff] [blame] | 469 | // the given time limit. On Linux |handle| must be a child process, however |
| 470 | // on Mac and Windows it can be any process. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 471 | BASE_API bool WaitForSingleProcess(ProcessHandle handle, |
| 472 | int64 wait_milliseconds); |
phajdan.jr@chromium.org | c7c98097 | 2011-04-05 18:05:34 | [diff] [blame] | 473 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 474 | // Waits a certain amount of time (can be 0) for all the processes with a given |
| 475 | // executable name to exit, then kills off any of them that are still around. |
| 476 | // If filter is non-null, then only processes selected by the filter are waited |
| 477 | // on. Killed processes are ended with the given exit code. Returns false if |
| 478 | // any processes needed to be killed, true if they all exited cleanly within |
| 479 | // the wait_milliseconds delay. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 480 | BASE_API bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 481 | int64 wait_milliseconds, |
| 482 | int exit_code, |
| 483 | const ProcessFilter* filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 484 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 485 | // This class provides a way to iterate through a list of processes on the |
| 486 | // current machine with a specified filter. |
| 487 | // To use, create an instance and then call NextProcessEntry() until it returns |
| 488 | // false. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 489 | class BASE_API ProcessIterator { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 490 | public: |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 491 | typedef std::list<ProcessEntry> ProcessEntries; |
| 492 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 493 | explicit ProcessIterator(const ProcessFilter* filter); |
| 494 | virtual ~ProcessIterator(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 495 | |
| 496 | // If there's another process that matches the given executable name, |
| 497 | // returns a const pointer to the corresponding PROCESSENTRY32. |
| 498 | // If there are no more matching processes, returns NULL. |
| 499 | // The returned pointer will remain valid until NextProcessEntry() |
| 500 | // is called again or this NamedProcessIterator goes out of scope. |
| 501 | const ProcessEntry* NextProcessEntry(); |
| 502 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 503 | // Takes a snapshot of all the ProcessEntry found. |
maruel@chromium.org | baead4f | 2010-06-11 19:10:30 | [diff] [blame] | 504 | ProcessEntries Snapshot(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 505 | |
| 506 | protected: |
| 507 | virtual bool IncludeEntry(); |
| 508 | const ProcessEntry& entry() { return entry_; } |
| 509 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 510 | private: |
| 511 | // Determines whether there's another process (regardless of executable) |
| 512 | // left in the list of all processes. Returns true and sets entry_ to |
| 513 | // that process's info if there is one, false otherwise. |
| 514 | bool CheckForNextProcess(); |
| 515 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 516 | // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 517 | // use with Process32First/Process32Next. |
| 518 | void InitProcessEntry(ProcessEntry* entry); |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 519 | |
| 520 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 521 | HANDLE snapshot_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 522 | bool started_iteration_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 523 | #elif defined(OS_MACOSX) |
mark@chromium.org | 962dd31 | 2009-02-05 21:44:13 | [diff] [blame] | 524 | std::vector<kinfo_proc> kinfo_procs_; |
| 525 | size_t index_of_kinfo_proc_; |
wtc@chromium.org | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 526 | #elif defined(OS_POSIX) |
| 527 | DIR *procfs_dir_; |
dkegel@google.com | ab0e222 | 2008-10-31 20:19:43 | [diff] [blame] | 528 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 529 | ProcessEntry entry_; |
| 530 | const ProcessFilter* filter_; |
| 531 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 532 | DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
| 533 | }; |
| 534 | |
| 535 | // This class provides a way to iterate through the list of processes |
| 536 | // on the current machine that were started from the given executable |
| 537 | // name. To use, create an instance and then call NextProcessEntry() |
| 538 | // until it returns false. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 539 | class BASE_API NamedProcessIterator : public ProcessIterator { |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 540 | public: |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 541 | NamedProcessIterator(const FilePath::StringType& executable_name, |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 542 | const ProcessFilter* filter); |
| 543 | virtual ~NamedProcessIterator(); |
| 544 | |
| 545 | protected: |
| 546 | virtual bool IncludeEntry(); |
| 547 | |
| 548 | private: |
avi@chromium.org | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 549 | FilePath::StringType executable_name_; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 550 | |
| 551 | DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | // Working Set (resident) memory usage broken down by |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 555 | // |
| 556 | // On Windows: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 557 | // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 558 | // shareable: These pages (kbytes) can be shared with other processes under |
| 559 | // the right circumstances. |
| 560 | // shared : These pages (kbytes) are currently shared with at least one |
| 561 | // other process. |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 562 | // |
| 563 | // On Linux: |
| 564 | // priv: Pages mapped only by this process |
| 565 | // shared: PSS or 0 if the kernel doesn't support this |
| 566 | // shareable: 0 |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 567 | // |
| 568 | // On OS X: TODO(thakis): Revise. |
| 569 | // priv: Memory. |
| 570 | // shared: 0 |
| 571 | // shareable: 0 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 572 | struct WorkingSetKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 573 | WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 574 | size_t priv; |
| 575 | size_t shareable; |
| 576 | size_t shared; |
| 577 | }; |
| 578 | |
| 579 | // Committed (resident + paged) memory usage broken down by |
| 580 | // private: These pages cannot be shared with any other process. |
| 581 | // mapped: These pages are mapped into the view of a section (backed by |
| 582 | // pagefile.sys) |
| 583 | // image: These pages are mapped into the view of an image section (backed by |
| 584 | // file system) |
| 585 | struct CommittedKBytes { |
agl@chromium.org | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 586 | CommittedKBytes() : priv(0), mapped(0), image(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 587 | size_t priv; |
| 588 | size_t mapped; |
| 589 | size_t image; |
| 590 | }; |
| 591 | |
| 592 | // Free memory (Megabytes marked as free) in the 2G process address space. |
| 593 | // total : total amount in megabytes marked as free. Maximum value is 2048. |
| 594 | // largest : size of the largest contiguous amount of memory found. It is |
| 595 | // always smaller or equal to FreeMBytes::total. |
| 596 | // largest_ptr: starting address of the largest memory block. |
| 597 | struct FreeMBytes { |
| 598 | size_t total; |
| 599 | size_t largest; |
| 600 | void* largest_ptr; |
| 601 | }; |
| 602 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 603 | // Convert a POSIX timeval to microseconds. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 604 | BASE_API int64 TimeValToMicroseconds(const struct timeval& tv); |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 605 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 606 | // Provides performance metrics for a specified process (CPU usage, memory and |
| 607 | // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 608 | // for a specific process, then access the information with the different get |
| 609 | // methods. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 610 | class BASE_API ProcessMetrics { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 611 | public: |
erg@google.com | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 612 | ~ProcessMetrics(); |
| 613 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 614 | // Creates a ProcessMetrics for the specified process. |
| 615 | // The caller owns the returned object. |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 616 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 617 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 618 | #else |
| 619 | class PortProvider { |
| 620 | public: |
thakis@chromium.org | b2e8e08 | 2009-12-21 17:44:20 | [diff] [blame] | 621 | // Should return the mach task for |process| if possible, or else |
| 622 | // |MACH_PORT_NULL|. Only processes that this returns tasks for will have |
| 623 | // metrics on OS X (except for the current process, which always gets |
| 624 | // metrics). |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 625 | virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; |
| 626 | }; |
| 627 | |
| 628 | // The port provider needs to outlive the ProcessMetrics object returned by |
| 629 | // this function. If NULL is passed as provider, the returned object |
| 630 | // only returns valid metrics if |process| is the current process. |
| 631 | static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, |
| 632 | PortProvider* port_provider); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 633 | #endif // !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 634 | |
| 635 | // Returns the current space allocated for the pagefile, in bytes (these pages |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 636 | // may or may not be in memory). On Linux, this returns the total virtual |
| 637 | // memory size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 638 | size_t GetPagefileUsage() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 639 | // Returns the peak space allocated for the pagefile, in bytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 640 | size_t GetPeakPagefileUsage() const; |
thomasvl@chromium.org | 796da7c | 2009-06-11 12:45:45 | [diff] [blame] | 641 | // Returns the current working set size, in bytes. On Linux, this returns |
| 642 | // the resident set size. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 643 | size_t GetWorkingSetSize() const; |
tc@google.com | 0c557f1 | 2009-05-11 23:35:52 | [diff] [blame] | 644 | // Returns the peak working set size, in bytes. |
| 645 | size_t GetPeakWorkingSetSize() const; |
erg@chromium.org | 98947a0 | 2010-05-11 17:46:08 | [diff] [blame] | 646 | // Returns private and sharedusage, in bytes. Private bytes is the amount of |
| 647 | // memory currently allocated to a process that cannot be shared. Returns |
| 648 | // false on platform specific error conditions. Note: |private_bytes| |
| 649 | // returns 0 on unsupported OSes: prior to XP SP2. |
| 650 | bool GetMemoryBytes(size_t* private_bytes, |
| 651 | size_t* shared_bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 652 | // Fills a CommittedKBytes with both resident and paged |
| 653 | // memory usage as per definition of CommittedBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 654 | void GetCommittedKBytes(CommittedKBytes* usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 655 | // Fills a WorkingSetKBytes containing resident private and shared memory |
| 656 | // usage in bytes, as per definition of WorkingSetBytes. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 657 | bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 658 | |
| 659 | // Computes the current process available memory for allocation. |
| 660 | // It does a linear scan of the address space querying each memory region |
| 661 | // for its free (unallocated) status. It is useful for estimating the memory |
| 662 | // load and fragmentation. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 663 | bool CalculateFreeMemory(FreeMBytes* free) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 664 | |
| 665 | // Returns the CPU usage in percent since the last time this method was |
| 666 | // called. The first time this method is called it returns 0 and will return |
| 667 | // the actual CPU info on subsequent calls. |
thakis@chromium.org | 022eab6 | 2010-01-13 04:55:06 | [diff] [blame] | 668 | // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and |
| 669 | // your process is using all the cycles of 1 CPU and not the other CPU, this |
| 670 | // method returns 50. |
| 671 | double GetCPUUsage(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 672 | |
| 673 | // Retrieves accounting information for all I/O operations performed by the |
| 674 | // process. |
| 675 | // If IO information is retrieved successfully, the function returns true |
| 676 | // and fills in the IO_COUNTERS passed in. The function returns false |
| 677 | // otherwise. |
phajdan.jr@chromium.org | d043c2cc | 2009-03-25 18:30:45 | [diff] [blame] | 678 | bool GetIOCounters(IoCounters* io_counters) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 679 | |
| 680 | private: |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 681 | #if !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 682 | explicit ProcessMetrics(ProcessHandle process); |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 683 | #else |
| 684 | ProcessMetrics(ProcessHandle process, PortProvider* port_provider); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 685 | #endif // !defined(OS_MACOSX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 686 | |
| 687 | ProcessHandle process_; |
| 688 | |
| 689 | int processor_count_; |
| 690 | |
evan@chromium.org | d2ed2383 | 2009-09-19 01:57:39 | [diff] [blame] | 691 | // Used to store the previous times and CPU usage counts so we can |
| 692 | // compute the CPU usage between calls. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 693 | int64 last_time_; |
| 694 | int64 last_system_time_; |
| 695 | |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 696 | #if defined(OS_MACOSX) |
| 697 | // Queries the port provider if it's set. |
| 698 | mach_port_t TaskForPid(ProcessHandle process) const; |
| 699 | |
| 700 | PortProvider* port_provider_; |
pvalchev@google.com | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 701 | #elif defined(OS_POSIX) |
| 702 | // Jiffie count at the last_time_ we updated. |
| 703 | int last_cpu_; |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 704 | #endif // defined(OS_MACOSX) |
viettrungluu@chromium.org | 3740cb9b5 | 2009-12-19 04:50:04 | [diff] [blame] | 705 | |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 706 | DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 707 | }; |
| 708 | |
sgk@chromium.org | ed26d94 | 2009-11-09 06:57:28 | [diff] [blame] | 709 | // Returns the memory commited by the system in KBytes. |
| 710 | // Returns 0 if it can't compute the commit charge. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 711 | BASE_API size_t GetSystemCommitCharge(); |
sgk@chromium.org | ed26d94 | 2009-11-09 06:57:28 | [diff] [blame] | 712 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 713 | // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 714 | // won't have any effect on heaps created after this function call. It will not |
| 715 | // modify data allocated in the heaps before calling this function. So it is |
| 716 | // better to call this function early in initialization and again before |
| 717 | // entering the main loop. |
| 718 | // Note: Returns true on Windows 2000 without doing anything. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 719 | BASE_API bool EnableLowFragmentationHeap(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 720 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 721 | // Enables 'terminate on heap corruption' flag. Helps protect against heap |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 722 | // overflow. Has no effect if the OS doesn't provide the necessary facility. |
rvargas@google.com | f5661ca | 2011-03-24 19:00:20 | [diff] [blame] | 723 | BASE_API void EnableTerminationOnHeapCorruption(); |
maruel@google.com | c9d4087 | 2008-09-24 12:58:37 | [diff] [blame] | 724 | |
avi@chromium.org | cccb2121 | 2009-11-12 20:39:56 | [diff] [blame] | 725 | #if !defined(OS_WIN) |
| 726 | // Turns on process termination if memory runs out. This is handled on Windows |
| 727 | // inside RegisterInvalidParamHandler(). |
evan@chromium.org | 91d91fa | 2011-04-29 20:45:22 | [diff] [blame] | 728 | BASE_API void EnableTerminationOnOutOfMemory(); |
avi@chromium.org | 6cfa339 | 2010-07-01 20:11:43 | [diff] [blame] | 729 | #if defined(OS_MACOSX) |
| 730 | // Exposed for testing. |
| 731 | malloc_zone_t* GetPurgeableZone(); |
| 732 | #endif |
avi@chromium.org | cccb2121 | 2009-11-12 20:39:56 | [diff] [blame] | 733 | #endif |
| 734 | |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 735 | // Enables stack dump to console output on exception and signals. |
| 736 | // When enabled, the process will quit immediately. This is meant to be used in |
| 737 | // unit_tests only! |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 738 | BASE_API bool EnableInProcessStackDumping(); |
maruel@chromium.org | d6fc9fd | 2009-10-27 18:03:47 | [diff] [blame] | 739 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 740 | // If supported on the platform, and the user has sufficent rights, increase |
| 741 | // the current process's scheduling priority to a high priority. |
rvargas@google.com | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 742 | BASE_API void RaiseProcessToHighPriority(); |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 743 | |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 744 | #if defined(OS_MACOSX) |
| 745 | // Restore the default exception handler, setting it to Apple Crash Reporter |
| 746 | // (ReportCrash). When forking and execing a new process, the child will |
| 747 | // inherit the parent's exception ports, which may be set to the Breakpad |
| 748 | // instance running inside the parent. The parent's Breakpad instance should |
| 749 | // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 750 | // in the child after forking will restore the standard exception handler. |
| 751 | // See http://crbug.com/20371/ for more details. |
| 752 | void RestoreDefaultExceptionHandler(); |
maruel@chromium.org | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 753 | #endif // defined(OS_MACOSX) |
mark@chromium.org | e9a8c19f | 2009-09-03 21:27:36 | [diff] [blame] | 754 | |
brettw@google.com | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 755 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 756 | |
deanm@google.com | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 757 | #endif // BASE_PROCESS_UTIL_H_ |