ipc/chromium/src/base/process_util_mac.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5
michael@0 6 #include "base/process_util.h"
michael@0 7
michael@0 8 #import <Cocoa/Cocoa.h>
michael@0 9 #include <crt_externs.h>
michael@0 10 #include <spawn.h>
michael@0 11 #include <sys/sysctl.h>
michael@0 12 #include <sys/types.h>
michael@0 13 #include <sys/wait.h>
michael@0 14
michael@0 15 #include <string>
michael@0 16
michael@0 17 #include "base/eintr_wrapper.h"
michael@0 18 #include "base/logging.h"
michael@0 19 #include "base/rand_util.h"
michael@0 20 #include "base/string_util.h"
michael@0 21 #include "base/time.h"
michael@0 22
michael@0 23 namespace base {
michael@0 24
michael@0 25 void FreeEnvVarsArray(char* array[], int length)
michael@0 26 {
michael@0 27 for (int i = 0; i < length; i++) {
michael@0 28 free(array[i]);
michael@0 29 }
michael@0 30 delete[] array;
michael@0 31 }
michael@0 32
michael@0 33 bool LaunchApp(const std::vector<std::string>& argv,
michael@0 34 const file_handle_mapping_vector& fds_to_remap,
michael@0 35 bool wait, ProcessHandle* process_handle) {
michael@0 36 return LaunchApp(argv, fds_to_remap, environment_map(),
michael@0 37 wait, process_handle);
michael@0 38 }
michael@0 39
michael@0 40 bool LaunchApp(const std::vector<std::string>& argv,
michael@0 41 const file_handle_mapping_vector& fds_to_remap,
michael@0 42 const environment_map& env_vars_to_set,
michael@0 43 bool wait, ProcessHandle* process_handle,
michael@0 44 ProcessArchitecture arch) {
michael@0 45 return LaunchApp(argv, fds_to_remap, env_vars_to_set,
michael@0 46 PRIVILEGES_INHERIT,
michael@0 47 wait, process_handle);
michael@0 48 }
michael@0 49
michael@0 50 bool LaunchApp(const std::vector<std::string>& argv,
michael@0 51 const file_handle_mapping_vector& fds_to_remap,
michael@0 52 const environment_map& env_vars_to_set,
michael@0 53 ChildPrivileges privs,
michael@0 54 bool wait, ProcessHandle* process_handle,
michael@0 55 ProcessArchitecture arch) {
michael@0 56 bool retval = true;
michael@0 57
michael@0 58 char* argv_copy[argv.size() + 1];
michael@0 59 for (size_t i = 0; i < argv.size(); i++) {
michael@0 60 argv_copy[i] = const_cast<char*>(argv[i].c_str());
michael@0 61 }
michael@0 62 argv_copy[argv.size()] = NULL;
michael@0 63
michael@0 64 // Make sure we don't leak any FDs to the child process by marking all FDs
michael@0 65 // as close-on-exec.
michael@0 66 SetAllFDsToCloseOnExec();
michael@0 67
michael@0 68 // Copy _NSGetEnviron() to a new char array and add the variables
michael@0 69 // in env_vars_to_set.
michael@0 70 // Existing variables are overwritten by env_vars_to_set.
michael@0 71 int pos = 0;
michael@0 72 environment_map combined_env_vars = env_vars_to_set;
michael@0 73 while((*_NSGetEnviron())[pos] != NULL) {
michael@0 74 std::string varString = (*_NSGetEnviron())[pos];
michael@0 75 std::string varName = varString.substr(0, varString.find_first_of('='));
michael@0 76 std::string varValue = varString.substr(varString.find_first_of('=') + 1);
michael@0 77 if (combined_env_vars.find(varName) == combined_env_vars.end()) {
michael@0 78 combined_env_vars[varName] = varValue;
michael@0 79 }
michael@0 80 pos++;
michael@0 81 }
michael@0 82 int varsLen = combined_env_vars.size() + 1;
michael@0 83
michael@0 84 char** vars = new char*[varsLen];
michael@0 85 int i = 0;
michael@0 86 for (environment_map::const_iterator it = combined_env_vars.begin();
michael@0 87 it != combined_env_vars.end(); ++it) {
michael@0 88 std::string entry(it->first);
michael@0 89 entry += "=";
michael@0 90 entry += it->second;
michael@0 91 vars[i] = strdup(entry.c_str());
michael@0 92 i++;
michael@0 93 }
michael@0 94 vars[i] = NULL;
michael@0 95
michael@0 96 posix_spawn_file_actions_t file_actions;
michael@0 97 if (posix_spawn_file_actions_init(&file_actions) != 0) {
michael@0 98 FreeEnvVarsArray(vars, varsLen);
michael@0 99 return false;
michael@0 100 }
michael@0 101
michael@0 102 // Turn fds_to_remap array into a set of dup2 calls.
michael@0 103 for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin();
michael@0 104 it != fds_to_remap.end();
michael@0 105 ++it) {
michael@0 106 int src_fd = it->first;
michael@0 107 int dest_fd = it->second;
michael@0 108
michael@0 109 if (src_fd == dest_fd) {
michael@0 110 int flags = fcntl(src_fd, F_GETFD);
michael@0 111 if (flags != -1) {
michael@0 112 fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC);
michael@0 113 }
michael@0 114 } else {
michael@0 115 if (posix_spawn_file_actions_adddup2(&file_actions, src_fd, dest_fd) != 0) {
michael@0 116 posix_spawn_file_actions_destroy(&file_actions);
michael@0 117 FreeEnvVarsArray(vars, varsLen);
michael@0 118 return false;
michael@0 119 }
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 // Set up the CPU preference array.
michael@0 124 cpu_type_t cpu_types[1];
michael@0 125 switch (arch) {
michael@0 126 case PROCESS_ARCH_I386:
michael@0 127 cpu_types[0] = CPU_TYPE_X86;
michael@0 128 break;
michael@0 129 case PROCESS_ARCH_X86_64:
michael@0 130 cpu_types[0] = CPU_TYPE_X86_64;
michael@0 131 break;
michael@0 132 case PROCESS_ARCH_PPC:
michael@0 133 cpu_types[0] = CPU_TYPE_POWERPC;
michael@0 134 default:
michael@0 135 cpu_types[0] = CPU_TYPE_ANY;
michael@0 136 break;
michael@0 137 }
michael@0 138
michael@0 139 // Initialize spawn attributes.
michael@0 140 posix_spawnattr_t spawnattr;
michael@0 141 if (posix_spawnattr_init(&spawnattr) != 0) {
michael@0 142 FreeEnvVarsArray(vars, varsLen);
michael@0 143 return false;
michael@0 144 }
michael@0 145
michael@0 146 // Set spawn attributes.
michael@0 147 size_t attr_count = 1;
michael@0 148 size_t attr_ocount = 0;
michael@0 149 if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, cpu_types, &attr_ocount) != 0 ||
michael@0 150 attr_ocount != attr_count) {
michael@0 151 FreeEnvVarsArray(vars, varsLen);
michael@0 152 posix_spawnattr_destroy(&spawnattr);
michael@0 153 return false;
michael@0 154 }
michael@0 155
michael@0 156 int pid = 0;
michael@0 157 int spawn_succeeded = (posix_spawnp(&pid,
michael@0 158 argv_copy[0],
michael@0 159 &file_actions,
michael@0 160 &spawnattr,
michael@0 161 argv_copy,
michael@0 162 vars) == 0);
michael@0 163
michael@0 164 FreeEnvVarsArray(vars, varsLen);
michael@0 165
michael@0 166 posix_spawn_file_actions_destroy(&file_actions);
michael@0 167
michael@0 168 posix_spawnattr_destroy(&spawnattr);
michael@0 169
michael@0 170 bool process_handle_valid = pid > 0;
michael@0 171 if (!spawn_succeeded || !process_handle_valid) {
michael@0 172 retval = false;
michael@0 173 } else {
michael@0 174 if (wait)
michael@0 175 HANDLE_EINTR(waitpid(pid, 0, 0));
michael@0 176
michael@0 177 if (process_handle)
michael@0 178 *process_handle = pid;
michael@0 179 }
michael@0 180
michael@0 181 return retval;
michael@0 182 }
michael@0 183
michael@0 184 bool LaunchApp(const CommandLine& cl,
michael@0 185 bool wait, bool start_hidden, ProcessHandle* process_handle) {
michael@0 186 // TODO(playmobil): Do we need to respect the start_hidden flag?
michael@0 187 file_handle_mapping_vector no_files;
michael@0 188 return LaunchApp(cl.argv(), no_files, wait, process_handle);
michael@0 189 }
michael@0 190
michael@0 191 void SetCurrentProcessPrivileges(ChildPrivileges privs) {
michael@0 192
michael@0 193 }
michael@0 194
michael@0 195 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
michael@0 196 const ProcessFilter* filter)
michael@0 197 : executable_name_(executable_name),
michael@0 198 index_of_kinfo_proc_(0),
michael@0 199 filter_(filter) {
michael@0 200 // Get a snapshot of all of my processes (yes, as we loop it can go stale, but
michael@0 201 // but trying to find where we were in a constantly changing list is basically
michael@0 202 // impossible.
michael@0 203
michael@0 204 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, int(geteuid()) };
michael@0 205
michael@0 206 // Since more processes could start between when we get the size and when
michael@0 207 // we get the list, we do a loop to keep trying until we get it.
michael@0 208 bool done = false;
michael@0 209 int try_num = 1;
michael@0 210 const int max_tries = 10;
michael@0 211 do {
michael@0 212 // Get the size of the buffer
michael@0 213 size_t len = 0;
michael@0 214 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) {
michael@0 215 CHROMIUM_LOG(ERROR) << "failed to get the size needed for the process list";
michael@0 216 kinfo_procs_.resize(0);
michael@0 217 done = true;
michael@0 218 } else {
michael@0 219 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
michael@0 220 // Leave some spare room for process table growth (more could show up
michael@0 221 // between when we check and now)
michael@0 222 num_of_kinfo_proc += 4;
michael@0 223 kinfo_procs_.resize(num_of_kinfo_proc);
michael@0 224 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
michael@0 225 // Load the list of processes
michael@0 226 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
michael@0 227 // If we get a mem error, it just means we need a bigger buffer, so
michael@0 228 // loop around again. Anything else is a real error and give up.
michael@0 229 if (errno != ENOMEM) {
michael@0 230 CHROMIUM_LOG(ERROR) << "failed to get the process list";
michael@0 231 kinfo_procs_.resize(0);
michael@0 232 done = true;
michael@0 233 }
michael@0 234 } else {
michael@0 235 // Got the list, just make sure we're sized exactly right
michael@0 236 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
michael@0 237 kinfo_procs_.resize(num_of_kinfo_proc);
michael@0 238 done = true;
michael@0 239 }
michael@0 240 }
michael@0 241 } while (!done && (try_num++ < max_tries));
michael@0 242
michael@0 243 if (!done) {
michael@0 244 CHROMIUM_LOG(ERROR) << "failed to collect the process list in a few tries";
michael@0 245 kinfo_procs_.resize(0);
michael@0 246 }
michael@0 247 }
michael@0 248
michael@0 249 NamedProcessIterator::~NamedProcessIterator() {
michael@0 250 }
michael@0 251
michael@0 252 const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
michael@0 253 bool result = false;
michael@0 254 do {
michael@0 255 result = CheckForNextProcess();
michael@0 256 } while (result && !IncludeEntry());
michael@0 257
michael@0 258 if (result) {
michael@0 259 return &entry_;
michael@0 260 }
michael@0 261
michael@0 262 return NULL;
michael@0 263 }
michael@0 264
michael@0 265 bool NamedProcessIterator::CheckForNextProcess() {
michael@0 266 std::string executable_name_utf8(WideToUTF8(executable_name_));
michael@0 267
michael@0 268 std::string data;
michael@0 269 std::string exec_name;
michael@0 270
michael@0 271 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
michael@0 272 kinfo_proc* kinfo = &kinfo_procs_[index_of_kinfo_proc_];
michael@0 273
michael@0 274 // Skip processes just awaiting collection
michael@0 275 if ((kinfo->kp_proc.p_pid > 0) && (kinfo->kp_proc.p_stat == SZOMB))
michael@0 276 continue;
michael@0 277
michael@0 278 int mib[] = { CTL_KERN, KERN_PROCARGS, kinfo->kp_proc.p_pid };
michael@0 279
michael@0 280 // Found out what size buffer we need
michael@0 281 size_t data_len = 0;
michael@0 282 if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) {
michael@0 283 CHROMIUM_LOG(ERROR) << "failed to figure out the buffer size for a commandline";
michael@0 284 continue;
michael@0 285 }
michael@0 286
michael@0 287 data.resize(data_len);
michael@0 288 if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) {
michael@0 289 CHROMIUM_LOG(ERROR) << "failed to fetch a commandline";
michael@0 290 continue;
michael@0 291 }
michael@0 292
michael@0 293 // Data starts w/ the full path null termed, so we have to extract just the
michael@0 294 // executable name from the path.
michael@0 295
michael@0 296 size_t exec_name_end = data.find('\0');
michael@0 297 if (exec_name_end == std::string::npos) {
michael@0 298 CHROMIUM_LOG(ERROR) << "command line data didn't match expected format";
michael@0 299 continue;
michael@0 300 }
michael@0 301 size_t last_slash = data.rfind('/', exec_name_end);
michael@0 302 if (last_slash == std::string::npos)
michael@0 303 exec_name = data.substr(0, exec_name_end);
michael@0 304 else
michael@0 305 exec_name = data.substr(last_slash + 1, exec_name_end - last_slash - 1);
michael@0 306
michael@0 307 // Check the name
michael@0 308 if (executable_name_utf8 == exec_name) {
michael@0 309 entry_.pid = kinfo->kp_proc.p_pid;
michael@0 310 entry_.ppid = kinfo->kp_eproc.e_ppid;
michael@0 311 base::strlcpy(entry_.szExeFile, exec_name.c_str(),
michael@0 312 sizeof(entry_.szExeFile));
michael@0 313 // Start w/ the next entry next time through
michael@0 314 ++index_of_kinfo_proc_;
michael@0 315 // Done
michael@0 316 return true;
michael@0 317 }
michael@0 318 }
michael@0 319 return false;
michael@0 320 }
michael@0 321
michael@0 322 bool NamedProcessIterator::IncludeEntry() {
michael@0 323 // Don't need to check the name, we did that w/in CheckForNextProcess.
michael@0 324 if (!filter_)
michael@0 325 return true;
michael@0 326 return filter_->Includes(entry_.pid, entry_.ppid);
michael@0 327 }
michael@0 328
michael@0 329 } // namespace base

mercurial