| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fnmatch.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "glob-util.h" |
| 8 |
#include "hostname-util.h" |
| 9 |
#include "id128-util.h" |
| 10 |
#include "journal-util.h" |
| 11 |
#include "journalctl.h" |
| 12 |
#include "journalctl-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "logs-show.h" |
| 15 |
#include "nulstr-util.h" |
| 16 |
#include "set.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "time-util.h" |
| 20 |
#include "unit-name.h" |
| 21 |
|
| 22 |
char* format_timestamp_maybe_utc(char *buf, size_t l, usec_t t) { |
| 23 |
assert(buf); |
| 24 |
|
| 25 |
if (arg_utc) |
| 26 |
return format_timestamp_style(buf, l, t, TIMESTAMP_UTC); |
| 27 |
|
| 28 |
return format_timestamp(buf, l, t); |
| 29 |
} |
| 30 |
|
| 31 |
int acquire_journal(sd_journal **ret) { |
| 32 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(ret); |
| 36 |
|
| 37 |
journal_browse_prepare(); |
| 38 |
|
| 39 |
if (arg_directory) |
| 40 |
r = sd_journal_open_directory(&j, arg_directory, arg_journal_type | arg_journal_additional_open_flags); |
| 41 |
else if (arg_root) |
| 42 |
r = sd_journal_open_directory(&j, arg_root, arg_journal_type | arg_journal_additional_open_flags | SD_JOURNAL_OS_ROOT); |
| 43 |
else if (arg_file_stdin) |
| 44 |
r = sd_journal_open_files_fd(&j, (int[]) { STDIN_FILENO }, 1, arg_journal_additional_open_flags); |
| 45 |
else if (arg_file) |
| 46 |
r = sd_journal_open_files(&j, (const char**) arg_file, arg_journal_additional_open_flags); |
| 47 |
else if (arg_machine) { |
| 48 |
_cleanup_free_ char *u = NULL, *h = NULL; |
| 49 |
|
| 50 |
r = split_user_at_host(arg_machine, &u, &h); |
| 51 |
if (r < 0) |
| 52 |
return log_error_errno(r, "Failed to split machine specification '%s': %m", arg_machine); |
| 53 |
|
| 54 |
if (!isempty(u) && !streq(u, "root")) |
| 55 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Connecting to a machine as non-root is not supported."); |
| 56 |
|
| 57 |
r = journal_open_machine(&j, h ?: ".host", arg_journal_additional_open_flags); |
| 58 |
} else |
| 59 |
r = sd_journal_open_namespace( |
| 60 |
&j, |
| 61 |
arg_namespace, |
| 62 |
(arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY) | |
| 63 |
arg_namespace_flags | arg_journal_type | arg_journal_additional_open_flags); |
| 64 |
if (r < 0) |
| 65 |
return log_error_errno(r, "Failed to open %s: %m", arg_directory ?: arg_file ? "files" : "journal"); |
| 66 |
|
| 67 |
r = journal_access_check_and_warn(j, arg_quiet, |
| 68 |
!(arg_journal_type == SD_JOURNAL_CURRENT_USER || arg_user_units)); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
*ret = TAKE_PTR(j); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
bool journal_boot_has_effect(sd_journal *j) { |
| 77 |
assert(j); |
| 78 |
|
| 79 |
if (arg_boot_offset != 0 && |
| 80 |
sd_journal_has_runtime_files(j) > 0 && |
| 81 |
sd_journal_has_persistent_files(j) == 0) { |
| 82 |
log_info("Specifying boot ID or boot offset has no effect, no persistent journal was found."); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
int journal_acquire_boot(sd_journal *j) { |
| 90 |
int r; |
| 91 |
|
| 92 |
assert(j); |
| 93 |
|
| 94 |
if (!arg_boot) |
| 95 |
return 0; |
| 96 |
|
| 97 |
/* Take a shortcut and use the current boot_id, which we can do very quickly. |
| 98 |
* We can do this only when the logs are coming from the current machine, |
| 99 |
* so take the slow path if log location is specified. */ |
| 100 |
if (arg_boot_offset == 0 && sd_id128_is_null(arg_boot_id) && |
| 101 |
!arg_directory && !arg_file && !arg_file_stdin && !arg_root) { |
| 102 |
r = id128_get_boot_for_machine(arg_machine, &arg_boot_id); |
| 103 |
if (r < 0) |
| 104 |
return log_error_errno(r, "Failed to get boot ID%s%s: %m", |
| 105 |
isempty(arg_machine) ? "" : " of container ", strempty(arg_machine)); |
| 106 |
} else { |
| 107 |
sd_id128_t boot_id; |
| 108 |
|
| 109 |
r = journal_find_boot(j, arg_boot_id, arg_boot_offset, &boot_id); |
| 110 |
if (r < 0) |
| 111 |
return log_error_errno(r, "Failed to find journal entry for the specified boot (%s%+i): %m", |
| 112 |
sd_id128_is_null(arg_boot_id) ? "" : SD_ID128_TO_STRING(arg_boot_id), |
| 113 |
arg_boot_offset); |
| 114 |
if (r == 0) |
| 115 |
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), |
| 116 |
"No journal boot entry found for the specified boot (%s%+i).", |
| 117 |
sd_id128_is_null(arg_boot_id) ? "" : SD_ID128_TO_STRING(arg_boot_id), |
| 118 |
arg_boot_offset); |
| 119 |
|
| 120 |
log_debug("Found boot %s for %s%+i", |
| 121 |
SD_ID128_TO_STRING(boot_id), |
| 122 |
sd_id128_is_null(arg_boot_id) ? "" : SD_ID128_TO_STRING(arg_boot_id), |
| 123 |
arg_boot_offset); |
| 124 |
|
| 125 |
arg_boot_id = boot_id; |
| 126 |
} |
| 127 |
|
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
int get_possible_units( |
| 132 |
sd_journal *j, |
| 133 |
const char *fields, |
| 134 |
char * const *patterns, |
| 135 |
Set **ret) { |
| 136 |
|
| 137 |
_cleanup_set_free_ Set *found = NULL; |
| 138 |
int r; |
| 139 |
|
| 140 |
assert(j); |
| 141 |
assert(fields); |
| 142 |
assert(ret); |
| 143 |
|
| 144 |
NULSTR_FOREACH(field, fields) { |
| 145 |
const void *data; |
| 146 |
size_t size; |
| 147 |
|
| 148 |
r = sd_journal_query_unique(j, field); |
| 149 |
if (r < 0) |
| 150 |
return r; |
| 151 |
|
| 152 |
SD_JOURNAL_FOREACH_UNIQUE(j, data, size) { |
| 153 |
_cleanup_free_ char *u = NULL; |
| 154 |
|
| 155 |
const char *eq = memchr(data, '=', size); |
| 156 |
if (eq) { |
| 157 |
size -= eq - (char*) data + 1; |
| 158 |
data = ++eq; |
| 159 |
} |
| 160 |
|
| 161 |
u = strndup(data, size); |
| 162 |
if (!u) |
| 163 |
return -ENOMEM; |
| 164 |
|
| 165 |
size_t i; |
| 166 |
if (!strv_fnmatch_full(patterns, u, FNM_NOESCAPE, &i)) |
| 167 |
continue; |
| 168 |
|
| 169 |
log_debug("Matched %s with pattern %s=%s", u, field, patterns[i]); |
| 170 |
r = set_ensure_consume(&found, &string_hash_ops_free, TAKE_PTR(u)); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
*ret = TAKE_PTR(found); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
int acquire_unit(sd_journal *j, const char *option_name, const char **ret_unit, LogIdType *ret_type) { |
| 181 |
size_t n; |
| 182 |
int r; |
| 183 |
|
| 184 |
assert(j); |
| 185 |
assert(option_name); |
| 186 |
assert(ret_unit); |
| 187 |
assert(ret_type); |
| 188 |
|
| 189 |
n = strv_length(arg_system_units) + strv_length(arg_user_units); |
| 190 |
if (n <= 0) |
| 191 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 192 |
"Using %s requires a unit. Please specify a unit name with -u/--unit=/--user-unit=.", |
| 193 |
option_name); |
| 194 |
if (n > 1) |
| 195 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 196 |
"Using %s with multiple units is not supported.", |
| 197 |
option_name); |
| 198 |
|
| 199 |
LogIdType type; |
| 200 |
char **units; |
| 201 |
if (!strv_isempty(arg_system_units)) { |
| 202 |
type = LOG_SYSTEM_UNIT_INVOCATION_ID; |
| 203 |
units = arg_system_units; |
| 204 |
} else { |
| 205 |
assert(!strv_isempty(arg_user_units)); |
| 206 |
type = LOG_USER_UNIT_INVOCATION_ID; |
| 207 |
units = arg_user_units; |
| 208 |
} |
| 209 |
|
| 210 |
_cleanup_free_ char *u = NULL; |
| 211 |
r = unit_name_mangle(units[0], UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN), &u); |
| 212 |
if (r < 0) |
| 213 |
return log_error_errno(r, "Failed to mangle unit name '%s': %m", units[0]); |
| 214 |
|
| 215 |
if (string_is_glob(u)) { |
| 216 |
_cleanup_set_free_ Set *s = NULL; |
| 217 |
|
| 218 |
r = get_possible_units(j, type == LOG_SYSTEM_UNIT_INVOCATION_ID ? SYSTEM_UNITS : USER_UNITS, |
| 219 |
STRV_MAKE(u), &s); |
| 220 |
if (r < 0) |
| 221 |
return log_error_errno(r, "Failed to get matching unit '%s' from journal: %m", u); |
| 222 |
if (set_isempty(s)) |
| 223 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No matching unit found for '%s' in journal.", u); |
| 224 |
if (set_size(s) > 1) |
| 225 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Multiple matching units found for '%s' in journal.", u); |
| 226 |
|
| 227 |
char *found = set_steal_first(s); |
| 228 |
log_debug("Found matching unit '%s' for '%s'.", found, u); |
| 229 |
|
| 230 |
free_and_replace(units[0], found); |
| 231 |
assert(set_isempty(s)); |
| 232 |
} else |
| 233 |
free_and_replace(units[0], u); |
| 234 |
|
| 235 |
*ret_type = type; |
| 236 |
*ret_unit = units[0]; |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
int journal_acquire_invocation(sd_journal *j) { |
| 241 |
LogIdType type = LOG_SYSTEM_UNIT_INVOCATION_ID; |
| 242 |
const char *unit = NULL; |
| 243 |
sd_id128_t id; |
| 244 |
int r; |
| 245 |
|
| 246 |
assert(j); |
| 247 |
|
| 248 |
/* journal_acquire_boot() must be called before this. */ |
| 249 |
|
| 250 |
if (!arg_invocation) { |
| 251 |
/* Clear relevant field for safety. */ |
| 252 |
arg_invocation_id = SD_ID128_NULL; |
| 253 |
arg_invocation_offset = 0; |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
/* When an invocation ID is explicitly specified without an offset, we do not care the ID is about |
| 258 |
* system unit or user unit, and calling without unit name is allowed. Otherwise, a unit name must |
| 259 |
* be specified. */ |
| 260 |
if (arg_invocation_offset != 0 || sd_id128_is_null(arg_invocation_id)) { |
| 261 |
r = acquire_unit(j, "-I/--invocation= with an offset", &unit, &type); |
| 262 |
if (r < 0) |
| 263 |
return r; |
| 264 |
} |
| 265 |
|
| 266 |
r = journal_find_log_id(j, type, arg_boot_id, unit, arg_invocation_id, arg_invocation_offset, &id); |
| 267 |
if (r < 0) |
| 268 |
return log_error_errno(r, "Failed to find journal entry for the invocation (%s%+i): %m", |
| 269 |
sd_id128_is_null(arg_invocation_id) ? "" : SD_ID128_TO_STRING(arg_invocation_id), |
| 270 |
arg_invocation_offset); |
| 271 |
if (r == 0) |
| 272 |
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), |
| 273 |
"No journal entry found for the invocation (%s%+i).", |
| 274 |
sd_id128_is_null(arg_invocation_id) ? "" : SD_ID128_TO_STRING(arg_invocation_id), |
| 275 |
arg_invocation_offset); |
| 276 |
|
| 277 |
log_debug("Found invocation ID %s for %s%+i", |
| 278 |
SD_ID128_TO_STRING(id), |
| 279 |
sd_id128_is_null(arg_invocation_id) ? "" : SD_ID128_TO_STRING(arg_invocation_id), |
| 280 |
arg_invocation_offset); |
| 281 |
|
| 282 |
arg_invocation_id = id; |
| 283 |
return 1; |
| 284 |
} |