From 3e24c014bff5005694bf461b2f2af44832264848 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 21 Aug 2026 07:10:40 +0900 Subject: [PATCH] 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', )