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