From 3fd564e8763a310bcfbd07b38ee0359870adf452 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 9 Aug 2026 05:22:55 +0900 Subject: [PATCH] coredump: do not try to read si_code more than once This also renames coredump_context_read_pidfd_info() to coredump_context_read_code(), as it only reads COREDUMP_CODE. This does not change anything effectively, as the function is called only once. Preparation for later change. --- src/coredump/coredump-context.c | 20 ++++++++++++-------- src/coredump/coredump-context.h | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/coredump/coredump-context.c b/src/coredump/coredump-context.c index f7e978bbfc..3f6c9f5a8c 100644 --- a/src/coredump/coredump-context.c +++ b/src/coredump/coredump-context.c @@ -181,7 +181,7 @@ static int get_process_container_parent_cmdline(PidRef *pid, char** ret_cmdline) /* The kernel passes si_signo through core_pattern (%s). Starting with v7.1, * si_code is reported as well via pidfd. */ -static int coredump_context_read_pidfd_info(CoredumpContext *context) { +static int coredump_context_read_code(CoredumpContext *context) { struct pidfd_info info = { .mask = PIDFD_INFO_COREDUMP, }; @@ -190,19 +190,23 @@ static int coredump_context_read_pidfd_info(CoredumpContext *context) { assert(context); assert(pidref_is_set(&context->pidref)); - if (!context->got_pidfd || context->pidref.fd < 0) + if (!context->got_pidfd || context->pidref.fd < 0 || context->got_code >= 0) return 0; r = pidfd_get_info(context->pidref.fd, &info); - if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) - return log_debug_errno(r, "PIDFD_INFO_COREDUMP not supported, ignoring: %m"); - if (r < 0) + if (r < 0) { + context->got_code = false; + + if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) + return log_debug_errno(r, "PIDFD_INFO_COREDUMP not supported, ignoring: %m"); return log_debug_errno(r, "Failed to get pidfd coredump info, ignoring: %m"); + } if (FLAGS_SET(info.mask, PIDFD_INFO_COREDUMP_CODE)) { context->code = (int) info.coredump_code; context->got_code = true; - } + } else + context->got_code = false; return 0; } @@ -246,7 +250,7 @@ int coredump_context_build_iovw(CoredumpContext *context) { (void) iovw_put_string_field(&context->iovw, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(context->signo)); /* Emit si_code if we learned it from pidfd_info */ - if (context->got_code) + if (context->got_code > 0) (void) iovw_put_string_fieldf(&context->iovw, "COREDUMP_CODE=", "%i", context->code); } @@ -402,7 +406,7 @@ static int coredump_context_parse_from_procfs(CoredumpContext *context) { if (r < 0) log_warning_errno(r, "Failed to get auxv, ignoring: %m"); - (void) coredump_context_read_pidfd_info(context); + (void) coredump_context_read_code(context); r = pidref_verify(&context->pidref); if (r < 0) diff --git a/src/coredump/coredump-context.h b/src/coredump/coredump-context.h index ee4334dce2..a149d06eda 100644 --- a/src/coredump/coredump-context.h +++ b/src/coredump/coredump-context.h @@ -65,7 +65,7 @@ struct CoredumpContext { size_t auxv_size; /* META_PROC_AUXV */ char *thread_name; /* META_THREAD_NAME */ bool got_pidfd; /* META_ARGV_PIDFD */ - bool got_code; /* META_CODE */ + int got_code; /* META_CODE, tristate */ bool same_pidns; bool forwarded; int input_fd; @@ -79,6 +79,7 @@ struct CoredumpContext { .uid = UID_INVALID, \ .gid = GID_INVALID, \ .tidref = PIDREF_NULL, \ + .got_code = -1, \ .mount_tree_fd = -EBADF, \ .input_fd = -EBADF, \ }