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"
18 template<typename Type
>
19 struct DefaultSingletonTraits
;
23 class CONTENT_EXPORT ZygoteHostImpl
: public ZygoteHost
{
25 // Returns the singleton instance.
26 static ZygoteHostImpl
* GetInstance();
28 void Init(const std::string
& sandbox_cmd
);
30 // After the last known Zygote child exits, notify the Zygote to exit.
31 void TearDownAfterLastChild();
33 // Tries to start a process of type indicated by process_type.
34 // Returns its pid on success, otherwise
35 // base::kNullProcessHandle;
36 pid_t
ForkRequest(const std::vector
<std::string
>& command_line
,
37 scoped_ptr
<FileDescriptorInfo
> mapping
,
38 const std::string
& process_type
);
39 void EnsureProcessTerminated(pid_t process
);
41 // Get the termination status (and, optionally, the exit code) of
42 // the process. |exit_code| is set to the exit code of the child
43 // process. (|exit_code| may be NULL.)
44 // Unfortunately the Zygote can not accurately figure out if a process
45 // is already dead without waiting synchronously for it.
46 // |known_dead| should be set to true when we already know that the process
47 // is dead. When |known_dead| is false, processes could be seen as
48 // still running, even when they're not. When |known_dead| is true, the
49 // process will be SIGKILL-ed first (which should have no effect if it was
50 // really dead). This is to prevent a waiting waitpid() from blocking in
51 // a single-threaded Zygote. See crbug.com/157458.
52 base::TerminationStatus
GetTerminationStatus(base::ProcessHandle handle
,
56 // ZygoteHost implementation:
57 pid_t
GetPid() const override
;
58 int GetSandboxStatus() const override
;
59 void AdjustRendererOOMScore(base::ProcessHandle process_handle
,
63 friend struct DefaultSingletonTraits
<ZygoteHostImpl
>;
66 ~ZygoteHostImpl() override
;
68 // Notify the Zygote to exit immediately. This object should not be
72 // Should be called every time a Zygote child is born.
73 void ZygoteChildBorn(pid_t process
);
75 // Should be called every time a Zygote child died.
76 void ZygoteChildDied(pid_t process
);
78 // Sends |data| to the zygote via |control_fd_|. If |fds| is non-NULL, the
79 // included file descriptors will also be passed. The caller is responsible
80 // for acquiring |control_lock_|.
81 bool SendMessage(const Pickle
& data
, const std::vector
<int>* fds
);
83 ssize_t
ReadReply(void* buf
, size_t buflen
);
85 // Whether we should use the namespace sandbox instead of the setuid sandbox.
86 bool ShouldUseNamespaceSandbox();
88 int control_fd_
; // the socket to the zygote
89 // A lock protecting all communication with the zygote. This lock must be
90 // acquired before sending a command and released after the result has been
92 base::Lock control_lock_
;
95 bool use_suid_sandbox_for_adj_oom_score_
;
96 std::string sandbox_binary_
;
97 bool have_read_sandbox_status_word_
;
99 // A lock protecting list_of_running_zygote_children_ and
100 // should_teardown_after_last_child_exits_.
101 base::Lock child_tracking_lock_
;
102 std::set
<pid_t
> list_of_running_zygote_children_
;
103 bool should_teardown_after_last_child_exits_
;
106 } // namespace content
108 #endif // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_