michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: michael@0: #include "base/process_util.h" michael@0: michael@0: #import michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "base/eintr_wrapper.h" michael@0: #include "base/logging.h" michael@0: #include "base/rand_util.h" michael@0: #include "base/string_util.h" michael@0: #include "base/time.h" michael@0: michael@0: namespace base { michael@0: michael@0: void FreeEnvVarsArray(char* array[], int length) michael@0: { michael@0: for (int i = 0; i < length; i++) { michael@0: free(array[i]); michael@0: } michael@0: delete[] array; michael@0: } michael@0: michael@0: bool LaunchApp(const std::vector& argv, michael@0: const file_handle_mapping_vector& fds_to_remap, michael@0: bool wait, ProcessHandle* process_handle) { michael@0: return LaunchApp(argv, fds_to_remap, environment_map(), michael@0: wait, process_handle); michael@0: } michael@0: michael@0: bool LaunchApp(const std::vector& argv, michael@0: const file_handle_mapping_vector& fds_to_remap, michael@0: const environment_map& env_vars_to_set, michael@0: bool wait, ProcessHandle* process_handle, michael@0: ProcessArchitecture arch) { michael@0: return LaunchApp(argv, fds_to_remap, env_vars_to_set, michael@0: PRIVILEGES_INHERIT, michael@0: wait, process_handle); michael@0: } michael@0: michael@0: bool LaunchApp(const std::vector& argv, michael@0: const file_handle_mapping_vector& fds_to_remap, michael@0: const environment_map& env_vars_to_set, michael@0: ChildPrivileges privs, michael@0: bool wait, ProcessHandle* process_handle, michael@0: ProcessArchitecture arch) { michael@0: bool retval = true; michael@0: michael@0: char* argv_copy[argv.size() + 1]; michael@0: for (size_t i = 0; i < argv.size(); i++) { michael@0: argv_copy[i] = const_cast(argv[i].c_str()); michael@0: } michael@0: argv_copy[argv.size()] = NULL; michael@0: michael@0: // Make sure we don't leak any FDs to the child process by marking all FDs michael@0: // as close-on-exec. michael@0: SetAllFDsToCloseOnExec(); michael@0: michael@0: // Copy _NSGetEnviron() to a new char array and add the variables michael@0: // in env_vars_to_set. michael@0: // Existing variables are overwritten by env_vars_to_set. michael@0: int pos = 0; michael@0: environment_map combined_env_vars = env_vars_to_set; michael@0: while((*_NSGetEnviron())[pos] != NULL) { michael@0: std::string varString = (*_NSGetEnviron())[pos]; michael@0: std::string varName = varString.substr(0, varString.find_first_of('=')); michael@0: std::string varValue = varString.substr(varString.find_first_of('=') + 1); michael@0: if (combined_env_vars.find(varName) == combined_env_vars.end()) { michael@0: combined_env_vars[varName] = varValue; michael@0: } michael@0: pos++; michael@0: } michael@0: int varsLen = combined_env_vars.size() + 1; michael@0: michael@0: char** vars = new char*[varsLen]; michael@0: int i = 0; michael@0: for (environment_map::const_iterator it = combined_env_vars.begin(); michael@0: it != combined_env_vars.end(); ++it) { michael@0: std::string entry(it->first); michael@0: entry += "="; michael@0: entry += it->second; michael@0: vars[i] = strdup(entry.c_str()); michael@0: i++; michael@0: } michael@0: vars[i] = NULL; michael@0: michael@0: posix_spawn_file_actions_t file_actions; michael@0: if (posix_spawn_file_actions_init(&file_actions) != 0) { michael@0: FreeEnvVarsArray(vars, varsLen); michael@0: return false; michael@0: } michael@0: michael@0: // Turn fds_to_remap array into a set of dup2 calls. michael@0: for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); michael@0: it != fds_to_remap.end(); michael@0: ++it) { michael@0: int src_fd = it->first; michael@0: int dest_fd = it->second; michael@0: michael@0: if (src_fd == dest_fd) { michael@0: int flags = fcntl(src_fd, F_GETFD); michael@0: if (flags != -1) { michael@0: fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC); michael@0: } michael@0: } else { michael@0: if (posix_spawn_file_actions_adddup2(&file_actions, src_fd, dest_fd) != 0) { michael@0: posix_spawn_file_actions_destroy(&file_actions); michael@0: FreeEnvVarsArray(vars, varsLen); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Set up the CPU preference array. michael@0: cpu_type_t cpu_types[1]; michael@0: switch (arch) { michael@0: case PROCESS_ARCH_I386: michael@0: cpu_types[0] = CPU_TYPE_X86; michael@0: break; michael@0: case PROCESS_ARCH_X86_64: michael@0: cpu_types[0] = CPU_TYPE_X86_64; michael@0: break; michael@0: case PROCESS_ARCH_PPC: michael@0: cpu_types[0] = CPU_TYPE_POWERPC; michael@0: default: michael@0: cpu_types[0] = CPU_TYPE_ANY; michael@0: break; michael@0: } michael@0: michael@0: // Initialize spawn attributes. michael@0: posix_spawnattr_t spawnattr; michael@0: if (posix_spawnattr_init(&spawnattr) != 0) { michael@0: FreeEnvVarsArray(vars, varsLen); michael@0: return false; michael@0: } michael@0: michael@0: // Set spawn attributes. michael@0: size_t attr_count = 1; michael@0: size_t attr_ocount = 0; michael@0: if (posix_spawnattr_setbinpref_np(&spawnattr, attr_count, cpu_types, &attr_ocount) != 0 || michael@0: attr_ocount != attr_count) { michael@0: FreeEnvVarsArray(vars, varsLen); michael@0: posix_spawnattr_destroy(&spawnattr); michael@0: return false; michael@0: } michael@0: michael@0: int pid = 0; michael@0: int spawn_succeeded = (posix_spawnp(&pid, michael@0: argv_copy[0], michael@0: &file_actions, michael@0: &spawnattr, michael@0: argv_copy, michael@0: vars) == 0); michael@0: michael@0: FreeEnvVarsArray(vars, varsLen); michael@0: michael@0: posix_spawn_file_actions_destroy(&file_actions); michael@0: michael@0: posix_spawnattr_destroy(&spawnattr); michael@0: michael@0: bool process_handle_valid = pid > 0; michael@0: if (!spawn_succeeded || !process_handle_valid) { michael@0: retval = false; michael@0: } else { michael@0: if (wait) michael@0: HANDLE_EINTR(waitpid(pid, 0, 0)); michael@0: michael@0: if (process_handle) michael@0: *process_handle = pid; michael@0: } michael@0: michael@0: return retval; michael@0: } michael@0: michael@0: bool LaunchApp(const CommandLine& cl, michael@0: bool wait, bool start_hidden, ProcessHandle* process_handle) { michael@0: // TODO(playmobil): Do we need to respect the start_hidden flag? michael@0: file_handle_mapping_vector no_files; michael@0: return LaunchApp(cl.argv(), no_files, wait, process_handle); michael@0: } michael@0: michael@0: void SetCurrentProcessPrivileges(ChildPrivileges privs) { michael@0: michael@0: } michael@0: michael@0: NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, michael@0: const ProcessFilter* filter) michael@0: : executable_name_(executable_name), michael@0: index_of_kinfo_proc_(0), michael@0: filter_(filter) { michael@0: // Get a snapshot of all of my processes (yes, as we loop it can go stale, but michael@0: // but trying to find where we were in a constantly changing list is basically michael@0: // impossible. michael@0: michael@0: int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, int(geteuid()) }; michael@0: michael@0: // Since more processes could start between when we get the size and when michael@0: // we get the list, we do a loop to keep trying until we get it. michael@0: bool done = false; michael@0: int try_num = 1; michael@0: const int max_tries = 10; michael@0: do { michael@0: // Get the size of the buffer michael@0: size_t len = 0; michael@0: if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) { michael@0: CHROMIUM_LOG(ERROR) << "failed to get the size needed for the process list"; michael@0: kinfo_procs_.resize(0); michael@0: done = true; michael@0: } else { michael@0: size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); michael@0: // Leave some spare room for process table growth (more could show up michael@0: // between when we check and now) michael@0: num_of_kinfo_proc += 4; michael@0: kinfo_procs_.resize(num_of_kinfo_proc); michael@0: len = num_of_kinfo_proc * sizeof(struct kinfo_proc); michael@0: // Load the list of processes michael@0: if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) { michael@0: // If we get a mem error, it just means we need a bigger buffer, so michael@0: // loop around again. Anything else is a real error and give up. michael@0: if (errno != ENOMEM) { michael@0: CHROMIUM_LOG(ERROR) << "failed to get the process list"; michael@0: kinfo_procs_.resize(0); michael@0: done = true; michael@0: } michael@0: } else { michael@0: // Got the list, just make sure we're sized exactly right michael@0: size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); michael@0: kinfo_procs_.resize(num_of_kinfo_proc); michael@0: done = true; michael@0: } michael@0: } michael@0: } while (!done && (try_num++ < max_tries)); michael@0: michael@0: if (!done) { michael@0: CHROMIUM_LOG(ERROR) << "failed to collect the process list in a few tries"; michael@0: kinfo_procs_.resize(0); michael@0: } michael@0: } michael@0: michael@0: NamedProcessIterator::~NamedProcessIterator() { michael@0: } michael@0: michael@0: const ProcessEntry* NamedProcessIterator::NextProcessEntry() { michael@0: bool result = false; michael@0: do { michael@0: result = CheckForNextProcess(); michael@0: } while (result && !IncludeEntry()); michael@0: michael@0: if (result) { michael@0: return &entry_; michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: bool NamedProcessIterator::CheckForNextProcess() { michael@0: std::string executable_name_utf8(WideToUTF8(executable_name_)); michael@0: michael@0: std::string data; michael@0: std::string exec_name; michael@0: michael@0: for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) { michael@0: kinfo_proc* kinfo = &kinfo_procs_[index_of_kinfo_proc_]; michael@0: michael@0: // Skip processes just awaiting collection michael@0: if ((kinfo->kp_proc.p_pid > 0) && (kinfo->kp_proc.p_stat == SZOMB)) michael@0: continue; michael@0: michael@0: int mib[] = { CTL_KERN, KERN_PROCARGS, kinfo->kp_proc.p_pid }; michael@0: michael@0: // Found out what size buffer we need michael@0: size_t data_len = 0; michael@0: if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) { michael@0: CHROMIUM_LOG(ERROR) << "failed to figure out the buffer size for a commandline"; michael@0: continue; michael@0: } michael@0: michael@0: data.resize(data_len); michael@0: if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) { michael@0: CHROMIUM_LOG(ERROR) << "failed to fetch a commandline"; michael@0: continue; michael@0: } michael@0: michael@0: // Data starts w/ the full path null termed, so we have to extract just the michael@0: // executable name from the path. michael@0: michael@0: size_t exec_name_end = data.find('\0'); michael@0: if (exec_name_end == std::string::npos) { michael@0: CHROMIUM_LOG(ERROR) << "command line data didn't match expected format"; michael@0: continue; michael@0: } michael@0: size_t last_slash = data.rfind('/', exec_name_end); michael@0: if (last_slash == std::string::npos) michael@0: exec_name = data.substr(0, exec_name_end); michael@0: else michael@0: exec_name = data.substr(last_slash + 1, exec_name_end - last_slash - 1); michael@0: michael@0: // Check the name michael@0: if (executable_name_utf8 == exec_name) { michael@0: entry_.pid = kinfo->kp_proc.p_pid; michael@0: entry_.ppid = kinfo->kp_eproc.e_ppid; michael@0: base::strlcpy(entry_.szExeFile, exec_name.c_str(), michael@0: sizeof(entry_.szExeFile)); michael@0: // Start w/ the next entry next time through michael@0: ++index_of_kinfo_proc_; michael@0: // Done michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool NamedProcessIterator::IncludeEntry() { michael@0: // Don't need to check the name, we did that w/in CheckForNextProcess. michael@0: if (!filter_) michael@0: return true; michael@0: return filter_->Includes(entry_.pid, entry_.ppid); michael@0: } michael@0: michael@0: } // namespace base