# HG changeset patch # User Michael Schloh von Bennewitz # Date 1407956415 -7200 # Node ID 42e83b3431d98cabb18107f3ecaaedc5a8c1e110 # Parent eff35b87fb5637e0aa73bfc7708fa87b3d07e74e Include signal handler and complete most of service daemon logic. diff -r eff35b87fb56 -r 42e83b3431d9 src/app.js --- a/src/app.js Wed Aug 13 20:56:48 2014 +0200 +++ b/src/app.js Wed Aug 13 21:00:15 2014 +0200 @@ -45,6 +45,16 @@ var nameinst = require('native-dns'); +// install POSIX signal handlers +process.on('SIGUSR2', function() { + console.log('SIGUSR2: Dumping mDNSGw entries at', Date()); + rediscli.hgetall('hostnames', function (error, object) {console.dir(object);}); +}); +process.on('SIGHUP', function() { + console.log('SIGHUP: Cleared all database entries at', Date()); + cleardb(); +}); + // instantiate a new redis client // http://www.rediscookbook.org/ var rediscli = redisdat.createClient(); @@ -53,17 +63,20 @@ }); // clear mDNS service keys -rediscli.del('hostnames'); -// this is not working unfortunately for the loop -rediscli.keys('*', function (error, replies) { - replies.forEach(function (reply, ident) { - rediscli.del(reply, function (error, value) { - if (error) throw(error); +function cleardb () { + rediscli.del('hostnames'); + // this is not working unfortunately for the loop + rediscli.keys('*', function (error, replies) { + replies.forEach(function (reply, ident) { + rediscli.del(reply, function (error, value) { + if (error) throw(error); + }); }); }); -}); +} // scan all advertised mDNS service types +cleardb(); // clear old data first var browsall = mdnsinst.browseThemAll(); browsall.on('serviceUp', function(service) { // iterate through hosts and watch accordingly @@ -88,9 +101,13 @@ rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses); }); browserv.on('serviceDown', function(service) { - //console.log('service down: ', service); + console.log('service down: ', service); //FIXME: still need to selectively remove hosts }); + browserv.on('serviceChanged', function(service) { + console.log('service changed: ', service); + //FIXME: still need to selectively update hosts + }); browserv.start(); } }); @@ -147,7 +164,36 @@ console.log('DNS problem: ', err.stack); }); -nameserv.serve(15353); +//// debug process user +//console.log('Start.'); +//console.log(process.env.USER); +//console.log(process.env.SUDO_USER); +//console.log('Done.'); +// +// <1024 must run privileged +var nudpport = 53; // default DNS +if (nudpport < 1024 && process.getuid() !== 0) { + //console.log('Serving on port <1024 from an unprivileged user.\nChange to root if using a privileged port number.') + throw new Error('Serving on port <1024 from an unprivileged user.') +} + +// start the DNS process +nameserv.serve(nudpport, function () { + try { + console.log('Starting mDNSGw on', Date()); + process.stdout.write('Old UID: ' + process.getuid() + ', Old GID: ' + process.getgid() + '... '); + process.umask('0644'); + process.setgid('daemon'); + if (process.env.SUDO_USER) + process.setuid(process.env.SUDO_USER); + else + process.setuid('daemon'); + console.log('New UID: ' + process.getuid() + ', New GID: ' + process.getgid()); + } catch (err) { + console.log('Cowardly refusing to keep the process alive as root.'); + process.exit(1); + } +}); //// debug print all key and value database entries //rediscli.hgetall('hostnames', function (error, object) {console.dir(object);});