docs: Which API calls were first supported in which upstream versions.
[libguestfs.git] / src / appliance.c
index 3c3279b..7dc42c1 100644 (file)
@@ -18,6 +18,8 @@
 
 #include <config.h>
 
+#include <errno.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -27,6 +29,7 @@
 #include <time.h>
 #include <sys/stat.h>
 #include <sys/select.h>
+#include <sys/wait.h>
 #include <utime.h>
 
 #ifdef HAVE_SYS_TYPES_H
@@ -38,8 +41,8 @@
 #include "guestfs-internal-actions.h"
 #include "guestfs_protocol.h"
 
-static const char *kernel_name = "vmlinuz." REPO "." host_cpu;
-static const char *initrd_name = "initramfs." REPO "." host_cpu ".img";
+static const char *kernel_name = "vmlinuz." host_cpu;
+static const char *initrd_name = "initramfs." host_cpu ".img";
 
 static int find_path (guestfs_h *g, int (*pred) (guestfs_h *g, const char *pelem, void *data), void *data, char **pelem);
 static int dir_contains_file (const char *dir, const char *file);
@@ -49,6 +52,7 @@ static int contains_ordinary_appliance (guestfs_h *g, const char *path, void *da
 static char *calculate_supermin_checksum (guestfs_h *g, const char *supermin_path);
 static int check_for_cached_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
 static int build_supermin_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
+static int run_supermin_helper (guestfs_h *g, const char *supermin_path, const char *cachedir, size_t cdlen);
 
 /* Locate or build the appliance.
  *
@@ -157,15 +161,27 @@ calculate_supermin_checksum (guestfs_h *g, const char *supermin_path)
 {
   size_t len = 2 * strlen (supermin_path) + 256;
   char cmd[len];
-  snprintf (cmd, len,
-            "febootstrap-supermin-helper%s "
-            "-f checksum "
-            "-k '%s/kmod.whitelist' "
-            "'%s/supermin.d' "
-            host_cpu,
-            g->verbose ? " --verbose" : "",
-            supermin_path,
-            supermin_path);
+  int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
+
+  if (!pass_u_g_args)
+    snprintf (cmd, len,
+              "febootstrap-supermin-helper%s "
+              "-f checksum "
+              "'%s/supermin.d' "
+              host_cpu,
+              g->verbose ? " --verbose" : "",
+              supermin_path);
+  else
+    snprintf (cmd, len,
+              "febootstrap-supermin-helper%s "
+              "-u %i "
+              "-g %i "
+              "-f checksum "
+              "'%s/supermin.d' "
+              host_cpu,
+              g->verbose ? " --verbose" : "",
+              geteuid (), getegid (),
+              supermin_path);
 
   if (g->verbose)
     guestfs___print_timestamped_message (g, "%s", cmd);
@@ -233,11 +249,11 @@ check_for_cached_appliance (guestfs_h *g,
                             const char *supermin_path, const char *checksum,
                             char **kernel, char **initrd, char **appliance)
 {
-  const char *tmpdir = guestfs___tmpdir ();
+  const char *tmpdir = guestfs_tmpdir ();
 
-  size_t len = strlen (tmpdir) + strlen (checksum) + 2;
+  size_t len = strlen (tmpdir) + strlen (checksum) + 10;
   char cachedir[len];
-  snprintf (cachedir, len, "%s/%s", tmpdir, checksum);
+  snprintf (cachedir, len, "%s/guestfs.%s", tmpdir, checksum);
 
   /* Touch the directory to prevent it being deleting in a rare race
    * between us doing the checks and a tmp cleaner running.  Note this
@@ -327,45 +343,106 @@ build_supermin_appliance (guestfs_h *g,
   if (g->verbose)
     guestfs___print_timestamped_message (g, "begin building supermin appliance");
 
-  const char *tmpdir = guestfs___tmpdir ();
-  size_t cdlen = strlen (tmpdir) + strlen (checksum) + 2;
-  char cachedir[cdlen];
-  snprintf (cachedir, cdlen, "%s/%s", tmpdir, checksum);
+  const char *tmpdir = guestfs_tmpdir ();
 
-  /* Don't worry about this failing, because the command below will
-   * fail if the directory doesn't exist.  Note the directory might
-   * already exist, eg. if a tmp cleaner has removed the existing
-   * appliance but not the directory itself.
-   */
-  (void) mkdir (cachedir, 0755);
+  size_t tmpcdlen = strlen (tmpdir) + 16;
+  char tmpcd[tmpcdlen];
+  snprintf (tmpcd, tmpcdlen, "%s/guestfs.XXXXXX", tmpdir);
 
-  /* Set a sensible umask in the subprocess, so kernel and initrd
-   * output files are world-readable (RHBZ#610880).
-   */
-  size_t cmdlen = 2 * strlen (supermin_path) + 3 * (cdlen + 16) + 256;
-  char cmd[cmdlen];
-  snprintf (cmd, cmdlen,
-            "umask 0022; "
-            "febootstrap-supermin-helper%s "
-            "-f ext2 "
-            "-k '%s/kmod.whitelist' "
-            "'%s/supermin.d' "
-            host_cpu " "
-            "%s/kernel %s/initrd %s/root",
-            g->verbose ? " --verbose" : "",
-            supermin_path, supermin_path,
-            cachedir, cachedir, cachedir);
-  if (g->verbose)
-    guestfs___print_timestamped_message (g, "%s", cmd);
-  int r = system (cmd);
-  if (r == -1 || WEXITSTATUS (r) != 0) {
-    error (g, _("external command failed: %s"), cmd);
+  if (NULL == mkdtemp (tmpcd)) {
+    error (g, _("failed to create temporary cache directory: %m"));
     return -1;
   }
 
   if (g->verbose)
+    guestfs___print_timestamped_message (g, "run febootstrap-supermin-helper");
+
+  int r = run_supermin_helper (g, supermin_path, tmpcd, tmpcdlen);
+  if (r == -1)
+    return -1;
+
+  if (g->verbose)
     guestfs___print_timestamped_message (g, "finished building supermin appliance");
 
+  size_t cdlen = strlen (tmpdir) + strlen (checksum) + 10;
+  char cachedir[cdlen];
+  snprintf (cachedir, cdlen, "%s/guestfs.%s", tmpdir, checksum);
+
+  /* Make the temporary directory world readable */
+  if (chmod (tmpcd, 0755) == -1) {
+    error (g, "chmod %s: %m", tmpcd);
+  }
+
+  /* Try to rename the temporary directory to its non-temporary name */
+  if (rename (tmpcd, cachedir) == -1) {
+    /* If the cache directory now exists, we may have been racing with another
+     * libguestfs process. Check the new directory and use it if it's valid. */
+    if (errno == ENOTEMPTY || errno == EEXIST) {
+      /* Appliance cache consists of 2 files and a symlink in the cache
+       * directory. Delete them first. */
+      DIR *dir = opendir (tmpcd);
+      if (dir == NULL) {
+        error (g, "opendir %s: %m", tmpcd);
+        return -1;
+      }
+
+      int fd = dirfd (dir);
+      if (fd == -1) {
+        error (g, "dirfd: %m");
+        closedir (dir);
+        return -1;
+      }
+
+      struct dirent *dirent;
+      for (;;) {
+        errno = 0;
+        dirent = readdir (dir);
+
+        if (dirent == NULL) {
+          break;
+        }
+
+        /* Check that dirent is a file so we don't try to delete . and .. */
+        struct stat st;
+        if (fstatat (fd, dirent->d_name, &st, AT_SYMLINK_NOFOLLOW) == -1) {
+          error (g, "fstatat %s: %m", dirent->d_name);
+          return -1;
+        }
+
+        if (!S_ISDIR(st.st_mode)) {
+          if (unlinkat (fd, dirent->d_name, 0) == -1) {
+            error (g, "unlinkat %s: %m", dirent->d_name);
+            closedir (dir);
+            return -1;
+          }
+        }
+      }
+
+      if (errno != 0) {
+        error (g, "readdir %s: %m", tmpcd);
+        closedir (dir);
+        return -1;
+      }
+
+      closedir (dir);
+
+      /* Delete the temporary cache directory itself. */
+      if (rmdir (tmpcd) == -1) {
+        error (g, "rmdir %s: %m", tmpcd);
+        return -1;
+      }
+
+      /* Check the new cache directory, and return it if valid */
+      return check_for_cached_appliance (g, supermin_path, checksum,
+                                         kernel, initrd, appliance);
+    }
+
+    else {
+      error (g, _("error renaming temporary cache directory: %m"));
+      return -1;
+    }
+  }
+
   *kernel = safe_malloc (g, cdlen + 8 /* / + "kernel" + \0 */);
   *initrd = safe_malloc (g, cdlen + 8 /* / + "initrd" + \0 */);
   *appliance = safe_malloc (g, cdlen + 6 /* / + "root" + \0 */);
@@ -376,6 +453,85 @@ build_supermin_appliance (guestfs_h *g,
   return 0;
 }
 
+/* Run febootstrap-supermin-helper and tell it to generate the
+ * appliance.
+ */
+static int
+run_supermin_helper (guestfs_h *g, const char *supermin_path,
+                     const char *cachedir, size_t cdlen)
+{
+  size_t pathlen = strlen (supermin_path);
+
+  const char *argv[30];
+  size_t i = 0;
+
+  char uid[32];
+  snprintf (uid, sizeof uid, "%i", geteuid ());
+  char gid[32];
+  snprintf (gid, sizeof gid, "%i", getegid ());
+  char supermin_d[pathlen + 32];
+  snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
+  char kernel[cdlen + 32];
+  snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
+  char initrd[cdlen + 32];
+  snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
+  char root[cdlen + 32];
+  snprintf (root, cdlen + 32, "%s/root", cachedir);
+
+  int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
+
+  argv[i++] = "febootstrap-supermin-helper";
+  if (g->verbose)
+    argv[i++] = "--verbose";
+  if (pass_u_g_args) {
+    argv[i++] = "-u";
+    argv[i++] = uid;
+    argv[i++] = "-g";
+    argv[i++] = gid;
+  }
+  argv[i++] = "-f";
+  argv[i++] = "ext2";
+  argv[i++] = supermin_d;
+  argv[i++] = host_cpu;
+  argv[i++] = kernel;
+  argv[i++] = initrd;
+  argv[i++] = root;
+  argv[i++] = NULL;
+
+  pid_t pid = fork ();
+  if (pid == -1) {
+    perrorf (g, "fork");
+    return -1;
+  }
+
+  if (pid > 0) {                /* Parent. */
+    if (g->verbose)
+      guestfs___print_timestamped_argv (g, argv);
+
+    int status;
+    if (waitpid (pid, &status, 0) == -1) {
+      perrorf (g, "waitpid");
+      return -1;
+    }
+    if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
+      error (g, _("external command failed, see earlier error messages"));
+      return -1;
+    }
+    return 0;
+  }
+
+  /* Child. */
+
+  /* Set a sensible umask in the subprocess, so kernel and initrd
+   * output files are world-readable (RHBZ#610880).
+   */
+  umask (0022);
+
+  execvp ("febootstrap-supermin-helper", (char * const *) argv);
+  perror ("execvp");
+  _exit (EXIT_FAILURE);
+}
+
 /* Search elements of g->path, returning the first path element which
  * matches the predicate function 'pred'.
  *