journal: synchronize seqnum across files
authorLennart Poettering <lennart@poettering.net>
Fri, 14 Oct 2011 03:12:58 +0000 (05:12 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 14 Oct 2011 03:12:58 +0000 (05:12 +0200)
src/journal/journal-file.c
src/journal/journal-file.h
src/journal/journalctl.c
src/journal/journald.c
src/journal/sd-journal.c
src/journal/sd-journal.h
src/journal/test-journal.c

index 6c8d712..5379781 100644 (file)
@@ -364,12 +364,24 @@ int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Objec
         return 0;
 }
 
-static uint64_t journal_file_seqnum(JournalFile *f) {
+static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
         uint64_t r;
 
         assert(f);
 
         r = le64toh(f->header->seqnum) + 1;
+
+        if (seqnum) {
+                /* If an external seqno counter was passed, we update
+                 * both the local and the external one, and set it to
+                 * the maximum of both */
+
+                if (*seqnum + 1 > r)
+                        r = *seqnum + 1;
+
+                *seqnum = r;
+        }
+
         f->header->seqnum = htole64(r);
 
         return r;
@@ -733,6 +745,7 @@ static int journal_file_append_entry_internal(
                 const dual_timestamp *ts,
                 uint64_t xor_hash,
                 const EntryItem items[], unsigned n_items,
+                uint64_t *seqno,
                 Object **ret, uint64_t *offset) {
         uint64_t np;
         uint64_t osize;
@@ -749,7 +762,7 @@ static int journal_file_append_entry_internal(
                 return r;
 
         o->object.type = htole64(OBJECT_ENTRY);
-        o->entry.seqnum = htole64(journal_file_seqnum(f));
+        o->entry.seqnum = htole64(journal_file_seqnum(f, seqno));
         memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
         o->entry.realtime = htole64(ts ? ts->realtime : now(CLOCK_REALTIME));
         o->entry.monotonic = htole64(ts ? ts->monotonic : now(CLOCK_MONOTONIC));
@@ -769,7 +782,7 @@ static int journal_file_append_entry_internal(
         return 0;
 }
 
-int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
+int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset) {
         unsigned i;
         EntryItem *items;
         int r;
@@ -794,7 +807,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
                 items[i].object_offset = htole64(p);
         }
 
-        r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
+        r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqno, ret, offset);
 
 finish:
         free(items);
index c51504d..92f671a 100644 (file)
@@ -63,7 +63,7 @@ int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Objec
 
 uint64_t journal_file_entry_n_items(Object *o);
 
-int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset);
+int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset);
 
 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset);
 
index bb1f18a..a6b6e0f 100644 (file)
@@ -50,6 +50,7 @@ int main(int argc, char *argv[]) {
                 const void *data;
                 size_t length;
                 char *cursor;
+                uint64_t realtime = 0, monotonic = 0;
 
                 r = sd_journal_get_cursor(j, &cursor);
                 if (r < 0) {
@@ -60,6 +61,13 @@ int main(int argc, char *argv[]) {
                 printf("entry: %s\n", cursor);
                 free(cursor);
 
+                sd_journal_get_realtime_usec(j, &realtime);
+                sd_journal_get_monotonic_usec(j, &monotonic);
+                printf("realtime: %llu\n"
+                       "monotonic: %llu\n",
+                       (unsigned long long) realtime,
+                       (unsigned long long) monotonic);
+
                 SD_JOURNAL_FOREACH_FIELD(j, data, length)
                         printf("\t%.*s\n", (int) length, (const char*) data);
         }
index 1143d81..ede314a 100644 (file)
@@ -43,6 +43,8 @@ typedef struct Server {
         JournalFile *runtime_journal;
         JournalFile *system_journal;
         Hashmap *user_journals;
+
+        uint64_t seqnum;
 } Server;
 
 static void fix_perms(JournalFile *f, uid_t uid) {
@@ -118,7 +120,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
         if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
                 return s->system_journal;
 
-        r = journal_file_open(p, O_RDWR|O_CREAT, 0640, NULL, &f);
+        r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
         free(p);
 
         if (r < 0)
@@ -252,7 +254,7 @@ static void process_message(Server *s, const char *buf, struct ucred *ucred, str
         if (!f)
                 log_warning("Dropping message, as we can't find a place to store the data.");
         else {
-                r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
+                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));
index 1f4ad0f..1614bbf 100644 (file)
@@ -561,3 +561,13 @@ int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
 
         return 1;
 }
+
+int sd_journal_seek_head(sd_journal *j) {
+        assert(j);
+        return -EINVAL;
+}
+
+int sd_journal_seek_tail(sd_journal *j) {
+        assert(j);
+        return -EINVAL;
+}
index bbfcda6..13b5f89 100644 (file)
@@ -56,7 +56,6 @@ void sd_journal_flush_matches(sd_journal *j);
 int sd_journal_seek_head(sd_journal *j);
 int sd_journal_seek_tail(sd_journal *j);
 
-int sd_journal_seek_seqnum(sd_journal *j, uint64_t seqnum);
 int sd_journal_seek_monotonic_usec(sd_journal *j, uint64_t usec);
 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
 
index 8dd26bb..3b67f1a 100644 (file)
@@ -42,15 +42,15 @@ int main(int argc, char *argv[]) {
 
         iovec.iov_base = (void*) test;
         iovec.iov_len = strlen(test);
-        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
+        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
 
         iovec.iov_base = (void*) test2;
         iovec.iov_len = strlen(test2);
-        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
+        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
 
         iovec.iov_base = (void*) test;
         iovec.iov_len = strlen(test);
-        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
+        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
 
         journal_file_dump(f);