Skip to content

Commit

Permalink
refactor(apport): Make public variable pidstat private
Browse files Browse the repository at this point in the history
Make public variable `pidstat` private and pass it around where needed.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
  • Loading branch information
bdrung authored and schopin-pro committed Jul 10, 2023
1 parent d5f1f43 commit 1d374b8
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions data/apport
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ def check_lock():
signal.signal(signal.SIGALRM, original_handler)


pidstat = None


def get_core_path(
options: argparse.Namespace,
crash_user: UserGroupID,
Expand All @@ -123,12 +120,8 @@ def get_core_path(
)[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 @@ def get_pid_info(proc_pid: ProcPid) -> UserGroupID:

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 @@ def write_user_coredump(
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 @@ def write_user_coredump(
# (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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)
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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)

# 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(
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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)
# 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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)
options.signal_number,
)
recover_privileges()
write_user_coredump(core_path, core_ulimit, proc_pid, crash_user, coredump_fd)
write_user_coredump(
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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)
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 @@ def process_crash_with_proc_pid(options: argparse.Namespace, proc_pid: ProcPid)
# 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

0 comments on commit 1d374b8

Please sign in to comment.