| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 | 5 |
| 6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
| 7 | 7 |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 #include <crt_externs.h> | 9 #include <crt_externs.h> |
| 10 #include <spawn.h> | 10 #include <spawn.h> |
| 11 #include <sys/sysctl.h> | 11 #include <sys/sysctl.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
| 14 | 14 |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/eintr_wrapper.h" | 17 #include "base/eintr_wrapper.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/time.h" | 20 #include "base/time.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 bool LaunchApp(const std::vector<std::string>& argv, | |
| 25 const file_handle_mapping_vector& fds_to_remap, | |
| 26 bool wait, ProcessHandle* process_handle) { | |
| 27 bool retval = true; | |
| 28 | |
| 29 char* argv_copy[argv.size() + 1]; | |
| 30 for (size_t i = 0; i < argv.size(); i++) { | |
| 31 argv_copy[i] = const_cast<char*>(argv[i].c_str()); | |
| 32 } | |
| 33 argv_copy[argv.size()] = NULL; | |
| 34 | |
| 35 // Make sure we don't leak any FDs to the child process by marking all FDs | |
| 36 // as close-on-exec. | |
| 37 SetAllFDsToCloseOnExec(); | |
| 38 | |
| 39 posix_spawn_file_actions_t file_actions; | |
| 40 if (posix_spawn_file_actions_init(&file_actions) != 0) { | |
| 41 return false; | |
| 42 } | |
| 43 | |
| 44 // Turn fds_to_remap array into a set of dup2 calls. | |
| 45 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); | |
| 46 it != fds_to_remap.end(); | |
| 47 ++it) { | |
| 48 int src_fd = it->first; | |
| 49 int dest_fd = it->second; | |
| 50 | |
| 51 if (src_fd == dest_fd) { | |
| 52 int flags = fcntl(src_fd, F_GETFD); | |
| 53 if (flags != -1) { | |
| 54 fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC); | |
| 55 } | |
| 56 } else { | |
| 57 if (posix_spawn_file_actions_adddup2(&file_actions, src_fd, dest_fd) != 0) | |
| 58 { | |
| 59 posix_spawn_file_actions_destroy(&file_actions); | |
| 60 return false; | |
| 61 } | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 int pid = 0; | |
| 66 int spawn_succeeded = (posix_spawnp(&pid, | |
| 67 argv_copy[0], | |
| 68 &file_actions, | |
| 69 NULL, | |
| 70 argv_copy, | |
| 71 *_NSGetEnviron()) == 0); | |
| 72 | |
| 73 posix_spawn_file_actions_destroy(&file_actions); | |
| 74 | |
| 75 bool process_handle_valid = pid > 0; | |
| 76 if (!spawn_succeeded || !process_handle_valid) { | |
| 77 retval = false; | |
| 78 } else { | |
| 79 if (wait) | |
| 80 HANDLE_EINTR(waitpid(pid, 0, 0)); | |
| 81 | |
| 82 if (process_handle) | |
| 83 *process_handle = pid; | |
| 84 } | |
| 85 | |
| 86 return retval; | |
| 87 } | |
| 88 | |
| 89 bool LaunchApp(const CommandLine& cl, | |
| 90 bool wait, bool start_hidden, ProcessHandle* process_handle) { | |
| 91 // TODO(playmobil): Do we need to respect the start_hidden flag? | |
| 92 file_handle_mapping_vector no_files; | |
| 93 return LaunchApp(cl.argv(), no_files, wait, process_handle); | |
| 94 } | |
| 95 | |
| 96 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, | 24 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, |
| 97 const ProcessFilter* filter) | 25 const ProcessFilter* filter) |
| 98 : executable_name_(executable_name), | 26 : executable_name_(executable_name), |
| 99 index_of_kinfo_proc_(0), | 27 index_of_kinfo_proc_(0), |
| 100 filter_(filter) { | 28 filter_(filter) { |
| 101 // Get a snapshot of all of my processes (yes, as we loop it can go stale, but | 29 // Get a snapshot of all of my processes (yes, as we loop it can go stale, but |
| 102 // but trying to find where we were in a constantly changing list is basically | 30 // but trying to find where we were in a constantly changing list is basically |
| 103 // impossible. | 31 // impossible. |
| 104 | 32 |
| 105 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, geteuid() }; | 33 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, geteuid() }; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 size_t ProcessMetrics::GetWorkingSetSize() const { | 176 size_t ProcessMetrics::GetWorkingSetSize() const { |
| 249 return 0; | 177 return 0; |
| 250 } | 178 } |
| 251 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | 179 size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
| 252 return 0; | 180 return 0; |
| 253 } | 181 } |
| 254 | 182 |
| 255 // ------------------------------------------------------------------------ | 183 // ------------------------------------------------------------------------ |
| 256 | 184 |
| 257 } // namespace base | 185 } // namespace base |
| OLD | NEW |