]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/logs-show.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / shared / logs-show.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <syslog.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #include "sd-id128.h"
15 #include "sd-journal.h"
16
17 #include "alloc-util.h"
18 #include "fd-util.h"
19 #include "format-util.h"
20 #include "hashmap.h"
21 #include "hostname-util.h"
22 #include "io-util.h"
23 #include "journal-internal.h"
24 #include "log.h"
25 #include "logs-show.h"
26 #include "macro.h"
27 #include "output-mode.h"
28 #include "parse-util.h"
29 #include "process-util.h"
30 #include "sparse-endian.h"
31 #include "stdio-util.h"
32 #include "string-table.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "terminal-util.h"
36 #include "time-util.h"
37 #include "utf8.h"
38 #include "util.h"
39
40 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
41 #define PRINT_LINE_THRESHOLD 3
42 #define PRINT_CHAR_THRESHOLD 300
43
44 #define JSON_THRESHOLD 4096
45
46 static int print_catalog(FILE *f, sd_journal *j) {
47 int r;
48 _cleanup_free_ char *t = NULL, *z = NULL;
49
50 r = sd_journal_get_catalog(j, &t);
51 if (r < 0)
52 return r;
53
54 z = strreplace(strstrip(t), "\n", "\n-- ");
55 if (!z)
56 return log_oom();
57
58 fputs("-- ", f);
59 fputs(z, f);
60 fputc('\n', f);
61
62 return 0;
63 }
64
65 static int parse_field(const void *data, size_t length, const char *field, size_t field_len, char **target, size_t *target_len) {
66 size_t nl;
67 char *buf;
68
69 assert(data);
70 assert(field);
71 assert(target);
72
73 if (length < field_len)
74 return 0;
75
76 if (memcmp(data, field, field_len))
77 return 0;
78
79 nl = length - field_len;
80
81 buf = newdup_suffix0(char, (const char*) data + field_len, nl);
82 if (!buf)
83 return log_oom();
84
85 free(*target);
86 *target = buf;
87
88 if (target_len)
89 *target_len = nl;
90
91 return 1;
92 }
93
94 typedef struct ParseFieldVec {
95 const char *field;
96 size_t field_len;
97 char **target;
98 size_t *target_len;
99 } ParseFieldVec;
100
101 #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) \
102 { .field = _field, .field_len = strlen(_field), .target = _target, .target_len = _target_len }
103
104 static int parse_fieldv(const void *data, size_t length, const ParseFieldVec *fields, unsigned n_fields) {
105 unsigned i;
106
107 for (i = 0; i < n_fields; i++) {
108 const ParseFieldVec *f = &fields[i];
109 int r;
110
111 r = parse_field(data, length, f->field, f->field_len, f->target, f->target_len);
112 if (r < 0)
113 return r;
114 else if (r > 0)
115 break;
116 }
117
118 return 0;
119 }
120
121 static int field_set_test(Set *fields, const char *name, size_t n) {
122 char *s = NULL;
123
124 if (!fields)
125 return 1;
126
127 s = strndupa(name, n);
128 if (!s)
129 return log_oom();
130
131 return set_get(fields, s) ? 1 : 0;
132 }
133
134 static bool shall_print(const char *p, size_t l, OutputFlags flags) {
135 assert(p);
136
137 if (flags & OUTPUT_SHOW_ALL)
138 return true;
139
140 if (l >= PRINT_CHAR_THRESHOLD)
141 return false;
142
143 if (!utf8_is_printable(p, l))
144 return false;
145
146 return true;
147 }
148
149 static bool print_multiline(
150 FILE *f,
151 unsigned prefix,
152 unsigned n_columns,
153 OutputFlags flags,
154 int priority,
155 const char* message,
156 size_t message_len,
157 size_t highlight[2]) {
158
159 const char *color_on = "", *color_off = "", *highlight_on = "";
160 const char *pos, *end;
161 bool ellipsized = false;
162 int line = 0;
163
164 if (flags & OUTPUT_COLOR) {
165 if (priority <= LOG_ERR) {
166 color_on = ANSI_HIGHLIGHT_RED;
167 color_off = ANSI_NORMAL;
168 highlight_on = ANSI_HIGHLIGHT;
169 } else if (priority <= LOG_NOTICE) {
170 color_on = ANSI_HIGHLIGHT;
171 color_off = ANSI_NORMAL;
172 highlight_on = ANSI_HIGHLIGHT_RED;
173 }
174 }
175
176 /* A special case: make sure that we print a newline when
177 the message is empty. */
178 if (message_len == 0)
179 fputs("\n", f);
180
181 for (pos = message;
182 pos < message + message_len;
183 pos = end + 1, line++) {
184 bool continuation = line > 0;
185 bool tail_line;
186 int len;
187 for (end = pos; end < message + message_len && *end != '\n'; end++)
188 ;
189 len = end - pos;
190 assert(len >= 0);
191
192 /* We need to figure out when we are showing not-last line, *and*
193 * will skip subsequent lines. In that case, we will put the dots
194 * at the end of the line, instead of putting dots in the middle
195 * or not at all.
196 */
197 tail_line =
198 line + 1 == PRINT_LINE_THRESHOLD ||
199 end + 1 >= message + PRINT_CHAR_THRESHOLD;
200
201 if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) ||
202 (prefix + len + 1 < n_columns && !tail_line)) {
203 if (highlight &&
204 (size_t) (pos - message) <= highlight[0] &&
205 highlight[0] < (size_t) len) {
206
207 fprintf(f, "%*s%s%.*s",
208 continuation * prefix, "",
209 color_on, (int) highlight[0], pos);
210 fprintf(f, "%s%.*s",
211 highlight_on,
212 (int) (MIN((size_t) len, highlight[1]) - highlight[0]),
213 pos + highlight[0]);
214 if ((size_t) len > highlight[1])
215 fprintf(f, "%s%.*s",
216 color_on,
217 (int) (len - highlight[1]),
218 pos + highlight[1]);
219 fprintf(f, "%s\n", color_off);
220
221 } else
222 fprintf(f, "%*s%s%.*s%s\n",
223 continuation * prefix, "",
224 color_on, len, pos, color_off);
225 continue;
226 }
227
228 /* Beyond this point, ellipsization will happen. */
229 ellipsized = true;
230
231 if (prefix < n_columns && n_columns - prefix >= 3) {
232 if (n_columns - prefix > (unsigned) len + 3)
233 fprintf(f, "%*s%s%.*s...%s\n",
234 continuation * prefix, "",
235 color_on, len, pos, color_off);
236 else {
237 _cleanup_free_ char *e;
238
239 e = ellipsize_mem(pos, len, n_columns - prefix,
240 tail_line ? 100 : 90);
241 if (!e)
242 fprintf(f, "%*s%s%.*s%s\n",
243 continuation * prefix, "",
244 color_on, len, pos, color_off);
245 else
246 fprintf(f, "%*s%s%s%s\n",
247 continuation * prefix, "",
248 color_on, e, color_off);
249 }
250 } else
251 fputs("...\n", f);
252
253 if (tail_line)
254 break;
255 }
256
257 return ellipsized;
258 }
259
260 static int output_timestamp_monotonic(FILE *f, sd_journal *j, const char *monotonic) {
261 sd_id128_t boot_id;
262 uint64_t t;
263 int r;
264
265 assert(f);
266 assert(j);
267
268 r = -ENXIO;
269 if (monotonic)
270 r = safe_atou64(monotonic, &t);
271 if (r < 0)
272 r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
273 if (r < 0)
274 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
275
276 fprintf(f, "[%5"PRI_USEC".%06"PRI_USEC"]", t / USEC_PER_SEC, t % USEC_PER_SEC);
277 return 1 + 5 + 1 + 6 + 1;
278 }
279
280 static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, OutputFlags flags, const char *realtime) {
281 char buf[MAX(FORMAT_TIMESTAMP_MAX, 64)];
282 struct tm *(*gettime_r)(const time_t *, struct tm *);
283 struct tm tm;
284 uint64_t x;
285 time_t t;
286 int r;
287
288 assert(f);
289 assert(j);
290
291 if (realtime)
292 r = safe_atou64(realtime, &x);
293 if (!realtime || r < 0 || !VALID_REALTIME(x))
294 r = sd_journal_get_realtime_usec(j, &x);
295 if (r < 0)
296 return log_error_errno(r, "Failed to get realtime timestamp: %m");
297
298 if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) {
299 const char *k;
300
301 if (flags & OUTPUT_UTC)
302 k = format_timestamp_utc(buf, sizeof(buf), x);
303 else
304 k = format_timestamp(buf, sizeof(buf), x);
305 if (!k) {
306 log_error("Failed to format timestamp: %"PRIu64, x);
307 return -EINVAL;
308 }
309
310 } else {
311 char usec[7];
312
313 gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r;
314 t = (time_t) (x / USEC_PER_SEC);
315
316 switch (mode) {
317
318 case OUTPUT_SHORT_UNIX:
319 xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, x % USEC_PER_SEC);
320 break;
321
322 case OUTPUT_SHORT_ISO:
323 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm)) <= 0) {
324 log_error("Failed to format ISO time");
325 return -EINVAL;
326 }
327 break;
328
329 case OUTPUT_SHORT_ISO_PRECISE:
330 /* No usec in strftime, so we leave space and copy over */
331 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t, &tm)) <= 0) {
332 log_error("Failed to format ISO-precise time");
333 return -EINVAL;
334 }
335 xsprintf(usec, "%06"PRI_USEC, x % USEC_PER_SEC);
336 memcpy(buf + 20, usec, 6);
337 break;
338
339 case OUTPUT_SHORT:
340 case OUTPUT_SHORT_PRECISE:
341
342 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm)) <= 0) {
343 log_error("Failed to format syslog time");
344 return -EINVAL;
345 }
346
347 if (mode == OUTPUT_SHORT_PRECISE) {
348 size_t k;
349
350 assert(sizeof(buf) > strlen(buf));
351 k = sizeof(buf) - strlen(buf);
352
353 r = snprintf(buf + strlen(buf), k, ".%06"PRIu64, x % USEC_PER_SEC);
354 if (r <= 0 || (size_t) r >= k) { /* too long? */
355 log_error("Failed to format precise time");
356 return -EINVAL;
357 }
358 }
359 break;
360
361 default:
362 assert_not_reached("Unknown time format");
363 }
364 }
365
366 fputs(buf, f);
367 return (int) strlen(buf);
368 }
369
370 static int output_short(
371 FILE *f,
372 sd_journal *j,
373 OutputMode mode,
374 unsigned n_columns,
375 OutputFlags flags,
376 Set *output_fields,
377 size_t highlight[2]) {
378
379 int r;
380 const void *data;
381 size_t length;
382 size_t n = 0;
383 _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL, *unit = NULL, *user_unit = NULL;
384 size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, realtime_len = 0, monotonic_len = 0, priority_len = 0, unit_len = 0, user_unit_len = 0;
385 int p = LOG_INFO;
386 bool ellipsized = false;
387 const ParseFieldVec fields[] = {
388 PARSE_FIELD_VEC_ENTRY("_PID=", &pid, &pid_len),
389 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm, &comm_len),
390 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message, &message_len),
391 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority, &priority_len),
392 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname, &hostname_len),
393 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid, &fake_pid_len),
394 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len),
395 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len),
396 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len),
397 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit, &unit_len),
398 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit, &user_unit_len),
399 };
400 size_t highlight_shifted[] = {highlight ? highlight[0] : 0, highlight ? highlight[1] : 0};
401
402 assert(f);
403 assert(j);
404
405 /* Set the threshold to one bigger than the actual print
406 * threshold, so that if the line is actually longer than what
407 * we're willing to print, ellipsization will occur. This way
408 * we won't output a misleading line without any indication of
409 * truncation.
410 */
411 sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
412
413 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
414 r = parse_fieldv(data, length, fields, ELEMENTSOF(fields));
415 if (r < 0)
416 return r;
417 }
418 if (r == -EBADMSG) {
419 log_debug_errno(r, "Skipping message we can't read: %m");
420 return 0;
421 }
422 if (r < 0)
423 return log_error_errno(r, "Failed to get journal fields: %m");
424
425 if (!message) {
426 log_debug("Skipping message without MESSAGE= field.");
427 return 0;
428 }
429
430 if (!(flags & OUTPUT_SHOW_ALL))
431 strip_tab_ansi(&message, &message_len, highlight_shifted);
432
433 if (priority_len == 1 && *priority >= '0' && *priority <= '7')
434 p = *priority - '0';
435
436 if (mode == OUTPUT_SHORT_MONOTONIC)
437 r = output_timestamp_monotonic(f, j, monotonic);
438 else
439 r = output_timestamp_realtime(f, j, mode, flags, realtime);
440 if (r < 0)
441 return r;
442 n += r;
443
444 if (flags & OUTPUT_NO_HOSTNAME) {
445 /* Suppress display of the hostname if this is requested. */
446 hostname = mfree(hostname);
447 hostname_len = 0;
448 }
449
450 if (hostname && shall_print(hostname, hostname_len, flags)) {
451 fprintf(f, " %.*s", (int) hostname_len, hostname);
452 n += hostname_len + 1;
453 }
454
455 if (mode == OUTPUT_WITH_UNIT && ((unit && shall_print(unit, unit_len, flags)) || (user_unit && shall_print(user_unit, user_unit_len, flags)))) {
456 if (unit) {
457 fprintf(f, " %.*s", (int) unit_len, unit);
458 n += unit_len + 1;
459 }
460 if (user_unit) {
461 if (unit)
462 fprintf(f, "/%.*s", (int) user_unit_len, user_unit);
463 else
464 fprintf(f, " %.*s", (int) user_unit_len, user_unit);
465 n += unit_len + 1;
466 }
467 } else if (identifier && shall_print(identifier, identifier_len, flags)) {
468 fprintf(f, " %.*s", (int) identifier_len, identifier);
469 n += identifier_len + 1;
470 } else if (comm && shall_print(comm, comm_len, flags)) {
471 fprintf(f, " %.*s", (int) comm_len, comm);
472 n += comm_len + 1;
473 } else
474 fputs(" unknown", f);
475
476 if (pid && shall_print(pid, pid_len, flags)) {
477 fprintf(f, "[%.*s]", (int) pid_len, pid);
478 n += pid_len + 2;
479 } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) {
480 fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid);
481 n += fake_pid_len + 2;
482 }
483
484 if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
485 char bytes[FORMAT_BYTES_MAX];
486 fprintf(f, ": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
487 } else {
488 fputs(": ", f);
489 ellipsized |=
490 print_multiline(f, n + 2, n_columns, flags, p,
491 message, message_len,
492 highlight_shifted);
493 }
494
495 if (flags & OUTPUT_CATALOG)
496 print_catalog(f, j);
497
498 return ellipsized;
499 }
500
501 static int output_verbose(
502 FILE *f,
503 sd_journal *j,
504 OutputMode mode,
505 unsigned n_columns,
506 OutputFlags flags,
507 Set *output_fields,
508 size_t highlight[2]) {
509
510 const void *data;
511 size_t length;
512 _cleanup_free_ char *cursor = NULL;
513 uint64_t realtime = 0;
514 char ts[FORMAT_TIMESTAMP_MAX + 7];
515 const char *timestamp;
516 int r;
517
518 assert(f);
519 assert(j);
520
521 sd_journal_set_data_threshold(j, 0);
522
523 r = sd_journal_get_data(j, "_SOURCE_REALTIME_TIMESTAMP", &data, &length);
524 if (r == -ENOENT)
525 log_debug("Source realtime timestamp not found");
526 else if (r < 0)
527 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get source realtime timestamp: %m");
528 else {
529 _cleanup_free_ char *value = NULL;
530
531 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=",
532 STRLEN("_SOURCE_REALTIME_TIMESTAMP="), &value,
533 NULL);
534 if (r < 0)
535 return r;
536 assert(r > 0);
537
538 r = safe_atou64(value, &realtime);
539 if (r < 0)
540 log_debug_errno(r, "Failed to parse realtime timestamp: %m");
541 }
542
543 if (r < 0) {
544 r = sd_journal_get_realtime_usec(j, &realtime);
545 if (r < 0)
546 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get realtime timestamp: %m");
547 }
548
549 r = sd_journal_get_cursor(j, &cursor);
550 if (r < 0)
551 return log_error_errno(r, "Failed to get cursor: %m");
552
553 timestamp = flags & OUTPUT_UTC ? format_timestamp_us_utc(ts, sizeof ts, realtime)
554 : format_timestamp_us(ts, sizeof ts, realtime);
555 fprintf(f, "%s [%s]\n",
556 timestamp ?: "(no timestamp)",
557 cursor);
558
559 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
560 const char *c;
561 int fieldlen;
562 const char *on = "", *off = "";
563
564 c = memchr(data, '=', length);
565 if (!c) {
566 log_error("Invalid field.");
567 return -EINVAL;
568 }
569 fieldlen = c - (const char*) data;
570
571 r = field_set_test(output_fields, data, fieldlen);
572 if (r < 0)
573 return r;
574 if (!r)
575 continue;
576
577 if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) {
578 on = ANSI_HIGHLIGHT;
579 off = ANSI_NORMAL;
580 }
581
582 if ((flags & OUTPUT_SHOW_ALL) ||
583 (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH)
584 && utf8_is_printable(data, length))) {
585 fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data);
586 print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1, NULL);
587 fputs(off, f);
588 } else {
589 char bytes[FORMAT_BYTES_MAX];
590
591 fprintf(f, " %s%.*s=[%s blob data]%s\n",
592 on,
593 (int) (c - (const char*) data),
594 (const char*) data,
595 format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
596 off);
597 }
598 }
599
600 if (r < 0)
601 return r;
602
603 if (flags & OUTPUT_CATALOG)
604 print_catalog(f, j);
605
606 return 0;
607 }
608
609 static int output_export(
610 FILE *f,
611 sd_journal *j,
612 OutputMode mode,
613 unsigned n_columns,
614 OutputFlags flags,
615 Set *output_fields,
616 size_t highlight[2]) {
617
618 sd_id128_t boot_id;
619 char sid[33];
620 int r;
621 usec_t realtime, monotonic;
622 _cleanup_free_ char *cursor = NULL;
623 const void *data;
624 size_t length;
625
626 assert(j);
627
628 sd_journal_set_data_threshold(j, 0);
629
630 r = sd_journal_get_realtime_usec(j, &realtime);
631 if (r < 0)
632 return log_error_errno(r, "Failed to get realtime timestamp: %m");
633
634 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
635 if (r < 0)
636 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
637
638 r = sd_journal_get_cursor(j, &cursor);
639 if (r < 0)
640 return log_error_errno(r, "Failed to get cursor: %m");
641
642 fprintf(f,
643 "__CURSOR=%s\n"
644 "__REALTIME_TIMESTAMP="USEC_FMT"\n"
645 "__MONOTONIC_TIMESTAMP="USEC_FMT"\n"
646 "_BOOT_ID=%s\n",
647 cursor,
648 realtime,
649 monotonic,
650 sd_id128_to_string(boot_id, sid));
651
652 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
653 const char *c;
654
655 /* We already printed the boot id from the data in the header, hence let's suppress it here */
656 if (memory_startswith(data, length, "_BOOT_ID="))
657 continue;
658
659 c = memchr(data, '=', length);
660 if (!c) {
661 log_error("Invalid field.");
662 return -EINVAL;
663 }
664
665 r = field_set_test(output_fields, data, c - (const char *) data);
666 if (r < 0)
667 return r;
668 if (!r)
669 continue;
670
671 if (utf8_is_printable_newline(data, length, false))
672 fwrite(data, length, 1, f);
673 else {
674 uint64_t le64;
675
676 fwrite(data, c - (const char*) data, 1, f);
677 fputc('\n', f);
678 le64 = htole64(length - (c - (const char*) data) - 1);
679 fwrite(&le64, sizeof(le64), 1, f);
680 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f);
681 }
682
683 fputc('\n', f);
684 }
685 if (r == -EBADMSG) {
686 log_debug_errno(r, "Skipping message we can't read: %m");
687 return 0;
688 }
689
690 if (r < 0)
691 return r;
692
693 fputc('\n', f);
694
695 return 0;
696 }
697
698 void json_escape(
699 FILE *f,
700 const char* p,
701 size_t l,
702 OutputFlags flags) {
703
704 assert(f);
705 assert(p);
706
707 if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
708 fputs("null", f);
709
710 else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) {
711 bool not_first = false;
712
713 fputs("[ ", f);
714
715 while (l > 0) {
716 if (not_first)
717 fprintf(f, ", %u", (uint8_t) *p);
718 else {
719 not_first = true;
720 fprintf(f, "%u", (uint8_t) *p);
721 }
722
723 p++;
724 l--;
725 }
726
727 fputs(" ]", f);
728 } else {
729 fputc('\"', f);
730
731 while (l > 0) {
732 if (IN_SET(*p, '"', '\\')) {
733 fputc('\\', f);
734 fputc(*p, f);
735 } else if (*p == '\n')
736 fputs("\\n", f);
737 else if ((uint8_t) *p < ' ')
738 fprintf(f, "\\u%04x", (uint8_t) *p);
739 else
740 fputc(*p, f);
741
742 p++;
743 l--;
744 }
745
746 fputc('\"', f);
747 }
748 }
749
750 static int output_json(
751 FILE *f,
752 sd_journal *j,
753 OutputMode mode,
754 unsigned n_columns,
755 OutputFlags flags,
756 Set *output_fields,
757 size_t highlight[2]) {
758
759 uint64_t realtime, monotonic;
760 _cleanup_free_ char *cursor = NULL;
761 const void *data;
762 size_t length;
763 sd_id128_t boot_id;
764 char sid[33], *k;
765 int r;
766 Hashmap *h = NULL;
767 bool done, separator;
768
769 assert(j);
770
771 sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
772
773 r = sd_journal_get_realtime_usec(j, &realtime);
774 if (r < 0)
775 return log_error_errno(r, "Failed to get realtime timestamp: %m");
776
777 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
778 if (r < 0)
779 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
780
781 r = sd_journal_get_cursor(j, &cursor);
782 if (r < 0)
783 return log_error_errno(r, "Failed to get cursor: %m");
784
785 if (mode == OUTPUT_JSON_PRETTY)
786 fprintf(f,
787 "{\n"
788 "\t\"__CURSOR\" : \"%s\",\n"
789 "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\",\n"
790 "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\",\n"
791 "\t\"_BOOT_ID\" : \"%s\"",
792 cursor,
793 realtime,
794 monotonic,
795 sd_id128_to_string(boot_id, sid));
796 else {
797 if (mode == OUTPUT_JSON_SSE)
798 fputs("data: ", f);
799
800 fprintf(f,
801 "{ \"__CURSOR\" : \"%s\", "
802 "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\", "
803 "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\", "
804 "\"_BOOT_ID\" : \"%s\"",
805 cursor,
806 realtime,
807 monotonic,
808 sd_id128_to_string(boot_id, sid));
809 }
810
811 h = hashmap_new(&string_hash_ops);
812 if (!h)
813 return log_oom();
814
815 /* First round, iterate through the entry and count how often each field appears */
816 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
817 const char *eq;
818 char *n;
819 unsigned u;
820
821 if (memory_startswith(data, length, "_BOOT_ID="))
822 continue;
823
824 eq = memchr(data, '=', length);
825 if (!eq)
826 continue;
827
828 n = memdup_suffix0(data, eq - (const char*) data);
829 if (!n) {
830 r = log_oom();
831 goto finish;
832 }
833
834 u = PTR_TO_UINT(hashmap_get(h, n));
835 if (u == 0) {
836 r = hashmap_put(h, n, UINT_TO_PTR(1));
837 if (r < 0) {
838 free(n);
839 log_oom();
840 goto finish;
841 }
842 } else {
843 r = hashmap_update(h, n, UINT_TO_PTR(u + 1));
844 free(n);
845 if (r < 0) {
846 log_oom();
847 goto finish;
848 }
849 }
850 }
851 if (r == -EBADMSG) {
852 log_debug_errno(r, "Skipping message we can't read: %m");
853 return 0;
854 }
855 if (r < 0)
856 return r;
857
858 separator = true;
859 do {
860 done = true;
861
862 SD_JOURNAL_FOREACH_DATA(j, data, length) {
863 const char *eq;
864 char *kk;
865 _cleanup_free_ char *n = NULL;
866 size_t m;
867 unsigned u;
868
869 /* We already printed the boot id from the data in
870 * the header, hence let's suppress it here */
871 if (memory_startswith(data, length, "_BOOT_ID="))
872 continue;
873
874 eq = memchr(data, '=', length);
875 if (!eq)
876 continue;
877
878 m = eq - (const char*) data;
879 n = memdup_suffix0(data, m);
880 if (!n) {
881 r = log_oom();
882 goto finish;
883 }
884
885 if (output_fields && !set_get(output_fields, n))
886 continue;
887
888 if (separator)
889 fputs(mode == OUTPUT_JSON_PRETTY ? ",\n\t" : ", ", f);
890
891 u = PTR_TO_UINT(hashmap_get2(h, n, (void**) &kk));
892 if (u == 0)
893 /* We already printed this, let's jump to the next */
894 separator = false;
895
896 else if (u == 1) {
897 /* Field only appears once, output it directly */
898
899 json_escape(f, data, m, flags);
900 fputs(" : ", f);
901
902 json_escape(f, eq + 1, length - m - 1, flags);
903
904 hashmap_remove(h, n);
905 free(kk);
906
907 separator = true;
908
909 } else {
910 /* Field appears multiple times, output it as array */
911 json_escape(f, data, m, flags);
912 fputs(" : [ ", f);
913 json_escape(f, eq + 1, length - m - 1, flags);
914
915 /* Iterate through the end of the list */
916
917 while (sd_journal_enumerate_data(j, &data, &length) > 0) {
918 if (length < m + 1)
919 continue;
920
921 if (memcmp(data, n, m) != 0)
922 continue;
923
924 if (((const char*) data)[m] != '=')
925 continue;
926
927 fputs(", ", f);
928 json_escape(f, (const char*) data + m + 1, length - m - 1, flags);
929 }
930
931 fputs(" ]", f);
932
933 hashmap_remove(h, n);
934 free(kk);
935
936 /* Iterate data fields form the beginning */
937 done = false;
938 separator = true;
939
940 break;
941 }
942 }
943
944 } while (!done);
945
946 if (mode == OUTPUT_JSON_PRETTY)
947 fputs("\n}\n", f);
948 else if (mode == OUTPUT_JSON_SSE)
949 fputs("}\n\n", f);
950 else
951 fputs(" }\n", f);
952
953 r = 0;
954
955 finish:
956 while ((k = hashmap_steal_first_key(h)))
957 free(k);
958
959 hashmap_free(h);
960
961 return r;
962 }
963
964 static int output_cat(
965 FILE *f,
966 sd_journal *j,
967 OutputMode mode,
968 unsigned n_columns,
969 OutputFlags flags,
970 Set *output_fields,
971 size_t highlight[2]) {
972
973 const void *data;
974 size_t l;
975 int r;
976 const char *highlight_on = "", *highlight_off = "";
977
978 assert(j);
979 assert(f);
980
981 if (flags & OUTPUT_COLOR) {
982 highlight_on = ANSI_HIGHLIGHT_RED;
983 highlight_off = ANSI_NORMAL;
984 }
985
986 sd_journal_set_data_threshold(j, 0);
987
988 r = sd_journal_get_data(j, "MESSAGE", &data, &l);
989 if (r == -EBADMSG) {
990 log_debug_errno(r, "Skipping message we can't read: %m");
991 return 0;
992 }
993 if (r < 0) {
994 /* An entry without MESSAGE=? */
995 if (r == -ENOENT)
996 return 0;
997
998 return log_error_errno(r, "Failed to get data: %m");
999 }
1000
1001 assert(l >= 8);
1002
1003 if (highlight && (flags & OUTPUT_COLOR)) {
1004 assert(highlight[0] <= highlight[1]);
1005 assert(highlight[1] <= l - 8);
1006
1007 fwrite((const char*) data + 8, 1, highlight[0], f);
1008 fwrite(highlight_on, 1, strlen(highlight_on), f);
1009 fwrite((const char*) data + 8 + highlight[0], 1, highlight[1] - highlight[0], f);
1010 fwrite(highlight_off, 1, strlen(highlight_off), f);
1011 fwrite((const char*) data + 8 + highlight[1], 1, l - 8 - highlight[1], f);
1012 } else
1013 fwrite((const char*) data + 8, 1, l - 8, f);
1014 fputc('\n', f);
1015
1016 return 0;
1017 }
1018
1019 static int (*output_funcs[_OUTPUT_MODE_MAX])(
1020 FILE *f,
1021 sd_journal*j,
1022 OutputMode mode,
1023 unsigned n_columns,
1024 OutputFlags flags,
1025 Set *output_fields,
1026 size_t highlight[2]) = {
1027
1028 [OUTPUT_SHORT] = output_short,
1029 [OUTPUT_SHORT_ISO] = output_short,
1030 [OUTPUT_SHORT_ISO_PRECISE] = output_short,
1031 [OUTPUT_SHORT_PRECISE] = output_short,
1032 [OUTPUT_SHORT_MONOTONIC] = output_short,
1033 [OUTPUT_SHORT_UNIX] = output_short,
1034 [OUTPUT_SHORT_FULL] = output_short,
1035 [OUTPUT_VERBOSE] = output_verbose,
1036 [OUTPUT_EXPORT] = output_export,
1037 [OUTPUT_JSON] = output_json,
1038 [OUTPUT_JSON_PRETTY] = output_json,
1039 [OUTPUT_JSON_SSE] = output_json,
1040 [OUTPUT_CAT] = output_cat,
1041 [OUTPUT_WITH_UNIT] = output_short,
1042 };
1043
1044 int show_journal_entry(
1045 FILE *f,
1046 sd_journal *j,
1047 OutputMode mode,
1048 unsigned n_columns,
1049 OutputFlags flags,
1050 char **output_fields,
1051 size_t highlight[2],
1052 bool *ellipsized) {
1053
1054 int ret;
1055 _cleanup_set_free_free_ Set *fields = NULL;
1056 assert(mode >= 0);
1057 assert(mode < _OUTPUT_MODE_MAX);
1058
1059 if (n_columns <= 0)
1060 n_columns = columns();
1061
1062 if (output_fields) {
1063 fields = set_new(&string_hash_ops);
1064 if (!fields)
1065 return log_oom();
1066
1067 ret = set_put_strdupv(fields, output_fields);
1068 if (ret < 0)
1069 return ret;
1070 }
1071
1072 ret = output_funcs[mode](f, j, mode, n_columns, flags, fields, highlight);
1073
1074 if (ellipsized && ret > 0)
1075 *ellipsized = true;
1076
1077 return ret;
1078 }
1079
1080 static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
1081 assert(f);
1082 assert(flags);
1083
1084 if (!(*flags & OUTPUT_BEGIN_NEWLINE))
1085 return 0;
1086
1087 /* Print a beginning new line if that's request, but only once
1088 * on the first line we print. */
1089
1090 fputc('\n', f);
1091 *flags &= ~OUTPUT_BEGIN_NEWLINE;
1092 return 0;
1093 }
1094
1095 int show_journal(
1096 FILE *f,
1097 sd_journal *j,
1098 OutputMode mode,
1099 unsigned n_columns,
1100 usec_t not_before,
1101 unsigned how_many,
1102 OutputFlags flags,
1103 bool *ellipsized) {
1104
1105 int r;
1106 unsigned line = 0;
1107 bool need_seek = false;
1108 int warn_cutoff = flags & OUTPUT_WARN_CUTOFF;
1109
1110 assert(j);
1111 assert(mode >= 0);
1112 assert(mode < _OUTPUT_MODE_MAX);
1113
1114 if (how_many == (unsigned) -1)
1115 need_seek = true;
1116 else {
1117 /* Seek to end */
1118 r = sd_journal_seek_tail(j);
1119 if (r < 0)
1120 return log_error_errno(r, "Failed to seek to tail: %m");
1121
1122 r = sd_journal_previous_skip(j, how_many);
1123 if (r < 0)
1124 return log_error_errno(r, "Failed to skip previous: %m");
1125 }
1126
1127 for (;;) {
1128 for (;;) {
1129 usec_t usec;
1130
1131 if (need_seek) {
1132 r = sd_journal_next(j);
1133 if (r < 0)
1134 return log_error_errno(r, "Failed to iterate through journal: %m");
1135 }
1136
1137 if (r == 0)
1138 break;
1139
1140 need_seek = true;
1141
1142 if (not_before > 0) {
1143 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
1144
1145 /* -ESTALE is returned if the
1146 timestamp is not from this boot */
1147 if (r == -ESTALE)
1148 continue;
1149 else if (r < 0)
1150 return log_error_errno(r, "Failed to get journal time: %m");
1151
1152 if (usec < not_before)
1153 continue;
1154 }
1155
1156 line++;
1157 maybe_print_begin_newline(f, &flags);
1158
1159 r = show_journal_entry(f, j, mode, n_columns, flags, NULL, NULL, ellipsized);
1160 if (r < 0)
1161 return r;
1162 }
1163
1164 if (warn_cutoff && line < how_many && not_before > 0) {
1165 sd_id128_t boot_id;
1166 usec_t cutoff = 0;
1167
1168 /* Check whether the cutoff line is too early */
1169
1170 r = sd_id128_get_boot(&boot_id);
1171 if (r < 0)
1172 return log_error_errno(r, "Failed to get boot id: %m");
1173
1174 r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL);
1175 if (r < 0)
1176 return log_error_errno(r, "Failed to get journal cutoff time: %m");
1177
1178 if (r > 0 && not_before < cutoff) {
1179 maybe_print_begin_newline(f, &flags);
1180 fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1181 }
1182
1183 warn_cutoff = false;
1184 }
1185
1186 if (!(flags & OUTPUT_FOLLOW))
1187 break;
1188
1189 r = sd_journal_wait(j, USEC_INFINITY);
1190 if (r < 0)
1191 return log_error_errno(r, "Failed to wait for journal: %m");
1192
1193 }
1194
1195 return 0;
1196 }
1197
1198 int add_matches_for_unit(sd_journal *j, const char *unit) {
1199 const char *m1, *m2, *m3, *m4;
1200 int r;
1201
1202 assert(j);
1203 assert(unit);
1204
1205 m1 = strjoina("_SYSTEMD_UNIT=", unit);
1206 m2 = strjoina("COREDUMP_UNIT=", unit);
1207 m3 = strjoina("UNIT=", unit);
1208 m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);
1209
1210 (void)(
1211 /* Look for messages from the service itself */
1212 (r = sd_journal_add_match(j, m1, 0)) ||
1213
1214 /* Look for coredumps of the service */
1215 (r = sd_journal_add_disjunction(j)) ||
1216 (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1217 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1218 (r = sd_journal_add_match(j, m2, 0)) ||
1219
1220 /* Look for messages from PID 1 about this service */
1221 (r = sd_journal_add_disjunction(j)) ||
1222 (r = sd_journal_add_match(j, "_PID=1", 0)) ||
1223 (r = sd_journal_add_match(j, m3, 0)) ||
1224
1225 /* Look for messages from authorized daemons about this service */
1226 (r = sd_journal_add_disjunction(j)) ||
1227 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1228 (r = sd_journal_add_match(j, m4, 0))
1229 );
1230
1231 if (r == 0 && endswith(unit, ".slice")) {
1232 const char *m5;
1233
1234 m5 = strjoina("_SYSTEMD_SLICE=", unit);
1235
1236 /* Show all messages belonging to a slice */
1237 (void)(
1238 (r = sd_journal_add_disjunction(j)) ||
1239 (r = sd_journal_add_match(j, m5, 0))
1240 );
1241 }
1242
1243 return r;
1244 }
1245
1246 int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
1247 int r;
1248 char *m1, *m2, *m3, *m4;
1249 char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)];
1250
1251 assert(j);
1252 assert(unit);
1253
1254 m1 = strjoina("_SYSTEMD_USER_UNIT=", unit);
1255 m2 = strjoina("USER_UNIT=", unit);
1256 m3 = strjoina("COREDUMP_USER_UNIT=", unit);
1257 m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit);
1258 sprintf(muid, "_UID="UID_FMT, uid);
1259
1260 (void) (
1261 /* Look for messages from the user service itself */
1262 (r = sd_journal_add_match(j, m1, 0)) ||
1263 (r = sd_journal_add_match(j, muid, 0)) ||
1264
1265 /* Look for messages from systemd about this service */
1266 (r = sd_journal_add_disjunction(j)) ||
1267 (r = sd_journal_add_match(j, m2, 0)) ||
1268 (r = sd_journal_add_match(j, muid, 0)) ||
1269
1270 /* Look for coredumps of the service */
1271 (r = sd_journal_add_disjunction(j)) ||
1272 (r = sd_journal_add_match(j, m3, 0)) ||
1273 (r = sd_journal_add_match(j, muid, 0)) ||
1274 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1275
1276 /* Look for messages from authorized daemons about this service */
1277 (r = sd_journal_add_disjunction(j)) ||
1278 (r = sd_journal_add_match(j, m4, 0)) ||
1279 (r = sd_journal_add_match(j, muid, 0)) ||
1280 (r = sd_journal_add_match(j, "_UID=0", 0))
1281 );
1282
1283 if (r == 0 && endswith(unit, ".slice")) {
1284 const char *m5;
1285
1286 m5 = strjoina("_SYSTEMD_SLICE=", unit);
1287
1288 /* Show all messages belonging to a slice */
1289 (void)(
1290 (r = sd_journal_add_disjunction(j)) ||
1291 (r = sd_journal_add_match(j, m5, 0)) ||
1292 (r = sd_journal_add_match(j, muid, 0))
1293 );
1294 }
1295
1296 return r;
1297 }
1298
1299 static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
1300 _cleanup_close_pair_ int pair[2] = { -1, -1 };
1301 _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
1302 pid_t pid, child;
1303 char buf[37];
1304 ssize_t k;
1305 int r;
1306
1307 assert(machine);
1308 assert(boot_id);
1309
1310 if (!machine_name_is_valid(machine))
1311 return -EINVAL;
1312
1313 r = container_get_leader(machine, &pid);
1314 if (r < 0)
1315 return r;
1316
1317 r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, NULL, &rootfd);
1318 if (r < 0)
1319 return r;
1320
1321 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
1322 return -errno;
1323
1324 r = safe_fork("(sd-bootid)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
1325 if (r < 0)
1326 return r;
1327 if (r == 0) {
1328 int fd;
1329
1330 pair[0] = safe_close(pair[0]);
1331
1332 r = namespace_enter(pidnsfd, mntnsfd, -1, -1, rootfd);
1333 if (r < 0)
1334 _exit(EXIT_FAILURE);
1335
1336 fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
1337 if (fd < 0)
1338 _exit(EXIT_FAILURE);
1339
1340 r = loop_read_exact(fd, buf, 36, false);
1341 safe_close(fd);
1342 if (r < 0)
1343 _exit(EXIT_FAILURE);
1344
1345 k = send(pair[1], buf, 36, MSG_NOSIGNAL);
1346 if (k != 36)
1347 _exit(EXIT_FAILURE);
1348
1349 _exit(EXIT_SUCCESS);
1350 }
1351
1352 pair[1] = safe_close(pair[1]);
1353
1354 r = wait_for_terminate_and_check("(sd-bootid)", child, 0);
1355 if (r < 0)
1356 return r;
1357 if (r != EXIT_SUCCESS)
1358 return -EIO;
1359
1360 k = recv(pair[0], buf, 36, 0);
1361 if (k != 36)
1362 return -EIO;
1363
1364 buf[36] = 0;
1365 r = sd_id128_from_string(buf, boot_id);
1366 if (r < 0)
1367 return r;
1368
1369 return 0;
1370 }
1371
1372 int add_match_this_boot(sd_journal *j, const char *machine) {
1373 char match[9+32+1] = "_BOOT_ID=";
1374 sd_id128_t boot_id;
1375 int r;
1376
1377 assert(j);
1378
1379 if (machine) {
1380 r = get_boot_id_for_machine(machine, &boot_id);
1381 if (r < 0)
1382 return log_error_errno(r, "Failed to get boot id of container %s: %m", machine);
1383 } else {
1384 r = sd_id128_get_boot(&boot_id);
1385 if (r < 0)
1386 return log_error_errno(r, "Failed to get boot id: %m");
1387 }
1388
1389 sd_id128_to_string(boot_id, match + 9);
1390 r = sd_journal_add_match(j, match, strlen(match));
1391 if (r < 0)
1392 return log_error_errno(r, "Failed to add match: %m");
1393
1394 r = sd_journal_add_conjunction(j);
1395 if (r < 0)
1396 return log_error_errno(r, "Failed to add conjunction: %m");
1397
1398 return 0;
1399 }
1400
1401 int show_journal_by_unit(
1402 FILE *f,
1403 const char *unit,
1404 OutputMode mode,
1405 unsigned n_columns,
1406 usec_t not_before,
1407 unsigned how_many,
1408 uid_t uid,
1409 OutputFlags flags,
1410 int journal_open_flags,
1411 bool system_unit,
1412 bool *ellipsized) {
1413
1414 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1415 int r;
1416
1417 assert(mode >= 0);
1418 assert(mode < _OUTPUT_MODE_MAX);
1419 assert(unit);
1420
1421 if (how_many <= 0)
1422 return 0;
1423
1424 r = sd_journal_open(&j, journal_open_flags);
1425 if (r < 0)
1426 return log_error_errno(r, "Failed to open journal: %m");
1427
1428 r = add_match_this_boot(j, NULL);
1429 if (r < 0)
1430 return r;
1431
1432 if (system_unit)
1433 r = add_matches_for_unit(j, unit);
1434 else
1435 r = add_matches_for_user_unit(j, unit, uid);
1436 if (r < 0)
1437 return log_error_errno(r, "Failed to add unit matches: %m");
1438
1439 if (DEBUG_LOGGING) {
1440 _cleanup_free_ char *filter;
1441
1442 filter = journal_make_match_string(j);
1443 if (!filter)
1444 return log_oom();
1445
1446 log_debug("Journal filter: %s", filter);
1447 }
1448
1449 return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized);
1450 }