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