sarugaku / shellingham

Tool to Detect Surrounding Shell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Command line parsing logic of `ps` output is wrong

uranusjr opened this issue · comments

From #12. Is there a specification on how ps formats the command line if it contains special characters (spaces in argument, quotes, etc.)? It seems that it is not using the “normal” shell escaping syntax, which makes the it unsuitable to parse with shlex.split().

https://github.com/freebsd/freebsd/blob/d843b0f4abd4782caf9abffd5f7628b51d65d541/bin/ps/fmt.c#L83-L93

Heh that’s interesting. If I’m reading it right, ps only escapes newlines and unprintable characters, but separate arguments with spaces. So a command line like this cmd foo 'bar baz' would be reported as cmd foo bar baz, and there’s no way to unambigiously tell what is what.

I guess all we can do is to split with space, and hope for the best. Or maybe we forgo ps altogether and tap into POSIX C API directly. We already do that for Windows anyway.

While I’m sure it’s technically possible, I’ve never seen a shell named with a space on posix

It’s likely because it is technically unfeasible. A lot of things won’t work if you do that, e.g. shebang.