/usr/share/passenger/helper-scripts/node-loader.js is in ruby-passenger 4.0.53-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* Phusion Passenger - https://www.phusionpassenger.com/
* Copyright (c) 2010-2014 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.
*/
module.paths.unshift(__dirname + "/../node_lib");
var EventEmitter = require('events').EventEmitter;
var os = require('os');
var fs = require('fs');
var net = require('net');
var http = require('http');
var util = require('util');
var LineReader = require('phusion_passenger/line_reader').LineReader;
module.isApplicationLoader = true; // https://groups.google.com/forum/#!topic/compoundjs/4txxkNtROQg
GLOBAL.PhusionPassenger = exports.PhusionPassenger = new EventEmitter();
var stdinReader = new LineReader(process.stdin);
beginHandshake();
readInitializationHeader();
function beginHandshake() {
process.stdout.write("!> I have control 1.0\n");
}
function readInitializationHeader() {
stdinReader.readLine(function(line) {
if (line != "You have control 1.0\n") {
console.error('Invalid initialization header');
process.exit(1);
} else {
readOptions();
}
});
}
function readOptions() {
var options = {};
function readNextOption() {
stdinReader.readLine(function(line) {
if (line == "\n") {
setupEnvironment(options);
} else if (line == "") {
console.error("End of stream encountered while reading initialization options");
process.exit(1);
} else {
var matches = line.replace(/\n/, '').match(/(.*?) *: *(.*)/);
options[matches[1]] = matches[2];
readNextOption();
}
});
}
readNextOption();
}
function setupEnvironment(options) {
PhusionPassenger.options = options;
PhusionPassenger.configure = configure;
PhusionPassenger._appInstalled = false;
process.title = 'Passenger NodeApp: ' + options.app_root;
http.Server.prototype.originalListen = http.Server.prototype.listen;
http.Server.prototype.listen = installServer;
stdinReader.close();
stdinReader = undefined;
process.stdin.on('end', shutdown);
process.stdin.resume();
loadApplication();
}
/**
* PhusionPassenger.configure(options)
*
* Configures Phusion Passenger's behavior inside this Node application.
*
* Options:
* autoInstall (boolean, default true)
* Whether to install the first HttpServer object for which listen() is called,
* as the Phusion Passenger request handler.
*/
function configure(_options) {
var options = {
autoInstall: true
};
for (var key in _options) {
options[key] = _options[key];
}
if (!options.autoInstall) {
http.Server.prototype.listen = listenAndMaybeInstall;
}
}
function loadApplication() {
var appRoot = PhusionPassenger.options.app_root || process.cwd();
var startupFile = PhusionPassenger.options.startup_file || 'app.js';
require(appRoot + '/' + startupFile);
}
function extractCallback(args) {
if (args.length > 1 && typeof(args[args.length - 1]) == 'function') {
return args[args.length - 1];
}
}
function generateServerSocketPath() {
var options = PhusionPassenger.options;
var socketDir, socketPrefix, socketSuffix;
if (options.generation_dir) {
socketDir = options.generation_dir + "/backends";
socketPrefix = "node";
} else {
socketDir = os.tmpdir().replace(/\/$/, '');
socketPrefix = "PsgNodeApp";
}
socketSuffix = ((Math.random() * 0xFFFFFFFF) & 0xFFFFFFF);
var result = socketDir + "/" + socketPrefix + "." + socketSuffix.toString(36);
var UNIX_PATH_MAX = options.UNIX_PATH_MAX || 100;
return result.substr(0, UNIX_PATH_MAX);
}
function addListenerAtBeginning(emitter, event, callback) {
var listeners = emitter.listeners(event);
var i;
emitter.removeAllListeners(event);
emitter.on(event, callback);
for (i = 0; i < listeners.length; i++) {
emitter.on(event, listeners[i]);
}
}
function installServer() {
var server = this;
if (!PhusionPassenger._appInstalled) {
PhusionPassenger._appInstalled = true;
PhusionPassenger._server = server;
// Ensure that req.connection.remoteAddress and remotePort return something
// instead of undefined. Apps like Etherpad expect it.
// See https://github.com/phusion/passenger/issues/1224
addListenerAtBeginning(server, 'request', function(req) {
req.connection.__defineGetter__('remoteAddress', function() {
return '127.0.0.1';
});
req.connection.__defineGetter__('remotePort', function() {
return 0;
});
});
var listenTries = 0;
doListen(extractCallback(arguments));
function doListen(callback) {
function errorHandler(error) {
if (error.errno == 'EADDRINUSE') {
if (listenTries == 100) {
server.emit('error', new Error(
'Phusion Passenger could not find suitable socket address to bind on'));
} else {
// Try again with another socket path.
listenTries++;
doListen(callback);
}
} else {
server.emit('error', error);
}
}
var socketPath = PhusionPassenger.options.socket_path = generateServerSocketPath();
server.once('error', errorHandler);
server.originalListen(socketPath, function() {
server.removeListener('error', errorHandler);
doneListening(callback);
process.nextTick(installControlServer);
});
}
function doneListening(callback) {
if (callback) {
server.once('listening', callback);
}
server.emit('listening');
}
return server;
} else {
throw new Error("http.Server.listen() was called more than once, which " +
"is not allowed because Phusion Passenger is in auto-install mode. " +
"This means that the first http.Server object for which listen() is called, " +
"is automatically installed as the Phusion Passenger request handler. " +
"If you want to create and listen on multiple http.Server object then " +
"you should disable auto-install mode. Please read " +
"http://stackoverflow.com/questions/20645231/phusion-passenger-error-http-server-listen-was-called-more-than-once/20645549");
}
}
function listenAndMaybeInstall(port) {
if (port === 'passenger' || port == '/passenger') {
if (!PhusionPassenger._appInstalled) {
return installServer.apply(this, arguments);
} else {
throw new Error("You may only call listen('passenger') once. Please read http://stackoverflow.com/questions/20645231/phusion-passenger-error-http-server-listen-was-called-more-than-once/20645549");
}
} else {
return this.originalListen.apply(this, arguments);
}
}
function installControlServer() {
var server = net.createServer(onNewClient);
var listenTries = 0;
doListen();
function onNewClient(client) {
client.on('error', function(e) {
console.trace(e);
});
readNextMessageHeader(client);
}
function readNextMessageHeader(client) {
client.once('readable', onReadable);
function onReadable() {
var size = client.read(2);
if (size !== null) {
readNextMessageBody(client, size.readUInt16BE(0));
} else {
client.once('readable', onReadable);
}
}
}
function readNextMessageBody(client, size) {
client.once('readable', onReadable);
function onReadable() {
var body = client.read(size);
if (body !== null) {
var args = body.toString('binary').split("\0");
args.pop();
handleMessage(client, args);
} else {
client.once('readable', onReadable);
}
}
}
function handleMessage(client, args) {
if (args[0] == 'abort_long_running_connections') {
shutdown();
client.end();
} else {
console.error("Invalid control message: " + util.inspect(args));
readNextMessageHeader(client);
}
}
function doListen() {
function errorHandler(error) {
if (error.errno == 'EADDRINUSE') {
if (listenTries == 100) {
server.emit('error', new Error(
'Phusion Passenger could not find suitable socket address to bind the control server on'));
} else {
// Try again with another socket path.
listenTries++;
doListen();
}
} else {
server.emit('error', error);
}
}
var socketPath = PhusionPassenger.options.control_socket_path =
generateServerSocketPath();
server.once('error', errorHandler);
server.listen(socketPath, function() {
server.removeListener('error', errorHandler);
process.nextTick(finalizeStartup);
});
}
}
function finalizeStartup() {
process.stdout.write("!> Ready\n");
process.stdout.write("!> socket: main;unix:" +
PhusionPassenger._server.address() +
";http_session;0\n");
if (process.env['_PASSENGER_NODE_CONTROL_SERVER']) {
process.stdout.write("!> socket: control;unix:" +
PhusionPassenger.options.control_socket_path +
";control;0\n");
}
process.stdout.write("!> \n");
}
function shutdown() {
if (PhusionPassenger.shutting_down) {
return;
}
PhusionPassenger.shutting_down = true;
try {
fs.unlinkSync(PhusionPassenger.options.socket_path);
} catch (e) {
// Ignore error.
}
try {
fs.unlinkSync(PhusionPassenger.options.control_socket_path);
} catch (e) {
// Ignore error.
}
if (PhusionPassenger.listeners('exit').length == 0) {
process.exit(0);
} else {
PhusionPassenger.emit('exit');
}
}
|