X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fcoredumpctl.c;h=756e793a4fed14bea20771f5e38ed7a8248fae44;hp=9eaa8979a012995b25851d26bc0d3e8f74af0167;hb=3774cf57081b595003e9236602d049b1011b6e97;hpb=8d4e028f1868c47864ec873d9f30c3ee961a8849 diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index 9eaa8979a..756e793a4 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -26,8 +26,7 @@ #include #include -#include - +#include "sd-journal.h" #include "build.h" #include "set.h" #include "util.h" @@ -37,6 +36,8 @@ #include "macro.h" #include "journal-internal.h" #include "copy.h" +#include "compress.h" +#include "sigbus.h" static enum { ACTION_NONE, @@ -45,19 +46,18 @@ static enum { ACTION_DUMP, ACTION_GDB, } arg_action = ACTION_LIST; - -static FILE* output = NULL; static const char* arg_field = NULL; - static int arg_no_pager = false; static int arg_no_legend = false; +static int arg_one = false; +static FILE* arg_output = NULL; static Set *new_matches(void) { Set *set; char *tmp; int r; - set = set_new(trivial_hash_func, trivial_compare_func); + set = set_new(NULL); if (!set) { log_oom(); return NULL; @@ -72,7 +72,7 @@ static Set *new_matches(void) { r = set_consume(set, tmp); if (r < 0) { - log_error("failed to add to set: %s", strerror(-r)); + log_error_errno(r, "failed to add to set: %m"); set_free(set); return NULL; } @@ -80,28 +80,6 @@ static Set *new_matches(void) { return set; } -static int help(void) { - - printf("%s [OPTIONS...]\n\n" - "List or retrieve coredumps from the journal.\n\n" - "Flags:\n" - " -o --output=FILE Write output to FILE\n" - " --no-pager Do not pipe output into a pager\n" - " --no-legend Do not print the column headers.\n\n" - - "Commands:\n" - " -h --help Show this help\n" - " --version Print version string\n" - " -F --field=FIELD List all values a certain field takes\n" - " list [MATCHES...] List available coredumps\n" - " info [MATCHES...] Show detailed information about one or more coredumps\n" - " dump [MATCHES...] Print first matching coredump to stdout\n" - " gdb [MATCHES...] Start gdb for the first matching coredump\n" - , program_invocation_short_name); - - return 0; -} - static int add_match(Set *set, const char *match) { int r = -ENOMEM; unsigned pid; @@ -129,18 +107,35 @@ static int add_match(Set *set, const char *match) { goto fail; log_debug("Adding pattern: %s", pattern); - r = set_put(set, pattern); + r = set_consume(set, pattern); if (r < 0) { - log_error("Failed to add pattern '%s': %s", - pattern, strerror(-r)); - free(pattern); + log_error_errno(r, "Failed to add pattern: %m"); goto fail; } return 0; fail: - log_error("Failed to add match: %s", strerror(-r)); - return r; + return log_error_errno(r, "Failed to add match: %m"); +} + +static void help(void) { + printf("%s [OPTIONS...]\n\n" + "List or retrieve coredumps from the journal.\n\n" + "Flags:\n" + " -h --help Show this help\n" + " --version Print version string\n" + " --no-pager Do not pipe output into a pager\n" + " --no-legend Do not print the column headers.\n" + " -1 Show information about most recent entry only\n" + " -F --field=FIELD List all values a certain field takes\n" + " -o --output=FILE Write output to FILE\n\n" + + "Commands:\n" + " list [MATCHES...] List available coredumps (default)\n" + " info [MATCHES...] Show detailed information about one or more coredumps\n" + " dump [MATCHES...] Print first matching coredump to stdout\n" + " gdb [MATCHES...] Start gdb for the first matching coredump\n" + , program_invocation_short_name); } static int parse_argv(int argc, char *argv[], Set *matches) { @@ -165,12 +160,13 @@ static int parse_argv(int argc, char *argv[], Set *matches) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "ho:F:", options, NULL)) >= 0) + while ((c = getopt_long(argc, argv, "ho:F:1", options, NULL)) >= 0) switch(c) { case 'h': arg_action = ACTION_NONE; - return help(); + help(); + return 0; case ARG_VERSION: arg_action = ACTION_NONE; @@ -187,16 +183,14 @@ static int parse_argv(int argc, char *argv[], Set *matches) { break; case 'o': - if (output) { + if (arg_output) { log_error("cannot set output more than once"); return -EINVAL; } - output = fopen(optarg, "we"); - if (!output) { - log_error("writing to '%s': %m", optarg); - return -errno; - } + arg_output = fopen(optarg, "we"); + if (!arg_output) + return log_error_errno(errno, "writing to '%s': %m", optarg); break; @@ -208,6 +202,10 @@ static int parse_argv(int argc, char *argv[], Set *matches) { arg_field = optarg; break; + case '1': + arg_one = true; + break; + case '?': return -EINVAL; @@ -275,55 +273,6 @@ static int retrieve(const void *data, return 0; } -#define filename_escape(s) xescape((s), "./") - -static int make_coredump_path(sd_journal *j, char **ret) { - _cleanup_free_ char - *pid = NULL, *boot_id = NULL, *tstamp = NULL, *comm = NULL, - *p = NULL, *b = NULL, *t = NULL, *c = NULL; - const void *d; - size_t l; - char *fn; - - assert(j); - assert(ret); - - SD_JOURNAL_FOREACH_DATA(j, d, l) { - retrieve(d, l, "COREDUMP_COMM", &comm); - retrieve(d, l, "COREDUMP_PID", &pid); - retrieve(d, l, "COREDUMP_TIMESTAMP", &tstamp); - retrieve(d, l, "_BOOT_ID", &boot_id); - } - - if (!pid || !comm || !tstamp || !boot_id) { - log_error("Failed to retrieve necessary fields to find coredump on disk."); - return -ENOENT; - } - - p = filename_escape(pid); - if (!p) - return log_oom(); - - t = filename_escape(tstamp); - if (!t) - return log_oom(); - - c = filename_escape(comm); - if (!t) - return log_oom(); - - b = filename_escape(boot_id); - if (!b) - return log_oom(); - - fn = strjoin("/var/lib/systemd/coredump/core.", c, ".", b, ".", p, ".", t, NULL); - if (!fn) - return log_oom(); - - *ret = fn; - return 0; -} - static void print_field(FILE* file, sd_journal *j) { _cleanup_free_ char *value = NULL; const void *d; @@ -344,12 +293,14 @@ static void print_field(FILE* file, sd_journal *j) { static int print_list(FILE* file, sd_journal *j, int had_legend) { _cleanup_free_ char *pid = NULL, *uid = NULL, *gid = NULL, - *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL; + *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, + *filename = NULL; const void *d; size_t l; usec_t t; char buf[FORMAT_TIMESTAMP_MAX]; int r; + bool present; assert(file); assert(j); @@ -362,36 +313,38 @@ static int print_list(FILE* file, sd_journal *j, int had_legend) { retrieve(d, l, "COREDUMP_EXE", &exe); retrieve(d, l, "COREDUMP_COMM", &comm); retrieve(d, l, "COREDUMP_CMDLINE", &cmdline); + retrieve(d, l, "COREDUMP_FILENAME", &filename); } - if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline) { + if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) { log_warning("Empty coredump log entry"); return -EINVAL; } r = sd_journal_get_realtime_usec(j, &t); - if (r < 0) { - log_error("Failed to get realtime timestamp: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to get realtime timestamp: %m"); format_timestamp(buf, sizeof(buf), t); + present = filename && access(filename, F_OK) == 0; if (!had_legend && !arg_no_legend) - fprintf(file, "%-*s %*s %*s %*s %*s %s\n", - FORMAT_TIMESTAMP_MAX-1, "TIME", + fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n", + FORMAT_TIMESTAMP_WIDTH, "TIME", 6, "PID", 5, "UID", 5, "GID", 3, "SIG", + 1, "PRESENT", "EXE"); - fprintf(file, "%*s %*s %*s %*s %*s %s\n", - FORMAT_TIMESTAMP_MAX-1, buf, + fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n", + FORMAT_TIMESTAMP_WIDTH, buf, 6, strna(pid), 5, strna(uid), 5, strna(gid), 3, strna(sgnl), + 1, present ? "*" : "", strna(exe ?: (comm ?: cmdline))); return 0; @@ -403,10 +356,11 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL, *unit = NULL, *user_unit = NULL, *session = NULL, *boot_id = NULL, *machine_id = NULL, *hostname = NULL, - *coredump = NULL, *slice = NULL, *cgroup = NULL, - *owner_uid = NULL, *message = NULL; + *slice = NULL, *cgroup = NULL, *owner_uid = NULL, + *message = NULL, *timestamp = NULL, *filename = NULL; const void *d; size_t l; + int r; assert(file); assert(j); @@ -425,6 +379,8 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid); retrieve(d, l, "COREDUMP_SLICE", &slice); retrieve(d, l, "COREDUMP_CGROUP", &cgroup); + retrieve(d, l, "COREDUMP_TIMESTAMP", ×tamp); + retrieve(d, l, "COREDUMP_FILENAME", &filename); retrieve(d, l, "_BOOT_ID", &boot_id); retrieve(d, l, "_MACHINE_ID", &machine_id); retrieve(d, l, "_HOSTNAME", &hostname); @@ -434,9 +390,14 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { if (need_space) fputs("\n", file); - fprintf(file, - " PID: %s%s%s\n", - ansi_highlight(), strna(pid), ansi_highlight_off()); + if (comm) + fprintf(file, + " PID: %s%s%s (%s)\n", + ansi_highlight(), strna(pid), ansi_highlight_off(), comm); + else + fprintf(file, + " PID: %s%s%s\n", + ansi_highlight(), strna(pid), ansi_highlight_off()); if (uid) { uid_t n; @@ -481,12 +442,26 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { fprintf(file, " Signal: %s\n", sgnl); } - if (exe) - fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off()); - if (comm) - fprintf(file, " Comm: %s\n", comm); + if (timestamp) { + usec_t u; + + r = safe_atou64(timestamp, &u); + if (r >= 0) { + char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX]; + + fprintf(file, + " Timestamp: %s (%s)\n", + format_timestamp(absolute, sizeof(absolute), u), + format_timestamp_relative(relative, sizeof(relative), u)); + + } else + fprintf(file, " Timestamp: %s\n", timestamp); + } + if (cmdline) fprintf(file, " Command Line: %s\n", cmdline); + if (exe) + fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off()); if (cgroup) fprintf(file, " Control Group: %s\n", cgroup); if (unit) @@ -520,9 +495,8 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { if (hostname) fprintf(file, " Hostname: %s\n", hostname); - if (make_coredump_path(j, &coredump) >= 0) - if (access(coredump, F_OK) >= 0) - fprintf(file, " Coredump: %s\n", coredump); + if (filename && access(filename, F_OK) == 0) + fprintf(file, " Coredump: %s\n", filename); if (message) { _cleanup_free_ char *m = NULL; @@ -535,8 +509,35 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { return 0; } +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) { + log_error("No match found."); + return -ESRCH; + } + return r; +} + +static void print_entry(sd_journal *j, unsigned n_found) { + assert(j); + + if (arg_action == ACTION_INFO) + print_info(stdout, j, n_found); + else if (arg_field) + print_field(stdout, j); + else + print_list(stdout, j, n_found); +} + static int dump_list(sd_journal *j) { - int found = 0; + unsigned n_found = 0; + int r; assert(j); @@ -545,102 +546,157 @@ static int dump_list(sd_journal *j) { * pick a fairly low data threshold here */ sd_journal_set_data_threshold(j, 4096); - SD_JOURNAL_FOREACH(j) { - if (arg_action == ACTION_INFO) - print_info(stdout, j, found++); - else if (arg_field) - print_field(stdout, j); - else - print_list(stdout, j, found++); - } + if (arg_one) { + r = focus(j); + if (r < 0) + return r; - if (!arg_field && !found) { - log_notice("No coredumps found"); - return -ESRCH; + print_entry(j, 0); + } else { + SD_JOURNAL_FOREACH(j) + print_entry(j, n_found++); + + if (!arg_field && n_found <= 0) { + log_notice("No coredumps found."); + return -ESRCH; + } } return 0; } -static int focus(sd_journal *j) { +static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) { + const char *data; + _cleanup_free_ char *filename = NULL; + size_t len; int r; - r = sd_journal_seek_tail(j); - if (r == 0) - r = sd_journal_previous(j); - if (r < 0) { - log_error("Failed to search journal: %s", strerror(-r)); - return r; - } - if (r == 0) { - log_error("No match found"); - return -ESRCH; + assert((fd >= 0) != !!path); + assert(!!path == !!unlink_temp); + + /* Prefer uncompressed file to journal (probably cached) to + * compressed file (probably uncached). */ + r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len); + if (r < 0 && r != -ENOENT) + log_warning_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m"); + else if (r == 0) + retrieve(data, len, "COREDUMP_FILENAME", &filename); + + if (filename && access(filename, R_OK) < 0) { + log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, + "File %s is not readable: %m", filename); + free(filename); + filename = NULL; } - return r; -} -static int dump_core(sd_journal* j) { - const void *data; - size_t len, ret; - int r; + if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { + if (path) { + *path = filename; + filename = NULL; + } - assert(j); + return 0; + } else { + _cleanup_close_ int fdt = -1; + char *temp = NULL; - /* We want full data, nothing truncated. */ - sd_journal_set_data_threshold(j, 0); + if (fd < 0) { + temp = strdup("/var/tmp/coredump-XXXXXX"); + if (!temp) + return log_oom(); - r = focus(j); - if (r < 0) - return r; + fdt = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC); + if (fdt < 0) + return log_error_errno(errno, "Failed to create temporary file: %m"); + log_debug("Created temporary file %s", temp); - print_info(output ? stdout : stderr, j, false); + fd = fdt; + } - if (on_tty() && !output) { - log_error("Refusing to dump core to tty."); - return -ENOTTY; - } + r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); + if (r == 0) { + ssize_t sz; - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r == ENOENT) { - _cleanup_free_ char *fn = NULL; - _cleanup_close_ int fd = -1; + assert(len >= 9); + data += 9; + len -= 9; - r = make_coredump_path(j, &fn); - if (r < 0) - return r; + sz = write(fdt, data, len); + if (sz < 0) { + log_error_errno(errno, "Failed to write temporary file: %m"); + r = -errno; + goto error; + } + if (sz != (ssize_t) len) { + log_error("Short write to temporary file."); + r = -EIO; + goto error; + } + } else if (filename) { +#if defined(HAVE_XZ) || defined(HAVE_LZ4) + _cleanup_close_ int fdf; + + fdf = open(filename, O_RDONLY | O_CLOEXEC); + if (fdf < 0) { + log_error_errno(errno, "Failed to open %s: %m", filename); + r = -errno; + goto error; + } - fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOCTTY); - if (fd < 0) { - if (errno == ENOENT) - log_error("Coredump neither in journal file nor stored externally on disk."); + r = decompress_stream(filename, fdf, fd, -1); + if (r < 0) { + log_error_errno(r, "Failed to decompress %s: %m", filename); + goto error; + } +#else + log_error("Cannot decompress file. Compiled without compression support."); + r = -ENOTSUP; + goto error; +#endif + } else { + if (r == -ENOENT) + log_error("Cannot retrieve coredump from journal nor disk."); else - log_error("Failed to open coredump file: %s", strerror(-r)); + log_error_errno(r, "Failed to retrieve COREDUMP field: %m"); + goto error; + } - return -errno; + if (temp) { + *path = temp; + *unlink_temp = true; } - r = copy_bytes(fd, output ? fileno(output) : STDOUT_FILENO); - if (r < 0) { - log_error("Failed to stream coredump: %s", strerror(-r)); - return r; + return 0; + +error: + if (temp) { + unlink(temp); + log_debug("Removed temporary file %s", temp); } + return r; + } +} - } else if (r < 0) { - log_error("Failed to retrieve COREDUMP field: %s", strerror(-r)); +static int dump_core(sd_journal* j) { + int r; + + assert(j); + + r = focus(j); + if (r < 0) return r; - } else { - assert(len >= 9); - data = (const uint8_t*) data + 9; - len -= 9; - - ret = fwrite(data, len, 1, output ?: stdout); - if (ret != 1) { - log_error("Dumping coredump failed: %m (%zu)", ret); - return -errno; - } + print_info(arg_output ? stdout : stderr, j, false); + + if (on_tty() && !arg_output) { + log_error("Refusing to dump core to tty."); + return -ENOTTY; } + r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL); + if (r < 0) + return log_error_errno(r, "Coredump retrieval failed: %m"); + r = sd_journal_previous(j); if (r >= 0) log_warning("More than one entry matches, ignoring rest."); @@ -649,12 +705,9 @@ static int dump_core(sd_journal* j) { } static int run_gdb(sd_journal *j) { - - _cleanup_free_ char *exe = NULL, *coredump = NULL; - char temp[] = "/var/tmp/coredump-XXXXXX"; - bool unlink_temp = false; - const char *path; - const void *data; + _cleanup_free_ char *exe = NULL, *path = NULL; + bool unlink_path = false; + const char *data; siginfo_t st; size_t len; pid_t pid; @@ -662,8 +715,6 @@ static int run_gdb(sd_journal *j) { assert(j); - sd_journal_set_data_threshold(j, 0); - r = focus(j); if (r < 0) return r; @@ -672,14 +723,12 @@ static int run_gdb(sd_journal *j) { fputs("\n", stdout); r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len); - if (r < 0) { - log_error("Failed to retrieve COREDUMP_EXE field: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m"); - assert(len >= 13); - data = (const uint8_t*) data + 13; - len -= 13; + assert(len > strlen("COREDUMP_EXE=")); + data += strlen("COREDUMP_EXE="); + len -= strlen("COREDUMP_EXE="); exe = strndup(data, len); if (!exe) @@ -695,83 +744,36 @@ static int run_gdb(sd_journal *j) { return -ENOENT; } - r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len); - if (r == -ENOENT) { - - r = make_coredump_path(j, &coredump); - if (r < 0) - return r; - - if (access(coredump, R_OK) < 0) { - if (errno == ENOENT) - log_error("Coredump neither in journal file nor stored externally on disk."); - else - log_error("Failed to access coredump file: %m"); - - return -errno; - } - - path = coredump; - - } else if (r < 0) { - log_error("Failed to retrieve COREDUMP field: %s", strerror(-r)); - return r; - - } else { - _cleanup_close_ int fd = -1; - ssize_t sz; - - assert(len >= 9); - data = (const uint8_t*) data + 9; - len -= 9; - - fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC); - if (fd < 0) { - log_error("Failed to create temporary file: %m"); - return -errno; - } - - unlink_temp = true; - - sz = write(fd, data, len); - if (sz < 0) { - log_error("Failed to write temporary file: %m"); - r = -errno; - goto finish; - } - if (sz != (ssize_t) len) { - log_error("Short write to temporary file."); - r = -EIO; - goto finish; - } - - path = temp; - } + r = save_core(j, -1, &path, &unlink_path); + if (r < 0) + return log_error_errno(r, "Failed to retrieve core: %m"); pid = fork(); if (pid < 0) { - log_error("Failed to fork(): %m"); + log_error_errno(errno, "Failed to fork(): %m"); r = -errno; goto finish; } if (pid == 0) { execlp("gdb", "gdb", exe, path, NULL); - log_error("Failed to invoke gdb: %m"); + log_error_errno(errno, "Failed to invoke gdb: %m"); _exit(1); } r = wait_for_terminate(pid, &st); if (r < 0) { - log_error("Failed to wait for gdb: %m"); + log_error_errno(errno, "Failed to wait for gdb: %m"); goto finish; } r = st.si_code == CLD_EXITED ? st.si_status : 255; finish: - if (unlink_temp) - unlink(temp); + if (unlink_path) { + log_debug("Removed temporary file %s", path); + unlink(path); + } return r; } @@ -800,17 +802,22 @@ int main(int argc, char *argv[]) { if (arg_action == ACTION_NONE) goto end; + sigbus_install(); + r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); if (r < 0) { - log_error("Failed to open journal: %s", strerror(-r)); + log_error_errno(r, "Failed to open journal: %m"); goto end; } + /* We want full data, nothing truncated. */ + sd_journal_set_data_threshold(j, 0); + SET_FOREACH(match, matches, it) { r = sd_journal_add_match(j, match, strlen(match)); if (r != 0) { - log_error("Failed to add match '%s': %s", - match, strerror(-r)); + log_error_errno(r, "Failed to add match '%s': %m", + match); goto end; } } @@ -847,8 +854,8 @@ int main(int argc, char *argv[]) { end: pager_close(); - if (output) - fclose(output); + if (arg_output) + fclose(arg_output); return r >= 0 ? r : EXIT_FAILURE; }