OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ | 5 #ifndef CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ |
6 #define CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ | 6 #define CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | |
11 | 10 |
12 #include "base/pickle.h" | |
13 #include "base/process/kill.h" | |
14 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
15 #include "content/public/browser/file_descriptor_info.h" | |
16 #include "content/public/browser/zygote_host_linux.h" | 12 #include "content/public/browser/zygote_host_linux.h" |
17 | 13 |
18 namespace base { | 14 namespace base { |
19 template<typename Type> | 15 template<typename Type> |
20 struct DefaultSingletonTraits; | 16 struct DefaultSingletonTraits; |
21 } // namespace base | 17 } // namespace base |
22 | 18 |
23 namespace content { | 19 namespace content { |
24 | 20 |
25 class CONTENT_EXPORT ZygoteHostImpl : public ZygoteHost { | 21 class CONTENT_EXPORT ZygoteHostImpl : public ZygoteHost { |
26 public: | 22 public: |
27 // Returns the singleton instance. | 23 // Returns the singleton instance. |
28 static ZygoteHostImpl* GetInstance(); | 24 static ZygoteHostImpl* GetInstance(); |
29 | 25 |
30 void Init(const std::string& sandbox_cmd); | 26 void Init(const std::string& sandbox_cmd); |
31 | 27 |
32 // After the last known Zygote child exits, notify the Zygote to exit. | 28 // Retrieves the sandbox command passed into Init(); |
33 void TearDownAfterLastChild(); | 29 const std::string& SandboxCommand() const; |
34 | 30 |
35 // Tries to start a process of type indicated by process_type. | 31 // Tells the ZygoteHost the PIDs of all the zygotes. |
36 // Returns its pid on success, otherwise | 32 void AddZygotePid(pid_t pid); |
37 // base::kNullProcessHandle; | |
38 pid_t ForkRequest(const std::vector<std::string>& command_line, | |
39 scoped_ptr<FileDescriptorInfo> mapping, | |
40 const std::string& process_type); | |
41 void EnsureProcessTerminated(pid_t process); | |
42 | 33 |
43 // Get the termination status (and, optionally, the exit code) of | 34 // Returns whether or not this pid is the pid of a zygote. |
44 // the process. |exit_code| is set to the exit code of the child | 35 bool IsZygotePid(pid_t pid) override; |
45 // process. (|exit_code| may be NULL.) | |
46 // Unfortunately the Zygote can not accurately figure out if a process | |
47 // is already dead without waiting synchronously for it. | |
48 // |known_dead| should be set to true when we already know that the process | |
49 // is dead. When |known_dead| is false, processes could be seen as | |
50 // still running, even when they're not. When |known_dead| is true, the | |
51 // process will be SIGKILL-ed first (which should have no effect if it was | |
52 // really dead). This is to prevent a waiting waitpid() from blocking in | |
53 // a single-threaded Zygote. See crbug.com/157458. | |
54 base::TerminationStatus GetTerminationStatus(base::ProcessHandle handle, | |
55 bool known_dead, | |
56 int* exit_code); | |
57 | 36 |
58 // ZygoteHost implementation: | 37 void SetRendererSandboxStatus(int status); |
59 pid_t GetPid() const override; | 38 int GetRendererSandboxStatus() const override; |
60 int GetSandboxStatus() const override; | |
61 void AdjustRendererOOMScore(base::ProcessHandle process_handle, | 39 void AdjustRendererOOMScore(base::ProcessHandle process_handle, |
62 int score) override; | 40 int score) override; |
63 | 41 |
64 private: | 42 private: |
65 friend struct base::DefaultSingletonTraits<ZygoteHostImpl>; | 43 friend struct base::DefaultSingletonTraits<ZygoteHostImpl>; |
66 | 44 |
67 ZygoteHostImpl(); | 45 ZygoteHostImpl(); |
68 ~ZygoteHostImpl() override; | 46 ~ZygoteHostImpl() override; |
69 | 47 |
70 // Notify the Zygote to exit immediately. This object should not be | 48 int renderer_sandbox_status_; |
71 // used afterwards. | |
72 void TearDown(); | |
73 | |
74 // Should be called every time a Zygote child is born. | |
75 void ZygoteChildBorn(pid_t process); | |
76 | |
77 // Should be called every time a Zygote child died. | |
78 void ZygoteChildDied(pid_t process); | |
79 | |
80 // Sends |data| to the zygote via |control_fd_|. If |fds| is non-NULL, the | |
81 // included file descriptors will also be passed. The caller is responsible | |
82 // for acquiring |control_lock_|. | |
83 bool SendMessage(const base::Pickle& data, const std::vector<int>* fds); | |
84 | |
85 ssize_t ReadReply(void* buf, size_t buflen); | |
86 | |
87 // Whether we should use the namespace sandbox instead of the setuid sandbox. | |
88 bool ShouldUseNamespaceSandbox(); | |
89 | |
90 int control_fd_; // the socket to the zygote | |
91 // A lock protecting all communication with the zygote. This lock must be | |
92 // acquired before sending a command and released after the result has been | |
93 // received. | |
94 base::Lock control_lock_; | |
95 pid_t pid_; | |
96 bool init_; | |
97 bool use_suid_sandbox_for_adj_oom_score_; | 49 bool use_suid_sandbox_for_adj_oom_score_; |
98 std::string sandbox_binary_; | 50 std::string sandbox_binary_; |
99 bool have_read_sandbox_status_word_; | 51 // This lock protects the |zygote_pids_| set. |
100 int sandbox_status_; | 52 base::Lock zygote_pids_lock_; |
101 // A lock protecting list_of_running_zygote_children_ and | 53 // This is a set of PIDs representing all the running zygotes. |
102 // should_teardown_after_last_child_exits_. | 54 std::set<pid_t> zygote_pids_; |
103 base::Lock child_tracking_lock_; | |
104 std::set<pid_t> list_of_running_zygote_children_; | |
105 bool should_teardown_after_last_child_exits_; | |
106 }; | 55 }; |
107 | 56 |
108 } // namespace content | 57 } // namespace content |
109 | 58 |
110 #endif // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ | 59 #endif // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_ |
OLD | NEW |