#define xdr_uint32_t xdr_u_int32_t
#endif
-/* Also in guestfsd.c */
+/* Network configuration of the appliance. Note these addresses are
+ * only meaningful within the context of the running appliance. QEMU
+ * translates network connections to these magic addresses into
+ * userspace calls on the host (eg. connect(2)). qemu-doc has a nice
+ * diagram which is also useful to refer to.
+ *
+ * NETWORK: The network.
+ *
+ * ROUTER: The address of the "host", ie. this library.
+ *
+ * [Note: If you change NETWORK and ROUTER then you also have to
+ * change the network configuration in appliance/init].
+ *
+ * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
+ * magically connects this pseudo-address to the guestfwd channel. In
+ * typical Linux configurations of libguestfs, guestfwd is not
+ * actually used any more.
+ */
+#define NETWORK "10.0.2.0/8"
+#define ROUTER "10.0.2.2"
#define GUESTFWD_ADDR "10.0.2.4"
#define GUESTFWD_PORT "6666"
return -1;
}
- if (qemu_supports (g, NULL) == -1)
- return -1;
-
- /* Only SCSI and virtio drivers support readonly mode.
- * This is only supported as a QEMU feature since 2010/01.
- */
- int supports_ro = 0;
- if ((STREQ (drive_if, "scsi") || STREQ (drive_if, "virtio")) &&
- qemu_supports (g, "readonly=on"))
- supports_ro = 1;
-
- size_t len = strlen (filename) + 100;
+ size_t len = strlen (filename) + 64;
char buf[len];
- snprintf (buf, len, "file=%s,snapshot=on,%sif=%s",
- filename,
- supports_ro ? "readonly=on," : "",
- drive_if);
+ snprintf (buf, len, "file=%s,snapshot=on,if=%s", filename, drive_if);
return guestfs__config (g, "-drive", buf);
}
char unixsock[256];
struct sockaddr_un addr;
+ /* Configured? */
+ if (!g->cmdline) {
+ error (g, _("you must call guestfs_add_drive before guestfs_launch"));
+ return -1;
+ }
+
+ if (g->state != CONFIG) {
+ error (g, _("the libguestfs handle has already been launched"));
+ return -1;
+ }
+
/* Start the clock ... */
gettimeofday (&g->launch_t, NULL);
+ /* Make the temporary directory. */
#ifdef P_tmpdir
tmpdir = P_tmpdir;
#else
tmpdir = getenv ("TMPDIR") ? : tmpdir;
snprintf (dir_template, sizeof dir_template, "%s/libguestfsXXXXXX", tmpdir);
- /* Configured? */
- if (!g->cmdline) {
- error (g, _("you must call guestfs_add_drive before guestfs_launch"));
- return -1;
- }
-
- if (g->state != CONFIG) {
- error (g, _("qemu has already been launched"));
- return -1;
- }
-
- /* Make the temporary directory. */
if (!g->tmpdir) {
g->tmpdir = safe_strdup (g, dir_template);
if (mkdtemp (g->tmpdir) == NULL) {
*/
if (null_vmchannel_sock) {
add_cmdline (g, "-net");
- add_cmdline (g, "user,vlan=0,net=10.0.2.0/8");
+ add_cmdline (g, "user,vlan=0,net=" NETWORK);
snprintf (buf, sizeof buf,
- "guestfs_vmchannel=tcp:10.0.2.2:%d", null_vmchannel_sock);
+ "guestfs_vmchannel=tcp:" ROUTER ":%d",
+ null_vmchannel_sock);
vmchannel = strdup (buf);
}
add_cmdline (g, buf);
snprintf (buf, sizeof buf,
- "user,vlan=0,net=10.0.2.0/8,"
+ "user,vlan=0,net=" NETWORK ","
"guestfwd=tcp:" GUESTFWD_ADDR ":" GUESTFWD_PORT
"-chardev:guestfsvmc");
add_cmdline (g, "-net");
add_cmdline (g, buf);
add_cmdline (g, "-net");
- add_cmdline (g, "user,vlan=0,net=10.0.2.0/8");
+ add_cmdline (g, "user,vlan=0,net=" NETWORK);
vmchannel = "guestfs_vmchannel=tcp:" GUESTFWD_ADDR ":" GUESTFWD_PORT;
}
close (wfd[1]);
close (rfd[0]);
}
- kill (g->pid, 9);
+ 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);
if (g->verbose)
fprintf (stderr, "sending SIGTERM to process %d\n", g->pid);
- kill (g->pid, SIGTERM);
+ if (g->pid > 0) kill (g->pid, SIGTERM);
if (g->recoverypid > 0) kill (g->recoverypid, 9);
return 0;
if (g->verbose)
fprintf (stderr, "child_cleanup: %p: child process died\n", g);
- /*kill (g->pid, SIGTERM);*/
+ /*if (g->pid > 0) 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);
}
static int
-read_log_message_or_eof (guestfs_h *g, int fd)
+read_log_message_or_eof (guestfs_h *g, int fd, int error_if_eof)
{
char buf[BUFSIZ];
int n;
if (n == 0) {
/* Hopefully this indicates the qemu child process has died. */
child_cleanup (g);
+
+ if (error_if_eof) {
+ /* We weren't expecting eof here (called from launch) so place
+ * something in the error buffer. RHBZ#588851.
+ */
+ error (g, "child process died unexpectedly");
+ }
return -1;
}
}
if (FD_ISSET (g->fd[1], &rset2)) {
- if (read_log_message_or_eof (g, g->fd[1]) == -1)
+ if (read_log_message_or_eof (g, g->fd[1], 0) == -1)
return -1;
}
if (FD_ISSET (g->sock, &rset2)) {
}
if (FD_ISSET (g->fd[1], &rset2)) {
- if (read_log_message_or_eof (g, g->fd[1]) == -1) {
+ if (read_log_message_or_eof (g, g->fd[1], 0) == -1) {
free (*buf_rtn);
*buf_rtn = NULL;
return -1;
int sock = -1;
while (sock == -1) {
+ /* If the qemu process has died, clean up the zombie (RHBZ#579155).
+ * By partially polling in the select below we ensure that this
+ * function will be called eventually.
+ */
+ waitpid (g->pid, NULL, WNOHANG);
+
rset2 = rset;
- int r = select (max_fd+1, &rset2, NULL, NULL, NULL);
+
+ struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
+ int r = select (max_fd+1, &rset2, NULL, NULL, &tv);
if (r == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
}
if (FD_ISSET (g->fd[1], &rset2)) {
- if (read_log_message_or_eof (g, g->fd[1]) == -1)
+ if (read_log_message_or_eof (g, g->fd[1], 1) == -1)
return -1;
}
if (FD_ISSET (g->sock, &rset2)) {