Skip to content

Commit

Permalink
check: only check processes owned by current user by default
Browse files Browse the repository at this point in the history
Add option '--all-users' to check all running processes
  • Loading branch information
tdesveaux committed Oct 3, 2024
1 parent b1b06dc commit 94fefef
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions nimp/base_commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

'''Environment check command'''

from __future__ import annotations

import abc
import fnmatch
import json
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 94fefef

Please sign in to comment.