comparison rlgwebd.js @ 139:dcd07c1d846a

Replace the daemon module with posix. The daemon module doesn't include chroot() anymore, so a replacement is needed. Detaching a daemon keeps getting harder to do in Node, so some setup has been moved into a shell script.
author John "Elwin" Edwards
date Sat, 20 Jul 2013 12:23:53 -0700
parents 823e878e5840
children 1a156a7746a7
comparison
equal deleted inserted replaced
138:144595e50376 139:dcd07c1d846a
7 var url = require('url'); 7 var url = require('url');
8 var path = require('path'); 8 var path = require('path');
9 var fs = require('fs'); 9 var fs = require('fs');
10 var events = require('events'); 10 var events = require('events');
11 var child_process = require('child_process'); 11 var child_process = require('child_process');
12 var daemon = require(path.join(localModules, "daemon")); 12 var posix = require(path.join(localModules, "posix"));
13 var pty = require(path.join(localModules, "pty.js")); 13 var pty = require(path.join(localModules, "pty.js"));
14 var WebSocketServer = require(path.join(localModules, "websocket")).server; 14 var WebSocketServer = require(path.join(localModules, "websocket")).server;
15 15
16 /* Configuration variables */ 16 /* Configuration variables */
17 // These first two files are NOT in the chroot. 17 // The first file is NOT in the chroot.
18 var ctlsocket = "/var/local/rlgwebd/ctl"; 18 var ctlsocket = "/var/local/rlgwebd/ctl";
19 var logfile = "/var/local/rlgwebd/log";
20 var httpPort = 8080; 19 var httpPort = 8080;
21 var chrootDir = "/var/dgl/"; 20 var chrootDir = "/var/dgl/";
22 var dropToUID = 501; 21 var dropToUID = 501;
23 var dropToGID = 501; 22 var dropToGID = 501;
24 var serveStaticRoot = "/var/www/"; // inside the chroot 23 var serveStaticRoot = "/var/www/"; // inside the chroot
909 if (err) { 908 if (err) {
910 sendError(res, 7); 909 sendError(res, 7);
911 return; 910 return;
912 } 911 }
913 var pid = parseInt(fdata.split('\n')[0], 10); 912 var pid = parseInt(fdata.split('\n')[0], 10);
914 process.kill(pid, 'SIGHUP'); 913 try {
914 process.kill(pid, 'SIGHUP');
915 }
916 catch (err) {
917 /* If the PID is invalid, the lockfile is stale. */
918 if (err.code == "ESRCH") {
919 var nodere = RegExp("^" + pname + ":node:");
920 if (fname.match(nodere)) {
921 fs.unlink(fullfile);
922 }
923 }
924 }
915 /* The response doesn't mean that the game is gone. The only way 925 /* The response doesn't mean that the game is gone. The only way
916 * to make sure a dgamelaunch-supervised game is over would be to 926 * to make sure a dgamelaunch-supervised game is over would be to
917 * poll fname until it disappears. */ 927 * poll fname until it disappears. */
918 res.writeHead(200, {'Content-Type': 'application/json'}); 928 res.writeHead(200, {'Content-Type': 'application/json'});
919 res.write(JSON.stringify({"t": "q"})); 929 res.write(JSON.stringify({"t": "q"}));
1394 /* Open the control socket before chrooting where it can't be found */ 1404 /* Open the control socket before chrooting where it can't be found */
1395 var ctlServer = net.createServer(function (sock) { 1405 var ctlServer = net.createServer(function (sock) {
1396 sock.on('data', conHandler); 1406 sock.on('data', conHandler);
1397 }); 1407 });
1398 ctlServer.listen(ctlsocket, function () { 1408 ctlServer.listen(ctlsocket, function () {
1399 /* fork off and die */ 1409 /* rlgwebd.js now assumes that it has been started by the rlgwebd shell
1410 * script, or some other method that detaches it and sets up stdio. */
1411 /* chroot and drop permissions. posix.chroot() does chdir() itself. */
1400 try { 1412 try {
1401 daemon.start(logfile); 1413 posix.chroot(chrootDir);
1402 }
1403 catch (err) {
1404 tslog("Daemonization failed: %s", err);
1405 process.exit(1);
1406 }
1407 /* chroot and drop permissions. daemon.chroot() does chdir() itself. */
1408 try {
1409 daemon.chroot(chrootDir);
1410 } 1414 }
1411 catch (err) { 1415 catch (err) {
1412 tslog("chroot to %s failed: %s", chrootDir, err); 1416 tslog("chroot to %s failed: %s", chrootDir, err);
1413 process.exit(1); 1417 process.exit(1);
1414 } 1418 }