]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/sd-journal.c
tree-wide: use mfree more
[thirdparty/systemd.git] / src / journal / sd-journal.c
CommitLineData
87d2c1ff
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
87d2c1ff
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
87d2c1ff 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
87d2c1ff
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
87d2c1ff 20#include <errno.h>
87d2c1ff 21#include <fcntl.h>
82e24b00 22#include <inttypes.h>
07630cea
LP
23#include <linux/magic.h>
24#include <poll.h>
3fbf9cbb 25#include <stddef.h>
50f20cfd 26#include <sys/inotify.h>
85210bff 27#include <sys/vfs.h>
07630cea 28#include <unistd.h>
87d2c1ff
LP
29
30#include "sd-journal.h"
c004493c 31
b5efdb8a 32#include "alloc-util.h"
07630cea
LP
33#include "catalog.h"
34#include "compress.h"
a0956174 35#include "dirent-util.h"
3ffd4af2 36#include "fd-util.h"
07630cea
LP
37#include "fileio.h"
38#include "formats-util.h"
77601719 39#include "fs-util.h"
07630cea
LP
40#include "hashmap.h"
41#include "hostname-util.h"
c004493c 42#include "io-util.h"
87d2c1ff 43#include "journal-def.h"
cec736d2 44#include "journal-file.h"
07630cea 45#include "journal-internal.h"
cec736d2 46#include "list.h"
de7b95cd 47#include "lookup3.h"
85210bff 48#include "missing.h"
07630cea 49#include "path-util.h"
d4205751 50#include "replace-var.h"
15a5e950
LP
51#include "stat-util.h"
52#include "stdio-util.h"
07630cea
LP
53#include "string-util.h"
54#include "strv.h"
87d2c1ff 55
9e8abdf0
LP
56#define JOURNAL_FILES_MAX 7168
57
85210bff
LP
58#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
59
d4205751
LP
60#define REPLACE_VAR_MAX 256
61
93b73b06
LP
62#define DEFAULT_DATA_THRESHOLD (64*1024)
63
a9a245c1
ZJS
64static void remove_file_real(sd_journal *j, JournalFile *f);
65
a65f06bb
ZJS
66static bool journal_pid_changed(sd_journal *j) {
67 assert(j);
68
69 /* We don't support people creating a journal object and
70 * keeping it around over a fork(). Let's complain. */
71
72 return j->original_pid != getpid();
73}
74
5768d259
LP
75static int journal_put_error(sd_journal *j, int r, const char *path) {
76 char *copy;
3ac251b8
LP
77 int k;
78
5768d259
LP
79 /* Memorize an error we encountered, and store which
80 * file/directory it was generated from. Note that we store
81 * only *one* path per error code, as the error code is the
82 * key into the hashmap, and the path is the value. This means
83 * we keep track only of all error kinds, but not of all error
84 * locations. This has the benefit that the hashmap cannot
85 * grow beyond bounds.
86 *
87 * We return an error here only if we didn't manage to
88 * memorize the real error. */
89
6fe391c5
ZJS
90 if (r >= 0)
91 return r;
92
5768d259 93 k = hashmap_ensure_allocated(&j->errors, NULL);
3ac251b8
LP
94 if (k < 0)
95 return k;
96
5768d259
LP
97 if (path) {
98 copy = strdup(path);
99 if (!copy)
100 return -ENOMEM;
101 } else
102 copy = NULL;
103
104 k = hashmap_put(j->errors, INT_TO_PTR(r), copy);
105 if (k < 0) {
106 free(copy);
107
108 if (k == -EEXIST)
109 return 0;
110
111 return k;
112 }
113
114 return 0;
6fe391c5
ZJS
115}
116
de190aef 117static void detach_location(sd_journal *j) {
8f9b6cd9
LP
118 Iterator i;
119 JournalFile *f;
120
121 assert(j);
122
123 j->current_file = NULL;
124 j->current_field = 0;
125
c1f906bd 126 ORDERED_HASHMAP_FOREACH(f, j->files, i)
1fc605b0 127 journal_file_reset_location(f);
8f9b6cd9
LP
128}
129
de190aef
LP
130static void reset_location(sd_journal *j) {
131 assert(j);
132
133 detach_location(j);
134 zero(j->current_location);
135}
136
a87247dd 137static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
de190aef 138 assert(l);
a87247dd 139 assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
de190aef
LP
140 assert(f);
141 assert(o->object.type == OBJECT_ENTRY);
142
a87247dd 143 l->type = type;
de190aef
LP
144 l->seqnum = le64toh(o->entry.seqnum);
145 l->seqnum_id = f->header->seqnum_id;
146 l->realtime = le64toh(o->entry.realtime);
147 l->monotonic = le64toh(o->entry.monotonic);
ce3fd7e7 148 l->boot_id = o->entry.boot_id;
de190aef
LP
149 l->xor_hash = le64toh(o->entry.xor_hash);
150
151 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
152}
153
1eb6332d 154static void set_location(sd_journal *j, JournalFile *f, Object *o) {
de190aef
LP
155 assert(j);
156 assert(f);
157 assert(o);
158
1eb6332d 159 init_location(&j->current_location, LOCATION_DISCRETE, f, o);
de190aef
LP
160
161 j->current_file = f;
162 j->current_field = 0;
163
6573ef05
MS
164 /* Let f know its candidate entry was picked. */
165 assert(f->location_type == LOCATION_SEEK);
166 f->location_type = LOCATION_DISCRETE;
de190aef
LP
167}
168
cbdca852
LP
169static int match_is_valid(const void *data, size_t size) {
170 const char *b, *p;
171
172 assert(data);
173
174 if (size < 2)
175 return false;
176
177 if (startswith(data, "__"))
178 return false;
179
180 b = data;
181 for (p = b; p < b + size; p++) {
182
183 if (*p == '=')
184 return p > b;
185
186 if (*p == '_')
187 continue;
188
189 if (*p >= 'A' && *p <= 'Z')
190 continue;
191
192 if (*p >= '0' && *p <= '9')
193 continue;
194
195 return false;
196 }
197
198 return false;
199}
200
201static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
de190aef
LP
202 const uint8_t *a = _a, *b = _b;
203 size_t j;
de190aef
LP
204
205 for (j = 0; j < s && j < t; j++) {
206
de190aef 207 if (a[j] != b[j])
cbdca852 208 return false;
de190aef 209
cbdca852
LP
210 if (a[j] == '=')
211 return true;
de190aef
LP
212 }
213
bc302926 214 assert_not_reached("\"=\" not found");
cbdca852
LP
215}
216
217static Match *match_new(Match *p, MatchType t) {
218 Match *m;
219
220 m = new0(Match, 1);
221 if (!m)
222 return NULL;
223
224 m->type = t;
225
226 if (p) {
227 m->parent = p;
71fda00f 228 LIST_PREPEND(matches, p->matches, m);
cbdca852
LP
229 }
230
231 return m;
232}
233
234static void match_free(Match *m) {
235 assert(m);
236
237 while (m->matches)
238 match_free(m->matches);
239
240 if (m->parent)
71fda00f 241 LIST_REMOVE(matches, m->parent->matches, m);
cbdca852
LP
242
243 free(m->data);
244 free(m);
245}
246
247static void match_free_if_empty(Match *m) {
c5a10d9c 248 if (!m || m->matches)
cbdca852
LP
249 return;
250
251 match_free(m);
de190aef
LP
252}
253
a5344d2c 254_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
cd34b3c6 255 Match *l3, *l4, *add_here = NULL, *m;
4fd052ae 256 le64_t le_hash;
87d2c1ff 257
1ae464e0
TA
258 assert_return(j, -EINVAL);
259 assert_return(!journal_pid_changed(j), -ECHILD);
260 assert_return(data, -EINVAL);
cbdca852
LP
261
262 if (size == 0)
263 size = strlen(data);
264
1ae464e0 265 assert_return(match_is_valid(data, size), -EINVAL);
1cc101f1 266
cd34b3c6
HH
267 /* level 0: AND term
268 * level 1: OR terms
269 * level 2: AND terms
270 * level 3: OR terms
271 * level 4: concrete matches */
cbdca852
LP
272
273 if (!j->level0) {
cd34b3c6 274 j->level0 = match_new(NULL, MATCH_AND_TERM);
cbdca852
LP
275 if (!j->level0)
276 return -ENOMEM;
277 }
278
279 if (!j->level1) {
cd34b3c6 280 j->level1 = match_new(j->level0, MATCH_OR_TERM);
cbdca852
LP
281 if (!j->level1)
282 return -ENOMEM;
283 }
284
cd34b3c6
HH
285 if (!j->level2) {
286 j->level2 = match_new(j->level1, MATCH_AND_TERM);
287 if (!j->level2)
288 return -ENOMEM;
289 }
290
291 assert(j->level0->type == MATCH_AND_TERM);
292 assert(j->level1->type == MATCH_OR_TERM);
293 assert(j->level2->type == MATCH_AND_TERM);
ab4979d2 294
de190aef
LP
295 le_hash = htole64(hash64(data, size));
296
cd34b3c6
HH
297 LIST_FOREACH(matches, l3, j->level2->matches) {
298 assert(l3->type == MATCH_OR_TERM);
de190aef 299
cd34b3c6
HH
300 LIST_FOREACH(matches, l4, l3->matches) {
301 assert(l4->type == MATCH_DISCRETE);
de190aef 302
cbdca852
LP
303 /* Exactly the same match already? Then ignore
304 * this addition */
cd34b3c6
HH
305 if (l4->le_hash == le_hash &&
306 l4->size == size &&
307 memcmp(l4->data, data, size) == 0)
cbdca852
LP
308 return 0;
309
310 /* Same field? Then let's add this to this OR term */
cd34b3c6
HH
311 if (same_field(data, size, l4->data, l4->size)) {
312 add_here = l3;
cbdca852
LP
313 break;
314 }
315 }
316
317 if (add_here)
318 break;
de190aef
LP
319 }
320
cbdca852 321 if (!add_here) {
cd34b3c6 322 add_here = match_new(j->level2, MATCH_OR_TERM);
cbdca852
LP
323 if (!add_here)
324 goto fail;
325 }
326
327 m = match_new(add_here, MATCH_DISCRETE);
cec736d2 328 if (!m)
cbdca852 329 goto fail;
87d2c1ff 330
cbdca852 331 m->le_hash = le_hash;
1cc101f1 332 m->size = size;
cbdca852
LP
333 m->data = memdup(data, size);
334 if (!m->data)
335 goto fail;
336
337 detach_location(j);
338
339 return 0;
340
341fail:
c5a10d9c
ZJS
342 match_free_if_empty(add_here);
343 match_free_if_empty(j->level2);
344 match_free_if_empty(j->level1);
345 match_free_if_empty(j->level0);
cbdca852
LP
346
347 return -ENOMEM;
348}
349
cd34b3c6 350_public_ int sd_journal_add_conjunction(sd_journal *j) {
1ae464e0
TA
351 assert_return(j, -EINVAL);
352 assert_return(!journal_pid_changed(j), -ECHILD);
1cc101f1 353
cbdca852
LP
354 if (!j->level0)
355 return 0;
356
357 if (!j->level1)
358 return 0;
359
360 if (!j->level1->matches)
361 return 0;
362
cd34b3c6
HH
363 j->level1 = NULL;
364 j->level2 = NULL;
365
366 return 0;
367}
368
369_public_ int sd_journal_add_disjunction(sd_journal *j) {
1ae464e0
TA
370 assert_return(j, -EINVAL);
371 assert_return(!journal_pid_changed(j), -ECHILD);
cd34b3c6
HH
372
373 if (!j->level0)
374 return 0;
375
376 if (!j->level1)
377 return 0;
378
379 if (!j->level2)
380 return 0;
381
382 if (!j->level2->matches)
383 return 0;
cbdca852 384
cd34b3c6 385 j->level2 = NULL;
cbdca852
LP
386 return 0;
387}
388
389static char *match_make_string(Match *m) {
6b430fdb 390 char *p = NULL, *r;
cbdca852
LP
391 Match *i;
392 bool enclose = false;
393
394 if (!m)
4ad16808 395 return strdup("none");
cbdca852
LP
396
397 if (m->type == MATCH_DISCRETE)
398 return strndup(m->data, m->size);
399
cbdca852
LP
400 LIST_FOREACH(matches, i, m->matches) {
401 char *t, *k;
402
403 t = match_make_string(i);
6b430fdb
ZJS
404 if (!t)
405 return mfree(p);
cbdca852
LP
406
407 if (p) {
b7def684 408 k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
cbdca852
LP
409 free(p);
410 free(t);
411
412 if (!k)
413 return NULL;
414
415 p = k;
416
417 enclose = true;
bc302926 418 } else
cbdca852 419 p = t;
87d2c1ff
LP
420 }
421
cbdca852 422 if (enclose) {
b7def684 423 r = strjoin("(", p, ")", NULL);
cbdca852
LP
424 free(p);
425 return r;
426 }
87d2c1ff 427
cbdca852
LP
428 return p;
429}
de7b95cd 430
cbdca852
LP
431char *journal_make_match_string(sd_journal *j) {
432 assert(j);
8f9b6cd9 433
cbdca852 434 return match_make_string(j->level0);
87d2c1ff
LP
435}
436
a5344d2c
LP
437_public_ void sd_journal_flush_matches(sd_journal *j) {
438 if (!j)
439 return;
87d2c1ff 440
cbdca852
LP
441 if (j->level0)
442 match_free(j->level0);
de7b95cd 443
cd34b3c6 444 j->level0 = j->level1 = j->level2 = NULL;
8f9b6cd9 445
de190aef 446 detach_location(j);
87d2c1ff
LP
447}
448
487d3720
MS
449_pure_ static int compare_with_location(JournalFile *f, Location *l) {
450 assert(f);
de190aef 451 assert(l);
487d3720 452 assert(f->location_type == LOCATION_SEEK);
a87247dd 453 assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
de190aef
LP
454
455 if (l->monotonic_set &&
487d3720 456 sd_id128_equal(f->current_boot_id, l->boot_id) &&
de190aef 457 l->realtime_set &&
487d3720 458 f->current_realtime == l->realtime &&
de190aef 459 l->xor_hash_set &&
487d3720 460 f->current_xor_hash == l->xor_hash)
de190aef
LP
461 return 0;
462
463 if (l->seqnum_set &&
487d3720 464 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
de190aef 465
487d3720 466 if (f->current_seqnum < l->seqnum)
de190aef 467 return -1;
487d3720 468 if (f->current_seqnum > l->seqnum)
de190aef
LP
469 return 1;
470 }
471
472 if (l->monotonic_set &&
487d3720 473 sd_id128_equal(f->current_boot_id, l->boot_id)) {
de190aef 474
487d3720 475 if (f->current_monotonic < l->monotonic)
de190aef 476 return -1;
487d3720 477 if (f->current_monotonic > l->monotonic)
de190aef
LP
478 return 1;
479 }
480
481 if (l->realtime_set) {
482
487d3720 483 if (f->current_realtime < l->realtime)
de190aef 484 return -1;
487d3720 485 if (f->current_realtime > l->realtime)
de190aef
LP
486 return 1;
487 }
488
489 if (l->xor_hash_set) {
de190aef 490
487d3720 491 if (f->current_xor_hash < l->xor_hash)
de190aef 492 return -1;
487d3720 493 if (f->current_xor_hash > l->xor_hash)
de190aef
LP
494 return 1;
495 }
496
497 return 0;
498}
499
cbdca852
LP
500static int next_for_match(
501 sd_journal *j,
502 Match *m,
503 JournalFile *f,
504 uint64_t after_offset,
505 direction_t direction,
506 Object **ret,
507 uint64_t *offset) {
508
de7b95cd 509 int r;
cbdca852
LP
510 uint64_t np = 0;
511 Object *n;
de7b95cd
LP
512
513 assert(j);
cbdca852
LP
514 assert(m);
515 assert(f);
de7b95cd 516
cbdca852
LP
517 if (m->type == MATCH_DISCRETE) {
518 uint64_t dp;
de190aef 519
cbdca852 520 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
de190aef
LP
521 if (r <= 0)
522 return r;
523
cbdca852 524 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
de190aef 525
cbdca852
LP
526 } else if (m->type == MATCH_OR_TERM) {
527 Match *i;
de7b95cd 528
cbdca852 529 /* Find the earliest match beyond after_offset */
de190aef 530
cbdca852
LP
531 LIST_FOREACH(matches, i, m->matches) {
532 uint64_t cp;
de190aef 533
cbdca852 534 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
b4e5f920
LP
535 if (r < 0)
536 return r;
cbdca852 537 else if (r > 0) {
bc302926 538 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
cbdca852
LP
539 np = cp;
540 }
541 }
b4e5f920 542
bc302926
ZJS
543 if (np == 0)
544 return 0;
545
cbdca852 546 } else if (m->type == MATCH_AND_TERM) {
2bc8ca0c 547 Match *i, *last_moved;
de190aef 548
cbdca852 549 /* Always jump to the next matching entry and repeat
2bc8ca0c 550 * this until we find an offset that matches for all
cbdca852 551 * matches. */
de190aef 552
cbdca852
LP
553 if (!m->matches)
554 return 0;
de7b95cd 555
2bc8ca0c
ZJS
556 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
557 if (r <= 0)
558 return r;
de190aef 559
2bc8ca0c
ZJS
560 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
561 last_moved = m->matches;
de190aef 562
2bc8ca0c
ZJS
563 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
564 uint64_t cp;
de190aef 565
2bc8ca0c
ZJS
566 r = next_for_match(j, i, f, np, direction, NULL, &cp);
567 if (r <= 0)
568 return r;
de190aef 569
2bc8ca0c
ZJS
570 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
571 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
572 np = cp;
573 last_moved = i;
de190aef 574 }
2bc8ca0c 575 }
cbdca852 576 }
de190aef 577
bc302926 578 assert(np > 0);
de190aef 579
cbdca852
LP
580 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
581 if (r < 0)
582 return r;
de7b95cd 583
de190aef 584 if (ret)
cbdca852 585 *ret = n;
de190aef 586 if (offset)
cbdca852 587 *offset = np;
de190aef
LP
588
589 return 1;
590}
591
cbdca852
LP
592static int find_location_for_match(
593 sd_journal *j,
594 Match *m,
595 JournalFile *f,
596 direction_t direction,
597 Object **ret,
598 uint64_t *offset) {
599
de190aef 600 int r;
de190aef
LP
601
602 assert(j);
cbdca852 603 assert(m);
de190aef 604 assert(f);
de190aef 605
cbdca852
LP
606 if (m->type == MATCH_DISCRETE) {
607 uint64_t dp;
de190aef 608
cbdca852 609 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
de7b95cd
LP
610 if (r <= 0)
611 return r;
612
cbdca852 613 /* FIXME: missing: find by monotonic */
de7b95cd 614
cbdca852
LP
615 if (j->current_location.type == LOCATION_HEAD)
616 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
617 if (j->current_location.type == LOCATION_TAIL)
618 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
619 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
620 return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
621 if (j->current_location.monotonic_set) {
622 r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
623 if (r != -ENOENT)
624 return r;
625 }
626 if (j->current_location.realtime_set)
627 return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
de190aef 628
cbdca852 629 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
de7b95cd 630
cbdca852
LP
631 } else if (m->type == MATCH_OR_TERM) {
632 uint64_t np = 0;
633 Object *n;
634 Match *i;
de7b95cd 635
cbdca852 636 /* Find the earliest match */
de7b95cd 637
cbdca852
LP
638 LIST_FOREACH(matches, i, m->matches) {
639 uint64_t cp;
640
641 r = find_location_for_match(j, i, f, direction, NULL, &cp);
642 if (r < 0)
643 return r;
644 else if (r > 0) {
645 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
646 np = cp;
de190aef 647 }
cbdca852 648 }
de190aef 649
cbdca852
LP
650 if (np == 0)
651 return 0;
de7b95cd 652
cbdca852
LP
653 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
654 if (r < 0)
655 return r;
de7b95cd 656
cbdca852
LP
657 if (ret)
658 *ret = n;
659 if (offset)
660 *offset = np;
de190aef 661
cbdca852 662 return 1;
e892bd17 663
cbdca852
LP
664 } else {
665 Match *i;
666 uint64_t np = 0;
667
668 assert(m->type == MATCH_AND_TERM);
669
670 /* First jump to the last match, and then find the
671 * next one where all matches match */
672
673 if (!m->matches)
674 return 0;
675
676 LIST_FOREACH(matches, i, m->matches) {
677 uint64_t cp;
678
679 r = find_location_for_match(j, i, f, direction, NULL, &cp);
680 if (r <= 0)
4b067dc9
LP
681 return r;
682
bc302926 683 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
cbdca852 684 np = cp;
de7b95cd
LP
685 }
686
cbdca852
LP
687 return next_for_match(j, m, f, np, direction, ret, offset);
688 }
689}
de190aef 690
cbdca852
LP
691static int find_location_with_matches(
692 sd_journal *j,
693 JournalFile *f,
694 direction_t direction,
695 Object **ret,
696 uint64_t *offset) {
697
698 int r;
699
700 assert(j);
701 assert(f);
702 assert(ret);
703 assert(offset);
704
705 if (!j->level0) {
706 /* No matches is simple */
707
708 if (j->current_location.type == LOCATION_HEAD)
f534928a 709 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
cbdca852 710 if (j->current_location.type == LOCATION_TAIL)
f534928a 711 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
cbdca852
LP
712 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
713 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
714 if (j->current_location.monotonic_set) {
715 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
716 if (r != -ENOENT)
717 return r;
de7b95cd 718 }
cbdca852
LP
719 if (j->current_location.realtime_set)
720 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
de7b95cd 721
f534928a 722 return journal_file_next_entry(f, 0, direction, ret, offset);
cbdca852
LP
723 } else
724 return find_location_for_match(j, j->level0, f, direction, ret, offset);
725}
de7b95cd 726
cbdca852
LP
727static int next_with_matches(
728 sd_journal *j,
729 JournalFile *f,
730 direction_t direction,
731 Object **ret,
732 uint64_t *offset) {
733
cbdca852
LP
734 assert(j);
735 assert(f);
736 assert(ret);
737 assert(offset);
738
cbdca852
LP
739 /* No matches is easy. We simple advance the file
740 * pointer by one. */
741 if (!j->level0)
b29ddfcb 742 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
cbdca852
LP
743
744 /* If we have a match then we look for the next matching entry
49f43d5f 745 * with an offset at least one step larger */
b29ddfcb
MS
746 return next_for_match(j, j->level0, f,
747 direction == DIRECTION_DOWN ? f->current_offset + 1
748 : f->current_offset - 1,
749 direction, ret, offset);
de7b95cd
LP
750}
751
58439db4 752static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
de190aef 753 Object *c;
6e693b42 754 uint64_t cp, n_entries;
cbdca852 755 int r;
de190aef
LP
756
757 assert(j);
758 assert(f);
759
950c07d4
MS
760 n_entries = le64toh(f->header->n_entries);
761
762 /* If we hit EOF before, we don't need to look into this file again
763 * unless direction changed or new entries appeared. */
764 if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
765 n_entries == f->last_n_entries)
766 return 0;
668c965a 767
950c07d4
MS
768 f->last_n_entries = n_entries;
769
770 if (f->last_direction == direction && f->current_offset > 0) {
7943f422
MS
771 /* LOCATION_SEEK here means we did the work in a previous
772 * iteration and the current location already points to a
773 * candidate entry. */
487d3720
MS
774 if (f->location_type != LOCATION_SEEK) {
775 r = next_with_matches(j, f, direction, &c, &cp);
776 if (r <= 0)
777 return r;
466ccd92 778
950c07d4 779 journal_file_save_location(f, c, cp);
487d3720 780 }
de190aef 781 } else {
950c07d4
MS
782 f->last_direction = direction;
783
cbdca852 784 r = find_location_with_matches(j, f, direction, &c, &cp);
de190aef
LP
785 if (r <= 0)
786 return r;
487d3720 787
950c07d4 788 journal_file_save_location(f, c, cp);
de190aef
LP
789 }
790
bc302926 791 /* OK, we found the spot, now let's advance until an entry
cbdca852
LP
792 * that is actually different from what we were previously
793 * looking at. This is necessary to handle entries which exist
794 * in two (or more) journal files, and which shall all be
795 * suppressed but one. */
796
de190aef
LP
797 for (;;) {
798 bool found;
799
800 if (j->current_location.type == LOCATION_DISCRETE) {
801 int k;
802
487d3720 803 k = compare_with_location(f, &j->current_location);
1cdf7175
CH
804
805 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
de190aef
LP
806 } else
807 found = true;
808
487d3720 809 if (found)
de190aef 810 return 1;
de190aef
LP
811
812 r = next_with_matches(j, f, direction, &c, &cp);
813 if (r <= 0)
814 return r;
487d3720 815
950c07d4 816 journal_file_save_location(f, c, cp);
de190aef
LP
817 }
818}
819
e892bd17 820static int real_journal_next(sd_journal *j, direction_t direction) {
468b21de 821 JournalFile *f, *new_file = NULL;
cec736d2 822 Iterator i;
a002d44b 823 Object *o;
87d2c1ff
LP
824 int r;
825
1ae464e0
TA
826 assert_return(j, -EINVAL);
827 assert_return(!journal_pid_changed(j), -ECHILD);
87d2c1ff 828
c1f906bd 829 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
de190aef 830 bool found;
87d2c1ff 831
58439db4 832 r = next_beyond_location(j, f, direction);
e590af26 833 if (r < 0) {
da927ba9 834 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
a9a245c1 835 remove_file_real(j, f);
e590af26 836 continue;
6573ef05
MS
837 } else if (r == 0) {
838 f->location_type = LOCATION_TAIL;
cec736d2 839 continue;
6573ef05 840 }
87d2c1ff 841
468b21de 842 if (!new_file)
de190aef
LP
843 found = true;
844 else {
845 int k;
846
d8ae66d7 847 k = journal_file_compare_locations(f, new_file);
de190aef 848
bc302926 849 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
de190aef
LP
850 }
851
e499c999 852 if (found)
468b21de 853 new_file = f;
87d2c1ff
LP
854 }
855
468b21de 856 if (!new_file)
de190aef 857 return 0;
ae2cc8ef 858
e499c999 859 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
468b21de
LP
860 if (r < 0)
861 return r;
862
1eb6332d 863 set_location(j, new_file, o);
ae2cc8ef 864
de190aef
LP
865 return 1;
866}
ae2cc8ef 867
a5344d2c 868_public_ int sd_journal_next(sd_journal *j) {
de190aef
LP
869 return real_journal_next(j, DIRECTION_DOWN);
870}
ae2cc8ef 871
a5344d2c 872_public_ int sd_journal_previous(sd_journal *j) {
de190aef
LP
873 return real_journal_next(j, DIRECTION_UP);
874}
ae2cc8ef 875
6f003b43 876static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
de190aef 877 int c = 0, r;
ae2cc8ef 878
1ae464e0
TA
879 assert_return(j, -EINVAL);
880 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef 881
6f003b43
LP
882 if (skip == 0) {
883 /* If this is not a discrete skip, then at least
884 * resolve the current location */
885 if (j->current_location.type != LOCATION_DISCRETE)
886 return real_journal_next(j, direction);
887
888 return 0;
889 }
890
891 do {
892 r = real_journal_next(j, direction);
de190aef
LP
893 if (r < 0)
894 return r;
895
896 if (r == 0)
897 return c;
898
899 skip--;
900 c++;
6f003b43 901 } while (skip > 0);
87d2c1ff 902
de190aef 903 return c;
87d2c1ff
LP
904}
905
6f003b43
LP
906_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
907 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
908}
de190aef 909
6f003b43
LP
910_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
911 return real_journal_next_skip(j, DIRECTION_UP, skip);
87d2c1ff
LP
912}
913
a5344d2c 914_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
cec736d2 915 Object *o;
87d2c1ff 916 int r;
3fbf9cbb 917 char bid[33], sid[33];
87d2c1ff 918
1ae464e0
TA
919 assert_return(j, -EINVAL);
920 assert_return(!journal_pid_changed(j), -ECHILD);
921 assert_return(cursor, -EINVAL);
87d2c1ff 922
3fbf9cbb
LP
923 if (!j->current_file || j->current_file->current_offset <= 0)
924 return -EADDRNOTAVAIL;
87d2c1ff 925
de190aef 926 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
87d2c1ff
LP
927 if (r < 0)
928 return r;
929
3fbf9cbb
LP
930 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
931 sd_id128_to_string(o->entry.boot_id, bid);
87d2c1ff 932
3fbf9cbb 933 if (asprintf(cursor,
507f22bd
ZJS
934 "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
935 sid, le64toh(o->entry.seqnum),
936 bid, le64toh(o->entry.monotonic),
937 le64toh(o->entry.realtime),
938 le64toh(o->entry.xor_hash)) < 0)
3fbf9cbb 939 return -ENOMEM;
87d2c1ff 940
6f47ad30 941 return 0;
87d2c1ff
LP
942}
943
a5344d2c 944_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
a2a5291b 945 const char *word, *state;
de190aef 946 size_t l;
de190aef
LP
947 unsigned long long seqnum, monotonic, realtime, xor_hash;
948 bool
949 seqnum_id_set = false,
950 seqnum_set = false,
951 boot_id_set = false,
952 monotonic_set = false,
953 realtime_set = false,
954 xor_hash_set = false;
955 sd_id128_t seqnum_id, boot_id;
956
1ae464e0
TA
957 assert_return(j, -EINVAL);
958 assert_return(!journal_pid_changed(j), -ECHILD);
959 assert_return(!isempty(cursor), -EINVAL);
de190aef 960
a2a5291b 961 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
de190aef
LP
962 char *item;
963 int k = 0;
964
a2a5291b 965 if (l < 2 || word[1] != '=')
de190aef
LP
966 return -EINVAL;
967
a2a5291b 968 item = strndup(word, l);
de190aef
LP
969 if (!item)
970 return -ENOMEM;
971
a2a5291b 972 switch (word[0]) {
de190aef
LP
973
974 case 's':
975 seqnum_id_set = true;
be3ea5ea 976 k = sd_id128_from_string(item+2, &seqnum_id);
de190aef
LP
977 break;
978
979 case 'i':
980 seqnum_set = true;
be3ea5ea 981 if (sscanf(item+2, "%llx", &seqnum) != 1)
de190aef
LP
982 k = -EINVAL;
983 break;
984
985 case 'b':
986 boot_id_set = true;
be3ea5ea 987 k = sd_id128_from_string(item+2, &boot_id);
de190aef
LP
988 break;
989
990 case 'm':
991 monotonic_set = true;
be3ea5ea 992 if (sscanf(item+2, "%llx", &monotonic) != 1)
de190aef
LP
993 k = -EINVAL;
994 break;
995
996 case 't':
997 realtime_set = true;
be3ea5ea 998 if (sscanf(item+2, "%llx", &realtime) != 1)
de190aef
LP
999 k = -EINVAL;
1000 break;
1001
1002 case 'x':
1003 xor_hash_set = true;
be3ea5ea 1004 if (sscanf(item+2, "%llx", &xor_hash) != 1)
de190aef
LP
1005 k = -EINVAL;
1006 break;
1007 }
1008
1009 free(item);
1010
1011 if (k < 0)
1012 return k;
1013 }
1014
1015 if ((!seqnum_set || !seqnum_id_set) &&
1016 (!monotonic_set || !boot_id_set) &&
1017 !realtime_set)
1018 return -EINVAL;
1019
1020 reset_location(j);
1021
a87247dd 1022 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1023
1024 if (realtime_set) {
1025 j->current_location.realtime = (uint64_t) realtime;
1026 j->current_location.realtime_set = true;
1027 }
1028
1029 if (seqnum_set && seqnum_id_set) {
1030 j->current_location.seqnum = (uint64_t) seqnum;
1031 j->current_location.seqnum_id = seqnum_id;
1032 j->current_location.seqnum_set = true;
1033 }
1034
1035 if (monotonic_set && boot_id_set) {
1036 j->current_location.monotonic = (uint64_t) monotonic;
1037 j->current_location.boot_id = boot_id;
1038 j->current_location.monotonic_set = true;
1039 }
1040
1041 if (xor_hash_set) {
1042 j->current_location.xor_hash = (uint64_t) xor_hash;
1043 j->current_location.xor_hash_set = true;
1044 }
1045
1046 return 0;
1047}
1048
c6511e85
LP
1049_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1050 int r;
c6511e85
LP
1051 Object *o;
1052
1ae464e0
TA
1053 assert_return(j, -EINVAL);
1054 assert_return(!journal_pid_changed(j), -ECHILD);
1055 assert_return(!isempty(cursor), -EINVAL);
c6511e85
LP
1056
1057 if (!j->current_file || j->current_file->current_offset <= 0)
1058 return -EADDRNOTAVAIL;
1059
1060 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1061 if (r < 0)
1062 return r;
1063
9ed794a3 1064 for (;;) {
c6511e85 1065 _cleanup_free_ char *item = NULL;
c6511e85 1066 unsigned long long ll;
7294bb5b 1067 sd_id128_t id;
c6511e85
LP
1068 int k = 0;
1069
7294bb5b
SS
1070 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1071 if (r < 0)
1072 return r;
c6511e85 1073
7294bb5b
SS
1074 if (r == 0)
1075 break;
c6511e85 1076
7294bb5b
SS
1077 if (strlen(item) < 2 || item[1] != '=')
1078 return -EINVAL;
1079
1080 switch (item[0]) {
c6511e85
LP
1081
1082 case 's':
1083 k = sd_id128_from_string(item+2, &id);
1084 if (k < 0)
1085 return k;
1086 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1087 return 0;
1088 break;
1089
1090 case 'i':
1091 if (sscanf(item+2, "%llx", &ll) != 1)
1092 return -EINVAL;
1093 if (ll != le64toh(o->entry.seqnum))
1094 return 0;
1095 break;
1096
1097 case 'b':
1098 k = sd_id128_from_string(item+2, &id);
1099 if (k < 0)
1100 return k;
1101 if (!sd_id128_equal(id, o->entry.boot_id))
1102 return 0;
1103 break;
1104
1105 case 'm':
1106 if (sscanf(item+2, "%llx", &ll) != 1)
1107 return -EINVAL;
1108 if (ll != le64toh(o->entry.monotonic))
1109 return 0;
1110 break;
1111
1112 case 't':
1113 if (sscanf(item+2, "%llx", &ll) != 1)
1114 return -EINVAL;
1115 if (ll != le64toh(o->entry.realtime))
1116 return 0;
1117 break;
1118
1119 case 'x':
1120 if (sscanf(item+2, "%llx", &ll) != 1)
1121 return -EINVAL;
1122 if (ll != le64toh(o->entry.xor_hash))
1123 return 0;
1124 break;
1125 }
1126 }
1127
1128 return 1;
1129}
1130
1131
a5344d2c 1132_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1ae464e0
TA
1133 assert_return(j, -EINVAL);
1134 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1135
1136 reset_location(j);
a87247dd 1137 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1138 j->current_location.boot_id = boot_id;
1139 j->current_location.monotonic = usec;
1140 j->current_location.monotonic_set = true;
1141
1142 return 0;
1143}
1144
a5344d2c 1145_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1ae464e0
TA
1146 assert_return(j, -EINVAL);
1147 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1148
1149 reset_location(j);
a87247dd 1150 j->current_location.type = LOCATION_SEEK;
de190aef
LP
1151 j->current_location.realtime = usec;
1152 j->current_location.realtime_set = true;
1153
1154 return 0;
1155}
1156
a5344d2c 1157_public_ int sd_journal_seek_head(sd_journal *j) {
1ae464e0
TA
1158 assert_return(j, -EINVAL);
1159 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1160
1161 reset_location(j);
1162 j->current_location.type = LOCATION_HEAD;
1163
1164 return 0;
1165}
1166
a5344d2c 1167_public_ int sd_journal_seek_tail(sd_journal *j) {
1ae464e0
TA
1168 assert_return(j, -EINVAL);
1169 assert_return(!journal_pid_changed(j), -ECHILD);
de190aef
LP
1170
1171 reset_location(j);
1172 j->current_location.type = LOCATION_TAIL;
1173
1174 return 0;
87d2c1ff
LP
1175}
1176
85210bff
LP
1177static void check_network(sd_journal *j, int fd) {
1178 struct statfs sfs;
1179
1180 assert(j);
1181
1182 if (j->on_network)
1183 return;
1184
1185 if (fstatfs(fd, &sfs) < 0)
1186 return;
1187
1188 j->on_network =
c51cf056
ZJS
1189 F_TYPE_EQUAL(sfs.f_type, CIFS_MAGIC_NUMBER) ||
1190 F_TYPE_EQUAL(sfs.f_type, CODA_SUPER_MAGIC) ||
1191 F_TYPE_EQUAL(sfs.f_type, NCP_SUPER_MAGIC) ||
1192 F_TYPE_EQUAL(sfs.f_type, NFS_SUPER_MAGIC) ||
1193 F_TYPE_EQUAL(sfs.f_type, SMB_SUPER_MAGIC);
85210bff
LP
1194}
1195
a688baa8
ZJS
1196static bool file_has_type_prefix(const char *prefix, const char *filename) {
1197 const char *full, *tilded, *atted;
1198
63c372cb
LP
1199 full = strjoina(prefix, ".journal");
1200 tilded = strjoina(full, "~");
1201 atted = strjoina(prefix, "@");
a688baa8
ZJS
1202
1203 return streq(filename, full) ||
1204 streq(filename, tilded) ||
1205 startswith(filename, atted);
1206}
1207
1208static bool file_type_wanted(int flags, const char *filename) {
d617408e
LP
1209 assert(filename);
1210
a688baa8
ZJS
1211 if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
1212 return false;
1213
1214 /* no flags set → every type is OK */
1215 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1216 return true;
1217
1218 if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
1219 return true;
1220
1221 if (flags & SD_JOURNAL_CURRENT_USER) {
1222 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1223
5ffa8c81 1224 xsprintf(prefix, "user-"UID_FMT, getuid());
a688baa8
ZJS
1225
1226 if (file_has_type_prefix(prefix, filename))
1227 return true;
1228 }
1229
1230 return false;
1231}
1232
5d1ce257
LP
1233static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1234 assert(j);
1235 assert(path);
1236 assert(prefix);
1237
1238 if (j->toplevel_fd >= 0)
1239 return false;
1240
1241 return path_startswith(path, prefix);
1242}
1243
1244static const char *skip_slash(const char *p) {
1245
1246 if (!p)
1247 return NULL;
1248
1249 while (*p == '/')
1250 p++;
1251
1252 return p;
1253}
1254
1255static int add_any_file(sd_journal *j, int fd, const char *path) {
39883f62 1256 JournalFile *f = NULL;
5d1ce257 1257 bool close_fd = false;
d617408e 1258 int r, k;
3fbf9cbb
LP
1259
1260 assert(j);
5d1ce257 1261 assert(fd >= 0 || path);
3fbf9cbb 1262
5d1ce257 1263 if (path && ordered_hashmap_get(j->files, path))
50f20cfd 1264 return 0;
50f20cfd 1265
9e8abdf0 1266 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
d617408e
LP
1267 log_debug("Too many open journal files, not adding %s.", path);
1268 r = -ETOOMANYREFS;
1269 goto fail;
9e8abdf0
LP
1270 }
1271
5d1ce257
LP
1272 if (fd < 0 && j->toplevel_fd >= 0) {
1273
1274 /* If there's a top-level fd defined, open the file relative to this now. (Make the path relative,
1275 * explicitly, since otherwise openat() ignores the first argument.) */
1276
1277 fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC);
1278 if (fd < 0) {
1279 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
1280 goto fail;
1281 }
1282
1283 close_fd = true;
1284 }
1285
1286 r = journal_file_open(fd, path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, NULL, &f);
d617408e 1287 if (r < 0) {
5d1ce257
LP
1288 if (close_fd)
1289 safe_close(fd);
d617408e
LP
1290 log_debug_errno(r, "Failed to open journal file %s: %m", path);
1291 goto fail;
1292 }
3fbf9cbb 1293
72f59706 1294 /* journal_file_dump(f); */
de190aef 1295
c1f906bd 1296 r = ordered_hashmap_put(j->files, f->path, f);
3fbf9cbb 1297 if (r < 0) {
5d1ce257 1298 f->close_fd = close_fd;
69a3a6fd 1299 (void) journal_file_close(f);
d617408e 1300 goto fail;
3fbf9cbb
LP
1301 }
1302
5d1ce257
LP
1303 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1304 j->has_runtime_files = true;
1305 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1306 j->has_persistent_files = true;
1307
5ec76417 1308 log_debug("File %s added.", f->path);
a50d7d43 1309
85210bff
LP
1310 check_network(j, f->fd);
1311
313cefa1 1312 j->current_invalidate_counter++;
a963990f 1313
50f20cfd 1314 return 0;
d617408e
LP
1315
1316fail:
5768d259 1317 k = journal_put_error(j, r, path);
d617408e
LP
1318 if (k < 0)
1319 return k;
1320
1321 return r;
50f20cfd
LP
1322}
1323
5302ebe1 1324static int add_file(sd_journal *j, const char *prefix, const char *filename) {
d617408e 1325 const char *path;
5302ebe1
ZJS
1326
1327 assert(j);
1328 assert(prefix);
1329 assert(filename);
1330
5d1ce257 1331 if (j->no_new_files)
5302ebe1
ZJS
1332 return 0;
1333
5d1ce257
LP
1334 if (!file_type_wanted(j->flags, filename))
1335 return 0;
39fd5b08 1336
5d1ce257
LP
1337 path = strjoina(prefix, "/", filename);
1338 return add_any_file(j, -1, path);
5302ebe1
ZJS
1339}
1340
d617408e
LP
1341static void remove_file(sd_journal *j, const char *prefix, const char *filename) {
1342 const char *path;
50f20cfd
LP
1343 JournalFile *f;
1344
1345 assert(j);
1346 assert(prefix);
1347 assert(filename);
1348
d617408e 1349 path = strjoina(prefix, "/", filename);
c1f906bd 1350 f = ordered_hashmap_get(j->files, path);
50f20cfd 1351 if (!f)
d617408e 1352 return;
50f20cfd 1353
a9a245c1 1354 remove_file_real(j, f);
a9a245c1
ZJS
1355}
1356
1357static void remove_file_real(sd_journal *j, JournalFile *f) {
1358 assert(j);
1359 assert(f);
1360
c1f906bd 1361 ordered_hashmap_remove(j->files, f->path);
44a5fa34 1362
5ec76417 1363 log_debug("File %s removed.", f->path);
44a5fa34 1364
3c1668da
LP
1365 if (j->current_file == f) {
1366 j->current_file = NULL;
1367 j->current_field = 0;
1368 }
1369
1370 if (j->unique_file == f) {
360af4cf 1371 /* Jump to the next unique_file or NULL if that one was last */
c1f906bd 1372 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
3c1668da 1373 j->unique_offset = 0;
360af4cf
ZJS
1374 if (!j->unique_file)
1375 j->unique_file_lost = true;
3c1668da
LP
1376 }
1377
eb86030e
LP
1378 if (j->fields_file == f) {
1379 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1380 j->fields_offset = 0;
1381 if (!j->fields_file)
1382 j->fields_file_lost = true;
1383 }
1384
69a3a6fd 1385 (void) journal_file_close(f);
50f20cfd 1386
313cefa1 1387 j->current_invalidate_counter++;
3fbf9cbb
LP
1388}
1389
d617408e
LP
1390static int dirname_is_machine_id(const char *fn) {
1391 sd_id128_t id, machine;
1392 int r;
1393
1394 r = sd_id128_get_machine(&machine);
1395 if (r < 0)
1396 return r;
1397
1398 r = sd_id128_from_string(fn, &id);
1399 if (r < 0)
1400 return r;
1401
1402 return sd_id128_equal(id, machine);
1403}
1404
a963990f 1405static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
7fd1b19b 1406 _cleanup_free_ char *path = NULL;
7fd1b19b 1407 _cleanup_closedir_ DIR *d = NULL;
d617408e 1408 struct dirent *de = NULL;
a963990f 1409 Directory *m;
d617408e 1410 int r, k;
3fbf9cbb
LP
1411
1412 assert(j);
1413 assert(prefix);
3fbf9cbb 1414
5d1ce257
LP
1415 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1416 * and reenumerates directory contents */
d95b1fb3 1417
5d1ce257
LP
1418 if (dirname)
1419 path = strjoin(prefix, "/", dirname, NULL);
1420 else
1421 path = strdup(prefix);
d617408e
LP
1422 if (!path) {
1423 r = -ENOMEM;
1424 goto fail;
1425 }
3fbf9cbb 1426
5d1ce257
LP
1427 log_debug("Considering directory %s.", path);
1428
1429 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1430 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1431 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
1432 return 0;
1433
1434
1435 if (j->toplevel_fd < 0)
1436 d = opendir(path);
1437 else
61233823 1438 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
5d1ce257
LP
1439 * relative, by dropping the initial slash */
1440 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
3fbf9cbb 1441 if (!d) {
d617408e
LP
1442 r = log_debug_errno(errno, "Failed to open directory %s: %m", path);
1443 goto fail;
3fbf9cbb
LP
1444 }
1445
a963990f
LP
1446 m = hashmap_get(j->directories_by_path, path);
1447 if (!m) {
1448 m = new0(Directory, 1);
d617408e
LP
1449 if (!m) {
1450 r = -ENOMEM;
1451 goto fail;
1452 }
a963990f
LP
1453
1454 m->is_root = false;
1455 m->path = path;
1456
1457 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f 1458 free(m);
d617408e
LP
1459 r = -ENOMEM;
1460 goto fail;
a963990f
LP
1461 }
1462
a50d7d43 1463 path = NULL; /* avoid freeing in cleanup */
313cefa1 1464 j->current_invalidate_counter++;
a963990f 1465
5ec76417 1466 log_debug("Directory %s added.", m->path);
a963990f 1467
a50d7d43 1468 } else if (m->is_root)
a963990f 1469 return 0;
a963990f
LP
1470
1471 if (m->wd <= 0 && j->inotify_fd >= 0) {
5d1ce257 1472 /* Watch this directory, if it not being watched yet. */
a963990f 1473
430fbf8e
LP
1474 m->wd = inotify_add_watch_fd(j->inotify_fd, dirfd(d),
1475 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1476 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1477 IN_ONLYDIR);
a963990f
LP
1478
1479 if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
1480 inotify_rm_watch(j->inotify_fd, m->wd);
1481 }
1482
f6377261 1483 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
a963990f 1484
de2c3907 1485 if (dirent_is_file_with_suffix(de, ".journal") ||
d617408e
LP
1486 dirent_is_file_with_suffix(de, ".journal~"))
1487 (void) add_file(j, m->path, de->d_name);
a963990f
LP
1488 }
1489
85210bff
LP
1490 check_network(j, dirfd(d));
1491
a963990f 1492 return 0;
d617408e
LP
1493
1494fail:
5d1ce257 1495 k = journal_put_error(j, r, path ?: prefix);
d617408e
LP
1496 if (k < 0)
1497 return k;
1498
1499 return r;
a963990f
LP
1500}
1501
d617408e 1502static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
5d1ce257 1503
7fd1b19b 1504 _cleanup_closedir_ DIR *d = NULL;
d617408e 1505 struct dirent *de;
a963990f 1506 Directory *m;
d617408e 1507 int r, k;
a963990f
LP
1508
1509 assert(j);
a963990f 1510
5d1ce257
LP
1511 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1512 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1513 * populate the set, as well as to update it later. */
a963990f 1514
5d1ce257
LP
1515 if (p) {
1516 /* If there's a path specified, use it. */
b6741478 1517
5d1ce257
LP
1518 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1519 !path_has_prefix(j, p, "/run"))
1520 return -EINVAL;
d617408e 1521
5d1ce257
LP
1522 if (j->prefix)
1523 p = strjoina(j->prefix, p);
1524
1525 if (j->toplevel_fd < 0)
1526 d = opendir(p);
1527 else
1528 d = xopendirat(j->toplevel_fd, skip_slash(p), 0);
1529
1530 if (!d) {
1531 if (errno == ENOENT && missing_ok)
1532 return 0;
1533
1534 r = log_debug_errno(errno, "Failed to open root directory %s: %m", p);
1535 goto fail;
1536 }
1537 } else {
1538 int dfd;
1539
1540 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1541 * opendir() will take possession of the fd, and close it, which we don't want. */
1542
1543 p = "."; /* store this as "." in the directories hashmap */
1544
1545 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1546 if (dfd < 0) {
1547 r = -errno;
1548 goto fail;
1549 }
1550
1551 d = fdopendir(dfd);
1552 if (!d) {
1553 r = -errno;
1554 safe_close(dfd);
1555 goto fail;
1556 }
1557
1558 rewinddir(d);
d617408e 1559 }
a963990f
LP
1560
1561 m = hashmap_get(j->directories_by_path, p);
1562 if (!m) {
1563 m = new0(Directory, 1);
d617408e
LP
1564 if (!m) {
1565 r = -ENOMEM;
1566 goto fail;
1567 }
a963990f
LP
1568
1569 m->is_root = true;
5d1ce257 1570
a963990f
LP
1571 m->path = strdup(p);
1572 if (!m->path) {
a963990f 1573 free(m);
d617408e
LP
1574 r = -ENOMEM;
1575 goto fail;
a963990f
LP
1576 }
1577
1578 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
a963990f
LP
1579 free(m->path);
1580 free(m);
d617408e
LP
1581 r = -ENOMEM;
1582 goto fail;
a963990f
LP
1583 }
1584
313cefa1 1585 j->current_invalidate_counter++;
a963990f 1586
5ec76417 1587 log_debug("Root directory %s added.", m->path);
a963990f 1588
a50d7d43 1589 } else if (!m->is_root)
a963990f 1590 return 0;
50f20cfd 1591
a963990f
LP
1592 if (m->wd <= 0 && j->inotify_fd >= 0) {
1593
430fbf8e 1594 m->wd = inotify_add_watch_fd(j->inotify_fd, dirfd(d),
a963990f 1595 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
4a842cad 1596 IN_ONLYDIR);
a963990f
LP
1597
1598 if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
1599 inotify_rm_watch(j->inotify_fd, m->wd);
1600 }
50f20cfd 1601
5302ebe1
ZJS
1602 if (j->no_new_files)
1603 return 0;
1604
f6377261 1605 FOREACH_DIRENT_ALL(de, d, r = log_debug_errno(errno, "Failed to read directory %s: %m", m->path); goto fail) {
a963990f 1606 sd_id128_t id;
3fbf9cbb 1607
de2c3907 1608 if (dirent_is_file_with_suffix(de, ".journal") ||
d617408e
LP
1609 dirent_is_file_with_suffix(de, ".journal~"))
1610 (void) add_file(j, m->path, de->d_name);
1611 else if (IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN) &&
1612 sd_id128_from_string(de->d_name, &id) >= 0)
1613 (void) add_directory(j, m->path, de->d_name);
3fbf9cbb
LP
1614 }
1615
85210bff
LP
1616 check_network(j, dirfd(d));
1617
a963990f 1618 return 0;
d617408e
LP
1619
1620fail:
5768d259 1621 k = journal_put_error(j, r, p);
d617408e
LP
1622 if (k < 0)
1623 return k;
1624
1625 return r;
a963990f
LP
1626}
1627
b2b46f91 1628static void remove_directory(sd_journal *j, Directory *d) {
a963990f
LP
1629 assert(j);
1630
1631 if (d->wd > 0) {
1632 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1633
1634 if (j->inotify_fd >= 0)
1635 inotify_rm_watch(j->inotify_fd, d->wd);
1636 }
1637
1638 hashmap_remove(j->directories_by_path, d->path);
1639
1640 if (d->is_root)
5ec76417 1641 log_debug("Root directory %s removed.", d->path);
a963990f 1642 else
5ec76417 1643 log_debug("Directory %s removed.", d->path);
a963990f
LP
1644
1645 free(d->path);
1646 free(d);
3fbf9cbb
LP
1647}
1648
89739579 1649static int add_search_paths(sd_journal *j) {
d617408e
LP
1650
1651 static const char search_paths[] =
a963990f
LP
1652 "/run/log/journal\0"
1653 "/var/log/journal\0";
1654 const char *p;
50f20cfd
LP
1655
1656 assert(j);
50f20cfd 1657
a963990f
LP
1658 /* We ignore most errors here, since the idea is to only open
1659 * what's actually accessible, and ignore the rest. */
50f20cfd 1660
d617408e
LP
1661 NULSTR_FOREACH(p, search_paths)
1662 (void) add_root_directory(j, p, true);
50f20cfd 1663
a963990f 1664 return 0;
50f20cfd
LP
1665}
1666
5302ebe1
ZJS
1667static int add_current_paths(sd_journal *j) {
1668 Iterator i;
1669 JournalFile *f;
1670
1671 assert(j);
1672 assert(j->no_new_files);
1673
5d1ce257 1674 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
5302ebe1
ZJS
1675 * treat them as fatal. */
1676
c1f906bd 1677 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
5302ebe1 1678 _cleanup_free_ char *dir;
e9174f29 1679 int r;
5302ebe1
ZJS
1680
1681 dir = dirname_malloc(f->path);
1682 if (!dir)
1683 return -ENOMEM;
1684
5d1ce257 1685 r = add_directory(j, dir, NULL);
d617408e 1686 if (r < 0)
5302ebe1 1687 return r;
5302ebe1
ZJS
1688 }
1689
1690 return 0;
1691}
1692
a963990f 1693static int allocate_inotify(sd_journal *j) {
50f20cfd 1694 assert(j);
50f20cfd 1695
a963990f
LP
1696 if (j->inotify_fd < 0) {
1697 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1698 if (j->inotify_fd < 0)
1699 return -errno;
1700 }
50f20cfd 1701
cb306f5d 1702 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
50f20cfd
LP
1703}
1704
7827b1a1 1705static sd_journal *journal_new(int flags, const char *path) {
a963990f 1706 sd_journal *j;
50f20cfd 1707
a963990f
LP
1708 j = new0(sd_journal, 1);
1709 if (!j)
1710 return NULL;
50f20cfd 1711
a65f06bb 1712 j->original_pid = getpid();
5d1ce257 1713 j->toplevel_fd = -1;
a963990f
LP
1714 j->inotify_fd = -1;
1715 j->flags = flags;
93b73b06 1716 j->data_threshold = DEFAULT_DATA_THRESHOLD;
50f20cfd 1717
7827b1a1 1718 if (path) {
16fefe90
ZJS
1719 char *t;
1720
1721 t = strdup(path);
1722 if (!t)
6180fc61 1723 goto fail;
16fefe90
ZJS
1724
1725 if (flags & SD_JOURNAL_OS_ROOT)
1726 j->prefix = t;
1727 else
1728 j->path = t;
7827b1a1
LP
1729 }
1730
c1f906bd 1731 j->files = ordered_hashmap_new(&string_hash_ops);
d5099efc 1732 j->directories_by_path = hashmap_new(&string_hash_ops);
84168d80 1733 j->mmap = mmap_cache_new();
3ac251b8 1734 if (!j->files || !j->directories_by_path || !j->mmap)
6180fc61 1735 goto fail;
16e9f408 1736
a963990f 1737 return j;
6180fc61
ZJS
1738
1739fail:
1740 sd_journal_close(j);
1741 return NULL;
50f20cfd
LP
1742}
1743
1aaa68f5
ZJS
1744#define OPEN_ALLOWED_FLAGS \
1745 (SD_JOURNAL_LOCAL_ONLY | \
1746 SD_JOURNAL_RUNTIME_ONLY | \
1747 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1748
a5344d2c 1749_public_ int sd_journal_open(sd_journal **ret, int flags) {
87d2c1ff 1750 sd_journal *j;
3fbf9cbb 1751 int r;
87d2c1ff 1752
1ae464e0 1753 assert_return(ret, -EINVAL);
1aaa68f5 1754 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1755
7827b1a1 1756 j = journal_new(flags, NULL);
87d2c1ff
LP
1757 if (!j)
1758 return -ENOMEM;
1759
89739579 1760 r = add_search_paths(j);
a963990f 1761 if (r < 0)
50f20cfd 1762 goto fail;
50f20cfd 1763
a963990f
LP
1764 *ret = j;
1765 return 0;
cf244689 1766
a963990f
LP
1767fail:
1768 sd_journal_close(j);
87d2c1ff 1769
a963990f
LP
1770 return r;
1771}
50f20cfd 1772
1aaa68f5
ZJS
1773#define OPEN_CONTAINER_ALLOWED_FLAGS \
1774 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1775
b6741478
LP
1776_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1777 _cleanup_free_ char *root = NULL, *class = NULL;
1778 sd_journal *j;
1779 char *p;
1780 int r;
1781
2daa9cbd
LP
1782 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1783 * combination with sd_journal_open_directory_fd(). */
1784
b6741478
LP
1785 assert_return(machine, -EINVAL);
1786 assert_return(ret, -EINVAL);
1aaa68f5 1787 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
affcf189 1788 assert_return(machine_name_is_valid(machine), -EINVAL);
b6741478 1789
63c372cb 1790 p = strjoina("/run/systemd/machines/", machine);
b6741478
LP
1791 r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
1792 if (r == -ENOENT)
1793 return -EHOSTDOWN;
1794 if (r < 0)
1795 return r;
1796 if (!root)
1797 return -ENODATA;
1798
1799 if (!streq_ptr(class, "container"))
1800 return -EIO;
1801
16fefe90 1802 j = journal_new(flags, root);
b6741478
LP
1803 if (!j)
1804 return -ENOMEM;
1805
89739579 1806 r = add_search_paths(j);
b6741478
LP
1807 if (r < 0)
1808 goto fail;
1809
1810 *ret = j;
1811 return 0;
1812
1813fail:
1814 sd_journal_close(j);
1815 return r;
1816}
1817
1aaa68f5 1818#define OPEN_DIRECTORY_ALLOWED_FLAGS \
10752e82
ZJS
1819 (SD_JOURNAL_OS_ROOT | \
1820 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 1821
a963990f
LP
1822_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
1823 sd_journal *j;
1824 int r;
87d2c1ff 1825
1ae464e0
TA
1826 assert_return(ret, -EINVAL);
1827 assert_return(path, -EINVAL);
1aaa68f5 1828 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
87d2c1ff 1829
7827b1a1 1830 j = journal_new(flags, path);
a963990f
LP
1831 if (!j)
1832 return -ENOMEM;
3fbf9cbb 1833
d077390c
LP
1834 if (flags & SD_JOURNAL_OS_ROOT)
1835 r = add_search_paths(j);
1836 else
1837 r = add_root_directory(j, path, false);
d617408e 1838 if (r < 0)
a963990f 1839 goto fail;
87d2c1ff
LP
1840
1841 *ret = j;
1842 return 0;
1843
1844fail:
1845 sd_journal_close(j);
87d2c1ff 1846 return r;
a963990f 1847}
87d2c1ff 1848
5302ebe1
ZJS
1849_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
1850 sd_journal *j;
1851 const char **path;
1852 int r;
1853
1ae464e0
TA
1854 assert_return(ret, -EINVAL);
1855 assert_return(flags == 0, -EINVAL);
5302ebe1
ZJS
1856
1857 j = journal_new(flags, NULL);
1858 if (!j)
1859 return -ENOMEM;
1860
1861 STRV_FOREACH(path, paths) {
5d1ce257 1862 r = add_any_file(j, -1, *path);
d617408e 1863 if (r < 0)
5302ebe1 1864 goto fail;
5302ebe1
ZJS
1865 }
1866
1867 j->no_new_files = true;
1868
1869 *ret = j;
1870 return 0;
1871
1872fail:
1873 sd_journal_close(j);
5d1ce257
LP
1874 return r;
1875}
1876
1aaa68f5 1877#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
10752e82
ZJS
1878 (SD_JOURNAL_OS_ROOT | \
1879 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1aaa68f5 1880
5d1ce257
LP
1881_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
1882 sd_journal *j;
1883 struct stat st;
1884 int r;
1885
1886 assert_return(ret, -EINVAL);
1887 assert_return(fd >= 0, -EBADF);
1aaa68f5 1888 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
5d1ce257
LP
1889
1890 if (fstat(fd, &st) < 0)
1891 return -errno;
1892
1893 if (!S_ISDIR(st.st_mode))
1894 return -EBADFD;
1895
1896 j = journal_new(flags, NULL);
1897 if (!j)
1898 return -ENOMEM;
1899
1900 j->toplevel_fd = fd;
1901
d077390c
LP
1902 if (flags & SD_JOURNAL_OS_ROOT)
1903 r = add_search_paths(j);
1904 else
1905 r = add_root_directory(j, NULL, false);
5d1ce257
LP
1906 if (r < 0)
1907 goto fail;
1908
1909 *ret = j;
1910 return 0;
1911
1912fail:
1913 sd_journal_close(j);
1914 return r;
1915}
1916
1917_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
1918 Iterator iterator;
1919 JournalFile *f;
1920 sd_journal *j;
1921 unsigned i;
1922 int r;
1923
1924 assert_return(ret, -EINVAL);
1925 assert_return(n_fds > 0, -EBADF);
1926 assert_return(flags == 0, -EINVAL);
1927
1928 j = journal_new(flags, NULL);
1929 if (!j)
1930 return -ENOMEM;
1931
1932 for (i = 0; i < n_fds; i++) {
1933 struct stat st;
1934
1935 if (fds[i] < 0) {
1936 r = -EBADF;
1937 goto fail;
1938 }
1939
1940 if (fstat(fds[i], &st) < 0) {
1941 r = -errno;
1942 goto fail;
1943 }
1944
1945 if (!S_ISREG(st.st_mode)) {
1946 r = -EBADFD;
1947 goto fail;
1948 }
1949
1950 r = add_any_file(j, fds[i], NULL);
1951 if (r < 0)
1952 goto fail;
1953 }
1954
1955 j->no_new_files = true;
1956 j->no_inotify = true;
5302ebe1 1957
5d1ce257
LP
1958 *ret = j;
1959 return 0;
1960
1961fail:
f8e2f4d6 1962 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
5d1ce257
LP
1963 * remain open */
1964 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
1965 f->close_fd = false;
1966
1967 sd_journal_close(j);
5302ebe1
ZJS
1968 return r;
1969}
1970
a5344d2c 1971_public_ void sd_journal_close(sd_journal *j) {
a963990f
LP
1972 Directory *d;
1973 JournalFile *f;
5768d259 1974 char *p;
a963990f 1975
a5344d2c
LP
1976 if (!j)
1977 return;
87d2c1ff 1978
54b1da83
LP
1979 sd_journal_flush_matches(j);
1980
c1f906bd 1981 while ((f = ordered_hashmap_steal_first(j->files)))
69a3a6fd 1982 (void) journal_file_close(f);
50f20cfd 1983
c1f906bd 1984 ordered_hashmap_free(j->files);
260a2be4 1985
a963990f
LP
1986 while ((d = hashmap_first(j->directories_by_path)))
1987 remove_directory(j, d);
260a2be4 1988
a963990f
LP
1989 while ((d = hashmap_first(j->directories_by_wd)))
1990 remove_directory(j, d);
87d2c1ff 1991
a963990f
LP
1992 hashmap_free(j->directories_by_path);
1993 hashmap_free(j->directories_by_wd);
1cc101f1 1994
03e334a1 1995 safe_close(j->inotify_fd);
50f20cfd 1996
bf807d4d
LP
1997 if (j->mmap) {
1998 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
16e9f408 1999 mmap_cache_unref(j->mmap);
bf807d4d 2000 }
16e9f408 2001
5768d259
LP
2002 while ((p = hashmap_steal_first(j->errors)))
2003 free(p);
2004 hashmap_free(j->errors);
2005
7827b1a1 2006 free(j->path);
89739579 2007 free(j->prefix);
3c1668da 2008 free(j->unique_field);
eb86030e 2009 free(j->fields_buffer);
87d2c1ff
LP
2010 free(j);
2011}
3fbf9cbb 2012
a5344d2c 2013_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
3fbf9cbb
LP
2014 Object *o;
2015 JournalFile *f;
2016 int r;
2017
1ae464e0
TA
2018 assert_return(j, -EINVAL);
2019 assert_return(!journal_pid_changed(j), -ECHILD);
2020 assert_return(ret, -EINVAL);
3fbf9cbb
LP
2021
2022 f = j->current_file;
2023 if (!f)
de190aef 2024 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2025
2026 if (f->current_offset <= 0)
de190aef 2027 return -EADDRNOTAVAIL;
3fbf9cbb 2028
de190aef 2029 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2030 if (r < 0)
2031 return r;
2032
2033 *ret = le64toh(o->entry.realtime);
de190aef 2034 return 0;
3fbf9cbb
LP
2035}
2036
a5344d2c 2037_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
3fbf9cbb
LP
2038 Object *o;
2039 JournalFile *f;
2040 int r;
2041 sd_id128_t id;
2042
1ae464e0
TA
2043 assert_return(j, -EINVAL);
2044 assert_return(!journal_pid_changed(j), -ECHILD);
3fbf9cbb
LP
2045
2046 f = j->current_file;
2047 if (!f)
de190aef 2048 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2049
2050 if (f->current_offset <= 0)
de190aef 2051 return -EADDRNOTAVAIL;
3fbf9cbb 2052
de190aef 2053 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2054 if (r < 0)
2055 return r;
2056
de190aef
LP
2057 if (ret_boot_id)
2058 *ret_boot_id = o->entry.boot_id;
2059 else {
2060 r = sd_id128_get_boot(&id);
2061 if (r < 0)
2062 return r;
3fbf9cbb 2063
de190aef 2064 if (!sd_id128_equal(id, o->entry.boot_id))
df50185b 2065 return -ESTALE;
de190aef 2066 }
3fbf9cbb 2067
14a65d65
LP
2068 if (ret)
2069 *ret = le64toh(o->entry.monotonic);
2070
de190aef 2071 return 0;
3fbf9cbb
LP
2072}
2073
362a3f81
LP
2074static bool field_is_valid(const char *field) {
2075 const char *p;
2076
2077 assert(field);
2078
2079 if (isempty(field))
2080 return false;
2081
2082 if (startswith(field, "__"))
2083 return false;
2084
2085 for (p = field; *p; p++) {
2086
2087 if (*p == '_')
2088 continue;
2089
2090 if (*p >= 'A' && *p <= 'Z')
2091 continue;
2092
2093 if (*p >= '0' && *p <= '9')
2094 continue;
2095
2096 return false;
2097 }
2098
2099 return true;
2100}
2101
a5344d2c 2102_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
3fbf9cbb
LP
2103 JournalFile *f;
2104 uint64_t i, n;
2105 size_t field_length;
2106 int r;
2107 Object *o;
2108
1ae464e0
TA
2109 assert_return(j, -EINVAL);
2110 assert_return(!journal_pid_changed(j), -ECHILD);
2111 assert_return(field, -EINVAL);
2112 assert_return(data, -EINVAL);
2113 assert_return(size, -EINVAL);
2114 assert_return(field_is_valid(field), -EINVAL);
3fbf9cbb
LP
2115
2116 f = j->current_file;
2117 if (!f)
de190aef 2118 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2119
2120 if (f->current_offset <= 0)
de190aef 2121 return -EADDRNOTAVAIL;
3fbf9cbb 2122
de190aef 2123 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2124 if (r < 0)
2125 return r;
2126
2127 field_length = strlen(field);
2128
2129 n = journal_file_entry_n_items(o);
2130 for (i = 0; i < n; i++) {
4fd052ae
FC
2131 uint64_t p, l;
2132 le64_t le_hash;
3fbf9cbb 2133 size_t t;
1ec7120e 2134 int compression;
3fbf9cbb
LP
2135
2136 p = le64toh(o->entry.items[i].object_offset);
807e17f0 2137 le_hash = o->entry.items[i].hash;
de190aef 2138 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2139 if (r < 0)
2140 return r;
2141
de190aef 2142 if (le_hash != o->data.hash)
de7b95cd
LP
2143 return -EBADMSG;
2144
3fbf9cbb
LP
2145 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2146
1ec7120e 2147 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
3b1a55e1
ZJS
2148 if (compression) {
2149#if defined(HAVE_XZ) || defined(HAVE_LZ4)
2aaec9b4 2150 r = decompress_startswith(compression,
3b1a55e1
ZJS
2151 o->data.payload, l,
2152 &f->compress_buffer, &f->compress_buffer_size,
2aaec9b4
ZJS
2153 field, field_length, '=');
2154 if (r < 0)
82e24b00 2155 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2aaec9b4
ZJS
2156 object_compressed_to_string(compression), l, p);
2157 else if (r > 0) {
3b1a55e1 2158
fa1c4b51 2159 size_t rsize;
3b1a55e1
ZJS
2160
2161 r = decompress_blob(compression,
2162 o->data.payload, l,
2163 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2164 j->data_threshold);
2165 if (r < 0)
2166 return r;
807e17f0 2167
3b1a55e1
ZJS
2168 *data = f->compress_buffer;
2169 *size = (size_t) rsize;
807e17f0 2170
3b1a55e1
ZJS
2171 return 0;
2172 }
2173#else
2174 return -EPROTONOSUPPORT;
2175#endif
807e17f0
LP
2176 } else if (l >= field_length+1 &&
2177 memcmp(o->data.payload, field, field_length) == 0 &&
2178 o->data.payload[field_length] == '=') {
3fbf9cbb 2179
161e54f8 2180 t = (size_t) l;
3fbf9cbb 2181
161e54f8
LP
2182 if ((uint64_t) t != l)
2183 return -E2BIG;
3fbf9cbb 2184
161e54f8
LP
2185 *data = o->data.payload;
2186 *size = t;
3fbf9cbb 2187
99613ec5 2188 return 0;
161e54f8 2189 }
3fbf9cbb 2190
de190aef 2191 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
161e54f8
LP
2192 if (r < 0)
2193 return r;
3fbf9cbb
LP
2194 }
2195
de190aef 2196 return -ENOENT;
3fbf9cbb
LP
2197}
2198
93b73b06 2199static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
3c1668da
LP
2200 size_t t;
2201 uint64_t l;
3b1a55e1 2202 int compression;
3c1668da
LP
2203
2204 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2205 t = (size_t) l;
2206
2207 /* We can't read objects larger than 4G on a 32bit machine */
2208 if ((uint64_t) t != l)
2209 return -E2BIG;
2210
1ec7120e
ZJS
2211 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2212 if (compression) {
3b1a55e1 2213#if defined(HAVE_XZ) || defined(HAVE_LZ4)
fa1c4b51 2214 size_t rsize;
3b1a55e1 2215 int r;
3c1668da 2216
1ec7120e
ZJS
2217 r = decompress_blob(compression,
2218 o->data.payload, l, &f->compress_buffer,
2219 &f->compress_buffer_size, &rsize, j->data_threshold);
2220 if (r < 0)
2221 return r;
3c1668da
LP
2222
2223 *data = f->compress_buffer;
2224 *size = (size_t) rsize;
3b1a55e1
ZJS
2225#else
2226 return -EPROTONOSUPPORT;
2227#endif
3c1668da
LP
2228 } else {
2229 *data = o->data.payload;
2230 *size = t;
2231 }
2232
2233 return 0;
2234}
2235
a5344d2c 2236_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
3fbf9cbb 2237 JournalFile *f;
3c1668da 2238 uint64_t p, n;
4fd052ae 2239 le64_t le_hash;
3fbf9cbb
LP
2240 int r;
2241 Object *o;
2242
1ae464e0
TA
2243 assert_return(j, -EINVAL);
2244 assert_return(!journal_pid_changed(j), -ECHILD);
2245 assert_return(data, -EINVAL);
2246 assert_return(size, -EINVAL);
3fbf9cbb
LP
2247
2248 f = j->current_file;
2249 if (!f)
de190aef 2250 return -EADDRNOTAVAIL;
3fbf9cbb
LP
2251
2252 if (f->current_offset <= 0)
de190aef 2253 return -EADDRNOTAVAIL;
3fbf9cbb 2254
de190aef 2255 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
3fbf9cbb
LP
2256 if (r < 0)
2257 return r;
2258
2259 n = journal_file_entry_n_items(o);
7210bfb3 2260 if (j->current_field >= n)
3fbf9cbb
LP
2261 return 0;
2262
7210bfb3 2263 p = le64toh(o->entry.items[j->current_field].object_offset);
de190aef
LP
2264 le_hash = o->entry.items[j->current_field].hash;
2265 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
3fbf9cbb
LP
2266 if (r < 0)
2267 return r;
2268
de190aef 2269 if (le_hash != o->data.hash)
de7b95cd
LP
2270 return -EBADMSG;
2271
93b73b06 2272 r = return_data(j, f, o, data, size);
3c1668da
LP
2273 if (r < 0)
2274 return r;
3fbf9cbb 2275
313cefa1 2276 j->current_field++;
3fbf9cbb
LP
2277
2278 return 1;
2279}
c2373f84 2280
a5344d2c
LP
2281_public_ void sd_journal_restart_data(sd_journal *j) {
2282 if (!j)
2283 return;
8725d60a
LP
2284
2285 j->current_field = 0;
c2373f84 2286}
50f20cfd 2287
a5344d2c 2288_public_ int sd_journal_get_fd(sd_journal *j) {
a963990f
LP
2289 int r;
2290
1ae464e0
TA
2291 assert_return(j, -EINVAL);
2292 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2293
5d1ce257
LP
2294 if (j->no_inotify)
2295 return -EMEDIUMTYPE;
2296
a963990f
LP
2297 if (j->inotify_fd >= 0)
2298 return j->inotify_fd;
2299
2300 r = allocate_inotify(j);
2301 if (r < 0)
2302 return r;
2303
5d1ce257
LP
2304 log_debug("Reiterating files to get inotify watches established");
2305
a963990f
LP
2306 /* Iterate through all dirs again, to add them to the
2307 * inotify */
5302ebe1
ZJS
2308 if (j->no_new_files)
2309 r = add_current_paths(j);
e73529f9
EV
2310 else if (j->flags & SD_JOURNAL_OS_ROOT)
2311 r = add_search_paths(j);
5d1ce257
LP
2312 else if (j->toplevel_fd >= 0)
2313 r = add_root_directory(j, NULL, false);
5302ebe1 2314 else if (j->path)
d617408e 2315 r = add_root_directory(j, j->path, true);
7827b1a1 2316 else
89739579 2317 r = add_search_paths(j);
a963990f
LP
2318 if (r < 0)
2319 return r;
2320
50f20cfd
LP
2321 return j->inotify_fd;
2322}
2323
ee531d94
LP
2324_public_ int sd_journal_get_events(sd_journal *j) {
2325 int fd;
2326
1ae464e0
TA
2327 assert_return(j, -EINVAL);
2328 assert_return(!journal_pid_changed(j), -ECHILD);
ee531d94
LP
2329
2330 fd = sd_journal_get_fd(j);
2331 if (fd < 0)
2332 return fd;
2333
2334 return POLLIN;
2335}
2336
39c155ea
LP
2337_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2338 int fd;
2339
1ae464e0
TA
2340 assert_return(j, -EINVAL);
2341 assert_return(!journal_pid_changed(j), -ECHILD);
2342 assert_return(timeout_usec, -EINVAL);
39c155ea
LP
2343
2344 fd = sd_journal_get_fd(j);
2345 if (fd < 0)
2346 return fd;
2347
2348 if (!j->on_network) {
2349 *timeout_usec = (uint64_t) -1;
2350 return 0;
2351 }
2352
2353 /* If we are on the network we need to regularly check for
2354 * changes manually */
2355
2356 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2357 return 1;
2358}
2359
50f20cfd 2360static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
a963990f 2361 Directory *d;
50f20cfd
LP
2362
2363 assert(j);
2364 assert(e);
2365
2366 /* Is this a subdirectory we watch? */
a963990f
LP
2367 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2368 if (d) {
2369 sd_id128_t id;
50f20cfd 2370
de2c3907
LP
2371 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2372 (endswith(e->name, ".journal") ||
2373 endswith(e->name, ".journal~"))) {
50f20cfd
LP
2374
2375 /* Event for a journal file */
2376
d617408e
LP
2377 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2378 (void) add_file(j, d->path, e->name);
2379 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
2380 remove_file(j, d->path, e->name);
50f20cfd 2381
a963990f 2382 } else if (!d->is_root && e->len == 0) {
50f20cfd 2383
a963990f 2384 /* Event for a subdirectory */
50f20cfd 2385
b2b46f91
TA
2386 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2387 remove_directory(j, d);
50f20cfd 2388
a963990f 2389 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
50f20cfd 2390
a963990f 2391 /* Event for root directory */
50f20cfd 2392
d617408e
LP
2393 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2394 (void) add_directory(j, d->path, e->name);
50f20cfd
LP
2395 }
2396
2397 return;
2398 }
2399
2400 if (e->mask & IN_IGNORED)
2401 return;
2402
d617408e 2403 log_debug("Unknown inotify event.");
50f20cfd
LP
2404}
2405
a963990f
LP
2406static int determine_change(sd_journal *j) {
2407 bool b;
2408
2409 assert(j);
2410
2411 b = j->current_invalidate_counter != j->last_invalidate_counter;
2412 j->last_invalidate_counter = j->current_invalidate_counter;
2413
2414 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2415}
2416
a5344d2c 2417_public_ int sd_journal_process(sd_journal *j) {
a963990f 2418 bool got_something = false;
50f20cfd 2419
1ae464e0
TA
2420 assert_return(j, -EINVAL);
2421 assert_return(!journal_pid_changed(j), -ECHILD);
50f20cfd 2422
39c155ea
LP
2423 j->last_process_usec = now(CLOCK_MONOTONIC);
2424
50f20cfd 2425 for (;;) {
0254e944 2426 union inotify_event_buffer buffer;
50f20cfd
LP
2427 struct inotify_event *e;
2428 ssize_t l;
2429
0254e944 2430 l = read(j->inotify_fd, &buffer, sizeof(buffer));
50f20cfd 2431 if (l < 0) {
a963990f
LP
2432 if (errno == EAGAIN || errno == EINTR)
2433 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
50f20cfd
LP
2434
2435 return -errno;
2436 }
2437
a963990f
LP
2438 got_something = true;
2439
f7c1ad4f 2440 FOREACH_INOTIFY_EVENT(e, buffer, l)
50f20cfd 2441 process_inotify_event(j, e);
50f20cfd
LP
2442 }
2443}
6ad1d1c3 2444
e02d1cf7 2445_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
a963990f 2446 int r;
39c155ea 2447 uint64_t t;
e02d1cf7 2448
1ae464e0
TA
2449 assert_return(j, -EINVAL);
2450 assert_return(!journal_pid_changed(j), -ECHILD);
e02d1cf7 2451
a963990f
LP
2452 if (j->inotify_fd < 0) {
2453
2454 /* This is the first invocation, hence create the
2455 * inotify watch */
2456 r = sd_journal_get_fd(j);
2457 if (r < 0)
2458 return r;
2459
2460 /* The journal might have changed since the context
2461 * object was created and we weren't watching before,
2462 * hence don't wait for anything, and return
2463 * immediately. */
2464 return determine_change(j);
2465 }
2466
39c155ea
LP
2467 r = sd_journal_get_timeout(j, &t);
2468 if (r < 0)
2469 return r;
2470
2471 if (t != (uint64_t) -1) {
2472 usec_t n;
2473
2474 n = now(CLOCK_MONOTONIC);
2475 t = t > n ? t - n : 0;
85210bff 2476
39c155ea
LP
2477 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2478 timeout_usec = t;
85210bff
LP
2479 }
2480
a963990f
LP
2481 do {
2482 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2483 } while (r == -EINTR);
e02d1cf7
LP
2484
2485 if (r < 0)
2486 return r;
2487
a963990f 2488 return sd_journal_process(j);
e02d1cf7
LP
2489}
2490
08984293
LP
2491_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2492 Iterator i;
2493 JournalFile *f;
2494 bool first = true;
581483bf 2495 uint64_t fmin = 0, tmax = 0;
08984293
LP
2496 int r;
2497
1ae464e0
TA
2498 assert_return(j, -EINVAL);
2499 assert_return(!journal_pid_changed(j), -ECHILD);
2500 assert_return(from || to, -EINVAL);
2501 assert_return(from != to, -EINVAL);
08984293 2502
c1f906bd 2503 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2504 usec_t fr, t;
2505
2506 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
9f8d2983
LP
2507 if (r == -ENOENT)
2508 continue;
08984293
LP
2509 if (r < 0)
2510 return r;
2511 if (r == 0)
2512 continue;
2513
2514 if (first) {
581483bf
LP
2515 fmin = fr;
2516 tmax = t;
08984293
LP
2517 first = false;
2518 } else {
581483bf
LP
2519 fmin = MIN(fr, fmin);
2520 tmax = MAX(t, tmax);
08984293
LP
2521 }
2522 }
2523
581483bf
LP
2524 if (from)
2525 *from = fmin;
2526 if (to)
2527 *to = tmax;
2528
08984293
LP
2529 return first ? 0 : 1;
2530}
2531
2532_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2533 Iterator i;
2534 JournalFile *f;
1651e2c6 2535 bool found = false;
08984293
LP
2536 int r;
2537
1ae464e0
TA
2538 assert_return(j, -EINVAL);
2539 assert_return(!journal_pid_changed(j), -ECHILD);
2540 assert_return(from || to, -EINVAL);
2541 assert_return(from != to, -EINVAL);
08984293 2542
c1f906bd 2543 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
08984293
LP
2544 usec_t fr, t;
2545
2546 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
9f8d2983
LP
2547 if (r == -ENOENT)
2548 continue;
08984293
LP
2549 if (r < 0)
2550 return r;
2551 if (r == 0)
2552 continue;
2553
1651e2c6 2554 if (found) {
08984293 2555 if (from)
1651e2c6 2556 *from = MIN(fr, *from);
08984293 2557 if (to)
1651e2c6 2558 *to = MAX(t, *to);
08984293
LP
2559 } else {
2560 if (from)
1651e2c6 2561 *from = fr;
08984293 2562 if (to)
1651e2c6
ZJS
2563 *to = t;
2564 found = true;
08984293
LP
2565 }
2566 }
2567
1651e2c6 2568 return found;
08984293
LP
2569}
2570
dca6219e
LP
2571void journal_print_header(sd_journal *j) {
2572 Iterator i;
2573 JournalFile *f;
2574 bool newline = false;
2575
2576 assert(j);
2577
c1f906bd 2578 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
dca6219e
LP
2579 if (newline)
2580 putchar('\n');
2581 else
2582 newline = true;
2583
2584 journal_file_print_header(f);
2585 }
2586}
08984293 2587
a1a03e30
LP
2588_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2589 Iterator i;
2590 JournalFile *f;
2591 uint64_t sum = 0;
2592
1ae464e0
TA
2593 assert_return(j, -EINVAL);
2594 assert_return(!journal_pid_changed(j), -ECHILD);
2595 assert_return(bytes, -EINVAL);
a1a03e30 2596
c1f906bd 2597 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
a1a03e30
LP
2598 struct stat st;
2599
2600 if (fstat(f->fd, &st) < 0)
2601 return -errno;
2602
2603 sum += (uint64_t) st.st_blocks * 512ULL;
2604 }
2605
2606 *bytes = sum;
2607 return 0;
2608}
2609
3c1668da
LP
2610_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2611 char *f;
2612
1ae464e0
TA
2613 assert_return(j, -EINVAL);
2614 assert_return(!journal_pid_changed(j), -ECHILD);
2615 assert_return(!isempty(field), -EINVAL);
2616 assert_return(field_is_valid(field), -EINVAL);
3c1668da
LP
2617
2618 f = strdup(field);
2619 if (!f)
2620 return -ENOMEM;
2621
2622 free(j->unique_field);
2623 j->unique_field = f;
2624 j->unique_file = NULL;
2625 j->unique_offset = 0;
360af4cf 2626 j->unique_file_lost = false;
3c1668da
LP
2627
2628 return 0;
2629}
2630
2631_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
3c1668da 2632 size_t k;
19a2bd80 2633
1ae464e0
TA
2634 assert_return(j, -EINVAL);
2635 assert_return(!journal_pid_changed(j), -ECHILD);
2636 assert_return(data, -EINVAL);
2637 assert_return(l, -EINVAL);
2638 assert_return(j->unique_field, -EINVAL);
19a2bd80 2639
3c1668da 2640 k = strlen(j->unique_field);
19a2bd80 2641
3c1668da 2642 if (!j->unique_file) {
360af4cf
ZJS
2643 if (j->unique_file_lost)
2644 return 0;
2645
c1f906bd 2646 j->unique_file = ordered_hashmap_first(j->files);
3c1668da
LP
2647 if (!j->unique_file)
2648 return 0;
360af4cf 2649
3c1668da
LP
2650 j->unique_offset = 0;
2651 }
19a2bd80 2652
3c1668da
LP
2653 for (;;) {
2654 JournalFile *of;
2655 Iterator i;
ae97089d 2656 Object *o;
3c1668da
LP
2657 const void *odata;
2658 size_t ol;
2659 bool found;
ae97089d 2660 int r;
3c1668da 2661
bdc02927 2662 /* Proceed to next data object in the field's linked list */
3c1668da
LP
2663 if (j->unique_offset == 0) {
2664 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2665 if (r < 0)
2666 return r;
2667
2668 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2669 } else {
2670 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2671 if (r < 0)
2672 return r;
2673
2674 j->unique_offset = le64toh(o->data.next_field_offset);
2675 }
2676
2677 /* We reached the end of the list? Then start again, with the next file */
2678 if (j->unique_offset == 0) {
c1f906bd 2679 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
360af4cf 2680 if (!j->unique_file)
3c1668da
LP
2681 return 0;
2682
3c1668da
LP
2683 continue;
2684 }
2685
d05089d8
MS
2686 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2687 * instead, so that we can look at this data object at the same
3c1668da 2688 * time as one on another file */
d05089d8 2689 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
3c1668da
LP
2690 if (r < 0)
2691 return r;
2692
2693 /* Let's do the type check by hand, since we used 0 context above. */
ae97089d 2694 if (o->object.type != OBJECT_DATA) {
36202fd2 2695 log_debug("%s:offset " OFSfmt ": object has type %d, expected %d",
ae97089d
ZJS
2696 j->unique_file->path, j->unique_offset,
2697 o->object.type, OBJECT_DATA);
3c1668da 2698 return -EBADMSG;
ae97089d
ZJS
2699 }
2700
93b73b06 2701 r = return_data(j, j->unique_file, o, &odata, &ol);
3c1668da
LP
2702 if (r < 0)
2703 return r;
2704
0f99f74a
ZJS
2705 /* Check if we have at least the field name and "=". */
2706 if (ol <= k) {
2707 log_debug("%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2708 j->unique_file->path, j->unique_offset,
2709 ol, k + 1);
2710 return -EBADMSG;
2711 }
2712
2713 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=') {
2714 log_debug("%s:offset " OFSfmt ": object does not start with \"%s=\"",
2715 j->unique_file->path, j->unique_offset,
2716 j->unique_field);
2717 return -EBADMSG;
2718 }
2719
3c1668da
LP
2720 /* OK, now let's see if we already returned this data
2721 * object by checking if it exists in the earlier
2722 * traversed files. */
2723 found = false;
c1f906bd 2724 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3c1668da
LP
2725 if (of == j->unique_file)
2726 break;
2727
ed71f956
LP
2728 /* Skip this file it didn't have any fields indexed */
2729 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3c1668da
LP
2730 continue;
2731
ed71f956 2732 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
3c1668da
LP
2733 if (r < 0)
2734 return r;
ed71f956 2735 if (r > 0) {
3c1668da 2736 found = true;
ed71f956
LP
2737 break;
2738 }
3c1668da
LP
2739 }
2740
06cc69d4
JJ
2741 if (found)
2742 continue;
2743
93b73b06 2744 r = return_data(j, j->unique_file, o, data, l);
3c1668da
LP
2745 if (r < 0)
2746 return r;
2747
2748 return 1;
2749 }
2750}
2751
115646c7 2752_public_ void sd_journal_restart_unique(sd_journal *j) {
3c1668da
LP
2753 if (!j)
2754 return;
2755
2756 j->unique_file = NULL;
2757 j->unique_offset = 0;
360af4cf 2758 j->unique_file_lost = false;
3c1668da 2759}
85210bff 2760
eb86030e
LP
2761_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
2762 int r;
2763
2764 assert_return(j, -EINVAL);
2765 assert_return(!journal_pid_changed(j), -ECHILD);
2766 assert_return(field, -EINVAL);
2767
2768 if (!j->fields_file) {
2769 if (j->fields_file_lost)
2770 return 0;
2771
2772 j->fields_file = ordered_hashmap_first(j->files);
2773 if (!j->fields_file)
2774 return 0;
2775
2776 j->fields_hash_table_index = 0;
2777 j->fields_offset = 0;
2778 }
2779
2780 for (;;) {
2781 JournalFile *f, *of;
2782 Iterator i;
2783 uint64_t m;
2784 Object *o;
2785 size_t sz;
2786 bool found;
2787
2788 f = j->fields_file;
2789
2790 if (j->fields_offset == 0) {
2791 bool eof = false;
2792
2793 /* We are not yet positioned at any field. Let's pick the first one */
2794 r = journal_file_map_field_hash_table(f);
2795 if (r < 0)
2796 return r;
2797
2798 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
2799 for (;;) {
2800 if (j->fields_hash_table_index >= m) {
2801 /* Reached the end of the hash table, go to the next file. */
2802 eof = true;
2803 break;
2804 }
2805
2806 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
2807
2808 if (j->fields_offset != 0)
2809 break;
2810
2811 /* Empty hash table bucket, go to next one */
2812 j->fields_hash_table_index++;
2813 }
2814
2815 if (eof) {
2816 /* Proceed with next file */
2817 j->fields_file = ordered_hashmap_next(j->files, f->path);
2818 if (!j->fields_file) {
2819 *field = NULL;
2820 return 0;
2821 }
2822
2823 j->fields_offset = 0;
2824 j->fields_hash_table_index = 0;
2825 continue;
2826 }
2827
2828 } else {
2829 /* We are already positioned at a field. If so, let's figure out the next field from it */
2830
2831 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
2832 if (r < 0)
2833 return r;
2834
2835 j->fields_offset = le64toh(o->field.next_hash_offset);
2836 if (j->fields_offset == 0) {
2837 /* Reached the end of the hash table chain */
2838 j->fields_hash_table_index++;
2839 continue;
2840 }
2841 }
2842
1f133e0d 2843 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
eb86030e
LP
2844 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
2845 if (r < 0)
2846 return r;
2847
2848 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2849 if (o->object.type != OBJECT_FIELD) {
2850 log_debug("%s:offset " OFSfmt ": object has type %i, expected %i", f->path, j->fields_offset, o->object.type, OBJECT_FIELD);
2851 return -EBADMSG;
2852 }
2853
2854 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
2855
2856 /* Let's see if we already returned this field name before. */
2857 found = false;
2858 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
2859 if (of == f)
2860 break;
2861
2862 /* Skip this file it didn't have any fields indexed */
2863 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
2864 continue;
2865
2866 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
2867 if (r < 0)
2868 return r;
2869 if (r > 0) {
2870 found = true;
2871 break;
2872 }
2873 }
2874
2875 if (found)
2876 continue;
2877
2878 /* Check if this is really a valid string containing no NUL byte */
2879 if (memchr(o->field.payload, 0, sz))
2880 return -EBADMSG;
2881
2882 if (sz > j->data_threshold)
2883 sz = j->data_threshold;
2884
2885 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
2886 return -ENOMEM;
2887
2888 memcpy(j->fields_buffer, o->field.payload, sz);
2889 j->fields_buffer[sz] = 0;
2890
2891 if (!field_is_valid(j->fields_buffer))
2892 return -EBADMSG;
2893
2894 *field = j->fields_buffer;
2895 return 1;
2896 }
2897}
2898
2899_public_ void sd_journal_restart_fields(sd_journal *j) {
2900 if (!j)
2901 return;
2902
2903 j->fields_file = NULL;
2904 j->fields_hash_table_index = 0;
2905 j->fields_offset = 0;
2906 j->fields_file_lost = false;
2907}
2908
85210bff 2909_public_ int sd_journal_reliable_fd(sd_journal *j) {
1ae464e0
TA
2910 assert_return(j, -EINVAL);
2911 assert_return(!journal_pid_changed(j), -ECHILD);
85210bff
LP
2912
2913 return !j->on_network;
2914}
d4205751
LP
2915
2916static char *lookup_field(const char *field, void *userdata) {
2917 sd_journal *j = userdata;
2918 const void *data;
2919 size_t size, d;
2920 int r;
2921
2922 assert(field);
2923 assert(j);
2924
2925 r = sd_journal_get_data(j, field, &data, &size);
2926 if (r < 0 ||
2927 size > REPLACE_VAR_MAX)
2928 return strdup(field);
2929
2930 d = strlen(field) + 1;
2931
2932 return strndup((const char*) data + d, size - d);
2933}
2934
2935_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
2936 const void *data;
2937 size_t size;
2938 sd_id128_t id;
2939 _cleanup_free_ char *text = NULL, *cid = NULL;
2940 char *t;
2941 int r;
2942
1ae464e0
TA
2943 assert_return(j, -EINVAL);
2944 assert_return(!journal_pid_changed(j), -ECHILD);
2945 assert_return(ret, -EINVAL);
d4205751
LP
2946
2947 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
2948 if (r < 0)
2949 return r;
2950
2951 cid = strndup((const char*) data + 11, size - 11);
2952 if (!cid)
2953 return -ENOMEM;
2954
2955 r = sd_id128_from_string(cid, &id);
2956 if (r < 0)
2957 return r;
2958
844ec79b 2959 r = catalog_get(CATALOG_DATABASE, id, &text);
d4205751
LP
2960 if (r < 0)
2961 return r;
2962
2963 t = replace_var(text, lookup_field, j);
2964 if (!t)
2965 return -ENOMEM;
2966
2967 *ret = t;
2968 return 0;
2969}
8f1e860f
LP
2970
2971_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
1ae464e0 2972 assert_return(ret, -EINVAL);
8f1e860f 2973
844ec79b 2974 return catalog_get(CATALOG_DATABASE, id, ret);
8f1e860f 2975}
93b73b06
LP
2976
2977_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
1ae464e0
TA
2978 assert_return(j, -EINVAL);
2979 assert_return(!journal_pid_changed(j), -ECHILD);
93b73b06
LP
2980
2981 j->data_threshold = sz;
2982 return 0;
2983}
2984
2985_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
1ae464e0
TA
2986 assert_return(j, -EINVAL);
2987 assert_return(!journal_pid_changed(j), -ECHILD);
2988 assert_return(sz, -EINVAL);
93b73b06
LP
2989
2990 *sz = j->data_threshold;
2991 return 0;
2992}
39fd5b08
JS
2993
2994_public_ int sd_journal_has_runtime_files(sd_journal *j) {
2995 assert_return(j, -EINVAL);
2996
2997 return j->has_runtime_files;
2998}
2999
3000_public_ int sd_journal_has_persistent_files(sd_journal *j) {
3001 assert_return(j, -EINVAL);
3002
3003 return j->has_persistent_files;
3004}