| 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/logging.h" | |
| 7 #include "base/process_util.h" | 6 #include "base/process_util.h" |
| 8 | 7 |
| 9 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 10 #include <crt_externs.h> | 9 #include <crt_externs.h> |
| 11 #include <spawn.h> | 10 #include <spawn.h> |
| 12 #include <string> | 11 #include <sys/sysctl.h> |
| 13 #include <sys/types.h> | 12 #include <sys/types.h> |
| 14 #include <sys/wait.h> | 13 #include <sys/wait.h> |
| 15 | 14 |
| 15 #include <string> |
| 16 |
| 17 #include "base/logging.h" |
| 18 #include "base/string_util.h" |
| 19 #include "base/time.h" |
| 20 |
| 16 namespace base { | 21 namespace base { |
| 17 | 22 |
| 18 bool LaunchApp(const std::vector<std::string>& argv, | 23 bool LaunchApp(const std::vector<std::string>& argv, |
| 19 const file_handle_mapping_vector& fds_to_remap, | 24 const file_handle_mapping_vector& fds_to_remap, |
| 20 bool wait, ProcessHandle* process_handle) { | 25 bool wait, ProcessHandle* process_handle) { |
| 21 bool retval = true; | 26 bool retval = true; |
| 22 | 27 |
| 23 char* argv_copy[argv.size() + 1]; | 28 char* argv_copy[argv.size() + 1]; |
| 24 for (size_t i = 0; i < argv.size(); i++) { | 29 for (size_t i = 0; i < argv.size(); i++) { |
| 25 argv_copy[i] = const_cast<char*>(argv[i].c_str()); | 30 argv_copy[i] = const_cast<char*>(argv[i].c_str()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 | 71 |
| 67 posix_spawn_file_actions_destroy(&file_actions); | 72 posix_spawn_file_actions_destroy(&file_actions); |
| 68 | 73 |
| 69 bool process_handle_valid = pid > 0; | 74 bool process_handle_valid = pid > 0; |
| 70 if (!spawn_succeeded || !process_handle_valid) { | 75 if (!spawn_succeeded || !process_handle_valid) { |
| 71 retval = false; | 76 retval = false; |
| 72 } else { | 77 } else { |
| 73 if (wait) | 78 if (wait) |
| 74 waitpid(pid, 0, 0); | 79 waitpid(pid, 0, 0); |
| 75 | 80 |
| 76 if(process_handle) | 81 if (process_handle) |
| 77 *process_handle = pid; | 82 *process_handle = pid; |
| 78 } | 83 } |
| 79 | 84 |
| 80 return retval; | 85 return retval; |
| 81 } | 86 } |
| 82 | 87 |
| 83 bool LaunchApp(const CommandLine& cl, | 88 bool LaunchApp(const CommandLine& cl, |
| 84 bool wait, bool start_hidden, ProcessHandle* process_handle) { | 89 bool wait, bool start_hidden, ProcessHandle* process_handle) { |
| 85 // TODO(playmobil): Do we need to respect the start_hidden flag? | 90 // TODO(playmobil): Do we need to respect the start_hidden flag? |
| 86 file_handle_mapping_vector no_files; | 91 file_handle_mapping_vector no_files; |
| 87 return LaunchApp(cl.argv(), no_files, wait, process_handle); | 92 return LaunchApp(cl.argv(), no_files, wait, process_handle); |
| 88 } | 93 } |
| 89 | 94 |
| 95 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, |
| 96 const ProcessFilter* filter) |
| 97 : executable_name_(executable_name), |
| 98 index_of_kinfo_proc_(0), |
| 99 filter_(filter) { |
| 100 } |
| 101 |
| 102 NamedProcessIterator::~NamedProcessIterator() { |
| 103 } |
| 104 |
| 105 const ProcessEntry* NamedProcessIterator::NextProcessEntry() { |
| 106 // Every call, you have to get new kinfo_procs_. |
| 107 // Because the process status might be changed. |
| 108 int num_of_kinfo_proc = 0; |
| 109 index_of_kinfo_proc_ = 0; |
| 110 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL }; |
| 111 size_t len = 0; |
| 112 |
| 113 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) |
| 114 return NULL; |
| 115 |
| 116 num_of_kinfo_proc = len / sizeof(struct kinfo_proc); |
| 117 // Leave some spare room for process table growth. |
| 118 num_of_kinfo_proc += 16; |
| 119 kinfo_procs_.resize(num_of_kinfo_proc); |
| 120 len = num_of_kinfo_proc * sizeof(struct kinfo_proc); |
| 121 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) |
| 122 return NULL; |
| 123 |
| 124 num_of_kinfo_proc = len / sizeof(struct kinfo_proc); |
| 125 kinfo_procs_.resize(num_of_kinfo_proc); |
| 126 |
| 127 bool result = false; |
| 128 do { |
| 129 result = CheckForNextProcess(); |
| 130 } while (result && !IncludeEntry()); |
| 131 |
| 132 if (result) |
| 133 return &entry_; |
| 134 |
| 135 return NULL; |
| 136 } |
| 137 |
| 138 bool NamedProcessIterator::CheckForNextProcess() { |
| 139 std::string exec_name; |
| 140 kinfo_proc* kinfo = NULL; |
| 141 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) { |
| 142 if (kinfo_procs_[index_of_kinfo_proc_].kp_proc.p_stat != SZOMB) { |
| 143 kinfo = &kinfo_procs_[index_of_kinfo_proc_]; |
| 144 |
| 145 int mib[] = { KERN_PROCARGS, KERN_PROCARGS, kinfo->kp_proc.p_pid }; |
| 146 |
| 147 size_t data_len = 0; |
| 148 if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) |
| 149 continue; |
| 150 |
| 151 std::string data; |
| 152 data.resize(data_len); |
| 153 if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) |
| 154 continue; |
| 155 |
| 156 // "data" has absolute process path with '/', |
| 157 // so we get the last part as execution process name. |
| 158 |
| 159 int exec_name_end = data.find('\0'); |
| 160 int last_slash = data.rfind('/', exec_name_end); |
| 161 |
| 162 // If the index is not -1, it means valid exec name is found. |
| 163 // Get the exec name and store the name into exec_name and break. |
| 164 // "last_slash" point is '/', so get substr from the next. |
| 165 if (last_slash != -1) { |
| 166 exec_name = data.substr(exec_name_end - last_slash - 1); |
| 167 } else { |
| 168 exec_name = data.substr(0, exec_name_end); |
| 169 } |
| 170 break; |
| 171 } |
| 172 } |
| 173 |
| 174 if (index_of_kinfo_proc_ >= kinfo_procs_.size()) |
| 175 return false; |
| 176 |
| 177 entry_.pid = kinfo->kp_proc.p_pid; |
| 178 entry_.ppid = kinfo->kp_proc.p_oppid; |
| 179 |
| 180 base::strlcpy(entry_.szExeFile, exec_name.c_str(), sizeof(entry_.szExeFile)); |
| 181 |
| 182 return true; |
| 183 } |
| 184 |
| 185 bool NamedProcessIterator::IncludeEntry() { |
| 186 if (WideToUTF8(executable_name_) != entry_.szExeFile) |
| 187 return false; |
| 188 if (!filter_) |
| 189 return true; |
| 190 return filter_->Includes(entry_.pid, entry_.ppid); |
| 191 } |
| 192 |
| 90 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) { | 193 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) { |
| 91 // TODO(pinkerton): can we implement this? On linux it relies on /proc. | 194 // TODO(pinkerton): can we implement this? On linux it relies on /proc. |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 int GetProcessCount(const std::wstring& executable_name, | |
| 96 const ProcessFilter* filter) { | |
| 97 NOTIMPLEMENTED(); | |
| 98 return 0; | |
| 99 } | |
| 100 | |
| 101 bool CleanupProcesses(const std::wstring& executable_name, | |
| 102 int wait_milliseconds, | |
| 103 int exit_code, | |
| 104 const ProcessFilter* filter) { | |
| 105 NOTIMPLEMENTED(); | 195 NOTIMPLEMENTED(); |
| 106 return false; | 196 return false; |
| 107 } | 197 } |
| 108 | 198 |
| 109 } // namespace base | 199 } // namespace base |
| OLD | NEW |