summaryrefslogtreecommitdiffstats
path: root/base/zygote_manager.h
blob: 5b94533f42dea89c6dfbd5f28b939c4119a16c5e (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2009 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 BASE_ZYGOTE_MANAGER_H_
#define BASE_ZYGOTE_MANAGER_H_

#include "build/build_config.h"

#if defined(OS_LINUX)

#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "base/pickle.h"
#include "base/time.h"
#include "base/process_util.h"  // for file_handle_mapping_vector

namespace base {

class ZygoteManager {
 public:
  // The normal way to get a ZygoteManager is via this singleton factory.
  static ZygoteManager* Get();

  ZygoteManager() :
      server_fd_(-1), client_fd_(-1), canary_fd_(-1), lockfd_(-1) {
  }

  ~ZygoteManager();

  // Measure round trip time.  Return true on success.
  // Only used during testing.
  bool Ping(base::TimeDelta* delta);

  // Start the zygote manager.
  // Called only once, but returns many times.
  // Returns once in original process and once in each spawned child.
  // In original process, returns NULL.
  // In child processes, returns the argv to use for the child.
  // In Chromium, called from ChromeMain().
  std::vector<std::string>* Start();

  // Like longjmp() and base::LaunchApp().
  // Ask the fork server to spawn a new process with
  // the given commandline and the given file descriptors.
  // Returns process id of copy, or -1 on failure.
  // In Chromium, called from base::ForkApp(), which is
  // called from BrowserRenderProcessHost::Init().
  pid_t LongFork(const std::vector<std::string>& argv,
    const file_handle_mapping_vector& fds_to_remap);

  // Tell ZygoteManager that we expect the given process to
  // exit on its own soon.  If it doesn't die within a few
  // seconds, kill it.  Does not block (unless pipe to server full).
  // In Chromium, called from ProcessWatcher::EnsureProcessTerminated().
  void EnsureProcessTerminated(pid_t childpid);

  // Open a file, or retrieve a previously cached file descriptor
  // for this file.  The files are opened for readonly access.
  // Caution: do not seek file descriptors returned
  // by this API, as all children share the same file objects, so
  // a seek on one is a seek on all.
  // Works even if the file is unlinked after the first call
  // (e.g. when an app is updated by the linux system autoupdater).
  // Returns file descriptor, or -1 for error.
  // In Chromium, called from MemoryMappedFile::MapFileToMemory().
  // Only allows openeing files named .pak in reasonable looking locations.
  int OpenFile(const std::string& filename);

 private:
  int UnpickleHeader(const Pickle& reply, void** iter);

  // Returns false on EOF
  // Sets *newargv to a new commandline if the remote side requested a fork.
  bool ReadAndHandleMessage(std::vector<std::string>** newargv);

  void PingHandler(const Pickle& request, void* iter, Pickle* reply,
                   std::vector<std::string>** newargv);

  bool LongForkHandler(const Pickle& request, void* iter, Pickle* reply,
                   std::vector<std::string>** newargv,
                   const int wire_fds[], int num_wire_fds);

  void EnsureProcessTerminatedHandler(const Pickle& request, void* iter);

  bool OpenFileHandler(const Pickle& request, void* iter, Pickle* reply,
      ::msghdr* msg);

  // The fd used by the server to receive requests
  int server_fd_;

  // The fd used by the clients to send requests
  int client_fd_;

  // fd used only to notify server of destruction.
  int canary_fd_;

  // Temporary file used only for locking.
  // Each client must do its own open for locking to work;
  // inherited file descriptors can't lock each other out.
  // FIXME: locking is lame.
  std::string lockfile_;
  int lockfd_;

  enum message_kind_t { ZMPING, ZMPINGED,
                ZMFORK, ZMFORKED,
                ZMREAP,
                ZMOPEN, ZMOPENED,
                ZMBAD };

  // See common/reserved_file_descriptors.h for who uses the reserved
  // file descriptors.  kReservedFds is one plus the highest fd mentioned there.
  // TODO(dkegel): move kReservedFds to reserved_file_descriptors.h
  static const int kReservedFds = 5;

  static const int kMAX_MSG_LEN = 2000;
  static const int kMAX_CMSG_LEN = 100;

  static const char kZMagic[];

  char msg_buf_[kMAX_MSG_LEN];
  char cmsg_buf_[kMAX_CMSG_LEN];

  // Where we remember file descriptors for already-opened files.
  // Both client and server maintain this table.
  // Client should check the table before requesting the
  // server to open a file, as it might have been already
  // opened before this client was forked.
  std::map<std::string, int> cached_fds_;
};

}  // namespace base

#endif  // defined(OS_LINUX)

#endif  // BASE_ZYGOTE_MANAGER_H_