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