File: zygote_communication_linux.h

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 (113 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download | duplicates (11)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_COMMON_ZYGOTE_ZYGOTE_COMMUNICATION_LINUX_H_
#define CONTENT_COMMON_ZYGOTE_ZYGOTE_COMMUNICATION_LINUX_H_

#include <memory>
#include <set>
#include <string>
#include <vector>

#include <sys/types.h>

#include "base/files/platform_file.h"
#include "base/files/scoped_file.h"
#include "base/functional/callback.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/synchronization/lock.h"
#include "content/common/content_export.h"

namespace base {
class Pickle;
}  // namespace base

namespace content {

// Handles interprocess communication with the Linux zygote process. The zygote
// does not use standard Chrome IPC or mojo, see:
// https://chromium.googlesource.com/chromium/src/+/main/docs/linux/sandbox_ipc.md
class CONTENT_EXPORT ZygoteCommunication {
 public:
  enum class ZygoteType { kSandboxed, kUnsandboxed };
  explicit ZygoteCommunication(ZygoteType type);
  ~ZygoteCommunication();

  void Init(
      base::OnceCallback<pid_t(base::CommandLine*, base::ScopedFD*)> launcher);

  // Reinitialize logging. Needed on ChromeOS, which switches to a log file
  // in the user's home directory once they log in.
  void ReinitializeLogging(uint32_t logging_dest,
                           base::PlatformFile log_file_fd);

  // Tries to start a process of type indicated by process_type.
  // Returns its pid on success, otherwise base::kNullProcessHandle;
  pid_t ForkRequest(const std::vector<std::string>& command_line,
                    const base::FileHandleMappingVector& mapping,
                    const std::string& process_type);

  void EnsureProcessTerminated(pid_t process);

  // Should be called every time a Zygote child died.
  void ZygoteChildDied(pid_t process);

  // Get the termination status (and, optionally, the exit code) of
  // the process. |exit_code| is set to the exit code of the child
  // process. (|exit_code| may be NULL.)
  // Unfortunately the Zygote can not accurately figure out if a process
  // is already dead without waiting synchronously for it.
  // |known_dead| should be set to true when we already know that the process
  // is dead. When |known_dead| is false, processes could be seen as
  // still running, even when they're not. When |known_dead| is true, the
  // process will be SIGKILL-ed first (which should have no effect if it was
  // really dead). This is to prevent a waiting waitpid() from blocking in
  // a single-threaded Zygote. See https://crbug.com/157458.
  base::TerminationStatus GetTerminationStatus(base::ProcessHandle handle,
                                               bool known_dead,
                                               int* exit_code);

  // Returns the sandbox status of this zygote.
  int GetSandboxStatus();

 private:
  // Should be called every time a Zygote child is born.
  void ZygoteChildBorn(pid_t process);

  // Read the reply from the zygote.
  ssize_t ReadReply(void* buf, size_t buf_len);

  // Sends |data| to the zygote via |control_fd_|.  If |fds| is non-NULL, the
  // included file descriptors will also be passed.  The caller is responsible
  // for acquiring |control_lock_|.
  bool SendMessage(const base::Pickle& data, const std::vector<int>* fds);

  // Get the sandbox status from the zygote.
  ssize_t ReadSandboxStatus();

  // Indicates whether the Zygote starts unsandboxed or not.
  const ZygoteType type_;

  base::ScopedFD control_fd_;  // the socket to the zygote.
  // A lock protecting all communication with the zygote. This lock must be
  // acquired before sending a command and released after the result has been
  // received.
  base::Lock control_lock_;
  // The pid of the zygote.
  pid_t pid_;
  // The list of running zygote children.
  std::set<pid_t> list_of_running_zygote_children_;
  // The lock to guard the list of running zygote children.
  base::Lock child_tracking_lock_;
  int sandbox_status_;
  bool have_read_sandbox_status_word_;
  // Set to true when the zygote is initialized successfully.
  bool init_;
};

}  // namespace content

#endif  // CONTENT_COMMON_ZYGOTE_ZYGOTE_COMMUNICATION_LINUX_H_