]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #15557 from poettering/journal-zero-fix
authorAnita Zhang <the.anitazha@gmail.com>
Thu, 21 May 2020 01:14:51 +0000 (18:14 -0700)
committerGitHub <noreply@github.com>
Thu, 21 May 2020 01:14:51 +0000 (18:14 -0700)
journal: deal better with reading from zeroed out journal mmaps

1  2 
src/journal/sd-journal.c

diff --combined src/journal/sd-journal.c
index 5c13134b3086906a3cf99f5c804201c1d09e3a67,dc3e61e48934e10f328315894097d9005f1987b9..9b6c425285e3825ff5c20f1a4a7be458ff375168
@@@ -115,28 -115,24 +115,24 @@@ static void detach_location(sd_journal 
                  journal_file_reset_location(f);
  }
  
- static void reset_location(sd_journal *j) {
-         assert(j);
-         detach_location(j);
-         zero(j->current_location);
- }
  static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
          assert(l);
          assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK));
          assert(f);
-         assert(o->object.type == OBJECT_ENTRY);
-         l->type = type;
-         l->seqnum = le64toh(o->entry.seqnum);
-         l->seqnum_id = f->header->seqnum_id;
-         l->realtime = le64toh(o->entry.realtime);
-         l->monotonic = le64toh(o->entry.monotonic);
-         l->boot_id = o->entry.boot_id;
-         l->xor_hash = le64toh(o->entry.xor_hash);
  
-         l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
+         *l = (Location) {
+                 .type = type,
+                 .seqnum = le64toh(o->entry.seqnum),
+                 .seqnum_id = f->header->seqnum_id,
+                 .realtime = le64toh(o->entry.realtime),
+                 .monotonic = le64toh(o->entry.monotonic),
+                 .boot_id = o->entry.boot_id,
+                 .xor_hash = le64toh(o->entry.xor_hash),
+                 .seqnum_set = true,
+                 .realtime_set = true,
+                 .monotonic_set = true,
+                 .xor_hash_set = true,
+         };
  }
  
  static void set_location(sd_journal *j, JournalFile *f, Object *o) {
@@@ -1014,9 -1010,10 +1010,10 @@@ _public_ int sd_journal_seek_cursor(sd_
              !realtime_set)
                  return -EINVAL;
  
-         reset_location(j);
-         j->current_location.type = LOCATION_SEEK;
+         detach_location(j);
+         j->current_location = (Location) {
+                 .type = LOCATION_SEEK,
+         };
  
          if (realtime_set) {
                  j->current_location.realtime = (uint64_t) realtime;
@@@ -1129,11 -1126,14 +1126,14 @@@ _public_ int sd_journal_seek_monotonic_
          assert_return(j, -EINVAL);
          assert_return(!journal_pid_changed(j), -ECHILD);
  
-         reset_location(j);
-         j->current_location.type = LOCATION_SEEK;
-         j->current_location.boot_id = boot_id;
-         j->current_location.monotonic = usec;
-         j->current_location.monotonic_set = true;
+         detach_location(j);
+         j->current_location = (Location) {
+                 .type = LOCATION_SEEK,
+                 .boot_id = boot_id,
+                 .monotonic = usec,
+                 .monotonic_set = true,
+         };
  
          return 0;
  }
@@@ -1142,10 -1142,13 +1142,13 @@@ _public_ int sd_journal_seek_realtime_u
          assert_return(j, -EINVAL);
          assert_return(!journal_pid_changed(j), -ECHILD);
  
-         reset_location(j);
-         j->current_location.type = LOCATION_SEEK;
-         j->current_location.realtime = usec;
-         j->current_location.realtime_set = true;
+         detach_location(j);
+         j->current_location = (Location) {
+                 .type = LOCATION_SEEK,
+                 .realtime = usec,
+                 .realtime_set = true,
+         };
  
          return 0;
  }
@@@ -1154,8 -1157,11 +1157,11 @@@ _public_ int sd_journal_seek_head(sd_jo
          assert_return(j, -EINVAL);
          assert_return(!journal_pid_changed(j), -ECHILD);
  
-         reset_location(j);
-         j->current_location.type = LOCATION_HEAD;
+         detach_location(j);
+         j->current_location = (Location) {
+                 .type = LOCATION_HEAD,
+         };
  
          return 0;
  }
@@@ -1164,8 -1170,11 +1170,11 @@@ _public_ int sd_journal_seek_tail(sd_jo
          assert_return(j, -EINVAL);
          assert_return(!journal_pid_changed(j), -ECHILD);
  
-         reset_location(j);
-         j->current_location.type = LOCATION_TAIL;
+         detach_location(j);
+         j->current_location = (Location) {
+                 .type = LOCATION_TAIL,
+         };
  
          return 0;
  }
@@@ -1626,10 -1635,9 +1635,10 @@@ static int add_directory
              !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
                  return 0;
  
 -        if (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
 -              dirname_has_namespace(dirname, j->namespace) > 0 ||
 -              (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0)))
 +        if (dirname &&
 +            (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
 +               dirname_has_namespace(dirname, j->namespace) > 0 ||
 +               (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0))))
                  return 0;
  
          r = directory_open(j, path, &d);
@@@ -2357,7 -2365,10 +2366,10 @@@ static int return_data(sd_journal *j, J
          uint64_t l;
          int compression;
  
-         l = le64toh(o->object.size) - offsetof(Object, data.payload);
+         l = le64toh(READ_NOW(o->object.size));
+         if (l < offsetof(Object, data.payload))
+                 return -EBADMSG;
+         l -= offsetof(Object, data.payload);
          t = (size_t) l;
  
          /* We can't read objects larger than 4G on a 32bit machine */