Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong split command and arguments if path contains spaces #11

Open
neekey opened this issue May 9, 2016 · 4 comments
Open

wrong split command and arguments if path contains spaces #11

neekey opened this issue May 9, 2016 · 4 comments

Comments

@neekey
Copy link
Owner

neekey commented May 9, 2016

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
Copy link
Owner Author

neekey commented May 9, 2016

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
Copy link

herve-g commented May 9, 2016

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.

moos added a commit to moos/ps that referenced this issue May 1, 2017
@neekey neekey mentioned this issue May 2, 2017
@neekey
Copy link
Owner Author

neekey commented May 10, 2017

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)

@herve-g
Copy link

herve-g commented May 10, 2017 via email

addisonElliott added a commit to addisonElliott/ps that referenced this issue Apr 17, 2018
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants