second commit

This commit is contained in:
2024-12-27 22:31:23 +09:00
parent 2353324570
commit 10a0f110ca
8819 changed files with 1307198 additions and 28 deletions

View File

@ -0,0 +1,23 @@
import importlib
import os
from ._core import ShellDetectionFailure
__version__ = "1.5.4"
def detect_shell(pid=None, max_depth=10):
name = os.name
try:
impl = importlib.import_module(".{}".format(name), __name__)
except ImportError:
message = "Shell detection not implemented for {0!r}".format(name)
raise RuntimeError(message)
try:
get_shell = impl.get_shell
except AttributeError:
raise RuntimeError("get_shell not implemented for {0!r}".format(name))
shell = get_shell(pid, max_depth=max_depth)
if shell:
return shell
raise ShellDetectionFailure()

View File

@ -0,0 +1,11 @@
SHELL_NAMES = (
{"sh", "bash", "dash", "ash"} # Bourne.
| {"csh", "tcsh"} # C.
| {"ksh", "zsh", "fish"} # Common alternatives.
| {"cmd", "powershell", "pwsh"} # Microsoft.
| {"elvish", "xonsh", "nu"} # More exotic.
)
class ShellDetectionFailure(EnvironmentError):
pass

View File

@ -0,0 +1,163 @@
import contextlib
import ctypes
import os
from ctypes.wintypes import (
BOOL,
CHAR,
DWORD,
HANDLE,
LONG,
LPWSTR,
MAX_PATH,
PDWORD,
ULONG,
)
from shellingham._core import SHELL_NAMES
INVALID_HANDLE_VALUE = HANDLE(-1).value
ERROR_NO_MORE_FILES = 18
ERROR_INSUFFICIENT_BUFFER = 122
TH32CS_SNAPPROCESS = 2
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
kernel32 = ctypes.windll.kernel32
def _check_handle(error_val=0):
def check(ret, func, args):
if ret == error_val:
raise ctypes.WinError()
return ret
return check
def _check_expected(expected):
def check(ret, func, args):
if ret:
return True
code = ctypes.GetLastError()
if code == expected:
return False
raise ctypes.WinError(code)
return check
class ProcessEntry32(ctypes.Structure):
_fields_ = (
("dwSize", DWORD),
("cntUsage", DWORD),
("th32ProcessID", DWORD),
("th32DefaultHeapID", ctypes.POINTER(ULONG)),
("th32ModuleID", DWORD),
("cntThreads", DWORD),
("th32ParentProcessID", DWORD),
("pcPriClassBase", LONG),
("dwFlags", DWORD),
("szExeFile", CHAR * MAX_PATH),
)
kernel32.CloseHandle.argtypes = [HANDLE]
kernel32.CloseHandle.restype = BOOL
kernel32.CreateToolhelp32Snapshot.argtypes = [DWORD, DWORD]
kernel32.CreateToolhelp32Snapshot.restype = HANDLE
kernel32.CreateToolhelp32Snapshot.errcheck = _check_handle( # type: ignore
INVALID_HANDLE_VALUE,
)
kernel32.Process32First.argtypes = [HANDLE, ctypes.POINTER(ProcessEntry32)]
kernel32.Process32First.restype = BOOL
kernel32.Process32First.errcheck = _check_expected( # type: ignore
ERROR_NO_MORE_FILES,
)
kernel32.Process32Next.argtypes = [HANDLE, ctypes.POINTER(ProcessEntry32)]
kernel32.Process32Next.restype = BOOL
kernel32.Process32Next.errcheck = _check_expected( # type: ignore
ERROR_NO_MORE_FILES,
)
kernel32.GetCurrentProcessId.argtypes = []
kernel32.GetCurrentProcessId.restype = DWORD
kernel32.OpenProcess.argtypes = [DWORD, BOOL, DWORD]
kernel32.OpenProcess.restype = HANDLE
kernel32.OpenProcess.errcheck = _check_handle( # type: ignore
INVALID_HANDLE_VALUE,
)
kernel32.QueryFullProcessImageNameW.argtypes = [HANDLE, DWORD, LPWSTR, PDWORD]
kernel32.QueryFullProcessImageNameW.restype = BOOL
kernel32.QueryFullProcessImageNameW.errcheck = _check_expected( # type: ignore
ERROR_INSUFFICIENT_BUFFER,
)
@contextlib.contextmanager
def _handle(f, *args, **kwargs):
handle = f(*args, **kwargs)
try:
yield handle
finally:
kernel32.CloseHandle(handle)
def _iter_processes():
f = kernel32.CreateToolhelp32Snapshot
with _handle(f, TH32CS_SNAPPROCESS, 0) as snap:
entry = ProcessEntry32()
entry.dwSize = ctypes.sizeof(entry)
ret = kernel32.Process32First(snap, entry)
while ret:
yield entry
ret = kernel32.Process32Next(snap, entry)
def _get_full_path(proch):
size = DWORD(MAX_PATH)
while True:
path_buff = ctypes.create_unicode_buffer("", size.value)
if kernel32.QueryFullProcessImageNameW(proch, 0, path_buff, size):
return path_buff.value
size.value *= 2
def get_shell(pid=None, max_depth=10):
proc_map = {
proc.th32ProcessID: (proc.th32ParentProcessID, proc.szExeFile)
for proc in _iter_processes()
}
pid = pid or os.getpid()
for _ in range(0, max_depth + 1):
try:
ppid, executable = proc_map[pid]
except KeyError: # No such process? Give up.
break
# The executable name would be encoded with the current code page if
# we're in ANSI mode (usually). Try to decode it into str/unicode,
# replacing invalid characters to be safe (not thoeratically necessary,
# I think). Note that we need to use 'mbcs' instead of encoding
# settings from sys because this is from the Windows API, not Python
# internals (which those settings reflect). (pypa/pipenv#3382)
if isinstance(executable, bytes):
executable = executable.decode("mbcs", "replace")
name = executable.rpartition(".")[0].lower()
if name not in SHELL_NAMES:
pid = ppid
continue
key = PROCESS_QUERY_LIMITED_INFORMATION
with _handle(kernel32.OpenProcess, key, 0, pid) as proch:
return (name, _get_full_path(proch))
return None

