Home | History | Annotate | Download | only in zygote_host
      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 CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
      6 #define CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     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"
     17 
     18 template<typename Type>
     19 struct DefaultSingletonTraits;
     20 
     21 namespace content {
     22 
     23 class CONTENT_EXPORT ZygoteHostImpl : public ZygoteHost {
     24  public:
     25   // Returns the singleton instance.
     26   static ZygoteHostImpl* GetInstance();
     27 
     28   void Init(const std::string& sandbox_cmd);
     29 
     30   // After the last known Zygote child exits, notify the Zygote to exit.
     31   void TearDownAfterLastChild();
     32 
     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);
     40 
     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,
     53                                                bool known_dead,
     54                                                int* exit_code);
     55 
     56   // ZygoteHost implementation:
     57   virtual pid_t GetPid() const OVERRIDE;
     58   virtual int GetSandboxStatus() const OVERRIDE;
     59   virtual void AdjustRendererOOMScore(base::ProcessHandle process_handle,
     60                                       int score) OVERRIDE;
     61 
     62  private:
     63   friend struct DefaultSingletonTraits<ZygoteHostImpl>;
     64 
     65   ZygoteHostImpl();
     66   virtual ~ZygoteHostImpl();
     67 
     68   // Notify the Zygote to exit immediately. This object should not be
     69   // used afterwards.
     70   void TearDown();
     71 
     72   // Should be called every time a Zygote child is born.
     73   void ZygoteChildBorn(pid_t process);
     74 
     75   // Should be called every time a Zygote child died.
     76   void ZygoteChildDied(pid_t process);
     77 
     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);
     82 
     83   ssize_t ReadReply(void* buf, size_t buflen);
     84 
     85   int control_fd_;  // the socket to the zygote
     86   // A lock protecting all communication with the zygote. This lock must be
     87   // acquired before sending a command and released after the result has been
     88   // received.
     89   base::Lock control_lock_;
     90   pid_t pid_;
     91   bool init_;
     92   bool using_suid_sandbox_;
     93   std::string sandbox_binary_;
     94   bool have_read_sandbox_status_word_;
     95   int sandbox_status_;
     96   // A lock protecting list_of_running_zygote_children_ and
     97   // should_teardown_after_last_child_exits_.
     98   base::Lock child_tracking_lock_;
     99   std::set<pid_t> list_of_running_zygote_children_;
    100   bool should_teardown_after_last_child_exits_;
    101 };
    102 
    103 }  // namespace content
    104 
    105 #endif  // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
    106