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