]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
87d2c1ff | 2 | |
87d2c1ff | 3 | #include <errno.h> |
87d2c1ff | 4 | #include <fcntl.h> |
82e24b00 | 5 | #include <inttypes.h> |
07630cea LP |
6 | #include <linux/magic.h> |
7 | #include <poll.h> | |
3fbf9cbb | 8 | #include <stddef.h> |
50f20cfd | 9 | #include <sys/inotify.h> |
85210bff | 10 | #include <sys/vfs.h> |
07630cea | 11 | #include <unistd.h> |
87d2c1ff LP |
12 | |
13 | #include "sd-journal.h" | |
c004493c | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
07630cea LP |
16 | #include "catalog.h" |
17 | #include "compress.h" | |
a0956174 | 18 | #include "dirent-util.h" |
686d13b9 | 19 | #include "env-file.h" |
9e8b1ec0 | 20 | #include "escape.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
07630cea | 22 | #include "fileio.h" |
f97b34a6 | 23 | #include "format-util.h" |
77601719 | 24 | #include "fs-util.h" |
07630cea LP |
25 | #include "hashmap.h" |
26 | #include "hostname-util.h" | |
a9be0692 | 27 | #include "id128-util.h" |
9e5fd717 | 28 | #include "inotify-util.h" |
c004493c | 29 | #include "io-util.h" |
87d2c1ff | 30 | #include "journal-def.h" |
cec736d2 | 31 | #include "journal-file.h" |
07630cea | 32 | #include "journal-internal.h" |
cec736d2 | 33 | #include "list.h" |
de7b95cd | 34 | #include "lookup3.h" |
d8b4d14d | 35 | #include "nulstr-util.h" |
07630cea | 36 | #include "path-util.h" |
34af7494 | 37 | #include "prioq.h" |
dccca82b | 38 | #include "process-util.h" |
d4205751 | 39 | #include "replace-var.h" |
15a5e950 LP |
40 | #include "stat-util.h" |
41 | #include "stdio-util.h" | |
07630cea LP |
42 | #include "string-util.h" |
43 | #include "strv.h" | |
456aa879 | 44 | #include "syslog-util.h" |
87d2c1ff | 45 | |
85210bff LP |
46 | #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC) |
47 | ||
8dd7cbce LP |
48 | /* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as |
49 | * we want to be able to show all officially valid paths at least */ | |
50 | #define REPLACE_VAR_MAX PATH_MAX | |
d4205751 | 51 | |
93b73b06 LP |
52 | #define DEFAULT_DATA_THRESHOLD (64*1024) |
53 | ||
a9a245c1 | 54 | static void remove_file_real(sd_journal *j, JournalFile *f); |
34af7494 LP |
55 | static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f); |
56 | static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f); | |
a9a245c1 | 57 | |
a65f06bb ZJS |
58 | static bool journal_pid_changed(sd_journal *j) { |
59 | assert(j); | |
60 | ||
61 | /* We don't support people creating a journal object and | |
62 | * keeping it around over a fork(). Let's complain. */ | |
63 | ||
df0ff127 | 64 | return j->original_pid != getpid_cached(); |
a65f06bb ZJS |
65 | } |
66 | ||
5768d259 | 67 | static int journal_put_error(sd_journal *j, int r, const char *path) { |
8231485b | 68 | _cleanup_free_ char *copy = NULL; |
3ac251b8 LP |
69 | int k; |
70 | ||
5768d259 LP |
71 | /* Memorize an error we encountered, and store which |
72 | * file/directory it was generated from. Note that we store | |
73 | * only *one* path per error code, as the error code is the | |
74 | * key into the hashmap, and the path is the value. This means | |
75 | * we keep track only of all error kinds, but not of all error | |
76 | * locations. This has the benefit that the hashmap cannot | |
77 | * grow beyond bounds. | |
78 | * | |
79 | * We return an error here only if we didn't manage to | |
80 | * memorize the real error. */ | |
81 | ||
6fe391c5 ZJS |
82 | if (r >= 0) |
83 | return r; | |
84 | ||
5768d259 LP |
85 | if (path) { |
86 | copy = strdup(path); | |
87 | if (!copy) | |
88 | return -ENOMEM; | |
8231485b | 89 | } |
5768d259 | 90 | |
faa7e5a4 | 91 | k = hashmap_ensure_put(&j->errors, NULL, INT_TO_PTR(r), copy); |
5768d259 | 92 | if (k < 0) { |
5768d259 LP |
93 | if (k == -EEXIST) |
94 | return 0; | |
95 | ||
96 | return k; | |
97 | } | |
98 | ||
8231485b | 99 | TAKE_PTR(copy); |
5768d259 | 100 | return 0; |
6fe391c5 ZJS |
101 | } |
102 | ||
de190aef | 103 | static void detach_location(sd_journal *j) { |
8f9b6cd9 LP |
104 | JournalFile *f; |
105 | ||
106 | assert(j); | |
107 | ||
108 | j->current_file = NULL; | |
109 | j->current_field = 0; | |
110 | ||
90e74a66 | 111 | ORDERED_HASHMAP_FOREACH(f, j->files) |
1fc605b0 | 112 | journal_file_reset_location(f); |
8f9b6cd9 LP |
113 | } |
114 | ||
a87247dd | 115 | static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) { |
de190aef | 116 | assert(l); |
3742095b | 117 | assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK)); |
de190aef | 118 | assert(f); |
de190aef | 119 | |
bba6e4ae LP |
120 | *l = (Location) { |
121 | .type = type, | |
122 | .seqnum = le64toh(o->entry.seqnum), | |
123 | .seqnum_id = f->header->seqnum_id, | |
124 | .realtime = le64toh(o->entry.realtime), | |
125 | .monotonic = le64toh(o->entry.monotonic), | |
126 | .boot_id = o->entry.boot_id, | |
127 | .xor_hash = le64toh(o->entry.xor_hash), | |
128 | .seqnum_set = true, | |
129 | .realtime_set = true, | |
130 | .monotonic_set = true, | |
131 | .xor_hash_set = true, | |
132 | }; | |
de190aef LP |
133 | } |
134 | ||
1eb6332d | 135 | static void set_location(sd_journal *j, JournalFile *f, Object *o) { |
de190aef LP |
136 | assert(j); |
137 | assert(f); | |
138 | assert(o); | |
139 | ||
1eb6332d | 140 | init_location(&j->current_location, LOCATION_DISCRETE, f, o); |
de190aef LP |
141 | |
142 | j->current_file = f; | |
143 | j->current_field = 0; | |
144 | ||
6573ef05 MS |
145 | /* Let f know its candidate entry was picked. */ |
146 | assert(f->location_type == LOCATION_SEEK); | |
147 | f->location_type = LOCATION_DISCRETE; | |
de190aef LP |
148 | } |
149 | ||
cbdca852 | 150 | static int match_is_valid(const void *data, size_t size) { |
99534007 | 151 | const char *b = ASSERT_PTR(data); |
cbdca852 LP |
152 | |
153 | if (size < 2) | |
154 | return false; | |
155 | ||
e0567bc8 | 156 | if (((char*) data)[0] == '_' && ((char*) data)[1] == '_') |
cbdca852 LP |
157 | return false; |
158 | ||
abcdc02c | 159 | for (const char *p = b; p < b + size; p++) { |
cbdca852 LP |
160 | |
161 | if (*p == '=') | |
162 | return p > b; | |
163 | ||
164 | if (*p == '_') | |
165 | continue; | |
166 | ||
167 | if (*p >= 'A' && *p <= 'Z') | |
168 | continue; | |
169 | ||
ff25d338 | 170 | if (ascii_isdigit(*p)) |
cbdca852 LP |
171 | continue; |
172 | ||
173 | return false; | |
174 | } | |
175 | ||
176 | return false; | |
177 | } | |
178 | ||
179 | static bool same_field(const void *_a, size_t s, const void *_b, size_t t) { | |
de190aef | 180 | const uint8_t *a = _a, *b = _b; |
de190aef | 181 | |
abcdc02c | 182 | for (size_t j = 0; j < s && j < t; j++) { |
de190aef | 183 | |
de190aef | 184 | if (a[j] != b[j]) |
cbdca852 | 185 | return false; |
de190aef | 186 | |
cbdca852 LP |
187 | if (a[j] == '=') |
188 | return true; | |
de190aef LP |
189 | } |
190 | ||
04499a70 | 191 | assert_not_reached(); |
cbdca852 LP |
192 | } |
193 | ||
194 | static Match *match_new(Match *p, MatchType t) { | |
195 | Match *m; | |
196 | ||
2f5435a1 | 197 | m = new(Match, 1); |
cbdca852 LP |
198 | if (!m) |
199 | return NULL; | |
200 | ||
2f5435a1 LP |
201 | *m = (Match) { |
202 | .type = t, | |
203 | .parent = p, | |
204 | }; | |
cbdca852 | 205 | |
2f5435a1 | 206 | if (p) |
71fda00f | 207 | LIST_PREPEND(matches, p->matches, m); |
cbdca852 LP |
208 | |
209 | return m; | |
210 | } | |
211 | ||
39dfc0de | 212 | static Match *match_free(Match *m) { |
cbdca852 LP |
213 | assert(m); |
214 | ||
215 | while (m->matches) | |
216 | match_free(m->matches); | |
217 | ||
218 | if (m->parent) | |
71fda00f | 219 | LIST_REMOVE(matches, m->parent->matches, m); |
cbdca852 LP |
220 | |
221 | free(m->data); | |
39dfc0de | 222 | return mfree(m); |
cbdca852 LP |
223 | } |
224 | ||
39dfc0de | 225 | static Match *match_free_if_empty(Match *m) { |
c5a10d9c | 226 | if (!m || m->matches) |
39dfc0de | 227 | return m; |
cbdca852 | 228 | |
39dfc0de | 229 | return match_free(m); |
de190aef LP |
230 | } |
231 | ||
a5344d2c | 232 | _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) { |
03677889 | 233 | Match *add_here = NULL, *m = NULL; |
cde8c5f7 | 234 | uint64_t hash; |
87d2c1ff | 235 | |
1ae464e0 TA |
236 | assert_return(j, -EINVAL); |
237 | assert_return(!journal_pid_changed(j), -ECHILD); | |
238 | assert_return(data, -EINVAL); | |
cbdca852 LP |
239 | |
240 | if (size == 0) | |
241 | size = strlen(data); | |
242 | ||
1ae464e0 | 243 | assert_return(match_is_valid(data, size), -EINVAL); |
1cc101f1 | 244 | |
cd34b3c6 HH |
245 | /* level 0: AND term |
246 | * level 1: OR terms | |
247 | * level 2: AND terms | |
248 | * level 3: OR terms | |
249 | * level 4: concrete matches */ | |
cbdca852 LP |
250 | |
251 | if (!j->level0) { | |
cd34b3c6 | 252 | j->level0 = match_new(NULL, MATCH_AND_TERM); |
cbdca852 LP |
253 | if (!j->level0) |
254 | return -ENOMEM; | |
255 | } | |
256 | ||
257 | if (!j->level1) { | |
cd34b3c6 | 258 | j->level1 = match_new(j->level0, MATCH_OR_TERM); |
cbdca852 LP |
259 | if (!j->level1) |
260 | return -ENOMEM; | |
261 | } | |
262 | ||
cd34b3c6 HH |
263 | if (!j->level2) { |
264 | j->level2 = match_new(j->level1, MATCH_AND_TERM); | |
265 | if (!j->level2) | |
266 | return -ENOMEM; | |
267 | } | |
268 | ||
269 | assert(j->level0->type == MATCH_AND_TERM); | |
270 | assert(j->level1->type == MATCH_OR_TERM); | |
271 | assert(j->level2->type == MATCH_AND_TERM); | |
ab4979d2 | 272 | |
4ce534f4 LP |
273 | /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing |
274 | * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */ | |
20b0acfa | 275 | hash = jenkins_hash64(data, size); |
de190aef | 276 | |
cd34b3c6 HH |
277 | LIST_FOREACH(matches, l3, j->level2->matches) { |
278 | assert(l3->type == MATCH_OR_TERM); | |
de190aef | 279 | |
cd34b3c6 HH |
280 | LIST_FOREACH(matches, l4, l3->matches) { |
281 | assert(l4->type == MATCH_DISCRETE); | |
de190aef | 282 | |
cbdca852 LP |
283 | /* Exactly the same match already? Then ignore |
284 | * this addition */ | |
cde8c5f7 | 285 | if (l4->hash == hash && |
cd34b3c6 HH |
286 | l4->size == size && |
287 | memcmp(l4->data, data, size) == 0) | |
cbdca852 LP |
288 | return 0; |
289 | ||
290 | /* Same field? Then let's add this to this OR term */ | |
cd34b3c6 HH |
291 | if (same_field(data, size, l4->data, l4->size)) { |
292 | add_here = l3; | |
cbdca852 LP |
293 | break; |
294 | } | |
295 | } | |
296 | ||
297 | if (add_here) | |
298 | break; | |
de190aef LP |
299 | } |
300 | ||
cbdca852 | 301 | if (!add_here) { |
cd34b3c6 | 302 | add_here = match_new(j->level2, MATCH_OR_TERM); |
cbdca852 LP |
303 | if (!add_here) |
304 | goto fail; | |
305 | } | |
306 | ||
307 | m = match_new(add_here, MATCH_DISCRETE); | |
cec736d2 | 308 | if (!m) |
cbdca852 | 309 | goto fail; |
87d2c1ff | 310 | |
cde8c5f7 | 311 | m->hash = hash; |
1cc101f1 | 312 | m->size = size; |
cbdca852 LP |
313 | m->data = memdup(data, size); |
314 | if (!m->data) | |
315 | goto fail; | |
316 | ||
317 | detach_location(j); | |
318 | ||
319 | return 0; | |
320 | ||
321 | fail: | |
418cce62 | 322 | match_free(m); |
c5a10d9c | 323 | match_free_if_empty(add_here); |
39dfc0de YW |
324 | j->level2 = match_free_if_empty(j->level2); |
325 | j->level1 = match_free_if_empty(j->level1); | |
326 | j->level0 = match_free_if_empty(j->level0); | |
cbdca852 LP |
327 | |
328 | return -ENOMEM; | |
329 | } | |
330 | ||
cd34b3c6 | 331 | _public_ int sd_journal_add_conjunction(sd_journal *j) { |
1ae464e0 TA |
332 | assert_return(j, -EINVAL); |
333 | assert_return(!journal_pid_changed(j), -ECHILD); | |
1cc101f1 | 334 | |
cbdca852 LP |
335 | if (!j->level0) |
336 | return 0; | |
337 | ||
338 | if (!j->level1) | |
339 | return 0; | |
340 | ||
341 | if (!j->level1->matches) | |
342 | return 0; | |
343 | ||
cd34b3c6 HH |
344 | j->level1 = NULL; |
345 | j->level2 = NULL; | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | _public_ int sd_journal_add_disjunction(sd_journal *j) { | |
1ae464e0 TA |
351 | assert_return(j, -EINVAL); |
352 | assert_return(!journal_pid_changed(j), -ECHILD); | |
cd34b3c6 HH |
353 | |
354 | if (!j->level0) | |
355 | return 0; | |
356 | ||
357 | if (!j->level1) | |
358 | return 0; | |
359 | ||
360 | if (!j->level2) | |
361 | return 0; | |
362 | ||
363 | if (!j->level2->matches) | |
364 | return 0; | |
cbdca852 | 365 | |
cd34b3c6 | 366 | j->level2 = NULL; |
cbdca852 LP |
367 | return 0; |
368 | } | |
369 | ||
370 | static char *match_make_string(Match *m) { | |
6320409c | 371 | _cleanup_free_ char *p = NULL; |
cbdca852 LP |
372 | bool enclose = false; |
373 | ||
374 | if (!m) | |
4ad16808 | 375 | return strdup("none"); |
cbdca852 LP |
376 | |
377 | if (m->type == MATCH_DISCRETE) | |
9e8b1ec0 | 378 | return cescape_length(m->data, m->size); |
cbdca852 | 379 | |
cbdca852 | 380 | LIST_FOREACH(matches, i, m->matches) { |
6320409c | 381 | _cleanup_free_ char *t = NULL; |
cbdca852 LP |
382 | |
383 | t = match_make_string(i); | |
6b430fdb | 384 | if (!t) |
6320409c | 385 | return NULL; |
cbdca852 LP |
386 | |
387 | if (p) { | |
6320409c | 388 | if (!strextend(&p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t)) |
cbdca852 LP |
389 | return NULL; |
390 | ||
cbdca852 | 391 | enclose = true; |
bc302926 | 392 | } else |
6320409c | 393 | p = TAKE_PTR(t); |
87d2c1ff LP |
394 | } |
395 | ||
6320409c LP |
396 | if (enclose) |
397 | return strjoin("(", p, ")"); | |
87d2c1ff | 398 | |
6320409c | 399 | return TAKE_PTR(p); |
cbdca852 | 400 | } |
de7b95cd | 401 | |
cbdca852 LP |
402 | char *journal_make_match_string(sd_journal *j) { |
403 | assert(j); | |
8f9b6cd9 | 404 | |
cbdca852 | 405 | return match_make_string(j->level0); |
87d2c1ff LP |
406 | } |
407 | ||
a5344d2c LP |
408 | _public_ void sd_journal_flush_matches(sd_journal *j) { |
409 | if (!j) | |
410 | return; | |
87d2c1ff | 411 | |
cbdca852 LP |
412 | if (j->level0) |
413 | match_free(j->level0); | |
de7b95cd | 414 | |
cd34b3c6 | 415 | j->level0 = j->level1 = j->level2 = NULL; |
8f9b6cd9 | 416 | |
de190aef | 417 | detach_location(j); |
87d2c1ff LP |
418 | } |
419 | ||
b6849042 | 420 | _pure_ static int compare_with_location(const JournalFile *f, const Location *l, const JournalFile *current_file) { |
90c88092 YW |
421 | int r; |
422 | ||
487d3720 | 423 | assert(f); |
de190aef | 424 | assert(l); |
487d3720 | 425 | assert(f->location_type == LOCATION_SEEK); |
4c701096 | 426 | assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK)); |
de190aef LP |
427 | |
428 | if (l->monotonic_set && | |
487d3720 | 429 | sd_id128_equal(f->current_boot_id, l->boot_id) && |
de190aef | 430 | l->realtime_set && |
487d3720 | 431 | f->current_realtime == l->realtime && |
de190aef | 432 | l->xor_hash_set && |
b6849042 | 433 | f->current_xor_hash == l->xor_hash && |
b17f651a | 434 | l->seqnum_set && |
435 | sd_id128_equal(f->header->seqnum_id, l->seqnum_id) && | |
436 | f->current_seqnum == l->seqnum && | |
b6849042 | 437 | f != current_file) |
de190aef LP |
438 | return 0; |
439 | ||
440 | if (l->seqnum_set && | |
487d3720 | 441 | sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) { |
de190aef | 442 | |
90c88092 YW |
443 | r = CMP(f->current_seqnum, l->seqnum); |
444 | if (r != 0) | |
445 | return r; | |
de190aef LP |
446 | } |
447 | ||
448 | if (l->monotonic_set && | |
487d3720 | 449 | sd_id128_equal(f->current_boot_id, l->boot_id)) { |
de190aef | 450 | |
90c88092 YW |
451 | r = CMP(f->current_monotonic, l->monotonic); |
452 | if (r != 0) | |
453 | return r; | |
de190aef LP |
454 | } |
455 | ||
456 | if (l->realtime_set) { | |
457 | ||
90c88092 YW |
458 | r = CMP(f->current_realtime, l->realtime); |
459 | if (r != 0) | |
460 | return r; | |
de190aef LP |
461 | } |
462 | ||
463 | if (l->xor_hash_set) { | |
de190aef | 464 | |
90c88092 YW |
465 | r = CMP(f->current_xor_hash, l->xor_hash); |
466 | if (r != 0) | |
467 | return r; | |
de190aef LP |
468 | } |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
cbdca852 LP |
473 | static int next_for_match( |
474 | sd_journal *j, | |
475 | Match *m, | |
476 | JournalFile *f, | |
477 | uint64_t after_offset, | |
478 | direction_t direction, | |
479 | Object **ret, | |
480 | uint64_t *offset) { | |
481 | ||
de7b95cd | 482 | int r; |
cbdca852 | 483 | uint64_t np = 0; |
de7b95cd LP |
484 | |
485 | assert(j); | |
cbdca852 LP |
486 | assert(m); |
487 | assert(f); | |
de7b95cd | 488 | |
cbdca852 | 489 | if (m->type == MATCH_DISCRETE) { |
ec50313d DDM |
490 | Object *d; |
491 | uint64_t hash; | |
de190aef | 492 | |
4ce534f4 LP |
493 | /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise |
494 | * we can use what we pre-calculated. */ | |
495 | if (JOURNAL_HEADER_KEYED_HASH(f->header)) | |
496 | hash = journal_file_hash_data(f, m->data, m->size); | |
497 | else | |
498 | hash = m->hash; | |
499 | ||
ec50313d | 500 | r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, NULL); |
de190aef LP |
501 | if (r <= 0) |
502 | return r; | |
503 | ||
ec50313d | 504 | return journal_file_move_to_entry_by_offset_for_data(f, d, after_offset, direction, ret, offset); |
de190aef | 505 | |
cbdca852 | 506 | } else if (m->type == MATCH_OR_TERM) { |
de7b95cd | 507 | |
cbdca852 | 508 | /* Find the earliest match beyond after_offset */ |
de190aef | 509 | |
cbdca852 LP |
510 | LIST_FOREACH(matches, i, m->matches) { |
511 | uint64_t cp; | |
de190aef | 512 | |
cbdca852 | 513 | r = next_for_match(j, i, f, after_offset, direction, NULL, &cp); |
b4e5f920 LP |
514 | if (r < 0) |
515 | return r; | |
cbdca852 | 516 | else if (r > 0) { |
bc302926 | 517 | if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np)) |
cbdca852 LP |
518 | np = cp; |
519 | } | |
520 | } | |
b4e5f920 | 521 | |
bc302926 ZJS |
522 | if (np == 0) |
523 | return 0; | |
524 | ||
cbdca852 | 525 | } else if (m->type == MATCH_AND_TERM) { |
03677889 | 526 | Match *last_moved; |
de190aef | 527 | |
cbdca852 | 528 | /* Always jump to the next matching entry and repeat |
2bc8ca0c | 529 | * this until we find an offset that matches for all |
cbdca852 | 530 | * matches. */ |
de190aef | 531 | |
cbdca852 LP |
532 | if (!m->matches) |
533 | return 0; | |
de7b95cd | 534 | |
2bc8ca0c ZJS |
535 | r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np); |
536 | if (r <= 0) | |
537 | return r; | |
de190aef | 538 | |
2bc8ca0c ZJS |
539 | assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset); |
540 | last_moved = m->matches; | |
de190aef | 541 | |
2bc8ca0c ZJS |
542 | LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) { |
543 | uint64_t cp; | |
de190aef | 544 | |
2bc8ca0c ZJS |
545 | r = next_for_match(j, i, f, np, direction, NULL, &cp); |
546 | if (r <= 0) | |
547 | return r; | |
de190aef | 548 | |
2bc8ca0c ZJS |
549 | assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np); |
550 | if (direction == DIRECTION_DOWN ? cp > np : cp < np) { | |
551 | np = cp; | |
552 | last_moved = i; | |
de190aef | 553 | } |
2bc8ca0c | 554 | } |
cbdca852 | 555 | } |
de190aef | 556 | |
bc302926 | 557 | assert(np > 0); |
de190aef | 558 | |
4d8b09e4 DDM |
559 | if (ret) { |
560 | r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret); | |
561 | if (r < 0) | |
562 | return r; | |
563 | } | |
de7b95cd | 564 | |
de190aef | 565 | if (offset) |
cbdca852 | 566 | *offset = np; |
de190aef LP |
567 | |
568 | return 1; | |
569 | } | |
570 | ||
cbdca852 LP |
571 | static int find_location_for_match( |
572 | sd_journal *j, | |
573 | Match *m, | |
574 | JournalFile *f, | |
575 | direction_t direction, | |
576 | Object **ret, | |
577 | uint64_t *offset) { | |
578 | ||
de190aef | 579 | int r; |
de190aef LP |
580 | |
581 | assert(j); | |
cbdca852 | 582 | assert(m); |
de190aef | 583 | assert(f); |
de190aef | 584 | |
cbdca852 | 585 | if (m->type == MATCH_DISCRETE) { |
ec50313d | 586 | Object *d; |
4ce534f4 LP |
587 | uint64_t dp, hash; |
588 | ||
589 | if (JOURNAL_HEADER_KEYED_HASH(f->header)) | |
590 | hash = journal_file_hash_data(f, m->data, m->size); | |
591 | else | |
592 | hash = m->hash; | |
de190aef | 593 | |
ec50313d | 594 | r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, &dp); |
de7b95cd LP |
595 | if (r <= 0) |
596 | return r; | |
597 | ||
cbdca852 | 598 | /* FIXME: missing: find by monotonic */ |
de7b95cd | 599 | |
cbdca852 | 600 | if (j->current_location.type == LOCATION_HEAD) |
1db6dbb1 | 601 | return journal_file_next_entry_for_data(f, d, DIRECTION_DOWN, ret, offset); |
cbdca852 | 602 | if (j->current_location.type == LOCATION_TAIL) |
1db6dbb1 | 603 | return journal_file_next_entry_for_data(f, d, DIRECTION_UP, ret, offset); |
cbdca852 | 604 | if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) |
ec50313d | 605 | return journal_file_move_to_entry_by_seqnum_for_data(f, d, j->current_location.seqnum, direction, ret, offset); |
cbdca852 | 606 | if (j->current_location.monotonic_set) { |
ec50313d | 607 | r = journal_file_move_to_entry_by_monotonic_for_data(f, d, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset); |
cbdca852 LP |
608 | if (r != -ENOENT) |
609 | return r; | |
ec50313d DDM |
610 | |
611 | /* The data object might have been invalidated. */ | |
612 | r = journal_file_move_to_object(f, OBJECT_DATA, dp, &d); | |
613 | if (r < 0) | |
614 | return r; | |
cbdca852 LP |
615 | } |
616 | if (j->current_location.realtime_set) | |
ec50313d | 617 | return journal_file_move_to_entry_by_realtime_for_data(f, d, j->current_location.realtime, direction, ret, offset); |
de190aef | 618 | |
ec50313d | 619 | return journal_file_next_entry_for_data(f, d, direction, ret, offset); |
de7b95cd | 620 | |
cbdca852 LP |
621 | } else if (m->type == MATCH_OR_TERM) { |
622 | uint64_t np = 0; | |
de7b95cd | 623 | |
cbdca852 | 624 | /* Find the earliest match */ |
de7b95cd | 625 | |
cbdca852 LP |
626 | LIST_FOREACH(matches, i, m->matches) { |
627 | uint64_t cp; | |
628 | ||
629 | r = find_location_for_match(j, i, f, direction, NULL, &cp); | |
630 | if (r < 0) | |
631 | return r; | |
632 | else if (r > 0) { | |
633 | if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp)) | |
634 | np = cp; | |
de190aef | 635 | } |
cbdca852 | 636 | } |
de190aef | 637 | |
cbdca852 LP |
638 | if (np == 0) |
639 | return 0; | |
de7b95cd | 640 | |
4d8b09e4 DDM |
641 | if (ret) { |
642 | r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret); | |
643 | if (r < 0) | |
644 | return r; | |
645 | } | |
de7b95cd | 646 | |
cbdca852 LP |
647 | if (offset) |
648 | *offset = np; | |
de190aef | 649 | |
cbdca852 | 650 | return 1; |
e892bd17 | 651 | |
cbdca852 | 652 | } else { |
cbdca852 LP |
653 | uint64_t np = 0; |
654 | ||
655 | assert(m->type == MATCH_AND_TERM); | |
656 | ||
657 | /* First jump to the last match, and then find the | |
658 | * next one where all matches match */ | |
659 | ||
660 | if (!m->matches) | |
661 | return 0; | |
662 | ||
663 | LIST_FOREACH(matches, i, m->matches) { | |
664 | uint64_t cp; | |
665 | ||
666 | r = find_location_for_match(j, i, f, direction, NULL, &cp); | |
667 | if (r <= 0) | |
4b067dc9 LP |
668 | return r; |
669 | ||
bc302926 | 670 | if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np)) |
cbdca852 | 671 | np = cp; |
de7b95cd LP |
672 | } |
673 | ||
cbdca852 LP |
674 | return next_for_match(j, m, f, np, direction, ret, offset); |
675 | } | |
676 | } | |
de190aef | 677 | |
cbdca852 LP |
678 | static int find_location_with_matches( |
679 | sd_journal *j, | |
680 | JournalFile *f, | |
681 | direction_t direction, | |
682 | Object **ret, | |
683 | uint64_t *offset) { | |
684 | ||
685 | int r; | |
686 | ||
687 | assert(j); | |
688 | assert(f); | |
689 | assert(ret); | |
690 | assert(offset); | |
691 | ||
692 | if (!j->level0) { | |
693 | /* No matches is simple */ | |
694 | ||
695 | if (j->current_location.type == LOCATION_HEAD) | |
1db6dbb1 | 696 | return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset); |
cbdca852 | 697 | if (j->current_location.type == LOCATION_TAIL) |
1db6dbb1 | 698 | return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset); |
cbdca852 LP |
699 | if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id)) |
700 | return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset); | |
701 | if (j->current_location.monotonic_set) { | |
702 | r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset); | |
703 | if (r != -ENOENT) | |
704 | return r; | |
de7b95cd | 705 | } |
cbdca852 LP |
706 | if (j->current_location.realtime_set) |
707 | return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset); | |
de7b95cd | 708 | |
f534928a | 709 | return journal_file_next_entry(f, 0, direction, ret, offset); |
cbdca852 LP |
710 | } else |
711 | return find_location_for_match(j, j->level0, f, direction, ret, offset); | |
712 | } | |
de7b95cd | 713 | |
cbdca852 LP |
714 | static int next_with_matches( |
715 | sd_journal *j, | |
716 | JournalFile *f, | |
717 | direction_t direction, | |
718 | Object **ret, | |
719 | uint64_t *offset) { | |
720 | ||
cbdca852 LP |
721 | assert(j); |
722 | assert(f); | |
723 | assert(ret); | |
724 | assert(offset); | |
725 | ||
cbdca852 LP |
726 | /* No matches is easy. We simple advance the file |
727 | * pointer by one. */ | |
728 | if (!j->level0) | |
b29ddfcb | 729 | return journal_file_next_entry(f, f->current_offset, direction, ret, offset); |
cbdca852 LP |
730 | |
731 | /* If we have a match then we look for the next matching entry | |
49f43d5f | 732 | * with an offset at least one step larger */ |
b29ddfcb MS |
733 | return next_for_match(j, j->level0, f, |
734 | direction == DIRECTION_DOWN ? f->current_offset + 1 | |
735 | : f->current_offset - 1, | |
736 | direction, ret, offset); | |
de7b95cd LP |
737 | } |
738 | ||
58439db4 | 739 | static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) { |
de190aef | 740 | Object *c; |
6e693b42 | 741 | uint64_t cp, n_entries; |
cbdca852 | 742 | int r; |
de190aef LP |
743 | |
744 | assert(j); | |
745 | assert(f); | |
746 | ||
34af7494 LP |
747 | (void) journal_file_read_tail_timestamp(j, f); |
748 | ||
950c07d4 MS |
749 | n_entries = le64toh(f->header->n_entries); |
750 | ||
751 | /* If we hit EOF before, we don't need to look into this file again | |
752 | * unless direction changed or new entries appeared. */ | |
753 | if (f->last_direction == direction && f->location_type == LOCATION_TAIL && | |
754 | n_entries == f->last_n_entries) | |
755 | return 0; | |
668c965a | 756 | |
950c07d4 MS |
757 | f->last_n_entries = n_entries; |
758 | ||
759 | if (f->last_direction == direction && f->current_offset > 0) { | |
7943f422 MS |
760 | /* LOCATION_SEEK here means we did the work in a previous |
761 | * iteration and the current location already points to a | |
762 | * candidate entry. */ | |
487d3720 MS |
763 | if (f->location_type != LOCATION_SEEK) { |
764 | r = next_with_matches(j, f, direction, &c, &cp); | |
765 | if (r <= 0) | |
766 | return r; | |
466ccd92 | 767 | |
950c07d4 | 768 | journal_file_save_location(f, c, cp); |
487d3720 | 769 | } |
de190aef | 770 | } else { |
950c07d4 MS |
771 | f->last_direction = direction; |
772 | ||
cbdca852 | 773 | r = find_location_with_matches(j, f, direction, &c, &cp); |
de190aef LP |
774 | if (r <= 0) |
775 | return r; | |
487d3720 | 776 | |
950c07d4 | 777 | journal_file_save_location(f, c, cp); |
de190aef LP |
778 | } |
779 | ||
bc302926 | 780 | /* OK, we found the spot, now let's advance until an entry |
cbdca852 LP |
781 | * that is actually different from what we were previously |
782 | * looking at. This is necessary to handle entries which exist | |
783 | * in two (or more) journal files, and which shall all be | |
784 | * suppressed but one. */ | |
785 | ||
de190aef LP |
786 | for (;;) { |
787 | bool found; | |
788 | ||
789 | if (j->current_location.type == LOCATION_DISCRETE) { | |
790 | int k; | |
791 | ||
b6849042 | 792 | k = compare_with_location(f, &j->current_location, j->current_file); |
1cdf7175 CH |
793 | |
794 | found = direction == DIRECTION_DOWN ? k > 0 : k < 0; | |
de190aef LP |
795 | } else |
796 | found = true; | |
797 | ||
487d3720 | 798 | if (found) |
de190aef | 799 | return 1; |
de190aef LP |
800 | |
801 | r = next_with_matches(j, f, direction, &c, &cp); | |
802 | if (r <= 0) | |
803 | return r; | |
487d3720 | 804 | |
950c07d4 | 805 | journal_file_save_location(f, c, cp); |
de190aef LP |
806 | } |
807 | } | |
808 | ||
01e4f03f LP |
809 | static int compare_locations(JournalFile *af, JournalFile *bf) { |
810 | int r; | |
811 | ||
812 | assert(af); | |
813 | assert(af->header); | |
814 | assert(bf); | |
815 | assert(bf->header); | |
816 | assert(af->location_type == LOCATION_SEEK); | |
817 | assert(bf->location_type == LOCATION_SEEK); | |
818 | ||
819 | /* If contents, timestamps and seqnum match, these entries are identical. */ | |
820 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) && | |
821 | af->current_monotonic == bf->current_monotonic && | |
822 | af->current_realtime == bf->current_realtime && | |
823 | af->current_xor_hash == bf->current_xor_hash && | |
824 | sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id) && | |
825 | af->current_seqnum == bf->current_seqnum) | |
826 | return 0; | |
827 | ||
828 | if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) { | |
829 | /* If this is from the same seqnum source, compare seqnums */ | |
830 | r = CMP(af->current_seqnum, bf->current_seqnum); | |
831 | if (r != 0) | |
832 | return r; | |
833 | ||
834 | /* Wow! This is weird, different data but the same seqnums? Something is borked, but let's | |
835 | * make the best of it and compare by time. */ | |
836 | } | |
837 | ||
838 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id)) { | |
839 | /* If the boot id matches, compare monotonic time */ | |
840 | r = CMP(af->current_monotonic, bf->current_monotonic); | |
841 | if (r != 0) | |
842 | return r; | |
843 | } | |
844 | ||
845 | /* Otherwise, compare UTC time */ | |
846 | r = CMP(af->current_realtime, bf->current_realtime); | |
847 | if (r != 0) | |
848 | return r; | |
849 | ||
850 | /* Finally, compare by contents */ | |
851 | return CMP(af->current_xor_hash, bf->current_xor_hash); | |
852 | } | |
853 | ||
e892bd17 | 854 | static int real_journal_next(sd_journal *j, direction_t direction) { |
5d4ba7f2 | 855 | JournalFile *new_file = NULL; |
abcdc02c | 856 | unsigned n_files; |
5d4ba7f2 | 857 | const void **files; |
a002d44b | 858 | Object *o; |
87d2c1ff LP |
859 | int r; |
860 | ||
1ae464e0 TA |
861 | assert_return(j, -EINVAL); |
862 | assert_return(!journal_pid_changed(j), -ECHILD); | |
87d2c1ff | 863 | |
5d4ba7f2 VC |
864 | r = iterated_cache_get(j->files_cache, NULL, &files, &n_files); |
865 | if (r < 0) | |
866 | return r; | |
867 | ||
abcdc02c | 868 | for (unsigned i = 0; i < n_files; i++) { |
5d4ba7f2 | 869 | JournalFile *f = (JournalFile *)files[i]; |
de190aef | 870 | bool found; |
87d2c1ff | 871 | |
58439db4 | 872 | r = next_beyond_location(j, f, direction); |
e590af26 | 873 | if (r < 0) { |
da927ba9 | 874 | log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path); |
a9a245c1 | 875 | remove_file_real(j, f); |
e590af26 | 876 | continue; |
6573ef05 MS |
877 | } else if (r == 0) { |
878 | f->location_type = LOCATION_TAIL; | |
cec736d2 | 879 | continue; |
6573ef05 | 880 | } |
87d2c1ff | 881 | |
468b21de | 882 | if (!new_file) |
de190aef LP |
883 | found = true; |
884 | else { | |
885 | int k; | |
886 | ||
01e4f03f | 887 | k = compare_locations(f, new_file); |
de190aef | 888 | |
bc302926 | 889 | found = direction == DIRECTION_DOWN ? k < 0 : k > 0; |
de190aef LP |
890 | } |
891 | ||
e499c999 | 892 | if (found) |
468b21de | 893 | new_file = f; |
87d2c1ff LP |
894 | } |
895 | ||
468b21de | 896 | if (!new_file) |
de190aef | 897 | return 0; |
ae2cc8ef | 898 | |
e499c999 | 899 | r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o); |
468b21de LP |
900 | if (r < 0) |
901 | return r; | |
902 | ||
1eb6332d | 903 | set_location(j, new_file, o); |
ae2cc8ef | 904 | |
de190aef LP |
905 | return 1; |
906 | } | |
ae2cc8ef | 907 | |
a5344d2c | 908 | _public_ int sd_journal_next(sd_journal *j) { |
de190aef LP |
909 | return real_journal_next(j, DIRECTION_DOWN); |
910 | } | |
ae2cc8ef | 911 | |
a5344d2c | 912 | _public_ int sd_journal_previous(sd_journal *j) { |
de190aef LP |
913 | return real_journal_next(j, DIRECTION_UP); |
914 | } | |
ae2cc8ef | 915 | |
6f003b43 | 916 | static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) { |
de190aef | 917 | int c = 0, r; |
ae2cc8ef | 918 | |
1ae464e0 TA |
919 | assert_return(j, -EINVAL); |
920 | assert_return(!journal_pid_changed(j), -ECHILD); | |
41d544a1 | 921 | assert_return(skip <= INT_MAX, -ERANGE); |
de190aef | 922 | |
6f003b43 LP |
923 | if (skip == 0) { |
924 | /* If this is not a discrete skip, then at least | |
925 | * resolve the current location */ | |
04884354 LK |
926 | if (j->current_location.type != LOCATION_DISCRETE) { |
927 | r = real_journal_next(j, direction); | |
928 | if (r < 0) | |
929 | return r; | |
930 | } | |
6f003b43 LP |
931 | |
932 | return 0; | |
933 | } | |
934 | ||
935 | do { | |
936 | r = real_journal_next(j, direction); | |
de190aef LP |
937 | if (r < 0) |
938 | return r; | |
939 | ||
940 | if (r == 0) | |
941 | return c; | |
942 | ||
943 | skip--; | |
944 | c++; | |
6f003b43 | 945 | } while (skip > 0); |
87d2c1ff | 946 | |
de190aef | 947 | return c; |
87d2c1ff LP |
948 | } |
949 | ||
6f003b43 LP |
950 | _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) { |
951 | return real_journal_next_skip(j, DIRECTION_DOWN, skip); | |
952 | } | |
de190aef | 953 | |
6f003b43 LP |
954 | _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) { |
955 | return real_journal_next_skip(j, DIRECTION_UP, skip); | |
87d2c1ff LP |
956 | } |
957 | ||
a5344d2c | 958 | _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) { |
cec736d2 | 959 | Object *o; |
87d2c1ff LP |
960 | int r; |
961 | ||
1ae464e0 TA |
962 | assert_return(j, -EINVAL); |
963 | assert_return(!journal_pid_changed(j), -ECHILD); | |
964 | assert_return(cursor, -EINVAL); | |
87d2c1ff | 965 | |
3fbf9cbb LP |
966 | if (!j->current_file || j->current_file->current_offset <= 0) |
967 | return -EADDRNOTAVAIL; | |
87d2c1ff | 968 | |
de190aef | 969 | r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o); |
87d2c1ff LP |
970 | if (r < 0) |
971 | return r; | |
972 | ||
3fbf9cbb | 973 | if (asprintf(cursor, |
507f22bd | 974 | "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64, |
85b55869 LP |
975 | SD_ID128_TO_STRING(j->current_file->header->seqnum_id), le64toh(o->entry.seqnum), |
976 | SD_ID128_TO_STRING(o->entry.boot_id), le64toh(o->entry.monotonic), | |
507f22bd ZJS |
977 | le64toh(o->entry.realtime), |
978 | le64toh(o->entry.xor_hash)) < 0) | |
3fbf9cbb | 979 | return -ENOMEM; |
87d2c1ff | 980 | |
6f47ad30 | 981 | return 0; |
87d2c1ff LP |
982 | } |
983 | ||
a5344d2c | 984 | _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) { |
de190aef | 985 | unsigned long long seqnum, monotonic, realtime, xor_hash; |
da277e90 ZJS |
986 | bool seqnum_id_set = false, |
987 | seqnum_set = false, | |
988 | boot_id_set = false, | |
989 | monotonic_set = false, | |
990 | realtime_set = false, | |
991 | xor_hash_set = false; | |
de190aef | 992 | sd_id128_t seqnum_id, boot_id; |
da277e90 | 993 | int r; |
de190aef | 994 | |
1ae464e0 TA |
995 | assert_return(j, -EINVAL); |
996 | assert_return(!journal_pid_changed(j), -ECHILD); | |
997 | assert_return(!isempty(cursor), -EINVAL); | |
de190aef | 998 | |
da277e90 ZJS |
999 | for (const char *p = cursor;;) { |
1000 | _cleanup_free_ char *word = NULL; | |
de190aef | 1001 | |
da277e90 ZJS |
1002 | r = extract_first_word(&p, &word, ";", EXTRACT_DONT_COALESCE_SEPARATORS); |
1003 | if (r < 0) | |
1004 | return r; | |
1005 | if (r == 0) | |
1006 | break; | |
de190aef | 1007 | |
da277e90 ZJS |
1008 | if (word[0] == '\0' || word[1] != '=') |
1009 | return -EINVAL; | |
de190aef | 1010 | |
a2a5291b | 1011 | switch (word[0]) { |
de190aef LP |
1012 | case 's': |
1013 | seqnum_id_set = true; | |
da277e90 ZJS |
1014 | r = sd_id128_from_string(word + 2, &seqnum_id); |
1015 | if (r < 0) | |
1016 | return r; | |
de190aef LP |
1017 | break; |
1018 | ||
1019 | case 'i': | |
1020 | seqnum_set = true; | |
da277e90 ZJS |
1021 | if (sscanf(word + 2, "%llx", &seqnum) != 1) |
1022 | return -EINVAL; | |
de190aef LP |
1023 | break; |
1024 | ||
1025 | case 'b': | |
1026 | boot_id_set = true; | |
da277e90 | 1027 | r = sd_id128_from_string(word + 2, &boot_id); |
c9f5ac09 SS |
1028 | if (r < 0) |
1029 | return r; | |
de190aef LP |
1030 | break; |
1031 | ||
1032 | case 'm': | |
1033 | monotonic_set = true; | |
da277e90 ZJS |
1034 | if (sscanf(word + 2, "%llx", &monotonic) != 1) |
1035 | return -EINVAL; | |
de190aef LP |
1036 | break; |
1037 | ||
1038 | case 't': | |
1039 | realtime_set = true; | |
da277e90 ZJS |
1040 | if (sscanf(word + 2, "%llx", &realtime) != 1) |
1041 | return -EINVAL; | |
de190aef LP |
1042 | break; |
1043 | ||
1044 | case 'x': | |
1045 | xor_hash_set = true; | |
da277e90 ZJS |
1046 | if (sscanf(word + 2, "%llx", &xor_hash) != 1) |
1047 | return -EINVAL; | |
de190aef LP |
1048 | break; |
1049 | } | |
de190aef LP |
1050 | } |
1051 | ||
1052 | if ((!seqnum_set || !seqnum_id_set) && | |
1053 | (!monotonic_set || !boot_id_set) && | |
1054 | !realtime_set) | |
1055 | return -EINVAL; | |
1056 | ||
bba6e4ae LP |
1057 | detach_location(j); |
1058 | j->current_location = (Location) { | |
1059 | .type = LOCATION_SEEK, | |
1060 | }; | |
de190aef LP |
1061 | |
1062 | if (realtime_set) { | |
1063 | j->current_location.realtime = (uint64_t) realtime; | |
1064 | j->current_location.realtime_set = true; | |
1065 | } | |
1066 | ||
1067 | if (seqnum_set && seqnum_id_set) { | |
1068 | j->current_location.seqnum = (uint64_t) seqnum; | |
1069 | j->current_location.seqnum_id = seqnum_id; | |
1070 | j->current_location.seqnum_set = true; | |
1071 | } | |
1072 | ||
1073 | if (monotonic_set && boot_id_set) { | |
1074 | j->current_location.monotonic = (uint64_t) monotonic; | |
1075 | j->current_location.boot_id = boot_id; | |
1076 | j->current_location.monotonic_set = true; | |
1077 | } | |
1078 | ||
1079 | if (xor_hash_set) { | |
1080 | j->current_location.xor_hash = (uint64_t) xor_hash; | |
1081 | j->current_location.xor_hash_set = true; | |
1082 | } | |
1083 | ||
1084 | return 0; | |
1085 | } | |
1086 | ||
c6511e85 LP |
1087 | _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) { |
1088 | int r; | |
c6511e85 LP |
1089 | Object *o; |
1090 | ||
1ae464e0 TA |
1091 | assert_return(j, -EINVAL); |
1092 | assert_return(!journal_pid_changed(j), -ECHILD); | |
1093 | assert_return(!isempty(cursor), -EINVAL); | |
c6511e85 LP |
1094 | |
1095 | if (!j->current_file || j->current_file->current_offset <= 0) | |
1096 | return -EADDRNOTAVAIL; | |
1097 | ||
1098 | r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o); | |
1099 | if (r < 0) | |
1100 | return r; | |
1101 | ||
9ed794a3 | 1102 | for (;;) { |
c6511e85 | 1103 | _cleanup_free_ char *item = NULL; |
c6511e85 | 1104 | unsigned long long ll; |
7294bb5b | 1105 | sd_id128_t id; |
c6511e85 LP |
1106 | int k = 0; |
1107 | ||
7294bb5b SS |
1108 | r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS); |
1109 | if (r < 0) | |
1110 | return r; | |
c6511e85 | 1111 | |
7294bb5b SS |
1112 | if (r == 0) |
1113 | break; | |
c6511e85 | 1114 | |
7294bb5b SS |
1115 | if (strlen(item) < 2 || item[1] != '=') |
1116 | return -EINVAL; | |
1117 | ||
1118 | switch (item[0]) { | |
c6511e85 LP |
1119 | |
1120 | case 's': | |
1121 | k = sd_id128_from_string(item+2, &id); | |
1122 | if (k < 0) | |
1123 | return k; | |
1124 | if (!sd_id128_equal(id, j->current_file->header->seqnum_id)) | |
1125 | return 0; | |
1126 | break; | |
1127 | ||
1128 | case 'i': | |
1129 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1130 | return -EINVAL; | |
1131 | if (ll != le64toh(o->entry.seqnum)) | |
1132 | return 0; | |
1133 | break; | |
1134 | ||
1135 | case 'b': | |
1136 | k = sd_id128_from_string(item+2, &id); | |
1137 | if (k < 0) | |
1138 | return k; | |
1139 | if (!sd_id128_equal(id, o->entry.boot_id)) | |
1140 | return 0; | |
1141 | break; | |
1142 | ||
1143 | case 'm': | |
1144 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1145 | return -EINVAL; | |
1146 | if (ll != le64toh(o->entry.monotonic)) | |
1147 | return 0; | |
1148 | break; | |
1149 | ||
1150 | case 't': | |
1151 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1152 | return -EINVAL; | |
1153 | if (ll != le64toh(o->entry.realtime)) | |
1154 | return 0; | |
1155 | break; | |
1156 | ||
1157 | case 'x': | |
1158 | if (sscanf(item+2, "%llx", &ll) != 1) | |
1159 | return -EINVAL; | |
1160 | if (ll != le64toh(o->entry.xor_hash)) | |
1161 | return 0; | |
1162 | break; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | return 1; | |
1167 | } | |
1168 | ||
a5344d2c | 1169 | _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) { |
1ae464e0 TA |
1170 | assert_return(j, -EINVAL); |
1171 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef | 1172 | |
bba6e4ae LP |
1173 | detach_location(j); |
1174 | ||
1175 | j->current_location = (Location) { | |
1176 | .type = LOCATION_SEEK, | |
1177 | .boot_id = boot_id, | |
1178 | .monotonic = usec, | |
1179 | .monotonic_set = true, | |
1180 | }; | |
de190aef LP |
1181 | |
1182 | return 0; | |
1183 | } | |
1184 | ||
a5344d2c | 1185 | _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { |
1ae464e0 TA |
1186 | assert_return(j, -EINVAL); |
1187 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef | 1188 | |
bba6e4ae LP |
1189 | detach_location(j); |
1190 | ||
1191 | j->current_location = (Location) { | |
1192 | .type = LOCATION_SEEK, | |
1193 | .realtime = usec, | |
1194 | .realtime_set = true, | |
1195 | }; | |
de190aef LP |
1196 | |
1197 | return 0; | |
1198 | } | |
1199 | ||
a5344d2c | 1200 | _public_ int sd_journal_seek_head(sd_journal *j) { |
1ae464e0 TA |
1201 | assert_return(j, -EINVAL); |
1202 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef | 1203 | |
bba6e4ae LP |
1204 | detach_location(j); |
1205 | ||
1206 | j->current_location = (Location) { | |
1207 | .type = LOCATION_HEAD, | |
1208 | }; | |
de190aef LP |
1209 | |
1210 | return 0; | |
1211 | } | |
1212 | ||
a5344d2c | 1213 | _public_ int sd_journal_seek_tail(sd_journal *j) { |
1ae464e0 TA |
1214 | assert_return(j, -EINVAL); |
1215 | assert_return(!journal_pid_changed(j), -ECHILD); | |
de190aef | 1216 | |
bba6e4ae LP |
1217 | detach_location(j); |
1218 | ||
1219 | j->current_location = (Location) { | |
1220 | .type = LOCATION_TAIL, | |
1221 | }; | |
de190aef LP |
1222 | |
1223 | return 0; | |
87d2c1ff LP |
1224 | } |
1225 | ||
85210bff | 1226 | static void check_network(sd_journal *j, int fd) { |
85210bff LP |
1227 | assert(j); |
1228 | ||
1229 | if (j->on_network) | |
1230 | return; | |
1231 | ||
77f9fa3b | 1232 | j->on_network = fd_is_network_fs(fd); |
85210bff LP |
1233 | } |
1234 | ||
a688baa8 ZJS |
1235 | static bool file_has_type_prefix(const char *prefix, const char *filename) { |
1236 | const char *full, *tilded, *atted; | |
1237 | ||
63c372cb LP |
1238 | full = strjoina(prefix, ".journal"); |
1239 | tilded = strjoina(full, "~"); | |
1240 | atted = strjoina(prefix, "@"); | |
a688baa8 | 1241 | |
0cbd293e | 1242 | return STR_IN_SET(filename, full, tilded) || |
a688baa8 ZJS |
1243 | startswith(filename, atted); |
1244 | } | |
1245 | ||
1246 | static bool file_type_wanted(int flags, const char *filename) { | |
d617408e LP |
1247 | assert(filename); |
1248 | ||
a688baa8 ZJS |
1249 | if (!endswith(filename, ".journal") && !endswith(filename, ".journal~")) |
1250 | return false; | |
1251 | ||
1252 | /* no flags set → every type is OK */ | |
1253 | if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER))) | |
1254 | return true; | |
1255 | ||
1256 | if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename)) | |
1257 | return true; | |
1258 | ||
1259 | if (flags & SD_JOURNAL_CURRENT_USER) { | |
1260 | char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1]; | |
1261 | ||
5ffa8c81 | 1262 | xsprintf(prefix, "user-"UID_FMT, getuid()); |
a688baa8 ZJS |
1263 | |
1264 | if (file_has_type_prefix(prefix, filename)) | |
1265 | return true; | |
1266 | } | |
1267 | ||
1268 | return false; | |
1269 | } | |
1270 | ||
5d1ce257 LP |
1271 | static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) { |
1272 | assert(j); | |
1273 | assert(path); | |
1274 | assert(prefix); | |
1275 | ||
1276 | if (j->toplevel_fd >= 0) | |
1277 | return false; | |
1278 | ||
1279 | return path_startswith(path, prefix); | |
1280 | } | |
1281 | ||
9c66f528 LP |
1282 | static void track_file_disposition(sd_journal *j, JournalFile *f) { |
1283 | assert(j); | |
1284 | assert(f); | |
1285 | ||
1286 | if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run")) | |
1287 | j->has_runtime_files = true; | |
1288 | else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var")) | |
1289 | j->has_persistent_files = true; | |
1290 | } | |
1291 | ||
5d1ce257 LP |
1292 | static const char *skip_slash(const char *p) { |
1293 | ||
1294 | if (!p) | |
1295 | return NULL; | |
1296 | ||
1297 | while (*p == '/') | |
1298 | p++; | |
1299 | ||
1300 | return p; | |
1301 | } | |
1302 | ||
9c66f528 LP |
1303 | static int add_any_file( |
1304 | sd_journal *j, | |
1305 | int fd, | |
1306 | const char *path) { | |
1307 | ||
254d1313 | 1308 | _cleanup_close_ int our_fd = -EBADF; |
9c66f528 LP |
1309 | JournalFile *f; |
1310 | struct stat st; | |
70f50a47 | 1311 | int r; |
3fbf9cbb LP |
1312 | |
1313 | assert(j); | |
5d1ce257 | 1314 | assert(fd >= 0 || path); |
3fbf9cbb | 1315 | |
9c66f528 | 1316 | if (fd < 0) { |
85b0ff8a | 1317 | assert(path); /* For gcc. */ |
9c66f528 LP |
1318 | if (j->toplevel_fd >= 0) |
1319 | /* If there's a top-level fd defined make the path relative, explicitly, since otherwise | |
1320 | * openat() ignores the first argument. */ | |
1321 | ||
70f50a47 | 1322 | fd = our_fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
9c66f528 | 1323 | else |
70f50a47 | 1324 | fd = our_fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
9c66f528 LP |
1325 | if (fd < 0) { |
1326 | r = log_debug_errno(errno, "Failed to open journal file %s: %m", path); | |
70f50a47 | 1327 | goto error; |
9c66f528 LP |
1328 | } |
1329 | ||
9c66f528 LP |
1330 | r = fd_nonblock(fd, false); |
1331 | if (r < 0) { | |
1332 | r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path); | |
70f50a47 | 1333 | goto error; |
858749f7 LP |
1334 | } |
1335 | } | |
50f20cfd | 1336 | |
9c66f528 | 1337 | if (fstat(fd, &st) < 0) { |
85b0ff8a | 1338 | r = log_debug_errno(errno, "Failed to fstat %s: %m", path ?: "fd"); |
70f50a47 | 1339 | goto error; |
9c66f528 | 1340 | } |
3cc44114 LP |
1341 | |
1342 | r = stat_verify_regular(&st); | |
1343 | if (r < 0) { | |
85b0ff8a | 1344 | log_debug_errno(r, "Refusing to open %s: %m", path ?: "fd"); |
70f50a47 | 1345 | goto error; |
9e8abdf0 LP |
1346 | } |
1347 | ||
85b0ff8a ZJS |
1348 | if (path) { |
1349 | f = ordered_hashmap_get(j->files, path); | |
1350 | if (f) { | |
1351 | if (stat_inode_same(&f->last_stat, &st)) { | |
1352 | /* We already track this file, under the same path and with the same | |
1353 | * device/inode numbers, it's hence really the same. Mark this file as seen | |
1354 | * in this generation. This is used to GC old files in process_q_overflow() | |
1355 | * to detect journal files that are still there and discern them from those | |
1356 | * which are gone. */ | |
1357 | ||
1358 | f->last_seen_generation = j->generation; | |
34af7494 | 1359 | (void) journal_file_read_tail_timestamp(j, f); |
70f50a47 | 1360 | return 0; |
85b0ff8a | 1361 | } |
5d1ce257 | 1362 | |
85b0ff8a ZJS |
1363 | /* So we tracked a file under this name, but it has a different inode/device. In that |
1364 | * case, it got replaced (probably due to rotation?), let's drop it hence from our | |
1365 | * list. */ | |
1366 | remove_file_real(j, f); | |
1367 | f = NULL; | |
5d1ce257 | 1368 | } |
9c66f528 LP |
1369 | } |
1370 | ||
1371 | if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) { | |
70f50a47 ZJS |
1372 | r = log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS), |
1373 | "Too many open journal files, not adding %s.", path ?: "fd"); | |
1374 | goto error; | |
5d1ce257 LP |
1375 | } |
1376 | ||
49615dbd | 1377 | r = journal_file_open(fd, path, O_RDONLY, 0, 0, 0, NULL, j->mmap, NULL, &f); |
d617408e | 1378 | if (r < 0) { |
85b0ff8a | 1379 | log_debug_errno(r, "Failed to open journal file %s: %m", path ?: "from fd"); |
70f50a47 | 1380 | goto error; |
d617408e | 1381 | } |
3fbf9cbb | 1382 | |
72f59706 | 1383 | /* journal_file_dump(f); */ |
de190aef | 1384 | |
85b0ff8a | 1385 | /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */ |
c1f906bd | 1386 | r = ordered_hashmap_put(j->files, f->path, f); |
3fbf9cbb | 1387 | if (r < 0) { |
85b0ff8a ZJS |
1388 | f->close_fd = false; /* Make sure journal_file_close() doesn't close the caller's fd |
1389 | * (or our own). The caller or we will do that ourselves. */ | |
69a3a6fd | 1390 | (void) journal_file_close(f); |
70f50a47 | 1391 | goto error; |
3fbf9cbb LP |
1392 | } |
1393 | ||
70f50a47 | 1394 | TAKE_FD(our_fd); /* the fd is now owned by the JournalFile object */ |
5d1ce257 | 1395 | |
9c66f528 | 1396 | f->last_seen_generation = j->generation; |
a50d7d43 | 1397 | |
9c66f528 | 1398 | track_file_disposition(j, f); |
85210bff | 1399 | check_network(j, f->fd); |
34af7494 | 1400 | (void) journal_file_read_tail_timestamp(j, f); |
85210bff | 1401 | |
313cefa1 | 1402 | j->current_invalidate_counter++; |
a963990f | 1403 | |
9c66f528 | 1404 | log_debug("File %s added.", f->path); |
d617408e | 1405 | |
70f50a47 | 1406 | return 0; |
d617408e | 1407 | |
70f50a47 ZJS |
1408 | error: |
1409 | (void) journal_put_error(j, r, path); /* path==NULL is OK. */ | |
d617408e | 1410 | return r; |
50f20cfd LP |
1411 | } |
1412 | ||
fc1813c0 LP |
1413 | static int add_file_by_name( |
1414 | sd_journal *j, | |
1415 | const char *prefix, | |
1416 | const char *filename) { | |
1417 | ||
8e7e4a73 | 1418 | _cleanup_free_ char *path = NULL; |
5302ebe1 ZJS |
1419 | |
1420 | assert(j); | |
1421 | assert(prefix); | |
1422 | assert(filename); | |
1423 | ||
5d1ce257 | 1424 | if (j->no_new_files) |
5302ebe1 ZJS |
1425 | return 0; |
1426 | ||
5d1ce257 LP |
1427 | if (!file_type_wanted(j->flags, filename)) |
1428 | return 0; | |
39fd5b08 | 1429 | |
8e7e4a73 LP |
1430 | path = path_join(prefix, filename); |
1431 | if (!path) | |
1432 | return -ENOMEM; | |
1433 | ||
5d1ce257 | 1434 | return add_any_file(j, -1, path); |
5302ebe1 ZJS |
1435 | } |
1436 | ||
8e7e4a73 | 1437 | static int remove_file_by_name( |
fc1813c0 LP |
1438 | sd_journal *j, |
1439 | const char *prefix, | |
1440 | const char *filename) { | |
1441 | ||
8e7e4a73 | 1442 | _cleanup_free_ char *path = NULL; |
50f20cfd LP |
1443 | JournalFile *f; |
1444 | ||
1445 | assert(j); | |
1446 | assert(prefix); | |
1447 | assert(filename); | |
1448 | ||
8e7e4a73 LP |
1449 | path = path_join(prefix, filename); |
1450 | if (!path) | |
1451 | return -ENOMEM; | |
1452 | ||
c1f906bd | 1453 | f = ordered_hashmap_get(j->files, path); |
50f20cfd | 1454 | if (!f) |
8e7e4a73 | 1455 | return 0; |
50f20cfd | 1456 | |
a9a245c1 | 1457 | remove_file_real(j, f); |
8e7e4a73 | 1458 | return 1; |
a9a245c1 ZJS |
1459 | } |
1460 | ||
1461 | static void remove_file_real(sd_journal *j, JournalFile *f) { | |
1462 | assert(j); | |
1463 | assert(f); | |
1464 | ||
fc1813c0 | 1465 | (void) ordered_hashmap_remove(j->files, f->path); |
44a5fa34 | 1466 | |
5ec76417 | 1467 | log_debug("File %s removed.", f->path); |
44a5fa34 | 1468 | |
3c1668da LP |
1469 | if (j->current_file == f) { |
1470 | j->current_file = NULL; | |
1471 | j->current_field = 0; | |
1472 | } | |
1473 | ||
1474 | if (j->unique_file == f) { | |
360af4cf | 1475 | /* Jump to the next unique_file or NULL if that one was last */ |
c1f906bd | 1476 | j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path); |
3c1668da | 1477 | j->unique_offset = 0; |
360af4cf ZJS |
1478 | if (!j->unique_file) |
1479 | j->unique_file_lost = true; | |
3c1668da LP |
1480 | } |
1481 | ||
eb86030e LP |
1482 | if (j->fields_file == f) { |
1483 | j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path); | |
1484 | j->fields_offset = 0; | |
1485 | if (!j->fields_file) | |
1486 | j->fields_file_lost = true; | |
1487 | } | |
1488 | ||
34af7494 | 1489 | journal_file_unlink_newest_by_bood_id(j, f); |
69a3a6fd | 1490 | (void) journal_file_close(f); |
50f20cfd | 1491 | |
313cefa1 | 1492 | j->current_invalidate_counter++; |
3fbf9cbb LP |
1493 | } |
1494 | ||
d617408e LP |
1495 | static int dirname_is_machine_id(const char *fn) { |
1496 | sd_id128_t id, machine; | |
456aa879 | 1497 | const char *e; |
d617408e LP |
1498 | int r; |
1499 | ||
456aa879 LP |
1500 | /* Returns true if the specified directory name matches the local machine ID */ |
1501 | ||
d617408e LP |
1502 | r = sd_id128_get_machine(&machine); |
1503 | if (r < 0) | |
1504 | return r; | |
1505 | ||
456aa879 LP |
1506 | e = strchr(fn, '.'); |
1507 | if (e) { | |
1508 | const char *k; | |
1509 | ||
1510 | /* Looks like it has a namespace suffix. Verify that. */ | |
1511 | if (!log_namespace_name_valid(e + 1)) | |
1512 | return false; | |
1513 | ||
2f82562b | 1514 | k = strndupa_safe(fn, e - fn); |
456aa879 LP |
1515 | r = sd_id128_from_string(k, &id); |
1516 | } else | |
1517 | r = sd_id128_from_string(fn, &id); | |
d617408e LP |
1518 | if (r < 0) |
1519 | return r; | |
1520 | ||
1521 | return sd_id128_equal(id, machine); | |
1522 | } | |
1523 | ||
456aa879 LP |
1524 | static int dirname_has_namespace(const char *fn, const char *namespace) { |
1525 | const char *e; | |
1526 | ||
1527 | /* Returns true if the specified directory name matches the specified namespace */ | |
1528 | ||
1529 | e = strchr(fn, '.'); | |
1530 | if (e) { | |
1531 | const char *k; | |
1532 | ||
1533 | if (!namespace) | |
1534 | return false; | |
1535 | ||
1536 | if (!streq(e + 1, namespace)) | |
1537 | return false; | |
1538 | ||
2f82562b | 1539 | k = strndupa_safe(fn, e - fn); |
456aa879 LP |
1540 | return id128_is_valid(k); |
1541 | } | |
1542 | ||
1543 | if (namespace) | |
1544 | return false; | |
1545 | ||
1546 | return id128_is_valid(fn); | |
1547 | } | |
1548 | ||
858749f7 LP |
1549 | static bool dirent_is_journal_file(const struct dirent *de) { |
1550 | assert(de); | |
1551 | ||
456aa879 LP |
1552 | /* Returns true if the specified directory entry looks like a journal file we might be interested in */ |
1553 | ||
858749f7 LP |
1554 | if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN)) |
1555 | return false; | |
1556 | ||
1557 | return endswith(de->d_name, ".journal") || | |
1558 | endswith(de->d_name, ".journal~"); | |
1559 | } | |
1560 | ||
456aa879 LP |
1561 | static bool dirent_is_journal_subdir(const struct dirent *de) { |
1562 | const char *e, *n; | |
858749f7 LP |
1563 | assert(de); |
1564 | ||
456aa879 LP |
1565 | /* returns true if the specified directory entry looks like a directory that might contain journal |
1566 | * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a | |
1567 | * namespace. */ | |
1568 | ||
858749f7 LP |
1569 | if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN)) |
1570 | return false; | |
1571 | ||
456aa879 LP |
1572 | e = strchr(de->d_name, '.'); |
1573 | if (!e) | |
1574 | return id128_is_valid(de->d_name); /* No namespace */ | |
1575 | ||
2f82562b | 1576 | n = strndupa_safe(de->d_name, e - de->d_name); |
456aa879 LP |
1577 | if (!id128_is_valid(n)) |
1578 | return false; | |
1579 | ||
1580 | return log_namespace_name_valid(e + 1); | |
858749f7 LP |
1581 | } |
1582 | ||
1583 | static int directory_open(sd_journal *j, const char *path, DIR **ret) { | |
1584 | DIR *d; | |
1585 | ||
1586 | assert(j); | |
1587 | assert(path); | |
1588 | assert(ret); | |
1589 | ||
1590 | if (j->toplevel_fd < 0) | |
1591 | d = opendir(path); | |
1592 | else | |
1593 | /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is | |
1594 | * relative, by dropping the initial slash */ | |
1595 | d = xopendirat(j->toplevel_fd, skip_slash(path), 0); | |
1596 | if (!d) | |
1597 | return -errno; | |
1598 | ||
1599 | *ret = d; | |
1600 | return 0; | |
1601 | } | |
1602 | ||
1603 | static int add_directory(sd_journal *j, const char *prefix, const char *dirname); | |
1604 | ||
1605 | static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) { | |
858749f7 LP |
1606 | assert(j); |
1607 | assert(m); | |
1608 | assert(d); | |
1609 | ||
1610 | FOREACH_DIRENT_ALL(de, d, goto fail) { | |
1611 | if (dirent_is_journal_file(de)) | |
fc1813c0 | 1612 | (void) add_file_by_name(j, m->path, de->d_name); |
858749f7 | 1613 | |
456aa879 | 1614 | if (m->is_root && dirent_is_journal_subdir(de)) |
858749f7 LP |
1615 | (void) add_directory(j, m->path, de->d_name); |
1616 | } | |
1617 | ||
1618 | return; | |
858749f7 LP |
1619 | fail: |
1620 | log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path); | |
1621 | } | |
1622 | ||
1623 | static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) { | |
1624 | int r; | |
1625 | ||
1626 | assert(j); | |
1627 | assert(m); | |
1628 | assert(fd >= 0); | |
1629 | ||
1630 | /* Watch this directory if that's enabled and if it not being watched yet. */ | |
1631 | ||
1632 | if (m->wd > 0) /* Already have a watch? */ | |
1633 | return; | |
1634 | if (j->inotify_fd < 0) /* Not watching at all? */ | |
1635 | return; | |
1636 | ||
1637 | m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask); | |
1638 | if (m->wd < 0) { | |
1639 | log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path); | |
1640 | return; | |
1641 | } | |
1642 | ||
1643 | r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m); | |
1644 | if (r == -EEXIST) | |
1645 | log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path); | |
1646 | if (r < 0) { | |
1647 | log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path); | |
1648 | (void) inotify_rm_watch(j->inotify_fd, m->wd); | |
1649 | m->wd = -1; | |
1650 | } | |
1651 | } | |
1652 | ||
456aa879 LP |
1653 | static int add_directory( |
1654 | sd_journal *j, | |
1655 | const char *prefix, | |
1656 | const char *dirname) { | |
1657 | ||
7fd1b19b | 1658 | _cleanup_free_ char *path = NULL; |
7fd1b19b | 1659 | _cleanup_closedir_ DIR *d = NULL; |
a963990f | 1660 | Directory *m; |
d617408e | 1661 | int r, k; |
3fbf9cbb LP |
1662 | |
1663 | assert(j); | |
1664 | assert(prefix); | |
3fbf9cbb | 1665 | |
5d1ce257 LP |
1666 | /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch |
1667 | * and reenumerates directory contents */ | |
d95b1fb3 | 1668 | |
657ee2d8 | 1669 | path = path_join(prefix, dirname); |
d617408e LP |
1670 | if (!path) { |
1671 | r = -ENOMEM; | |
1672 | goto fail; | |
1673 | } | |
3fbf9cbb | 1674 | |
858749f7 | 1675 | log_debug("Considering directory '%s'.", path); |
5d1ce257 LP |
1676 | |
1677 | /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */ | |
1678 | if ((j->flags & SD_JOURNAL_LOCAL_ONLY) && | |
1679 | !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run"))) | |
858749f7 | 1680 | return 0; |
5d1ce257 | 1681 | |
2b6df46d LP |
1682 | if (dirname && |
1683 | (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) || | |
1684 | dirname_has_namespace(dirname, j->namespace) > 0 || | |
1685 | (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0)))) | |
456aa879 LP |
1686 | return 0; |
1687 | ||
858749f7 LP |
1688 | r = directory_open(j, path, &d); |
1689 | if (r < 0) { | |
1690 | log_debug_errno(r, "Failed to open directory '%s': %m", path); | |
d617408e | 1691 | goto fail; |
3fbf9cbb LP |
1692 | } |
1693 | ||
a963990f LP |
1694 | m = hashmap_get(j->directories_by_path, path); |
1695 | if (!m) { | |
2f5435a1 | 1696 | m = new(Directory, 1); |
d617408e LP |
1697 | if (!m) { |
1698 | r = -ENOMEM; | |
1699 | goto fail; | |
1700 | } | |
a963990f | 1701 | |
2f5435a1 LP |
1702 | *m = (Directory) { |
1703 | .is_root = false, | |
1704 | .path = path, | |
1705 | }; | |
a963990f LP |
1706 | |
1707 | if (hashmap_put(j->directories_by_path, m->path, m) < 0) { | |
a963990f | 1708 | free(m); |
d617408e LP |
1709 | r = -ENOMEM; |
1710 | goto fail; | |
a963990f LP |
1711 | } |
1712 | ||
a50d7d43 | 1713 | path = NULL; /* avoid freeing in cleanup */ |
313cefa1 | 1714 | j->current_invalidate_counter++; |
a963990f | 1715 | |
5ec76417 | 1716 | log_debug("Directory %s added.", m->path); |
a963990f | 1717 | |
a50d7d43 | 1718 | } else if (m->is_root) |
858749f7 | 1719 | return 0; /* Don't 'downgrade' from root directory */ |
a963990f | 1720 | |
858749f7 | 1721 | m->last_seen_generation = j->generation; |
a963990f | 1722 | |
858749f7 LP |
1723 | directory_watch(j, m, dirfd(d), |
1724 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1725 | IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM| | |
1726 | IN_ONLYDIR); | |
a963990f | 1727 | |
858749f7 LP |
1728 | if (!j->no_new_files) |
1729 | directory_enumerate(j, m, d); | |
a963990f | 1730 | |
85210bff LP |
1731 | check_network(j, dirfd(d)); |
1732 | ||
a963990f | 1733 | return 0; |
d617408e LP |
1734 | |
1735 | fail: | |
5d1ce257 | 1736 | k = journal_put_error(j, r, path ?: prefix); |
d617408e LP |
1737 | if (k < 0) |
1738 | return k; | |
1739 | ||
1740 | return r; | |
a963990f LP |
1741 | } |
1742 | ||
d617408e | 1743 | static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) { |
5d1ce257 | 1744 | |
7fd1b19b | 1745 | _cleanup_closedir_ DIR *d = NULL; |
a963990f | 1746 | Directory *m; |
d617408e | 1747 | int r, k; |
a963990f LP |
1748 | |
1749 | assert(j); | |
a963990f | 1750 | |
5d1ce257 LP |
1751 | /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we |
1752 | * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially | |
1753 | * populate the set, as well as to update it later. */ | |
a963990f | 1754 | |
5d1ce257 LP |
1755 | if (p) { |
1756 | /* If there's a path specified, use it. */ | |
b6741478 | 1757 | |
858749f7 LP |
1758 | log_debug("Considering root directory '%s'.", p); |
1759 | ||
5d1ce257 LP |
1760 | if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) && |
1761 | !path_has_prefix(j, p, "/run")) | |
1762 | return -EINVAL; | |
d617408e | 1763 | |
5d1ce257 LP |
1764 | if (j->prefix) |
1765 | p = strjoina(j->prefix, p); | |
1766 | ||
858749f7 LP |
1767 | r = directory_open(j, p, &d); |
1768 | if (r == -ENOENT && missing_ok) | |
1769 | return 0; | |
1770 | if (r < 0) { | |
1771 | log_debug_errno(r, "Failed to open root directory %s: %m", p); | |
5d1ce257 LP |
1772 | goto fail; |
1773 | } | |
1774 | } else { | |
254d1313 | 1775 | _cleanup_close_ int dfd = -EBADF; |
5d1ce257 LP |
1776 | |
1777 | /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since | |
1778 | * opendir() will take possession of the fd, and close it, which we don't want. */ | |
1779 | ||
1780 | p = "."; /* store this as "." in the directories hashmap */ | |
1781 | ||
1782 | dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3); | |
1783 | if (dfd < 0) { | |
1784 | r = -errno; | |
1785 | goto fail; | |
1786 | } | |
1787 | ||
8e06af80 | 1788 | d = take_fdopendir(&dfd); |
5d1ce257 LP |
1789 | if (!d) { |
1790 | r = -errno; | |
5d1ce257 LP |
1791 | goto fail; |
1792 | } | |
1793 | ||
1794 | rewinddir(d); | |
d617408e | 1795 | } |
a963990f LP |
1796 | |
1797 | m = hashmap_get(j->directories_by_path, p); | |
1798 | if (!m) { | |
1799 | m = new0(Directory, 1); | |
d617408e LP |
1800 | if (!m) { |
1801 | r = -ENOMEM; | |
1802 | goto fail; | |
1803 | } | |
a963990f LP |
1804 | |
1805 | m->is_root = true; | |
5d1ce257 | 1806 | |
a963990f LP |
1807 | m->path = strdup(p); |
1808 | if (!m->path) { | |
a963990f | 1809 | free(m); |
d617408e LP |
1810 | r = -ENOMEM; |
1811 | goto fail; | |
a963990f LP |
1812 | } |
1813 | ||
1814 | if (hashmap_put(j->directories_by_path, m->path, m) < 0) { | |
a963990f LP |
1815 | free(m->path); |
1816 | free(m); | |
d617408e LP |
1817 | r = -ENOMEM; |
1818 | goto fail; | |
a963990f LP |
1819 | } |
1820 | ||
313cefa1 | 1821 | j->current_invalidate_counter++; |
a963990f | 1822 | |
5ec76417 | 1823 | log_debug("Root directory %s added.", m->path); |
a963990f | 1824 | |
a50d7d43 | 1825 | } else if (!m->is_root) |
a963990f | 1826 | return 0; |
50f20cfd | 1827 | |
858749f7 LP |
1828 | directory_watch(j, m, dirfd(d), |
1829 | IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE| | |
1830 | IN_ONLYDIR); | |
a963990f | 1831 | |
858749f7 LP |
1832 | if (!j->no_new_files) |
1833 | directory_enumerate(j, m, d); | |
3fbf9cbb | 1834 | |
85210bff LP |
1835 | check_network(j, dirfd(d)); |
1836 | ||
a963990f | 1837 | return 0; |
d617408e LP |
1838 | |
1839 | fail: | |
5768d259 | 1840 | k = journal_put_error(j, r, p); |
d617408e LP |
1841 | if (k < 0) |
1842 | return k; | |
1843 | ||
1844 | return r; | |
a963990f LP |
1845 | } |
1846 | ||
b2b46f91 | 1847 | static void remove_directory(sd_journal *j, Directory *d) { |
a963990f LP |
1848 | assert(j); |
1849 | ||
1850 | if (d->wd > 0) { | |
1851 | hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd)); | |
1852 | ||
1853 | if (j->inotify_fd >= 0) | |
98d4a4df | 1854 | (void) inotify_rm_watch(j->inotify_fd, d->wd); |
a963990f LP |
1855 | } |
1856 | ||
1857 | hashmap_remove(j->directories_by_path, d->path); | |
1858 | ||
1859 | if (d->is_root) | |
5ec76417 | 1860 | log_debug("Root directory %s removed.", d->path); |
a963990f | 1861 | else |
5ec76417 | 1862 | log_debug("Directory %s removed.", d->path); |
a963990f LP |
1863 | |
1864 | free(d->path); | |
1865 | free(d); | |
3fbf9cbb LP |
1866 | } |
1867 | ||
89739579 | 1868 | static int add_search_paths(sd_journal *j) { |
d617408e LP |
1869 | |
1870 | static const char search_paths[] = | |
a963990f LP |
1871 | "/run/log/journal\0" |
1872 | "/var/log/journal\0"; | |
50f20cfd LP |
1873 | |
1874 | assert(j); | |
50f20cfd | 1875 | |
a963990f LP |
1876 | /* We ignore most errors here, since the idea is to only open |
1877 | * what's actually accessible, and ignore the rest. */ | |
50f20cfd | 1878 | |
d617408e LP |
1879 | NULSTR_FOREACH(p, search_paths) |
1880 | (void) add_root_directory(j, p, true); | |
50f20cfd | 1881 | |
574b77ef MB |
1882 | if (!(j->flags & SD_JOURNAL_LOCAL_ONLY)) |
1883 | (void) add_root_directory(j, "/var/log/journal/remote", true); | |
1884 | ||
a963990f | 1885 | return 0; |
50f20cfd LP |
1886 | } |
1887 | ||
5302ebe1 | 1888 | static int add_current_paths(sd_journal *j) { |
5302ebe1 ZJS |
1889 | JournalFile *f; |
1890 | ||
1891 | assert(j); | |
1892 | assert(j->no_new_files); | |
1893 | ||
5d1ce257 | 1894 | /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we |
5302ebe1 ZJS |
1895 | * treat them as fatal. */ |
1896 | ||
90e74a66 | 1897 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
c2b2df60 | 1898 | _cleanup_free_ char *dir = NULL; |
e9174f29 | 1899 | int r; |
5302ebe1 | 1900 | |
45519d13 LP |
1901 | r = path_extract_directory(f->path, &dir); |
1902 | if (r < 0) | |
1903 | return r; | |
5302ebe1 | 1904 | |
5d1ce257 | 1905 | r = add_directory(j, dir, NULL); |
d617408e | 1906 | if (r < 0) |
5302ebe1 | 1907 | return r; |
5302ebe1 ZJS |
1908 | } |
1909 | ||
1910 | return 0; | |
1911 | } | |
1912 | ||
a963990f | 1913 | static int allocate_inotify(sd_journal *j) { |
50f20cfd | 1914 | assert(j); |
50f20cfd | 1915 | |
a963990f LP |
1916 | if (j->inotify_fd < 0) { |
1917 | j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
1918 | if (j->inotify_fd < 0) | |
1919 | return -errno; | |
1920 | } | |
50f20cfd | 1921 | |
cb306f5d | 1922 | return hashmap_ensure_allocated(&j->directories_by_wd, NULL); |
50f20cfd LP |
1923 | } |
1924 | ||
456aa879 | 1925 | static sd_journal *journal_new(int flags, const char *path, const char *namespace) { |
17c9aff8 | 1926 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
50f20cfd | 1927 | |
6f30a67a | 1928 | j = new(sd_journal, 1); |
a963990f LP |
1929 | if (!j) |
1930 | return NULL; | |
50f20cfd | 1931 | |
6f30a67a LP |
1932 | *j = (sd_journal) { |
1933 | .original_pid = getpid_cached(), | |
1934 | .toplevel_fd = -EBADF, | |
1935 | .inotify_fd = -EBADF, | |
1936 | .flags = flags, | |
1937 | .data_threshold = DEFAULT_DATA_THRESHOLD, | |
1938 | }; | |
50f20cfd | 1939 | |
7827b1a1 | 1940 | if (path) { |
16fefe90 ZJS |
1941 | char *t; |
1942 | ||
1943 | t = strdup(path); | |
1944 | if (!t) | |
17c9aff8 | 1945 | return NULL; |
16fefe90 ZJS |
1946 | |
1947 | if (flags & SD_JOURNAL_OS_ROOT) | |
1948 | j->prefix = t; | |
1949 | else | |
1950 | j->path = t; | |
7827b1a1 LP |
1951 | } |
1952 | ||
456aa879 LP |
1953 | if (namespace) { |
1954 | j->namespace = strdup(namespace); | |
1955 | if (!j->namespace) | |
1956 | return NULL; | |
1957 | } | |
1958 | ||
548f6937 | 1959 | j->files = ordered_hashmap_new(&path_hash_ops); |
5d4ba7f2 | 1960 | if (!j->files) |
17c9aff8 | 1961 | return NULL; |
5d4ba7f2 VC |
1962 | |
1963 | j->files_cache = ordered_hashmap_iterated_cache_new(j->files); | |
548f6937 | 1964 | j->directories_by_path = hashmap_new(&path_hash_ops); |
84168d80 | 1965 | j->mmap = mmap_cache_new(); |
5d4ba7f2 | 1966 | if (!j->files_cache || !j->directories_by_path || !j->mmap) |
17c9aff8 | 1967 | return NULL; |
6180fc61 | 1968 | |
17c9aff8 | 1969 | return TAKE_PTR(j); |
50f20cfd LP |
1970 | } |
1971 | ||
1aaa68f5 ZJS |
1972 | #define OPEN_ALLOWED_FLAGS \ |
1973 | (SD_JOURNAL_LOCAL_ONLY | \ | |
1974 | SD_JOURNAL_RUNTIME_ONLY | \ | |
456aa879 LP |
1975 | SD_JOURNAL_SYSTEM | \ |
1976 | SD_JOURNAL_CURRENT_USER | \ | |
1977 | SD_JOURNAL_ALL_NAMESPACES | \ | |
1978 | SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) | |
1aaa68f5 | 1979 | |
456aa879 | 1980 | _public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) { |
17c9aff8 | 1981 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
3fbf9cbb | 1982 | int r; |
87d2c1ff | 1983 | |
1ae464e0 | 1984 | assert_return(ret, -EINVAL); |
1aaa68f5 | 1985 | assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL); |
87d2c1ff | 1986 | |
456aa879 | 1987 | j = journal_new(flags, NULL, namespace); |
87d2c1ff LP |
1988 | if (!j) |
1989 | return -ENOMEM; | |
1990 | ||
89739579 | 1991 | r = add_search_paths(j); |
a963990f | 1992 | if (r < 0) |
17c9aff8 | 1993 | return r; |
50f20cfd | 1994 | |
17c9aff8 | 1995 | *ret = TAKE_PTR(j); |
a963990f | 1996 | return 0; |
a963990f | 1997 | } |
50f20cfd | 1998 | |
456aa879 LP |
1999 | _public_ int sd_journal_open(sd_journal **ret, int flags) { |
2000 | return sd_journal_open_namespace(ret, NULL, flags); | |
2001 | } | |
2002 | ||
1aaa68f5 ZJS |
2003 | #define OPEN_CONTAINER_ALLOWED_FLAGS \ |
2004 | (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM) | |
2005 | ||
b6741478 LP |
2006 | _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) { |
2007 | _cleanup_free_ char *root = NULL, *class = NULL; | |
17c9aff8 | 2008 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
b6741478 LP |
2009 | char *p; |
2010 | int r; | |
2011 | ||
68312977 | 2012 | /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in |
2daa9cbd LP |
2013 | * combination with sd_journal_open_directory_fd(). */ |
2014 | ||
b6741478 LP |
2015 | assert_return(machine, -EINVAL); |
2016 | assert_return(ret, -EINVAL); | |
1aaa68f5 | 2017 | assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL); |
52ef5dd7 | 2018 | assert_return(hostname_is_valid(machine, 0), -EINVAL); |
b6741478 | 2019 | |
63c372cb | 2020 | p = strjoina("/run/systemd/machines/", machine); |
13df9c39 LP |
2021 | r = parse_env_file(NULL, p, |
2022 | "ROOT", &root, | |
2023 | "CLASS", &class); | |
b6741478 LP |
2024 | if (r == -ENOENT) |
2025 | return -EHOSTDOWN; | |
2026 | if (r < 0) | |
2027 | return r; | |
2028 | if (!root) | |
2029 | return -ENODATA; | |
2030 | ||
2031 | if (!streq_ptr(class, "container")) | |
2032 | return -EIO; | |
2033 | ||
456aa879 | 2034 | j = journal_new(flags, root, NULL); |
b6741478 LP |
2035 | if (!j) |
2036 | return -ENOMEM; | |
2037 | ||
89739579 | 2038 | r = add_search_paths(j); |
b6741478 | 2039 | if (r < 0) |
17c9aff8 | 2040 | return r; |
b6741478 | 2041 | |
17c9aff8 | 2042 | *ret = TAKE_PTR(j); |
b6741478 | 2043 | return 0; |
b6741478 LP |
2044 | } |
2045 | ||
1aaa68f5 | 2046 | #define OPEN_DIRECTORY_ALLOWED_FLAGS \ |
10752e82 ZJS |
2047 | (SD_JOURNAL_OS_ROOT | \ |
2048 | SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) | |
1aaa68f5 | 2049 | |
a963990f | 2050 | _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) { |
17c9aff8 | 2051 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
a963990f | 2052 | int r; |
87d2c1ff | 2053 | |
1ae464e0 TA |
2054 | assert_return(ret, -EINVAL); |
2055 | assert_return(path, -EINVAL); | |
1aaa68f5 | 2056 | assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL); |
87d2c1ff | 2057 | |
456aa879 | 2058 | j = journal_new(flags, path, NULL); |
a963990f LP |
2059 | if (!j) |
2060 | return -ENOMEM; | |
3fbf9cbb | 2061 | |
d077390c LP |
2062 | if (flags & SD_JOURNAL_OS_ROOT) |
2063 | r = add_search_paths(j); | |
2064 | else | |
2065 | r = add_root_directory(j, path, false); | |
d617408e | 2066 | if (r < 0) |
17c9aff8 | 2067 | return r; |
87d2c1ff | 2068 | |
17c9aff8 | 2069 | *ret = TAKE_PTR(j); |
87d2c1ff | 2070 | return 0; |
a963990f | 2071 | } |
87d2c1ff | 2072 | |
5302ebe1 | 2073 | _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) { |
17c9aff8 | 2074 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5302ebe1 ZJS |
2075 | int r; |
2076 | ||
1ae464e0 TA |
2077 | assert_return(ret, -EINVAL); |
2078 | assert_return(flags == 0, -EINVAL); | |
5302ebe1 | 2079 | |
456aa879 | 2080 | j = journal_new(flags, NULL, NULL); |
5302ebe1 ZJS |
2081 | if (!j) |
2082 | return -ENOMEM; | |
2083 | ||
2084 | STRV_FOREACH(path, paths) { | |
5d1ce257 | 2085 | r = add_any_file(j, -1, *path); |
d617408e | 2086 | if (r < 0) |
17c9aff8 | 2087 | return r; |
5302ebe1 ZJS |
2088 | } |
2089 | ||
2090 | j->no_new_files = true; | |
2091 | ||
17c9aff8 | 2092 | *ret = TAKE_PTR(j); |
5302ebe1 | 2093 | return 0; |
5d1ce257 LP |
2094 | } |
2095 | ||
1aaa68f5 | 2096 | #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \ |
10752e82 ZJS |
2097 | (SD_JOURNAL_OS_ROOT | \ |
2098 | SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER ) | |
1aaa68f5 | 2099 | |
5d1ce257 | 2100 | _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) { |
17c9aff8 | 2101 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5d1ce257 LP |
2102 | struct stat st; |
2103 | int r; | |
2104 | ||
2105 | assert_return(ret, -EINVAL); | |
2106 | assert_return(fd >= 0, -EBADF); | |
1aaa68f5 | 2107 | assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL); |
5d1ce257 LP |
2108 | |
2109 | if (fstat(fd, &st) < 0) | |
2110 | return -errno; | |
2111 | ||
2112 | if (!S_ISDIR(st.st_mode)) | |
2113 | return -EBADFD; | |
2114 | ||
456aa879 | 2115 | j = journal_new(flags, NULL, NULL); |
5d1ce257 LP |
2116 | if (!j) |
2117 | return -ENOMEM; | |
2118 | ||
2119 | j->toplevel_fd = fd; | |
2120 | ||
d077390c LP |
2121 | if (flags & SD_JOURNAL_OS_ROOT) |
2122 | r = add_search_paths(j); | |
2123 | else | |
2124 | r = add_root_directory(j, NULL, false); | |
5d1ce257 | 2125 | if (r < 0) |
17c9aff8 | 2126 | return r; |
5d1ce257 | 2127 | |
17c9aff8 | 2128 | *ret = TAKE_PTR(j); |
5d1ce257 | 2129 | return 0; |
5d1ce257 LP |
2130 | } |
2131 | ||
2132 | _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) { | |
5d1ce257 | 2133 | JournalFile *f; |
17c9aff8 | 2134 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
5d1ce257 LP |
2135 | int r; |
2136 | ||
2137 | assert_return(ret, -EINVAL); | |
2138 | assert_return(n_fds > 0, -EBADF); | |
2139 | assert_return(flags == 0, -EINVAL); | |
2140 | ||
456aa879 | 2141 | j = journal_new(flags, NULL, NULL); |
5d1ce257 LP |
2142 | if (!j) |
2143 | return -ENOMEM; | |
2144 | ||
abcdc02c | 2145 | for (unsigned i = 0; i < n_fds; i++) { |
5d1ce257 LP |
2146 | struct stat st; |
2147 | ||
2148 | if (fds[i] < 0) { | |
2149 | r = -EBADF; | |
2150 | goto fail; | |
2151 | } | |
2152 | ||
2153 | if (fstat(fds[i], &st) < 0) { | |
2154 | r = -errno; | |
2155 | goto fail; | |
2156 | } | |
2157 | ||
3cc44114 LP |
2158 | r = stat_verify_regular(&st); |
2159 | if (r < 0) | |
5d1ce257 | 2160 | goto fail; |
5d1ce257 LP |
2161 | |
2162 | r = add_any_file(j, fds[i], NULL); | |
2163 | if (r < 0) | |
2164 | goto fail; | |
2165 | } | |
2166 | ||
2167 | j->no_new_files = true; | |
2168 | j->no_inotify = true; | |
5302ebe1 | 2169 | |
17c9aff8 | 2170 | *ret = TAKE_PTR(j); |
5d1ce257 LP |
2171 | return 0; |
2172 | ||
2173 | fail: | |
f8e2f4d6 | 2174 | /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they |
5d1ce257 | 2175 | * remain open */ |
90e74a66 | 2176 | ORDERED_HASHMAP_FOREACH(f, j->files) |
5d1ce257 LP |
2177 | f->close_fd = false; |
2178 | ||
5302ebe1 ZJS |
2179 | return r; |
2180 | } | |
2181 | ||
a5344d2c | 2182 | _public_ void sd_journal_close(sd_journal *j) { |
a963990f | 2183 | Directory *d; |
34af7494 | 2184 | Prioq *p; |
a963990f | 2185 | |
a5344d2c LP |
2186 | if (!j) |
2187 | return; | |
87d2c1ff | 2188 | |
34af7494 LP |
2189 | while ((p = hashmap_first(j->newest_by_boot_id))) |
2190 | journal_file_unlink_newest_by_bood_id(j, prioq_peek(p)); | |
2191 | hashmap_free(j->newest_by_boot_id); | |
2192 | ||
54b1da83 LP |
2193 | sd_journal_flush_matches(j); |
2194 | ||
f9168190 | 2195 | ordered_hashmap_free_with_destructor(j->files, journal_file_close); |
5d4ba7f2 | 2196 | iterated_cache_free(j->files_cache); |
260a2be4 | 2197 | |
a963990f LP |
2198 | while ((d = hashmap_first(j->directories_by_path))) |
2199 | remove_directory(j, d); | |
260a2be4 | 2200 | |
a963990f LP |
2201 | while ((d = hashmap_first(j->directories_by_wd))) |
2202 | remove_directory(j, d); | |
87d2c1ff | 2203 | |
a963990f LP |
2204 | hashmap_free(j->directories_by_path); |
2205 | hashmap_free(j->directories_by_wd); | |
1cc101f1 | 2206 | |
03e334a1 | 2207 | safe_close(j->inotify_fd); |
50f20cfd | 2208 | |
bf807d4d | 2209 | if (j->mmap) { |
3a595c59 | 2210 | mmap_cache_stats_log_debug(j->mmap); |
16e9f408 | 2211 | mmap_cache_unref(j->mmap); |
bf807d4d | 2212 | } |
16e9f408 | 2213 | |
ec1d2909 | 2214 | hashmap_free_free(j->errors); |
5768d259 | 2215 | |
7827b1a1 | 2216 | free(j->path); |
89739579 | 2217 | free(j->prefix); |
456aa879 | 2218 | free(j->namespace); |
3c1668da | 2219 | free(j->unique_field); |
eb86030e | 2220 | free(j->fields_buffer); |
87d2c1ff LP |
2221 | free(j); |
2222 | } | |
3fbf9cbb | 2223 | |
34af7494 LP |
2224 | static void journal_file_unlink_newest_by_bood_id(sd_journal *j, JournalFile *f) { |
2225 | JournalFile *nf; | |
2226 | Prioq *p; | |
2227 | ||
2228 | assert(j); | |
2229 | assert(f); | |
2230 | ||
2231 | if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) /* not linked currently, hence this is a NOP */ | |
2232 | return; | |
2233 | ||
2234 | assert_se(p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id)); | |
2235 | assert_se(prioq_remove(p, f, &f->newest_boot_id_prioq_idx) > 0); | |
2236 | ||
2237 | nf = prioq_peek(p); | |
2238 | if (nf) | |
2239 | /* There's still a member in the prioq? Then make sure the hashmap key now points to its | |
2240 | * .newest_boot_id field (and not ours!). Not we only replace the memory of the key here, the | |
2241 | * value of the key (and the data associated with it) remain the same. */ | |
2242 | assert_se(hashmap_update(j->newest_by_boot_id, &nf->newest_boot_id, p) >= 0); | |
2243 | else { | |
2244 | assert_se(hashmap_remove(j->newest_by_boot_id, &f->newest_boot_id) == p); | |
2245 | prioq_free(p); | |
2246 | } | |
2247 | ||
2248 | f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL; | |
2249 | } | |
2250 | ||
2251 | static int journal_file_newest_monotonic_compare(const void *a, const void *b) { | |
2252 | const JournalFile *x = a, *y = b; | |
2253 | ||
2254 | return -CMP(x->newest_monotonic_usec, y->newest_monotonic_usec); /* Invert order, we want newest first! */ | |
2255 | } | |
2256 | ||
2257 | static int journal_file_reshuffle_newest_by_boot_id(sd_journal *j, JournalFile *f) { | |
2258 | Prioq *p; | |
2259 | int r; | |
2260 | ||
2261 | assert(j); | |
2262 | assert(f); | |
2263 | ||
2264 | p = hashmap_get(j->newest_by_boot_id, &f->newest_boot_id); | |
2265 | if (p) { | |
2266 | /* There's already a priority queue for this boot ID */ | |
2267 | ||
2268 | if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) { | |
2269 | r = prioq_put(p, f, &f->newest_boot_id_prioq_idx); /* Insert if we aren't in there yet */ | |
2270 | if (r < 0) | |
2271 | return r; | |
2272 | } else | |
2273 | prioq_reshuffle(p, f, &f->newest_boot_id_prioq_idx); /* Reshuffle otherwise */ | |
2274 | ||
2275 | } else { | |
2276 | _cleanup_(prioq_freep) Prioq *q = NULL; | |
2277 | ||
2278 | /* No priority queue yet, then allocate one */ | |
2279 | ||
2280 | assert(f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL); /* we can't be a member either */ | |
2281 | ||
2282 | q = prioq_new(journal_file_newest_monotonic_compare); | |
2283 | if (!q) | |
2284 | return -ENOMEM; | |
2285 | ||
2286 | r = prioq_put(q, f, &f->newest_boot_id_prioq_idx); | |
2287 | if (r < 0) | |
2288 | return r; | |
2289 | ||
2290 | r = hashmap_ensure_put(&j->newest_by_boot_id, &id128_hash_ops, &f->newest_boot_id, q); | |
2291 | if (r < 0) | |
2292 | return r; | |
2293 | ||
2294 | TAKE_PTR(q); | |
2295 | } | |
2296 | ||
2297 | return 0; | |
2298 | } | |
2299 | ||
2300 | static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) { | |
2301 | uint64_t offset, mo, rt; | |
2302 | sd_id128_t id; | |
2303 | ObjectType type; | |
2304 | Object *o; | |
2305 | int r; | |
2306 | ||
2307 | assert(j); | |
2308 | assert(f); | |
2309 | assert(f->header); | |
2310 | ||
2311 | /* Tries to read the timestamp of the most recently written entry. */ | |
2312 | ||
2313 | r = journal_file_fstat(f); | |
2314 | if (r < 0) | |
2315 | return r; | |
2316 | if (f->newest_mtime == timespec_load(&f->last_stat.st_mtim)) | |
2317 | return 0; /* mtime didn't change since last time, don't bother */ | |
2318 | ||
2319 | if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) { | |
2320 | offset = le64toh(READ_NOW(f->header->tail_entry_offset)); | |
2321 | type = OBJECT_ENTRY; | |
2322 | } else { | |
2323 | offset = le64toh(READ_NOW(f->header->tail_object_offset)); | |
2324 | type = OBJECT_UNUSED; | |
2325 | } | |
2326 | if (offset == 0) | |
2327 | return -ENODATA; /* not a single object/entry, hence no tail timestamp */ | |
2328 | ||
2329 | /* Move to the last object in the journal file, in the hope it is an entry (which it usually will | |
2330 | * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED | |
2331 | * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't | |
2332 | * an entry. We instead check after figuring out the pointer. */ | |
2333 | r = journal_file_move_to_object(f, type, offset, &o); | |
2334 | if (r < 0) { | |
2335 | log_debug_errno(r, "Failed to move to last object in journal file, ignoring: %m"); | |
2336 | o = NULL; | |
2337 | } | |
2338 | if (o && o->object.type == OBJECT_ENTRY) { | |
2339 | /* Yay, last object is an entry, let's use the data. */ | |
2340 | id = o->entry.boot_id; | |
2341 | mo = le64toh(o->entry.monotonic); | |
2342 | rt = le64toh(o->entry.realtime); | |
2343 | } else { | |
2344 | /* So the object is not an entry or we couldn't access it? In that case, let's read the most | |
2345 | * recent entry timestamps from the header. It's equally good. Unfortunately though, in old | |
2346 | * versions of the journal the boot ID in the header doesn't have to match the monotonic | |
2347 | * timestamp of the header. Let's check the header flag that indicates whether this strictly | |
2348 | * matches first hence, before using the data. */ | |
2349 | ||
2350 | if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) && f->header->state == STATE_ARCHIVED) { | |
2351 | mo = le64toh(f->header->tail_entry_monotonic); | |
2352 | rt = le64toh(f->header->tail_entry_realtime); | |
2353 | id = f->header->tail_entry_boot_id; | |
2354 | ||
2355 | /* Some superficial checking if what we read makes sense. Note that we only do this | |
2356 | * when reading the timestamps from the Header object, but not when reading them from | |
2357 | * the most recent entry object, because in that case journal_file_move_to_object() | |
2358 | * already validated them. */ | |
2359 | if (!VALID_MONOTONIC(mo) || !VALID_REALTIME(rt)) | |
2360 | return -ENODATA; | |
2361 | ||
2362 | } else { | |
2363 | /* Otherwise let's find the last entry manually (this possibly means traversing the | |
2364 | * chain of entry arrays, till the end */ | |
2365 | r = journal_file_next_entry(f, 0, DIRECTION_UP, &o, NULL); | |
2366 | if (r < 0) | |
2367 | return r; | |
2368 | ||
2369 | id = o->entry.boot_id; | |
2370 | mo = le64toh(o->entry.monotonic); | |
2371 | rt = le64toh(o->entry.realtime); | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | if (mo > rt) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */ | |
2376 | return -ENODATA; | |
2377 | ||
2378 | if (!sd_id128_equal(f->newest_boot_id, id)) | |
2379 | journal_file_unlink_newest_by_bood_id(j, f); | |
2380 | ||
2381 | f->newest_boot_id = id; | |
2382 | f->newest_monotonic_usec = mo; | |
2383 | f->newest_realtime_usec = rt; | |
2384 | f->newest_machine_id = f->header->machine_id; | |
2385 | f->newest_mtime = timespec_load(&f->last_stat.st_mtim); | |
2386 | ||
2387 | r = journal_file_reshuffle_newest_by_boot_id(j, f); | |
2388 | if (r < 0) | |
2389 | return r; | |
2390 | ||
2391 | return 0; | |
2392 | } | |
2393 | ||
a5344d2c | 2394 | _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) { |
3fbf9cbb | 2395 | JournalFile *f; |
834f759c | 2396 | Object *o; |
3fbf9cbb LP |
2397 | int r; |
2398 | ||
1ae464e0 TA |
2399 | assert_return(j, -EINVAL); |
2400 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3fbf9cbb LP |
2401 | |
2402 | f = j->current_file; | |
2403 | if (!f) | |
de190aef | 2404 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2405 | if (f->current_offset <= 0) |
de190aef | 2406 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2407 | |
de190aef | 2408 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2409 | if (r < 0) |
2410 | return r; | |
2411 | ||
834f759c LP |
2412 | uint64_t t = le64toh(o->entry.realtime); |
2413 | if (!VALID_REALTIME(t)) | |
2414 | return -EBADMSG; | |
2415 | ||
2416 | if (ret) | |
2417 | *ret = t; | |
2418 | ||
de190aef | 2419 | return 0; |
3fbf9cbb LP |
2420 | } |
2421 | ||
a5344d2c | 2422 | _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) { |
3fbf9cbb | 2423 | JournalFile *f; |
404803e6 | 2424 | Object *o; |
3fbf9cbb | 2425 | int r; |
3fbf9cbb | 2426 | |
1ae464e0 TA |
2427 | assert_return(j, -EINVAL); |
2428 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3fbf9cbb LP |
2429 | |
2430 | f = j->current_file; | |
2431 | if (!f) | |
de190aef | 2432 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2433 | if (f->current_offset <= 0) |
de190aef | 2434 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2435 | |
de190aef | 2436 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2437 | if (r < 0) |
2438 | return r; | |
2439 | ||
de190aef LP |
2440 | if (ret_boot_id) |
2441 | *ret_boot_id = o->entry.boot_id; | |
2442 | else { | |
d4739bc4 VC |
2443 | sd_id128_t id; |
2444 | ||
de190aef LP |
2445 | r = sd_id128_get_boot(&id); |
2446 | if (r < 0) | |
2447 | return r; | |
3fbf9cbb | 2448 | |
de190aef | 2449 | if (!sd_id128_equal(id, o->entry.boot_id)) |
df50185b | 2450 | return -ESTALE; |
de190aef | 2451 | } |
3fbf9cbb | 2452 | |
404803e6 LP |
2453 | uint64_t t = le64toh(o->entry.monotonic); |
2454 | if (!VALID_MONOTONIC(t)) | |
2455 | return -EBADMSG; | |
2456 | ||
14a65d65 | 2457 | if (ret) |
404803e6 | 2458 | *ret = t; |
14a65d65 | 2459 | |
de190aef | 2460 | return 0; |
3fbf9cbb LP |
2461 | } |
2462 | ||
b1712fab LP |
2463 | _public_ int sd_journal_get_seqnum( |
2464 | sd_journal *j, | |
2465 | uint64_t *ret_seqnum, | |
2466 | sd_id128_t *ret_seqnum_id) { | |
2467 | ||
2468 | JournalFile *f; | |
2469 | Object *o; | |
2470 | int r; | |
2471 | ||
2472 | assert_return(j, -EINVAL); | |
2473 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2474 | ||
2475 | f = j->current_file; | |
2476 | if (!f) | |
2477 | return -EADDRNOTAVAIL; | |
2478 | ||
2479 | if (f->current_offset <= 0) | |
2480 | return -EADDRNOTAVAIL; | |
2481 | ||
2482 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); | |
2483 | if (r < 0) | |
2484 | return r; | |
2485 | ||
2486 | if (ret_seqnum_id) | |
2487 | *ret_seqnum_id = f->header->seqnum_id; | |
2488 | if (ret_seqnum) | |
2489 | *ret_seqnum = le64toh(o->entry.seqnum); | |
2490 | ||
2491 | return 0; | |
2492 | } | |
2493 | ||
362a3f81 | 2494 | static bool field_is_valid(const char *field) { |
362a3f81 LP |
2495 | assert(field); |
2496 | ||
2497 | if (isempty(field)) | |
2498 | return false; | |
2499 | ||
2500 | if (startswith(field, "__")) | |
2501 | return false; | |
2502 | ||
abcdc02c | 2503 | for (const char *p = field; *p; p++) { |
362a3f81 LP |
2504 | |
2505 | if (*p == '_') | |
2506 | continue; | |
2507 | ||
2508 | if (*p >= 'A' && *p <= 'Z') | |
2509 | continue; | |
2510 | ||
ff25d338 | 2511 | if (ascii_isdigit(*p)) |
362a3f81 LP |
2512 | continue; |
2513 | ||
2514 | return false; | |
2515 | } | |
2516 | ||
2517 | return true; | |
2518 | } | |
2519 | ||
a5344d2c | 2520 | _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) { |
3fbf9cbb | 2521 | JournalFile *f; |
3fbf9cbb | 2522 | size_t field_length; |
3fbf9cbb | 2523 | Object *o; |
a1640191 | 2524 | int r; |
3fbf9cbb | 2525 | |
1ae464e0 TA |
2526 | assert_return(j, -EINVAL); |
2527 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2528 | assert_return(field, -EINVAL); | |
2529 | assert_return(data, -EINVAL); | |
2530 | assert_return(size, -EINVAL); | |
2531 | assert_return(field_is_valid(field), -EINVAL); | |
3fbf9cbb LP |
2532 | |
2533 | f = j->current_file; | |
2534 | if (!f) | |
de190aef | 2535 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2536 | |
2537 | if (f->current_offset <= 0) | |
de190aef | 2538 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2539 | |
de190aef | 2540 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2541 | if (r < 0) |
2542 | return r; | |
2543 | ||
2544 | field_length = strlen(field); | |
2545 | ||
a9089a66 | 2546 | uint64_t n = journal_file_entry_n_items(f, o); |
abcdc02c | 2547 | for (uint64_t i = 0; i < n; i++) { |
0e35afff DDM |
2548 | uint64_t p; |
2549 | void *d; | |
2550 | size_t l; | |
3fbf9cbb | 2551 | |
a9089a66 | 2552 | p = journal_file_entry_item_object_offset(f, o, i); |
0e35afff DDM |
2553 | r = journal_file_data_payload(f, NULL, p, field, field_length, j->data_threshold, &d, &l); |
2554 | if (r == 0) | |
a1640191 | 2555 | goto next; |
df207ccb DDM |
2556 | if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) { |
2557 | log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i); | |
a1640191 | 2558 | goto next; |
8a799bed | 2559 | } |
3fbf9cbb LP |
2560 | if (r < 0) |
2561 | return r; | |
2562 | ||
0e35afff DDM |
2563 | *data = d; |
2564 | *size = l; | |
3fbf9cbb | 2565 | |
0e35afff | 2566 | return 0; |
a1640191 YW |
2567 | |
2568 | next: | |
2569 | /* journal_file_data_payload() may clear or overwrite cached object. */ | |
2570 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); | |
2571 | if (r < 0) | |
2572 | return r; | |
3fbf9cbb LP |
2573 | } |
2574 | ||
de190aef | 2575 | return -ENOENT; |
3fbf9cbb LP |
2576 | } |
2577 | ||
a5344d2c | 2578 | _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) { |
3fbf9cbb | 2579 | JournalFile *f; |
3fbf9cbb | 2580 | Object *o; |
5a94a2bf | 2581 | int r; |
3fbf9cbb | 2582 | |
1ae464e0 TA |
2583 | assert_return(j, -EINVAL); |
2584 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2585 | assert_return(data, -EINVAL); | |
2586 | assert_return(size, -EINVAL); | |
3fbf9cbb LP |
2587 | |
2588 | f = j->current_file; | |
2589 | if (!f) | |
de190aef | 2590 | return -EADDRNOTAVAIL; |
3fbf9cbb LP |
2591 | |
2592 | if (f->current_offset <= 0) | |
de190aef | 2593 | return -EADDRNOTAVAIL; |
3fbf9cbb | 2594 | |
de190aef | 2595 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); |
3fbf9cbb LP |
2596 | if (r < 0) |
2597 | return r; | |
2598 | ||
a9089a66 | 2599 | for (uint64_t n = journal_file_entry_n_items(f, o); j->current_field < n; j->current_field++) { |
5a94a2bf | 2600 | uint64_t p; |
0e35afff DDM |
2601 | void *d; |
2602 | size_t l; | |
3fbf9cbb | 2603 | |
a9089a66 | 2604 | p = journal_file_entry_item_object_offset(f, o, j->current_field); |
0e35afff | 2605 | r = journal_file_data_payload(f, NULL, p, NULL, 0, j->data_threshold, &d, &l); |
df207ccb DDM |
2606 | if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) { |
2607 | log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", j->current_field); | |
a1640191 | 2608 | goto next; |
5a94a2bf DDM |
2609 | } |
2610 | if (r < 0) | |
2611 | return r; | |
0e35afff | 2612 | assert(r > 0); |
3fbf9cbb | 2613 | |
0e35afff DDM |
2614 | *data = d; |
2615 | *size = l; | |
3fbf9cbb | 2616 | |
5a94a2bf | 2617 | j->current_field++; |
3fbf9cbb | 2618 | |
5a94a2bf | 2619 | return 1; |
a1640191 YW |
2620 | |
2621 | next: | |
2622 | /* journal_file_data_payload() may clear or overwrite cached object. */ | |
2623 | r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o); | |
2624 | if (r < 0) | |
2625 | return r; | |
5a94a2bf DDM |
2626 | } |
2627 | ||
2628 | return 0; | |
3fbf9cbb | 2629 | } |
c2373f84 | 2630 | |
76cbafcd ZJS |
2631 | _public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **data, size_t *size) { |
2632 | for (;;) { | |
2633 | int r; | |
2634 | ||
2635 | r = sd_journal_enumerate_data(j, data, size); | |
2636 | if (r >= 0) | |
2637 | return r; | |
2638 | if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r)) | |
2639 | return r; | |
2640 | j->current_field++; /* Try with the next field */ | |
2641 | } | |
2642 | } | |
2643 | ||
a5344d2c LP |
2644 | _public_ void sd_journal_restart_data(sd_journal *j) { |
2645 | if (!j) | |
2646 | return; | |
8725d60a LP |
2647 | |
2648 | j->current_field = 0; | |
c2373f84 | 2649 | } |
50f20cfd | 2650 | |
858749f7 LP |
2651 | static int reiterate_all_paths(sd_journal *j) { |
2652 | assert(j); | |
2653 | ||
2654 | if (j->no_new_files) | |
2655 | return add_current_paths(j); | |
2656 | ||
2657 | if (j->flags & SD_JOURNAL_OS_ROOT) | |
2658 | return add_search_paths(j); | |
2659 | ||
2660 | if (j->toplevel_fd >= 0) | |
2661 | return add_root_directory(j, NULL, false); | |
2662 | ||
2663 | if (j->path) | |
2664 | return add_root_directory(j, j->path, true); | |
2665 | ||
2666 | return add_search_paths(j); | |
2667 | } | |
2668 | ||
a5344d2c | 2669 | _public_ int sd_journal_get_fd(sd_journal *j) { |
a963990f LP |
2670 | int r; |
2671 | ||
1ae464e0 TA |
2672 | assert_return(j, -EINVAL); |
2673 | assert_return(!journal_pid_changed(j), -ECHILD); | |
50f20cfd | 2674 | |
5d1ce257 LP |
2675 | if (j->no_inotify) |
2676 | return -EMEDIUMTYPE; | |
2677 | ||
a963990f LP |
2678 | if (j->inotify_fd >= 0) |
2679 | return j->inotify_fd; | |
2680 | ||
2681 | r = allocate_inotify(j); | |
2682 | if (r < 0) | |
2683 | return r; | |
2684 | ||
858749f7 | 2685 | log_debug("Reiterating files to get inotify watches established."); |
5d1ce257 | 2686 | |
858749f7 LP |
2687 | /* Iterate through all dirs again, to add them to the inotify */ |
2688 | r = reiterate_all_paths(j); | |
a963990f LP |
2689 | if (r < 0) |
2690 | return r; | |
2691 | ||
50f20cfd LP |
2692 | return j->inotify_fd; |
2693 | } | |
2694 | ||
ee531d94 LP |
2695 | _public_ int sd_journal_get_events(sd_journal *j) { |
2696 | int fd; | |
2697 | ||
1ae464e0 TA |
2698 | assert_return(j, -EINVAL); |
2699 | assert_return(!journal_pid_changed(j), -ECHILD); | |
ee531d94 LP |
2700 | |
2701 | fd = sd_journal_get_fd(j); | |
2702 | if (fd < 0) | |
2703 | return fd; | |
2704 | ||
2705 | return POLLIN; | |
2706 | } | |
2707 | ||
39c155ea LP |
2708 | _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) { |
2709 | int fd; | |
2710 | ||
1ae464e0 TA |
2711 | assert_return(j, -EINVAL); |
2712 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2713 | assert_return(timeout_usec, -EINVAL); | |
39c155ea LP |
2714 | |
2715 | fd = sd_journal_get_fd(j); | |
2716 | if (fd < 0) | |
2717 | return fd; | |
2718 | ||
2719 | if (!j->on_network) { | |
f5fbe71d | 2720 | *timeout_usec = UINT64_MAX; |
39c155ea LP |
2721 | return 0; |
2722 | } | |
2723 | ||
2724 | /* If we are on the network we need to regularly check for | |
2725 | * changes manually */ | |
2726 | ||
2727 | *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC; | |
2728 | return 1; | |
2729 | } | |
2730 | ||
858749f7 LP |
2731 | static void process_q_overflow(sd_journal *j) { |
2732 | JournalFile *f; | |
2733 | Directory *m; | |
858749f7 LP |
2734 | |
2735 | assert(j); | |
2736 | ||
2737 | /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list | |
2738 | * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all | |
2739 | * journal files we encounter. All journal files and all directories that don't carry it after reenumeration | |
2740 | * are subject for unloading. */ | |
2741 | ||
2742 | log_debug("Inotify queue overrun, reiterating everything."); | |
2743 | ||
2744 | j->generation++; | |
2745 | (void) reiterate_all_paths(j); | |
2746 | ||
90e74a66 | 2747 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
858749f7 LP |
2748 | |
2749 | if (f->last_seen_generation == j->generation) | |
2750 | continue; | |
2751 | ||
2752 | log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path); | |
2753 | remove_file_real(j, f); | |
2754 | } | |
2755 | ||
90e74a66 | 2756 | HASHMAP_FOREACH(m, j->directories_by_path) { |
858749f7 LP |
2757 | |
2758 | if (m->last_seen_generation == j->generation) | |
2759 | continue; | |
2760 | ||
2761 | if (m->is_root) /* Never GC root directories */ | |
2762 | continue; | |
2763 | ||
2764 | log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path); | |
2765 | remove_directory(j, m); | |
2766 | } | |
2767 | ||
2768 | log_debug("Reiteration complete."); | |
2769 | } | |
2770 | ||
31e99dd2 | 2771 | static void process_inotify_event(sd_journal *j, const struct inotify_event *e) { |
a963990f | 2772 | Directory *d; |
50f20cfd LP |
2773 | |
2774 | assert(j); | |
2775 | assert(e); | |
2776 | ||
858749f7 LP |
2777 | if (e->mask & IN_Q_OVERFLOW) { |
2778 | process_q_overflow(j); | |
2779 | return; | |
2780 | } | |
2781 | ||
50f20cfd | 2782 | /* Is this a subdirectory we watch? */ |
a963990f LP |
2783 | d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd)); |
2784 | if (d) { | |
de2c3907 LP |
2785 | if (!(e->mask & IN_ISDIR) && e->len > 0 && |
2786 | (endswith(e->name, ".journal") || | |
2787 | endswith(e->name, ".journal~"))) { | |
50f20cfd LP |
2788 | |
2789 | /* Event for a journal file */ | |
2790 | ||
d617408e | 2791 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) |
fc1813c0 | 2792 | (void) add_file_by_name(j, d->path, e->name); |
d617408e | 2793 | else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) |
8e7e4a73 | 2794 | (void) remove_file_by_name(j, d->path, e->name); |
50f20cfd | 2795 | |
a963990f | 2796 | } else if (!d->is_root && e->len == 0) { |
50f20cfd | 2797 | |
a963990f | 2798 | /* Event for a subdirectory */ |
50f20cfd | 2799 | |
b2b46f91 TA |
2800 | if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) |
2801 | remove_directory(j, d); | |
50f20cfd | 2802 | |
a9be0692 | 2803 | } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) { |
50f20cfd | 2804 | |
a963990f | 2805 | /* Event for root directory */ |
50f20cfd | 2806 | |
d617408e LP |
2807 | if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) |
2808 | (void) add_directory(j, d->path, e->name); | |
50f20cfd LP |
2809 | } |
2810 | ||
2811 | return; | |
2812 | } | |
2813 | ||
2814 | if (e->mask & IN_IGNORED) | |
2815 | return; | |
2816 | ||
a9be0692 | 2817 | log_debug("Unexpected inotify event."); |
50f20cfd LP |
2818 | } |
2819 | ||
a963990f LP |
2820 | static int determine_change(sd_journal *j) { |
2821 | bool b; | |
2822 | ||
2823 | assert(j); | |
2824 | ||
2825 | b = j->current_invalidate_counter != j->last_invalidate_counter; | |
2826 | j->last_invalidate_counter = j->current_invalidate_counter; | |
2827 | ||
2828 | return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND; | |
2829 | } | |
2830 | ||
a5344d2c | 2831 | _public_ int sd_journal_process(sd_journal *j) { |
a963990f | 2832 | bool got_something = false; |
50f20cfd | 2833 | |
1ae464e0 TA |
2834 | assert_return(j, -EINVAL); |
2835 | assert_return(!journal_pid_changed(j), -ECHILD); | |
50f20cfd | 2836 | |
10c4d640 LP |
2837 | if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */ |
2838 | return 0; | |
2839 | ||
39c155ea | 2840 | j->last_process_usec = now(CLOCK_MONOTONIC); |
f9346444 | 2841 | j->last_invalidate_counter = j->current_invalidate_counter; |
39c155ea | 2842 | |
50f20cfd | 2843 | for (;;) { |
0254e944 | 2844 | union inotify_event_buffer buffer; |
50f20cfd LP |
2845 | ssize_t l; |
2846 | ||
0254e944 | 2847 | l = read(j->inotify_fd, &buffer, sizeof(buffer)); |
50f20cfd | 2848 | if (l < 0) { |
8add30a0 | 2849 | if (ERRNO_IS_TRANSIENT(errno)) |
a963990f | 2850 | return got_something ? determine_change(j) : SD_JOURNAL_NOP; |
50f20cfd LP |
2851 | |
2852 | return -errno; | |
2853 | } | |
2854 | ||
a963990f LP |
2855 | got_something = true; |
2856 | ||
f7c1ad4f | 2857 | FOREACH_INOTIFY_EVENT(e, buffer, l) |
50f20cfd | 2858 | process_inotify_event(j, e); |
50f20cfd LP |
2859 | } |
2860 | } | |
6ad1d1c3 | 2861 | |
e02d1cf7 | 2862 | _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) { |
a963990f | 2863 | int r; |
39c155ea | 2864 | uint64_t t; |
e02d1cf7 | 2865 | |
1ae464e0 TA |
2866 | assert_return(j, -EINVAL); |
2867 | assert_return(!journal_pid_changed(j), -ECHILD); | |
e02d1cf7 | 2868 | |
a963990f | 2869 | if (j->inotify_fd < 0) { |
28ca867a | 2870 | JournalFile *f; |
a963990f | 2871 | |
9eba03c7 | 2872 | /* This is the first invocation, hence create the inotify watch */ |
a963990f LP |
2873 | r = sd_journal_get_fd(j); |
2874 | if (r < 0) | |
2875 | return r; | |
2876 | ||
9eba03c7 LP |
2877 | /* Server might have done some vacuuming while we weren't watching. Get rid of the deleted |
2878 | * files now so they don't stay around indefinitely. */ | |
90e74a66 | 2879 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
28ca867a | 2880 | r = journal_file_fstat(f); |
8581b9f9 MS |
2881 | if (r == -EIDRM) |
2882 | remove_file_real(j, f); | |
9eba03c7 LP |
2883 | else if (r < 0) |
2884 | log_debug_errno(r, "Failed to fstat() journal file '%s', ignoring: %m", f->path); | |
28ca867a MS |
2885 | } |
2886 | ||
9eba03c7 LP |
2887 | /* The journal might have changed since the context object was created and we weren't |
2888 | * watching before, hence don't wait for anything, and return immediately. */ | |
a963990f LP |
2889 | return determine_change(j); |
2890 | } | |
2891 | ||
39c155ea LP |
2892 | r = sd_journal_get_timeout(j, &t); |
2893 | if (r < 0) | |
2894 | return r; | |
2895 | ||
f5fbe71d | 2896 | if (t != UINT64_MAX) { |
496db330 | 2897 | t = usec_sub_unsigned(t, now(CLOCK_MONOTONIC)); |
85210bff | 2898 | |
f5fbe71d | 2899 | if (timeout_usec == UINT64_MAX || timeout_usec > t) |
39c155ea | 2900 | timeout_usec = t; |
85210bff LP |
2901 | } |
2902 | ||
a963990f LP |
2903 | do { |
2904 | r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec); | |
2905 | } while (r == -EINTR); | |
e02d1cf7 LP |
2906 | |
2907 | if (r < 0) | |
2908 | return r; | |
2909 | ||
a963990f | 2910 | return sd_journal_process(j); |
e02d1cf7 LP |
2911 | } |
2912 | ||
08984293 | 2913 | _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) { |
08984293 LP |
2914 | JournalFile *f; |
2915 | bool first = true; | |
581483bf | 2916 | uint64_t fmin = 0, tmax = 0; |
08984293 LP |
2917 | int r; |
2918 | ||
1ae464e0 TA |
2919 | assert_return(j, -EINVAL); |
2920 | assert_return(!journal_pid_changed(j), -ECHILD); | |
2921 | assert_return(from || to, -EINVAL); | |
2922 | assert_return(from != to, -EINVAL); | |
08984293 | 2923 | |
90e74a66 | 2924 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
08984293 LP |
2925 | usec_t fr, t; |
2926 | ||
2927 | r = journal_file_get_cutoff_realtime_usec(f, &fr, &t); | |
9f8d2983 LP |
2928 | if (r == -ENOENT) |
2929 | continue; | |
08984293 LP |
2930 | if (r < 0) |
2931 | return r; | |
2932 | if (r == 0) | |
2933 | continue; | |
2934 | ||
2935 | if (first) { | |
581483bf LP |
2936 | fmin = fr; |
2937 | tmax = t; | |
08984293 LP |
2938 | first = false; |
2939 | } else { | |
581483bf LP |
2940 | fmin = MIN(fr, fmin); |
2941 | tmax = MAX(t, tmax); | |
08984293 LP |
2942 | } |
2943 | } | |
2944 | ||
581483bf LP |
2945 | if (from) |
2946 | *from = fmin; | |
2947 | if (to) | |
2948 | *to = tmax; | |
2949 | ||
08984293 LP |
2950 | return first ? 0 : 1; |
2951 | } | |
2952 | ||
f4cb1bfd LP |
2953 | _public_ int sd_journal_get_cutoff_monotonic_usec( |
2954 | sd_journal *j, | |
2955 | sd_id128_t boot_id, | |
2956 | uint64_t *ret_from, | |
2957 | uint64_t *ret_to) { | |
2958 | ||
2959 | uint64_t from = UINT64_MAX, to = UINT64_MAX; | |
1651e2c6 | 2960 | bool found = false; |
f4cb1bfd | 2961 | JournalFile *f; |
08984293 LP |
2962 | int r; |
2963 | ||
1ae464e0 TA |
2964 | assert_return(j, -EINVAL); |
2965 | assert_return(!journal_pid_changed(j), -ECHILD); | |
f4cb1bfd | 2966 | assert_return(ret_from != ret_to, -EINVAL); |
08984293 | 2967 | |
90e74a66 | 2968 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
f4cb1bfd | 2969 | usec_t ff, tt; |
08984293 | 2970 | |
f4cb1bfd | 2971 | r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &ff, &tt); |
9f8d2983 LP |
2972 | if (r == -ENOENT) |
2973 | continue; | |
08984293 LP |
2974 | if (r < 0) |
2975 | return r; | |
2976 | if (r == 0) | |
2977 | continue; | |
2978 | ||
1651e2c6 | 2979 | if (found) { |
f4cb1bfd LP |
2980 | from = MIN(ff, from); |
2981 | to = MAX(tt, to); | |
08984293 | 2982 | } else { |
f4cb1bfd LP |
2983 | from = ff; |
2984 | to = tt; | |
1651e2c6 | 2985 | found = true; |
08984293 LP |
2986 | } |
2987 | } | |
2988 | ||
f4cb1bfd LP |
2989 | if (ret_from) |
2990 | *ret_from = from; | |
2991 | if (ret_to) | |
2992 | *ret_to = to; | |
2993 | ||
1651e2c6 | 2994 | return found; |
08984293 LP |
2995 | } |
2996 | ||
dca6219e | 2997 | void journal_print_header(sd_journal *j) { |
dca6219e LP |
2998 | JournalFile *f; |
2999 | bool newline = false; | |
3000 | ||
3001 | assert(j); | |
3002 | ||
90e74a66 | 3003 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
dca6219e LP |
3004 | if (newline) |
3005 | putchar('\n'); | |
3006 | else | |
3007 | newline = true; | |
3008 | ||
3009 | journal_file_print_header(f); | |
3010 | } | |
3011 | } | |
08984293 | 3012 | |
d8671b1c | 3013 | _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret) { |
a1a03e30 LP |
3014 | JournalFile *f; |
3015 | uint64_t sum = 0; | |
3016 | ||
1ae464e0 TA |
3017 | assert_return(j, -EINVAL); |
3018 | assert_return(!journal_pid_changed(j), -ECHILD); | |
d8671b1c | 3019 | assert_return(ret, -EINVAL); |
a1a03e30 | 3020 | |
90e74a66 | 3021 | ORDERED_HASHMAP_FOREACH(f, j->files) { |
a1a03e30 | 3022 | struct stat st; |
d8671b1c | 3023 | uint64_t b; |
a1a03e30 LP |
3024 | |
3025 | if (fstat(f->fd, &st) < 0) | |
3026 | return -errno; | |
3027 | ||
d8671b1c LP |
3028 | b = (uint64_t) st.st_blocks; |
3029 | if (b > UINT64_MAX / 512) | |
3030 | return -EOVERFLOW; | |
3031 | b *= 512; | |
3032 | ||
3033 | if (sum > UINT64_MAX - b) | |
3034 | return -EOVERFLOW; | |
3035 | sum += b; | |
a1a03e30 LP |
3036 | } |
3037 | ||
d8671b1c | 3038 | *ret = sum; |
a1a03e30 LP |
3039 | return 0; |
3040 | } | |
3041 | ||
3c1668da | 3042 | _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { |
900952ec | 3043 | int r; |
3c1668da | 3044 | |
1ae464e0 TA |
3045 | assert_return(j, -EINVAL); |
3046 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3047 | assert_return(!isempty(field), -EINVAL); | |
3048 | assert_return(field_is_valid(field), -EINVAL); | |
3c1668da | 3049 | |
900952ec LP |
3050 | r = free_and_strdup(&j->unique_field, field); |
3051 | if (r < 0) | |
3052 | return r; | |
3c1668da | 3053 | |
3c1668da LP |
3054 | j->unique_file = NULL; |
3055 | j->unique_offset = 0; | |
360af4cf | 3056 | j->unique_file_lost = false; |
3c1668da LP |
3057 | |
3058 | return 0; | |
3059 | } | |
3060 | ||
0e0b0529 LP |
3061 | _public_ int sd_journal_enumerate_unique( |
3062 | sd_journal *j, | |
3063 | const void **ret_data, | |
3064 | size_t *ret_size) { | |
3065 | ||
3c1668da | 3066 | size_t k; |
19a2bd80 | 3067 | |
1ae464e0 TA |
3068 | assert_return(j, -EINVAL); |
3069 | assert_return(!journal_pid_changed(j), -ECHILD); | |
1ae464e0 | 3070 | assert_return(j->unique_field, -EINVAL); |
19a2bd80 | 3071 | |
3c1668da | 3072 | k = strlen(j->unique_field); |
19a2bd80 | 3073 | |
3c1668da | 3074 | if (!j->unique_file) { |
360af4cf ZJS |
3075 | if (j->unique_file_lost) |
3076 | return 0; | |
3077 | ||
c1f906bd | 3078 | j->unique_file = ordered_hashmap_first(j->files); |
3c1668da LP |
3079 | if (!j->unique_file) |
3080 | return 0; | |
360af4cf | 3081 | |
3c1668da LP |
3082 | j->unique_offset = 0; |
3083 | } | |
19a2bd80 | 3084 | |
3c1668da LP |
3085 | for (;;) { |
3086 | JournalFile *of; | |
ae97089d | 3087 | Object *o; |
0e35afff | 3088 | void *odata; |
3c1668da LP |
3089 | size_t ol; |
3090 | bool found; | |
ae97089d | 3091 | int r; |
3c1668da | 3092 | |
bdc02927 | 3093 | /* Proceed to next data object in the field's linked list */ |
3c1668da LP |
3094 | if (j->unique_offset == 0) { |
3095 | r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL); | |
3096 | if (r < 0) | |
3097 | return r; | |
3098 | ||
3099 | j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0; | |
3100 | } else { | |
3101 | r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o); | |
3102 | if (r < 0) | |
3103 | return r; | |
3104 | ||
3105 | j->unique_offset = le64toh(o->data.next_field_offset); | |
3106 | } | |
3107 | ||
3108 | /* We reached the end of the list? Then start again, with the next file */ | |
3109 | if (j->unique_offset == 0) { | |
c1f906bd | 3110 | j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path); |
360af4cf | 3111 | if (!j->unique_file) |
3c1668da LP |
3112 | return 0; |
3113 | ||
3c1668da LP |
3114 | continue; |
3115 | } | |
3116 | ||
d05089d8 MS |
3117 | /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED |
3118 | * instead, so that we can look at this data object at the same | |
3c1668da | 3119 | * time as one on another file */ |
d05089d8 | 3120 | r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o); |
3c1668da LP |
3121 | if (r < 0) |
3122 | return r; | |
3123 | ||
3124 | /* Let's do the type check by hand, since we used 0 context above. */ | |
baaa35ad ZJS |
3125 | if (o->object.type != OBJECT_DATA) |
3126 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
3127 | "%s:offset " OFSfmt ": object has type %d, expected %d", | |
3128 | j->unique_file->path, | |
3129 | j->unique_offset, | |
3130 | o->object.type, OBJECT_DATA); | |
ae97089d | 3131 | |
0e35afff DDM |
3132 | r = journal_file_data_payload(j->unique_file, o, j->unique_offset, NULL, 0, |
3133 | j->data_threshold, &odata, &ol); | |
3c1668da LP |
3134 | if (r < 0) |
3135 | return r; | |
3136 | ||
0f99f74a | 3137 | /* Check if we have at least the field name and "=". */ |
baaa35ad ZJS |
3138 | if (ol <= k) |
3139 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
3140 | "%s:offset " OFSfmt ": object has size %zu, expected at least %zu", | |
3141 | j->unique_file->path, | |
3142 | j->unique_offset, ol, k + 1); | |
3143 | ||
0e0b0529 | 3144 | if (memcmp(odata, j->unique_field, k) != 0 || ((const char*) odata)[k] != '=') |
baaa35ad ZJS |
3145 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
3146 | "%s:offset " OFSfmt ": object does not start with \"%s=\"", | |
3147 | j->unique_file->path, | |
3148 | j->unique_offset, | |
3149 | j->unique_field); | |
0f99f74a | 3150 | |
0e0b0529 LP |
3151 | /* OK, now let's see if we already returned this data object by checking if it exists in the |
3152 | * earlier traversed files. */ | |
3c1668da | 3153 | found = false; |
90e74a66 | 3154 | ORDERED_HASHMAP_FOREACH(of, j->files) { |
3c1668da LP |
3155 | if (of == j->unique_file) |
3156 | break; | |
3157 | ||
ed71f956 LP |
3158 | /* Skip this file it didn't have any fields indexed */ |
3159 | if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0) | |
3c1668da LP |
3160 | continue; |
3161 | ||
2e1a8a5d LP |
3162 | /* We can reuse the hash from our current file only on old-style journal files |
3163 | * without keyed hashes. On new-style files we have to calculate the hash anew, to | |
3164 | * take the per-file hash seed into consideration. */ | |
3165 | if (!JOURNAL_HEADER_KEYED_HASH(j->unique_file->header) && !JOURNAL_HEADER_KEYED_HASH(of->header)) | |
3166 | r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL); | |
3167 | else | |
3168 | r = journal_file_find_data_object(of, odata, ol, NULL, NULL); | |
3c1668da LP |
3169 | if (r < 0) |
3170 | return r; | |
ed71f956 | 3171 | if (r > 0) { |
3c1668da | 3172 | found = true; |
ed71f956 LP |
3173 | break; |
3174 | } | |
3c1668da LP |
3175 | } |
3176 | ||
06cc69d4 JJ |
3177 | if (found) |
3178 | continue; | |
3179 | ||
0e35afff DDM |
3180 | *ret_data = odata; |
3181 | *ret_size = ol; | |
3c1668da LP |
3182 | |
3183 | return 1; | |
3184 | } | |
3185 | } | |
3186 | ||
76cbafcd ZJS |
3187 | _public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **data, size_t *size) { |
3188 | for (;;) { | |
3189 | int r; | |
3190 | ||
3191 | r = sd_journal_enumerate_unique(j, data, size); | |
3192 | if (r >= 0) | |
3193 | return r; | |
3194 | if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r)) | |
3195 | return r; | |
3196 | /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try | |
3197 | * we will access the next field. */ | |
3198 | } | |
3199 | } | |
3200 | ||
115646c7 | 3201 | _public_ void sd_journal_restart_unique(sd_journal *j) { |
3c1668da LP |
3202 | if (!j) |
3203 | return; | |
3204 | ||
3205 | j->unique_file = NULL; | |
3206 | j->unique_offset = 0; | |
360af4cf | 3207 | j->unique_file_lost = false; |
3c1668da | 3208 | } |
85210bff | 3209 | |
eb86030e LP |
3210 | _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) { |
3211 | int r; | |
3212 | ||
3213 | assert_return(j, -EINVAL); | |
3214 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3215 | assert_return(field, -EINVAL); | |
3216 | ||
3217 | if (!j->fields_file) { | |
3218 | if (j->fields_file_lost) | |
3219 | return 0; | |
3220 | ||
3221 | j->fields_file = ordered_hashmap_first(j->files); | |
3222 | if (!j->fields_file) | |
3223 | return 0; | |
3224 | ||
3225 | j->fields_hash_table_index = 0; | |
3226 | j->fields_offset = 0; | |
3227 | } | |
3228 | ||
3229 | for (;;) { | |
3230 | JournalFile *f, *of; | |
eb86030e LP |
3231 | uint64_t m; |
3232 | Object *o; | |
3233 | size_t sz; | |
3234 | bool found; | |
3235 | ||
3236 | f = j->fields_file; | |
3237 | ||
3238 | if (j->fields_offset == 0) { | |
3239 | bool eof = false; | |
3240 | ||
3241 | /* We are not yet positioned at any field. Let's pick the first one */ | |
3242 | r = journal_file_map_field_hash_table(f); | |
3243 | if (r < 0) | |
3244 | return r; | |
3245 | ||
3246 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); | |
3247 | for (;;) { | |
3248 | if (j->fields_hash_table_index >= m) { | |
3249 | /* Reached the end of the hash table, go to the next file. */ | |
3250 | eof = true; | |
3251 | break; | |
3252 | } | |
3253 | ||
3254 | j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset); | |
3255 | ||
3256 | if (j->fields_offset != 0) | |
3257 | break; | |
3258 | ||
3259 | /* Empty hash table bucket, go to next one */ | |
3260 | j->fields_hash_table_index++; | |
3261 | } | |
3262 | ||
3263 | if (eof) { | |
3264 | /* Proceed with next file */ | |
3265 | j->fields_file = ordered_hashmap_next(j->files, f->path); | |
3266 | if (!j->fields_file) { | |
3267 | *field = NULL; | |
3268 | return 0; | |
3269 | } | |
3270 | ||
3271 | j->fields_offset = 0; | |
3272 | j->fields_hash_table_index = 0; | |
3273 | continue; | |
3274 | } | |
3275 | ||
3276 | } else { | |
3277 | /* We are already positioned at a field. If so, let's figure out the next field from it */ | |
3278 | ||
3279 | r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o); | |
3280 | if (r < 0) | |
3281 | return r; | |
3282 | ||
3283 | j->fields_offset = le64toh(o->field.next_hash_offset); | |
3284 | if (j->fields_offset == 0) { | |
3285 | /* Reached the end of the hash table chain */ | |
3286 | j->fields_hash_table_index++; | |
3287 | continue; | |
3288 | } | |
3289 | } | |
3290 | ||
1f133e0d | 3291 | /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */ |
eb86030e LP |
3292 | r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o); |
3293 | if (r < 0) | |
3294 | return r; | |
3295 | ||
3296 | /* Because we used OBJECT_UNUSED above, we need to do our type check manually */ | |
baaa35ad ZJS |
3297 | if (o->object.type != OBJECT_FIELD) |
3298 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
3299 | "%s:offset " OFSfmt ": object has type %i, expected %i", | |
3300 | f->path, j->fields_offset, | |
3301 | o->object.type, OBJECT_FIELD); | |
eb86030e LP |
3302 | |
3303 | sz = le64toh(o->object.size) - offsetof(Object, field.payload); | |
3304 | ||
3305 | /* Let's see if we already returned this field name before. */ | |
3306 | found = false; | |
90e74a66 | 3307 | ORDERED_HASHMAP_FOREACH(of, j->files) { |
eb86030e LP |
3308 | if (of == f) |
3309 | break; | |
3310 | ||
3311 | /* Skip this file it didn't have any fields indexed */ | |
3312 | if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0) | |
3313 | continue; | |
3314 | ||
27bf0ab7 DDM |
3315 | if (!JOURNAL_HEADER_KEYED_HASH(f->header) && !JOURNAL_HEADER_KEYED_HASH(of->header)) |
3316 | r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, | |
3317 | le64toh(o->field.hash), NULL, NULL); | |
3318 | else | |
3319 | r = journal_file_find_field_object(of, o->field.payload, sz, NULL, NULL); | |
eb86030e LP |
3320 | if (r < 0) |
3321 | return r; | |
3322 | if (r > 0) { | |
3323 | found = true; | |
3324 | break; | |
3325 | } | |
3326 | } | |
3327 | ||
3328 | if (found) | |
3329 | continue; | |
3330 | ||
3331 | /* Check if this is really a valid string containing no NUL byte */ | |
3332 | if (memchr(o->field.payload, 0, sz)) | |
3333 | return -EBADMSG; | |
3334 | ||
adbd80f5 | 3335 | if (j->data_threshold > 0 && sz > j->data_threshold) |
eb86030e LP |
3336 | sz = j->data_threshold; |
3337 | ||
319a4f4b | 3338 | if (!GREEDY_REALLOC(j->fields_buffer, sz + 1)) |
eb86030e LP |
3339 | return -ENOMEM; |
3340 | ||
3341 | memcpy(j->fields_buffer, o->field.payload, sz); | |
3342 | j->fields_buffer[sz] = 0; | |
3343 | ||
3344 | if (!field_is_valid(j->fields_buffer)) | |
3345 | return -EBADMSG; | |
3346 | ||
3347 | *field = j->fields_buffer; | |
3348 | return 1; | |
3349 | } | |
3350 | } | |
3351 | ||
3352 | _public_ void sd_journal_restart_fields(sd_journal *j) { | |
3353 | if (!j) | |
3354 | return; | |
3355 | ||
3356 | j->fields_file = NULL; | |
3357 | j->fields_hash_table_index = 0; | |
3358 | j->fields_offset = 0; | |
3359 | j->fields_file_lost = false; | |
3360 | } | |
3361 | ||
85210bff | 3362 | _public_ int sd_journal_reliable_fd(sd_journal *j) { |
1ae464e0 TA |
3363 | assert_return(j, -EINVAL); |
3364 | assert_return(!journal_pid_changed(j), -ECHILD); | |
85210bff LP |
3365 | |
3366 | return !j->on_network; | |
3367 | } | |
d4205751 LP |
3368 | |
3369 | static char *lookup_field(const char *field, void *userdata) { | |
99534007 | 3370 | sd_journal *j = ASSERT_PTR(userdata); |
d4205751 LP |
3371 | const void *data; |
3372 | size_t size, d; | |
3373 | int r; | |
3374 | ||
3375 | assert(field); | |
d4205751 LP |
3376 | |
3377 | r = sd_journal_get_data(j, field, &data, &size); | |
3378 | if (r < 0 || | |
3379 | size > REPLACE_VAR_MAX) | |
3380 | return strdup(field); | |
3381 | ||
3382 | d = strlen(field) + 1; | |
3383 | ||
3384 | return strndup((const char*) data + d, size - d); | |
3385 | } | |
3386 | ||
3387 | _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) { | |
3388 | const void *data; | |
3389 | size_t size; | |
3390 | sd_id128_t id; | |
3391 | _cleanup_free_ char *text = NULL, *cid = NULL; | |
3392 | char *t; | |
3393 | int r; | |
3394 | ||
1ae464e0 TA |
3395 | assert_return(j, -EINVAL); |
3396 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3397 | assert_return(ret, -EINVAL); | |
d4205751 LP |
3398 | |
3399 | r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size); | |
3400 | if (r < 0) | |
3401 | return r; | |
3402 | ||
3403 | cid = strndup((const char*) data + 11, size - 11); | |
3404 | if (!cid) | |
3405 | return -ENOMEM; | |
3406 | ||
3407 | r = sd_id128_from_string(cid, &id); | |
3408 | if (r < 0) | |
3409 | return r; | |
3410 | ||
844ec79b | 3411 | r = catalog_get(CATALOG_DATABASE, id, &text); |
d4205751 LP |
3412 | if (r < 0) |
3413 | return r; | |
3414 | ||
3415 | t = replace_var(text, lookup_field, j); | |
3416 | if (!t) | |
3417 | return -ENOMEM; | |
3418 | ||
3419 | *ret = t; | |
3420 | return 0; | |
3421 | } | |
8f1e860f LP |
3422 | |
3423 | _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) { | |
1ae464e0 | 3424 | assert_return(ret, -EINVAL); |
8f1e860f | 3425 | |
844ec79b | 3426 | return catalog_get(CATALOG_DATABASE, id, ret); |
8f1e860f | 3427 | } |
93b73b06 LP |
3428 | |
3429 | _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) { | |
1ae464e0 TA |
3430 | assert_return(j, -EINVAL); |
3431 | assert_return(!journal_pid_changed(j), -ECHILD); | |
93b73b06 LP |
3432 | |
3433 | j->data_threshold = sz; | |
3434 | return 0; | |
3435 | } | |
3436 | ||
3437 | _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) { | |
1ae464e0 TA |
3438 | assert_return(j, -EINVAL); |
3439 | assert_return(!journal_pid_changed(j), -ECHILD); | |
3440 | assert_return(sz, -EINVAL); | |
93b73b06 LP |
3441 | |
3442 | *sz = j->data_threshold; | |
3443 | return 0; | |
3444 | } | |
39fd5b08 JS |
3445 | |
3446 | _public_ int sd_journal_has_runtime_files(sd_journal *j) { | |
3447 | assert_return(j, -EINVAL); | |
3448 | ||
3449 | return j->has_runtime_files; | |
3450 | } | |
3451 | ||
3452 | _public_ int sd_journal_has_persistent_files(sd_journal *j) { | |
3453 | assert_return(j, -EINVAL); | |
3454 | ||
3455 | return j->has_persistent_files; | |
3456 | } |