Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: base/process_util_posix.cc

Issue 165067: Remove the Mac-specific implementation of LaunchApp, and share the Linux version. (Closed)
Patch Set: Removing stray files from patch Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/process_util_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-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 <dirent.h> 5 #include <dirent.h>
6 #include <errno.h> 6 #include <errno.h>
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <signal.h> 8 #include <signal.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 if (i <= STDERR_FILENO) 213 if (i <= STDERR_FILENO)
214 continue; 214 continue;
215 215
216 int flags = fcntl(i, F_GETFD); 216 int flags = fcntl(i, F_GETFD);
217 if ((flags == -1) || (fcntl(i, F_SETFD, flags | FD_CLOEXEC) == -1)) { 217 if ((flags == -1) || (fcntl(i, F_SETFD, flags | FD_CLOEXEC) == -1)) {
218 DLOG(ERROR) << "fcntl failure."; 218 DLOG(ERROR) << "fcntl failure.";
219 } 219 }
220 } 220 }
221 } 221 }
222 222
223 bool LaunchApp(const std::vector<std::string>& argv,
224 const environment_vector& environ,
225 const file_handle_mapping_vector& fds_to_remap,
226 bool wait, ProcessHandle* process_handle) {
227 pid_t pid = fork();
228 if (pid < 0)
229 return false;
230
231 if (pid == 0) {
232 // Child process
233 InjectiveMultimap fd_shuffle;
234 for (file_handle_mapping_vector::const_iterator
235 it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) {
236 fd_shuffle.push_back(InjectionArc(it->first, it->second, false));
237 }
238
239 for (environment_vector::const_iterator it = environ.begin();
240 it != environ.end(); ++it) {
241 if (it->first) {
242 if (it->second) {
243 setenv(it->first, it->second, 1);
244 } else {
245 unsetenv(it->first);
246 }
247 }
248 }
249
250 // Obscure fork() rule: in the child, if you don't end up doing exec*(),
251 // you call _exit() instead of exit(). This is because _exit() does not
252 // call any previously-registered (in the parent) exit handlers, which
253 // might do things like block waiting for threads that don't even exist
254 // in the child.
255 if (!ShuffleFileDescriptors(fd_shuffle))
256 _exit(127);
257
258 // If we are using the SUID sandbox, it sets a magic environment variable
259 // ("SBX_D"), so we remove that variable from the environment here on the
260 // off chance that it's already set.
261 unsetenv("SBX_D");
262
263 CloseSuperfluousFds(fd_shuffle);
264
265 scoped_array<char*> argv_cstr(new char*[argv.size() + 1]);
266 for (size_t i = 0; i < argv.size(); i++)
267 argv_cstr[i] = const_cast<char*>(argv[i].c_str());
268 argv_cstr[argv.size()] = NULL;
269 execvp(argv_cstr[0], argv_cstr.get());
270 LOG(ERROR) << "LaunchApp: exec failed!, argv_cstr[0] " << argv_cstr[0]
271 << ", errno " << errno;
272 _exit(127);
273 } else {
274 // Parent process
275 if (wait)
276 HANDLE_EINTR(waitpid(pid, 0, 0));
277
278 if (process_handle)
279 *process_handle = pid;
280 }
281
282 return true;
283 }
284
285 bool LaunchApp(const std::vector<std::string>& argv,
286 const file_handle_mapping_vector& fds_to_remap,
287 bool wait, ProcessHandle* process_handle) {
288 base::environment_vector no_env;
289 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle);
290 }
291
292 bool LaunchApp(const CommandLine& cl,
293 bool wait, bool start_hidden,
294 ProcessHandle* process_handle) {
295 file_handle_mapping_vector no_files;
296 return LaunchApp(cl.argv(), no_files, wait, process_handle);
297 }
298
223 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), 299 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process),
224 last_time_(0), 300 last_time_(0),
225 last_system_time_(0) { 301 last_system_time_(0) {
226 processor_count_ = base::SysInfo::NumberOfProcessors(); 302 processor_count_ = base::SysInfo::NumberOfProcessors();
227 } 303 }
228 304
229 // static 305 // static
230 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 306 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
231 return new ProcessMetrics(process); 307 return new ProcessMetrics(process);
232 } 308 }
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 const ProcessFilter* filter) { 624 const ProcessFilter* filter) {
549 bool exited_cleanly = 625 bool exited_cleanly =
550 WaitForProcessesToExit(executable_name, wait_milliseconds, 626 WaitForProcessesToExit(executable_name, wait_milliseconds,
551 filter); 627 filter);
552 if (!exited_cleanly) 628 if (!exited_cleanly)
553 KillProcesses(executable_name, exit_code, filter); 629 KillProcesses(executable_name, exit_code, filter);
554 return exited_cleanly; 630 return exited_cleanly;
555 } 631 }
556 632
557 } // namespace base 633 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698