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 <string> | |
11 #include <sys/types.h> | 10 #include <sys/types.h> |
12 #include <sys/wait.h> | 11 #include <sys/wait.h> |
13 #include <unistd.h> | 12 #include <unistd.h> |
14 | 13 |
| 14 #include <string> |
| 15 |
15 #include "base/eintr_wrapper.h" | 16 #include "base/eintr_wrapper.h" |
16 #include "base/file_util.h" | 17 #include "base/file_util.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
18 #include "base/string_tokenizer.h" | 19 #include "base/string_tokenizer.h" |
19 #include "base/string_util.h" | 20 #include "base/string_util.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 enum ParsingState { | 24 enum ParsingState { |
24 KEY_NAME, | 25 KEY_NAME, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 char exename[2048]; | 80 char exename[2048]; |
80 ssize_t len = readlink(stat_file.value().c_str(), exename, sizeof(exename)); | 81 ssize_t len = readlink(stat_file.value().c_str(), exename, sizeof(exename)); |
81 if (len < 1) { | 82 if (len < 1) { |
82 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses | 83 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses |
83 return FilePath(); | 84 return FilePath(); |
84 } | 85 } |
85 return FilePath(std::string(exename, len)); | 86 return FilePath(std::string(exename, len)); |
86 } | 87 } |
87 | 88 |
88 bool LaunchApp(const std::vector<std::string>& argv, | 89 bool LaunchApp(const std::vector<std::string>& argv, |
| 90 const environment_vector& environ, |
89 const file_handle_mapping_vector& fds_to_remap, | 91 const file_handle_mapping_vector& fds_to_remap, |
90 bool wait, ProcessHandle* process_handle) { | 92 bool wait, ProcessHandle* process_handle) { |
91 pid_t pid = fork(); | 93 pid_t pid = fork(); |
92 if (pid < 0) | 94 if (pid < 0) |
93 return false; | 95 return false; |
94 | 96 |
95 if (pid == 0) { | 97 if (pid == 0) { |
| 98 // Child process |
96 InjectiveMultimap fd_shuffle; | 99 InjectiveMultimap fd_shuffle; |
97 for (file_handle_mapping_vector::const_iterator | 100 for (file_handle_mapping_vector::const_iterator |
98 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { | 101 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { |
99 fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); | 102 fd_shuffle.push_back(InjectionArc(it->first, it->second, false)); |
100 } | 103 } |
101 | 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 |
102 if (!ShuffleFileDescriptors(fd_shuffle)) | 116 if (!ShuffleFileDescriptors(fd_shuffle)) |
103 exit(127); | 117 exit(127); |
104 | 118 |
105 // If we are using the SUID sandbox, it sets a magic environment variable | 119 // If we are using the SUID sandbox, it sets a magic environment variable |
106 // ("SBX_D"), so we remove that variable from the environment here on the | 120 // ("SBX_D"), so we remove that variable from the environment here on the |
107 // off chance that it's already set. | 121 // off chance that it's already set. |
108 unsetenv("SBX_D"); | 122 unsetenv("SBX_D"); |
109 | 123 |
110 CloseSuperfluousFds(fd_shuffle); | 124 CloseSuperfluousFds(fd_shuffle); |
111 | 125 |
112 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); | 126 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); |
113 for (size_t i = 0; i < argv.size(); i++) | 127 for (size_t i = 0; i < argv.size(); i++) |
114 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); | 128 argv_cstr[i] = const_cast<char*>(argv[i].c_str()); |
115 argv_cstr[argv.size()] = NULL; | 129 argv_cstr[argv.size()] = NULL; |
116 execvp(argv_cstr[0], argv_cstr.get()); | 130 execvp(argv_cstr[0], argv_cstr.get()); |
117 LOG(ERROR) << "LaunchApp: exec failed!, argv_cstr[0] " << argv_cstr[0] | 131 LOG(ERROR) << "LaunchApp: exec failed!, argv_cstr[0] " << argv_cstr[0] |
118 << ", errno " << errno; | 132 << ", errno " << errno; |
119 exit(127); | 133 exit(127); |
120 } else { | 134 } else { |
| 135 // Parent process |
121 if (wait) | 136 if (wait) |
122 HANDLE_EINTR(waitpid(pid, 0, 0)); | 137 HANDLE_EINTR(waitpid(pid, 0, 0)); |
123 | 138 |
124 if (process_handle) | 139 if (process_handle) |
125 *process_handle = pid; | 140 *process_handle = pid; |
126 } | 141 } |
127 | 142 |
128 return true; | 143 return true; |
129 } | 144 } |
130 | 145 |
| 146 bool LaunchApp(const std::vector<std::string>& argv, |
| 147 const file_handle_mapping_vector& fds_to_remap, |
| 148 bool wait, ProcessHandle* process_handle) { |
| 149 base::environment_vector no_env; |
| 150 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); |
| 151 } |
| 152 |
131 bool LaunchApp(const CommandLine& cl, | 153 bool LaunchApp(const CommandLine& cl, |
132 bool wait, bool start_hidden, | 154 bool wait, bool start_hidden, |
133 ProcessHandle* process_handle) { | 155 ProcessHandle* process_handle) { |
134 file_handle_mapping_vector no_files; | 156 file_handle_mapping_vector no_files; |
135 return LaunchApp(cl.argv(), no_files, wait, process_handle); | 157 return LaunchApp(cl.argv(), no_files, wait, process_handle); |
136 } | 158 } |
137 | 159 |
138 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, | 160 NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, |
139 const ProcessFilter* filter) | 161 const ProcessFilter* filter) |
140 : executable_name_(executable_name), filter_(filter) { | 162 : executable_name_(executable_name), filter_(filter) { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); | 348 (*io_counters).WriteTransferCount = StringToInt64(tokenizer.token()); |
327 } | 349 } |
328 state = KEY_NAME; | 350 state = KEY_NAME; |
329 break; | 351 break; |
330 } | 352 } |
331 } | 353 } |
332 return true; | 354 return true; |
333 } | 355 } |
334 | 356 |
335 } // namespace base | 357 } // namespace base |
OLD | NEW |