rlgwebd.js: become a real daemon.

The RLG-Web server now forks off, writes to a logfile, and reads from a
control socket.  Unfortunately it can't handle the problem of another
rlgwebd process already running.  Maybe a lockfile would help.
This commit is contained in:
John "Elwin" Edwards 2012-06-04 20:45:27 -07:00
parent d0e3b1da2f
commit bb17dd58a2

View file

@ -3,12 +3,16 @@
// If you can't quite trust node to find it on its own
var localModules = '/usr/local/lib/node_modules/';
var http = require('http');
var net = require('net');
var url = require('url');
var path = require('path');
var fs = require('fs');
var child_process = require('child_process');
var daemon = require(path.join(localModules, "daemon"));
// These first two files are NOT in the chroot.
var ctlsocket = "/var/local/rlgwebd/ctl";
var logfile = "/var/local/rlgwebd/log";
var chrootDir = "/var/dgl/";
var dropToUID = 501;
var dropToGID = 501;
@ -651,7 +655,7 @@ function webHandler(req, res) {
function shutdown () {
httpServer.close();
httpServer.removeAllListeners('request');
process.stdin.removeAllListeners('data');
ctlServer.close();
tslog("Shutting down...");
process.exit();
}
@ -684,13 +688,21 @@ if (process.getuid() != 0) {
tslog("Not running as root, cannot chroot.");
process.exit(1);
}
/* Open the control socket before chrooting where it can't be found */
var ctlServer = net.createServer(function (sock) {
sock.on('data', conHandler);
});
ctlServer.listen(ctlsocket, function () {
/* fork off and die */
try {
process.chdir(chrootDir);
daemon.start(logfile);
}
catch (err) {
tslog("Cannot enter %s: %s", chrootDir, err);
tslog("Daemonization failed: %s", err);
process.exit(1);
}
/* chroot and drop permissions. daemon.chroot() does chdir() itself. */
try {
daemon.chroot(chrootDir);
}
@ -707,9 +719,8 @@ catch (err) {
tslog("Could not drop permissions: %s", err);
process.exit(1);
}
process.stdin.on('data', conHandler);
process.stdin.resume();
var httpServer = http.createServer(webHandler);
httpServer.listen(8080, "127.0.0.1");
tslog('rlgwebd running at http://127.0.0.1:8080/');
});