chiark / gitweb /
Remove src/journal
authorAndy Wingo <wingo@pobox.com>
Sun, 19 Apr 2015 11:28:46 +0000 (13:28 +0200)
committerAndy Wingo <wingo@pobox.com>
Sun, 19 Apr 2015 11:28:46 +0000 (13:28 +0200)
71 files changed:
src/journal/.gitignore [deleted file]
src/journal/Makefile [deleted symlink]
src/journal/cat.c [deleted file]
src/journal/catalog.c [deleted file]
src/journal/catalog.h [deleted file]
src/journal/compress.c [deleted file]
src/journal/compress.h [deleted file]
src/journal/coredump-vacuum.c [deleted file]
src/journal/coredump-vacuum.h [deleted file]
src/journal/coredump.c [deleted file]
src/journal/coredump.conf [deleted file]
src/journal/coredumpctl.c [deleted file]
src/journal/fsprg.c [deleted file]
src/journal/fsprg.h [deleted file]
src/journal/journal-authenticate.c [deleted file]
src/journal/journal-authenticate.h [deleted file]
src/journal/journal-def.h [deleted file]
src/journal/journal-file.c [deleted file]
src/journal/journal-file.h [deleted file]
src/journal/journal-internal.h [deleted file]
src/journal/journal-qrcode.c [deleted file]
src/journal/journal-qrcode.h [deleted file]
src/journal/journal-send.c [deleted file]
src/journal/journal-vacuum.c [deleted file]
src/journal/journal-vacuum.h [deleted file]
src/journal/journal-verify.c [deleted file]
src/journal/journal-verify.h [deleted file]
src/journal/journalctl.c [deleted file]
src/journal/journald-audit.c [deleted file]
src/journal/journald-audit.h [deleted file]
src/journal/journald-console.c [deleted file]
src/journal/journald-console.h [deleted file]
src/journal/journald-gperf.gperf [deleted file]
src/journal/journald-kmsg.c [deleted file]
src/journal/journald-kmsg.h [deleted file]
src/journal/journald-native.c [deleted file]
src/journal/journald-native.h [deleted file]
src/journal/journald-rate-limit.c [deleted file]
src/journal/journald-rate-limit.h [deleted file]
src/journal/journald-server.c [deleted file]
src/journal/journald-server.h [deleted file]
src/journal/journald-stream.c [deleted file]
src/journal/journald-stream.h [deleted file]
src/journal/journald-syslog.c [deleted file]
src/journal/journald-syslog.h [deleted file]
src/journal/journald-wall.c [deleted file]
src/journal/journald-wall.h [deleted file]
src/journal/journald.c [deleted file]
src/journal/journald.conf [deleted file]
src/journal/lookup3.c [deleted file]
src/journal/lookup3.h [deleted file]
src/journal/mmap-cache.c [deleted file]
src/journal/mmap-cache.h [deleted file]
src/journal/sd-journal.c [deleted file]
src/journal/stacktrace.c [deleted file]
src/journal/stacktrace.h [deleted file]
src/journal/test-catalog.c [deleted file]
src/journal/test-compress-benchmark.c [deleted file]
src/journal/test-compress.c [deleted file]
src/journal/test-coredump-vacuum.c [deleted file]
src/journal/test-journal-enum.c [deleted file]
src/journal/test-journal-flush.c [deleted file]
src/journal/test-journal-init.c [deleted file]
src/journal/test-journal-interleaving.c [deleted file]
src/journal/test-journal-match.c [deleted file]
src/journal/test-journal-send.c [deleted file]
src/journal/test-journal-stream.c [deleted file]
src/journal/test-journal-syslog.c [deleted file]
src/journal/test-journal-verify.c [deleted file]
src/journal/test-journal.c [deleted file]
src/journal/test-mmap-cache.c [deleted file]

