sarugaku / shellingham

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

treat cygwin like linux #4

Closed badboybeyer closed 6 years ago

badboybeyer commented 6 years ago

Treat cygwin like linux, because its ps command is limited and the proc file system works.

 src/shellingham/posix/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/shellingham/posix/__init__.py b/src/shellingham/posix/__init__.py
index ec27b3a..1f4b80c 100644
--- a/src/shellingham/posix/__init__.py
+++ b/src/shellingham/posix/__init__.py
@@ -6,7 +6,7 @@ from .._consts import SHELL_NAMES

 def _get_process_mapping():
     system = platform.system()
-    if system == 'Linux':
+    if system in ('Linux', 'CYGWIN_NT-6.1'):
         from . import linux as impl
     else:
         from . import _default as impl

I think the "6.1" string may change a couple times a year. It may be better to do:

if system == 'Linux' or system.startswith('CYGWIN_NT'):
uranusjr commented 6 years ago

I wonder if it is a good idea to use /proc whenever it is available. Solution is up in #5 that should work on both Linux and Cygwin, but I wonder if it would break systems such as FreeBSD. Do they all have compatible /proc? I’ll need to find out.

badboybeyer commented 6 years ago

It looks like the BSD4.4 children use /proc/$PID/status instead of /proc/$PID/stat.

old openbsd man page

modern freebsd manpage

Maybe write the module to:

try:
    proc()
except:
    ps()
uranusjr commented 6 years ago

Urrgh! Yeah I guess I need to revise the logic. A try-except is a good idea (but probably not a bare one).

techalchemy commented 6 years ago

Just handle the different structures of proc. There are only like 3 filesystems. It’s a finite space.

uranusjr commented 6 years ago

Okay I think we have two. What’s the third /facepalm

uranusjr commented 6 years ago

I decided to

  1. Try /proc (detect if it is Linux and BSD styles)
  2. If that fails, try ps
  3. If ps also fails, raise an error

We can add compatibility to more uncommon formats when someone actually wants them.