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.
4 
5 #ifndef SERVICES_SERVICE_MANAGER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
6 #define SERVICES_SERVICE_MANAGER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
7 
8 #include <sys/types.h>
9 
10 #include <set>
11 #include <string>
12 
13 #include "base/command_line.h"
14 #include "base/component_export.h"
15 #include "base/files/scoped_file.h"
16 #include "base/process/launch.h"
17 #include "base/process/process_handle.h"
18 #include "base/synchronization/lock.h"
19 
20 #include "services/service_manager/zygote/zygote_host_linux.h"
21 
22 namespace base {
23 template <typename Type>
24 struct DefaultSingletonTraits;
25 }  // namespace base
26 
27 namespace service_manager {
28 
COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE)29 class COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE) ZygoteHostImpl
30     : public ZygoteHost {
31  public:
32   // Returns the singleton instance.
33   static ZygoteHostImpl* GetInstance();
34 
35   void Init(const base::CommandLine& cmd_line);
36 
37   // Returns whether or not this pid is the pid of a zygote.
38   bool IsZygotePid(pid_t pid) override;
39 
40   void SetRendererSandboxStatus(int status);
41   int GetRendererSandboxStatus() const override;
42 
43   pid_t LaunchZygote(base::CommandLine* cmd_line,
44                      base::ScopedFD* control_fd,
45                      base::FileHandleMappingVector additional_remapped_fds);
46 
47 #if !defined(OS_BSD)
48   void AdjustRendererOOMScore(base::ProcessHandle process_handle,
49                               int score) override;
50 #endif
51   bool HasZygote() { return !zygote_pids_.empty(); }
52 
53  private:
54   friend struct base::DefaultSingletonTraits<ZygoteHostImpl>;
55 
56   ZygoteHostImpl();
57   ~ZygoteHostImpl() override;
58 
59   // Tells the ZygoteHost the PIDs of all the zygotes.
60   void AddZygotePid(pid_t pid);
61 
62   int renderer_sandbox_status_;
63 
64   bool use_namespace_sandbox_;
65   bool use_suid_sandbox_;
66   bool use_suid_sandbox_for_adj_oom_score_;
67   std::string sandbox_binary_;
68 
69   // This lock protects the |zygote_pids_| set.
70   base::Lock zygote_pids_lock_;
71   // This is a set of PIDs representing all the running zygotes.
72   std::set<pid_t> zygote_pids_;
73 };
74 
75 }  // namespace service_manager
76 
77 #endif  // SERVICES_SERVICE_MANAGER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
78