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 const std::vector
<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 virtual pid_t
GetPid() const OVERRIDE
;
58 virtual pid_t
GetSandboxHelperPid() const OVERRIDE
;
59 virtual int GetSandboxStatus() const OVERRIDE
;
60 virtual void AdjustRendererOOMScore(base::ProcessHandle process_handle
,
64 friend struct DefaultSingletonTraits
<ZygoteHostImpl
>;
67 virtual ~ZygoteHostImpl();
69 // Notify the Zygote to exit immediately. This object should not be
73 // Should be called every time a Zygote child is born.
74 void ZygoteChildBorn(pid_t process
);
76 // Should be called every time a Zygote child died.
77 void ZygoteChildDied(pid_t process
);
79 // Sends |data| to the zygote via |control_fd_|. If |fds| is non-NULL, the
80 // included file descriptors will also be passed. The caller is responsible
81 // for acquiring |control_lock_|.
82 bool SendMessage(const Pickle
& data
, const std::vector
<int>* fds
);
84 ssize_t
ReadReply(void* buf
, size_t buflen
);
86 int control_fd_
; // the socket to the zygote
87 // A lock protecting all communication with the zygote. This lock must be
88 // acquired before sending a command and released after the result has been
90 base::Lock control_lock_
;
93 bool using_suid_sandbox_
;
94 std::string sandbox_binary_
;
95 bool have_read_sandbox_status_word_
;
97 // A lock protecting list_of_running_zygote_children_ and
98 // should_teardown_after_last_child_exits_.
99 base::Lock child_tracking_lock_
;
100 std::set
<pid_t
> list_of_running_zygote_children_
;
101 bool should_teardown_after_last_child_exits_
;
104 } // namespace content
106 #endif // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_