sarugaku / shellingham

Tool to Detect Surrounding Shell
ISC License
221 stars 33 forks source link

Switch to parse 'ps wwl' for better compatibility #23

Open uranusjr opened 5 years ago

uranusjr commented 5 years ago

The “Berkeley standard’ argument has better compatibility than either BSD or GNU standard.

Fix #21, close #22.

@kadler and @sirn It’d be wonderful if you could test this on your environments to make sure things still work.

kadler commented 5 years ago
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/QOpenSys/pkgs/lib/python3.6/site-packages/shellingham/posix/ps.py", line 115, in get_process_mapping
    return dict(_parse_ps_output(output))
  File "/QOpenSys/pkgs/lib/python3.6/site-packages/shellingham/posix/ps.py", line 100, in _parse_ps_output
    for k in ('pid', 'ppid', 'command')
  File "/QOpenSys/pkgs/lib/python3.6/site-packages/shellingham/posix/ps.py", line 100, in <genexpr>
    for k in ('pid', 'ppid', 'command')
KeyError: 'command'

On AIX/PASE, this is CMD, not COMMAND

uranusjr commented 5 years ago

Arrrrg! Why IBM, why.

uranusjr commented 5 years ago

OK. How about this.

kadler commented 5 years ago

Still not right:

Python 3.6.8 (default, Mar 15 2019, 19:26:20) 
[GCC 6.3.0] on aix6
Type "help", "copyright", "credits" or "license" for more information.
>>> import shellingham.posix.ps as ps
>>> print(ps._get_ps_output())
     F S      UID    PID   PPID   C PRI NI ADDR  SZ  RSS   WCHAN    TTY  TIME CMD
240000 A      105 932687 932686   0  20 20    0 7516    0          pts/0  0:00 -bash 
200000 A      105 932807 932687   0  20 20    0 16504    0          pts/0  0:00 python3 
200000 A      105 932808 932807   0  20 20    0 6792    0          pts/0  0:00 ps wwl 

>>> from pprint import pprint
>>> pprint(ps.get_process_mapping())
{'932687': Process(args=('0', '-bash'), pid='932687', ppid='932686'),
 '932807': Process(args=('00', 'python3'), pid='932807', ppid='932687'),
 '932810': Process(args=('0', 'ps', 'wwl'), pid='932810', ppid='932807')}

The assumption that the column header offsets match the columns is not correct on the AIX ps. Here's another example that's even worse:

     F S UID      PID     PPID   C PRI NI ADDR  SZ  RSS   WCHAN    TTY  TIME CMD
240001 A 203 14549132  8192226   1  60 20 96aaf7590 2620 2552          pts/0  0:00 -bash 
200001 A 203 16384118 14549132   3  61 20 8aaeeb590 1440 1468          pts/0  0:00 ps wwl
uranusjr commented 5 years ago

What is IBM even doing. I can’t even read that output as a human.