chiark / gitweb /
journald: do not calculate free space too early
[elogind.git] / src / journal / journald-server.c
index 88163c01167a19407bb38098ec28b768b3fb6773..de9604024242beae5c0c3384579d1a12bab1b9c8 100644 (file)
@@ -227,9 +227,11 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
                 }
         }
 
+        /* We do not recalculate the mask unconditionally here,
+         * so that the fchmod() mask above stays intact. */
         if (acl_get_permset(entry, &permset) < 0 ||
             acl_add_perm(permset, ACL_READ) < 0 ||
-            acl_calc_mask(&acl) < 0) {
+            calc_acl_mask_if_needed(&acl) < 0) {
                 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
                 goto finish;
         }
@@ -243,7 +245,7 @@ finish:
 }
 
 static JournalFile* find_journal(Server *s, uid_t uid) {
-        char *p;
+        _cleanup_free_ char *p = NULL;
         int r;
         JournalFile *f;
         sd_id128_t machine;
@@ -280,9 +282,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
                 journal_file_close(f);
         }
 
-        r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, s->system_journal, &f);
-        free(p);
-
+        r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &f);
         if (r < 0)
                 return s->system_journal;
 
@@ -370,7 +370,6 @@ void server_sync(Server *s) {
 }
 
 void server_vacuum(Server *s) {
-        char *p;
         char ids[33];
         sd_id128_t machine;
         int r;
@@ -388,29 +387,19 @@ void server_vacuum(Server *s) {
         sd_id128_to_string(machine, ids);
 
         if (s->system_journal) {
-                p = strappend("/var/log/journal/", ids);
-                if (!p) {
-                        log_oom();
-                        return;
-                }
+                char *p = strappenda("/var/log/journal/", ids);
 
                 r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
                 if (r < 0 && r != -ENOENT)
                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
-                free(p);
         }
 
         if (s->runtime_journal) {
-                p = strappend("/run/log/journal/", ids);
-                if (!p) {
-                        log_oom();
-                        return;
-                }
+                char *p = strappenda("/run/log/journal/", ids);
 
                 r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
                 if (r < 0 && r != -ENOENT)
                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
-                free(p);
         }
 
         s->cached_available_space_timestamp = 0;
@@ -476,7 +465,12 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
         }
 
         if (vacuumed || !shall_try_append_again(f, r)) {
-                log_error("Failed to write entry, ignoring: %s", strerror(-r));
+                size_t size = 0;
+                unsigned i;
+                for (i = 0; i < n; i++)
+                        size += iovec[i].iov_len;
+
+                log_error("Failed to write entry (%d items, %zu bytes), ignoring: %s", n, size, strerror(-r));
                 return;
         }
 
@@ -489,8 +483,14 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
 
         log_debug("Retrying write.");
         r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
-        if (r < 0)
-                log_error("Failed to write entry, ignoring: %s", strerror(-r));
+        if (r < 0) {
+                size_t size = 0;
+                unsigned i;
+                for (i = 0; i < n; i++)
+                        size += iovec[i].iov_len;
+
+                log_error("Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %s", n, size, strerror(-r));
+        }
 }
 
 static void dispatch_message_real(
@@ -798,27 +798,27 @@ static int system_journal_open(Server *s) {
                 if (s->storage == STORAGE_PERSISTENT)
                         (void) mkdir("/var/log/journal/", 0755);
 
-                fn = strappend("/var/log/journal/", ids);
-                if (!fn)
-                        return -ENOMEM;
-
+                fn = strappenda("/var/log/journal/", ids);
                 (void) mkdir(fn, 0755);
-                free(fn);
-
-                fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
-                if (!fn)
-                        return -ENOMEM;
 
+                fn = strappenda(fn, "/system.journal");
                 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
-                free(fn);
 
                 if (r >= 0) {
                         char fb[FORMAT_BYTES_MAX];
+                        uint64_t avail;
 
                         server_fix_perms(s, s->system_journal, 0);
+
                         server_driver_message(s, SD_ID128_NULL, "Allowing system journal files to grow to %s.",
                                               format_bytes(fb, sizeof(fb), s->system_metrics.max_use));
 
+                        avail = available_space(s);
+
+                        if (s->system_metrics.max_use > avail)
+                               server_driver_message(s, SD_ID128_NULL, "Journal size currently limited to %s due to SystemKeepFree.",
+                                                     format_bytes(fb, sizeof(fb), avail));
+
                 } else if (r < 0) {
 
                         if (r != -ENOENT && r != -EROFS)
@@ -868,10 +868,17 @@ static int system_journal_open(Server *s) {
 
                 if (s->runtime_journal) {
                         char fb[FORMAT_BYTES_MAX];
+                        uint64_t avail;
 
                         server_fix_perms(s, s->runtime_journal, 0);
                         server_driver_message(s, SD_ID128_NULL, "Allowing runtime journal files to grow to %s.",
                                               format_bytes(fb, sizeof(fb), s->runtime_metrics.max_use));
+
+                        avail = available_space(s);
+
+                        if (s->system_metrics.max_use > avail)
+                               server_driver_message(s, SD_ID128_NULL, "Journal size currently limited to %s due to RuntimeKeepFree.",
+                                                     format_bytes(fb, sizeof(fb), avail));
                 }
         }
 
@@ -1332,10 +1339,9 @@ int server_schedule_sync(Server *s) {
                 return 0;
 
         if (s->sync_interval_usec) {
-                struct itimerspec sync_timer_enable = {
-                        .it_value.tv_sec = s->sync_interval_usec / USEC_PER_SEC,
-                        .it_value.tv_nsec = s->sync_interval_usec % MSEC_PER_SEC,
-                };
+                struct itimerspec sync_timer_enable = {};
+
+                timespec_store(&sync_timer_enable.it_value, s->sync_interval_usec);
 
                 r = timerfd_settime(s->sync_timer_fd, 0, &sync_timer_enable, NULL);
                 if (r < 0)