Skip to content

Commit

Permalink
use a process-queue to avoid xtables errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Sep 18, 2014
1 parent 50f0471 commit 21a9da9
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 64 deletions.
61 changes: 61 additions & 0 deletions lib/child_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var processQueue = require('process-queue');

var default_queue = processQueue.createQueue({concurrency:1});

exports.exec = function (line, opts, cb) {
var stdout, stdout_length, stderr, stderr_length;

// avoid allocation
if (cb) {
stdout = [];
stdout_length = 0;
stderr = [];
stderr_length = 0;
}

var child;
var queue = opts.queue || default_queue;
if (cb) {
queue = queue.wrap({
child: function (spawned_child, next) {
child = spawned_child;
if (cb) {
child.stdout.on('data', function (data) {
if (!Buffer.isBuffer(data)) data = new Buffer(data);
stdout_length += data.length;
stdout.push(data);
});
child.stderr.on('data', function (data) {
if (!Buffer.isBuffer(data)) data = new Buffer(data);
stderr_length += data.length;
stderr.push(data);
});
}
next(null, child);
}
});
}
var cmd = {
spawnOptions: ['sh', ['-c', 'exec ' + line]]
};
function onFinish(err) {
if (!cb) return;
if (!child) {
cb(new Error('there was a problem spawning the child process'));
return;
}
if (err) {
err.killed = child.killed;
err.code = child.exitCode;
err.signal = child.signalCode;
}
cb(err,
stdout_length ? String(Buffer.concat(stdout, stdout_length)) : '',
stderr_length ? String(Buffer.concat(stderr, stderr_length)) : ''
);
}
queue.push(cmd, onFinish);

// cleanup for gc
cmd = null;
}
6 changes: 3 additions & 3 deletions lib/ipset/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Add a given entry to the set.
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -58,4 +58,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Create a set identified with setsetname and specified type.
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -58,4 +58,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/del.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Delete an entry from a set.
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -58,4 +58,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/destroy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Destroy the specified set or all the sets if none is given.
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = function (/* options?, cb */) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -56,4 +56,4 @@ module.exports = function (/* options?, cb */) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/flush.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Flush all entries from the specified set or flush all sets if none is given.
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = function (/* options?, cb */) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -56,4 +56,4 @@ module.exports = function (/* options?, cb */) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/rename.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Rename a set.
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -47,4 +47,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/swap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Swap the content of two sets.
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -47,4 +47,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/ipset/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Test wether an entry is in a set or not.
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -58,4 +58,4 @@ module.exports = function (options, cb) {
cb(null, 0);
}
});
};
};
14 changes: 9 additions & 5 deletions lib/ipset/version.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

/**
* Print program version.
*
* @param cb
*/
module.exports = function (cb) {
if (typeof arguments[0] != 'function') {
module.exports = function (options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
if (typeof cb != 'function') {
throw new Error('Invalid arguments. Signature: (callback)');
}

Expand All @@ -22,7 +26,7 @@ module.exports = function (cb) {
/*
* Execute command.
*/
exec(cmd.join(' '), function (error, stdout, stderror) {
exec(cmd.join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -34,4 +38,4 @@ module.exports = function (cb) {
cb(null, stdout.split('\n')[0]);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/iptables/append.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

var tables = require('./utils').tables;
var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs;
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -58,4 +58,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
4 changes: 2 additions & 2 deletions lib/iptables/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

var tables = require('./utils').tables;
var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs;
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand Down
6 changes: 3 additions & 3 deletions lib/iptables/delete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

var tables = require('./utils').tables;
var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs;
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -63,4 +63,4 @@ module.exports = function (options, cb) {
cb(null);
}
});
};
};
6 changes: 3 additions & 3 deletions lib/iptables/delete_chain.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

var tables = require('./utils').tables;

Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = function (/* options?, cb */) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -66,4 +66,4 @@ module.exports = function (/* options?, cb */) {
cb(null);
}
});
};
};
4 changes: 2 additions & 2 deletions lib/iptables/dump.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;
var shell_quote = require('shell-quote');

var tables = require('./utils').tables;
Expand Down Expand Up @@ -117,7 +117,7 @@ module.exports = function (options, cb) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand Down
6 changes: 3 additions & 3 deletions lib/iptables/flush.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var exec = require('child_process').exec;
var exec = require('../child_utils').exec;

var tables = require('./utils').tables;

Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = function (/* options?, cb */) {
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), function (error, stdout, stderror) {
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) {
if (error && cb) {
var err = new Error(stderror.split('\n')[0]);
err.cmd = cmd.concat(args).join(' ');
Expand All @@ -66,4 +66,4 @@ module.exports = function (/* options?, cb */) {
cb(null);
}
});
};
};
Loading

0 comments on commit 21a9da9

Please sign in to comment.