From fa30374e1589074aa129e816528dafe50c05fd3f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 8 Oct 2025 09:43:14 +0900 Subject: [PATCH] coredump: split out process_backtrace() to coredump-backtrace.[ch] Then, rename to coredump_backtrace(). --- src/coredump/coredump-backtrace.c | 73 +++++++++++++++++++++++++++++++ src/coredump/coredump-backtrace.h | 4 ++ src/coredump/coredump.c | 67 +--------------------------- src/coredump/meson.build | 1 + 4 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 src/coredump/coredump-backtrace.c create mode 100644 src/coredump/coredump-backtrace.h diff --git a/src/coredump/coredump-backtrace.c b/src/coredump/coredump-backtrace.c new file mode 100644 index 00000000000..4e223e52200 --- /dev/null +++ b/src/coredump/coredump-backtrace.c @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-journal.h" +#include "sd-messages.h" + +#include "coredump-backtrace.h" +#include "coredump-context.h" +#include "iovec-util.h" +#include "journal-importer.h" +#include "log.h" +#include "string-util.h" + +int coredump_backtrace(int argc, char *argv[]) { + _cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO); + _cleanup_(iovw_free_freep) struct iovec_wrapper *iovw = NULL; + _cleanup_(context_done) Context context = CONTEXT_NULL; + int r; + + assert(argc >= 2); + + log_debug("Processing backtrace on stdin..."); + + iovw = iovw_new(); + if (!iovw) + return log_oom(); + + (void) iovw_put_string_field(iovw, "MESSAGE_ID=", SD_MESSAGE_BACKTRACE_STR); + (void) iovw_put_string_field(iovw, "PRIORITY=", STRINGIFY(LOG_CRIT)); + + /* Collect all process metadata from argv[] by making sure to skip the '--backtrace' option. */ + r = gather_pid_metadata_from_argv(iovw, &context, argc - 2, argv + 2); + if (r < 0) + return r; + + /* Collect the rest of the process metadata retrieved from the runtime */ + r = gather_pid_metadata_from_procfs(iovw, &context); + if (r < 0) + return r; + + for (;;) { + r = journal_importer_process_data(&importer); + if (r < 0) + return log_error_errno(r, "Failed to parse journal entry on stdin: %m"); + if (r == 1 || /* complete entry */ + journal_importer_eof(&importer)) /* end of data */ + break; + } + + if (journal_importer_eof(&importer)) { + log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter."); + + const char *message = strjoina("Process ", context.meta[META_ARGV_PID], + " (", context.meta[META_COMM], ")" + " of user ", context.meta[META_ARGV_UID], + " failed with ", context.meta[META_ARGV_SIGNAL]); + + r = iovw_put_string_field(iovw, "MESSAGE=", message); + if (r < 0) + return r; + } else { + /* The imported iovecs are not supposed to be freed by us so let's copy and merge them at the + * end of the array. */ + r = iovw_append(iovw, &importer.iovw); + if (r < 0) + return r; + } + + r = sd_journal_sendv(iovw->iovec, iovw->count); + if (r < 0) + return log_error_errno(r, "Failed to log backtrace: %m"); + + return 0; +} diff --git a/src/coredump/coredump-backtrace.h b/src/coredump/coredump-backtrace.h new file mode 100644 index 00000000000..8ede3f15613 --- /dev/null +++ b/src/coredump/coredump-backtrace.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +int coredump_backtrace(int argc, char *argv[]); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index ee162346adb..049b737822e 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -22,6 +22,7 @@ #include "compress.h" #include "conf-parser.h" #include "copy.h" +#include "coredump-backtrace.h" #include "coredump-config.h" #include "coredump-context.h" #include "coredump-util.h" @@ -1349,70 +1350,6 @@ static int process_kernel(int argc, char *argv[]) { return send_iovec(iovw, STDIN_FILENO, &context.pidref, context.mount_tree_fd); } -static int process_backtrace(int argc, char *argv[]) { - _cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO); - _cleanup_(iovw_free_freep) struct iovec_wrapper *iovw = NULL; - _cleanup_(context_done) Context context = CONTEXT_NULL; - char *message; - int r; - - assert(argc >= 2); - - log_debug("Processing backtrace on stdin..."); - - iovw = iovw_new(); - if (!iovw) - return log_oom(); - - (void) iovw_put_string_field(iovw, "MESSAGE_ID=", SD_MESSAGE_BACKTRACE_STR); - (void) iovw_put_string_field(iovw, "PRIORITY=", STRINGIFY(LOG_CRIT)); - - /* Collect all process metadata from argv[] by making sure to skip the - * '--backtrace' option */ - r = gather_pid_metadata_from_argv(iovw, &context, argc - 2, argv + 2); - if (r < 0) - return r; - - /* Collect the rest of the process metadata retrieved from the runtime */ - r = gather_pid_metadata_from_procfs(iovw, &context); - if (r < 0) - return r; - - for (;;) { - r = journal_importer_process_data(&importer); - if (r < 0) - return log_error_errno(r, "Failed to parse journal entry on stdin: %m"); - if (r == 1 || /* complete entry */ - journal_importer_eof(&importer)) /* end of data */ - break; - } - - if (journal_importer_eof(&importer)) { - log_warning("Did not receive a full journal entry on stdin, ignoring message sent by reporter"); - - message = strjoina("Process ", context.meta[META_ARGV_PID], - " (", context.meta[META_COMM], ")" - " of user ", context.meta[META_ARGV_UID], - " failed with ", context.meta[META_ARGV_SIGNAL]); - - r = iovw_put_string_field(iovw, "MESSAGE=", message); - if (r < 0) - return r; - } else { - /* The imported iovecs are not supposed to be freed by us so let's copy and merge them at the - * end of the array. */ - r = iovw_append(iovw, &importer.iovw); - if (r < 0) - return r; - } - - r = sd_journal_sendv(iovw->iovec, iovw->count); - if (r < 0) - return log_error_errno(r, "Failed to log backtrace: %m"); - - return 0; -} - static int run(int argc, char *argv[]) { int r; @@ -1435,7 +1372,7 @@ static int run(int argc, char *argv[]) { * are invoked from the kernel as coredump handler. */ if (r == 0) { if (streq_ptr(argv[1], "--backtrace")) - return process_backtrace(argc, argv); + return coredump_backtrace(argc, argv); else return process_kernel(argc, argv); } else if (r == 1) diff --git a/src/coredump/meson.build b/src/coredump/meson.build index 0f2db421de0..a92642b6c26 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -6,6 +6,7 @@ endif systemd_coredump_sources = files( 'coredump.c', + 'coredump-backtrace.c', 'coredump-config.c', 'coredump-context.c', ) -- 2.47.3