]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump: split out process_backtrace() to coredump-backtrace.[ch]
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 8 Oct 2025 00:43:14 +0000 (09:43 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 19 Oct 2025 01:01:38 +0000 (10:01 +0900)
Then, rename to coredump_backtrace().

src/coredump/coredump-backtrace.c [new file with mode: 0644]
src/coredump/coredump-backtrace.h [new file with mode: 0644]
src/coredump/coredump.c
src/coredump/meson.build

diff --git a/src/coredump/coredump-backtrace.c b/src/coredump/coredump-backtrace.c
new file mode 100644 (file)
index 0000000..4e223e5
--- /dev/null
@@ -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 (file)
index 0000000..8ede3f1
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int coredump_backtrace(int argc, char *argv[]);
index ee162346adbc04c81c847fe11772cacac35c2549..049b737822e96b4db9ed159410ef24dd4f2f8cde 100644 (file)
@@ -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)
index 0f2db421de0018739edcc59ca3cd8bddf2208994..a92642b6c26626f7e29ad441a3d25d99798dafc6 100644 (file)
@@ -6,6 +6,7 @@ endif
 
 systemd_coredump_sources = files(
         'coredump.c',
+        'coredump-backtrace.c',
         'coredump-config.c',
         'coredump-context.c',
 )