| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 BASE_ZYGOTE_MANAGER_H_ |
| 6 #define BASE_ZYGOTE_MANAGER_H_ |
| 7 |
| 8 #include "build/build_config.h" |
| 9 |
| 10 #if defined(OS_LINUX) |
| 11 |
| 12 #include <sys/socket.h> |
| 13 #include <sys/types.h> |
| 14 #include <unistd.h> |
| 15 |
| 16 #include <map> |
| 17 #include <string> |
| 18 #include <utility> |
| 19 #include <vector> |
| 20 |
| 21 #include "base/pickle.h" |
| 22 #include "base/time.h" |
| 23 #include "base/process_util.h" // for file_handle_mapping_vector |
| 24 |
| 25 namespace base { |
| 26 |
| 27 class ZygoteManager { |
| 28 public: |
| 29 // The normal way to get a ZygoteManager is via this singleton factory. |
| 30 static ZygoteManager* Get(); |
| 31 |
| 32 ZygoteManager() : server_fd_(-1), client_fd_(-1), canary_fd_(-1), |
| 33 lockfd_(-1) { |
| 34 } |
| 35 |
| 36 ~ZygoteManager(); |
| 37 |
| 38 // Measure round trip time. Return true on success. |
| 39 // Only used during testing. |
| 40 bool Ping(base::TimeDelta* delta); |
| 41 |
| 42 // Start the zygote manager. |
| 43 // Called only once, but returns many times. |
| 44 // Returns once in original process and once in each spawned child. |
| 45 // In original process, returns NULL. |
| 46 // In child processes, returns the argv to use for the child. |
| 47 // In Chromium, called from ChromeMain(). |
| 48 std::vector<std::string>* Start(); |
| 49 |
| 50 // Like longjmp() and base::LaunchApp(). |
| 51 // Ask the fork server to spawn a new process with |
| 52 // the given commandline and the given file descriptors. |
| 53 // Returns process id of copy, or -1 on failure. |
| 54 // In Chromium, called from base::ForkApp(), which is |
| 55 // called from BrowserRenderProcessHost::Init(). |
| 56 pid_t LongFork(const std::vector<std::string>& argv, |
| 57 const file_handle_mapping_vector& fds_to_remap); |
| 58 |
| 59 // Tell ZygoteManager that we expect the given process to |
| 60 // exit on its own soon. If it doesn't die within a few |
| 61 // seconds, kill it. Does not block (unless pipe to server full). |
| 62 // In Chromium, called from ProcessWatcher::EnsureProcessTerminated(). |
| 63 void EnsureProcessTerminated(pid_t childpid); |
| 64 |
| 65 // Open a file, or retrieve a previously cached file descriptor |
| 66 // for this file. The files are opened for readonly access. |
| 67 // Caution: do not seek file descriptors returned |
| 68 // by this API, as all children share the same file objects, so |
| 69 // a seek on one is a seek on all. |
| 70 // Works even if the file is unlinked after the first call |
| 71 // (e.g. when an app is updated by the linux system autoupdater). |
| 72 // Returns file descriptor, or -1 for error. |
| 73 // In Chromium, called from MemoryMappedFile::MapFileToMemory(). |
| 74 // Only allows openeing files named .pak in reasonable looking locations. |
| 75 int OpenFile(const std::string& filename); |
| 76 |
| 77 private: |
| 78 int UnpickleHeader(const Pickle& reply, void** iter); |
| 79 |
| 80 // Returns false on EOF |
| 81 // Sets *newargv to a new commandline if the remote side requested a fork. |
| 82 bool ReadAndHandleMessage(std::vector<std::string>** newargv); |
| 83 |
| 84 void PingHandler(const Pickle& request, void* iter, Pickle* reply, |
| 85 std::vector<std::string>** newargv); |
| 86 |
| 87 bool LongForkHandler(const Pickle& request, void* iter, Pickle* reply, |
| 88 std::vector<std::string>** newargv, |
| 89 const int wire_fds[], int num_wire_fds); |
| 90 |
| 91 void EnsureProcessTerminatedHandler(const Pickle& request, void* iter); |
| 92 |
| 93 bool OpenFileHandler(const Pickle& request, void* iter, Pickle* reply, |
| 94 ::msghdr* msg); |
| 95 |
| 96 // The fd used by the server to receive requests |
| 97 int server_fd_; |
| 98 |
| 99 // The fd used by the clients to send requests |
| 100 int client_fd_; |
| 101 |
| 102 // fd used only to notify server of destruction. |
| 103 int canary_fd_; |
| 104 |
| 105 // Temporary file used only for locking. |
| 106 // Each client must do its own open for locking to work; |
| 107 // inherited file descriptors can't lock each other out. |
| 108 // FIXME: locking is lame. |
| 109 std::string lockfile_; |
| 110 int lockfd_; |
| 111 |
| 112 enum message_kind_t { ZMPING, ZMPINGED, |
| 113 ZMFORK, ZMFORKED, |
| 114 ZMREAP, |
| 115 ZMOPEN, ZMOPENED, |
| 116 ZMBAD }; |
| 117 |
| 118 // See common/reserved_file_descriptors.h for who uses the reserved |
| 119 // file descriptors. kReservedFds is one plus the highest fd mentioned there. |
| 120 // TODO(dkegel): move kReservedFds to reserved_file_descriptors.h |
| 121 static const int kReservedFds = 5; |
| 122 |
| 123 static const int kMAX_MSG_LEN = 2000; |
| 124 static const int kMAX_CMSG_LEN = 100; |
| 125 |
| 126 static const char kZMagic[]; |
| 127 |
| 128 char msg_buf_[kMAX_MSG_LEN]; |
| 129 char cmsg_buf_[kMAX_CMSG_LEN]; |
| 130 |
| 131 // Where we remember file descriptors for already-opened files. |
| 132 // Both client and server maintain this table. |
| 133 // Client should check the table before requesting the |
| 134 // server to open a file, as it might have been already |
| 135 // opened before this client was forked. |
| 136 std::map<std::string, int> cached_fds_; |
| 137 }; |
| 138 |
| 139 } // namespace base |
| 140 |
| 141 #endif // defined(OS_LINUX) |
| 142 |
| 143 #endif // BASE_ZYGOTE_MANAGER_H_ |
| OLD | NEW |