neekey / ps

A Node.js module for looking up running processes
MIT License
128 stars 41 forks source link

wrong split command and arguments if path contains spaces #11

Open neekey opened 8 years ago

neekey commented 8 years ago

A issue reported:

I’m porting my program to osX, and found a bug in ps-node.

On osX, commands path may contain spaces. For instance I’m trying to find ocurrences of Google Chrome, and here is an example of ‘ps -A’ command output:

Mac-Mini:herve$ ps -A
  PID TTY           TIME CMD
54292 ??         0:01.00 /Applications/Google Chrome.app/Contents/Versions/47.0
54359 ??         0:02.96 /Applications/Google Chrome.app/Contents/Versions/47.0
54391 ??         0:01.41 /Applications/Google Chrome.app/Contents/Versions/47.0
54694 ??         0:00.87 /Applications/Google Chrome.app/Contents/Versions/47.0

See the space between ‘Google' and ‘Chrome’?

You obviously use the space to separate the command from its arguments, which is wrong on osX. The resulting object of your parsing contains (for instance):

{ pid: '54359',
  command: '/Applications/Google',
  arguments: 
   [ 'Chrome.app/Contents/Versions/47.0.2526.106/Google',
     'Chrome',
     'Helper.app/Contents/MacOS/Google',
     'Chrome',
     'Helper',
     '--type=renderer',
     '--enable-experimental-web-platform-features',
     '--lang=fr' ],
  ppid: undefined }

I suppose that you could replace the line in table-parser/lib/index.js

var fields = line.split( /\s+/ );

by something like:

var fields = line.split( /[^\/]\s+[^\/]/ ); // not tested so it doesn’t work :-)

to eliminate the spaces between slashes inside fields. This may benefit to arguments too, since they can contain a path with a space.

I didn’t check the bug on previous versions of ps-node.

neekey commented 8 years ago

this actually is an already known issue that I don't have a good solution for now. This could be solved easily if only by your case, but the key point is the way to distinguish a normal space and a space that within a path.

Actually table-parser already handled similar situation in Windows like:

CommandLine
"C:\Program Files\Parallels\Parallels Tools\Services\prl_tools.exe" aaa --hello

Table-Parser will not split if the space is within double-quotes, but the situation you pointed out is quite different to identify.

I have checked out another ps node module, named ps-list by @sindresorhus and turned out this module doesn't split command and arguments at all so that it doesn't need to worry about this problem.

Any idea is welcomed, and I will keep on looking for solutions.

herve-g commented 8 years ago

Since I use ps-node only for non regression tests, an obvious solution is to rename "Google Chrome" to "GoogleChrome" on my mac (or use ps-list, or hack my own really simple dedicated code).

Having thought a bit about this problem, I think that you can't get a general solution, since sometimes arguments are indistinguishable from command name without knowledge of the OS. See this example of 'ps -A' output on osX: PID TTY TIME CMD 189 ?? 0:00.67 /usr/libexec/lsd runAsRoot

What does this mean? A program with a space in its name ("lsd runAsRoot") or an argument to the lsd program without single or double hyphen? It's the latter, but how your program could know this?

The output of ps command is not a table with space separated fields, so you can't use your table-parser module to analyse it.

neekey commented 7 years ago

Hi @herve-g not sure if you are still watching this issue, but just want to let you know that @moos got a solution for it, you can take a look https://github.com/neekey/ps/pull/56#issuecomment-300385568

herve-g commented 7 years ago

Thank you for the info. I managed to do otherwise.

Le 10 mai 2017 à 08:17, Neekey notifications@github.com a écrit :

Hi @herve-g not sure if you are still watching this issue, but just want to let you know that @moos got a solution for it, you can take a look #56 (comment)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.