summaryrefslogtreecommitdiffstats
path: root/content/browser/zygote_host_impl_linux.h
blob: 3c6e824c6fd6b7bb1ab07f88771300221d511c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_ZYGOTE_HOST_IMPL_LINUX_H_
#define CONTENT_BROWSER_ZYGOTE_HOST_IMPL_LINUX_H_
#pragma once

#include <string>
#include <vector>

#include "base/global_descriptors_posix.h"
#include "base/process_util.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/zygote_host_linux.h"

template<typename Type>
struct DefaultSingletonTraits;

static const char kZygoteMagic[] = "ZYGOTE_OK";

class CONTENT_EXPORT ZygoteHostImpl : public content::ZygoteHost {
 public:
  // Returns the singleton instance.
  static ZygoteHostImpl* GetInstance();

  void Init(const std::string& sandbox_cmd);

  // Tries to start a process of type indicated by process_type.
  // Returns its pid on success, otherwise
  // base::kNullProcessHandle;
  pid_t ForkRequest(const std::vector<std::string>& command_line,
                    const base::GlobalDescriptors::Mapping& mapping,
                    const std::string& process_type);
  void EnsureProcessTerminated(pid_t process);

  // Get the termination status (and, optionally, the exit code) of
  // the process. |exit_code| is set to the exit code of the child
  // process. (|exit_code| may be NULL.)
  base::TerminationStatus GetTerminationStatus(base::ProcessHandle handle,
                                               int* exit_code);

  // These are the command codes used on the wire between the browser and the
  // zygote.
  enum {
    kCmdFork = 0,                  // Fork off a new renderer.
    kCmdReap = 1,                  // Reap a renderer child.
    kCmdGetTerminationStatus = 2,  // Check what happend to a child process.
    kCmdGetSandboxStatus = 3,      // Read a bitmask of kSandbox*
  };

  // ZygoteHost implementation:
  virtual pid_t GetPid() const OVERRIDE;
  virtual pid_t GetSandboxHelperPid() const OVERRIDE;
  virtual int GetSandboxStatus() const OVERRIDE;
  virtual void AdjustRendererOOMScore(base::ProcessHandle process_handle,
                                      int score) OVERRIDE;

 private:
  friend struct DefaultSingletonTraits<ZygoteHostImpl>;
  ZygoteHostImpl();
  virtual ~ZygoteHostImpl();

  ssize_t ReadReply(void* buf, size_t buflen);

  int control_fd_;  // the socket to the zygote
  // A lock protecting all communication with the zygote. This lock must be
  // acquired before sending a command and released after the result has been
  // received.
  base::Lock control_lock_;
  pid_t pid_;
  bool init_;
  bool using_suid_sandbox_;
  std::string sandbox_binary_;
  bool have_read_sandbox_status_word_;
  int sandbox_status_;
};

#endif  // CONTENT_BROWSER_ZYGOTE_HOST_IMPL_LINUX_H_