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_
10 #include "base/containers/small_map.h"
11 #include "base/files/scoped_file.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/posix/global_descriptors.h"
14 #include "base/process/kill.h"
15 #include "base/process/process.h"
22 class ZygoteForkDelegate;
24 // This is the object which implements the zygote. The ZygoteMain function,
25 // which is called from ChromeMain, simply constructs one of these objects and
29 Zygote(int sandbox_flags,
30 ZygoteForkDelegate* helper);
33 bool ProcessRequests();
36 struct ZygoteProcessInfo {
37 // Pid from inside the Zygote's PID namespace.
38 base::ProcessHandle internal_pid;
39 // Keeps track of whether or not a process was started from a fork
41 bool started_from_helper;
43 typedef base::SmallMap< std::map<base::ProcessHandle, ZygoteProcessInfo> >
46 // Retrieve a ZygoteProcessInfo from the process_info_map_.
47 // Returns true and write to process_info if |pid| can be found, return
49 bool GetProcessInfo(base::ProcessHandle pid,
50 ZygoteProcessInfo* process_info);
52 // Returns true if the SUID sandbox is active.
53 bool UsingSUIDSandbox() const;
55 // ---------------------------------------------------------------------------
56 // Requests from the browser...
58 // Read and process a request from the browser. Returns true if we are in a
59 // new process and thus need to unwind back into ChromeMain.
60 bool HandleRequestFromBrowser(int fd);
62 void HandleReapRequest(int fd, const Pickle& pickle, PickleIterator iter);
64 // Get the termination status of |real_pid|. |real_pid| is the PID as it
65 // appears outside of the sandbox.
66 // Return true if it managed to get the termination status and return the
67 // status in |status| and the exit code in |exit_code|.
68 bool GetTerminationStatus(base::ProcessHandle real_pid, bool known_dead,
69 base::TerminationStatus* status,
72 void HandleGetTerminationStatus(int fd,
76 // This is equivalent to fork(), except that, when using the SUID sandbox, it
77 // returns the real PID of the child process as it appears outside the
78 // sandbox, rather than returning the PID inside the sandbox. The child's
79 // real PID is determined by having it call content::SendZygoteChildPing(int)
80 // using the |pid_oracle| descriptor.
81 // Finally, when using a ZygoteForkDelegate helper, |uma_name|, |uma_sample|,
82 // and |uma_boundary_value| may be set if the helper wants to make a UMA
83 // report via UMA_HISTOGRAM_ENUMERATION.
84 int ForkWithRealPid(const std::string& process_type,
85 const base::GlobalDescriptors::Mapping& fd_mapping,
86 const std::string& channel_id,
87 base::ScopedFD pid_oracle,
88 std::string* uma_name,
90 int* uma_boundary_value);
92 // Unpacks process type and arguments from |pickle| and forks a new process.
93 // Returns -1 on error, otherwise returns twice, returning 0 to the child
94 // process and the child process ID to the parent process, like fork().
95 base::ProcessId ReadArgsAndFork(const Pickle& pickle,
97 ScopedVector<base::ScopedFD> fds,
98 std::string* uma_name,
100 int* uma_boundary_value);
102 // Handle a 'fork' request from the browser: this means that the browser
103 // wishes to start a new renderer. Returns true if we are in a new process,
104 // otherwise writes the child_pid back to the browser via |fd|. Writes a
105 // child_pid of -1 on error.
106 bool HandleForkRequest(int fd,
107 const Pickle& pickle,
109 ScopedVector<base::ScopedFD> fds);
111 bool HandleGetSandboxStatus(int fd,
112 const Pickle& pickle,
113 PickleIterator iter);
115 // The Zygote needs to keep some information about each process. Most
116 // notably what the PID of the process is inside the PID namespace of
117 // the Zygote and whether or not a process was started by the
118 // ZygoteForkDelegate helper.
119 ZygoteProcessMap process_info_map_;
121 const int sandbox_flags_;
122 ZygoteForkDelegate* helper_;
124 // These might be set by helper_->InitialUMA. They supply a UMA enumeration
125 // sample we should report on the first fork.
126 std::string initial_uma_name_;
127 int initial_uma_sample_;
128 int initial_uma_boundary_value_;
131 } // namespace content
133 #endif // CONTENT_ZYGOTE_ZYGOTE_H_