View File

@ -0,0 +1,112 @@
import os
import re
from .._core import SHELL_NAMES, ShellDetectionFailure
from . import proc, ps
# Based on QEMU docs: https://www.qemu.org/docs/master/user/main.html
QEMU_BIN_REGEX = re.compile(
r"""qemu-
(alpha
|armeb
|arm
|m68k
|cris
|i386
|x86_64
|microblaze
|mips
|mipsel
|mips64
|mips64el
|mipsn32
|mipsn32el
|nios2
|ppc64
|ppc
|sh4eb
|sh4
|sparc
|sparc32plus
|sparc64
)""",
re.VERBOSE,
)
def _iter_process_parents(pid, max_depth=10):
"""Select a way to obtain process information from the system.
* `/proc` is used if supported.
* The system `ps` utility is used as a fallback option.
"""
for impl in (proc, ps):
try:
iterator = impl.iter_process_parents(pid, max_depth)
except EnvironmentError:
continue
return iterator
raise ShellDetectionFailure("compatible proc fs or ps utility is required")
def _get_login_shell(proc_cmd):
"""Form shell information from SHELL environ if possible."""
login_shell = os.environ.get("SHELL", "")
if login_shell:
proc_cmd = login_shell
else:
proc_cmd = proc_cmd[1:]
return (os.path.basename(proc_cmd).lower(), proc_cmd)
_INTERPRETER_SHELL_NAMES = [
(re.compile(r"^python(\d+(\.\d+)?)?$"), {"xonsh"}),
]
def _get_interpreter_shell(proc_name, proc_args):
"""Get shell invoked via an interpreter.
Some shells are implemented on, and invoked with an interpreter, e.g. xonsh
is commonly executed with an executable Python script. This detects what
script the interpreter is actually running, and check whether that looks
like a shell.
See sarugaku/shellingham#26 for rational.
"""
for pattern, shell_names in _INTERPRETER_SHELL_NAMES:
if not pattern.match(proc_name):
continue
for arg in proc_args:
name = os.path.basename(arg).lower()
if os.path.isfile(arg) and name in shell_names:
return (name, arg)
return None
def _get_shell(cmd, *args):
if cmd.startswith("-"): # Login shell! Let's use this.
return _get_login_shell(cmd)
name = os.path.basename(cmd).lower()
if name == "rosetta" or QEMU_BIN_REGEX.fullmatch(name):
# If the current process is Rosetta or QEMU, this likely is a
# containerized process. Parse out the actual command instead.
cmd = args[0]
args = args[1:]
name = os.path.basename(cmd).lower()
if name in SHELL_NAMES: # Command looks like a shell.
return (name, cmd)
shell = _get_interpreter_shell(name, args)
if shell:
return shell
return None
def get_shell(pid=None, max_depth=10):
"""Get the shell that the supplied pid or os.getpid() is running in."""
pid = str(pid or os.getpid())
for proc_args, _, _ in _iter_process_parents(pid, max_depth):
shell = _get_shell(*proc_args)
if shell:
return shell
return None

