Generated code for ping-daemon command.
[libguestfs.git] / src / guestfs-actions.c
index b340bb7..c0762df 100644 (file)
@@ -86,27 +86,26 @@ check_state (guestfs_h *g, const char *caller)
 struct mount_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void mount_send_cb (guestfs_h *g, void *data)
+static void mount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct mount_ctx *ctx = (struct mount_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mount");
+    return;
+  }
 
-static void mount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct mount_ctx *ctx = (struct mount_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_mount");
@@ -120,8 +119,7 @@ static void mount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_mount (guestfs_h *g,
@@ -134,22 +132,16 @@ int guestfs_mount (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_mount") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
   args.mountpoint = (char *) mountpoint;
-  serial = guestfs__send (g, GUESTFS_PROC_MOUNT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MOUNT,
         (xdrproc_t) xdr_guestfs_mount_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, mount_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_mount");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -158,46 +150,50 @@ int guestfs_mount (guestfs_h *g,
   guestfs_set_reply_callback (g, mount_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_mount");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct sync_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void sync_send_cb (guestfs_h *g, void *data)
+static void sync_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct sync_ctx *ctx = (struct sync_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_sync");
+    return;
+  }
 
-static void sync_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct sync_ctx *ctx = (struct sync_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_sync");
@@ -211,8 +207,7 @@ static void sync_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_sync (guestfs_h *g)
@@ -222,19 +217,13 @@ int guestfs_sync (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_sync") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_SYNC, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, sync_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_sync");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_SYNC, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -243,46 +232,50 @@ int guestfs_sync (guestfs_h *g)
   guestfs_set_reply_callback (g, sync_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_sync");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SYNC, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SYNC, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct touch_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void touch_send_cb (guestfs_h *g, void *data)
+static void touch_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct touch_ctx *ctx = (struct touch_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_touch");
+    return;
+  }
 
-static void touch_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct touch_ctx *ctx = (struct touch_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_touch");
@@ -296,8 +289,7 @@ static void touch_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_touch (guestfs_h *g,
@@ -309,21 +301,15 @@ int guestfs_touch (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_touch") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_TOUCH,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TOUCH,
         (xdrproc_t) xdr_guestfs_touch_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, touch_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_touch");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -332,27 +318,31 @@ int guestfs_touch (guestfs_h *g,
   guestfs_set_reply_callback (g, touch_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_touch");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TOUCH, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TOUCH, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct cat_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -360,19 +350,19 @@ struct cat_ctx {
   struct guestfs_cat_ret ret;
 };
 
-static void cat_send_cb (guestfs_h *g, void *data)
+static void cat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct cat_ctx *ctx = (struct cat_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_cat");
+    return;
+  }
 
-static void cat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct cat_ctx *ctx = (struct cat_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_cat");
@@ -390,8 +380,7 @@ static void cat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char *guestfs_cat (guestfs_h *g,
@@ -403,21 +392,15 @@ char *guestfs_cat (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_cat") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_CAT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CAT,
         (xdrproc_t) xdr_guestfs_cat_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, cat_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_cat");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -426,27 +409,31 @@ char *guestfs_cat (guestfs_h *g,
   guestfs_set_reply_callback (g, cat_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_cat");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CAT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CAT, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.content; /* caller will free */
 }
 
 struct ll_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -454,19 +441,19 @@ struct ll_ctx {
   struct guestfs_ll_ret ret;
 };
 
-static void ll_send_cb (guestfs_h *g, void *data)
+static void ll_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct ll_ctx *ctx = (struct ll_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_ll");
+    return;
+  }
 
-static void ll_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct ll_ctx *ctx = (struct ll_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_ll");
@@ -484,8 +471,7 @@ static void ll_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char *guestfs_ll (guestfs_h *g,
@@ -497,21 +483,15 @@ char *guestfs_ll (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_ll") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.directory = (char *) directory;
-  serial = guestfs__send (g, GUESTFS_PROC_LL,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LL,
         (xdrproc_t) xdr_guestfs_ll_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, ll_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_ll");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -520,27 +500,31 @@ char *guestfs_ll (guestfs_h *g,
   guestfs_set_reply_callback (g, ll_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_ll");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LL, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.listing; /* caller will free */
 }
 
 struct ls_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -548,19 +532,19 @@ struct ls_ctx {
   struct guestfs_ls_ret ret;
 };
 
-static void ls_send_cb (guestfs_h *g, void *data)
+static void ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct ls_ctx *ctx = (struct ls_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_ls");
+    return;
+  }
 
-static void ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct ls_ctx *ctx = (struct ls_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_ls");
@@ -578,8 +562,7 @@ static void ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_ls (guestfs_h *g,
@@ -591,21 +574,15 @@ char **guestfs_ls (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_ls") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.directory = (char *) directory;
-  serial = guestfs__send (g, GUESTFS_PROC_LS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LS,
         (xdrproc_t) xdr_guestfs_ls_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, ls_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_ls");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -614,19 +591,24 @@ char **guestfs_ls (guestfs_h *g,
   guestfs_set_reply_callback (g, ls_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_ls");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.listing.listing_val =
     safe_realloc (g, ctx.ret.listing.listing_val,
@@ -638,8 +620,7 @@ char **guestfs_ls (guestfs_h *g,
 struct list_devices_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -647,19 +628,19 @@ struct list_devices_ctx {
   struct guestfs_list_devices_ret ret;
 };
 
-static void list_devices_send_cb (guestfs_h *g, void *data)
+static void list_devices_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct list_devices_ctx *ctx = (struct list_devices_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_list_devices");
+    return;
+  }
 
-static void list_devices_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct list_devices_ctx *ctx = (struct list_devices_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_list_devices");
@@ -677,8 +658,7 @@ static void list_devices_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_list_devices (guestfs_h *g)
@@ -688,19 +668,13 @@ char **guestfs_list_devices (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_list_devices") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_LIST_DEVICES, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, list_devices_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_list_devices");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LIST_DEVICES, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -709,19 +683,24 @@ char **guestfs_list_devices (guestfs_h *g)
   guestfs_set_reply_callback (g, list_devices_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_list_devices");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LIST_DEVICES, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LIST_DEVICES, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.devices.devices_val =
     safe_realloc (g, ctx.ret.devices.devices_val,
@@ -733,8 +712,7 @@ char **guestfs_list_devices (guestfs_h *g)
 struct list_partitions_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -742,19 +720,19 @@ struct list_partitions_ctx {
   struct guestfs_list_partitions_ret ret;
 };
 
-static void list_partitions_send_cb (guestfs_h *g, void *data)
+static void list_partitions_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct list_partitions_ctx *ctx = (struct list_partitions_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_list_partitions");
+    return;
+  }
 
-static void list_partitions_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct list_partitions_ctx *ctx = (struct list_partitions_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_list_partitions");
@@ -772,8 +750,7 @@ static void list_partitions_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_list_partitions (guestfs_h *g)
@@ -783,19 +760,13 @@ char **guestfs_list_partitions (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_list_partitions") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_LIST_PARTITIONS, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, list_partitions_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_list_partitions");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LIST_PARTITIONS, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -804,19 +775,24 @@ char **guestfs_list_partitions (guestfs_h *g)
   guestfs_set_reply_callback (g, list_partitions_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_list_partitions");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LIST_PARTITIONS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LIST_PARTITIONS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.partitions.partitions_val =
     safe_realloc (g, ctx.ret.partitions.partitions_val,
@@ -828,8 +804,7 @@ char **guestfs_list_partitions (guestfs_h *g)
 struct pvs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -837,19 +812,19 @@ struct pvs_ctx {
   struct guestfs_pvs_ret ret;
 };
 
-static void pvs_send_cb (guestfs_h *g, void *data)
+static void pvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct pvs_ctx *ctx = (struct pvs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_pvs");
+    return;
+  }
 
-static void pvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct pvs_ctx *ctx = (struct pvs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_pvs");
@@ -867,8 +842,7 @@ static void pvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_pvs (guestfs_h *g)
@@ -878,19 +852,13 @@ char **guestfs_pvs (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_pvs") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_PVS, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, pvs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_pvs");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_PVS, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -899,19 +867,24 @@ char **guestfs_pvs (guestfs_h *g)
   guestfs_set_reply_callback (g, pvs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_pvs");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.physvols.physvols_val =
     safe_realloc (g, ctx.ret.physvols.physvols_val,
@@ -923,8 +896,7 @@ char **guestfs_pvs (guestfs_h *g)
 struct vgs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -932,19 +904,19 @@ struct vgs_ctx {
   struct guestfs_vgs_ret ret;
 };
 
-static void vgs_send_cb (guestfs_h *g, void *data)
+static void vgs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct vgs_ctx *ctx = (struct vgs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_vgs");
+    return;
+  }
 
-static void vgs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct vgs_ctx *ctx = (struct vgs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_vgs");
@@ -962,8 +934,7 @@ static void vgs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_vgs (guestfs_h *g)
@@ -973,19 +944,13 @@ char **guestfs_vgs (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_vgs") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_VGS, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, vgs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_vgs");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_VGS, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -994,19 +959,24 @@ char **guestfs_vgs (guestfs_h *g)
   guestfs_set_reply_callback (g, vgs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_vgs");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.volgroups.volgroups_val =
     safe_realloc (g, ctx.ret.volgroups.volgroups_val,
@@ -1018,8 +988,7 @@ char **guestfs_vgs (guestfs_h *g)
 struct lvs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1027,19 +996,19 @@ struct lvs_ctx {
   struct guestfs_lvs_ret ret;
 };
 
-static void lvs_send_cb (guestfs_h *g, void *data)
+static void lvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct lvs_ctx *ctx = (struct lvs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lvs");
+    return;
+  }
 
-static void lvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct lvs_ctx *ctx = (struct lvs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_lvs");
@@ -1057,8 +1026,7 @@ static void lvs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_lvs (guestfs_h *g)
@@ -1068,19 +1036,13 @@ char **guestfs_lvs (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_lvs") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_LVS, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, lvs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_lvs");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LVS, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1089,19 +1051,24 @@ char **guestfs_lvs (guestfs_h *g)
   guestfs_set_reply_callback (g, lvs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_lvs");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.logvols.logvols_val =
     safe_realloc (g, ctx.ret.logvols.logvols_val,
@@ -1113,8 +1080,7 @@ char **guestfs_lvs (guestfs_h *g)
 struct pvs_full_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1122,19 +1088,19 @@ struct pvs_full_ctx {
   struct guestfs_pvs_full_ret ret;
 };
 
-static void pvs_full_send_cb (guestfs_h *g, void *data)
+static void pvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct pvs_full_ctx *ctx = (struct pvs_full_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_pvs_full");
+    return;
+  }
 
-static void pvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct pvs_full_ctx *ctx = (struct pvs_full_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_pvs_full");
@@ -1152,8 +1118,7 @@ static void pvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_lvm_pv_list *guestfs_pvs_full (guestfs_h *g)
@@ -1163,19 +1128,13 @@ struct guestfs_lvm_pv_list *guestfs_pvs_full (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_pvs_full") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_PVS_FULL, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, pvs_full_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_pvs_full");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_PVS_FULL, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1184,19 +1143,24 @@ struct guestfs_lvm_pv_list *guestfs_pvs_full (guestfs_h *g)
   guestfs_set_reply_callback (g, pvs_full_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_pvs_full");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVS_FULL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVS_FULL, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.physvols, sizeof (ctx.ret.physvols));
 }
@@ -1204,8 +1168,7 @@ struct guestfs_lvm_pv_list *guestfs_pvs_full (guestfs_h *g)
 struct vgs_full_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1213,19 +1176,19 @@ struct vgs_full_ctx {
   struct guestfs_vgs_full_ret ret;
 };
 
-static void vgs_full_send_cb (guestfs_h *g, void *data)
+static void vgs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct vgs_full_ctx *ctx = (struct vgs_full_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_vgs_full");
+    return;
+  }
 
-static void vgs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct vgs_full_ctx *ctx = (struct vgs_full_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_vgs_full");
@@ -1243,8 +1206,7 @@ static void vgs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_lvm_vg_list *guestfs_vgs_full (guestfs_h *g)
@@ -1254,40 +1216,39 @@ struct guestfs_lvm_vg_list *guestfs_vgs_full (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_vgs_full") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_VGS_FULL, NULL, NULL);
-  if (serial == -1)
+  serial = guestfs__send_sync (g, GUESTFS_PROC_VGS_FULL, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
+  guestfs__switch_to_receiving (g);
   ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, vgs_full_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_vgs_full");
-    return NULL;
-  }
-
-  guestfs__switch_to_receiving (g);
-  ctx.cb_sequence = 0;
-  guestfs_set_reply_callback (g, vgs_full_reply_cb, &ctx);
+  guestfs_set_reply_callback (g, vgs_full_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_vgs_full");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGS_FULL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGS_FULL, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.volgroups, sizeof (ctx.ret.volgroups));
 }
@@ -1295,8 +1256,7 @@ struct guestfs_lvm_vg_list *guestfs_vgs_full (guestfs_h *g)
 struct lvs_full_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1304,19 +1264,19 @@ struct lvs_full_ctx {
   struct guestfs_lvs_full_ret ret;
 };
 
-static void lvs_full_send_cb (guestfs_h *g, void *data)
+static void lvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct lvs_full_ctx *ctx = (struct lvs_full_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lvs_full");
+    return;
+  }
 
-static void lvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct lvs_full_ctx *ctx = (struct lvs_full_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_lvs_full");
@@ -1334,8 +1294,7 @@ static void lvs_full_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_lvm_lv_list *guestfs_lvs_full (guestfs_h *g)
@@ -1345,19 +1304,13 @@ struct guestfs_lvm_lv_list *guestfs_lvs_full (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_lvs_full") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_LVS_FULL, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, lvs_full_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_lvs_full");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LVS_FULL, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1366,19 +1319,24 @@ struct guestfs_lvm_lv_list *guestfs_lvs_full (guestfs_h *g)
   guestfs_set_reply_callback (g, lvs_full_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_lvs_full");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVS_FULL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVS_FULL, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.logvols, sizeof (ctx.ret.logvols));
 }
@@ -1386,8 +1344,7 @@ struct guestfs_lvm_lv_list *guestfs_lvs_full (guestfs_h *g)
 struct read_lines_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1395,19 +1352,19 @@ struct read_lines_ctx {
   struct guestfs_read_lines_ret ret;
 };
 
-static void read_lines_send_cb (guestfs_h *g, void *data)
+static void read_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct read_lines_ctx *ctx = (struct read_lines_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_read_lines");
+    return;
+  }
 
-static void read_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct read_lines_ctx *ctx = (struct read_lines_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_read_lines");
@@ -1425,8 +1382,7 @@ static void read_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_read_lines (guestfs_h *g,
@@ -1438,21 +1394,15 @@ char **guestfs_read_lines (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_read_lines") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_READ_LINES,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_READ_LINES,
         (xdrproc_t) xdr_guestfs_read_lines_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, read_lines_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_read_lines");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1461,19 +1411,24 @@ char **guestfs_read_lines (guestfs_h *g,
   guestfs_set_reply_callback (g, read_lines_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_read_lines");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_READ_LINES, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_READ_LINES, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.lines.lines_val =
     safe_realloc (g, ctx.ret.lines.lines_val,
@@ -1485,27 +1440,26 @@ char **guestfs_read_lines (guestfs_h *g,
 struct aug_init_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_init_send_cb (guestfs_h *g, void *data)
+static void aug_init_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_init_ctx *ctx = (struct aug_init_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_init");
+    return;
+  }
 
-static void aug_init_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_init_ctx *ctx = (struct aug_init_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_init");
@@ -1519,8 +1473,7 @@ static void aug_init_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_init (guestfs_h *g,
@@ -1533,22 +1486,16 @@ int guestfs_aug_init (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_init") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.root = (char *) root;
   args.flags = flags;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_INIT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_INIT,
         (xdrproc_t) xdr_guestfs_aug_init_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_init_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_init");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -1557,46 +1504,50 @@ int guestfs_aug_init (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_init_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_init");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_INIT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_INIT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_close_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_close_send_cb (guestfs_h *g, void *data)
+static void aug_close_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_close_ctx *ctx = (struct aug_close_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_close");
+    return;
+  }
 
-static void aug_close_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_close_ctx *ctx = (struct aug_close_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_close");
@@ -1610,8 +1561,7 @@ static void aug_close_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_close (guestfs_h *g)
@@ -1621,19 +1571,13 @@ int guestfs_aug_close (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_aug_close") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_CLOSE, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_close_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_close");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_CLOSE, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -1642,27 +1586,31 @@ int guestfs_aug_close (guestfs_h *g)
   guestfs_set_reply_callback (g, aug_close_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_close");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_CLOSE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_CLOSE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_defvar_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1670,19 +1618,19 @@ struct aug_defvar_ctx {
   struct guestfs_aug_defvar_ret ret;
 };
 
-static void aug_defvar_send_cb (guestfs_h *g, void *data)
+static void aug_defvar_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_defvar_ctx *ctx = (struct aug_defvar_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_defvar");
+    return;
+  }
 
-static void aug_defvar_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_defvar_ctx *ctx = (struct aug_defvar_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_defvar");
@@ -1700,8 +1648,7 @@ static void aug_defvar_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_defvar (guestfs_h *g,
@@ -1714,22 +1661,16 @@ int guestfs_aug_defvar (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_defvar") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.name = (char *) name;
   args.expr = expr ? (char **) &expr : NULL;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_DEFVAR,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_DEFVAR,
         (xdrproc_t) xdr_guestfs_aug_defvar_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_defvar_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_defvar");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -1738,27 +1679,31 @@ int guestfs_aug_defvar (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_defvar_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_defvar");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_DEFVAR, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_DEFVAR, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.nrnodes;
 }
 
 struct aug_defnode_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1766,19 +1711,19 @@ struct aug_defnode_ctx {
   struct guestfs_aug_defnode_ret ret;
 };
 
-static void aug_defnode_send_cb (guestfs_h *g, void *data)
+static void aug_defnode_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_defnode_ctx *ctx = (struct aug_defnode_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_defnode");
+    return;
+  }
 
-static void aug_defnode_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_defnode_ctx *ctx = (struct aug_defnode_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_defnode");
@@ -1796,8 +1741,7 @@ static void aug_defnode_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_int_bool *guestfs_aug_defnode (guestfs_h *g,
@@ -1811,23 +1755,17 @@ struct guestfs_int_bool *guestfs_aug_defnode (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_defnode") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.name = (char *) name;
   args.expr = (char *) expr;
   args.val = (char *) val;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_DEFNODE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_DEFNODE,
         (xdrproc_t) xdr_guestfs_aug_defnode_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_defnode_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_defnode");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1836,19 +1774,24 @@ struct guestfs_int_bool *guestfs_aug_defnode (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_defnode_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_defnode");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_DEFNODE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_DEFNODE, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller with free this */
   return safe_memdup (g, &ctx.ret, sizeof (ctx.ret));
 }
@@ -1856,8 +1799,7 @@ struct guestfs_int_bool *guestfs_aug_defnode (guestfs_h *g,
 struct aug_get_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -1865,19 +1807,19 @@ struct aug_get_ctx {
   struct guestfs_aug_get_ret ret;
 };
 
-static void aug_get_send_cb (guestfs_h *g, void *data)
+static void aug_get_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_get_ctx *ctx = (struct aug_get_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_get");
+    return;
+  }
 
-static void aug_get_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_get_ctx *ctx = (struct aug_get_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_get");
@@ -1895,8 +1837,7 @@ static void aug_get_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char *guestfs_aug_get (guestfs_h *g,
@@ -1908,21 +1849,15 @@ char *guestfs_aug_get (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_get") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_GET,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_GET,
         (xdrproc_t) xdr_guestfs_aug_get_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_get_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_get");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -1931,46 +1866,50 @@ char *guestfs_aug_get (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_get_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_get");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_GET, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_GET, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.val; /* caller will free */
 }
 
 struct aug_set_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_set_send_cb (guestfs_h *g, void *data)
+static void aug_set_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_set_ctx *ctx = (struct aug_set_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_set");
+    return;
+  }
 
-static void aug_set_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_set_ctx *ctx = (struct aug_set_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_set");
@@ -1984,8 +1923,7 @@ static void aug_set_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_set (guestfs_h *g,
@@ -1998,22 +1936,16 @@ int guestfs_aug_set (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_set") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
   args.val = (char *) val;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_SET,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_SET,
         (xdrproc_t) xdr_guestfs_aug_set_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_set_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_set");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2022,46 +1954,50 @@ int guestfs_aug_set (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_set_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_set");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_SET, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_SET, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_insert_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_insert_send_cb (guestfs_h *g, void *data)
+static void aug_insert_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_insert_ctx *ctx = (struct aug_insert_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_insert");
+    return;
+  }
 
-static void aug_insert_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_insert_ctx *ctx = (struct aug_insert_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_insert");
@@ -2075,8 +2011,7 @@ static void aug_insert_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_insert (guestfs_h *g,
@@ -2090,23 +2025,17 @@ int guestfs_aug_insert (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_insert") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
   args.label = (char *) label;
   args.before = before;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_INSERT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_INSERT,
         (xdrproc_t) xdr_guestfs_aug_insert_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_insert_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_insert");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2115,27 +2044,31 @@ int guestfs_aug_insert (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_insert_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_insert");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_INSERT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_INSERT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_rm_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -2143,19 +2076,19 @@ struct aug_rm_ctx {
   struct guestfs_aug_rm_ret ret;
 };
 
-static void aug_rm_send_cb (guestfs_h *g, void *data)
+static void aug_rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_rm_ctx *ctx = (struct aug_rm_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_rm");
+    return;
+  }
 
-static void aug_rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_rm_ctx *ctx = (struct aug_rm_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_rm");
@@ -2173,8 +2106,7 @@ static void aug_rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_rm (guestfs_h *g,
@@ -2186,21 +2118,15 @@ int guestfs_aug_rm (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_rm") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_RM,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_RM,
         (xdrproc_t) xdr_guestfs_aug_rm_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_rm_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_rm");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2209,46 +2135,50 @@ int guestfs_aug_rm (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_rm_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_rm");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_RM, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_RM, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.nrnodes;
 }
 
 struct aug_mv_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_mv_send_cb (guestfs_h *g, void *data)
+static void aug_mv_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_mv_ctx *ctx = (struct aug_mv_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_mv");
+    return;
+  }
 
-static void aug_mv_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_mv_ctx *ctx = (struct aug_mv_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_mv");
@@ -2262,8 +2192,7 @@ static void aug_mv_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_mv (guestfs_h *g,
@@ -2276,22 +2205,16 @@ int guestfs_aug_mv (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_mv") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.src = (char *) src;
   args.dest = (char *) dest;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_MV,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_MV,
         (xdrproc_t) xdr_guestfs_aug_mv_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_mv_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_mv");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2300,27 +2223,31 @@ int guestfs_aug_mv (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_mv_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_mv");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_MV, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_MV, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_match_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -2328,19 +2255,19 @@ struct aug_match_ctx {
   struct guestfs_aug_match_ret ret;
 };
 
-static void aug_match_send_cb (guestfs_h *g, void *data)
+static void aug_match_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_match_ctx *ctx = (struct aug_match_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_match");
+    return;
+  }
 
-static void aug_match_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_match_ctx *ctx = (struct aug_match_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_match");
@@ -2358,8 +2285,7 @@ static void aug_match_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_aug_match (guestfs_h *g,
@@ -2371,21 +2297,15 @@ char **guestfs_aug_match (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_match") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_MATCH,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_MATCH,
         (xdrproc_t) xdr_guestfs_aug_match_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_match_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_match");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -2394,19 +2314,24 @@ char **guestfs_aug_match (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_match_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_match");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_MATCH, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_MATCH, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.matches.matches_val =
     safe_realloc (g, ctx.ret.matches.matches_val,
@@ -2418,27 +2343,26 @@ char **guestfs_aug_match (guestfs_h *g,
 struct aug_save_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_save_send_cb (guestfs_h *g, void *data)
+static void aug_save_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_save_ctx *ctx = (struct aug_save_ctx *) data;
 
-  ctx->cb_sequence = 1;
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_save");
+    return;
+  }
+
   ml->main_loop_quit (ml, g);
-}
-
-static void aug_save_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_save_ctx *ctx = (struct aug_save_ctx *) data;
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_save");
@@ -2452,8 +2376,7 @@ static void aug_save_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_save (guestfs_h *g)
@@ -2463,19 +2386,13 @@ int guestfs_aug_save (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_aug_save") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_SAVE, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_save_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_save");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_SAVE, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2484,46 +2401,50 @@ int guestfs_aug_save (guestfs_h *g)
   guestfs_set_reply_callback (g, aug_save_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_save");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_SAVE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_SAVE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_load_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void aug_load_send_cb (guestfs_h *g, void *data)
+static void aug_load_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_load_ctx *ctx = (struct aug_load_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_load");
+    return;
+  }
 
-static void aug_load_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_load_ctx *ctx = (struct aug_load_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_load");
@@ -2537,8 +2458,7 @@ static void aug_load_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_aug_load (guestfs_h *g)
@@ -2548,19 +2468,13 @@ int guestfs_aug_load (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_aug_load") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_LOAD, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_load_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_load");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_LOAD, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2569,27 +2483,31 @@ int guestfs_aug_load (guestfs_h *g)
   guestfs_set_reply_callback (g, aug_load_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_load");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_LOAD, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_LOAD, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct aug_ls_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -2597,19 +2515,19 @@ struct aug_ls_ctx {
   struct guestfs_aug_ls_ret ret;
 };
 
-static void aug_ls_send_cb (guestfs_h *g, void *data)
+static void aug_ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct aug_ls_ctx *ctx = (struct aug_ls_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_aug_ls");
+    return;
+  }
 
-static void aug_ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct aug_ls_ctx *ctx = (struct aug_ls_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_aug_ls");
@@ -2627,8 +2545,7 @@ static void aug_ls_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_aug_ls (guestfs_h *g,
@@ -2640,21 +2557,15 @@ char **guestfs_aug_ls (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_aug_ls") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_AUG_LS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_AUG_LS,
         (xdrproc_t) xdr_guestfs_aug_ls_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, aug_ls_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_aug_ls");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -2663,19 +2574,24 @@ char **guestfs_aug_ls (guestfs_h *g,
   guestfs_set_reply_callback (g, aug_ls_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_aug_ls");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_LS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_AUG_LS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.matches.matches_val =
     safe_realloc (g, ctx.ret.matches.matches_val,
@@ -2687,27 +2603,26 @@ char **guestfs_aug_ls (guestfs_h *g,
 struct rm_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void rm_send_cb (guestfs_h *g, void *data)
+static void rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct rm_ctx *ctx = (struct rm_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_rm");
+    return;
+  }
 
-static void rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct rm_ctx *ctx = (struct rm_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_rm");
@@ -2721,8 +2636,7 @@ static void rm_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_rm (guestfs_h *g,
@@ -2734,21 +2648,15 @@ int guestfs_rm (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_rm") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_RM,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_RM,
         (xdrproc_t) xdr_guestfs_rm_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, rm_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_rm");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2757,46 +2665,50 @@ int guestfs_rm (guestfs_h *g,
   guestfs_set_reply_callback (g, rm_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_rm");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RM, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RM, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct rmdir_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void rmdir_send_cb (guestfs_h *g, void *data)
+static void rmdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct rmdir_ctx *ctx = (struct rmdir_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_rmdir");
+    return;
+  }
 
-static void rmdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct rmdir_ctx *ctx = (struct rmdir_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_rmdir");
@@ -2810,8 +2722,7 @@ static void rmdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_rmdir (guestfs_h *g,
@@ -2823,21 +2734,15 @@ int guestfs_rmdir (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_rmdir") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_RMDIR,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_RMDIR,
         (xdrproc_t) xdr_guestfs_rmdir_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, rmdir_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_rmdir");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2846,46 +2751,50 @@ int guestfs_rmdir (guestfs_h *g,
   guestfs_set_reply_callback (g, rmdir_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_rmdir");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RMDIR, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RMDIR, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct rm_rf_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void rm_rf_send_cb (guestfs_h *g, void *data)
+static void rm_rf_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct rm_rf_ctx *ctx = (struct rm_rf_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_rm_rf");
+    return;
+  }
 
-static void rm_rf_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct rm_rf_ctx *ctx = (struct rm_rf_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_rm_rf");
@@ -2899,8 +2808,7 @@ static void rm_rf_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_rm_rf (guestfs_h *g,
@@ -2912,21 +2820,15 @@ int guestfs_rm_rf (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_rm_rf") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_RM_RF,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_RM_RF,
         (xdrproc_t) xdr_guestfs_rm_rf_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, rm_rf_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_rm_rf");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -2935,46 +2837,50 @@ int guestfs_rm_rf (guestfs_h *g,
   guestfs_set_reply_callback (g, rm_rf_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_rm_rf");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RM_RF, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_RM_RF, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct mkdir_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void mkdir_send_cb (guestfs_h *g, void *data)
+static void mkdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct mkdir_ctx *ctx = (struct mkdir_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mkdir");
+    return;
+  }
 
-static void mkdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct mkdir_ctx *ctx = (struct mkdir_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_mkdir");
@@ -2988,8 +2894,7 @@ static void mkdir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_mkdir (guestfs_h *g,
@@ -3001,21 +2906,15 @@ int guestfs_mkdir (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_mkdir") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_MKDIR,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MKDIR,
         (xdrproc_t) xdr_guestfs_mkdir_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, mkdir_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_mkdir");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3024,46 +2923,50 @@ int guestfs_mkdir (guestfs_h *g,
   guestfs_set_reply_callback (g, mkdir_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_mkdir");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKDIR, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKDIR, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct mkdir_p_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void mkdir_p_send_cb (guestfs_h *g, void *data)
+static void mkdir_p_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct mkdir_p_ctx *ctx = (struct mkdir_p_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mkdir_p");
+    return;
+  }
 
-static void mkdir_p_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct mkdir_p_ctx *ctx = (struct mkdir_p_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_mkdir_p");
@@ -3077,8 +2980,7 @@ static void mkdir_p_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_mkdir_p (guestfs_h *g,
@@ -3090,21 +2992,15 @@ int guestfs_mkdir_p (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_mkdir_p") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_MKDIR_P,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MKDIR_P,
         (xdrproc_t) xdr_guestfs_mkdir_p_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, mkdir_p_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_mkdir_p");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3113,46 +3009,50 @@ int guestfs_mkdir_p (guestfs_h *g,
   guestfs_set_reply_callback (g, mkdir_p_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_mkdir_p");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKDIR_P, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKDIR_P, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct chmod_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void chmod_send_cb (guestfs_h *g, void *data)
+static void chmod_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct chmod_ctx *ctx = (struct chmod_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_chmod");
+    return;
+  }
 
-static void chmod_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct chmod_ctx *ctx = (struct chmod_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_chmod");
@@ -3166,8 +3066,7 @@ static void chmod_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_chmod (guestfs_h *g,
@@ -3180,22 +3079,16 @@ int guestfs_chmod (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_chmod") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.mode = mode;
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_CHMOD,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CHMOD,
         (xdrproc_t) xdr_guestfs_chmod_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, chmod_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_chmod");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3204,46 +3097,50 @@ int guestfs_chmod (guestfs_h *g,
   guestfs_set_reply_callback (g, chmod_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_chmod");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CHMOD, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CHMOD, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct chown_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void chown_send_cb (guestfs_h *g, void *data)
+static void chown_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct chown_ctx *ctx = (struct chown_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_chown");
+    return;
+  }
 
-static void chown_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct chown_ctx *ctx = (struct chown_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_chown");
@@ -3257,8 +3154,7 @@ static void chown_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_chown (guestfs_h *g,
@@ -3272,23 +3168,17 @@ int guestfs_chown (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_chown") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.owner = owner;
   args.group = group;
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_CHOWN,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CHOWN,
         (xdrproc_t) xdr_guestfs_chown_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, chown_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_chown");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3297,27 +3187,31 @@ int guestfs_chown (guestfs_h *g,
   guestfs_set_reply_callback (g, chown_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_chown");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CHOWN, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CHOWN, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct exists_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -3325,19 +3219,19 @@ struct exists_ctx {
   struct guestfs_exists_ret ret;
 };
 
-static void exists_send_cb (guestfs_h *g, void *data)
+static void exists_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct exists_ctx *ctx = (struct exists_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_exists");
+    return;
+  }
 
-static void exists_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct exists_ctx *ctx = (struct exists_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_exists");
@@ -3355,8 +3249,7 @@ static void exists_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_exists (guestfs_h *g,
@@ -3368,21 +3261,15 @@ int guestfs_exists (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_exists") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_EXISTS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_EXISTS,
         (xdrproc_t) xdr_guestfs_exists_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, exists_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_exists");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3391,27 +3278,31 @@ int guestfs_exists (guestfs_h *g,
   guestfs_set_reply_callback (g, exists_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_exists");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_EXISTS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_EXISTS, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.existsflag;
 }
 
 struct is_file_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -3419,19 +3310,19 @@ struct is_file_ctx {
   struct guestfs_is_file_ret ret;
 };
 
-static void is_file_send_cb (guestfs_h *g, void *data)
+static void is_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct is_file_ctx *ctx = (struct is_file_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_is_file");
+    return;
+  }
 
-static void is_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct is_file_ctx *ctx = (struct is_file_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_is_file");
@@ -3449,8 +3340,7 @@ static void is_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_is_file (guestfs_h *g,
@@ -3462,21 +3352,15 @@ int guestfs_is_file (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_is_file") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_IS_FILE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_IS_FILE,
         (xdrproc_t) xdr_guestfs_is_file_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, is_file_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_is_file");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3485,27 +3369,31 @@ int guestfs_is_file (guestfs_h *g,
   guestfs_set_reply_callback (g, is_file_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_is_file");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_IS_FILE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_IS_FILE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.fileflag;
 }
 
 struct is_dir_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -3513,19 +3401,19 @@ struct is_dir_ctx {
   struct guestfs_is_dir_ret ret;
 };
 
-static void is_dir_send_cb (guestfs_h *g, void *data)
+static void is_dir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct is_dir_ctx *ctx = (struct is_dir_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_is_dir");
+    return;
+  }
 
-static void is_dir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct is_dir_ctx *ctx = (struct is_dir_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_is_dir");
@@ -3543,8 +3431,7 @@ static void is_dir_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_is_dir (guestfs_h *g,
@@ -3556,21 +3443,15 @@ int guestfs_is_dir (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_is_dir") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_IS_DIR,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_IS_DIR,
         (xdrproc_t) xdr_guestfs_is_dir_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, is_dir_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_is_dir");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3579,46 +3460,50 @@ int guestfs_is_dir (guestfs_h *g,
   guestfs_set_reply_callback (g, is_dir_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_is_dir");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_IS_DIR, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_IS_DIR, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.dirflag;
 }
 
 struct pvcreate_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void pvcreate_send_cb (guestfs_h *g, void *data)
+static void pvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct pvcreate_ctx *ctx = (struct pvcreate_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_pvcreate");
+    return;
+  }
 
-static void pvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct pvcreate_ctx *ctx = (struct pvcreate_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_pvcreate");
@@ -3632,8 +3517,7 @@ static void pvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_pvcreate (guestfs_h *g,
@@ -3645,21 +3529,15 @@ int guestfs_pvcreate (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_pvcreate") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_PVCREATE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_PVCREATE,
         (xdrproc_t) xdr_guestfs_pvcreate_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, pvcreate_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_pvcreate");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3668,46 +3546,50 @@ int guestfs_pvcreate (guestfs_h *g,
   guestfs_set_reply_callback (g, pvcreate_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_pvcreate");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVCREATE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVCREATE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct vgcreate_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void vgcreate_send_cb (guestfs_h *g, void *data)
+static void vgcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct vgcreate_ctx *ctx = (struct vgcreate_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_vgcreate");
+    return;
+  }
 
-static void vgcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct vgcreate_ctx *ctx = (struct vgcreate_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_vgcreate");
@@ -3721,8 +3603,7 @@ static void vgcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_vgcreate (guestfs_h *g,
@@ -3735,23 +3616,17 @@ int guestfs_vgcreate (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_vgcreate") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.volgroup = (char *) volgroup;
   args.physvols.physvols_val = (char **) physvols;
   for (args.physvols.physvols_len = 0; physvols[args.physvols.physvols_len]; args.physvols.physvols_len++) ;
-  serial = guestfs__send (g, GUESTFS_PROC_VGCREATE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_VGCREATE,
         (xdrproc_t) xdr_guestfs_vgcreate_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, vgcreate_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_vgcreate");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3760,46 +3635,50 @@ int guestfs_vgcreate (guestfs_h *g,
   guestfs_set_reply_callback (g, vgcreate_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_vgcreate");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGCREATE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGCREATE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct lvcreate_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void lvcreate_send_cb (guestfs_h *g, void *data)
+static void lvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct lvcreate_ctx *ctx = (struct lvcreate_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lvcreate");
+    return;
+  }
 
-static void lvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct lvcreate_ctx *ctx = (struct lvcreate_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_lvcreate");
@@ -3813,8 +3692,7 @@ static void lvcreate_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_lvcreate (guestfs_h *g,
@@ -3828,23 +3706,17 @@ int guestfs_lvcreate (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_lvcreate") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.logvol = (char *) logvol;
   args.volgroup = (char *) volgroup;
   args.mbytes = mbytes;
-  serial = guestfs__send (g, GUESTFS_PROC_LVCREATE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LVCREATE,
         (xdrproc_t) xdr_guestfs_lvcreate_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, lvcreate_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_lvcreate");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3853,46 +3725,50 @@ int guestfs_lvcreate (guestfs_h *g,
   guestfs_set_reply_callback (g, lvcreate_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_lvcreate");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVCREATE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVCREATE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct mkfs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void mkfs_send_cb (guestfs_h *g, void *data)
+static void mkfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct mkfs_ctx *ctx = (struct mkfs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mkfs");
+    return;
+  }
 
-static void mkfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct mkfs_ctx *ctx = (struct mkfs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_mkfs");
@@ -3906,8 +3782,7 @@ static void mkfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_mkfs (guestfs_h *g,
@@ -3920,22 +3795,16 @@ int guestfs_mkfs (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_mkfs") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.fstype = (char *) fstype;
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_MKFS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MKFS,
         (xdrproc_t) xdr_guestfs_mkfs_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, mkfs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_mkfs");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -3944,46 +3813,50 @@ int guestfs_mkfs (guestfs_h *g,
   guestfs_set_reply_callback (g, mkfs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_mkfs");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKFS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKFS, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct sfdisk_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void sfdisk_send_cb (guestfs_h *g, void *data)
+static void sfdisk_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct sfdisk_ctx *ctx = (struct sfdisk_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_sfdisk");
+    return;
+  }
 
-static void sfdisk_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct sfdisk_ctx *ctx = (struct sfdisk_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_sfdisk");
@@ -3997,8 +3870,7 @@ static void sfdisk_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_sfdisk (guestfs_h *g,
@@ -4014,6 +3886,7 @@ int guestfs_sfdisk (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_sfdisk") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
@@ -4023,17 +3896,10 @@ int guestfs_sfdisk (guestfs_h *g,
   args.sectors = sectors;
   args.lines.lines_val = (char **) lines;
   for (args.lines.lines_len = 0; lines[args.lines.lines_len]; args.lines.lines_len++) ;
-  serial = guestfs__send (g, GUESTFS_PROC_SFDISK,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_SFDISK,
         (xdrproc_t) xdr_guestfs_sfdisk_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, sfdisk_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_sfdisk");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -4042,46 +3908,50 @@ int guestfs_sfdisk (guestfs_h *g,
   guestfs_set_reply_callback (g, sfdisk_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_sfdisk");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SFDISK, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SFDISK, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct write_file_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void write_file_send_cb (guestfs_h *g, void *data)
+static void write_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct write_file_ctx *ctx = (struct write_file_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_write_file");
+    return;
+  }
 
-static void write_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct write_file_ctx *ctx = (struct write_file_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_write_file");
@@ -4095,8 +3965,7 @@ static void write_file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_write_file (guestfs_h *g,
@@ -4110,23 +3979,17 @@ int guestfs_write_file (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_write_file") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
   args.content = (char *) content;
   args.size = size;
-  serial = guestfs__send (g, GUESTFS_PROC_WRITE_FILE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_WRITE_FILE,
         (xdrproc_t) xdr_guestfs_write_file_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, write_file_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_write_file");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -4135,46 +3998,50 @@ int guestfs_write_file (guestfs_h *g,
   guestfs_set_reply_callback (g, write_file_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_write_file");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_WRITE_FILE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_WRITE_FILE, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct umount_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void umount_send_cb (guestfs_h *g, void *data)
+static void umount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct umount_ctx *ctx = (struct umount_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_umount");
+    return;
+  }
 
-static void umount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct umount_ctx *ctx = (struct umount_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_umount");
@@ -4188,8 +4055,7 @@ static void umount_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_umount (guestfs_h *g,
@@ -4201,21 +4067,15 @@ int guestfs_umount (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_umount") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.pathordevice = (char *) pathordevice;
-  serial = guestfs__send (g, GUESTFS_PROC_UMOUNT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_UMOUNT,
         (xdrproc_t) xdr_guestfs_umount_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, umount_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_umount");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -4224,27 +4084,31 @@ int guestfs_umount (guestfs_h *g,
   guestfs_set_reply_callback (g, umount_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_umount");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UMOUNT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UMOUNT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct mounts_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4252,19 +4116,19 @@ struct mounts_ctx {
   struct guestfs_mounts_ret ret;
 };
 
-static void mounts_send_cb (guestfs_h *g, void *data)
+static void mounts_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct mounts_ctx *ctx = (struct mounts_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mounts");
+    return;
+  }
 
-static void mounts_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct mounts_ctx *ctx = (struct mounts_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_mounts");
@@ -4282,8 +4146,7 @@ static void mounts_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_mounts (guestfs_h *g)
@@ -4293,19 +4156,13 @@ char **guestfs_mounts (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_mounts") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_MOUNTS, NULL, NULL);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, mounts_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_mounts");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MOUNTS, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4314,19 +4171,24 @@ char **guestfs_mounts (guestfs_h *g)
   guestfs_set_reply_callback (g, mounts_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_mounts");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNTS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNTS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.devices.devices_val =
     safe_realloc (g, ctx.ret.devices.devices_val,
@@ -4338,27 +4200,26 @@ char **guestfs_mounts (guestfs_h *g)
 struct umount_all_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void umount_all_send_cb (guestfs_h *g, void *data)
+static void umount_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct umount_all_ctx *ctx = (struct umount_all_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_umount_all");
+    return;
+  }
 
-static void umount_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct umount_all_ctx *ctx = (struct umount_all_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_umount_all");
@@ -4372,8 +4233,7 @@ static void umount_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_umount_all (guestfs_h *g)
@@ -4383,19 +4243,13 @@ int guestfs_umount_all (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_umount_all") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_UMOUNT_ALL, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, umount_all_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_umount_all");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_UMOUNT_ALL, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -4404,46 +4258,50 @@ int guestfs_umount_all (guestfs_h *g)
   guestfs_set_reply_callback (g, umount_all_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_umount_all");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UMOUNT_ALL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UMOUNT_ALL, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct lvm_remove_all_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void lvm_remove_all_send_cb (guestfs_h *g, void *data)
+static void lvm_remove_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct lvm_remove_all_ctx *ctx = (struct lvm_remove_all_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lvm_remove_all");
+    return;
+  }
 
-static void lvm_remove_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct lvm_remove_all_ctx *ctx = (struct lvm_remove_all_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_lvm_remove_all");
@@ -4457,8 +4315,7 @@ static void lvm_remove_all_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_lvm_remove_all (guestfs_h *g)
@@ -4468,19 +4325,13 @@ int guestfs_lvm_remove_all (guestfs_h *g)
   int serial;
 
   if (check_state (g, "guestfs_lvm_remove_all") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
-  serial = guestfs__send (g, GUESTFS_PROC_LVM_REMOVE_ALL, NULL, NULL);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, lvm_remove_all_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_lvm_remove_all");
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LVM_REMOVE_ALL, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -4489,27 +4340,31 @@ int guestfs_lvm_remove_all (guestfs_h *g)
   guestfs_set_reply_callback (g, lvm_remove_all_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_lvm_remove_all");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVM_REMOVE_ALL, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVM_REMOVE_ALL, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct file_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4517,19 +4372,19 @@ struct file_ctx {
   struct guestfs_file_ret ret;
 };
 
-static void file_send_cb (guestfs_h *g, void *data)
+static void file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct file_ctx *ctx = (struct file_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_file");
+    return;
+  }
 
-static void file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct file_ctx *ctx = (struct file_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_file");
@@ -4547,8 +4402,7 @@ static void file_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char *guestfs_file (guestfs_h *g,
@@ -4560,21 +4414,15 @@ char *guestfs_file (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_file") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_FILE,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_FILE,
         (xdrproc_t) xdr_guestfs_file_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, file_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_file");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4583,27 +4431,31 @@ char *guestfs_file (guestfs_h *g,
   guestfs_set_reply_callback (g, file_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_file");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_FILE, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_FILE, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.description; /* caller will free */
 }
 
 struct command_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4611,19 +4463,19 @@ struct command_ctx {
   struct guestfs_command_ret ret;
 };
 
-static void command_send_cb (guestfs_h *g, void *data)
+static void command_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct command_ctx *ctx = (struct command_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_command");
+    return;
+  }
 
-static void command_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct command_ctx *ctx = (struct command_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_command");
@@ -4641,8 +4493,7 @@ static void command_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char *guestfs_command (guestfs_h *g,
@@ -4654,22 +4505,16 @@ char *guestfs_command (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_command") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.arguments.arguments_val = (char **) arguments;
   for (args.arguments.arguments_len = 0; arguments[args.arguments.arguments_len]; args.arguments.arguments_len++) ;
-  serial = guestfs__send (g, GUESTFS_PROC_COMMAND,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_COMMAND,
         (xdrproc_t) xdr_guestfs_command_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, command_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_command");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4678,27 +4523,31 @@ char *guestfs_command (guestfs_h *g,
   guestfs_set_reply_callback (g, command_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_command");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_COMMAND, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_COMMAND, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.output; /* caller will free */
 }
 
 struct command_lines_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4706,19 +4555,19 @@ struct command_lines_ctx {
   struct guestfs_command_lines_ret ret;
 };
 
-static void command_lines_send_cb (guestfs_h *g, void *data)
+static void command_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct command_lines_ctx *ctx = (struct command_lines_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_command_lines");
+    return;
+  }
 
-static void command_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct command_lines_ctx *ctx = (struct command_lines_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_command_lines");
@@ -4736,8 +4585,7 @@ static void command_lines_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_command_lines (guestfs_h *g,
@@ -4749,22 +4597,16 @@ char **guestfs_command_lines (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_command_lines") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.arguments.arguments_val = (char **) arguments;
   for (args.arguments.arguments_len = 0; arguments[args.arguments.arguments_len]; args.arguments.arguments_len++) ;
-  serial = guestfs__send (g, GUESTFS_PROC_COMMAND_LINES,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_COMMAND_LINES,
         (xdrproc_t) xdr_guestfs_command_lines_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, command_lines_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_command_lines");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4773,19 +4615,24 @@ char **guestfs_command_lines (guestfs_h *g,
   guestfs_set_reply_callback (g, command_lines_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_command_lines");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_COMMAND_LINES, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_COMMAND_LINES, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.lines.lines_val =
     safe_realloc (g, ctx.ret.lines.lines_val,
@@ -4797,8 +4644,7 @@ char **guestfs_command_lines (guestfs_h *g,
 struct stat_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4806,19 +4652,19 @@ struct stat_ctx {
   struct guestfs_stat_ret ret;
 };
 
-static void stat_send_cb (guestfs_h *g, void *data)
+static void stat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct stat_ctx *ctx = (struct stat_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_stat");
+    return;
+  }
 
-static void stat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct stat_ctx *ctx = (struct stat_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_stat");
@@ -4836,8 +4682,7 @@ static void stat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_stat *guestfs_stat (guestfs_h *g,
@@ -4849,21 +4694,15 @@ struct guestfs_stat *guestfs_stat (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_stat") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_STAT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_STAT,
         (xdrproc_t) xdr_guestfs_stat_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, stat_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_stat");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4872,19 +4711,24 @@ struct guestfs_stat *guestfs_stat (guestfs_h *g,
   guestfs_set_reply_callback (g, stat_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_stat");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_STAT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_STAT, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.statbuf, sizeof (ctx.ret.statbuf));
 }
@@ -4892,8 +4736,7 @@ struct guestfs_stat *guestfs_stat (guestfs_h *g,
 struct lstat_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4901,19 +4744,19 @@ struct lstat_ctx {
   struct guestfs_lstat_ret ret;
 };
 
-static void lstat_send_cb (guestfs_h *g, void *data)
+static void lstat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct lstat_ctx *ctx = (struct lstat_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lstat");
+    return;
+  }
 
-static void lstat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct lstat_ctx *ctx = (struct lstat_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_lstat");
@@ -4931,8 +4774,7 @@ static void lstat_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_stat *guestfs_lstat (guestfs_h *g,
@@ -4944,21 +4786,15 @@ struct guestfs_stat *guestfs_lstat (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_lstat") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_LSTAT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LSTAT,
         (xdrproc_t) xdr_guestfs_lstat_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, lstat_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_lstat");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -4967,19 +4803,24 @@ struct guestfs_stat *guestfs_lstat (guestfs_h *g,
   guestfs_set_reply_callback (g, lstat_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_lstat");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LSTAT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LSTAT, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.statbuf, sizeof (ctx.ret.statbuf));
 }
@@ -4987,8 +4828,7 @@ struct guestfs_stat *guestfs_lstat (guestfs_h *g,
 struct statvfs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -4996,19 +4836,19 @@ struct statvfs_ctx {
   struct guestfs_statvfs_ret ret;
 };
 
-static void statvfs_send_cb (guestfs_h *g, void *data)
+static void statvfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct statvfs_ctx *ctx = (struct statvfs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_statvfs");
+    return;
+  }
 
-static void statvfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct statvfs_ctx *ctx = (struct statvfs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_statvfs");
@@ -5026,8 +4866,7 @@ static void statvfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 struct guestfs_statvfs *guestfs_statvfs (guestfs_h *g,
@@ -5039,21 +4878,15 @@ struct guestfs_statvfs *guestfs_statvfs (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_statvfs") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.path = (char *) path;
-  serial = guestfs__send (g, GUESTFS_PROC_STATVFS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_STATVFS,
         (xdrproc_t) xdr_guestfs_statvfs_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, statvfs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_statvfs");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -5062,19 +4895,24 @@ struct guestfs_statvfs *guestfs_statvfs (guestfs_h *g,
   guestfs_set_reply_callback (g, statvfs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_statvfs");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_STATVFS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_STATVFS, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this */
   return safe_memdup (g, &ctx.ret.statbuf, sizeof (ctx.ret.statbuf));
 }
@@ -5082,8 +4920,7 @@ struct guestfs_statvfs *guestfs_statvfs (guestfs_h *g,
 struct tune2fs_l_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5091,19 +4928,19 @@ struct tune2fs_l_ctx {
   struct guestfs_tune2fs_l_ret ret;
 };
 
-static void tune2fs_l_send_cb (guestfs_h *g, void *data)
+static void tune2fs_l_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct tune2fs_l_ctx *ctx = (struct tune2fs_l_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_tune2fs_l");
+    return;
+  }
 
-static void tune2fs_l_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct tune2fs_l_ctx *ctx = (struct tune2fs_l_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_tune2fs_l");
@@ -5121,8 +4958,7 @@ static void tune2fs_l_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 char **guestfs_tune2fs_l (guestfs_h *g,
@@ -5134,21 +4970,15 @@ char **guestfs_tune2fs_l (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_tune2fs_l") == -1) return NULL;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_TUNE2FS_L,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TUNE2FS_L,
         (xdrproc_t) xdr_guestfs_tune2fs_l_args, (char *) &args);
-  if (serial == -1)
-    return NULL;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, tune2fs_l_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_tune2fs_l");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return NULL;
   }
 
@@ -5157,19 +4987,24 @@ char **guestfs_tune2fs_l (guestfs_h *g,
   guestfs_set_reply_callback (g, tune2fs_l_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_tune2fs_l");
+    guestfs_set_ready (g);
     return NULL;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TUNE2FS_L, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TUNE2FS_L, serial) == -1) {
+    guestfs_set_ready (g);
     return NULL;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return NULL;
   }
 
+  guestfs_set_ready (g);
   /* caller will free this, but we need to add a NULL entry */
   ctx.ret.superblock.superblock_val =
     safe_realloc (g, ctx.ret.superblock.superblock_val,
@@ -5181,27 +5016,26 @@ char **guestfs_tune2fs_l (guestfs_h *g,
 struct blockdev_setro_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void blockdev_setro_send_cb (guestfs_h *g, void *data)
+static void blockdev_setro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_setro_ctx *ctx = (struct blockdev_setro_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_setro");
+    return;
+  }
 
-static void blockdev_setro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_setro_ctx *ctx = (struct blockdev_setro_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_setro");
@@ -5215,8 +5049,7 @@ static void blockdev_setro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_setro (guestfs_h *g,
@@ -5228,21 +5061,15 @@ int guestfs_blockdev_setro (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_setro") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_SETRO,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_SETRO,
         (xdrproc_t) xdr_guestfs_blockdev_setro_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_setro_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_setro");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5251,46 +5078,50 @@ int guestfs_blockdev_setro (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_setro_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_setro");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETRO, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETRO, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct blockdev_setrw_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void blockdev_setrw_send_cb (guestfs_h *g, void *data)
+static void blockdev_setrw_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_setrw_ctx *ctx = (struct blockdev_setrw_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_setrw");
+    return;
+  }
 
-static void blockdev_setrw_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_setrw_ctx *ctx = (struct blockdev_setrw_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_setrw");
@@ -5304,8 +5135,7 @@ static void blockdev_setrw_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_setrw (guestfs_h *g,
@@ -5317,21 +5147,15 @@ int guestfs_blockdev_setrw (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_setrw") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_SETRW,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_SETRW,
         (xdrproc_t) xdr_guestfs_blockdev_setrw_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_setrw_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_setrw");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5340,27 +5164,31 @@ int guestfs_blockdev_setrw (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_setrw_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_setrw");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETRW, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETRW, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct blockdev_getro_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5368,19 +5196,19 @@ struct blockdev_getro_ctx {
   struct guestfs_blockdev_getro_ret ret;
 };
 
-static void blockdev_getro_send_cb (guestfs_h *g, void *data)
+static void blockdev_getro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_getro_ctx *ctx = (struct blockdev_getro_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_getro");
+    return;
+  }
 
-static void blockdev_getro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_getro_ctx *ctx = (struct blockdev_getro_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_getro");
@@ -5398,8 +5226,7 @@ static void blockdev_getro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_getro (guestfs_h *g,
@@ -5411,21 +5238,15 @@ int guestfs_blockdev_getro (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_getro") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_GETRO,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_GETRO,
         (xdrproc_t) xdr_guestfs_blockdev_getro_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_getro_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_getro");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5434,27 +5255,31 @@ int guestfs_blockdev_getro (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_getro_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_getro");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETRO, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETRO, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.ro;
 }
 
 struct blockdev_getss_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5462,19 +5287,19 @@ struct blockdev_getss_ctx {
   struct guestfs_blockdev_getss_ret ret;
 };
 
-static void blockdev_getss_send_cb (guestfs_h *g, void *data)
+static void blockdev_getss_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_getss_ctx *ctx = (struct blockdev_getss_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_getss");
+    return;
+  }
 
-static void blockdev_getss_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_getss_ctx *ctx = (struct blockdev_getss_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_getss");
@@ -5492,8 +5317,7 @@ static void blockdev_getss_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_getss (guestfs_h *g,
@@ -5505,21 +5329,15 @@ int guestfs_blockdev_getss (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_getss") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_GETSS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_GETSS,
         (xdrproc_t) xdr_guestfs_blockdev_getss_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_getss_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_getss");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5528,27 +5346,31 @@ int guestfs_blockdev_getss (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_getss_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_getss");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSS, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.sectorsize;
 }
 
 struct blockdev_getbsz_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5556,19 +5378,19 @@ struct blockdev_getbsz_ctx {
   struct guestfs_blockdev_getbsz_ret ret;
 };
 
-static void blockdev_getbsz_send_cb (guestfs_h *g, void *data)
+static void blockdev_getbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_getbsz_ctx *ctx = (struct blockdev_getbsz_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_getbsz");
+    return;
+  }
 
-static void blockdev_getbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_getbsz_ctx *ctx = (struct blockdev_getbsz_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_getbsz");
@@ -5586,8 +5408,7 @@ static void blockdev_getbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_getbsz (guestfs_h *g,
@@ -5599,21 +5420,15 @@ int guestfs_blockdev_getbsz (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_getbsz") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_GETBSZ,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_GETBSZ,
         (xdrproc_t) xdr_guestfs_blockdev_getbsz_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_getbsz_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_getbsz");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5622,46 +5437,50 @@ int guestfs_blockdev_getbsz (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_getbsz_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_getbsz");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETBSZ, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETBSZ, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.blocksize;
 }
 
 struct blockdev_setbsz_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void blockdev_setbsz_send_cb (guestfs_h *g, void *data)
+static void blockdev_setbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_setbsz_ctx *ctx = (struct blockdev_setbsz_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_setbsz");
+    return;
+  }
 
-static void blockdev_setbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_setbsz_ctx *ctx = (struct blockdev_setbsz_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_setbsz");
@@ -5675,8 +5494,7 @@ static void blockdev_setbsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_setbsz (guestfs_h *g,
@@ -5689,22 +5507,16 @@ int guestfs_blockdev_setbsz (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_setbsz") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
   args.blocksize = blocksize;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_SETBSZ,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_SETBSZ,
         (xdrproc_t) xdr_guestfs_blockdev_setbsz_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_setbsz_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_setbsz");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5713,27 +5525,31 @@ int guestfs_blockdev_setbsz (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_setbsz_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_setbsz");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETBSZ, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_SETBSZ, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct blockdev_getsz_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5741,19 +5557,19 @@ struct blockdev_getsz_ctx {
   struct guestfs_blockdev_getsz_ret ret;
 };
 
-static void blockdev_getsz_send_cb (guestfs_h *g, void *data)
+static void blockdev_getsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_getsz_ctx *ctx = (struct blockdev_getsz_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_getsz");
+    return;
+  }
 
-static void blockdev_getsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_getsz_ctx *ctx = (struct blockdev_getsz_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_getsz");
@@ -5771,8 +5587,7 @@ static void blockdev_getsz_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int64_t guestfs_blockdev_getsz (guestfs_h *g,
@@ -5784,21 +5599,15 @@ int64_t guestfs_blockdev_getsz (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_getsz") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_GETSZ,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_GETSZ,
         (xdrproc_t) xdr_guestfs_blockdev_getsz_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_getsz_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_getsz");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5807,27 +5616,31 @@ int64_t guestfs_blockdev_getsz (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_getsz_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_getsz");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSZ, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSZ, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.sizeinsectors;
 }
 
 struct blockdev_getsize64_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
@@ -5835,19 +5648,19 @@ struct blockdev_getsize64_ctx {
   struct guestfs_blockdev_getsize64_ret ret;
 };
 
-static void blockdev_getsize64_send_cb (guestfs_h *g, void *data)
+static void blockdev_getsize64_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_getsize64_ctx *ctx = (struct blockdev_getsize64_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_getsize64");
+    return;
+  }
 
-static void blockdev_getsize64_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_getsize64_ctx *ctx = (struct blockdev_getsize64_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_getsize64");
@@ -5865,8 +5678,7 @@ static void blockdev_getsize64_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     return;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int64_t guestfs_blockdev_getsize64 (guestfs_h *g,
@@ -5878,21 +5690,15 @@ int64_t guestfs_blockdev_getsize64 (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_getsize64") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_GETSIZE64,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_GETSIZE64,
         (xdrproc_t) xdr_guestfs_blockdev_getsize64_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_getsize64_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_getsize64");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -5901,46 +5707,50 @@ int64_t guestfs_blockdev_getsize64 (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_getsize64_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_getsize64");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSIZE64, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_GETSIZE64, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return ctx.ret.sizeinbytes;
 }
 
 struct blockdev_flushbufs_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void blockdev_flushbufs_send_cb (guestfs_h *g, void *data)
+static void blockdev_flushbufs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_flushbufs_ctx *ctx = (struct blockdev_flushbufs_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_flushbufs");
+    return;
+  }
 
-static void blockdev_flushbufs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_flushbufs_ctx *ctx = (struct blockdev_flushbufs_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_flushbufs");
@@ -5954,8 +5764,7 @@ static void blockdev_flushbufs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_flushbufs (guestfs_h *g,
@@ -5967,69 +5776,67 @@ int guestfs_blockdev_flushbufs (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_flushbufs") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_FLUSHBUFS,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_FLUSHBUFS,
         (xdrproc_t) xdr_guestfs_blockdev_flushbufs_args, (char *) &args);
-  if (serial == -1)
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_flushbufs_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_flushbufs");
-    return -1;
-  }
+  }
 
   guestfs__switch_to_receiving (g);
   ctx.cb_sequence = 0;
   guestfs_set_reply_callback (g, blockdev_flushbufs_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_flushbufs");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_FLUSHBUFS, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_FLUSHBUFS, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct blockdev_rereadpt_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void blockdev_rereadpt_send_cb (guestfs_h *g, void *data)
+static void blockdev_rereadpt_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct blockdev_rereadpt_ctx *ctx = (struct blockdev_rereadpt_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_blockdev_rereadpt");
+    return;
+  }
 
-static void blockdev_rereadpt_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct blockdev_rereadpt_ctx *ctx = (struct blockdev_rereadpt_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_blockdev_rereadpt");
@@ -6043,8 +5850,7 @@ static void blockdev_rereadpt_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_blockdev_rereadpt (guestfs_h *g,
@@ -6056,21 +5862,15 @@ int guestfs_blockdev_rereadpt (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_blockdev_rereadpt") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.device = (char *) device;
-  serial = guestfs__send (g, GUESTFS_PROC_BLOCKDEV_REREADPT,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_BLOCKDEV_REREADPT,
         (xdrproc_t) xdr_guestfs_blockdev_rereadpt_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, blockdev_rereadpt_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_blockdev_rereadpt");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
@@ -6079,46 +5879,50 @@ int guestfs_blockdev_rereadpt (guestfs_h *g,
   guestfs_set_reply_callback (g, blockdev_rereadpt_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_blockdev_rereadpt");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_REREADPT, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_BLOCKDEV_REREADPT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct upload_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void upload_send_cb (guestfs_h *g, void *data)
+static void upload_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct upload_ctx *ctx = (struct upload_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_upload");
+    return;
+  }
 
-static void upload_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct upload_ctx *ctx = (struct upload_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_upload");
@@ -6132,8 +5936,7 @@ static void upload_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_upload (guestfs_h *g,
@@ -6146,72 +5949,80 @@ int guestfs_upload (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_upload") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.remotefilename = (char *) remotefilename;
-  serial = guestfs__send (g, GUESTFS_PROC_UPLOAD,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_UPLOAD,
         (xdrproc_t) xdr_guestfs_upload_args, (char *) &args);
-  if (serial == -1)
-    return -1;
-
-  ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, upload_send_cb, &ctx);
-  (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_upload");
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (guestfs__send_file_sync (ml, g, filename) == -1)
-    return -1;
+  {
+    int r;
+
+    r = guestfs__send_file_sync (g, filename);
+    if (r == -1) {
+      guestfs_set_ready (g);
+      return -1;
+    }
+    if (r == -2) /* daemon cancelled */
+      goto read_reply;
+  }
 
+ read_reply:
   guestfs__switch_to_receiving (g);
   ctx.cb_sequence = 0;
   guestfs_set_reply_callback (g, upload_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
+  if (ctx.cb_sequence != 1) {
     error (g, "%s reply failed, see earlier error messages", "guestfs_upload");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UPLOAD, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_UPLOAD, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
   return 0;
 }
 
 struct download_ctx {
   /* This flag is set by the callbacks, so we know we've done
    * the callbacks as expected, and in the right sequence.
-   * 0 = not called, 1 = send called,
-   * 1001 = reply called.
+   * 0 = not called, 1 = reply_cb called.
    */
   int cb_sequence;
   struct guestfs_message_header hdr;
   struct guestfs_message_error err;
 };
 
-static void download_send_cb (guestfs_h *g, void *data)
+static void download_reply_cb (guestfs_h *g, void *data, XDR *xdr)
 {
   guestfs_main_loop *ml = guestfs_get_main_loop (g);
   struct download_ctx *ctx = (struct download_ctx *) data;
 
-  ctx->cb_sequence = 1;
-  ml->main_loop_quit (ml, g);
-}
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_download");
+    return;
+  }
 
-static void download_reply_cb (guestfs_h *g, void *data, XDR *xdr)
-{
-  guestfs_main_loop *ml = guestfs_get_main_loop (g);
-  struct download_ctx *ctx = (struct download_ctx *) data;
+  ml->main_loop_quit (ml, g);
 
   if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
     error (g, "%s: failed to parse reply header", "guestfs_download");
@@ -6225,8 +6036,7 @@ static void download_reply_cb (guestfs_h *g, void *data, XDR *xdr)
     goto done;
   }
  done:
-  ctx->cb_sequence = 1001;
-  ml->main_loop_quit (ml, g);
+  ctx->cb_sequence = 1;
 }
 
 int guestfs_download (guestfs_h *g,
@@ -6239,45 +6049,2289 @@ int guestfs_download (guestfs_h *g,
   int serial;
 
   if (check_state (g, "guestfs_download") == -1) return -1;
+  guestfs_set_busy (g);
 
   memset (&ctx, 0, sizeof ctx);
 
   args.remotefilename = (char *) remotefilename;
-  serial = guestfs__send (g, GUESTFS_PROC_DOWNLOAD,
+  serial = guestfs__send_sync (g, GUESTFS_PROC_DOWNLOAD,
         (xdrproc_t) xdr_guestfs_download_args, (char *) &args);
-  if (serial == -1)
+  if (serial == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
+  guestfs__switch_to_receiving (g);
   ctx.cb_sequence = 0;
-  guestfs_set_send_callback (g, download_send_cb, &ctx);
+  guestfs_set_reply_callback (g, download_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
-  guestfs_set_send_callback (g, NULL, NULL);
+  guestfs_set_reply_callback (g, NULL, NULL);
   if (ctx.cb_sequence != 1) {
-    error (g, "%s send failed, see earlier error messages", "guestfs_download");
+    error (g, "%s reply failed, see earlier error messages", "guestfs_download");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DOWNLOAD, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (guestfs__receive_file_sync (g, filename) == -1) {
+    guestfs_set_ready (g);
     return -1;
   }
 
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct checksum_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_checksum_ret ret;
+};
+
+static void checksum_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct checksum_ctx *ctx = (struct checksum_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_checksum");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_checksum");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_checksum");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_checksum_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_checksum");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+char *guestfs_checksum (guestfs_h *g,
+               const char *csumtype,
+               const char *path)
+{
+  struct guestfs_checksum_args args;
+  struct checksum_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_checksum") == -1) return NULL;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.csumtype = (char *) csumtype;
+  args.path = (char *) path;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CHECKSUM,
+        (xdrproc_t) xdr_guestfs_checksum_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
   guestfs__switch_to_receiving (g);
   ctx.cb_sequence = 0;
-  guestfs_set_reply_callback (g, download_reply_cb, &ctx);
+  guestfs_set_reply_callback (g, checksum_reply_cb, &ctx);
   (void) ml->main_loop_run (ml, g);
   guestfs_set_reply_callback (g, NULL, NULL);
-  if (ctx.cb_sequence != 1001) {
-    error (g, "%s reply failed, see earlier error messages", "guestfs_download");
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_checksum");
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CHECKSUM, serial) == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.checksum; /* caller will free */
+}
+
+struct tar_in_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void tar_in_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct tar_in_ctx *ctx = (struct tar_in_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_tar_in");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_tar_in");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_tar_in");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_tar_in (guestfs_h *g,
+               const char *tarfile,
+               const char *directory)
+{
+  struct guestfs_tar_in_args args;
+  struct tar_in_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_tar_in") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.directory = (char *) directory;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TAR_IN,
+        (xdrproc_t) xdr_guestfs_tar_in_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  {
+    int r;
+
+    r = guestfs__send_file_sync (g, tarfile);
+    if (r == -1) {
+      guestfs_set_ready (g);
+      return -1;
+    }
+    if (r == -2) /* daemon cancelled */
+      goto read_reply;
+  }
+
+ read_reply:
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, tar_in_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_tar_in");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TAR_IN, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct tar_out_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void tar_out_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct tar_out_ctx *ctx = (struct tar_out_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_tar_out");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_tar_out");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_tar_out");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_tar_out (guestfs_h *g,
+               const char *directory,
+               const char *tarfile)
+{
+  struct guestfs_tar_out_args args;
+  struct tar_out_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_tar_out") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.directory = (char *) directory;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TAR_OUT,
+        (xdrproc_t) xdr_guestfs_tar_out_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, tar_out_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_tar_out");
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DOWNLOAD, serial) == -1)
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TAR_OUT, serial) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
 
   if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
     error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
     return -1;
   }
 
-  if (guestfs__receive_file_sync (ml, g, filename) == -1)
+  if (guestfs__receive_file_sync (g, tarfile) == -1) {
+    guestfs_set_ready (g);
     return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct tgz_in_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void tgz_in_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct tgz_in_ctx *ctx = (struct tgz_in_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_tgz_in");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_tgz_in");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_tgz_in");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_tgz_in (guestfs_h *g,
+               const char *tarball,
+               const char *directory)
+{
+  struct guestfs_tgz_in_args args;
+  struct tgz_in_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_tgz_in") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.directory = (char *) directory;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TGZ_IN,
+        (xdrproc_t) xdr_guestfs_tgz_in_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  {
+    int r;
+
+    r = guestfs__send_file_sync (g, tarball);
+    if (r == -1) {
+      guestfs_set_ready (g);
+      return -1;
+    }
+    if (r == -2) /* daemon cancelled */
+      goto read_reply;
+  }
+
+ read_reply:
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, tgz_in_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_tgz_in");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TGZ_IN, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct tgz_out_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void tgz_out_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct tgz_out_ctx *ctx = (struct tgz_out_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_tgz_out");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_tgz_out");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_tgz_out");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_tgz_out (guestfs_h *g,
+               const char *directory,
+               const char *tarball)
+{
+  struct guestfs_tgz_out_args args;
+  struct tgz_out_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_tgz_out") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.directory = (char *) directory;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_TGZ_OUT,
+        (xdrproc_t) xdr_guestfs_tgz_out_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, tgz_out_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_tgz_out");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_TGZ_OUT, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (guestfs__receive_file_sync (g, tarball) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct mount_ro_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void mount_ro_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct mount_ro_ctx *ctx = (struct mount_ro_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mount_ro");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_mount_ro");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_mount_ro");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_mount_ro (guestfs_h *g,
+               const char *device,
+               const char *mountpoint)
+{
+  struct guestfs_mount_ro_args args;
+  struct mount_ro_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_mount_ro") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  args.mountpoint = (char *) mountpoint;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MOUNT_RO,
+        (xdrproc_t) xdr_guestfs_mount_ro_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, mount_ro_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_mount_ro");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNT_RO, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct mount_options_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void mount_options_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct mount_options_ctx *ctx = (struct mount_options_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mount_options");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_mount_options");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_mount_options");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_mount_options (guestfs_h *g,
+               const char *options,
+               const char *device,
+               const char *mountpoint)
+{
+  struct guestfs_mount_options_args args;
+  struct mount_options_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_mount_options") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.options = (char *) options;
+  args.device = (char *) device;
+  args.mountpoint = (char *) mountpoint;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MOUNT_OPTIONS,
+        (xdrproc_t) xdr_guestfs_mount_options_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, mount_options_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_mount_options");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNT_OPTIONS, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct mount_vfs_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void mount_vfs_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct mount_vfs_ctx *ctx = (struct mount_vfs_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mount_vfs");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_mount_vfs");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_mount_vfs");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_mount_vfs (guestfs_h *g,
+               const char *options,
+               const char *vfstype,
+               const char *device,
+               const char *mountpoint)
+{
+  struct guestfs_mount_vfs_args args;
+  struct mount_vfs_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_mount_vfs") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.options = (char *) options;
+  args.vfstype = (char *) vfstype;
+  args.device = (char *) device;
+  args.mountpoint = (char *) mountpoint;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MOUNT_VFS,
+        (xdrproc_t) xdr_guestfs_mount_vfs_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, mount_vfs_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_mount_vfs");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MOUNT_VFS, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct debug_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_debug_ret ret;
+};
+
+static void debug_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct debug_ctx *ctx = (struct debug_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_debug");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_debug");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_debug");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_debug_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_debug");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+char *guestfs_debug (guestfs_h *g,
+               const char *subcmd,
+               char * const* const extraargs)
+{
+  struct guestfs_debug_args args;
+  struct debug_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_debug") == -1) return NULL;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.subcmd = (char *) subcmd;
+  args.extraargs.extraargs_val = (char **) extraargs;
+  for (args.extraargs.extraargs_len = 0; extraargs[args.extraargs.extraargs_len]; args.extraargs.extraargs_len++) ;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_DEBUG,
+        (xdrproc_t) xdr_guestfs_debug_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, debug_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_debug");
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DEBUG, serial) == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.result; /* caller will free */
+}
+
+struct lvremove_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void lvremove_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct lvremove_ctx *ctx = (struct lvremove_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_lvremove");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_lvremove");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_lvremove");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_lvremove (guestfs_h *g,
+               const char *device)
+{
+  struct guestfs_lvremove_args args;
+  struct lvremove_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_lvremove") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_LVREMOVE,
+        (xdrproc_t) xdr_guestfs_lvremove_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, lvremove_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_lvremove");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_LVREMOVE, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct vgremove_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void vgremove_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct vgremove_ctx *ctx = (struct vgremove_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_vgremove");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_vgremove");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_vgremove");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_vgremove (guestfs_h *g,
+               const char *vgname)
+{
+  struct guestfs_vgremove_args args;
+  struct vgremove_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_vgremove") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.vgname = (char *) vgname;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_VGREMOVE,
+        (xdrproc_t) xdr_guestfs_vgremove_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, vgremove_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_vgremove");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_VGREMOVE, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct pvremove_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void pvremove_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct pvremove_ctx *ctx = (struct pvremove_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_pvremove");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_pvremove");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_pvremove");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_pvremove (guestfs_h *g,
+               const char *device)
+{
+  struct guestfs_pvremove_args args;
+  struct pvremove_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_pvremove") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_PVREMOVE,
+        (xdrproc_t) xdr_guestfs_pvremove_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, pvremove_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_pvremove");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PVREMOVE, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct set_e2label_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void set_e2label_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct set_e2label_ctx *ctx = (struct set_e2label_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_set_e2label");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_set_e2label");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_set_e2label");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_set_e2label (guestfs_h *g,
+               const char *device,
+               const char *label)
+{
+  struct guestfs_set_e2label_args args;
+  struct set_e2label_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_set_e2label") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  args.label = (char *) label;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_SET_E2LABEL,
+        (xdrproc_t) xdr_guestfs_set_e2label_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, set_e2label_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_set_e2label");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SET_E2LABEL, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct get_e2label_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_get_e2label_ret ret;
+};
+
+static void get_e2label_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct get_e2label_ctx *ctx = (struct get_e2label_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_get_e2label");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_get_e2label");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_get_e2label");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_get_e2label_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_get_e2label");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+char *guestfs_get_e2label (guestfs_h *g,
+               const char *device)
+{
+  struct guestfs_get_e2label_args args;
+  struct get_e2label_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_get_e2label") == -1) return NULL;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_GET_E2LABEL,
+        (xdrproc_t) xdr_guestfs_get_e2label_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, get_e2label_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_get_e2label");
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_GET_E2LABEL, serial) == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.label; /* caller will free */
+}
+
+struct set_e2uuid_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void set_e2uuid_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct set_e2uuid_ctx *ctx = (struct set_e2uuid_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_set_e2uuid");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_set_e2uuid");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_set_e2uuid");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_set_e2uuid (guestfs_h *g,
+               const char *device,
+               const char *uuid)
+{
+  struct guestfs_set_e2uuid_args args;
+  struct set_e2uuid_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_set_e2uuid") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  args.uuid = (char *) uuid;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_SET_E2UUID,
+        (xdrproc_t) xdr_guestfs_set_e2uuid_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, set_e2uuid_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_set_e2uuid");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_SET_E2UUID, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct get_e2uuid_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_get_e2uuid_ret ret;
+};
+
+static void get_e2uuid_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct get_e2uuid_ctx *ctx = (struct get_e2uuid_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_get_e2uuid");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_get_e2uuid");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_get_e2uuid");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_get_e2uuid_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_get_e2uuid");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+char *guestfs_get_e2uuid (guestfs_h *g,
+               const char *device)
+{
+  struct guestfs_get_e2uuid_args args;
+  struct get_e2uuid_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_get_e2uuid") == -1) return NULL;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_GET_E2UUID,
+        (xdrproc_t) xdr_guestfs_get_e2uuid_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, get_e2uuid_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_get_e2uuid");
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_GET_E2UUID, serial) == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.uuid; /* caller will free */
+}
+
+struct fsck_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_fsck_ret ret;
+};
+
+static void fsck_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct fsck_ctx *ctx = (struct fsck_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_fsck");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_fsck");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_fsck");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_fsck_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_fsck");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_fsck (guestfs_h *g,
+               const char *fstype,
+               const char *device)
+{
+  struct guestfs_fsck_args args;
+  struct fsck_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_fsck") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.fstype = (char *) fstype;
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_FSCK,
+        (xdrproc_t) xdr_guestfs_fsck_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, fsck_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_fsck");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_FSCK, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.status;
+}
+
+struct zero_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void zero_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct zero_ctx *ctx = (struct zero_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_zero");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_zero");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_zero");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_zero (guestfs_h *g,
+               const char *device)
+{
+  struct guestfs_zero_args args;
+  struct zero_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_zero") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_ZERO,
+        (xdrproc_t) xdr_guestfs_zero_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, zero_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_zero");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_ZERO, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct grub_install_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void grub_install_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct grub_install_ctx *ctx = (struct grub_install_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_grub_install");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_grub_install");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_grub_install");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_grub_install (guestfs_h *g,
+               const char *root,
+               const char *device)
+{
+  struct guestfs_grub_install_args args;
+  struct grub_install_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_grub_install") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.root = (char *) root;
+  args.device = (char *) device;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_GRUB_INSTALL,
+        (xdrproc_t) xdr_guestfs_grub_install_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, grub_install_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_grub_install");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_GRUB_INSTALL, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct cp_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void cp_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct cp_ctx *ctx = (struct cp_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_cp");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_cp");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_cp");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_cp (guestfs_h *g,
+               const char *src,
+               const char *dest)
+{
+  struct guestfs_cp_args args;
+  struct cp_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_cp") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.src = (char *) src;
+  args.dest = (char *) dest;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CP,
+        (xdrproc_t) xdr_guestfs_cp_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, cp_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_cp");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CP, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct cp_a_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void cp_a_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct cp_a_ctx *ctx = (struct cp_a_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_cp_a");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_cp_a");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_cp_a");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_cp_a (guestfs_h *g,
+               const char *src,
+               const char *dest)
+{
+  struct guestfs_cp_a_args args;
+  struct cp_a_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_cp_a") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.src = (char *) src;
+  args.dest = (char *) dest;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_CP_A,
+        (xdrproc_t) xdr_guestfs_cp_a_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, cp_a_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_cp_a");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CP_A, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct mv_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void mv_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct mv_ctx *ctx = (struct mv_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_mv");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_mv");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_mv");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_mv (guestfs_h *g,
+               const char *src,
+               const char *dest)
+{
+  struct guestfs_mv_args args;
+  struct mv_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_mv") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.src = (char *) src;
+  args.dest = (char *) dest;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_MV,
+        (xdrproc_t) xdr_guestfs_mv_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, mv_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_mv");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MV, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct drop_caches_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void drop_caches_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct drop_caches_ctx *ctx = (struct drop_caches_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_drop_caches");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_drop_caches");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_drop_caches");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_drop_caches (guestfs_h *g,
+               int whattodrop)
+{
+  struct guestfs_drop_caches_args args;
+  struct drop_caches_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_drop_caches") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  args.whattodrop = whattodrop;
+  serial = guestfs__send_sync (g, GUESTFS_PROC_DROP_CACHES,
+        (xdrproc_t) xdr_guestfs_drop_caches_args, (char *) &args);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, drop_caches_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_drop_caches");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DROP_CACHES, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs_set_ready (g);
+  return 0;
+}
+
+struct dmesg_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+  struct guestfs_dmesg_ret ret;
+};
+
+static void dmesg_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct dmesg_ctx *ctx = (struct dmesg_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_dmesg");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_dmesg");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_dmesg");
+      return;
+    }
+    goto done;
+  }
+  if (!xdr_guestfs_dmesg_ret (xdr, &ctx->ret)) {
+    error (g, "%s: failed to parse reply", "guestfs_dmesg");
+    return;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+char *guestfs_dmesg (guestfs_h *g)
+{
+  struct dmesg_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_dmesg") == -1) return NULL;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  serial = guestfs__send_sync (g, GUESTFS_PROC_DMESG, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, dmesg_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_dmesg");
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_DMESG, serial) == -1) {
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return NULL;
+  }
+
+  guestfs_set_ready (g);
+  return ctx.ret.kmsgs; /* caller will free */
+}
+
+struct ping_daemon_ctx {
+  /* This flag is set by the callbacks, so we know we've done
+   * the callbacks as expected, and in the right sequence.
+   * 0 = not called, 1 = reply_cb called.
+   */
+  int cb_sequence;
+  struct guestfs_message_header hdr;
+  struct guestfs_message_error err;
+};
+
+static void ping_daemon_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  struct ping_daemon_ctx *ctx = (struct ping_daemon_ctx *) data;
+
+  /* This should definitely not happen. */
+  if (ctx->cb_sequence != 0) {
+    ctx->cb_sequence = 9999;
+    error (g, "%s: internal error: reply callback called twice", "guestfs_ping_daemon");
+    return;
+  }
+
+  ml->main_loop_quit (ml, g);
+
+  if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+    error (g, "%s: failed to parse reply header", "guestfs_ping_daemon");
+    return;
+  }
+  if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+    if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+      error (g, "%s: failed to parse reply error", "guestfs_ping_daemon");
+      return;
+    }
+    goto done;
+  }
+ done:
+  ctx->cb_sequence = 1;
+}
+
+int guestfs_ping_daemon (guestfs_h *g)
+{
+  struct ping_daemon_ctx ctx;
+  guestfs_main_loop *ml = guestfs_get_main_loop (g);
+  int serial;
+
+  if (check_state (g, "guestfs_ping_daemon") == -1) return -1;
+  guestfs_set_busy (g);
+
+  memset (&ctx, 0, sizeof ctx);
+
+  serial = guestfs__send_sync (g, GUESTFS_PROC_PING_DAEMON, NULL, NULL);
+  if (serial == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  guestfs__switch_to_receiving (g);
+  ctx.cb_sequence = 0;
+  guestfs_set_reply_callback (g, ping_daemon_reply_cb, &ctx);
+  (void) ml->main_loop_run (ml, g);
+  guestfs_set_reply_callback (g, NULL, NULL);
+  if (ctx.cb_sequence != 1) {
+    error (g, "%s reply failed, see earlier error messages", "guestfs_ping_daemon");
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_PING_DAEMON, serial) == -1) {
+    guestfs_set_ready (g);
+    return -1;
+  }
+
+  if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+    error (g, "%s", ctx.err.error_message);
+    guestfs_set_ready (g);
+    return -1;
+  }
 
+  guestfs_set_ready (g);
   return 0;
 }