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