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_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
6 #define CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
12 #include "base/pickle.h"
13 #include "base/process/kill.h"
14 #include "base/synchronization/lock.h"
15 #include "content/public/browser/file_descriptor_info.h"
16 #include "content/public/browser/zygote_host_linux.h"
19 template<typename Type
>
20 struct DefaultSingletonTraits
;
25 class CONTENT_EXPORT ZygoteHostImpl
: public ZygoteHost
{
27 // Returns the singleton instance.
28 static ZygoteHostImpl
* GetInstance();
30 void Init(const std::string
& sandbox_cmd
);
32 // After the last known Zygote child exits, notify the Zygote to exit.
33 void TearDownAfterLastChild();
35 // Tries to start a process of type indicated by process_type.
36 // Returns its pid on success, otherwise
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
);
43 // Get the termination status (and, optionally, the exit code) of
44 // the process. |exit_code| is set to the exit code of the child
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
,
58 // ZygoteHost implementation:
59 pid_t
GetPid() const override
;
60 int GetSandboxStatus() const override
;
61 void AdjustRendererOOMScore(base::ProcessHandle process_handle
,
65 friend struct base::DefaultSingletonTraits
<ZygoteHostImpl
>;
68 ~ZygoteHostImpl() override
;
70 // Notify the Zygote to exit immediately. This object should not be
74 // Should be called every time a Zygote child is born.
75 void ZygoteChildBorn(pid_t process
);
77 // Should be called every time a Zygote child died.
78 void ZygoteChildDied(pid_t process
);
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
);
85 ssize_t
ReadReply(void* buf
, size_t buflen
);
87 // Whether we should use the namespace sandbox instead of the setuid sandbox.
88 bool ShouldUseNamespaceSandbox();
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
94 base::Lock control_lock_
;
97 bool use_suid_sandbox_for_adj_oom_score_
;
98 std::string sandbox_binary_
;
99 bool have_read_sandbox_status_word_
;
101 // A lock protecting list_of_running_zygote_children_ and
102 // should_teardown_after_last_child_exits_.
103 base::Lock child_tracking_lock_
;
104 std::set
<pid_t
> list_of_running_zygote_children_
;
105 bool should_teardown_after_last_child_exits_
;
108 } // namespace content
110 #endif // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_