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