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

refactor(apport): Make public variable pidstat private #197

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 13 additions & 16 deletions data/apport
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@
signal.signal(signal.SIGALRM, original_handler)


pidstat = None


def get_core_path(
options: argparse.Namespace,
crash_user: UserGroupID,
Expand All @@ -123,12 +120,8 @@
)[1]


def get_pid_info(proc_pid: ProcPid) -> UserGroupID:
def get_pid_info(proc_pid: ProcPid) -> typing.Tuple[UserGroupID, os.stat_result]:
"""Read /proc information about pid"""

# pylint: disable=global-statement
global pidstat

# unhandled exceptions on missing or invalidly formatted files are okay
# here -- we want to know in the log file
pidstat = os.stat("stat", dir_fd=proc_pid.fd)
Expand All @@ -142,7 +135,7 @@

assert crash_uid is not None, "failed to parse Uid"
assert crash_gid is not None, "failed to parse Gid"
return UserGroupID(crash_uid, crash_gid)
return UserGroupID(crash_uid, crash_gid), pidstat


def get_process_starttime(proc_pid: ProcPid) -> int:
Expand Down Expand Up @@ -237,6 +230,7 @@
limit: int,
proc_pid: ProcPid,
crash_user: UserGroupID,
pidstat: os.stat_result,
coredump_fd=None,
from_report=None,
) -> None:
Expand All @@ -257,7 +251,6 @@
# (suid_dumpable==2 and core_pattern restrictions); when this happens,
# /proc/pid/stat is owned by root (or the user suid'ed to), but we already
# changed to the crashed process' uid
assert pidstat, "pidstat not initialized"
if UserGroupID(pidstat.st_uid, pidstat.st_gid) != crash_user:
logger.error("disabling core dump for suid/sgid/unreadable executable")
return
Expand Down Expand Up @@ -917,7 +910,7 @@
logger = logging.getLogger()

coredump_fd = sys.stdin.fileno()
crash_user = get_pid_info(proc_pid)
crash_user, pidstat = get_pid_info(proc_pid)

process_start = get_process_starttime(proc_pid)
if not consistency_checks(options, process_start, proc_pid, crash_user):
Expand All @@ -937,7 +930,9 @@

# ignore SIGQUIT (it's usually deliberately generated by users)
if options.signal_number == int(signal.SIGQUIT):
write_user_coredump(core_path, core_ulimit, proc_pid, crash_user, coredump_fd)
write_user_coredump(

Check warning on line 933 in data/apport

View check run for this annotation

Codecov / codecov/patch

data/apport#L933

Added line #L933 was not covered by tests
core_path, core_ulimit, proc_pid, crash_user, pidstat, coredump_fd
)
return 0

info = apport.report.Report("Crash")
Expand Down Expand Up @@ -994,7 +989,7 @@
# check if the user wants a core dump
recover_privileges()
write_user_coredump(
core_path, core_ulimit, proc_pid, crash_user, coredump_fd
core_path, core_ulimit, proc_pid, crash_user, pidstat, coredump_fd
)
return 0

Expand All @@ -1006,7 +1001,9 @@
options.signal_number,
)
recover_privileges()
write_user_coredump(core_path, core_ulimit, proc_pid, crash_user, coredump_fd)
write_user_coredump(

Check warning on line 1004 in data/apport

View check run for this annotation

Codecov / codecov/patch

data/apport#L1004

Added line #L1004 was not covered by tests
core_path, core_ulimit, proc_pid, crash_user, pidstat, coredump_fd
)
return 0

if info.check_ignored():
Expand Down Expand Up @@ -1042,7 +1039,7 @@
if skip_msg:
logger.error("%s", skip_msg)
write_user_coredump(
core_path, core_ulimit, proc_pid, crash_user, coredump_fd
core_path, core_ulimit, proc_pid, crash_user, pidstat, coredump_fd
)
return 0
# remove the old file, so that we can create the new one
Expand Down Expand Up @@ -1100,7 +1097,7 @@
# than the core size.
reportfile.seek(0)
write_user_coredump(
core_path, core_ulimit, proc_pid, crash_user, from_report=reportfile
core_path, core_ulimit, proc_pid, crash_user, pidstat, from_report=reportfile
)
return 0

Expand Down