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

Side by Side Diff: base/process/launch_posix.cc

Issue 21415003: Block signals while forking so they aren't run in the child process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tweak comment Created 7 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 | « no previous file | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/launch.h" 5 #include "base/process/launch.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <signal.h> 10 #include <signal.h>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // Set the process's "environment" (i.e. the thing that setenv/getenv 70 // Set the process's "environment" (i.e. the thing that setenv/getenv
71 // work with). 71 // work with).
72 void SetEnvironment(char** env) { 72 void SetEnvironment(char** env) {
73 #if defined(OS_MACOSX) 73 #if defined(OS_MACOSX)
74 *_NSGetEnviron() = env; 74 *_NSGetEnviron() = env;
75 #else 75 #else
76 environ = env; 76 environ = env;
77 #endif 77 #endif
78 } 78 }
79 79
80 // Set the calling thread's signal mask to new_sigmask and return
81 // the previous signal mask.
82 sigset_t SetSignalMask(const sigset_t& new_sigmask) {
83 sigset_t old_sigmask;
84 #if defined(OS_ANDROID)
85 // POSIX says pthread_sigmask() must be used in multi-threaded processes,
86 // but Android's pthread_sigmask() was broken until 4.1:
87 // https://code.google.com/p/android/issues/detail?id=15337
88 // http://stackoverflow.com/questions/13777109/pthread-sigmask-on-android-not- working
89 RAW_CHECK(sigprocmask(SIG_SETMASK, &new_sigmask, &old_sigmask) == 0);
90 #else
91 RAW_CHECK(pthread_sigmask(SIG_SETMASK, &new_sigmask, &old_sigmask) == 0);
92 #endif
93 return old_sigmask;
94 }
95
80 #if !defined(OS_LINUX) || \ 96 #if !defined(OS_LINUX) || \
81 (!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__)) 97 (!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__))
82 void ResetChildSignalHandlersToDefaults() { 98 void ResetChildSignalHandlersToDefaults() {
83 // The previous signal handlers are likely to be meaningless in the child's 99 // The previous signal handlers are likely to be meaningless in the child's
84 // context so we reset them to the defaults for now. http://crbug.com/44953 100 // context so we reset them to the defaults for now. http://crbug.com/44953
85 // These signal handlers are set up at least in browser_main_posix.cc: 101 // These signal handlers are set up at least in browser_main_posix.cc:
86 // BrowserMainPartsPosix::PreEarlyInitialization and stack_trace_posix.cc: 102 // BrowserMainPartsPosix::PreEarlyInitialization and stack_trace_posix.cc:
87 // EnableInProcessStackDumping. 103 // EnableInProcessStackDumping.
88 signal(SIGHUP, SIG_DFL); 104 signal(SIGHUP, SIG_DFL);
89 signal(SIGINT, SIG_DFL); 105 signal(SIGINT, SIG_DFL);
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 InjectiveMultimap fd_shuffle1; 403 InjectiveMultimap fd_shuffle1;
388 InjectiveMultimap fd_shuffle2; 404 InjectiveMultimap fd_shuffle2;
389 fd_shuffle1.reserve(fd_shuffle_size); 405 fd_shuffle1.reserve(fd_shuffle_size);
390 fd_shuffle2.reserve(fd_shuffle_size); 406 fd_shuffle2.reserve(fd_shuffle_size);
391 407
392 scoped_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]); 408 scoped_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]);
393 scoped_ptr<char*[]> new_environ; 409 scoped_ptr<char*[]> new_environ;
394 if (options.environ) 410 if (options.environ)
395 new_environ.reset(AlterEnvironment(*options.environ, GetEnvironment())); 411 new_environ.reset(AlterEnvironment(*options.environ, GetEnvironment()));
396 412
413 sigset_t full_sigset;
414 sigfillset(&full_sigset);
415 const sigset_t orig_sigmask = SetSignalMask(full_sigset);
416
397 pid_t pid; 417 pid_t pid;
398 #if defined(OS_LINUX) 418 #if defined(OS_LINUX)
399 if (options.clone_flags) { 419 if (options.clone_flags) {
420 RAW_CHECK(!(options.clone_flags & (CLONE_THREAD | CLONE_VM)));
jln (very slow on Chromium) 2013/08/05 21:55:40 Maybe add CLONE_SIGHAND as well here ? Add a comm
mdempsky_google 2013/08/05 22:05:19 Done.
400 pid = syscall(__NR_clone, options.clone_flags, 0, 0, 0); 421 pid = syscall(__NR_clone, options.clone_flags, 0, 0, 0);
401 } else 422 } else
402 #endif 423 #endif
403 { 424 {
404 pid = fork(); 425 pid = fork();
405 } 426 }
406 427
428 // Always restore the original signal mask in the parent.
429 if (pid != 0) {
430 SetSignalMask(orig_sigmask);
431 }
432
407 if (pid < 0) { 433 if (pid < 0) {
408 DPLOG(ERROR) << "fork"; 434 DPLOG(ERROR) << "fork";
409 return false; 435 return false;
410 } else if (pid == 0) { 436 } else if (pid == 0) {
411 // Child process 437 // Child process
412 438
413 // DANGER: fork() rule: in the child, if you don't end up doing exec*(), 439 // DANGER: fork() rule: in the child, if you don't end up doing exec*(),
414 // you call _exit() instead of exit(). This is because _exit() does not 440 // you call _exit() instead of exit(). This is because _exit() does not
415 // call any previously-registered (in the parent) exit handlers, which 441 // call any previously-registered (in the parent) exit handlers, which
416 // might do things like block waiting for threads that don't even exist 442 // might do things like block waiting for threads that don't even exist
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 } 488 }
463 } 489 }
464 } 490 }
465 } 491 }
466 492
467 #if defined(OS_MACOSX) 493 #if defined(OS_MACOSX)
468 RestoreDefaultExceptionHandler(); 494 RestoreDefaultExceptionHandler();
469 #endif // defined(OS_MACOSX) 495 #endif // defined(OS_MACOSX)
470 496
471 ResetChildSignalHandlersToDefaults(); 497 ResetChildSignalHandlersToDefaults();
498 SetSignalMask(orig_sigmask);
472 499
473 #if 0 500 #if 0
474 // When debugging it can be helpful to check that we really aren't making 501 // When debugging it can be helpful to check that we really aren't making
475 // any hidden calls to malloc. 502 // any hidden calls to malloc.
476 void *malloc_thunk = 503 void *malloc_thunk =
477 reinterpret_cast<void*>(reinterpret_cast<intptr_t>(malloc) & ~4095); 504 reinterpret_cast<void*>(reinterpret_cast<intptr_t>(malloc) & ~4095);
478 mprotect(malloc_thunk, 4096, PROT_READ | PROT_WRITE | PROT_EXEC); 505 mprotect(malloc_thunk, 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
479 memset(reinterpret_cast<void*>(malloc), 0xff, 8); 506 memset(reinterpret_cast<void*>(malloc), 0xff, 8);
480 #endif // 0 507 #endif // 0
481 508
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 std::string* output, 747 std::string* output,
721 int* exit_code) { 748 int* exit_code) {
722 // Run |execve()| with the current environment and store "unlimited" data. 749 // Run |execve()| with the current environment and store "unlimited" data.
723 GetAppOutputInternalResult result = GetAppOutputInternal( 750 GetAppOutputInternalResult result = GetAppOutputInternal(
724 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true, 751 cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true,
725 exit_code); 752 exit_code);
726 return result == EXECUTE_SUCCESS; 753 return result == EXECUTE_SUCCESS;
727 } 754 }
728 755
729 } // namespace base 756 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698