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