HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/journal/journald-manager.c

101,383 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: a0fc0d8c9bae282356d0744068ab54f859896e0c

Lines 1–1000 of 2,634

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/sockios.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/signalfd.h>
#include <sys/statvfs.h>
#include <unistd.h>
 
#include "sd-daemon.h"
#include "sd-journal.h"
#include "sd-messages.h"
#include "sd-varlink.h"
 
#include "acl-util.h"
#include "alloc-util.h"
#include "audit-util.h"
#include "cgroup-util.h"
#include "daemon-util.h"
#include "dirent-util.h"
#include "errno-util.h"
#include "event-util.h"
#include "fd-util.h"
#include "fdset.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "hostname-setup.h"
#include "initrd-util.h"
#include "iovec-util.h"
#include "journal-authenticate.h"
#include "journal-file-util.h"
#include "journal-internal.h"
#include "journal-vacuum.h"
#include "journald-audit.h"
#include "journald-config.h"
#include "journald-context.h"
#include "journald-kmsg.h"
#include "journald-manager.h"
#include "journald-native.h"
#include "journald-rate-limit.h"
#include "journald-socket.h"
#include "journald-stream.h"
#include "journald-sync.h"
#include "journald-syslog.h"
#include "journald-varlink.h"
#include "libaudit-util.h"
#include "log.h"
#include "log-ratelimit.h"
#include "memory-util.h"
#include "mkdir.h"
#include "path-util.h"
#include "prioq.h"
#include "process-util.h"
#include "rm-rf.h"
#include "set.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "socket-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "syslog-util.h"
#include "time-util.h"
#include "uid-classification.h"
#include "user-util.h"
 
#define USER_JOURNALS_MAX 1024
 
#define DEFAULT_KMSG_OWN_INTERVAL (5 * USEC_PER_SEC)
#define DEFAULT_KMSG_OWN_BURST 50
 
#define RECHECK_SPACE_USEC (30*USEC_PER_SEC)
 
#define NOTIFY_SNDBUF_SIZE (8*1024*1024)
 
/* The period to insert between posting changes for coalescing */
#define POST_CHANGE_TIMER_INTERVAL_USEC (250*USEC_PER_MSEC)
 
#define DEFERRED_CLOSES_MAX (4096)
 
#define IDLE_TIMEOUT_USEC (30*USEC_PER_SEC)
 
#define FAILED_TO_WRITE_ENTRY_RATELIMIT ((const RateLimit) { .interval = 1 * USEC_PER_SEC, .burst = 1 })
 
static int manager_schedule_sync(Manager *m, int priority);
static int manager_refresh_idle_timer(Manager *m);
 
static int manager_determine_path_usage(
                Manager *m,
                const char *path,
                uint64_t *ret_used,
                uint64_t *ret_free) {
 
        _cleanup_closedir_ DIR *d = NULL;
        struct statvfs ss;
 
        assert(m);
        assert(path);
        assert(ret_used);
        assert(ret_free);
 
        d = opendir(path);
        if (!d)
                return log_ratelimit_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
                                                errno, JOURNAL_LOG_RATELIMIT, "Failed to open %s: %m", path);
 
        if (fstatvfs(dirfd(d), &ss) < 0)
                return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT,
                                                 "Failed to fstatvfs(%s): %m", path);
 
        *ret_free = ss.f_bsize * ss.f_bavail;
        *ret_used = 0;
        FOREACH_DIRENT_ALL(de, d, break) {
                struct stat st;
 
                if (!endswith(de->d_name, ".journal") &&
                    !endswith(de->d_name, ".journal~"))
                        continue;
 
                if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
                        log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", path, de->d_name);
                        continue;
                }
 
                if (!S_ISREG(st.st_mode))
                        continue;
 
                *ret_used += (uint64_t) st.st_blocks * 512UL;
        }
 
        return 0;
}
 
static void cache_space_invalidate(JournalStorageSpace *space) {
        assert(space);
 
        zero(*space);
}
 
static int cache_space_refresh(Manager *m, JournalStorage *storage) {
        JournalStorageSpace *space;
        JournalMetrics *metrics;
        uint64_t vfs_used, vfs_avail, avail;
        usec_t ts;
        int r;
 
        assert(m);
        assert(storage);
 
        metrics = &storage->metrics;
        space = &storage->space;
 
        ts = now(CLOCK_MONOTONIC);
 
        if (space->timestamp != 0 && usec_add(space->timestamp, RECHECK_SPACE_USEC) > ts)
                return 0;
 
        r = manager_determine_path_usage(m, storage->path, &vfs_used, &vfs_avail);
        if (r < 0)
                return r;
 
        space->vfs_used = vfs_used;
        space->vfs_available = vfs_avail;
 
        avail = LESS_BY(vfs_avail, metrics->keep_free);
 
        space->limit = CLAMP(vfs_used + avail, metrics->min_use, metrics->max_use);
        space->available = LESS_BY(space->limit, vfs_used);
        space->timestamp = ts;
        return 1;
}
 
static void patch_min_use(JournalStorage *storage) {
        assert(storage);
 
        /* Let's bump the min_use limit to the current usage on disk. We do
         * this when starting up and first opening the journal files. This way
         * sudden spikes in disk usage will not cause journald to vacuum files
         * without bounds. Note that this means that only a restart of journald
         * will make it reset this value. */
 
        storage->metrics.min_use = MAX(storage->metrics.min_use, storage->space.vfs_used);
}
 
static JournalStorage* manager_current_storage(Manager *m) {
        assert(m);
 
        return m->system_journal ? &m->system_storage : &m->runtime_storage;
}
 
static int manager_determine_space(Manager *m, uint64_t *available, uint64_t *limit) {
        JournalStorage *js;
        int r;
 
        assert(m);
 
        js = manager_current_storage(m);
 
        r = cache_space_refresh(m, js);
        if (r >= 0) {
                if (available)
                        *available = js->space.available;
                if (limit)
                        *limit = js->space.limit;
        }
        return r;
}
 
void manager_space_usage_message(Manager *m, JournalStorage *storage) {
        assert(m);
 
        if (!storage)
                storage = manager_current_storage(m);
 
        if (cache_space_refresh(m, storage) < 0)
                return;
 
        const JournalMetrics *metrics = &storage->metrics;
 
        manager_driver_message(m, 0,
                               LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_USAGE_STR),
                               LOG_MESSAGE("%s (%s) is %s, max %s, %s free.",
                                           storage->name, storage->path,
                                           FORMAT_BYTES(storage->space.vfs_used),
                                           FORMAT_BYTES(storage->space.limit),
                                           FORMAT_BYTES(storage->space.available)),
                               LOG_ITEM("JOURNAL_NAME=%s", storage->name),
                               LOG_ITEM("JOURNAL_PATH=%s", storage->path),
                               LOG_ITEM("CURRENT_USE=%"PRIu64, storage->space.vfs_used),
                               LOG_ITEM("CURRENT_USE_PRETTY=%s", FORMAT_BYTES(storage->space.vfs_used)),
                               LOG_ITEM("MAX_USE=%"PRIu64, metrics->max_use),
                               LOG_ITEM("MAX_USE_PRETTY=%s", FORMAT_BYTES(metrics->max_use)),
                               LOG_ITEM("DISK_KEEP_FREE=%"PRIu64, metrics->keep_free),
                               LOG_ITEM("DISK_KEEP_FREE_PRETTY=%s", FORMAT_BYTES(metrics->keep_free)),
                               LOG_ITEM("DISK_AVAILABLE=%"PRIu64, storage->space.vfs_available),
                               LOG_ITEM("DISK_AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage->space.vfs_available)),
                               LOG_ITEM("LIMIT=%"PRIu64, storage->space.limit),
                               LOG_ITEM("LIMIT_PRETTY=%s", FORMAT_BYTES(storage->space.limit)),
                               LOG_ITEM("AVAILABLE=%"PRIu64, storage->space.available),
                               LOG_ITEM("AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage->space.available)));
}
 
static void manager_add_acls(JournalFile *f, uid_t uid) {
        assert(f);
 
#if HAVE_ACL
        int r;
 
        if (uid_for_system_journal(uid))
                return;
 
        r = fd_add_uid_acl_permission(f->fd, uid, ACL_READ);
        if (r < 0)
                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                            "Failed to set ACL on %s, ignoring: %m", f->path);
#endif
}
 
static JournalFileFlags manager_get_file_flags(Manager *m, bool seal) {
        assert(m);
 
        return (m->config.compress.enabled ? JOURNAL_COMPRESS : 0) |
                (seal ? JOURNAL_SEAL : 0) |
                JOURNAL_STRICT_ORDER;
}
 
static int manager_open_journal(
                Manager *m,
                bool reliably,
                const char *fname,
                int open_flags,
                bool seal,
                JournalMetrics *metrics,
                JournalFile **ret) {
 
        _cleanup_(journal_file_offline_closep) JournalFile *f = NULL;
        JournalFileFlags file_flags;
        int r;
 
        assert(m);
        assert(fname);
        assert(ret);
 
        file_flags = manager_get_file_flags(m, seal);
 
        set_clear(m->deferred_closes);
 
        if (reliably)
                r = journal_file_open_reliably(
                                fname,
                                open_flags,
                                file_flags,
                                0640,
                                m->config.compress.threshold_bytes,
                                metrics,
                                m->mmap,
                                &f);
        else
                r = journal_file_open(
                                /* fd= */ -EBADF,
                                fname,
                                open_flags,
                                file_flags,
                                0640,
                                m->config.compress.threshold_bytes,
                                metrics,
                                m->mmap,
                                /* template= */ NULL,
                                &f);
        if (r < 0)
                return r;
 
        r = journal_file_enable_post_change_timer(f, m->event, POST_CHANGE_TIMER_INTERVAL_USEC);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(f);
        return r;
}
 
static bool manager_flushed_flag_is_set(Manager *m) {
        const char *fn;
 
        assert(m);
 
        /* We don't support the "flushing" concept for namespace instances, we assume them to always have
         * access to /var */
        if (m->namespace)
                return true;
 
        fn = strjoina(m->runtime_directory, "/flushed");
        return access(fn, F_OK) >= 0;
}
 
static void manager_drop_flushed_flag(Manager *m) {
        const char *fn;
 
        assert(m);
 
        if (m->namespace)
                return;
 
        fn = strjoina(m->runtime_directory, "/flushed");
        if (unlink(fn) < 0 && errno != ENOENT)
                log_ratelimit_warning_errno(errno, JOURNAL_LOG_RATELIMIT,
                                            "Failed to unlink %s, ignoring: %m", fn);
}
 
static int manager_system_journal_open(
                Manager *m,
                bool flush_requested,
                bool relinquish_requested) {
 
        const char *fn;
        int r = 0;
 
        if (!m->system_journal &&
            IN_SET(m->config.storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
            (flush_requested || manager_flushed_flag_is_set(m)) &&
            !relinquish_requested) {
 
                /* If in auto mode: first try to create the machine path, but not the prefix.
                 *
                 * If in persistent mode: create /var/log/journal and the machine path */
 
                if (m->config.storage == STORAGE_PERSISTENT)
                        (void) mkdir_parents(m->system_storage.path, 0755);
 
                (void) mkdir(m->system_storage.path, 0755);
 
                fn = strjoina(m->system_storage.path, "/system.journal");
                r = manager_open_journal(
                                m,
                                /* reliably= */ true,
                                fn,
                                O_RDWR|O_CREAT,
                                m->config.seal,
                                &m->system_storage.metrics,
                                &m->system_journal);
                if (r >= 0) {
                        manager_add_acls(m->system_journal, 0);
                        (void) cache_space_refresh(m, &m->system_storage);
                        patch_min_use(&m->system_storage);
                } else {
                        if (!IN_SET(r, -ENOENT, -EROFS))
                                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                            "Failed to open system journal: %m");
 
                        r = 0;
                }
 
                /* If the runtime journal is open, and we're post-flush, we're recovering from a failed
                 * system journal rotate (ENOSPC) for which the runtime journal was reopened.
                 *
                 * Perform an implicit flush to var, leaving the runtime journal closed, now that the system
                 * journal is back.
                 */
                if (!flush_requested)
                        (void) manager_flush_to_var(m, true);
        }
 
        if (!m->runtime_journal &&
            (m->config.storage != STORAGE_NONE)) {
 
                fn = strjoina(m->runtime_storage.path, "/system.journal");
 
                if (!m->system_journal || relinquish_requested) {
 
                        /* OK, we really need the runtime journal, so create it if necessary. */
 
                        (void) mkdir_parents(m->runtime_storage.path, 0755);
                        (void) mkdir(m->runtime_storage.path, 0750);
 
                        r = manager_open_journal(
                                        m,
                                        /* reliably= */ true,
                                        fn,
                                        O_RDWR|O_CREAT,
                                        /* seal= */ false,
                                        &m->runtime_storage.metrics,
                                        &m->runtime_journal);
                        if (r < 0)
                                return log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                                   "Failed to open runtime journal: %m");
 
                } else if (!manager_flushed_flag_is_set(m)) {
                        /* Try to open the runtime journal, but only if it already exists, so that we can
                         * flush it into the system journal */
 
                        r = manager_open_journal(
                                        m,
                                        /* reliably= */ false,
                                        fn,
                                        O_RDWR,
                                        /* seal= */ false,
                                        &m->runtime_storage.metrics,
                                        &m->runtime_journal);
                        if (r < 0) {
                                if (r != -ENOENT)
                                        log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                                    "Failed to open runtime journal: %m");
 
                                r = 0;
                        }
                }
 
                if (m->runtime_journal) {
                        manager_add_acls(m->runtime_journal, 0);
                        (void) cache_space_refresh(m, &m->runtime_storage);
                        patch_min_use(&m->runtime_storage);
                        manager_drop_flushed_flag(m);
                }
        }
 
        return r;
}
 
static int manager_find_user_journal(Manager *m, uid_t uid, JournalFile **ret) {
        _cleanup_(journal_file_offline_closep) JournalFile *f = NULL;
        _cleanup_free_ char *p = NULL;
        int r;
 
        assert(m);
        assert(!uid_for_system_journal(uid));
        assert(ret);
 
        f = ordered_hashmap_get(m->user_journals, UID_TO_PTR(uid));
        if (f)
                goto found;
 
        if (asprintf(&p, "%s/user-" UID_FMT ".journal", m->system_storage.path, uid) < 0)
                return log_oom();
 
        /* Too many open? Then let's close one (or more) */
        while (ordered_hashmap_size(m->user_journals) >= USER_JOURNALS_MAX) {
                JournalFile *first;
 
                assert_se(first = ordered_hashmap_steal_first(m->user_journals));
                (void) journal_file_offline_close(first);
        }
 
        r = manager_open_journal(
                        m,
                        /* reliably= */ true,
                        p,
                        O_RDWR|O_CREAT,
                        m->config.seal,
                        &m->system_storage.metrics,
                        &f);
        if (r < 0)
                return r;
 
        r = ordered_hashmap_put(m->user_journals, UID_TO_PTR(uid), f);
        if (r < 0)
                return r;
 
        manager_add_acls(f, uid);
 
found:
        *ret = TAKE_PTR(f);
        return 0;
}
 
static JournalFile* manager_find_journal(Manager *m, uid_t uid) {
        int r;
 
        assert(m);
 
        /* A rotate that fails to create the new journal (ENOSPC) leaves the rotated journal as NULL.  Unless
         * we revisit opening, even after space is made available we'll continue to return NULL indefinitely.
         *
         * system_journal_open() is a noop if the journals are already open, so we can just call it here to
         * recover from failed rotates (or anything else that's left the journals as NULL).
         *
         * Fixes https://github.com/systemd/systemd/issues/3968 */
        (void) manager_system_journal_open(m, /* flush_requested= */ false, /* relinquish_requested= */ false);
 
        /* We split up user logs only on /var, not on /run. If the runtime file is open, we write to it
         * exclusively, in order to guarantee proper order as soon as we flush /run to /var and close the
         * runtime file. */
 
        if (m->runtime_journal)
                return m->runtime_journal;
 
        /* If we are not in persistent mode, then we need return NULL immediately rather than opening a
         * persistent journal of any sort.
         *
         * Fixes https://github.com/systemd/systemd/issues/20390 */
        if (!IN_SET(m->config.storage, STORAGE_AUTO, STORAGE_PERSISTENT))
                return NULL;
 
        if (!uid_for_system_journal(uid)) {
                JournalFile *f = NULL;
 
                r = manager_find_user_journal(m, uid, &f);
                if (r >= 0)
                        return ASSERT_PTR(f);
 
                log_warning_errno(r, "Failed to open user journal file, falling back to system journal: %m");
        }
 
        return m->system_journal;
}
 
static int manager_do_rotate(
                Manager *m,
                JournalFile **f,
                const char* name,
                bool seal,
                uint32_t uid) {
 
        int r;
 
        assert(f);
        assert(m);
 
        if (!*f)
                return -EINVAL;
 
        log_debug("Rotating journal file %s.", (*f)->path);
 
        r = journal_file_rotate(f, m->mmap, manager_get_file_flags(m, seal), m->config.compress.threshold_bytes, m->deferred_closes);
        if (r < 0) {
                if (*f)
                        return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT,
                                                         "Failed to rotate %s: %m", (*f)->path);
                else
                        return log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT,
                                                         "Failed to create new %s journal: %m", name);
        }
 
        manager_add_acls(*f, uid);
        return r;
}
 
static void manager_process_deferred_closes(Manager *m) {
        JournalFile *f;
 
        /* Perform any deferred closes which aren't still offlining. */
        SET_FOREACH(f, m->deferred_closes) {
                if (journal_file_is_offlining(f))
                        continue;
 
                (void) set_remove(m->deferred_closes, f);
                (void) journal_file_offline_close(f);
        }
}
 
static void manager_vacuum_deferred_closes(Manager *m) {
        assert(m);
 
        /* Make some room in the deferred closes list, so that it doesn't grow without bounds */
        if (set_size(m->deferred_closes) < DEFERRED_CLOSES_MAX)
                return;
 
        /* Let's first remove all journal files that might already have completed closing */
        manager_process_deferred_closes(m);
 
        /* And now, let's close some more until we reach the limit again. */
        while (set_size(m->deferred_closes) >= DEFERRED_CLOSES_MAX) {
                JournalFile *f;
 
                assert_se(f = set_steal_first(m->deferred_closes));
                journal_file_offline_close(f);
        }
}
 
static int manager_archive_offline_user_journals(Manager *m) {
        _cleanup_closedir_ DIR *d = NULL;
        int r;
 
        assert(m);
 
        d = opendir(m->system_storage.path);
        if (!d) {
                if (errno == ENOENT)
                        return 0;
 
                return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT,
                                                 "Failed to open %s: %m", m->system_storage.path);
        }
 
        for (;;) {
                _cleanup_free_ char *full = NULL;
                _cleanup_close_ int fd = -EBADF;
                struct dirent *de;
                JournalFile *f;
                uid_t uid;
 
                errno = 0;
                de = readdir_no_dot(d);
                if (!de) {
                        if (errno != 0)
                                log_ratelimit_warning_errno(errno, JOURNAL_LOG_RATELIMIT,
                                                            "Failed to enumerate %s, ignoring: %m",
                                                            m->system_storage.path);
                        break;
                }
 
                r = journal_file_parse_uid_from_filename(de->d_name, &uid);
                if (r < 0) {
                        /* Don't warn if the file is not an online or offline user journal. */
                        if (r != -EREMOTE)
                                log_warning_errno(r, "Failed to parse UID from file name '%s', ignoring: %m", de->d_name);
                        continue;
                }
 
                /* Already rotated in the above loop? i.e. is it an open user journal? */
                if (ordered_hashmap_contains(m->user_journals, UID_TO_PTR(uid)))
                        continue;
 
                full = path_join(m->system_storage.path, de->d_name);
                if (!full)
                        return log_oom();
 
                fd = openat(dirfd(d), de->d_name, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK);
                if (fd < 0) {
                        log_ratelimit_full_errno(IN_SET(errno, ELOOP, ENOENT) ? LOG_DEBUG : LOG_WARNING,
                                                 errno, JOURNAL_LOG_RATELIMIT,
                                                 "Failed to open journal file '%s' for rotation: %m", full);
                        continue;
                }
 
                /* Make some room in the set of deferred close()s */
                manager_vacuum_deferred_closes(m);
 
                /* Open the file briefly, so that we can archive it */
                r = journal_file_open(
                                fd,
                                full,
                                O_RDWR,
                                manager_get_file_flags(m, m->config.seal) & ~JOURNAL_STRICT_ORDER, /* strict order does not matter here */
                                0640,
                                m->config.compress.threshold_bytes,
                                &m->system_storage.metrics,
                                m->mmap,
                                /* template= */ NULL,
                                &f);
                if (r < 0) {
                        log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                    "Failed to read journal file %s for rotation, trying to move it out of the way: %m",
                                                    full);
 
                        r = journal_file_dispose(dirfd(d), de->d_name);
                        if (r < 0)
                                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                            "Failed to move %s out of the way, ignoring: %m",
                                                            full);
                        else
                                log_debug("Successfully moved %s out of the way.", full);
 
                        continue;
                }
 
                TAKE_FD(fd); /* Donated to journal_file_open() */
 
                r = journal_file_auth_append_tag(f);
                if (r < 0)
                        log_debug_errno(r, "Failed to append tag when closing journal, ignoring: %m");
 
                r = journal_file_archive(f, NULL);
                if (r < 0)
                        log_debug_errno(r, "Failed to archive journal file '%s', ignoring: %m", full);
 
                journal_file_initiate_close(TAKE_PTR(f), m->deferred_closes);
        }
 
        return 0;
}
 
void manager_rotate(Manager *m) {
        JournalFile *f;
        void *k;
        int r;
 
        log_debug("Rotating...");
 
        /* First, rotate the system journal (either in its runtime flavour or in its runtime flavour) */
        (void) manager_do_rotate(m, &m->runtime_journal, "runtime", /* seal= */ false, /* uid= */ 0);
        (void) manager_do_rotate(m, &m->system_journal, "system", m->config.seal, /* uid= */ 0);
 
        /* Then, rotate all user journals we have open (keeping them open) */
        ORDERED_HASHMAP_FOREACH_KEY(f, k, m->user_journals) {
                r = manager_do_rotate(m, &f, "user", m->config.seal, PTR_TO_UID(k));
                if (r >= 0)
                        ordered_hashmap_replace(m->user_journals, k, f);
                else if (!f)
                        /* Old file has been closed and deallocated */
                        ordered_hashmap_remove(m->user_journals, k);
        }
 
        /* Finally, also rotate all user journals we currently do not have open. (But do so only if we
         * actually have access to /var, i.e. are not in the log-to-runtime-journal mode). */
        if (!m->runtime_journal)
                (void) manager_archive_offline_user_journals(m);
 
        manager_process_deferred_closes(m);
}
 
static void manager_rotate_journal(Manager *m, JournalFile *f, uid_t uid) {
        int r;
 
        assert(m);
        assert(f);
 
        /* This is similar to manager_rotate(), but rotates only specified journal file.
         *
         * 💣💣💣 This invalidate 'f', and the caller cannot reuse the passed JournalFile object. 💣💣💣 */
 
        if (f == m->system_journal)
                (void) manager_do_rotate(m, &m->system_journal, "system", m->config.seal, /* uid= */ 0);
        else if (f == m->runtime_journal)
                (void) manager_do_rotate(m, &m->runtime_journal, "runtime", /* seal= */ false, /* uid= */ 0);
        else {
                assert(ordered_hashmap_get(m->user_journals, UID_TO_PTR(uid)) == f);
                r = manager_do_rotate(m, &f, "user", m->config.seal, uid);
                if (r >= 0)
                        ordered_hashmap_replace(m->user_journals, UID_TO_PTR(uid), f);
                else if (!f)
                        /* Old file has been closed and deallocated */
                        ordered_hashmap_remove(m->user_journals, UID_TO_PTR(uid));
        }
 
        manager_process_deferred_closes(m);
}
 
static void manager_sync(Manager *m, bool wait) {
        JournalFile *f;
        int r;
 
        if (m->system_journal) {
                r = journal_file_set_offline(m->system_journal, wait);
                if (r < 0)
                        log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                    "Failed to sync system journal, ignoring: %m");
        }
 
        ORDERED_HASHMAP_FOREACH(f, m->user_journals) {
                r = journal_file_set_offline(f, wait);
                if (r < 0)
                        log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                                    "Failed to sync user journal, ignoring: %m");
        }
 
        r = sd_event_source_set_enabled(m->sync_event_source, SD_EVENT_OFF);
        if (r < 0)
                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                            "Failed to disable sync timer source, ignoring: %m");
 
        m->sync_scheduled = false;
}
 
static void manager_do_vacuum(Manager *m, JournalStorage *storage, bool verbose) {
        int r;
 
        assert(m);
        assert(storage);
 
        (void) cache_space_refresh(m, storage);
 
        if (verbose)
                manager_space_usage_message(m, storage);
 
        r = journal_directory_vacuum(storage->path, storage->space.limit,
                                     storage->metrics.n_max_files, m->config.max_retention_usec,
                                     &m->oldest_file_usec, verbose);
        if (r < 0 && r != -ENOENT)
                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT,
                                            "Failed to vacuum %s, ignoring: %m", storage->path);
 
        cache_space_invalidate(&storage->space);
}
 
void manager_vacuum(Manager *m, bool verbose) {
        assert(m);
 
        log_debug("Vacuuming...");
 
        m->oldest_file_usec = 0;
 
        if (m->system_journal)
                manager_do_vacuum(m, &m->system_storage, verbose);
        if (m->runtime_journal)
                manager_do_vacuum(m, &m->runtime_storage, verbose);
}
 
static void manager_cache_machine_id(Manager *m) {
        sd_id128_t id;
        int r;
 
        assert(m);
 
        r = sd_id128_get_machine(&id);
        if (r < 0)
                return;
 
        sd_id128_to_string(id, stpcpy(m->machine_id_field, "_MACHINE_ID="));
}
 
static void manager_cache_boot_id(Manager *m) {
        sd_id128_t id;
        int r;
 
        assert(m);
 
        r = sd_id128_get_boot(&id);
        if (r < 0)
                return;
 
        sd_id128_to_string(id, stpcpy(m->boot_id_field, "_BOOT_ID="));
}
 
static void manager_cache_hostname(Manager *m) {
        _cleanup_free_ char *t = NULL;
        char *x;
 
        assert(m);
 
        t = gethostname_malloc();
        if (!t)
                return;
 
        x = strjoin("_HOSTNAME=", t);
        if (!x)
                return;
 
        free_and_replace(m->hostname_field, x);
}
 
static bool shall_try_append_again(JournalFile *f, int r) {
        switch (r) {
 
        case -E2BIG:           /* Hit configured limit          */
        case -EFBIG:           /* Hit fs limit                  */
        case -EDQUOT:          /* Quota limit hit               */
        case -ENOSPC:          /* Disk full                     */
                log_debug_errno(r, "%s: Allocation limit reached, rotating.", f->path);
                return true;
 
        case -EROFS: /* Read-only file system */
                /* When appending an entry fails if shall_try_append_again returns true, the journal is
                 * rotated. If the FS is read-only, rotation will fail and m->system_journal will be set to
                 * NULL. After that, when find_journal will try to open the journal since m->system_journal
                 * will be NULL, it will open the runtime journal. */
                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Read-only file system, rotating.", f->path);
                return true;
 
        case -EIO:             /* I/O error of some kind (mmap) */
                log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "%s: IO error, rotating.", f->path);
                return true;
 
        case -EHOSTDOWN:       /* Other machine                 */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Journal file from other machine, rotating.", f->path);
                return true;
 
        case -EBUSY:           /* Unclean shutdown              */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Unclean shutdown, rotating.", f->path);
                return true;
 
        case -EPROTONOSUPPORT: /* Unsupported feature           */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Unsupported feature, rotating.", f->path);
                return true;
 
        case -EBADMSG:         /* Corrupted                     */
        case -ENODATA:         /* Truncated                     */
        case -ESHUTDOWN:       /* Already archived              */
        case -EADDRNOTAVAIL:   /* Referenced object offset out of bounds */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Journal file corrupted, rotating.", f->path);
                return true;
 
        case -EIDRM:           /* Journal file has been deleted */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Journal file has been deleted, rotating.", f->path);
                return true;
 
        case -EREMCHG:         /* Wallclock time (CLOCK_REALTIME) jumped backwards relative to last journal entry */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Realtime clock jumped backwards relative to last journal entry, rotating.", f->path);
                return true;
 
        case -ENOTNAM: /* Monotonic time (CLOCK_MONOTONIC) jumped backwards relative to last journal entry with the same boot ID */
                log_ratelimit_info_errno(
                                r,
                                JOURNAL_LOG_RATELIMIT,
                                "%s: Monotonic clock jumped backwards relative to last journal entry with the same boot ID, rotating.",
                                f->path);
                return true;
 
        case -EILSEQ:          /* seqnum ID last used in the file doesn't match the one we'd passed when writing an entry to it */
                log_ratelimit_info_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Journal file uses a different sequence number ID, rotating.", f->path);
                return true;
 
        case -EAFNOSUPPORT:
                log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Underlying file system does not support memory mapping or another required file system feature.", f->path);
                return false;
 
        default:
                log_ratelimit_error_errno(r, JOURNAL_LOG_RATELIMIT, "%s: Unexpected error while writing to journal file: %m", f->path);
                return false;
        }
}
 
static void manager_write_to_journal(
                Manager *m,
                uid_t uid,
                const struct iovec *iovec,
                size_t n,
                const dual_timestamp *ts,
                int priority) {
 
        bool vacuumed = false;
        JournalFile *f;
        int r;
 
        assert(m);
        assert(iovec);
        assert(n > 0);
        assert(ts);
 
        if (ts->realtime < m->last_realtime_clock) {
                /* When the time jumps backwards, let's immediately rotate. Of course, this should not happen during
                 * regular operation. However, when it does happen, then we should make sure that we start fresh files
                 * to ensure that the entries in the journal files are strictly ordered by time, in order to ensure
                 * bisection works correctly. */
 
                log_ratelimit_info(JOURNAL_LOG_RATELIMIT, "Time jumped backwards, rotating.");
                manager_rotate(m);
                manager_vacuum(m, /* verbose= */ false);
                vacuumed = true;
        }
 
        f = manager_find_journal(m, uid);
        if (!f)
                return;
 
        if (journal_file_rotate_suggested(f, m->config.max_file_usec, LOG_DEBUG)) {
                if (vacuumed) {
                        log_ratelimit_warning(JOURNAL_LOG_RATELIMIT,
                                              "Suppressing rotation, as we already rotated immediately before write attempt. Giving up.");
                        return;
                }
 
                log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path);
 
                manager_rotate_journal(m, TAKE_PTR(f), uid);
                manager_vacuum(m, /* verbose= */ false);
                vacuumed = true;
 
                f = manager_find_journal(m, uid);
                if (!f)
                        return;
        }
 
        m->last_realtime_clock = ts->realtime;
 
        r = journal_file_append_entry(
                        f,
                        ts,
                        /* boot_id= */ NULL,