| 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 #include "base/process_util.h" | 5 #include "base/process_util.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <dirent.h> | 8 #include <dirent.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/eintr_wrapper.h" | |
| 17 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 18 #include "base/logging.h" | 17 #include "base/logging.h" |
| 19 #include "base/string_tokenizer.h" | 18 #include "base/string_tokenizer.h" |
| 20 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 enum ParsingState { | 23 enum ParsingState { |
| 25 KEY_NAME, | 24 KEY_NAME, |
| 26 KEY_VALUE | 25 KEY_VALUE |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 stat_file = stat_file.Append("exe"); | 78 stat_file = stat_file.Append("exe"); |
| 80 char exename[2048]; | 79 char exename[2048]; |
| 81 ssize_t len = readlink(stat_file.value().c_str(), exename, sizeof(exename)); | 80 ssize_t len = readlink(stat_file.value().c_str(), exename, sizeof(exename)); |
| 82 if (len < 1) { | 81 if (len < 1) { |
| 83 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses | 82 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses |
| 84 return FilePath(); | 83 return FilePath(); |
| 85 } | 84 } |
| 86 return FilePath(std::string(exename, len)); | 85 return FilePath(std::string(exename, len)); |
| 87 } | 86 } |
| 88 | 87 |
| 89 bool LaunchApp(const std::vector<std::string>& argv, | |
| 90 const environment_vector& environ, | |
| 91 const file_handle_mapping_vector& fds_to_remap, | |
| 92 bool wait, ProcessHandle* process_handle) { | |
| 93 pid_t pid = fork(); | |
| 94 if (pid < 0) | |
| 95 return false; | |
| 96 | |
| 97 if (pid == 0) { | |
| 98 // Child process | |
| 99 InjectiveMultimap fd_shuffle; | |
| 100 for (file_handle_mapping_vector::const_iterator | |
| 101 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { | |
| 102 fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); | |
| 103 } | |
| 104 | |
| 105 for (environment_vector::const_iterator it = environ.begin(); | |
| 106 it != environ.end(); ++it) { | |
| 107 if (it->first) { | |
| 108 if (it->second) { | |
| 109 setenv(it->first, it->second, 1); | |
| 110 } else { | |
| 111 unsetenv(it->first); | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 // Obscure fork() rule: in the child, if you don't end up doing exec*(), | |
| 117 // you call _exit() instead of exit(). This is because _exit() does not | |
| 118 // call any previously-registered (in the parent) exit handlers, which | |
| 119 // might do things like block waiting for threads that don't even exist | |
| 120 // in the child. | |
| 121 if (!ShuffleFileDescriptors(fd_shuffle)) | |
| 122 _exit(127); | |
| 123 | |
| 124 // If we are using the SUID sandbox, it sets a magic environment variable | |
| 125 // ("SBX_D"), so we remove that variable from the environment here on the | |
| 126 // off chance that it's already set. | |
| 127 unsetenv("SBX_D"); | |
| 128 | |
| 129 CloseSuperfluousFds(fd_shuffle); | |
| 130 | |
| 131 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); | |
| 132 for (size_t i = 0; i < argv.size(); i++) | |
| 133 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); | |
| 134 argv_cstr[argv.size()] = NULL; | |
| 135 execvp(argv_cstr[0], argv_cstr.get()); | |
| 136 LOG(ERROR) << "LaunchApp: exec failed!, argv_cstr[0] " << argv_cstr[0] | |
| 137 << ", errno " << errno; | |
| 138 _exit(127); | |
| 139 } else { | |
| 140 // Parent process | |
| 141 if (wait) | |
| 142 HANDLE_EINTR(waitpid(pid, 0, 0)); | |
| 143 | |
| 144 if (process_handle) | |
| 145 *process_handle = pid; | |
| 146 } | |
| 147 | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 bool LaunchApp(const std::vector<std::string>& argv, | |
| 152 const file_handle_mapping_vector& fds_to_remap, | |
| 153 bool wait, ProcessHandle* process_handle) { | |
| 154 base::environment_vector no_env; | |
| 155 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); | |
| 156 } | |
| 157 | |
| 158 bool LaunchApp(const CommandLine& cl, | |
| 159 bool wait, bool start_hidden, | |
| 160 ProcessHandle* process_handle) { | |
| 161 file_handle_mapping_vector no_files; | |
| 162 return LaunchApp(cl.argv(), no_files, wait, process_handle); | |
| 163 } | |
| 164 | |
| 165 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, | 88 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, |
| 166 const ProcessFilter* filter) | 89 const ProcessFilter* filter) |
| 167 : executable_name_(executable_name), filter_(filter) { | 90 : executable_name_(executable_name), filter_(filter) { |
| 168 procfs_dir_ = opendir("/proc"); | 91 procfs_dir_ = opendir("/proc"); |
| 169 } | 92 } |
| 170 | 93 |
| 171 NamedProcessIterator::~NamedProcessIterator() { | 94 NamedProcessIterator::~NamedProcessIterator() { |
| 172 if (procfs_dir_) { | 95 if (procfs_dir_) { |
| 173 closedir(procfs_dir_); | 96 closedir(procfs_dir_); |
| 174 procfs_dir_ = NULL; | 97 procfs_dir_ = NULL; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); | 334 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); |
| 412 } | 335 } |
| 413 state = KEY_NAME; | 336 state = KEY_NAME; |
| 414 break; | 337 break; |
| 415 } | 338 } |
| 416 } | 339 } |
| 417 return true; | 340 return true; |
| 418 } | 341 } |
| 419 | 342 |
| 420 } // namespace base | 343 } // namespace base |
| OLD | NEW |