From 7c8480fe375c0423a2ba83b7d5cd7623a32adf3f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 07:16:46 +0900 Subject: [PATCH 01/16] coredumpctl: extern several global variables --- src/coredump/coredumpctl.c | 34 ++++++++++++++++++---------------- src/coredump/coredumpctl.h | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 src/coredump/coredumpctl.h diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 350479e8df..e312c54c90 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -20,6 +20,7 @@ #include "bus-util.h" #include "chase.h" #include "compress.h" +#include "coredumpctl.h" #include "dissect-image.h" #include "dlopen-note.h" #include "errno-util.h" @@ -57,28 +58,29 @@ #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) -static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY; -static const char* arg_field = NULL; -static const char *arg_debugger = NULL; -static char **arg_debugger_args = NULL; -static const char *arg_directory = NULL; -static char *arg_root = NULL; +usec_t arg_since = USEC_INFINITY; +usec_t arg_until = USEC_INFINITY; +const char *arg_field = NULL; +const char *arg_debugger = NULL; +char **arg_debugger_args = NULL; +const char *arg_directory = NULL; +char **arg_file = NULL; +sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; +PagerFlags arg_pager_flags = 0; +int arg_legend = true; +size_t arg_rows_max = SIZE_MAX; +const char *arg_output = NULL; +bool arg_reverse = false; +bool arg_quiet = false; +bool arg_all = false; +char *arg_root = NULL; static char *arg_image = NULL; -static char **arg_file = NULL; -static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; -static PagerFlags arg_pager_flags = 0; -static int arg_legend = true; -static size_t arg_rows_max = SIZE_MAX; -static const char* arg_output = NULL; -static bool arg_reverse = false; -static bool arg_quiet = false; -static bool arg_all = false; static ImagePolicy *arg_image_policy = NULL; STATIC_DESTRUCTOR_REGISTER(arg_debugger_args, strv_freep); +STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_root, freep); STATIC_DESTRUCTOR_REGISTER(arg_image, freep); -STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); COMMAND( diff --git a/src/coredump/coredumpctl.h b/src/coredump/coredumpctl.h new file mode 100644 index 0000000000..2827901cea --- /dev/null +++ b/src/coredump/coredumpctl.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +extern usec_t arg_since; +extern usec_t arg_until; +extern const char *arg_field; +extern const char *arg_debugger; +extern char **arg_debugger_args; +extern const char *arg_directory; +extern char **arg_file; +extern sd_json_format_flags_t arg_json_format_flags; +extern PagerFlags arg_pager_flags; +extern int arg_legend; +extern size_t arg_rows_max; +extern const char *arg_output; +extern bool arg_reverse; +extern bool arg_quiet; +extern bool arg_all; +extern char *arg_root; From a3b8ce6e3373c0d3a9f543e70936a65b360ed94b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 09:22:18 +0900 Subject: [PATCH 02/16] coredumpctl: move resolve_filename() and retrieve() to coredumpctl-util.c --- src/coredump/coredumpctl-util.c | 51 +++++++++++++++++++++++++++++++++ src/coredump/coredumpctl-util.h | 8 ++++++ src/coredump/coredumpctl.c | 51 +-------------------------------- src/coredump/meson.build | 6 +++- 4 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 src/coredump/coredumpctl-util.c create mode 100644 src/coredump/coredumpctl-util.h diff --git a/src/coredump/coredumpctl-util.c b/src/coredump/coredumpctl-util.c new file mode 100644 index 0000000000..2ea10fb6bb --- /dev/null +++ b/src/coredump/coredumpctl-util.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "chase.h" +#include "coredumpctl-util.h" +#include "log.h" +#include "string-util.h" + +int resolve_filename(const char *root, char **p) { + char *resolved = NULL; + int r; + + assert(p); + + if (!*p) + return 0; + + r = chase(*p, root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved, NULL); + if (r < 0) + return log_error_errno(r, "Failed to resolve \"%s%s\": %m", strempty(root), *p); + + free_and_replace(*p, resolved); + + /* chase() with flag CHASE_NONEXISTENT will return 0 if the file doesn't exist and 1 if it does. + * Return that to the caller. */ + return r; +} + +int retrieve(const void *data, size_t len, const char *name, char **var) { + size_t ident; + char *v; + + assert(var); + + ident = strlen(name) + 1; /* name + "=" */ + + if (len < ident) + return 0; + + if (memcmp(data, name, ident - 1) != 0) + return 0; + + if (((const char*) data)[ident - 1] != '=') + return 0; + + v = strndup((const char*) data + ident, len - ident); + if (!v) + return log_oom(); + + free_and_replace(*var, v); + return 1; +} diff --git a/src/coredump/coredumpctl-util.h b/src/coredump/coredumpctl-util.h new file mode 100644 index 0000000000..3f5b33ae84 --- /dev/null +++ b/src/coredump/coredumpctl-util.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int resolve_filename(const char *root, char **p); + +int retrieve(const void *data, size_t len, const char *name, char **var); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index e312c54c90..156179518b 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -21,6 +21,7 @@ #include "chase.h" #include "compress.h" #include "coredumpctl.h" +#include "coredumpctl-util.h" #include "dissect-image.h" #include "dlopen-note.h" #include "errno-util.h" @@ -332,35 +333,6 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { return 1; } -static int retrieve(const void *data, - size_t len, - const char *name, - char **var) { - - size_t ident; - char *v; - - assert(var); - - ident = strlen(name) + 1; /* name + "=" */ - - if (len < ident) - return 0; - - if (memcmp(data, name, ident - 1) != 0) - return 0; - - if (((const char*) data)[ident - 1] != '=') - return 0; - - v = strndup((const char*)data + ident, len - ident); - if (!v) - return log_oom(); - - free_and_replace(*var, v); - return 1; -} - static int print_field(FILE* file, sd_journal *j) { const void *d; size_t l; @@ -452,27 +424,6 @@ error: *ret_size = UINT64_MAX; } -static int resolve_filename(const char *root, char **p) { - char *resolved = NULL; - int r; - - assert(p); - - if (!*p) - return 0; - - r = chase(*p, root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved, NULL); - if (r < 0) - return log_error_errno(r, "Failed to resolve \"%s%s\": %m", strempty(root), *p); - - free_and_replace(*p, resolved); - - /* chase() with flag CHASE_NONEXISTENT will return 0 if the file doesn't exist and 1 if it does. - * Return that to the caller - */ - return r; -} - static int print_list(FILE* file, sd_journal *j, Table *t) { _cleanup_free_ char *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, diff --git a/src/coredump/meson.build b/src/coredump/meson.build index 2ae293ae99..65530e5141 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -17,6 +17,10 @@ systemd_coredump_sources = files( systemd_coredump_export_sources = files( 'coredump-vacuum.c', ) +coredumpctl_sources = files( + 'coredumpctl.c', + 'coredumpctl-util.c', +) coredump_gperf_c = custom_target( input : 'coredump-gperf.gperf', @@ -46,7 +50,7 @@ executables += [ executable_template + { 'name' : 'coredumpctl', 'public' : true, - 'sources' : files('coredumpctl.c'), + 'sources' : coredumpctl_sources, 'link_with' : [libshared], 'dependencies' : common_dependencies, }, From 751a23097412837f96a21c3e4aa391f6db067de1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 09:27:36 +0900 Subject: [PATCH 03/16] coredumpctl: move print_info() and print_entry() to coredumpctl-info.c --- src/coredump/coredumpctl-info.c | 611 ++++++++++++++++++++++++++++++++ src/coredump/coredumpctl-info.h | 7 + src/coredump/coredumpctl.c | 594 +------------------------------ src/coredump/meson.build | 1 + 4 files changed, 620 insertions(+), 593 deletions(-) create mode 100644 src/coredump/coredumpctl-info.c create mode 100644 src/coredump/coredumpctl-info.h diff --git a/src/coredump/coredumpctl-info.c b/src/coredump/coredumpctl-info.c new file mode 100644 index 0000000000..f2cad48114 --- /dev/null +++ b/src/coredump/coredumpctl-info.c @@ -0,0 +1,611 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +#include "sd-journal.h" +#include "sd-messages.h" + +#include "ansi-color.h" +#include "coredumpctl.h" +#include "coredumpctl-info.h" +#include "coredumpctl-util.h" +#include "escape.h" +#include "fd-util.h" +#include "format-table.h" +#include "fs-util.h" +#include "json-util.h" +#include "log.h" +#include "parse-util.h" +#include "path-util.h" +#include "process-util.h" +#include "signal-util.h" +#include "string-util.h" +#include "strv.h" +#include "time-util.h" +#include "user-util.h" + +static int print_field(FILE *file, sd_journal *j) { + const void *d; + size_t l; + + assert(file); + assert(j); + + assert(arg_field); + + /* A (user-specified) field may appear more than once for a given entry. We will print all of the + * occurrences. This is different below for fields that systemd-coredump uses, because they cannot + * meaningfully appear more than once. */ + SD_JOURNAL_FOREACH_DATA(j, d, l) { + _cleanup_free_ char *value = NULL; + int r; + + r = retrieve(d, l, arg_field, &value); + if (r < 0) + return r; + if (r > 0) + fprintf(file, "%s\n", value); + } + + return 0; +} + +static void analyze_coredump_file( + const char *path, + const char **ret_state, + const char **ret_color, + uint64_t *ret_size) { + + _cleanup_close_ int fd = -EBADF; + struct stat st; + int r; + + assert(path); + assert(ret_state); + assert(ret_color); + assert(ret_size); + + fd = open(path, O_PATH|O_CLOEXEC); + if (fd < 0) { + if (errno == ENOENT) { + *ret_state = "missing"; + *ret_color = ansi_grey(); + *ret_size = UINT64_MAX; + return; + } + + r = -errno; + } else + r = access_fd(fd, R_OK); + if (r < 0) { + if (ERRNO_IS_NEG_PRIVILEGE(r)) { + *ret_state = "inaccessible"; + *ret_color = ansi_highlight_yellow(); + *ret_size = UINT64_MAX; + return; + } + goto error; + } + + if (fstat(fd, &st) < 0) + goto error; + + if (!S_ISREG(st.st_mode)) + goto error; + + *ret_state = "present"; + *ret_color = NULL; + *ret_size = st.st_size; + return; + +error: + *ret_state = "error"; + *ret_color = ansi_highlight_red(); + *ret_size = UINT64_MAX; +} + +#define RETRIEVE(d, l, name, arg) \ + { \ + int _r = retrieve(d, l, name, &arg); \ + if (_r < 0) \ + return _r; \ + if (_r > 0) \ + continue; \ + } + +static int print_list(FILE *file, sd_journal *j, Table *t) { + _cleanup_free_ char + *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, + *sgnl = NULL, *exe = NULL, *comm = NULL, + *filename = NULL, *truncated = NULL; + const void *d; + size_t l; + usec_t ts; + int r, signal_as_int = 0; + const char *present = NULL, *color = NULL; + uint64_t size = UINT64_MAX; + bool normal_coredump, has_inline_coredump; + uid_t uid_as_int = UID_INVALID; + gid_t gid_as_int = GID_INVALID; + pid_t pid_as_int = 0; + + assert(file); + assert(j); + assert(t); + + SD_JOURNAL_FOREACH_DATA(j, d, l) { + RETRIEVE(d, l, "MESSAGE_ID", mid); + RETRIEVE(d, l, "COREDUMP_PID", pid); + RETRIEVE(d, l, "COREDUMP_UID", uid); + RETRIEVE(d, l, "COREDUMP_GID", gid); + RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); + RETRIEVE(d, l, "COREDUMP_EXE", exe); + RETRIEVE(d, l, "COREDUMP_COMM", comm); + RETRIEVE(d, l, "COREDUMP_FILENAME", filename); + RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); + } + + /* Check for an inline coredump without copying the (potentially large) payload to heap. */ + has_inline_coredump = sd_journal_get_data(j, "COREDUMP", /* ret_data= */ NULL, /* ret_size= */ NULL) >= 0; + + if (!pid || !uid || !gid || !sgnl || !comm) { + log_warning("Found a coredump entry without mandatory fields (PID=%s, UID=%s, GID=%s, SIGNAL=%s, COMM=%s), ignoring.", + strna(pid), strna(uid), strna(gid), strna(sgnl), strna(comm)); + return 0; + } + + (void) parse_uid(uid, &uid_as_int); + (void) parse_gid(gid, &gid_as_int); + (void) parse_pid(pid, &pid_as_int); + signal_as_int = signal_from_string(sgnl); + + r = sd_journal_get_realtime_usec(j, &ts); + if (r < 0) + return log_error_errno(r, "Failed to get realtime timestamp: %m"); + + normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); + + if (filename) { + r = resolve_filename(arg_root, &filename); + if (r < 0) + return r; + + analyze_coredump_file(filename, &present, &color, &size); + } else if (has_inline_coredump) + present = "journal"; + else if (normal_coredump) { + present = "none"; + color = ansi_grey(); + } else + present = NULL; + + if (STRPTR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) + present = "truncated"; + + r = table_add_many( + t, + TABLE_TIMESTAMP, ts, + TABLE_PID, pid_as_int, + TABLE_UID, uid_as_int, + TABLE_GID, gid_as_int, + TABLE_SIGNAL, normal_coredump ? signal_as_int : 0, + TABLE_STRING, present, + TABLE_SET_COLOR, color, + TABLE_STRING, exe ?: comm, + TABLE_SIZE, size); + if (r < 0) + return table_log_add_error(r); + + return 0; +} + +typedef enum CoredumpField { + COREDUMP_FIELD_MID, + COREDUMP_FIELD_PID, + COREDUMP_FIELD_UID, + COREDUMP_FIELD_GID, + COREDUMP_FIELD_SGNL, + COREDUMP_FIELD_CODE, + COREDUMP_FIELD_EXE, + COREDUMP_FIELD_COMM, + COREDUMP_FIELD_CMDLINE, + COREDUMP_FIELD_HOSTNAME, + COREDUMP_FIELD_UNIT, + COREDUMP_FIELD_USER_UNIT, + COREDUMP_FIELD_SESSION, + COREDUMP_FIELD_OWNER_UID, + COREDUMP_FIELD_SLICE, + COREDUMP_FIELD_CGROUP, + COREDUMP_FIELD_TIMESTAMP, + COREDUMP_FIELD_FILENAME, + COREDUMP_FIELD_TRUNCATED, + COREDUMP_FIELD_PKGMETA_NAME, + COREDUMP_FIELD_PKGMETA_VERSION, + COREDUMP_FIELD_PKGMETA_JSON, + COREDUMP_FIELD_TID, + COREDUMP_FIELD_THREAD_NAME, + COREDUMP_FIELD_BOOT_ID, + COREDUMP_FIELD_MACHINE_ID, + COREDUMP_FIELD_MESSAGE, + _COREDUMP_FIELD_MAX, +} CoredumpField; + +static const char* const coredump_field_table[_COREDUMP_FIELD_MAX] = { + [COREDUMP_FIELD_MID] = "MESSAGE_ID", + [COREDUMP_FIELD_PID] = "COREDUMP_PID", + [COREDUMP_FIELD_UID] = "COREDUMP_UID", + [COREDUMP_FIELD_GID] = "COREDUMP_GID", + [COREDUMP_FIELD_SGNL] = "COREDUMP_SIGNAL", + [COREDUMP_FIELD_CODE] = "COREDUMP_CODE", + [COREDUMP_FIELD_EXE] = "COREDUMP_EXE", + [COREDUMP_FIELD_COMM] = "COREDUMP_COMM", + [COREDUMP_FIELD_CMDLINE] = "COREDUMP_CMDLINE", + [COREDUMP_FIELD_HOSTNAME] = "COREDUMP_HOSTNAME", + [COREDUMP_FIELD_UNIT] = "COREDUMP_UNIT", + [COREDUMP_FIELD_USER_UNIT] = "COREDUMP_USER_UNIT", + [COREDUMP_FIELD_SESSION] = "COREDUMP_SESSION", + [COREDUMP_FIELD_OWNER_UID] = "COREDUMP_OWNER_UID", + [COREDUMP_FIELD_SLICE] = "COREDUMP_SLICE", + [COREDUMP_FIELD_CGROUP] = "COREDUMP_CGROUP", + [COREDUMP_FIELD_TIMESTAMP] = "COREDUMP_TIMESTAMP", + [COREDUMP_FIELD_FILENAME] = "COREDUMP_FILENAME", + [COREDUMP_FIELD_TRUNCATED] = "COREDUMP_TRUNCATED", + [COREDUMP_FIELD_PKGMETA_NAME] = "COREDUMP_PACKAGE_NAME", + [COREDUMP_FIELD_PKGMETA_VERSION] = "COREDUMP_PACKAGE_VERSION", + [COREDUMP_FIELD_PKGMETA_JSON] = "COREDUMP_PACKAGE_JSON", + [COREDUMP_FIELD_TID] = "COREDUMP_TID", + [COREDUMP_FIELD_THREAD_NAME] = "COREDUMP_THREAD_NAME", + [COREDUMP_FIELD_BOOT_ID] = "_BOOT_ID", + [COREDUMP_FIELD_MACHINE_ID] = "_MACHINE_ID", + [COREDUMP_FIELD_MESSAGE] = "MESSAGE", +}; + +typedef struct CoredumpFields { + char *fields[_COREDUMP_FIELD_MAX]; + + bool normal_coredump; + const char *storage_state; /* points to a static string, not owned */ + const char *storage_color; /* points to a static string, not owned */ + uint64_t disk_size; + sd_json_variant *package_json; +} CoredumpFields; + +static void coredump_fields_done(CoredumpFields *f) { + assert(f); + + free_many_charp(f->fields, _COREDUMP_FIELD_MAX); + sd_json_variant_unref(f->package_json); +} + +static int coredump_fields_load(sd_journal *j, CoredumpFields *f) { + const void *d; + size_t l; + int r; + + assert(j); + assert(f); + + (void) sd_journal_set_data_threshold(j, 0); + + SD_JOURNAL_FOREACH_DATA(j, d, l) + for (CoredumpField i = 0; i < _COREDUMP_FIELD_MAX; i++) { + r = retrieve(d, l, coredump_field_table[i], &f->fields[i]); + if (r < 0) + return r; + if (r > 0) + break; + } + + f->normal_coredump = streq_ptr(f->fields[COREDUMP_FIELD_MID], SD_MESSAGE_COREDUMP_STR); + + if (f->fields[COREDUMP_FIELD_FILENAME]) { + r = resolve_filename(arg_root, &f->fields[COREDUMP_FIELD_FILENAME]); + if (r < 0) + return r; + + analyze_coredump_file(f->fields[COREDUMP_FIELD_FILENAME], &f->storage_state, &f->storage_color, &f->disk_size); + + if (STRPTR_IN_SET(f->storage_state, "present", "journal") && + f->fields[COREDUMP_FIELD_TRUNCATED] && + parse_boolean(f->fields[COREDUMP_FIELD_TRUNCATED]) > 0) + f->storage_state = "truncated"; + + } else if (sd_journal_get_data(j, "COREDUMP", /* ret_data= */ NULL, /* ret_size= */ NULL) >= 0) + f->storage_state = "journal"; + else + f->storage_state = "none"; + + if (f->fields[COREDUMP_FIELD_PKGMETA_JSON]) { + r = sd_json_parse(f->fields[COREDUMP_FIELD_PKGMETA_JSON], + SD_JSON_PARSE_MUST_BE_OBJECT, + &f->package_json, + /* reterr_line= */ NULL, + /* reterr_column= */ NULL); + if (r < 0) { + _cleanup_free_ char *esc = cescape(f->fields[COREDUMP_FIELD_PKGMETA_JSON]); + log_warning_errno(r, "Failed to parse COREDUMP_PACKAGE_JSON \"%s\", ignoring: %m", strnull(esc)); + } + } + + return 0; +} + +int print_info(FILE *file, sd_journal *j, bool need_space) { + _cleanup_(coredump_fields_done) CoredumpFields f = { + .disk_size = UINT64_MAX, + }; + int r; + + assert(file); + assert(j); + + r = coredump_fields_load(j, &f); + if (r < 0) + return r; + + if (need_space) + fputs("\n", file); + + if (f.fields[COREDUMP_FIELD_COMM]) + fprintf(file, + " PID: %s%s%s (%s)\n", + ansi_highlight(), strna(f.fields[COREDUMP_FIELD_PID]), ansi_normal(), f.fields[COREDUMP_FIELD_COMM]); + else + fprintf(file, + " PID: %s%s%s\n", + ansi_highlight(), strna(f.fields[COREDUMP_FIELD_PID]), ansi_normal()); + + if (f.fields[COREDUMP_FIELD_TID]) { + if (f.fields[COREDUMP_FIELD_THREAD_NAME]) + fprintf(file, " TID: %s (%s)\n", f.fields[COREDUMP_FIELD_TID], f.fields[COREDUMP_FIELD_THREAD_NAME]); + else + fprintf(file, " TID: %s\n", f.fields[COREDUMP_FIELD_TID]); + } + + if (f.fields[COREDUMP_FIELD_UID]) { + uid_t n; + + if (parse_uid(f.fields[COREDUMP_FIELD_UID], &n) >= 0) { + _cleanup_free_ char *u = NULL; + + u = uid_to_name(n); + fprintf(file, + " UID: %s (%s)\n", + f.fields[COREDUMP_FIELD_UID], u); + } else + fprintf(file, + " UID: %s\n", + f.fields[COREDUMP_FIELD_UID]); + } + + if (f.fields[COREDUMP_FIELD_GID]) { + gid_t n; + + if (parse_gid(f.fields[COREDUMP_FIELD_GID], &n) >= 0) { + _cleanup_free_ char *g = NULL; + + g = gid_to_name(n); + fprintf(file, + " GID: %s (%s)\n", + f.fields[COREDUMP_FIELD_GID], g); + } else + fprintf(file, + " GID: %s\n", + f.fields[COREDUMP_FIELD_GID]); + } + + if (f.fields[COREDUMP_FIELD_SGNL]) { + int sig; + const char *name = f.normal_coredump ? "Signal" : "Reason"; + + if (f.normal_coredump && safe_atoi(f.fields[COREDUMP_FIELD_SGNL], &sig) >= 0) { + fprintf(file, " %s: %s (%s)", name, f.fields[COREDUMP_FIELD_SGNL], signal_to_string(sig)); + + if (f.fields[COREDUMP_FIELD_CODE]) { + int n; + const char *s; + + if (safe_atoi(f.fields[COREDUMP_FIELD_CODE], &n) >= 0) + s = signal_code_to_string(sig, n); + else + s = NULL; + + fprintf(file, " si_code: %s", s ?: f.fields[COREDUMP_FIELD_CODE]); + } + + fputc('\n', file); + } else + fprintf(file, " %s: %s\n", name, f.fields[COREDUMP_FIELD_SGNL]); + } + + if (f.fields[COREDUMP_FIELD_TIMESTAMP]) { + usec_t u; + + r = safe_atou64(f.fields[COREDUMP_FIELD_TIMESTAMP], &u); + if (r >= 0) + fprintf(file, " Timestamp: %s (%s)\n", + FORMAT_TIMESTAMP(u), FORMAT_TIMESTAMP_RELATIVE(u)); + else + fprintf(file, " Timestamp: %s\n", f.fields[COREDUMP_FIELD_TIMESTAMP]); + } + + if (f.fields[COREDUMP_FIELD_CMDLINE]) + fprintf(file, " Command Line: %s\n", f.fields[COREDUMP_FIELD_CMDLINE]); + if (f.fields[COREDUMP_FIELD_EXE]) + fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), f.fields[COREDUMP_FIELD_EXE], ansi_normal()); + if (f.fields[COREDUMP_FIELD_CGROUP]) + fprintf(file, " Control Group: %s\n", f.fields[COREDUMP_FIELD_CGROUP]); + if (f.fields[COREDUMP_FIELD_UNIT]) + fprintf(file, " Unit: %s\n", f.fields[COREDUMP_FIELD_UNIT]); + if (f.fields[COREDUMP_FIELD_USER_UNIT]) + fprintf(file, " User Unit: %s\n", f.fields[COREDUMP_FIELD_USER_UNIT]); + if (f.fields[COREDUMP_FIELD_SLICE]) + fprintf(file, " Slice: %s\n", f.fields[COREDUMP_FIELD_SLICE]); + if (f.fields[COREDUMP_FIELD_SESSION]) + fprintf(file, " Session: %s\n", f.fields[COREDUMP_FIELD_SESSION]); + if (f.fields[COREDUMP_FIELD_OWNER_UID]) { + uid_t n; + + if (parse_uid(f.fields[COREDUMP_FIELD_OWNER_UID], &n) >= 0) { + _cleanup_free_ char *u = NULL; + + u = uid_to_name(n); + fprintf(file, + " Owner UID: %s (%s)\n", + f.fields[COREDUMP_FIELD_OWNER_UID], u); + } else + fprintf(file, + " Owner UID: %s\n", + f.fields[COREDUMP_FIELD_OWNER_UID]); + } + if (f.fields[COREDUMP_FIELD_BOOT_ID]) + fprintf(file, " Boot ID: %s\n", f.fields[COREDUMP_FIELD_BOOT_ID]); + if (f.fields[COREDUMP_FIELD_MACHINE_ID]) + fprintf(file, " Machine ID: %s\n", f.fields[COREDUMP_FIELD_MACHINE_ID]); + if (f.fields[COREDUMP_FIELD_HOSTNAME]) + fprintf(file, " Hostname: %s\n", f.fields[COREDUMP_FIELD_HOSTNAME]); + + if (f.fields[COREDUMP_FIELD_FILENAME]) { + fprintf(file, + " Storage: %s%s (%s)%s\n", + strempty(f.storage_color), + f.fields[COREDUMP_FIELD_FILENAME], + f.storage_state, + ansi_normal()); + + if (f.disk_size != UINT64_MAX) + fprintf(file, " Size on Disk: %s\n", FORMAT_BYTES(f.disk_size)); + } else + fprintf(file, " Storage: %s\n", f.storage_state); + + if (f.fields[COREDUMP_FIELD_PKGMETA_NAME] && f.fields[COREDUMP_FIELD_PKGMETA_VERSION]) + fprintf(file, " Package: %s/%s\n", f.fields[COREDUMP_FIELD_PKGMETA_NAME], f.fields[COREDUMP_FIELD_PKGMETA_VERSION]); + + /* Print out the build-id of the 'main' ELF module, by matching the JSON key + * with the 'exe' field. */ + if (f.fields[COREDUMP_FIELD_EXE] && f.package_json) { + const char *module_name; + sd_json_variant *module_json; + + JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, f.package_json) { + sd_json_variant *build_id; + + /* We only print the build-id for the 'main' ELF module */ + if (!path_equal_filename(module_name, f.fields[COREDUMP_FIELD_EXE])) + continue; + + build_id = sd_json_variant_by_key(module_json, "buildId"); + if (build_id) + fprintf(file, " build-id: %s\n", sd_json_variant_string(build_id)); + + break; + } + } + + if (f.fields[COREDUMP_FIELD_MESSAGE]) { + _cleanup_free_ char *m = NULL; + + m = strreplace(f.fields[COREDUMP_FIELD_MESSAGE], "\n", "\n "); + + fprintf(file, " Message: %s\n", strstrip(m ?: f.fields[COREDUMP_FIELD_MESSAGE])); + } + + return 0; +} + +static int print_info_json(FILE *file, sd_journal *j) { + _cleanup_(coredump_fields_done) CoredumpFields f = { + .disk_size = UINT64_MAX, + }; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + pid_t pid_as_int = 0, tid_as_int = 0; + uid_t uid_as_int = UID_INVALID, owner_uid_as_int = UID_INVALID; + gid_t gid_as_int = GID_INVALID; + int sig_as_int = 0, code_as_int = 0; + bool code_is_valid = false; + usec_t ts = USEC_INFINITY; + int r; + + assert(file); + assert(j); + + r = coredump_fields_load(j, &f); + if (r < 0) + return r; + + if (f.fields[COREDUMP_FIELD_PID]) + (void) parse_pid(f.fields[COREDUMP_FIELD_PID], &pid_as_int); + if (f.fields[COREDUMP_FIELD_TID]) + (void) parse_pid(f.fields[COREDUMP_FIELD_TID], &tid_as_int); + if (f.fields[COREDUMP_FIELD_UID]) + (void) parse_uid(f.fields[COREDUMP_FIELD_UID], &uid_as_int); + if (f.fields[COREDUMP_FIELD_GID]) + (void) parse_gid(f.fields[COREDUMP_FIELD_GID], &gid_as_int); + if (f.fields[COREDUMP_FIELD_OWNER_UID]) + (void) parse_uid(f.fields[COREDUMP_FIELD_OWNER_UID], &owner_uid_as_int); + if (f.normal_coredump && f.fields[COREDUMP_FIELD_SGNL]) + (void) safe_atoi(f.fields[COREDUMP_FIELD_SGNL], &sig_as_int); + if (f.normal_coredump && f.fields[COREDUMP_FIELD_CODE]) + code_is_valid = safe_atoi(f.fields[COREDUMP_FIELD_CODE], &code_as_int) >= 0; + if (f.fields[COREDUMP_FIELD_TIMESTAMP]) + (void) safe_atou64(f.fields[COREDUMP_FIELD_TIMESTAMP], &ts); + + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(pid_is_valid(pid_as_int), "PID", SD_JSON_BUILD_UNSIGNED(pid_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(!pid_is_valid(pid_as_int) && !!f.fields[COREDUMP_FIELD_PID], "PID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PID])), + SD_JSON_BUILD_PAIR_CONDITION(pid_is_valid(tid_as_int), "TID", SD_JSON_BUILD_UNSIGNED(tid_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(!pid_is_valid(tid_as_int) && !!f.fields[COREDUMP_FIELD_TID], "TID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_TID])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_THREAD_NAME], "ThreadName", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_THREAD_NAME])), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid_as_int), "UID", SD_JSON_BUILD_UNSIGNED(uid_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(!uid_is_valid(uid_as_int) && !!f.fields[COREDUMP_FIELD_UID], "UID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_UID])), + SD_JSON_BUILD_PAIR_CONDITION(gid_is_valid(gid_as_int), "GID", SD_JSON_BUILD_UNSIGNED(gid_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(!gid_is_valid(gid_as_int) && !!f.fields[COREDUMP_FIELD_GID], "GID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_GID])), + SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int > 0, "Signal", SD_JSON_BUILD_INTEGER(sig_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int > 0 && !!signal_to_string(sig_as_int), "SignalName", SD_JSON_BUILD_STRING(signal_to_string(sig_as_int))), + SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int <= 0 && !!f.fields[COREDUMP_FIELD_SGNL], "Signal", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SGNL])), + SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && code_is_valid, "SignalCode", SD_JSON_BUILD_INTEGER(code_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && !code_is_valid && !!f.fields[COREDUMP_FIELD_CODE], "SignalCode", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CODE])), + SD_JSON_BUILD_PAIR_CONDITION(!f.normal_coredump && !!f.fields[COREDUMP_FIELD_SGNL], "Reason", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SGNL])), + SD_JSON_BUILD_PAIR_CONDITION(ts != USEC_INFINITY, "Timestamp", SD_JSON_BUILD_UNSIGNED(ts)), + SD_JSON_BUILD_PAIR_CONDITION(ts == USEC_INFINITY && !!f.fields[COREDUMP_FIELD_TIMESTAMP], "Timestamp", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_TIMESTAMP])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_EXE], "Executable", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_EXE])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_COMM], "Command", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_COMM])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_CMDLINE], "CommandLine", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CMDLINE])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_CGROUP], "ControlGroup", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CGROUP])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_UNIT], "Unit", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_UNIT])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_USER_UNIT], "UserUnit", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_USER_UNIT])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_SLICE], "Slice", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SLICE])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_SESSION], "Session", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SESSION])), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(owner_uid_as_int), "OwnerUID", SD_JSON_BUILD_UNSIGNED(owner_uid_as_int)), + SD_JSON_BUILD_PAIR_CONDITION(!uid_is_valid(owner_uid_as_int) && !!f.fields[COREDUMP_FIELD_OWNER_UID], "OwnerUID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_OWNER_UID])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_BOOT_ID], "BootID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_BOOT_ID])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_MACHINE_ID], "MachineID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_MACHINE_ID])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_HOSTNAME], "Hostname", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_HOSTNAME])), + SD_JSON_BUILD_PAIR("Storage", SD_JSON_BUILD_STRING(f.storage_state)), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_FILENAME], "Filename", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_FILENAME])), + SD_JSON_BUILD_PAIR_CONDITION(f.disk_size != UINT64_MAX, "DiskSize", SD_JSON_BUILD_UNSIGNED(f.disk_size)), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_PKGMETA_NAME], "PackageName", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PKGMETA_NAME])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_PKGMETA_VERSION], "PackageVersion", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PKGMETA_VERSION])), + SD_JSON_BUILD_PAIR_CONDITION(!!f.package_json, "Package", SD_JSON_BUILD_VARIANT(f.package_json)), + SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_MESSAGE], "Message", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_MESSAGE])))); + if (r < 0) + return log_error_errno(r, "Failed to build JSON object: %m"); + + r = sd_json_variant_dump(v, arg_json_format_flags, file, /* prefix= */ NULL); + if (r < 0) + return log_error_errno(r, "Failed to dump JSON object: %m"); + + return 0; +} + +int print_entry(sd_journal *j, size_t n_found, Table *t) { + assert(j); + + if (t) + return print_list(stdout, j, t); + if (arg_field) + return print_field(stdout, j); + if (sd_json_format_enabled(arg_json_format_flags)) + return print_info_json(stdout, j); + return print_info(stdout, j, n_found > 0); +} diff --git a/src/coredump/coredumpctl-info.h b/src/coredump/coredumpctl-info.h new file mode 100644 index 0000000000..5caf2b55cc --- /dev/null +++ b/src/coredump/coredumpctl-info.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int print_info(FILE *file, sd_journal *j, bool need_space); +int print_entry(sd_journal *j, size_t n_found, Table *t); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 156179518b..db7ab5bb2f 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "sd-bus.h" @@ -21,22 +20,19 @@ #include "chase.h" #include "compress.h" #include "coredumpctl.h" +#include "coredumpctl-info.h" #include "coredumpctl-util.h" #include "dissect-image.h" #include "dlopen-note.h" -#include "errno-util.h" -#include "escape.h" #include "extract-word.h" #include "fd-util.h" #include "format-table.h" -#include "format-util.h" #include "fs-util.h" #include "glob-util.h" #include "image-policy.h" #include "io-util.h" #include "journal-internal.h" #include "journal-util.h" -#include "json-util.h" #include "log.h" #include "logs-show.h" #include "loop-util.h" @@ -54,7 +50,6 @@ #include "terminal-util.h" #include "time-util.h" #include "tmpfile-util.h" -#include "user-util.h" #include "verbs.h" #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) @@ -333,491 +328,6 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { return 1; } -static int print_field(FILE* file, sd_journal *j) { - const void *d; - size_t l; - - assert(file); - assert(j); - - assert(arg_field); - - /* A (user-specified) field may appear more than once for a given entry. - * We will print all of the occurrences. - * This is different below for fields that systemd-coredump uses, - * because they cannot meaningfully appear more than once. - */ - SD_JOURNAL_FOREACH_DATA(j, d, l) { - _cleanup_free_ char *value = NULL; - int r; - - r = retrieve(d, l, arg_field, &value); - if (r < 0) - return r; - if (r > 0) - fprintf(file, "%s\n", value); - } - - return 0; -} - -#define RETRIEVE(d, l, name, arg) \ - { \ - int _r = retrieve(d, l, name, &arg); \ - if (_r < 0) \ - return _r; \ - if (_r > 0) \ - continue; \ - } - -static void analyze_coredump_file( - const char *path, - const char **ret_state, - const char **ret_color, - uint64_t *ret_size) { - - _cleanup_close_ int fd = -EBADF; - struct stat st; - int r; - - assert(path); - assert(ret_state); - assert(ret_color); - assert(ret_size); - - fd = open(path, O_PATH|O_CLOEXEC); - if (fd < 0) { - if (errno == ENOENT) { - *ret_state = "missing"; - *ret_color = ansi_grey(); - *ret_size = UINT64_MAX; - return; - } - - r = -errno; - } else - r = access_fd(fd, R_OK); - if (r < 0) { - if (ERRNO_IS_PRIVILEGE(r)) { - *ret_state = "inaccessible"; - *ret_color = ansi_highlight_yellow(); - *ret_size = UINT64_MAX; - return; - } - goto error; - } - - if (fstat(fd, &st) < 0) - goto error; - - if (!S_ISREG(st.st_mode)) - goto error; - - *ret_state = "present"; - *ret_color = NULL; - *ret_size = st.st_size; - return; - -error: - *ret_state = "error"; - *ret_color = ansi_highlight_red(); - *ret_size = UINT64_MAX; -} - -static int print_list(FILE* file, sd_journal *j, Table *t) { - _cleanup_free_ char - *mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL, - *sgnl = NULL, *exe = NULL, *comm = NULL, - *filename = NULL, *truncated = NULL; - const void *d; - size_t l; - usec_t ts; - int r, signal_as_int = 0; - const char *present = NULL, *color = NULL; - uint64_t size = UINT64_MAX; - bool normal_coredump, has_inline_coredump; - uid_t uid_as_int = UID_INVALID; - gid_t gid_as_int = GID_INVALID; - pid_t pid_as_int = 0; - - assert(file); - assert(j); - assert(t); - - SD_JOURNAL_FOREACH_DATA(j, d, l) { - RETRIEVE(d, l, "MESSAGE_ID", mid); - RETRIEVE(d, l, "COREDUMP_PID", pid); - RETRIEVE(d, l, "COREDUMP_UID", uid); - RETRIEVE(d, l, "COREDUMP_GID", gid); - RETRIEVE(d, l, "COREDUMP_SIGNAL", sgnl); - RETRIEVE(d, l, "COREDUMP_EXE", exe); - RETRIEVE(d, l, "COREDUMP_COMM", comm); - RETRIEVE(d, l, "COREDUMP_FILENAME", filename); - RETRIEVE(d, l, "COREDUMP_TRUNCATED", truncated); - } - - /* Check for an inline coredump without copying the (potentially large) payload to heap. */ - has_inline_coredump = sd_journal_get_data(j, "COREDUMP", NULL, NULL) >= 0; - - if (!pid || !uid || !gid || !sgnl || !comm) { - log_warning("Found a coredump entry without mandatory fields (PID=%s, UID=%s, GID=%s, SIGNAL=%s, COMM=%s), ignoring.", - strna(pid), strna(uid), strna(gid), strna(sgnl), strna(comm)); - return 0; - } - - (void) parse_uid(uid, &uid_as_int); - (void) parse_gid(gid, &gid_as_int); - (void) parse_pid(pid, &pid_as_int); - signal_as_int = signal_from_string(sgnl); - - r = sd_journal_get_realtime_usec(j, &ts); - if (r < 0) - return log_error_errno(r, "Failed to get realtime timestamp: %m"); - - normal_coredump = streq_ptr(mid, SD_MESSAGE_COREDUMP_STR); - - if (filename) { - r = resolve_filename(arg_root, &filename); - if (r < 0) - return r; - - analyze_coredump_file(filename, &present, &color, &size); - } else if (has_inline_coredump) - present = "journal"; - else if (normal_coredump) { - present = "none"; - color = ansi_grey(); - } else - present = NULL; - - if (STRPTR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) - present = "truncated"; - - r = table_add_many( - t, - TABLE_TIMESTAMP, ts, - TABLE_PID, pid_as_int, - TABLE_UID, uid_as_int, - TABLE_GID, gid_as_int, - TABLE_SIGNAL, normal_coredump ? signal_as_int : 0, - TABLE_STRING, present, - TABLE_SET_COLOR, color, - TABLE_STRING, exe ?: comm, - TABLE_SIZE, size); - if (r < 0) - return table_log_add_error(r); - - return 0; -} - -typedef enum CoredumpField { - COREDUMP_FIELD_MID, - COREDUMP_FIELD_PID, - COREDUMP_FIELD_UID, - COREDUMP_FIELD_GID, - COREDUMP_FIELD_SGNL, - COREDUMP_FIELD_CODE, - COREDUMP_FIELD_EXE, - COREDUMP_FIELD_COMM, - COREDUMP_FIELD_CMDLINE, - COREDUMP_FIELD_HOSTNAME, - COREDUMP_FIELD_UNIT, - COREDUMP_FIELD_USER_UNIT, - COREDUMP_FIELD_SESSION, - COREDUMP_FIELD_OWNER_UID, - COREDUMP_FIELD_SLICE, - COREDUMP_FIELD_CGROUP, - COREDUMP_FIELD_TIMESTAMP, - COREDUMP_FIELD_FILENAME, - COREDUMP_FIELD_TRUNCATED, - COREDUMP_FIELD_PKGMETA_NAME, - COREDUMP_FIELD_PKGMETA_VERSION, - COREDUMP_FIELD_PKGMETA_JSON, - COREDUMP_FIELD_TID, - COREDUMP_FIELD_THREAD_NAME, - COREDUMP_FIELD_BOOT_ID, - COREDUMP_FIELD_MACHINE_ID, - COREDUMP_FIELD_MESSAGE, - _COREDUMP_FIELD_MAX, -} CoredumpField; - -static const char* const coredump_field_table[_COREDUMP_FIELD_MAX] = { - [COREDUMP_FIELD_MID] = "MESSAGE_ID", - [COREDUMP_FIELD_PID] = "COREDUMP_PID", - [COREDUMP_FIELD_UID] = "COREDUMP_UID", - [COREDUMP_FIELD_GID] = "COREDUMP_GID", - [COREDUMP_FIELD_SGNL] = "COREDUMP_SIGNAL", - [COREDUMP_FIELD_CODE] = "COREDUMP_CODE", - [COREDUMP_FIELD_EXE] = "COREDUMP_EXE", - [COREDUMP_FIELD_COMM] = "COREDUMP_COMM", - [COREDUMP_FIELD_CMDLINE] = "COREDUMP_CMDLINE", - [COREDUMP_FIELD_HOSTNAME] = "COREDUMP_HOSTNAME", - [COREDUMP_FIELD_UNIT] = "COREDUMP_UNIT", - [COREDUMP_FIELD_USER_UNIT] = "COREDUMP_USER_UNIT", - [COREDUMP_FIELD_SESSION] = "COREDUMP_SESSION", - [COREDUMP_FIELD_OWNER_UID] = "COREDUMP_OWNER_UID", - [COREDUMP_FIELD_SLICE] = "COREDUMP_SLICE", - [COREDUMP_FIELD_CGROUP] = "COREDUMP_CGROUP", - [COREDUMP_FIELD_TIMESTAMP] = "COREDUMP_TIMESTAMP", - [COREDUMP_FIELD_FILENAME] = "COREDUMP_FILENAME", - [COREDUMP_FIELD_TRUNCATED] = "COREDUMP_TRUNCATED", - [COREDUMP_FIELD_PKGMETA_NAME] = "COREDUMP_PACKAGE_NAME", - [COREDUMP_FIELD_PKGMETA_VERSION] = "COREDUMP_PACKAGE_VERSION", - [COREDUMP_FIELD_PKGMETA_JSON] = "COREDUMP_PACKAGE_JSON", - [COREDUMP_FIELD_TID] = "COREDUMP_TID", - [COREDUMP_FIELD_THREAD_NAME] = "COREDUMP_THREAD_NAME", - [COREDUMP_FIELD_BOOT_ID] = "_BOOT_ID", - [COREDUMP_FIELD_MACHINE_ID] = "_MACHINE_ID", - [COREDUMP_FIELD_MESSAGE] = "MESSAGE", -}; - -typedef struct CoredumpFields { - char *fields[_COREDUMP_FIELD_MAX]; - - bool normal_coredump; - const char *storage_state; /* points to a static string, not owned */ - const char *storage_color; /* points to a static string, not owned */ - uint64_t disk_size; - sd_json_variant *package_json; -} CoredumpFields; - -static void coredump_fields_done(CoredumpFields *f) { - assert(f); - - free_many_charp(f->fields, _COREDUMP_FIELD_MAX); - sd_json_variant_unref(f->package_json); -} - -static int coredump_fields_load(sd_journal *j, CoredumpFields *ret) { - const void *d; - size_t l; - int r; - - assert(j); - assert(ret); - - (void) sd_journal_set_data_threshold(j, 0); - - SD_JOURNAL_FOREACH_DATA(j, d, l) { - for (CoredumpField i = 0; i < _COREDUMP_FIELD_MAX; i++) { - int k = retrieve(d, l, coredump_field_table[i], &ret->fields[i]); - if (k < 0) - return k; - if (k > 0) - break; - } - } - - ret->normal_coredump = streq_ptr(ret->fields[COREDUMP_FIELD_MID], SD_MESSAGE_COREDUMP_STR); - - if (ret->fields[COREDUMP_FIELD_FILENAME]) { - r = resolve_filename(arg_root, &ret->fields[COREDUMP_FIELD_FILENAME]); - if (r < 0) - return r; - - analyze_coredump_file(ret->fields[COREDUMP_FIELD_FILENAME], &ret->storage_state, &ret->storage_color, &ret->disk_size); - - if (STRPTR_IN_SET(ret->storage_state, "present", "journal") && ret->fields[COREDUMP_FIELD_TRUNCATED] && parse_boolean(ret->fields[COREDUMP_FIELD_TRUNCATED]) > 0) - ret->storage_state = "truncated"; - } else if (sd_journal_get_data(j, "COREDUMP", NULL, NULL) >= 0) - ret->storage_state = "journal"; - else - ret->storage_state = "none"; - - if (ret->fields[COREDUMP_FIELD_PKGMETA_JSON]) { - r = sd_json_parse(ret->fields[COREDUMP_FIELD_PKGMETA_JSON], SD_JSON_PARSE_MUST_BE_OBJECT, &ret->package_json, NULL, NULL); - if (r < 0) { - _cleanup_free_ char *esc = cescape(ret->fields[COREDUMP_FIELD_PKGMETA_JSON]); - log_warning_errno(r, "Failed to parse COREDUMP_PACKAGE_JSON \"%s\", ignoring: %m", strnull(esc)); - } - } - - return 0; -} - -static int print_info(FILE *file, sd_journal *j, bool need_space) { - _cleanup_(coredump_fields_done) CoredumpFields f = { - .disk_size = UINT64_MAX, - }; - int r; - - assert(file); - assert(j); - - r = coredump_fields_load(j, &f); - if (r < 0) - return r; - - if (need_space) - fputs("\n", file); - - if (f.fields[COREDUMP_FIELD_COMM]) - fprintf(file, - " PID: %s%s%s (%s)\n", - ansi_highlight(), strna(f.fields[COREDUMP_FIELD_PID]), ansi_normal(), f.fields[COREDUMP_FIELD_COMM]); - else - fprintf(file, - " PID: %s%s%s\n", - ansi_highlight(), strna(f.fields[COREDUMP_FIELD_PID]), ansi_normal()); - - if (f.fields[COREDUMP_FIELD_TID]) { - if (f.fields[COREDUMP_FIELD_THREAD_NAME]) - fprintf(file, " TID: %s (%s)\n", f.fields[COREDUMP_FIELD_TID], f.fields[COREDUMP_FIELD_THREAD_NAME]); - else - fprintf(file, " TID: %s\n", f.fields[COREDUMP_FIELD_TID]); - } - - if (f.fields[COREDUMP_FIELD_UID]) { - uid_t n; - - if (parse_uid(f.fields[COREDUMP_FIELD_UID], &n) >= 0) { - _cleanup_free_ char *u = NULL; - - u = uid_to_name(n); - fprintf(file, - " UID: %s (%s)\n", - f.fields[COREDUMP_FIELD_UID], u); - } else - fprintf(file, - " UID: %s\n", - f.fields[COREDUMP_FIELD_UID]); - } - - if (f.fields[COREDUMP_FIELD_GID]) { - gid_t n; - - if (parse_gid(f.fields[COREDUMP_FIELD_GID], &n) >= 0) { - _cleanup_free_ char *g = NULL; - - g = gid_to_name(n); - fprintf(file, - " GID: %s (%s)\n", - f.fields[COREDUMP_FIELD_GID], g); - } else - fprintf(file, - " GID: %s\n", - f.fields[COREDUMP_FIELD_GID]); - } - - if (f.fields[COREDUMP_FIELD_SGNL]) { - int sig; - const char *name = f.normal_coredump ? "Signal" : "Reason"; - - if (f.normal_coredump && safe_atoi(f.fields[COREDUMP_FIELD_SGNL], &sig) >= 0) { - fprintf(file, " %s: %s (%s)", name, f.fields[COREDUMP_FIELD_SGNL], signal_to_string(sig)); - - if (f.fields[COREDUMP_FIELD_CODE]) { - int n; - const char *s; - - if (safe_atoi(f.fields[COREDUMP_FIELD_CODE], &n) >= 0) - s = signal_code_to_string(sig, n); - else - s = NULL; - - fprintf(file, " si_code: %s", s ?: f.fields[COREDUMP_FIELD_CODE]); - } - - fputc('\n', file); - } else - fprintf(file, " %s: %s\n", name, f.fields[COREDUMP_FIELD_SGNL]); - } - - if (f.fields[COREDUMP_FIELD_TIMESTAMP]) { - usec_t u; - - r = safe_atou64(f.fields[COREDUMP_FIELD_TIMESTAMP], &u); - if (r >= 0) - fprintf(file, " Timestamp: %s (%s)\n", - FORMAT_TIMESTAMP(u), FORMAT_TIMESTAMP_RELATIVE(u)); - else - fprintf(file, " Timestamp: %s\n", f.fields[COREDUMP_FIELD_TIMESTAMP]); - } - - if (f.fields[COREDUMP_FIELD_CMDLINE]) - fprintf(file, " Command Line: %s\n", f.fields[COREDUMP_FIELD_CMDLINE]); - if (f.fields[COREDUMP_FIELD_EXE]) - fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), f.fields[COREDUMP_FIELD_EXE], ansi_normal()); - if (f.fields[COREDUMP_FIELD_CGROUP]) - fprintf(file, " Control Group: %s\n", f.fields[COREDUMP_FIELD_CGROUP]); - if (f.fields[COREDUMP_FIELD_UNIT]) - fprintf(file, " Unit: %s\n", f.fields[COREDUMP_FIELD_UNIT]); - if (f.fields[COREDUMP_FIELD_USER_UNIT]) - fprintf(file, " User Unit: %s\n", f.fields[COREDUMP_FIELD_USER_UNIT]); - if (f.fields[COREDUMP_FIELD_SLICE]) - fprintf(file, " Slice: %s\n", f.fields[COREDUMP_FIELD_SLICE]); - if (f.fields[COREDUMP_FIELD_SESSION]) - fprintf(file, " Session: %s\n", f.fields[COREDUMP_FIELD_SESSION]); - if (f.fields[COREDUMP_FIELD_OWNER_UID]) { - uid_t n; - - if (parse_uid(f.fields[COREDUMP_FIELD_OWNER_UID], &n) >= 0) { - _cleanup_free_ char *u = NULL; - - u = uid_to_name(n); - fprintf(file, - " Owner UID: %s (%s)\n", - f.fields[COREDUMP_FIELD_OWNER_UID], u); - } else - fprintf(file, - " Owner UID: %s\n", - f.fields[COREDUMP_FIELD_OWNER_UID]); - } - if (f.fields[COREDUMP_FIELD_BOOT_ID]) - fprintf(file, " Boot ID: %s\n", f.fields[COREDUMP_FIELD_BOOT_ID]); - if (f.fields[COREDUMP_FIELD_MACHINE_ID]) - fprintf(file, " Machine ID: %s\n", f.fields[COREDUMP_FIELD_MACHINE_ID]); - if (f.fields[COREDUMP_FIELD_HOSTNAME]) - fprintf(file, " Hostname: %s\n", f.fields[COREDUMP_FIELD_HOSTNAME]); - - if (f.fields[COREDUMP_FIELD_FILENAME]) { - fprintf(file, - " Storage: %s%s (%s)%s\n", - strempty(f.storage_color), - f.fields[COREDUMP_FIELD_FILENAME], - f.storage_state, - ansi_normal()); - - if (f.disk_size != UINT64_MAX) - fprintf(file, " Size on Disk: %s\n", FORMAT_BYTES(f.disk_size)); - } else - fprintf(file, " Storage: %s\n", f.storage_state); - - if (f.fields[COREDUMP_FIELD_PKGMETA_NAME] && f.fields[COREDUMP_FIELD_PKGMETA_VERSION]) - fprintf(file, " Package: %s/%s\n", f.fields[COREDUMP_FIELD_PKGMETA_NAME], f.fields[COREDUMP_FIELD_PKGMETA_VERSION]); - - /* Print out the build-id of the 'main' ELF module, by matching the JSON key - * with the 'exe' field. */ - if (f.fields[COREDUMP_FIELD_EXE] && f.package_json) { - const char *module_name; - sd_json_variant *module_json; - - JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, f.package_json) { - sd_json_variant *build_id; - - /* We only print the build-id for the 'main' ELF module */ - if (!path_equal_filename(module_name, f.fields[COREDUMP_FIELD_EXE])) - continue; - - build_id = sd_json_variant_by_key(module_json, "buildId"); - if (build_id) - fprintf(file, " build-id: %s\n", sd_json_variant_string(build_id)); - - break; - } - } - - if (f.fields[COREDUMP_FIELD_MESSAGE]) { - _cleanup_free_ char *m = NULL; - - m = strreplace(f.fields[COREDUMP_FIELD_MESSAGE], "\n", "\n "); - - fprintf(file, " Message: %s\n", strstrip(m ?: f.fields[COREDUMP_FIELD_MESSAGE])); - } - - return 0; -} - static int focus(sd_journal *j) { int r; @@ -832,108 +342,6 @@ static int focus(sd_journal *j) { return r; } -static int print_info_json(FILE *file, sd_journal *j) { - _cleanup_(coredump_fields_done) CoredumpFields f = { - .disk_size = UINT64_MAX, - }; - _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - pid_t pid_as_int = 0, tid_as_int = 0; - uid_t uid_as_int = UID_INVALID, owner_uid_as_int = UID_INVALID; - gid_t gid_as_int = GID_INVALID; - int sig_as_int = 0, code_as_int = 0; - bool code_is_valid = false; - usec_t ts = USEC_INFINITY; - int r; - - assert(file); - assert(j); - - r = coredump_fields_load(j, &f); - if (r < 0) - return r; - - if (f.fields[COREDUMP_FIELD_PID]) - (void) parse_pid(f.fields[COREDUMP_FIELD_PID], &pid_as_int); - if (f.fields[COREDUMP_FIELD_TID]) - (void) parse_pid(f.fields[COREDUMP_FIELD_TID], &tid_as_int); - if (f.fields[COREDUMP_FIELD_UID]) - (void) parse_uid(f.fields[COREDUMP_FIELD_UID], &uid_as_int); - if (f.fields[COREDUMP_FIELD_GID]) - (void) parse_gid(f.fields[COREDUMP_FIELD_GID], &gid_as_int); - if (f.fields[COREDUMP_FIELD_OWNER_UID]) - (void) parse_uid(f.fields[COREDUMP_FIELD_OWNER_UID], &owner_uid_as_int); - if (f.normal_coredump && f.fields[COREDUMP_FIELD_SGNL]) - (void) safe_atoi(f.fields[COREDUMP_FIELD_SGNL], &sig_as_int); - if (f.normal_coredump && f.fields[COREDUMP_FIELD_CODE]) - code_is_valid = safe_atoi(f.fields[COREDUMP_FIELD_CODE], &code_as_int) >= 0; - if (f.fields[COREDUMP_FIELD_TIMESTAMP]) - (void) safe_atou64(f.fields[COREDUMP_FIELD_TIMESTAMP], &ts); - - r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( - SD_JSON_BUILD_PAIR_CONDITION(pid_is_valid(pid_as_int), "PID", SD_JSON_BUILD_UNSIGNED(pid_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(!pid_is_valid(pid_as_int) && !!f.fields[COREDUMP_FIELD_PID], "PID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PID])), - SD_JSON_BUILD_PAIR_CONDITION(pid_is_valid(tid_as_int), "TID", SD_JSON_BUILD_UNSIGNED(tid_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(!pid_is_valid(tid_as_int) && !!f.fields[COREDUMP_FIELD_TID], "TID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_TID])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_THREAD_NAME], "ThreadName", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_THREAD_NAME])), - SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid_as_int), "UID", SD_JSON_BUILD_UNSIGNED(uid_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(!uid_is_valid(uid_as_int) && !!f.fields[COREDUMP_FIELD_UID], "UID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_UID])), - SD_JSON_BUILD_PAIR_CONDITION(gid_is_valid(gid_as_int), "GID", SD_JSON_BUILD_UNSIGNED(gid_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(!gid_is_valid(gid_as_int) && !!f.fields[COREDUMP_FIELD_GID], "GID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_GID])), - SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int > 0, "Signal", SD_JSON_BUILD_INTEGER(sig_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int > 0 && !!signal_to_string(sig_as_int), "SignalName", SD_JSON_BUILD_STRING(signal_to_string(sig_as_int))), - SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && sig_as_int <= 0 && !!f.fields[COREDUMP_FIELD_SGNL], "Signal", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SGNL])), - SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && code_is_valid, "SignalCode", SD_JSON_BUILD_INTEGER(code_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(f.normal_coredump && !code_is_valid && !!f.fields[COREDUMP_FIELD_CODE], "SignalCode", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CODE])), - SD_JSON_BUILD_PAIR_CONDITION(!f.normal_coredump && !!f.fields[COREDUMP_FIELD_SGNL], "Reason", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SGNL])), - SD_JSON_BUILD_PAIR_CONDITION(ts != USEC_INFINITY, "Timestamp", SD_JSON_BUILD_UNSIGNED(ts)), - SD_JSON_BUILD_PAIR_CONDITION(ts == USEC_INFINITY && !!f.fields[COREDUMP_FIELD_TIMESTAMP], "Timestamp", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_TIMESTAMP])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_EXE], "Executable", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_EXE])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_COMM], "Command", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_COMM])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_CMDLINE], "CommandLine", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CMDLINE])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_CGROUP], "ControlGroup", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_CGROUP])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_UNIT], "Unit", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_UNIT])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_USER_UNIT], "UserUnit", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_USER_UNIT])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_SLICE], "Slice", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SLICE])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_SESSION], "Session", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_SESSION])), - SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(owner_uid_as_int), "OwnerUID", SD_JSON_BUILD_UNSIGNED(owner_uid_as_int)), - SD_JSON_BUILD_PAIR_CONDITION(!uid_is_valid(owner_uid_as_int) && !!f.fields[COREDUMP_FIELD_OWNER_UID], "OwnerUID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_OWNER_UID])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_BOOT_ID], "BootID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_BOOT_ID])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_MACHINE_ID], "MachineID", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_MACHINE_ID])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_HOSTNAME], "Hostname", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_HOSTNAME])), - SD_JSON_BUILD_PAIR("Storage", SD_JSON_BUILD_STRING(f.storage_state)), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_FILENAME], "Filename", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_FILENAME])), - SD_JSON_BUILD_PAIR_CONDITION(f.disk_size != UINT64_MAX, "DiskSize", SD_JSON_BUILD_UNSIGNED(f.disk_size)), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_PKGMETA_NAME], "PackageName", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PKGMETA_NAME])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_PKGMETA_VERSION], "PackageVersion", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_PKGMETA_VERSION])), - SD_JSON_BUILD_PAIR_CONDITION(!!f.package_json, "Package", SD_JSON_BUILD_VARIANT(f.package_json)), - SD_JSON_BUILD_PAIR_CONDITION(!!f.fields[COREDUMP_FIELD_MESSAGE], "Message", SD_JSON_BUILD_STRING(f.fields[COREDUMP_FIELD_MESSAGE])))); - if (r < 0) - return log_error_errno(r, "Failed to build JSON object: %m"); - - r = sd_json_variant_dump(v, arg_json_format_flags, file, NULL); - if (r < 0) - return log_error_errno(r, "Failed to dump JSON object: %m"); - - return 0; -} - -static int print_entry( - sd_journal *j, - size_t n_found, - Table *t) { - - assert(j); - - if (t) - return print_list(stdout, j, t); - else if (arg_field) - return print_field(stdout, j); - else if (sd_json_format_enabled(arg_json_format_flags)) - return print_info_json(stdout, j); - else - return print_info(stdout, j, n_found > 0); -} - VERB(verb_dump_list, "list", "[MATCHES…]", VERB_ANY, VERB_ANY, VERB_DEFAULT, "List available coredumps"); VERB(verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, diff --git a/src/coredump/meson.build b/src/coredump/meson.build index 65530e5141..e677af5d6d 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -19,6 +19,7 @@ systemd_coredump_export_sources = files( ) coredumpctl_sources = files( 'coredumpctl.c', + 'coredumpctl-info.c', 'coredumpctl-util.c', ) From d75e0fb9323479cefd950296ef154da03ea73c76 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 06:18:34 +0900 Subject: [PATCH 04/16] coredumpctl: move acquire_journal() and focus() to coredumpctl-journal.c --- src/coredump/coredumpctl-journal.c | 119 +++++++++++++++++++++++++++++ src/coredump/coredumpctl-journal.h | 7 ++ src/coredump/coredumpctl.c | 113 +-------------------------- src/coredump/meson.build | 1 + 4 files changed, 128 insertions(+), 112 deletions(-) create mode 100644 src/coredump/coredumpctl-journal.c create mode 100644 src/coredump/coredumpctl-journal.h diff --git a/src/coredump/coredumpctl-journal.c b/src/coredump/coredumpctl-journal.c new file mode 100644 index 0000000000..a2c71a7b84 --- /dev/null +++ b/src/coredump/coredumpctl-journal.c @@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-messages.h" + +#include "coredumpctl.h" +#include "coredumpctl-journal.h" +#include "journal-internal.h" +#include "journal-util.h" +#include "log.h" +#include "parse-util.h" +#include "path-util.h" +#include "string-util.h" +#include "strv.h" + +static int add_match(sd_journal *j, const char *match) { + _cleanup_free_ char *p = NULL; + const char *field; + int r; + + if (strchr(match, '=')) + field = NULL; + else if (is_path(match)) { + r = path_make_absolute_cwd(match, &p); + if (r < 0) + return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); + + match = p; + field = "COREDUMP_EXE"; + } else if (parse_pid(match, NULL) >= 0) + field = "COREDUMP_PID"; + else + field = "COREDUMP_COMM"; + + log_debug("Adding match: %s%s%s", strempty(field), field ? "=" : "", match); + if (field) + r = journal_add_match_pair(j, field, match); + else + r = sd_journal_add_match(j, match, SIZE_MAX); + if (r < 0) + return log_error_errno(r, "Failed to add match \"%s%s%s\": %m", + strempty(field), field ? "=" : "", match); + + return 0; +} + +static int add_matches(sd_journal *j, char * const *matches) { + int r; + + r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, SIZE_MAX); + if (r < 0) + return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); + + r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, SIZE_MAX); + if (r < 0) + return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); + + STRV_FOREACH(match, matches) { + r = add_match(j, *match); + if (r < 0) + return r; + } + + return 0; +} + +int acquire_journal(sd_journal **ret, char * const *matches) { + _cleanup_(sd_journal_closep) sd_journal *j = NULL; + int r; + + assert(ret); + + if (arg_directory) { + r = sd_journal_open_directory(&j, arg_directory, SD_JOURNAL_ASSUME_IMMUTABLE); + if (r < 0) + return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); + } else if (arg_root) { + r = sd_journal_open_directory(&j, arg_root, SD_JOURNAL_OS_ROOT | SD_JOURNAL_ASSUME_IMMUTABLE); + if (r < 0) + return log_error_errno(r, "Failed to open journals in root directory: %s: %m", arg_root); + } else if (arg_file) { + r = sd_journal_open_files(&j, (const char**)arg_file, SD_JOURNAL_ASSUME_IMMUTABLE); + if (r < 0) + return log_error_errno(r, "Failed to open journal files: %m"); + } else { + r = sd_journal_open(&j, arg_all ? 0 : SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE); + if (r < 0) + return log_error_errno(r, "Failed to open journal: %m"); + } + + r = journal_access_check_and_warn(j, arg_quiet, true); + if (r < 0) + return r; + + r = add_matches(j, matches); + if (r < 0) + return r; + + if (DEBUG_LOGGING) { + _cleanup_free_ char *filter = journal_make_match_string(j); + log_debug("Journal filter: %s", strna(filter)); + } + + *ret = TAKE_PTR(j); + return 0; +} + +int focus(sd_journal *j) { + int r; + + r = sd_journal_seek_tail(j); + if (r == 0) + r = sd_journal_previous(j); + if (r < 0) + return log_error_errno(r, "Failed to search journal: %m"); + if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "No match found."); + + return 0; +} diff --git a/src/coredump/coredumpctl-journal.h b/src/coredump/coredumpctl-journal.h new file mode 100644 index 0000000000..9a2a31e37d --- /dev/null +++ b/src/coredump/coredumpctl-journal.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int acquire_journal(sd_journal **ret, char * const *matches); +int focus(sd_journal *j); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index db7ab5bb2f..0917aa6aaf 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -9,7 +9,6 @@ #include "sd-bus.h" #include "sd-journal.h" #include "sd-json.h" -#include "sd-messages.h" #include "alloc-util.h" #include "ansi-color.h" @@ -21,6 +20,7 @@ #include "compress.h" #include "coredumpctl.h" #include "coredumpctl-info.h" +#include "coredumpctl-journal.h" #include "coredumpctl-util.h" #include "dissect-image.h" #include "dlopen-note.h" @@ -31,8 +31,6 @@ #include "glob-util.h" #include "image-policy.h" #include "io-util.h" -#include "journal-internal.h" -#include "journal-util.h" #include "log.h" #include "logs-show.h" #include "loop-util.h" @@ -86,101 +84,6 @@ COMMAND( .pager_flags = &arg_pager_flags, ); -static int add_match(sd_journal *j, const char *match) { - _cleanup_free_ char *p = NULL; - const char *field; - int r; - - if (strchr(match, '=')) - field = NULL; - else if (is_path(match)) { - r = path_make_absolute_cwd(match, &p); - if (r < 0) - return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); - - match = p; - field = "COREDUMP_EXE"; - } else if (parse_pid(match, NULL) >= 0) - field = "COREDUMP_PID"; - else - field = "COREDUMP_COMM"; - - log_debug("Adding match: %s%s%s", strempty(field), field ? "=" : "", match); - if (field) - r = journal_add_match_pair(j, field, match); - else - r = sd_journal_add_match(j, match, SIZE_MAX); - if (r < 0) - return log_error_errno(r, "Failed to add match \"%s%s%s\": %m", - strempty(field), field ? "=" : "", match); - - return 0; -} - -static int add_matches(sd_journal *j, char **matches) { - int r; - - r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR, SIZE_MAX); - if (r < 0) - return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR); - - r = sd_journal_add_match(j, "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR, SIZE_MAX); - if (r < 0) - return log_error_errno(r, "Failed to add match \"%s\": %m", "MESSAGE_ID=" SD_MESSAGE_BACKTRACE_STR); - - STRV_FOREACH(match, matches) { - r = add_match(j, *match); - if (r < 0) - return r; - } - - return 0; -} - -static int acquire_journal(sd_journal **ret, char **matches) { - _cleanup_(sd_journal_closep) sd_journal *j = NULL; - int r; - - assert(ret); - - if (arg_directory) { - r = sd_journal_open_directory(&j, arg_directory, SD_JOURNAL_ASSUME_IMMUTABLE); - if (r < 0) - return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory); - } else if (arg_root) { - r = sd_journal_open_directory(&j, arg_root, SD_JOURNAL_OS_ROOT | SD_JOURNAL_ASSUME_IMMUTABLE); - if (r < 0) - return log_error_errno(r, "Failed to open journals in root directory: %s: %m", arg_root); - } else if (arg_file) { - r = sd_journal_open_files(&j, (const char**)arg_file, SD_JOURNAL_ASSUME_IMMUTABLE); - if (r < 0) - return log_error_errno(r, "Failed to open journal files: %m"); - } else { - r = sd_journal_open(&j, arg_all ? 0 : SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE); - if (r < 0) - return log_error_errno(r, "Failed to open journal: %m"); - } - - r = journal_access_check_and_warn(j, arg_quiet, true); - if (r < 0) - return r; - - r = add_matches(j, matches); - if (r < 0) - return r; - - if (DEBUG_LOGGING) { - _cleanup_free_ char *filter = NULL; - - filter = journal_make_match_string(j); - log_debug("Journal filter: %s", filter); - } - - *ret = TAKE_PTR(j); - - return 0; -} - VERB_COMMON_HELP_AUTO_HIDDEN(); static int parse_argv(int argc, char *argv[], char ***remaining_args) { @@ -328,20 +231,6 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { return 1; } -static int focus(sd_journal *j) { - int r; - - r = sd_journal_seek_tail(j); - if (r == 0) - r = sd_journal_previous(j); - if (r < 0) - return log_error_errno(r, "Failed to search journal: %m"); - if (r == 0) - return log_error_errno(SYNTHETIC_ERRNO(ESRCH), - "No match found."); - return r; -} - VERB(verb_dump_list, "list", "[MATCHES…]", VERB_ANY, VERB_ANY, VERB_DEFAULT, "List available coredumps"); VERB(verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, diff --git a/src/coredump/meson.build b/src/coredump/meson.build index e677af5d6d..0ace2c16b9 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -20,6 +20,7 @@ systemd_coredump_export_sources = files( coredumpctl_sources = files( 'coredumpctl.c', 'coredumpctl-info.c', + 'coredumpctl-journal.c', 'coredumpctl-util.c', ) From 3e24c014bff5005694bf461b2f2af44832264848 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 07:10:40 +0900 Subject: [PATCH 05/16] coredumpctl: move verb_dump_list() to coredumpctl-list.c --- src/coredump/coredumpctl-list.c | 118 ++++++++++++++++++++++++++++++++ src/coredump/coredumpctl-list.h | 6 ++ src/coredump/coredumpctl.c | 115 ++----------------------------- src/coredump/meson.build | 1 + 4 files changed, 130 insertions(+), 110 deletions(-) create mode 100644 src/coredump/coredumpctl-list.c create mode 100644 src/coredump/coredumpctl-list.h diff --git a/src/coredump/coredumpctl-list.c b/src/coredump/coredumpctl-list.c new file mode 100644 index 0000000000..08872110e8 --- /dev/null +++ b/src/coredump/coredumpctl-list.c @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "sd-journal.h" +#include "sd-json.h" + +#include "coredumpctl.h" +#include "coredumpctl-info.h" +#include "coredumpctl-journal.h" +#include "coredumpctl-list.h" +#include "format-table.h" +#include "string-util.h" +#include "strv.h" + +int verb_dump_list(int argc, char *argv[], uintptr_t _data, void *userdata) { + _cleanup_(sd_journal_closep) sd_journal *j = NULL; + _cleanup_(table_unrefp) Table *t = NULL; + size_t n_found = 0; + bool verb_is_info; + int r; + + verb_is_info = argc >= 1 && streq(argv[0], "info"); + + r = acquire_journal(&j, strv_skip(argv, 1)); + if (r < 0) + return r; + + /* The coredumps are likely compressed, and for just listing them we don't need to decompress them, + * so let's pick a fairly low data threshold here */ + (void) sd_journal_set_data_threshold(j, 4096); + + if (!verb_is_info && !arg_field) { + t = table_new("time", "pid", "uid", "gid", "sig", "corefile", "exe", "size"); + if (!t) + return log_oom(); + + (void) table_set_align_percent(t, TABLE_HEADER_CELL(1), 100); + (void) table_set_align_percent(t, TABLE_HEADER_CELL(2), 100); + (void) table_set_align_percent(t, TABLE_HEADER_CELL(3), 100); + (void) table_set_align_percent(t, TABLE_HEADER_CELL(7), 100); + + table_set_ersatz_string(t, TABLE_ERSATZ_DASH); + } else if (!sd_json_format_enabled(arg_json_format_flags)) + pager_open(arg_pager_flags); + + /* "info" without pattern implies "-1" */ + if ((arg_rows_max == 1 && arg_reverse) || (verb_is_info && argc == 1)) { + r = focus(j); + if (r < 0) + return r; + + r = print_entry(j, 0, t); + if (r < 0) + return r; + } else { + if (arg_since != USEC_INFINITY && !arg_reverse) + r = sd_journal_seek_realtime_usec(j, arg_since); + else if (arg_until != USEC_INFINITY && arg_reverse) + r = sd_journal_seek_realtime_usec(j, arg_until); + else if (arg_reverse) + r = sd_journal_seek_tail(j); + else + r = sd_journal_seek_head(j); + if (r < 0) + return log_error_errno(r, "Failed to seek to date: %m"); + + for (;;) { + if (!arg_reverse) + r = sd_journal_next(j); + else + r = sd_journal_previous(j); + if (r < 0) + return log_error_errno(r, "Failed to iterate through journal: %m"); + if (r == 0) + break; + + if (arg_until != USEC_INFINITY && !arg_reverse) { + usec_t usec; + + r = sd_journal_get_realtime_usec(j, &usec); + if (r < 0) + return log_error_errno(r, "Failed to determine timestamp: %m"); + if (usec > arg_until) + break; + } + + if (arg_since != USEC_INFINITY && arg_reverse) { + usec_t usec; + + r = sd_journal_get_realtime_usec(j, &usec); + if (r < 0) + return log_error_errno(r, "Failed to determine timestamp: %m"); + if (usec < arg_since) + break; + } + + r = print_entry(j, n_found++, t); + if (r < 0) + return r; + + if (arg_rows_max != SIZE_MAX && n_found >= arg_rows_max) + break; + } + + if (!arg_field && n_found <= 0) { + if (!arg_quiet) + log_notice("No coredumps found."); + return -ESRCH; + } + } + + if (t) { + r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); + if (r < 0) + return r; + } + + return 0; +} diff --git a/src/coredump/coredumpctl-list.h b/src/coredump/coredumpctl-list.h new file mode 100644 index 0000000000..9d61a2669b --- /dev/null +++ b/src/coredump/coredumpctl-list.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int verb_dump_list(int argc, char *argv[], uintptr_t _data, void *userdata); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 0917aa6aaf..80336f1a6c 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -21,12 +21,12 @@ #include "coredumpctl.h" #include "coredumpctl-info.h" #include "coredumpctl-journal.h" +#include "coredumpctl-list.h" #include "coredumpctl-util.h" #include "dissect-image.h" #include "dlopen-note.h" #include "extract-word.h" #include "fd-util.h" -#include "format-table.h" #include "fs-util.h" #include "glob-util.h" #include "image-policy.h" @@ -231,115 +231,10 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { return 1; } -VERB(verb_dump_list, "list", "[MATCHES…]", VERB_ANY, VERB_ANY, VERB_DEFAULT, - "List available coredumps"); -VERB(verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, - "Show detailed information about one or more coredumps"); -static int verb_dump_list(int argc, char *argv[], uintptr_t _data, void *userdata) { - _cleanup_(sd_journal_closep) sd_journal *j = NULL; - _cleanup_(table_unrefp) Table *t = NULL; - size_t n_found = 0; - bool verb_is_info; - int r; - - verb_is_info = argc >= 1 && streq(argv[0], "info"); - - r = acquire_journal(&j, strv_skip(argv, 1)); - if (r < 0) - return r; - - /* The coredumps are likely compressed, and for just listing them we don't need to decompress them, - * so let's pick a fairly low data threshold here */ - (void) sd_journal_set_data_threshold(j, 4096); - - if (!verb_is_info && !arg_field) { - t = table_new("time", "pid", "uid", "gid", "sig", "corefile", "exe", "size"); - if (!t) - return log_oom(); - - (void) table_set_align_percent(t, TABLE_HEADER_CELL(1), 100); - (void) table_set_align_percent(t, TABLE_HEADER_CELL(2), 100); - (void) table_set_align_percent(t, TABLE_HEADER_CELL(3), 100); - (void) table_set_align_percent(t, TABLE_HEADER_CELL(7), 100); - - table_set_ersatz_string(t, TABLE_ERSATZ_DASH); - } else if (!sd_json_format_enabled(arg_json_format_flags)) - pager_open(arg_pager_flags); - - /* "info" without pattern implies "-1" */ - if ((arg_rows_max == 1 && arg_reverse) || (verb_is_info && argc == 1)) { - r = focus(j); - if (r < 0) - return r; - - r = print_entry(j, 0, t); - if (r < 0) - return r; - } else { - if (arg_since != USEC_INFINITY && !arg_reverse) - r = sd_journal_seek_realtime_usec(j, arg_since); - else if (arg_until != USEC_INFINITY && arg_reverse) - r = sd_journal_seek_realtime_usec(j, arg_until); - else if (arg_reverse) - r = sd_journal_seek_tail(j); - else - r = sd_journal_seek_head(j); - if (r < 0) - return log_error_errno(r, "Failed to seek to date: %m"); - - for (;;) { - if (!arg_reverse) - r = sd_journal_next(j); - else - r = sd_journal_previous(j); - if (r < 0) - return log_error_errno(r, "Failed to iterate through journal: %m"); - if (r == 0) - break; - - if (arg_until != USEC_INFINITY && !arg_reverse) { - usec_t usec; - - r = sd_journal_get_realtime_usec(j, &usec); - if (r < 0) - return log_error_errno(r, "Failed to determine timestamp: %m"); - if (usec > arg_until) - break; - } - - if (arg_since != USEC_INFINITY && arg_reverse) { - usec_t usec; - - r = sd_journal_get_realtime_usec(j, &usec); - if (r < 0) - return log_error_errno(r, "Failed to determine timestamp: %m"); - if (usec < arg_since) - break; - } - - r = print_entry(j, n_found++, t); - if (r < 0) - return r; - - if (arg_rows_max != SIZE_MAX && n_found >= arg_rows_max) - break; - } - - if (!arg_field && n_found <= 0) { - if (!arg_quiet) - log_notice("No coredumps found."); - return -ESRCH; - } - } - - if (t) { - r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend); - if (r < 0) - return r; - } - - return 0; -} +VERB_SCOPE(, verb_dump_list, "list", "[MATCHES…]", VERB_ANY, VERB_ANY, VERB_DEFAULT, + "List available coredumps"); +VERB_SCOPE(, verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, + "Show detailed information about one or more coredumps"); static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { const char *data; diff --git a/src/coredump/meson.build b/src/coredump/meson.build index 0ace2c16b9..9379affb7f 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -21,6 +21,7 @@ coredumpctl_sources = files( 'coredumpctl.c', 'coredumpctl-info.c', 'coredumpctl-journal.c', + 'coredumpctl-list.c', 'coredumpctl-util.c', ) From ea75664af1dd20097d8af6f8672c5ff4bf63bef2 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 05:56:46 +0900 Subject: [PATCH 06/16] coredumpctl: move verb_dump_core and save_core() to coredumpctl-core.c --- src/coredump/coredumpctl-core.c | 194 ++++++++++++++++++++++++++++++++ src/coredump/coredumpctl-core.h | 8 ++ src/coredump/coredumpctl.c | 187 +----------------------------- src/coredump/meson.build | 1 + 4 files changed, 206 insertions(+), 184 deletions(-) create mode 100644 src/coredump/coredumpctl-core.c create mode 100644 src/coredump/coredumpctl-core.h diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c new file mode 100644 index 0000000000..4baaadd4b5 --- /dev/null +++ b/src/coredump/coredumpctl-core.c @@ -0,0 +1,194 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +#include "sd-journal.h" + +#include "chase.h" +#include "compress.h" +#include "coredumpctl.h" +#include "coredumpctl-core.h" +#include "coredumpctl-info.h" +#include "coredumpctl-journal.h" +#include "coredumpctl-util.h" +#include "fd-util.h" +#include "fs-util.h" +#include "io-util.h" +#include "log.h" +#include "path-util.h" +#include "string-util.h" +#include "strv.h" +#include "terminal-util.h" +#include "tmpfile-util.h" + +int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { + const char *data; + _cleanup_free_ char *filename = NULL; + size_t len; + int r, fd; + _cleanup_close_ int fdt = -EBADF; + char *temp = NULL; + + assert(!(file && path)); /* At most one can be specified */ + assert(!!path == !!unlink_temp); /* Those must be specified together */ + + /* Look for a coredump on disk first. */ + r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); + if (r == 0) { + _cleanup_free_ char *resolved = NULL; + + r = retrieve(data, len, "COREDUMP_FILENAME", &filename); + if (r < 0) + return r; + assert(r > 0); + + r = chase_and_access(filename, arg_root, CHASE_PREFIX_ROOT, F_OK, &resolved); + if (r < 0) + return log_error_errno(r, "Cannot access \"%s%s\": %m", strempty(arg_root), filename); + + free_and_replace(filename, resolved); + + if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { + *path = TAKE_PTR(filename); + + return 0; + } + + } else { + if (r != -ENOENT) + return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); + + /* Check that we can have a COREDUMP field. We still haven't set a high data threshold, so + * we'll get a few kilobytes at most. */ + + r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); + if (r == -ENOENT) + return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); + } + + if (path) { + const char *vt; + + /* Create a temporary file to write the uncompressed core to. */ + + r = var_tmp_dir(&vt); + if (r < 0) + return log_error_errno(r, "Failed to acquire temporary directory path: %m"); + + temp = path_join(vt, "coredump-XXXXXX"); + if (!temp) + return log_oom(); + + fdt = mkostemp_safe(temp); + if (fdt < 0) + return log_error_errno(fdt, "Failed to create temporary file: %m"); + log_debug("Created temporary file %s", temp); + + fd = fdt; + } else { + /* If neither path or file are specified, we will write to stdout. Let's now check if stdout + * is connected to a tty. We checked that the file exists, or that the core might be stored + * in the journal. In this second case, if we found the entry, in all likelihood we will be + * able to access the COREDUMP= field. In either case, we stop before doing any "real" work, + * i.e. before starting decompression or reading from the file or creating temporary files. */ + if (!file) { + if (on_tty()) + return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), + "Refusing to dump core to tty" + " (use shell redirection or specify --output)."); + file = stdout; + } + + fd = fileno(file); + } + + if (filename) { +#if HAVE_COMPRESSION + _cleanup_close_ int fdf = -EBADF; + + fdf = open(filename, O_RDONLY | O_CLOEXEC); + if (fdf < 0) { + r = log_error_errno(errno, "Failed to open %s: %m", filename); + goto error; + } + + r = decompress_stream_by_filename(filename, fdf, fd, -1); + if (r < 0) { + log_error_errno(r, "Failed to decompress %s: %m", filename); + goto error; + } +#else + r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Cannot decompress file. Compiled without compression support."); + goto error; +#endif + } else { + /* We want full data, nothing truncated. */ + sd_journal_set_data_threshold(j, 0); + + r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); + + assert(len >= 9); + data += 9; + len -= 9; + + r = loop_write(fd, data, len); + if (r < 0) { + log_error_errno(r, "Failed to write output: %m"); + goto error; + } + } + + if (temp) { + *path = temp; + *unlink_temp = true; + } + return 0; + +error: + if (temp) { + (void) unlink(temp); + log_debug("Removed temporary file %s", temp); + } + return r; +} + +int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { + _cleanup_(sd_journal_closep) sd_journal *j = NULL; + _cleanup_fclose_ FILE *f = NULL; + int r; + + if (arg_field) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Option --field/-F only makes sense with list"); + + r = acquire_journal(&j, strv_skip(argv, 1)); + if (r < 0) + return r; + + r = focus(j); + if (r < 0) + return r; + + if (arg_output) { + f = fopen(arg_output, "we"); + if (!f) + return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); + } + + print_info(f ? stdout : stderr, j, false); + + r = save_core(j, f, NULL, NULL); + if (r < 0) + return r; + + r = sd_journal_previous(j); + if (r > 0 && !arg_quiet) + log_notice("More than one entry matches, ignoring rest."); + + return 0; +} diff --git a/src/coredump/coredumpctl-core.h b/src/coredump/coredumpctl-core.h new file mode 100644 index 0000000000..2742496dca --- /dev/null +++ b/src/coredump/coredumpctl-core.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp); + +int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 80336f1a6c..4e9c8bfd70 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -1,6 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include #include #include #include @@ -16,9 +15,8 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-util.h" -#include "chase.h" -#include "compress.h" #include "coredumpctl.h" +#include "coredumpctl-core.h" #include "coredumpctl-info.h" #include "coredumpctl-journal.h" #include "coredumpctl-list.h" @@ -26,11 +24,8 @@ #include "dissect-image.h" #include "dlopen-note.h" #include "extract-word.h" -#include "fd-util.h" -#include "fs-util.h" #include "glob-util.h" #include "image-policy.h" -#include "io-util.h" #include "log.h" #include "logs-show.h" #include "loop-util.h" @@ -45,9 +40,7 @@ #include "signal-util.h" #include "string-util.h" #include "strv.h" -#include "terminal-util.h" #include "time-util.h" -#include "tmpfile-util.h" #include "verbs.h" #define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) @@ -235,182 +228,8 @@ VERB_SCOPE(, verb_dump_list, "list", "[MATCHES…]", VERB_ANY, VERB_ANY, VERB_DE "List available coredumps"); VERB_SCOPE(, verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, "Show detailed information about one or more coredumps"); - -static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { - const char *data; - _cleanup_free_ char *filename = NULL; - size_t len; - int r, fd; - _cleanup_close_ int fdt = -EBADF; - char *temp = NULL; - - assert(!(file && path)); /* At most one can be specified */ - assert(!!path == !!unlink_temp); /* Those must be specified together */ - - /* Look for a coredump on disk first. */ - r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); - if (r == 0) { - _cleanup_free_ char *resolved = NULL; - - r = retrieve(data, len, "COREDUMP_FILENAME", &filename); - if (r < 0) - return r; - assert(r > 0); - - r = chase_and_access(filename, arg_root, CHASE_PREFIX_ROOT, F_OK, &resolved); - if (r < 0) - return log_error_errno(r, "Cannot access \"%s%s\": %m", strempty(arg_root), filename); - - free_and_replace(filename, resolved); - - if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { - *path = TAKE_PTR(filename); - - return 0; - } - - } else { - if (r != -ENOENT) - return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); - /* Check that we can have a COREDUMP field. We still haven't set a high - * data threshold, so we'll get a few kilobytes at most. - */ - - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r == -ENOENT) - return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); - if (r < 0) - return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); - } - - if (path) { - const char *vt; - - /* Create a temporary file to write the uncompressed core to. */ - - r = var_tmp_dir(&vt); - if (r < 0) - return log_error_errno(r, "Failed to acquire temporary directory path: %m"); - - temp = path_join(vt, "coredump-XXXXXX"); - if (!temp) - return log_oom(); - - fdt = mkostemp_safe(temp); - if (fdt < 0) - return log_error_errno(fdt, "Failed to create temporary file: %m"); - log_debug("Created temporary file %s", temp); - - fd = fdt; - } else { - /* If neither path or file are specified, we will write to stdout. Let's now check - * if stdout is connected to a tty. We checked that the file exists, or that the - * core might be stored in the journal. In this second case, if we found the entry, - * in all likelihood we will be able to access the COREDUMP= field. In either case, - * we stop before doing any "real" work, i.e. before starting decompression or - * reading from the file or creating temporary files. - */ - if (!file) { - if (on_tty()) - return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), - "Refusing to dump core to tty" - " (use shell redirection or specify --output)."); - file = stdout; - } - - fd = fileno(file); - } - - if (filename) { -#if HAVE_COMPRESSION - _cleanup_close_ int fdf = -EBADF; - - fdf = open(filename, O_RDONLY | O_CLOEXEC); - if (fdf < 0) { - r = log_error_errno(errno, "Failed to open %s: %m", filename); - goto error; - } - - r = decompress_stream_by_filename(filename, fdf, fd, -1); - if (r < 0) { - log_error_errno(r, "Failed to decompress %s: %m", filename); - goto error; - } -#else - r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), - "Cannot decompress file. Compiled without compression support."); - goto error; -#endif - } else { - /* We want full data, nothing truncated. */ - sd_journal_set_data_threshold(j, 0); - - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r < 0) - return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); - - assert(len >= 9); - data += 9; - len -= 9; - - r = loop_write(fd, data, len); - if (r < 0) { - log_error_errno(r, "Failed to write output: %m"); - goto error; - } - } - - if (temp) { - *path = temp; - *unlink_temp = true; - } - return 0; - -error: - if (temp) { - (void) unlink(temp); - log_debug("Removed temporary file %s", temp); - } - return r; -} - -VERB(verb_dump_core, "dump", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, - "Print first matching coredump to stdout"); -static int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { - _cleanup_(sd_journal_closep) sd_journal *j = NULL; - _cleanup_fclose_ FILE *f = NULL; - int r; - - if (arg_field) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Option --field/-F only makes sense with list"); - - r = acquire_journal(&j, strv_skip(argv, 1)); - if (r < 0) - return r; - - r = focus(j); - if (r < 0) - return r; - - if (arg_output) { - f = fopen(arg_output, "we"); - if (!f) - return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); - } - - print_info(f ? stdout : stderr, j, false); - - r = save_core(j, f, NULL, NULL); - if (r < 0) - return r; - - r = sd_journal_previous(j); - if (r > 0 && !arg_quiet) - log_notice("More than one entry matches, ignoring rest."); - - return 0; -} +VERB_SCOPE(, verb_dump_core, "dump", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, + "Print first matching coredump to stdout"); VERB(verb_run_debug, "debug", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, "Start a debugger for the first matching coredump"); diff --git a/src/coredump/meson.build b/src/coredump/meson.build index 9379affb7f..ab186fd472 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -19,6 +19,7 @@ systemd_coredump_export_sources = files( ) coredumpctl_sources = files( 'coredumpctl.c', + 'coredumpctl-core.c', 'coredumpctl-info.c', 'coredumpctl-journal.c', 'coredumpctl-list.c', From 063d7b451da3dbaac4cf45fa0fe6488f8acb4a4d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 07:47:45 +0900 Subject: [PATCH 07/16] coredumpctl: move verb_run_debug() to coredumpctl-debugger.c --- src/coredump/coredumpctl-debugger.c | 149 +++++++++++++++++++++++++++ src/coredump/coredumpctl-debugger.h | 6 ++ src/coredump/coredumpctl.c | 152 +--------------------------- src/coredump/meson.build | 1 + 4 files changed, 161 insertions(+), 147 deletions(-) create mode 100644 src/coredump/coredumpctl-debugger.c create mode 100644 src/coredump/coredumpctl-debugger.h diff --git a/src/coredump/coredumpctl-debugger.c b/src/coredump/coredumpctl-debugger.c new file mode 100644 index 0000000000..482f86024d --- /dev/null +++ b/src/coredump/coredumpctl-debugger.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +#include "sd-journal.h" + +#include "coredumpctl.h" +#include "coredumpctl-core.h" +#include "coredumpctl-debugger.h" +#include "coredumpctl-info.h" +#include "coredumpctl-journal.h" +#include "coredumpctl-util.h" +#include "log.h" +#include "path-util.h" +#include "pidref.h" +#include "process-util.h" +#include "signal-util.h" +#include "string-util.h" +#include "strv.h" + +int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { + static const struct sigaction sa = { + .sa_sigaction = sigterm_process_group_handler, + .sa_flags = SA_SIGINFO, + }; + + _cleanup_(sd_journal_closep) sd_journal *j = NULL; + _cleanup_free_ char *exe = NULL, *path = NULL; + _cleanup_strv_free_ char **debugger_call = NULL; + bool unlink_path = false; + const char *data, *fork_name; + size_t len; + int r; + + if (!arg_debugger) { + char *env_debugger; + + env_debugger = getenv("SYSTEMD_DEBUGGER"); + if (env_debugger) + arg_debugger = env_debugger; + else + arg_debugger = "gdb"; + } + + r = strv_extend(&debugger_call, arg_debugger); + if (r < 0) + return log_oom(); + + r = strv_extend_strv(&debugger_call, arg_debugger_args, false); + if (r < 0) + return log_oom(); + + if (arg_field) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Option --field/-F only makes sense with list"); + + r = acquire_journal(&j, strv_skip(argv, 1)); + if (r < 0) + return r; + + r = focus(j); + if (r < 0) + return r; + + if (!arg_quiet) { + print_info(stdout, j, false); + fputs("\n", stdout); + } + + r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); + + assert(len > STRLEN("COREDUMP_EXE=")); + data += STRLEN("COREDUMP_EXE="); + len -= STRLEN("COREDUMP_EXE="); + + exe = strndup(data, len); + if (!exe) + return log_oom(); + + if (endswith(exe, " (deleted)")) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Binary already deleted."); + + if (!path_is_absolute(exe)) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Binary is not an absolute path."); + + r = resolve_filename(arg_root, &exe); + if (r < 0) + return r; + + r = save_core(j, NULL, &path, &unlink_path); + if (r < 0) + return r; + + r = strv_extend_many(&debugger_call, exe, "-c", path); + if (r < 0) + return log_oom(); + + if (arg_root) { + if (streq(arg_debugger, "gdb")) { + const char *sysroot_cmd; + sysroot_cmd = strjoina("set sysroot ", arg_root); + + r = strv_extend_many(&debugger_call, "-iex", sysroot_cmd); + if (r < 0) + return log_oom(); + } else if (streq(arg_debugger, "lldb")) { + const char *sysroot_cmd; + sysroot_cmd = strjoina("platform select --sysroot ", arg_root, " host"); + + r = strv_extend_many(&debugger_call, "-O", sysroot_cmd); + if (r < 0) + return log_oom(); + } + } + + /* Don't interfere with gdb and its handling of SIGINT. */ + (void) ignore_signals(SIGINT); + (void) sigaction(SIGTERM, &sa, NULL); + + fork_name = strjoina("(", debugger_call[0], ")"); + + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; + r = pidref_safe_fork( + fork_name, + FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_FLUSH_STDIO, + &pidref); + if (r < 0) + goto finish; + if (r == 0) { + execvp(debugger_call[0], debugger_call); + log_open(); + log_error_errno(errno, "Failed to invoke %s: %m", debugger_call[0]); + _exit(EXIT_FAILURE); + } + + r = pidref_wait_for_terminate_and_check(debugger_call[0], &pidref, WAIT_LOG_ABNORMAL); + +finish: + (void) default_signals(SIGINT, SIGTERM); + + if (unlink_path) { + log_debug("Removed temporary file %s", path); + (void) unlink(path); + } + + return r; +} diff --git a/src/coredump/coredumpctl-debugger.h b/src/coredump/coredumpctl-debugger.h new file mode 100644 index 0000000000..d3fe114906 --- /dev/null +++ b/src/coredump/coredumpctl-debugger.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "forward.h" + +int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 4e9c8bfd70..b6c63be8ba 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -1,12 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include -#include -#include -#include #include "sd-bus.h" -#include "sd-journal.h" #include "sd-json.h" #include "alloc-util.h" @@ -17,10 +13,8 @@ #include "bus-util.h" #include "coredumpctl.h" #include "coredumpctl-core.h" -#include "coredumpctl-info.h" -#include "coredumpctl-journal.h" +#include "coredumpctl-debugger.h" #include "coredumpctl-list.h" -#include "coredumpctl-util.h" #include "dissect-image.h" #include "dlopen-note.h" #include "extract-word.h" @@ -34,10 +28,6 @@ #include "pager.h" #include "parse-argument.h" #include "parse-util.h" -#include "path-util.h" -#include "pidref.h" -#include "process-util.h" -#include "signal-util.h" #include "string-util.h" #include "strv.h" #include "time-util.h" @@ -230,142 +220,10 @@ VERB_SCOPE(, verb_dump_list, "info", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, "Show detailed information about one or more coredumps"); VERB_SCOPE(, verb_dump_core, "dump", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, "Print first matching coredump to stdout"); - -VERB(verb_run_debug, "debug", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, - "Start a debugger for the first matching coredump"); -VERB(verb_run_debug, "gdb", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, - /* help= */ NULL); -static int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { - static const struct sigaction sa = { - .sa_sigaction = sigterm_process_group_handler, - .sa_flags = SA_SIGINFO, - }; - - _cleanup_(sd_journal_closep) sd_journal *j = NULL; - _cleanup_free_ char *exe = NULL, *path = NULL; - _cleanup_strv_free_ char **debugger_call = NULL; - bool unlink_path = false; - const char *data, *fork_name; - size_t len; - int r; - - if (!arg_debugger) { - char *env_debugger; - - env_debugger = getenv("SYSTEMD_DEBUGGER"); - if (env_debugger) - arg_debugger = env_debugger; - else - arg_debugger = "gdb"; - } - - r = strv_extend(&debugger_call, arg_debugger); - if (r < 0) - return log_oom(); - - r = strv_extend_strv(&debugger_call, arg_debugger_args, false); - if (r < 0) - return log_oom(); - - if (arg_field) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), - "Option --field/-F only makes sense with list"); - - r = acquire_journal(&j, strv_skip(argv, 1)); - if (r < 0) - return r; - - r = focus(j); - if (r < 0) - return r; - - if (!arg_quiet) { - print_info(stdout, j, false); - fputs("\n", stdout); - } - - r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); - if (r < 0) - return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); - - assert(len > STRLEN("COREDUMP_EXE=")); - data += STRLEN("COREDUMP_EXE="); - len -= STRLEN("COREDUMP_EXE="); - - exe = strndup(data, len); - if (!exe) - return log_oom(); - - if (endswith(exe, " (deleted)")) - return log_error_errno(SYNTHETIC_ERRNO(ENOENT), - "Binary already deleted."); - - if (!path_is_absolute(exe)) - return log_error_errno(SYNTHETIC_ERRNO(ENOENT), - "Binary is not an absolute path."); - - r = resolve_filename(arg_root, &exe); - if (r < 0) - return r; - - r = save_core(j, NULL, &path, &unlink_path); - if (r < 0) - return r; - - r = strv_extend_many(&debugger_call, exe, "-c", path); - if (r < 0) - return log_oom(); - - if (arg_root) { - if (streq(arg_debugger, "gdb")) { - const char *sysroot_cmd; - sysroot_cmd = strjoina("set sysroot ", arg_root); - - r = strv_extend_many(&debugger_call, "-iex", sysroot_cmd); - if (r < 0) - return log_oom(); - } else if (streq(arg_debugger, "lldb")) { - const char *sysroot_cmd; - sysroot_cmd = strjoina("platform select --sysroot ", arg_root, " host"); - - r = strv_extend_many(&debugger_call, "-O", sysroot_cmd); - if (r < 0) - return log_oom(); - } - } - - /* Don't interfere with gdb and its handling of SIGINT. */ - (void) ignore_signals(SIGINT); - (void) sigaction(SIGTERM, &sa, NULL); - - fork_name = strjoina("(", debugger_call[0], ")"); - - _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; - r = pidref_safe_fork( - fork_name, - FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_FLUSH_STDIO, - &pidref); - if (r < 0) - goto finish; - if (r == 0) { - execvp(debugger_call[0], debugger_call); - log_open(); - log_error_errno(errno, "Failed to invoke %s: %m", debugger_call[0]); - _exit(EXIT_FAILURE); - } - - r = pidref_wait_for_terminate_and_check(debugger_call[0], &pidref, WAIT_LOG_ABNORMAL); - -finish: - (void) default_signals(SIGINT, SIGTERM); - - if (unlink_path) { - log_debug("Removed temporary file %s", path); - (void) unlink(path); - } - - return r; -} +VERB_SCOPE(, verb_run_debug, "debug", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, + "Start a debugger for the first matching coredump"); +VERB_SCOPE(, verb_run_debug, "gdb", "[MATCHES…]", VERB_ANY, VERB_ANY, 0, + /* help= */ NULL); static int check_units_active(void) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; diff --git a/src/coredump/meson.build b/src/coredump/meson.build index ab186fd472..084088dde8 100644 --- a/src/coredump/meson.build +++ b/src/coredump/meson.build @@ -20,6 +20,7 @@ systemd_coredump_export_sources = files( coredumpctl_sources = files( 'coredumpctl.c', 'coredumpctl-core.c', + 'coredumpctl-debugger.c', 'coredumpctl-info.c', 'coredumpctl-journal.c', 'coredumpctl-list.c', From 6f8b23398cc04778143e5fc58ee6ea21c674a45d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 26 Aug 2026 02:10:27 +0900 Subject: [PATCH 08/16] coredumpctl: fix assertion The field is not checked by journald. Hence, the value may be missing. An empty value will be refused by path_is_absolute() in the below. --- src/coredump/coredumpctl-debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coredump/coredumpctl-debugger.c b/src/coredump/coredumpctl-debugger.c index 482f86024d..9145d38709 100644 --- a/src/coredump/coredumpctl-debugger.c +++ b/src/coredump/coredumpctl-debugger.c @@ -71,7 +71,7 @@ int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { if (r < 0) return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); - assert(len > STRLEN("COREDUMP_EXE=")); + assert(len >= STRLEN("COREDUMP_EXE=")); data += STRLEN("COREDUMP_EXE="); len -= STRLEN("COREDUMP_EXE="); From 8972b051a78d5d2e2959f4f2e97f82a2c035069c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 10:11:54 +0900 Subject: [PATCH 09/16] coredumpctl: allow to show coredump info in json format in dump and debug command Previously `coredumpctl dump/debug` silently ignored --json= option. Let's honor the option. --- src/coredump/coredumpctl-core.c | 2 +- src/coredump/coredumpctl-debugger.c | 2 +- src/coredump/coredumpctl-info.c | 15 ++++++++------- src/coredump/coredumpctl-info.h | 3 +-- src/coredump/coredumpctl-list.c | 6 ++++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index 4baaadd4b5..5a69d1fc20 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -180,7 +180,7 @@ int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); } - print_info(f ? stdout : stderr, j, false); + (void) print_entry(f ? stdout : stderr, j, /* need_space= */ false, /* table= */ NULL); r = save_core(j, f, NULL, NULL); if (r < 0) diff --git a/src/coredump/coredumpctl-debugger.c b/src/coredump/coredumpctl-debugger.c index 9145d38709..89bc25df97 100644 --- a/src/coredump/coredumpctl-debugger.c +++ b/src/coredump/coredumpctl-debugger.c @@ -63,7 +63,7 @@ int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { return r; if (!arg_quiet) { - print_info(stdout, j, false); + (void) print_entry(stdout, j, /* need_space= */ false, /* table= */ NULL); fputs("\n", stdout); } diff --git a/src/coredump/coredumpctl-info.c b/src/coredump/coredumpctl-info.c index f2cad48114..6205ac8662 100644 --- a/src/coredump/coredumpctl-info.c +++ b/src/coredump/coredumpctl-info.c @@ -330,7 +330,7 @@ static int coredump_fields_load(sd_journal *j, CoredumpFields *f) { return 0; } -int print_info(FILE *file, sd_journal *j, bool need_space) { +static int print_info(FILE *file, sd_journal *j, bool need_space) { _cleanup_(coredump_fields_done) CoredumpFields f = { .disk_size = UINT64_MAX, }; @@ -598,14 +598,15 @@ static int print_info_json(FILE *file, sd_journal *j) { return 0; } -int print_entry(sd_journal *j, size_t n_found, Table *t) { +int print_entry(FILE *f, sd_journal *j, bool need_space, Table *table) { + assert(f); assert(j); - if (t) - return print_list(stdout, j, t); + if (table) + return print_list(f, j, table); if (arg_field) - return print_field(stdout, j); + return print_field(f, j); if (sd_json_format_enabled(arg_json_format_flags)) - return print_info_json(stdout, j); - return print_info(stdout, j, n_found > 0); + return print_info_json(f, j); + return print_info(f, j, need_space); } diff --git a/src/coredump/coredumpctl-info.h b/src/coredump/coredumpctl-info.h index 5caf2b55cc..8120fb0f66 100644 --- a/src/coredump/coredumpctl-info.h +++ b/src/coredump/coredumpctl-info.h @@ -3,5 +3,4 @@ #include "forward.h" -int print_info(FILE *file, sd_journal *j, bool need_space); -int print_entry(sd_journal *j, size_t n_found, Table *t); +int print_entry(FILE *f, sd_journal *j, bool need_space, Table *table); diff --git a/src/coredump/coredumpctl-list.c b/src/coredump/coredumpctl-list.c index 08872110e8..009268fdf1 100644 --- a/src/coredump/coredumpctl-list.c +++ b/src/coredump/coredumpctl-list.c @@ -48,7 +48,7 @@ int verb_dump_list(int argc, char *argv[], uintptr_t _data, void *userdata) { if (r < 0) return r; - r = print_entry(j, 0, t); + r = print_entry(stdout, j, /* need_space= */ false, t); if (r < 0) return r; } else { @@ -93,10 +93,12 @@ int verb_dump_list(int argc, char *argv[], uintptr_t _data, void *userdata) { break; } - r = print_entry(j, n_found++, t); + r = print_entry(stdout, j, /* need_space= */ n_found > 0, t); if (r < 0) return r; + n_found++; + if (arg_rows_max != SIZE_MAX && n_found >= arg_rows_max) break; } From d1806141880e24a06f08644804da4ba007b7ce4f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 23:27:18 +0900 Subject: [PATCH 10/16] coredumpctl: check earlier if stdout is not connected to a tty --- src/coredump/coredumpctl-core.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index 5a69d1fc20..f7d96d8ff7 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -29,7 +29,7 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_close_ int fdt = -EBADF; char *temp = NULL; - assert(!(file && path)); /* At most one can be specified */ + assert(!!file == !path); /* file or path must be specified. They must not be specified together. */ assert(!!path == !!unlink_temp); /* Those must be specified together */ /* Look for a coredump on disk first. */ @@ -87,22 +87,8 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { log_debug("Created temporary file %s", temp); fd = fdt; - } else { - /* If neither path or file are specified, we will write to stdout. Let's now check if stdout - * is connected to a tty. We checked that the file exists, or that the core might be stored - * in the journal. In this second case, if we found the entry, in all likelihood we will be - * able to access the COREDUMP= field. In either case, we stop before doing any "real" work, - * i.e. before starting decompression or reading from the file or creating temporary files. */ - if (!file) { - if (on_tty()) - return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), - "Refusing to dump core to tty" - " (use shell redirection or specify --output)."); - file = stdout; - } - + } else fd = fileno(file); - } if (filename) { #if HAVE_COMPRESSION @@ -178,11 +164,14 @@ int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { f = fopen(arg_output, "we"); if (!f) return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", arg_output); - } + } else if (on_tty()) + /* We will write core to stdout. Let's refuse if stdout is connected to a tty. */ + return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), + "Refusing to dump core to tty (use shell redirection or specify --output)."); (void) print_entry(f ? stdout : stderr, j, /* need_space= */ false, /* table= */ NULL); - r = save_core(j, f, NULL, NULL); + r = save_core(j, f ?: stdout, NULL, NULL); if (r < 0) return r; From af8ff25a64d2be63076d5af84b96b9f614475229 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Aug 2026 00:00:59 +0900 Subject: [PATCH 11/16] coredumpctl: use _cleanup_(unlink_and_free) This fixes memleak on failure paths, and remaining temp file on failure in reading coredump from journal. --- src/coredump/coredumpctl-core.c | 38 ++++++++++----------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index f7d96d8ff7..6332e4576b 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include - #include "sd-journal.h" #include "chase.h" @@ -26,8 +24,6 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_free_ char *filename = NULL; size_t len; int r, fd; - _cleanup_close_ int fdt = -EBADF; - char *temp = NULL; assert(!!file == !path); /* file or path must be specified. They must not be specified together. */ assert(!!path == !!unlink_temp); /* Those must be specified together */ @@ -68,6 +64,8 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); } + _cleanup_(unlink_and_freep) char *temp = NULL; + _cleanup_close_ int fdt = -EBADF; if (path) { const char *vt; @@ -95,20 +93,15 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_close_ int fdf = -EBADF; fdf = open(filename, O_RDONLY | O_CLOEXEC); - if (fdf < 0) { - r = log_error_errno(errno, "Failed to open %s: %m", filename); - goto error; - } + if (fdf < 0) + return log_error_errno(errno, "Failed to open %s: %m", filename); r = decompress_stream_by_filename(filename, fdf, fd, -1); - if (r < 0) { - log_error_errno(r, "Failed to decompress %s: %m", filename); - goto error; - } + if (r < 0) + return log_error_errno(r, "Failed to decompress %s: %m", filename); #else - r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), - "Cannot decompress file. Compiled without compression support."); - goto error; + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Cannot decompress file. Compiled without compression support."); #endif } else { /* We want full data, nothing truncated. */ @@ -123,24 +116,15 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { len -= 9; r = loop_write(fd, data, len); - if (r < 0) { - log_error_errno(r, "Failed to write output: %m"); - goto error; - } + if (r < 0) + return log_error_errno(r, "Failed to write output: %m"); } if (temp) { - *path = temp; + *path = TAKE_PTR(temp); *unlink_temp = true; } return 0; - -error: - if (temp) { - (void) unlink(temp); - log_debug("Removed temporary file %s", temp); - } - return r; } int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { From f2496d862a2d5e42abce50ba6a223a694e4aa672 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 23:21:40 +0900 Subject: [PATCH 12/16] coredumpctl: split out open_tmpfile() from save_core() No functional change. Preparation for later change. --- src/coredump/coredumpctl-core.c | 43 +++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index 6332e4576b..da2f04b8e2 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -19,6 +19,32 @@ #include "terminal-util.h" #include "tmpfile-util.h" +static int coredump_open_tmpfile(char **ret_path, int *ret_fd) { + int r; + + assert(ret_path); + assert(ret_fd); + + const char *vt; + r = var_tmp_dir(&vt); + if (r < 0) + return log_error_errno(r, "Failed to acquire temporary directory path: %m"); + + _cleanup_free_ char *path = path_join(vt, "coredump-XXXXXX"); + if (!path) + return log_oom(); + + _cleanup_close_ int fd = mkostemp_safe(path); + if (fd < 0) + return log_error_errno(fd, "Failed to create temporary file: %m"); + + log_debug("Created temporary file %s", path); + + *ret_path = TAKE_PTR(path); + *ret_fd = TAKE_FD(fd); + return 0; +} + int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { const char *data; _cleanup_free_ char *filename = NULL; @@ -67,22 +93,9 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_(unlink_and_freep) char *temp = NULL; _cleanup_close_ int fdt = -EBADF; if (path) { - const char *vt; - - /* Create a temporary file to write the uncompressed core to. */ - - r = var_tmp_dir(&vt); + r = coredump_open_tmpfile(&temp, &fdt); if (r < 0) - return log_error_errno(r, "Failed to acquire temporary directory path: %m"); - - temp = path_join(vt, "coredump-XXXXXX"); - if (!temp) - return log_oom(); - - fdt = mkostemp_safe(temp); - if (fdt < 0) - return log_error_errno(fdt, "Failed to create temporary file: %m"); - log_debug("Created temporary file %s", temp); + return r; fd = fdt; } else From ab9c1bdae05cac9a4e57eeb99d9560441b71ee6d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Aug 2026 00:03:30 +0900 Subject: [PATCH 13/16] coredumpctl: several cleanups for save_core() - Rename to acquire_core(). - Make it take a fd rather than FILE*. - Make it take two ret_ arguments to store paths so that callers can easily use _cleanup_(unlink_and_freep) for the created temp file. --- src/coredump/coredumpctl-core.c | 31 +++++++++++++++-------------- src/coredump/coredumpctl-core.h | 2 +- src/coredump/coredumpctl-debugger.c | 16 +++++++-------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index da2f04b8e2..f3c2d31eeb 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -45,14 +45,15 @@ static int coredump_open_tmpfile(char **ret_path, int *ret_fd) { return 0; } -int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { +int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { const char *data; _cleanup_free_ char *filename = NULL; size_t len; - int r, fd; + int r; - assert(!!file == !path); /* file or path must be specified. They must not be specified together. */ - assert(!!path == !!unlink_temp); /* Those must be specified together */ + assert(j); + assert((fd >= 0) == !ret_tmpfile); + assert((fd >= 0) == !ret_path); /* Look for a coredump on disk first. */ r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); @@ -70,9 +71,9 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { free_and_replace(filename, resolved); - if (path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { - *path = TAKE_PTR(filename); - + if (ret_path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { + *ret_tmpfile = NULL; + *ret_path = TAKE_PTR(filename); return 0; } @@ -92,14 +93,13 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { _cleanup_(unlink_and_freep) char *temp = NULL; _cleanup_close_ int fdt = -EBADF; - if (path) { + if (fd < 0) { r = coredump_open_tmpfile(&temp, &fdt); if (r < 0) return r; fd = fdt; - } else - fd = fileno(file); + } if (filename) { #if HAVE_COMPRESSION @@ -133,10 +133,11 @@ int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) { return log_error_errno(r, "Failed to write output: %m"); } - if (temp) { - *path = TAKE_PTR(temp); - *unlink_temp = true; - } + if (ret_tmpfile) + *ret_tmpfile = TAKE_PTR(temp); + if (ret_path) + *ret_path = NULL; + return 0; } @@ -168,7 +169,7 @@ int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) { (void) print_entry(f ? stdout : stderr, j, /* need_space= */ false, /* table= */ NULL); - r = save_core(j, f ?: stdout, NULL, NULL); + r = acquire_core(j, fileno(f ?: stdout), /* ret_tmpfile= */ NULL, /* ret_path= */ NULL); if (r < 0) return r; diff --git a/src/coredump/coredumpctl-core.h b/src/coredump/coredumpctl-core.h index 2742496dca..c46b8f296a 100644 --- a/src/coredump/coredumpctl-core.h +++ b/src/coredump/coredumpctl-core.h @@ -3,6 +3,6 @@ #include "forward.h" -int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp); +int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path); int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata); diff --git a/src/coredump/coredumpctl-debugger.c b/src/coredump/coredumpctl-debugger.c index 89bc25df97..fe288df16e 100644 --- a/src/coredump/coredumpctl-debugger.c +++ b/src/coredump/coredumpctl-debugger.c @@ -10,6 +10,7 @@ #include "coredumpctl-info.h" #include "coredumpctl-journal.h" #include "coredumpctl-util.h" +#include "fs-util.h" #include "log.h" #include "path-util.h" #include "pidref.h" @@ -25,9 +26,8 @@ int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { }; _cleanup_(sd_journal_closep) sd_journal *j = NULL; - _cleanup_free_ char *exe = NULL, *path = NULL; + _cleanup_free_ char *exe = NULL; _cleanup_strv_free_ char **debugger_call = NULL; - bool unlink_path = false; const char *data, *fork_name; size_t len; int r; @@ -89,11 +89,14 @@ int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { if (r < 0) return r; - r = save_core(j, NULL, &path, &unlink_path); + _cleanup_(unlink_and_freep) char *temp = NULL; + _cleanup_free_ char *path = NULL; + r = acquire_core(j, /* fd= */ -EBADF, &temp, &path); if (r < 0) return r; - r = strv_extend_many(&debugger_call, exe, "-c", path); + assert(!path == !!temp); + r = strv_extend_many(&debugger_call, exe, "-c", path ?: temp); if (r < 0) return log_oom(); @@ -140,10 +143,5 @@ int verb_run_debug(int argc, char *argv[], uintptr_t _data, void *userdata) { finish: (void) default_signals(SIGINT, SIGTERM); - if (unlink_path) { - log_debug("Removed temporary file %s", path); - (void) unlink(path); - } - return r; } From c616943f7e57d3f9bdd44af6883785c4bba6dafc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Aug 2026 00:21:46 +0900 Subject: [PATCH 14/16] coredumpctl: split out acquire_core_from_journal() --- src/coredump/coredumpctl-core.c | 73 ++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index f3c2d31eeb..a802e1bee0 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -45,6 +45,46 @@ static int coredump_open_tmpfile(char **ret_path, int *ret_fd) { return 0; } +static int acquire_core_from_journal(sd_journal *j, int fd, char **ret_tmpfile) { + int r; + + assert(j); + assert((fd >= 0) == !ret_tmpfile); + + _cleanup_(unlink_and_freep) char *temp = NULL; + _cleanup_close_ int fdt = -EBADF; + if (fd < 0) { + r = coredump_open_tmpfile(&temp, &fdt); + if (r < 0) + return r; + + fd = fdt; + } + + /* We want full data, nothing truncated. */ + (void) sd_journal_set_data_threshold(j, 0); + + const void *data; + size_t len; + r = sd_journal_get_data(j, "COREDUMP", &data, &len); + if (r == -ENOENT) + return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); + + data = ASSERT_PTR(memory_startswith(data, len, "COREDUMP=")); + len -= STRLEN("COREDUMP="); + + r = loop_write(fd, data, len); + if (r < 0) + return log_error_errno(r, "Failed to write core: %m"); + + if (ret_tmpfile) + *ret_tmpfile = TAKE_PTR(temp); + + return 0; +} + int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { const char *data; _cleanup_free_ char *filename = NULL; @@ -81,14 +121,15 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { if (r != -ENOENT) return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); - /* Check that we can have a COREDUMP field. We still haven't set a high data threshold, so - * we'll get a few kilobytes at most. */ - - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r == -ENOENT) - return log_error_errno(r, "Coredump entry has no core attached (neither internally in the journal nor externally on disk)."); + /* If not found, try to obtain a coredump from a COREDUMP field. */ + r = acquire_core_from_journal(j, fd, ret_tmpfile); if (r < 0) - return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); + return r; + + if (ret_path) + *ret_path = NULL; + + return 0; } _cleanup_(unlink_and_freep) char *temp = NULL; @@ -116,22 +157,8 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Cannot decompress file. Compiled without compression support."); #endif - } else { - /* We want full data, nothing truncated. */ - sd_journal_set_data_threshold(j, 0); - - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r < 0) - return log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); - - assert(len >= 9); - data += 9; - len -= 9; - - r = loop_write(fd, data, len); - if (r < 0) - return log_error_errno(r, "Failed to write output: %m"); - } + } else + assert_not_reached(); if (ret_tmpfile) *ret_tmpfile = TAKE_PTR(temp); From 89a5de18f382a9a4804b44421e8d8d4067df3067 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Aug 2026 00:28:53 +0900 Subject: [PATCH 15/16] coredumpctl: handle errors earlier No functional change, just refactoring. --- src/coredump/coredumpctl-core.c | 51 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index a802e1bee0..ea445289e9 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -86,9 +86,6 @@ static int acquire_core_from_journal(sd_journal *j, int fd, char **ret_tmpfile) } int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { - const char *data; - _cleanup_free_ char *filename = NULL; - size_t len; int r; assert(j); @@ -96,31 +93,10 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { assert((fd >= 0) == !ret_path); /* Look for a coredump on disk first. */ + const char *data; + size_t len; r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); - if (r == 0) { - _cleanup_free_ char *resolved = NULL; - - r = retrieve(data, len, "COREDUMP_FILENAME", &filename); - if (r < 0) - return r; - assert(r > 0); - - r = chase_and_access(filename, arg_root, CHASE_PREFIX_ROOT, F_OK, &resolved); - if (r < 0) - return log_error_errno(r, "Cannot access \"%s%s\": %m", strempty(arg_root), filename); - - free_and_replace(filename, resolved); - - if (ret_path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { - *ret_tmpfile = NULL; - *ret_path = TAKE_PTR(filename); - return 0; - } - - } else { - if (r != -ENOENT) - return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); - + if (r == -ENOENT) { /* If not found, try to obtain a coredump from a COREDUMP field. */ r = acquire_core_from_journal(j, fd, ret_tmpfile); if (r < 0) @@ -131,6 +107,27 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { return 0; } + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP_FILENAME field: %m"); + + _cleanup_free_ char *filename = NULL; + r = retrieve(data, len, "COREDUMP_FILENAME", &filename); + if (r < 0) + return r; + assert(r > 0); + + _cleanup_free_ char *path = NULL; + r = chase_and_access(filename, arg_root, CHASE_PREFIX_ROOT, F_OK, &path); + if (r < 0) + return log_error_errno(r, "Cannot access \"%s%s\": %m", strempty(arg_root), filename); + + free_and_replace(filename, path); + + if (ret_path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { + *ret_tmpfile = NULL; + *ret_path = TAKE_PTR(filename); + return 0; + } _cleanup_(unlink_and_freep) char *temp = NULL; _cleanup_close_ int fdt = -EBADF; From 385fa7641dcf37c97a34c4d9118233b217b276e6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 22 Aug 2026 00:22:41 +0900 Subject: [PATCH 16/16] coredumpctl: allow to dump uncompressed core file Previously, if a core file is not compressed, `coredumpctl dump` is failed with -EPROTONOSUPPORT, as the file was unconditionally passed to decompress_stream_by_filename(). --- src/coredump/coredumpctl-core.c | 51 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/coredump/coredumpctl-core.c b/src/coredump/coredumpctl-core.c index ea445289e9..69710db522 100644 --- a/src/coredump/coredumpctl-core.c +++ b/src/coredump/coredumpctl-core.c @@ -4,6 +4,7 @@ #include "chase.h" #include "compress.h" +#include "copy.h" #include "coredumpctl.h" #include "coredumpctl-core.h" #include "coredumpctl-info.h" @@ -117,18 +118,32 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { assert(r > 0); _cleanup_free_ char *path = NULL; - r = chase_and_access(filename, arg_root, CHASE_PREFIX_ROOT, F_OK, &path); + _cleanup_close_ int fdf_opath = -EBADF; + r = chase(filename, arg_root, CHASE_PREFIX_ROOT | CHASE_MUST_BE_REGULAR, &path, &fdf_opath); if (r < 0) - return log_error_errno(r, "Cannot access \"%s%s\": %m", strempty(arg_root), filename); + return log_error_errno(r, "Failed to chase '%s%s': %m", strempty(arg_root), filename); - free_and_replace(filename, path); + Compression c = compression_from_filename(path); + if (c == COMPRESSION_NONE) { + if (fd >= 0) { + r = copy_bytes(fdf_opath, fd, /* max_bytes= */ UINT64_MAX, /* copy_flags= */ 0); + if (r < 0) + return log_error_errno(r, "Failed to dump '%s': %m", path); + } + + if (ret_tmpfile) + *ret_tmpfile = NULL; + if (ret_path) + *ret_path = TAKE_PTR(path); - if (ret_path && !ENDSWITH_SET(filename, ".xz", ".lz4", ".zst")) { - *ret_tmpfile = NULL; - *ret_path = TAKE_PTR(filename); return 0; } +#if HAVE_COMPRESSION + _cleanup_close_ int fdf = fd_reopen(fdf_opath, O_RDONLY | O_CLOEXEC); + if (fdf < 0) + return log_error_errno(fdf, "Failed to open '%s': %m", path); + _cleanup_(unlink_and_freep) char *temp = NULL; _cleanup_close_ int fdt = -EBADF; if (fd < 0) { @@ -139,23 +154,9 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { fd = fdt; } - if (filename) { -#if HAVE_COMPRESSION - _cleanup_close_ int fdf = -EBADF; - - fdf = open(filename, O_RDONLY | O_CLOEXEC); - if (fdf < 0) - return log_error_errno(errno, "Failed to open %s: %m", filename); - - r = decompress_stream_by_filename(filename, fdf, fd, -1); - if (r < 0) - return log_error_errno(r, "Failed to decompress %s: %m", filename); -#else - return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), - "Cannot decompress file. Compiled without compression support."); -#endif - } else - assert_not_reached(); + r = decompress_stream(c, fdf, fd, /* max_bytes= */ UINT64_MAX); + if (r < 0) + return log_error_errno(r, "Failed to decompress '%s': %m", path); if (ret_tmpfile) *ret_tmpfile = TAKE_PTR(temp); @@ -163,6 +164,10 @@ int acquire_core(sd_journal *j, int fd, char **ret_tmpfile, char **ret_path) { *ret_path = NULL; return 0; +#else + return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), + "Cannot decompress file. Compiled without compression support."); +#endif } int verb_dump_core(int argc, char *argv[], uintptr_t _data, void *userdata) {