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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2008, 2009 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_SPAWN_OPTIONS_H_
#define _PASSENGER_SPAWN_OPTIONS_H_
#include <string>
#include <vector>
#include "StringListCreator.h"
namespace Passenger {
using namespace std;
/**
* This struct encapsulates information for ApplicationPool::get() and for
* SpawnManager::spawn(), such as which application is to be spawned.
*
* <h2>Notes on privilege lowering support</h2>
*
* If <tt>lowerPrivilege</tt> is true, then it will be attempt to
* switch the spawned application instance to the user who owns the
* application's <tt>config/environment.rb</tt>, and to the default
* group of that user.
*
* If that user doesn't exist on the system, or if that user is root,
* then it will be attempted to switch to the username given by
* <tt>lowestUser</tt> (and to the default group of that user).
* If <tt>lowestUser</tt> doesn't exist either, or if switching user failed
* (because the spawn server process does not have the privilege to do so),
* then the application will be spawned anyway, without reporting an error.
*
* It goes without saying that lowering privilege is only possible if
* the spawn server is running as root (and thus, by induction, that
* Phusion Passenger and Apache's control process are also running as root).
* Note that if Apache is listening on port 80, then its control process must
* be running as root. See "doc/Security of user switching.txt" for
* a detailed explanation.
*/
struct PoolOptions {
/**
* The root directory of the application to spawn. In case of a Ruby on Rails
* application, this is the folder that contains 'app/', 'public/', 'config/',
* etc. This must be a valid directory, but the path does not have to be absolute.
*/
string appRoot;
/** Whether to lower the application's privileges. */
bool lowerPrivilege;
/**
* The user to fallback to if lowering privilege fails.
*/
string lowestUser;
/**
* The RAILS_ENV/RACK_ENV environment that should be used. May not be an
* empty string.
*/
string environment;
/**
* The spawn method to use. Either "smart" or "conservative". See the Ruby
* class <tt>SpawnManager</tt> for details.
*/
string spawnMethod;
/** The application type. Either "rails", "rack" or "wsgi". */
string appType;
/**
* The idle timeout, in seconds, of Rails framework spawners.
* A timeout of 0 means that the framework spawner should never idle timeout. A timeout
* of -1 means that the default timeout value should be used.
*
* For more details about Rails framework spawners, please
* read the documentation on the Railz::FrameworkSpawner
* Ruby class.
*/
long frameworkSpawnerTimeout;
/**
* The idle timeout, in seconds, of Rails application spawners.
* A timeout of 0 means that the application spawner should never idle timeout. A timeout
* of -1 means that the default timeout value should be used.
*
* For more details about Rails application spawners, please
* read the documentation on the Railz::ApplicationSpawner
* Ruby class.
*/
long appSpawnerTimeout;
/**
* The maximum number of requests that the spawned application may process
* before exiting. A value of 0 means unlimited.
*/
unsigned long maxRequests;
/**
* The maximum amount of memory (in MB) the spawned application may use.
* A value of 0 means unlimited.
*/
unsigned long memoryLimit;
/**
* Whether to use a global queue instead of a per-backend process
* queue. This option is only used by ApplicationPool::get().
*
* If enabled, when all backend processes are active, get() will
* wait until there's at least one backend process that's idle, instead
* of queuing the request into a random process's private queue.
* This is especially useful if a website has one or more long-running
* requests.
*/
bool useGlobalQueue;
/**
* A throttling rate for file stats. When set to a non-zero value N,
* restart.txt and other files which are usually stat()ted on every
* ApplicationPool::get() call will be stat()ed at most every N seconds.
*/
unsigned long statThrottleRate;
/**
* The directory which contains restart.txt and always_restart.txt.
* An empty string means that the default directory should be used.
*/
string restartDir;
/**
* If a new backend process is started, then the getItems() method
* on this object will be called, which is to return environment
* variables that should be passed to the newly spawned backend process.
* Odd indices in the resulting array contain keys, even indices contain
* the value for the key in the previous index.
*
* May be set to NULL.
*
* @invariant environmentVariables.size() is an even number.
*/
StringListCreatorPtr environmentVariables;
/**
* The base URI on which the application runs. If the application is
* running on the root URI, then this value must be "/".
*
* @invariant baseURI != ""
*/
string baseURI;
/*********************************/
/**
* Creates a new PoolOptions object with the default values filled in.
* One must still set appRoot manually, after having used this constructor.
*/
PoolOptions() {
lowerPrivilege = true;
lowestUser = "nobody";
environment = "production";
spawnMethod = "smart";
appType = "rails";
frameworkSpawnerTimeout = -1;
appSpawnerTimeout = -1;
maxRequests = 0;
memoryLimit = 0;
useGlobalQueue = false;
statThrottleRate = 0;
baseURI = "/";
}
/**
* Creates a new PoolOptions object with the given values.
*/
PoolOptions(const string &appRoot,
bool lowerPrivilege = true,
const string &lowestUser = "nobody",
const string &environment = "production",
const string &spawnMethod = "smart",
const string &appType = "rails",
long frameworkSpawnerTimeout = -1,
long appSpawnerTimeout = -1,
unsigned long maxRequests = 0,
unsigned long memoryLimit = 0,
bool useGlobalQueue = false,
unsigned long statThrottleRate = 0,
const string &restartDir = "",
const string &baseURI = "/"
) {
this->appRoot = appRoot;
this->lowerPrivilege = lowerPrivilege;
this->lowestUser = lowestUser;
this->environment = environment;
this->spawnMethod = spawnMethod;
this->appType = appType;
this->frameworkSpawnerTimeout = frameworkSpawnerTimeout;
this->appSpawnerTimeout = appSpawnerTimeout;
this->maxRequests = maxRequests;
this->memoryLimit = memoryLimit;
this->useGlobalQueue = useGlobalQueue;
this->statThrottleRate = statThrottleRate;
this->restartDir = restartDir;
this->baseURI = baseURI;
}
/**
* Creates a new PoolOptions object from the given string vector.
* This vector contains information that's written to by toVector().
*
* For example:
* @code
* PoolOptions options(...);
* vector<string> vec;
*
* vec.push_back("my");
* vec.push_back("data");
* options.toVector(vec); // PoolOptions information will start at index 2.
*
* PoolOptions copy(vec, 2);
* @endcode
*
* @param vec The vector containing spawn options information.
* @param startIndex The index in vec at which the information starts.
*/
PoolOptions(const vector<string> &vec, unsigned int startIndex = 0) {
appRoot = vec[startIndex + 1];
lowerPrivilege = vec[startIndex + 3] == "true";
lowestUser = vec[startIndex + 5];
environment = vec[startIndex + 7];
spawnMethod = vec[startIndex + 9];
appType = vec[startIndex + 11];
frameworkSpawnerTimeout = atol(vec[startIndex + 13]);
appSpawnerTimeout = atol(vec[startIndex + 15]);
maxRequests = atol(vec[startIndex + 17]);
memoryLimit = atol(vec[startIndex + 19]);
useGlobalQueue = vec[startIndex + 21] == "true";
statThrottleRate = atol(vec[startIndex + 23]);
restartDir = vec[startIndex + 25];
baseURI = vec[startIndex + 27];
if (vec.size() > startIndex + 29) {
environmentVariables = ptr(new SimpleStringListCreator(vec[startIndex + 29]));
}
}
/**
* Append the information in this PoolOptions object to the given
* string vector. The resulting array could, for example, be used
* as a message to be sent to the spawn server.
*
* @param vec The vector to store the information in.
* @param storeEnvVars Whether to store environment variable information into vec as well.
* @throws Anything thrown by environmentVariables->getItems().
*/
void toVector(vector<string> &vec, bool storeEnvVars = true) const {
if (vec.capacity() < vec.size() + 30) {
vec.reserve(vec.size() + 30);
}
appendKeyValue (vec, "app_root", appRoot);
appendKeyValue (vec, "lower_privilege", lowerPrivilege ? "true" : "false");
appendKeyValue (vec, "lowest_user", lowestUser);
appendKeyValue (vec, "environment", environment);
appendKeyValue (vec, "spawn_method", spawnMethod);
appendKeyValue (vec, "app_type", appType);
appendKeyValue2(vec, "framework_spawner_timeout", frameworkSpawnerTimeout);
appendKeyValue2(vec, "app_spawner_timeout", appSpawnerTimeout);
appendKeyValue3(vec, "max_requests", maxRequests);
appendKeyValue3(vec, "memory_limit", memoryLimit);
appendKeyValue (vec, "use_global_queue", useGlobalQueue ? "true" : "false");
appendKeyValue3(vec, "stat_throttle_rate", statThrottleRate);
appendKeyValue (vec, "restart_dir", restartDir);
appendKeyValue (vec, "base_uri", baseURI);
if (storeEnvVars) {
vec.push_back("environment_variables");
vec.push_back(serializeEnvironmentVariables());
}
}
/**
* Serializes the items in environmentVariables into a string, which
* can be used to create a SimpleStringListCreator object.
*
* @throws Anything thrown by environmentVariables->getItems().
*/
string serializeEnvironmentVariables() const {
vector<string>::const_iterator it, end;
string result;
if (environmentVariables) {
result.reserve(1024);
StringListPtr items = environmentVariables->getItems();
end = items->end();
for (it = items->begin(); it != end; it++) {
result.append(*it);
result.append(1, '\0');
it++;
result.append(*it);
result.append(1, '\0');
}
}
return Base64::encode(result);
}
private:
static inline void
appendKeyValue(vector<string> &vec, const char *key, const string &value) {
vec.push_back(key);
vec.push_back(const_cast<string &>(value));
}
static inline void
appendKeyValue(vector<string> &vec, const char *key, const char *value) {
vec.push_back(key);
vec.push_back(value);
}
static inline void
appendKeyValue2(vector<string> &vec, const char *key, long value) {
vec.push_back(key);
vec.push_back(toString(value));
}
static inline void
appendKeyValue3(vector<string> &vec, const char *key, unsigned long value) {
vec.push_back(key);
vec.push_back(toString(value));
}
};
} // namespace Passenger
#endif /* _PASSENGER_SPAWN_OPTIONS_H_ */
|