- if (fcntl (sock, F_SETFL, O_NONBLOCK) == -1) {
- perrorf (g, "fcntl");
- goto cleanup1;
- }
-
- close (g->sock);
- g->sock = sock;
- } else {
- /* Other vmchannel. 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 cleanup1;
- }
-
- addr.sun_family = AF_UNIX;
- strncpy (addr.sun_path, unixsock, UNIX_PATH_MAX);
- addr.sun_path[UNIX_PATH_MAX-1] = '\0';
-
- tries = 100;
- /* Always sleep at least once to give qemu a small chance to start up. */
- usleep (10000);
- while (tries > 0) {
- 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--;
- usleep (100000);
- }
-
- error (g, _("failed to connect to vmchannel socket"));
- goto cleanup1;
-
- connected: ;
- }
-
- g->state = LAUNCHING;
-
- /* Wait for qemu to start and to connect back to us via vmchannel and
- * send the GUESTFS_LAUNCH_FLAG message.
- */
- uint32_t size;
- void *buf = NULL;
- r = recv_from_daemon (g, &size, &buf);
- free (buf);
-
- if (r == -1) return -1;
-
- if (size != GUESTFS_LAUNCH_FLAG) {
- error (g, _("guestfs_launch failed, see earlier error messages"));
- goto cleanup1;
- }
-
- if (g->verbose)
- print_timestamped_message (g, "appliance is up");
-
- /* 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"));
- goto cleanup1;
- }
-
- return 0;
-
- cleanup1:
- if (!g->direct) {
- close (wfd[1]);
- close (rfd[0]);
- }
- if (g->pid > 0) kill (g->pid, 9);
- if (g->recoverypid > 0) kill (g->recoverypid, 9);
- waitpid (g->pid, NULL, 0);
- if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
- g->fd[0] = -1;
- g->fd[1] = -1;
- g->pid = 0;
- g->recoverypid = 0;
- memset (&g->launch_t, 0, sizeof g->launch_t);
-
- cleanup0:
- if (g->sock >= 0) {
- close (g->sock);
- g->sock = -1;
- }
- g->state = CONFIG;
- free (kernel);
- free (initrd);
- return -1;
-}
-
-/* This function is used to print the qemu command line before it gets
- * executed, when in verbose mode.
- */
-static void
-print_cmdline (guestfs_h *g)
-{
- int i = 0;
- int needs_quote;
-
- while (g->cmdline[i]) {
- if (g->cmdline[i][0] == '-') /* -option starts a new line */
- fprintf (stderr, " \\\n ");
-
- if (i > 0) fputc (' ', stderr);
-
- /* Does it need shell quoting? This only deals with simple cases. */
- needs_quote = strcspn (g->cmdline[i], " ") != strlen (g->cmdline[i]);
-
- if (needs_quote) fputc ('\'', stderr);
- fprintf (stderr, "%s", g->cmdline[i]);
- if (needs_quote) fputc ('\'', stderr);
- i++;
- }
-
- fputc ('\n', stderr);
-}
-
-/* This function does the hard work of building the supermin appliance
- * on the fly. 'path' is the directory containing the control files.
- * 'kernel' and 'initrd' are where we will return the names of the
- * kernel and initrd (only initrd is built). The work is done by
- * an external script. We just tell it where to put the result.
- */
-static int
-build_supermin_appliance (guestfs_h *g, const char *path,
- char **kernel, char **initrd)
-{
- char cmd[4096];
- int r, len;
-
- if (g->verbose)
- print_timestamped_message (g, "begin building supermin appliance");
-
- len = strlen (g->tmpdir);
- *kernel = safe_malloc (g, len + 8);
- snprintf (*kernel, len+8, "%s/kernel", g->tmpdir);
- *initrd = safe_malloc (g, len + 8);
- snprintf (*initrd, len+8, "%s/initrd", g->tmpdir);
-
- snprintf (cmd, sizeof cmd,
- "PATH='%s':$PATH "
- "libguestfs-supermin-helper%s '%s' " host_cpu " " REPO " %s %s",
- path,
- g->verbose ? " --verbose" : "",
- path, *kernel, *initrd);
- if (g->verbose)
- print_timestamped_message (g, "%s", cmd);
-
- r = system (cmd);
- if (r == -1 || WEXITSTATUS(r) != 0) {
- error (g, _("external command failed: %s"), cmd);
- free (*kernel);
- free (*initrd);
- *kernel = *initrd = NULL;
- return -1;
- }
-
- if (g->verbose)
- print_timestamped_message (g, "finished building supermin appliance");
-
- return 0;
-}
-
-/* Compute Y - X and return the result in milliseconds.
- * Approximately the same as this code:
- * http://www.mpp.mpg.de/~huber/util/timevaldiff.c
- */
-static int64_t
-timeval_diff (const struct timeval *x, const struct timeval *y)
-{
- int64_t msec;
-
- msec = (y->tv_sec - x->tv_sec) * 1000;
- msec += (y->tv_usec - x->tv_usec) / 1000;
- return msec;
-}
-
-static void
-print_timestamped_message (guestfs_h *g, const char *fs, ...)
-{
- va_list args;
- char *msg;
- int err;
- struct timeval tv;
-
- va_start (args, fs);
- err = vasprintf (&msg, fs, args);
- va_end (args);
-
- if (err < 0) return;
-
- gettimeofday (&tv, NULL);
-
- fprintf (stderr, "[%05" PRIi64 "ms] %s\n",
- timeval_diff (&g->launch_t, &tv), msg);
-
- free (msg);
-}
-
-static int read_all (guestfs_h *g, FILE *fp, char **ret);
-
-/* Test qemu binary (or wrapper) runs, and do 'qemu -help' and
- * 'qemu -version' so we know what options this qemu supports and
- * the version.
- */
-static int
-test_qemu (guestfs_h *g)
-{
- char cmd[1024];
- FILE *fp;
-
- snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -help", g->qemu);
-
- fp = popen (cmd, "r");
- /* qemu -help should always work (qemu -version OTOH wasn't
- * supported by qemu 0.9). If this command doesn't work then it
- * probably indicates that the qemu binary is missing.
- */
- if (!fp) {
- /* XXX This error is never printed, even if the qemu binary
- * doesn't exist. Why?
- */
- error:
- perrorf (g, _("%s: command failed: If qemu is located on a non-standard path, try setting the LIBGUESTFS_QEMU environment variable."), cmd);
- return -1;
- }
-
- if (read_all (g, fp, &g->qemu_help) == -1)
- goto error;
-
- if (pclose (fp) == -1)
- goto error;
-
- snprintf (cmd, sizeof cmd, "LC_ALL=C '%s' -nographic -version 2>/dev/null",
- g->qemu);
-
- fp = popen (cmd, "r");
- if (fp) {
- /* Intentionally ignore errors. */
- read_all (g, fp, &g->qemu_version);
- pclose (fp);
- }
-
- return 0;
-}
-
-static int
-read_all (guestfs_h *g, FILE *fp, char **ret)
-{
- int r, n = 0;
- char *p;
-
- again:
- if (feof (fp)) {
- *ret = safe_realloc (g, *ret, n + 1);
- (*ret)[n] = '\0';
- return n;
- }
-
- *ret = safe_realloc (g, *ret, n + BUFSIZ);
- p = &(*ret)[n];
- r = fread (p, 1, BUFSIZ, fp);
- if (ferror (fp)) {
- perrorf (g, "read");
- return -1;
- }
- n += r;
- goto again;
-}
-
-/* Test if option is supported by qemu command line (just by grepping
- * the help text).
- *
- * The first time this is used, it has to run the external qemu
- * binary. If that fails, it returns -1.
- *
- * To just do the first-time run of the qemu binary, call this with
- * option == NULL, in which case it will return -1 if there was an
- * error doing that.
- */
-static int
-qemu_supports (guestfs_h *g, const char *option)
-{
- if (!g->qemu_help) {
- if (test_qemu (g) == -1)
- return -1;
- }
-
- if (option == NULL)
- return 1;
-
- return strstr (g->qemu_help, option) != NULL;
-}
-
-/* Check if a file can be opened. */
-static int
-is_openable (guestfs_h *g, const char *path, int flags)
-{
- int fd = open (path, flags);
- if (fd == -1) {
- if (g->verbose)
- perror (path);
- return 0;
- }
- close (fd);
- return 1;
-}
-
-/* Check the peer effective UID for a TCP socket. Ideally we'd like
- * SO_PEERCRED for a loopback TCP socket. This isn't possible on
- * Linux (but it is on Solaris!) so we read /proc/net/tcp instead.
- */
-static int
-check_peer_euid (guestfs_h *g, int sock, uid_t *rtn)
-{
-#if CAN_CHECK_PEER_EUID
- struct sockaddr_in peer;
- socklen_t addrlen = sizeof peer;
-
- if (getpeername (sock, (struct sockaddr *) &peer, &addrlen) == -1) {
- perrorf (g, "getpeername");
- return -1;
- }
-
- if (peer.sin_family != AF_INET ||
- ntohl (peer.sin_addr.s_addr) != INADDR_LOOPBACK) {
- error (g, "check_peer_euid: unexpected connection from non-IPv4, non-loopback peer (family = %d, addr = %s)",
- peer.sin_family, inet_ntoa (peer.sin_addr));
- return -1;
- }
-
- struct sockaddr_in our;
- addrlen = sizeof our;
- if (getsockname (sock, (struct sockaddr *) &our, &addrlen) == -1) {
- perrorf (g, "getsockname");
- return -1;
- }
-
- FILE *fp = fopen ("/proc/net/tcp", "r");
- if (fp == NULL) {
- perrorf (g, "/proc/net/tcp");
- return -1;
- }
-
- char line[256];
- if (fgets (line, sizeof line, fp) == NULL) { /* Drop first line. */
- error (g, "unexpected end of file in /proc/net/tcp");
- fclose (fp);
- return -1;
- }
-
- while (fgets (line, sizeof line, fp) != NULL) {
- unsigned line_our_addr, line_our_port, line_peer_addr, line_peer_port;
- int dummy0, dummy1, dummy2, dummy3, dummy4, dummy5, dummy6;
- int line_uid;
-
- if (sscanf (line, "%d:%08X:%04X %08X:%04X %02X %08X:%08X %02X:%08X %08X %d",
- &dummy0,
- &line_our_addr, &line_our_port,
- &line_peer_addr, &line_peer_port,
- &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, &dummy6,
- &line_uid) == 12) {
- /* Note about /proc/net/tcp: local_address and rem_address are
- * always in network byte order. However the port part is
- * always in host byte order.
- *
- * The sockname and peername that we got above are in network
- * byte order. So we have to byte swap the port but not the
- * address part.
- */
- if (line_our_addr == our.sin_addr.s_addr &&
- line_our_port == ntohs (our.sin_port) &&
- line_peer_addr == peer.sin_addr.s_addr &&
- line_peer_port == ntohs (peer.sin_port)) {
- *rtn = line_uid;
- fclose (fp);
- return 0;
- }
- }
- }
-
- error (g, "check_peer_euid: no matching TCP connection found in /proc/net/tcp");
- fclose (fp);
- return -1;
-#else /* !CAN_CHECK_PEER_EUID */
- /* This function exists but should never be called in this
- * configuration.
- */
- abort ();
-#endif /* !CAN_CHECK_PEER_EUID */
-}
-
-/* You had to call this function after launch in versions <= 1.0.70,
- * but it is now a no-op.
- */
-int
-guestfs__wait_ready (guestfs_h *g)
-{
- if (g->state != READY) {
- error (g, _("qemu has not been launched yet"));
- return -1;
- }
-
- return 0;
-}
-
-int
-guestfs__kill_subprocess (guestfs_h *g)
-{
- if (g->state == CONFIG) {
- error (g, _("no subprocess to kill"));
- return -1;
- }
-
- if (g->verbose)
- fprintf (stderr, "sending SIGTERM to process %d\n", g->pid);
-
- if (g->pid > 0) kill (g->pid, SIGTERM);
- if (g->recoverypid > 0) kill (g->recoverypid, 9);
-
- return 0;
-}
-
-/* Access current state. */
-int
-guestfs__is_config (guestfs_h *g)
-{
- return g->state == CONFIG;
-}
-
-int
-guestfs__is_launching (guestfs_h *g)
-{
- return g->state == LAUNCHING;
-}
-
-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;
-}
-
-void
-guestfs_set_log_message_callback (guestfs_h *g,
- guestfs_log_message_cb cb, void *opaque)
-{
- g->log_message_cb = cb;
- g->log_message_cb_data = opaque;
-}