For more information on states, see L<guestfs(3)>.");
+ ("end_busy", (RErr, []), -1, [NotInFish],
+ [],
+ "leave the busy state",
+ "\
+This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
+state as is. This is only used when implementing
+actions using the low-level API.
+
+For more information on states, see L<guestfs(3)>.");
+
]
let daemon_functions = [
name;
);
pr " if (serial == -1) {\n";
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
pr "\n";
pr " r = guestfs__send_file_sync (g, %s);\n" n;
pr " if (r == -1) {\n";
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr " if (r == -2) /* daemon cancelled */\n";
pr " guestfs_set_reply_callback (g, NULL, NULL);\n";
pr " if (ctx.cb_sequence != 1) {\n";
pr " error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
pr " if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
(String.uppercase shortname);
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
pr " if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
pr " error (g, \"%%s\", ctx.err.error_message);\n";
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
function
| FileOut n ->
pr " if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
pr " return %s;\n" error_code;
pr " }\n";
pr "\n";
| _ -> ()
) (snd style);
- pr " guestfs_set_ready (g);\n";
+ pr " guestfs_end_busy (g);\n";
(match fst style with
| RErr -> pr " return 0;\n"
return 0;
}
+int
+guestfs_end_busy (guestfs_h *g)
+{
+ switch (g->state)
+ {
+ case BUSY:
+ g->state = READY;
+ break;
+ case CONFIG:
+ case READY:
+ break;
+ case LAUNCHING:
+ case NO_HANDLE:
+ error (g, "guestfs_end_busy: called when in state %d", g->state);
+ return -1;
+ }
+ return 0;
+}
+
/* Structure-freeing functions. These rely on the fact that the
* structure format is identical to the XDR format. See note in
* generator.ml.
free (x);
}
+/* We don't know if stdout_event or sock_read_event will be the
+ * first to receive EOF if the qemu process dies. This function
+ * has the common cleanup code for both.
+ */
+static void
+child_cleanup (guestfs_h *g)
+{
+ if (g->verbose)
+ fprintf (stderr, "stdout_event: %p: child process died\n", g);
+ /*kill (g->pid, SIGTERM);*/
+ if (g->recoverypid > 0) kill (g->recoverypid, 9);
+ waitpid (g->pid, NULL, 0);
+ if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
+ if (g->stdout_watch >= 0)
+ g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
+ if (g->sock_watch >= 0)
+ g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
+ close (g->fd[0]);
+ close (g->fd[1]);
+ close (g->sock);
+ g->fd[0] = -1;
+ g->fd[1] = -1;
+ g->sock = -1;
+ g->pid = 0;
+ g->recoverypid = 0;
+ g->start_t = 0;
+ g->stdout_watch = -1;
+ g->sock_watch = -1;
+ g->state = CONFIG;
+ if (g->subprocess_quit_cb)
+ g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
+}
+
/* This function is called whenever qemu prints something on stdout.
* Qemu's stdout is also connected to the guest's serial console, so
* we see kernel messages here too.
n = read (fd, buf, sizeof buf);
if (n == 0) {
/* Hopefully this indicates the qemu child process has died. */
- if (g->verbose)
- fprintf (stderr, "stdout_event: %p: child process died\n", g);
- /*kill (g->pid, SIGTERM);*/
- if (g->recoverypid > 0) kill (g->recoverypid, 9);
- waitpid (g->pid, NULL, 0);
- if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
- if (g->stdout_watch >= 0)
- g->main_loop->remove_handle (g->main_loop, g, g->stdout_watch);
- if (g->sock_watch >= 0)
- g->main_loop->remove_handle (g->main_loop, g, g->sock_watch);
- close (g->fd[0]);
- close (g->fd[1]);
- close (g->sock);
- g->fd[0] = -1;
- g->fd[1] = -1;
- g->sock = -1;
- g->pid = 0;
- g->recoverypid = 0;
- g->start_t = 0;
- g->stdout_watch = -1;
- g->sock_watch = -1;
- g->state = CONFIG;
- if (g->subprocess_quit_cb)
- g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
+ child_cleanup (g);
return;
}
}
n = read (g->sock, g->msg_in + g->msg_in_size,
g->msg_in_allocated - g->msg_in_size);
- if (n == 0)
- /* Disconnected? Ignore it because stdout_watch will get called
- * and will do the cleanup.
- */
+ if (n == 0) {
+ /* Disconnected. */
+ child_cleanup (g);
return;
+ }
if (n == -1) {
if (errno != EINTR && errno != EAGAIN)