]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logs-show.c
journal: move journal TODO into main TODO
[thirdparty/systemd.git] / src / logs-show.c
CommitLineData
86aa7ba4
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <time.h>
23#include <assert.h>
24#include <errno.h>
df50185b 25#include <sys/poll.h>
55d7bfc1 26#include <string.h>
86aa7ba4
LP
27
28#include "logs-show.h"
29#include "log.h"
30#include "util.h"
31
32#define PRINT_THRESHOLD 128
33
34static bool contains_unprintable(const void *p, size_t l) {
35 const char *j;
36
37 for (j = p; j < (const char *) p + l; j++)
38 if (*j < ' ' || *j >= 127)
39 return true;
40
41 return false;
42}
43
55d7bfc1
LP
44static int parse_field(const void *data, size_t length, const char *field, char **target, size_t *target_size) {
45 size_t fl, nl;
46 void *buf;
47
48 assert(data);
49 assert(field);
50 assert(target);
51 assert(target_size);
52
53 fl = strlen(field);
54 if (length < fl)
55 return 0;
56
57 if (memcmp(data, field, fl))
58 return 0;
59
60 nl = length - fl;
bf967366
LP
61 buf = malloc(nl+1);
62 memcpy(buf, (const char*) data + fl, nl);
63 ((char*)buf)[nl] = 0;
55d7bfc1
LP
64 if (!buf) {
65 log_error("Out of memory");
66 return -ENOMEM;
67 }
68
6c1e6b98 69 free(*target);
55d7bfc1
LP
70 *target = buf;
71 *target_size = nl;
72
73 return 1;
74}
75
76static bool shall_print(bool show_all, char *p, size_t l) {
77 if (show_all)
78 return true;
79
80 if (l > PRINT_THRESHOLD)
81 return false;
82
83 if (contains_unprintable(p, l))
84 return false;
85
86 return true;
87}
88
bf967366 89static int output_short(sd_journal *j, unsigned line, bool show_all, bool monotonic_mode) {
86aa7ba4 90 int r;
86aa7ba4
LP
91 const void *data;
92 size_t length;
93 size_t n = 0;
bf967366
LP
94 char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, *message = NULL, *realtime = NULL, *monotonic = NULL;
95 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;
86aa7ba4
LP
96
97 assert(j);
98
55d7bfc1
LP
99 SD_JOURNAL_FOREACH_DATA(j, data, length) {
100
101 r = parse_field(data, length, "_HOSTNAME=", &hostname, &hostname_len);
102 if (r < 0)
103 goto finish;
104 else if (r > 0)
105 continue;
106
4cd9a9d9 107 r = parse_field(data, length, "SYSLOG_IDENTIFIER=", &identifier, &identifier_len);
55d7bfc1
LP
108 if (r < 0)
109 goto finish;
110 else if (r > 0)
111 continue;
112
113 r = parse_field(data, length, "_COMM=", &comm, &comm_len);
114 if (r < 0)
115 goto finish;
116 else if (r > 0)
117 continue;
118
119 r = parse_field(data, length, "_PID=", &pid, &pid_len);
120 if (r < 0)
121 goto finish;
122 else if (r > 0)
123 continue;
124
6c1e6b98
LP
125 r = parse_field(data, length, "SYSLOG_PID=", &fake_pid, &fake_pid_len);
126 if (r < 0)
127 goto finish;
128 else if (r > 0)
129 continue;
130
bf967366
LP
131 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len);
132 if (r < 0)
133 goto finish;
134 else if (r > 0)
135 continue;
136
137 r = parse_field(data, length, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len);
138 if (r < 0)
139 goto finish;
140 else if (r > 0)
141 continue;
142
55d7bfc1
LP
143 r = parse_field(data, length, "MESSAGE=", &message, &message_len);
144 if (r < 0)
145 goto finish;
146 }
147
148 if (!message) {
149 r = 0;
150 goto finish;
151 }
152
bf967366 153 if (monotonic_mode) {
67a12205 154 uint64_t t;
3ebcdf8c
LP
155 sd_id128_t boot_id;
156
bf967366
LP
157 r = -ENOENT;
158
159 if (monotonic)
160 r = safe_atou64(monotonic, &t);
161
162 if (r < 0)
3ebcdf8c 163 r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
86aa7ba4 164
3ebcdf8c
LP
165 if (r < 0) {
166 log_error("Failed to get monotonic: %s", strerror(-r));
167 goto finish;
67a12205
LP
168 }
169
3ebcdf8c
LP
170 printf("[%5llu.%06llu]",
171 (unsigned long long) (t / USEC_PER_SEC),
172 (unsigned long long) (t % USEC_PER_SEC));
173
174 n += 1 + 5 + 1 + 6 + 1;
175
67a12205
LP
176 } else {
177 char buf[64];
bf967366 178 uint64_t x;
67a12205
LP
179 time_t t;
180 struct tm tm;
731a676c 181
bf967366
LP
182 r = -ENOENT;
183
184 if (realtime)
185 r = safe_atou64(realtime, &x);
186
187 if (r < 0)
188 r = sd_journal_get_realtime_usec(j, &x);
67a12205 189
67a12205
LP
190 if (r < 0) {
191 log_error("Failed to get realtime: %s", strerror(-r));
192 goto finish;
193 }
194
bf967366 195 t = (time_t) (x / USEC_PER_SEC);
67a12205
LP
196 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
197 log_error("Failed to format time.");
198 goto finish;
199 }
200
201 fputs(buf, stdout);
202 n += strlen(buf);
203 }
86aa7ba4 204
55d7bfc1
LP
205 if (hostname && shall_print(show_all, hostname, hostname_len)) {
206 printf(" %.*s", (int) hostname_len, hostname);
207 n += hostname_len + 1;
208 }
209
4cd9a9d9
LP
210 if (identifier && shall_print(show_all, identifier, identifier_len)) {
211 printf(" %.*s", (int) identifier_len, identifier);
212 n += identifier_len + 1;
55d7bfc1
LP
213 } else if (comm && shall_print(show_all, comm, comm_len)) {
214 printf(" %.*s", (int) comm_len, comm);
215 n += comm_len + 1;
86aa7ba4
LP
216 }
217
55d7bfc1
LP
218 if (pid && shall_print(show_all, pid, pid_len)) {
219 printf("[%.*s]", (int) pid_len, pid);
220 n += pid_len + 2;
6c1e6b98
LP
221 } else if (fake_pid && shall_print(show_all, fake_pid, fake_pid_len)) {
222 printf("[%.*s]", (int) fake_pid_len, fake_pid);
223 n += fake_pid_len + 2;
86aa7ba4
LP
224 }
225
55d7bfc1
LP
226 if (show_all)
227 printf(": %.*s\n", (int) message_len, message);
e6acda19
LP
228 else if (contains_unprintable(message, message_len)) {
229 char bytes[FORMAT_BYTES_MAX];
230 printf(": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
231 } else if (message_len + n < columns())
55d7bfc1
LP
232 printf(": %.*s\n", (int) message_len, message);
233 else if (n < columns()) {
234 char *e;
235
236 e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
237
238 if (!e)
239 printf(": %.*s\n", (int) message_len, message);
240 else
52beb2c3 241 printf(": %s\n", e);
55d7bfc1
LP
242
243 free(e);
244 } else
245 fputs("\n", stdout);
246
247 r = 0;
248
249finish:
250 free(hostname);
4cd9a9d9 251 free(identifier);
55d7bfc1
LP
252 free(comm);
253 free(pid);
6c1e6b98 254 free(fake_pid);
55d7bfc1 255 free(message);
bf967366
LP
256 free(monotonic);
257 free(realtime);
86aa7ba4 258
55d7bfc1 259 return r;
86aa7ba4
LP
260}
261
67a12205
LP
262static int output_short_realtime(sd_journal *j, unsigned line, bool show_all) {
263 return output_short(j, line, show_all, false);
264}
265
266static int output_short_monotonic(sd_journal *j, unsigned line, bool show_all) {
267 return output_short(j, line, show_all, true);
268}
269
86aa7ba4
LP
270static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
271 const void *data;
272 size_t length;
273 char *cursor;
274 uint64_t realtime;
275 char ts[FORMAT_TIMESTAMP_MAX];
276 int r;
277
278 assert(j);
279
280 r = sd_journal_get_realtime_usec(j, &realtime);
281 if (r < 0) {
282 log_error("Failed to get realtime timestamp: %s", strerror(-r));
283 return r;
284 }
285
286 r = sd_journal_get_cursor(j, &cursor);
287 if (r < 0) {
288 log_error("Failed to get cursor: %s", strerror(-r));
289 return r;
290 }
291
292 printf("%s [%s]\n",
293 format_timestamp(ts, sizeof(ts), realtime),
294 cursor);
295
296 free(cursor);
297
298 SD_JOURNAL_FOREACH_DATA(j, data, length) {
299 if (!show_all && (length > PRINT_THRESHOLD ||
300 contains_unprintable(data, length))) {
301 const char *c;
e6acda19 302 char bytes[FORMAT_BYTES_MAX];
86aa7ba4
LP
303
304 c = memchr(data, '=', length);
305 if (!c) {
306 log_error("Invalid field.");
307 return -EINVAL;
308 }
309
e6acda19 310 printf("\t%.*s=[%s blob data]\n",
86aa7ba4 311 (int) (c - (const char*) data),
e6acda19
LP
312 (const char*) data,
313 format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1));
86aa7ba4
LP
314 } else
315 printf("\t%.*s\n", (int) length, (const char*) data);
316 }
317
318 return 0;
319}
320
321static int output_export(sd_journal *j, unsigned line, bool show_all) {
322 sd_id128_t boot_id;
323 char sid[33];
324 int r;
325 usec_t realtime, monotonic;
326 char *cursor;
327 const void *data;
328 size_t length;
329
330 assert(j);
331
332 r = sd_journal_get_realtime_usec(j, &realtime);
333 if (r < 0) {
334 log_error("Failed to get realtime timestamp: %s", strerror(-r));
335 return r;
336 }
337
338 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
339 if (r < 0) {
340 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
341 return r;
342 }
343
344 r = sd_journal_get_cursor(j, &cursor);
345 if (r < 0) {
346 log_error("Failed to get cursor: %s", strerror(-r));
347 return r;
348 }
349
350 printf(".cursor=%s\n"
351 ".realtime=%llu\n"
352 ".monotonic=%llu\n"
353 ".boot_id=%s\n",
354 cursor,
355 (unsigned long long) realtime,
356 (unsigned long long) monotonic,
357 sd_id128_to_string(boot_id, sid));
358
359 free(cursor);
360
361 SD_JOURNAL_FOREACH_DATA(j, data, length) {
362
363 if (contains_unprintable(data, length)) {
364 const char *c;
365 uint64_t le64;
366
367 c = memchr(data, '=', length);
368 if (!c) {
369 log_error("Invalid field.");
370 return -EINVAL;
371 }
372
373 fwrite(data, c - (const char*) data, 1, stdout);
374 fputc('\n', stdout);
375 le64 = htole64(length - (c - (const char*) data) - 1);
376 fwrite(&le64, sizeof(le64), 1, stdout);
377 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
378 } else
379 fwrite(data, length, 1, stdout);
380
381 fputc('\n', stdout);
382 }
383
384 fputc('\n', stdout);
385
386 return 0;
387}
388
389static void json_escape(const char* p, size_t l) {
390
391 if (contains_unprintable(p, l)) {
392 bool not_first = false;
393
394 fputs("[ ", stdout);
395
396 while (l > 0) {
397 if (not_first)
398 printf(", %u", (uint8_t) *p);
399 else {
400 not_first = true;
401 printf("%u", (uint8_t) *p);
402 }
403
404 p++;
405 l--;
406 }
407
408 fputs(" ]", stdout);
409 } else {
410 fputc('\"', stdout);
411
412 while (l > 0) {
413 if (*p == '"' || *p == '\\') {
414 fputc('\\', stdout);
415 fputc(*p, stdout);
416 } else
417 fputc(*p, stdout);
418
419 p++;
420 l--;
421 }
422
423 fputc('\"', stdout);
424 }
425}
426
427static int output_json(sd_journal *j, unsigned line, bool show_all) {
428 uint64_t realtime, monotonic;
429 char *cursor;
430 const void *data;
431 size_t length;
432 sd_id128_t boot_id;
433 char sid[33];
434 int r;
435
436 assert(j);
437
438 r = sd_journal_get_realtime_usec(j, &realtime);
439 if (r < 0) {
440 log_error("Failed to get realtime timestamp: %s", strerror(-r));
441 return r;
442 }
443
444 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
445 if (r < 0) {
446 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
447 return r;
448 }
449
450 r = sd_journal_get_cursor(j, &cursor);
451 if (r < 0) {
452 log_error("Failed to get cursor: %s", strerror(-r));
453 return r;
454 }
455
456 if (line == 1)
457 fputc('\n', stdout);
458 else
459 fputs(",\n", stdout);
460
461 printf("{\n"
462 "\t\".cursor\" : \"%s\",\n"
463 "\t\".realtime\" : %llu,\n"
464 "\t\".monotonic\" : %llu,\n"
465 "\t\".boot_id\" : \"%s\"",
466 cursor,
467 (unsigned long long) realtime,
468 (unsigned long long) monotonic,
469 sd_id128_to_string(boot_id, sid));
470
471 free(cursor);
472
473 SD_JOURNAL_FOREACH_DATA(j, data, length) {
474 const char *c;
475
476 c = memchr(data, '=', length);
477 if (!c) {
478 log_error("Invalid field.");
479 return -EINVAL;
480 }
481
482 fputs(",\n\t", stdout);
483 json_escape(data, c - (const char*) data);
484 fputs(" : ", stdout);
485 json_escape(c + 1, length - (c - (const char*) data) - 1);
486 }
487
488 fputs("\n}", stdout);
489 fflush(stdout);
490
491 return 0;
492}
493
d3f2bdbf
LP
494static int output_cat(sd_journal *j, unsigned line, bool show_all) {
495 const void *data;
496 size_t l;
497 int r;
498
499 assert(j);
500
501 r = sd_journal_get_data(j, "MESSAGE", &data, &l);
502 if (r < 0) {
503 log_error("Failed to get data: %s", strerror(-r));
504 return r;
505 }
506
507 assert(l >= 8);
508
509 fwrite((const char*) data + 8, 1, l - 8, stdout);
510 putchar('\n');
511
512 return 0;
513}
514
86aa7ba4 515static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
67a12205
LP
516 [OUTPUT_SHORT] = output_short_realtime,
517 [OUTPUT_SHORT_MONOTONIC] = output_short_monotonic,
86aa7ba4
LP
518 [OUTPUT_VERBOSE] = output_verbose,
519 [OUTPUT_EXPORT] = output_export,
d3f2bdbf
LP
520 [OUTPUT_JSON] = output_json,
521 [OUTPUT_CAT] = output_cat
86aa7ba4
LP
522};
523
df50185b
LP
524int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
525 assert(mode >= 0);
86aa7ba4
LP
526 assert(mode < _OUTPUT_MODE_MAX);
527
528 return output_funcs[mode](j, line, show_all);
529}
530
df50185b
LP
531int show_journal_by_unit(
532 const char *unit,
533 OutputMode mode,
86aa7ba4
LP
534 const char *prefix,
535 unsigned n_columns,
536 usec_t not_before,
537 unsigned how_many,
df50185b
LP
538 bool show_all,
539 bool follow) {
86aa7ba4
LP
540
541 char *m = NULL;
542 sd_journal *j;
543 int r;
df50185b
LP
544 int fd;
545 unsigned line = 0;
546 bool need_seek = false;
86aa7ba4 547
df50185b
LP
548 assert(mode >= 0);
549 assert(mode < _OUTPUT_MODE_MAX);
550 assert(unit);
551
552 if (!endswith(unit, ".service") &&
553 !endswith(unit, ".socket") &&
554 !endswith(unit, ".mount") &&
555 !endswith(unit, ".swap"))
556 return 0;
86aa7ba4 557
df50185b 558 if (how_many <= 0)
f4fb21c1
LP
559 return 0;
560
86aa7ba4
LP
561 if (n_columns <= 0)
562 n_columns = columns();
563
86aa7ba4
LP
564 if (!prefix)
565 prefix = "";
566
df50185b 567 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
86aa7ba4
LP
568 r = -ENOMEM;
569 goto finish;
570 }
571
572 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
573 if (r < 0)
574 goto finish;
575
df50185b
LP
576 fd = sd_journal_get_fd(j);
577 if (fd < 0)
578 goto finish;
579
86aa7ba4
LP
580 r = sd_journal_add_match(j, m, strlen(m));
581 if (r < 0)
582 goto finish;
583
584 r = sd_journal_seek_tail(j);
585 if (r < 0)
586 goto finish;
587
df50185b
LP
588 r = sd_journal_previous_skip(j, how_many);
589 if (r < 0)
590 goto finish;
86aa7ba4 591
df50185b
LP
592 if (mode == OUTPUT_JSON) {
593 fputc('[', stdout);
594 fflush(stdout);
595 }
86aa7ba4 596
df50185b
LP
597 for (;;) {
598 for (;;) {
599 usec_t usec;
600
601 if (need_seek) {
602 r = sd_journal_next(j);
603 if (r < 0)
604 goto finish;
605 }
606
607 if (r == 0)
608 break;
86aa7ba4 609
df50185b
LP
610 need_seek = true;
611
612 if (not_before > 0) {
613 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
614
615 /* -ESTALE is returned if the
616 timestamp is not from this boot */
617 if (r == -ESTALE)
618 continue;
619 else if (r < 0)
620 goto finish;
621
622 if (usec < not_before)
623 continue;
624 }
625
626 line ++;
627
628 r = output_journal(j, mode, line, show_all);
629 if (r < 0)
630 goto finish;
631 }
632
633 if (!follow)
86aa7ba4
LP
634 break;
635
8f2d43a0 636 r = fd_wait_for_event(fd, POLLIN, (usec_t) -1);
86aa7ba4
LP
637 if (r < 0)
638 goto finish;
df50185b
LP
639
640 r = sd_journal_process(j);
641 if (r < 0)
642 goto finish;
643
86aa7ba4
LP
644 }
645
df50185b
LP
646 if (mode == OUTPUT_JSON)
647 fputs("\n]\n", stdout);
648
86aa7ba4
LP
649finish:
650 if (m)
651 free(m);
652
653 if (j)
654 sd_journal_close(j);
655
656 return r;
657}
df50185b
LP
658
659static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
660 [OUTPUT_SHORT] = "short",
67a12205 661 [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
df50185b
LP
662 [OUTPUT_VERBOSE] = "verbose",
663 [OUTPUT_EXPORT] = "export",
d3f2bdbf
LP
664 [OUTPUT_JSON] = "json",
665 [OUTPUT_CAT] = "cat"
df50185b
LP
666};
667
668DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);