]>
Commit | Line | Data |
---|---|---|
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 |
62 | static 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 |
82 | static 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 |
114 | static 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 | 129 | static 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 |
210 | static 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 | ||
230 | static 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 { | |
267 | gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r; | |
268 | t = (time_t) (x / USEC_PER_SEC); | |
269 | ||
270 | switch (mode) { | |
271 | ||
272 | case OUTPUT_SHORT_UNIX: | |
70887c5f | 273 | xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, x % USEC_PER_SEC); |
29a753df LP |
274 | break; |
275 | ||
276 | case OUTPUT_SHORT_ISO: | |
277 | if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm)) <= 0) { | |
278 | log_error("Failed for format ISO time"); | |
279 | return -EINVAL; | |
280 | } | |
281 | break; | |
282 | ||
283 | case OUTPUT_SHORT: | |
284 | case OUTPUT_SHORT_PRECISE: | |
285 | ||
286 | if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm)) <= 0) { | |
287 | log_error("Failed to format syslog time"); | |
288 | return -EINVAL; | |
289 | } | |
290 | ||
291 | if (mode == OUTPUT_SHORT_PRECISE) { | |
292 | size_t k; | |
293 | ||
294 | assert(sizeof(buf) > strlen(buf)); | |
295 | k = sizeof(buf) - strlen(buf); | |
296 | ||
70887c5f | 297 | r = snprintf(buf + strlen(buf), k, ".%06"PRIu64, x % USEC_PER_SEC); |
29a753df LP |
298 | if (r <= 0 || (size_t) r >= k) { /* too long? */ |
299 | log_error("Failed to format precise time"); | |
300 | return -EINVAL; | |
301 | } | |
302 | } | |
303 | break; | |
304 | ||
305 | default: | |
306 | assert_not_reached("Unknown time format"); | |
307 | } | |
308 | } | |
309 | ||
310 | fputs(buf, f); | |
311 | return (int) strlen(buf); | |
312 | } | |
313 | ||
08ace05b LP |
314 | static int output_short( |
315 | FILE *f, | |
316 | sd_journal *j, | |
317 | OutputMode mode, | |
318 | unsigned n_columns, | |
319 | OutputFlags flags) { | |
320 | ||
86aa7ba4 | 321 | int r; |
86aa7ba4 LP |
322 | const void *data; |
323 | size_t length; | |
324 | size_t n = 0; | |
08ace05b | 325 | _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL, *priority = NULL; |
49826187 LP |
326 | 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; |
327 | int p = LOG_INFO; | |
94e0bd7d | 328 | bool ellipsized = false; |
86aa7ba4 | 329 | |
08ace05b | 330 | assert(f); |
86aa7ba4 LP |
331 | assert(j); |
332 | ||
a6f0104a | 333 | /* Set the threshold to one bigger than the actual print |
69ab8088 | 334 | * threshold, so that if the line is actually longer than what |
a6f0104a ZJS |
335 | * we're willing to print, ellipsization will occur. This way |
336 | * we won't output a misleading line without any indication of | |
337 | * truncation. | |
338 | */ | |
339 | sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1); | |
93b73b06 | 340 | |
a72b6353 | 341 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
55d7bfc1 | 342 | |
49826187 LP |
343 | r = parse_field(data, length, "PRIORITY=", &priority, &priority_len); |
344 | if (r < 0) | |
08ace05b | 345 | return r; |
49826187 LP |
346 | else if (r > 0) |
347 | continue; | |
348 | ||
55d7bfc1 LP |
349 | r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len); |
350 | if (r < 0) | |
08ace05b | 351 | return r; |
55d7bfc1 LP |
352 | else if (r > 0) |
353 | continue; | |
354 | ||
4cd9a9d9 | 355 | r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len); |
55d7bfc1 | 356 | if (r < 0) |
08ace05b | 357 | return r; |
55d7bfc1 LP |
358 | else if (r > 0) |
359 | continue; | |
360 | ||
361 | r = parse_field(data, length, "_COMM=", &comm, &comm_len); | |
362 | if (r < 0) | |
08ace05b | 363 | return r; |
55d7bfc1 LP |
364 | else if (r > 0) |
365 | continue; | |
366 | ||
367 | r = parse_field(data, length, "_PID=", &pid, &pid_len); | |
368 | if (r < 0) | |
08ace05b | 369 | return r; |
55d7bfc1 LP |
370 | else if (r > 0) |
371 | continue; | |
372 | ||
6c1e6b98 LP |
373 | r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len); |
374 | if (r < 0) | |
08ace05b | 375 | return r; |
6c1e6b98 LP |
376 | else if (r > 0) |
377 | continue; | |
378 | ||
bf967366 LP |
379 | r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len); |
380 | if (r < 0) | |
08ace05b | 381 | return r; |
bf967366 LP |
382 | else if (r > 0) |
383 | continue; | |
384 | ||
385 | r = parse_field(data, length, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len); | |
386 | if (r < 0) | |
08ace05b | 387 | return r; |
bf967366 LP |
388 | else if (r > 0) |
389 | continue; | |
390 | ||
55d7bfc1 LP |
391 | r = parse_field(data, length, "MESSAGE=", &message, &message_len); |
392 | if (r < 0) | |
08ace05b | 393 | return r; |
55d7bfc1 | 394 | } |
d00f1d57 LP |
395 | if (r == -EBADMSG) { |
396 | log_debug_errno(r, "Skipping message we can't read: %m"); | |
397 | return 0; | |
398 | } | |
a72b6353 | 399 | if (r < 0) |
b56d608e | 400 | return log_error_errno(r, "Failed to get journal fields: %m"); |
a72b6353 | 401 | |
07d21025 LP |
402 | if (!message) { |
403 | log_debug("Skipping message without MESSAGE= field."); | |
08ace05b | 404 | return 0; |
07d21025 | 405 | } |
55d7bfc1 | 406 | |
e8bc0ea2 LP |
407 | if (!(flags & OUTPUT_SHOW_ALL)) |
408 | strip_tab_ansi(&message, &message_len); | |
409 | ||
49826187 LP |
410 | if (priority_len == 1 && *priority >= '0' && *priority <= '7') |
411 | p = *priority - '0'; | |
412 | ||
29a753df LP |
413 | if (mode == OUTPUT_SHORT_MONOTONIC) |
414 | r = output_timestamp_monotonic(f, j, monotonic); | |
415 | else | |
416 | r = output_timestamp_realtime(f, j, mode, flags, realtime); | |
417 | if (r < 0) | |
418 | return r; | |
419 | n += r; | |
86aa7ba4 | 420 | |
29a753df | 421 | if (flags & OUTPUT_NO_HOSTNAME) { |
991e274b | 422 | /* Suppress display of the hostname if this is requested. */ |
12104159 | 423 | hostname = mfree(hostname); |
991e274b LP |
424 | hostname_len = 0; |
425 | } | |
426 | ||
08ace05b LP |
427 | if (hostname && shall_print(hostname, hostname_len, flags)) { |
428 | fprintf(f, " %.*s", (int) hostname_len, hostname); | |
55d7bfc1 LP |
429 | n += hostname_len + 1; |
430 | } | |
431 | ||
08ace05b LP |
432 | if (identifier && shall_print(identifier, identifier_len, flags)) { |
433 | fprintf(f, " %.*s", (int) identifier_len, identifier); | |
4cd9a9d9 | 434 | n += identifier_len + 1; |
08ace05b LP |
435 | } else if (comm && shall_print(comm, comm_len, flags)) { |
436 | fprintf(f, " %.*s", (int) comm_len, comm); | |
55d7bfc1 | 437 | n += comm_len + 1; |
b5936820 | 438 | } else |
1248e840 | 439 | fputs(" unknown", f); |
86aa7ba4 | 440 | |
08ace05b LP |
441 | if (pid && shall_print(pid, pid_len, flags)) { |
442 | fprintf(f, "[%.*s]", (int) pid_len, pid); | |
55d7bfc1 | 443 | n += pid_len + 2; |
08ace05b LP |
444 | } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) { |
445 | fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid); | |
6c1e6b98 | 446 | n += fake_pid_len + 2; |
86aa7ba4 LP |
447 | } |
448 | ||
31f7bf19 | 449 | if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) { |
e6acda19 | 450 | char bytes[FORMAT_BYTES_MAX]; |
08ace05b | 451 | fprintf(f, ": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len)); |
31f7bf19 ZJS |
452 | } else { |
453 | fputs(": ", f); | |
94e0bd7d ZJS |
454 | ellipsized |= |
455 | print_multiline(f, n + 2, n_columns, flags, p, message, message_len); | |
31f7bf19 | 456 | } |
55d7bfc1 | 457 | |
d4205751 LP |
458 | if (flags & OUTPUT_CATALOG) |
459 | print_catalog(f, j); | |
460 | ||
94e0bd7d | 461 | return ellipsized; |
86aa7ba4 LP |
462 | } |
463 | ||
08ace05b LP |
464 | static int output_verbose( |
465 | FILE *f, | |
466 | sd_journal *j, | |
467 | OutputMode mode, | |
468 | unsigned n_columns, | |
469 | OutputFlags flags) { | |
470 | ||
86aa7ba4 LP |
471 | const void *data; |
472 | size_t length; | |
7fd1b19b | 473 | _cleanup_free_ char *cursor = NULL; |
d813d7a3 | 474 | uint64_t realtime = 0; |
f02d8367 | 475 | char ts[FORMAT_TIMESTAMP_MAX + 7]; |
86aa7ba4 LP |
476 | int r; |
477 | ||
08ace05b | 478 | assert(f); |
86aa7ba4 LP |
479 | assert(j); |
480 | ||
93b73b06 LP |
481 | sd_journal_set_data_threshold(j, 0); |
482 | ||
cf40f0be ZJS |
483 | r = sd_journal_get_data(j, "_SOURCE_REALTIME_TIMESTAMP", &data, &length); |
484 | if (r == -ENOENT) | |
485 | log_debug("Source realtime timestamp not found"); | |
b56d608e LP |
486 | else if (r < 0) |
487 | return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get source realtime timestamp: %m"); | |
488 | else { | |
cf40f0be | 489 | _cleanup_free_ char *value = NULL; |
cf40f0be | 490 | |
d813d7a3 | 491 | r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &value, NULL); |
cf40f0be | 492 | if (r < 0) |
e64c53fd | 493 | return r; |
d813d7a3 LP |
494 | assert(r > 0); |
495 | ||
496 | r = safe_atou64(value, &realtime); | |
497 | if (r < 0) | |
498 | log_debug_errno(r, "Failed to parse realtime timestamp: %m"); | |
cf40f0be ZJS |
499 | } |
500 | ||
501 | if (r < 0) { | |
502 | r = sd_journal_get_realtime_usec(j, &realtime); | |
b56d608e LP |
503 | if (r < 0) |
504 | return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get realtime timestamp: %m"); | |
86aa7ba4 LP |
505 | } |
506 | ||
507 | r = sd_journal_get_cursor(j, &cursor); | |
f647962d MS |
508 | if (r < 0) |
509 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 510 | |
08ace05b | 511 | fprintf(f, "%s [%s]\n", |
5ab99e07 LP |
512 | flags & OUTPUT_UTC ? |
513 | format_timestamp_us_utc(ts, sizeof(ts), realtime) : | |
514 | format_timestamp_us(ts, sizeof(ts), realtime), | |
08ace05b | 515 | cursor); |
86aa7ba4 | 516 | |
a72b6353 | 517 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
31f7bf19 ZJS |
518 | const char *c; |
519 | int fieldlen; | |
7ac4fa7e ZJS |
520 | const char *on = "", *off = ""; |
521 | ||
31f7bf19 ZJS |
522 | c = memchr(data, '=', length); |
523 | if (!c) { | |
524 | log_error("Invalid field."); | |
525 | return -EINVAL; | |
526 | } | |
527 | fieldlen = c - (const char*) data; | |
86aa7ba4 | 528 | |
7ac4fa7e | 529 | if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) { |
1fc464f6 LP |
530 | on = ANSI_HIGHLIGHT; |
531 | off = ANSI_NORMAL; | |
7ac4fa7e ZJS |
532 | } |
533 | ||
8980058a | 534 | if ((flags & OUTPUT_SHOW_ALL) || |
a6f0104a ZJS |
535 | (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH) |
536 | && utf8_is_printable(data, length))) { | |
7ac4fa7e | 537 | fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data); |
31f7bf19 | 538 | print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1); |
7ac4fa7e | 539 | fputs(off, f); |
31f7bf19 ZJS |
540 | } else { |
541 | char bytes[FORMAT_BYTES_MAX]; | |
86aa7ba4 | 542 | |
7ac4fa7e ZJS |
543 | fprintf(f, " %s%.*s=[%s blob data]%s\n", |
544 | on, | |
31f7bf19 ZJS |
545 | (int) (c - (const char*) data), |
546 | (const char*) data, | |
7ac4fa7e ZJS |
547 | format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1), |
548 | off); | |
31f7bf19 | 549 | } |
86aa7ba4 LP |
550 | } |
551 | ||
a72b6353 ZJS |
552 | if (r < 0) |
553 | return r; | |
554 | ||
d4205751 LP |
555 | if (flags & OUTPUT_CATALOG) |
556 | print_catalog(f, j); | |
557 | ||
86aa7ba4 LP |
558 | return 0; |
559 | } | |
560 | ||
08ace05b LP |
561 | static int output_export( |
562 | FILE *f, | |
563 | sd_journal *j, | |
564 | OutputMode mode, | |
565 | unsigned n_columns, | |
566 | OutputFlags flags) { | |
567 | ||
86aa7ba4 LP |
568 | sd_id128_t boot_id; |
569 | char sid[33]; | |
570 | int r; | |
571 | usec_t realtime, monotonic; | |
7fd1b19b | 572 | _cleanup_free_ char *cursor = NULL; |
86aa7ba4 LP |
573 | const void *data; |
574 | size_t length; | |
575 | ||
576 | assert(j); | |
577 | ||
93b73b06 LP |
578 | sd_journal_set_data_threshold(j, 0); |
579 | ||
86aa7ba4 | 580 | r = sd_journal_get_realtime_usec(j, &realtime); |
f647962d MS |
581 | if (r < 0) |
582 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
86aa7ba4 LP |
583 | |
584 | r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id); | |
f647962d MS |
585 | if (r < 0) |
586 | return log_error_errno(r, "Failed to get monotonic timestamp: %m"); | |
86aa7ba4 LP |
587 | |
588 | r = sd_journal_get_cursor(j, &cursor); | |
f647962d MS |
589 | if (r < 0) |
590 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 591 | |
08ace05b LP |
592 | fprintf(f, |
593 | "__CURSOR=%s\n" | |
de0671ee ZJS |
594 | "__REALTIME_TIMESTAMP="USEC_FMT"\n" |
595 | "__MONOTONIC_TIMESTAMP="USEC_FMT"\n" | |
08ace05b LP |
596 | "_BOOT_ID=%s\n", |
597 | cursor, | |
de0671ee ZJS |
598 | realtime, |
599 | monotonic, | |
08ace05b | 600 | sd_id128_to_string(boot_id, sid)); |
86aa7ba4 | 601 | |
a72b6353 | 602 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
86aa7ba4 | 603 | |
112301ae LP |
604 | /* We already printed the boot id, from the data in |
605 | * the header, hence let's suppress it here */ | |
606 | if (length >= 9 && | |
2a0e0692 | 607 | startswith(data, "_BOOT_ID=")) |
112301ae LP |
608 | continue; |
609 | ||
0ade5ffe ZJS |
610 | if (utf8_is_printable_newline(data, length, false)) |
611 | fwrite(data, length, 1, f); | |
612 | else { | |
86aa7ba4 LP |
613 | const char *c; |
614 | uint64_t le64; | |
615 | ||
616 | c = memchr(data, '=', length); | |
617 | if (!c) { | |
618 | log_error("Invalid field."); | |
619 | return -EINVAL; | |
620 | } | |
621 | ||
08ace05b LP |
622 | fwrite(data, c - (const char*) data, 1, f); |
623 | fputc('\n', f); | |
86aa7ba4 | 624 | le64 = htole64(length - (c - (const char*) data) - 1); |
08ace05b LP |
625 | fwrite(&le64, sizeof(le64), 1, f); |
626 | fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f); | |
0ade5ffe | 627 | } |
86aa7ba4 | 628 | |
08ace05b | 629 | fputc('\n', f); |
86aa7ba4 LP |
630 | } |
631 | ||
a72b6353 ZJS |
632 | if (r < 0) |
633 | return r; | |
634 | ||
08ace05b | 635 | fputc('\n', f); |
86aa7ba4 LP |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
240a5fe8 | 640 | void json_escape( |
08ace05b LP |
641 | FILE *f, |
642 | const char* p, | |
643 | size_t l, | |
644 | OutputFlags flags) { | |
645 | ||
646 | assert(f); | |
647 | assert(p); | |
648 | ||
93b73b06 | 649 | if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD) |
08ace05b LP |
650 | fputs("null", f); |
651 | ||
8980058a | 652 | else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) { |
86aa7ba4 LP |
653 | bool not_first = false; |
654 | ||
08ace05b | 655 | fputs("[ ", f); |
86aa7ba4 LP |
656 | |
657 | while (l > 0) { | |
658 | if (not_first) | |
08ace05b | 659 | fprintf(f, ", %u", (uint8_t) *p); |
86aa7ba4 LP |
660 | else { |
661 | not_first = true; | |
08ace05b | 662 | fprintf(f, "%u", (uint8_t) *p); |
86aa7ba4 LP |
663 | } |
664 | ||
665 | p++; | |
666 | l--; | |
667 | } | |
668 | ||
08ace05b | 669 | fputs(" ]", f); |
86aa7ba4 | 670 | } else { |
08ace05b | 671 | fputc('\"', f); |
86aa7ba4 LP |
672 | |
673 | while (l > 0) { | |
674 | if (*p == '"' || *p == '\\') { | |
08ace05b LP |
675 | fputc('\\', f); |
676 | fputc(*p, f); | |
31f7bf19 ZJS |
677 | } else if (*p == '\n') |
678 | fputs("\\n", f); | |
91a8a108 DM |
679 | else if ((uint8_t) *p < ' ') |
680 | fprintf(f, "\\u%04x", (uint8_t) *p); | |
08ace05b LP |
681 | else |
682 | fputc(*p, f); | |
86aa7ba4 LP |
683 | |
684 | p++; | |
685 | l--; | |
686 | } | |
687 | ||
08ace05b | 688 | fputc('\"', f); |
86aa7ba4 LP |
689 | } |
690 | } | |
691 | ||
08ace05b LP |
692 | static int output_json( |
693 | FILE *f, | |
694 | sd_journal *j, | |
695 | OutputMode mode, | |
696 | unsigned n_columns, | |
697 | OutputFlags flags) { | |
698 | ||
86aa7ba4 | 699 | uint64_t realtime, monotonic; |
7fd1b19b | 700 | _cleanup_free_ char *cursor = NULL; |
86aa7ba4 LP |
701 | const void *data; |
702 | size_t length; | |
703 | sd_id128_t boot_id; | |
2e729834 | 704 | char sid[33], *k; |
86aa7ba4 | 705 | int r; |
d99ae53a LP |
706 | Hashmap *h = NULL; |
707 | bool done, separator; | |
86aa7ba4 LP |
708 | |
709 | assert(j); | |
710 | ||
93b73b06 LP |
711 | sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD); |
712 | ||
86aa7ba4 | 713 | r = sd_journal_get_realtime_usec(j, &realtime); |
f647962d MS |
714 | if (r < 0) |
715 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
86aa7ba4 LP |
716 | |
717 | r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id); | |
f647962d MS |
718 | if (r < 0) |
719 | return log_error_errno(r, "Failed to get monotonic timestamp: %m"); | |
86aa7ba4 LP |
720 | |
721 | r = sd_journal_get_cursor(j, &cursor); | |
f647962d MS |
722 | if (r < 0) |
723 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 724 | |
a6e87e90 | 725 | if (mode == OUTPUT_JSON_PRETTY) |
08ace05b LP |
726 | fprintf(f, |
727 | "{\n" | |
728 | "\t\"__CURSOR\" : \"%s\",\n" | |
de0671ee ZJS |
729 | "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\",\n" |
730 | "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\",\n" | |
08ace05b LP |
731 | "\t\"_BOOT_ID\" : \"%s\"", |
732 | cursor, | |
de0671ee ZJS |
733 | realtime, |
734 | monotonic, | |
08ace05b | 735 | sd_id128_to_string(boot_id, sid)); |
48383c25 LP |
736 | else { |
737 | if (mode == OUTPUT_JSON_SSE) | |
738 | fputs("data: ", f); | |
739 | ||
08ace05b LP |
740 | fprintf(f, |
741 | "{ \"__CURSOR\" : \"%s\", " | |
de0671ee ZJS |
742 | "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT"\", " |
743 | "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT"\", " | |
08ace05b LP |
744 | "\"_BOOT_ID\" : \"%s\"", |
745 | cursor, | |
de0671ee ZJS |
746 | realtime, |
747 | monotonic, | |
08ace05b | 748 | sd_id128_to_string(boot_id, sid)); |
48383c25 | 749 | } |
86aa7ba4 | 750 | |
d5099efc | 751 | h = hashmap_new(&string_hash_ops); |
d99ae53a | 752 | if (!h) |
b56d608e | 753 | return log_oom(); |
d99ae53a LP |
754 | |
755 | /* First round, iterate through the entry and count how often each field appears */ | |
a72b6353 | 756 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
d99ae53a LP |
757 | const char *eq; |
758 | char *n; | |
759 | unsigned u; | |
86aa7ba4 | 760 | |
112301ae LP |
761 | if (length >= 9 && |
762 | memcmp(data, "_BOOT_ID=", 9) == 0) | |
763 | continue; | |
764 | ||
d99ae53a LP |
765 | eq = memchr(data, '=', length); |
766 | if (!eq) | |
767 | continue; | |
86aa7ba4 | 768 | |
d99ae53a LP |
769 | n = strndup(data, eq - (const char*) data); |
770 | if (!n) { | |
b56d608e | 771 | r = log_oom(); |
d99ae53a LP |
772 | goto finish; |
773 | } | |
a6e87e90 | 774 | |
d99ae53a LP |
775 | u = PTR_TO_UINT(hashmap_get(h, n)); |
776 | if (u == 0) { | |
777 | r = hashmap_put(h, n, UINT_TO_PTR(1)); | |
778 | if (r < 0) { | |
779 | free(n); | |
b56d608e | 780 | log_oom(); |
d99ae53a LP |
781 | goto finish; |
782 | } | |
783 | } else { | |
784 | r = hashmap_update(h, n, UINT_TO_PTR(u + 1)); | |
785 | free(n); | |
b56d608e LP |
786 | if (r < 0) { |
787 | log_oom(); | |
d99ae53a | 788 | goto finish; |
b56d608e | 789 | } |
d99ae53a | 790 | } |
86aa7ba4 LP |
791 | } |
792 | ||
a72b6353 ZJS |
793 | if (r < 0) |
794 | return r; | |
795 | ||
d99ae53a LP |
796 | separator = true; |
797 | do { | |
798 | done = true; | |
799 | ||
800 | SD_JOURNAL_FOREACH_DATA(j, data, length) { | |
801 | const char *eq; | |
802 | char *kk, *n; | |
803 | size_t m; | |
804 | unsigned u; | |
805 | ||
806 | /* We already printed the boot id, from the data in | |
807 | * the header, hence let's suppress it here */ | |
808 | if (length >= 9 && | |
809 | memcmp(data, "_BOOT_ID=", 9) == 0) | |
810 | continue; | |
811 | ||
812 | eq = memchr(data, '=', length); | |
813 | if (!eq) | |
814 | continue; | |
815 | ||
816 | if (separator) { | |
817 | if (mode == OUTPUT_JSON_PRETTY) | |
818 | fputs(",\n\t", f); | |
819 | else | |
820 | fputs(", ", f); | |
821 | } | |
822 | ||
823 | m = eq - (const char*) data; | |
824 | ||
825 | n = strndup(data, m); | |
826 | if (!n) { | |
b56d608e | 827 | r = log_oom(); |
d99ae53a LP |
828 | goto finish; |
829 | } | |
830 | ||
831 | u = PTR_TO_UINT(hashmap_get2(h, n, (void**) &kk)); | |
832 | if (u == 0) { | |
833 | /* We already printed this, let's jump to the next */ | |
834 | free(n); | |
835 | separator = false; | |
836 | ||
837 | continue; | |
838 | } else if (u == 1) { | |
839 | /* Field only appears once, output it directly */ | |
840 | ||
841 | json_escape(f, data, m, flags); | |
842 | fputs(" : ", f); | |
843 | ||
844 | json_escape(f, eq + 1, length - m - 1, flags); | |
845 | ||
846 | hashmap_remove(h, n); | |
847 | free(kk); | |
848 | free(n); | |
849 | ||
850 | separator = true; | |
851 | ||
852 | continue; | |
853 | ||
854 | } else { | |
855 | /* Field appears multiple times, output it as array */ | |
856 | json_escape(f, data, m, flags); | |
857 | fputs(" : [ ", f); | |
858 | json_escape(f, eq + 1, length - m - 1, flags); | |
859 | ||
860 | /* Iterate through the end of the list */ | |
861 | ||
862 | while (sd_journal_enumerate_data(j, &data, &length) > 0) { | |
863 | if (length < m + 1) | |
864 | continue; | |
865 | ||
866 | if (memcmp(data, n, m) != 0) | |
867 | continue; | |
868 | ||
869 | if (((const char*) data)[m] != '=') | |
870 | continue; | |
871 | ||
872 | fputs(", ", f); | |
873 | json_escape(f, (const char*) data + m + 1, length - m - 1, flags); | |
874 | } | |
875 | ||
876 | fputs(" ]", f); | |
877 | ||
878 | hashmap_remove(h, n); | |
879 | free(kk); | |
880 | free(n); | |
881 | ||
882 | /* Iterate data fields form the beginning */ | |
883 | done = false; | |
884 | separator = true; | |
885 | ||
886 | break; | |
887 | } | |
888 | } | |
889 | ||
890 | } while (!done); | |
891 | ||
a6e87e90 | 892 | if (mode == OUTPUT_JSON_PRETTY) |
08ace05b | 893 | fputs("\n}\n", f); |
48383c25 LP |
894 | else if (mode == OUTPUT_JSON_SSE) |
895 | fputs("}\n\n", f); | |
a6e87e90 | 896 | else |
08ace05b | 897 | fputs(" }\n", f); |
86aa7ba4 | 898 | |
d99ae53a LP |
899 | r = 0; |
900 | ||
901 | finish: | |
902 | while ((k = hashmap_steal_first_key(h))) | |
903 | free(k); | |
904 | ||
905 | hashmap_free(h); | |
906 | ||
907 | return r; | |
86aa7ba4 LP |
908 | } |
909 | ||
08ace05b LP |
910 | static int output_cat( |
911 | FILE *f, | |
912 | sd_journal *j, | |
913 | OutputMode mode, | |
914 | unsigned n_columns, | |
915 | OutputFlags flags) { | |
916 | ||
d3f2bdbf LP |
917 | const void *data; |
918 | size_t l; | |
919 | int r; | |
920 | ||
921 | assert(j); | |
08ace05b | 922 | assert(f); |
d3f2bdbf | 923 | |
93b73b06 LP |
924 | sd_journal_set_data_threshold(j, 0); |
925 | ||
d3f2bdbf LP |
926 | r = sd_journal_get_data(j, "MESSAGE", &data, &l); |
927 | if (r < 0) { | |
c198300f LP |
928 | /* An entry without MESSAGE=? */ |
929 | if (r == -ENOENT) | |
930 | return 0; | |
931 | ||
8d3d7072 | 932 | return log_error_errno(r, "Failed to get data: %m"); |
d3f2bdbf LP |
933 | } |
934 | ||
935 | assert(l >= 8); | |
936 | ||
08ace05b LP |
937 | fwrite((const char*) data + 8, 1, l - 8, f); |
938 | fputc('\n', f); | |
d3f2bdbf LP |
939 | |
940 | return 0; | |
941 | } | |
942 | ||
08ace05b LP |
943 | static int (*output_funcs[_OUTPUT_MODE_MAX])( |
944 | FILE *f, | |
945 | sd_journal*j, | |
946 | OutputMode mode, | |
947 | unsigned n_columns, | |
948 | OutputFlags flags) = { | |
949 | ||
a6e87e90 | 950 | [OUTPUT_SHORT] = output_short, |
44bc6e1f | 951 | [OUTPUT_SHORT_ISO] = output_short, |
f02d8367 ZJS |
952 | [OUTPUT_SHORT_PRECISE] = output_short, |
953 | [OUTPUT_SHORT_MONOTONIC] = output_short, | |
bb321ed9 | 954 | [OUTPUT_SHORT_UNIX] = output_short, |
29a753df | 955 | [OUTPUT_SHORT_FULL] = output_short, |
86aa7ba4 LP |
956 | [OUTPUT_VERBOSE] = output_verbose, |
957 | [OUTPUT_EXPORT] = output_export, | |
d3f2bdbf | 958 | [OUTPUT_JSON] = output_json, |
a6e87e90 | 959 | [OUTPUT_JSON_PRETTY] = output_json, |
48383c25 | 960 | [OUTPUT_JSON_SSE] = output_json, |
d3f2bdbf | 961 | [OUTPUT_CAT] = output_cat |
86aa7ba4 LP |
962 | }; |
963 | ||
08ace05b LP |
964 | int output_journal( |
965 | FILE *f, | |
966 | sd_journal *j, | |
967 | OutputMode mode, | |
968 | unsigned n_columns, | |
94e0bd7d ZJS |
969 | OutputFlags flags, |
970 | bool *ellipsized) { | |
08ace05b | 971 | |
e268b81e | 972 | int ret; |
df50185b | 973 | assert(mode >= 0); |
86aa7ba4 LP |
974 | assert(mode < _OUTPUT_MODE_MAX); |
975 | ||
34a35ece LP |
976 | if (n_columns <= 0) |
977 | n_columns = columns(); | |
978 | ||
08ace05b | 979 | ret = output_funcs[mode](f, j, mode, n_columns, flags); |
e268b81e | 980 | fflush(stdout); |
94e0bd7d ZJS |
981 | |
982 | if (ellipsized && ret > 0) | |
983 | *ellipsized = true; | |
984 | ||
e268b81e | 985 | return ret; |
86aa7ba4 LP |
986 | } |
987 | ||
ea6c2dd1 LP |
988 | static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) { |
989 | assert(f); | |
990 | assert(flags); | |
991 | ||
992 | if (!(*flags & OUTPUT_BEGIN_NEWLINE)) | |
993 | return 0; | |
994 | ||
995 | /* Print a beginning new line if that's request, but only once | |
996 | * on the first line we print. */ | |
997 | ||
998 | fputc('\n', f); | |
999 | *flags &= ~OUTPUT_BEGIN_NEWLINE; | |
1000 | return 0; | |
1001 | } | |
1002 | ||
1a6c43e9 MT |
1003 | static int show_journal(FILE *f, |
1004 | sd_journal *j, | |
1005 | OutputMode mode, | |
1006 | unsigned n_columns, | |
1007 | usec_t not_before, | |
1008 | unsigned how_many, | |
94e0bd7d ZJS |
1009 | OutputFlags flags, |
1010 | bool *ellipsized) { | |
86aa7ba4 | 1011 | |
86aa7ba4 | 1012 | int r; |
df50185b LP |
1013 | unsigned line = 0; |
1014 | bool need_seek = false; | |
085d7120 | 1015 | int warn_cutoff = flags & OUTPUT_WARN_CUTOFF; |
86aa7ba4 | 1016 | |
1a6c43e9 | 1017 | assert(j); |
df50185b LP |
1018 | assert(mode >= 0); |
1019 | assert(mode < _OUTPUT_MODE_MAX); | |
1946b0bd LP |
1020 | |
1021 | /* Seek to end */ | |
86aa7ba4 LP |
1022 | r = sd_journal_seek_tail(j); |
1023 | if (r < 0) | |
b56d608e | 1024 | return log_error_errno(r, "Failed to seek to tail: %m"); |
86aa7ba4 | 1025 | |
df50185b LP |
1026 | r = sd_journal_previous_skip(j, how_many); |
1027 | if (r < 0) | |
b56d608e | 1028 | return log_error_errno(r, "Failed to skip previous: %m"); |
86aa7ba4 | 1029 | |
df50185b LP |
1030 | for (;;) { |
1031 | for (;;) { | |
1032 | usec_t usec; | |
1033 | ||
1034 | if (need_seek) { | |
1035 | r = sd_journal_next(j); | |
1036 | if (r < 0) | |
b56d608e | 1037 | return log_error_errno(r, "Failed to iterate through journal: %m"); |
df50185b LP |
1038 | } |
1039 | ||
1040 | if (r == 0) | |
1041 | break; | |
86aa7ba4 | 1042 | |
df50185b LP |
1043 | need_seek = true; |
1044 | ||
1045 | if (not_before > 0) { | |
1046 | r = sd_journal_get_monotonic_usec(j, &usec, NULL); | |
1047 | ||
1048 | /* -ESTALE is returned if the | |
1049 | timestamp is not from this boot */ | |
1050 | if (r == -ESTALE) | |
1051 | continue; | |
1052 | else if (r < 0) | |
b56d608e | 1053 | return log_error_errno(r, "Failed to get journal time: %m"); |
df50185b LP |
1054 | |
1055 | if (usec < not_before) | |
1056 | continue; | |
1057 | } | |
1058 | ||
313cefa1 | 1059 | line++; |
ea6c2dd1 | 1060 | maybe_print_begin_newline(f, &flags); |
df50185b | 1061 | |
94e0bd7d | 1062 | r = output_journal(f, j, mode, n_columns, flags, ellipsized); |
df50185b | 1063 | if (r < 0) |
b56d608e | 1064 | return r; |
df50185b LP |
1065 | } |
1066 | ||
08984293 LP |
1067 | if (warn_cutoff && line < how_many && not_before > 0) { |
1068 | sd_id128_t boot_id; | |
a7f7d1bd | 1069 | usec_t cutoff = 0; |
08984293 LP |
1070 | |
1071 | /* Check whether the cutoff line is too early */ | |
1072 | ||
1073 | r = sd_id128_get_boot(&boot_id); | |
1074 | if (r < 0) | |
b56d608e | 1075 | return log_error_errno(r, "Failed to get boot id: %m"); |
08984293 LP |
1076 | |
1077 | r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL); | |
1078 | if (r < 0) | |
b56d608e | 1079 | return log_error_errno(r, "Failed to get journal cutoff time: %m"); |
08984293 | 1080 | |
ea6c2dd1 LP |
1081 | if (r > 0 && not_before < cutoff) { |
1082 | maybe_print_begin_newline(f, &flags); | |
08ace05b | 1083 | fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n"); |
ea6c2dd1 | 1084 | } |
08984293 LP |
1085 | |
1086 | warn_cutoff = false; | |
1087 | } | |
1088 | ||
085d7120 | 1089 | if (!(flags & OUTPUT_FOLLOW)) |
86aa7ba4 LP |
1090 | break; |
1091 | ||
3a43da28 | 1092 | r = sd_journal_wait(j, USEC_INFINITY); |
df50185b | 1093 | if (r < 0) |
b56d608e | 1094 | return log_error_errno(r, "Failed to wait for journal: %m"); |
df50185b | 1095 | |
86aa7ba4 LP |
1096 | } |
1097 | ||
b56d608e | 1098 | return 0; |
1a6c43e9 MT |
1099 | } |
1100 | ||
886a64fe | 1101 | int add_matches_for_unit(sd_journal *j, const char *unit) { |
2b45d881 | 1102 | const char *m1, *m2, *m3, *m4; |
1a6c43e9 MT |
1103 | int r; |
1104 | ||
886a64fe | 1105 | assert(j); |
1a6c43e9 MT |
1106 | assert(unit); |
1107 | ||
63c372cb LP |
1108 | m1 = strjoina("_SYSTEMD_UNIT=", unit); |
1109 | m2 = strjoina("COREDUMP_UNIT=", unit); | |
1110 | m3 = strjoina("UNIT=", unit); | |
1111 | m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit); | |
1a6c43e9 | 1112 | |
886a64fe ZJS |
1113 | (void)( |
1114 | /* Look for messages from the service itself */ | |
1115 | (r = sd_journal_add_match(j, m1, 0)) || | |
1116 | ||
1117 | /* Look for coredumps of the service */ | |
1118 | (r = sd_journal_add_disjunction(j)) || | |
fdcd37df ZJS |
1119 | (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) || |
1120 | (r = sd_journal_add_match(j, "_UID=0", 0)) || | |
886a64fe ZJS |
1121 | (r = sd_journal_add_match(j, m2, 0)) || |
1122 | ||
1123 | /* Look for messages from PID 1 about this service */ | |
1124 | (r = sd_journal_add_disjunction(j)) || | |
1125 | (r = sd_journal_add_match(j, "_PID=1", 0)) || | |
2d0b2e87 ZJS |
1126 | (r = sd_journal_add_match(j, m3, 0)) || |
1127 | ||
1128 | /* Look for messages from authorized daemons about this service */ | |
1129 | (r = sd_journal_add_disjunction(j)) || | |
1130 | (r = sd_journal_add_match(j, "_UID=0", 0)) || | |
1131 | (r = sd_journal_add_match(j, m4, 0)) | |
886a64fe | 1132 | ); |
2d0b2e87 | 1133 | |
69ae3ee0 | 1134 | if (r == 0 && endswith(unit, ".slice")) { |
2b45d881 LP |
1135 | const char *m5; |
1136 | ||
1137 | m5 = strjoina("_SYSTEMD_SLICE=", unit); | |
69ae3ee0 ZJS |
1138 | |
1139 | /* Show all messages belonging to a slice */ | |
1140 | (void)( | |
1141 | (r = sd_journal_add_disjunction(j)) || | |
1142 | (r = sd_journal_add_match(j, m5, 0)) | |
1143 | ); | |
1144 | } | |
1145 | ||
886a64fe ZJS |
1146 | return r; |
1147 | } | |
1a6c43e9 | 1148 | |
886a64fe ZJS |
1149 | int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) { |
1150 | int r; | |
2d0b2e87 ZJS |
1151 | char *m1, *m2, *m3, *m4; |
1152 | char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)]; | |
1a6c43e9 | 1153 | |
886a64fe ZJS |
1154 | assert(j); |
1155 | assert(unit); | |
1a6c43e9 | 1156 | |
63c372cb LP |
1157 | m1 = strjoina("_SYSTEMD_USER_UNIT=", unit); |
1158 | m2 = strjoina("USER_UNIT=", unit); | |
1159 | m3 = strjoina("COREDUMP_USER_UNIT=", unit); | |
1160 | m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit); | |
de0671ee | 1161 | sprintf(muid, "_UID="UID_FMT, uid); |
1a6c43e9 | 1162 | |
886a64fe ZJS |
1163 | (void) ( |
1164 | /* Look for messages from the user service itself */ | |
1165 | (r = sd_journal_add_match(j, m1, 0)) || | |
2d0b2e87 | 1166 | (r = sd_journal_add_match(j, muid, 0)) || |
886a64fe ZJS |
1167 | |
1168 | /* Look for messages from systemd about this service */ | |
1169 | (r = sd_journal_add_disjunction(j)) || | |
1170 | (r = sd_journal_add_match(j, m2, 0)) || | |
2d0b2e87 | 1171 | (r = sd_journal_add_match(j, muid, 0)) || |
886a64fe ZJS |
1172 | |
1173 | /* Look for coredumps of the service */ | |
1174 | (r = sd_journal_add_disjunction(j)) || | |
1175 | (r = sd_journal_add_match(j, m3, 0)) || | |
2d0b2e87 ZJS |
1176 | (r = sd_journal_add_match(j, muid, 0)) || |
1177 | (r = sd_journal_add_match(j, "_UID=0", 0)) || | |
1178 | ||
1179 | /* Look for messages from authorized daemons about this service */ | |
1180 | (r = sd_journal_add_disjunction(j)) || | |
fdcd37df | 1181 | (r = sd_journal_add_match(j, m4, 0)) || |
2d0b2e87 | 1182 | (r = sd_journal_add_match(j, muid, 0)) || |
fdcd37df | 1183 | (r = sd_journal_add_match(j, "_UID=0", 0)) |
886a64fe | 1184 | ); |
69ae3ee0 ZJS |
1185 | |
1186 | if (r == 0 && endswith(unit, ".slice")) { | |
2b45d881 LP |
1187 | const char *m5; |
1188 | ||
1189 | m5 = strjoina("_SYSTEMD_SLICE=", unit); | |
69ae3ee0 ZJS |
1190 | |
1191 | /* Show all messages belonging to a slice */ | |
1192 | (void)( | |
1193 | (r = sd_journal_add_disjunction(j)) || | |
1194 | (r = sd_journal_add_match(j, m5, 0)) || | |
1195 | (r = sd_journal_add_match(j, muid, 0)) | |
1196 | ); | |
1197 | } | |
1198 | ||
1a6c43e9 MT |
1199 | return r; |
1200 | } | |
1201 | ||
b6741478 | 1202 | static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) { |
3d94f76c | 1203 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; |
a4475f57 | 1204 | _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1; |
b6741478 LP |
1205 | pid_t pid, child; |
1206 | siginfo_t si; | |
1207 | char buf[37]; | |
1208 | ssize_t k; | |
1209 | int r; | |
1210 | ||
1211 | assert(machine); | |
1212 | assert(boot_id); | |
1213 | ||
affcf189 | 1214 | if (!machine_name_is_valid(machine)) |
b6741478 LP |
1215 | return -EINVAL; |
1216 | ||
e04b0cdb | 1217 | r = container_get_leader(machine, &pid); |
b6741478 LP |
1218 | if (r < 0) |
1219 | return r; | |
e04b0cdb | 1220 | |
671c3419 | 1221 | r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, NULL, &rootfd); |
b6741478 LP |
1222 | if (r < 0) |
1223 | return r; | |
1224 | ||
e04b0cdb | 1225 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) |
b6741478 LP |
1226 | return -errno; |
1227 | ||
1228 | child = fork(); | |
1229 | if (child < 0) | |
1230 | return -errno; | |
1231 | ||
1232 | if (child == 0) { | |
1233 | int fd; | |
1234 | ||
03e334a1 | 1235 | pair[0] = safe_close(pair[0]); |
b6741478 | 1236 | |
671c3419 | 1237 | r = namespace_enter(pidnsfd, mntnsfd, -1, -1, rootfd); |
b6741478 LP |
1238 | if (r < 0) |
1239 | _exit(EXIT_FAILURE); | |
1240 | ||
1241 | fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
1242 | if (fd < 0) | |
1243 | _exit(EXIT_FAILURE); | |
1244 | ||
a6dcc7e5 | 1245 | r = loop_read_exact(fd, buf, 36, false); |
03e334a1 | 1246 | safe_close(fd); |
6c767d1e | 1247 | if (r < 0) |
b6741478 LP |
1248 | _exit(EXIT_FAILURE); |
1249 | ||
e04b0cdb | 1250 | k = send(pair[1], buf, 36, MSG_NOSIGNAL); |
b6741478 LP |
1251 | if (k != 36) |
1252 | _exit(EXIT_FAILURE); | |
1253 | ||
1254 | _exit(EXIT_SUCCESS); | |
1255 | } | |
1256 | ||
03e334a1 | 1257 | pair[1] = safe_close(pair[1]); |
b6741478 | 1258 | |
b6741478 LP |
1259 | r = wait_for_terminate(child, &si); |
1260 | if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) | |
1261 | return r < 0 ? r : -EIO; | |
1262 | ||
fbadf045 LP |
1263 | k = recv(pair[0], buf, 36, 0); |
1264 | if (k != 36) | |
1265 | return -EIO; | |
1266 | ||
b6741478 LP |
1267 | buf[36] = 0; |
1268 | r = sd_id128_from_string(buf, boot_id); | |
1269 | if (r < 0) | |
1270 | return r; | |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | int add_match_this_boot(sd_journal *j, const char *machine) { | |
5ec76417 ZJS |
1276 | char match[9+32+1] = "_BOOT_ID="; |
1277 | sd_id128_t boot_id; | |
1278 | int r; | |
1279 | ||
1280 | assert(j); | |
1281 | ||
b6741478 LP |
1282 | if (machine) { |
1283 | r = get_boot_id_for_machine(machine, &boot_id); | |
f647962d MS |
1284 | if (r < 0) |
1285 | return log_error_errno(r, "Failed to get boot id of container %s: %m", machine); | |
b6741478 LP |
1286 | } else { |
1287 | r = sd_id128_get_boot(&boot_id); | |
f647962d MS |
1288 | if (r < 0) |
1289 | return log_error_errno(r, "Failed to get boot id: %m"); | |
5ec76417 ZJS |
1290 | } |
1291 | ||
1292 | sd_id128_to_string(boot_id, match + 9); | |
1293 | r = sd_journal_add_match(j, match, strlen(match)); | |
f647962d MS |
1294 | if (r < 0) |
1295 | return log_error_errno(r, "Failed to add match: %m"); | |
5ec76417 ZJS |
1296 | |
1297 | r = sd_journal_add_conjunction(j); | |
1298 | if (r < 0) | |
b56d608e | 1299 | return log_error_errno(r, "Failed to add conjunction: %m"); |
5ec76417 ZJS |
1300 | |
1301 | return 0; | |
1302 | } | |
1303 | ||
886a64fe | 1304 | int show_journal_by_unit( |
1a6c43e9 MT |
1305 | FILE *f, |
1306 | const char *unit, | |
1307 | OutputMode mode, | |
1308 | unsigned n_columns, | |
1309 | usec_t not_before, | |
1310 | unsigned how_many, | |
1311 | uid_t uid, | |
886a64fe | 1312 | OutputFlags flags, |
3c756001 LP |
1313 | int journal_open_flags, |
1314 | bool system_unit, | |
94e0bd7d | 1315 | bool *ellipsized) { |
1a6c43e9 | 1316 | |
4afd3348 | 1317 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
1a6c43e9 MT |
1318 | int r; |
1319 | ||
1320 | assert(mode >= 0); | |
1321 | assert(mode < _OUTPUT_MODE_MAX); | |
1322 | assert(unit); | |
1323 | ||
1a6c43e9 MT |
1324 | if (how_many <= 0) |
1325 | return 0; | |
1326 | ||
3c756001 | 1327 | r = sd_journal_open(&j, journal_open_flags); |
f9045468 | 1328 | if (r < 0) |
b56d608e | 1329 | return log_error_errno(r, "Failed to open journal: %m"); |
f9045468 | 1330 | |
b6741478 | 1331 | r = add_match_this_boot(j, NULL); |
5ec76417 ZJS |
1332 | if (r < 0) |
1333 | return r; | |
1334 | ||
3c756001 | 1335 | if (system_unit) |
886a64fe ZJS |
1336 | r = add_matches_for_unit(j, unit); |
1337 | else | |
1338 | r = add_matches_for_user_unit(j, unit, uid); | |
1a6c43e9 | 1339 | if (r < 0) |
b56d608e | 1340 | return log_error_errno(r, "Failed to add unit matches: %m"); |
1a6c43e9 | 1341 | |
553d2243 | 1342 | if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) { |
4ad16808 ZJS |
1343 | _cleanup_free_ char *filter; |
1344 | ||
1345 | filter = journal_make_match_string(j); | |
b56d608e LP |
1346 | if (!filter) |
1347 | return log_oom(); | |
1348 | ||
4ad16808 ZJS |
1349 | log_debug("Journal filter: %s", filter); |
1350 | } | |
5ec76417 | 1351 | |
94e0bd7d | 1352 | return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized); |
86aa7ba4 | 1353 | } |