]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/logs-show.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / shared / logs-show.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
86aa7ba4 2
86aa7ba4 3#include <errno.h>
b6741478 4#include <fcntl.h>
a8fbdf54
TA
5#include <signal.h>
6#include <stdint.h>
7#include <stdlib.h>
07630cea 8#include <sys/socket.h>
a8fbdf54 9#include <syslog.h>
a8fbdf54
TA
10#include <unistd.h>
11
12#include "sd-id128.h"
13#include "sd-journal.h"
86aa7ba4 14
b5efdb8a 15#include "alloc-util.h"
3ffd4af2 16#include "fd-util.h"
f97b34a6 17#include "format-util.h"
d99ae53a 18#include "hashmap.h"
07630cea 19#include "hostname-util.h"
b5ea030d 20#include "id128-util.h"
c004493c 21#include "io-util.h"
dfb33a97 22#include "journal-internal.h"
1a8f0ce6 23#include "journal-util.h"
8e044443 24#include "json.h"
2108b567 25#include "locale-util.h"
07630cea 26#include "log.h"
3ffd4af2 27#include "logs-show.h"
a8fbdf54 28#include "macro.h"
0cb8e3d1 29#include "namespace-util.h"
a8fbdf54 30#include "output-mode.h"
6bedfcbb 31#include "parse-util.h"
76f7cc3f 32#include "pretty-print.h"
b5ea030d 33#include "process-util.h"
a8fbdf54 34#include "sparse-endian.h"
29a753df 35#include "stdio-util.h"
8b43440b 36#include "string-table.h"
07630cea 37#include "string-util.h"
cc25a67e 38#include "strv.h"
288a74cc 39#include "terminal-util.h"
a8fbdf54 40#include "time-util.h"
07630cea
LP
41#include "utf8.h"
42#include "util.h"
2108b567 43#include "web-util.h"
86aa7ba4 44
07630cea 45/* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
a6f0104a
ZJS
46#define PRINT_LINE_THRESHOLD 3
47#define PRINT_CHAR_THRESHOLD 300
48
8e044443 49#define JSON_THRESHOLD 4096U
86aa7ba4 50
d4205751 51static int print_catalog(FILE *f, sd_journal *j) {
d4205751 52 _cleanup_free_ char *t = NULL, *z = NULL;
ee5c1311 53 const char *newline, *prefix;
2108b567
LP
54 int r;
55
56 assert(j);
d4205751 57
d4205751 58 r = sd_journal_get_catalog(j, &t);
2108b567
LP
59 if (r == -ENOENT)
60 return 0;
d4205751 61 if (r < 0)
2108b567 62 return log_error_errno(r, "Failed to find catalog entry: %m");
d4205751 63
ee5c1311
LP
64 if (is_locale_utf8())
65 prefix = strjoina(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), special_glyph(SPECIAL_GLYPH_LIGHT_SHADE));
66 else
67 prefix = "--";
68
69 if (colors_enabled())
70 newline = strjoina(ANSI_NORMAL "\n" ANSI_GREY, prefix, ANSI_NORMAL " " ANSI_GREEN);
71 else
72 newline = strjoina("\n", prefix, " ");
73
74 z = strreplace(strstrip(t), "\n", newline);
d4205751
LP
75 if (!z)
76 return log_oom();
77
ee5c1311
LP
78 if (colors_enabled())
79 fprintf(f, ANSI_GREY "%s" ANSI_NORMAL " " ANSI_GREEN, prefix);
80 else
81 fprintf(f, "%s ", prefix);
82
d4205751 83 fputs(z, f);
ee5c1311
LP
84
85 if (colors_enabled())
86 fputs(ANSI_NORMAL "\n", f);
87 else
88 fputc('\n', f);
d4205751 89
2108b567
LP
90 return 1;
91}
92
93static int url_from_catalog(sd_journal *j, char **ret) {
94 _cleanup_free_ char *t = NULL, *url = NULL;
95 const char *weblink;
96 int r;
97
98 assert(j);
99 assert(ret);
100
101 r = sd_journal_get_catalog(j, &t);
102 if (r == -ENOENT)
103 goto notfound;
104 if (r < 0)
105 return log_error_errno(r, "Failed to find catalog entry: %m");
106
107 weblink = startswith(t, "Documentation:");
108 if (!weblink) {
109 weblink = strstr(t + 1, "\nDocumentation:");
110 if (!weblink)
111 goto notfound;
112
113 weblink += 15;
114 }
115
116 /* Skip whitespace to value */
117 weblink += strspn(weblink, " \t");
118
119 /* Cut out till next whitespace/newline */
120 url = strndup(weblink, strcspn(weblink, WHITESPACE));
121 if (!url)
122 return log_oom();
123
124 if (!documentation_url_is_valid(url))
125 goto notfound;
126
127 *ret = TAKE_PTR(url);
128 return 1;
129
130notfound:
131 *ret = NULL;
d4205751
LP
132 return 0;
133}
134
ed054520
VC
135static int parse_field(const void *data, size_t length, const char *field, size_t field_len, char **target, size_t *target_len) {
136 size_t nl;
d813d7a3 137 char *buf;
55d7bfc1
LP
138
139 assert(data);
140 assert(field);
141 assert(target);
55d7bfc1 142
ed054520 143 if (length < field_len)
55d7bfc1
LP
144 return 0;
145
ed054520 146 if (memcmp(data, field, field_len))
55d7bfc1
LP
147 return 0;
148
ed054520 149 nl = length - field_len;
c165d97d 150
ed054520 151 buf = newdup_suffix0(char, (const char*) data + field_len, nl);
0d0f0c50
SL
152 if (!buf)
153 return log_oom();
55d7bfc1 154
6c1e6b98 155 free(*target);
55d7bfc1 156 *target = buf;
d813d7a3 157
ed054520
VC
158 if (target_len)
159 *target_len = nl;
55d7bfc1
LP
160
161 return 1;
162}
163
ed054520
VC
164typedef struct ParseFieldVec {
165 const char *field;
166 size_t field_len;
167 char **target;
168 size_t *target_len;
169} ParseFieldVec;
170
e3eec1fd
LP
171#define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) { \
172 .field = _field, \
173 .field_len = strlen(_field), \
174 .target = _target, \
175 .target_len = _target_len \
176 }
ed054520
VC
177
178static int parse_fieldv(const void *data, size_t length, const ParseFieldVec *fields, unsigned n_fields) {
179 unsigned i;
180
181 for (i = 0; i < n_fields; i++) {
182 const ParseFieldVec *f = &fields[i];
183 int r;
184
185 r = parse_field(data, length, f->field, f->field_len, f->target, f->target_len);
186 if (r < 0)
187 return r;
188 else if (r > 0)
189 break;
190 }
191
192 return 0;
193}
194
2f063186
ZJS
195static int field_set_test(const Set *fields, const char *name, size_t n) {
196 char *s;
cc25a67e
LK
197
198 if (!fields)
199 return 1;
200
201 s = strndupa(name, n);
54ff74d2 202 return set_contains(fields, s);
cc25a67e
LK
203}
204
08ace05b
LP
205static bool shall_print(const char *p, size_t l, OutputFlags flags) {
206 assert(p);
207
208 if (flags & OUTPUT_SHOW_ALL)
55d7bfc1
LP
209 return true;
210
a6f0104a 211 if (l >= PRINT_CHAR_THRESHOLD)
55d7bfc1
LP
212 return false;
213
31f7bf19 214 if (!utf8_is_printable(p, l))
55d7bfc1
LP
215 return false;
216
217 return true;
218}
219
b4766d5f
ZJS
220static bool print_multiline(
221 FILE *f,
222 unsigned prefix,
223 unsigned n_columns,
224 OutputFlags flags,
225 int priority,
194da5ca 226 bool audit,
b4766d5f
ZJS
227 const char* message,
228 size_t message_len,
05c7d9bf 229 size_t highlight[2]) {
b4766d5f
ZJS
230
231 const char *color_on = "", *color_off = "", *highlight_on = "";
31f7bf19 232 const char *pos, *end;
94e0bd7d 233 bool ellipsized = false;
a6f0104a 234 int line = 0;
31f7bf19 235
194da5ca 236 if (flags & OUTPUT_COLOR) {
37b8d2f6 237 get_log_colors(priority, &color_on, &color_off, &highlight_on);
31f7bf19 238
194da5ca
ZJS
239 if (audit && strempty(color_on)) {
240 color_on = ANSI_BLUE;
241 color_off = ANSI_NORMAL;
242 }
243 }
244
47d80904
UU
245 /* A special case: make sure that we print a newline when
246 the message is empty. */
247 if (message_len == 0)
248 fputs("\n", f);
249
a6f0104a
ZJS
250 for (pos = message;
251 pos < message + message_len;
252 pos = end + 1, line++) {
253 bool continuation = line > 0;
254 bool tail_line;
31f7bf19
ZJS
255 int len;
256 for (end = pos; end < message + message_len && *end != '\n'; end++)
257 ;
258 len = end - pos;
259 assert(len >= 0);
260
2526d626 261 /* We need to figure out when we are showing not-last line, *and*
a6f0104a
ZJS
262 * will skip subsequent lines. In that case, we will put the dots
263 * at the end of the line, instead of putting dots in the middle
264 * or not at all.
265 */
266 tail_line =
267 line + 1 == PRINT_LINE_THRESHOLD ||
2526d626 268 end + 1 >= message + PRINT_CHAR_THRESHOLD;
a6f0104a
ZJS
269
270 if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) ||
271 (prefix + len + 1 < n_columns && !tail_line)) {
b4766d5f
ZJS
272 if (highlight &&
273 (size_t) (pos - message) <= highlight[0] &&
274 highlight[0] < (size_t) len) {
275
276 fprintf(f, "%*s%s%.*s",
277 continuation * prefix, "",
278 color_on, (int) highlight[0], pos);
279 fprintf(f, "%s%.*s",
280 highlight_on,
281 (int) (MIN((size_t) len, highlight[1]) - highlight[0]),
282 pos + highlight[0]);
283 if ((size_t) len > highlight[1])
284 fprintf(f, "%s%.*s",
285 color_on,
286 (int) (len - highlight[1]),
287 pos + highlight[1]);
288 fprintf(f, "%s\n", color_off);
289
290 } else
291 fprintf(f, "%*s%s%.*s%s\n",
292 continuation * prefix, "",
293 color_on, len, pos, color_off);
a6f0104a
ZJS
294 continue;
295 }
31f7bf19 296
a6f0104a
ZJS
297 /* Beyond this point, ellipsization will happen. */
298 ellipsized = true;
31f7bf19 299
a6f0104a
ZJS
300 if (prefix < n_columns && n_columns - prefix >= 3) {
301 if (n_columns - prefix > (unsigned) len + 3)
302 fprintf(f, "%*s%s%.*s...%s\n",
b4b02cbe
ZJS
303 continuation * prefix, "",
304 color_on, len, pos, color_off);
a6f0104a
ZJS
305 else {
306 _cleanup_free_ char *e;
307
308 e = ellipsize_mem(pos, len, n_columns - prefix,
309 tail_line ? 100 : 90);
310 if (!e)
311 fprintf(f, "%*s%s%.*s%s\n",
312 continuation * prefix, "",
313 color_on, len, pos, color_off);
314 else
315 fprintf(f, "%*s%s%s%s\n",
316 continuation * prefix, "",
317 color_on, e, color_off);
318 }
319 } else
31f7bf19
ZJS
320 fputs("...\n", f);
321
a6f0104a
ZJS
322 if (tail_line)
323 break;
31f7bf19 324 }
94e0bd7d
ZJS
325
326 return ellipsized;
31f7bf19
ZJS
327}
328
29a753df
LP
329static int output_timestamp_monotonic(FILE *f, sd_journal *j, const char *monotonic) {
330 sd_id128_t boot_id;
331 uint64_t t;
332 int r;
333
334 assert(f);
335 assert(j);
336
337 r = -ENXIO;
338 if (monotonic)
339 r = safe_atou64(monotonic, &t);
340 if (r < 0)
341 r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
342 if (r < 0)
343 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
344
70887c5f 345 fprintf(f, "[%5"PRI_USEC".%06"PRI_USEC"]", t / USEC_PER_SEC, t % USEC_PER_SEC);
29a753df
LP
346 return 1 + 5 + 1 + 6 + 1;
347}
348
349static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, OutputFlags flags, const char *realtime) {
67bd5620 350 char buf[MAX(FORMAT_TIMESTAMP_MAX, 64U)];
29a753df
LP
351 struct tm *(*gettime_r)(const time_t *, struct tm *);
352 struct tm tm;
353 uint64_t x;
354 time_t t;
355 int r;
356
357 assert(f);
358 assert(j);
359
29a753df
LP
360 if (realtime)
361 r = safe_atou64(realtime, &x);
03d1319b 362 if (!realtime || r < 0 || !VALID_REALTIME(x))
29a753df
LP
363 r = sd_journal_get_realtime_usec(j, &x);
364 if (r < 0)
365 return log_error_errno(r, "Failed to get realtime timestamp: %m");
366
49805b3d 367 if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) {
29a753df
LP
368 const char *k;
369
370 if (flags & OUTPUT_UTC)
7b3eb5c9 371 k = format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC);
29a753df
LP
372 else
373 k = format_timestamp(buf, sizeof(buf), x);
baaa35ad
ZJS
374 if (!k)
375 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
376 "Failed to format timestamp: %" PRIu64, x);
29a753df
LP
377
378 } else {
7e563bfc
IW
379 char usec[7];
380
29a753df
LP
381 gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r;
382 t = (time_t) (x / USEC_PER_SEC);
383
384 switch (mode) {
385
386 case OUTPUT_SHORT_UNIX:
70887c5f 387 xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, x % USEC_PER_SEC);
29a753df
LP
388 break;
389
390 case OUTPUT_SHORT_ISO:
baaa35ad
ZJS
391 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm)) <= 0)
392 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
393 "Failed to format ISO time");
7e563bfc
IW
394 break;
395
396 case OUTPUT_SHORT_ISO_PRECISE:
397 /* No usec in strftime, so we leave space and copy over */
baaa35ad
ZJS
398 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t, &tm)) <= 0)
399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
400 "Failed to format ISO-precise time");
7e563bfc
IW
401 xsprintf(usec, "%06"PRI_USEC, x % USEC_PER_SEC);
402 memcpy(buf + 20, usec, 6);
29a753df
LP
403 break;
404
405 case OUTPUT_SHORT:
406 case OUTPUT_SHORT_PRECISE:
407
baaa35ad
ZJS
408 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm)) <= 0)
409 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
410 "Failed to format syslog time");
29a753df
LP
411
412 if (mode == OUTPUT_SHORT_PRECISE) {
413 size_t k;
414
415 assert(sizeof(buf) > strlen(buf));
416 k = sizeof(buf) - strlen(buf);
417
70887c5f 418 r = snprintf(buf + strlen(buf), k, ".%06"PRIu64, x % USEC_PER_SEC);
baaa35ad
ZJS
419 if (r <= 0 || (size_t) r >= k) /* too long? */
420 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
421 "Failed to format precise time");
29a753df
LP
422 }
423 break;
424
425 default:
426 assert_not_reached("Unknown time format");
427 }
428 }
429
430 fputs(buf, f);
431 return (int) strlen(buf);
432}
433
08ace05b
LP
434static int output_short(
435 FILE *f,
436 sd_journal *j,
437 OutputMode mode,
438 unsigned n_columns,
cc25a67e 439 OutputFlags flags,
2f063186 440 const Set *output_fields,
05c7d9bf 441 const size_t highlight[2]) {
08ace05b 442
86aa7ba4 443 int r;
86aa7ba4 444 const void *data;
2108b567
LP
445 size_t length, n = 0;
446 _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL,
447 *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL, *transport = NULL,
448 *config_file = NULL, *unit = NULL, *user_unit = NULL, *documentation_url = NULL;
449 size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0,
450 realtime_len = 0, monotonic_len = 0, priority_len = 0, transport_len = 0, config_file_len = 0,
451 unit_len = 0, user_unit_len = 0, documentation_url_len = 0;
49826187 452 int p = LOG_INFO;
194da5ca 453 bool ellipsized = false, audit;
ed054520
VC
454 const ParseFieldVec fields[] = {
455 PARSE_FIELD_VEC_ENTRY("_PID=", &pid, &pid_len),
456 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm, &comm_len),
457 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message, &message_len),
458 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority, &priority_len),
194da5ca 459 PARSE_FIELD_VEC_ENTRY("_TRANSPORT=", &transport, &transport_len),
ed054520
VC
460 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname, &hostname_len),
461 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid, &fake_pid_len),
462 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len),
463 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len),
464 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len),
76f7cc3f 465 PARSE_FIELD_VEC_ENTRY("CONFIG_FILE=", &config_file, &config_file_len),
49805b3d
LB
466 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit, &unit_len),
467 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit, &user_unit_len),
2108b567 468 PARSE_FIELD_VEC_ENTRY("DOCUMENTATION=", &documentation_url, &documentation_url_len),
ed054520 469 };
b4766d5f 470 size_t highlight_shifted[] = {highlight ? highlight[0] : 0, highlight ? highlight[1] : 0};
86aa7ba4 471
08ace05b 472 assert(f);
86aa7ba4
LP
473 assert(j);
474
a6f0104a 475 /* Set the threshold to one bigger than the actual print
69ab8088 476 * threshold, so that if the line is actually longer than what
a6f0104a
ZJS
477 * we're willing to print, ellipsization will occur. This way
478 * we won't output a misleading line without any indication of
479 * truncation.
480 */
481 sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
93b73b06 482
a72b6353 483 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
ed054520 484 r = parse_fieldv(data, length, fields, ELEMENTSOF(fields));
55d7bfc1 485 if (r < 0)
08ace05b 486 return r;
55d7bfc1 487 }
d00f1d57
LP
488 if (r == -EBADMSG) {
489 log_debug_errno(r, "Skipping message we can't read: %m");
490 return 0;
491 }
a72b6353 492 if (r < 0)
b56d608e 493 return log_error_errno(r, "Failed to get journal fields: %m");
a72b6353 494
07d21025
LP
495 if (!message) {
496 log_debug("Skipping message without MESSAGE= field.");
08ace05b 497 return 0;
07d21025 498 }
55d7bfc1 499
e8bc0ea2 500 if (!(flags & OUTPUT_SHOW_ALL))
b4766d5f 501 strip_tab_ansi(&message, &message_len, highlight_shifted);
e8bc0ea2 502
49826187
LP
503 if (priority_len == 1 && *priority >= '0' && *priority <= '7')
504 p = *priority - '0';
505
194da5ca
ZJS
506 audit = streq_ptr(transport, "audit");
507
29a753df
LP
508 if (mode == OUTPUT_SHORT_MONOTONIC)
509 r = output_timestamp_monotonic(f, j, monotonic);
510 else
511 r = output_timestamp_realtime(f, j, mode, flags, realtime);
512 if (r < 0)
513 return r;
514 n += r;
86aa7ba4 515
29a753df 516 if (flags & OUTPUT_NO_HOSTNAME) {
991e274b 517 /* Suppress display of the hostname if this is requested. */
12104159 518 hostname = mfree(hostname);
991e274b
LP
519 hostname_len = 0;
520 }
521
08ace05b
LP
522 if (hostname && shall_print(hostname, hostname_len, flags)) {
523 fprintf(f, " %.*s", (int) hostname_len, hostname);
55d7bfc1
LP
524 n += hostname_len + 1;
525 }
526
76f7cc3f
ZJS
527 if (mode == OUTPUT_WITH_UNIT && ((unit && shall_print(unit, unit_len, flags)) ||
528 (user_unit && shall_print(user_unit, user_unit_len, flags)))) {
49805b3d
LB
529 if (unit) {
530 fprintf(f, " %.*s", (int) unit_len, unit);
531 n += unit_len + 1;
532 }
533 if (user_unit) {
534 if (unit)
535 fprintf(f, "/%.*s", (int) user_unit_len, user_unit);
536 else
537 fprintf(f, " %.*s", (int) user_unit_len, user_unit);
538 n += unit_len + 1;
539 }
540 } else if (identifier && shall_print(identifier, identifier_len, flags)) {
08ace05b 541 fprintf(f, " %.*s", (int) identifier_len, identifier);
4cd9a9d9 542 n += identifier_len + 1;
08ace05b
LP
543 } else if (comm && shall_print(comm, comm_len, flags)) {
544 fprintf(f, " %.*s", (int) comm_len, comm);
55d7bfc1 545 n += comm_len + 1;
b5936820 546 } else
1248e840 547 fputs(" unknown", f);
86aa7ba4 548
08ace05b
LP
549 if (pid && shall_print(pid, pid_len, flags)) {
550 fprintf(f, "[%.*s]", (int) pid_len, pid);
55d7bfc1 551 n += pid_len + 2;
08ace05b
LP
552 } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) {
553 fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid);
6c1e6b98 554 n += fake_pid_len + 2;
86aa7ba4
LP
555 }
556
2108b567
LP
557 fputs(": ", f);
558
559 if (urlify_enabled()) {
560 _cleanup_free_ char *c = NULL;
561
562 /* Insert a hyperlink to a documentation URL before the message. Note that we don't make the
563 * whole message a hyperlink, since otherwise the whole screen might end up being just
564 * hyperlinks. Moreover, we want to be able to highlight parts of the message (such as the
565 * config file, see below) hence let's keep the documentation URL link separate. */
566
567 if (documentation_url && shall_print(documentation_url, documentation_url_len, flags)) {
568 c = strndup(documentation_url, documentation_url_len);
569 if (!c)
570 return log_oom();
571
572 if (!documentation_url_is_valid(c)) /* Eat up invalid links */
573 c = mfree(c);
574 }
575
576 if (!c)
577 (void) url_from_catalog(j, &c); /* Acquire from catalog if not embedded in log message itself */
578
579 if (c) {
580 _cleanup_free_ char *urlified = NULL;
581
582 if (terminal_urlify(c, special_glyph(SPECIAL_GLYPH_EXTERNAL_LINK), &urlified) >= 0) {
583 fputs(urlified, f);
584 fputc(' ', f);
585 }
586 }
587 }
588
31f7bf19 589 if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
e6acda19 590 char bytes[FORMAT_BYTES_MAX];
2108b567 591 fprintf(f, "[%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
31f7bf19 592 } else {
76f7cc3f
ZJS
593
594 /* URLify config_file string in message, if the message starts with it.
595 * Skip URLification if the highlighted pattern overlaps. */
596 if (config_file &&
597 message_len >= config_file_len &&
598 memcmp(message, config_file, config_file_len) == 0 &&
85fbebe6 599 (message_len == config_file_len || IN_SET(message[config_file_len], ':', ' ')) &&
76f7cc3f
ZJS
600 (!highlight || highlight_shifted[0] == 0 || highlight_shifted[0] > config_file_len)) {
601
602 _cleanup_free_ char *t = NULL, *urlified = NULL;
603
604 t = strndup(config_file, config_file_len);
605 if (t && terminal_urlify_path(t, NULL, &urlified) >= 0) {
85fbebe6
ZJS
606 size_t urlified_len = strlen(urlified);
607 size_t shift = urlified_len - config_file_len;
76f7cc3f
ZJS
608 char *joined;
609
85fbebe6 610 joined = realloc(urlified, message_len + shift);
76f7cc3f 611 if (joined) {
85fbebe6 612 memcpy(joined + urlified_len, message + config_file_len, message_len - config_file_len);
76f7cc3f 613 free_and_replace(message, joined);
85fbebe6 614 TAKE_PTR(urlified);
76f7cc3f
ZJS
615 message_len += shift;
616 if (highlight) {
617 highlight_shifted[0] += shift;
618 highlight_shifted[1] += shift;
619 }
620 }
621 }
622 }
623
94e0bd7d 624 ellipsized |=
194da5ca 625 print_multiline(f, n + 2, n_columns, flags, p, audit,
b4766d5f
ZJS
626 message, message_len,
627 highlight_shifted);
31f7bf19 628 }
55d7bfc1 629
d4205751 630 if (flags & OUTPUT_CATALOG)
2108b567 631 (void) print_catalog(f, j);
d4205751 632
94e0bd7d 633 return ellipsized;
86aa7ba4
LP
634}
635
08ace05b
LP
636static int output_verbose(
637 FILE *f,
638 sd_journal *j,
639 OutputMode mode,
640 unsigned n_columns,
cc25a67e 641 OutputFlags flags,
2f063186 642 const Set *output_fields,
05c7d9bf 643 const size_t highlight[2]) {
08ace05b 644
86aa7ba4
LP
645 const void *data;
646 size_t length;
7fd1b19b 647 _cleanup_free_ char *cursor = NULL;
d813d7a3 648 uint64_t realtime = 0;
f02d8367 649 char ts[FORMAT_TIMESTAMP_MAX + 7];
8924973a 650 const char *timestamp;
86aa7ba4
LP
651 int r;
652
08ace05b 653 assert(f);
86aa7ba4
LP
654 assert(j);
655
93b73b06
LP
656 sd_journal_set_data_threshold(j, 0);
657
cf40f0be
ZJS
658 r = sd_journal_get_data(j, "_SOURCE_REALTIME_TIMESTAMP", &data, &length);
659 if (r == -ENOENT)
660 log_debug("Source realtime timestamp not found");
b56d608e
LP
661 else if (r < 0)
662 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get source realtime timestamp: %m");
663 else {
cf40f0be 664 _cleanup_free_ char *value = NULL;
cf40f0be 665
fbd0b64f
LP
666 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=",
667 STRLEN("_SOURCE_REALTIME_TIMESTAMP="), &value,
668 NULL);
cf40f0be 669 if (r < 0)
e64c53fd 670 return r;
d813d7a3
LP
671 assert(r > 0);
672
673 r = safe_atou64(value, &realtime);
674 if (r < 0)
675 log_debug_errno(r, "Failed to parse realtime timestamp: %m");
cf40f0be
ZJS
676 }
677
678 if (r < 0) {
679 r = sd_journal_get_realtime_usec(j, &realtime);
b56d608e
LP
680 if (r < 0)
681 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get realtime timestamp: %m");
86aa7ba4
LP
682 }
683
684 r = sd_journal_get_cursor(j, &cursor);
f647962d
MS
685 if (r < 0)
686 return log_error_errno(r, "Failed to get cursor: %m");
86aa7ba4 687
7b3eb5c9
LB
688 timestamp = format_timestamp_style(ts, sizeof ts, realtime,
689 flags & OUTPUT_UTC ? TIMESTAMP_US_UTC : TIMESTAMP_US);
08ace05b 690 fprintf(f, "%s [%s]\n",
8924973a 691 timestamp ?: "(no timestamp)",
08ace05b 692 cursor);
86aa7ba4 693
a72b6353 694 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
79dc477f 695 const char *c, *p;
31f7bf19 696 int fieldlen;
7ac4fa7e 697 const char *on = "", *off = "";
79dc477f
ZJS
698 _cleanup_free_ char *urlified = NULL;
699 size_t valuelen;
7ac4fa7e 700
31f7bf19 701 c = memchr(data, '=', length);
baaa35ad
ZJS
702 if (!c)
703 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
704 "Invalid field.");
31f7bf19 705 fieldlen = c - (const char*) data;
86aa7ba4 706
cc25a67e
LK
707 r = field_set_test(output_fields, data, fieldlen);
708 if (r < 0)
709 return r;
79dc477f 710 if (r == 0)
cc25a67e
LK
711 continue;
712
79dc477f
ZJS
713 valuelen = length - 1 - fieldlen;
714
715 if ((flags & OUTPUT_COLOR) && (p = startswith(data, "MESSAGE="))) {
1fc464f6
LP
716 on = ANSI_HIGHLIGHT;
717 off = ANSI_NORMAL;
79dc477f
ZJS
718 } else if ((p = startswith(data, "CONFIG_FILE="))) {
719 if (terminal_urlify_path(p, NULL, &urlified) >= 0) {
720 p = urlified;
721 valuelen = strlen(urlified);
722 }
723 } else
724 p = c + 1;
7ac4fa7e 725
8980058a 726 if ((flags & OUTPUT_SHOW_ALL) ||
a6f0104a
ZJS
727 (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH)
728 && utf8_is_printable(data, length))) {
7ac4fa7e 729 fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data);
194da5ca 730 print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, false,
79dc477f 731 p, valuelen,
194da5ca 732 NULL);
7ac4fa7e 733 fputs(off, f);
31f7bf19
ZJS
734 } else {
735 char bytes[FORMAT_BYTES_MAX];
86aa7ba4 736
7ac4fa7e
ZJS
737 fprintf(f, " %s%.*s=[%s blob data]%s\n",
738 on,
31f7bf19
ZJS
739 (int) (c - (const char*) data),
740 (const char*) data,
7ac4fa7e
ZJS
741 format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
742 off);
31f7bf19 743 }
86aa7ba4
LP
744 }
745
a72b6353
ZJS
746 if (r < 0)
747 return r;
748
d4205751 749 if (flags & OUTPUT_CATALOG)
2108b567 750 (void) print_catalog(f, j);
d4205751 751
86aa7ba4
LP
752 return 0;
753}
754
08ace05b
LP
755static int output_export(
756 FILE *f,
757 sd_journal *j,
758 OutputMode mode,
759 unsigned n_columns,
cc25a67e 760 OutputFlags flags,
2f063186 761 const Set *output_fields,
05c7d9bf 762 const size_t highlight[2]) {
08ace05b 763
86aa7ba4 764 sd_id128_t boot_id;
5905d7cf 765 char sid[SD_ID128_STRING_MAX];
86aa7ba4
LP
766 int r;
767 usec_t realtime, monotonic;
7fd1b19b 768 _cleanup_free_ char *cursor = NULL;
86aa7ba4
LP
769 const void *data;
770 size_t length;
771
772 assert(j);
773
93b73b06
LP
774 sd_journal_set_data_threshold(j, 0);
775
86aa7ba4 776 r = sd_journal_get_realtime_usec(j, &realtime);
f647962d
MS
777 if (r < 0)
778 return log_error_errno(r, "Failed to get realtime timestamp: %m");
86aa7ba4
LP
779
780 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
f647962d
MS
781 if (r < 0)
782 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
86aa7ba4
LP
783
784 r = sd_journal_get_cursor(j, &cursor);
f647962d
MS
785 if (r < 0)
786 return log_error_errno(r, "Failed to get cursor: %m");
86aa7ba4 787
08ace05b
LP
788 fprintf(f,
789 "__CURSOR=%s\n"
de0671ee
ZJS
790 "__REALTIME_TIMESTAMP="USEC_FMT"\n"
791 "__MONOTONIC_TIMESTAMP="USEC_FMT"\n"
08ace05b
LP
792 "_BOOT_ID=%s\n",
793 cursor,
de0671ee
ZJS
794 realtime,
795 monotonic,
08ace05b 796 sd_id128_to_string(boot_id, sid));
86aa7ba4 797
a72b6353 798 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
cc25a67e 799 const char *c;
86aa7ba4 800
0ab896b3
ZJS
801 /* We already printed the boot id from the data in the header, hence let's suppress it here */
802 if (memory_startswith(data, length, "_BOOT_ID="))
112301ae
LP
803 continue;
804
cc25a67e 805 c = memchr(data, '=', length);
baaa35ad
ZJS
806 if (!c)
807 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
808 "Invalid field.");
cc25a67e
LK
809
810 r = field_set_test(output_fields, data, c - (const char *) data);
811 if (r < 0)
812 return r;
813 if (!r)
814 continue;
815
0ade5ffe
ZJS
816 if (utf8_is_printable_newline(data, length, false))
817 fwrite(data, length, 1, f);
818 else {
86aa7ba4
LP
819 uint64_t le64;
820
08ace05b
LP
821 fwrite(data, c - (const char*) data, 1, f);
822 fputc('\n', f);
86aa7ba4 823 le64 = htole64(length - (c - (const char*) data) - 1);
08ace05b
LP
824 fwrite(&le64, sizeof(le64), 1, f);
825 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f);
0ade5ffe 826 }
86aa7ba4 827
08ace05b 828 fputc('\n', f);
86aa7ba4 829 }
4f5e1723
AW
830 if (r == -EBADMSG) {
831 log_debug_errno(r, "Skipping message we can't read: %m");
832 return 0;
833 }
86aa7ba4 834
a72b6353
ZJS
835 if (r < 0)
836 return r;
837
08ace05b 838 fputc('\n', f);
86aa7ba4
LP
839
840 return 0;
841}
842
240a5fe8 843void json_escape(
08ace05b
LP
844 FILE *f,
845 const char* p,
846 size_t l,
847 OutputFlags flags) {
848
849 assert(f);
850 assert(p);
851
93b73b06 852 if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
08ace05b
LP
853 fputs("null", f);
854
8980058a 855 else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) {
86aa7ba4
LP
856 bool not_first = false;
857
08ace05b 858 fputs("[ ", f);
86aa7ba4
LP
859
860 while (l > 0) {
861 if (not_first)
08ace05b 862 fprintf(f, ", %u", (uint8_t) *p);
86aa7ba4
LP
863 else {
864 not_first = true;
08ace05b 865 fprintf(f, "%u", (uint8_t) *p);
86aa7ba4
LP
866 }
867
868 p++;
869 l--;
870 }
871
08ace05b 872 fputs(" ]", f);
86aa7ba4 873 } else {
e768a4f0 874 fputc('"', f);
86aa7ba4
LP
875
876 while (l > 0) {
4c701096 877 if (IN_SET(*p, '"', '\\')) {
08ace05b
LP
878 fputc('\\', f);
879 fputc(*p, f);
31f7bf19
ZJS
880 } else if (*p == '\n')
881 fputs("\\n", f);
91a8a108
DM
882 else if ((uint8_t) *p < ' ')
883 fprintf(f, "\\u%04x", (uint8_t) *p);
08ace05b
LP
884 else
885 fputc(*p, f);
86aa7ba4
LP
886
887 p++;
888 l--;
889 }
890
e768a4f0 891 fputc('"', f);
86aa7ba4
LP
892 }
893}
894
8e044443
LP
895struct json_data {
896 JsonVariant* name;
897 size_t n_values;
898 JsonVariant* values[];
899};
900
901static int update_json_data(
902 Hashmap *h,
903 OutputFlags flags,
904 const char *name,
905 const void *value,
906 size_t size) {
907
908 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
909 struct json_data *d;
910 int r;
911
912 if (!(flags & OUTPUT_SHOW_ALL) && strlen(name) + 1 + size >= JSON_THRESHOLD)
913 r = json_variant_new_null(&v);
914 else if (utf8_is_printable(value, size))
915 r = json_variant_new_stringn(&v, value, size);
916 else
917 r = json_variant_new_array_bytes(&v, value, size);
918 if (r < 0)
919 return log_error_errno(r, "Failed to allocate JSON data: %m");
920
921 d = hashmap_get(h, name);
922 if (d) {
923 struct json_data *w;
924
925 w = realloc(d, offsetof(struct json_data, values) + sizeof(JsonVariant*) * (d->n_values + 1));
926 if (!w)
927 return log_oom();
928
929 d = w;
930 assert_se(hashmap_update(h, json_variant_string(d->name), d) >= 0);
931 } else {
932 _cleanup_(json_variant_unrefp) JsonVariant *n = NULL;
933
934 r = json_variant_new_string(&n, name);
935 if (r < 0)
936 return log_error_errno(r, "Failed to allocate JSON name variant: %m");
937
938 d = malloc0(offsetof(struct json_data, values) + sizeof(JsonVariant*));
939 if (!d)
940 return log_oom();
941
942 r = hashmap_put(h, json_variant_string(n), d);
943 if (r < 0) {
944 free(d);
945 return log_error_errno(r, "Failed to insert JSON name into hashmap: %m");
946 }
947
948 d->name = TAKE_PTR(n);
949 }
950
951 d->values[d->n_values++] = TAKE_PTR(v);
952 return 0;
953}
954
955static int update_json_data_split(
956 Hashmap *h,
957 OutputFlags flags,
2f063186 958 const Set *output_fields,
8e044443
LP
959 const void *data,
960 size_t size) {
961
962 const char *eq;
963 char *name;
964
965 assert(h);
966 assert(data || size == 0);
967
968 if (memory_startswith(data, size, "_BOOT_ID="))
969 return 0;
970
971 eq = memchr(data, '=', MIN(size, JSON_THRESHOLD));
972 if (!eq)
973 return 0;
974
975 if (eq == data)
976 return 0;
977
978 name = strndupa(data, eq - (const char*) data);
2f063186 979 if (output_fields && !set_contains(output_fields, name))
8e044443
LP
980 return 0;
981
982 return update_json_data(h, flags, name, eq + 1, size - (eq - (const char*) data) - 1);
983}
984
08ace05b
LP
985static int output_json(
986 FILE *f,
987 sd_journal *j,
988 OutputMode mode,
989 unsigned n_columns,
cc25a67e 990 OutputFlags flags,
2f063186 991 const Set *output_fields,
05c7d9bf 992 const size_t highlight[2]) {
08ace05b 993
8e044443
LP
994 char sid[SD_ID128_STRING_MAX], usecbuf[DECIMAL_STR_MAX(usec_t)];
995 _cleanup_(json_variant_unrefp) JsonVariant *object = NULL;
7fd1b19b 996 _cleanup_free_ char *cursor = NULL;
8e044443
LP
997 uint64_t realtime, monotonic;
998 JsonVariant **array = NULL;
999 struct json_data *d;
86aa7ba4 1000 sd_id128_t boot_id;
d99ae53a 1001 Hashmap *h = NULL;
8e044443 1002 size_t n = 0;
8e044443 1003 int r;
86aa7ba4
LP
1004
1005 assert(j);
1006
8e044443 1007 (void) sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
93b73b06 1008
86aa7ba4 1009 r = sd_journal_get_realtime_usec(j, &realtime);
f647962d
MS
1010 if (r < 0)
1011 return log_error_errno(r, "Failed to get realtime timestamp: %m");
86aa7ba4
LP
1012
1013 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
f647962d
MS
1014 if (r < 0)
1015 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
86aa7ba4
LP
1016
1017 r = sd_journal_get_cursor(j, &cursor);
f647962d
MS
1018 if (r < 0)
1019 return log_error_errno(r, "Failed to get cursor: %m");
86aa7ba4 1020
d5099efc 1021 h = hashmap_new(&string_hash_ops);
d99ae53a 1022 if (!h)
b56d608e 1023 return log_oom();
d99ae53a 1024
8e044443 1025 r = update_json_data(h, flags, "__CURSOR", cursor, strlen(cursor));
a72b6353 1026 if (r < 0)
8e044443 1027 goto finish;
d99ae53a 1028
8e044443
LP
1029 xsprintf(usecbuf, USEC_FMT, realtime);
1030 r = update_json_data(h, flags, "__REALTIME_TIMESTAMP", usecbuf, strlen(usecbuf));
1031 if (r < 0)
1032 goto finish;
d99ae53a 1033
8e044443
LP
1034 xsprintf(usecbuf, USEC_FMT, monotonic);
1035 r = update_json_data(h, flags, "__MONOTONIC_TIMESTAMP", usecbuf, strlen(usecbuf));
1036 if (r < 0)
1037 goto finish;
d99ae53a 1038
8e044443
LP
1039 sd_id128_to_string(boot_id, sid);
1040 r = update_json_data(h, flags, "_BOOT_ID", sid, strlen(sid));
1041 if (r < 0)
1042 goto finish;
d99ae53a 1043
8e044443
LP
1044 for (;;) {
1045 const void *data;
1046 size_t size;
d99ae53a 1047
8e044443
LP
1048 r = sd_journal_enumerate_data(j, &data, &size);
1049 if (r == -EBADMSG) {
1050 log_debug_errno(r, "Skipping message we can't read: %m");
1051 r = 0;
1052 goto finish;
1053 }
1054 if (r < 0) {
1055 log_error_errno(r, "Failed to read journal: %m");
1056 goto finish;
1057 }
1058 if (r == 0)
1059 break;
d99ae53a 1060
8e044443
LP
1061 r = update_json_data_split(h, flags, output_fields, data, size);
1062 if (r < 0)
1063 goto finish;
1064 }
d99ae53a 1065
8e044443
LP
1066 array = new(JsonVariant*, hashmap_size(h)*2);
1067 if (!array) {
1068 r = log_oom();
1069 goto finish;
1070 }
d99ae53a 1071
90e74a66 1072 HASHMAP_FOREACH(d, h) {
8e044443 1073 assert(d->n_values > 0);
d99ae53a 1074
8e044443 1075 array[n++] = json_variant_ref(d->name);
d99ae53a 1076
8e044443
LP
1077 if (d->n_values == 1)
1078 array[n++] = json_variant_ref(d->values[0]);
1079 else {
1080 _cleanup_(json_variant_unrefp) JsonVariant *q = NULL;
d99ae53a 1081
8e044443
LP
1082 r = json_variant_new_array(&q, d->values, d->n_values);
1083 if (r < 0) {
1084 log_error_errno(r, "Failed to create JSON array: %m");
1085 goto finish;
d99ae53a 1086 }
8e044443
LP
1087
1088 array[n++] = TAKE_PTR(q);
d99ae53a 1089 }
8e044443 1090 }
d99ae53a 1091
8e044443
LP
1092 r = json_variant_new_object(&object, array, n);
1093 if (r < 0) {
1094 log_error_errno(r, "Failed to allocate JSON object: %m");
1095 goto finish;
1096 }
d99ae53a 1097
8e044443 1098 json_variant_dump(object,
9e964bb8 1099 output_mode_to_json_format_flags(mode) |
8e044443
LP
1100 (FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0),
1101 f, NULL);
86aa7ba4 1102
d99ae53a
LP
1103 r = 0;
1104
1105finish:
8e044443
LP
1106 while ((d = hashmap_steal_first(h))) {
1107 size_t k;
1108
1109 json_variant_unref(d->name);
1110 for (k = 0; k < d->n_values; k++)
1111 json_variant_unref(d->values[k]);
1112
1113 free(d);
1114 }
d99ae53a
LP
1115
1116 hashmap_free(h);
1117
8e044443
LP
1118 json_variant_unref_many(array, n);
1119 free(array);
1120
d99ae53a 1121 return r;
86aa7ba4
LP
1122}
1123
4d5d1bba 1124static int output_cat_field(
08ace05b
LP
1125 FILE *f,
1126 sd_journal *j,
cc25a67e 1127 OutputFlags flags,
e3eec1fd 1128 int prio,
4d5d1bba 1129 const char *field,
05c7d9bf 1130 const size_t highlight[2]) {
08ace05b 1131
e3eec1fd 1132 const char *color_on = "", *color_off = "", *highlight_on = "";
d3f2bdbf 1133 const void *data;
4d5d1bba 1134 size_t l, fl;
d3f2bdbf
LP
1135 int r;
1136
e3eec1fd
LP
1137 if (FLAGS_SET(flags, OUTPUT_COLOR))
1138 get_log_colors(prio, &color_on, &color_off, &highlight_on);
93b73b06 1139
4d5d1bba 1140 r = sd_journal_get_data(j, field, &data, &l);
4f5e1723
AW
1141 if (r == -EBADMSG) {
1142 log_debug_errno(r, "Skipping message we can't read: %m");
1143 return 0;
1144 }
4d5d1bba
LP
1145 if (r == -ENOENT) /* An entry without the requested field */
1146 return 0;
1147 if (r < 0)
8d3d7072 1148 return log_error_errno(r, "Failed to get data: %m");
d3f2bdbf 1149
4d5d1bba
LP
1150 fl = strlen(field);
1151 assert(l >= fl + 1);
1152 assert(((char*) data)[fl] == '=');
1153
1154 data = (const uint8_t*) data + fl + 1;
1155 l -= fl + 1;
d3f2bdbf 1156
e3eec1fd
LP
1157 if (FLAGS_SET(flags, OUTPUT_COLOR)) {
1158 if (highlight) {
1159 assert(highlight[0] <= highlight[1]);
1160 assert(highlight[1] <= l);
1161
1162 fputs(color_on, f);
1163 fwrite((const char*) data, 1, highlight[0], f);
1164 fputs(highlight_on, f);
1165 fwrite((const char*) data + highlight[0], 1, highlight[1] - highlight[0], f);
1166 fputs(color_on, f);
1167 fwrite((const char*) data + highlight[1], 1, l - highlight[1], f);
1168 fputs(color_off, f);
1169 } else {
1170 fputs(color_on, f);
1171 fwrite((const char*) data, 1, l, f);
1172 fputs(color_off, f);
1173 }
b4766d5f 1174 } else
4d5d1bba
LP
1175 fwrite((const char*) data, 1, l, f);
1176
08ace05b 1177 fputc('\n', f);
4d5d1bba
LP
1178 return 0;
1179}
1180
1181static int output_cat(
1182 FILE *f,
1183 sd_journal *j,
1184 OutputMode mode,
1185 unsigned n_columns,
1186 OutputFlags flags,
2f063186 1187 const Set *output_fields,
05c7d9bf 1188 const size_t highlight[2]) {
4d5d1bba 1189
e3eec1fd 1190 int r, prio = LOG_INFO;
4d5d1bba 1191 const char *field;
4d5d1bba
LP
1192
1193 assert(j);
1194 assert(f);
1195
1196 (void) sd_journal_set_data_threshold(j, 0);
1197
e3eec1fd
LP
1198 if (FLAGS_SET(flags, OUTPUT_COLOR)) {
1199 const void *data;
1200 size_t l;
1201
1202 /* Determine priority of this entry, so that we can color it nicely */
1203
1204 r = sd_journal_get_data(j, "PRIORITY", &data, &l);
1205 if (r == -EBADMSG) {
1206 log_debug_errno(r, "Skipping message we can't read: %m");
1207 return 0;
1208 }
1209 if (r < 0) {
1210 if (r != -ENOENT)
1211 return log_error_errno(r, "Failed to get data: %m");
1212
1213 /* An entry without PRIORITY */
1214 } else if (l == 10 && memcmp(data, "PRIORITY=", 9) == 0) {
1215 char c = ((char*) data)[9];
1216
1217 if (c >= '0' && c <= '7')
1218 prio = c - '0';
1219 }
1220 }
1221
4d5d1bba 1222 if (set_isempty(output_fields))
e3eec1fd 1223 return output_cat_field(f, j, flags, prio, "MESSAGE", highlight);
4d5d1bba 1224
90e74a66 1225 SET_FOREACH(field, output_fields) {
e3eec1fd 1226 r = output_cat_field(f, j, flags, prio, field, streq(field, "MESSAGE") ? highlight : NULL);
4d5d1bba
LP
1227 if (r < 0)
1228 return r;
1229 }
d3f2bdbf
LP
1230
1231 return 0;
1232}
1233
08ace05b
LP
1234static int (*output_funcs[_OUTPUT_MODE_MAX])(
1235 FILE *f,
f2a3de01 1236 sd_journal *j,
08ace05b
LP
1237 OutputMode mode,
1238 unsigned n_columns,
cc25a67e 1239 OutputFlags flags,
2f063186 1240 const Set *output_fields,
05c7d9bf 1241 const size_t highlight[2]) = {
08ace05b 1242
71b7a6c4
ZJS
1243 [OUTPUT_SHORT] = output_short,
1244 [OUTPUT_SHORT_ISO] = output_short,
7e563bfc 1245 [OUTPUT_SHORT_ISO_PRECISE] = output_short,
71b7a6c4
ZJS
1246 [OUTPUT_SHORT_PRECISE] = output_short,
1247 [OUTPUT_SHORT_MONOTONIC] = output_short,
1248 [OUTPUT_SHORT_UNIX] = output_short,
1249 [OUTPUT_SHORT_FULL] = output_short,
1250 [OUTPUT_VERBOSE] = output_verbose,
1251 [OUTPUT_EXPORT] = output_export,
1252 [OUTPUT_JSON] = output_json,
1253 [OUTPUT_JSON_PRETTY] = output_json,
1254 [OUTPUT_JSON_SSE] = output_json,
1255 [OUTPUT_JSON_SEQ] = output_json,
1256 [OUTPUT_CAT] = output_cat,
1257 [OUTPUT_WITH_UNIT] = output_short,
86aa7ba4
LP
1258};
1259
9b972c9a 1260int show_journal_entry(
08ace05b
LP
1261 FILE *f,
1262 sd_journal *j,
1263 OutputMode mode,
1264 unsigned n_columns,
94e0bd7d 1265 OutputFlags flags,
cc25a67e 1266 char **output_fields,
05c7d9bf 1267 const size_t highlight[2],
94e0bd7d 1268 bool *ellipsized) {
08ace05b 1269
be327321
ZJS
1270 _cleanup_set_free_ Set *fields = NULL;
1271 int r;
1272
df50185b 1273 assert(mode >= 0);
86aa7ba4
LP
1274 assert(mode < _OUTPUT_MODE_MAX);
1275
34a35ece
LP
1276 if (n_columns <= 0)
1277 n_columns = columns();
1278
be327321
ZJS
1279 r = set_put_strdupv(&fields, output_fields);
1280 if (r < 0)
1281 return r;
cc25a67e 1282
be327321 1283 r = output_funcs[mode](f, j, mode, n_columns, flags, fields, highlight);
94e0bd7d 1284
be327321 1285 if (ellipsized && r > 0)
94e0bd7d
ZJS
1286 *ellipsized = true;
1287
be327321 1288 return r;
86aa7ba4
LP
1289}
1290
ea6c2dd1
LP
1291static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
1292 assert(f);
1293 assert(flags);
1294
1295 if (!(*flags & OUTPUT_BEGIN_NEWLINE))
1296 return 0;
1297
1298 /* Print a beginning new line if that's request, but only once
1299 * on the first line we print. */
1300
1301 fputc('\n', f);
1302 *flags &= ~OUTPUT_BEGIN_NEWLINE;
1303 return 0;
1304}
1305
889e3960
ZJS
1306int show_journal(
1307 FILE *f,
1308 sd_journal *j,
1309 OutputMode mode,
1310 unsigned n_columns,
1311 usec_t not_before,
1312 unsigned how_many,
1313 OutputFlags flags,
1314 bool *ellipsized) {
86aa7ba4 1315
86aa7ba4 1316 int r;
df50185b
LP
1317 unsigned line = 0;
1318 bool need_seek = false;
085d7120 1319 int warn_cutoff = flags & OUTPUT_WARN_CUTOFF;
86aa7ba4 1320
1a6c43e9 1321 assert(j);
df50185b
LP
1322 assert(mode >= 0);
1323 assert(mode < _OUTPUT_MODE_MAX);
1946b0bd 1324
889e3960
ZJS
1325 if (how_many == (unsigned) -1)
1326 need_seek = true;
1327 else {
1328 /* Seek to end */
1329 r = sd_journal_seek_tail(j);
1330 if (r < 0)
1331 return log_error_errno(r, "Failed to seek to tail: %m");
86aa7ba4 1332
889e3960
ZJS
1333 r = sd_journal_previous_skip(j, how_many);
1334 if (r < 0)
1335 return log_error_errno(r, "Failed to skip previous: %m");
1336 }
86aa7ba4 1337
df50185b 1338 for (;;) {
27f31daf 1339 usec_t usec;
df50185b 1340
27f31daf
HD
1341 if (need_seek) {
1342 r = sd_journal_next(j);
1343 if (r < 0)
1344 return log_error_errno(r, "Failed to iterate through journal: %m");
1345 }
df50185b 1346
27f31daf
HD
1347 if (r == 0)
1348 break;
df50185b 1349
27f31daf 1350 need_seek = true;
df50185b 1351
27f31daf
HD
1352 if (not_before > 0) {
1353 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
df50185b 1354
27f31daf
HD
1355 /* -ESTALE is returned if the timestamp is not from this boot */
1356 if (r == -ESTALE)
1357 continue;
1358 else if (r < 0)
1359 return log_error_errno(r, "Failed to get journal time: %m");
df50185b 1360
27f31daf
HD
1361 if (usec < not_before)
1362 continue;
df50185b
LP
1363 }
1364
27f31daf
HD
1365 line++;
1366 maybe_print_begin_newline(f, &flags);
08984293 1367
27f31daf
HD
1368 r = show_journal_entry(f, j, mode, n_columns, flags, NULL, NULL, ellipsized);
1369 if (r < 0)
1370 return r;
1371 }
08984293 1372
27f31daf
HD
1373 if (warn_cutoff && line < how_many && not_before > 0) {
1374 sd_id128_t boot_id;
1375 usec_t cutoff = 0;
08984293 1376
27f31daf 1377 /* Check whether the cutoff line is too early */
1a8f0ce6 1378
27f31daf
HD
1379 r = sd_id128_get_boot(&boot_id);
1380 if (r < 0)
1381 return log_error_errno(r, "Failed to get boot id: %m");
1a8f0ce6 1382
27f31daf
HD
1383 r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL);
1384 if (r < 0)
1385 return log_error_errno(r, "Failed to get journal cutoff time: %m");
1a8f0ce6 1386
27f31daf
HD
1387 if (r > 0 && not_before < cutoff) {
1388 maybe_print_begin_newline(f, &flags);
08984293 1389
27f31daf
HD
1390 /* If we logged *something* and no permission error happened, than we can reliably
1391 * emit the warning about rotation. If we didn't log anything and access errors
1392 * happened, emit hint about permissions. Otherwise, give a generic message, since we
1393 * can't diagnose the issue. */
08984293 1394
27f31daf 1395 bool noaccess = journal_access_blocked(j);
86aa7ba4 1396
27f31daf
HD
1397 if (line == 0 && noaccess)
1398 fprintf(f, "Warning: some journal files were not opened due to insufficient permissions.");
1399 else if (!noaccess)
1400 fprintf(f, "Warning: journal has been rotated since unit was started, output may be incomplete.\n");
1401 else
1402 fprintf(f, "Warning: journal has been rotated since unit was started and some journal "
1403 "files were not opened due to insufficient permissions, output may be incomplete.\n");
1404 }
df50185b 1405
27f31daf 1406 warn_cutoff = false;
86aa7ba4
LP
1407 }
1408
b56d608e 1409 return 0;
1a6c43e9
MT
1410}
1411
886a64fe 1412int add_matches_for_unit(sd_journal *j, const char *unit) {
2b45d881 1413 const char *m1, *m2, *m3, *m4;
1a6c43e9
MT
1414 int r;
1415
886a64fe 1416 assert(j);
1a6c43e9
MT
1417 assert(unit);
1418
63c372cb
LP
1419 m1 = strjoina("_SYSTEMD_UNIT=", unit);
1420 m2 = strjoina("COREDUMP_UNIT=", unit);
1421 m3 = strjoina("UNIT=", unit);
1422 m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);
1a6c43e9 1423
886a64fe
ZJS
1424 (void)(
1425 /* Look for messages from the service itself */
1426 (r = sd_journal_add_match(j, m1, 0)) ||
1427
1428 /* Look for coredumps of the service */
1429 (r = sd_journal_add_disjunction(j)) ||
fdcd37df
ZJS
1430 (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1431 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
886a64fe
ZJS
1432 (r = sd_journal_add_match(j, m2, 0)) ||
1433
1434 /* Look for messages from PID 1 about this service */
1435 (r = sd_journal_add_disjunction(j)) ||
1436 (r = sd_journal_add_match(j, "_PID=1", 0)) ||
2d0b2e87
ZJS
1437 (r = sd_journal_add_match(j, m3, 0)) ||
1438
1439 /* Look for messages from authorized daemons about this service */
1440 (r = sd_journal_add_disjunction(j)) ||
1441 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1442 (r = sd_journal_add_match(j, m4, 0))
886a64fe 1443 );
2d0b2e87 1444
69ae3ee0 1445 if (r == 0 && endswith(unit, ".slice")) {
2b45d881
LP
1446 const char *m5;
1447
1448 m5 = strjoina("_SYSTEMD_SLICE=", unit);
69ae3ee0
ZJS
1449
1450 /* Show all messages belonging to a slice */
1451 (void)(
1452 (r = sd_journal_add_disjunction(j)) ||
1453 (r = sd_journal_add_match(j, m5, 0))
1454 );
1455 }
1456
886a64fe
ZJS
1457 return r;
1458}
1a6c43e9 1459
886a64fe
ZJS
1460int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
1461 int r;
2d0b2e87
ZJS
1462 char *m1, *m2, *m3, *m4;
1463 char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)];
1a6c43e9 1464
886a64fe
ZJS
1465 assert(j);
1466 assert(unit);
1a6c43e9 1467
63c372cb
LP
1468 m1 = strjoina("_SYSTEMD_USER_UNIT=", unit);
1469 m2 = strjoina("USER_UNIT=", unit);
1470 m3 = strjoina("COREDUMP_USER_UNIT=", unit);
1471 m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit);
de0671ee 1472 sprintf(muid, "_UID="UID_FMT, uid);
1a6c43e9 1473
886a64fe
ZJS
1474 (void) (
1475 /* Look for messages from the user service itself */
1476 (r = sd_journal_add_match(j, m1, 0)) ||
2d0b2e87 1477 (r = sd_journal_add_match(j, muid, 0)) ||
886a64fe
ZJS
1478
1479 /* Look for messages from systemd about this service */
1480 (r = sd_journal_add_disjunction(j)) ||
1481 (r = sd_journal_add_match(j, m2, 0)) ||
2d0b2e87 1482 (r = sd_journal_add_match(j, muid, 0)) ||
886a64fe
ZJS
1483
1484 /* Look for coredumps of the service */
1485 (r = sd_journal_add_disjunction(j)) ||
1486 (r = sd_journal_add_match(j, m3, 0)) ||
2d0b2e87
ZJS
1487 (r = sd_journal_add_match(j, muid, 0)) ||
1488 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1489
1490 /* Look for messages from authorized daemons about this service */
1491 (r = sd_journal_add_disjunction(j)) ||
fdcd37df 1492 (r = sd_journal_add_match(j, m4, 0)) ||
2d0b2e87 1493 (r = sd_journal_add_match(j, muid, 0)) ||
fdcd37df 1494 (r = sd_journal_add_match(j, "_UID=0", 0))
886a64fe 1495 );
69ae3ee0
ZJS
1496
1497 if (r == 0 && endswith(unit, ".slice")) {
2b45d881
LP
1498 const char *m5;
1499
1500 m5 = strjoina("_SYSTEMD_SLICE=", unit);
69ae3ee0
ZJS
1501
1502 /* Show all messages belonging to a slice */
1503 (void)(
1504 (r = sd_journal_add_disjunction(j)) ||
1505 (r = sd_journal_add_match(j, m5, 0)) ||
1506 (r = sd_journal_add_match(j, muid, 0))
1507 );
1508 }
1509
1a6c43e9
MT
1510 return r;
1511}
1512
b6741478 1513static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
3d94f76c 1514 _cleanup_close_pair_ int pair[2] = { -1, -1 };
a4475f57 1515 _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
b5ea030d 1516 char buf[ID128_UUID_STRING_MAX];
b6741478 1517 pid_t pid, child;
b6741478
LP
1518 ssize_t k;
1519 int r;
1520
1521 assert(machine);
1522 assert(boot_id);
1523
affcf189 1524 if (!machine_name_is_valid(machine))
b6741478
LP
1525 return -EINVAL;
1526
e04b0cdb 1527 r = container_get_leader(machine, &pid);
b6741478
LP
1528 if (r < 0)
1529 return r;
e04b0cdb 1530
671c3419 1531 r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, NULL, &rootfd);
b6741478
LP
1532 if (r < 0)
1533 return r;
1534
e04b0cdb 1535 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
b6741478
LP
1536 return -errno;
1537
1edcb6a9
LP
1538 r = namespace_fork("(sd-bootidns)", "(sd-bootid)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG,
1539 pidnsfd, mntnsfd, -1, -1, rootfd, &child);
4c253ed1
LP
1540 if (r < 0)
1541 return r;
1542 if (r == 0) {
b6741478
LP
1543 int fd;
1544
03e334a1 1545 pair[0] = safe_close(pair[0]);
b6741478 1546
b6741478
LP
1547 fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
1548 if (fd < 0)
1549 _exit(EXIT_FAILURE);
1550
a6dcc7e5 1551 r = loop_read_exact(fd, buf, 36, false);
03e334a1 1552 safe_close(fd);
6c767d1e 1553 if (r < 0)
b6741478
LP
1554 _exit(EXIT_FAILURE);
1555
e04b0cdb 1556 k = send(pair[1], buf, 36, MSG_NOSIGNAL);
b6741478
LP
1557 if (k != 36)
1558 _exit(EXIT_FAILURE);
1559
1560 _exit(EXIT_SUCCESS);
1561 }
1562
03e334a1 1563 pair[1] = safe_close(pair[1]);
b6741478 1564
1edcb6a9 1565 r = wait_for_terminate_and_check("(sd-bootidns)", child, 0);
2e87a1fd
LP
1566 if (r < 0)
1567 return r;
1568 if (r != EXIT_SUCCESS)
1569 return -EIO;
b6741478 1570
fbadf045
LP
1571 k = recv(pair[0], buf, 36, 0);
1572 if (k != 36)
1573 return -EIO;
1574
b6741478
LP
1575 buf[36] = 0;
1576 r = sd_id128_from_string(buf, boot_id);
1577 if (r < 0)
1578 return r;
1579
1580 return 0;
1581}
1582
1583int add_match_this_boot(sd_journal *j, const char *machine) {
5ec76417
ZJS
1584 char match[9+32+1] = "_BOOT_ID=";
1585 sd_id128_t boot_id;
1586 int r;
1587
1588 assert(j);
1589
b6741478
LP
1590 if (machine) {
1591 r = get_boot_id_for_machine(machine, &boot_id);
f647962d
MS
1592 if (r < 0)
1593 return log_error_errno(r, "Failed to get boot id of container %s: %m", machine);
b6741478
LP
1594 } else {
1595 r = sd_id128_get_boot(&boot_id);
f647962d
MS
1596 if (r < 0)
1597 return log_error_errno(r, "Failed to get boot id: %m");
5ec76417
ZJS
1598 }
1599
1600 sd_id128_to_string(boot_id, match + 9);
1601 r = sd_journal_add_match(j, match, strlen(match));
f647962d
MS
1602 if (r < 0)
1603 return log_error_errno(r, "Failed to add match: %m");
5ec76417
ZJS
1604
1605 r = sd_journal_add_conjunction(j);
1606 if (r < 0)
b56d608e 1607 return log_error_errno(r, "Failed to add conjunction: %m");
5ec76417
ZJS
1608
1609 return 0;
1610}
1611
886a64fe 1612int show_journal_by_unit(
1a6c43e9
MT
1613 FILE *f,
1614 const char *unit,
d93dda3a 1615 const char *log_namespace,
1a6c43e9
MT
1616 OutputMode mode,
1617 unsigned n_columns,
1618 usec_t not_before,
1619 unsigned how_many,
1620 uid_t uid,
886a64fe 1621 OutputFlags flags,
3c756001
LP
1622 int journal_open_flags,
1623 bool system_unit,
94e0bd7d 1624 bool *ellipsized) {
1a6c43e9 1625
4afd3348 1626 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1a6c43e9
MT
1627 int r;
1628
1629 assert(mode >= 0);
1630 assert(mode < _OUTPUT_MODE_MAX);
1631 assert(unit);
1632
1a6c43e9
MT
1633 if (how_many <= 0)
1634 return 0;
1635
d93dda3a 1636 r = sd_journal_open_namespace(&j, log_namespace, journal_open_flags | SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE);
f9045468 1637 if (r < 0)
b56d608e 1638 return log_error_errno(r, "Failed to open journal: %m");
f9045468 1639
b6741478 1640 r = add_match_this_boot(j, NULL);
5ec76417
ZJS
1641 if (r < 0)
1642 return r;
1643
3c756001 1644 if (system_unit)
886a64fe
ZJS
1645 r = add_matches_for_unit(j, unit);
1646 else
1647 r = add_matches_for_user_unit(j, unit, uid);
1a6c43e9 1648 if (r < 0)
b56d608e 1649 return log_error_errno(r, "Failed to add unit matches: %m");
1a6c43e9 1650
f1d34068 1651 if (DEBUG_LOGGING) {
4ad16808
ZJS
1652 _cleanup_free_ char *filter;
1653
1654 filter = journal_make_match_string(j);
b56d608e
LP
1655 if (!filter)
1656 return log_oom();
1657
4ad16808
ZJS
1658 log_debug("Journal filter: %s", filter);
1659 }
5ec76417 1660
94e0bd7d 1661 return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized);
86aa7ba4 1662}