]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journald: split find_journal() up 26021/head
authorFranck Bui <fbui@suse.com>
Mon, 14 Mar 2022 13:53:38 +0000 (14:53 +0100)
committerFranck Bui <fbui@suse.com>
Mon, 16 Jan 2023 14:04:29 +0000 (15:04 +0100)
No functional change.

src/journal/journald-server.c

index e3ce6aaea8bc75a4676bdb1fea39edcb6e88a0e7..d31da2d129519f0b20e57356e3fa65af58a6e2e9 100644 (file)
@@ -416,9 +416,44 @@ static int system_journal_open(Server *s, bool flush_requested, bool relinquish_
         return r;
 }
 
-static ManagedJournalFile* find_journal(Server *s, uid_t uid) {
+static int find_user_journal(Server *s, uid_t uid, ManagedJournalFile **ret) {
+        _cleanup_(managed_journal_file_closep) ManagedJournalFile *f = NULL;
         _cleanup_free_ char *p = NULL;
-        ManagedJournalFile *f;
+        int r;
+
+        assert(!uid_for_system_journal(uid));
+
+        f = ordered_hashmap_get(s->user_journals, UID_TO_PTR(uid));
+        if (f)
+                goto found;
+
+        if (asprintf(&p, "%s/user-" UID_FMT ".journal", s->system_storage.path, uid) < 0)
+                return log_oom();
+
+        /* Too many open? Then let's close one (or more) */
+        while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
+                ManagedJournalFile *first;
+
+                assert_se(first = ordered_hashmap_steal_first(s->user_journals));
+                (void) managed_journal_file_close(first);
+        }
+
+        r = open_journal(s, true, p, O_RDWR|O_CREAT, s->seal, &s->system_storage.metrics, &f);
+        if (r < 0)
+                return r;
+
+        r = ordered_hashmap_put(s->user_journals, UID_TO_PTR(uid), f);
+        if (r < 0)
+                return r;
+
+        server_add_acls(f, uid);
+
+found:
+        *ret = TAKE_PTR(f);
+        return 0;
+}
+
+static ManagedJournalFile* find_journal(Server *s, uid_t uid) {
         int r;
 
         assert(s);
@@ -446,36 +481,17 @@ static ManagedJournalFile* find_journal(Server *s, uid_t uid) {
         if (!IN_SET(s->storage, STORAGE_AUTO, STORAGE_PERSISTENT))
                 return NULL;
 
-        if (uid_for_system_journal(uid))
-                return s->system_journal;
-
-        f = ordered_hashmap_get(s->user_journals, UID_TO_PTR(uid));
-        if (f)
-                return f;
-
-        if (asprintf(&p, "%s/user-" UID_FMT ".journal", s->system_storage.path, uid) < 0) {
-                log_oom();
-                return s->system_journal;
-        }
-
-        /* Too many open? Then let's close one (or more) */
-        while (ordered_hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
-                assert_se(f = ordered_hashmap_steal_first(s->user_journals));
-                (void) managed_journal_file_close(f);
-        }
+        if (!uid_for_system_journal(uid)) {
+                ManagedJournalFile *f = NULL;
 
-        r = open_journal(s, true, p, O_RDWR|O_CREAT, s->seal, &s->system_storage.metrics, &f);
-        if (r < 0)
-                return s->system_journal;
+                r = find_user_journal(s, uid, &f);
+                if (r >= 0)
+                        return ASSERT_PTR(f);
 
-        r = ordered_hashmap_put(s->user_journals, UID_TO_PTR(uid), f);
-        if (r < 0) {
-                (void) managed_journal_file_close(f);
-                return s->system_journal;
+                log_warning_errno(r, "Failed to open user journal file, falling back to system journal: %m");
         }
 
-        server_add_acls(f, uid);
-        return f;
+        return s->system_journal;
 }
 
 static int do_rotate(