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