| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-daemon.h" |
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-journal.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "ansi-color.h" |
| 11 |
#include "fileio.h" |
| 12 |
#include "journalctl.h" |
| 13 |
#include "journalctl-filter.h" |
| 14 |
#include "journalctl-show.h" |
| 15 |
#include "journalctl-util.h" |
| 16 |
#include "journalctl-varlink.h" |
| 17 |
#include "log.h" |
| 18 |
#include "logs-show.h" |
| 19 |
#include "output-mode.h" |
| 20 |
#include "pager.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "terminal-util.h" |
| 23 |
#include "time-util.h" |
| 24 |
|
| 25 |
#define PROCESS_INOTIFY_INTERVAL 1024 /* Every 1024 messages processed */ |
| 26 |
|
| 27 |
typedef struct Context { |
| 28 |
sd_journal *journal; |
| 29 |
bool has_cursor; |
| 30 |
bool need_seek; |
| 31 |
bool since_seeked; |
| 32 |
bool until_safe; |
| 33 |
bool ellipsized; |
| 34 |
bool previous_boot_id_valid; |
| 35 |
sd_id128_t previous_boot_id; |
| 36 |
sd_id128_t previous_boot_id_output; |
| 37 |
dual_timestamp previous_ts_output; |
| 38 |
sd_event *event; |
| 39 |
sd_varlink *synchronize_varlink; |
| 40 |
} Context; |
| 41 |
|
| 42 |
static void context_done(Context *c) { |
| 43 |
assert(c); |
| 44 |
|
| 45 |
sd_varlink_flush_close_unref(c->synchronize_varlink); |
| 46 |
sd_event_unref(c->event); |
| 47 |
sd_journal_close(c->journal); |
| 48 |
} |
| 49 |
|
| 50 |
static int seek_journal(Context *c) { |
| 51 |
sd_journal *j = ASSERT_PTR(ASSERT_PTR(c)->journal); |
| 52 |
_cleanup_free_ char *cursor_from_file = NULL; |
| 53 |
const char *cursor = NULL; |
| 54 |
bool after_cursor = false; |
| 55 |
int r; |
| 56 |
|
| 57 |
if (arg_cursor || arg_after_cursor) { |
| 58 |
assert(!!arg_cursor != !!arg_after_cursor); |
| 59 |
|
| 60 |
cursor = arg_cursor ?: arg_after_cursor; |
| 61 |
after_cursor = arg_after_cursor; |
| 62 |
|
| 63 |
} else if (arg_cursor_file) { |
| 64 |
r = read_one_line_file(arg_cursor_file, &cursor_from_file); |
| 65 |
if (r < 0 && r != -ENOENT) |
| 66 |
return log_error_errno(r, "Failed to read cursor file %s: %m", arg_cursor_file); |
| 67 |
if (r > 0) { |
| 68 |
cursor = cursor_from_file; |
| 69 |
after_cursor = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if (cursor) { |
| 74 |
c->has_cursor = true; |
| 75 |
|
| 76 |
r = sd_journal_seek_cursor(j, cursor); |
| 77 |
if (r < 0) |
| 78 |
return log_error_errno(r, "Failed to seek to cursor: %m"); |
| 79 |
|
| 80 |
r = sd_journal_step_one(j, !arg_reverse); |
| 81 |
if (r < 0) |
| 82 |
return log_error_errno(r, "Failed to iterate through journal: %m"); |
| 83 |
|
| 84 |
if (after_cursor && r > 0) { |
| 85 |
/* With --after-cursor=/--cursor-file= we want to skip the first entry only if it's |
| 86 |
* the entry the cursor is pointing at, otherwise, if some journal filters are used, |
| 87 |
* we might skip the first entry of the filter match, which leads to unexpectedly |
| 88 |
* missing journal entries. */ |
| 89 |
int k; |
| 90 |
|
| 91 |
k = sd_journal_test_cursor(j, cursor); |
| 92 |
if (k < 0) |
| 93 |
return log_error_errno(k, "Failed to test cursor against current entry: %m"); |
| 94 |
if (k > 0) |
| 95 |
/* Current entry matches the one our cursor is pointing at, so let's try |
| 96 |
* to advance the next entry. */ |
| 97 |
r = sd_journal_step_one(j, !arg_reverse); |
| 98 |
} |
| 99 |
|
| 100 |
if (r == 0 && !arg_follow) |
| 101 |
/* We couldn't find the next entry after the cursor. */ |
| 102 |
arg_lines = 0; |
| 103 |
|
| 104 |
} else if (arg_reverse || arg_lines_needs_seek_end()) { |
| 105 |
/* If --reverse and/or --lines=N are specified, things get a little tricky. First we seek to |
| 106 |
* the place of --until if specified, otherwise seek to tail. Then, if --reverse is |
| 107 |
* specified, we search backwards and let the output counter in show() handle --lines for us. |
| 108 |
* If --reverse is unspecified, we just jump backwards arg_lines and search afterwards from |
| 109 |
* there. */ |
| 110 |
|
| 111 |
if (arg_until_set) { |
| 112 |
r = sd_journal_seek_realtime_usec(j, arg_until); |
| 113 |
if (r < 0) |
| 114 |
return log_error_errno(r, "Failed to seek to date: %m"); |
| 115 |
} else { |
| 116 |
r = sd_journal_seek_tail(j); |
| 117 |
if (r < 0) |
| 118 |
return log_error_errno(r, "Failed to seek to tail: %m"); |
| 119 |
} |
| 120 |
|
| 121 |
if (arg_reverse) { |
| 122 |
r = sd_journal_previous(j); |
| 123 |
c->until_safe = true; /* can't possibly go beyond --until= if --reverse */ |
| 124 |
|
| 125 |
} else { /* arg_lines_needs_seek_end() */ |
| 126 |
r = sd_journal_previous_skip(j, arg_lines); |
| 127 |
c->until_safe = r >= arg_lines; /* We have enough lines to output before --until= is hit. |
| 128 |
No need to check timestamp of each journal entry */ |
| 129 |
} |
| 130 |
|
| 131 |
} else if (arg_since_set) { |
| 132 |
/* This is placed after arg_reverse and arg_lines. If --since is used without |
| 133 |
* both, we seek to the place of --since and search afterwards from there. |
| 134 |
* If used with --reverse or --lines, we seek to the tail first and check if |
| 135 |
* the entry is within the range of --since later. */ |
| 136 |
|
| 137 |
r = sd_journal_seek_realtime_usec(j, arg_since); |
| 138 |
if (r < 0) |
| 139 |
return log_error_errno(r, "Failed to seek to date: %m"); |
| 140 |
c->since_seeked = true; |
| 141 |
|
| 142 |
r = sd_journal_next(j); |
| 143 |
|
| 144 |
} else { |
| 145 |
r = sd_journal_seek_head(j); |
| 146 |
if (r < 0) |
| 147 |
return log_error_errno(r, "Failed to seek to head: %m"); |
| 148 |
|
| 149 |
r = sd_journal_next(j); |
| 150 |
} |
| 151 |
if (r < 0) |
| 152 |
return log_error_errno(r, "Failed to iterate through journal: %m"); |
| 153 |
if (r == 0) |
| 154 |
c->need_seek = true; |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int show(Context *c) { |
| 160 |
sd_journal *j = ASSERT_PTR(ASSERT_PTR(c)->journal); |
| 161 |
int r, n_shown = 0; |
| 162 |
|
| 163 |
OutputFlags flags = |
| 164 |
arg_all * OUTPUT_SHOW_ALL | |
| 165 |
arg_full * OUTPUT_FULL_WIDTH | |
| 166 |
colors_enabled() * OUTPUT_COLOR | |
| 167 |
arg_catalog * OUTPUT_CATALOG | |
| 168 |
arg_utc * OUTPUT_UTC | |
| 169 |
arg_truncate_newline * OUTPUT_TRUNCATE_NEWLINE | |
| 170 |
arg_no_hostname * OUTPUT_NO_HOSTNAME; |
| 171 |
|
| 172 |
while (arg_lines < 0 || n_shown < arg_lines || arg_follow) { |
| 173 |
size_t highlight[2] = {}; |
| 174 |
|
| 175 |
if (c->need_seek) { |
| 176 |
r = sd_journal_step_one(j, !arg_reverse); |
| 177 |
if (r < 0) |
| 178 |
return log_error_errno(r, "Failed to iterate through journal: %m"); |
| 179 |
if (r == 0) |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
if (arg_until_set && !c->until_safe) { |
| 184 |
/* If --lines= is set, we usually rely on the n_shown to tell us when to stop. |
| 185 |
* However, in the case where we may have less than --lines= to output let's check |
| 186 |
* whether the individual entries are in range. */ |
| 187 |
|
| 188 |
usec_t usec; |
| 189 |
|
| 190 |
r = sd_journal_get_realtime_usec(j, &usec); |
| 191 |
if (r < 0) |
| 192 |
return log_error_errno(r, "Failed to determine timestamp: %m"); |
| 193 |
if (usec > arg_until) |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
if (arg_since_set && (arg_reverse || !c->since_seeked)) { |
| 198 |
usec_t usec; |
| 199 |
|
| 200 |
r = sd_journal_get_realtime_usec(j, &usec); |
| 201 |
if (r < 0) |
| 202 |
return log_error_errno(r, "Failed to determine timestamp: %m"); |
| 203 |
|
| 204 |
if (usec < arg_since) { |
| 205 |
if (arg_reverse) |
| 206 |
break; /* Reached the earliest entry */ |
| 207 |
|
| 208 |
/* arg_lines >= 0 (!since_seeked): |
| 209 |
* We jumped arg_lines back and it seems to be too much */ |
| 210 |
r = sd_journal_seek_realtime_usec(j, arg_since); |
| 211 |
if (r < 0) |
| 212 |
return log_error_errno(r, "Failed to seek to date: %m"); |
| 213 |
c->since_seeked = true; |
| 214 |
|
| 215 |
/* We just jumped forward, meaning there might suddenly be less than |
| 216 |
* --lines= to show within the --until= range, hence keep a close eye on |
| 217 |
* timestamps from now on. */ |
| 218 |
c->until_safe = false; |
| 219 |
|
| 220 |
c->need_seek = true; |
| 221 |
continue; |
| 222 |
} |
| 223 |
c->since_seeked = true; /* We're surely within the range of --since now */ |
| 224 |
} |
| 225 |
|
| 226 |
if (!arg_merge && !arg_quiet) { |
| 227 |
sd_id128_t boot_id; |
| 228 |
|
| 229 |
r = sd_journal_get_monotonic_usec(j, NULL, &boot_id); |
| 230 |
if (r >= 0) { |
| 231 |
if (c->previous_boot_id_valid && |
| 232 |
!sd_id128_equal(boot_id, c->previous_boot_id)) |
| 233 |
printf("%s-- Boot "SD_ID128_FORMAT_STR" --%s\n", |
| 234 |
ansi_highlight(), SD_ID128_FORMAT_VAL(boot_id), ansi_normal()); |
| 235 |
|
| 236 |
c->previous_boot_id = boot_id; |
| 237 |
c->previous_boot_id_valid = true; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (arg_compiled_pattern) { |
| 242 |
const void *message; |
| 243 |
size_t len; |
| 244 |
|
| 245 |
r = sd_journal_get_data(j, "MESSAGE", &message, &len); |
| 246 |
if (r < 0) { |
| 247 |
if (r == -ENOENT) { |
| 248 |
/* We will skip some entries forward, meaning there might suddenly |
| 249 |
* be less than --lines= to show within the --until= range, hence |
| 250 |
* keep a close eye on timestamps from now on. */ |
| 251 |
if (!arg_reverse) |
| 252 |
c->until_safe = false; |
| 253 |
|
| 254 |
c->need_seek = true; |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
return log_error_errno(r, "Failed to get MESSAGE field: %m"); |
| 259 |
} |
| 260 |
|
| 261 |
message = startswith(message, "MESSAGE="); |
| 262 |
if (!message) { |
| 263 |
/* The data object doesn't carry the expected field prefix, e.g. because |
| 264 |
* the journal file is corrupted. Skip over it instead of aborting. */ |
| 265 |
log_notice("MESSAGE field does not start with \"MESSAGE=\", skipping."); |
| 266 |
|
| 267 |
if (!arg_reverse) |
| 268 |
c->until_safe = false; |
| 269 |
|
| 270 |
c->need_seek = true; |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
r = pattern_matches_and_log(arg_compiled_pattern, message, |
| 275 |
len - strlen("MESSAGE="), highlight); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
if (r == 0) { |
| 279 |
/* We will skip some entries forward, meaning there might suddenly |
| 280 |
* be less than --lines= to show within the --until= range, hence |
| 281 |
* keep a close eye on timestamps from now on. */ |
| 282 |
if (!arg_reverse) |
| 283 |
c->until_safe = false; |
| 284 |
|
| 285 |
c->need_seek = true; |
| 286 |
continue; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
r = show_journal_entry(stdout, j, arg_output, 0, flags, |
| 291 |
arg_output_fields, highlight, &c->ellipsized, |
| 292 |
&c->previous_ts_output, &c->previous_boot_id_output); |
| 293 |
c->need_seek = true; |
| 294 |
if (r == -EADDRNOTAVAIL) |
| 295 |
break; |
| 296 |
if (r < 0) |
| 297 |
return r; |
| 298 |
|
| 299 |
n_shown++; |
| 300 |
|
| 301 |
/* If journalctl take a long time to process messages, and during that time journal file |
| 302 |
* rotation occurs, a journalctl client will keep those rotated files open until it calls |
| 303 |
* sd_journal_process(), which typically happens as a result of calling sd_journal_wait() below |
| 304 |
* in the "following" case. By periodically calling sd_journal_process() during the processing |
| 305 |
* loop we shrink the window of time a client instance has open file descriptors for rotated |
| 306 |
* (deleted) journal files. */ |
| 307 |
if ((n_shown % PROCESS_INOTIFY_INTERVAL) == 0) { |
| 308 |
r = sd_journal_process(j); |
| 309 |
if (r < 0) |
| 310 |
return log_error_errno(r, "Failed to process inotify events: %m"); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return n_shown; |
| 315 |
} |
| 316 |
|
| 317 |
static int show_and_fflush(Context *c) { |
| 318 |
int r; |
| 319 |
|
| 320 |
assert(c); |
| 321 |
|
| 322 |
r = show(c); |
| 323 |
if (r < 0) |
| 324 |
return sd_event_exit(c->event, r); |
| 325 |
|
| 326 |
fflush(stdout); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static int on_journal_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 331 |
Context *c = ASSERT_PTR(userdata); |
| 332 |
int r; |
| 333 |
|
| 334 |
assert(s); |
| 335 |
|
| 336 |
r = sd_journal_process(c->journal); |
| 337 |
if (r < 0) { |
| 338 |
log_error_errno(r, "Failed to process journal events: %m"); |
| 339 |
return sd_event_exit(c->event, r); |
| 340 |
} |
| 341 |
|
| 342 |
return show_and_fflush(c); |
| 343 |
} |
| 344 |
|
| 345 |
static int on_first_event(sd_event_source *s, void *userdata) { |
| 346 |
Context *c = ASSERT_PTR(userdata); |
| 347 |
int r; |
| 348 |
|
| 349 |
assert(s); |
| 350 |
|
| 351 |
r = show_and_fflush(c); |
| 352 |
if (r < 0) |
| 353 |
return r; |
| 354 |
|
| 355 |
if (arg_follow && !c->has_cursor && !arg_since_set) { |
| 356 |
r = sd_journal_get_cursor(c->journal, /* ret= */ NULL); |
| 357 |
if (r == -EADDRNOTAVAIL) { |
| 358 |
/* If we shall operate in --follow mode, and we are unable to get a cursor after |
| 359 |
* doing our first round of output, then this means there was no data to show |
| 360 |
* whatsoever, and we hence have no stable position on any line at all. This means, |
| 361 |
* when we get notified about changes, we shouldn't try to position the cursor at the |
| 362 |
* end of the logs anymore, but at the beginning, since anything showing up from now |
| 363 |
* that matches our filters is good now. Hence, simply disable the effect of --lines= |
| 364 |
* now. */ |
| 365 |
|
| 366 |
r = sd_journal_seek_head(c->journal); |
| 367 |
if (r < 0) |
| 368 |
return log_error_errno(r, "Failed to seek to head: %m"); |
| 369 |
|
| 370 |
c->need_seek = true; |
| 371 |
|
| 372 |
} else if (r < 0) |
| 373 |
return log_error_errno(r, "Failed to get cursor: %m"); |
| 374 |
} |
| 375 |
|
| 376 |
/* Setup and initial processing are done, we're ready to wait for more data. */ |
| 377 |
(void) sd_notify(/* unset_environment= */ false, "READY=1"); |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
static int on_synchronize_reply( |
| 383 |
sd_varlink *vl, |
| 384 |
sd_json_variant *parameters, |
| 385 |
const char *error_id, |
| 386 |
sd_varlink_reply_flags_t flags, |
| 387 |
void *userdata) { |
| 388 |
|
| 389 |
Context *c = ASSERT_PTR(userdata); |
| 390 |
int r; |
| 391 |
|
| 392 |
assert(vl); |
| 393 |
|
| 394 |
if (error_id) { |
| 395 |
log_warning("Failed to synchronize on Journal, ignoring: %s", error_id); |
| 396 |
(void) sd_notifyf(/* unset_environment= */ false, "VARLINKERROR=%s", error_id); |
| 397 |
} |
| 398 |
|
| 399 |
r = show_and_fflush(c); |
| 400 |
if (r < 0) |
| 401 |
return r; |
| 402 |
|
| 403 |
return sd_event_exit(c->event, EXIT_SUCCESS); |
| 404 |
} |
| 405 |
|
| 406 |
static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 407 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 408 |
Context *c = ASSERT_PTR(userdata); |
| 409 |
int r = 0; |
| 410 |
|
| 411 |
assert(s); |
| 412 |
assert(si); |
| 413 |
assert(IN_SET(si->ssi_signo, SIGTERM, SIGINT)); |
| 414 |
|
| 415 |
if (!arg_synchronize_on_exit) |
| 416 |
goto finish; |
| 417 |
|
| 418 |
if (c->synchronize_varlink) /* Already pending? Then exit immediately, so that user can cancel the sync */ |
| 419 |
goto finish; |
| 420 |
|
| 421 |
r = varlink_connect_journal(&vl); |
| 422 |
if (r < 0) { |
| 423 |
log_error_errno(r, "Failed to connect to Journal Varlink IPC interface, skipping synchronization: %m"); |
| 424 |
goto finish; |
| 425 |
} |
| 426 |
|
| 427 |
/* Set a low priority on the idle event handler, so that we show any log messages first */ |
| 428 |
r = sd_varlink_attach_event(vl, c->event, SD_EVENT_PRIORITY_IDLE); |
| 429 |
if (r < 0) { |
| 430 |
log_warning_errno(r, "Failed to attach Varlink connection to event loop: %m"); |
| 431 |
goto finish; |
| 432 |
} |
| 433 |
|
| 434 |
r = sd_varlink_bind_reply(vl, on_synchronize_reply); |
| 435 |
if (r < 0) { |
| 436 |
log_warning_errno(r, "Failed to bind synchronization reply: %m"); |
| 437 |
goto finish; |
| 438 |
} |
| 439 |
|
| 440 |
(void) sd_varlink_set_userdata(vl, c); |
| 441 |
|
| 442 |
r = sd_varlink_invokebo( |
| 443 |
vl, |
| 444 |
"io.systemd.Journal.Synchronize", |
| 445 |
SD_JSON_BUILD_PAIR_BOOLEAN("offline", false)); |
| 446 |
if (r < 0) { |
| 447 |
log_warning_errno(r, "Failed to issue synchronization request: %m"); |
| 448 |
goto finish; |
| 449 |
} |
| 450 |
|
| 451 |
c->synchronize_varlink = TAKE_PTR(vl); |
| 452 |
return 0; |
| 453 |
|
| 454 |
finish: |
| 455 |
return sd_event_exit(c->event, r); |
| 456 |
} |
| 457 |
|
| 458 |
static int setup_event(Context *c, int fd) { |
| 459 |
int r; |
| 460 |
|
| 461 |
assert(arg_follow); |
| 462 |
assert(c); |
| 463 |
assert(fd >= 0); |
| 464 |
assert(!c->event); |
| 465 |
|
| 466 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 467 |
r = sd_event_default(&e); |
| 468 |
if (r < 0) |
| 469 |
return log_error_errno(r, "Failed to allocate sd_event object: %m"); |
| 470 |
|
| 471 |
(void) sd_event_add_signal(e, /* ret= */ NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, on_signal, c); |
| 472 |
(void) sd_event_add_signal(e, /* ret= */ NULL, SIGINT | SD_EVENT_SIGNAL_PROCMASK, on_signal, c); |
| 473 |
|
| 474 |
r = sd_event_add_io(e, /* ret= */ NULL, fd, EPOLLIN, &on_journal_event, c); |
| 475 |
if (r < 0) |
| 476 |
return log_error_errno(r, "Failed to add io event source for journal: %m"); |
| 477 |
|
| 478 |
/* Also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that, i.e. when it is closed. */ |
| 479 |
r = sd_event_add_io(e, /* ret= */ NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, /* callback= */ NULL, /* userdata= */ NULL); |
| 480 |
if (r == -EPERM) |
| 481 |
/* Installing an epoll watch on a regular file doesn't work and fails with EPERM. Which is |
| 482 |
* totally OK, handle it gracefully. epoll_ctl() documents EPERM as the error returned when |
| 483 |
* the specified fd doesn't support epoll, hence it's safe to check for that. */ |
| 484 |
log_debug_errno(r, "Unable to install EPOLLHUP watch on stderr, not watching for hangups."); |
| 485 |
else if (r < 0) |
| 486 |
return log_error_errno(r, "Failed to add io event source for stdout: %m"); |
| 487 |
|
| 488 |
r = sd_event_add_defer(e, NULL, on_first_event, c); |
| 489 |
if (r < 0) |
| 490 |
return log_error_errno(r, "Failed to add defer event source: %m"); |
| 491 |
|
| 492 |
c->event = TAKE_PTR(e); |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static int update_cursor(sd_journal *j) { |
| 497 |
_cleanup_free_ char *cursor = NULL; |
| 498 |
int r; |
| 499 |
|
| 500 |
assert(j); |
| 501 |
|
| 502 |
if (!arg_show_cursor && !arg_cursor_file) |
| 503 |
return 0; |
| 504 |
|
| 505 |
r = sd_journal_get_cursor(j, &cursor); |
| 506 |
if (r == -EADDRNOTAVAIL) |
| 507 |
return 0; |
| 508 |
if (r < 0) |
| 509 |
return log_error_errno(r, "Failed to get cursor: %m"); |
| 510 |
|
| 511 |
if (arg_show_cursor) |
| 512 |
printf("-- cursor: %s\n", cursor); |
| 513 |
|
| 514 |
if (arg_cursor_file) { |
| 515 |
r = write_string_file(arg_cursor_file, cursor, WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_ATOMIC); |
| 516 |
if (r < 0) |
| 517 |
return log_error_errno(r, "Failed to write new cursor to %s: %m", arg_cursor_file); |
| 518 |
} |
| 519 |
|
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
int action_show(char **matches) { |
| 524 |
_cleanup_(context_done) Context c = {}; |
| 525 |
int n_shown, r, poll_fd = -EBADF; |
| 526 |
|
| 527 |
assert(arg_action == ACTION_SHOW); |
| 528 |
|
| 529 |
(void) signal(SIGWINCH, columns_lines_cache_reset); |
| 530 |
|
| 531 |
r = acquire_journal(&c.journal); |
| 532 |
if (r < 0) |
| 533 |
return r; |
| 534 |
|
| 535 |
if (!journal_boot_has_effect(c.journal)) |
| 536 |
return arg_compiled_pattern ? -ENOENT : 0; |
| 537 |
|
| 538 |
r = add_filters(c.journal, matches); |
| 539 |
if (r < 0) |
| 540 |
return r; |
| 541 |
|
| 542 |
r = seek_journal(&c); |
| 543 |
if (r < 0) |
| 544 |
return r; |
| 545 |
|
| 546 |
/* Opening the fd now means the first sd_journal_wait() will actually wait */ |
| 547 |
if (arg_follow) { |
| 548 |
poll_fd = sd_journal_get_fd(c.journal); |
| 549 |
if (poll_fd == -EMFILE) { |
| 550 |
log_warning_errno(poll_fd, "Insufficient watch descriptors available. Reverting to -n."); |
| 551 |
arg_follow = false; |
| 552 |
} else if (poll_fd == -EMEDIUMTYPE) |
| 553 |
return log_error_errno(poll_fd, "The --follow switch is not supported in conjunction with reading from STDIN."); |
| 554 |
else if (poll_fd < 0) |
| 555 |
return log_error_errno(poll_fd, "Failed to get journal fd: %m"); |
| 556 |
} |
| 557 |
|
| 558 |
if (!arg_follow) |
| 559 |
pager_open(arg_pager_flags); |
| 560 |
|
| 561 |
if (!arg_quiet && (arg_lines != 0 || arg_follow) && DEBUG_LOGGING) { |
| 562 |
usec_t start, end; |
| 563 |
char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX]; |
| 564 |
|
| 565 |
r = sd_journal_get_cutoff_realtime_usec(c.journal, &start, &end); |
| 566 |
if (r < 0) |
| 567 |
return log_error_errno(r, "Failed to get cutoff: %m"); |
| 568 |
if (r > 0) { |
| 569 |
if (arg_follow) |
| 570 |
printf("-- Journal begins at %s. --\n", |
| 571 |
format_timestamp_maybe_utc(start_buf, sizeof(start_buf), start)); |
| 572 |
else |
| 573 |
printf("-- Journal begins at %s, ends at %s. --\n", |
| 574 |
format_timestamp_maybe_utc(start_buf, sizeof(start_buf), start), |
| 575 |
format_timestamp_maybe_utc(end_buf, sizeof(end_buf), end)); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (arg_follow) { |
| 580 |
assert(poll_fd >= 0); |
| 581 |
|
| 582 |
r = setup_event(&c, poll_fd); |
| 583 |
if (r < 0) |
| 584 |
return r; |
| 585 |
|
| 586 |
r = sd_event_loop(c.event); |
| 587 |
if (r < 0) |
| 588 |
return r; |
| 589 |
|
| 590 |
r = update_cursor(c.journal); |
| 591 |
if (r < 0) |
| 592 |
return r; |
| 593 |
|
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
/* Setup is done, we'll start processing data. */ |
| 598 |
(void) sd_notify(/* unset_environment= */ false, "READY=1"); |
| 599 |
|
| 600 |
r = show(&c); |
| 601 |
if (r < 0) |
| 602 |
return r; |
| 603 |
n_shown = r; |
| 604 |
|
| 605 |
if (n_shown == 0 && !arg_quiet) |
| 606 |
printf("-- No entries --\n"); |
| 607 |
|
| 608 |
r = update_cursor(c.journal); |
| 609 |
if (r < 0) |
| 610 |
return r; |
| 611 |
|
| 612 |
if (arg_compiled_pattern && n_shown == 0) |
| 613 |
/* --grep was used, no error was thrown, but the pattern didn't |
| 614 |
* match anything. Let's mimic grep's behavior here and return |
| 615 |
* a non-zero exit code, so journalctl --grep can be used |
| 616 |
* in scripts and such */ |
| 617 |
return -ENOENT; |
| 618 |
|
| 619 |
return 0; |
| 620 |
} |