blob: 3eb7a1562fb766d190307b57462e198857482dbb [file] [log] [blame]
[email protected]b75346c2012-05-13 03:48:381// Copyright (c) 2012 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
tfarinabccc34c72015-02-27 21:32:155#ifndef CONTENT_ZYGOTE_ZYGOTE_LINUX_H_
6#define CONTENT_ZYGOTE_ZYGOTE_LINUX_H_
[email protected]b75346c2012-05-13 03:48:387
[email protected]54ad01a2014-05-11 01:17:478#include <stddef.h>
9
leon.han79db18e2017-01-27 05:26:2610#include <memory>
[email protected]b75346c2012-05-13 03:48:3811#include <string>
mdempskyf12295a2015-12-09 22:54:4612#include <vector>
[email protected]b75346c2012-05-13 03:48:3813
[email protected]9f2bdf122013-08-21 02:59:3414#include "base/containers/small_map.h"
[email protected]8feaa672014-04-30 21:57:1015#include "base/files/scoped_file.h"
[email protected]9e5a0a62013-10-11 19:39:4116#include "base/posix/global_descriptors.h"
[email protected]9f2bdf122013-08-21 02:59:3417#include "base/process/kill.h"
[email protected]54724e22013-07-25 13:02:1518#include "base/process/process.h"
kerrnel26b892f2015-11-10 19:06:3119#include "base/process/process_handle.h"
20#include "base/time/time.h"
[email protected]b75346c2012-05-13 03:48:3821
brettw05cfd8ddb2015-06-02 07:02:4722namespace base {
[email protected]b75346c2012-05-13 03:48:3823class PickleIterator;
brettw05cfd8ddb2015-06-02 07:02:4724}
[email protected]b75346c2012-05-13 03:48:3825
26namespace content {
27
28class ZygoteForkDelegate;
29
30// This is the object which implements the zygote. The ZygoteMain function,
31// which is called from ChromeMain, simply constructs one of these objects and
32// runs it.
33class Zygote {
34 public:
leon.han79db18e2017-01-27 05:26:2635 Zygote(int sandbox_flags,
36 std::vector<std::unique_ptr<ZygoteForkDelegate>> helpers,
[email protected]655abd522014-06-02 15:23:4337 const std::vector<base::ProcessHandle>& extra_children,
38 const std::vector<int>& extra_fds);
[email protected]b75346c2012-05-13 03:48:3839 ~Zygote();
40
41 bool ProcessRequests();
42
[email protected]b75346c2012-05-13 03:48:3843 private:
[email protected]9f2bdf122013-08-21 02:59:3444 struct ZygoteProcessInfo {
45 // Pid from inside the Zygote's PID namespace.
46 base::ProcessHandle internal_pid;
[email protected]54ad01a2014-05-11 01:17:4747 // Keeps track of which fork delegate helper the process was started from.
48 ZygoteForkDelegate* started_from_helper;
kerrnel26b892f2015-11-10 19:06:3149 // Records when the browser requested the zygote to reap this process.
50 base::TimeTicks time_of_reap_request;
51 // Notes whether the zygote has sent SIGKILL to this process.
52 bool sent_sigkill;
[email protected]9f2bdf122013-08-21 02:59:3453 };
brettwca2ec7632017-04-20 06:10:2054 using ZygoteProcessMap =
55 base::small_map<std::map<base::ProcessHandle, ZygoteProcessInfo>>;
[email protected]9f2bdf122013-08-21 02:59:3456
[email protected]500d24e2013-08-21 19:55:3257 // Retrieve a ZygoteProcessInfo from the process_info_map_.
58 // Returns true and write to process_info if |pid| can be found, return
59 // false otherwise.
brettwca2ec7632017-04-20 06:10:2060 bool GetProcessInfo(base::ProcessHandle pid, ZygoteProcessInfo* process_info);
[email protected]500d24e2013-08-21 19:55:3261
[email protected]b75346c2012-05-13 03:48:3862 // Returns true if the SUID sandbox is active.
63 bool UsingSUIDSandbox() const;
jln151ebd72015-02-19 02:21:5564 // Returns true if the NS sandbox is active.
65 bool UsingNSSandbox() const;
[email protected]b75346c2012-05-13 03:48:3866
67 // ---------------------------------------------------------------------------
68 // Requests from the browser...
69
70 // Read and process a request from the browser. Returns true if we are in a
71 // new process and thus need to unwind back into ChromeMain.
72 bool HandleRequestFromBrowser(int fd);
73
brettw05cfd8ddb2015-06-02 07:02:4774 void HandleReapRequest(int fd, base::PickleIterator iter);
[email protected]b75346c2012-05-13 03:48:3875
[email protected]9f2bdf122013-08-21 02:59:3476 // Get the termination status of |real_pid|. |real_pid| is the PID as it
77 // appears outside of the sandbox.
78 // Return true if it managed to get the termination status and return the
79 // status in |status| and the exit code in |exit_code|.
80 bool GetTerminationStatus(base::ProcessHandle real_pid, bool known_dead,
81 base::TerminationStatus* status,
82 int* exit_code);
83
[email protected]b75346c2012-05-13 03:48:3884 void HandleGetTerminationStatus(int fd,
brettw05cfd8ddb2015-06-02 07:02:4785 base::PickleIterator iter);
[email protected]b75346c2012-05-13 03:48:3886
87 // This is equivalent to fork(), except that, when using the SUID sandbox, it
88 // returns the real PID of the child process as it appears outside the
[email protected]53cbbe252014-05-08 01:26:2889 // sandbox, rather than returning the PID inside the sandbox. The child's
90 // real PID is determined by having it call content::SendZygoteChildPing(int)
91 // using the |pid_oracle| descriptor.
92 // Finally, when using a ZygoteForkDelegate helper, |uma_name|, |uma_sample|,
93 // and |uma_boundary_value| may be set if the helper wants to make a UMA
94 // report via UMA_HISTOGRAM_ENUMERATION.
[email protected]b75346c2012-05-13 03:48:3895 int ForkWithRealPid(const std::string& process_type,
[email protected]9e5a0a62013-10-11 19:39:4196 const base::GlobalDescriptors::Mapping& fd_mapping,
[email protected]3a161242014-04-18 00:07:3397 const std::string& channel_id,
[email protected]53cbbe252014-05-08 01:26:2898 base::ScopedFD pid_oracle,
[email protected]b75346c2012-05-13 03:48:3899 std::string* uma_name,
100 int* uma_sample,
101 int* uma_boundary_value);
102
avi48fc13b2014-12-28 23:31:48103 // Unpacks process type and arguments from |iter| and forks a new process.
[email protected]b75346c2012-05-13 03:48:38104 // Returns -1 on error, otherwise returns twice, returning 0 to the child
105 // process and the child process ID to the parent process, like fork().
brettw05cfd8ddb2015-06-02 07:02:47106 base::ProcessId ReadArgsAndFork(base::PickleIterator iter,
mdempskyf12295a2015-12-09 22:54:46107 std::vector<base::ScopedFD> fds,
[email protected]b75346c2012-05-13 03:48:38108 std::string* uma_name,
109 int* uma_sample,
110 int* uma_boundary_value);
111
112 // Handle a 'fork' request from the browser: this means that the browser
113 // wishes to start a new renderer. Returns true if we are in a new process,
114 // otherwise writes the child_pid back to the browser via |fd|. Writes a
115 // child_pid of -1 on error.
116 bool HandleForkRequest(int fd,
brettw05cfd8ddb2015-06-02 07:02:47117 base::PickleIterator iter,
mdempskyf12295a2015-12-09 22:54:46118 std::vector<base::ScopedFD> fds);
[email protected]b75346c2012-05-13 03:48:38119
120 bool HandleGetSandboxStatus(int fd,
brettw05cfd8ddb2015-06-02 07:02:47121 base::PickleIterator iter);
[email protected]b75346c2012-05-13 03:48:38122
kerrnel26b892f2015-11-10 19:06:31123 // Attempt to reap the child process by calling waitpid, and return
124 // whether successful. If the process has not terminated within
125 // 2 seconds of its reap request, send it SIGKILL.
126 bool ReapChild(const base::TimeTicks& now, ZygoteProcessInfo* child);
127
128 // Attempt to reap all outstanding children in |to_reap_|.
129 void ReapChildren();
130
[email protected]9f2bdf122013-08-21 02:59:34131 // The Zygote needs to keep some information about each process. Most
132 // notably what the PID of the process is inside the PID namespace of
133 // the Zygote and whether or not a process was started by the
134 // ZygoteForkDelegate helper.
135 ZygoteProcessMap process_info_map_;
[email protected]b75346c2012-05-13 03:48:38136
137 const int sandbox_flags_;
leon.han79db18e2017-01-27 05:26:26138 std::vector<std::unique_ptr<ZygoteForkDelegate>> helpers_;
[email protected]b75346c2012-05-13 03:48:38139
[email protected]54ad01a2014-05-11 01:17:47140 // Count of how many fork delegates for which we've invoked InitialUMA().
141 size_t initial_uma_index_;
[email protected]655abd522014-06-02 15:23:43142
143 // This vector contains the PIDs of any child processes which have been
144 // created prior to the construction of the Zygote object, and must be reaped
145 // before the Zygote exits. The Zygote will perform a blocking wait on these
146 // children, so they must be guaranteed to be exiting by the time the Zygote
147 // exits.
148 std::vector<base::ProcessHandle> extra_children_;
149
150 // This vector contains the FDs that must be closed before reaping the extra
151 // children.
152 std::vector<int> extra_fds_;
kerrnel26b892f2015-11-10 19:06:31153
154 // The vector contains the child processes that need to be reaped.
155 std::vector<ZygoteProcessInfo> to_reap_;
[email protected]b75346c2012-05-13 03:48:38156};
157
158} // namespace content
159
tfarinabccc34c72015-02-27 21:32:15160#endif // CONTENT_ZYGOTE_ZYGOTE_LINUX_H_