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