From ce00b8b1e2bd04f9785684ad67a65de19f082cb7 Mon Sep 17 00:00:00 2001 From: jacklag <73839788+jacklag@users.noreply.github.com> Date: Mon, 9 Jan 2023 16:08:12 +0000 Subject: [PATCH 1/2] select correct /proc/#/status file fields on BSD systems --- src/shellingham/posix/proc.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/shellingham/posix/proc.py b/src/shellingham/posix/proc.py index 14cf9da..9dcc45b 100644 --- a/src/shellingham/posix/proc.py +++ b/src/shellingham/posix/proc.py @@ -5,9 +5,18 @@ from ._core import Process - -STAT_PPID = 3 -STAT_TTY = 6 +if os.uname().sysname.lower() in ('freebsd', 'netbsd', 'dragonfly'): + # /proc/#/status fields on BSD systems + # FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=procfs&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE&arch=default&format=html + # NetBSD: https://man.netbsd.org/NetBSD-9.3-STABLE/mount_procfs.8 + # DragonFlyBSD: https://www.dragonflybsd.org/cgi/web-man?command=procfs + STAT_PPID = 2 + STAT_TTY = 5 +else: + # /proc/#/status fields on linux systems + # see https://docs.kernel.org/filesystems/proc.html + STAT_PPID = 3 + STAT_TTY = 6 STAT_PATTERN = re.compile(r"\(.+\)|\S+") From 9f422532276c55110be5281458b6cb8dfb395713 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Mon, 13 Feb 2023 14:05:58 +0800 Subject: [PATCH 2/2] Make uname-checking slightly safer --- src/shellingham/posix/proc.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/shellingham/posix/proc.py b/src/shellingham/posix/proc.py index 9dcc45b..174b908 100644 --- a/src/shellingham/posix/proc.py +++ b/src/shellingham/posix/proc.py @@ -5,18 +5,15 @@ from ._core import Process -if os.uname().sysname.lower() in ('freebsd', 'netbsd', 'dragonfly'): - # /proc/#/status fields on BSD systems - # FreeBSD: https://www.freebsd.org/cgi/man.cgi?query=procfs&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE&arch=default&format=html - # NetBSD: https://man.netbsd.org/NetBSD-9.3-STABLE/mount_procfs.8 - # DragonFlyBSD: https://www.dragonflybsd.org/cgi/web-man?command=procfs - STAT_PPID = 2 - STAT_TTY = 5 -else: - # /proc/#/status fields on linux systems - # see https://docs.kernel.org/filesystems/proc.html - STAT_PPID = 3 - STAT_TTY = 6 +# 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 +BSD_STAT_TTY = 5 + +# See https://docs.kernel.org/filesystems/proc.html +LINUX_STAT_PPID = 3 +LINUX_STAT_TTY = 6 STAT_PATTERN = re.compile(r"\(.+\)|\S+") @@ -37,12 +34,21 @@ def detect_proc(): 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_stat(pid, name): path = os.path.join("/proc", str(pid), name) with io.open(path, encoding="ascii", errors="replace") as f: - # We only care about TTY and PPID -- all numbers. parts = STAT_PATTERN.findall(f.read()) - return parts[STAT_TTY], parts[STAT_PPID] + # We only care about TTY and PPID -- both are numbers. + if _use_bsd_stat_format(): + return parts[BSD_STAT_PPID], parts[BSD_STAT_TTY] + return parts[LINUX_STAT_PPID], parts[LINUX_STAT_TTY] def _get_cmdline(pid):