]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/test-journal-interleaving.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / test-journal-interleaving.c
index af0d43e39e77a9bfc4e2bbfd3869a6170cef267a..5a88b2774fa53a20331a28d78edc95da9cc2981c 100644 (file)
@@ -1,5 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <unistd.h>
 #include <fcntl.h>
+#include <unistd.h>
 
-#include <systemd/sd-journal.h>
+#include "sd-journal.h"
 
+#include "alloc-util.h"
 #include "journal-file.h"
-#include "journal-internal.h"
 #include "journal-vacuum.h"
-#include "util.h"
 #include "log.h"
+#include "parse-util.h"
+#include "rm-rf.h"
+#include "util.h"
 
 /* This program tests skipping around in a multi-file journal.
  */
 
 static bool arg_keep = false;
 
-_noreturn_ static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
-        log_meta(LOG_CRIT, file, line, func,
-                 "'%s' failed at %s:%u (%s): %s.",
-                 text, file, line, func, strerror(eno));
+noreturn static void log_assert_errno(const char *text, int error, const char *file, int line, const char *func) {
+        log_internal(LOG_CRIT, error, file, line, func,
+                     "'%s' failed at %s:%u (%s): %m", text, file, line, func);
         abort();
 }
 
@@ -52,21 +52,30 @@ _noreturn_ static void log_assert_errno(const char *text, int eno, const char *f
 
 static JournalFile *test_open(const char *name) {
         JournalFile *f;
-        assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
+        assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, NULL, &f));
         return f;
 }
 
 static void test_close(JournalFile *f) {
-        journal_file_close (f);
+        (void) journal_file_close (f);
 }
 
 static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
         char *p;
         dual_timestamp ts;
+        static dual_timestamp previous_ts = {};
         struct iovec iovec[1];
 
         dual_timestamp_get(&ts);
 
+        if (ts.monotonic <= previous_ts.monotonic)
+                ts.monotonic = previous_ts.monotonic + 1;
+
+        if (ts.realtime <= previous_ts.realtime)
+                ts.realtime = previous_ts.realtime + 1;
+
+        previous_ts = ts;
+
         assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
         iovec[0].iov_base = p;
         iovec[0].iov_len = strlen(p);
@@ -189,9 +198,9 @@ static void test_skip(void (*setup)(void)) {
         if (arg_keep)
                 log_info("Not removing %s", t);
         else {
-                journal_directory_vacuum(".", 3000000, 0, 0, NULL);
+                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
 
-                assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
+                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
         }
 
         puts("------------------------------------------------------------");
@@ -207,62 +216,62 @@ static void test_sequence_numbers(void) {
         assert_se(mkdtemp(t));
         assert_se(chdir(t) >= 0);
 
-        assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0644,
-                                    true, false, NULL, NULL, NULL, &one) == 0);
+        assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
+                                    true, false, NULL, NULL, NULL, NULL, &one) == 0);
 
         append_number(one, 1, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 1);
+        assert_se(seqnum == 1);
         append_number(one, 2, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 2);
+        assert_se(seqnum == 2);
 
-        assert(one->header->state == STATE_ONLINE);
-        assert(!sd_id128_equal(one->header->file_id, one->header->machine_id));
-        assert(!sd_id128_equal(one->header->file_id, one->header->boot_id));
-        assert(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
+        assert_se(one->header->state == STATE_ONLINE);
+        assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
+        assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
+        assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
 
         memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
 
-        assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644,
-                                    true, false, NULL, NULL, one, &two) == 0);
+        assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
+                                    true, false, NULL, NULL, NULL, one, &two) == 0);
 
-        assert(two->header->state == STATE_ONLINE);
-        assert(!sd_id128_equal(two->header->file_id, one->header->file_id));
-        assert(sd_id128_equal(one->header->machine_id, one->header->machine_id));
-        assert(sd_id128_equal(one->header->boot_id, one->header->boot_id));
-        assert(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
+        assert_se(two->header->state == STATE_ONLINE);
+        assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
+        assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
+        assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
+        assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
 
         append_number(two, 3, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 3);
+        assert_se(seqnum == 3);
         append_number(two, 4, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 4);
+        assert_se(seqnum == 4);
 
         test_close(two);
 
         append_number(one, 5, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 5);
+        assert_se(seqnum == 5);
 
         append_number(one, 6, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 6);
+        assert_se(seqnum == 6);
 
         test_close(one);
 
         /* restart server */
         seqnum = 0;
 
-        assert_se(journal_file_open("two.journal", O_RDWR, 0,
-                                    true, false, NULL, NULL, NULL, &two) == 0);
+        assert_se(journal_file_open(-1, "two.journal", O_RDWR, 0,
+                                    true, false, NULL, NULL, NULL, NULL, &two) == 0);
 
-        assert(sd_id128_equal(two->header->seqnum_id, seqnum_id));
+        assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
 
         append_number(two, 7, &seqnum);
         printf("seqnum=%"PRIu64"\n", seqnum);
-        assert(seqnum == 5);
+        assert_se(seqnum == 5);
 
         /* So..., here we have the same seqnum in two files with the
          * same seqnum_id. */
@@ -274,9 +283,9 @@ static void test_sequence_numbers(void) {
         if (arg_keep)
                 log_info("Not removing %s", t);
         else {
-                journal_directory_vacuum(".", 3000000, 0, 0, NULL);
+                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
 
-                assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
+                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
         }
 }