| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/namespace contains utility functions for enumerating, ending and | 5 // This file/namespace contains utility functions for enumerating, ending and |
| 6 // computing statistics of processes. | 6 // computing statistics of processes. |
| 7 | 7 |
| 8 #ifndef BASE_PROCESS_UTIL_H_ | 8 #ifndef BASE_PROCESS_UTIL_H_ |
| 9 #define BASE_PROCESS_UTIL_H_ | 9 #define BASE_PROCESS_UTIL_H_ |
| 10 #pragma once | 10 #pragma once |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include <dirent.h> | 26 #include <dirent.h> |
| 27 #include <limits.h> | 27 #include <limits.h> |
| 28 #include <sys/types.h> | 28 #include <sys/types.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #include <list> | 31 #include <list> |
| 32 #include <string> | 32 #include <string> |
| 33 #include <utility> | 33 #include <utility> |
| 34 #include <vector> | 34 #include <vector> |
| 35 | 35 |
| 36 #include "base/base_api.h" | 36 #include "base/base_export.h" |
| 37 #include "base/file_descriptor_shuffle.h" | 37 #include "base/file_descriptor_shuffle.h" |
| 38 #include "base/file_path.h" | 38 #include "base/file_path.h" |
| 39 #include "base/process.h" | 39 #include "base/process.h" |
| 40 | 40 |
| 41 class CommandLine; | 41 class CommandLine; |
| 42 | 42 |
| 43 namespace base { | 43 namespace base { |
| 44 | 44 |
| 45 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
| 46 struct ProcessEntry : public PROCESSENTRY32 { | 46 struct ProcessEntry : public PROCESSENTRY32 { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 enum TerminationStatus { | 125 enum TerminationStatus { |
| 126 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status | 126 TERMINATION_STATUS_NORMAL_TERMINATION, // zero exit status |
| 127 TERMINATION_STATUS_ABNORMAL_TERMINATION, // non-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 | 128 TERMINATION_STATUS_PROCESS_WAS_KILLED, // e.g. SIGKILL or task manager kill |
| 129 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault | 129 TERMINATION_STATUS_PROCESS_CRASHED, // e.g. Segmentation fault |
| 130 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet | 130 TERMINATION_STATUS_STILL_RUNNING, // child hasn't exited yet |
| 131 TERMINATION_STATUS_MAX_ENUM | 131 TERMINATION_STATUS_MAX_ENUM |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 // Returns the id of the current process. | 134 // Returns the id of the current process. |
| 135 BASE_API ProcessId GetCurrentProcId(); | 135 BASE_EXPORT ProcessId GetCurrentProcId(); |
| 136 | 136 |
| 137 // Returns the ProcessHandle of the current process. | 137 // Returns the ProcessHandle of the current process. |
| 138 BASE_API ProcessHandle GetCurrentProcessHandle(); | 138 BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
| 139 | 139 |
| 140 // Converts a PID to a process handle. This handle must be closed by | 140 // Converts a PID to a process handle. This handle must be closed by |
| 141 // CloseProcessHandle when you are done with it. Returns true on success. | 141 // CloseProcessHandle when you are done with it. Returns true on success. |
| 142 BASE_API bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); | 142 BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
| 143 | 143 |
| 144 // Converts a PID to a process handle. On Windows the handle is opened | 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. | 145 // with more access rights and must only be used by trusted code. |
| 146 // You have to close returned handle using CloseProcessHandle. Returns true | 146 // You have to close returned handle using CloseProcessHandle. Returns true |
| 147 // on success. | 147 // on success. |
| 148 // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the | 148 // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
| 149 // more specific OpenProcessHandleWithAccess method and delete this. | 149 // more specific OpenProcessHandleWithAccess method and delete this. |
| 150 BASE_API bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle); | 150 BASE_EXPORT bool OpenPrivilegedProcessHandle(ProcessId pid, |
| 151 ProcessHandle* handle); |
| 151 | 152 |
| 152 // Converts a PID to a process handle using the desired access flags. Use a | 153 // 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|. | 154 // combination of the kProcessAccess* flags defined above for |access_flags|. |
| 154 BASE_API bool OpenProcessHandleWithAccess(ProcessId pid, | 155 BASE_EXPORT bool OpenProcessHandleWithAccess(ProcessId pid, |
| 155 uint32 access_flags, | 156 uint32 access_flags, |
| 156 ProcessHandle* handle); | 157 ProcessHandle* handle); |
| 157 | 158 |
| 158 // Closes the process handle opened by OpenProcessHandle. | 159 // Closes the process handle opened by OpenProcessHandle. |
| 159 BASE_API void CloseProcessHandle(ProcessHandle process); | 160 BASE_EXPORT void CloseProcessHandle(ProcessHandle process); |
| 160 | 161 |
| 161 // Returns the unique ID for the specified process. This is functionally the | 162 // Returns the unique ID for the specified process. This is functionally the |
| 162 // same as Windows' GetProcessId(), but works on versions of Windows before | 163 // same as Windows' GetProcessId(), but works on versions of Windows before |
| 163 // Win XP SP1 as well. | 164 // Win XP SP1 as well. |
| 164 BASE_API ProcessId GetProcId(ProcessHandle process); | 165 BASE_EXPORT ProcessId GetProcId(ProcessHandle process); |
| 165 | 166 |
| 166 #if defined(OS_LINUX) | 167 #if defined(OS_LINUX) |
| 167 // Returns the path to the executable of the given process. | 168 // Returns the path to the executable of the given process. |
| 168 BASE_API FilePath GetProcessExecutablePath(ProcessHandle process); | 169 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
| 169 | 170 |
| 170 // Parse the data found in /proc/<pid>/stat and return the sum of the | 171 // Parse the data found in /proc/<pid>/stat and return the sum of the |
| 171 // CPU-related ticks. Returns -1 on parse error. | 172 // CPU-related ticks. Returns -1 on parse error. |
| 172 // Exposed for testing. | 173 // Exposed for testing. |
| 173 BASE_API int ParseProcStatCPU(const std::string& input); | 174 BASE_EXPORT int ParseProcStatCPU(const std::string& input); |
| 174 | 175 |
| 175 static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; | 176 static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; |
| 176 | 177 |
| 177 // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer | 178 // 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 // certain process types over others. The range for the adjustment is |
| 179 // [-17,15], with [0,15] being user accessible. | 180 // [-17,15], with [0,15] being user accessible. |
| 180 BASE_API bool AdjustOOMScore(ProcessId process, int score); | 181 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); |
| 181 #endif | 182 #endif |
| 182 | 183 |
| 183 #if defined(OS_POSIX) | 184 #if defined(OS_POSIX) |
| 184 // Returns the ID for the parent of the given process. | 185 // Returns the ID for the parent of the given process. |
| 185 BASE_API ProcessId GetParentProcessId(ProcessHandle process); | 186 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
| 186 | 187 |
| 187 // Close all file descriptors, except those which are a destination in the | 188 // Close all file descriptors, except those which are a destination in the |
| 188 // given multimap. Only call this function in a child process where you know | 189 // given multimap. Only call this function in a child process where you know |
| 189 // that there aren't any other threads. | 190 // that there aren't any other threads. |
| 190 BASE_API void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 191 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
| 191 #endif | 192 #endif |
| 192 | 193 |
| 193 // TODO(evan): rename these to use StudlyCaps. | 194 // 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<std::string, std::string> > environment_vector; |
| 195 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; | 196 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
| 196 | 197 |
| 197 // Options for launching a subprocess that are passed to LaunchProcess(). | 198 // Options for launching a subprocess that are passed to LaunchProcess(). |
| 198 // The default constructor constructs the object with default options. | 199 // The default constructor constructs the object with default options. |
| 199 struct LaunchOptions { | 200 struct LaunchOptions { |
| 200 LaunchOptions() : wait(false), | 201 LaunchOptions() : wait(false), |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 // If |process_handle| is non-NULL, it will be filled in with the | 257 // If |process_handle| is non-NULL, it will be filled in with the |
| 257 // handle of the launched process. NOTE: In this case, the caller is | 258 // handle of the launched process. NOTE: In this case, the caller is |
| 258 // responsible for closing the handle so that it doesn't leak! | 259 // responsible for closing the handle so that it doesn't leak! |
| 259 // Otherwise, the process handle will be implicitly closed. | 260 // Otherwise, the process handle will be implicitly closed. |
| 260 // | 261 // |
| 261 // Unix-specific notes: | 262 // Unix-specific notes: |
| 262 // - Before launching, all FDs open in the parent process will be marked as | 263 // - Before launching, all FDs open in the parent process will be marked as |
| 263 // close-on-exec. | 264 // close-on-exec. |
| 264 // - If the first argument on the command line does not contain a slash, | 265 // - If the first argument on the command line does not contain a slash, |
| 265 // PATH will be searched. (See man execvp.) | 266 // PATH will be searched. (See man execvp.) |
| 266 BASE_API bool LaunchProcess(const CommandLine& cmdline, | 267 BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, |
| 267 const LaunchOptions& options, | 268 const LaunchOptions& options, |
| 268 ProcessHandle* process_handle); | 269 ProcessHandle* process_handle); |
| 269 | 270 |
| 270 #if defined(OS_WIN) | 271 #if defined(OS_WIN) |
| 271 | 272 |
| 272 enum IntegrityLevel { | 273 enum IntegrityLevel { |
| 273 INTEGRITY_UNKNOWN, | 274 INTEGRITY_UNKNOWN, |
| 274 LOW_INTEGRITY, | 275 LOW_INTEGRITY, |
| 275 MEDIUM_INTEGRITY, | 276 MEDIUM_INTEGRITY, |
| 276 HIGH_INTEGRITY, | 277 HIGH_INTEGRITY, |
| 277 }; | 278 }; |
| 278 // Determine the integrity level of the specified process. Returns false | 279 // Determine the integrity level of the specified process. Returns false |
| 279 // if the system does not support integrity levels (pre-Vista) or in the case | 280 // if the system does not support integrity levels (pre-Vista) or in the case |
| 280 // of an underlying system failure. | 281 // of an underlying system failure. |
| 281 BASE_API bool GetProcessIntegrityLevel(ProcessHandle process, | 282 BASE_EXPORT bool GetProcessIntegrityLevel(ProcessHandle process, |
| 282 IntegrityLevel *level); | 283 IntegrityLevel *level); |
| 283 | 284 |
| 284 // Windows-specific LaunchProcess that takes the command line as a | 285 // Windows-specific LaunchProcess that takes the command line as a |
| 285 // string. Useful for situations where you need to control the | 286 // string. Useful for situations where you need to control the |
| 286 // command line arguments directly, but prefer the CommandLine version | 287 // command line arguments directly, but prefer the CommandLine version |
| 287 // if launching Chrome itself. | 288 // if launching Chrome itself. |
| 288 // | 289 // |
| 289 // The first command line argument should be the path to the process, | 290 // The first command line argument should be the path to the process, |
| 290 // and don't forget to quote it. | 291 // and don't forget to quote it. |
| 291 // | 292 // |
| 292 // Example (including literal quotes) | 293 // Example (including literal quotes) |
| 293 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" | 294 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| 294 BASE_API bool LaunchProcess(const string16& cmdline, | 295 BASE_EXPORT bool LaunchProcess(const string16& cmdline, |
| 295 const LaunchOptions& options, | 296 const LaunchOptions& options, |
| 296 ProcessHandle* process_handle); | 297 ProcessHandle* process_handle); |
| 297 | 298 |
| 298 #elif defined(OS_POSIX) | 299 #elif defined(OS_POSIX) |
| 299 // A POSIX-specific version of LaunchProcess that takes an argv array | 300 // A POSIX-specific version of LaunchProcess that takes an argv array |
| 300 // instead of a CommandLine. Useful for situations where you need to | 301 // instead of a CommandLine. Useful for situations where you need to |
| 301 // control the command line arguments directly, but prefer the | 302 // control the command line arguments directly, but prefer the |
| 302 // CommandLine version if launching Chrome itself. | 303 // CommandLine version if launching Chrome itself. |
| 303 BASE_API bool LaunchProcess(const std::vector<std::string>& argv, | 304 BASE_EXPORT bool LaunchProcess(const std::vector<std::string>& argv, |
| 304 const LaunchOptions& options, | 305 const LaunchOptions& options, |
| 305 ProcessHandle* process_handle); | 306 ProcessHandle* process_handle); |
| 306 | 307 |
| 307 // AlterEnvironment returns a modified environment vector, constructed from the | 308 // AlterEnvironment returns a modified environment vector, constructed from the |
| 308 // given environment and the list of changes given in |changes|. Each key in | 309 // given environment and the list of changes given in |changes|. Each key in |
| 309 // the environment is matched against the first element of the pairs. In the | 310 // the environment is matched against the first element of the pairs. In the |
| 310 // event of a match, the value is replaced by the second of the pair, unless | 311 // event of a match, the value is replaced by the second of the pair, unless |
| 311 // the second is empty, in which case the key-value is removed. | 312 // the second is empty, in which case the key-value is removed. |
| 312 // | 313 // |
| 313 // The returned array is allocated using new[] and must be freed by the caller. | 314 // The returned array is allocated using new[] and must be freed by the caller. |
| 314 BASE_API char** AlterEnvironment(const environment_vector& changes, | 315 BASE_EXPORT char** AlterEnvironment(const environment_vector& changes, |
| 315 const char* const* const env); | 316 const char* const* const env); |
| 316 #endif // defined(OS_POSIX) | 317 #endif // defined(OS_POSIX) |
| 317 | 318 |
| 318 // Executes the application specified by |cl| and wait for it to exit. Stores | 319 // Executes the application specified by |cl| and wait for it to exit. Stores |
| 319 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true | 320 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 320 // on success (application launched and exited cleanly, with exit code | 321 // on success (application launched and exited cleanly, with exit code |
| 321 // indicating success). | 322 // indicating success). |
| 322 BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); | 323 BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); |
| 323 | 324 |
| 324 #if defined(OS_POSIX) | 325 #if defined(OS_POSIX) |
| 325 // A restricted version of |GetAppOutput()| which (a) clears the environment, | 326 // A restricted version of |GetAppOutput()| which (a) clears the environment, |
| 326 // and (b) stores at most |max_output| bytes; also, it doesn't search the path | 327 // and (b) stores at most |max_output| bytes; also, it doesn't search the path |
| 327 // for the command. | 328 // for the command. |
| 328 BASE_API bool GetAppOutputRestricted(const CommandLine& cl, | 329 BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl, |
| 329 std::string* output, size_t max_output); | 330 std::string* output, size_t max_output); |
| 330 | 331 |
| 331 // A version of |GetAppOutput()| which also returns the exit code of the | 332 // A version of |GetAppOutput()| which also returns the exit code of the |
| 332 // executed command. Returns true if the application runs and exits cleanly. If | 333 // executed command. Returns true if the application runs and exits cleanly. If |
| 333 // this is the case the exit code of the application is available in | 334 // this is the case the exit code of the application is available in |
| 334 // |*exit_code|. | 335 // |*exit_code|. |
| 335 BASE_API bool GetAppOutputWithExitCode(const CommandLine& cl, | 336 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 336 std::string* output, int* exit_code); | 337 std::string* output, int* exit_code); |
| 337 #endif | 338 #endif |
| 338 | 339 |
| 339 // Used to filter processes by process ID. | 340 // Used to filter processes by process ID. |
| 340 class ProcessFilter { | 341 class ProcessFilter { |
| 341 public: | 342 public: |
| 342 // Returns true to indicate set-inclusion and false otherwise. This method | 343 // Returns true to indicate set-inclusion and false otherwise. This method |
| 343 // should not have side-effects and should be idempotent. | 344 // should not have side-effects and should be idempotent. |
| 344 virtual bool Includes(const ProcessEntry& entry) const = 0; | 345 virtual bool Includes(const ProcessEntry& entry) const = 0; |
| 345 | 346 |
| 346 protected: | 347 protected: |
| 347 virtual ~ProcessFilter() {} | 348 virtual ~ProcessFilter() {} |
| 348 }; | 349 }; |
| 349 | 350 |
| 350 // Returns the number of processes on the machine that are running from the | 351 // Returns the number of processes on the machine that are running from the |
| 351 // given executable name. If filter is non-null, then only processes selected | 352 // given executable name. If filter is non-null, then only processes selected |
| 352 // by the filter will be counted. | 353 // by the filter will be counted. |
| 353 BASE_API int GetProcessCount(const FilePath::StringType& executable_name, | 354 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, |
| 354 const ProcessFilter* filter); | 355 const ProcessFilter* filter); |
| 355 | 356 |
| 356 // Attempts to kill all the processes on the current machine that were launched | 357 // Attempts to kill all the processes on the current machine that were launched |
| 357 // from the given executable name, ending them with the given exit code. If | 358 // from the given executable name, ending them with the given exit code. If |
| 358 // filter is non-null, then only processes selected by the filter are killed. | 359 // filter is non-null, then only processes selected by the filter are killed. |
| 359 // Returns true if all processes were able to be killed off, false if at least | 360 // Returns true if all processes were able to be killed off, false if at least |
| 360 // one couldn't be killed. | 361 // one couldn't be killed. |
| 361 BASE_API bool KillProcesses(const FilePath::StringType& executable_name, | 362 BASE_EXPORT bool KillProcesses(const FilePath::StringType& executable_name, |
| 362 int exit_code, const ProcessFilter* filter); | 363 int exit_code, const ProcessFilter* filter); |
| 363 | 364 |
| 364 // Attempts to kill the process identified by the given process | 365 // Attempts to kill the process identified by the given process |
| 365 // entry structure, giving it the specified exit code. If |wait| is true, wait | 366 // entry structure, giving it the specified exit code. If |wait| is true, wait |
| 366 // for the process to be actually terminated before returning. | 367 // for the process to be actually terminated before returning. |
| 367 // Returns true if this is successful, false otherwise. | 368 // Returns true if this is successful, false otherwise. |
| 368 BASE_API bool KillProcess(ProcessHandle process, int exit_code, bool wait); | 369 BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
| 369 | 370 |
| 370 #if defined(OS_POSIX) | 371 #if defined(OS_POSIX) |
| 371 // Attempts to kill the process group identified by |process_group_id|. Returns | 372 // Attempts to kill the process group identified by |process_group_id|. Returns |
| 372 // true on success. | 373 // true on success. |
| 373 BASE_API bool KillProcessGroup(ProcessHandle process_group_id); | 374 BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); |
| 374 #endif | 375 #endif |
| 375 | 376 |
| 376 #if defined(OS_WIN) | 377 #if defined(OS_WIN) |
| 377 BASE_API bool KillProcessById(ProcessId process_id, int exit_code, bool wait); | 378 BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code, |
| 379 bool wait); |
| 378 #endif | 380 #endif |
| 379 | 381 |
| 380 // Get the termination status of the process by interpreting the | 382 // Get the termination status of the process by interpreting the |
| 381 // circumstances of the child process' death. |exit_code| is set to | 383 // circumstances of the child process' death. |exit_code| is set to |
| 382 // the status returned by waitpid() on POSIX, and from | 384 // the status returned by waitpid() on POSIX, and from |
| 383 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the | 385 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the |
| 384 // caller is not interested in it. Note that on Linux, this function | 386 // caller is not interested in it. Note that on Linux, this function |
| 385 // will only return a useful result the first time it is called after | 387 // will only return a useful result the first time it is called after |
| 386 // the child exits (because it will reap the child and the information | 388 // the child exits (because it will reap the child and the information |
| 387 // will no longer be available). | 389 // will no longer be available). |
| 388 BASE_API TerminationStatus GetTerminationStatus(ProcessHandle handle, | 390 BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, |
| 389 int* exit_code); | 391 int* exit_code); |
| 390 | 392 |
| 391 // Waits for process to exit. On POSIX systems, if the process hasn't been | 393 // Waits for process to exit. On POSIX systems, if the process hasn't been |
| 392 // signaled then puts the exit code in |exit_code|; otherwise it's considered | 394 // signaled then puts the exit code in |exit_code|; otherwise it's considered |
| 393 // a failure. On Windows |exit_code| is always filled. Returns true on success, | 395 // a failure. On Windows |exit_code| is always filled. Returns true on success, |
| 394 // and closes |handle| in any case. | 396 // and closes |handle| in any case. |
| 395 BASE_API bool WaitForExitCode(ProcessHandle handle, int* exit_code); | 397 BASE_EXPORT bool WaitForExitCode(ProcessHandle handle, int* exit_code); |
| 396 | 398 |
| 397 // Waits for process to exit. If it did exit within |timeout_milliseconds|, | 399 // Waits for process to exit. If it did exit within |timeout_milliseconds|, |
| 398 // then puts the exit code in |exit_code|, and returns true. | 400 // then puts the exit code in |exit_code|, and returns true. |
| 399 // In POSIX systems, if the process has been signaled then |exit_code| is set | 401 // In POSIX systems, if the process has been signaled then |exit_code| is set |
| 400 // to -1. Returns false on failure (the caller is then responsible for closing | 402 // to -1. Returns false on failure (the caller is then responsible for closing |
| 401 // |handle|). | 403 // |handle|). |
| 402 // The caller is always responsible for closing the |handle|. | 404 // The caller is always responsible for closing the |handle|. |
| 403 BASE_API bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code, | 405 BASE_EXPORT bool WaitForExitCodeWithTimeout(ProcessHandle handle, |
| 404 int64 timeout_milliseconds); | 406 int* exit_code, |
| 407 int64 timeout_milliseconds); |
| 405 | 408 |
| 406 // Wait for all the processes based on the named executable to exit. If filter | 409 // Wait for all the processes based on the named executable to exit. If filter |
| 407 // is non-null, then only processes selected by the filter are waited on. | 410 // is non-null, then only processes selected by the filter are waited on. |
| 408 // Returns after all processes have exited or wait_milliseconds have expired. | 411 // Returns after all processes have exited or wait_milliseconds have expired. |
| 409 // Returns true if all the processes exited, false otherwise. | 412 // Returns true if all the processes exited, false otherwise. |
| 410 BASE_API bool WaitForProcessesToExit( | 413 BASE_EXPORT bool WaitForProcessesToExit( |
| 411 const FilePath::StringType& executable_name, | 414 const FilePath::StringType& executable_name, |
| 412 int64 wait_milliseconds, | 415 int64 wait_milliseconds, |
| 413 const ProcessFilter* filter); | 416 const ProcessFilter* filter); |
| 414 | 417 |
| 415 // Wait for a single process to exit. Return true if it exited cleanly within | 418 // Wait for a single process to exit. Return true if it exited cleanly within |
| 416 // the given time limit. On Linux |handle| must be a child process, however | 419 // the given time limit. On Linux |handle| must be a child process, however |
| 417 // on Mac and Windows it can be any process. | 420 // on Mac and Windows it can be any process. |
| 418 BASE_API bool WaitForSingleProcess(ProcessHandle handle, | 421 BASE_EXPORT bool WaitForSingleProcess(ProcessHandle handle, |
| 419 int64 wait_milliseconds); | 422 int64 wait_milliseconds); |
| 420 | 423 |
| 421 // Waits a certain amount of time (can be 0) for all the processes with a given | 424 // Waits a certain amount of time (can be 0) for all the processes with a given |
| 422 // executable name to exit, then kills off any of them that are still around. | 425 // executable name to exit, then kills off any of them that are still around. |
| 423 // If filter is non-null, then only processes selected by the filter are waited | 426 // If filter is non-null, then only processes selected by the filter are waited |
| 424 // on. Killed processes are ended with the given exit code. Returns false if | 427 // on. Killed processes are ended with the given exit code. Returns false if |
| 425 // any processes needed to be killed, true if they all exited cleanly within | 428 // any processes needed to be killed, true if they all exited cleanly within |
| 426 // the wait_milliseconds delay. | 429 // the wait_milliseconds delay. |
| 427 BASE_API bool CleanupProcesses(const FilePath::StringType& executable_name, | 430 BASE_EXPORT bool CleanupProcesses(const FilePath::StringType& executable_name, |
| 428 int64 wait_milliseconds, | 431 int64 wait_milliseconds, |
| 429 int exit_code, | 432 int exit_code, |
| 430 const ProcessFilter* filter); | 433 const ProcessFilter* filter); |
| 431 | 434 |
| 432 // This class provides a way to iterate through a list of processes on the | 435 // This class provides a way to iterate through a list of processes on the |
| 433 // current machine with a specified filter. | 436 // current machine with a specified filter. |
| 434 // To use, create an instance and then call NextProcessEntry() until it returns | 437 // To use, create an instance and then call NextProcessEntry() until it returns |
| 435 // false. | 438 // false. |
| 436 class BASE_API ProcessIterator { | 439 class BASE_EXPORT ProcessIterator { |
| 437 public: | 440 public: |
| 438 typedef std::list<ProcessEntry> ProcessEntries; | 441 typedef std::list<ProcessEntry> ProcessEntries; |
| 439 | 442 |
| 440 explicit ProcessIterator(const ProcessFilter* filter); | 443 explicit ProcessIterator(const ProcessFilter* filter); |
| 441 virtual ~ProcessIterator(); | 444 virtual ~ProcessIterator(); |
| 442 | 445 |
| 443 // If there's another process that matches the given executable name, | 446 // If there's another process that matches the given executable name, |
| 444 // returns a const pointer to the corresponding PROCESSENTRY32. | 447 // returns a const pointer to the corresponding PROCESSENTRY32. |
| 445 // If there are no more matching processes, returns NULL. | 448 // If there are no more matching processes, returns NULL. |
| 446 // The returned pointer will remain valid until NextProcessEntry() | 449 // The returned pointer will remain valid until NextProcessEntry() |
| (...skipping 29 matching lines...) Expand all Loading... |
| 476 ProcessEntry entry_; | 479 ProcessEntry entry_; |
| 477 const ProcessFilter* filter_; | 480 const ProcessFilter* filter_; |
| 478 | 481 |
| 479 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); | 482 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
| 480 }; | 483 }; |
| 481 | 484 |
| 482 // This class provides a way to iterate through the list of processes | 485 // This class provides a way to iterate through the list of processes |
| 483 // on the current machine that were started from the given executable | 486 // on the current machine that were started from the given executable |
| 484 // name. To use, create an instance and then call NextProcessEntry() | 487 // name. To use, create an instance and then call NextProcessEntry() |
| 485 // until it returns false. | 488 // until it returns false. |
| 486 class BASE_API NamedProcessIterator : public ProcessIterator { | 489 class BASE_EXPORT NamedProcessIterator : public ProcessIterator { |
| 487 public: | 490 public: |
| 488 NamedProcessIterator(const FilePath::StringType& executable_name, | 491 NamedProcessIterator(const FilePath::StringType& executable_name, |
| 489 const ProcessFilter* filter); | 492 const ProcessFilter* filter); |
| 490 virtual ~NamedProcessIterator(); | 493 virtual ~NamedProcessIterator(); |
| 491 | 494 |
| 492 protected: | 495 protected: |
| 493 virtual bool IncludeEntry(); | 496 virtual bool IncludeEntry(); |
| 494 | 497 |
| 495 private: | 498 private: |
| 496 FilePath::StringType executable_name_; | 499 FilePath::StringType executable_name_; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 // largest : size of the largest contiguous amount of memory found. It is | 544 // largest : size of the largest contiguous amount of memory found. It is |
| 542 // always smaller or equal to FreeMBytes::total. | 545 // always smaller or equal to FreeMBytes::total. |
| 543 // largest_ptr: starting address of the largest memory block. | 546 // largest_ptr: starting address of the largest memory block. |
| 544 struct FreeMBytes { | 547 struct FreeMBytes { |
| 545 size_t total; | 548 size_t total; |
| 546 size_t largest; | 549 size_t largest; |
| 547 void* largest_ptr; | 550 void* largest_ptr; |
| 548 }; | 551 }; |
| 549 | 552 |
| 550 // Convert a POSIX timeval to microseconds. | 553 // Convert a POSIX timeval to microseconds. |
| 551 BASE_API int64 TimeValToMicroseconds(const struct timeval& tv); | 554 BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv); |
| 552 | 555 |
| 553 // Provides performance metrics for a specified process (CPU usage, memory and | 556 // Provides performance metrics for a specified process (CPU usage, memory and |
| 554 // IO counters). To use it, invoke CreateProcessMetrics() to get an instance | 557 // IO counters). To use it, invoke CreateProcessMetrics() to get an instance |
| 555 // for a specific process, then access the information with the different get | 558 // for a specific process, then access the information with the different get |
| 556 // methods. | 559 // methods. |
| 557 class BASE_API ProcessMetrics { | 560 class BASE_EXPORT ProcessMetrics { |
| 558 public: | 561 public: |
| 559 ~ProcessMetrics(); | 562 ~ProcessMetrics(); |
| 560 | 563 |
| 561 // Creates a ProcessMetrics for the specified process. | 564 // Creates a ProcessMetrics for the specified process. |
| 562 // The caller owns the returned object. | 565 // The caller owns the returned object. |
| 563 #if !defined(OS_MACOSX) | 566 #if !defined(OS_MACOSX) |
| 564 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); | 567 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); |
| 565 #else | 568 #else |
| 566 class PortProvider { | 569 class PortProvider { |
| 567 public: | 570 public: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 // Jiffie count at the last_time_ we updated. | 652 // Jiffie count at the last_time_ we updated. |
| 650 int last_cpu_; | 653 int last_cpu_; |
| 651 #endif // defined(OS_MACOSX) | 654 #endif // defined(OS_MACOSX) |
| 652 | 655 |
| 653 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); | 656 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
| 654 }; | 657 }; |
| 655 | 658 |
| 656 #if defined(OS_LINUX) | 659 #if defined(OS_LINUX) |
| 657 // Retrieves data from /proc/meminfo about system-wide memory consumption. | 660 // Retrieves data from /proc/meminfo about system-wide memory consumption. |
| 658 // Values are in KB. Returns true on success. | 661 // Values are in KB. Returns true on success. |
| 659 BASE_API bool GetSystemMemoryInfo(int* total_kb, int* free_kb, int* buffers_kb, | 662 BASE_EXPORT bool GetSystemMemoryInfo(int* total_kb, int* free_kb, |
| 660 int* cache_kb, int* shmem_kb); | 663 int* buffers_kb, int* cache_kb, |
| 664 int* shmem_kb); |
| 661 #endif | 665 #endif |
| 662 | 666 |
| 663 // Returns the memory committed by the system in KBytes. | 667 // Returns the memory committed by the system in KBytes. |
| 664 // Returns 0 if it can't compute the commit charge. | 668 // Returns 0 if it can't compute the commit charge. |
| 665 BASE_API size_t GetSystemCommitCharge(); | 669 BASE_EXPORT size_t GetSystemCommitCharge(); |
| 666 | 670 |
| 667 // Enables low fragmentation heap (LFH) for every heaps of this process. This | 671 // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 668 // won't have any effect on heaps created after this function call. It will not | 672 // won't have any effect on heaps created after this function call. It will not |
| 669 // modify data allocated in the heaps before calling this function. So it is | 673 // modify data allocated in the heaps before calling this function. So it is |
| 670 // better to call this function early in initialization and again before | 674 // better to call this function early in initialization and again before |
| 671 // entering the main loop. | 675 // entering the main loop. |
| 672 // Note: Returns true on Windows 2000 without doing anything. | 676 // Note: Returns true on Windows 2000 without doing anything. |
| 673 BASE_API bool EnableLowFragmentationHeap(); | 677 BASE_EXPORT bool EnableLowFragmentationHeap(); |
| 674 | 678 |
| 675 // Enables 'terminate on heap corruption' flag. Helps protect against heap | 679 // Enables 'terminate on heap corruption' flag. Helps protect against heap |
| 676 // overflow. Has no effect if the OS doesn't provide the necessary facility. | 680 // overflow. Has no effect if the OS doesn't provide the necessary facility. |
| 677 BASE_API void EnableTerminationOnHeapCorruption(); | 681 BASE_EXPORT void EnableTerminationOnHeapCorruption(); |
| 678 | 682 |
| 679 #if !defined(OS_WIN) | 683 #if !defined(OS_WIN) |
| 680 // Turns on process termination if memory runs out. This is handled on Windows | 684 // Turns on process termination if memory runs out. This is handled on Windows |
| 681 // inside RegisterInvalidParamHandler(). | 685 // inside RegisterInvalidParamHandler(). |
| 682 BASE_API void EnableTerminationOnOutOfMemory(); | 686 BASE_EXPORT void EnableTerminationOnOutOfMemory(); |
| 683 #if defined(OS_MACOSX) | 687 #if defined(OS_MACOSX) |
| 684 // Exposed for testing. | 688 // Exposed for testing. |
| 685 BASE_API malloc_zone_t* GetPurgeableZone(); | 689 BASE_EXPORT malloc_zone_t* GetPurgeableZone(); |
| 686 #endif | 690 #endif |
| 687 #endif | 691 #endif |
| 688 | 692 |
| 689 // Enables stack dump to console output on exception and signals. | 693 // Enables stack dump to console output on exception and signals. |
| 690 // When enabled, the process will quit immediately. This is meant to be used in | 694 // When enabled, the process will quit immediately. This is meant to be used in |
| 691 // unit_tests only! | 695 // unit_tests only! |
| 692 BASE_API bool EnableInProcessStackDumping(); | 696 BASE_EXPORT bool EnableInProcessStackDumping(); |
| 693 | 697 |
| 694 // If supported on the platform, and the user has sufficent rights, increase | 698 // If supported on the platform, and the user has sufficent rights, increase |
| 695 // the current process's scheduling priority to a high priority. | 699 // the current process's scheduling priority to a high priority. |
| 696 BASE_API void RaiseProcessToHighPriority(); | 700 BASE_EXPORT void RaiseProcessToHighPriority(); |
| 697 | 701 |
| 698 #if defined(OS_MACOSX) | 702 #if defined(OS_MACOSX) |
| 699 // Restore the default exception handler, setting it to Apple Crash Reporter | 703 // Restore the default exception handler, setting it to Apple Crash Reporter |
| 700 // (ReportCrash). When forking and execing a new process, the child will | 704 // (ReportCrash). When forking and execing a new process, the child will |
| 701 // inherit the parent's exception ports, which may be set to the Breakpad | 705 // inherit the parent's exception ports, which may be set to the Breakpad |
| 702 // instance running inside the parent. The parent's Breakpad instance should | 706 // instance running inside the parent. The parent's Breakpad instance should |
| 703 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 707 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 704 // in the child after forking will restore the standard exception handler. | 708 // in the child after forking will restore the standard exception handler. |
| 705 // See http://crbug.com/20371/ for more details. | 709 // See http://crbug.com/20371/ for more details. |
| 706 void RestoreDefaultExceptionHandler(); | 710 void RestoreDefaultExceptionHandler(); |
| 707 #endif // defined(OS_MACOSX) | 711 #endif // defined(OS_MACOSX) |
| 708 | 712 |
| 709 } // namespace base | 713 } // namespace base |
| 710 | 714 |
| 711 #endif // BASE_PROCESS_UTIL_H_ | 715 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |