Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / zygote / zygote_linux.h
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.
4
5 #ifndef CONTENT_ZYGOTE_ZYGOTE_H_
6 #define CONTENT_ZYGOTE_ZYGOTE_H_
7
8 #include <string>
9
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"
16
17 class Pickle;
18 class PickleIterator;
19
20 namespace content {
21
22 class ZygoteForkDelegate;
23
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
26 // runs it.
27 class Zygote {
28  public:
29   Zygote(int sandbox_flags,
30          ZygoteForkDelegate* helper);
31   ~Zygote();
32
33   bool ProcessRequests();
34
35  private:
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
40     // delegate helper.
41     bool started_from_helper;
42   };
43   typedef base::SmallMap< std::map<base::ProcessHandle, ZygoteProcessInfo> >
44       ZygoteProcessMap;
45
46   // Retrieve a ZygoteProcessInfo from the process_info_map_.
47   // Returns true and write to process_info if |pid| can be found, return
48   // false otherwise.
49   bool GetProcessInfo(base::ProcessHandle pid,
50                       ZygoteProcessInfo* process_info);
51
52   // Returns true if the SUID sandbox is active.
53   bool UsingSUIDSandbox() const;
54
55   // ---------------------------------------------------------------------------
56   // Requests from the browser...
57
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);
61
62   void HandleReapRequest(int fd, const Pickle& pickle, PickleIterator iter);
63
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,
70                             int* exit_code);
71
72   void HandleGetTerminationStatus(int fd,
73                                   const Pickle& pickle,
74                                   PickleIterator iter);
75
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,
89                       int* uma_sample,
90                       int* uma_boundary_value);
91
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,
96                                   PickleIterator iter,
97                                   ScopedVector<base::ScopedFD> fds,
98                                   std::string* uma_name,
99                                   int* uma_sample,
100                                   int* uma_boundary_value);
101
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,
108                          PickleIterator iter,
109                          ScopedVector<base::ScopedFD> fds);
110
111   bool HandleGetSandboxStatus(int fd,
112                               const Pickle& pickle,
113                               PickleIterator iter);
114
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_;
120
121   const int sandbox_flags_;
122   ZygoteForkDelegate* helper_;
123
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_;
129 };
130
131 }  // namespace content
132
133 #endif  // CONTENT_ZYGOTE_ZYGOTE_H_