Permalink
Browse files

hardening streams

  • Loading branch information...
1 parent c9b9613 commit 0c0f2f3a3aa04dbbd70ba67602884f70e97a58c2 @spion committed Nov 8, 2013
Showing with 20 additions and 9 deletions.
  1. +8 −1 bin/nacd.js
  2. +12 −8 lib/daemon/app.js
View
@@ -36,13 +36,18 @@ async.parallel([
function(cb) { outlog.once('open', function() { cb(); }) },
], runDaemon);
+function clientError(e) {
+ var str = e ? (e.stack ? e.stack : e) : 'description not available';
+ console.error('Client socket/dnode error:', str);
+}
+
function runDaemon() {
if (~process.argv.indexOf('--daemon'))
require('daemon')({
stdout: outlog,
stderr: errlog
- });
+ });
Daemon.create(function (err, daemon) {
@@ -59,9 +64,11 @@ function runDaemon() {
});
function serveClient(client) {
+ client.on('error', clientError);
usc.getCredentials(client, function (err, cred) {
if (err) return console.log(err);
var d = dnode(Daemon.interface(cred.uid, daemon));
+ d.on('error', clientError);
client.pipe(d).pipe(client);
});
}
View
@@ -175,10 +175,11 @@ App.prototype._runProcess = function (done) {
this._onExit(err)
}.bind(this));
proc.on('exit', this._onExit.bind(this));
+ proc.stdout.on('error', function() {});
+ proc.stderr.on('error', function() {});
proc.stdout.pipe(this.logger.stream('stdout'));
proc.stderr.pipe(this.logger.stream('stderr'));
-
-
+
var self = this;
// Hack: the node.js api docs don't say when
@@ -361,11 +362,15 @@ App.prototype.run = function run(script) {
scriptp.stdout.pipe(output);
scriptp.stderr.pipe(output);
-
- scriptp.on('error', function (e) {
+
+ var scriptError = function scriptError(e) {
var msg = script + args.join(' ') + " > error " + e.message;
this.logger.log("script", msg);
- }.bind(this));
+ }.bind(this);
+
+ scriptp.on('error', scriptError);
+ scriptp.stdout.on('error', scriptError);
+ scriptp.stderr.on('error', scriptError);
scriptp.on('exit', function (code) {
setImmediate(this.logger.log.bind(this.logger, "script",
@@ -380,14 +385,13 @@ App.prototype.run = function run(script) {
output.pipe(clientStream);
output.pipe(this.logger.stream('script'));
-
- var dataend = function (datacb, endcb) {
+ function dataend(datacb, endcb) {
clientStream.on('data', function (d) {
datacb(d.toString());
});
clientStream.on('end', endcb);
clientStream.resume();
- };
+ }
return cb && cb(null, {

0 comments on commit 0c0f2f3

Please sign in to comment.