]>
Commit | Line | Data |
---|---|---|
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 | ||
87d2c1ff | 22 | #include <errno.h> |
87d2c1ff | 23 | #include <fcntl.h> |
3fbf9cbb | 24 | #include <stddef.h> |
50f20cfd LP |
25 | #include <unistd.h> |
26 | #include <sys/inotify.h> | |
87d2c1ff LP |
27 | |
28 | #include "sd-journal.h" | |
29 | #include "journal-def.h" | |
cec736d2 | 30 | #include "journal-file.h" |
260a2be4 | 31 | #include "hashmap.h" |
cec736d2 | 32 | #include "list.h" |
de7b95cd | 33 | #include "lookup3.h" |
807e17f0 | 34 | #include "compress.h" |
cf244689 | 35 | #include "journal-internal.h" |
87d2c1ff | 36 | |
cab8ac60 LP |
37 | #define JOURNAL_FILES_MAX 1024 |
38 | ||
de190aef | 39 | static void detach_location(sd_journal *j) { |
8f9b6cd9 LP |
40 | Iterator i; |
41 | JournalFile *f; | |
42 | ||
43 | assert(j); | |
44 | ||
45 | j->current_file = NULL; | |
46 | j->current_field = 0; | |
47 | ||
48 | HASHMAP_FOREACH(f, j->files, i) | |
49 | f->current_offset = 0; | |
50 | } | |
51 | ||
de190aef LP |
52 | static void reset_location(sd_journal *j) { |
53 | assert(j); | |
54 | ||
55 | detach_location(j); | |
56 | zero(j->current_location); | |
57 | } | |
58 | ||
59 | static void init_location(Location *l, JournalFile *f, Object *o) { | |
60 | assert(l); | |
61 | assert(f); | |
62 | assert(o->object.type == OBJECT_ENTRY); | |
63 | ||
64 | l->type = LOCATION_DISCRETE; | |
65 | l->seqnum = le64toh(o->entry.seqnum); | |
66 | l->seqnum_id = f->header->seqnum_id; | |
67 | l->realtime = le64toh(o->entry.realtime); | |
68 | l->monotonic = le64toh(o->entry.monotonic); | |
69 | l->boot_id = le64toh(o->entry.boot_id); | |
70 | l->xor_hash = le64toh(o->entry.xor_hash); | |
71 | ||
72 | l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true; | |
73 | } | |
74 | ||
75 | static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) { | |
76 | assert(j); | |
77 | assert(f); | |
78 | assert(o); | |
79 | ||
80 | init_location(&j->current_location, f, o); | |
81 | ||
82 | j->current_file = f; | |
83 | j->current_field = 0; | |
84 | ||
85 | f->current_offset = offset; | |
86 | } | |
87 | ||
88 | static int same_field(const void *_a, size_t s, const void *_b, size_t t) { | |
89 | const uint8_t *a = _a, *b = _b; | |
90 | size_t j; | |
91 | bool a_good = false, b_good = false, different = false; | |
92 | ||
93 | for (j = 0; j < s && j < t; j++) { | |
94 | ||
95 | if (a[j] == '=') | |
96 | a_good = true; | |
97 | if (b[j] == '=') | |
98 | b_good = true; | |
99 | if (a[j] != b[j]) | |
100 | different = true; | |
101 | ||
102 | if (a_good && b_good) | |
103 | return different ? 0 : 1; | |
104 | } | |
105 | ||
106 | return -EINVAL; | |
107 | } | |
108 | ||
a5344d2c | 109 | _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) { |
de190aef LP |
110 | Match *m, *after = NULL; |
111 | uint64_t le_hash; | |
87d2c1ff | 112 | |
a5344d2c LP |
113 | if (!j) |
114 | return -EINVAL; | |
115 | if (!data) | |
116 | return -EINVAL; | |
1cc101f1 LP |
117 | if (size <= 0) |
118 | return -EINVAL; | |
119 | ||
de190aef LP |
120 | le_hash = htole64(hash64(data, size)); |
121 | ||
122 | LIST_FOREACH(matches, m, j->matches) { | |
123 | int r; | |
124 | ||
125 | if (m->le_hash == le_hash && | |
126 | m->size == size && | |
127 | memcmp(m->data, data, size) == 0) | |
128 | return 0; | |
129 | ||
130 | r = same_field(data, size, m->data, m->size); | |
131 | if (r < 0) | |
132 | return r; | |
133 | else if (r > 0) | |
134 | after = m; | |
135 | } | |
136 | ||
cec736d2 LP |
137 | m = new0(Match, 1); |
138 | if (!m) | |
139 | return -ENOMEM; | |
87d2c1ff | 140 | |
1cc101f1 LP |
141 | m->size = size; |
142 | ||
cec736d2 LP |
143 | m->data = malloc(m->size); |
144 | if (!m->data) { | |
145 | free(m); | |
146 | return -ENOMEM; | |
87d2c1ff LP |
147 | } |
148 | ||
1cc101f1 | 149 | memcpy(m->data, data, size); |
de190aef | 150 | m->le_hash = le_hash; |
87d2c1ff | 151 | |
de190aef LP |
152 | /* Matches for the same fields we order adjacent to each |
153 | * other */ | |
154 | LIST_INSERT_AFTER(Match, matches, j->matches, after, m); | |
de7b95cd LP |
155 | j->n_matches ++; |
156 | ||
de190aef | 157 | detach_location(j); |
8f9b6cd9 | 158 | |
87d2c1ff LP |
159 | return 0; |
160 | } | |
161 | ||
a5344d2c LP |
162 | _public_ void sd_journal_flush_matches(sd_journal *j) { |
163 | if (!j) | |
164 | return; | |
87d2c1ff | 165 | |
cec736d2 LP |
166 | while (j->matches) { |
167 | Match *m = j->matches; | |
87d2c1ff | 168 | |
cec736d2 LP |
169 | LIST_REMOVE(Match, matches, j->matches, m); |
170 | free(m->data); | |
171 | free(m); | |
87d2c1ff | 172 | } |
de7b95cd LP |
173 | |
174 | j->n_matches = 0; | |
8f9b6cd9 | 175 | |
de190aef | 176 | detach_location(j); |
87d2c1ff LP |
177 | } |
178 | ||
de190aef LP |
179 | static int compare_order(JournalFile *af, Object *ao, |
180 | JournalFile *bf, Object *bo) { | |
87d2c1ff | 181 | |
cec736d2 | 182 | uint64_t a, b; |
87d2c1ff | 183 | |
de190aef LP |
184 | assert(af); |
185 | assert(ao); | |
186 | assert(bf); | |
187 | assert(bo); | |
188 | ||
ae2cc8ef | 189 | /* We operate on two different files here, hence we can access |
1cc101f1 LP |
190 | * two objects at the same time, which we normally can't. |
191 | * | |
192 | * If contents and timestamps match, these entries are | |
193 | * identical, even if the seqnum does not match */ | |
194 | ||
195 | if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) && | |
196 | ao->entry.monotonic == bo->entry.monotonic && | |
197 | ao->entry.realtime == bo->entry.realtime && | |
198 | ao->entry.xor_hash == bo->entry.xor_hash) | |
199 | return 0; | |
ae2cc8ef | 200 | |
cec736d2 | 201 | if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) { |
87d2c1ff | 202 | |
cec736d2 LP |
203 | /* If this is from the same seqnum source, compare |
204 | * seqnums */ | |
205 | a = le64toh(ao->entry.seqnum); | |
206 | b = le64toh(bo->entry.seqnum); | |
87d2c1ff | 207 | |
ae2cc8ef LP |
208 | if (a < b) |
209 | return -1; | |
210 | if (a > b) | |
211 | return 1; | |
1cc101f1 LP |
212 | |
213 | /* Wow! This is weird, different data but the same | |
214 | * seqnums? Something is borked, but let's make the | |
215 | * best of it and compare by time. */ | |
ae2cc8ef | 216 | } |
87d2c1ff | 217 | |
ae2cc8ef | 218 | if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) { |
87d2c1ff | 219 | |
cec736d2 LP |
220 | /* If the boot id matches compare monotonic time */ |
221 | a = le64toh(ao->entry.monotonic); | |
222 | b = le64toh(bo->entry.monotonic); | |
87d2c1ff | 223 | |
ae2cc8ef LP |
224 | if (a < b) |
225 | return -1; | |
226 | if (a > b) | |
227 | return 1; | |
87d2c1ff LP |
228 | } |
229 | ||
ae2cc8ef LP |
230 | /* Otherwise compare UTC time */ |
231 | a = le64toh(ao->entry.realtime); | |
232 | b = le64toh(ao->entry.realtime); | |
233 | ||
234 | if (a < b) | |
235 | return -1; | |
236 | if (a > b) | |
237 | return 1; | |
238 | ||
239 | /* Finally, compare by contents */ | |
240 | a = le64toh(ao->entry.xor_hash); | |
241 | b = le64toh(ao->entry.xor_hash); | |
242 | ||
243 | if (a < b) | |
244 | return -1; | |
245 | if (a > b) | |
246 | return 1; | |
247 | ||
248 | return 0; | |
87d2c1ff LP |
249 | } |
250 | ||
de190aef LP |
251 | static int compare_with_location(JournalFile *af, Object *ao, Location *l) { |
252 | uint64_t a; | |
253 | ||
254 | assert(af); | |
255 | assert(ao); | |
256 | assert(l); | |
257 | assert(l->type == LOCATION_DISCRETE); | |
258 | ||
259 | if (l->monotonic_set && | |
260 | sd_id128_equal(ao->entry.boot_id, l->boot_id) && | |
261 | l->realtime_set && | |
262 | le64toh(ao->entry.realtime) == l->realtime && | |
263 | l->xor_hash_set && | |
264 | le64toh(ao->entry.xor_hash) == l->xor_hash) | |
265 | return 0; | |
266 | ||
267 | if (l->seqnum_set && | |
268 | sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) { | |
269 | ||
270 | a = le64toh(ao->entry.seqnum); | |
271 | ||
272 | if (a < l->seqnum) | |
273 | return -1; | |
274 | if (a > l->seqnum) | |
275 | return 1; | |
276 | } | |
277 | ||
278 | if (l->monotonic_set && | |
279 | sd_id128_equal(ao->entry.boot_id, l->boot_id)) { | |
280 | ||
281 | a = le64toh(ao->entry.monotonic); | |
282 | ||
283 | if (a < l->monotonic) | |
284 | return -1; | |
285 | if (a > l->monotonic) | |
286 | return 1; | |
287 | } | |
288 | ||
289 | if (l->realtime_set) { | |
290 | ||
291 | a = le64toh(ao->entry.realtime); | |
292 | ||
293 | if (a < l->realtime) | |
294 | return -1; | |
295 | if (a > l->realtime) | |
296 | return 1; | |
297 | } | |
298 | ||
299 | if (l->xor_hash_set) { | |
300 | a = le64toh(ao->entry.xor_hash); | |
301 | ||
302 | if (a < l->xor_hash) | |
303 | return -1; | |
304 | if (a > l->xor_hash) | |
305 | return 1; | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) { | |
312 | Object *o = NULL; | |
313 | uint64_t p = 0; | |
de7b95cd | 314 | int r; |
de7b95cd LP |
315 | |
316 | assert(j); | |
de7b95cd LP |
317 | |
318 | if (!j->matches) { | |
de190aef LP |
319 | /* No matches is simple */ |
320 | ||
321 | if (j->current_location.type == LOCATION_HEAD) | |
322 | r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p); | |
323 | else if (j->current_location.type == LOCATION_TAIL) | |
324 | r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p); | |
325 | else if (j->current_location.seqnum_set && | |
326 | sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) | |
327 | r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p); | |
328 | else if (j->current_location.monotonic_set) | |
329 | r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p); | |
330 | else if (j->current_location.realtime_set) | |
331 | r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p); | |
332 | else | |
333 | r = journal_file_next_entry(f, NULL, 0, direction, &o, &p); | |
334 | ||
335 | if (r <= 0) | |
336 | return r; | |
337 | ||
338 | } else { | |
339 | Match *m, *term_match = NULL; | |
340 | Object *to = NULL; | |
341 | uint64_t tp = 0; | |
342 | ||
343 | /* We have matches, first, let's jump to the monotonic | |
344 | * position if we have any, since it implies a | |
345 | * match. */ | |
de7b95cd | 346 | |
de190aef LP |
347 | if (j->current_location.type == LOCATION_DISCRETE && |
348 | j->current_location.monotonic_set) { | |
349 | ||
350 | r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p); | |
351 | if (r <= 0) | |
de7b95cd | 352 | return r; |
de190aef LP |
353 | } |
354 | ||
355 | LIST_FOREACH(matches, m, j->matches) { | |
356 | Object *c, *d; | |
357 | uint64_t cp, dp; | |
358 | ||
359 | r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp); | |
360 | if (r <= 0) | |
361 | return r; | |
362 | ||
363 | if (j->current_location.type == LOCATION_HEAD) | |
364 | r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp); | |
365 | else if (j->current_location.type == LOCATION_TAIL) | |
366 | r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp); | |
367 | else if (j->current_location.seqnum_set && | |
368 | sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) | |
369 | r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp); | |
370 | else if (j->current_location.realtime_set) | |
371 | r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp); | |
372 | else | |
373 | r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp); | |
374 | ||
375 | if (!term_match) { | |
376 | term_match = m; | |
377 | ||
378 | if (r > 0) { | |
379 | to = c; | |
380 | tp = cp; | |
381 | } | |
382 | } else if (same_field(term_match->data, term_match->size, m->data, m->size)) { | |
383 | ||
384 | /* Same field as previous match... */ | |
385 | if (r > 0) { | |
386 | ||
387 | /* Find the earliest of the OR matches */ | |
388 | ||
389 | if (!to || | |
390 | (direction == DIRECTION_DOWN && cp < tp) || | |
391 | (direction == DIRECTION_UP && cp > tp)) { | |
392 | to = c; | |
24b51289 | 393 | tp = cp; |
de190aef LP |
394 | } |
395 | ||
396 | } | |
de7b95cd | 397 | |
de190aef LP |
398 | } else { |
399 | ||
400 | /* Previous term is finished, did anything match? */ | |
401 | if (!to) | |
402 | return 0; | |
403 | ||
404 | /* Find the last of the AND matches */ | |
405 | if (!o || | |
406 | (direction == DIRECTION_DOWN && tp > p) || | |
407 | (direction == DIRECTION_UP && tp < p)) { | |
408 | o = to; | |
409 | p = tp; | |
410 | } | |
411 | ||
412 | term_match = m; | |
413 | ||
414 | if (r > 0) { | |
415 | to = c; | |
416 | tp = cp; | |
417 | } else { | |
418 | to = NULL; | |
419 | tp = 0; | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | /* Last term is finished, did anything match? */ | |
425 | if (!to) | |
426 | return 0; | |
427 | ||
428 | if (!o || | |
429 | (direction == DIRECTION_DOWN && tp > p) || | |
430 | (direction == DIRECTION_UP && tp < p)) { | |
431 | o = to; | |
432 | p = tp; | |
433 | } | |
434 | ||
435 | if (!o) | |
436 | return 0; | |
de7b95cd LP |
437 | } |
438 | ||
de190aef LP |
439 | if (ret) |
440 | *ret = o; | |
de7b95cd | 441 | |
de190aef LP |
442 | if (offset) |
443 | *offset = p; | |
444 | ||
445 | return 1; | |
446 | } | |
447 | ||
448 | static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) { | |
449 | int r; | |
450 | uint64_t cp; | |
451 | Object *c; | |
452 | ||
453 | assert(j); | |
454 | assert(f); | |
455 | assert(ret); | |
456 | assert(offset); | |
457 | ||
458 | c = *ret; | |
459 | cp = *offset; | |
460 | ||
461 | if (!j->matches) { | |
462 | /* No matches is easy */ | |
463 | ||
464 | r = journal_file_next_entry(f, c, cp, direction, &c, &cp); | |
de7b95cd LP |
465 | if (r <= 0) |
466 | return r; | |
467 | ||
de190aef LP |
468 | if (ret) |
469 | *ret = c; | |
470 | if (offset) | |
471 | *offset = cp; | |
472 | return 1; | |
de7b95cd LP |
473 | } |
474 | ||
de190aef LP |
475 | /* So there are matches we have to adhere to, let's find the |
476 | * first entry that matches all of them */ | |
477 | ||
de7b95cd LP |
478 | for (;;) { |
479 | uint64_t np, n; | |
de190aef LP |
480 | bool found, term_result = false; |
481 | Match *m, *term_match = NULL; | |
466ccd92 | 482 | Object *npo = NULL; |
de7b95cd LP |
483 | |
484 | n = journal_file_entry_n_items(c); | |
485 | ||
486 | /* Make sure we don't match the entry we are starting | |
487 | * from. */ | |
de190aef | 488 | found = cp > *offset; |
de7b95cd LP |
489 | |
490 | np = 0; | |
491 | LIST_FOREACH(matches, m, j->matches) { | |
492 | uint64_t q, k; | |
466ccd92 | 493 | Object *qo = NULL; |
de7b95cd | 494 | |
de190aef LP |
495 | /* Let's check if this is the beginning of a |
496 | * new term, i.e. has a different field prefix | |
497 | * as the preceeding match. */ | |
498 | if (!term_match) { | |
499 | term_match = m; | |
500 | term_result = false; | |
501 | } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) { | |
502 | if (!term_result) | |
503 | found = false; | |
504 | ||
505 | term_match = m; | |
506 | term_result = false; | |
507 | } | |
508 | ||
de7b95cd LP |
509 | for (k = 0; k < n; k++) |
510 | if (c->entry.items[k].hash == m->le_hash) | |
511 | break; | |
512 | ||
513 | if (k >= n) { | |
de190aef LP |
514 | /* Hmm, didn't find any field that |
515 | * matched this rule, so ignore this | |
516 | * match. Go on with next match */ | |
de7b95cd LP |
517 | continue; |
518 | } | |
519 | ||
de190aef LP |
520 | term_result = true; |
521 | ||
de7b95cd LP |
522 | /* Hmm, so, this field matched, let's remember |
523 | * where we'd have to try next, in case the other | |
524 | * matches are not OK */ | |
e892bd17 | 525 | |
466ccd92 | 526 | r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, &qo, &q); |
de190aef | 527 | if (r > 0) { |
e892bd17 | 528 | |
de190aef | 529 | if (direction == DIRECTION_DOWN) { |
466ccd92 | 530 | if (q > np) { |
de190aef | 531 | np = q; |
466ccd92 LP |
532 | npo = qo; |
533 | } | |
de190aef | 534 | } else { |
466ccd92 | 535 | if (np == 0 || q < np) { |
de190aef | 536 | np = q; |
466ccd92 LP |
537 | npo = qo; |
538 | } | |
de190aef | 539 | } |
e892bd17 | 540 | } |
de7b95cd LP |
541 | } |
542 | ||
de190aef | 543 | /* Check the last term */ |
466ccd92 LP |
544 | if (term_match && !term_result) |
545 | found = false; | |
de190aef | 546 | |
de7b95cd LP |
547 | /* Did this entry match against all matches? */ |
548 | if (found) { | |
de190aef LP |
549 | if (ret) |
550 | *ret = c; | |
551 | if (offset) | |
552 | *offset = cp; | |
de7b95cd LP |
553 | return 1; |
554 | } | |
555 | ||
556 | /* Did we find a subsequent entry? */ | |
557 | if (np == 0) | |
558 | return 0; | |
559 | ||
560 | /* Hmm, ok, this entry only matched partially, so | |
561 | * let's try another one */ | |
562 | cp = np; | |
466ccd92 | 563 | c = npo; |
de7b95cd LP |
564 | } |
565 | } | |
566 | ||
de190aef LP |
567 | static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) { |
568 | Object *c; | |
569 | uint64_t cp; | |
570 | int compare_value, r; | |
571 | ||
572 | assert(j); | |
573 | assert(f); | |
574 | ||
575 | if (f->current_offset > 0) { | |
466ccd92 LP |
576 | cp = f->current_offset; |
577 | ||
578 | r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c); | |
de190aef LP |
579 | if (r < 0) |
580 | return r; | |
581 | ||
de190aef LP |
582 | r = next_with_matches(j, f, direction, &c, &cp); |
583 | if (r <= 0) | |
584 | return r; | |
585 | ||
586 | compare_value = 1; | |
587 | } else { | |
588 | r = find_location(j, f, direction, &c, &cp); | |
589 | if (r <= 0) | |
590 | return r; | |
591 | ||
592 | compare_value = 0; | |
593 | } | |
594 | ||
595 | for (;;) { | |
596 | bool found; | |
597 | ||
598 | if (j->current_location.type == LOCATION_DISCRETE) { | |
599 | int k; | |
600 | ||
601 | k = compare_with_location(f, c, &j->current_location); | |
602 | if (direction == DIRECTION_DOWN) | |
603 | found = k >= compare_value; | |
604 | else | |
605 | found = k <= -compare_value; | |
606 | } else | |
607 | found = true; | |
608 | ||
609 | if (found) { | |
610 | if (ret) | |
611 | *ret = c; | |
612 | if (offset) | |
613 | *offset = cp; | |
614 | return 1; | |
615 | } | |
616 | ||
617 | r = next_with_matches(j, f, direction, &c, &cp); | |
618 | if (r <= 0) | |
619 | return r; | |
620 | } | |
621 | } | |
622 | ||
e892bd17 | 623 | static int real_journal_next(sd_journal *j, direction_t direction) { |
cec736d2 LP |
624 | JournalFile *f, *new_current = NULL; |
625 | Iterator i; | |
87d2c1ff | 626 | int r; |
cec736d2 LP |
627 | uint64_t new_offset = 0; |
628 | Object *new_entry = NULL; | |
87d2c1ff | 629 | |
a5344d2c LP |
630 | if (!j) |
631 | return -EINVAL; | |
87d2c1ff | 632 | |
cec736d2 LP |
633 | HASHMAP_FOREACH(f, j->files, i) { |
634 | Object *o; | |
635 | uint64_t p; | |
de190aef | 636 | bool found; |
87d2c1ff | 637 | |
de190aef | 638 | r = next_beyond_location(j, f, direction, &o, &p); |
87d2c1ff LP |
639 | if (r < 0) |
640 | return r; | |
cec736d2 LP |
641 | else if (r == 0) |
642 | continue; | |
87d2c1ff | 643 | |
de190aef LP |
644 | if (!new_current) |
645 | found = true; | |
646 | else { | |
647 | int k; | |
648 | ||
649 | k = compare_order(f, o, new_current, new_entry); | |
650 | ||
651 | if (direction == DIRECTION_DOWN) | |
652 | found = k < 0; | |
653 | else | |
654 | found = k > 0; | |
655 | } | |
656 | ||
657 | if (found) { | |
cec736d2 LP |
658 | new_current = f; |
659 | new_entry = o; | |
660 | new_offset = p; | |
87d2c1ff | 661 | } |
87d2c1ff LP |
662 | } |
663 | ||
de190aef LP |
664 | if (!new_current) |
665 | return 0; | |
ae2cc8ef | 666 | |
de190aef | 667 | set_location(j, new_current, new_entry, new_offset); |
ae2cc8ef | 668 | |
de190aef LP |
669 | return 1; |
670 | } | |
ae2cc8ef | 671 | |
a5344d2c | 672 | _public_ int sd_journal_next(sd_journal *j) { |
de190aef LP |
673 | return real_journal_next(j, DIRECTION_DOWN); |
674 | } | |
ae2cc8ef | 675 | |
a5344d2c | 676 | _public_ int sd_journal_previous(sd_journal *j) { |
de190aef LP |
677 | return real_journal_next(j, DIRECTION_UP); |
678 | } | |
ae2cc8ef | 679 | |
a5344d2c | 680 | _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) { |
de190aef | 681 | int c = 0, r; |
ae2cc8ef | 682 | |
a5344d2c LP |
683 | if (!j) |
684 | return -EINVAL; | |
de190aef LP |
685 | |
686 | while (skip > 0) { | |
687 | r = sd_journal_next(j); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | if (r == 0) | |
692 | return c; | |
693 | ||
694 | skip--; | |
695 | c++; | |
87d2c1ff LP |
696 | } |
697 | ||
de190aef | 698 | return c; |
87d2c1ff LP |
699 | } |
700 | ||
a5344d2c | 701 | _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) { |
de190aef | 702 | int c = 0, r; |
87d2c1ff | 703 | |
a5344d2c LP |
704 | if (!j) |
705 | return -EINVAL; | |
de190aef LP |
706 | |
707 | while (skip > 0) { | |
708 | r = sd_journal_previous(j); | |
709 | if (r < 0) | |
710 | return r; | |
711 | ||
712 | if (r == 0) | |
713 | return c; | |
714 | ||
715 | skip--; | |
716 | c++; | |
717 | } | |
718 | ||
719 | return 1; | |
87d2c1ff LP |
720 | } |
721 | ||
a5344d2c | 722 | _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) { |
cec736d2 | 723 | Object *o; |
87d2c1ff | 724 | int r; |
3fbf9cbb | 725 | char bid[33], sid[33]; |
87d2c1ff | 726 | |
a5344d2c LP |
727 | if (!j) |
728 | return -EINVAL; | |
729 | if (!cursor) | |
730 | return -EINVAL; | |
87d2c1ff | 731 | |
3fbf9cbb LP |
732 | if (!j->current_file || j->current_file->current_offset <= 0) |
733 | return -EADDRNOTAVAIL; | |
87d2c1ff | 734 | |
de190aef | 735 | r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o); |
87d2c1ff LP |
736 | if (r < 0) |
737 | return r; | |
738 | ||
3fbf9cbb LP |
739 | sd_id128_to_string(j->current_file->header->seqnum_id, sid); |
740 | sd_id128_to_string(o->entry.boot_id, bid); | |
87d2c1ff | 741 | |
3fbf9cbb LP |
742 | if (asprintf(cursor, |
743 | "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s", | |
744 | sid, (unsigned long long) le64toh(o->entry.seqnum), | |
745 | bid, (unsigned long long) le64toh(o->entry.monotonic), | |
746 | (unsigned long long) le64toh(o->entry.realtime), | |
747 | (unsigned long long) le64toh(o->entry.xor_hash), | |
748 | file_name_from_path(j->current_file->path)) < 0) | |
749 | return -ENOMEM; | |
87d2c1ff LP |
750 | |
751 | return 1; | |
752 | } | |
753 | ||
a5344d2c | 754 | _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) { |
de190aef LP |
755 | char *w; |
756 | size_t l; | |
757 | char *state; | |
758 | unsigned long long seqnum, monotonic, realtime, xor_hash; | |
759 | bool | |
760 | seqnum_id_set = false, | |
761 | seqnum_set = false, | |
762 | boot_id_set = false, | |
763 | monotonic_set = false, | |
764 | realtime_set = false, | |
765 | xor_hash_set = false; | |
766 | sd_id128_t seqnum_id, boot_id; | |
767 | ||
a5344d2c LP |
768 | if (!j) |
769 | return -EINVAL; | |
770 | if (!cursor) | |
771 | return -EINVAL; | |
de190aef LP |
772 | |
773 | FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) { | |
774 | char *item; | |
775 | int k = 0; | |
776 | ||
777 | if (l < 2 || w[1] != '=') | |
778 | return -EINVAL; | |
779 | ||
780 | item = strndup(w, l); | |
781 | if (!item) | |
782 | return -ENOMEM; | |
783 | ||
784 | switch (w[0]) { | |
785 | ||
786 | case 's': | |
787 | seqnum_id_set = true; | |
788 | k = sd_id128_from_string(w+2, &seqnum_id); | |
789 | break; | |
790 | ||
791 | case 'i': | |
792 | seqnum_set = true; | |
793 | if (sscanf(w+2, "%llx", &seqnum) != 1) | |
794 | k = -EINVAL; | |
795 | break; | |
796 | ||
797 | case 'b': | |
798 | boot_id_set = true; | |
799 | k = sd_id128_from_string(w+2, &boot_id); | |
800 | break; | |
801 | ||
802 | case 'm': | |
803 | monotonic_set = true; | |
804 | if (sscanf(w+2, "%llx", &monotonic) != 1) | |
805 | k = -EINVAL; | |
806 | break; | |
807 | ||
808 | case 't': | |
809 | realtime_set = true; | |
810 | if (sscanf(w+2, "%llx", &realtime) != 1) | |
811 | k = -EINVAL; | |
812 | break; | |
813 | ||
814 | case 'x': | |
815 | xor_hash_set = true; | |
816 | if (sscanf(w+2, "%llx", &xor_hash) != 1) | |
817 | k = -EINVAL; | |
818 | break; | |
819 | } | |
820 | ||
821 | free(item); | |
822 | ||
823 | if (k < 0) | |
824 | return k; | |
825 | } | |
826 | ||
827 | if ((!seqnum_set || !seqnum_id_set) && | |
828 | (!monotonic_set || !boot_id_set) && | |
829 | !realtime_set) | |
830 | return -EINVAL; | |
831 | ||
832 | reset_location(j); | |
833 | ||
834 | j->current_location.type = LOCATION_DISCRETE; | |
835 | ||
836 | if (realtime_set) { | |
837 | j->current_location.realtime = (uint64_t) realtime; | |
838 | j->current_location.realtime_set = true; | |
839 | } | |
840 | ||
841 | if (seqnum_set && seqnum_id_set) { | |
842 | j->current_location.seqnum = (uint64_t) seqnum; | |
843 | j->current_location.seqnum_id = seqnum_id; | |
844 | j->current_location.seqnum_set = true; | |
845 | } | |
846 | ||
847 | if (monotonic_set && boot_id_set) { | |
848 | j->current_location.monotonic = (uint64_t) monotonic; | |
849 | j->current_location.boot_id = boot_id; | |
850 | j->current_location.monotonic_set = true; | |
851 | } | |
852 | ||
853 | if (xor_hash_set) { | |
854 | j->current_location.xor_hash = (uint64_t) xor_hash; | |
855 | j->current_location.xor_hash_set = true; | |
856 | } | |
857 | ||
858 | return 0; | |
859 | } | |
860 | ||
a5344d2c LP |
861 | _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) { |
862 | if (!j) | |
863 | return -EINVAL; | |
de190aef LP |
864 | |
865 | reset_location(j); | |
866 | j->current_location.type = LOCATION_DISCRETE; | |
867 | j->current_location.boot_id = boot_id; | |
868 | j->current_location.monotonic = usec; | |
869 | j->current_location.monotonic_set = true; | |
870 | ||
871 | return 0; | |
872 | } | |
873 | ||
a5344d2c LP |
874 | _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { |
875 | if (!j) | |
876 | return -EINVAL; | |
de190aef LP |
877 | |
878 | reset_location(j); | |
879 | j->current_location.type = LOCATION_DISCRETE; | |
880 | j->current_location.realtime = usec; | |
881 | j->current_location.realtime_set = true; | |
882 | ||
883 | return 0; | |
884 | } | |
885 | ||
a5344d2c LP |
886 | _public_ int sd_journal_seek_head(sd_journal *j) { |
887 | if (!j) | |
888 | return -EINVAL; | |
de190aef LP |
889 | |
890 | reset_location(j); | |
891 | j->current_location.type = LOCATION_HEAD; | |
892 | ||
893 | return 0; | |
894 | } | |
895 | ||
a5344d2c LP |
896 | _public_ int sd_journal_seek_tail(sd_journal *j) { |
897 | if (!j) | |
898 | return -EINVAL; | |
de190aef LP |
899 | |
900 | reset_location(j); | |
901 | j->current_location.type = LOCATION_TAIL; | |
902 | ||
903 | return 0; | |
87d2c1ff LP |
904 | } |
905 | ||
3fbf9cbb LP |
906 | static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) { |
907 | char *fn; | |
908 | int r; | |
909 | JournalFile *f; | |
910 | ||
911 | assert(j); | |
912 | assert(prefix); | |
913 | assert(filename); | |
914 | ||
cf244689 LP |
915 | if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) && |
916 | !startswith(filename, "system.journal")) | |
917 | return 0; | |
918 | ||
3fbf9cbb LP |
919 | if (dir) |
920 | fn = join(prefix, "/", dir, "/", filename, NULL); | |
921 | else | |
922 | fn = join(prefix, "/", filename, NULL); | |
923 | ||
924 | if (!fn) | |
925 | return -ENOMEM; | |
926 | ||
50f20cfd LP |
927 | if (hashmap_get(j->files, fn)) { |
928 | free(fn); | |
929 | return 0; | |
930 | } | |
931 | ||
932 | if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) { | |
933 | log_debug("Too many open journal files, not adding %s, ignoring.", fn); | |
934 | free(fn); | |
935 | return 0; | |
936 | } | |
937 | ||
3fbf9cbb LP |
938 | r = journal_file_open(fn, O_RDONLY, 0, NULL, &f); |
939 | free(fn); | |
940 | ||
941 | if (r < 0) { | |
942 | if (errno == ENOENT) | |
943 | return 0; | |
944 | ||
945 | return r; | |
946 | } | |
947 | ||
72f59706 | 948 | /* journal_file_dump(f); */ |
de190aef | 949 | |
3fbf9cbb LP |
950 | r = hashmap_put(j->files, f->path, f); |
951 | if (r < 0) { | |
952 | journal_file_close(f); | |
953 | return r; | |
954 | } | |
955 | ||
50f20cfd LP |
956 | log_debug("File %s got added.", f->path); |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
961 | static int remove_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) { | |
962 | char *fn; | |
963 | JournalFile *f; | |
964 | ||
965 | assert(j); | |
966 | assert(prefix); | |
967 | assert(filename); | |
968 | ||
969 | if (dir) | |
970 | fn = join(prefix, "/", dir, "/", filename, NULL); | |
971 | else | |
972 | fn = join(prefix, "/", filename, NULL); | |
973 | ||
974 | if (!fn) | |
975 | return -ENOMEM; | |
976 | ||
977 | f = hashmap_get(j->files, fn); | |
978 | free(fn); | |
979 | ||
980 | if (!f) | |
981 | return 0; | |
982 | ||
983 | hashmap_remove(j->files, f->path); | |
984 | journal_file_close(f); | |
985 | ||
986 | log_debug("File %s got removed.", f->path); | |
3fbf9cbb LP |
987 | return 0; |
988 | } | |
989 | ||
990 | static int add_directory(sd_journal *j, const char *prefix, const char *dir) { | |
991 | char *fn; | |
992 | int r; | |
993 | DIR *d; | |
50f20cfd | 994 | int wd; |
cf244689 | 995 | sd_id128_t id, mid; |
3fbf9cbb LP |
996 | |
997 | assert(j); | |
998 | assert(prefix); | |
999 | assert(dir); | |
1000 | ||
cf244689 LP |
1001 | if ((j->flags & SD_JOURNAL_LOCAL_ONLY) && |
1002 | (sd_id128_from_string(dir, &id) < 0 || | |
1003 | sd_id128_get_machine(&mid) < 0 || | |
1004 | !sd_id128_equal(id, mid))) | |
1005 | return 0; | |
1006 | ||
3fbf9cbb LP |
1007 | fn = join(prefix, "/", dir, NULL); |
1008 | if (!fn) | |
1009 | return -ENOMEM; | |
1010 | ||
1011 | d = opendir(fn); | |
3fbf9cbb LP |
1012 | |
1013 | if (!d) { | |
50f20cfd | 1014 | free(fn); |
3fbf9cbb LP |
1015 | if (errno == ENOENT) |
1016 | return 0; | |
1017 | ||
1018 | return -errno; | |
1019 | } | |
1020 | ||
50f20cfd LP |
1021 | wd = inotify_add_watch(j->inotify_fd, fn, |
1022 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1023 | IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT| | |
1024 | IN_DONT_FOLLOW|IN_ONLYDIR); | |
1025 | if (wd > 0) { | |
1026 | if (hashmap_put(j->inotify_wd_dirs, INT_TO_PTR(wd), fn) < 0) | |
1027 | inotify_rm_watch(j->inotify_fd, wd); | |
1028 | else | |
1029 | fn = NULL; | |
1030 | } | |
1031 | ||
1032 | free(fn); | |
1033 | ||
3fbf9cbb LP |
1034 | for (;;) { |
1035 | struct dirent buf, *de; | |
1036 | ||
1037 | r = readdir_r(d, &buf, &de); | |
1038 | if (r != 0 || !de) | |
1039 | break; | |
1040 | ||
1041 | if (!dirent_is_file_with_suffix(de, ".journal")) | |
1042 | continue; | |
1043 | ||
1044 | r = add_file(j, prefix, dir, de->d_name); | |
1045 | if (r < 0) | |
1046 | log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r)); | |
1047 | } | |
1048 | ||
1049 | closedir(d); | |
1050 | ||
50f20cfd LP |
1051 | log_debug("Directory %s/%s got added.", prefix, dir); |
1052 | ||
3fbf9cbb LP |
1053 | return 0; |
1054 | } | |
1055 | ||
50f20cfd LP |
1056 | static void remove_directory_wd(sd_journal *j, int wd) { |
1057 | char *p; | |
1058 | ||
1059 | assert(j); | |
1060 | assert(wd > 0); | |
1061 | ||
1062 | if (j->inotify_fd >= 0) | |
1063 | inotify_rm_watch(j->inotify_fd, wd); | |
1064 | ||
1065 | p = hashmap_remove(j->inotify_wd_dirs, INT_TO_PTR(wd)); | |
1066 | ||
1067 | if (p) { | |
1068 | log_debug("Directory %s got removed.", p); | |
1069 | free(p); | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | static void add_root_wd(sd_journal *j, const char *p) { | |
1074 | int wd; | |
1075 | char *k; | |
1076 | ||
1077 | assert(j); | |
1078 | assert(p); | |
1079 | ||
1080 | wd = inotify_add_watch(j->inotify_fd, p, | |
1081 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1082 | IN_DONT_FOLLOW|IN_ONLYDIR); | |
1083 | if (wd <= 0) | |
1084 | return; | |
1085 | ||
1086 | k = strdup(p); | |
1087 | if (!k || hashmap_put(j->inotify_wd_roots, INT_TO_PTR(wd), k) < 0) { | |
1088 | inotify_rm_watch(j->inotify_fd, wd); | |
1089 | free(k); | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | static void remove_root_wd(sd_journal *j, int wd) { | |
1094 | char *p; | |
1095 | ||
1096 | assert(j); | |
1097 | assert(wd > 0); | |
1098 | ||
1099 | if (j->inotify_fd >= 0) | |
1100 | inotify_rm_watch(j->inotify_fd, wd); | |
1101 | ||
1102 | p = hashmap_remove(j->inotify_wd_roots, INT_TO_PTR(wd)); | |
1103 | ||
1104 | if (p) { | |
1105 | log_debug("Root %s got removed.", p); | |
1106 | free(p); | |
1107 | } | |
1108 | } | |
1109 | ||
a5344d2c | 1110 | _public_ int sd_journal_open(sd_journal **ret, int flags) { |
87d2c1ff | 1111 | sd_journal *j; |
87d2c1ff | 1112 | const char *p; |
87d2c1ff LP |
1113 | const char search_paths[] = |
1114 | "/run/log/journal\0" | |
1115 | "/var/log/journal\0"; | |
3fbf9cbb | 1116 | int r; |
87d2c1ff | 1117 | |
a5344d2c LP |
1118 | if (!ret) |
1119 | return -EINVAL; | |
1120 | ||
1121 | if (flags & ~(SD_JOURNAL_LOCAL_ONLY| | |
1122 | SD_JOURNAL_RUNTIME_ONLY| | |
1123 | SD_JOURNAL_SYSTEM_ONLY)) | |
1124 | return -EINVAL; | |
87d2c1ff LP |
1125 | |
1126 | j = new0(sd_journal, 1); | |
1127 | if (!j) | |
1128 | return -ENOMEM; | |
1129 | ||
cf244689 LP |
1130 | j->flags = flags; |
1131 | ||
50f20cfd LP |
1132 | j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
1133 | if (j->inotify_fd < 0) { | |
1134 | r = -errno; | |
1135 | goto fail; | |
1136 | } | |
1137 | ||
260a2be4 | 1138 | j->files = hashmap_new(string_hash_func, string_compare_func); |
3fbf9cbb LP |
1139 | if (!j->files) { |
1140 | r = -ENOMEM; | |
260a2be4 | 1141 | goto fail; |
3fbf9cbb LP |
1142 | } |
1143 | ||
50f20cfd LP |
1144 | j->inotify_wd_dirs = hashmap_new(trivial_hash_func, trivial_compare_func); |
1145 | j->inotify_wd_roots = hashmap_new(trivial_hash_func, trivial_compare_func); | |
1146 | ||
1147 | if (!j->inotify_wd_dirs || !j->inotify_wd_roots) { | |
1148 | r = -ENOMEM; | |
1149 | goto fail; | |
1150 | } | |
1151 | ||
3fbf9cbb LP |
1152 | /* We ignore most errors here, since the idea is to only open |
1153 | * what's actually accessible, and ignore the rest. */ | |
260a2be4 | 1154 | |
87d2c1ff LP |
1155 | NULSTR_FOREACH(p, search_paths) { |
1156 | DIR *d; | |
1157 | ||
cf244689 LP |
1158 | if ((flags & SD_JOURNAL_RUNTIME_ONLY) && |
1159 | !path_startswith(p, "/run")) | |
1160 | continue; | |
1161 | ||
87d2c1ff LP |
1162 | d = opendir(p); |
1163 | if (!d) { | |
3fbf9cbb LP |
1164 | if (errno != ENOENT) |
1165 | log_debug("Failed to open %s: %m", p); | |
87d2c1ff LP |
1166 | continue; |
1167 | } | |
1168 | ||
50f20cfd LP |
1169 | add_root_wd(j, p); |
1170 | ||
87d2c1ff LP |
1171 | for (;;) { |
1172 | struct dirent buf, *de; | |
3fbf9cbb | 1173 | sd_id128_t id; |
87d2c1ff | 1174 | |
3fbf9cbb LP |
1175 | r = readdir_r(d, &buf, &de); |
1176 | if (r != 0 || !de) | |
87d2c1ff LP |
1177 | break; |
1178 | ||
3fbf9cbb LP |
1179 | if (dirent_is_file_with_suffix(de, ".journal")) { |
1180 | r = add_file(j, p, NULL, de->d_name); | |
1181 | if (r < 0) | |
1182 | log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r)); | |
87d2c1ff | 1183 | |
3fbf9cbb LP |
1184 | } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) && |
1185 | sd_id128_from_string(de->d_name, &id) >= 0) { | |
87d2c1ff | 1186 | |
3fbf9cbb LP |
1187 | r = add_directory(j, p, de->d_name); |
1188 | if (r < 0) | |
1189 | log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r)); | |
260a2be4 LP |
1190 | } |
1191 | } | |
3fbf9cbb LP |
1192 | |
1193 | closedir(d); | |
87d2c1ff LP |
1194 | } |
1195 | ||
1196 | *ret = j; | |
1197 | return 0; | |
1198 | ||
1199 | fail: | |
1200 | sd_journal_close(j); | |
1201 | ||
1202 | return r; | |
1203 | }; | |
1204 | ||
a5344d2c LP |
1205 | _public_ void sd_journal_close(sd_journal *j) { |
1206 | if (!j) | |
1207 | return; | |
87d2c1ff | 1208 | |
50f20cfd LP |
1209 | if (j->inotify_wd_dirs) { |
1210 | void *k; | |
1211 | ||
1212 | while ((k = hashmap_first_key(j->inotify_wd_dirs))) | |
1213 | remove_directory_wd(j, PTR_TO_INT(k)); | |
1214 | ||
1215 | hashmap_free(j->inotify_wd_dirs); | |
1216 | } | |
1217 | ||
1218 | if (j->inotify_wd_roots) { | |
1219 | void *k; | |
1220 | ||
1221 | while ((k = hashmap_first_key(j->inotify_wd_roots))) | |
1222 | remove_root_wd(j, PTR_TO_INT(k)); | |
1223 | ||
1224 | hashmap_free(j->inotify_wd_roots); | |
1225 | } | |
1226 | ||
260a2be4 LP |
1227 | if (j->files) { |
1228 | JournalFile *f; | |
1229 | ||
1230 | while ((f = hashmap_steal_first(j->files))) | |
1231 | journal_file_close(f); | |
1232 | ||
1233 | hashmap_free(j->files); | |
1234 | } | |
87d2c1ff | 1235 | |
1cc101f1 LP |
1236 | sd_journal_flush_matches(j); |
1237 | ||
50f20cfd LP |
1238 | if (j->inotify_fd >= 0) |
1239 | close_nointr_nofail(j->inotify_fd); | |
1240 | ||
87d2c1ff LP |
1241 | free(j); |
1242 | } | |
3fbf9cbb | 1243 | |
a5344d2c | 1244 | _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) { |
3fbf9cbb LP |
1245 | Object *o; |
1246 | JournalFile *f; | |
1247 | int r; | |
1248 | ||
a5344d2c LP |
1249 | if (!j) |
1250 | return -EINVAL; | |
1251 | if (!ret) | |
1252 | return -EINVAL; | |
3fbf9cbb LP |
1253 | |
1254 | f = j->current_file; | |
1255 | if (!f) | |
de190aef | 1256 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
1257 | |
1258 | if (f->current_offset <= 0) | |
de190aef | 1259 | return -EADDRNOTAVAIL; |
3fbf9cbb | 1260 | |
de190aef | 1261 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
1262 | if (r < 0) |
1263 | return r; | |
1264 | ||
1265 | *ret = le64toh(o->entry.realtime); | |
de190aef | 1266 | return 0; |
3fbf9cbb LP |
1267 | } |
1268 | ||
a5344d2c | 1269 | _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) { |
3fbf9cbb LP |
1270 | Object *o; |
1271 | JournalFile *f; | |
1272 | int r; | |
1273 | sd_id128_t id; | |
1274 | ||
a5344d2c LP |
1275 | if (!j) |
1276 | return -EINVAL; | |
1277 | if (!ret) | |
1278 | return -EINVAL; | |
3fbf9cbb LP |
1279 | |
1280 | f = j->current_file; | |
1281 | if (!f) | |
de190aef | 1282 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
1283 | |
1284 | if (f->current_offset <= 0) | |
de190aef | 1285 | return -EADDRNOTAVAIL; |
3fbf9cbb | 1286 | |
de190aef | 1287 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
1288 | if (r < 0) |
1289 | return r; | |
1290 | ||
de190aef LP |
1291 | if (ret_boot_id) |
1292 | *ret_boot_id = o->entry.boot_id; | |
1293 | else { | |
1294 | r = sd_id128_get_boot(&id); | |
1295 | if (r < 0) | |
1296 | return r; | |
3fbf9cbb | 1297 | |
de190aef LP |
1298 | if (!sd_id128_equal(id, o->entry.boot_id)) |
1299 | return -ENOENT; | |
1300 | } | |
3fbf9cbb LP |
1301 | |
1302 | *ret = le64toh(o->entry.monotonic); | |
de190aef | 1303 | return 0; |
3fbf9cbb LP |
1304 | } |
1305 | ||
a5344d2c | 1306 | _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) { |
3fbf9cbb LP |
1307 | JournalFile *f; |
1308 | uint64_t i, n; | |
1309 | size_t field_length; | |
1310 | int r; | |
1311 | Object *o; | |
1312 | ||
a5344d2c LP |
1313 | if (!j) |
1314 | return -EINVAL; | |
1315 | if (!field) | |
1316 | return -EINVAL; | |
1317 | if (!data) | |
1318 | return -EINVAL; | |
1319 | if (!size) | |
1320 | return -EINVAL; | |
3fbf9cbb LP |
1321 | |
1322 | if (isempty(field) || strchr(field, '=')) | |
1323 | return -EINVAL; | |
1324 | ||
1325 | f = j->current_file; | |
1326 | if (!f) | |
de190aef | 1327 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
1328 | |
1329 | if (f->current_offset <= 0) | |
de190aef | 1330 | return -EADDRNOTAVAIL; |
3fbf9cbb | 1331 | |
de190aef | 1332 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
1333 | if (r < 0) |
1334 | return r; | |
1335 | ||
1336 | field_length = strlen(field); | |
1337 | ||
1338 | n = journal_file_entry_n_items(o); | |
1339 | for (i = 0; i < n; i++) { | |
de190aef | 1340 | uint64_t p, l, le_hash; |
3fbf9cbb LP |
1341 | size_t t; |
1342 | ||
1343 | p = le64toh(o->entry.items[i].object_offset); | |
807e17f0 | 1344 | le_hash = o->entry.items[i].hash; |
de190aef | 1345 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
3fbf9cbb LP |
1346 | if (r < 0) |
1347 | return r; | |
1348 | ||
de190aef | 1349 | if (le_hash != o->data.hash) |
de7b95cd LP |
1350 | return -EBADMSG; |
1351 | ||
3fbf9cbb LP |
1352 | l = le64toh(o->object.size) - offsetof(Object, data.payload); |
1353 | ||
807e17f0 LP |
1354 | if (o->object.flags & OBJECT_COMPRESSED) { |
1355 | ||
1356 | #ifdef HAVE_XZ | |
1357 | if (uncompress_startswith(o->data.payload, l, | |
1358 | &f->compress_buffer, &f->compress_buffer_size, | |
1359 | field, field_length, '=')) { | |
1360 | ||
1361 | uint64_t rsize; | |
1362 | ||
1363 | if (!uncompress_blob(o->data.payload, l, | |
1364 | &f->compress_buffer, &f->compress_buffer_size, &rsize)) | |
1365 | return -EBADMSG; | |
1366 | ||
1367 | *data = f->compress_buffer; | |
1368 | *size = (size_t) rsize; | |
1369 | ||
1370 | return 0; | |
1371 | } | |
1372 | #else | |
1373 | return -EPROTONOSUPPORT; | |
1374 | #endif | |
1375 | ||
1376 | } else if (l >= field_length+1 && | |
1377 | memcmp(o->data.payload, field, field_length) == 0 && | |
1378 | o->data.payload[field_length] == '=') { | |
3fbf9cbb | 1379 | |
161e54f8 | 1380 | t = (size_t) l; |
3fbf9cbb | 1381 | |
161e54f8 LP |
1382 | if ((uint64_t) t != l) |
1383 | return -E2BIG; | |
3fbf9cbb | 1384 | |
161e54f8 LP |
1385 | *data = o->data.payload; |
1386 | *size = t; | |
3fbf9cbb | 1387 | |
de190aef | 1388 | return 0; |
161e54f8 | 1389 | } |
3fbf9cbb | 1390 | |
de190aef | 1391 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
161e54f8 LP |
1392 | if (r < 0) |
1393 | return r; | |
3fbf9cbb LP |
1394 | } |
1395 | ||
de190aef | 1396 | return -ENOENT; |
3fbf9cbb LP |
1397 | } |
1398 | ||
a5344d2c | 1399 | _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) { |
3fbf9cbb | 1400 | JournalFile *f; |
de190aef | 1401 | uint64_t p, l, n, le_hash; |
3fbf9cbb LP |
1402 | int r; |
1403 | Object *o; | |
de190aef | 1404 | size_t t; |
3fbf9cbb | 1405 | |
a5344d2c LP |
1406 | if (!j) |
1407 | return -EINVAL; | |
1408 | if (!data) | |
1409 | return -EINVAL; | |
1410 | if (!size) | |
1411 | return -EINVAL; | |
3fbf9cbb LP |
1412 | |
1413 | f = j->current_file; | |
1414 | if (!f) | |
de190aef | 1415 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
1416 | |
1417 | if (f->current_offset <= 0) | |
de190aef | 1418 | return -EADDRNOTAVAIL; |
3fbf9cbb | 1419 | |
de190aef | 1420 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
1421 | if (r < 0) |
1422 | return r; | |
1423 | ||
1424 | n = journal_file_entry_n_items(o); | |
7210bfb3 | 1425 | if (j->current_field >= n) |
3fbf9cbb LP |
1426 | return 0; |
1427 | ||
7210bfb3 | 1428 | p = le64toh(o->entry.items[j->current_field].object_offset); |
de190aef LP |
1429 | le_hash = o->entry.items[j->current_field].hash; |
1430 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
3fbf9cbb LP |
1431 | if (r < 0) |
1432 | return r; | |
1433 | ||
de190aef | 1434 | if (le_hash != o->data.hash) |
de7b95cd LP |
1435 | return -EBADMSG; |
1436 | ||
3fbf9cbb LP |
1437 | l = le64toh(o->object.size) - offsetof(Object, data.payload); |
1438 | t = (size_t) l; | |
1439 | ||
1440 | /* We can't read objects larger than 4G on a 32bit machine */ | |
1441 | if ((uint64_t) t != l) | |
1442 | return -E2BIG; | |
1443 | ||
807e17f0 LP |
1444 | if (o->object.flags & OBJECT_COMPRESSED) { |
1445 | #ifdef HAVE_XZ | |
1446 | uint64_t rsize; | |
1447 | ||
1448 | if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize)) | |
1449 | return -EBADMSG; | |
1450 | ||
1451 | *data = f->compress_buffer; | |
1452 | *size = (size_t) rsize; | |
1453 | #else | |
1454 | return -EPROTONOSUPPORT; | |
1455 | #endif | |
1456 | } else { | |
1457 | *data = o->data.payload; | |
1458 | *size = t; | |
1459 | } | |
3fbf9cbb | 1460 | |
7210bfb3 | 1461 | j->current_field ++; |
3fbf9cbb LP |
1462 | |
1463 | return 1; | |
1464 | } | |
c2373f84 | 1465 | |
a5344d2c LP |
1466 | _public_ void sd_journal_restart_data(sd_journal *j) { |
1467 | if (!j) | |
1468 | return; | |
8725d60a LP |
1469 | |
1470 | j->current_field = 0; | |
c2373f84 | 1471 | } |
50f20cfd | 1472 | |
a5344d2c LP |
1473 | _public_ int sd_journal_get_fd(sd_journal *j) { |
1474 | if (!j) | |
1475 | return -EINVAL; | |
50f20cfd LP |
1476 | |
1477 | return j->inotify_fd; | |
1478 | } | |
1479 | ||
1480 | static void process_inotify_event(sd_journal *j, struct inotify_event *e) { | |
1481 | char *p; | |
1482 | int r; | |
1483 | ||
1484 | assert(j); | |
1485 | assert(e); | |
1486 | ||
1487 | /* Is this a subdirectory we watch? */ | |
1488 | p = hashmap_get(j->inotify_wd_dirs, INT_TO_PTR(e->wd)); | |
1489 | if (p) { | |
1490 | ||
1491 | if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) { | |
1492 | ||
1493 | /* Event for a journal file */ | |
1494 | ||
1495 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) { | |
1496 | r = add_file(j, p, NULL, e->name); | |
1497 | if (r < 0) | |
1498 | log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r)); | |
1499 | } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) { | |
1500 | ||
1501 | r = remove_file(j, p, NULL, e->name); | |
1502 | if (r < 0) | |
1503 | log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r)); | |
1504 | } | |
1505 | ||
1506 | } else if (e->len == 0) { | |
1507 | ||
1508 | /* Event for the directory itself */ | |
1509 | ||
1510 | if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) | |
1511 | remove_directory_wd(j, e->wd); | |
1512 | } | |
1513 | ||
1514 | return; | |
1515 | } | |
1516 | ||
1517 | /* Must be the root directory then? */ | |
1518 | p = hashmap_get(j->inotify_wd_roots, INT_TO_PTR(e->wd)); | |
1519 | if (p) { | |
1520 | sd_id128_t id; | |
1521 | ||
1522 | if (!(e->mask & IN_ISDIR) && e->len > 0 && endswith(e->name, ".journal")) { | |
1523 | ||
1524 | /* Event for a journal file */ | |
1525 | ||
1526 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) { | |
1527 | r = add_file(j, p, NULL, e->name); | |
1528 | if (r < 0) | |
1529 | log_debug("Failed to add file %s/%s: %s", p, e->name, strerror(-r)); | |
1530 | } else if (e->mask & (IN_DELETE|IN_UNMOUNT)) { | |
1531 | ||
1532 | r = remove_file(j, p, NULL, e->name); | |
1533 | if (r < 0) | |
1534 | log_debug("Failed to remove file %s/%s: %s", p, e->name, strerror(-r)); | |
1535 | } | |
1536 | ||
1537 | } else if ((e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) { | |
1538 | ||
1539 | /* Event for subdirectory */ | |
1540 | ||
1541 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) { | |
1542 | ||
1543 | r = add_directory(j, p, e->name); | |
1544 | if (r < 0) | |
1545 | log_debug("Failed to add directory %s/%s: %s", p, e->name, strerror(-r)); | |
1546 | } | |
1547 | } | |
1548 | ||
1549 | return; | |
1550 | } | |
1551 | ||
1552 | if (e->mask & IN_IGNORED) | |
1553 | return; | |
1554 | ||
1555 | log_warning("Unknown inotify event."); | |
1556 | } | |
1557 | ||
a5344d2c | 1558 | _public_ int sd_journal_process(sd_journal *j) { |
50f20cfd LP |
1559 | uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX]; |
1560 | ||
a5344d2c LP |
1561 | if (!j) |
1562 | return -EINVAL; | |
50f20cfd LP |
1563 | |
1564 | for (;;) { | |
1565 | struct inotify_event *e; | |
1566 | ssize_t l; | |
1567 | ||
1568 | l = read(j->inotify_fd, buffer, sizeof(buffer)); | |
1569 | if (l < 0) { | |
1570 | if (errno == EINTR || errno == EAGAIN) | |
1571 | return 0; | |
1572 | ||
1573 | return -errno; | |
1574 | } | |
1575 | ||
1576 | e = (struct inotify_event*) buffer; | |
1577 | while (l > 0) { | |
1578 | size_t step; | |
1579 | ||
1580 | process_inotify_event(j, e); | |
1581 | ||
1582 | step = sizeof(struct inotify_event) + e->len; | |
1583 | assert(step <= (size_t) l); | |
1584 | ||
1585 | e = (struct inotify_event*) ((uint8_t*) e + step); | |
1586 | l -= step; | |
1587 | } | |
1588 | } | |
1589 | } | |
6ad1d1c3 | 1590 | |
a5344d2c LP |
1591 | _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { |
1592 | if (!j) | |
1593 | return -EINVAL; | |
1594 | if (!field) | |
1595 | return -EINVAL; | |
6ad1d1c3 LP |
1596 | |
1597 | return -ENOTSUP; | |
1598 | } | |
1599 | ||
a5344d2c LP |
1600 | _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) { |
1601 | if (!j) | |
1602 | return -EINVAL; | |
1603 | if (!data) | |
1604 | return -EINVAL; | |
1605 | if (!l) | |
1606 | return -EINVAL; | |
6ad1d1c3 LP |
1607 | |
1608 | return -ENOTSUP; | |
1609 | } | |
1610 | ||
a5344d2c LP |
1611 | _public_ void sd_journal_restart_unique(sd_journal *j) { |
1612 | if (!j) | |
1613 | return; | |
6ad1d1c3 | 1614 | } |