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