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