switchroot,generator: Only read /proc/cmdline once
authorColin Walters <walters@verbum.org>
Wed, 16 Aug 2023 13:02:11 +0000 (09:02 -0400)
committerColin Walters <walters@verbum.org>
Wed, 16 Aug 2023 13:05:19 +0000 (09:05 -0400)
Change the helper function to parse an existing cmdline instead
of potentially reading `/proc/cmdline` multiple times.

src/libostree/ostree-impl-system-generator.c
src/switchroot/ostree-mount-util.h
src/switchroot/ostree-prepare-root-static.c
src/switchroot/ostree-prepare-root.c

index e96becff81f0c75842103dd111d4104f4112f330..c5db4aa3396b75f2cf6b22be0fada528f5450149 100644 (file)
@@ -251,12 +251,16 @@ _ostree_impl_system_generator (const char *normal_dir, const char *early_dir, co
   if (unlinkat (AT_FDCWD, INITRAMFS_MOUNT_VAR, 0) == 0)
     return TRUE;
 
+  g_autofree char *cmdline = read_proc_cmdline ();
+  if (!cmdline)
+    return glnx_throw (error, "Failed to read /proc/cmdline");
+
   /* If we're installed on a system which isn't using OSTree for boot (e.g.
    * package installed as a dependency for flatpak or whatever), silently
    * exit so that we don't error, but at the same time work where switchroot
    * is PID 1 (and so hasn't created /run/ostree-booted).
    */
-  autofree char *ostree_cmdline = read_proc_cmdline_key ("ostree");
+  g_autofree char *ostree_cmdline = find_proc_cmdline_key (cmdline, "ostree");
   if (!ostree_cmdline)
     return TRUE;
 
index 10e63f98c4089e993d53733351a4a0e68106f30f..1c15fe9004874b70f1366b7773e67e88fbea006f 100644 (file)
@@ -85,18 +85,12 @@ cleanup_free (void *p)
 }
 
 static inline char *
-read_proc_cmdline_key (const char *key)
+find_proc_cmdline_key (const char *cmdline, const char *key)
 {
-  char *cmdline = NULL;
-  const char *iter;
   char *ret = NULL;
   size_t key_len = strlen (key);
 
-  cmdline = read_proc_cmdline ();
-  if (!cmdline)
-    err (EXIT_FAILURE, "failed to read /proc/cmdline");
-
-  iter = cmdline;
+  const char *iter = cmdline;
   while (iter != NULL)
     {
       const char *next = strchr (iter, ' ');
@@ -115,7 +109,6 @@ read_proc_cmdline_key (const char *key)
       iter = next_nonspc;
     }
 
-  free (cmdline);
   return ret;
 }
 
index cd36070df0684c668e8ab5f3594d473df6e42ccd..4aaa46924681c71e46788275bc6536826547a3c0 100644 (file)
@@ -116,7 +116,10 @@ resolve_deploy_path (const char *root_mountpoint)
   char destpath[PATH_MAX];
   struct stat stbuf;
   char *deploy_path;
-  autofree char *ostree_cmdline = read_proc_cmdline_key ("ostree");
+  autofree char *kernel_cmdline = read_proc_cmdline ();
+  if (!kernel_cmdline)
+    errx (EXIT_FAILURE, "Failed to read kernel cmdline");
+  autofree char *ostree_cmdline = find_proc_cmdline_key (kernel_cmdline, "ostree");
 
   if (snprintf (destpath, sizeof (destpath), "%s/%s", root_mountpoint, ostree_cmdline) < 0)
     err (EXIT_FAILURE, "failed to assemble ostree target path");
index 31365b91b30a064d791ebe0e06cc6b8981caa48d..baf52a6eb7e2a0052128a2684a9c669a1a2c5363 100644 (file)
@@ -160,13 +160,13 @@ get_aboot_root_slot (const char *slot_suffix)
 }
 
 static inline char *
-get_ostree_target (void)
+get_ostree_target (const char *cmdline)
 {
-  autofree char *slot_suffix = read_proc_cmdline_key ("androidboot.slot_suffix");
+  autofree char *slot_suffix = find_proc_cmdline_key (cmdline, "androidboot.slot_suffix");
   if (slot_suffix)
     return get_aboot_root_slot (slot_suffix);
 
-  return read_proc_cmdline_key ("ostree");
+  return find_proc_cmdline_key (cmdline, "ostree");
 }
 
 static char *
@@ -175,7 +175,11 @@ resolve_deploy_path (const char *root_mountpoint)
   char destpath[PATH_MAX];
   struct stat stbuf;
   char *deploy_path;
-  autofree char *ostree_target = get_ostree_target ();
+  g_autofree char *kernel_cmdline = read_proc_cmdline ();
+  if (!kernel_cmdline)
+    errx (EXIT_FAILURE, "Failed to read kernel cmdline");
+
+  g_autofree char *ostree_target = get_ostree_target (kernel_cmdline);
   if (!ostree_target)
     errx (EXIT_FAILURE, "No ostree target");