chiark / gitweb /
journal: implementation rotation
[elogind.git] / src / journal / journalctl.c
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
26 #include "journal-file.h"
27
28 static 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
44         r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
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
59         r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
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
70 int main(int argc, char *argv[]) {
71         int r;
72         JournalFile *f;
73         Object *o = NULL;
74
75         log_parse_environment();
76         log_open();
77
78         r = system_journal_open(&f);
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);
104                         r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
105                         if (r < 0) {
106                                 log_error("Failed to move to data: %s", strerror(-r));
107                                 goto finish;
108                         }
109
110                         l = o->object.size - offsetof(Object, data.payload);
111                         printf("\t[%.*s]\n", (int) l, o->data.payload);
112
113                         r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
114                         if (r < 0) {
115                                 log_error("Failed to move back to entry: %s", strerror(-r));
116                                 goto finish;
117                         }
118                 }
119         }
120
121 finish:
122         journal_file_close(f);
123
124         return 0;
125 }