X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fsd-journal.c;h=b55cf37e50d93c609b0726e065d5bab4d7a990c2;hb=0c24bb2346b6b6232d67aacd5236b56ea4989de4;hp=3ccb14a242832fa2b4d0e0c903313d40004bed16;hpb=71fda00f320379f5cbee8e118848de98caaa229d;p=elogind.git diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index 3ccb14a24..b55cf37e5 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -41,6 +41,7 @@ #include "missing.h" #include "catalog.h" #include "replace-var.h" +#include "fileio.h" #define JOURNAL_FILES_MAX 1024 @@ -216,19 +217,14 @@ _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) Match *l3, *l4, *add_here = NULL, *m; le64_t le_hash; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - - if (!data) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(data, -EINVAL); if (size == 0) size = strlen(data); - if (!match_is_valid(data, size)) - return -EINVAL; + assert_return(match_is_valid(data, size), -EINVAL); /* level 0: AND term * level 1: OR terms @@ -314,10 +310,8 @@ fail: } _public_ int sd_journal_add_conjunction(sd_journal *j) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); if (!j->level0) return 0; @@ -335,10 +329,8 @@ _public_ int sd_journal_add_conjunction(sd_journal *j) { } _public_ int sd_journal_add_disjunction(sd_journal *j) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); if (!j->level0) return 0; @@ -884,10 +876,8 @@ static int real_journal_next(sd_journal *j, direction_t direction) { Iterator i; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); HASHMAP_FOREACH(f, j->files, i) { bool found; @@ -938,10 +928,8 @@ _public_ int sd_journal_previous(sd_journal *j) { static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) { int c = 0, r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); if (skip == 0) { /* If this is not a discrete skip, then at least @@ -980,12 +968,9 @@ _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) { int r; char bid[33], sid[33]; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!cursor) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(cursor, -EINVAL); if (!j->current_file || j->current_file->current_offset <= 0) return -EADDRNOTAVAIL; @@ -1021,12 +1006,9 @@ _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) { xor_hash_set = false; sd_id128_t seqnum_id, boot_id; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (isempty(cursor)) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(!isempty(cursor), -EINVAL); FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) { char *item; @@ -1122,12 +1104,9 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) { size_t l; Object *o; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (isempty(cursor)) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(!isempty(cursor), -EINVAL); if (!j->current_file || j->current_file->current_offset <= 0) return -EADDRNOTAVAIL; @@ -1202,10 +1181,8 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) { _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); reset_location(j); j->current_location.type = LOCATION_SEEK; @@ -1217,10 +1194,8 @@ _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, u } _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); reset_location(j); j->current_location.type = LOCATION_SEEK; @@ -1231,10 +1206,8 @@ _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) { } _public_ int sd_journal_seek_head(sd_journal *j) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); reset_location(j); j->current_location.type = LOCATION_HEAD; @@ -1243,10 +1216,8 @@ _public_ int sd_journal_seek_head(sd_journal *j) { } _public_ int sd_journal_seek_tail(sd_journal *j) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); reset_location(j); j->current_location.type = LOCATION_TAIL; @@ -1276,7 +1247,7 @@ static void check_network(sd_journal *j, int fd) { static bool file_has_type_prefix(const char *prefix, const char *filename) { const char *full, *tilded, *atted; - full = strappend(prefix, ".journal"); + full = strappenda(prefix, ".journal"); tilded = strappenda(full, "~"); atted = strappenda(prefix, "@"); @@ -1495,7 +1466,7 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname) return 0; } -static int add_root_directory(sd_journal *j, const char *p) { +static int add_root_directory(sd_journal *j, const char *p, const char *prefix) { _cleanup_closedir_ DIR *d = NULL; Directory *m; int r; @@ -1507,6 +1478,9 @@ static int add_root_directory(sd_journal *j, const char *p) { !path_startswith(p, "/run")) return -EINVAL; + if (prefix) + p = strappenda(prefix, p); + d = opendir(p); if (!d) return -errno; @@ -1606,7 +1580,7 @@ static int remove_directory(sd_journal *j, Directory *d) { return 0; } -static int add_search_paths(sd_journal *j) { +static int add_search_paths(sd_journal *j, const char *prefix) { int r; const char search_paths[] = "/run/log/journal\0" @@ -1619,7 +1593,7 @@ static int add_search_paths(sd_journal *j) { * what's actually accessible, and ignore the rest. */ NULSTR_FOREACH(p, search_paths) { - r = add_root_directory(j, p); + r = add_root_directory(j, p, prefix); if (r < 0 && r != -ENOENT) { r = set_put_error(j, r); if (r < 0) @@ -1649,7 +1623,7 @@ static int add_current_paths(sd_journal *j) { if (!dir) return -ENOMEM; - r = add_root_directory(j, dir); + r = add_root_directory(j, dir, NULL); if (r < 0) { set_put_error(j, r); return r; @@ -1713,20 +1687,14 @@ _public_ int sd_journal_open(sd_journal **ret, int flags) { sd_journal *j; int r; - if (!ret) - return -EINVAL; - - if (flags & ~(SD_JOURNAL_LOCAL_ONLY| - SD_JOURNAL_RUNTIME_ONLY| - SD_JOURNAL_SYSTEM| - SD_JOURNAL_CURRENT_USER)) - return -EINVAL; + assert_return(ret, -EINVAL); + assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY|SD_JOURNAL_SYSTEM|SD_JOURNAL_CURRENT_USER)) == 0, -EINVAL); j = journal_new(flags, NULL); if (!j) return -ENOMEM; - r = add_search_paths(j); + r = add_search_paths(j, NULL); if (r < 0) goto fail; @@ -1739,24 +1707,58 @@ fail: return r; } -_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) { +_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) { + _cleanup_free_ char *root = NULL, *class = NULL; sd_journal *j; + char *p; int r; - if (!ret) - return -EINVAL; + assert_return(machine, -EINVAL); + assert_return(ret, -EINVAL); + assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL); + assert_return(filename_is_safe(machine), -EINVAL); - if (!path) - return -EINVAL; + p = strappenda("/run/systemd/machines/", machine); + r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL); + if (r == -ENOENT) + return -EHOSTDOWN; + if (r < 0) + return r; + if (!root) + return -ENODATA; - if (flags != 0) - return -EINVAL; + if (!streq_ptr(class, "container")) + return -EIO; + + j = journal_new(flags, NULL); + if (!j) + return -ENOMEM; + + r = add_search_paths(j, root); + if (r < 0) + goto fail; + + *ret = j; + return 0; + +fail: + sd_journal_close(j); + return r; +} + +_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) { + sd_journal *j; + int r; + + assert_return(ret, -EINVAL); + assert_return(path, -EINVAL); + assert_return(flags == 0, -EINVAL); j = journal_new(flags, path); if (!j) return -ENOMEM; - r = add_root_directory(j, path); + r = add_root_directory(j, path, NULL); if (r < 0) { set_put_error(j, r); goto fail; @@ -1776,11 +1778,8 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla const char **path; int r; - if (!ret) - return -EINVAL; - - if (flags != 0) - return -EINVAL; + assert_return(ret, -EINVAL); + assert_return(flags == 0, -EINVAL); j = journal_new(flags, NULL); if (!j) @@ -1831,8 +1830,10 @@ _public_ void sd_journal_close(sd_journal *j) { if (j->inotify_fd >= 0) close_nointr_nofail(j->inotify_fd); - if (j->mmap) + if (j->mmap) { + log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap)); mmap_cache_unref(j->mmap); + } free(j->path); free(j->unique_field); @@ -1845,12 +1846,9 @@ _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) { JournalFile *f; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!ret) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(ret, -EINVAL); f = j->current_file; if (!f) @@ -1873,10 +1871,8 @@ _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id12 int r; sd_id128_t id; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); f = j->current_file; if (!f) @@ -1941,19 +1937,12 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void ** int r; Object *o; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!field) - return -EINVAL; - if (!data) - return -EINVAL; - if (!size) - return -EINVAL; - - if (!field_is_valid(field)) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(field, -EINVAL); + assert_return(data, -EINVAL); + assert_return(size, -EINVAL); + assert_return(field_is_valid(field), -EINVAL); f = j->current_file; if (!f) @@ -2069,14 +2058,10 @@ _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t int r; Object *o; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!data) - return -EINVAL; - if (!size) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(data, -EINVAL); + assert_return(size, -EINVAL); f = j->current_file; if (!f) @@ -2121,10 +2106,8 @@ _public_ void sd_journal_restart_data(sd_journal *j) { _public_ int sd_journal_get_fd(sd_journal *j) { int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); if (j->inotify_fd >= 0) return j->inotify_fd; @@ -2138,9 +2121,9 @@ _public_ int sd_journal_get_fd(sd_journal *j) { if (j->no_new_files) r = add_current_paths(j); else if (j->path) - r = add_root_directory(j, j->path); + r = add_root_directory(j, j->path, NULL); else - r = add_search_paths(j); + r = add_search_paths(j, NULL); if (r < 0) return r; @@ -2150,10 +2133,8 @@ _public_ int sd_journal_get_fd(sd_journal *j) { _public_ int sd_journal_get_events(sd_journal *j) { int fd; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); fd = sd_journal_get_fd(j); if (fd < 0) @@ -2165,12 +2146,9 @@ _public_ int sd_journal_get_events(sd_journal *j) { _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) { int fd; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!timeout_usec) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(timeout_usec, -EINVAL); fd = sd_journal_get_fd(j); if (fd < 0) @@ -2267,10 +2245,8 @@ _public_ int sd_journal_process(sd_journal *j) { uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX] _alignas_(struct inotify_event); bool got_something = false; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); j->last_process_usec = now(CLOCK_MONOTONIC); @@ -2309,10 +2285,8 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) { int r; uint64_t t; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); if (j->inotify_fd < 0) { @@ -2359,14 +2333,10 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, bool first = true; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!from && !to) - return -EINVAL; - if (from == to) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(from || to, -EINVAL); + assert_return(from != to, -EINVAL); HASHMAP_FOREACH(f, j->files, i) { usec_t fr, t; @@ -2402,14 +2372,10 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot bool first = true; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!from && !to) - return -EINVAL; - if (from == to) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(from || to, -EINVAL); + assert_return(from != to, -EINVAL); HASHMAP_FOREACH(f, j->files, i) { usec_t fr, t; @@ -2461,12 +2427,9 @@ _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) { JournalFile *f; uint64_t sum = 0; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!bytes) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(bytes, -EINVAL); HASHMAP_FOREACH(f, j->files, i) { struct stat st; @@ -2484,14 +2447,10 @@ _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) { _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { char *f; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (isempty(field)) - return -EINVAL; - if (!field_is_valid(field)) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(!isempty(field), -EINVAL); + assert_return(field_is_valid(field), -EINVAL); f = strdup(field); if (!f) @@ -2510,16 +2469,11 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_ size_t k; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!data) - return -EINVAL; - if (!l) - return -EINVAL; - if (!j->unique_field) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(data, -EINVAL); + assert_return(l, -EINVAL); + assert_return(j->unique_field, -EINVAL); k = strlen(j->unique_field); @@ -2624,10 +2578,8 @@ _public_ void sd_journal_restart_unique(sd_journal *j) { } _public_ int sd_journal_reliable_fd(sd_journal *j) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); return !j->on_network; } @@ -2659,12 +2611,9 @@ _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) { char *t; int r; - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!ret) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(ret, -EINVAL); r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size); if (r < 0) @@ -2691,29 +2640,23 @@ _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) { } _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) { - if (!ret) - return -EINVAL; + assert_return(ret, -EINVAL); return catalog_get(CATALOG_DATABASE, id, ret); } _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); j->data_threshold = sz; return 0; } _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) { - if (!j) - return -EINVAL; - if (journal_pid_changed(j)) - return -ECHILD; - if (!sz) - return -EINVAL; + assert_return(j, -EINVAL); + assert_return(!journal_pid_changed(j), -ECHILD); + assert_return(sz, -EINVAL); *sz = j->data_threshold; return 0;