Include signal handler and complete most of service daemon logic.

Wed, 13 Aug 2014 21:00:15 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 13 Aug 2014 21:00:15 +0200
changeset 3
42e83b3431d9
parent 2
eff35b87fb56
child 4
3de96d11e417

Include signal handler and complete most of service daemon logic.

src/app.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/app.js	Wed Aug 13 20:56:48 2014 +0200
     1.2 +++ b/src/app.js	Wed Aug 13 21:00:15 2014 +0200
     1.3 @@ -45,6 +45,16 @@
     1.4  var nameinst = require('native-dns');
     1.5  
     1.6  
     1.7 +// install POSIX signal handlers
     1.8 +process.on('SIGUSR2', function() {
     1.9 +  console.log('SIGUSR2: Dumping mDNSGw entries at', Date());
    1.10 +  rediscli.hgetall('hostnames', function (error, object) {console.dir(object);});
    1.11 +});
    1.12 +process.on('SIGHUP', function() {
    1.13 +  console.log('SIGHUP: Cleared all database entries at', Date());
    1.14 +  cleardb();
    1.15 +});
    1.16 +
    1.17  // instantiate a new redis client
    1.18  // http://www.rediscookbook.org/
    1.19  var rediscli = redisdat.createClient();
    1.20 @@ -53,17 +63,20 @@
    1.21  });
    1.22  
    1.23  // clear mDNS service keys
    1.24 -rediscli.del('hostnames');
    1.25 -// this is not working unfortunately for the loop
    1.26 -rediscli.keys('*', function (error, replies) {
    1.27 -  replies.forEach(function (reply, ident) {
    1.28 -    rediscli.del(reply, function (error, value) {
    1.29 -      if (error) throw(error);
    1.30 +function cleardb () {
    1.31 +  rediscli.del('hostnames');
    1.32 +  // this is not working unfortunately for the loop
    1.33 +  rediscli.keys('*', function (error, replies) {
    1.34 +    replies.forEach(function (reply, ident) {
    1.35 +      rediscli.del(reply, function (error, value) {
    1.36 +        if (error) throw(error);
    1.37 +      });
    1.38      });
    1.39    });
    1.40 -});
    1.41 +}
    1.42  
    1.43  // scan all advertised mDNS service types
    1.44 +cleardb(); // clear old data first
    1.45  var browsall = mdnsinst.browseThemAll();
    1.46  browsall.on('serviceUp', function(service) {
    1.47      // iterate through hosts and watch accordingly
    1.48 @@ -88,9 +101,13 @@
    1.49          rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses);
    1.50        });
    1.51        browserv.on('serviceDown', function(service) {
    1.52 -        //console.log('service down: ', service);
    1.53 +        console.log('service down: ', service);
    1.54          //FIXME: still need to selectively remove hosts
    1.55        });
    1.56 +      browserv.on('serviceChanged', function(service) {
    1.57 +        console.log('service changed: ', service);
    1.58 +        //FIXME: still need to selectively update hosts
    1.59 +      });
    1.60        browserv.start();
    1.61      }
    1.62  });
    1.63 @@ -147,7 +164,36 @@
    1.64    console.log('DNS problem: ', err.stack);
    1.65  });
    1.66  
    1.67 -nameserv.serve(15353);
    1.68 +//// debug process user
    1.69 +//console.log('Start.');
    1.70 +//console.log(process.env.USER);
    1.71 +//console.log(process.env.SUDO_USER);
    1.72 +//console.log('Done.');
    1.73 +//
    1.74 +// <1024 must run privileged
    1.75 +var nudpport = 53; // default DNS
    1.76 +if (nudpport < 1024 && process.getuid() !== 0) {
    1.77 +  //console.log('Serving on port <1024 from an unprivileged user.\nChange to root if using a privileged port number.')
    1.78 +  throw new Error('Serving on port <1024 from an unprivileged user.')
    1.79 +}
    1.80 +
    1.81 +// start the DNS process
    1.82 +nameserv.serve(nudpport, function () {
    1.83 +  try {
    1.84 +    console.log('Starting mDNSGw on', Date());
    1.85 +    process.stdout.write('Old UID: ' + process.getuid() + ', Old GID: ' + process.getgid() + '... ');
    1.86 +    process.umask('0644');
    1.87 +    process.setgid('daemon');
    1.88 +    if (process.env.SUDO_USER)
    1.89 +      process.setuid(process.env.SUDO_USER);
    1.90 +    else
    1.91 +      process.setuid('daemon');
    1.92 +    console.log('New UID: ' + process.getuid() + ', New GID: ' + process.getgid());
    1.93 +  } catch (err) {
    1.94 +    console.log('Cowardly refusing to keep the process alive as root.');
    1.95 +    process.exit(1);
    1.96 +  }
    1.97 +});
    1.98  
    1.99  //// debug print all key and value database entries
   1.100  //rediscli.hgetall('hostnames', function (error, object) {console.dir(object);});

mercurial