Skip to content

Commit

Permalink
Fix issue neekey#11 where spaces in commands are not escaped or surro…
Browse files Browse the repository at this point in the history
…unded by quotations

This is done by calling /proc/<PID>/cmdline where the arguments are separated by NUL characters rather than spaces. This can then be split up and parsed.
  • Loading branch information
addisonElliott committed Apr 17, 2018
1 parent b022882 commit 3eb5212
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.iws
**/node_modules
node_modules
.idea
.idea
.vscode/
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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 = '';

Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
});
});
2 changes: 2 additions & 0 deletions test/with space/sleep with space
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
/bin/sleep 5

0 comments on commit 3eb5212

Please sign in to comment.