zipl: Convert to a data input stream
authorColin Walters <walters@verbum.org>
Thu, 14 Dec 2023 13:06:57 +0000 (08:06 -0500)
committerColin Walters <walters@verbum.org>
Thu, 14 Dec 2023 13:09:20 +0000 (08:09 -0500)
This high level reader API avoids all the bugs that were
found in previous patches.

src/libostree/ostree-bootloader-zipl.c

index 0329eb658c3c9ac585bce477baf0e8fda05b7fe9..2804ed2644f94ac280f8d0adbd3ec438aa79e9c4 100644 (file)
@@ -22,6 +22,7 @@
 #include "ostree-libarchive-private.h"
 #include "ostree-sysroot-private.h"
 #include "otutil.h"
+#include <gio/gunixinputstream.h>
 #include <string.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -145,15 +146,14 @@ _ostree_secure_boot_is_enabled (gboolean *out_enabled, GCancellable *cancellable
   fd = openat (AT_FDCWD, "/dev/kmsg", O_NONBLOCK | O_RDONLY | O_CLOEXEC);
   if (fd == -1)
     return glnx_throw_errno_prefix (error, "openat(/dev/kmsg)");
-  unsigned max_lines = 5; // no need to read dozens of messages, ours comes really early
+  g_autoptr (GInputStream) istream = g_unix_input_stream_new (g_steal_fd (&fd), TRUE);
+  g_autoptr (GDataInputStream) stream = g_data_input_stream_new (istream);
+  unsigned int max_lines = 5; // no need to read dozens of messages, ours comes really early
   while (*out_enabled != TRUE && max_lines > 0)
     {
-      char buf[1024];
-      ssize_t len = TEMP_FAILURE_RETRY (read (fd, buf, sizeof (buf) - 1));
-      if (len < 0)
-        break;
-      buf[len] = '\0';
-      *out_enabled = strstr (buf, "Secure-IPL enabled") != NULL;
+      gsize len = 0;
+      g_autofree char *line = g_data_input_stream_read_line (stream, &len, NULL, error);
+      *out_enabled = strstr (line, "Secure-IPL enabled") != NULL;
       --max_lines;
     }
   ot_journal_print (LOG_INFO, "s390x: kmsg: Secure Boot enabled: %d", *out_enabled);