File: scoped_process.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 (122 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (5)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


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

#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <ostream>

#include "base/check_op.h"
#include "base/functional/callback.h"
#include "base/notreached.h"
#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
#include "sandbox/linux/services/syscall_wrappers.h"
#include "sandbox/linux/services/thread_helpers.h"

namespace sandbox {

namespace {

const char kSynchronisationChar[] = "D";

void WaitForever() {
  while(true) {
    pause();
  }
}

}  // namespace

ScopedProcess::ScopedProcess(base::OnceClosure child_callback)
    : child_process_id_(-1), process_id_(getpid()) {
  PCHECK(0 == pipe(pipe_fds_));
#if !defined(THREAD_SANITIZER)
  // Make sure that we can safely fork().
  CHECK(ThreadHelpers::IsSingleThreaded());
#endif
  child_process_id_ = fork();
  PCHECK(0 <= child_process_id_);

  if (0 == child_process_id_) {
    PCHECK(0 == IGNORE_EINTR(close(pipe_fds_[0])));
    pipe_fds_[0] = -1;
    std::move(child_callback).Run();
    // Notify the parent that the closure has run.
    CHECK_EQ(1, HANDLE_EINTR(write(pipe_fds_[1], kSynchronisationChar, 1)));
    WaitForever();
    NOTREACHED();
  }

  PCHECK(0 == IGNORE_EINTR(close(pipe_fds_[1])));
  pipe_fds_[1] = -1;
}

ScopedProcess::~ScopedProcess() {
  CHECK(IsOriginalProcess());
  if (child_process_id_ >= 0) {
    PCHECK(0 == kill(child_process_id_, SIGKILL));
    siginfo_t process_info;

    PCHECK(0 == HANDLE_EINTR(
                    waitid(P_PID, child_process_id_, &process_info, WEXITED)));
  }
  if (pipe_fds_[0] >= 0) {
    PCHECK(0 == IGNORE_EINTR(close(pipe_fds_[0])));
  }
  if (pipe_fds_[1] >= 0) {
    PCHECK(0 == IGNORE_EINTR(close(pipe_fds_[1])));
  }
}

int ScopedProcess::WaitForExit(bool* got_signaled) {
  DCHECK(got_signaled);
  CHECK(IsOriginalProcess());
  siginfo_t process_info;
  // WNOWAIT to make sure that the destructor can wait on the child.
  int ret = HANDLE_EINTR(
      waitid(P_PID, child_process_id_, &process_info, WEXITED | WNOWAIT));
  PCHECK(0 == ret) << "Did something else wait on the child?";

  if (process_info.si_code == CLD_EXITED) {
    *got_signaled = false;
  } else if (process_info.si_code == CLD_KILLED ||
             process_info.si_code == CLD_DUMPED) {
    *got_signaled = true;
  } else {
    NOTREACHED() << "ScopedProcess needs to be extended for si_code "
                 << process_info.si_code;
  }
  return process_info.si_status;
}

bool ScopedProcess::WaitForClosureToRun() {
  char c = 0;
  int ret = HANDLE_EINTR(read(pipe_fds_[0], &c, 1));
  PCHECK(ret >= 0);
  if (0 == ret)
    return false;

  CHECK_EQ(c, kSynchronisationChar[0]);
  return true;
}

// It would be problematic if after a fork(), another process would start using
// this object.
// This method allows to assert it is not happening.
bool ScopedProcess::IsOriginalProcess() {
  // Make a direct syscall to bypass glibc caching of PIDs.
  pid_t pid = sys_getpid();
  return pid == process_id_;
}

}  // namespace sandbox