Android: Fix potential null-ptr deref
[chromium-blink-merge.git] / content / zygote / zygote_linux.h
blob41773fa474b94f1db97b862523ac809e2f5f256c
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_
8 #include <stddef.h>
10 #include <string>
12 #include "base/containers/small_map.h"
13 #include "base/files/scoped_file.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/posix/global_descriptors.h"
16 #include "base/process/kill.h"
17 #include "base/process/process.h"
19 class Pickle;
20 class PickleIterator;
22 namespace content {
24 class ZygoteForkDelegate;
26 // This is the object which implements the zygote. The ZygoteMain function,
27 // which is called from ChromeMain, simply constructs one of these objects and
28 // runs it.
29 class Zygote {
30 public:
31 Zygote(int sandbox_flags, ScopedVector<ZygoteForkDelegate> helpers,
32 const std::vector<base::ProcessHandle>& extra_children,
33 const std::vector<int>& extra_fds);
34 ~Zygote();
36 bool ProcessRequests();
38 private:
39 struct ZygoteProcessInfo {
40 // Pid from inside the Zygote's PID namespace.
41 base::ProcessHandle internal_pid;
42 // Keeps track of which fork delegate helper the process was started from.
43 ZygoteForkDelegate* started_from_helper;
45 typedef base::SmallMap< std::map<base::ProcessHandle, ZygoteProcessInfo> >
46 ZygoteProcessMap;
48 // Retrieve a ZygoteProcessInfo from the process_info_map_.
49 // Returns true and write to process_info if |pid| can be found, return
50 // false otherwise.
51 bool GetProcessInfo(base::ProcessHandle pid,
52 ZygoteProcessInfo* process_info);
54 // Returns true if the SUID sandbox is active.
55 bool UsingSUIDSandbox() const;
57 // ---------------------------------------------------------------------------
58 // Requests from the browser...
60 // Read and process a request from the browser. Returns true if we are in a
61 // new process and thus need to unwind back into ChromeMain.
62 bool HandleRequestFromBrowser(int fd);
64 void HandleReapRequest(int fd, PickleIterator iter);
66 // Get the termination status of |real_pid|. |real_pid| is the PID as it
67 // appears outside of the sandbox.
68 // Return true if it managed to get the termination status and return the
69 // status in |status| and the exit code in |exit_code|.
70 bool GetTerminationStatus(base::ProcessHandle real_pid, bool known_dead,
71 base::TerminationStatus* status,
72 int* exit_code);
74 void HandleGetTerminationStatus(int fd,
75 PickleIterator iter);
77 // This is equivalent to fork(), except that, when using the SUID sandbox, it
78 // returns the real PID of the child process as it appears outside the
79 // sandbox, rather than returning the PID inside the sandbox. The child's
80 // real PID is determined by having it call content::SendZygoteChildPing(int)
81 // using the |pid_oracle| descriptor.
82 // Finally, when using a ZygoteForkDelegate helper, |uma_name|, |uma_sample|,
83 // and |uma_boundary_value| may be set if the helper wants to make a UMA
84 // report via UMA_HISTOGRAM_ENUMERATION.
85 int ForkWithRealPid(const std::string& process_type,
86 const base::GlobalDescriptors::Mapping& fd_mapping,
87 const std::string& channel_id,
88 base::ScopedFD pid_oracle,
89 std::string* uma_name,
90 int* uma_sample,
91 int* uma_boundary_value);
93 // Unpacks process type and arguments from |iter| and forks a new process.
94 // Returns -1 on error, otherwise returns twice, returning 0 to the child
95 // process and the child process ID to the parent process, like fork().
96 base::ProcessId ReadArgsAndFork(PickleIterator iter,
97 ScopedVector<base::ScopedFD> fds,
98 std::string* uma_name,
99 int* uma_sample,
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 PickleIterator iter,
108 ScopedVector<base::ScopedFD> fds);
110 bool HandleGetSandboxStatus(int fd,
111 PickleIterator iter);
113 // The Zygote needs to keep some information about each process. Most
114 // notably what the PID of the process is inside the PID namespace of
115 // the Zygote and whether or not a process was started by the
116 // ZygoteForkDelegate helper.
117 ZygoteProcessMap process_info_map_;
119 const int sandbox_flags_;
120 ScopedVector<ZygoteForkDelegate> helpers_;
122 // Count of how many fork delegates for which we've invoked InitialUMA().
123 size_t initial_uma_index_;
125 // This vector contains the PIDs of any child processes which have been
126 // created prior to the construction of the Zygote object, and must be reaped
127 // before the Zygote exits. The Zygote will perform a blocking wait on these
128 // children, so they must be guaranteed to be exiting by the time the Zygote
129 // exits.
130 std::vector<base::ProcessHandle> extra_children_;
132 // This vector contains the FDs that must be closed before reaping the extra
133 // children.
134 std::vector<int> extra_fds_;
137 } // namespace content
139 #endif // CONTENT_ZYGOTE_ZYGOTE_H_