]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journalctl.c
journal: implementation rotation
[thirdparty/systemd.git] / src / journal / journalctl.c
CommitLineData
87d2c1ff
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 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 <fcntl.h>
23#include <errno.h>
24#include <stddef.h>
25
cec736d2 26#include "journal-file.h"
87d2c1ff 27
250d54b5
LP
28static int system_journal_open(JournalFile **f) {
29 int r;
30 char *fn;
31 sd_id128_t machine;
32 char ids[33];
33
34 assert(f);
35
36 r = sd_id128_get_machine(&machine);
37 if (r < 0)
38 return r;
39
40 fn = join("/var/log/journal/", sd_id128_to_string(machine, ids), "/system.journal", NULL);
41 if (!fn)
42 return -ENOMEM;
43
0ac38b70 44 r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
250d54b5
LP
45 free(fn);
46
47 if (r >= 0)
48 return r;
49
50 if (r < 0 && r != -ENOENT) {
51 log_error("Failed to open system journal: %s", strerror(-r));
52 return r;
53 }
54
55 fn = join("/run/log/journal/", ids, "/system.journal", NULL);
56 if (!fn)
57 return -ENOMEM;
58
0ac38b70 59 r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
250d54b5
LP
60 free(fn);
61
62 if (r < 0) {
63 log_error("Failed to open system journal: %s", strerror(-r));
64 return r;
65 }
66
67 return r;
68}
69
87d2c1ff
LP
70int main(int argc, char *argv[]) {
71 int r;
72 JournalFile *f;
73 Object *o = NULL;
74
75 log_parse_environment();
76 log_open();
77
250d54b5 78 r = system_journal_open(&f);
87d2c1ff
LP
79 if (r < 0) {
80 log_error("Failed to open journal: %s", strerror(-r));
81 return EXIT_FAILURE;
82 }
83
84 for (;;) {
85 uint64_t offset;
86 uint64_t n, i;
87
88 r = journal_file_next_entry(f, o, &o, &offset);
89 if (r < 0) {
90 log_error("Failed to read journal: %s", strerror(-r));
91 goto finish;
92 }
93
94 if (r == 0)
95 break;
96
97 printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
98
99 n = journal_file_entry_n_items(o);
100 for (i = 0; i < n; i++) {
101 uint64_t p, l;
102
103 p = le64toh(o->entry.items[i].object_offset);
cec736d2 104 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
87d2c1ff
LP
105 if (r < 0) {
106 log_error("Failed to move to data: %s", strerror(-r));
107 goto finish;
108 }
109
87d2c1ff
LP
110 l = o->object.size - offsetof(Object, data.payload);
111 printf("\t[%.*s]\n", (int) l, o->data.payload);
112
cec736d2 113 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
87d2c1ff
LP
114 if (r < 0) {
115 log_error("Failed to move back to entry: %s", strerror(-r));
116 goto finish;
117 }
118 }
119 }
120
121finish:
122 journal_file_close(f);
123
124 return 0;
125}