Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check: only check processes owned by current user by default #105

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading