+ return 0;
+}
+
+static int read_all (guestfs_h *g, FILE *fp, char **ret);
+
+/* Test qemu binary (or wrapper) runs, and do 'qemu -help' and
+ * 'qemu -version' so we know what options this qemu supports and
+ * the version.
+ */
+static int
+test_qemu (guestfs_h *g)
+{
+ char cmd[1024];
+ FILE *fp;
+
+ free (g->qemu_help);
+ free (g->qemu_version);
+ g->qemu_help = NULL;
+ g->qemu_version = NULL;
+
+ snprintf (cmd, sizeof cmd, "'%s' -help", g->qemu);
+
+ fp = popen (cmd, "r");
+ /* qemu -help should always work (qemu -version OTOH wasn't
+ * supported by qemu 0.9). If this command doesn't work then it
+ * probably indicates that the qemu binary is missing.
+ */
+ if (!fp) {
+ /* XXX This error is never printed, even if the qemu binary
+ * doesn't exist. Why?
+ */
+ error:
+ perrorf (g, _("%s: command failed: If qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU environment variable."), cmd);
+ return -1;
+ }
+
+ if (read_all (g, fp, &g->qemu_help) == -1)
+ goto error;
+
+ if (pclose (fp) == -1)
+ goto error;
+
+ snprintf (cmd, sizeof cmd, "'%s' -version 2>/dev/null", g->qemu);
+
+ fp = popen (cmd, "r");
+ if (fp) {
+ /* Intentionally ignore errors. */
+ read_all (g, fp, &g->qemu_version);
+ pclose (fp);
+ }
+
+ return 0;
+}
+
+static int
+read_all (guestfs_h *g, FILE *fp, char **ret)
+{
+ int r, n = 0;
+ char *p;
+
+ again:
+ if (feof (fp)) {
+ *ret = safe_realloc (g, *ret, n + 1);
+ (*ret)[n] = '\0';
+ return n;
+ }
+
+ *ret = safe_realloc (g, *ret, n + BUFSIZ);
+ p = &(*ret)[n];
+ r = fread (p, 1, BUFSIZ, fp);
+ if (ferror (fp)) {
+ perrorf (g, "read");
+ return -1;
+ }
+ n += r;
+ goto again;
+}
+
+/* Test if option is supported by qemu command line (just by grepping
+ * the help text).
+ */
+static int
+qemu_supports (guestfs_h *g, const char *option)
+{
+ return g->qemu_help && strstr (g->qemu_help, option) != NULL;
+}
+
+static void
+finish_wait_ready (guestfs_h *g, void *vp)
+{
+ if (g->verbose)
+ fprintf (stderr, "finish_wait_ready called, %p, vp = %p\n", g, vp);
+
+ *((int *)vp) = 1;
+ g->main_loop->main_loop_quit (g->main_loop, g);
+}
+
+int
+guestfs_wait_ready (guestfs_h *g)
+{
+ int finished = 0, r;
+
+ if (g->state == READY) return 0;
+
+ if (g->state == BUSY) {
+ error (g, _("qemu has finished launching already"));
+ return -1;
+ }
+
+ if (g->state != LAUNCHING) {
+ error (g, _("qemu has not been launched yet"));
+ return -1;
+ }
+
+ g->launch_done_cb = finish_wait_ready;
+ g->launch_done_cb_data = &finished;
+ r = g->main_loop->main_loop_run (g->main_loop, g);
+ g->launch_done_cb = NULL;
+ g->launch_done_cb_data = NULL;
+
+ if (r == -1) return -1;
+
+ if (finished != 1) {
+ error (g, _("guestfs_wait_ready failed, see earlier error messages"));
+ return -1;
+ }
+
+ /* This is possible in some really strange situations, such as
+ * guestfsd starts up OK but then qemu immediately exits. Check for
+ * it because the caller is probably expecting to be able to send
+ * commands after this function returns.
+ */
+ if (g->state != READY) {
+ error (g, _("qemu launched and contacted daemon, but state != READY"));
+ return -1;
+ }