+/* Run febootstrap-supermin-helper and tell it to generate the
+ * appliance. Note that we have to do an explicit fork/exec here.
+ * 'system' goes via the shell, and on systems that have bash, bash
+ * has a misfeature where it resets the euid to uid which breaks
+ * virt-v2v. 'posix_spawn' was also considered but that doesn't allow
+ * us to reset the umask.
+ */
+static int
+run_supermin_helper (guestfs_h *g, const char *supermin_path,
+ const char *cachedir, size_t cdlen)
+{
+ pid_t pid = fork ();
+ if (pid == -1) {
+ perrorf (g, "fork");
+ return -1;
+ }
+
+ if (pid > 0) { /* Parent. */
+ 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);
+
+ /* Set uid/gid in the child. This is a workaround for a misfeature
+ * in bash which breaks virt-v2v - see the comment at the top of
+ * this function.
+ */
+ if (getuid () == 0) {
+ int egid = getegid ();
+ int euid = geteuid ();
+
+ if (egid != 0 || euid != 0) {
+ if (seteuid (0) == -1) {
+ perror ("seteuid");
+ _exit (EXIT_FAILURE);
+ }
+
+ if (setgid (egid) == -1) {
+ perror ("setgid");
+ _exit (EXIT_FAILURE);
+ }
+
+ if (setuid (euid) == -1) {
+ perror ("setuid");
+ _exit (EXIT_FAILURE);
+ }
+ }
+ }
+
+ size_t pathlen = strlen (supermin_path);
+
+ const char *argv[30];
+ size_t i = 0;
+
+ argv[i++] = "febootstrap-supermin-helper";
+ if (g->verbose)
+ argv[i++] = "--verbose";
+ argv[i++] = "-f";
+ argv[i++] = "ext2";
+ argv[i++] = "-k";
+ char whitelist[pathlen + 32];
+ snprintf (whitelist, pathlen + 32, "%s/kmod.whitelist", supermin_path);
+ argv[i++] = whitelist;
+ char supermin_d[pathlen + 32];
+ snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
+ argv[i++] = supermin_d;
+ argv[i++] = host_cpu;
+ char kernel[cdlen + 32];
+ snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
+ argv[i++] = kernel;
+ char initrd[cdlen + 32];
+ snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
+ argv[i++] = initrd;
+ char root[cdlen + 32];
+ snprintf (root, cdlen + 32, "%s/root", cachedir);
+ argv[i++] = root;
+ argv[i++] = NULL;
+
+ execvp ("febootstrap-supermin-helper", (char * const *) argv);
+ perror ("execvp");
+ _exit (EXIT_FAILURE);
+}
+