]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logs-show.c
Update 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;
bf967366
LP
155 r = -ENOENT;
156
157 if (monotonic)
158 r = safe_atou64(monotonic, &t);
159
160 if (r < 0)
161 r = sd_journal_get_monotonic_usec(j, &t, NULL);
86aa7ba4 162
67a12205
LP
163 if (r >= 0) {
164 printf("[%5llu.%06llu]",
165 (unsigned long long) (t / USEC_PER_SEC),
166 (unsigned long long) (t % USEC_PER_SEC));
86aa7ba4 167
67a12205
LP
168 n += 1 + 5 + 1 + 6 + 1;
169 }
170
171 } else {
172 char buf[64];
bf967366 173 uint64_t x;
67a12205
LP
174 time_t t;
175 struct tm tm;
bf967366
LP
176 r = -ENOENT;
177
178 if (realtime)
179 r = safe_atou64(realtime, &x);
180
181 if (r < 0)
182 r = sd_journal_get_realtime_usec(j, &x);
67a12205 183
67a12205
LP
184 if (r < 0) {
185 log_error("Failed to get realtime: %s", strerror(-r));
186 goto finish;
187 }
188
bf967366 189 t = (time_t) (x / USEC_PER_SEC);
67a12205
LP
190 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime_r(&t, &tm)) <= 0) {
191 log_error("Failed to format time.");
192 goto finish;
193 }
194
195 fputs(buf, stdout);
196 n += strlen(buf);
197 }
86aa7ba4 198
55d7bfc1
LP
199 if (hostname && shall_print(show_all, hostname, hostname_len)) {
200 printf(" %.*s", (int) hostname_len, hostname);
201 n += hostname_len + 1;
202 }
203
4cd9a9d9
LP
204 if (identifier && shall_print(show_all, identifier, identifier_len)) {
205 printf(" %.*s", (int) identifier_len, identifier);
206 n += identifier_len + 1;
55d7bfc1
LP
207 } else if (comm && shall_print(show_all, comm, comm_len)) {
208 printf(" %.*s", (int) comm_len, comm);
209 n += comm_len + 1;
86aa7ba4
LP
210 }
211
55d7bfc1
LP
212 if (pid && shall_print(show_all, pid, pid_len)) {
213 printf("[%.*s]", (int) pid_len, pid);
214 n += pid_len + 2;
6c1e6b98
LP
215 } else if (fake_pid && shall_print(show_all, fake_pid, fake_pid_len)) {
216 printf("[%.*s]", (int) fake_pid_len, fake_pid);
217 n += fake_pid_len + 2;
86aa7ba4
LP
218 }
219
55d7bfc1
LP
220 if (show_all)
221 printf(": %.*s\n", (int) message_len, message);
222 else if (contains_unprintable(message, message_len))
223 fputs(": [blob data]\n", stdout);
224 else if (message_len + n < columns())
225 printf(": %.*s\n", (int) message_len, message);
226 else if (n < columns()) {
227 char *e;
228
229 e = ellipsize_mem(message, message_len, columns() - n - 2, 90);
230
231 if (!e)
232 printf(": %.*s\n", (int) message_len, message);
233 else
234 printf(": %s", e);
235
236 free(e);
237 } else
238 fputs("\n", stdout);
239
240 r = 0;
241
242finish:
243 free(hostname);
4cd9a9d9 244 free(identifier);
55d7bfc1
LP
245 free(comm);
246 free(pid);
6c1e6b98 247 free(fake_pid);
55d7bfc1 248 free(message);
bf967366
LP
249 free(monotonic);
250 free(realtime);
86aa7ba4 251
55d7bfc1 252 return r;
86aa7ba4
LP
253}
254
67a12205
LP
255static int output_short_realtime(sd_journal *j, unsigned line, bool show_all) {
256 return output_short(j, line, show_all, false);
257}
258
259static int output_short_monotonic(sd_journal *j, unsigned line, bool show_all) {
260 return output_short(j, line, show_all, true);
261}
262
86aa7ba4
LP
263static int output_verbose(sd_journal *j, unsigned line, bool show_all) {
264 const void *data;
265 size_t length;
266 char *cursor;
267 uint64_t realtime;
268 char ts[FORMAT_TIMESTAMP_MAX];
269 int r;
270
271 assert(j);
272
273 r = sd_journal_get_realtime_usec(j, &realtime);
274 if (r < 0) {
275 log_error("Failed to get realtime timestamp: %s", strerror(-r));
276 return r;
277 }
278
279 r = sd_journal_get_cursor(j, &cursor);
280 if (r < 0) {
281 log_error("Failed to get cursor: %s", strerror(-r));
282 return r;
283 }
284
285 printf("%s [%s]\n",
286 format_timestamp(ts, sizeof(ts), realtime),
287 cursor);
288
289 free(cursor);
290
291 SD_JOURNAL_FOREACH_DATA(j, data, length) {
292 if (!show_all && (length > PRINT_THRESHOLD ||
293 contains_unprintable(data, length))) {
294 const char *c;
295
296 c = memchr(data, '=', length);
297 if (!c) {
298 log_error("Invalid field.");
299 return -EINVAL;
300 }
301
302 printf("\t%.*s=[blob data]\n",
303 (int) (c - (const char*) data),
304 (const char*) data);
305 } else
306 printf("\t%.*s\n", (int) length, (const char*) data);
307 }
308
309 return 0;
310}
311
312static int output_export(sd_journal *j, unsigned line, bool show_all) {
313 sd_id128_t boot_id;
314 char sid[33];
315 int r;
316 usec_t realtime, monotonic;
317 char *cursor;
318 const void *data;
319 size_t length;
320
321 assert(j);
322
323 r = sd_journal_get_realtime_usec(j, &realtime);
324 if (r < 0) {
325 log_error("Failed to get realtime timestamp: %s", strerror(-r));
326 return r;
327 }
328
329 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
330 if (r < 0) {
331 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
332 return r;
333 }
334
335 r = sd_journal_get_cursor(j, &cursor);
336 if (r < 0) {
337 log_error("Failed to get cursor: %s", strerror(-r));
338 return r;
339 }
340
341 printf(".cursor=%s\n"
342 ".realtime=%llu\n"
343 ".monotonic=%llu\n"
344 ".boot_id=%s\n",
345 cursor,
346 (unsigned long long) realtime,
347 (unsigned long long) monotonic,
348 sd_id128_to_string(boot_id, sid));
349
350 free(cursor);
351
352 SD_JOURNAL_FOREACH_DATA(j, data, length) {
353
354 if (contains_unprintable(data, length)) {
355 const char *c;
356 uint64_t le64;
357
358 c = memchr(data, '=', length);
359 if (!c) {
360 log_error("Invalid field.");
361 return -EINVAL;
362 }
363
364 fwrite(data, c - (const char*) data, 1, stdout);
365 fputc('\n', stdout);
366 le64 = htole64(length - (c - (const char*) data) - 1);
367 fwrite(&le64, sizeof(le64), 1, stdout);
368 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, stdout);
369 } else
370 fwrite(data, length, 1, stdout);
371
372 fputc('\n', stdout);
373 }
374
375 fputc('\n', stdout);
376
377 return 0;
378}
379
380static void json_escape(const char* p, size_t l) {
381
382 if (contains_unprintable(p, l)) {
383 bool not_first = false;
384
385 fputs("[ ", stdout);
386
387 while (l > 0) {
388 if (not_first)
389 printf(", %u", (uint8_t) *p);
390 else {
391 not_first = true;
392 printf("%u", (uint8_t) *p);
393 }
394
395 p++;
396 l--;
397 }
398
399 fputs(" ]", stdout);
400 } else {
401 fputc('\"', stdout);
402
403 while (l > 0) {
404 if (*p == '"' || *p == '\\') {
405 fputc('\\', stdout);
406 fputc(*p, stdout);
407 } else
408 fputc(*p, stdout);
409
410 p++;
411 l--;
412 }
413
414 fputc('\"', stdout);
415 }
416}
417
418static int output_json(sd_journal *j, unsigned line, bool show_all) {
419 uint64_t realtime, monotonic;
420 char *cursor;
421 const void *data;
422 size_t length;
423 sd_id128_t boot_id;
424 char sid[33];
425 int r;
426
427 assert(j);
428
429 r = sd_journal_get_realtime_usec(j, &realtime);
430 if (r < 0) {
431 log_error("Failed to get realtime timestamp: %s", strerror(-r));
432 return r;
433 }
434
435 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
436 if (r < 0) {
437 log_error("Failed to get monotonic timestamp: %s", strerror(-r));
438 return r;
439 }
440
441 r = sd_journal_get_cursor(j, &cursor);
442 if (r < 0) {
443 log_error("Failed to get cursor: %s", strerror(-r));
444 return r;
445 }
446
447 if (line == 1)
448 fputc('\n', stdout);
449 else
450 fputs(",\n", stdout);
451
452 printf("{\n"
453 "\t\".cursor\" : \"%s\",\n"
454 "\t\".realtime\" : %llu,\n"
455 "\t\".monotonic\" : %llu,\n"
456 "\t\".boot_id\" : \"%s\"",
457 cursor,
458 (unsigned long long) realtime,
459 (unsigned long long) monotonic,
460 sd_id128_to_string(boot_id, sid));
461
462 free(cursor);
463
464 SD_JOURNAL_FOREACH_DATA(j, data, length) {
465 const char *c;
466
467 c = memchr(data, '=', length);
468 if (!c) {
469 log_error("Invalid field.");
470 return -EINVAL;
471 }
472
473 fputs(",\n\t", stdout);
474 json_escape(data, c - (const char*) data);
475 fputs(" : ", stdout);
476 json_escape(c + 1, length - (c - (const char*) data) - 1);
477 }
478
479 fputs("\n}", stdout);
480 fflush(stdout);
481
482 return 0;
483}
484
485static int (*output_funcs[_OUTPUT_MODE_MAX])(sd_journal*j, unsigned line, bool show_all) = {
67a12205
LP
486 [OUTPUT_SHORT] = output_short_realtime,
487 [OUTPUT_SHORT_MONOTONIC] = output_short_monotonic,
86aa7ba4
LP
488 [OUTPUT_VERBOSE] = output_verbose,
489 [OUTPUT_EXPORT] = output_export,
490 [OUTPUT_JSON] = output_json
491};
492
df50185b
LP
493int output_journal(sd_journal *j, OutputMode mode, unsigned line, bool show_all) {
494 assert(mode >= 0);
86aa7ba4
LP
495 assert(mode < _OUTPUT_MODE_MAX);
496
497 return output_funcs[mode](j, line, show_all);
498}
499
df50185b
LP
500int show_journal_by_unit(
501 const char *unit,
502 OutputMode mode,
86aa7ba4
LP
503 const char *prefix,
504 unsigned n_columns,
505 usec_t not_before,
506 unsigned how_many,
df50185b
LP
507 bool show_all,
508 bool follow) {
86aa7ba4
LP
509
510 char *m = NULL;
511 sd_journal *j;
512 int r;
df50185b
LP
513 int fd;
514 unsigned line = 0;
515 bool need_seek = false;
86aa7ba4 516
df50185b
LP
517 assert(mode >= 0);
518 assert(mode < _OUTPUT_MODE_MAX);
519 assert(unit);
520
521 if (!endswith(unit, ".service") &&
522 !endswith(unit, ".socket") &&
523 !endswith(unit, ".mount") &&
524 !endswith(unit, ".swap"))
525 return 0;
86aa7ba4 526
df50185b 527 if (how_many <= 0)
f4fb21c1
LP
528 return 0;
529
86aa7ba4
LP
530 if (n_columns <= 0)
531 n_columns = columns();
532
86aa7ba4
LP
533 if (!prefix)
534 prefix = "";
535
df50185b 536 if (asprintf(&m, "_SYSTEMD_UNIT=%s", unit) < 0) {
86aa7ba4
LP
537 r = -ENOMEM;
538 goto finish;
539 }
540
541 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
542 if (r < 0)
543 goto finish;
544
df50185b
LP
545 fd = sd_journal_get_fd(j);
546 if (fd < 0)
547 goto finish;
548
86aa7ba4
LP
549 r = sd_journal_add_match(j, m, strlen(m));
550 if (r < 0)
551 goto finish;
552
553 r = sd_journal_seek_tail(j);
554 if (r < 0)
555 goto finish;
556
df50185b
LP
557 r = sd_journal_previous_skip(j, how_many);
558 if (r < 0)
559 goto finish;
86aa7ba4 560
df50185b
LP
561 if (mode == OUTPUT_JSON) {
562 fputc('[', stdout);
563 fflush(stdout);
564 }
86aa7ba4 565
df50185b
LP
566 for (;;) {
567 for (;;) {
568 usec_t usec;
569
570 if (need_seek) {
571 r = sd_journal_next(j);
572 if (r < 0)
573 goto finish;
574 }
575
576 if (r == 0)
577 break;
86aa7ba4 578
df50185b
LP
579 need_seek = true;
580
581 if (not_before > 0) {
582 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
583
584 /* -ESTALE is returned if the
585 timestamp is not from this boot */
586 if (r == -ESTALE)
587 continue;
588 else if (r < 0)
589 goto finish;
590
591 if (usec < not_before)
592 continue;
593 }
594
595 line ++;
596
597 r = output_journal(j, mode, line, show_all);
598 if (r < 0)
599 goto finish;
600 }
601
602 if (!follow)
86aa7ba4
LP
603 break;
604
df50185b 605 r = fd_wait_for_event(fd, POLLIN);
86aa7ba4
LP
606 if (r < 0)
607 goto finish;
df50185b
LP
608
609 r = sd_journal_process(j);
610 if (r < 0)
611 goto finish;
612
86aa7ba4
LP
613 }
614
df50185b
LP
615 if (mode == OUTPUT_JSON)
616 fputs("\n]\n", stdout);
617
86aa7ba4
LP
618finish:
619 if (m)
620 free(m);
621
622 if (j)
623 sd_journal_close(j);
624
625 return r;
626}
df50185b
LP
627
628static const char *const output_mode_table[_OUTPUT_MODE_MAX] = {
629 [OUTPUT_SHORT] = "short",
67a12205 630 [OUTPUT_SHORT_MONOTONIC] = "short-monotonic",
df50185b
LP
631 [OUTPUT_VERBOSE] = "verbose",
632 [OUTPUT_EXPORT] = "export",
633 [OUTPUT_JSON] = "json"
634};
635
636DEFINE_STRING_TABLE_LOOKUP(output_mode, OutputMode);