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 |
| 25 } // namespace |
| 26 |
| 27 bool CreateInitProcessReaper(base::Closure* post_fork_parent_callback) { |
| 28 int sync_fds[2]; |
| 29 // We want to use send, so we can't use a pipe |
| 30 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sync_fds)) { |
| 31 LOG(ERROR) << "Failed to create socketpair"; |
| 32 return false; |
| 33 } |
| 34 // We use normal fork, not the ForkDelegate in this case since we are not a |
| 35 // true Zygote yet. |
| 36 pid_t child_pid = fork(); |
| 37 if (child_pid == -1) { |
| 38 (void) HANDLE_EINTR(close(sync_fds[0])); |
| 39 (void) HANDLE_EINTR(close(sync_fds[1])); |
| 40 return false; |
| 41 } |
| 42 if (child_pid) { |
| 43 // We are the parent, assuming the role of an init process. |
| 44 // The disposition for SIGCHLD cannot be SIG_IGN or wait() will only return |
| 45 // once all of our childs are dead. Since we're init we need to reap childs |
| 46 // as they come. |
| 47 struct sigaction action; |
| 48 memset(&action, 0, sizeof(action)); |
| 49 action.sa_handler = &DoNothingSignalHandler; |
| 50 CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 51 |
| 52 (void) HANDLE_EINTR(close(sync_fds[0])); |
| 53 shutdown(sync_fds[1], SHUT_RD); |
| 54 if (post_fork_parent_callback) |
| 55 post_fork_parent_callback->Run(); |
| 56 // Tell the child to continue |
| 57 CHECK(HANDLE_EINTR(send(sync_fds[1], "C", 1, MSG_NOSIGNAL)) == 1); |
| 58 (void) HANDLE_EINTR(close(sync_fds[1])); |
| 59 |
| 60 for (;;) { |
| 61 // Loop until we have reaped our one natural child |
| 62 siginfo_t reaped_child_info; |
| 63 int wait_ret = |
| 64 HANDLE_EINTR(waitid(P_ALL, 0, &reaped_child_info, WEXITED)); |
| 65 if (wait_ret) |
| 66 _exit(1); |
| 67 if (reaped_child_info.si_pid == child_pid) { |
| 68 int exit_code = 0; |
| 69 // We're done waiting |
| 70 if (reaped_child_info.si_code == CLD_EXITED) { |
| 71 exit_code = reaped_child_info.si_status; |
| 72 } |
| 73 // Exit with the same exit code as our parent. This is most likely |
| 74 // useless. _exit with 0 if we got signaled. |
| 75 _exit(exit_code); |
| 76 } |
| 77 } |
| 78 } else { |
| 79 // The child needs to wait for the parent to close kZygoteIdFd to avoid a |
| 80 // race condition |
| 81 (void) HANDLE_EINTR(close(sync_fds[1])); |
| 82 shutdown(sync_fds[0], SHUT_WR); |
| 83 char should_continue; |
| 84 int read_ret = HANDLE_EINTR(read(sync_fds[0], &should_continue, 1)); |
| 85 (void) HANDLE_EINTR(close(sync_fds[0])); |
| 86 if (read_ret == 1) |
| 87 return true; |
| 88 else |
| 89 return false; |
| 90 } |
| 91 } |
| 92 |
| 93 } // namespace sandbox. |
OLD | NEW |