- /* Start the clock ... */
- time (&g->start_t);
-
- /* Close the other ends of the pipe. */
- close (wfd[0]);
- close (rfd[1]);
-
- if (fcntl (wfd[1], F_SETFL, O_NONBLOCK) == -1 ||
- fcntl (rfd[0], F_SETFL, O_NONBLOCK) == -1) {
- perrorf (g, "fcntl");
- goto cleanup1;
- }
-
- g->fd[0] = wfd[1]; /* stdin of child */
- g->fd[1] = rfd[0]; /* stdout of child */
-
- /* Open the Unix socket. The vmchannel implementation that got
- * merged with qemu sucks in a number of ways. Both ends do
- * connect(2), which means that no one knows what, if anything, is
- * connected to the other end, or if it becomes disconnected. Even
- * worse, we have to wait some indeterminate time for qemu to create
- * the socket and connect to it (which happens very early in qemu's
- * start-up), so any code that uses vmchannel is inherently racy.
- * Hence this silly loop.
- */
- g->sock = socket (AF_UNIX, SOCK_STREAM, 0);
- if (g->sock == -1) {
- perrorf (g, "socket");
- goto cleanup1;
- }
-
- if (fcntl (g->sock, F_SETFL, O_NONBLOCK) == -1) {
- perrorf (g, "fcntl");
- goto cleanup2;
- }
-
- addr.sun_family = AF_UNIX;
- strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
- addr.sun_path[UNIX_PATH_MAX-1] = '\0';
-
- tries = 100;
- while (tries > 0) {
- /* Always sleep at least once to give qemu a small chance to start up. */
- usleep (10000);
-
- r = connect (g->sock, (struct sockaddr *) &addr, sizeof addr);
- if ((r == -1 && errno == EINPROGRESS) || r == 0)
- goto connected;
-
- if (errno != ENOENT)
- perrorf (g, "connect");
- tries--;
- }
-
- error (g, "failed to connect to vmchannel socket");
- goto cleanup2;
-
- connected:
- /* Watch the file descriptors. */
- free (g->msg_in);
- g->msg_in = NULL;
- g->msg_in_size = g->msg_in_allocated = 0;
-
- free (g->msg_out);
- g->msg_out = NULL;
- g->msg_out_size = 0;
- g->msg_out_pos = 0;
-
- g->stdout_watch =
- g->main_loop->add_handle (g->main_loop, g, g->fd[1],
- GUESTFS_HANDLE_READABLE,
- stdout_event, NULL);
- if (g->stdout_watch == -1) {
- error (g, "could not watch qemu stdout");
- goto cleanup3;
- }
-
- if (guestfs__switch_to_receiving (g) == -1)
- goto cleanup3;
-
- g->state = LAUNCHING;
- return 0;
-
- cleanup3:
- 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);
-
- cleanup2:
- close (g->sock);
-
- cleanup1:
- close (wfd[1]);
- close (rfd[0]);
- kill (g->pid, 9);
- waitpid (g->pid, NULL, 0);
- 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;
-
- cleanup0:
- free (kernel);
- free (initrd);
- return -1;
-}
-
-static void
-finish_wait_ready (guestfs_h *g, void *vp)
-{
- if (g->verbose)
- fprintf (stderr, "finish_wait_ready called, %p, vp = %p\n", g, vp);
-
- *((int *)vp) = 1;
- g->main_loop->main_loop_quit (g->main_loop, g);
-}
-
-int
-guestfs_wait_ready (guestfs_h *g)
-{
- int finished = 0, r;
-
- if (g->state == READY) return 0;
-
- if (g->state == BUSY) {
- error (g, "qemu has finished launching already");
- return -1;
- }
-
- if (g->state != LAUNCHING) {
- error (g, "qemu has not been launched yet");
- return -1;
- }
-
- g->launch_done_cb = finish_wait_ready;
- g->launch_done_cb_data = &finished;
- r = g->main_loop->main_loop_run (g->main_loop, g);
- g->launch_done_cb = NULL;
- g->launch_done_cb_data = NULL;
-
- if (r == -1) return -1;
-
- if (finished != 1) {
- error (g, "guestfs_wait_ready failed, see earlier error messages");
- return -1;
- }
-
- /* This is possible in some really strange situations, such as
- * guestfsd starts up OK but then qemu immediately exits. Check for
- * it because the caller is probably expecting to be able to send
- * commands after this function returns.
- */
- if (g->state != READY) {
- error (g, "qemu launched and contacted daemon, but state != READY");
- return -1;
- }
-
- return 0;
-}