1 // 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.
5 #ifndef CONTENT_ZYGOTE_ZYGOTE_H_
6 #define CONTENT_ZYGOTE_ZYGOTE_H_
11 #include "base/containers/small_map.h"
12 #include "base/process/kill.h"
13 #include "base/process/process.h"
20 class ZygoteForkDelegate
;
22 // This is the object which implements the zygote. The ZygoteMain function,
23 // which is called from ChromeMain, simply constructs one of these objects and
27 Zygote(int sandbox_flags
,
28 ZygoteForkDelegate
* helper
);
31 bool ProcessRequests();
34 struct ZygoteProcessInfo
{
35 // Pid from inside the Zygote's PID namespace.
36 base::ProcessHandle internal_pid
;
37 // Keeps track of whether or not a process was started from a fork
39 bool started_from_helper
;
41 typedef base::SmallMap
< std::map
<base::ProcessHandle
, ZygoteProcessInfo
> >
44 // Retrieve a ZygoteProcessInfo from the process_info_map_.
45 // Returns true and write to process_info if |pid| can be found, return
47 bool GetProcessInfo(base::ProcessHandle pid
,
48 ZygoteProcessInfo
* process_info
);
50 // Returns true if the SUID sandbox is active.
51 bool UsingSUIDSandbox() const;
53 // ---------------------------------------------------------------------------
54 // Requests from the browser...
56 // Read and process a request from the browser. Returns true if we are in a
57 // new process and thus need to unwind back into ChromeMain.
58 bool HandleRequestFromBrowser(int fd
);
60 void HandleReapRequest(int fd
, const Pickle
& pickle
, PickleIterator iter
);
62 // Get the termination status of |real_pid|. |real_pid| is the PID as it
63 // appears outside of the sandbox.
64 // Return true if it managed to get the termination status and return the
65 // status in |status| and the exit code in |exit_code|.
66 bool GetTerminationStatus(base::ProcessHandle real_pid
, bool known_dead
,
67 base::TerminationStatus
* status
,
70 void HandleGetTerminationStatus(int fd
,
74 // This is equivalent to fork(), except that, when using the SUID sandbox, it
75 // returns the real PID of the child process as it appears outside the
76 // sandbox, rather than returning the PID inside the sandbox. Optionally, it
77 // fills in uma_name et al with a report the helper wants to make via
78 // UMA_HISTOGRAM_ENUMERATION.
79 int ForkWithRealPid(const std::string
& process_type
,
80 std::vector
<int>& fds
,
81 const std::string
& channel_switch
,
82 std::string
* uma_name
,
84 int* uma_boundary_value
);
86 // Unpacks process type and arguments from |pickle| and forks a new process.
87 // Returns -1 on error, otherwise returns twice, returning 0 to the child
88 // process and the child process ID to the parent process, like fork().
89 base::ProcessId
ReadArgsAndFork(const Pickle
& pickle
,
91 std::vector
<int>& fds
,
92 std::string
* uma_name
,
94 int* uma_boundary_value
);
96 // Handle a 'fork' request from the browser: this means that the browser
97 // wishes to start a new renderer. Returns true if we are in a new process,
98 // otherwise writes the child_pid back to the browser via |fd|. Writes a
99 // child_pid of -1 on error.
100 bool HandleForkRequest(int fd
,
101 const Pickle
& pickle
,
103 std::vector
<int>& fds
);
105 bool HandleGetSandboxStatus(int fd
,
106 const Pickle
& pickle
,
107 PickleIterator iter
);
109 // The Zygote needs to keep some information about each process. Most
110 // notably what the PID of the process is inside the PID namespace of
111 // the Zygote and whether or not a process was started by the
112 // ZygoteForkDelegate helper.
113 ZygoteProcessMap process_info_map_
;
115 const int sandbox_flags_
;
116 ZygoteForkDelegate
* helper_
;
118 // These might be set by helper_->InitialUMA. They supply a UMA enumeration
119 // sample we should report on the first fork.
120 std::string initial_uma_name_
;
121 int initial_uma_sample_
;
122 int initial_uma_boundary_value_
;
125 } // namespace content
127 #endif // CONTENT_ZYGOTE_ZYGOTE_H_