| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include <set> | 31 #include <set> |
| 32 #include <string> | 32 #include <string> |
| 33 #include <utility> | 33 #include <utility> |
| 34 #include <vector> | 34 #include <vector> |
| 35 | 35 |
| 36 #include "base/base_export.h" | 36 #include "base/base_export.h" |
| 37 #include "base/files/file_path.h" | 37 #include "base/files/file_path.h" |
| 38 #include "base/process.h" | 38 #include "base/process.h" |
| 39 #include "base/process/memory.h" | 39 #include "base/process/memory.h" |
| 40 #include "base/process/kill.h" | 40 #include "base/process/kill.h" |
| 41 #include "base/process/process_handle.h" |
| 41 #include "base/process/process_iterator.h" | 42 #include "base/process/process_iterator.h" |
| 42 #include "base/process/process_metrics.h" | 43 #include "base/process/process_metrics.h" |
| 43 | 44 |
| 44 #if defined(OS_POSIX) | 45 #if defined(OS_POSIX) |
| 45 #include "base/posix/file_descriptor_shuffle.h" | 46 #include "base/posix/file_descriptor_shuffle.h" |
| 46 #endif | 47 #endif |
| 47 | 48 |
| 48 class CommandLine; | 49 class CommandLine; |
| 49 | 50 |
| 50 namespace base { | 51 namespace base { |
| 51 | 52 |
| 52 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 53 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran | 54 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran |
| 54 // chrome. This is not thread-safe: only call from main thread. | 55 // chrome. This is not thread-safe: only call from main thread. |
| 55 BASE_EXPORT void RouteStdioToConsole(); | 56 BASE_EXPORT void RouteStdioToConsole(); |
| 56 #endif | 57 #endif |
| 57 | 58 |
| 58 // Returns the id of the current process. | |
| 59 BASE_EXPORT ProcessId GetCurrentProcId(); | |
| 60 | |
| 61 // Returns the ProcessHandle of the current process. | |
| 62 BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); | |
| 63 | |
| 64 // Converts a PID to a process handle. This handle must be closed by | |
| 65 // CloseProcessHandle when you are done with it. Returns true on success. | |
| 66 BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); | |
| 67 | |
| 68 // Converts a PID to a process handle. On Windows the handle is opened | |
| 69 // with more access rights and must only be used by trusted code. | |
| 70 // You have to close returned handle using CloseProcessHandle. Returns true | |
| 71 // on success. | |
| 72 // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the | |
| 73 // more specific OpenProcessHandleWithAccess method and delete this. | |
| 74 BASE_EXPORT bool OpenPrivilegedProcessHandle(ProcessId pid, | |
| 75 ProcessHandle* handle); | |
| 76 | |
| 77 // Converts a PID to a process handle using the desired access flags. Use a | |
| 78 // combination of the kProcessAccess* flags defined above for |access_flags|. | |
| 79 BASE_EXPORT bool OpenProcessHandleWithAccess(ProcessId pid, | |
| 80 uint32 access_flags, | |
| 81 ProcessHandle* handle); | |
| 82 | |
| 83 // Closes the process handle opened by OpenProcessHandle. | |
| 84 BASE_EXPORT void CloseProcessHandle(ProcessHandle process); | |
| 85 | |
| 86 // Returns the unique ID for the specified process. This is functionally the | |
| 87 // same as Windows' GetProcessId(), but works on versions of Windows before | |
| 88 // Win XP SP1 as well. | |
| 89 BASE_EXPORT ProcessId GetProcId(ProcessHandle process); | |
| 90 | |
| 91 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_BSD) | |
| 92 // Returns the path to the executable of the given process. | |
| 93 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); | |
| 94 #endif | |
| 95 | |
| 96 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 97 // Get the number of threads of |process| as available in /proc/<pid>/stat. | |
| 98 // This should be used with care as no synchronization with running threads is | |
| 99 // done. This is mostly useful to guarantee being single-threaded. | |
| 100 // Returns 0 on failure. | |
| 101 BASE_EXPORT int GetNumberOfThreads(ProcessHandle process); | |
| 102 | |
| 103 // /proc/self/exe refers to the current executable. | |
| 104 BASE_EXPORT extern const char kProcSelfExe[]; | |
| 105 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
| 106 | |
| 107 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
| 108 // Returns the ID for the parent of the given process. | |
| 109 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); | |
| 110 | |
| 111 // Returns the maximum number of file descriptors that can be open by a process | |
| 112 // at once. If the number is unavailable, a conservative best guess is returned. | |
| 113 size_t GetMaxFds(); | |
| 114 | |
| 115 // Close all file descriptors, except those which are a destination in the | 60 // Close all file descriptors, except those which are a destination in the |
| 116 // given multimap. Only call this function in a child process where you know | 61 // given multimap. Only call this function in a child process where you know |
| 117 // that there aren't any other threads. | 62 // that there aren't any other threads. |
| 118 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 63 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
| 119 #endif // defined(OS_POSIX) | 64 #endif // defined(OS_POSIX) |
| 120 | 65 |
| 121 typedef std::vector<std::pair<std::string, std::string> > EnvironmentVector; | 66 typedef std::vector<std::pair<std::string, std::string> > EnvironmentVector; |
| 122 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; | 67 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; |
| 123 | 68 |
| 124 // Options for launching a subprocess that are passed to LaunchProcess(). | 69 // Options for launching a subprocess that are passed to LaunchProcess(). |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 // stdin, stdout, and stderr. If not remapped by options::fds_to_remap, | 189 // stdin, stdout, and stderr. If not remapped by options::fds_to_remap, |
| 245 // stdin is reopened as /dev/null, and the child is allowed to inherit its | 190 // stdin is reopened as /dev/null, and the child is allowed to inherit its |
| 246 // parent's stdout and stderr. | 191 // parent's stdout and stderr. |
| 247 // - If the first argument on the command line does not contain a slash, | 192 // - If the first argument on the command line does not contain a slash, |
| 248 // PATH will be searched. (See man execvp.) | 193 // PATH will be searched. (See man execvp.) |
| 249 BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, | 194 BASE_EXPORT bool LaunchProcess(const CommandLine& cmdline, |
| 250 const LaunchOptions& options, | 195 const LaunchOptions& options, |
| 251 ProcessHandle* process_handle); | 196 ProcessHandle* process_handle); |
| 252 | 197 |
| 253 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
| 254 | |
| 255 enum IntegrityLevel { | |
| 256 INTEGRITY_UNKNOWN, | |
| 257 LOW_INTEGRITY, | |
| 258 MEDIUM_INTEGRITY, | |
| 259 HIGH_INTEGRITY, | |
| 260 }; | |
| 261 // Determine the integrity level of the specified process. Returns false | |
| 262 // if the system does not support integrity levels (pre-Vista) or in the case | |
| 263 // of an underlying system failure. | |
| 264 BASE_EXPORT bool GetProcessIntegrityLevel(ProcessHandle process, | |
| 265 IntegrityLevel* level); | |
| 266 | |
| 267 // Windows-specific LaunchProcess that takes the command line as a | 199 // Windows-specific LaunchProcess that takes the command line as a |
| 268 // string. Useful for situations where you need to control the | 200 // string. Useful for situations where you need to control the |
| 269 // command line arguments directly, but prefer the CommandLine version | 201 // command line arguments directly, but prefer the CommandLine version |
| 270 // if launching Chrome itself. | 202 // if launching Chrome itself. |
| 271 // | 203 // |
| 272 // The first command line argument should be the path to the process, | 204 // The first command line argument should be the path to the process, |
| 273 // and don't forget to quote it. | 205 // and don't forget to quote it. |
| 274 // | 206 // |
| 275 // Example (including literal quotes) | 207 // Example (including literal quotes) |
| 276 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" | 208 // cmdline = "c:\windows\explorer.exe" -foo "c:\bar\" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 // instance running inside the parent. The parent's Breakpad instance should | 277 // instance running inside the parent. The parent's Breakpad instance should |
| 346 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 278 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 347 // in the child after forking will restore the standard exception handler. | 279 // in the child after forking will restore the standard exception handler. |
| 348 // See http://crbug.com/20371/ for more details. | 280 // See http://crbug.com/20371/ for more details. |
| 349 void RestoreDefaultExceptionHandler(); | 281 void RestoreDefaultExceptionHandler(); |
| 350 #endif // defined(OS_MACOSX) | 282 #endif // defined(OS_MACOSX) |
| 351 | 283 |
| 352 } // namespace base | 284 } // namespace base |
| 353 | 285 |
| 354 #endif // BASE_PROCESS_UTIL_H_ | 286 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |