coredumpctl: move acquire_journal() and focus() to coredumpctl-journal.c
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
+1
-112
@@ -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,
|
||||
|
||||
@@ -20,6 +20,7 @@ systemd_coredump_export_sources = files(
|
||||
coredumpctl_sources = files(
|
||||
'coredumpctl.c',
|
||||
'coredumpctl-info.c',
|
||||
'coredumpctl-journal.c',
|
||||
'coredumpctl-util.c',
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user