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