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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2010 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_PROCESS_H_
#define _PASSENGER_PROCESS_H_
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <oxt/system_calls.hpp>
#include <oxt/backtrace.hpp>
#include <string>
#include <map>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <Session.h>
#include <Exceptions.h>
#include <Logging.h>
#include <Utils.h>
#include <Utils/IOUtils.h>
namespace Passenger {
using namespace std;
using namespace boost;
/**
* Represents a single application process, as spawned by SpawnManager
* or by ApplicationPool::Interface::get().
*
* @ingroup Support
*/
class Process {
public:
struct SocketInfo {
string address;
string type;
SocketInfo() {}
SocketInfo(const string &address, const string &type) {
this->address = address;
this->type = type;
}
};
typedef map<string, SocketInfo> SocketInfoMap;
private:
string appRoot;
pid_t pid;
int ownerPipe;
string detachKey;
string connectPassword;
string gupid;
SocketInfoMap serverSockets;
SocketInfo *mainServerSocket;
function<void ()> destructionCallback;
public:
/**
* Construct a new Process object.
*
* @param appRoot The application root of an application.
* This must be a valid directory, but the path does not have to be absolute.
* @param pid The process ID of this application process.
* @param ownerPipe The owner pipe of this application process.
* @param serverSockets All the server sockets that this process listens on.
* There must a server socket with the name 'main'.
* @param detachKey A detach key. Used by the ApplicationPool algorithm.
* @param connectPassword The password to use when connecting to this process.
* Must be valid ASCII.
* @param gupid A string which uniquely identifies this process.
* @param destructionCallback A callback to be called when this Process is destroyed.
* @throws ArgumentException If serverSockets has no socket named 'main'.
*/
Process(const string &appRoot, pid_t pid, int ownerPipe, const SocketInfoMap &serverSockets,
const string &detachKey, const string &connectPassword, const string &gupid,
const function<void ()> &destructionCallback = function<void ()>())
{
this->appRoot = appRoot;
this->pid = pid;
this->ownerPipe = ownerPipe;
this->serverSockets = serverSockets;
this->detachKey = detachKey;
this->connectPassword = connectPassword;
this->gupid = gupid;
this->destructionCallback = destructionCallback;
if (serverSockets.find("main") == serverSockets.end()) {
TRACE_POINT();
throw ArgumentException("There must be a server socket named 'main'.");
}
mainServerSocket = &this->serverSockets["main"];
P_TRACE(3, "Application process " << pid << " (" << this << "): created.");
}
virtual ~Process() {
TRACE_POINT();
SocketInfoMap::const_iterator it;
int ret;
if (ownerPipe != -1) {
safelyClose(ownerPipe, true);
}
for (it = serverSockets.begin(); it != serverSockets.end(); it++) {
const SocketInfo &info = it->second;
if (info.type == "unix") {
do {
ret = unlink(info.address.c_str());
} while (ret == -1 && errno == EINTR);
}
}
P_TRACE(3, "Application process " << pid << " (" << this << "): destroyed.");
if (destructionCallback) {
destructionCallback();
}
}
/**
* Returns the application root for this application process. See
* the constructor for information about the application root.
*/
string getAppRoot() const {
return appRoot;
}
/**
* Returns the process ID of this application process.
*/
pid_t getPid() const {
return pid;
}
/**
* Returns this process's detach key.
*/
string getDetachKey() const {
return detachKey;
}
/**
* Returns this process's connect password. This password is
* guaranteed to be valid ASCII.
*/
string getConnectPassword() const {
return connectPassword;
}
/**
* Returns this process's gupid. This is like a PID, but does not rotate
* and is even unique over multiple servers.
*/
string getGupid() const {
return gupid;
}
/**
* Returns a map containing all server sockets that this process
* listens on.
*/
const SocketInfoMap *getServerSockets() const {
return &serverSockets;
}
/**
* Request a new session from this application process by connecting to its
* main server socket. This session represents the life time of a single
* request/response pair, and can be used to send the request data to the
* application process, as well as receiving the response data.
*
* The use of connect() is demonstrated in the following example.
* @code
* // Request a new session from the process.
* SessionPtr session = process->newSession(...);
*
* // Send the request headers and request body data.
* session->sendHeaders(...);
* session->sendBodyBlock(...);
* // Done sending data, so we close the writer channel.
* session->shutdownWriter();
*
* // Now read the HTTP response.
* string responseData = readAllDataFromSocket(session->getReader());
* // Done reading data, so we close the reader channel.
* session->shutdownReader();
*
* // This session has now finished, so we close the session by resetting
* // the smart pointer to NULL (thereby destroying the Session object).
* session.reset();
*
* // We can connect to a Process multiple times. Just make sure
* // the previous session is closed.
* session = process->newSession(...);
* @endcode
*
* You <b>must</b> close a session when you no longer need it. If you
* call connect() without having properly closed a previous session,
* you might cause a deadlock because the application process may be
* waiting for you to close the previous session.
*
* @param closeCallback A function which will be called when the session has been closed.
* @param initiateNow Whether the session should be initiated immediately.
* If set to false then you must call <tt>initiate()</tt> on
* the session before it's usable.
* @return A smart pointer to a Session object, which represents the created session.
* @post result->initiated() == initiateNow
* @throws SystemException Something went wrong during session initiation.
* @throws IOException Something went wrong during session initiation.
* @throws boost::thread_interrupted
*/
SessionPtr newSession(const StandardSession::CloseCallback &closeCallback = StandardSession::CloseCallback(),
bool initiateNow = true)
{
SessionPtr session(new StandardSession(pid, closeCallback,
mainServerSocket->type, mainServerSocket->address,
detachKey, connectPassword, gupid));
if (initiateNow) {
session->initiate();
}
return session;
}
};
/** Convenient alias for Process smart pointer. */
typedef shared_ptr<Process> ProcessPtr;
} // namespace Passenger
#endif /* _PASSENGER_PROCESS_H_ */
|