- 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);*/
- waitpid (g->pid, NULL, 0);
- if (g->stdout_watch >= 0)
- main_loop.remove_handle (g, g->stdout_watch);
- if (g->sock_watch >= 0)
- main_loop.remove_handle (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->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);
- return;
- }
+int
+guestfs_is_ready (guestfs_h *g)
+{
+ return g->state == READY;
+}
+
+int
+guestfs_is_busy (guestfs_h *g)
+{
+ return g->state == BUSY;
+}
+
+int
+guestfs_get_state (guestfs_h *g)
+{
+ return g->state;
+}
+
+int
+guestfs_set_ready (guestfs_h *g)
+{
+ if (g->state != BUSY) {
+ error (g, _("guestfs_set_ready: called when in state %d != BUSY"),
+ g->state);
+ return -1;
+ }
+ g->state = READY;
+ return 0;
+}
+
+int
+guestfs_set_busy (guestfs_h *g)
+{
+ if (g->state != READY) {
+ error (g, _("guestfs_set_busy: called when in state %d != READY"),
+ g->state);
+ return -1;
+ }
+ g->state = BUSY;
+ 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.
+ */
+void
+guestfs_free_int_bool (struct guestfs_int_bool *x)
+{
+ free (x);
+}
+
+void
+guestfs_free_lvm_pv_list (struct guestfs_lvm_pv_list *x)
+{
+ xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_pv_list, (char *) x);
+ free (x);
+}
+
+void
+guestfs_free_lvm_vg_list (struct guestfs_lvm_vg_list *x)
+{
+ xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_vg_list, (char *) x);
+ free (x);
+}
+
+void
+guestfs_free_lvm_lv_list (struct guestfs_lvm_lv_list *x)
+{
+ xdr_free ((xdrproc_t) xdr_guestfs_lvm_int_lv_list, (char *) x);
+ 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.
+ */
+static void
+stdout_event (struct guestfs_main_loop *ml, guestfs_h *g, void *data,
+ int watch, int fd, int events)
+{
+ char buf[4096];
+ int n;
+
+#if 0
+ if (g->verbose)
+ fprintf (stderr,
+ "stdout_event: %p g->state = %d, fd = %d, events = 0x%x\n",
+ g, g->state, fd, events);
+#endif
+
+ if (g->fd[1] != fd) {
+ error (g, _("stdout_event: internal error: %d != %d"), g->fd[1], fd);
+ return;
+ }
+
+ n = read (fd, buf, sizeof buf);
+ if (n == 0) {
+ /* Hopefully this indicates the qemu child process has died. */
+ child_cleanup (g);
+ return;
+ }