View File

@ -0,0 +1,3 @@
import collections
Process = collections.namedtuple("Process", "args pid ppid")

View File

@ -0,0 +1,83 @@
import io
import os
import re
import sys
from ._core import Process
# FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=procfs
# NetBSD: https://man.netbsd.org/NetBSD-9.3-STABLE/mount_procfs.8
# DragonFlyBSD: https://www.dragonflybsd.org/cgi/web-man?command=procfs
BSD_STAT_PPID = 2
# See https://docs.kernel.org/filesystems/proc.html
LINUX_STAT_PPID = 3
STAT_PATTERN = re.compile(r"\(.+\)|\S+")
def detect_proc():
"""Detect /proc filesystem style.
This checks the /proc/{pid} directory for possible formats. Returns one of
the following as str:
* `stat`: Linux-style, i.e. ``/proc/{pid}/stat``.
* `status`: BSD-style, i.e. ``/proc/{pid}/status``.
"""
pid = os.getpid()
for name in ("stat", "status"):
if os.path.exists(os.path.join("/proc", str(pid), name)):
return name
raise ProcFormatError("unsupported proc format")
def _use_bsd_stat_format():
try:
return os.uname().sysname.lower() in ("freebsd", "netbsd", "dragonfly")
except Exception:
return False
def _get_ppid(pid, name):
path = os.path.join("/proc", str(pid), name)
with io.open(path, encoding="ascii", errors="replace") as f:
parts = STAT_PATTERN.findall(f.read())
# We only care about TTY and PPID -- both are numbers.
if _use_bsd_stat_format():
return parts[BSD_STAT_PPID]
return parts[LINUX_STAT_PPID]
def _get_cmdline(pid):
path = os.path.join("/proc", str(pid), "cmdline")
encoding = sys.getfilesystemencoding() or "utf-8"
with io.open(path, encoding=encoding, errors="replace") as f:
# XXX: Command line arguments can be arbitrary byte sequences, not
# necessarily decodable. For Shellingham's purpose, however, we don't
# care. (pypa/pipenv#2820)
# cmdline appends an extra NULL at the end, hence the [:-1].
return tuple(f.read().split("\0")[:-1])
class ProcFormatError(EnvironmentError):
pass
def iter_process_parents(pid, max_depth=10):
"""Try to look up the process tree via the /proc interface."""
stat_name = detect_proc()
# Inner generator function so we correctly throw an error eagerly if proc
# is not supported, rather than on the first call to the iterator. This
# allows the call site detects the correct implementation.
def _iter_process_parents(pid, max_depth):
for _ in range(max_depth):
ppid = _get_ppid(pid, stat_name)
args = _get_cmdline(pid)
yield Process(args=args, pid=pid, ppid=ppid)
if ppid == "0":
break
pid = ppid
return _iter_process_parents(pid, max_depth)

View File

@ -0,0 +1,51 @@
import errno
import subprocess
import sys
from ._core import Process
class PsNotAvailable(EnvironmentError):
pass
def iter_process_parents(pid, max_depth=10):
"""Try to look up the process tree via the output of `ps`."""
try:
cmd = ["ps", "-ww", "-o", "pid=", "-o", "ppid=", "-o", "args="]
output = subprocess.check_output(cmd)
except OSError as e: # Python 2-compatible FileNotFoundError.
if e.errno != errno.ENOENT:
raise
raise PsNotAvailable("ps not found")
except subprocess.CalledProcessError as e:
# `ps` can return 1 if the process list is completely empty.
# (sarugaku/shellingham#15)
if not e.output.strip():
return
raise
if not isinstance(output, str):
encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
output = output.decode(encoding)
processes_mapping = {}
for line in output.split("\n"):
try:
_pid, ppid, args = line.strip().split(None, 2)
# XXX: This is not right, but we are really out of options.
# ps does not offer a sane way to decode the argument display,
# and this is "Good Enough" for obtaining shell names. Hopefully
# people don't name their shell with a space, or have something
# like "/usr/bin/xonsh is uber". (sarugaku/shellingham#14)
args = tuple(a.strip() for a in args.split(" "))
except ValueError:
continue
processes_mapping[_pid] = Process(args=args, pid=_pid, ppid=ppid)
for _ in range(max_depth):
try:
process = processes_mapping[pid]
except KeyError:
return
yield process
pid = process.ppid