File: init_process_reaper.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,212 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "sandbox/linux/services/init_process_reaper.h"

#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"

namespace sandbox {

namespace {

void DoNothingSignalHandler(int signal) {}

}  // namespace

bool CreateInitProcessReaper(base::OnceClosure post_fork_parent_callback) {
  int sync_fds[2];
  // We want to use send, so we can't use a pipe
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sync_fds)) {
    PLOG(ERROR) << "Failed to create socketpair";
    return false;
  }
  pid_t child_pid = fork();
  if (child_pid == -1) {
    int close_ret;
    close_ret = IGNORE_EINTR(close(sync_fds[0]));
    DPCHECK(!close_ret);
    close_ret = IGNORE_EINTR(close(sync_fds[1]));
    DPCHECK(!close_ret);
    return false;
  }
  if (child_pid) {
    // In the parent, assuming the role of an init process.
    // The disposition for SIGCHLD cannot be SIG_IGN or wait() will only return
    // once all of our childs are dead. Since we're init we need to reap childs
    // as they come.
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.sa_handler = &DoNothingSignalHandler;
    CHECK(sigaction(SIGCHLD, &action, NULL) == 0);

    int close_ret;
    close_ret = IGNORE_EINTR(close(sync_fds[0]));
    DPCHECK(!close_ret);
    close_ret = shutdown(sync_fds[1], SHUT_RD);
    DPCHECK(!close_ret);
    if (!post_fork_parent_callback.is_null())
      std::move(post_fork_parent_callback).Run();

    // Tell the child to continue
    CHECK(HANDLE_EINTR(send(sync_fds[1], "C", 1, MSG_NOSIGNAL)) == 1);
    close_ret = IGNORE_EINTR(close(sync_fds[1]));
    DPCHECK(!close_ret);

    for (;;) {
      // Loop until we have reaped our one natural child
      siginfo_t reaped_child_info;
      int wait_ret =
          HANDLE_EINTR(waitid(P_ALL, 0, &reaped_child_info, WEXITED));
      if (wait_ret)
        _exit(1);
      if (reaped_child_info.si_pid == child_pid) {
        int exit_code = 0;
        // We're done waiting
        if (reaped_child_info.si_code == CLD_EXITED) {
          exit_code = reaped_child_info.si_status;
        }
        // Exit with the same exit code as our child. Exit with 0 if we got
        // signaled.
        _exit(exit_code);
      }
    }
  } else {
    // The child needs to wait for the parent to run the callback to avoid a
    // race condition.
    int close_ret;
    close_ret = IGNORE_EINTR(close(sync_fds[1]));
    DPCHECK(!close_ret);
    close_ret = shutdown(sync_fds[0], SHUT_WR);
    DPCHECK(!close_ret);
    char should_continue;
    int read_ret = HANDLE_EINTR(read(sync_fds[0], &should_continue, 1));
    close_ret = IGNORE_EINTR(close(sync_fds[0]));
    DPCHECK(!close_ret);
    if (read_ret == 1)
      return true;
    else
      return false;
  }
}

}  // namespace sandbox.