| 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 <unistd.h> | 10 #include <unistd.h> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
| 14 | 14 |
| 15 #include "base/eintr_wrapper.h" | 15 #include "base/eintr_wrapper.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/string_tokenizer.h" | 18 #include "base/string_tokenizer.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/zygote_manager.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 enum ParsingState { | 24 enum ParsingState { |
| 24 KEY_NAME, | 25 KEY_NAME, |
| 25 KEY_VALUE | 26 KEY_VALUE |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 // Reads /proc/<pid>/stat and populates |proc_stats| with the values split by | 29 // Reads /proc/<pid>/stat and populates |proc_stats| with the values split by |
| 29 // spaces. | 30 // spaces. |
| 30 void GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { | 31 void GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { |
| 31 FilePath stat_file("/proc"); | 32 FilePath stat_file("/proc"); |
| 32 stat_file = stat_file.Append(IntToString(pid)); | 33 stat_file = stat_file.Append(IntToString(pid)); |
| 33 stat_file = stat_file.Append("stat"); | 34 stat_file = stat_file.Append("stat"); |
| 34 std::string mem_stats; | 35 std::string mem_stats; |
| 35 if (!file_util::ReadFileToString(stat_file, &mem_stats)) | 36 if (!file_util::ReadFileToString(stat_file, &mem_stats)) |
| 36 return; | 37 return; |
| 37 SplitString(mem_stats, ' ', proc_stats); | 38 SplitString(mem_stats, ' ', proc_stats); |
| 38 } | 39 } |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 namespace base { | 43 namespace base { |
| 43 | 44 |
| 45 bool ForkApp(const std::vector<std::string>& argv, |
| 46 const file_handle_mapping_vector& fds_to_remap, |
| 47 ProcessHandle* process_handle) { |
| 48 ZygoteManager* zm = ZygoteManager::Get(); |
| 49 if (!zm) |
| 50 return LaunchApp(argv, fds_to_remap, false, process_handle); |
| 51 |
| 52 pid_t pid = zm->LongFork(argv, fds_to_remap); |
| 53 if (pid < 0) |
| 54 return false; |
| 55 |
| 56 if (process_handle) |
| 57 *process_handle = pid; |
| 58 return true; |
| 59 } |
| 60 |
| 44 bool LaunchApp(const std::vector<std::string>& argv, | 61 bool LaunchApp(const std::vector<std::string>& argv, |
| 45 const file_handle_mapping_vector& fds_to_remap, | 62 const file_handle_mapping_vector& fds_to_remap, |
| 46 bool wait, ProcessHandle* process_handle) { | 63 bool wait, ProcessHandle* process_handle) { |
| 47 pid_t pid = fork(); | 64 pid_t pid = fork(); |
| 48 if (pid < 0) | 65 if (pid < 0) |
| 49 return false; | 66 return false; |
| 50 | 67 |
| 51 if (pid == 0) { | 68 if (pid == 0) { |
| 52 InjectiveMultimap fd_shuffle; | 69 InjectiveMultimap fd_shuffle; |
| 53 for (file_handle_mapping_vector::const_iterator | 70 for (file_handle_mapping_vector::const_iterator |
| 54 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { | 71 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { |
| 55 fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); | 72 fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); |
| 56 } | 73 } |
| 57 | 74 |
| 58 if (!ShuffleFileDescriptors(fd_shuffle)) | 75 if (!ShuffleFileDescriptors(fd_shuffle)) |
| 59 exit(127); | 76 exit(127); |
| 60 | 77 |
| 61 CloseSuperfluousFds(fd_shuffle); | 78 CloseSuperfluousFds(fd_shuffle); |
| 62 | 79 |
| 63 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); | 80 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); |
| 64 for (size_t i = 0; i < argv.size(); i++) | 81 for (size_t i = 0; i < argv.size(); i++) |
| 65 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); | 82 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); |
| 66 argv_cstr[argv.size()] = NULL; | 83 argv_cstr[argv.size()] = NULL; |
| 67 execvp(argv_cstr[0], argv_cstr.get()); | 84 execvp(argv_cstr[0], argv_cstr.get()); |
| 85 LOG(ERROR) << "LaunchApp: exec failed!, argv_cstr[0] " << argv_cstr[0] |
| 86 << ", errno " << errno; |
| 68 exit(127); | 87 exit(127); |
| 69 } else { | 88 } else { |
| 70 if (wait) | 89 if (wait) |
| 71 HANDLE_EINTR(waitpid(pid, 0, 0)); | 90 HANDLE_EINTR(waitpid(pid, 0, 0)); |
| 72 | 91 |
| 73 if (process_handle) | 92 if (process_handle) |
| 74 *process_handle = pid; | 93 *process_handle = pid; |
| 75 } | 94 } |
| 76 | 95 |
| 77 return true; | 96 return true; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); | 294 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); |
| 276 } | 295 } |
| 277 state = KEY_NAME; | 296 state = KEY_NAME; |
| 278 break; | 297 break; |
| 279 } | 298 } |
| 280 } | 299 } |
| 281 return true; | 300 return true; |
| 282 } | 301 } |
| 283 | 302 |
| 284 } // namespace base | 303 } // namespace base |
| OLD | NEW |