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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Win XP SP1 as well. | 163 // Win XP SP1 as well. |
164 BASE_API ProcessId GetProcId(ProcessHandle process); | 164 BASE_API ProcessId GetProcId(ProcessHandle process); |
165 | 165 |
166 #if defined(OS_LINUX) | 166 #if defined(OS_LINUX) |
167 // Returns the path to the executable of the given process. | 167 // Returns the path to the executable of the given process. |
168 FilePath GetProcessExecutablePath(ProcessHandle process); | 168 FilePath GetProcessExecutablePath(ProcessHandle process); |
169 | 169 |
170 // Parse the data found in /proc/<pid>/stat and return the sum of the | 170 // Parse the data found in /proc/<pid>/stat and return the sum of the |
171 // CPU-related ticks. Returns -1 on parse error. | 171 // CPU-related ticks. Returns -1 on parse error. |
172 // Exposed for testing. | 172 // Exposed for testing. |
173 int ParseProcStatCPU(const std::string& input); | 173 BASE_API int ParseProcStatCPU(const std::string& input); |
174 | 174 |
175 static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; | 175 static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; |
176 | 176 |
177 // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer | 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 | 178 // certain process types over others. The range for the adjustment is |
179 // [-17,15], with [0,15] being user accessible. | 179 // [-17,15], with [0,15] being user accessible. |
180 bool AdjustOOMScore(ProcessId process, int score); | 180 bool AdjustOOMScore(ProcessId process, int score); |
181 #endif | 181 #endif |
182 | 182 |
183 #if defined(OS_POSIX) | 183 #if defined(OS_POSIX) |
184 // Returns the ID for the parent of the given process. | 184 // Returns the ID for the parent of the given process. |
185 ProcessId GetParentProcessId(ProcessHandle process); | 185 BASE_API ProcessId GetParentProcessId(ProcessHandle process); |
186 | 186 |
187 // Close all file descriptors, except those which are a destination in the | 187 // 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 | 188 // given multimap. Only call this function in a child process where you know |
189 // that there aren't any other threads. | 189 // that there aren't any other threads. |
190 void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 190 void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
191 #endif | 191 #endif |
192 | 192 |
193 #if defined(OS_WIN) | 193 #if defined(OS_WIN) |
194 | 194 |
195 enum IntegrityLevel { | 195 enum IntegrityLevel { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // Before launching all FDs open in the parent process will be marked as | 257 // Before launching all FDs open in the parent process will be marked as |
258 // close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to | 258 // close-on-exec. |fds_to_remap| defines a mapping of src fd->dest fd to |
259 // propagate FDs into the child process. | 259 // propagate FDs into the child process. |
260 // | 260 // |
261 // As above, if wait is true, execute synchronously. The pid will be stored | 261 // As above, if wait is true, execute synchronously. The pid will be stored |
262 // in process_handle if that pointer is non-null. | 262 // in process_handle if that pointer is non-null. |
263 // | 263 // |
264 // Note that the first argument in argv must point to the executable filename. | 264 // Note that the first argument in argv must point to the executable filename. |
265 // If the filename is not fully specified, PATH will be searched. | 265 // If the filename is not fully specified, PATH will be searched. |
266 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; | 266 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
267 bool LaunchApp(const std::vector<std::string>& argv, | 267 BASE_API bool LaunchApp(const std::vector<std::string>& argv, |
268 const file_handle_mapping_vector& fds_to_remap, | 268 const file_handle_mapping_vector& fds_to_remap, |
269 bool wait, ProcessHandle* process_handle); | 269 bool wait, ProcessHandle* process_handle); |
270 | 270 |
271 // Similar to the above, but also (un)set environment variables in child process | 271 // Similar to the above, but also (un)set environment variables in child process |
272 // through |environ|. | 272 // through |environ|. |
273 typedef std::vector<std::pair<std::string, std::string> > environment_vector; | 273 typedef std::vector<std::pair<std::string, std::string> > environment_vector; |
274 bool LaunchApp(const std::vector<std::string>& argv, | 274 BASE_API bool LaunchApp(const std::vector<std::string>& argv, |
275 const environment_vector& environ, | 275 const environment_vector& environ, |
276 const file_handle_mapping_vector& fds_to_remap, | 276 const file_handle_mapping_vector& fds_to_remap, |
277 bool wait, ProcessHandle* process_handle); | 277 bool wait, ProcessHandle* process_handle); |
278 | 278 |
279 // Similar to the above two methods, but starts the child process in a process | 279 // Similar to the above two methods, but starts the child process in a process |
280 // group of its own, instead of allowing it to inherit the parent's process | 280 // group of its own, instead of allowing it to inherit the parent's process |
281 // group. The pgid of the child process will be the same as its pid. | 281 // group. The pgid of the child process will be the same as its pid. |
282 bool LaunchAppInNewProcessGroup(const std::vector<std::string>& argv, | 282 bool LaunchAppInNewProcessGroup(const std::vector<std::string>& argv, |
283 const environment_vector& environ, | 283 const environment_vector& environ, |
284 const file_handle_mapping_vector& fds_to_remap, | 284 const file_handle_mapping_vector& fds_to_remap, |
285 bool wait, ProcessHandle* process_handle); | 285 bool wait, ProcessHandle* process_handle); |
286 | 286 |
287 // AlterEnvironment returns a modified environment vector, constructed from the | 287 // AlterEnvironment returns a modified environment vector, constructed from the |
288 // given environment and the list of changes given in |changes|. Each key in | 288 // given environment and the list of changes given in |changes|. Each key in |
289 // the environment is matched against the first element of the pairs. In the | 289 // the environment is matched against the first element of the pairs. In the |
290 // event of a match, the value is replaced by the second of the pair, unless | 290 // event of a match, the value is replaced by the second of the pair, unless |
291 // the second is empty, in which case the key-value is removed. | 291 // the second is empty, in which case the key-value is removed. |
292 // | 292 // |
293 // The returned array is allocated using new[] and must be freed by the caller. | 293 // The returned array is allocated using new[] and must be freed by the caller. |
294 char** AlterEnvironment(const environment_vector& changes, | 294 BASE_API char** AlterEnvironment(const environment_vector& changes, |
295 const char* const* const env); | 295 const char* const* const env); |
296 #endif // defined(OS_POSIX) | 296 #endif // defined(OS_POSIX) |
297 | 297 |
298 // Executes the application specified by cl. This function delegates to one | 298 // Executes the application specified by cl. This function delegates to one |
299 // of the above two platform-specific functions. | 299 // of the above two platform-specific functions. |
300 BASE_API bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, | 300 BASE_API bool LaunchApp(const CommandLine& cl, bool wait, bool start_hidden, |
301 ProcessHandle* process_handle); | 301 ProcessHandle* process_handle); |
302 | 302 |
303 // Executes the application specified by |cl| and wait for it to exit. Stores | 303 // Executes the application specified by |cl| and wait for it to exit. Stores |
304 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true | 304 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
305 // on success (application launched and exited cleanly, with exit code | 305 // on success (application launched and exited cleanly, with exit code |
306 // indicating success). | 306 // indicating success). |
307 BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); | 307 BASE_API bool GetAppOutput(const CommandLine& cl, std::string* output); |
308 | 308 |
309 #if defined(OS_POSIX) | 309 #if defined(OS_POSIX) |
310 // A restricted version of |GetAppOutput()| which (a) clears the environment, | 310 // A restricted version of |GetAppOutput()| which (a) clears the environment, |
311 // and (b) stores at most |max_output| bytes; also, it doesn't search the path | 311 // and (b) stores at most |max_output| bytes; also, it doesn't search the path |
312 // for the command. | 312 // for the command. |
313 bool GetAppOutputRestricted(const CommandLine& cl, | 313 BASE_API bool GetAppOutputRestricted(const CommandLine& cl, |
314 std::string* output, size_t max_output); | 314 std::string* output, size_t max_output); |
315 #endif | 315 #endif |
316 | 316 |
317 // Used to filter processes by process ID. | 317 // Used to filter processes by process ID. |
318 class ProcessFilter { | 318 class ProcessFilter { |
319 public: | 319 public: |
320 // Returns true to indicate set-inclusion and false otherwise. This method | 320 // Returns true to indicate set-inclusion and false otherwise. This method |
321 // should not have side-effects and should be idempotent. | 321 // should not have side-effects and should be idempotent. |
322 virtual bool Includes(const ProcessEntry& entry) const = 0; | 322 virtual bool Includes(const ProcessEntry& entry) const = 0; |
323 | 323 |
324 protected: | 324 protected: |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 // Note: Returns true on Windows 2000 without doing anything. | 643 // Note: Returns true on Windows 2000 without doing anything. |
644 BASE_API bool EnableLowFragmentationHeap(); | 644 BASE_API bool EnableLowFragmentationHeap(); |
645 | 645 |
646 // Enables 'terminate on heap corruption' flag. Helps protect against heap | 646 // Enables 'terminate on heap corruption' flag. Helps protect against heap |
647 // overflow. Has no effect if the OS doesn't provide the necessary facility. | 647 // overflow. Has no effect if the OS doesn't provide the necessary facility. |
648 BASE_API void EnableTerminationOnHeapCorruption(); | 648 BASE_API void EnableTerminationOnHeapCorruption(); |
649 | 649 |
650 #if !defined(OS_WIN) | 650 #if !defined(OS_WIN) |
651 // Turns on process termination if memory runs out. This is handled on Windows | 651 // Turns on process termination if memory runs out. This is handled on Windows |
652 // inside RegisterInvalidParamHandler(). | 652 // inside RegisterInvalidParamHandler(). |
653 void EnableTerminationOnOutOfMemory(); | 653 BASE_API void EnableTerminationOnOutOfMemory(); |
654 #if defined(OS_MACOSX) | 654 #if defined(OS_MACOSX) |
655 // Exposed for testing. | 655 // Exposed for testing. |
656 malloc_zone_t* GetPurgeableZone(); | 656 malloc_zone_t* GetPurgeableZone(); |
657 #endif | 657 #endif |
658 #endif | 658 #endif |
659 | 659 |
660 // Enables stack dump to console output on exception and signals. | 660 // Enables stack dump to console output on exception and signals. |
661 // When enabled, the process will quit immediately. This is meant to be used in | 661 // When enabled, the process will quit immediately. This is meant to be used in |
662 // unit_tests only! | 662 // unit_tests only! |
663 BASE_API bool EnableInProcessStackDumping(); | 663 BASE_API bool EnableInProcessStackDumping(); |
664 | 664 |
665 // If supported on the platform, and the user has sufficent rights, increase | 665 // If supported on the platform, and the user has sufficent rights, increase |
666 // the current process's scheduling priority to a high priority. | 666 // the current process's scheduling priority to a high priority. |
667 BASE_API void RaiseProcessToHighPriority(); | 667 BASE_API void RaiseProcessToHighPriority(); |
668 | 668 |
669 #if defined(OS_MACOSX) | 669 #if defined(OS_MACOSX) |
670 // Restore the default exception handler, setting it to Apple Crash Reporter | 670 // Restore the default exception handler, setting it to Apple Crash Reporter |
671 // (ReportCrash). When forking and execing a new process, the child will | 671 // (ReportCrash). When forking and execing a new process, the child will |
672 // inherit the parent's exception ports, which may be set to the Breakpad | 672 // inherit the parent's exception ports, which may be set to the Breakpad |
673 // instance running inside the parent. The parent's Breakpad instance should | 673 // instance running inside the parent. The parent's Breakpad instance should |
674 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 674 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
675 // in the child after forking will restore the standard exception handler. | 675 // in the child after forking will restore the standard exception handler. |
676 // See http://crbug.com/20371/ for more details. | 676 // See http://crbug.com/20371/ for more details. |
677 void RestoreDefaultExceptionHandler(); | 677 void RestoreDefaultExceptionHandler(); |
678 #endif // defined(OS_MACOSX) | 678 #endif // defined(OS_MACOSX) |
679 | 679 |
680 } // namespace base | 680 } // namespace base |
681 | 681 |
682 #endif // BASE_PROCESS_UTIL_H_ | 682 #endif // BASE_PROCESS_UTIL_H_ |
OLD | NEW |