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 <string>
      9 #include <vector>
     10 
     11 #include "base/pickle.h"
     12 #include "base/process/kill.h"
     13 #include "base/synchronization/lock.h"
     14 #include "content/public/browser/file_descriptor_info.h"
     15 #include "content/public/browser/zygote_host_linux.h"
     16 
     17 template<typename Type>
     18 struct DefaultSingletonTraits;
     19 
     20 namespace content {
     21 
     22 class CONTENT_EXPORT ZygoteHostImpl : public ZygoteHost {
     23  public:
     24   // Returns the singleton instance.
     25   static ZygoteHostImpl* GetInstance();
     26 
     27   void Init(const std::string& sandbox_cmd);
     28 
     29   // Tries to start a process of type indicated by process_type.
     30   // Returns its pid on success, otherwise
     31   // base::kNullProcessHandle;
     32   pid_t ForkRequest(const std::vector<std::string>& command_line,
     33                     const std::vector<FileDescriptorInfo>& mapping,
     34                     const std::string& process_type);
     35   void EnsureProcessTerminated(pid_t process);
     36 
     37   // Get the termination status (and, optionally, the exit code) of
     38   // the process. |exit_code| is set to the exit code of the child
     39   // process. (|exit_code| may be NULL.)
     40   // Unfortunately the Zygote can not accurately figure out if a process
     41   // is already dead without waiting synchronously for it.
     42   // |known_dead| should be set to true when we already know that the process
     43   // is dead.
     44   base::TerminationStatus GetTerminationStatus(base::ProcessHandle handle,
     45                                                bool known_dead,
     46                                                int* exit_code);
     47 
     48   // ZygoteHost implementation:
     49   virtual pid_t GetPid() const OVERRIDE;
     50   virtual pid_t GetSandboxHelperPid() const OVERRIDE;
     51   virtual int GetSandboxStatus() const OVERRIDE;
     52   virtual void AdjustRendererOOMScore(base::ProcessHandle process_handle,
     53                                       int score) OVERRIDE;
     54   virtual void AdjustLowMemoryMargin(int64 margin_mb) OVERRIDE;
     55 
     56  private:
     57   friend struct DefaultSingletonTraits<ZygoteHostImpl>;
     58 
     59   ZygoteHostImpl();
     60   virtual ~ZygoteHostImpl();
     61 
     62   // Sends |data| to the zygote via |control_fd_|.  If |fds| is non-NULL, the
     63   // included file descriptors will also be passed.  The caller is responsible
     64   // for acquiring |control_lock_|.
     65   bool SendMessage(const Pickle& data, const std::vector<int>* fds);
     66 
     67   ssize_t ReadReply(void* buf, size_t buflen);
     68 
     69   int control_fd_;  // the socket to the zygote
     70   // A lock protecting all communication with the zygote. This lock must be
     71   // acquired before sending a command and released after the result has been
     72   // received.
     73   base::Lock control_lock_;
     74   pid_t pid_;
     75   bool init_;
     76   bool using_suid_sandbox_;
     77   std::string sandbox_binary_;
     78   bool have_read_sandbox_status_word_;
     79   int sandbox_status_;
     80 };
     81 
     82 }  // namespace content
     83 
     84 #endif  // CONTENT_BROWSER_ZYGOTE_HOST_ZYGOTE_HOST_IMPL_LINUX_H_
     85