diff --git a/src/journal/.gitignore b/src/journal/.gitignore
deleted file mode 100644 (file)
index d6a7946..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-/journald-gperf.c
-/libsystemd-journal.pc
diff --git a/src/journal/Makefile b/src/journal/Makefile
deleted file mode 120000 (symlink)
index d0b0e8e..0000000
+++ /dev/null
@@ -1 +0,0 @@
-../Makefile
\ No newline at end of file
diff --git a/src/journal/cat.c b/src/journal/cat.c
deleted file mode 100644 (file)
index 2e236f0..0000000
+++ /dev/null
@@ -1,173 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdio.h>
-#include <getopt.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <fcntl.h>
-
-#include "systemd/sd-journal.h"
-
-#include "util.h"
-#include "build.h"
-
-static char *arg_identifier = NULL;
-static int arg_priority = LOG_INFO;
-static bool arg_level_prefix = true;
-
-static void help(void) {
-        printf("%s [OPTIONS...] {COMMAND} ...\n\n"
-               "Execute process with stdout/stderr connected to the journal.\n\n"
-               "  -h --help               Show this help\n"
-               "     --version            Show package version\n"
-               "  -t --identifier=STRING  Set syslog identifier\n"
-               "  -p --priority=PRIORITY  Set priority value (0..7)\n"
-               "     --level-prefix=BOOL  Control whether level prefix shall be parsed\n"
-               , program_invocation_short_name);
-}
-
-static int parse_argv(int argc, char *argv[]) {
-
-        enum {
-                ARG_VERSION = 0x100,
-                ARG_LEVEL_PREFIX
-        };
-
-        static const struct option options[] = {
-                { "help",         no_argument,       NULL, 'h'              },
-                { "version",      no_argument,       NULL, ARG_VERSION      },
-                { "identifier",   required_argument, NULL, 't'              },
-                { "priority",     required_argument, NULL, 'p'              },
-                { "level-prefix", required_argument, NULL, ARG_LEVEL_PREFIX },
-                {}
-        };
-
-        int c;
-
-        assert(argc >= 0);
-        assert(argv);
-
-        while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
-
-                switch (c) {
-
-                case 'h':
-                        help();
-                        return 0;
-
-                case ARG_VERSION:
-                        puts(PACKAGE_STRING);
-                        puts(SYSTEMD_FEATURES);
-                        return 0;
-
-                case 't':
-                        free(arg_identifier);
-                        if (isempty(optarg))
-                                arg_identifier = NULL;
-                        else {
-                                arg_identifier = strdup(optarg);
-                                if (!arg_identifier)
-                                        return log_oom();
-                        }
-                        break;
-
-                case 'p':
-                        arg_priority = log_level_from_string(optarg);
-                        if (arg_priority < 0) {
-                                log_error("Failed to parse priority value.");
-                                return arg_priority;
-                        }
-                        break;
-
-                case ARG_LEVEL_PREFIX: {
-                        int k;
-
-                        k = parse_boolean(optarg);
-                        if (k < 0) {
-                                log_error("Failed to parse level prefix value.");
-                                return k;
-                        }
-                        arg_level_prefix = k;
-                        break;
-                }
-
-                case '?':
-                        return -EINVAL;
-
-                default:
-                        assert_not_reached("Unhandled option");
-                }
-
-        return 1;
-}
-
-int main(int argc, char *argv[]) {
-        int r, fd = -1, saved_stderr = -1;
-
-        log_parse_environment();
-        log_open();
-
-        r = parse_argv(argc, argv);
-        if (r <= 0)
-                goto finish;
-
-        fd = sd_journal_stream_fd(arg_identifier, arg_priority, arg_level_prefix);
-        if (fd < 0) {
-                log_error_errno(fd, "Failed to create stream fd: %m");
-                r = fd;
-                goto finish;
-        }
-
-        saved_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
-
-        if (dup3(fd, STDOUT_FILENO, 0) < 0 ||
-            dup3(fd, STDERR_FILENO, 0) < 0) {
-                log_error_errno(errno, "Failed to duplicate fd: %m");
-                r = -errno;
-                goto finish;
-        }
-
-        if (fd >= 3)
-                safe_close(fd);
-
-        fd = -1;
-
-        if (argc <= optind)
-                execl("/bin/cat", "/bin/cat", NULL);
-        else
-                execvp(argv[optind], argv + optind);
-
-        r = -errno;
-
-        /* Let's try to restore a working stderr, so we can print the error message */
-        if (saved_stderr >= 0)
-                dup3(saved_stderr, STDERR_FILENO, 0);
-
-        log_error_errno(r, "Failed to execute process: %m");
-
-finish:
-        safe_close(fd);
-        safe_close(saved_stderr);
-
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
-}
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
deleted file mode 100644 (file)
index 0801e13..0000000
+++ /dev/null
@@ -1,703 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <locale.h>
-
-#include "util.h"
-#include "log.h"
-#include "sparse-endian.h"
-#include "sd-id128.h"
-#include "hashmap.h"
-#include "strv.h"
-#include "strbuf.h"
-#include "conf-files.h"
-#include "mkdir.h"
-#include "catalog.h"
-#include "siphash24.h"
-
-const char * const catalog_file_dirs[] = {
-        "/usr/local/lib/systemd/catalog/",
-        "/usr/lib/systemd/catalog/",
-        NULL
-};
-
-#define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
-
-typedef struct CatalogHeader {
-        uint8_t signature[8];  /* "RHHHKSLP" */
-        le32_t compatible_flags;
-        le32_t incompatible_flags;
-        le64_t header_size;
-        le64_t n_items;
-        le64_t catalog_item_size;
-} CatalogHeader;
-
-typedef struct CatalogItem {
-        sd_id128_t id;
-        char language[32];
-        le64_t offset;
-} CatalogItem;
-
-static unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
-        const CatalogItem *i = p;
-        uint64_t u;
-        size_t l, sz;
-        void *v;
-
-        l = strlen(i->language);
-        sz = sizeof(i->id) + l;
-        v = alloca(sz);
-
-        memcpy(mempcpy(v, &i->id, sizeof(i->id)), i->language, l);
-
-        siphash24((uint8_t*) &u, v, sz, hash_key);
-
-        return (unsigned long) u;
-}
-
-static int catalog_compare_func(const void *a, const void *b) {
-        const CatalogItem *i = a, *j = b;
-        unsigned k;
-
-        for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) {
-                 if (i->id.bytes[k] < j->id.bytes[k])
-                        return -1;
-                 if (i->id.bytes[k] > j->id.bytes[k])
-                        return 1;
-        }
-
-        return strcmp(i->language, j->language);
-}
-
-const struct hash_ops catalog_hash_ops = {
-        .hash = catalog_hash_func,
-        .compare = catalog_compare_func
-};
-
-static int finish_item(
-                Hashmap *h,
-                struct strbuf *sb,
-                sd_id128_t id,
-                const char *language,
-                const char *payload) {
-
-        ssize_t offset;
-        _cleanup_free_ CatalogItem *i = NULL;
-        int r;
-
-        assert(h);
-        assert(sb);
-        assert(payload);
-
-        offset = strbuf_add_string(sb, payload, strlen(payload));
-        if (offset < 0)
-                return log_oom();
-
-        i = new0(CatalogItem, 1);
-        if (!i)
-                return log_oom();
-
-        i->id = id;
-        if (language) {
-                assert(strlen(language) > 1 && strlen(language) < 32);
-                strcpy(i->language, language);
-        }
-        i->offset = htole64((uint64_t) offset);
-
-        r = hashmap_put(h, i, i);
-        if (r == -EEXIST) {
-                log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
-                            SD_ID128_FORMAT_VAL(id), language ? language : "C");
-                return 0;
-        } else if (r < 0)
-                return r;
-
-        i = NULL;
-        return 0;
-}
-
-int catalog_file_lang(const char* filename, char **lang) {
-        char *beg, *end, *_lang;
-
-        end = endswith(filename, ".catalog");
-        if (!end)
-                return 0;
-
-        beg = end - 1;
-        while (beg > filename && *beg != '.' && *beg != '/' && end - beg < 32)
-                beg --;
-
-        if (*beg != '.' || end <= beg + 1)
-                return 0;
-
-        _lang = strndup(beg + 1, end - beg - 1);
-        if (!_lang)
-                return -ENOMEM;
-
-        *lang = _lang;
-        return 1;
-}
-
-static int catalog_entry_lang(const char* filename, int line,
-                              const char* t, const char* deflang, char **lang) {
-        size_t c;
-
-        c = strlen(t);
-        if (c == 0) {
-                log_error("[%s:%u] Language too short.", filename, line);
-                return -EINVAL;
-        }
-        if (c > 31) {
-                log_error("[%s:%u] language too long.", filename, line);
-                return -EINVAL;
-        }
-
-        if (deflang) {
-                if (streq(t, deflang)) {
-                        log_warning("[%s:%u] language specified unnecessarily",
-                                    filename, line);
-                        return 0;
-                } else
-                        log_warning("[%s:%u] language differs from default for file",
-                                    filename, line);
-        }
-
-        *lang = strdup(t);
-        if (!*lang)
-                        return -ENOMEM;
-
-        return 0;
-}
-
-int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *payload = NULL;
-        unsigned n = 0;
-        sd_id128_t id;
-        _cleanup_free_ char *deflang = NULL, *lang = NULL;
-        bool got_id = false, empty_line = true;
-        int r;
-
-        assert(h);
-        assert(sb);
-        assert(path);
-
-        f = fopen(path, "re");
-        if (!f)
-                return log_error_errno(errno, "Failed to open file %s: %m", path);
-
-        r = catalog_file_lang(path, &deflang);
-        if (r < 0)
-                log_error_errno(errno, "Failed to determine language for file %s: %m", path);
-        if (r == 1)
-                log_debug("File %s has language %s.", path, deflang);
-
-        for (;;) {
-                char line[LINE_MAX];
-                size_t a, b, c;
-                char *t;
-
-                if (!fgets(line, sizeof(line), f)) {
-                        if (feof(f))
-                                break;
-
-                        log_error_errno(errno, "Failed to read file %s: %m", path);
-                        return -errno;
-                }
-
-                n++;
-
-                truncate_nl(line);
-
-                if (line[0] == 0) {
-                        empty_line = true;
-                        continue;
-                }
-
-                if (strchr(COMMENTS "\n", line[0]))
-                        continue;
-
-                if (empty_line &&
-                    strlen(line) >= 2+1+32 &&
-                    line[0] == '-' &&
-                    line[1] == '-' &&
-                    line[2] == ' ' &&
-                    (line[2+1+32] == ' ' || line[2+1+32] == '\0')) {
-
-                        bool with_language;
-                        sd_id128_t jd;
-
-                        /* New entry */
-
-                        with_language = line[2+1+32] != '\0';
-                        line[2+1+32] = '\0';
-
-                        if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
-
-                                if (got_id) {
-                                        r = finish_item(h, sb, id, lang ?: deflang, payload);
-                                        if (r < 0)
-                                                return r;
-
-                                        free(lang);
-                                        lang = NULL;
-                                }
-
-                                if (with_language) {
-                                        t = strstrip(line + 2 + 1 + 32 + 1);
-
-                                        r = catalog_entry_lang(path, n, t, deflang, &lang);
-                                        if (r < 0)
-                                                return r;
-                                }
-
-                                got_id = true;
-                                empty_line = false;
-                                id = jd;
-
-                                if (payload)
-                                        payload[0] = '\0';
-
-                                continue;
-                        }
-                }
-
-                /* Payload */
-                if (!got_id) {
-                        log_error("[%s:%u] Got payload before ID.", path, n);
-                        return -EINVAL;
-                }
-
-                a = payload ? strlen(payload) : 0;
-                b = strlen(line);
-
-                c = a + (empty_line ? 1 : 0) + b + 1 + 1;
-                t = realloc(payload, c);
-                if (!t)
-                        return log_oom();
-
-                if (empty_line) {
-                        t[a] = '\n';
-                        memcpy(t + a + 1, line, b);
-                        t[a+b+1] = '\n';
-                        t[a+b+2] = 0;
-                } else {
-                        memcpy(t + a, line, b);
-                        t[a+b] = '\n';
-                        t[a+b+1] = 0;
-                }
-
-                payload = t;
-                empty_line = false;
-        }
-
-        if (got_id) {
-                r = finish_item(h, sb, id, lang ?: deflang, payload);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
-}
-
-static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb,
-                          CatalogItem *items, size_t n) {
-        CatalogHeader header;
-        _cleanup_fclose_ FILE *w = NULL;
-        int r;
-        _cleanup_free_ char *d, *p = NULL;
-        size_t k;
-
-        d = dirname_malloc(database);
-        if (!d)
-                return log_oom();
-
-        r = mkdir_p(d, 0775);
-        if (r < 0)
-                return log_error_errno(r, "Recursive mkdir %s: %m", d);
-
-        r = fopen_temporary(database, &w, &p);
-        if (r < 0)
-                return log_error_errno(r, "Failed to open database for writing: %s: %m",
-                                       database);
-
-        zero(header);
-        memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature));
-        header.header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8));
-        header.catalog_item_size = htole64(sizeof(CatalogItem));
-        header.n_items = htole64(hashmap_size(h));
-
-        r = -EIO;
-
-        k = fwrite(&header, 1, sizeof(header), w);
-        if (k != sizeof(header)) {
-                log_error("%s: failed to write header.", p);
-                goto error;
-        }
-
-        k = fwrite(items, 1, n * sizeof(CatalogItem), w);
-        if (k != n * sizeof(CatalogItem)) {
-                log_error("%s: failed to write database.", p);
-                goto error;
-        }
-
-        k = fwrite(sb->buf, 1, sb->len, w);
-        if (k != sb->len) {
-                log_error("%s: failed to write strings.", p);
-                goto error;
-        }
-
-        fflush(w);
-
-        if (ferror(w)) {
-                log_error("%s: failed to write database.", p);
-                goto error;
-        }
-
-        fchmod(fileno(w), 0644);
-
-        if (rename(p, database) < 0) {
-                log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
-                r = -errno;
-                goto error;
-        }
-
-        return ftell(w);
-
-error:
-        unlink(p);
-        return r;
-}
-
-int catalog_update(const char* database, const char* root, const char* const* dirs) {
-        _cleanup_strv_free_ char **files = NULL;
-        char **f;
-        struct strbuf *sb = NULL;
-        _cleanup_hashmap_free_free_ Hashmap *h = NULL;
-        _cleanup_free_ CatalogItem *items = NULL;
-        CatalogItem *i;
-        Iterator j;
-        unsigned n;
-        long r;
-
-        h = hashmap_new(&catalog_hash_ops);
-        sb = strbuf_new();
-
-        if (!h || !sb) {
-                r = log_oom();
-                goto finish;
-        }
-
-        r = conf_files_list_strv(&files, ".catalog", root, dirs);
-        if (r < 0) {
-                log_error_errno(r, "Failed to get catalog files: %m");
-                goto finish;
-        }
-
-        STRV_FOREACH(f, files) {
-                log_debug("Reading file '%s'", *f);
-                r = catalog_import_file(h, sb, *f);
-                if (r < 0) {
-                        log_error("Failed to import file '%s': %s.",
-                                  *f, strerror(-r));
-                        goto finish;
-                }
-        }
-
-        if (hashmap_size(h) <= 0) {
-                log_info("No items in catalog.");
-                goto finish;
-        } else
-                log_debug("Found %u items in catalog.", hashmap_size(h));
-
-        strbuf_complete(sb);
-
-        items = new(CatalogItem, hashmap_size(h));
-        if (!items) {
-                r = log_oom();
-                goto finish;
-        }
-
-        n = 0;
-        HASHMAP_FOREACH(i, h, j) {
-                log_debug("Found " SD_ID128_FORMAT_STR ", language %s",
-                          SD_ID128_FORMAT_VAL(i->id),
-                          isempty(i->language) ? "C" : i->language);
-                items[n++] = *i;
-        }
-
-        assert(n == hashmap_size(h));
-        qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
-
-        r = write_catalog(database, h, sb, items, n);
-        if (r < 0)
-                log_error_errno(r, "Failed to write %s: %m", database);
-        else
-                log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
-                          database, n, sb->len, r);
-
-finish:
-        if (sb)
-                strbuf_cleanup(sb);
-
-        return r < 0 ? r : 0;
-}
-
-static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) {
-        const CatalogHeader *h;
-        int fd;
-        void *p;
-        struct stat st;
-
-        assert(_fd);
-        assert(_st);
-        assert(_p);
-
-        fd = open(database, O_RDONLY|O_CLOEXEC);
-        if (fd < 0)
-                return -errno;
-
-        if (fstat(fd, &st) < 0) {
-                safe_close(fd);
-                return -errno;
-        }
-
-        if (st.st_size < (off_t) sizeof(CatalogHeader)) {
-                safe_close(fd);
-                return -EINVAL;
-        }
-
-        p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
-        if (p == MAP_FAILED) {
-                safe_close(fd);
-                return -errno;
-        }
-
-        h = p;
-        if (memcmp(h->signature, CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
-            le64toh(h->header_size) < sizeof(CatalogHeader) ||
-            le64toh(h->catalog_item_size) < sizeof(CatalogItem) ||
-            h->incompatible_flags != 0 ||
-            le64toh(h->n_items) <= 0 ||
-            st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
-                safe_close(fd);
-                munmap(p, st.st_size);
-                return -EBADMSG;
-        }
-
-        *_fd = fd;
-        *_st = st;
-        *_p = p;
-
-        return 0;
-}
-
-static const char *find_id(void *p, sd_id128_t id) {
-        CatalogItem key, *f = NULL;
-        const CatalogHeader *h = p;
-        const char *loc;
-
-        zero(key);
-        key.id = id;
-
-        loc = setlocale(LC_MESSAGES, NULL);
-        if (loc && loc[0] && !streq(loc, "C") && !streq(loc, "POSIX")) {
-                strncpy(key.language, loc, sizeof(key.language));
-                key.language[strcspn(key.language, ".@")] = 0;
-
-                f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
-                if (!f) {
-                        char *e;
-
-                        e = strchr(key.language, '_');
-                        if (e) {
-                                *e = 0;
-                                f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
-                        }
-                }
-        }
-
-        if (!f) {
-                zero(key.language);
-                f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
-        }
-
-        if (!f)
-                return NULL;
-
-        return (const char*) p +
-                le64toh(h->header_size) +
-                le64toh(h->n_items) * le64toh(h->catalog_item_size) +
-                le64toh(f->offset);
-}
-
-int catalog_get(const char* database, sd_id128_t id, char **_text) {
-        _cleanup_close_ int fd = -1;
-        void *p = NULL;
-        struct stat st = {};
-        char *text = NULL;
-        int r;
-        const char *s;
-
-        assert(_text);
-
-        r = open_mmap(database, &fd, &st, &p);
-        if (r < 0)
-                return r;
-
-        s = find_id(p, id);
-        if (!s) {
-                r = -ENOENT;
-                goto finish;
-        }
-
-        text = strdup(s);
-        if (!text) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        *_text = text;
-        r = 0;
-
-finish:
-        if (p)
-                munmap(p, st.st_size);
-
-        return r;
-}
-
-static char *find_header(const char *s, const char *header) {
-
-        for (;;) {
-                const char *v, *e;
-
-                v = startswith(s, header);
-                if (v) {
-                        v += strspn(v, WHITESPACE);
-                        return strndup(v, strcspn(v, NEWLINE));
-                }
-
-                /* End of text */
-                e = strchr(s, '\n');
-                if (!e)
-                        return NULL;
-
-                /* End of header */
-                if (e == s)
-                        return NULL;
-
-                s = e + 1;
-        }
-}
-
-static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) {
-        if (oneline) {
-                _cleanup_free_ char *subject = NULL, *defined_by = NULL;
-
-                subject = find_header(s, "Subject:");
-                defined_by = find_header(s, "Defined-By:");
-
-                fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n",
-                        SD_ID128_FORMAT_VAL(id),
-                        strna(defined_by), strna(subject));
-        } else
-                fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n",
-                        SD_ID128_FORMAT_VAL(id), s);
-}
-
-
-int catalog_list(FILE *f, const char *database, bool oneline) {
-        _cleanup_close_ int fd = -1;
-        void *p = NULL;
-        struct stat st;
-        const CatalogHeader *h;
-        const CatalogItem *items;
-        int r;
-        unsigned n;
-        sd_id128_t last_id;
-        bool last_id_set = false;
-
-        r = open_mmap(database, &fd, &st, &p);
-        if (r < 0)
-                return r;
-
-        h = p;
-        items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size));
-
-        for (n = 0; n < le64toh(h->n_items); n++) {
-                const char *s;
-
-                if (last_id_set && sd_id128_equal(last_id, items[n].id))
-                        continue;
-
-                assert_se(s = find_id(p, items[n].id));
-
-                dump_catalog_entry(f, items[n].id, s, oneline);
-
-                last_id_set = true;
-                last_id = items[n].id;
-        }
-
-        munmap(p, st.st_size);
-
-        return 0;
-}
-
-int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) {
-        char **item;
-        int r = 0;
-
-        STRV_FOREACH(item, items) {
-                sd_id128_t id;
-                int k;
-                _cleanup_free_ char *msg = NULL;
-
-                k = sd_id128_from_string(*item, &id);
-                if (k < 0) {
-                        log_error_errno(k, "Failed to parse id128 '%s': %m",
-                                        *item);
-                        if (r == 0)
-                                r = k;
-                        continue;
-                }
-
-                k = catalog_get(database, id, &msg);
-                if (k < 0) {
-                        log_full(k == -ENOENT ? LOG_NOTICE : LOG_ERR,
-                                 "Failed to retrieve catalog entry for '%s': %s",
-                                  *item, strerror(-k));
-                        if (r == 0)
-                                r = k;
-                        continue;
-                }
-
-                dump_catalog_entry(f, id, msg, oneline);
-        }
-
-        return r;
-}
diff --git a/src/journal/catalog.h b/src/journal/catalog.h
deleted file mode 100644 (file)
index a72ecf6..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "sd-id128.h"
-#include "hashmap.h"
-#include "strbuf.h"
-
-int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path);
-int catalog_update(const char* database, const char* root, const char* const* dirs);
-int catalog_get(const char* database, sd_id128_t id, char **data);
-int catalog_list(FILE *f, const char* database, bool oneline);
-int catalog_list_items(FILE *f, const char* database, bool oneline, char **items);
-int catalog_file_lang(const char *filename, char **lang);
-extern const char * const catalog_file_dirs[];
-extern const struct hash_ops catalog_hash_ops;
diff --git a/src/journal/compress.c b/src/journal/compress.c
deleted file mode 100644 (file)
index 383f6a6..0000000
+++ /dev/null
@@ -1,658 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef HAVE_XZ
-#  include <lzma.h>
-#endif
-
-#ifdef HAVE_LZ4
-#  include <lz4.h>
-#endif
-
-#include "compress.h"
-#include "macro.h"
-#include "util.h"
-#include "sparse-endian.h"
-#include "journal-def.h"
-
-#define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
-
-static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
-        [OBJECT_COMPRESSED_XZ] = "XZ",
-        [OBJECT_COMPRESSED_LZ4] = "LZ4",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(object_compressed, int);
-
-int compress_blob_xz(const void *src, uint64_t src_size, void *dst, size_t *dst_size) {
-#ifdef HAVE_XZ
-        static const lzma_options_lzma opt = {
-                1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
-                LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4};
-        static const lzma_filter filters[2] = {
-                {LZMA_FILTER_LZMA2, (lzma_options_lzma*) &opt},
-                {LZMA_VLI_UNKNOWN, NULL}
-        };
-        lzma_ret ret;
-        size_t out_pos = 0;
-
-        assert(src);
-        assert(src_size > 0);
-        assert(dst);
-        assert(dst_size);
-
-        /* Returns < 0 if we couldn't compress the data or the
-         * compressed result is longer than the original */
-
-        if (src_size < 80)
-                return -ENOBUFS;
-
-        ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL,
-                                        src, src_size, dst, &out_pos, src_size - 1);
-        if (ret != LZMA_OK)
-                return -ENOBUFS;
-
-        *dst_size = out_pos;
-        return 0;
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int compress_blob_lz4(const void *src, uint64_t src_size, void *dst, size_t *dst_size) {
-#ifdef HAVE_LZ4
-        int r;
-
-        assert(src);
-        assert(src_size > 0);
-        assert(dst);
-        assert(dst_size);
-
-        /* Returns < 0 if we couldn't compress the data or the
-         * compressed result is longer than the original */
-
-        if (src_size < 9)
-                return -ENOBUFS;
-
-        r = LZ4_compress_limitedOutput(src, dst + 8, src_size, src_size - 8 - 1);
-        if (r <= 0)
-                return -ENOBUFS;
-
-        *(le64_t*) dst = htole64(src_size);
-        *dst_size = r + 8;
-
-        return 0;
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-
-int decompress_blob_xz(const void *src, uint64_t src_size,
-                       void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
-
-#ifdef HAVE_XZ
-        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
-        lzma_ret ret;
-        size_t space;
-
-        assert(src);
-        assert(src_size > 0);
-        assert(dst);
-        assert(dst_alloc_size);
-        assert(dst_size);
-        assert(*dst_alloc_size == 0 || *dst);
-
-        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
-        if (ret != LZMA_OK)
-                return -ENOMEM;
-
-        space = MIN(src_size * 2, dst_max ?: (size_t) -1);
-        if (!greedy_realloc(dst, dst_alloc_size, space, 1))
-                return -ENOMEM;
-
-        s.next_in = src;
-        s.avail_in = src_size;
-
-        s.next_out = *dst;
-        s.avail_out = space;
-
-        for (;;) {
-                size_t used;
-
-                ret = lzma_code(&s, LZMA_FINISH);
-
-                if (ret == LZMA_STREAM_END)
-                        break;
-                else if (ret != LZMA_OK)
-                        return -ENOMEM;
-
-                if (dst_max > 0 && (space - s.avail_out) >= dst_max)
-                        break;
-                else if (dst_max > 0 && space == dst_max)
-                        return -ENOBUFS;
-
-                used = space - s.avail_out;
-                space = MIN(2 * space, dst_max ?: (size_t) -1);
-                if (!greedy_realloc(dst, dst_alloc_size, space, 1))
-                        return -ENOMEM;
-
-                s.avail_out = space - used;
-                s.next_out = *dst + used;
-        }
-
-        *dst_size = space - s.avail_out;
-        return 0;
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_blob_lz4(const void *src, uint64_t src_size,
-                        void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
-
-#ifdef HAVE_LZ4
-        char* out;
-        int r, size; /* LZ4 uses int for size */
-
-        assert(src);
-        assert(src_size > 0);
-        assert(dst);
-        assert(dst_alloc_size);
-        assert(dst_size);
-        assert(*dst_alloc_size == 0 || *dst);
-
-        if (src_size <= 8)
-                return -EBADMSG;
-
-        size = le64toh( *(le64_t*)src );
-        if (size < 0 || (le64_t) size != *(le64_t*)src)
-                return -EFBIG;
-        if ((size_t) size > *dst_alloc_size) {
-                out = realloc(*dst, size);
-                if (!out)
-                        return -ENOMEM;
-                *dst = out;
-                *dst_alloc_size = size;
-        } else
-                out = *dst;
-
-        r = LZ4_decompress_safe(src + 8, out, src_size - 8, size);
-        if (r < 0 || r != size)
-                return -EBADMSG;
-
-        *dst_size = size;
-        return 0;
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_blob(int compression,
-                    const void *src, uint64_t src_size,
-                    void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
-        if (compression == OBJECT_COMPRESSED_XZ)
-                return decompress_blob_xz(src, src_size,
-                                          dst, dst_alloc_size, dst_size, dst_max);
-        else if (compression == OBJECT_COMPRESSED_LZ4)
-                return decompress_blob_lz4(src, src_size,
-                                           dst, dst_alloc_size, dst_size, dst_max);
-        else
-                return -EBADMSG;
-}
-
-
-int decompress_startswith_xz(const void *src, uint64_t src_size,
-                             void **buffer, size_t *buffer_size,
-                             const void *prefix, size_t prefix_len,
-                             uint8_t extra) {
-
-#ifdef HAVE_XZ
-        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
-        lzma_ret ret;
-
-        /* Checks whether the decompressed blob starts with the
-         * mentioned prefix. The byte extra needs to follow the
-         * prefix */
-
-        assert(src);
-        assert(src_size > 0);
-        assert(buffer);
-        assert(buffer_size);
-        assert(prefix);
-        assert(*buffer_size == 0 || *buffer);
-
-        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
-        if (ret != LZMA_OK)
-                return -EBADMSG;
-
-        if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
-                return -ENOMEM;
-
-        s.next_in = src;
-        s.avail_in = src_size;
-
-        s.next_out = *buffer;
-        s.avail_out = *buffer_size;
-
-        for (;;) {
-                ret = lzma_code(&s, LZMA_FINISH);
-
-                if (ret != LZMA_STREAM_END && ret != LZMA_OK)
-                        return -EBADMSG;
-
-                if (*buffer_size - s.avail_out >= prefix_len + 1)
-                        return memcmp(*buffer, prefix, prefix_len) == 0 &&
-                                ((const uint8_t*) *buffer)[prefix_len] == extra;
-
-                if (ret == LZMA_STREAM_END)
-                        return 0;
-
-                s.avail_out += *buffer_size;
-
-                if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
-                        return -ENOMEM;
-
-                s.next_out = *buffer + *buffer_size - s.avail_out;
-        }
-
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_startswith_lz4(const void *src, uint64_t src_size,
-                              void **buffer, size_t *buffer_size,
-                              const void *prefix, size_t prefix_len,
-                              uint8_t extra) {
-#ifdef HAVE_LZ4
-        /* Checks whether the decompressed blob starts with the
-         * mentioned prefix. The byte extra needs to follow the
-         * prefix */
-
-        int r;
-
-        assert(src);
-        assert(src_size > 0);
-        assert(buffer);
-        assert(buffer_size);
-        assert(prefix);
-        assert(*buffer_size == 0 || *buffer);
-
-        if (src_size <= 8)
-                return -EBADMSG;
-
-        if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
-                return -ENOMEM;
-
-        r = LZ4_decompress_safe_partial(src + 8, *buffer, src_size - 8,
-                                        prefix_len + 1, *buffer_size);
-
-        if (r < 0)
-                return -EBADMSG;
-        if ((unsigned) r >= prefix_len + 1)
-                return memcmp(*buffer, prefix, prefix_len) == 0 &&
-                        ((const uint8_t*) *buffer)[prefix_len] == extra;
-        else
-                return 0;
-
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_startswith(int compression,
-                          const void *src, uint64_t src_size,
-                          void **buffer, size_t *buffer_size,
-                          const void *prefix, size_t prefix_len,
-                          uint8_t extra) {
-        if (compression == OBJECT_COMPRESSED_XZ)
-                return decompress_startswith_xz(src, src_size,
-                                                buffer, buffer_size,
-                                                prefix, prefix_len,
-                                                extra);
-        else if (compression == OBJECT_COMPRESSED_LZ4)
-                return decompress_startswith_lz4(src, src_size,
-                                                 buffer, buffer_size,
-                                                 prefix, prefix_len,
-                                                 extra);
-        else
-                return -EBADMSG;
-}
-
-int compress_stream_xz(int fdf, int fdt, off_t max_bytes) {
-#ifdef HAVE_XZ
-        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
-        lzma_ret ret;
-
-        uint8_t buf[BUFSIZ], out[BUFSIZ];
-        lzma_action action = LZMA_RUN;
-
-        assert(fdf >= 0);
-        assert(fdt >= 0);
-
-        ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
-        if (ret != LZMA_OK) {
-                log_error("Failed to initialize XZ encoder: code %u", ret);
-                return -EINVAL;
-        }
-
-        for (;;) {
-                if (s.avail_in == 0 && action == LZMA_RUN) {
-                        size_t m = sizeof(buf);
-                        ssize_t n;
-
-                        if (max_bytes != -1 && m > (size_t) max_bytes)
-                                m = max_bytes;
-
-                        n = read(fdf, buf, m);
-                        if (n < 0)
-                                return -errno;
-                        if (n == 0)
-                                action = LZMA_FINISH;
-                        else {
-                                s.next_in = buf;
-                                s.avail_in = n;
-
-                                if (max_bytes != -1) {
-                                        assert(max_bytes >= n);
-                                        max_bytes -= n;
-                                }
-                        }
-                }
-
-                if (s.avail_out == 0) {
-                        s.next_out = out;
-                        s.avail_out = sizeof(out);
-                }
-
-                ret = lzma_code(&s, action);
-                if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
-                        log_error("Compression failed: code %u", ret);
-                        return -EBADMSG;
-                }
-
-                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
-                        ssize_t n, k;
-
-                        n = sizeof(out) - s.avail_out;
-
-                        k = loop_write(fdt, out, n, false);
-                        if (k < 0)
-                                return k;
-
-                        if (ret == LZMA_STREAM_END) {
-                                log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
-                                          s.total_in, s.total_out,
-                                          (double) s.total_out / s.total_in * 100);
-
-                                return 0;
-                        }
-                }
-        }
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-#define LZ4_BUFSIZE (512*1024)
-
-int compress_stream_lz4(int fdf, int fdt, off_t max_bytes) {
-
-#ifdef HAVE_LZ4
-
-        _cleanup_free_ char *buf1 = NULL, *buf2 = NULL, *out = NULL;
-        char *buf;
-        LZ4_stream_t lz4_data = {};
-        le32_t header;
-        size_t total_in = 0, total_out = sizeof(header);
-        ssize_t n;
-
-        assert(fdf >= 0);
-        assert(fdt >= 0);
-
-        buf1 = malloc(LZ4_BUFSIZE);
-        buf2 = malloc(LZ4_BUFSIZE);
-        out = malloc(LZ4_COMPRESSBOUND(LZ4_BUFSIZE));
-        if (!buf1 || !buf2 || !out)
-                return log_oom();
-
-        buf = buf1;
-        for (;;) {
-                size_t m;
-                int r;
-
-                m = LZ4_BUFSIZE;
-                if (max_bytes != -1 && m > (size_t) max_bytes - total_in)
-                        m = max_bytes - total_in;
-
-                n = read(fdf, buf, m);
-                if (n < 0)
-                        return -errno;
-                if (n == 0)
-                        break;
-
-                total_in += n;
-
-                r = LZ4_compress_continue(&lz4_data, buf, out, n);
-                if (r == 0) {
-                        log_error("LZ4 compression failed.");
-                        return -EBADMSG;
-                }
-
-                header = htole32(r);
-                errno = 0;
-
-                n = write(fdt, &header, sizeof(header));
-                if (n < 0)
-                        return -errno;
-                if (n != sizeof(header))
-                        return errno ? -errno : -EIO;
-
-                n = loop_write(fdt, out, r, false);
-                if (n < 0)
-                        return n;
-
-                total_out += sizeof(header) + r;
-
-                buf = buf == buf1 ? buf2 : buf1;
-        }
-
-        header = htole32(0);
-        n = write(fdt, &header, sizeof(header));
-        if (n < 0)
-                return -errno;
-        if (n != sizeof(header))
-                return errno ? -errno : -EIO;
-
-        log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
-                  total_in, total_out,
-                  (double) total_out / total_in * 100);
-
-        return 0;
-#else
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_stream_xz(int fdf, int fdt, off_t max_bytes) {
-
-#ifdef HAVE_XZ
-        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
-        lzma_ret ret;
-
-        uint8_t buf[BUFSIZ], out[BUFSIZ];
-        lzma_action action = LZMA_RUN;
-
-        assert(fdf >= 0);
-        assert(fdt >= 0);
-
-        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
-        if (ret != LZMA_OK) {
-                log_error("Failed to initialize XZ decoder: code %u", ret);
-                return -ENOMEM;
-        }
-
-        for (;;) {
-                if (s.avail_in == 0 && action == LZMA_RUN) {
-                        ssize_t n;
-
-                        n = read(fdf, buf, sizeof(buf));
-                        if (n < 0)
-                                return -errno;
-                        if (n == 0)
-                                action = LZMA_FINISH;
-                        else {
-                                s.next_in = buf;
-                                s.avail_in = n;
-                        }
-                }
-
-                if (s.avail_out == 0) {
-                        s.next_out = out;
-                        s.avail_out = sizeof(out);
-                }
-
-                ret = lzma_code(&s, action);
-                if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
-                        log_error("Decompression failed: code %u", ret);
-                        return -EBADMSG;
-                }
-
-                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
-                        ssize_t n, k;
-
-                        n = sizeof(out) - s.avail_out;
-
-                        if (max_bytes != -1) {
-                                if (max_bytes < n)
-                                        return -EFBIG;
-
-                                max_bytes -= n;
-                        }
-
-                        k = loop_write(fdt, out, n, false);
-                        if (k < 0)
-                                return k;
-
-                        if (ret == LZMA_STREAM_END) {
-                                log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
-                                          s.total_in, s.total_out,
-                                          (double) s.total_out / s.total_in * 100);
-
-                                return 0;
-                        }
-                }
-        }
-#else
-        log_error("Cannot decompress file. Compiled without XZ support.");
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_stream_lz4(int fdf, int fdt, off_t max_bytes) {
-
-#ifdef HAVE_LZ4
-        _cleanup_free_ char *buf = NULL, *out = NULL;
-        size_t buf_size = 0;
-        LZ4_streamDecode_t lz4_data = {};
-        le32_t header;
-        size_t total_in = sizeof(header), total_out = 0;
-
-        assert(fdf >= 0);
-        assert(fdt >= 0);
-
-        out = malloc(4*LZ4_BUFSIZE);
-        if (!out)
-                return log_oom();
-
-        for (;;) {
-                ssize_t m;
-                int r;
-
-                r = loop_read_exact(fdf, &header, sizeof(header), false);
-                if (r < 0)
-                        return r;
-
-                m = le32toh(header);
-                if (m == 0)
-                        break;
-
-                /* We refuse to use a bigger decompression buffer than
-                 * the one used for compression by 4 times. This means
-                 * that compression buffer size can be enlarged 4
-                 * times. This can be changed, but old binaries might
-                 * not accept buffers compressed by newer binaries then.
-                 */
-                if (m > LZ4_COMPRESSBOUND(LZ4_BUFSIZE * 4)) {
-                        log_error("Compressed stream block too big: %zd bytes", m);
-                        return -EBADMSG;
-                }
-
-                total_in += sizeof(header) + m;
-
-                if (!GREEDY_REALLOC(buf, buf_size, m))
-                        return log_oom();
-
-                r = loop_read_exact(fdf, buf, m, false);
-                if (r < 0)
-                        return r;
-
-                r = LZ4_decompress_safe_continue(&lz4_data, buf, out, m, 4*LZ4_BUFSIZE);
-                if (r <= 0)
-                        log_error("LZ4 decompression failed.");
-
-                total_out += r;
-
-                if (max_bytes != -1 && total_out > (size_t) max_bytes) {
-                        log_debug("Decompressed stream longer than %zd bytes", max_bytes);
-                        return -EFBIG;
-                }
-
-                r = loop_write(fdt, out, r, false);
-                if (r < 0)
-                        return r;
-        }
-
-        log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
-                  total_in, total_out,
-                  (double) total_out / total_in * 100);
-
-        return 0;
-#else
-        log_error("Cannot decompress file. Compiled without LZ4 support.");
-        return -EPROTONOSUPPORT;
-#endif
-}
-
-int decompress_stream(const char *filename, int fdf, int fdt, off_t max_bytes) {
-
-        if (endswith(filename, ".lz4"))
-                return decompress_stream_lz4(fdf, fdt, max_bytes);
-        else if (endswith(filename, ".xz"))
-                return decompress_stream_xz(fdf, fdt, max_bytes);
-        else
-                return -EPROTONOSUPPORT;
-}
diff --git a/src/journal/compress.h b/src/journal/compress.h
deleted file mode 100644 (file)
index 6294f16..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <unistd.h>
-
-#include "journal-def.h"
-
-const char* object_compressed_to_string(int compression);
-int object_compressed_from_string(const char *compression);
-
-int compress_blob_xz(const void *src, uint64_t src_size, void *dst, size_t *dst_size);
-int compress_blob_lz4(const void *src, uint64_t src_size, void *dst, size_t *dst_size);
-
-static inline int compress_blob(const void *src, uint64_t src_size, void *dst, size_t *dst_size) {
-        int r;
-#ifdef HAVE_LZ4
-        r = compress_blob_lz4(src, src_size, dst, dst_size);
-        if (r == 0)
-                return OBJECT_COMPRESSED_LZ4;
-#else
-        r = compress_blob_xz(src, src_size, dst, dst_size);
-        if (r == 0)
-                return OBJECT_COMPRESSED_XZ;
-#endif
-        return r;
-}
-
-int decompress_blob_xz(const void *src, uint64_t src_size,
-                       void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
-int decompress_blob_lz4(const void *src, uint64_t src_size,
-                        void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
-int decompress_blob(int compression,
-                    const void *src, uint64_t src_size,
-                    void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
-
-int decompress_startswith_xz(const void *src, uint64_t src_size,
-                             void **buffer, size_t *buffer_size,
-                             const void *prefix, size_t prefix_len,
-                             uint8_t extra);
-int decompress_startswith_lz4(const void *src, uint64_t src_size,
-                              void **buffer, size_t *buffer_size,
-                              const void *prefix, size_t prefix_len,
-                              uint8_t extra);
-int decompress_startswith(int compression,
-                          const void *src, uint64_t src_size,
-                          void **buffer, size_t *buffer_size,
-                          const void *prefix, size_t prefix_len,
-                          uint8_t extra);
-
-int compress_stream_xz(int fdf, int fdt, off_t max_bytes);
-int compress_stream_lz4(int fdf, int fdt, off_t max_bytes);
-
-int decompress_stream_xz(int fdf, int fdt, off_t max_size);
-int decompress_stream_lz4(int fdf, int fdt, off_t max_size);
-
-#ifdef HAVE_LZ4
-#  define compress_stream compress_stream_lz4
-#  define COMPRESSED_EXT ".lz4"
-#else
-#  define compress_stream compress_stream_xz
-#  define COMPRESSED_EXT ".xz"
-#endif
-
-int decompress_stream(const char *filename, int fdf, int fdt, off_t max_bytes);
diff --git a/src/journal/coredump-vacuum.c b/src/journal/coredump-vacuum.c
deleted file mode 100644 (file)
index 9b73795..0000000
+++ /dev/null
@@ -1,270 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/statvfs.h>
-
-#include "util.h"
-#include "time-util.h"
-#include "hashmap.h"
-#include "macro.h"
-
-#include "coredump-vacuum.h"
-
-#define DEFAULT_MAX_USE_LOWER (off_t) (1ULL*1024ULL*1024ULL)           /* 1 MiB */
-#define DEFAULT_MAX_USE_UPPER (off_t) (4ULL*1024ULL*1024ULL*1024ULL)   /* 4 GiB */
-#define DEFAULT_KEEP_FREE_UPPER (off_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
-#define DEFAULT_KEEP_FREE (off_t) (1024ULL*1024ULL)                    /* 1 MB */
-
-struct vacuum_candidate {
-        unsigned n_files;
-        char *oldest_file;
-        usec_t oldest_mtime;
-};
-
-static void vacuum_candidate_free(struct vacuum_candidate *c) {
-        if (!c)
-                return;
-
-        free(c->oldest_file);
-        free(c);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate*, vacuum_candidate_free);
-
-static void vacuum_candidate_hasmap_free(Hashmap *h) {
-        struct vacuum_candidate *c;
-
-        while ((c = hashmap_steal_first(h)))
-                vacuum_candidate_free(c);
-
-        hashmap_free(h);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hasmap_free);
-
-static int uid_from_file_name(const char *filename, uid_t *uid) {
-        const char *p, *e, *u;
-
-        p = startswith(filename, "core.");
-        if (!p)
-                return -EINVAL;
-
-        /* Skip the comm field */
-        p = strchr(p, '.');
-        if (!p)
-                return -EINVAL;
-        p++;
-
-        /* Find end up UID */
-        e = strchr(p, '.');
-        if (!e)
-                return -EINVAL;
-
-        u = strndupa(p, e-p);
-        return parse_uid(u, uid);
-}
-
-static bool vacuum_necessary(int fd, off_t sum, off_t keep_free, off_t max_use) {
-        off_t fs_size = 0, fs_free = (off_t) -1;
-        struct statvfs sv;
-
-        assert(fd >= 0);
-
-        if (fstatvfs(fd, &sv) >= 0) {
-                fs_size = sv.f_frsize * sv.f_blocks;
-                fs_free = sv.f_frsize * sv.f_bfree;
-        }
-
-        if (max_use == (off_t) -1) {
-
-                if (fs_size > 0) {
-                        max_use = PAGE_ALIGN(fs_size / 10); /* 10% */
-
-                        if (max_use > DEFAULT_MAX_USE_UPPER)
-                                max_use = DEFAULT_MAX_USE_UPPER;
-
-                        if (max_use < DEFAULT_MAX_USE_LOWER)
-                                max_use = DEFAULT_MAX_USE_LOWER;
-                }
-                else
-                        max_use = DEFAULT_MAX_USE_LOWER;
-        } else
-                max_use = PAGE_ALIGN(max_use);
-
-        if (max_use > 0 && sum > max_use)
-                return true;
-
-        if (keep_free == (off_t) -1) {
-
-                if (fs_size > 0) {
-                        keep_free = PAGE_ALIGN((fs_size * 3) / 20); /* 15% */
-
-                        if (keep_free > DEFAULT_KEEP_FREE_UPPER)
-                                keep_free = DEFAULT_KEEP_FREE_UPPER;
-                } else
-                        keep_free = DEFAULT_KEEP_FREE;
-        } else
-                keep_free = PAGE_ALIGN(keep_free);
-
-        if (keep_free > 0 && fs_free < keep_free)
-                return true;
-
-        return false;
-}
-
-int coredump_vacuum(int exclude_fd, off_t keep_free, off_t max_use) {
-        _cleanup_closedir_ DIR *d = NULL;
-        struct stat exclude_st;
-        int r;
-
-        if (keep_free <= 0 && max_use <= 0)
-                return 0;
-
-        if (exclude_fd >= 0) {
-                if (fstat(exclude_fd, &exclude_st) < 0)
-                        return log_error_errno(errno, "Failed to fstat(): %m");
-        }
-
-        /* This algorithm will keep deleting the oldest file of the
-         * user with the most coredumps until we are back in the size
-         * limits. Note that vacuuming for journal files is different,
-         * because we rely on rate-limiting of the messages there,
-         * to avoid being flooded. */
-
-        d = opendir("/var/lib/systemd/coredump");
-        if (!d) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_error_errno(errno, "Can't open coredump directory: %m");
-                return -errno;
-        }
-
-        for (;;) {
-                _cleanup_(vacuum_candidate_hasmap_freep) Hashmap *h = NULL;
-                struct vacuum_candidate *worst = NULL;
-                struct dirent *de;
-                off_t sum = 0;
-
-                rewinddir(d);
-
-                FOREACH_DIRENT(de, d, goto fail) {
-                        struct vacuum_candidate *c;
-                        struct stat st;
-                        uid_t uid;
-                        usec_t t;
-
-                        r = uid_from_file_name(de->d_name, &uid);
-                        if (r < 0)
-                                continue;
-
-                        if (fstatat(dirfd(d), de->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) < 0) {
-                                if (errno == ENOENT)
-                                        continue;
-
-                                log_warning("Failed to stat /var/lib/systemd/coredump/%s", de->d_name);
-                                continue;
-                        }
-
-                        if (!S_ISREG(st.st_mode))
-                                continue;
-
-                        if (exclude_fd >= 0 &&
-                            exclude_st.st_dev == st.st_dev &&
-                            exclude_st.st_ino == st.st_ino)
-                                continue;
-
-                        r = hashmap_ensure_allocated(&h, NULL);
-                        if (r < 0)
-                                return log_oom();
-
-                        t = timespec_load(&st.st_mtim);
-
-                        c = hashmap_get(h, UINT32_TO_PTR(uid));
-                        if (c) {
-
-                                if (t < c->oldest_mtime) {
-                                        char *n;
-
-                                        n = strdup(de->d_name);
-                                        if (!n)
-                                                return log_oom();
-
-                                        free(c->oldest_file);
-                                        c->oldest_file = n;
-                                        c->oldest_mtime = t;
-                                }
-
-                        } else {
-                                _cleanup_(vacuum_candidate_freep) struct vacuum_candidate *n = NULL;
-
-                                n = new0(struct vacuum_candidate, 1);
-                                if (!n)
-                                        return log_oom();
-
-                                n->oldest_file = strdup(de->d_name);
-                                if (!n->oldest_file)
-                                        return log_oom();
-
-                                n->oldest_mtime = t;
-
-                                r = hashmap_put(h, UINT32_TO_PTR(uid), n);
-                                if (r < 0)
-                                        return log_oom();
-
-                                c = n;
-                                n = NULL;
-                        }
-
-                        c->n_files++;
-
-                        if (!worst ||
-                            worst->n_files < c->n_files ||
-                            (worst->n_files == c->n_files && c->oldest_mtime < worst->oldest_mtime))
-                                worst = c;
-
-                        sum += st.st_blocks * 512;
-                }
-
-                if (!worst)
-                        break;
-
-                r = vacuum_necessary(dirfd(d), sum, keep_free, max_use);
-                if (r <= 0)
-                        return r;
-
-                if (unlinkat(dirfd(d), worst->oldest_file, 0) < 0) {
-
-                        if (errno == ENOENT)
-                                continue;
-
-                        log_error_errno(errno, "Failed to remove file %s: %m", worst->oldest_file);
-                        return -errno;
-                } else
-                        log_info("Removed old coredump %s.", worst->oldest_file);
-        }
-
-        return 0;
-
-fail:
-        log_error_errno(errno, "Failed to read directory: %m");
-        return -errno;
-}
diff --git a/src/journal/coredump-vacuum.h b/src/journal/coredump-vacuum.h
deleted file mode 100644 (file)
index 7ad4399..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/types.h>
-
-int coredump_vacuum(int exclude_fd, off_t keep_free, off_t max_use);
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
deleted file mode 100644 (file)
index 4e23e48..0000000
+++ /dev/null
@@ -1,862 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <sys/prctl.h>
-#include <sys/xattr.h>
-
-#ifdef HAVE_ELFUTILS
-#  include <dwarf.h>
-#  include <elfutils/libdwfl.h>
-#endif
-
-#include "sd-journal.h"
-#include "sd-login.h"
-#include "log.h"
-#include "util.h"
-#include "fileio.h"
-#include "strv.h"
-#include "macro.h"
-#include "mkdir.h"
-#include "special.h"
-#include "cgroup-util.h"
-#include "conf-parser.h"
-#include "copy.h"
-#include "stacktrace.h"
-#include "compress.h"
-#include "acl-util.h"
-#include "capability.h"
-#include "journald-native.h"
-#include "coredump-vacuum.h"
-
-/* The maximum size up to which we process coredumps */
-#define PROCESS_SIZE_MAX ((off_t) (2LLU*1024LLU*1024LLU*1024LLU))
-
-/* The maximum size up to which we leave the coredump around on
- * disk */
-#define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
-
-/* The maximum size up to which we store the coredump in the
- * journal */
-#define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU))
-
-/* Make sure to not make this larger than the maximum journal entry
- * size. See DATA_SIZE_MAX in journald-native.c. */
-assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX);
-
-enum {
-        INFO_PID,
-        INFO_UID,
-        INFO_GID,
-        INFO_SIGNAL,
-        INFO_TIMESTAMP,
-        INFO_COMM,
-        INFO_EXE,
-        _INFO_LEN
-};
-
-typedef enum CoredumpStorage {
-        COREDUMP_STORAGE_NONE,
-        COREDUMP_STORAGE_EXTERNAL,
-        COREDUMP_STORAGE_JOURNAL,
-        COREDUMP_STORAGE_BOTH,
-        _COREDUMP_STORAGE_MAX,
-        _COREDUMP_STORAGE_INVALID = -1
-} CoredumpStorage;
-
-static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = {
-        [COREDUMP_STORAGE_NONE] = "none",
-        [COREDUMP_STORAGE_EXTERNAL] = "external",
-        [COREDUMP_STORAGE_JOURNAL] = "journal",
-        [COREDUMP_STORAGE_BOTH] = "both",
-};
-
-DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
-static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
-
-static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
-static bool arg_compress = true;
-static off_t arg_process_size_max = PROCESS_SIZE_MAX;
-static off_t arg_external_size_max = EXTERNAL_SIZE_MAX;
-static size_t arg_journal_size_max = JOURNAL_SIZE_MAX;
-static off_t arg_keep_free = (off_t) -1;
-static off_t arg_max_use = (off_t) -1;
-
-static int parse_config(void) {
-        static const ConfigTableItem items[] = {
-                { "Coredump", "Storage",          config_parse_coredump_storage,  0, &arg_storage           },
-                { "Coredump", "Compress",         config_parse_bool,              0, &arg_compress          },
-                { "Coredump", "ProcessSizeMax",   config_parse_iec_off,           0, &arg_process_size_max  },
-                { "Coredump", "ExternalSizeMax",  config_parse_iec_off,           0, &arg_external_size_max },
-                { "Coredump", "JournalSizeMax",   config_parse_iec_size,          0, &arg_journal_size_max  },
-                { "Coredump", "KeepFree",         config_parse_iec_off,           0, &arg_keep_free         },
-                { "Coredump", "MaxUse",           config_parse_iec_off,           0, &arg_max_use           },
-                {}
-        };
-
-        return config_parse_many("/etc/systemd/coredump.conf",
-                                 CONF_DIRS_NULSTR("systemd/coredump.conf"),
-                                 "Coredump\0",
-                                 config_item_table_lookup, items,
-                                 false, NULL);
-}
-
-static int fix_acl(int fd, uid_t uid) {
-
-#ifdef HAVE_ACL
-        _cleanup_(acl_freep) acl_t acl = NULL;
-        acl_entry_t entry;
-        acl_permset_t permset;
-
-        assert(fd >= 0);
-
-        if (uid <= SYSTEM_UID_MAX)
-                return 0;
-
-        /* Make sure normal users can read (but not write or delete)
-         * their own coredumps */
-
-        acl = acl_get_fd(fd);
-        if (!acl)
-                return log_error_errno(errno, "Failed to get ACL: %m");
-
-        if (acl_create_entry(&acl, &entry) < 0 ||
-            acl_set_tag_type(entry, ACL_USER) < 0 ||
-            acl_set_qualifier(entry, &uid) < 0) {
-                log_error_errno(errno, "Failed to patch ACL: %m");
-                return -errno;
-        }
-
-        if (acl_get_permset(entry, &permset) < 0 ||
-            acl_add_perm(permset, ACL_READ) < 0 ||
-            calc_acl_mask_if_needed(&acl) < 0) {
-                log_warning_errno(errno, "Failed to patch ACL: %m");
-                return -errno;
-        }
-
-        if (acl_set_fd(fd, acl) < 0)
-                return log_error_errno(errno, "Failed to apply ACL: %m");
-#endif
-
-        return 0;
-}
-
-static int fix_xattr(int fd, const char *info[_INFO_LEN]) {
-
-        static const char * const xattrs[_INFO_LEN] = {
-                [INFO_PID] = "user.coredump.pid",
-                [INFO_UID] = "user.coredump.uid",
-                [INFO_GID] = "user.coredump.gid",
-                [INFO_SIGNAL] = "user.coredump.signal",
-                [INFO_TIMESTAMP] = "user.coredump.timestamp",
-                [INFO_COMM] = "user.coredump.comm",
-                [INFO_EXE] = "user.coredump.exe",
-        };
-
-        int r = 0;
-        unsigned i;
-
-        assert(fd >= 0);
-
-        /* Attach some metadata to coredumps via extended
-         * attributes. Just because we can. */
-
-        for (i = 0; i < _INFO_LEN; i++) {
-                int k;
-
-                if (isempty(info[i]) || !xattrs[i])
-                        continue;
-
-                k = fsetxattr(fd, xattrs[i], info[i], strlen(info[i]), XATTR_CREATE);
-                if (k < 0 && r == 0)
-                        r = -errno;
-        }
-
-        return r;
-}
-
-#define filename_escape(s) xescape((s), "./ ")
-
-static int fix_permissions(
-                int fd,
-                const char *filename,
-                const char *target,
-                const char *info[_INFO_LEN],
-                uid_t uid) {
-
-        assert(fd >= 0);
-        assert(filename);
-        assert(target);
-        assert(info);
-
-        /* Ignore errors on these */
-        fchmod(fd, 0640);
-        fix_acl(fd, uid);
-        fix_xattr(fd, info);
-
-        if (fsync(fd) < 0)
-                return log_error_errno(errno, "Failed to sync coredump %s: %m", filename);
-
-        if (rename(filename, target) < 0)
-                return log_error_errno(errno, "Failed to rename coredump %s -> %s: %m", filename, target);
-
-        return 0;
-}
-
-static int maybe_remove_external_coredump(const char *filename, off_t size) {
-
-        /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
-
-        if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
-            size <= arg_external_size_max)
-                return 0;
-
-        if (!filename)
-                return 1;
-
-        if (unlink(filename) < 0 && errno != ENOENT)
-                return log_error_errno(errno, "Failed to unlink %s: %m", filename);
-
-        return 1;
-}
-
-static int make_filename(const char *info[_INFO_LEN], char **ret) {
-        _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
-        sd_id128_t boot = {};
-        int r;
-
-        assert(info);
-
-        c = filename_escape(info[INFO_COMM]);
-        if (!c)
-                return -ENOMEM;
-
-        u = filename_escape(info[INFO_UID]);
-        if (!u)
-                return -ENOMEM;
-
-        r = sd_id128_get_boot(&boot);
-        if (r < 0)
-                return r;
-
-        p = filename_escape(info[INFO_PID]);
-        if (!p)
-                return -ENOMEM;
-
-        t = filename_escape(info[INFO_TIMESTAMP]);
-        if (!t)
-                return -ENOMEM;
-
-        if (asprintf(ret,
-                     "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
-                     c,
-                     u,
-                     SD_ID128_FORMAT_VAL(boot),
-                     p,
-                     t) < 0)
-                return -ENOMEM;
-
-        return 0;
-}
-
-static int save_external_coredump(
-                const char *info[_INFO_LEN],
-                uid_t uid,
-                char **ret_filename,
-                int *ret_fd,
-                off_t *ret_size) {
-
-        _cleanup_free_ char *fn = NULL, *tmp = NULL;
-        _cleanup_close_ int fd = -1;
-        struct stat st;
-        int r;
-
-        assert(info);
-        assert(ret_filename);
-        assert(ret_fd);
-        assert(ret_size);
-
-        r = make_filename(info, &fn);
-        if (r < 0)
-                return log_error_errno(r, "Failed to determine coredump file name: %m");
-
-        r = tempfn_random(fn, &tmp);
-        if (r < 0)
-                return log_error_errno(r, "Failed to determine temporary file name: %m");
-
-        mkdir_p_label("/var/lib/systemd/coredump", 0755);
-
-        fd = open(tmp, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
-        if (fd < 0)
-                return log_error_errno(errno, "Failed to create coredump file %s: %m", tmp);
-
-        r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max, false);
-        if (r == -EFBIG) {
-                log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", info[INFO_PID], info[INFO_COMM]);
-                goto fail;
-        } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
-                log_error("Not enough disk space for coredump of %s (%s), refusing.", info[INFO_PID], info[INFO_COMM]);
-                goto fail;
-        } else if (r < 0) {
-                log_error_errno(r, "Failed to dump coredump to file: %m");
-                goto fail;
-        }
-
-        if (fstat(fd, &st) < 0) {
-                log_error_errno(errno, "Failed to fstat coredump %s: %m", tmp);
-                goto fail;
-        }
-
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
-                log_error_errno(errno, "Failed to seek on %s: %m", tmp);
-                goto fail;
-        }
-
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-        /* If we will remove the coredump anyway, do not compress. */
-        if (maybe_remove_external_coredump(NULL, st.st_size) == 0
-            && arg_compress) {
-
-                _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
-                _cleanup_close_ int fd_compressed = -1;
-
-                fn_compressed = strappend(fn, COMPRESSED_EXT);
-                if (!fn_compressed) {
-                        log_oom();
-                        goto uncompressed;
-                }
-
-                r = tempfn_random(fn_compressed, &tmp_compressed);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to determine temporary file name for %s: %m", fn_compressed);
-                        goto uncompressed;
-                }
-
-                fd_compressed = open(tmp_compressed, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
-                if (fd_compressed < 0) {
-                        log_error_errno(errno, "Failed to create file %s: %m", tmp_compressed);
-                        goto uncompressed;
-                }
-
-                r = compress_stream(fd, fd_compressed, -1);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to compress %s: %m", tmp_compressed);
-                        goto fail_compressed;
-                }
-
-                r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, info, uid);
-                if (r < 0)
-                        goto fail_compressed;
-
-                /* OK, this worked, we can get rid of the uncompressed version now */
-                unlink_noerrno(tmp);
-
-                *ret_filename = fn_compressed;    /* compressed */
-                *ret_fd = fd;                     /* uncompressed */
-                *ret_size = st.st_size;           /* uncompressed */
-
-                fn_compressed = NULL;
-                fd = -1;
-
-                return 0;
-
-        fail_compressed:
-                unlink_noerrno(tmp_compressed);
-        }
-
-uncompressed:
-#endif
-        r = fix_permissions(fd, tmp, fn, info, uid);
-        if (r < 0)
-                goto fail;
-
-        *ret_filename = fn;
-        *ret_fd = fd;
-        *ret_size = st.st_size;
-
-        fn = NULL;
-        fd = -1;
-
-        return 0;
-
-fail:
-        unlink_noerrno(tmp);
-        return r;
-}
-
-static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
-        _cleanup_free_ char *field = NULL;
-        ssize_t n;
-
-        assert(fd >= 0);
-        assert(ret);
-        assert(ret_size);
-
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
-                return log_warning_errno(errno, "Failed to seek: %m");
-
-        field = malloc(9 + size);
-        if (!field) {
-                log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
-                return -ENOMEM;
-        }
-
-        memcpy(field, "COREDUMP=", 9);
-
-        n = read(fd, field + 9, size);
-        if (n < 0)
-                return log_error_errno((int) n, "Failed to read core data: %m");
-        if ((size_t) n < size) {
-                log_error("Core data too short.");
-                return -EIO;
-        }
-
-        *ret = field;
-        *ret_size = size + 9;
-
-        field = NULL;
-
-        return 0;
-}
-
-/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
- * 0:/dev/pts/23
- * pos:    0
- * flags:  0100002
- *
- * 1:/dev/pts/23
- * pos:    0
- * flags:  0100002
- *
- * 2:/dev/pts/23
- * pos:    0
- * flags:  0100002
- * EOF
- */
-static int compose_open_fds(pid_t pid, char **open_fds) {
-        _cleanup_closedir_ DIR *proc_fd_dir = NULL;
-        _cleanup_close_ int proc_fdinfo_fd = -1;
-        _cleanup_free_ char *buffer = NULL;
-        _cleanup_fclose_ FILE *stream = NULL;
-        const char *fddelim = "", *path;
-        struct dirent *dent = NULL;
-        size_t size = 0;
-        int r = 0;
-
-        assert(pid >= 0);
-        assert(open_fds != NULL);
-
-        path = procfs_file_alloca(pid, "fd");
-        proc_fd_dir = opendir(path);
-        if (!proc_fd_dir)
-                return -errno;
-
-        proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
-        if (proc_fdinfo_fd < 0)
-                return -errno;
-
-        stream = open_memstream(&buffer, &size);
-        if (!stream)
-                return -ENOMEM;
-
-        FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
-                _cleanup_fclose_ FILE *fdinfo = NULL;
-                _cleanup_free_ char *fdname = NULL;
-                char line[LINE_MAX];
-                int fd;
-
-                r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
-                if (r < 0)
-                        return r;
-
-                fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
-                fddelim = "\n";
-
-                /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
-                fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
-                if (fd < 0)
-                        continue;
-
-                fdinfo = fdopen(fd, "re");
-                if (fdinfo == NULL) {
-                        close(fd);
-                        continue;
-                }
-
-                FOREACH_LINE(line, fdinfo, break) {
-                        fputs(line, stream);
-                        if (!endswith(line, "\n"))
-                                fputc('\n', stream);
-                }
-        }
-
-        errno = 0;
-        fclose(stream);
-        stream = NULL;
-
-        if (errno != 0)
-                return -errno;
-
-        *open_fds = buffer;
-        buffer = NULL;
-
-        return 0;
-}
-
-int main(int argc, char* argv[]) {
-
-        /* The small core field we allocate on the stack, to keep things simple */
-        char
-                *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
-                *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL,
-                *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL,
-                *core_slice = NULL;
-
-        /* The larger ones we allocate on the heap */
-        _cleanup_free_ char
-                *core_timestamp = NULL,  *core_message = NULL, *coredump_data = NULL, *core_owner_uid = NULL,
-                *core_open_fds = NULL, *core_proc_status = NULL, *core_proc_maps = NULL, *core_proc_limits = NULL,
-                *core_proc_cgroup = NULL, *core_environ = NULL;
-
-        _cleanup_free_ char *exe = NULL, *comm = NULL, *filename = NULL;
-        const char *info[_INFO_LEN];
-
-        _cleanup_close_ int coredump_fd = -1;
-
-        struct iovec iovec[26];
-        off_t coredump_size;
-        int r, j = 0;
-        uid_t uid, owner_uid;
-        gid_t gid;
-        pid_t pid;
-        char *t;
-        const char *p;
-
-        /* Make sure we never enter a loop */
-        prctl(PR_SET_DUMPABLE, 0);
-
-        /* First, log to a safe place, since we don't know what
-         * crashed and it might be journald which we'd rather not log
-         * to then. */
-        log_set_target(LOG_TARGET_KMSG);
-        log_open();
-
-        if (argc < INFO_COMM + 1) {
-                log_error("Not enough arguments passed from kernel (%d, expected %d).",
-                          argc - 1, INFO_COMM + 1 - 1);
-                r = -EINVAL;
-                goto finish;
-        }
-
-        /* Ignore all parse errors */
-        parse_config();
-
-        log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
-        log_debug("Selected compression %s.", yes_no(arg_compress));
-
-        r = parse_uid(argv[INFO_UID + 1], &uid);
-        if (r < 0) {
-                log_error("Failed to parse UID.");
-                goto finish;
-        }
-
-        r = parse_pid(argv[INFO_PID + 1], &pid);
-        if (r < 0) {
-                log_error("Failed to parse PID.");
-                goto finish;
-        }
-
-        r = parse_gid(argv[INFO_GID + 1], &gid);
-        if (r < 0) {
-                log_error("Failed to parse GID.");
-                goto finish;
-        }
-
-        if (get_process_comm(pid, &comm) < 0) {
-                log_warning("Failed to get COMM, falling back to the command line.");
-                comm = strv_join(argv + INFO_COMM + 1, " ");
-        }
-
-        if (get_process_exe(pid, &exe) < 0)
-                log_warning("Failed to get EXE.");
-
-        info[INFO_PID] = argv[INFO_PID + 1];
-        info[INFO_UID] = argv[INFO_UID + 1];
-        info[INFO_GID] = argv[INFO_GID + 1];
-        info[INFO_SIGNAL] = argv[INFO_SIGNAL + 1];
-        info[INFO_TIMESTAMP] = argv[INFO_TIMESTAMP + 1];
-        info[INFO_COMM] = comm;
-        info[INFO_EXE] = exe;
-
-        if (cg_pid_get_unit(pid, &t) >= 0) {
-
-                if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
-                        free(t);
-
-                        /* If we are journald, we cut things short,
-                         * don't write to the journal, but still
-                         * create a coredump. */
-
-                        if (arg_storage != COREDUMP_STORAGE_NONE)
-                                arg_storage = COREDUMP_STORAGE_EXTERNAL;
-
-                        r = save_external_coredump(info, uid, &filename, &coredump_fd, &coredump_size);
-                        if (r < 0)
-                                goto finish;
-
-                        r = maybe_remove_external_coredump(filename, coredump_size);
-                        if (r < 0)
-                                goto finish;
-
-                        log_info("Detected coredump of the journal daemon itself, diverted to %s.", filename);
-                        goto finish;
-                }
-
-                core_unit = strjoina("COREDUMP_UNIT=", t);
-                free(t);
-
-        } else if (cg_pid_get_user_unit(pid, &t) >= 0) {
-                core_unit = strjoina("COREDUMP_USER_UNIT=", t);
-                free(t);
-        }
-
-        if (core_unit)
-                IOVEC_SET_STRING(iovec[j++], core_unit);
-
-        /* OK, now we know it's not the journal, hence we can make use
-         * of it now. */
-        log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
-        log_open();
-
-        core_pid = strjoina("COREDUMP_PID=", info[INFO_PID]);
-        IOVEC_SET_STRING(iovec[j++], core_pid);
-
-        core_uid = strjoina("COREDUMP_UID=", info[INFO_UID]);
-        IOVEC_SET_STRING(iovec[j++], core_uid);
-
-        core_gid = strjoina("COREDUMP_GID=", info[INFO_GID]);
-        IOVEC_SET_STRING(iovec[j++], core_gid);
-
-        core_signal = strjoina("COREDUMP_SIGNAL=", info[INFO_SIGNAL]);
-        IOVEC_SET_STRING(iovec[j++], core_signal);
-
-        if (sd_pid_get_session(pid, &t) >= 0) {
-                core_session = strjoina("COREDUMP_SESSION=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_session);
-        }
-
-        if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
-                r = asprintf(&core_owner_uid,
-                             "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
-                if (r > 0)
-                        IOVEC_SET_STRING(iovec[j++], core_owner_uid);
-        }
-
-        if (sd_pid_get_slice(pid, &t) >= 0) {
-                core_slice = strjoina("COREDUMP_SLICE=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_slice);
-        }
-
-        if (comm) {
-                core_comm = strjoina("COREDUMP_COMM=", comm);
-                IOVEC_SET_STRING(iovec[j++], core_comm);
-        }
-
-        if (exe) {
-                core_exe = strjoina("COREDUMP_EXE=", exe);
-                IOVEC_SET_STRING(iovec[j++], core_exe);
-        }
-
-        if (get_process_cmdline(pid, 0, false, &t) >= 0) {
-                core_cmdline = strjoina("COREDUMP_CMDLINE=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_cmdline);
-        }
-
-        if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
-                core_cgroup = strjoina("COREDUMP_CGROUP=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_cgroup);
-        }
-
-        if (compose_open_fds(pid, &t) >= 0) {
-                core_open_fds = strappend("COREDUMP_OPEN_FDS=", t);
-                free(t);
-
-                if (core_open_fds)
-                        IOVEC_SET_STRING(iovec[j++], core_open_fds);
-        }
-
-        p = procfs_file_alloca(pid, "status");
-        if (read_full_file(p, &t, NULL) >= 0) {
-                core_proc_status = strappend("COREDUMP_PROC_STATUS=", t);
-                free(t);
-
-                if (core_proc_status)
-                        IOVEC_SET_STRING(iovec[j++], core_proc_status);
-        }
-
-        p = procfs_file_alloca(pid, "maps");
-        if (read_full_file(p, &t, NULL) >= 0) {
-                core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t);
-                free(t);
-
-                if (core_proc_maps)
-                        IOVEC_SET_STRING(iovec[j++], core_proc_maps);
-        }
-
-        p = procfs_file_alloca(pid, "limits");
-        if (read_full_file(p, &t, NULL) >= 0) {
-                core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t);
-                free(t);
-
-                if (core_proc_limits)
-                        IOVEC_SET_STRING(iovec[j++], core_proc_limits);
-        }
-
-        p = procfs_file_alloca(pid, "cgroup");
-        if (read_full_file(p, &t, NULL) >=0) {
-                core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t);
-                free(t);
-
-                if (core_proc_cgroup)
-                        IOVEC_SET_STRING(iovec[j++], core_proc_cgroup);
-        }
-
-        if (get_process_cwd(pid, &t) >= 0) {
-                core_cwd = strjoina("COREDUMP_CWD=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_cwd);
-        }
-
-        if (get_process_root(pid, &t) >= 0) {
-                core_root = strjoina("COREDUMP_ROOT=", t);
-                free(t);
-
-                IOVEC_SET_STRING(iovec[j++], core_root);
-        }
-
-        if (get_process_environ(pid, &t) >= 0) {
-                core_environ = strappend("COREDUMP_ENVIRON=", t);
-                free(t);
-
-                if (core_environ)
-                        IOVEC_SET_STRING(iovec[j++], core_environ);
-        }
-
-        core_timestamp = strjoin("COREDUMP_TIMESTAMP=", info[INFO_TIMESTAMP], "000000", NULL);
-        if (core_timestamp)
-                IOVEC_SET_STRING(iovec[j++], core_timestamp);
-
-        IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
-        IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
-
-        /* Vacuum before we write anything again */
-        coredump_vacuum(-1, arg_keep_free, arg_max_use);
-
-        /* Always stream the coredump to disk, if that's possible */
-        r = save_external_coredump(info, uid, &filename, &coredump_fd, &coredump_size);
-        if (r < 0)
-                /* skip whole core dumping part */
-                goto log;
-
-        /* If we don't want to keep the coredump on disk, remove it
-         * now, as later on we will lack the privileges for
-         * it. However, we keep the fd to it, so that we can still
-         * process it and log it. */
-        r = maybe_remove_external_coredump(filename, coredump_size);
-        if (r < 0)
-                goto finish;
-        if (r == 0) {
-                const char *coredump_filename;
-
-                coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
-                IOVEC_SET_STRING(iovec[j++], coredump_filename);
-        }
-
-        /* Vacuum again, but exclude the coredump we just created */
-        coredump_vacuum(coredump_fd, arg_keep_free, arg_max_use);
-
-        /* Now, let's drop privileges to become the user who owns the
-         * segfaulted process and allocate the coredump memory under
-         * the user's uid. This also ensures that the credentials
-         * journald will see are the ones of the coredumping user,
-         * thus making sure the user gets access to the core
-         * dump. Let's also get rid of all capabilities, if we run as
-         * root, we won't need them anymore. */
-        r = drop_privileges(uid, gid, 0);
-        if (r < 0) {
-                log_error_errno(r, "Failed to drop privileges: %m");
-                goto finish;
-        }
-
-#ifdef HAVE_ELFUTILS
-        /* Try to get a strack trace if we can */
-        if (coredump_size <= arg_process_size_max) {
-                _cleanup_free_ char *stacktrace = NULL;
-
-                r = coredump_make_stack_trace(coredump_fd, exe, &stacktrace);
-                if (r >= 0)
-                        core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.\n\n", stacktrace, NULL);
-                else if (r == -EINVAL)
-                        log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
-                else
-                        log_warning_errno(r, "Failed to generate stack trace: %m");
-        }
-
-        if (!core_message)
-#endif
-log:
-        core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.", NULL);
-        if (core_message)
-                IOVEC_SET_STRING(iovec[j++], core_message);
-
-        /* Optionally store the entire coredump in the journal */
-        if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
-            coredump_size <= (off_t) arg_journal_size_max) {
-                size_t sz = 0;
-
-                /* Store the coredump itself in the journal */
-
-                r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
-                if (r >= 0) {
-                        iovec[j].iov_base = coredump_data;
-                        iovec[j].iov_len = sz;
-                        j++;
-                }
-        }
-
-        r = sd_journal_sendv(iovec, j);
-        if (r < 0)
-                log_error_errno(r, "Failed to log coredump: %m");
-
-finish:
-        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
-}
diff --git a/src/journal/coredump.conf b/src/journal/coredump.conf
deleted file mode 100644 (file)
index c2f0643..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#  This file is part of systemd.
-#
-#  systemd is free software; you can redistribute it and/or modify it
-#  under the terms of the GNU Lesser General Public License as published by
-#  the Free Software Foundation; either version 2.1 of the License, or
-#  (at your option) any later version.
-#
-# Entries in this file show the compile time defaults.
-# You can change settings by editing this file.
-# Defaults can be restored by simply deleting this file.
-#
-# See coredump.conf(5) for details.
-
-[Coredump]
-#Storage=external
-#Compress=yes
-#ProcessSizeMax=2G
-#ExternalSizeMax=2G
-#JournalSizeMax=767M
-#MaxUse=
-#KeepFree=
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
deleted file mode 100644 (file)
index 4026a1c..0000000
+++ /dev/null
@@ -1,860 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Zbigniew JÄ™drzejewski-Szmek
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <locale.h>
-#include <stdio.h>
-#include <string.h>
-#include <getopt.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "sd-journal.h"
-#include "build.h"
-#include "set.h"
-#include "util.h"
-#include "log.h"
-#include "path-util.h"
-#include "pager.h"
-#include "macro.h"
-#include "journal-internal.h"
-#include "compress.h"
-#include "sigbus.h"
-
-static enum {
-        ACTION_NONE,
-        ACTION_INFO,
-        ACTION_LIST,
-        ACTION_DUMP,
-        ACTION_GDB,
-} arg_action = ACTION_LIST;
-static const char* arg_field = NULL;
-static int arg_no_pager = false;
-static int arg_no_legend = false;
-static int arg_one = false;
-static FILE* arg_output = NULL;
-
-static Set *new_matches(void) {
-        Set *set;
-        char *tmp;
-        int r;
-
-        set = set_new(NULL);
-        if (!set) {
-                log_oom();
-                return NULL;
-        }
-
-        tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
-        if (!tmp) {
-                log_oom();
-                set_free(set);
-                return NULL;
-        }
-
-        r = set_consume(set, tmp);
-        if (r < 0) {
-                log_error_errno(r, "failed to add to set: %m");
-                set_free(set);
-                return NULL;
-        }
-
-        return set;
-}
-
-static int add_match(Set *set, const char *match) {
-        int r = -ENOMEM;
-        unsigned pid;
-        const char* prefix;
-        char *pattern = NULL;
-        _cleanup_free_ char *p = NULL;
-
-        if (strchr(match, '='))
-                prefix = "";
-        else if (strchr(match, '/')) {
-                p = path_make_absolute_cwd(match);
-                if (!p)
-                        goto fail;
-
-                match = p;
-                prefix = "COREDUMP_EXE=";
-        }
-        else if (safe_atou(match, &pid) == 0)
-                prefix = "COREDUMP_PID=";
-        else
-                prefix = "COREDUMP_COMM=";
-
-        pattern = strjoin(prefix, match, NULL);
-        if (!pattern)
-                goto fail;
-
-        log_debug("Adding pattern: %s", pattern);
-        r = set_consume(set, pattern);
-        if (r < 0) {
-                log_error_errno(r, "Failed to add pattern: %m");
-                goto fail;
-        }
-
-        return 0;
-fail:
-        return log_error_errno(r, "Failed to add match: %m");
-}
-
-static void help(void) {
-        printf("%s [OPTIONS...]\n\n"
-               "List or retrieve coredumps from the journal.\n\n"
-               "Flags:\n"
-               "  -h --help          Show this help\n"
-               "     --version       Print version string\n"
-               "     --no-pager      Do not pipe output into a pager\n"
-               "     --no-legend     Do not print the column headers.\n"
-               "  -1                 Show information about most recent entry only\n"
-               "  -F --field=FIELD   List all values a certain field takes\n"
-               "  -o --output=FILE   Write output to FILE\n\n"
-
-               "Commands:\n"
-               "  list [MATCHES...]  List available coredumps (default)\n"
-               "  info [MATCHES...]  Show detailed information about one or more coredumps\n"
-               "  dump [MATCHES...]  Print first matching coredump to stdout\n"
-               "  gdb [MATCHES...]   Start gdb for the first matching coredump\n"
-               , program_invocation_short_name);
-}
-
-static int parse_argv(int argc, char *argv[], Set *matches) {
-        enum {
-                ARG_VERSION = 0x100,
-                ARG_NO_PAGER,
-                ARG_NO_LEGEND,
-        };
-
-        int r, c;
-
-        static const struct option options[] = {
-                { "help",         no_argument,       NULL, 'h'           },
-                { "version" ,     no_argument,       NULL, ARG_VERSION   },
-                { "no-pager",     no_argument,       NULL, ARG_NO_PAGER  },
-                { "no-legend",    no_argument,       NULL, ARG_NO_LEGEND },
-                { "output",       required_argument, NULL, 'o'           },
-                { "field",        required_argument, NULL, 'F'           },
-                {}
-        };
-
-        assert(argc >= 0);
-        assert(argv);
-
-        while ((c = getopt_long(argc, argv, "ho:F:1", options, NULL)) >= 0)
-                switch(c) {
-
-                case 'h':
-                        arg_action = ACTION_NONE;
-                        help();
-                        return 0;
-
-                case ARG_VERSION:
-                        arg_action = ACTION_NONE;
-                        puts(PACKAGE_STRING);
-                        puts(SYSTEMD_FEATURES);
-                        return 0;
-
-                case ARG_NO_PAGER:
-                        arg_no_pager = true;
-                        break;
-
-                case ARG_NO_LEGEND:
-                        arg_no_legend = true;
-                        break;
-
-                case 'o':
-                        if (arg_output) {
-                                log_error("cannot set output more than once");
-                                return -EINVAL;
-                        }
-
-                        arg_output = fopen(optarg, "we");
-                        if (!arg_output)
-                                return log_error_errno(errno, "writing to '%s': %m", optarg);
-
-                        break;
-
-                case 'F':
-                        if (arg_field) {
-                                log_error("cannot use --field/-F more than once");
-                                return -EINVAL;
-                        }
-                        arg_field = optarg;
-                        break;
-
-                case '1':
-                        arg_one = true;
-                        break;
-
-                case '?':
-                        return -EINVAL;
-
-                default:
-                        assert_not_reached("Unhandled option");
-                }
-
-        if (optind < argc) {
-                const char *cmd = argv[optind++];
-                if (streq(cmd, "list"))
-                        arg_action = ACTION_LIST;
-                else if (streq(cmd, "dump"))
-                        arg_action = ACTION_DUMP;
-                else if (streq(cmd, "gdb"))
-                        arg_action = ACTION_GDB;
-                else if (streq(cmd, "info"))
-                        arg_action = ACTION_INFO;
-                else {
-                        log_error("Unknown action '%s'", cmd);
-                        return -EINVAL;
-                }
-        }
-
-        if (arg_field && arg_action != ACTION_LIST) {
-                log_error("Option --field/-F only makes sense with list");
-                return -EINVAL;
-        }
-
-        while (optind < argc) {
-                r = add_match(matches, argv[optind]);
-                if (r != 0)
-                        return r;
-                optind++;
-        }
-
-        return 0;
-}
-
-static int retrieve(const void *data,
-                    size_t len,
-                    const char *name,
-                    char **var) {
-
-        size_t ident;
-        char *v;
-
-        ident = strlen(name) + 1; /* name + "=" */
-
-        if (len < ident)
-                return 0;
-
-        if (memcmp(data, name, ident - 1) != 0)
-                return 0;
-
-        if (((const char*) data)[ident - 1] != '=')
-                return 0;
-
-        v = strndup((const char*)data + ident, len - ident);
-        if (!v)
-                return log_oom();
-
-        free(*var);
-        *var = v;
-
-        return 0;
-}
-
-static void print_field(FILE* file, sd_journal *j) {
-        _cleanup_free_ char *value = NULL;
-        const void *d;
-        size_t l;
-
-        assert(file);
-        assert(j);
-
-        assert(arg_field);
-
-        SD_JOURNAL_FOREACH_DATA(j, d, l)
-                retrieve(d, l, arg_field, &value);
-
-        if (value)
-                fprintf(file, "%s\n", value);
-}
-
-static int print_list(FILE* file, sd_journal *j, int had_legend) {
-        _cleanup_free_ char
-                *pid = NULL, *uid = NULL, *gid = NULL,
-                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
-                *filename = NULL;
-        const void *d;
-        size_t l;
-        usec_t t;
-        char buf[FORMAT_TIMESTAMP_MAX];
-        int r;
-        bool present;
-
-        assert(file);
-        assert(j);
-
-        SD_JOURNAL_FOREACH_DATA(j, d, l) {
-                retrieve(d, l, "COREDUMP_PID", &pid);
-                retrieve(d, l, "COREDUMP_UID", &uid);
-                retrieve(d, l, "COREDUMP_GID", &gid);
-                retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
-                retrieve(d, l, "COREDUMP_EXE", &exe);
-                retrieve(d, l, "COREDUMP_COMM", &comm);
-                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
-                retrieve(d, l, "COREDUMP_FILENAME", &filename);
-        }
-
-        if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
-                log_warning("Empty coredump log entry");
-                return -EINVAL;
-        }
-
-        r = sd_journal_get_realtime_usec(j, &t);
-        if (r < 0)
-                return log_error_errno(r, "Failed to get realtime timestamp: %m");
-
-        format_timestamp(buf, sizeof(buf), t);
-        present = filename && access(filename, F_OK) == 0;
-
-        if (!had_legend && !arg_no_legend)
-                fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
-                        FORMAT_TIMESTAMP_WIDTH, "TIME",
-                        6, "PID",
-                        5, "UID",
-                        5, "GID",
-                        3, "SIG",
-                        1, "PRESENT",
-                           "EXE");
-
-        fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
-                FORMAT_TIMESTAMP_WIDTH, buf,
-                6, strna(pid),
-                5, strna(uid),
-                5, strna(gid),
-                3, strna(sgnl),
-                1, present ? "*" : "",
-                strna(exe ?: (comm ?: cmdline)));
-
-        return 0;
-}
-
-static int print_info(FILE *file, sd_journal *j, bool need_space) {
-        _cleanup_free_ char
-                *pid = NULL, *uid = NULL, *gid = NULL,
-                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
-                *unit = NULL, *user_unit = NULL, *session = NULL,
-                *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
-                *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
-                *message = NULL, *timestamp = NULL, *filename = NULL;
-        const void *d;
-        size_t l;
-        int r;
-
-        assert(file);
-        assert(j);
-
-        SD_JOURNAL_FOREACH_DATA(j, d, l) {
-                retrieve(d, l, "COREDUMP_PID", &pid);
-                retrieve(d, l, "COREDUMP_UID", &uid);
-                retrieve(d, l, "COREDUMP_GID", &gid);
-                retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
-                retrieve(d, l, "COREDUMP_EXE", &exe);
-                retrieve(d, l, "COREDUMP_COMM", &comm);
-                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
-                retrieve(d, l, "COREDUMP_UNIT", &unit);
-                retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
-                retrieve(d, l, "COREDUMP_SESSION", &session);
-                retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid);
-                retrieve(d, l, "COREDUMP_SLICE", &slice);
-                retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
-                retrieve(d, l, "COREDUMP_TIMESTAMP", &timestamp);
-                retrieve(d, l, "COREDUMP_FILENAME", &filename);
-                retrieve(d, l, "_BOOT_ID", &boot_id);
-                retrieve(d, l, "_MACHINE_ID", &machine_id);
-                retrieve(d, l, "_HOSTNAME", &hostname);
-                retrieve(d, l, "MESSAGE", &message);
-        }
-
-        if (need_space)
-                fputs("\n", file);
-
-        if (comm)
-                fprintf(file,
-                        "           PID: %s%s%s (%s)\n",
-                        ansi_highlight(), strna(pid), ansi_highlight_off(), comm);
-        else
-                fprintf(file,
-                        "           PID: %s%s%s\n",
-                        ansi_highlight(), strna(pid), ansi_highlight_off());
-
-        if (uid) {
-                uid_t n;
-
-                if (parse_uid(uid, &n) >= 0) {
-                        _cleanup_free_ char *u = NULL;
-
-                        u = uid_to_name(n);
-                        fprintf(file,
-                                "           UID: %s (%s)\n",
-                                uid, u);
-                } else {
-                        fprintf(file,
-                                "           UID: %s\n",
-                                uid);
-                }
-        }
-
-        if (gid) {
-                gid_t n;
-
-                if (parse_gid(gid, &n) >= 0) {
-                        _cleanup_free_ char *g = NULL;
-
-                        g = gid_to_name(n);
-                        fprintf(file,
-                                "           GID: %s (%s)\n",
-                                gid, g);
-                } else {
-                        fprintf(file,
-                                "           GID: %s\n",
-                                gid);
-                }
-        }
-
-        if (sgnl) {
-                int sig;
-
-                if (safe_atoi(sgnl, &sig) >= 0)
-                        fprintf(file, "        Signal: %s (%s)\n", sgnl, signal_to_string(sig));
-                else
-                        fprintf(file, "        Signal: %s\n", sgnl);
-        }
-
-        if (timestamp) {
-                usec_t u;
-
-                r = safe_atou64(timestamp, &u);
-                if (r >= 0) {
-                        char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
-
-                        fprintf(file,
-                                "     Timestamp: %s (%s)\n",
-                                format_timestamp(absolute, sizeof(absolute), u),
-                                format_timestamp_relative(relative, sizeof(relative), u));
-
-                } else
-                        fprintf(file, "     Timestamp: %s\n", timestamp);
-        }
-
-        if (cmdline)
-                fprintf(file, "  Command Line: %s\n", cmdline);
-        if (exe)
-                fprintf(file, "    Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off());
-        if (cgroup)
-                fprintf(file, " Control Group: %s\n", cgroup);
-        if (unit)
-                fprintf(file, "          Unit: %s\n", unit);
-        if (user_unit)
-                fprintf(file, "     User Unit: %s\n", unit);
-        if (slice)
-                fprintf(file, "         Slice: %s\n", slice);
-        if (session)
-                fprintf(file, "       Session: %s\n", session);
-        if (owner_uid) {
-                uid_t n;
-
-                if (parse_uid(owner_uid, &n) >= 0) {
-                        _cleanup_free_ char *u = NULL;
-
-                        u = uid_to_name(n);
-                        fprintf(file,
-                                "     Owner UID: %s (%s)\n",
-                                owner_uid, u);
-                } else {
-                        fprintf(file,
-                                "     Owner UID: %s\n",
-                                owner_uid);
-                }
-        }
-        if (boot_id)
-                fprintf(file, "       Boot ID: %s\n", boot_id);
-        if (machine_id)
-                fprintf(file, "    Machine ID: %s\n", machine_id);
-        if (hostname)
-                fprintf(file, "      Hostname: %s\n", hostname);
-
-        if (filename && access(filename, F_OK) == 0)
-                fprintf(file, "      Coredump: %s\n", filename);
-
-        if (message) {
-                _cleanup_free_ char *m = NULL;
-
-                m = strreplace(message, "\n", "\n                ");
-
-                fprintf(file, "       Message: %s\n", strstrip(m ?: message));
-        }
-
-        return 0;
-}
-
-static int focus(sd_journal *j) {
-        int r;
-
-        r = sd_journal_seek_tail(j);
-        if (r == 0)
-                r = sd_journal_previous(j);
-        if (r < 0)
-                return log_error_errno(r, "Failed to search journal: %m");
-        if (r == 0) {
-                log_error("No match found.");
-                return -ESRCH;
-        }
-        return r;
-}
-
-static void print_entry(sd_journal *j, unsigned n_found) {
-        assert(j);
-
-        if (arg_action == ACTION_INFO)
-                print_info(stdout, j, n_found);
-        else if (arg_field)
-                print_field(stdout, j);
-        else
-                print_list(stdout, j, n_found);
-}
-
-static int dump_list(sd_journal *j) {
-        unsigned n_found = 0;
-        int r;
-
-        assert(j);
-
-        /* The coredumps are likely to compressed, and for just
-         * listing them we don't need to decompress them, so let's
-         * pick a fairly low data threshold here */
-        sd_journal_set_data_threshold(j, 4096);
-
-        if (arg_one) {
-                r = focus(j);
-                if (r < 0)
-                        return r;
-
-                print_entry(j, 0);
-        } else {
-                SD_JOURNAL_FOREACH(j)
-                        print_entry(j, n_found++);
-
-                if (!arg_field && n_found <= 0) {
-                        log_notice("No coredumps found.");
-                        return -ESRCH;
-                }
-        }
-
-        return 0;
-}
-
-static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
-        const char *data;
-        _cleanup_free_ char *filename = NULL;
-        size_t len;
-        int r;
-
-        assert((fd >= 0) != !!path);
-        assert(!!path == !!unlink_temp);
-
-        /* Prefer uncompressed file to journal (probably cached) to
-         * compressed file (probably uncached). */
-        r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
-        if (r < 0 && r != -ENOENT)
-                log_warning_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
-        else if (r == 0)
-                retrieve(data, len, "COREDUMP_FILENAME", &filename);
-
-        if (filename && access(filename, R_OK) < 0) {
-                log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
-                         "File %s is not readable: %m", filename);
-                free(filename);
-                filename = NULL;
-        }
-
-        if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
-                if (path) {
-                        *path = filename;
-                        filename = NULL;
-                }
-
-                return 0;
-        } else {
-                _cleanup_close_ int fdt = -1;
-                char *temp = NULL;
-
-                if (fd < 0) {
-                        temp = strdup("/var/tmp/coredump-XXXXXX");
-                        if (!temp)
-                                return log_oom();
-
-                        fdt = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
-                        if (fdt < 0)
-                                return log_error_errno(errno, "Failed to create temporary file: %m");
-                        log_debug("Created temporary file %s", temp);
-
-                        fd = fdt;
-                }
-
-                r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
-                if (r == 0) {
-                        ssize_t sz;
-
-                        assert(len >= 9);
-                        data += 9;
-                        len -= 9;
-
-                        sz = write(fdt, data, len);
-                        if (sz < 0) {
-                                log_error_errno(errno, "Failed to write temporary file: %m");
-                                r = -errno;
-                                goto error;
-                        }
-                        if (sz != (ssize_t) len) {
-                                log_error("Short write to temporary file.");
-                                r = -EIO;
-                                goto error;
-                        }
-                } else if (filename) {
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-                        _cleanup_close_ int fdf;
-
-                        fdf = open(filename, O_RDONLY | O_CLOEXEC);
-                        if (fdf < 0) {
-                                log_error_errno(errno, "Failed to open %s: %m", filename);
-                                r = -errno;
-                                goto error;
-                        }
-
-                        r = decompress_stream(filename, fdf, fd, -1);
-                        if (r < 0) {
-                                log_error_errno(r, "Failed to decompress %s: %m", filename);
-                                goto error;
-                        }
-#else
-                        log_error("Cannot decompress file. Compiled without compression support.");
-                        r = -EOPNOTSUPP;
-                        goto error;
-#endif
-                } else {
-                        if (r == -ENOENT)
-                                log_error("Cannot retrieve coredump from journal nor disk.");
-                        else
-                                log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
-                        goto error;
-                }
-
-                if (temp) {
-                        *path = temp;
-                        *unlink_temp = true;
-                }
-
-                return 0;
-
-error:
-                if (temp) {
-                        unlink(temp);
-                        log_debug("Removed temporary file %s", temp);
-                }
-                return r;
-        }
-}
-
-static int dump_core(sd_journal* j) {
-        int r;
-
-        assert(j);
-
-        r = focus(j);
-        if (r < 0)
-                return r;
-
-        print_info(arg_output ? stdout : stderr, j, false);
-
-        if (on_tty() && !arg_output) {
-                log_error("Refusing to dump core to tty.");
-                return -ENOTTY;
-        }
-
-        r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL);
-        if (r < 0)
-                return log_error_errno(r, "Coredump retrieval failed: %m");
-
-        r = sd_journal_previous(j);
-        if (r >= 0)
-                log_warning("More than one entry matches, ignoring rest.");
-
-        return 0;
-}
-
-static int run_gdb(sd_journal *j) {
-        _cleanup_free_ char *exe = NULL, *path = NULL;
-        bool unlink_path = false;
-        const char *data;
-        siginfo_t st;
-        size_t len;
-        pid_t pid;
-        int r;
-
-        assert(j);
-
-        r = focus(j);
-        if (r < 0)
-                return r;
-
-        print_info(stdout, j, false);
-        fputs("\n", stdout);
-
-        r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
-        if (r < 0)
-                return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
-
-        assert(len > strlen("COREDUMP_EXE="));
-        data += strlen("COREDUMP_EXE=");
-        len -= strlen("COREDUMP_EXE=");
-
-        exe = strndup(data, len);
-        if (!exe)
-                return log_oom();
-
-        if (endswith(exe, " (deleted)")) {
-                log_error("Binary already deleted.");
-                return -ENOENT;
-        }
-
-        if (!path_is_absolute(exe)) {
-                log_error("Binary is not an absolute path.");
-                return -ENOENT;
-        }
-
-        r = save_core(j, -1, &path, &unlink_path);
-        if (r < 0)
-                return log_error_errno(r, "Failed to retrieve core: %m");
-
-        pid = fork();
-        if (pid < 0) {
-                log_error_errno(errno, "Failed to fork(): %m");
-                r = -errno;
-                goto finish;
-        }
-        if (pid == 0) {
-                execlp("gdb", "gdb", exe, path, NULL);
-
-                log_error_errno(errno, "Failed to invoke gdb: %m");
-                _exit(1);
-        }
-
-        r = wait_for_terminate(pid, &st);
-        if (r < 0) {
-                log_error_errno(errno, "Failed to wait for gdb: %m");
-                goto finish;
-        }
-
-        r = st.si_code == CLD_EXITED ? st.si_status : 255;
-
-finish:
-        if (unlink_path) {
-                log_debug("Removed temporary file %s", path);
-                unlink(path);
-        }
-
-        return r;
-}
-
-int main(int argc, char *argv[]) {
-        _cleanup_journal_close_ sd_journal*j = NULL;
-        const char* match;
-        Iterator it;
-        int r = 0;
-        _cleanup_set_free_free_ Set *matches = NULL;
-
-        setlocale(LC_ALL, "");
-        log_parse_environment();
-        log_open();
-
-        matches = new_matches();
-        if (!matches) {
-                r = -ENOMEM;
-                goto end;
-        }
-
-        r = parse_argv(argc, argv, matches);
-        if (r < 0)
-                goto end;
-
-        if (arg_action == ACTION_NONE)
-                goto end;
-
-        sigbus_install();
-
-        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
-        if (r < 0) {
-                log_error_errno(r, "Failed to open journal: %m");
-                goto end;
-        }
-
-        /* We want full data, nothing truncated. */
-        sd_journal_set_data_threshold(j, 0);
-
-        SET_FOREACH(match, matches, it) {
-                r = sd_journal_add_match(j, match, strlen(match));
-                if (r != 0) {
-                        log_error_errno(r, "Failed to add match '%s': %m",
-                                        match);
-                        goto end;
-                }
-        }
-
-        if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) {
-                _cleanup_free_ char *filter;
-
-                filter = journal_make_match_string(j);
-                log_debug("Journal filter: %s", filter);
-        }
-
-        switch(arg_action) {
-
-        case ACTION_LIST:
-        case ACTION_INFO:
-                if (!arg_no_pager)
-                        pager_open(false);
-
-                r = dump_list(j);
-                break;
-
-        case ACTION_DUMP:
-                r = dump_core(j);
-                break;
-
-        case  ACTION_GDB:
-                r = run_gdb(j);
-                break;
-
-        default:
-                assert_not_reached("Shouldn't be here");
-        }
-
-end:
-        pager_close();
-
-        if (arg_output)
-                fclose(arg_output);
-
-        return r >= 0 ? r : EXIT_FAILURE;
-}
diff --git a/src/journal/fsprg.c b/src/journal/fsprg.c
deleted file mode 100644 (file)
index a9f564c..0000000
+++ /dev/null
@@ -1,389 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/*
- * fsprg v0.1  -  (seekable) forward-secure pseudorandom generator
- * Copyright (C) 2012 B. Poettering
- * Contact: fsprg@point-at-infinity.org
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301  USA
- */
-
-/*
- * See "Practical Secure Logging: Seekable Sequential Key Generators"
- * by G. A. Marson, B. Poettering for details:
- *
- * http://eprint.iacr.org/2013/397
- */
-
-#include <gcrypt.h>
-#include <string.h>
-
-#include "fsprg.h"
-
-#define ISVALID_SECPAR(secpar) (((secpar) % 16 == 0) && ((secpar) >= 16) && ((secpar) <= 16384))
-#define VALIDATE_SECPAR(secpar) assert(ISVALID_SECPAR(secpar));
-
-#define RND_HASH GCRY_MD_SHA256
-#define RND_GEN_P 0x01
-#define RND_GEN_Q 0x02
-#define RND_GEN_X 0x03
-
-/******************************************************************************/
-
-static void mpi_export(void *buf, size_t buflen, const gcry_mpi_t x) {
-        unsigned len;
-        size_t nwritten;
-
-        assert(gcry_mpi_cmp_ui(x, 0) >= 0);
-        len = (gcry_mpi_get_nbits(x) + 7) / 8;
-        assert(len <= buflen);
-        memzero(buf, buflen);
-        gcry_mpi_print(GCRYMPI_FMT_USG, buf + (buflen - len), len, &nwritten, x);
-        assert(nwritten == len);
-}
-
-static gcry_mpi_t mpi_import(const void *buf, size_t buflen) {
-        gcry_mpi_t h;
-        unsigned len;
-
-        gcry_mpi_scan(&h, GCRYMPI_FMT_USG, buf, buflen, NULL);
-        len = (gcry_mpi_get_nbits(h) + 7) / 8;
-        assert(len <= buflen);
-        assert(gcry_mpi_cmp_ui(h, 0) >= 0);
-
-        return h;
-}
-
-static void uint64_export(void *buf, size_t buflen, uint64_t x) {
-        assert(buflen == 8);
-        ((uint8_t*) buf)[0] = (x >> 56) & 0xff;
-        ((uint8_t*) buf)[1] = (x >> 48) & 0xff;
-        ((uint8_t*) buf)[2] = (x >> 40) & 0xff;
-        ((uint8_t*) buf)[3] = (x >> 32) & 0xff;
-        ((uint8_t*) buf)[4] = (x >> 24) & 0xff;
-        ((uint8_t*) buf)[5] = (x >> 16) & 0xff;
-        ((uint8_t*) buf)[6] = (x >>  8) & 0xff;
-        ((uint8_t*) buf)[7] = (x >>  0) & 0xff;
-}
-
-_pure_ static uint64_t uint64_import(const void *buf, size_t buflen) {
-        assert(buflen == 8);
-        return
-                (uint64_t)(((uint8_t*) buf)[0]) << 56 |
-                (uint64_t)(((uint8_t*) buf)[1]) << 48 |
-                (uint64_t)(((uint8_t*) buf)[2]) << 40 |
-                (uint64_t)(((uint8_t*) buf)[3]) << 32 |
-                (uint64_t)(((uint8_t*) buf)[4]) << 24 |
-                (uint64_t)(((uint8_t*) buf)[5]) << 16 |
-                (uint64_t)(((uint8_t*) buf)[6]) <<  8 |
-                (uint64_t)(((uint8_t*) buf)[7]) <<  0;
-}
-
-/* deterministically generate from seed/idx a string of buflen pseudorandom bytes */
-static void det_randomize(void *buf, size_t buflen, const void *seed, size_t seedlen, uint32_t idx) {
-        gcry_md_hd_t hd, hd2;
-        size_t olen, cpylen;
-        uint32_t ctr;
-
-        olen = gcry_md_get_algo_dlen(RND_HASH);
-        gcry_md_open(&hd, RND_HASH, 0);
-        gcry_md_write(hd, seed, seedlen);
-        gcry_md_putc(hd, (idx >> 24) & 0xff);
-        gcry_md_putc(hd, (idx >> 16) & 0xff);
-        gcry_md_putc(hd, (idx >>  8) & 0xff);
-        gcry_md_putc(hd, (idx >>  0) & 0xff);
-
-        for (ctr = 0; buflen; ctr++) {
-                gcry_md_copy(&hd2, hd);
-                gcry_md_putc(hd2, (ctr >> 24) & 0xff);
-                gcry_md_putc(hd2, (ctr >> 16) & 0xff);
-                gcry_md_putc(hd2, (ctr >>  8) & 0xff);
-                gcry_md_putc(hd2, (ctr >>  0) & 0xff);
-                gcry_md_final(hd2);
-                cpylen = (buflen < olen) ? buflen : olen;
-                memcpy(buf, gcry_md_read(hd2, RND_HASH), cpylen);
-                gcry_md_close(hd2);
-                buf += cpylen;
-                buflen -= cpylen;
-        }
-        gcry_md_close(hd);
-}
-
-/* deterministically generate from seed/idx a prime of length `bits' that is 3 (mod 4) */
-static gcry_mpi_t genprime3mod4(int bits, const void *seed, size_t seedlen, uint32_t idx) {
-        size_t buflen = bits / 8;
-        uint8_t buf[buflen];
-        gcry_mpi_t p;
-
-        assert(bits % 8 == 0);
-        assert(buflen > 0);
-
-        det_randomize(buf, buflen, seed, seedlen, idx);
-        buf[0] |= 0xc0; /* set upper two bits, so that n=pq has maximum size */
-        buf[buflen - 1] |= 0x03; /* set lower two bits, to have result 3 (mod 4) */
-
-        p = mpi_import(buf, buflen);
-        while (gcry_prime_check(p, 0))
-                gcry_mpi_add_ui(p, p, 4);
-
-        return p;
-}
-
-/* deterministically generate from seed/idx a quadratic residue (mod n) */
-static gcry_mpi_t gensquare(const gcry_mpi_t n, const void *seed, size_t seedlen, uint32_t idx, unsigned secpar) {
-        size_t buflen = secpar / 8;
-        uint8_t buf[buflen];
-        gcry_mpi_t x;
-
-        det_randomize(buf, buflen, seed, seedlen, idx);
-        buf[0] &= 0x7f; /* clear upper bit, so that we have x < n */
-        x = mpi_import(buf, buflen);
-        assert(gcry_mpi_cmp(x, n) < 0);
-        gcry_mpi_mulm(x, x, x, n);
-        return x;
-}
-
-/* compute 2^m (mod phi(p)), for a prime p */
-static gcry_mpi_t twopowmodphi(uint64_t m, const gcry_mpi_t p) {
-        gcry_mpi_t phi, r;
-        int n;
-
-        phi = gcry_mpi_new(0);
-        gcry_mpi_sub_ui(phi, p, 1);
-
-        /* count number of used bits in m */
-        for (n = 0; (1ULL << n) <= m; n++)
-                ;
-
-        r = gcry_mpi_new(0);
-        gcry_mpi_set_ui(r, 1);
-        while (n) { /* square and multiply algorithm for fast exponentiation */
-                n--;
-                gcry_mpi_mulm(r, r, r, phi);
-                if (m & ((uint64_t)1 << n)) {
-                        gcry_mpi_add(r, r, r);
-                        if (gcry_mpi_cmp(r, phi) >= 0)
-                                gcry_mpi_sub(r, r, phi);
-                }
-        }
-
-        gcry_mpi_release(phi);
-        return r;
-}
-
-/* Decompose $x \in Z_n$ into $(xp,xq) \in Z_p \times Z_q$ using Chinese Remainder Theorem */
-static void CRT_decompose(gcry_mpi_t *xp, gcry_mpi_t *xq, const gcry_mpi_t x, const gcry_mpi_t p, const gcry_mpi_t q) {
-        *xp = gcry_mpi_new(0);
-        *xq = gcry_mpi_new(0);
-        gcry_mpi_mod(*xp, x, p);
-        gcry_mpi_mod(*xq, x, q);
-}
-
-/* Compose $(xp,xq) \in Z_p \times Z_q$ into $x \in Z_n$ using Chinese Remainder Theorem */
-static void CRT_compose(gcry_mpi_t *x, const gcry_mpi_t xp, const gcry_mpi_t xq, const gcry_mpi_t p, const gcry_mpi_t q) {
-        gcry_mpi_t a, u;
-
-        a = gcry_mpi_new(0);
-        u = gcry_mpi_new(0);
-        *x = gcry_mpi_new(0);
-        gcry_mpi_subm(a, xq, xp, q);
-        gcry_mpi_invm(u, p, q);
-        gcry_mpi_mulm(a, a, u, q); /* a = (xq - xp) / p  (mod q) */
-        gcry_mpi_mul(*x, p, a);
-        gcry_mpi_add(*x, *x, xp); /* x = p * ((xq - xp) / p mod q) + xp */
-        gcry_mpi_release(a);
-        gcry_mpi_release(u);
-}
-
-static void initialize_libgcrypt(void) {
-        const char *p;
-        if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
-                return;
-
-        p = gcry_check_version("1.4.5");
-        assert(p);
-
-        /* Turn off "secmem". Clients which whish to make use of this
-         * feature should initialize the library manually */
-        gcry_control(GCRYCTL_DISABLE_SECMEM);
-        gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
-}
-
-/******************************************************************************/
-
-size_t FSPRG_mskinbytes(unsigned _secpar) {
-        VALIDATE_SECPAR(_secpar);
-        return 2 + 2 * (_secpar / 2) / 8; /* to store header,p,q */
-}
-
-size_t FSPRG_mpkinbytes(unsigned _secpar) {
-        VALIDATE_SECPAR(_secpar);
-        return 2 + _secpar / 8; /* to store header,n */
-}
-
-size_t FSPRG_stateinbytes(unsigned _secpar) {
-        VALIDATE_SECPAR(_secpar);
-        return 2 + 2 * _secpar / 8 + 8; /* to store header,n,x,epoch */
-}
-
-static void store_secpar(void *buf, uint16_t secpar) {
-        secpar = secpar / 16 - 1;
-        ((uint8_t*) buf)[0] = (secpar >> 8) & 0xff;
-        ((uint8_t*) buf)[1] = (secpar >> 0) & 0xff;
-}
-
-static uint16_t read_secpar(const void *buf) {
-        uint16_t secpar;
-        secpar =
-                (uint16_t)(((uint8_t*) buf)[0]) << 8 |
-                (uint16_t)(((uint8_t*) buf)[1]) << 0;
-        return 16 * (secpar + 1);
-}
-
-void FSPRG_GenMK(void *msk, void *mpk, const void *seed, size_t seedlen, unsigned _secpar) {
-        uint8_t iseed[FSPRG_RECOMMENDED_SEEDLEN];
-        gcry_mpi_t n, p, q;
-        uint16_t secpar;
-
-        VALIDATE_SECPAR(_secpar);
-        secpar = _secpar;
-
-        initialize_libgcrypt();
-
-        if (!seed) {
-                gcry_randomize(iseed, FSPRG_RECOMMENDED_SEEDLEN, GCRY_STRONG_RANDOM);
-                seed = iseed;
-                seedlen = FSPRG_RECOMMENDED_SEEDLEN;
-        }
-
-        p = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_P);
-        q = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_Q);
-
-        if (msk) {
-                store_secpar(msk + 0, secpar);
-                mpi_export(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8, p);
-                mpi_export(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8, q);
-        }
-
-        if (mpk) {
-                n = gcry_mpi_new(0);
-                gcry_mpi_mul(n, p, q);
-                assert(gcry_mpi_get_nbits(n) == secpar);
-
-                store_secpar(mpk + 0, secpar);
-                mpi_export(mpk + 2, secpar / 8, n);
-
-                gcry_mpi_release(n);
-        }
-
-        gcry_mpi_release(p);
-        gcry_mpi_release(q);
-}
-
-void FSPRG_GenState0(void *state, const void *mpk, const void *seed, size_t seedlen) {
-        gcry_mpi_t n, x;
-        uint16_t secpar;
-
-        initialize_libgcrypt();
-
-        secpar = read_secpar(mpk + 0);
-        n = mpi_import(mpk + 2, secpar / 8);
-        x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
-
-        memcpy(state, mpk, 2 + secpar / 8);
-        mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
-        memzero(state + 2 + 2 * secpar / 8, 8);
-
-        gcry_mpi_release(n);
-        gcry_mpi_release(x);
-}
-
-void FSPRG_Evolve(void *state) {
-        gcry_mpi_t n, x;
-        uint16_t secpar;
-        uint64_t epoch;
-
-        initialize_libgcrypt();
-
-        secpar = read_secpar(state + 0);
-        n = mpi_import(state + 2 + 0 * secpar / 8, secpar / 8);
-        x = mpi_import(state + 2 + 1 * secpar / 8, secpar / 8);
-        epoch = uint64_import(state + 2 + 2 * secpar / 8, 8);
-
-        gcry_mpi_mulm(x, x, x, n);
-        epoch++;
-
-        mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
-        uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
-
-        gcry_mpi_release(n);
-        gcry_mpi_release(x);
-}
-
-uint64_t FSPRG_GetEpoch(const void *state) {
-        uint16_t secpar;
-        secpar = read_secpar(state + 0);
-        return uint64_import(state + 2 + 2 * secpar / 8, 8);
-}
-
-void FSPRG_Seek(void *state, uint64_t epoch, const void *msk, const void *seed, size_t seedlen) {
-        gcry_mpi_t p, q, n, x, xp, xq, kp, kq, xm;
-        uint16_t secpar;
-
-        initialize_libgcrypt();
-
-        secpar = read_secpar(msk + 0);
-        p  = mpi_import(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8);
-        q  = mpi_import(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8);
-
-        n = gcry_mpi_new(0);
-        gcry_mpi_mul(n, p, q);
-
-        x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
-        CRT_decompose(&xp, &xq, x, p, q); /* split (mod n) into (mod p) and (mod q) using CRT */
-
-        kp = twopowmodphi(epoch, p); /* compute 2^epoch (mod phi(p)) */
-        kq = twopowmodphi(epoch, q); /* compute 2^epoch (mod phi(q)) */
-
-        gcry_mpi_powm(xp, xp, kp, p); /* compute x^(2^epoch) (mod p) */
-        gcry_mpi_powm(xq, xq, kq, q); /* compute x^(2^epoch) (mod q) */
-
-        CRT_compose(&xm, xp, xq, p, q); /* combine (mod p) and (mod q) to (mod n) using CRT */
-
-        store_secpar(state + 0, secpar);
-        mpi_export(state + 2 + 0 * secpar / 8, secpar / 8, n);
-        mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, xm);
-        uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
-
-        gcry_mpi_release(p);
-        gcry_mpi_release(q);
-        gcry_mpi_release(n);
-        gcry_mpi_release(x);
-        gcry_mpi_release(xp);
-        gcry_mpi_release(xq);
-        gcry_mpi_release(kp);
-        gcry_mpi_release(kq);
-        gcry_mpi_release(xm);
-}
-
-void FSPRG_GetKey(const void *state, void *key, size_t keylen, uint32_t idx) {
-        uint16_t secpar;
-
-        initialize_libgcrypt();
-
-        secpar = read_secpar(state + 0);
-        det_randomize(key, keylen, state + 2, 2 * secpar / 8 + 8, idx);
-}
diff --git a/src/journal/fsprg.h b/src/journal/fsprg.h
deleted file mode 100644 (file)
index 150d034..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#ifndef __fsprgh__
-#define __fsprgh__
-
-/*
- * fsprg v0.1  -  (seekable) forward-secure pseudorandom generator
- * Copyright (C) 2012 B. Poettering
- * Contact: fsprg@point-at-infinity.org
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301  USA
- *
- */
-
-#include <sys/types.h>
-#include <inttypes.h>
-
-#include "macro.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#define FSPRG_RECOMMENDED_SECPAR 1536
-#define FSPRG_RECOMMENDED_SEEDLEN (96/8)
-
-size_t FSPRG_mskinbytes(unsigned secpar) _const_;
-size_t FSPRG_mpkinbytes(unsigned secpar) _const_;
-size_t FSPRG_stateinbytes(unsigned secpar) _const_;
-
-/* Setup msk and mpk. Providing seed != NULL makes this algorithm deterministic. */
-void FSPRG_GenMK(void *msk, void *mpk, const void *seed, size_t seedlen, unsigned secpar);
-
-/* Initialize state deterministically in dependence on seed. */
-/* Note: in case one wants to run only one GenState0 per GenMK it is safe to use
-   the same seed for both GenMK and GenState0.
-*/
-void FSPRG_GenState0(void *state, const void *mpk, const void *seed, size_t seedlen);
-
-void FSPRG_Evolve(void *state);
-
-uint64_t FSPRG_GetEpoch(const void *state) _pure_;
-
-/* Seek to any arbitrary state (by providing msk together with seed from GenState0). */
-void FSPRG_Seek(void *state, uint64_t epoch, const void *msk, const void *seed, size_t seedlen);
-
-void FSPRG_GetKey(const void *state, void *key, size_t keylen, uint32_t idx);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/src/journal/journal-authenticate.c b/src/journal/journal-authenticate.c
deleted file mode 100644 (file)
index cdc80e2..0000000
+++ /dev/null
@@ -1,562 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <fcntl.h>
-#include <sys/mman.h>
-
-#include "journal-def.h"
-#include "journal-file.h"
-#include "journal-authenticate.h"
-#include "fsprg.h"
-
-static uint64_t journal_file_tag_seqnum(JournalFile *f) {
-        uint64_t r;
-
-        assert(f);
-
-        r = le64toh(f->header->n_tags) + 1;
-        f->header->n_tags = htole64(r);
-
-        return r;
-}
-
-int journal_file_append_tag(JournalFile *f) {
-        Object *o;
-        uint64_t p;
-        int r;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        if (!f->hmac_running)
-                return 0;
-
-        assert(f->hmac);
-
-        r = journal_file_append_object(f, OBJECT_TAG, sizeof(struct TagObject), &o, &p);
-        if (r < 0)
-                return r;
-
-        o->tag.seqnum = htole64(journal_file_tag_seqnum(f));
-        o->tag.epoch = htole64(FSPRG_GetEpoch(f->fsprg_state));
-
-        log_debug("Writing tag %"PRIu64" for epoch %"PRIu64"",
-                  le64toh(o->tag.seqnum),
-                  FSPRG_GetEpoch(f->fsprg_state));
-
-        /* Add the tag object itself, so that we can protect its
-         * header. This will exclude the actual hash value in it */
-        r = journal_file_hmac_put_object(f, OBJECT_TAG, o, p);
-        if (r < 0)
-                return r;
-
-        /* Get the HMAC tag and store it in the object */
-        memcpy(o->tag.tag, gcry_md_read(f->hmac, 0), TAG_LENGTH);
-        f->hmac_running = false;
-
-        return 0;
-}
-
-int journal_file_hmac_start(JournalFile *f) {
-        uint8_t key[256 / 8]; /* Let's pass 256 bit from FSPRG to HMAC */
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        if (f->hmac_running)
-                return 0;
-
-        /* Prepare HMAC for next cycle */
-        gcry_md_reset(f->hmac);
-        FSPRG_GetKey(f->fsprg_state, key, sizeof(key), 0);
-        gcry_md_setkey(f->hmac, key, sizeof(key));
-
-        f->hmac_running = true;
-
-        return 0;
-}
-
-static int journal_file_get_epoch(JournalFile *f, uint64_t realtime, uint64_t *epoch) {
-        uint64_t t;
-
-        assert(f);
-        assert(epoch);
-        assert(f->seal);
-
-        if (f->fss_start_usec == 0 ||
-            f->fss_interval_usec == 0)
-                return -EOPNOTSUPP;
-
-        if (realtime < f->fss_start_usec)
-                return -ESTALE;
-
-        t = realtime - f->fss_start_usec;
-        t = t / f->fss_interval_usec;
-
-        *epoch = t;
-        return 0;
-}
-
-static int journal_file_fsprg_need_evolve(JournalFile *f, uint64_t realtime) {
-        uint64_t goal, epoch;
-        int r;
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        r = journal_file_get_epoch(f, realtime, &goal);
-        if (r < 0)
-                return r;
-
-        epoch = FSPRG_GetEpoch(f->fsprg_state);
-        if (epoch > goal)
-                return -ESTALE;
-
-        return epoch != goal;
-}
-
-int journal_file_fsprg_evolve(JournalFile *f, uint64_t realtime) {
-        uint64_t goal, epoch;
-        int r;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        r = journal_file_get_epoch(f, realtime, &goal);
-        if (r < 0)
-                return r;
-
-        epoch = FSPRG_GetEpoch(f->fsprg_state);
-        if (epoch < goal)
-                log_debug("Evolving FSPRG key from epoch %"PRIu64" to %"PRIu64".", epoch, goal);
-
-        for (;;) {
-                if (epoch > goal)
-                        return -ESTALE;
-                if (epoch == goal)
-                        return 0;
-
-                FSPRG_Evolve(f->fsprg_state);
-                epoch = FSPRG_GetEpoch(f->fsprg_state);
-        }
-}
-
-int journal_file_fsprg_seek(JournalFile *f, uint64_t goal) {
-        void *msk;
-        uint64_t epoch;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        assert(f->fsprg_seed);
-
-        if (f->fsprg_state) {
-                /* Cheaper... */
-
-                epoch = FSPRG_GetEpoch(f->fsprg_state);
-                if (goal == epoch)
-                        return 0;
-
-                if (goal == epoch+1) {
-                        FSPRG_Evolve(f->fsprg_state);
-                        return 0;
-                }
-        } else {
-                f->fsprg_state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR);
-                f->fsprg_state = malloc(f->fsprg_state_size);
-
-                if (!f->fsprg_state)
-                        return -ENOMEM;
-        }
-
-        log_debug("Seeking FSPRG key to %"PRIu64".", goal);
-
-        msk = alloca(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR));
-        FSPRG_GenMK(msk, NULL, f->fsprg_seed, f->fsprg_seed_size, FSPRG_RECOMMENDED_SECPAR);
-        FSPRG_Seek(f->fsprg_state, goal, msk, f->fsprg_seed, f->fsprg_seed_size);
-        return 0;
-}
-
-int journal_file_maybe_append_tag(JournalFile *f, uint64_t realtime) {
-        int r;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        if (realtime <= 0)
-                realtime = now(CLOCK_REALTIME);
-
-        r = journal_file_fsprg_need_evolve(f, realtime);
-        if (r <= 0)
-                return 0;
-
-        r = journal_file_append_tag(f);
-        if (r < 0)
-                return r;
-
-        r = journal_file_fsprg_evolve(f, realtime);
-        if (r < 0)
-                return r;
-
-        return 0;
-}
-
-int journal_file_hmac_put_object(JournalFile *f, ObjectType type, Object *o, uint64_t p) {
-        int r;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        r = journal_file_hmac_start(f);
-        if (r < 0)
-                return r;
-
-        if (!o) {
-                r = journal_file_move_to_object(f, type, p, &o);
-                if (r < 0)
-                        return r;
-        } else {
-                if (type > OBJECT_UNUSED && o->object.type != type)
-                        return -EBADMSG;
-        }
-
-        gcry_md_write(f->hmac, o, offsetof(ObjectHeader, payload));
-
-        switch (o->object.type) {
-
-        case OBJECT_DATA:
-                /* All but hash and payload are mutable */
-                gcry_md_write(f->hmac, &o->data.hash, sizeof(o->data.hash));
-                gcry_md_write(f->hmac, o->data.payload, le64toh(o->object.size) - offsetof(DataObject, payload));
-                break;
-
-        case OBJECT_FIELD:
-                /* Same here */
-                gcry_md_write(f->hmac, &o->field.hash, sizeof(o->field.hash));
-                gcry_md_write(f->hmac, o->field.payload, le64toh(o->object.size) - offsetof(FieldObject, payload));
-                break;
-
-        case OBJECT_ENTRY:
-                /* All */
-                gcry_md_write(f->hmac, &o->entry.seqnum, le64toh(o->object.size) - offsetof(EntryObject, seqnum));
-                break;
-
-        case OBJECT_FIELD_HASH_TABLE:
-        case OBJECT_DATA_HASH_TABLE:
-        case OBJECT_ENTRY_ARRAY:
-                /* Nothing: everything is mutable */
-                break;
-
-        case OBJECT_TAG:
-                /* All but the tag itself */
-                gcry_md_write(f->hmac, &o->tag.seqnum, sizeof(o->tag.seqnum));
-                gcry_md_write(f->hmac, &o->tag.epoch, sizeof(o->tag.epoch));
-                break;
-        default:
-                return -EINVAL;
-        }
-
-        return 0;
-}
-
-int journal_file_hmac_put_header(JournalFile *f) {
-        int r;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        r = journal_file_hmac_start(f);
-        if (r < 0)
-                return r;
-
-        /* All but state+reserved, boot_id, arena_size,
-         * tail_object_offset, n_objects, n_entries,
-         * tail_entry_seqnum, head_entry_seqnum, entry_array_offset,
-         * head_entry_realtime, tail_entry_realtime,
-         * tail_entry_monotonic, n_data, n_fields, n_tags,
-         * n_entry_arrays. */
-
-        gcry_md_write(f->hmac, f->header->signature, offsetof(Header, state) - offsetof(Header, signature));
-        gcry_md_write(f->hmac, &f->header->file_id, offsetof(Header, boot_id) - offsetof(Header, file_id));
-        gcry_md_write(f->hmac, &f->header->seqnum_id, offsetof(Header, arena_size) - offsetof(Header, seqnum_id));
-        gcry_md_write(f->hmac, &f->header->data_hash_table_offset, offsetof(Header, tail_object_offset) - offsetof(Header, data_hash_table_offset));
-
-        return 0;
-}
-
-int journal_file_fss_load(JournalFile *f) {
-        int r, fd = -1;
-        char *p = NULL;
-        struct stat st;
-        FSSHeader *m = NULL;
-        sd_id128_t machine;
-
-        assert(f);
-
-        if (!f->seal)
-                return 0;
-
-        r = sd_id128_get_machine(&machine);
-        if (r < 0)
-                return r;
-
-        if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/fss",
-                     SD_ID128_FORMAT_VAL(machine)) < 0)
-                return -ENOMEM;
-
-        fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
-        if (fd < 0) {
-                if (errno != ENOENT)
-                        log_error_errno(errno, "Failed to open %s: %m", p);
-
-                r = -errno;
-                goto finish;
-        }
-
-        if (fstat(fd, &st) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
-        if (st.st_size < (off_t) sizeof(FSSHeader)) {
-                r = -ENODATA;
-                goto finish;
-        }
-
-        m = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED, fd, 0);
-        if (m == MAP_FAILED) {
-                m = NULL;
-                r = -errno;
-                goto finish;
-        }
-
-        if (memcmp(m->signature, FSS_HEADER_SIGNATURE, 8) != 0) {
-                r = -EBADMSG;
-                goto finish;
-        }
-
-        if (m->incompatible_flags != 0) {
-                r = -EPROTONOSUPPORT;
-                goto finish;
-        }
-
-        if (le64toh(m->header_size) < sizeof(FSSHeader)) {
-                r = -EBADMSG;
-                goto finish;
-        }
-
-        if (le64toh(m->fsprg_state_size) != FSPRG_stateinbytes(le16toh(m->fsprg_secpar))) {
-                r = -EBADMSG;
-                goto finish;
-        }
-
-        f->fss_file_size = le64toh(m->header_size) + le64toh(m->fsprg_state_size);
-        if ((uint64_t) st.st_size < f->fss_file_size) {
-                r = -ENODATA;
-                goto finish;
-        }
-
-        if (!sd_id128_equal(machine, m->machine_id)) {
-                r = -EHOSTDOWN;
-                goto finish;
-        }
-
-        if (le64toh(m->start_usec) <= 0 ||
-            le64toh(m->interval_usec) <= 0) {
-                r = -EBADMSG;
-                goto finish;
-        }
-
-        f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
-        if (f->fss_file == MAP_FAILED) {
-                f->fss_file = NULL;
-                r = -errno;
-                goto finish;
-        }
-
-        f->fss_start_usec = le64toh(f->fss_file->start_usec);
-        f->fss_interval_usec = le64toh(f->fss_file->interval_usec);
-
-        f->fsprg_state = (uint8_t*) f->fss_file + le64toh(f->fss_file->header_size);
-        f->fsprg_state_size = le64toh(f->fss_file->fsprg_state_size);
-
-        r = 0;
-
-finish:
-        if (m)
-                munmap(m, PAGE_ALIGN(sizeof(FSSHeader)));
-
-        safe_close(fd);
-        free(p);
-
-        return r;
-}
-
-static void initialize_libgcrypt(void) {
-        const char *p;
-
-        if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
-                return;
-
-        p = gcry_check_version("1.4.5");
-        assert(p);
-
-        gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
-}
-
-int journal_file_hmac_setup(JournalFile *f) {
-        gcry_error_t e;
-
-        if (!f->seal)
-                return 0;
-
-        initialize_libgcrypt();
-
-        e = gcry_md_open(&f->hmac, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
-        if (e != 0)
-                return -EOPNOTSUPP;
-
-        return 0;
-}
-
-int journal_file_append_first_tag(JournalFile *f) {
-        int r;
-        uint64_t p;
-
-        if (!f->seal)
-                return 0;
-
-        log_debug("Calculating first tag...");
-
-        r = journal_file_hmac_put_header(f);
-        if (r < 0)
-                return r;
-
-        p = le64toh(f->header->field_hash_table_offset);
-        if (p < offsetof(Object, hash_table.items))
-                return -EINVAL;
-        p -= offsetof(Object, hash_table.items);
-
-        r = journal_file_hmac_put_object(f, OBJECT_FIELD_HASH_TABLE, NULL, p);
-        if (r < 0)
-                return r;
-
-        p = le64toh(f->header->data_hash_table_offset);
-        if (p < offsetof(Object, hash_table.items))
-                return -EINVAL;
-        p -= offsetof(Object, hash_table.items);
-
-        r = journal_file_hmac_put_object(f, OBJECT_DATA_HASH_TABLE, NULL, p);
-        if (r < 0)
-                return r;
-
-        r = journal_file_append_tag(f);
-        if (r < 0)
-                return r;
-
-        return 0;
-}
-
-int journal_file_parse_verification_key(JournalFile *f, const char *key) {
-        uint8_t *seed;
-        size_t seed_size, c;
-        const char *k;
-        int r;
-        unsigned long long start, interval;
-
-        seed_size = FSPRG_RECOMMENDED_SEEDLEN;
-        seed = malloc(seed_size);
-        if (!seed)
-                return -ENOMEM;
-
-        k = key;
-        for (c = 0; c < seed_size; c++) {
-                int x, y;
-
-                while (*k == '-')
-                        k++;
-
-                x = unhexchar(*k);
-                if (x < 0) {
-                        free(seed);
-                        return -EINVAL;
-                }
-                k++;
-                y = unhexchar(*k);
-                if (y < 0) {
-                        free(seed);
-                        return -EINVAL;
-                }
-                k++;
-
-                seed[c] = (uint8_t) (x * 16 + y);
-        }
-
-        if (*k != '/') {
-                free(seed);
-                return -EINVAL;
-        }
-        k++;
-
-        r = sscanf(k, "%llx-%llx", &start, &interval);
-        if (r != 2) {
-                free(seed);
-                return -EINVAL;
-        }
-
-        f->fsprg_seed = seed;
-        f->fsprg_seed_size = seed_size;
-
-        f->fss_start_usec = start * interval;
-        f->fss_interval_usec = interval;
-
-        return 0;
-}
-
-bool journal_file_next_evolve_usec(JournalFile *f, usec_t *u) {
-        uint64_t epoch;
-
-        assert(f);
-        assert(u);
-
-        if (!f->seal)
-                return false;
-
-        epoch = FSPRG_GetEpoch(f->fsprg_state);
-
-        *u = (usec_t) (f->fss_start_usec + f->fss_interval_usec * epoch + f->fss_interval_usec);
-
-        return true;
-}
diff --git a/src/journal/journal-authenticate.h b/src/journal/journal-authenticate.h
deleted file mode 100644 (file)
index 118bb13..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
-  This file is part of systemd.
-
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdbool.h>
-
-#include "journal-file.h"
-
-int journal_file_append_tag(JournalFile *f);
-int journal_file_maybe_append_tag(JournalFile *f, uint64_t realtime);
-int journal_file_append_first_tag(JournalFile *f);
-
-int journal_file_hmac_setup(JournalFile *f);
-int journal_file_hmac_start(JournalFile *f);
-int journal_file_hmac_put_header(JournalFile *f);
-int journal_file_hmac_put_object(JournalFile *f, ObjectType type, Object *o, uint64_t p);
-
-int journal_file_fss_load(JournalFile *f);
-int journal_file_parse_verification_key(JournalFile *f, const char *key);
-
-int journal_file_fsprg_evolve(JournalFile *f, uint64_t realtime);
-int journal_file_fsprg_seek(JournalFile *f, uint64_t epoch);
-
-bool journal_file_next_evolve_usec(JournalFile *f, usec_t *u);
diff --git a/src/journal/journal-def.h b/src/journal/journal-def.h
deleted file mode 100644 (file)
index ab089cb..0000000
+++ /dev/null
@@ -1,240 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include "sparse-endian.h"
-
-#include "systemd/sd-id128.h"
-
-#include "macro.h"
-
-/*
- * If you change this file you probably should also change its documentation:
- *
- * http://www.freedesktop.org/wiki/Software/systemd/journal-files
- *
- */
-
-typedef struct Header Header;
-
-typedef struct ObjectHeader ObjectHeader;
-typedef union Object Object;
-
-typedef struct DataObject DataObject;
-typedef struct FieldObject FieldObject;
-typedef struct EntryObject EntryObject;
-typedef struct HashTableObject HashTableObject;
-typedef struct EntryArrayObject EntryArrayObject;
-typedef struct TagObject TagObject;
-
-typedef struct EntryItem EntryItem;
-typedef struct HashItem HashItem;
-
-typedef struct FSSHeader FSSHeader;
-
-/* Object types */
-typedef enum ObjectType {
-        OBJECT_UNUSED, /* also serves as "any type" or "additional context" */
-        OBJECT_DATA,
-        OBJECT_FIELD,
-        OBJECT_ENTRY,
-        OBJECT_DATA_HASH_TABLE,
-        OBJECT_FIELD_HASH_TABLE,
-        OBJECT_ENTRY_ARRAY,
-        OBJECT_TAG,
-        _OBJECT_TYPE_MAX
-} ObjectType;
-
-/* Object flags */
-enum {
-        OBJECT_COMPRESSED_XZ = 1 << 0,
-        OBJECT_COMPRESSED_LZ4 = 1 << 1,
-        _OBJECT_COMPRESSED_MAX
-};
-
-#define OBJECT_COMPRESSION_MASK (OBJECT_COMPRESSED_XZ | OBJECT_COMPRESSED_LZ4)
-
-struct ObjectHeader {
-        uint8_t type;
-        uint8_t flags;
-        uint8_t reserved[6];
-        le64_t size;
-        uint8_t payload[];
-} _packed_;
-
-struct DataObject {
-        ObjectHeader object;
-        le64_t hash;
-        le64_t next_hash_offset;
-        le64_t next_field_offset;
-        le64_t entry_offset; /* the first array entry we store inline */
-        le64_t entry_array_offset;
-        le64_t n_entries;
-        uint8_t payload[];
-} _packed_;
-
-struct FieldObject {
-        ObjectHeader object;
-        le64_t hash;
-        le64_t next_hash_offset;
-        le64_t head_data_offset;
-        uint8_t payload[];
-} _packed_;
-
-struct EntryItem {
-        le64_t object_offset;
-        le64_t hash;
-} _packed_;
-
-struct EntryObject {
-        ObjectHeader object;
-        le64_t seqnum;
-        le64_t realtime;
-        le64_t monotonic;
-        sd_id128_t boot_id;
-        le64_t xor_hash;
-        EntryItem items[];
-} _packed_;
-
-struct HashItem {
-        le64_t head_hash_offset;
-        le64_t tail_hash_offset;
-} _packed_;
-
-struct HashTableObject {
-        ObjectHeader object;
-        HashItem items[];
-} _packed_;
-
-struct EntryArrayObject {
-        ObjectHeader object;
-        le64_t next_entry_array_offset;
-        le64_t items[];
-} _packed_;
-
-#define TAG_LENGTH (256/8)
-
-struct TagObject {
-        ObjectHeader object;
-        le64_t seqnum;
-        le64_t epoch;
-        uint8_t tag[TAG_LENGTH]; /* SHA-256 HMAC */
-} _packed_;
-
-union Object {
-        ObjectHeader object;
-        DataObject data;
-        FieldObject field;
-        EntryObject entry;
-        HashTableObject hash_table;
-        EntryArrayObject entry_array;
-        TagObject tag;
-};
-
-enum {
-        STATE_OFFLINE = 0,
-        STATE_ONLINE = 1,
-        STATE_ARCHIVED = 2,
-        _STATE_MAX
-};
-
-/* Header flags */
-enum {
-        HEADER_INCOMPATIBLE_COMPRESSED_XZ = 1 << 0,
-        HEADER_INCOMPATIBLE_COMPRESSED_LZ4 = 1 << 1,
-};
-
-#define HEADER_INCOMPATIBLE_ANY (HEADER_INCOMPATIBLE_COMPRESSED_XZ|HEADER_INCOMPATIBLE_COMPRESSED_LZ4)
-
-#if defined(HAVE_XZ) && defined(HAVE_LZ4)
-#  define HEADER_INCOMPATIBLE_SUPPORTED HEADER_INCOMPATIBLE_ANY
-#elif defined(HAVE_XZ)
-#  define HEADER_INCOMPATIBLE_SUPPORTED HEADER_INCOMPATIBLE_COMPRESSED_XZ
-#elif defined(HAVE_LZ4)
-#  define HEADER_INCOMPATIBLE_SUPPORTED HEADER_INCOMPATIBLE_COMPRESSED_LZ4
-#else
-#  define HEADER_INCOMPATIBLE_SUPPORTED 0
-#endif
-
-enum {
-        HEADER_COMPATIBLE_SEALED = 1
-};
-
-#define HEADER_COMPATIBLE_ANY HEADER_COMPATIBLE_SEALED
-#ifdef HAVE_GCRYPT
-#  define HEADER_COMPATIBLE_SUPPORTED HEADER_COMPATIBLE_SEALED
-#else
-#  define HEADER_COMPATIBLE_SUPPORTED 0
-#endif
-
-#define HEADER_SIGNATURE ((char[]) { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' })
-
-struct Header {
-        uint8_t signature[8]; /* "LPKSHHRH" */
-        le32_t compatible_flags;
-        le32_t incompatible_flags;
-        uint8_t state;
-        uint8_t reserved[7];
-        sd_id128_t file_id;
-        sd_id128_t machine_id;
-        sd_id128_t boot_id;    /* last writer */
-        sd_id128_t seqnum_id;
-        le64_t header_size;
-        le64_t arena_size;
-        le64_t data_hash_table_offset;
-        le64_t data_hash_table_size;
-        le64_t field_hash_table_offset;
-        le64_t field_hash_table_size;
-        le64_t tail_object_offset;
-        le64_t n_objects;
-        le64_t n_entries;
-        le64_t tail_entry_seqnum;
-        le64_t head_entry_seqnum;
-        le64_t entry_array_offset;
-        le64_t head_entry_realtime;
-        le64_t tail_entry_realtime;
-        le64_t tail_entry_monotonic;
-        /* Added in 187 */
-        le64_t n_data;
-        le64_t n_fields;
-        /* Added in 189 */
-        le64_t n_tags;
-        le64_t n_entry_arrays;
-
-        /* Size: 224 */
-} _packed_;
-
-#define FSS_HEADER_SIGNATURE ((char[]) { 'K', 'S', 'H', 'H', 'R', 'H', 'L', 'P' })
-
-struct FSSHeader {
-        uint8_t signature[8]; /* "KSHHRHLP" */
-        le32_t compatible_flags;
-        le32_t incompatible_flags;
-        sd_id128_t machine_id;
-        sd_id128_t boot_id;    /* last writer */
-        le64_t header_size;
-        le64_t start_usec;
-        le64_t interval_usec;
-        le16_t fsprg_secpar;
-        le16_t reserved[3];
-        le64_t fsprg_state_size;
-} _packed_;
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
deleted file mode 100644 (file)
index 5a4e7cb..0000000
+++ /dev/null
@@ -1,3119 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/mman.h>
-#include <errno.h>
-#include <sys/uio.h>
-#include <unistd.h>
-#include <sys/statvfs.h>
-#include <fcntl.h>
-#include <stddef.h>
-#include <linux/fs.h>
-
-#include "btrfs-util.h"
-#include "journal-def.h"
-#include "journal-file.h"
-#include "journal-authenticate.h"
-#include "lookup3.h"
-#include "compress.h"
-
-#define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
-#define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem))
-
-#define COMPRESSION_SIZE_THRESHOLD (512ULL)
-
-/* This is the minimum journal file size */
-#define JOURNAL_FILE_SIZE_MIN (4ULL*1024ULL*1024ULL)           /* 4 MiB */
-
-/* These are the lower and upper bounds if we deduce the max_use value
- * from the file system size */
-#define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL)           /* 1 MiB */
-#define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL)   /* 4 GiB */
-
-/* This is the upper bound if we deduce max_size from max_use */
-#define DEFAULT_MAX_SIZE_UPPER (128ULL*1024ULL*1024ULL)        /* 128 MiB */
-
-/* This is the upper bound if we deduce the keep_free value from the
- * file system size */
-#define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
-
-/* This is the keep_free value when we can't determine the system
- * size */
-#define DEFAULT_KEEP_FREE (1024ULL*1024ULL)                    /* 1 MB */
-
-/* n_data was the first entry we added after the initial file format design */
-#define HEADER_SIZE_MIN ALIGN64(offsetof(Header, n_data))
-
-/* How many entries to keep in the entry array chain cache at max */
-#define CHAIN_CACHE_MAX 20
-
-/* How much to increase the journal file size at once each time we allocate something new. */
-#define FILE_SIZE_INCREASE (8ULL*1024ULL*1024ULL)              /* 8MB */
-
-/* Reread fstat() of the file for detecting deletions at least this often */
-#define LAST_STAT_REFRESH_USEC (5*USEC_PER_SEC)
-
-/* The mmap context to use for the header we pick as one above the last defined typed */
-#define CONTEXT_HEADER _OBJECT_TYPE_MAX
-
-static int journal_file_set_online(JournalFile *f) {
-        assert(f);
-
-        if (!f->writable)
-                return -EPERM;
-
-        if (!(f->fd >= 0 && f->header))
-                return -EINVAL;
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd))
-                return -EIO;
-
-        switch(f->header->state) {
-                case STATE_ONLINE:
-                        return 0;
-
-                case STATE_OFFLINE:
-                        f->header->state = STATE_ONLINE;
-                        fsync(f->fd);
-                        return 0;
-
-                default:
-                        return -EINVAL;
-        }
-}
-
-int journal_file_set_offline(JournalFile *f) {
-        assert(f);
-
-        if (!f->writable)
-                return -EPERM;
-
-        if (!(f->fd >= 0 && f->header))
-                return -EINVAL;
-
-        if (f->header->state != STATE_ONLINE)
-                return 0;
-
-        fsync(f->fd);
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd))
-                return -EIO;
-
-        f->header->state = STATE_OFFLINE;
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd))
-                return -EIO;
-
-        fsync(f->fd);
-
-        return 0;
-}
-
-void journal_file_close(JournalFile *f) {
-        assert(f);
-
-#ifdef HAVE_GCRYPT
-        /* Write the final tag */
-        if (f->seal && f->writable)
-                journal_file_append_tag(f);
-#endif
-
-        journal_file_set_offline(f);
-
-        if (f->mmap && f->fd >= 0)
-                mmap_cache_close_fd(f->mmap, f->fd);
-
-        if (f->fd >= 0 && f->defrag_on_close) {
-
-                /* Be friendly to btrfs: turn COW back on again now,
-                 * and defragment the file. We won't write to the file
-                 * ever again, hence remove all fragmentation, and
-                 * reenable all the good bits COW usually provides
-                 * (such as data checksumming). */
-
-                (void) chattr_fd(f->fd, false, FS_NOCOW_FL);
-                (void) btrfs_defrag_fd(f->fd);
-        }
-
-        safe_close(f->fd);
-        free(f->path);
-
-        if (f->mmap)
-                mmap_cache_unref(f->mmap);
-
-        ordered_hashmap_free_free(f->chain_cache);
-
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-        free(f->compress_buffer);
-#endif
-
-#ifdef HAVE_GCRYPT
-        if (f->fss_file)
-                munmap(f->fss_file, PAGE_ALIGN(f->fss_file_size));
-        else if (f->fsprg_state)
-                free(f->fsprg_state);
-
-        free(f->fsprg_seed);
-
-        if (f->hmac)
-                gcry_md_close(f->hmac);
-#endif
-
-        free(f);
-}
-
-static int journal_file_init_header(JournalFile *f, JournalFile *template) {
-        Header h = {};
-        ssize_t k;
-        int r;
-
-        assert(f);
-
-        memcpy(h.signature, HEADER_SIGNATURE, 8);
-        h.header_size = htole64(ALIGN64(sizeof(h)));
-
-        h.incompatible_flags |= htole32(
-                f->compress_xz * HEADER_INCOMPATIBLE_COMPRESSED_XZ |
-                f->compress_lz4 * HEADER_INCOMPATIBLE_COMPRESSED_LZ4);
-
-        h.compatible_flags = htole32(
-                f->seal * HEADER_COMPATIBLE_SEALED);
-
-        r = sd_id128_randomize(&h.file_id);
-        if (r < 0)
-                return r;
-
-        if (template) {
-                h.seqnum_id = template->header->seqnum_id;
-                h.tail_entry_seqnum = template->header->tail_entry_seqnum;
-        } else
-                h.seqnum_id = h.file_id;
-
-        k = pwrite(f->fd, &h, sizeof(h), 0);
-        if (k < 0)
-                return -errno;
-
-        if (k != sizeof(h))
-                return -EIO;
-
-        return 0;
-}
-
-static int journal_file_refresh_header(JournalFile *f) {
-        sd_id128_t boot_id;
-        int r;
-
-        assert(f);
-
-        r = sd_id128_get_machine(&f->header->machine_id);
-        if (r < 0)
-                return r;
-
-        r = sd_id128_get_boot(&boot_id);
-        if (r < 0)
-                return r;
-
-        if (sd_id128_equal(boot_id, f->header->boot_id))
-                f->tail_entry_monotonic_valid = true;
-
-        f->header->boot_id = boot_id;
-
-        r = journal_file_set_online(f);
-
-        /* Sync the online state to disk */
-        fsync(f->fd);
-
-        return r;
-}
-
-static int journal_file_verify_header(JournalFile *f) {
-        uint32_t flags;
-
-        assert(f);
-
-        if (memcmp(f->header->signature, HEADER_SIGNATURE, 8))
-                return -EBADMSG;
-
-        /* In both read and write mode we refuse to open files with
-         * incompatible flags we don't know */
-        flags = le32toh(f->header->incompatible_flags);
-        if (flags & ~HEADER_INCOMPATIBLE_SUPPORTED) {
-                if (flags & ~HEADER_INCOMPATIBLE_ANY)
-                        log_debug("Journal file %s has unknown incompatible flags %"PRIx32,
-                                  f->path, flags & ~HEADER_INCOMPATIBLE_ANY);
-                flags = (flags & HEADER_INCOMPATIBLE_ANY) & ~HEADER_INCOMPATIBLE_SUPPORTED;
-                if (flags)
-                        log_debug("Journal file %s uses incompatible flags %"PRIx32
-                                  " disabled at compilation time.", f->path, flags);
-                return -EPROTONOSUPPORT;
-        }
-
-        /* When open for writing we refuse to open files with
-         * compatible flags, too */
-        flags = le32toh(f->header->compatible_flags);
-        if (f->writable && (flags & ~HEADER_COMPATIBLE_SUPPORTED)) {
-                if (flags & ~HEADER_COMPATIBLE_ANY)
-                        log_debug("Journal file %s has unknown compatible flags %"PRIx32,
-                                  f->path, flags & ~HEADER_COMPATIBLE_ANY);
-                flags = (flags & HEADER_COMPATIBLE_ANY) & ~HEADER_COMPATIBLE_SUPPORTED;
-                if (flags)
-                        log_debug("Journal file %s uses compatible flags %"PRIx32
-                                  " disabled at compilation time.", f->path, flags);
-                return -EPROTONOSUPPORT;
-        }
-
-        if (f->header->state >= _STATE_MAX)
-                return -EBADMSG;
-
-        /* The first addition was n_data, so check that we are at least this large */
-        if (le64toh(f->header->header_size) < HEADER_SIZE_MIN)
-                return -EBADMSG;
-
-        if (JOURNAL_HEADER_SEALED(f->header) && !JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
-                return -EBADMSG;
-
-        if ((le64toh(f->header->header_size) + le64toh(f->header->arena_size)) > (uint64_t) f->last_stat.st_size)
-                return -ENODATA;
-
-        if (le64toh(f->header->tail_object_offset) > (le64toh(f->header->header_size) + le64toh(f->header->arena_size)))
-                return -ENODATA;
-
-        if (!VALID64(le64toh(f->header->data_hash_table_offset)) ||
-            !VALID64(le64toh(f->header->field_hash_table_offset)) ||
-            !VALID64(le64toh(f->header->tail_object_offset)) ||
-            !VALID64(le64toh(f->header->entry_array_offset)))
-                return -ENODATA;
-
-        if (f->writable) {
-                uint8_t state;
-                sd_id128_t machine_id;
-                int r;
-
-                r = sd_id128_get_machine(&machine_id);
-                if (r < 0)
-                        return r;
-
-                if (!sd_id128_equal(machine_id, f->header->machine_id))
-                        return -EHOSTDOWN;
-
-                state = f->header->state;
-
-                if (state == STATE_ONLINE) {
-                        log_debug("Journal file %s is already online. Assuming unclean closing.", f->path);
-                        return -EBUSY;
-                } else if (state == STATE_ARCHIVED)
-                        return -ESHUTDOWN;
-                else if (state != STATE_OFFLINE) {
-                        log_debug("Journal file %s has unknown state %i.", f->path, state);
-                        return -EBUSY;
-                }
-        }
-
-        f->compress_xz = JOURNAL_HEADER_COMPRESSED_XZ(f->header);
-        f->compress_lz4 = JOURNAL_HEADER_COMPRESSED_LZ4(f->header);
-
-        f->seal = JOURNAL_HEADER_SEALED(f->header);
-
-        return 0;
-}
-
-static int journal_file_fstat(JournalFile *f) {
-        assert(f);
-        assert(f->fd >= 0);
-
-        if (fstat(f->fd, &f->last_stat) < 0)
-                return -errno;
-
-        f->last_stat_usec = now(CLOCK_MONOTONIC);
-
-        /* Refuse appending to files that are already deleted */
-        if (f->last_stat.st_nlink <= 0)
-                return -EIDRM;
-
-        return 0;
-}
-
-static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
-        uint64_t old_size, new_size;
-        int r;
-
-        assert(f);
-
-        /* We assume that this file is not sparse, and we know that
-         * for sure, since we always call posix_fallocate()
-         * ourselves */
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd))
-                return -EIO;
-
-        old_size =
-                le64toh(f->header->header_size) +
-                le64toh(f->header->arena_size);
-
-        new_size = PAGE_ALIGN(offset + size);
-        if (new_size < le64toh(f->header->header_size))
-                new_size = le64toh(f->header->header_size);
-
-        if (new_size <= old_size) {
-
-                /* We already pre-allocated enough space, but before
-                 * we write to it, let's check with fstat() if the
-                 * file got deleted, in order make sure we don't throw
-                 * away the data immediately. Don't check fstat() for
-                 * all writes though, but only once ever 10s. */
-
-                if (f->last_stat_usec + LAST_STAT_REFRESH_USEC > now(CLOCK_MONOTONIC))
-                        return 0;
-
-                return journal_file_fstat(f);
-        }
-
-        /* Allocate more space. */
-
-        if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
-                return -E2BIG;
-
-        if (new_size > f->metrics.min_size && f->metrics.keep_free > 0) {
-                struct statvfs svfs;
-
-                if (fstatvfs(f->fd, &svfs) >= 0) {
-                        uint64_t available;
-
-                        available = svfs.f_bfree * svfs.f_bsize;
-
-                        if (available >= f->metrics.keep_free)
-                                available -= f->metrics.keep_free;
-                        else
-                                available = 0;
-
-                        if (new_size - old_size > available)
-                                return -E2BIG;
-                }
-        }
-
-        /* Increase by larger blocks at once */
-        new_size = ((new_size+FILE_SIZE_INCREASE-1) / FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE;
-        if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
-                new_size = f->metrics.max_size;
-
-        /* Note that the glibc fallocate() fallback is very
-           inefficient, hence we try to minimize the allocation area
-           as we can. */
-        r = posix_fallocate(f->fd, old_size, new_size - old_size);
-        if (r != 0)
-                return -r;
-
-        f->header->arena_size = htole64(new_size - le64toh(f->header->header_size));
-
-        return journal_file_fstat(f);
-}
-
-static unsigned type_to_context(ObjectType type) {
-        /* One context for each type, plus one catch-all for the rest */
-        assert_cc(_OBJECT_TYPE_MAX <= MMAP_CACHE_MAX_CONTEXTS);
-        assert_cc(CONTEXT_HEADER < MMAP_CACHE_MAX_CONTEXTS);
-        return type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX ? type : 0;
-}
-
-static int journal_file_move_to(JournalFile *f, ObjectType type, bool keep_always, uint64_t offset, uint64_t size, void **ret) {
-        int r;
-
-        assert(f);
-        assert(ret);
-
-        if (size <= 0)
-                return -EINVAL;
-
-        /* Avoid SIGBUS on invalid accesses */
-        if (offset + size > (uint64_t) f->last_stat.st_size) {
-                /* Hmm, out of range? Let's refresh the fstat() data
-                 * first, before we trust that check. */
-
-                r = journal_file_fstat(f);
-                if (r < 0)
-                        return r;
-
-                if (offset + size > (uint64_t) f->last_stat.st_size)
-                        return -EADDRNOTAVAIL;
-        }
-
-        return mmap_cache_get(f->mmap, f->fd, f->prot, type_to_context(type), keep_always, offset, size, &f->last_stat, ret);
-}
-
-static uint64_t minimum_header_size(Object *o) {
-
-        static const uint64_t table[] = {
-                [OBJECT_DATA] = sizeof(DataObject),
-                [OBJECT_FIELD] = sizeof(FieldObject),
-                [OBJECT_ENTRY] = sizeof(EntryObject),
-                [OBJECT_DATA_HASH_TABLE] = sizeof(HashTableObject),
-                [OBJECT_FIELD_HASH_TABLE] = sizeof(HashTableObject),
-                [OBJECT_ENTRY_ARRAY] = sizeof(EntryArrayObject),
-                [OBJECT_TAG] = sizeof(TagObject),
-        };
-
-        if (o->object.type >= ELEMENTSOF(table) || table[o->object.type] <= 0)
-                return sizeof(ObjectHeader);
-
-        return table[o->object.type];
-}
-
-int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret) {
-        int r;
-        void *t;
-        Object *o;
-        uint64_t s;
-
-        assert(f);
-        assert(ret);
-
-        /* Objects may only be located at multiple of 64 bit */
-        if (!VALID64(offset))
-                return -EFAULT;
-
-        r = journal_file_move_to(f, type, false, offset, sizeof(ObjectHeader), &t);
-        if (r < 0)
-                return r;
-
-        o = (Object*) t;
-        s = le64toh(o->object.size);
-
-        if (s < sizeof(ObjectHeader))
-                return -EBADMSG;
-
-        if (o->object.type <= OBJECT_UNUSED)
-                return -EBADMSG;
-
-        if (s < minimum_header_size(o))
-                return -EBADMSG;
-
-        if (type > OBJECT_UNUSED && o->object.type != type)
-                return -EBADMSG;
-
-        if (s > sizeof(ObjectHeader)) {
-                r = journal_file_move_to(f, type, false, offset, s, &t);
-                if (r < 0)
-                        return r;
-
-                o = (Object*) t;
-        }
-
-        *ret = o;
-        return 0;
-}
-
-static uint64_t journal_file_entry_seqnum(JournalFile *f, uint64_t *seqnum) {
-        uint64_t r;
-
-        assert(f);
-
-        r = le64toh(f->header->tail_entry_seqnum) + 1;
-
-        if (seqnum) {
-                /* If an external seqnum counter was passed, we update
-                 * both the local and the external one, and set it to
-                 * the maximum of both */
-
-                if (*seqnum + 1 > r)
-                        r = *seqnum + 1;
-
-                *seqnum = r;
-        }
-
-        f->header->tail_entry_seqnum = htole64(r);
-
-        if (f->header->head_entry_seqnum == 0)
-                f->header->head_entry_seqnum = htole64(r);
-
-        return r;
-}
-
-int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret, uint64_t *offset) {
-        int r;
-        uint64_t p;
-        Object *tail, *o;
-        void *t;
-
-        assert(f);
-        assert(type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX);
-        assert(size >= sizeof(ObjectHeader));
-        assert(offset);
-        assert(ret);
-
-        r = journal_file_set_online(f);
-        if (r < 0)
-                return r;
-
-        p = le64toh(f->header->tail_object_offset);
-        if (p == 0)
-                p = le64toh(f->header->header_size);
-        else {
-                r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &tail);
-                if (r < 0)
-                        return r;
-
-                p += ALIGN64(le64toh(tail->object.size));
-        }
-
-        r = journal_file_allocate(f, p, size);
-        if (r < 0)
-                return r;
-
-        r = journal_file_move_to(f, type, false, p, size, &t);
-        if (r < 0)
-                return r;
-
-        o = (Object*) t;
-
-        zero(o->object);
-        o->object.type = type;
-        o->object.size = htole64(size);
-
-        f->header->tail_object_offset = htole64(p);
-        f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
-
-        *ret = o;
-        *offset = p;
-
-        return 0;
-}
-
-static int journal_file_setup_data_hash_table(JournalFile *f) {
-        uint64_t s, p;
-        Object *o;
-        int r;
-
-        assert(f);
-
-        /* We estimate that we need 1 hash table entry per 768 of
-           journal file and we want to make sure we never get beyond
-           75% fill level. Calculate the hash table size for the
-           maximum file size based on these metrics. */
-
-        s = (f->metrics.max_size * 4 / 768 / 3) * sizeof(HashItem);
-        if (s < DEFAULT_DATA_HASH_TABLE_SIZE)
-                s = DEFAULT_DATA_HASH_TABLE_SIZE;
-
-        log_debug("Reserving %"PRIu64" entries in hash table.", s / sizeof(HashItem));
-
-        r = journal_file_append_object(f,
-                                       OBJECT_DATA_HASH_TABLE,
-                                       offsetof(Object, hash_table.items) + s,
-                                       &o, &p);
-        if (r < 0)
-                return r;
-
-        memzero(o->hash_table.items, s);
-
-        f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
-        f->header->data_hash_table_size = htole64(s);
-
-        return 0;
-}
-
-static int journal_file_setup_field_hash_table(JournalFile *f) {
-        uint64_t s, p;
-        Object *o;
-        int r;
-
-        assert(f);
-
-        /* We use a fixed size hash table for the fields as this
-         * number should grow very slowly only */
-
-        s = DEFAULT_FIELD_HASH_TABLE_SIZE;
-        r = journal_file_append_object(f,
-                                       OBJECT_FIELD_HASH_TABLE,
-                                       offsetof(Object, hash_table.items) + s,
-                                       &o, &p);
-        if (r < 0)
-                return r;
-
-        memzero(o->hash_table.items, s);
-
-        f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
-        f->header->field_hash_table_size = htole64(s);
-
-        return 0;
-}
-
-static int journal_file_map_data_hash_table(JournalFile *f) {
-        uint64_t s, p;
-        void *t;
-        int r;
-
-        assert(f);
-
-        p = le64toh(f->header->data_hash_table_offset);
-        s = le64toh(f->header->data_hash_table_size);
-
-        r = journal_file_move_to(f,
-                                 OBJECT_DATA_HASH_TABLE,
-                                 true,
-                                 p, s,
-                                 &t);
-        if (r < 0)
-                return r;
-
-        f->data_hash_table = t;
-        return 0;
-}
-
-static int journal_file_map_field_hash_table(JournalFile *f) {
-        uint64_t s, p;
-        void *t;
-        int r;
-
-        assert(f);
-
-        p = le64toh(f->header->field_hash_table_offset);
-        s = le64toh(f->header->field_hash_table_size);
-
-        r = journal_file_move_to(f,
-                                 OBJECT_FIELD_HASH_TABLE,
-                                 true,
-                                 p, s,
-                                 &t);
-        if (r < 0)
-                return r;
-
-        f->field_hash_table = t;
-        return 0;
-}
-
-static int journal_file_link_field(
-                JournalFile *f,
-                Object *o,
-                uint64_t offset,
-                uint64_t hash) {
-
-        uint64_t p, h, m;
-        int r;
-
-        assert(f);
-        assert(o);
-        assert(offset > 0);
-
-        if (o->object.type != OBJECT_FIELD)
-                return -EINVAL;
-
-        m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
-        if (m <= 0)
-                return -EBADMSG;
-
-        /* This might alter the window we are looking at */
-        o->field.next_hash_offset = o->field.head_data_offset = 0;
-
-        h = hash % m;
-        p = le64toh(f->field_hash_table[h].tail_hash_offset);
-        if (p == 0)
-                f->field_hash_table[h].head_hash_offset = htole64(offset);
-        else {
-                r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o);
-                if (r < 0)
-                        return r;
-
-                o->field.next_hash_offset = htole64(offset);
-        }
-
-        f->field_hash_table[h].tail_hash_offset = htole64(offset);
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_fields))
-                f->header->n_fields = htole64(le64toh(f->header->n_fields) + 1);
-
-        return 0;
-}
-
-static int journal_file_link_data(
-                JournalFile *f,
-                Object *o,
-                uint64_t offset,
-                uint64_t hash) {
-
-        uint64_t p, h, m;
-        int r;
-
-        assert(f);
-        assert(o);
-        assert(offset > 0);
-
-        if (o->object.type != OBJECT_DATA)
-                return -EINVAL;
-
-        m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
-        if (m <= 0)
-                return -EBADMSG;
-
-        /* This might alter the window we are looking at */
-        o->data.next_hash_offset = o->data.next_field_offset = 0;
-        o->data.entry_offset = o->data.entry_array_offset = 0;
-        o->data.n_entries = 0;
-
-        h = hash % m;
-        p = le64toh(f->data_hash_table[h].tail_hash_offset);
-        if (p == 0)
-                /* Only entry in the hash table is easy */
-                f->data_hash_table[h].head_hash_offset = htole64(offset);
-        else {
-                /* Move back to the previous data object, to patch in
-                 * pointer */
-
-                r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
-                if (r < 0)
-                        return r;
-
-                o->data.next_hash_offset = htole64(offset);
-        }
-
-        f->data_hash_table[h].tail_hash_offset = htole64(offset);
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_data))
-                f->header->n_data = htole64(le64toh(f->header->n_data) + 1);
-
-        return 0;
-}
-
-int journal_file_find_field_object_with_hash(
-                JournalFile *f,
-                const void *field, uint64_t size, uint64_t hash,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t p, osize, h, m;
-        int r;
-
-        assert(f);
-        assert(field && size > 0);
-
-        osize = offsetof(Object, field.payload) + size;
-
-        m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
-
-        if (m <= 0)
-                return -EBADMSG;
-
-        h = hash % m;
-        p = le64toh(f->field_hash_table[h].head_hash_offset);
-
-        while (p > 0) {
-                Object *o;
-
-                r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o);
-                if (r < 0)
-                        return r;
-
-                if (le64toh(o->field.hash) == hash &&
-                    le64toh(o->object.size) == osize &&
-                    memcmp(o->field.payload, field, size) == 0) {
-
-                        if (ret)
-                                *ret = o;
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-                }
-
-                p = le64toh(o->field.next_hash_offset);
-        }
-
-        return 0;
-}
-
-int journal_file_find_field_object(
-                JournalFile *f,
-                const void *field, uint64_t size,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t hash;
-
-        assert(f);
-        assert(field && size > 0);
-
-        hash = hash64(field, size);
-
-        return journal_file_find_field_object_with_hash(f,
-                                                        field, size, hash,
-                                                        ret, offset);
-}
-
-int journal_file_find_data_object_with_hash(
-                JournalFile *f,
-                const void *data, uint64_t size, uint64_t hash,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t p, osize, h, m;
-        int r;
-
-        assert(f);
-        assert(data || size == 0);
-
-        osize = offsetof(Object, data.payload) + size;
-
-        m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
-        if (m <= 0)
-                return -EBADMSG;
-
-        h = hash % m;
-        p = le64toh(f->data_hash_table[h].head_hash_offset);
-
-        while (p > 0) {
-                Object *o;
-
-                r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
-                if (r < 0)
-                        return r;
-
-                if (le64toh(o->data.hash) != hash)
-                        goto next;
-
-                if (o->object.flags & OBJECT_COMPRESSION_MASK) {
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-                        uint64_t l;
-                        size_t rsize = 0;
-
-                        l = le64toh(o->object.size);
-                        if (l <= offsetof(Object, data.payload))
-                                return -EBADMSG;
-
-                        l -= offsetof(Object, data.payload);
-
-                        r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK,
-                                            o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0);
-                        if (r < 0)
-                                return r;
-
-                        if (rsize == size &&
-                            memcmp(f->compress_buffer, data, size) == 0) {
-
-                                if (ret)
-                                        *ret = o;
-
-                                if (offset)
-                                        *offset = p;
-
-                                return 1;
-                        }
-#else
-                        return -EPROTONOSUPPORT;
-#endif
-                } else if (le64toh(o->object.size) == osize &&
-                           memcmp(o->data.payload, data, size) == 0) {
-
-                        if (ret)
-                                *ret = o;
-
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-                }
-
-        next:
-                p = le64toh(o->data.next_hash_offset);
-        }
-
-        return 0;
-}
-
-int journal_file_find_data_object(
-                JournalFile *f,
-                const void *data, uint64_t size,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t hash;
-
-        assert(f);
-        assert(data || size == 0);
-
-        hash = hash64(data, size);
-
-        return journal_file_find_data_object_with_hash(f,
-                                                       data, size, hash,
-                                                       ret, offset);
-}
-
-static int journal_file_append_field(
-                JournalFile *f,
-                const void *field, uint64_t size,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t hash, p;
-        uint64_t osize;
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(field && size > 0);
-
-        hash = hash64(field, size);
-
-        r = journal_file_find_field_object_with_hash(f, field, size, hash, &o, &p);
-        if (r < 0)
-                return r;
-        else if (r > 0) {
-
-                if (ret)
-                        *ret = o;
-
-                if (offset)
-                        *offset = p;
-
-                return 0;
-        }
-
-        osize = offsetof(Object, field.payload) + size;
-        r = journal_file_append_object(f, OBJECT_FIELD, osize, &o, &p);
-        if (r < 0)
-                return r;
-
-        o->field.hash = htole64(hash);
-        memcpy(o->field.payload, field, size);
-
-        r = journal_file_link_field(f, o, p, hash);
-        if (r < 0)
-                return r;
-
-        /* The linking might have altered the window, so let's
-         * refresh our pointer */
-        r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o);
-        if (r < 0)
-                return r;
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_hmac_put_object(f, OBJECT_FIELD, o, p);
-        if (r < 0)
-                return r;
-#endif
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = p;
-
-        return 0;
-}
-
-static int journal_file_append_data(
-                JournalFile *f,
-                const void *data, uint64_t size,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t hash, p;
-        uint64_t osize;
-        Object *o;
-        int r, compression = 0;
-        const void *eq;
-
-        assert(f);
-        assert(data || size == 0);
-
-        hash = hash64(data, size);
-
-        r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
-        if (r < 0)
-                return r;
-        else if (r > 0) {
-
-                if (ret)
-                        *ret = o;
-
-                if (offset)
-                        *offset = p;
-
-                return 0;
-        }
-
-        osize = offsetof(Object, data.payload) + size;
-        r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
-        if (r < 0)
-                return r;
-
-        o->data.hash = htole64(hash);
-
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-        if (f->compress_xz &&
-            size >= COMPRESSION_SIZE_THRESHOLD) {
-                size_t rsize = 0;
-
-                compression = compress_blob(data, size, o->data.payload, &rsize);
-
-                if (compression) {
-                        o->object.size = htole64(offsetof(Object, data.payload) + rsize);
-                        o->object.flags |= compression;
-
-                        log_debug("Compressed data object %"PRIu64" -> %zu using %s",
-                                  size, rsize, object_compressed_to_string(compression));
-                }
-        }
-#endif
-
-        if (!compression && size > 0)
-                memcpy(o->data.payload, data, size);
-
-        r = journal_file_link_data(f, o, p, hash);
-        if (r < 0)
-                return r;
-
-        /* The linking might have altered the window, so let's
-         * refresh our pointer */
-        r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
-        if (r < 0)
-                return r;
-
-        if (!data)
-                eq = NULL;
-        else
-                eq = memchr(data, '=', size);
-        if (eq && eq > data) {
-                Object *fo = NULL;
-                uint64_t fp;
-
-                /* Create field object ... */
-                r = journal_file_append_field(f, data, (uint8_t*) eq - (uint8_t*) data, &fo, &fp);
-                if (r < 0)
-                        return r;
-
-                /* ... and link it in. */
-                o->data.next_field_offset = fo->field.head_data_offset;
-                fo->field.head_data_offset = le64toh(p);
-        }
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p);
-        if (r < 0)
-                return r;
-#endif
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = p;
-
-        return 0;
-}
-
-uint64_t journal_file_entry_n_items(Object *o) {
-        assert(o);
-
-        if (o->object.type != OBJECT_ENTRY)
-                return 0;
-
-        return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
-}
-
-uint64_t journal_file_entry_array_n_items(Object *o) {
-        assert(o);
-
-        if (o->object.type != OBJECT_ENTRY_ARRAY)
-                return 0;
-
-        return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
-}
-
-uint64_t journal_file_hash_table_n_items(Object *o) {
-        assert(o);
-
-        if (o->object.type != OBJECT_DATA_HASH_TABLE &&
-            o->object.type != OBJECT_FIELD_HASH_TABLE)
-                return 0;
-
-        return (le64toh(o->object.size) - offsetof(Object, hash_table.items)) / sizeof(HashItem);
-}
-
-static int link_entry_into_array(JournalFile *f,
-                                 le64_t *first,
-                                 le64_t *idx,
-                                 uint64_t p) {
-        int r;
-        uint64_t n = 0, ap = 0, q, i, a, hidx;
-        Object *o;
-
-        assert(f);
-        assert(first);
-        assert(idx);
-        assert(p > 0);
-
-        a = le64toh(*first);
-        i = hidx = le64toh(*idx);
-        while (a > 0) {
-
-                r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
-                if (r < 0)
-                        return r;
-
-                n = journal_file_entry_array_n_items(o);
-                if (i < n) {
-                        o->entry_array.items[i] = htole64(p);
-                        *idx = htole64(hidx + 1);
-                        return 0;
-                }
-
-                i -= n;
-                ap = a;
-                a = le64toh(o->entry_array.next_entry_array_offset);
-        }
-
-        if (hidx > n)
-                n = (hidx+1) * 2;
-        else
-                n = n * 2;
-
-        if (n < 4)
-                n = 4;
-
-        r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
-                                       offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
-                                       &o, &q);
-        if (r < 0)
-                return r;
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q);
-        if (r < 0)
-                return r;
-#endif
-
-        o->entry_array.items[i] = htole64(p);
-
-        if (ap == 0)
-                *first = htole64(q);
-        else {
-                r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
-                if (r < 0)
-                        return r;
-
-                o->entry_array.next_entry_array_offset = htole64(q);
-        }
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
-                f->header->n_entry_arrays = htole64(le64toh(f->header->n_entry_arrays) + 1);
-
-        *idx = htole64(hidx + 1);
-
-        return 0;
-}
-
-static int link_entry_into_array_plus_one(JournalFile *f,
-                                          le64_t *extra,
-                                          le64_t *first,
-                                          le64_t *idx,
-                                          uint64_t p) {
-
-        int r;
-
-        assert(f);
-        assert(extra);
-        assert(first);
-        assert(idx);
-        assert(p > 0);
-
-        if (*idx == 0)
-                *extra = htole64(p);
-        else {
-                le64_t i;
-
-                i = htole64(le64toh(*idx) - 1);
-                r = link_entry_into_array(f, first, &i, p);
-                if (r < 0)
-                        return r;
-        }
-
-        *idx = htole64(le64toh(*idx) + 1);
-        return 0;
-}
-
-static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
-        uint64_t p;
-        int r;
-        assert(f);
-        assert(o);
-        assert(offset > 0);
-
-        p = le64toh(o->entry.items[i].object_offset);
-        if (p == 0)
-                return -EINVAL;
-
-        r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
-        if (r < 0)
-                return r;
-
-        return link_entry_into_array_plus_one(f,
-                                              &o->data.entry_offset,
-                                              &o->data.entry_array_offset,
-                                              &o->data.n_entries,
-                                              offset);
-}
-
-static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
-        uint64_t n, i;
-        int r;
-
-        assert(f);
-        assert(o);
-        assert(offset > 0);
-
-        if (o->object.type != OBJECT_ENTRY)
-                return -EINVAL;
-
-        __sync_synchronize();
-
-        /* Link up the entry itself */
-        r = link_entry_into_array(f,
-                                  &f->header->entry_array_offset,
-                                  &f->header->n_entries,
-                                  offset);
-        if (r < 0)
-                return r;
-
-        /* log_debug("=> %s seqnr=%"PRIu64" n_entries=%"PRIu64, f->path, o->entry.seqnum, f->header->n_entries); */
-
-        if (f->header->head_entry_realtime == 0)
-                f->header->head_entry_realtime = o->entry.realtime;
-
-        f->header->tail_entry_realtime = o->entry.realtime;
-        f->header->tail_entry_monotonic = o->entry.monotonic;
-
-        f->tail_entry_monotonic_valid = true;
-
-        /* Link up the items */
-        n = journal_file_entry_n_items(o);
-        for (i = 0; i < n; i++) {
-                r = journal_file_link_entry_item(f, o, offset, i);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
-}
-
-static int journal_file_append_entry_internal(
-                JournalFile *f,
-                const dual_timestamp *ts,
-                uint64_t xor_hash,
-                const EntryItem items[], unsigned n_items,
-                uint64_t *seqnum,
-                Object **ret, uint64_t *offset) {
-        uint64_t np;
-        uint64_t osize;
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(items || n_items == 0);
-        assert(ts);
-
-        osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
-
-        r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
-        if (r < 0)
-                return r;
-
-        o->entry.seqnum = htole64(journal_file_entry_seqnum(f, seqnum));
-        memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
-        o->entry.realtime = htole64(ts->realtime);
-        o->entry.monotonic = htole64(ts->monotonic);
-        o->entry.xor_hash = htole64(xor_hash);
-        o->entry.boot_id = f->header->boot_id;
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np);
-        if (r < 0)
-                return r;
-#endif
-
-        r = journal_file_link_entry(f, o, np);
-        if (r < 0)
-                return r;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = np;
-
-        return 0;
-}
-
-void journal_file_post_change(JournalFile *f) {
-        assert(f);
-
-        /* inotify() does not receive IN_MODIFY events from file
-         * accesses done via mmap(). After each access we hence
-         * trigger IN_MODIFY by truncating the journal file to its
-         * current size which triggers IN_MODIFY. */
-
-        __sync_synchronize();
-
-        if (ftruncate(f->fd, f->last_stat.st_size) < 0)
-                log_error_errno(errno, "Failed to truncate file to its own size: %m");
-}
-
-static int entry_item_cmp(const void *_a, const void *_b) {
-        const EntryItem *a = _a, *b = _b;
-
-        if (le64toh(a->object_offset) < le64toh(b->object_offset))
-                return -1;
-        if (le64toh(a->object_offset) > le64toh(b->object_offset))
-                return 1;
-        return 0;
-}
-
-int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqnum, Object **ret, uint64_t *offset) {
-        unsigned i;
-        EntryItem *items;
-        int r;
-        uint64_t xor_hash = 0;
-        struct dual_timestamp _ts;
-
-        assert(f);
-        assert(iovec || n_iovec == 0);
-
-        if (!ts) {
-                dual_timestamp_get(&_ts);
-                ts = &_ts;
-        }
-
-        if (f->tail_entry_monotonic_valid &&
-            ts->monotonic < le64toh(f->header->tail_entry_monotonic))
-                return -EINVAL;
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_maybe_append_tag(f, ts->realtime);
-        if (r < 0)
-                return r;
-#endif
-
-        /* alloca() can't take 0, hence let's allocate at least one */
-        items = alloca(sizeof(EntryItem) * MAX(1u, n_iovec));
-
-        for (i = 0; i < n_iovec; i++) {
-                uint64_t p;
-                Object *o;
-
-                r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
-                if (r < 0)
-                        return r;
-
-                xor_hash ^= le64toh(o->data.hash);
-                items[i].object_offset = htole64(p);
-                items[i].hash = o->data.hash;
-        }
-
-        /* Order by the position on disk, in order to improve seek
-         * times for rotating media. */
-        qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
-
-        r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
-
-        /* If the memory mapping triggered a SIGBUS then we return an
-         * IO error and ignore the error code passed down to us, since
-         * it is very likely just an effect of a nullified replacement
-         * mapping page */
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd))
-                r = -EIO;
-
-        journal_file_post_change(f);
-
-        return r;
-}
-
-typedef struct ChainCacheItem {
-        uint64_t first; /* the array at the beginning of the chain */
-        uint64_t array; /* the cached array */
-        uint64_t begin; /* the first item in the cached array */
-        uint64_t total; /* the total number of items in all arrays before this one in the chain */
-        uint64_t last_index; /* the last index we looked at, to optimize locality when bisecting */
-} ChainCacheItem;
-
-static void chain_cache_put(
-                OrderedHashmap *h,
-                ChainCacheItem *ci,
-                uint64_t first,
-                uint64_t array,
-                uint64_t begin,
-                uint64_t total,
-                uint64_t last_index) {
-
-        if (!ci) {
-                /* If the chain item to cache for this chain is the
-                 * first one it's not worth caching anything */
-                if (array == first)
-                        return;
-
-                if (ordered_hashmap_size(h) >= CHAIN_CACHE_MAX) {
-                        ci = ordered_hashmap_steal_first(h);
-                        assert(ci);
-                } else {
-                        ci = new(ChainCacheItem, 1);
-                        if (!ci)
-                                return;
-                }
-
-                ci->first = first;
-
-                if (ordered_hashmap_put(h, &ci->first, ci) < 0) {
-                        free(ci);
-                        return;
-                }
-        } else
-                assert(ci->first == first);
-
-        ci->array = array;
-        ci->begin = begin;
-        ci->total = total;
-        ci->last_index = last_index;
-}
-
-static int generic_array_get(
-                JournalFile *f,
-                uint64_t first,
-                uint64_t i,
-                Object **ret, uint64_t *offset) {
-
-        Object *o;
-        uint64_t p = 0, a, t = 0;
-        int r;
-        ChainCacheItem *ci;
-
-        assert(f);
-
-        a = first;
-
-        /* Try the chain cache first */
-        ci = ordered_hashmap_get(f->chain_cache, &first);
-        if (ci && i > ci->total) {
-                a = ci->array;
-                i -= ci->total;
-                t = ci->total;
-        }
-
-        while (a > 0) {
-                uint64_t k;
-
-                r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
-                if (r < 0)
-                        return r;
-
-                k = journal_file_entry_array_n_items(o);
-                if (i < k) {
-                        p = le64toh(o->entry_array.items[i]);
-                        goto found;
-                }
-
-                i -= k;
-                t += k;
-                a = le64toh(o->entry_array.next_entry_array_offset);
-        }
-
-        return 0;
-
-found:
-        /* Let's cache this item for the next invocation */
-        chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i);
-
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
-        if (r < 0)
-                return r;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = p;
-
-        return 1;
-}
-
-static int generic_array_get_plus_one(
-                JournalFile *f,
-                uint64_t extra,
-                uint64_t first,
-                uint64_t i,
-                Object **ret, uint64_t *offset) {
-
-        Object *o;
-
-        assert(f);
-
-        if (i == 0) {
-                int r;
-
-                r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
-                if (r < 0)
-                        return r;
-
-                if (ret)
-                        *ret = o;
-
-                if (offset)
-                        *offset = extra;
-
-                return 1;
-        }
-
-        return generic_array_get(f, first, i-1, ret, offset);
-}
-
-enum {
-        TEST_FOUND,
-        TEST_LEFT,
-        TEST_RIGHT
-};
-
-static int generic_array_bisect(
-                JournalFile *f,
-                uint64_t first,
-                uint64_t n,
-                uint64_t needle,
-                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
-                direction_t direction,
-                Object **ret,
-                uint64_t *offset,
-                uint64_t *idx) {
-
-        uint64_t a, p, t = 0, i = 0, last_p = 0, last_index = (uint64_t) -1;
-        bool subtract_one = false;
-        Object *o, *array = NULL;
-        int r;
-        ChainCacheItem *ci;
-
-        assert(f);
-        assert(test_object);
-
-        /* Start with the first array in the chain */
-        a = first;
-
-        ci = ordered_hashmap_get(f->chain_cache, &first);
-        if (ci && n > ci->total) {
-                /* Ah, we have iterated this bisection array chain
-                 * previously! Let's see if we can skip ahead in the
-                 * chain, as far as the last time. But we can't jump
-                 * backwards in the chain, so let's check that
-                 * first. */
-
-                r = test_object(f, ci->begin, needle);
-                if (r < 0)
-                        return r;
-
-                if (r == TEST_LEFT) {
-                        /* OK, what we are looking for is right of the
-                         * begin of this EntryArray, so let's jump
-                         * straight to previously cached array in the
-                         * chain */
-
-                        a = ci->array;
-                        n -= ci->total;
-                        t = ci->total;
-                        last_index = ci->last_index;
-                }
-        }
-
-        while (a > 0) {
-                uint64_t left, right, k, lp;
-
-                r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
-                if (r < 0)
-                        return r;
-
-                k = journal_file_entry_array_n_items(array);
-                right = MIN(k, n);
-                if (right <= 0)
-                        return 0;
-
-                i = right - 1;
-                lp = p = le64toh(array->entry_array.items[i]);
-                if (p <= 0)
-                        return -EBADMSG;
-
-                r = test_object(f, p, needle);
-                if (r < 0)
-                        return r;
-
-                if (r == TEST_FOUND)
-                        r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
-
-                if (r == TEST_RIGHT) {
-                        left = 0;
-                        right -= 1;
-
-                        if (last_index != (uint64_t) -1) {
-                                assert(last_index <= right);
-
-                                /* If we cached the last index we
-                                 * looked at, let's try to not to jump
-                                 * too wildly around and see if we can
-                                 * limit the range to look at early to
-                                 * the immediate neighbors of the last
-                                 * index we looked at. */
-
-                                if (last_index > 0) {
-                                        uint64_t x = last_index - 1;
-
-                                        p = le64toh(array->entry_array.items[x]);
-                                        if (p <= 0)
-                                                return -EBADMSG;
-
-                                        r = test_object(f, p, needle);
-                                        if (r < 0)
-                                                return r;
-
-                                        if (r == TEST_FOUND)
-                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
-
-                                        if (r == TEST_RIGHT)
-                                                right = x;
-                                        else
-                                                left = x + 1;
-                                }
-
-                                if (last_index < right) {
-                                        uint64_t y = last_index + 1;
-
-                                        p = le64toh(array->entry_array.items[y]);
-                                        if (p <= 0)
-                                                return -EBADMSG;
-
-                                        r = test_object(f, p, needle);
-                                        if (r < 0)
-                                                return r;
-
-                                        if (r == TEST_FOUND)
-                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
-
-                                        if (r == TEST_RIGHT)
-                                                right = y;
-                                        else
-                                                left = y + 1;
-                                }
-                        }
-
-                        for (;;) {
-                                if (left == right) {
-                                        if (direction == DIRECTION_UP)
-                                                subtract_one = true;
-
-                                        i = left;
-                                        goto found;
-                                }
-
-                                assert(left < right);
-                                i = (left + right) / 2;
-
-                                p = le64toh(array->entry_array.items[i]);
-                                if (p <= 0)
-                                        return -EBADMSG;
-
-                                r = test_object(f, p, needle);
-                                if (r < 0)
-                                        return r;
-
-                                if (r == TEST_FOUND)
-                                        r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
-
-                                if (r == TEST_RIGHT)
-                                        right = i;
-                                else
-                                        left = i + 1;
-                        }
-                }
-
-                if (k >= n) {
-                        if (direction == DIRECTION_UP) {
-                                i = n;
-                                subtract_one = true;
-                                goto found;
-                        }
-
-                        return 0;
-                }
-
-                last_p = lp;
-
-                n -= k;
-                t += k;
-                last_index = (uint64_t) -1;
-                a = le64toh(array->entry_array.next_entry_array_offset);
-        }
-
-        return 0;
-
-found:
-        if (subtract_one && t == 0 && i == 0)
-                return 0;
-
-        /* Let's cache this item for the next invocation */
-        chain_cache_put(f->chain_cache, ci, first, a, le64toh(array->entry_array.items[0]), t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i);
-
-        if (subtract_one && i == 0)
-                p = last_p;
-        else if (subtract_one)
-                p = le64toh(array->entry_array.items[i-1]);
-        else
-                p = le64toh(array->entry_array.items[i]);
-
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
-        if (r < 0)
-                return r;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = p;
-
-        if (idx)
-                *idx = t + i + (subtract_one ? -1 : 0);
-
-        return 1;
-}
-
-static int generic_array_bisect_plus_one(
-                JournalFile *f,
-                uint64_t extra,
-                uint64_t first,
-                uint64_t n,
-                uint64_t needle,
-                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
-                direction_t direction,
-                Object **ret,
-                uint64_t *offset,
-                uint64_t *idx) {
-
-        int r;
-        bool step_back = false;
-        Object *o;
-
-        assert(f);
-        assert(test_object);
-
-        if (n <= 0)
-                return 0;
-
-        /* This bisects the array in object 'first', but first checks
-         * an extra  */
-        r = test_object(f, extra, needle);
-        if (r < 0)
-                return r;
-
-        if (r == TEST_FOUND)
-                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
-
-        /* if we are looking with DIRECTION_UP then we need to first
-           see if in the actual array there is a matching entry, and
-           return the last one of that. But if there isn't any we need
-           to return this one. Hence remember this, and return it
-           below. */
-        if (r == TEST_LEFT)
-                step_back = direction == DIRECTION_UP;
-
-        if (r == TEST_RIGHT) {
-                if (direction == DIRECTION_DOWN)
-                        goto found;
-                else
-                        return 0;
-        }
-
-        r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
-
-        if (r == 0 && step_back)
-                goto found;
-
-        if (r > 0 && idx)
-                (*idx) ++;
-
-        return r;
-
-found:
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
-        if (r < 0)
-                return r;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = extra;
-
-        if (idx)
-                *idx = 0;
-
-        return 1;
-}
-
-_pure_ static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
-        assert(f);
-        assert(p > 0);
-
-        if (p == needle)
-                return TEST_FOUND;
-        else if (p < needle)
-                return TEST_LEFT;
-        else
-                return TEST_RIGHT;
-}
-
-static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(p > 0);
-
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
-        if (r < 0)
-                return r;
-
-        if (le64toh(o->entry.seqnum) == needle)
-                return TEST_FOUND;
-        else if (le64toh(o->entry.seqnum) < needle)
-                return TEST_LEFT;
-        else
-                return TEST_RIGHT;
-}
-
-int journal_file_move_to_entry_by_seqnum(
-                JournalFile *f,
-                uint64_t seqnum,
-                direction_t direction,
-                Object **ret,
-                uint64_t *offset) {
-
-        return generic_array_bisect(f,
-                                    le64toh(f->header->entry_array_offset),
-                                    le64toh(f->header->n_entries),
-                                    seqnum,
-                                    test_object_seqnum,
-                                    direction,
-                                    ret, offset, NULL);
-}
-
-static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(p > 0);
-
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
-        if (r < 0)
-                return r;
-
-        if (le64toh(o->entry.realtime) == needle)
-                return TEST_FOUND;
-        else if (le64toh(o->entry.realtime) < needle)
-                return TEST_LEFT;
-        else
-                return TEST_RIGHT;
-}
-
-int journal_file_move_to_entry_by_realtime(
-                JournalFile *f,
-                uint64_t realtime,
-                direction_t direction,
-                Object **ret,
-                uint64_t *offset) {
-
-        return generic_array_bisect(f,
-                                    le64toh(f->header->entry_array_offset),
-                                    le64toh(f->header->n_entries),
-                                    realtime,
-                                    test_object_realtime,
-                                    direction,
-                                    ret, offset, NULL);
-}
-
-static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(p > 0);
-
-        r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
-        if (r < 0)
-                return r;
-
-        if (le64toh(o->entry.monotonic) == needle)
-                return TEST_FOUND;
-        else if (le64toh(o->entry.monotonic) < needle)
-                return TEST_LEFT;
-        else
-                return TEST_RIGHT;
-}
-
-static int find_data_object_by_boot_id(
-                JournalFile *f,
-                sd_id128_t boot_id,
-                Object **o,
-                uint64_t *b) {
-
-        char t[sizeof("_BOOT_ID=")-1 + 32 + 1] = "_BOOT_ID=";
-
-        sd_id128_to_string(boot_id, t + 9);
-        return journal_file_find_data_object(f, t, sizeof(t) - 1, o, b);
-}
-
-int journal_file_move_to_entry_by_monotonic(
-                JournalFile *f,
-                sd_id128_t boot_id,
-                uint64_t monotonic,
-                direction_t direction,
-                Object **ret,
-                uint64_t *offset) {
-
-        Object *o;
-        int r;
-
-        assert(f);
-
-        r = find_data_object_by_boot_id(f, boot_id, &o, NULL);
-        if (r < 0)
-                return r;
-        if (r == 0)
-                return -ENOENT;
-
-        return generic_array_bisect_plus_one(f,
-                                             le64toh(o->data.entry_offset),
-                                             le64toh(o->data.entry_array_offset),
-                                             le64toh(o->data.n_entries),
-                                             monotonic,
-                                             test_object_monotonic,
-                                             direction,
-                                             ret, offset, NULL);
-}
-
-void journal_file_reset_location(JournalFile *f) {
-        f->location_type = LOCATION_HEAD;
-        f->current_offset = 0;
-        f->current_seqnum = 0;
-        f->current_realtime = 0;
-        f->current_monotonic = 0;
-        zero(f->current_boot_id);
-        f->current_xor_hash = 0;
-}
-
-void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) {
-        f->location_type = LOCATION_SEEK;
-        f->current_offset = offset;
-        f->current_seqnum = le64toh(o->entry.seqnum);
-        f->current_realtime = le64toh(o->entry.realtime);
-        f->current_monotonic = le64toh(o->entry.monotonic);
-        f->current_boot_id = o->entry.boot_id;
-        f->current_xor_hash = le64toh(o->entry.xor_hash);
-}
-
-int journal_file_compare_locations(JournalFile *af, JournalFile *bf) {
-        assert(af);
-        assert(bf);
-        assert(af->location_type == LOCATION_SEEK);
-        assert(bf->location_type == LOCATION_SEEK);
-
-        /* If contents and timestamps match, these entries are
-         * identical, even if the seqnum does not match */
-        if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) &&
-            af->current_monotonic == bf->current_monotonic &&
-            af->current_realtime == bf->current_realtime &&
-            af->current_xor_hash == bf->current_xor_hash)
-                return 0;
-
-        if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
-
-                /* If this is from the same seqnum source, compare
-                 * seqnums */
-                if (af->current_seqnum < bf->current_seqnum)
-                        return -1;
-                if (af->current_seqnum > bf->current_seqnum)
-                        return 1;
-
-                /* Wow! This is weird, different data but the same
-                 * seqnums? Something is borked, but let's make the
-                 * best of it and compare by time. */
-        }
-
-        if (sd_id128_equal(af->current_boot_id, bf->current_boot_id)) {
-
-                /* If the boot id matches, compare monotonic time */
-                if (af->current_monotonic < bf->current_monotonic)
-                        return -1;
-                if (af->current_monotonic > bf->current_monotonic)
-                        return 1;
-        }
-
-        /* Otherwise, compare UTC time */
-        if (af->current_realtime < bf->current_realtime)
-                return -1;
-        if (af->current_realtime > bf->current_realtime)
-                return 1;
-
-        /* Finally, compare by contents */
-        if (af->current_xor_hash < bf->current_xor_hash)
-                return -1;
-        if (af->current_xor_hash > bf->current_xor_hash)
-                return 1;
-
-        return 0;
-}
-
-int journal_file_next_entry(
-                JournalFile *f,
-                uint64_t p,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t i, n, ofs;
-        int r;
-
-        assert(f);
-
-        n = le64toh(f->header->n_entries);
-        if (n <= 0)
-                return 0;
-
-        if (p == 0)
-                i = direction == DIRECTION_DOWN ? 0 : n - 1;
-        else {
-                r = generic_array_bisect(f,
-                                         le64toh(f->header->entry_array_offset),
-                                         le64toh(f->header->n_entries),
-                                         p,
-                                         test_object_offset,
-                                         DIRECTION_DOWN,
-                                         NULL, NULL,
-                                         &i);
-                if (r <= 0)
-                        return r;
-
-                if (direction == DIRECTION_DOWN) {
-                        if (i >= n - 1)
-                                return 0;
-
-                        i++;
-                } else {
-                        if (i <= 0)
-                                return 0;
-
-                        i--;
-                }
-        }
-
-        /* And jump to it */
-        r = generic_array_get(f,
-                              le64toh(f->header->entry_array_offset),
-                              i,
-                              ret, &ofs);
-        if (r <= 0)
-                return r;
-
-        if (p > 0 &&
-            (direction == DIRECTION_DOWN ? ofs <= p : ofs >= p)) {
-                log_debug("%s: entry array corrupted at entry %"PRIu64,
-                          f->path, i);
-                return -EBADMSG;
-        }
-
-        if (offset)
-                *offset = ofs;
-
-        return 1;
-}
-
-int journal_file_next_entry_for_data(
-                JournalFile *f,
-                Object *o, uint64_t p,
-                uint64_t data_offset,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        uint64_t n, i;
-        int r;
-        Object *d;
-
-        assert(f);
-        assert(p > 0 || !o);
-
-        r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
-        if (r < 0)
-                return r;
-
-        n = le64toh(d->data.n_entries);
-        if (n <= 0)
-                return n;
-
-        if (!o)
-                i = direction == DIRECTION_DOWN ? 0 : n - 1;
-        else {
-                if (o->object.type != OBJECT_ENTRY)
-                        return -EINVAL;
-
-                r = generic_array_bisect_plus_one(f,
-                                                  le64toh(d->data.entry_offset),
-                                                  le64toh(d->data.entry_array_offset),
-                                                  le64toh(d->data.n_entries),
-                                                  p,
-                                                  test_object_offset,
-                                                  DIRECTION_DOWN,
-                                                  NULL, NULL,
-                                                  &i);
-
-                if (r <= 0)
-                        return r;
-
-                if (direction == DIRECTION_DOWN) {
-                        if (i >= n - 1)
-                                return 0;
-
-                        i++;
-                } else {
-                        if (i <= 0)
-                                return 0;
-
-                        i--;
-                }
-
-        }
-
-        return generic_array_get_plus_one(f,
-                                          le64toh(d->data.entry_offset),
-                                          le64toh(d->data.entry_array_offset),
-                                          i,
-                                          ret, offset);
-}
-
-int journal_file_move_to_entry_by_offset_for_data(
-                JournalFile *f,
-                uint64_t data_offset,
-                uint64_t p,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        int r;
-        Object *d;
-
-        assert(f);
-
-        r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
-        if (r < 0)
-                return r;
-
-        return generic_array_bisect_plus_one(f,
-                                             le64toh(d->data.entry_offset),
-                                             le64toh(d->data.entry_array_offset),
-                                             le64toh(d->data.n_entries),
-                                             p,
-                                             test_object_offset,
-                                             direction,
-                                             ret, offset, NULL);
-}
-
-int journal_file_move_to_entry_by_monotonic_for_data(
-                JournalFile *f,
-                uint64_t data_offset,
-                sd_id128_t boot_id,
-                uint64_t monotonic,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        Object *o, *d;
-        int r;
-        uint64_t b, z;
-
-        assert(f);
-
-        /* First, seek by time */
-        r = find_data_object_by_boot_id(f, boot_id, &o, &b);
-        if (r < 0)
-                return r;
-        if (r == 0)
-                return -ENOENT;
-
-        r = generic_array_bisect_plus_one(f,
-                                          le64toh(o->data.entry_offset),
-                                          le64toh(o->data.entry_array_offset),
-                                          le64toh(o->data.n_entries),
-                                          monotonic,
-                                          test_object_monotonic,
-                                          direction,
-                                          NULL, &z, NULL);
-        if (r <= 0)
-                return r;
-
-        /* And now, continue seeking until we find an entry that
-         * exists in both bisection arrays */
-
-        for (;;) {
-                Object *qo;
-                uint64_t p, q;
-
-                r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
-                if (r < 0)
-                        return r;
-
-                r = generic_array_bisect_plus_one(f,
-                                                  le64toh(d->data.entry_offset),
-                                                  le64toh(d->data.entry_array_offset),
-                                                  le64toh(d->data.n_entries),
-                                                  z,
-                                                  test_object_offset,
-                                                  direction,
-                                                  NULL, &p, NULL);
-                if (r <= 0)
-                        return r;
-
-                r = journal_file_move_to_object(f, OBJECT_DATA, b, &o);
-                if (r < 0)
-                        return r;
-
-                r = generic_array_bisect_plus_one(f,
-                                                  le64toh(o->data.entry_offset),
-                                                  le64toh(o->data.entry_array_offset),
-                                                  le64toh(o->data.n_entries),
-                                                  p,
-                                                  test_object_offset,
-                                                  direction,
-                                                  &qo, &q, NULL);
-
-                if (r <= 0)
-                        return r;
-
-                if (p == q) {
-                        if (ret)
-                                *ret = qo;
-                        if (offset)
-                                *offset = q;
-
-                        return 1;
-                }
-
-                z = q;
-        }
-}
-
-int journal_file_move_to_entry_by_seqnum_for_data(
-                JournalFile *f,
-                uint64_t data_offset,
-                uint64_t seqnum,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        Object *d;
-        int r;
-
-        assert(f);
-
-        r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
-        if (r < 0)
-                return r;
-
-        return generic_array_bisect_plus_one(f,
-                                             le64toh(d->data.entry_offset),
-                                             le64toh(d->data.entry_array_offset),
-                                             le64toh(d->data.n_entries),
-                                             seqnum,
-                                             test_object_seqnum,
-                                             direction,
-                                             ret, offset, NULL);
-}
-
-int journal_file_move_to_entry_by_realtime_for_data(
-                JournalFile *f,
-                uint64_t data_offset,
-                uint64_t realtime,
-                direction_t direction,
-                Object **ret, uint64_t *offset) {
-
-        Object *d;
-        int r;
-
-        assert(f);
-
-        r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
-        if (r < 0)
-                return r;
-
-        return generic_array_bisect_plus_one(f,
-                                             le64toh(d->data.entry_offset),
-                                             le64toh(d->data.entry_array_offset),
-                                             le64toh(d->data.n_entries),
-                                             realtime,
-                                             test_object_realtime,
-                                             direction,
-                                             ret, offset, NULL);
-}
-
-void journal_file_dump(JournalFile *f) {
-        Object *o;
-        int r;
-        uint64_t p;
-
-        assert(f);
-
-        journal_file_print_header(f);
-
-        p = le64toh(f->header->header_size);
-        while (p != 0) {
-                r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
-                if (r < 0)
-                        goto fail;
-
-                switch (o->object.type) {
-
-                case OBJECT_UNUSED:
-                        printf("Type: OBJECT_UNUSED\n");
-                        break;
-
-                case OBJECT_DATA:
-                        printf("Type: OBJECT_DATA\n");
-                        break;
-
-                case OBJECT_FIELD:
-                        printf("Type: OBJECT_FIELD\n");
-                        break;
-
-                case OBJECT_ENTRY:
-                        printf("Type: OBJECT_ENTRY seqnum=%"PRIu64" monotonic=%"PRIu64" realtime=%"PRIu64"\n",
-                               le64toh(o->entry.seqnum),
-                               le64toh(o->entry.monotonic),
-                               le64toh(o->entry.realtime));
-                        break;
-
-                case OBJECT_FIELD_HASH_TABLE:
-                        printf("Type: OBJECT_FIELD_HASH_TABLE\n");
-                        break;
-
-                case OBJECT_DATA_HASH_TABLE:
-                        printf("Type: OBJECT_DATA_HASH_TABLE\n");
-                        break;
-
-                case OBJECT_ENTRY_ARRAY:
-                        printf("Type: OBJECT_ENTRY_ARRAY\n");
-                        break;
-
-                case OBJECT_TAG:
-                        printf("Type: OBJECT_TAG seqnum=%"PRIu64" epoch=%"PRIu64"\n",
-                               le64toh(o->tag.seqnum),
-                               le64toh(o->tag.epoch));
-                        break;
-
-                default:
-                        printf("Type: unknown (%i)\n", o->object.type);
-                        break;
-                }
-
-                if (o->object.flags & OBJECT_COMPRESSION_MASK)
-                        printf("Flags: %s\n",
-                               object_compressed_to_string(o->object.flags & OBJECT_COMPRESSION_MASK));
-
-                if (p == le64toh(f->header->tail_object_offset))
-                        p = 0;
-                else
-                        p = p + ALIGN64(le64toh(o->object.size));
-        }
-
-        return;
-fail:
-        log_error("File corrupt");
-}
-
-static const char* format_timestamp_safe(char *buf, size_t l, usec_t t) {
-        const char *x;
-
-        x = format_timestamp(buf, l, t);
-        if (x)
-                return x;
-        return " --- ";
-}
-
-void journal_file_print_header(JournalFile *f) {
-        char a[33], b[33], c[33], d[33];
-        char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX], z[FORMAT_TIMESTAMP_MAX];
-        struct stat st;
-        char bytes[FORMAT_BYTES_MAX];
-
-        assert(f);
-
-        printf("File Path: %s\n"
-               "File ID: %s\n"
-               "Machine ID: %s\n"
-               "Boot ID: %s\n"
-               "Sequential Number ID: %s\n"
-               "State: %s\n"
-               "Compatible Flags:%s%s\n"
-               "Incompatible Flags:%s%s%s\n"
-               "Header size: %"PRIu64"\n"
-               "Arena size: %"PRIu64"\n"
-               "Data Hash Table Size: %"PRIu64"\n"
-               "Field Hash Table Size: %"PRIu64"\n"
-               "Rotate Suggested: %s\n"
-               "Head Sequential Number: %"PRIu64"\n"
-               "Tail Sequential Number: %"PRIu64"\n"
-               "Head Realtime Timestamp: %s\n"
-               "Tail Realtime Timestamp: %s\n"
-               "Tail Monotonic Timestamp: %s\n"
-               "Objects: %"PRIu64"\n"
-               "Entry Objects: %"PRIu64"\n",
-               f->path,
-               sd_id128_to_string(f->header->file_id, a),
-               sd_id128_to_string(f->header->machine_id, b),
-               sd_id128_to_string(f->header->boot_id, c),
-               sd_id128_to_string(f->header->seqnum_id, d),
-               f->header->state == STATE_OFFLINE ? "OFFLINE" :
-               f->header->state == STATE_ONLINE ? "ONLINE" :
-               f->header->state == STATE_ARCHIVED ? "ARCHIVED" : "UNKNOWN",
-               JOURNAL_HEADER_SEALED(f->header) ? " SEALED" : "",
-               (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_ANY) ? " ???" : "",
-               JOURNAL_HEADER_COMPRESSED_XZ(f->header) ? " COMPRESSED-XZ" : "",
-               JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ? " COMPRESSED-LZ4" : "",
-               (le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_ANY) ? " ???" : "",
-               le64toh(f->header->header_size),
-               le64toh(f->header->arena_size),
-               le64toh(f->header->data_hash_table_size) / sizeof(HashItem),
-               le64toh(f->header->field_hash_table_size) / sizeof(HashItem),
-               yes_no(journal_file_rotate_suggested(f, 0)),
-               le64toh(f->header->head_entry_seqnum),
-               le64toh(f->header->tail_entry_seqnum),
-               format_timestamp_safe(x, sizeof(x), le64toh(f->header->head_entry_realtime)),
-               format_timestamp_safe(y, sizeof(y), le64toh(f->header->tail_entry_realtime)),
-               format_timespan(z, sizeof(z), le64toh(f->header->tail_entry_monotonic), USEC_PER_MSEC),
-               le64toh(f->header->n_objects),
-               le64toh(f->header->n_entries));
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_data))
-                printf("Data Objects: %"PRIu64"\n"
-                       "Data Hash Table Fill: %.1f%%\n",
-                       le64toh(f->header->n_data),
-                       100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem))));
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_fields))
-                printf("Field Objects: %"PRIu64"\n"
-                       "Field Hash Table Fill: %.1f%%\n",
-                       le64toh(f->header->n_fields),
-                       100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem))));
-
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_tags))
-                printf("Tag Objects: %"PRIu64"\n",
-                       le64toh(f->header->n_tags));
-        if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
-                printf("Entry Array Objects: %"PRIu64"\n",
-                       le64toh(f->header->n_entry_arrays));
-
-        if (fstat(f->fd, &st) >= 0)
-                printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (off_t) st.st_blocks * 512ULL));
-}
-
-int journal_file_open(
-                const char *fname,
-                int flags,
-                mode_t mode,
-                bool compress,
-                bool seal,
-                JournalMetrics *metrics,
-                MMapCache *mmap_cache,
-                JournalFile *template,
-                JournalFile **ret) {
-
-        bool newly_created = false;
-        JournalFile *f;
-        void *h;
-        int r;
-
-        assert(fname);
-        assert(ret);
-
-        if ((flags & O_ACCMODE) != O_RDONLY &&
-            (flags & O_ACCMODE) != O_RDWR)
-                return -EINVAL;
-
-        if (!endswith(fname, ".journal") &&
-            !endswith(fname, ".journal~"))
-                return -EINVAL;
-
-        f = new0(JournalFile, 1);
-        if (!f)
-                return -ENOMEM;
-
-        f->fd = -1;
-        f->mode = mode;
-
-        f->flags = flags;
-        f->prot = prot_from_flags(flags);
-        f->writable = (flags & O_ACCMODE) != O_RDONLY;
-#if defined(HAVE_LZ4)
-        f->compress_lz4 = compress;
-#elif defined(HAVE_XZ)
-        f->compress_xz = compress;
-#endif
-#ifdef HAVE_GCRYPT
-        f->seal = seal;
-#endif
-
-        if (mmap_cache)
-                f->mmap = mmap_cache_ref(mmap_cache);
-        else {
-                f->mmap = mmap_cache_new();
-                if (!f->mmap) {
-                        r = -ENOMEM;
-                        goto fail;
-                }
-        }
-
-        f->path = strdup(fname);
-        if (!f->path) {
-                r = -ENOMEM;
-                goto fail;
-        }
-
-        f->chain_cache = ordered_hashmap_new(&uint64_hash_ops);
-        if (!f->chain_cache) {
-                r = -ENOMEM;
-                goto fail;
-        }
-
-        f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
-        if (f->fd < 0) {
-                r = -errno;
-                goto fail;
-        }
-
-        r = journal_file_fstat(f);
-        if (r < 0)
-                goto fail;
-
-        if (f->last_stat.st_size == 0 && f->writable) {
-
-                /* Before we write anything, turn off COW logic. Given
-                 * our write pattern that is quite unfriendly to COW
-                 * file systems this should greatly improve
-                 * performance on COW file systems, such as btrfs, at
-                 * the expense of data integrity features (which
-                 * shouldn't be too bad, given that we do our own
-                 * checksumming). */
-                r = chattr_fd(f->fd, true, FS_NOCOW_FL);
-                if (r < 0 && r != -ENOTTY)
-                        log_warning_errno(r, "Failed to set file attributes: %m");
-
-                /* Let's attach the creation time to the journal file,
-                 * so that the vacuuming code knows the age of this
-                 * file even if the file might end up corrupted one
-                 * day... Ideally we'd just use the creation time many
-                 * file systems maintain for each file, but there is
-                 * currently no usable API to query this, hence let's
-                 * emulate this via extended attributes. If extended
-                 * attributes are not supported we'll just skip this,
-                 * and rely solely on mtime/atime/ctime of the file. */
-
-                fd_setcrtime(f->fd, 0);
-
-#ifdef HAVE_GCRYPT
-                /* Try to load the FSPRG state, and if we can't, then
-                 * just don't do sealing */
-                if (f->seal) {
-                        r = journal_file_fss_load(f);
-                        if (r < 0)
-                                f->seal = false;
-                }
-#endif
-
-                r = journal_file_init_header(f, template);
-                if (r < 0)
-                        goto fail;
-
-                r = journal_file_fstat(f);
-                if (r < 0)
-                        goto fail;
-
-                newly_created = true;
-        }
-
-        if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) {
-                r = -EIO;
-                goto fail;
-        }
-
-        r = mmap_cache_get(f->mmap, f->fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h);
-        if (r < 0)
-                goto fail;
-
-        f->header = h;
-
-        if (!newly_created) {
-                r = journal_file_verify_header(f);
-                if (r < 0)
-                        goto fail;
-        }
-
-#ifdef HAVE_GCRYPT
-        if (!newly_created && f->writable) {
-                r = journal_file_fss_load(f);
-                if (r < 0)
-                        goto fail;
-        }
-#endif
-
-        if (f->writable) {
-                if (metrics) {
-                        journal_default_metrics(metrics, f->fd);
-                        f->metrics = *metrics;
-                } else if (template)
-                        f->metrics = template->metrics;
-
-                r = journal_file_refresh_header(f);
-                if (r < 0)
-                        goto fail;
-        }
-
-#ifdef HAVE_GCRYPT
-        r = journal_file_hmac_setup(f);
-        if (r < 0)
-                goto fail;
-#endif
-
-        if (newly_created) {
-                r = journal_file_setup_field_hash_table(f);
-                if (r < 0)
-                        goto fail;
-
-                r = journal_file_setup_data_hash_table(f);
-                if (r < 0)
-                        goto fail;
-
-#ifdef HAVE_GCRYPT
-                r = journal_file_append_first_tag(f);
-                if (r < 0)
-                        goto fail;
-#endif
-        }
-
-        r = journal_file_map_field_hash_table(f);
-        if (r < 0)
-                goto fail;
-
-        r = journal_file_map_data_hash_table(f);
-        if (r < 0)
-                goto fail;
-
-        if (mmap_cache_got_sigbus(f->mmap, f->fd)) {
-                r = -EIO;
-                goto fail;
-        }
-
-        *ret = f;
-        return 0;
-
-fail:
-        if (f->fd >= 0 && mmap_cache_got_sigbus(f->mmap, f->fd))
-                r = -EIO;
-
-        journal_file_close(f);
-
-        return r;
-}
-
-int journal_file_rotate(JournalFile **f, bool compress, bool seal) {
-        _cleanup_free_ char *p = NULL;
-        size_t l;
-        JournalFile *old_file, *new_file = NULL;
-        int r;
-
-        assert(f);
-        assert(*f);
-
-        old_file = *f;
-
-        if (!old_file->writable)
-                return -EINVAL;
-
-        if (!endswith(old_file->path, ".journal"))
-                return -EINVAL;
-
-        l = strlen(old_file->path);
-        r = asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal",
-                     (int) l - 8, old_file->path,
-                     SD_ID128_FORMAT_VAL(old_file->header->seqnum_id),
-                     le64toh((*f)->header->head_entry_seqnum),
-                     le64toh((*f)->header->head_entry_realtime));
-        if (r < 0)
-                return -ENOMEM;
-
-        /* Try to rename the file to the archived version. If the file
-         * already was deleted, we'll get ENOENT, let's ignore that
-         * case. */
-        r = rename(old_file->path, p);
-        if (r < 0 && errno != ENOENT)
-                return -errno;
-
-        old_file->header->state = STATE_ARCHIVED;
-
-        /* Currently, btrfs is not very good with out write patterns
-         * and fragments heavily. Let's defrag our journal files when
-         * we archive them */
-        old_file->defrag_on_close = true;
-
-        r = journal_file_open(old_file->path, old_file->flags, old_file->mode, compress, seal, NULL, old_file->mmap, old_file, &new_file);
-        journal_file_close(old_file);
-
-        *f = new_file;
-        return r;
-}
-
-int journal_file_open_reliably(
-                const char *fname,
-                int flags,
-                mode_t mode,
-                bool compress,
-                bool seal,
-                JournalMetrics *metrics,
-                MMapCache *mmap_cache,
-                JournalFile *template,
-                JournalFile **ret) {
-
-        int r;
-        size_t l;
-        _cleanup_free_ char *p = NULL;
-
-        r = journal_file_open(fname, flags, mode, compress, seal,
-                              metrics, mmap_cache, template, ret);
-        if (!IN_SET(r,
-                    -EBADMSG,           /* corrupted */
-                    -ENODATA,           /* truncated */
-                    -EHOSTDOWN,         /* other machine */
-                    -EPROTONOSUPPORT,   /* incompatible feature */
-                    -EBUSY,             /* unclean shutdown */
-                    -ESHUTDOWN,         /* already archived */
-                    -EIO,               /* IO error, including SIGBUS on mmap */
-                    -EIDRM              /* File has been deleted */))
-                return r;
-
-        if ((flags & O_ACCMODE) == O_RDONLY)
-                return r;
-
-        if (!(flags & O_CREAT))
-                return r;
-
-        if (!endswith(fname, ".journal"))
-                return r;
-
-        /* The file is corrupted. Rotate it away and try it again (but only once) */
-
-        l = strlen(fname);
-        if (asprintf(&p, "%.*s@%016"PRIx64 "-%016"PRIx64 ".journal~",
-                     (int) l - 8, fname,
-                     now(CLOCK_REALTIME),
-                     random_u64()) < 0)
-                return -ENOMEM;
-
-        r = rename(fname, p);
-        if (r < 0)
-                return -errno;
-
-        /* btrfs doesn't cope well with our write pattern and
-         * fragments heavily. Let's defrag all files we rotate */
-
-        (void) chattr_path(p, false, FS_NOCOW_FL);
-        (void) btrfs_defrag(p);
-
-        log_warning("File %s corrupted or uncleanly shut down, renaming and replacing.", fname);
-
-        return journal_file_open(fname, flags, mode, compress, seal,
-                                 metrics, mmap_cache, template, ret);
-}
-
-int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, Object **ret, uint64_t *offset) {
-        uint64_t i, n;
-        uint64_t q, xor_hash = 0;
-        int r;
-        EntryItem *items;
-        dual_timestamp ts;
-
-        assert(from);
-        assert(to);
-        assert(o);
-        assert(p);
-
-        if (!to->writable)
-                return -EPERM;
-
-        ts.monotonic = le64toh(o->entry.monotonic);
-        ts.realtime = le64toh(o->entry.realtime);
-
-        n = journal_file_entry_n_items(o);
-        /* alloca() can't take 0, hence let's allocate at least one */
-        items = alloca(sizeof(EntryItem) * MAX(1u, n));
-
-        for (i = 0; i < n; i++) {
-                uint64_t l, h;
-                le64_t le_hash;
-                size_t t;
-                void *data;
-                Object *u;
-
-                q = le64toh(o->entry.items[i].object_offset);
-                le_hash = o->entry.items[i].hash;
-
-                r = journal_file_move_to_object(from, OBJECT_DATA, q, &o);
-                if (r < 0)
-                        return r;
-
-                if (le_hash != o->data.hash)
-                        return -EBADMSG;
-
-                l = le64toh(o->object.size) - offsetof(Object, data.payload);
-                t = (size_t) l;
-
-                /* We hit the limit on 32bit machines */
-                if ((uint64_t) t != l)
-                        return -E2BIG;
-
-                if (o->object.flags & OBJECT_COMPRESSION_MASK) {
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
-                        size_t rsize = 0;
-
-                        r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK,
-                                            o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0);
-                        if (r < 0)
-                                return r;
-
-                        data = from->compress_buffer;
-                        l = rsize;
-#else
-                        return -EPROTONOSUPPORT;
-#endif
-                } else
-                        data = o->data.payload;
-
-                r = journal_file_append_data(to, data, l, &u, &h);
-                if (r < 0)
-                        return r;
-
-                xor_hash ^= le64toh(u->data.hash);
-                items[i].object_offset = htole64(h);
-                items[i].hash = u->data.hash;
-
-                r = journal_file_move_to_object(from, OBJECT_ENTRY, p, &o);
-                if (r < 0)
-                        return r;
-        }
-
-        r = journal_file_append_entry_internal(to, &ts, xor_hash, items, n, seqnum, ret, offset);
-
-        if (mmap_cache_got_sigbus(to->mmap, to->fd))
-                return -EIO;
-
-        return r;
-}
-
-void journal_default_metrics(JournalMetrics *m, int fd) {
-        uint64_t fs_size = 0;
-        struct statvfs ss;
-        char a[FORMAT_BYTES_MAX], b[FORMAT_BYTES_MAX], c[FORMAT_BYTES_MAX], d[FORMAT_BYTES_MAX];
-
-        assert(m);
-        assert(fd >= 0);
-
-        if (fstatvfs(fd, &ss) >= 0)
-                fs_size = ss.f_frsize * ss.f_blocks;
-
-        if (m->max_use == (uint64_t) -1) {
-
-                if (fs_size > 0) {
-                        m->max_use = PAGE_ALIGN(fs_size / 10); /* 10% of file system size */
-
-                        if (m->max_use > DEFAULT_MAX_USE_UPPER)
-                                m->max_use = DEFAULT_MAX_USE_UPPER;
-
-                        if (m->max_use < DEFAULT_MAX_USE_LOWER)
-                                m->max_use = DEFAULT_MAX_USE_LOWER;
-                } else
-                        m->max_use = DEFAULT_MAX_USE_LOWER;
-        } else {
-                m->max_use = PAGE_ALIGN(m->max_use);
-
-                if (m->max_use < JOURNAL_FILE_SIZE_MIN*2)
-                        m->max_use = JOURNAL_FILE_SIZE_MIN*2;
-        }
-
-        if (m->max_size == (uint64_t) -1) {
-                m->max_size = PAGE_ALIGN(m->max_use / 8); /* 8 chunks */
-
-                if (m->max_size > DEFAULT_MAX_SIZE_UPPER)
-                        m->max_size = DEFAULT_MAX_SIZE_UPPER;
-        } else
-                m->max_size = PAGE_ALIGN(m->max_size);
-
-        if (m->max_size < JOURNAL_FILE_SIZE_MIN)
-                m->max_size = JOURNAL_FILE_SIZE_MIN;
-
-        if (m->max_size*2 > m->max_use)
-                m->max_use = m->max_size*2;
-
-        if (m->min_size == (uint64_t) -1)
-                m->min_size = JOURNAL_FILE_SIZE_MIN;
-        else {
-                m->min_size = PAGE_ALIGN(m->min_size);
-
-                if (m->min_size < JOURNAL_FILE_SIZE_MIN)
-                        m->min_size = JOURNAL_FILE_SIZE_MIN;
-
-                if (m->min_size > m->max_size)
-                        m->max_size = m->min_size;
-        }
-
-        if (m->keep_free == (uint64_t) -1) {
-
-                if (fs_size > 0) {
-                        m->keep_free = PAGE_ALIGN(fs_size * 3 / 20); /* 15% of file system size */
-
-                        if (m->keep_free > DEFAULT_KEEP_FREE_UPPER)
-                                m->keep_free = DEFAULT_KEEP_FREE_UPPER;
-
-                } else
-                        m->keep_free = DEFAULT_KEEP_FREE;
-        }
-
-        log_debug("Fixed max_use=%s max_size=%s min_size=%s keep_free=%s",
-                  format_bytes(a, sizeof(a), m->max_use),
-                  format_bytes(b, sizeof(b), m->max_size),
-                  format_bytes(c, sizeof(c), m->min_size),
-                  format_bytes(d, sizeof(d), m->keep_free));
-}
-
-int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *to) {
-        assert(f);
-        assert(from || to);
-
-        if (from) {
-                if (f->header->head_entry_realtime == 0)
-                        return -ENOENT;
-
-                *from = le64toh(f->header->head_entry_realtime);
-        }
-
-        if (to) {
-                if (f->header->tail_entry_realtime == 0)
-                        return -ENOENT;
-
-                *to = le64toh(f->header->tail_entry_realtime);
-        }
-
-        return 1;
-}
-
-int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *from, usec_t *to) {
-        Object *o;
-        uint64_t p;
-        int r;
-
-        assert(f);
-        assert(from || to);
-
-        r = find_data_object_by_boot_id(f, boot_id, &o, &p);
-        if (r <= 0)
-                return r;
-
-        if (le64toh(o->data.n_entries) <= 0)
-                return 0;
-
-        if (from) {
-                r = journal_file_move_to_object(f, OBJECT_ENTRY, le64toh(o->data.entry_offset), &o);
-                if (r < 0)
-                        return r;
-
-                *from = le64toh(o->entry.monotonic);
-        }
-
-        if (to) {
-                r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
-                if (r < 0)
-                        return r;
-
-                r = generic_array_get_plus_one(f,
-                                               le64toh(o->data.entry_offset),
-                                               le64toh(o->data.entry_array_offset),
-                                               le64toh(o->data.n_entries)-1,