Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sandbox/linux/services/init_process_reaper.h" | |
| 6 | |
| 7 #include <signal.h> | |
| 8 #include <string.h> | |
| 9 #include <sys/socket.h> | |
| 10 #include <sys/types.h> | |
| 11 #include <sys/wait.h> | |
| 12 #include <unistd.h> | |
| 13 | |
| 14 #include "base/callback.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/posix/eintr_wrapper.h" | |
| 17 | |
| 18 namespace sandbox { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void DoNothingSignalHandler(int signal) {} | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 bool CreateInitProcessReaper(base::Closure* post_fork_parent_callback) { | |
| 27 int sync_fds[2]; | |
| 28 // We want to use send, so we can't use a pipe | |
| 29 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sync_fds)) { | |
| 30 LOG(ERROR) << "Failed to create socketpair"; | |
|
Jorge Lucangeli Obes
2013/11/27 02:31:53
PLOG?
jln (very slow on Chromium)
2013/11/27 02:37:14
Done.
| |
| 31 return false; | |
| 32 } | |
| 33 // We use normal fork, not the ForkDelegate in this case since we are not a | |
| 34 // true Zygote yet. | |
| 35 pid_t child_pid = fork(); | |
| 36 if (child_pid == -1) { | |
| 37 int close_ret; | |
| 38 close_ret = HANDLE_EINTR(close(sync_fds[0])); | |
| 39 DPCHECK(!close_ret); | |
| 40 close_ret = HANDLE_EINTR(close(sync_fds[1])); | |
| 41 DPCHECK(!close_ret); | |
| 42 return false; | |
| 43 } | |
| 44 if (child_pid) { | |
| 45 // We are the parent, assuming the role of an init process. | |
| 46 // The disposition for SIGCHLD cannot be SIG_IGN or wait() will only return | |
| 47 // once all of our childs are dead. Since we're init we need to reap childs | |
| 48 // as they come. | |
| 49 struct sigaction action; | |
| 50 memset(&action, 0, sizeof(action)); | |
| 51 action.sa_handler = &DoNothingSignalHandler; | |
| 52 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); | |
| 53 | |
| 54 int close_ret; | |
| 55 close_ret = HANDLE_EINTR(close(sync_fds[0])); | |
| 56 DPCHECK(!close_ret); | |
| 57 close_ret = shutdown(sync_fds[1], SHUT_RD); | |
| 58 DPCHECK(!close_ret); | |
| 59 if (post_fork_parent_callback) | |
| 60 post_fork_parent_callback->Run(); | |
| 61 // Tell the child to continue | |
| 62 CHECK(HANDLE_EINTR(send(sync_fds[1], "C", 1, MSG_NOSIGNAL)) == 1); | |
| 63 close_ret = HANDLE_EINTR(close(sync_fds[1])); | |
| 64 DPCHECK(!close_ret); | |
| 65 | |
| 66 for (;;) { | |
| 67 // Loop until we have reaped our one natural child | |
| 68 siginfo_t reaped_child_info; | |
| 69 int wait_ret = | |
| 70 HANDLE_EINTR(waitid(P_ALL, 0, &reaped_child_info, WEXITED)); | |
| 71 if (wait_ret) | |
| 72 _exit(1); | |
| 73 if (reaped_child_info.si_pid == child_pid) { | |
| 74 int exit_code = 0; | |
| 75 // We're done waiting | |
| 76 if (reaped_child_info.si_code == CLD_EXITED) { | |
| 77 exit_code = reaped_child_info.si_status; | |
| 78 } | |
| 79 // Exit with the same exit code as our parent. This is most likely | |
| 80 // useless. _exit with 0 if we got signaled. | |
| 81 _exit(exit_code); | |
| 82 } | |
| 83 } | |
| 84 } else { | |
| 85 // The child needs to wait for the parent to close kZygoteIdFd to avoid a | |
| 86 // race condition | |
| 87 int close_ret; | |
| 88 close_ret = HANDLE_EINTR(close(sync_fds[1])); | |
| 89 DPCHECK(!close_ret); | |
| 90 close_ret = shutdown(sync_fds[0], SHUT_WR); | |
| 91 DPCHECK(!close_ret); | |
| 92 char should_continue; | |
| 93 int read_ret = HANDLE_EINTR(read(sync_fds[0], &should_continue, 1)); | |
| 94 close_ret = HANDLE_EINTR(close(sync_fds[0])); | |
| 95 DPCHECK(!close_ret); | |
| 96 if (read_ret == 1) | |
| 97 return true; | |
| 98 else | |
| 99 return false; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 } // namespace sandbox. | |
| OLD | NEW |