chiark / gitweb /
log: introduce a macro to format message id
[elogind.git] / src / journal / journald.c
index a1506b23747b910848ef81b38593bf743d4e49bb..4dcf7d32c2327f5810ee0b8af266f7f70c8e5f4a 100644 (file)
@@ -440,6 +440,35 @@ static char *shortened_cgroup_path(pid_t pid) {
         return path;
 }
 
+static bool shall_try_append_again(JournalFile *f, int r) {
+
+        /* -E2BIG            Hit configured limit
+           -EFBIG            Hit fs limit
+           -EDQUOT           Quota limit hit
+           -ENOSPC           Disk full
+           -EHOSTDOWN        Other machine
+           -EBUSY            Unclean shutdown
+           -EPROTONOSUPPORT  Unsupported feature
+           -EBADMSG          Corrupted
+           -ENODATA          Truncated
+           -ESHUTDOWN        Already archived */
+
+        if (r == -E2BIG || r == -EFBIG || r == -EDQUOT || r == -ENOSPC)
+                log_debug("%s: Allocation limit reached, rotating.", f->path);
+        else if (r == -EHOSTDOWN)
+                log_info("%s: Journal file from other machine, rotating.", f->path);
+        else if (r == -EBUSY)
+                log_info("%s: Unclean shutdown, rotating.", f->path);
+        else if (r == -EPROTONOSUPPORT)
+                log_info("%s: Unsupported feature, rotating.", f->path);
+        else if (r == -EBADMSG || r == -ENODATA || r == ESHUTDOWN)
+                log_warning("%s: Journal file corrupted, rotating.", f->path);
+        else
+                return false;
+
+        return true;
+}
+
 static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
         JournalFile *f;
         bool vacuumed = false;
@@ -454,7 +483,7 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
                 return;
 
         if (journal_file_rotate_suggested(f)) {
-                log_debug("Journal header limits reached or header out-of-date, rotating.");
+                log_debug("%s: Journal header limits reached or header out-of-date, rotating.", f->path);
                 server_rotate(s);
                 server_vacuum(s);
                 vacuumed = true;
@@ -464,45 +493,26 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
                         return;
         }
 
-        for (;;) {
-                r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
-                if (r >= 0)
-                        return;
-
-                if (vacuumed ||
-                    (r != -E2BIG && /* hit limit */
-                     r != -EFBIG && /* hit fs limit */
-                     r != -EDQUOT && /* quota hit */
-                     r != -ENOSPC && /* disk full */
-                     r != -EBADMSG && /* corrupted */
-                     r != -ENODATA && /* truncated */
-                     r != -EHOSTDOWN && /* other machine */
-                     r != -EPROTONOSUPPORT && /* unsupported feature */
-                     r != -EBUSY && /* unclean shutdown */
-                     r != -ESHUTDOWN /* already archived */)) {
-                        log_error("Failed to write entry, ignoring: %s", strerror(-r));
-                        return;
-                }
+        r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
+        if (r >= 0)
+                return;
 
-                if (r == -E2BIG || r == -EFBIG || r == EDQUOT || r == ENOSPC)
-                        log_debug("Allocation limit reached, rotating.");
-                else if (r == -EHOSTDOWN)
-                        log_info("Journal file from other machine, rotating.");
-                else if (r == -EBUSY)
-                        log_info("Unclean shutdown, rotating.");
-                else
-                        log_warning("Journal file corrupted, rotating.");
+        if (vacuumed || !shall_try_append_again(f, r)) {
+                log_error("Failed to write entry, ignoring: %s", strerror(-r));
+                return;
+        }
 
-                server_rotate(s);
-                server_vacuum(s);
-                vacuumed = true;
+        server_rotate(s);
+        server_vacuum(s);
 
-                f = find_journal(s, uid);
-                if (!f)
-                        return;
+        f = find_journal(s, uid);
+        if (!f)
+                return;
 
-                log_debug("Retrying write.");
-        }
+        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));
 }
 
 static void dispatch_message_real(
@@ -715,7 +725,7 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
         IOVEC_SET_STRING(iovec[n++], buffer);
 
         if (!sd_id128_equal(message_id, SD_ID128_NULL)) {
-                snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
+                snprintf(mid, sizeof(mid), MESSAGE_ID(message_id));
                 char_array_0(mid);
                 IOVEC_SET_STRING(iovec[n++], mid);
         }
@@ -942,16 +952,19 @@ static int server_flush_to_var(Server *s) {
                 }
 
                 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
-                if (r == -E2BIG) {
-                        log_debug("Allocation limit reached.");
-
-                        journal_file_post_change(s->system_journal);
-                        server_rotate(s);
-                        server_vacuum(s);
+                if (r >= 0)
+                        continue;
 
-                        r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
+                if (!shall_try_append_again(s->system_journal, r)) {
+                        log_error("Can't write entry: %s", strerror(-r));
+                        goto finish;
                 }
 
+                server_rotate(s);
+                server_vacuum(s);
+
+                log_debug("Retrying write.");
+                r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
                 if (r < 0) {
                         log_error("Can't write entry: %s", strerror(-r));
                         goto finish;