From 94fefefb6e9d6b12221051b21a5688c90847f9c0 Mon Sep 17 00:00:00 2001 From: Thomas Desveaux Date: Thu, 3 Oct 2024 12:40:33 +0200 Subject: [PATCH] check: only check processes owned by current user by default Add option '--all-users' to check all running processes --- nimp/base_commands/check.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/nimp/base_commands/check.py b/nimp/base_commands/check.py index 320571e1..09064c30 100644 --- a/nimp/base_commands/check.py +++ b/nimp/base_commands/check.py @@ -22,6 +22,8 @@ '''Environment check command''' +from __future__ import annotations + import abc import fnmatch import json @@ -147,6 +149,11 @@ def configure_arguments(self, env, parser): help='fnmatch filters, defaults to workspace', default=[os.path.normpath(f'{os.path.abspath(env.root_dir)}/*')], ) + parser.add_argument( + '--all-users', + help='By default, only check processes owned by the current user. Use this to check all running processes.', + action='store_true', + ) return True def _run_check(self, env: NimpEnvironment): @@ -158,6 +165,11 @@ def _run_check(self, env: NimpEnvironment): logging.warning("Command only available on Windows platform") return True + current_user: str | None = None + if not env.all_users: + current_user = psutil.Process().username() + logging.debug("Only act on processes owned by %s", current_user) + # Find all running processes running a program that any filter match either: # - the program executable # - an open file handle @@ -191,6 +203,26 @@ def _run_check(self, env: NimpEnvironment): logging.debug("[Process(%d)] ignore process (self, parent or child)", process.pid) continue + if current_user is not None: + process_user = None + try: + process_user = process.username() + except psutil.AccessDenied: + logging.debug( + "[Process(%d)] Failed to retrieve process user", + process.pid, + ) + continue + + if current_user != process_user: + logging.debug( + "[Process(%d)] ignore process from other user (self: %s, process user: %s)", + process.pid, + current_user, + process_user, + ) + continue + checked_processes_count += 1 if not _Processes._process_matches_filters(process, env.filters): continue