| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-journal.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "dirent-util.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "format-table.h" |
| 9 |
#include "format-util.h" |
| 10 |
#include "hashmap.h" |
| 11 |
#include "journal-internal.h" |
| 12 |
#include "journal-verify.h" |
| 13 |
#include "journalctl.h" |
| 14 |
#include "journalctl-misc.h" |
| 15 |
#include "journalctl-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "logs-show.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "syslog-util.h" |
| 20 |
#include "time-util.h" |
| 21 |
|
| 22 |
int action_print_header(void) { |
| 23 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 24 |
int r; |
| 25 |
|
| 26 |
assert(arg_action == ACTION_PRINT_HEADER); |
| 27 |
|
| 28 |
r = acquire_journal(&j); |
| 29 |
if (r < 0) |
| 30 |
return r; |
| 31 |
|
| 32 |
journal_print_header(j); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
int action_verify(void) { |
| 37 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 38 |
int r; |
| 39 |
|
| 40 |
assert(arg_action == ACTION_VERIFY); |
| 41 |
|
| 42 |
r = acquire_journal(&j); |
| 43 |
if (r < 0) |
| 44 |
return r; |
| 45 |
|
| 46 |
log_show_color(true); |
| 47 |
|
| 48 |
JournalFile *f; |
| 49 |
ORDERED_HASHMAP_FOREACH(f, j->files) { |
| 50 |
int k; |
| 51 |
usec_t first = 0, validated = 0, last = 0; |
| 52 |
|
| 53 |
#if HAVE_GCRYPT |
| 54 |
if (!arg_verify_key && JOURNAL_HEADER_SEALED(f->header)) |
| 55 |
log_notice("Journal file %s has sealing enabled but verification key has not been passed using --verify-key=.", f->path); |
| 56 |
#endif |
| 57 |
|
| 58 |
k = journal_file_verify(f, arg_verify_key, &first, &validated, &last, /* show_progress= */ !arg_quiet); |
| 59 |
if (k == -EINVAL) |
| 60 |
/* If the key was invalid give up right-away. */ |
| 61 |
return k; |
| 62 |
if (k < 0) |
| 63 |
r = log_warning_errno(k, "FAIL: %s (%m)", f->path); |
| 64 |
else { |
| 65 |
char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX]; |
| 66 |
log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "PASS: %s", f->path); |
| 67 |
|
| 68 |
if (arg_verify_key && JOURNAL_HEADER_SEALED(f->header)) { |
| 69 |
if (validated > 0) { |
| 70 |
log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
| 71 |
"=> Validated from %s to %s, final %s entries not sealed.", |
| 72 |
format_timestamp_maybe_utc(a, sizeof(a), first), |
| 73 |
format_timestamp_maybe_utc(b, sizeof(b), validated), |
| 74 |
FORMAT_TIMESPAN(last > validated ? last - validated : 0, 0)); |
| 75 |
} else if (last > 0) |
| 76 |
log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
| 77 |
"=> No sealing yet, %s of entries not sealed.", |
| 78 |
FORMAT_TIMESPAN(last - first, 0)); |
| 79 |
else |
| 80 |
log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
| 81 |
"=> No sealing yet, no entries in file."); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return r; |
| 87 |
} |
| 88 |
|
| 89 |
int action_disk_usage(void) { |
| 90 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 91 |
uint64_t bytes = 0; |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(arg_action == ACTION_DISK_USAGE); |
| 95 |
|
| 96 |
r = acquire_journal(&j); |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
|
| 100 |
r = sd_journal_get_usage(j, &bytes); |
| 101 |
if (r < 0) |
| 102 |
return log_error_errno(r, "Failed to get disk usage: %m"); |
| 103 |
|
| 104 |
printf("Archived and active journals take up %s in the file system.\n", FORMAT_BYTES(bytes)); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static int show_log_ids(const LogId *ids, size_t n_ids, const char *name) { |
| 109 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 110 |
int r; |
| 111 |
|
| 112 |
assert(ids); |
| 113 |
assert(n_ids > 0); |
| 114 |
assert((uint64_t) INT64_MAX - n_ids > 0); |
| 115 |
assert(name); |
| 116 |
|
| 117 |
table = table_new("idx", name, "first entry", "last entry"); |
| 118 |
if (!table) |
| 119 |
return log_oom(); |
| 120 |
|
| 121 |
if (arg_full) |
| 122 |
table_set_width(table, 0); |
| 123 |
|
| 124 |
r = table_set_json_field_name(table, 0, "index"); |
| 125 |
if (r < 0) |
| 126 |
return log_error_errno(r, "Failed to set JSON field name of column 0: %m"); |
| 127 |
|
| 128 |
(void) table_set_sort(table, (size_t) 0); |
| 129 |
(void) table_set_reverse(table, 0, arg_reverse); |
| 130 |
|
| 131 |
for (size_t i = 0; i < n_ids; i++) { |
| 132 |
int64_t index; |
| 133 |
|
| 134 |
if (arg_lines_needs_seek_end()) |
| 135 |
/* With --lines=N, we only know the negative index, and the older ID is located earlier. */ |
| 136 |
index = - (int64_t) i; |
| 137 |
else if (arg_lines >= 0) |
| 138 |
/* With --lines=+N, we only know the positive index, and the newer ID is located earlier. */ |
| 139 |
index = i + 1; |
| 140 |
else |
| 141 |
/* Otherwise, show negative index. Note, in this case, newer ID is located earlier. */ |
| 142 |
index = (int64_t) (i + 1) - (int64_t) n_ids; |
| 143 |
|
| 144 |
r = table_add_many(table, |
| 145 |
TABLE_INT64, index, |
| 146 |
TABLE_SET_ALIGN_PERCENT, 100, |
| 147 |
TABLE_ID128, ids[i].id, |
| 148 |
TABLE_TIMESTAMP, ids[i].first_usec, |
| 149 |
TABLE_TIMESTAMP, ids[i].last_usec); |
| 150 |
if (r < 0) |
| 151 |
return table_log_add_error(r); |
| 152 |
} |
| 153 |
|
| 154 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet); |
| 155 |
if (r < 0) |
| 156 |
return r; |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
int action_list_boots(void) { |
| 162 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 163 |
_cleanup_free_ LogId *ids = NULL; |
| 164 |
size_t n_ids; |
| 165 |
int r; |
| 166 |
|
| 167 |
assert(arg_action == ACTION_LIST_BOOTS); |
| 168 |
|
| 169 |
r = acquire_journal(&j); |
| 170 |
if (r < 0) |
| 171 |
return r; |
| 172 |
|
| 173 |
r = journal_get_boots( |
| 174 |
j, |
| 175 |
/* advance_older= */ arg_lines_needs_seek_end(), |
| 176 |
/* max_ids= */ arg_lines >= 0 ? (size_t) arg_lines : SIZE_MAX, |
| 177 |
&ids, &n_ids); |
| 178 |
if (r < 0) |
| 179 |
return log_error_errno(r, "Failed to determine boots: %m"); |
| 180 |
if (r == 0) |
| 181 |
return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(ENODATA), |
| 182 |
"No boot found."); |
| 183 |
|
| 184 |
return show_log_ids(ids, n_ids, "boot id"); |
| 185 |
} |
| 186 |
|
| 187 |
int action_list_fields(void) { |
| 188 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 189 |
int r, n_shown = 0; |
| 190 |
|
| 191 |
assert(arg_action == ACTION_LIST_FIELDS); |
| 192 |
assert(arg_field); |
| 193 |
|
| 194 |
r = acquire_journal(&j); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
if (!journal_boot_has_effect(j)) |
| 199 |
return 0; |
| 200 |
|
| 201 |
r = sd_journal_set_data_threshold(j, 0); |
| 202 |
if (r < 0) |
| 203 |
return log_error_errno(r, "Failed to unset data size threshold: %m"); |
| 204 |
|
| 205 |
r = sd_journal_query_unique(j, arg_field); |
| 206 |
if (r < 0) |
| 207 |
return log_error_errno(r, "Failed to query unique data objects: %m"); |
| 208 |
|
| 209 |
const void *data; |
| 210 |
size_t size; |
| 211 |
SD_JOURNAL_FOREACH_UNIQUE(j, data, size) { |
| 212 |
const void *eq; |
| 213 |
|
| 214 |
if (arg_lines >= 0 && n_shown >= arg_lines) |
| 215 |
break; |
| 216 |
|
| 217 |
eq = memchr(data, '=', size); |
| 218 |
if (eq) |
| 219 |
printf("%.*s\n", (int) (size - ((const uint8_t*) eq - (const uint8_t*) data + 1)), (const char*) eq + 1); |
| 220 |
else |
| 221 |
printf("%.*s\n", (int) size, (const char*) data); |
| 222 |
|
| 223 |
n_shown++; |
| 224 |
} |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
int action_list_field_names(void) { |
| 230 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 231 |
int r; |
| 232 |
|
| 233 |
assert(arg_action == ACTION_LIST_FIELD_NAMES); |
| 234 |
|
| 235 |
r = acquire_journal(&j); |
| 236 |
if (r < 0) |
| 237 |
return r; |
| 238 |
|
| 239 |
const char *field; |
| 240 |
SD_JOURNAL_FOREACH_FIELD(j, field) |
| 241 |
printf("%s\n", field); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
int action_list_invocations(void) { |
| 247 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 248 |
_cleanup_free_ LogId *ids = NULL; |
| 249 |
size_t n_ids; |
| 250 |
LogIdType type; |
| 251 |
const char *unit; |
| 252 |
int r; |
| 253 |
|
| 254 |
assert(arg_action == ACTION_LIST_INVOCATIONS); |
| 255 |
|
| 256 |
r = acquire_journal(&j); |
| 257 |
if (r < 0) |
| 258 |
return r; |
| 259 |
|
| 260 |
r = acquire_unit(j, "--list-invocations", &unit, &type); |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
|
| 264 |
r = journal_acquire_boot(j); |
| 265 |
if (r < 0) |
| 266 |
return r; |
| 267 |
|
| 268 |
r = journal_get_log_ids( |
| 269 |
j, type, |
| 270 |
/* boot_id= */ arg_boot_id, /* unit= */ unit, |
| 271 |
/* advance_older= */ arg_lines_needs_seek_end(), |
| 272 |
/* max_ids= */ arg_lines >= 0 ? (size_t) arg_lines : SIZE_MAX, |
| 273 |
&ids, &n_ids); |
| 274 |
if (r < 0) |
| 275 |
return log_error_errno(r, "Failed to list invocation id for %s: %m", unit); |
| 276 |
if (r == 0) |
| 277 |
return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(ENODATA), |
| 278 |
"No invocation ID for %s found.", unit); |
| 279 |
|
| 280 |
return show_log_ids(ids, n_ids, "invocation id"); |
| 281 |
} |
| 282 |
|
| 283 |
int action_list_namespaces(void) { |
| 284 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 285 |
sd_id128_t machine; |
| 286 |
int r; |
| 287 |
|
| 288 |
assert(arg_action == ACTION_LIST_NAMESPACES); |
| 289 |
|
| 290 |
r = sd_id128_get_machine(&machine); |
| 291 |
if (r < 0) |
| 292 |
return log_error_errno(r, "Failed to get machine ID: %m"); |
| 293 |
|
| 294 |
table = table_new("namespace"); |
| 295 |
if (!table) |
| 296 |
return log_oom(); |
| 297 |
|
| 298 |
(void) table_set_sort(table, (size_t) 0); |
| 299 |
|
| 300 |
FOREACH_STRING(dir, "/var/log/journal", "/run/log/journal") { |
| 301 |
_cleanup_free_ char *path = NULL; |
| 302 |
_cleanup_closedir_ DIR *dirp = NULL; |
| 303 |
|
| 304 |
path = path_join(arg_root, dir); |
| 305 |
if (!path) |
| 306 |
return log_oom(); |
| 307 |
|
| 308 |
dirp = opendir(path); |
| 309 |
if (!dirp) { |
| 310 |
log_debug_errno(errno, "Failed to open directory %s, ignoring: %m", path); |
| 311 |
continue; |
| 312 |
} |
| 313 |
|
| 314 |
FOREACH_DIRENT(de, dirp, return log_error_errno(errno, "Failed to iterate through %s: %m", path)) { |
| 315 |
|
| 316 |
const char *e = strchr(de->d_name, '.'); |
| 317 |
if (!e) |
| 318 |
continue; |
| 319 |
|
| 320 |
_cleanup_free_ char *ids = strndup(de->d_name, e - de->d_name); |
| 321 |
if (!ids) |
| 322 |
return log_oom(); |
| 323 |
|
| 324 |
sd_id128_t id; |
| 325 |
r = sd_id128_from_string(ids, &id); |
| 326 |
if (r < 0) |
| 327 |
continue; |
| 328 |
|
| 329 |
if (!sd_id128_equal(machine, id)) |
| 330 |
continue; |
| 331 |
|
| 332 |
e++; |
| 333 |
|
| 334 |
if (!log_namespace_name_valid(e)) |
| 335 |
continue; |
| 336 |
|
| 337 |
r = table_add_cell(table, NULL, TABLE_STRING, e); |
| 338 |
if (r < 0) |
| 339 |
return table_log_add_error(r); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if (table_isempty(table) && !sd_json_format_enabled(arg_json_format_flags)) { |
| 344 |
if (!arg_quiet) |
| 345 |
log_notice("No namespaces found."); |
| 346 |
} else { |
| 347 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet); |
| 348 |
if (r < 0) |
| 349 |
return r; |
| 350 |
} |
| 351 |
|
| 352 |
return 0; |
| 353 |
} |