From 3eb52128b832c067d88677abee48103117a948b5 Mon Sep 17 00:00:00 2001 From: Addison Elliott Date: Tue, 17 Apr 2018 07:11:01 -0500 Subject: [PATCH 1/2] Fix issue #11 where spaces in commands are not escaped or surrounded by quotations This is done by calling /proc//cmdline where the arguments are separated by NUL characters rather than spaces. This can then be split up and parsed. --- .gitignore | 3 ++- lib/index.js | 16 ++++++++++++++++ test/test.js | 21 +++++++++++++++++++++ test/with space/sleep with space | 2 ++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/with space/sleep with space diff --git a/.gitignore b/.gitignore index ecb9a1f..307271f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.iws **/node_modules node_modules -.idea \ No newline at end of file +.idea +.vscode/ \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 893c57b..3ffec08 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ var ChildProcess = require('child_process'); var IS_WIN = process.platform === 'win32'; var TableParser = require('table-parser'); +var fs = require('fs'); /** * End of line. * Basically, the EOL should be: @@ -273,6 +274,21 @@ function formatOutput(data) { var ppid = ( d.PPID && d.PPID[0] ) || ( d.ParentProcessId && d.ParentProcessId[0] ) || undefined; if (pid && cmd) { + // console.log('Print `%j`', cmd) + if (!IS_WIN && cmd.length > 1) { + // console.log('Print yes `%j`', cmd) + try { + cmd = fs.readFileSync('/proc/' + pid + '/cmdline', 'utf8').split('\0'); + } + catch (err) { + if (err.code !== 'ENOENT') { + throw err; + } + } + // console.log('data: `%j`', data); + // cmd = fs.readFileSync('/proc/' + pid + '/cmdline').split('\0'); + } + var command = cmd[0]; var args = ''; diff --git a/test/test.js b/test/test.js index a682004..059d82d 100644 --- a/test/test.js +++ b/test/test.js @@ -5,6 +5,7 @@ var Path = require('path'); var Sinon = require('sinon'); var serverPath = Path.resolve(__dirname, './node_process_for_test.js'); +var serverPathSpace = Path.resolve(__dirname, './with space/sleep with space'); var UpperCaseArg = '--UPPER_CASE'; var child = null; var pid = null; @@ -197,3 +198,23 @@ describe('test', function () { }); }); }); + +// don't run on Windows +(process.platform === 'win32' ? describe.skip : describe)('test command with space', function () { + before(function () { + child = CP.spawn(serverPathSpace); + pid = child.pid; + }); + + afterEach(killProcess); + + it('by command with space in path', function (done) { + PS.lookup({pid: pid}, function (err, list) { + assert.equal(list.length, 1); + assert.equal(list[0].pid, pid); + assert.equal(list[0].command, '/bin/bash'); + assert.equal(list[0].arguments[0], serverPathSpace); + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/with space/sleep with space b/test/with space/sleep with space new file mode 100644 index 0000000..259e284 --- /dev/null +++ b/test/with space/sleep with space @@ -0,0 +1,2 @@ +#!/bin/bash +/bin/sleep 5 \ No newline at end of file From c44569c1b1dd402a4f129d9bbd6fe6b04ca5b45c Mon Sep 17 00:00:00 2001 From: Addison Elliott Date: Tue, 17 Apr 2018 07:33:52 -0500 Subject: [PATCH 2/2] Fix small issues and update bash script to be executable --- .gitignore | 2 +- test/test.js | 2 +- test/with space/sleep with space | 2 +- test/with space/sleep with space.sh | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) create mode 100755 test/with space/sleep with space.sh diff --git a/.gitignore b/.gitignore index 307271f..da2cf06 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ **/node_modules node_modules .idea -.vscode/ \ No newline at end of file +.vscode/ diff --git a/test/test.js b/test/test.js index 059d82d..bbd8e2c 100644 --- a/test/test.js +++ b/test/test.js @@ -5,7 +5,7 @@ var Path = require('path'); var Sinon = require('sinon'); var serverPath = Path.resolve(__dirname, './node_process_for_test.js'); -var serverPathSpace = Path.resolve(__dirname, './with space/sleep with space'); +var serverPathSpace = Path.resolve(__dirname, './with space/sleep with space.sh'); var UpperCaseArg = '--UPPER_CASE'; var child = null; var pid = null; diff --git a/test/with space/sleep with space b/test/with space/sleep with space index 259e284..1b3f24d 100644 --- a/test/with space/sleep with space +++ b/test/with space/sleep with space @@ -1,2 +1,2 @@ #!/bin/bash -/bin/sleep 5 \ No newline at end of file +/bin/sleep 5 diff --git a/test/with space/sleep with space.sh b/test/with space/sleep with space.sh new file mode 100755 index 0000000..1b3f24d --- /dev/null +++ b/test/with space/sleep with space.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/bin/sleep 5