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

Unified 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: Use RAW_CHECK as suggested by jln; cleanup SetSignalMask interface Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/process/launch_posix.cc
diff --git a/base/process/launch_posix.cc b/base/process/launch_posix.cc
index 3708af6bac417b49e4015fed93eb49966ed78363..f0fc2a6dffbf1ec351bd859b3d0696c9df120578 100644
--- a/base/process/launch_posix.cc
+++ b/base/process/launch_posix.cc
@@ -77,6 +77,22 @@ void SetEnvironment(char** env) {
#endif
}
+// Set the calling thread's signal mask to new_mask, and return
+// the previous signal mask.
+sigset_t SetSignalMask(const sigset_t& new_sigmask) {
+ sigset_t old_sigmask;
+#if defined(OS_ANDROID)
+ // POSIX says pthread_sigmask() must be used in multi-threaded processes,
+ // but Android's pthread_sigmask() was broken until 4.1:
+ // https://code.google.com/p/android/issues/detail?id=15337
+ // http://stackoverflow.com/questions/13777109/pthread-sigmask-on-android-not-working
+ RAW_CHECK(sigprocmask(SIG_SETMASK, &new_sigmask, &old_sigmask) == 0);
+#else
+ RAW_CHECK(pthread_sigmask(SIG_SETMASK, &new_sigmask, &old_sigmask) == 0);
+#endif
+ return old_sigmask;
+}
+
#if !defined(OS_LINUX) || \
(!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__))
void ResetChildSignalHandlersToDefaults() {
@@ -394,9 +410,14 @@ bool LaunchProcess(const std::vector<std::string>& argv,
if (options.environ)
new_environ.reset(AlterEnvironment(*options.environ, GetEnvironment()));
+ sigset_t full_sigset;
+ sigfillset(&full_sigset);
+ const sigset_t orig_sigmask = SetSignalMask(full_sigset);
mdempsky_google 2013/08/02 18:45:00 Now that SetSignalMask() doesn't need to return su
+
pid_t pid;
#if defined(OS_LINUX)
if (options.clone_flags) {
+ RAW_CHECK(!(options.clone_flags & (CLONE_THREAD | CLONE_VM)));
pid = syscall(__NR_clone, options.clone_flags, 0, 0, 0);
} else
#endif
@@ -404,6 +425,11 @@ bool LaunchProcess(const std::vector<std::string>& argv,
pid = fork();
}
+ // Always restore the original signal mask in the parent.
+ if (pid != 0) {
+ SetSignalMask(orig_sigmask);
+ }
+
if (pid < 0) {
DPLOG(ERROR) << "fork";
return false;
@@ -469,6 +495,7 @@ bool LaunchProcess(const std::vector<std::string>& argv,
#endif // defined(OS_MACOSX)
ResetChildSignalHandlersToDefaults();
+ SetSignalMask(orig_sigmask);
#if 0
// When debugging it can be helpful to check that we really aren't making
« 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