2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
34 #include <sys/select.h>
38 #include <rpc/types.h>
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
49 #ifdef HAVE_SYS_WAIT_H
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
61 #include <arpa/inet.h>
62 #include <netinet/in.h>
65 #include "glthread/lock.h"
66 #include "ignore-value.h"
69 #include "guestfs-internal.h"
70 #include "guestfs-internal-actions.h"
71 #include "guestfs_protocol.h"
73 /* This is the code used to send and receive RPC messages and (for
74 * certain types of message) to perform file transfers. This code is
75 * driven from the generated actions (src/actions.c). There
76 * are five different cases to consider:
78 * (1) A non-daemon function. There is no RPC involved at all, it's
79 * all handled inside the library.
81 * (2) A simple RPC (eg. "mount"). We write the request, then read
82 * the reply. The sequence of calls is:
89 * (3) An RPC with FileOut parameters (eg. "upload"). We write the
90 * request, then write the file(s), then read the reply. The sequence
95 * guestfs___send_file (possibly multiple times)
99 * (4) An RPC with FileIn parameters (eg. "download"). We write the
100 * request, then read the reply, then read the file(s). The sequence
106 * guestfs___recv_file (possibly multiple times)
109 * (5) Both FileOut and FileIn parameters. There are no calls like
110 * this in the current API, but they would be implemented as a
111 * combination of cases (3) and (4).
113 * During all writes and reads, we also select(2) on qemu stdout
114 * looking for messages (guestfsd stderr and guest kernel dmesg), and
115 * anything received is passed up through the log_message_cb. This is
116 * also the reason why all the sockets are non-blocking. We also have
117 * to check for EOF (qemu died). All of this is handled by the
118 * functions send_to_daemon and recv_from_daemon.
122 xwrite (int fd, const void *v_buf, size_t len)
124 const char *buf = v_buf;
128 r = write (fd, buf, len);
140 guestfs___set_busy (guestfs_h *g)
142 if (g->state != READY) {
143 error (g, _("guestfs_set_busy: called when in state %d != READY"),
152 guestfs___end_busy (guestfs_h *g)
166 error (g, _("guestfs_end_busy: called when in state %d"), g->state);
172 /* This is called if we detect EOF, ie. qemu died. */
174 child_cleanup (guestfs_h *g)
177 fprintf (stderr, "child_cleanup: %p: child process died\n", g);
179 /*if (g->pid > 0) kill (g->pid, SIGTERM);*/
180 if (g->recoverypid > 0) kill (g->recoverypid, 9);
181 waitpid (g->pid, NULL, 0);
182 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
191 memset (&g->launch_t, 0, sizeof g->launch_t);
193 if (g->subprocess_quit_cb)
194 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
198 read_log_message_or_eof (guestfs_h *g, int fd, int error_if_eof)
206 "read_log_message_or_eof: %p g->state = %d, fd = %d\n",
210 /* QEMU's console emulates a 16550A serial port. The real 16550A
211 * device has a small FIFO buffer (16 bytes) which means here we see
212 * lots of small reads of 1-16 bytes in length, usually single
215 n = read (fd, buf, sizeof buf);
217 /* Hopefully this indicates the qemu child process has died. */
221 /* We weren't expecting eof here (called from launch) so place
222 * something in the error buffer. RHBZ#588851.
224 error (g, "child process died unexpectedly");
230 if (errno == EINTR || errno == EAGAIN)
237 /* In verbose mode, copy all log messages to stderr. */
239 ignore_value (write (STDERR_FILENO, buf, n));
241 /* It's an actual log message, send it upwards if anyone is listening. */
242 if (g->log_message_cb)
243 g->log_message_cb (g, g->log_message_cb_data, buf, n);
248 /* Read 'n' bytes, setting the socket to blocking temporarily so
249 * that we really read the number of bytes requested.
250 * Returns: 0 == EOF while reading
251 * -1 == error, error() function has been called
252 * n == read 'n' bytes in full
255 really_read_from_socket (guestfs_h *g, int sock, char *buf, size_t n)
261 /* Set socket to blocking. */
262 flags = fcntl (sock, F_GETFL);
264 perrorf (g, "fcntl");
267 if (fcntl (sock, F_SETFL, flags & ~O_NONBLOCK) == -1) {
268 perrorf (g, "fcntl");
274 r = read (sock, &buf[got], n-got);
284 /* Restore original socket flags. */
285 if (fcntl (sock, F_SETFL, flags) == -1) {
286 perrorf (g, "fcntl");
290 return (ssize_t) got;
294 check_for_daemon_cancellation_or_eof (guestfs_h *g, int fd)
303 "check_for_daemon_cancellation_or_eof: %p g->state = %d, fd = %d\n",
306 n = really_read_from_socket (g, fd, buf, 4);
310 /* Hopefully this indicates the qemu child process has died. */
315 xdrmem_create (&xdr, buf, 4, XDR_DECODE);
316 xdr_uint32_t (&xdr, &flag);
319 if (flag != GUESTFS_CANCEL_FLAG) {
320 error (g, _("check_for_daemon_cancellation_or_eof: read 0x%x from daemon, expected 0x%x\n"),
321 flag, GUESTFS_CANCEL_FLAG);
328 /* This writes the whole N bytes of BUF to the daemon socket.
330 * If the whole write is successful, it returns 0.
331 * If there was an error, it returns -1.
332 * If the daemon sent a cancellation message, it returns -2.
334 * It also checks qemu stdout for log messages and passes those up
335 * through log_message_cb.
337 * It also checks for EOF (qemu died) and passes that up through the
338 * child_cleanup function above.
341 guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n)
343 const char *buf = v_buf;
349 "send_to_daemon: %p g->state = %d, n = %zu\n", g, g->state, n);
354 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
355 FD_SET (g->sock, &rset); /* Read socket for cancellation & EOF. */
356 FD_SET (g->sock, &wset); /* Write to socket to send the data. */
358 int max_fd = MAX (g->sock, g->fd[1]);
363 int r = select (max_fd+1, &rset2, &wset2, NULL, NULL);
365 if (errno == EINTR || errno == EAGAIN)
367 perrorf (g, "select");
371 if (FD_ISSET (g->fd[1], &rset2)) {
372 if (read_log_message_or_eof (g, g->fd[1], 0) == -1)
375 if (FD_ISSET (g->sock, &rset2)) {
376 r = check_for_daemon_cancellation_or_eof (g, g->sock);
380 if (FD_ISSET (g->sock, &wset2)) {
381 r = write (g->sock, buf, n);
383 if (errno == EINTR || errno == EAGAIN)
385 perrorf (g, "write");
386 if (errno == EPIPE) /* Disconnected from guest (RHBZ#508713). */
398 /* This reads a single message, file chunk, launch flag or
399 * cancellation flag from the daemon. If something was read, it
400 * returns 0, otherwise -1.
402 * Both size_rtn and buf_rtn must be passed by the caller as non-NULL.
404 * *size_rtn returns the size of the returned message or it may be
405 * GUESTFS_LAUNCH_FLAG or GUESTFS_CANCEL_FLAG.
407 * *buf_rtn is returned containing the message (if any) or will be set
408 * to NULL. *buf_rtn must be freed by the caller.
410 * It also checks qemu stdout for log messages and passes those up
411 * through log_message_cb.
413 * It also checks for EOF (qemu died) and passes that up through the
414 * child_cleanup function above.
416 * Progress notifications are handled transparently by this function.
417 * If the callback exists, it is called. The caller of this function
418 * will not see GUESTFS_PROGRESS_FLAG.
421 /* Size of guestfs_progress message on the wire. */
422 #define PROGRESS_MESSAGE_SIZE 24
425 guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn)
431 "recv_from_daemon: %p g->state = %d, size_rtn = %p, buf_rtn = %p\n",
432 g, g->state, size_rtn, buf_rtn);
436 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
437 FD_SET (g->sock, &rset); /* Read socket for data & EOF. */
439 int max_fd = MAX (g->sock, g->fd[1]);
445 /* nr is the size of the message, but we prime it as -4 because we
446 * have to read the message length word first.
451 ssize_t message_size =
452 *size_rtn != GUESTFS_PROGRESS_FLAG ?
453 *size_rtn : PROGRESS_MESSAGE_SIZE;
454 if (nr >= message_size)
458 int r = select (max_fd+1, &rset2, NULL, NULL, NULL);
460 if (errno == EINTR || errno == EAGAIN)
462 perrorf (g, "select");
468 if (FD_ISSET (g->fd[1], &rset2)) {
469 if (read_log_message_or_eof (g, g->fd[1], 0) == -1) {
475 if (FD_ISSET (g->sock, &rset2)) {
476 if (nr < 0) { /* Have we read the message length word yet? */
477 r = read (g->sock, lenbuf+nr+4, -nr);
479 if (errno == EINTR || errno == EAGAIN)
483 /* Under some circumstances we see "Connection reset by peer"
484 * here when the child dies suddenly. Catch this and call
485 * the cleanup function, same as for EOF.
487 if (err == ECONNRESET)
492 error (g, _("unexpected end of file when reading from daemon"));
498 if (nr < 0) /* Still not got the whole length word. */
502 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
503 xdr_uint32_t (&xdr, size_rtn);
506 /* *size_rtn changed, recalculate message_size */
508 *size_rtn != GUESTFS_PROGRESS_FLAG ?
509 *size_rtn : PROGRESS_MESSAGE_SIZE;
511 if (*size_rtn == GUESTFS_LAUNCH_FLAG) {
512 if (g->state != LAUNCHING)
513 error (g, _("received magic signature from guestfsd, but in state %d"),
517 if (g->launch_done_cb)
518 g->launch_done_cb (g, g->launch_done_cb_data);
522 else if (*size_rtn == GUESTFS_CANCEL_FLAG)
524 else if (*size_rtn == GUESTFS_PROGRESS_FLAG)
526 /* If this happens, it's pretty bad and we've probably lost
529 else if (*size_rtn > GUESTFS_MESSAGE_MAX) {
530 error (g, _("message length (%u) > maximum possible size (%d)"),
531 (unsigned) *size_rtn, GUESTFS_MESSAGE_MAX);
535 /* Allocate the complete buffer, size now known. */
536 *buf_rtn = safe_malloc (g, message_size);
540 size_t sizetoread = message_size - nr;
541 if (sizetoread > BUFSIZ) sizetoread = BUFSIZ;
543 r = read (g->sock, (char *) (*buf_rtn) + nr, sizetoread);
545 if (errno == EINTR || errno == EAGAIN)
553 error (g, _("unexpected end of file when reading from daemon"));
563 /* Got the full message, caller can start processing it. */
564 #ifdef ENABLE_PACKET_DUMP
568 for (i = 0; i < nr; i += 16) {
569 printf ("%04zx: ", i);
570 for (j = i; j < MIN (i+16, nr); ++j)
571 printf ("%02x ", (*(unsigned char **)buf_rtn)[j]);
572 for (; j < i+16; ++j)
575 for (j = i; j < MIN (i+16, nr); ++j)
576 if (c_isprint ((*(char **)buf_rtn)[j]))
577 printf ("%c", (*(char **)buf_rtn)[j]);
580 for (; j < i+16; ++j)
587 if (*size_rtn == GUESTFS_PROGRESS_FLAG) {
588 if (g->state == BUSY && g->progress_cb) {
589 guestfs_progress message;
591 xdrmem_create (&xdr, *buf_rtn, PROGRESS_MESSAGE_SIZE, XDR_DECODE);
592 xdr_guestfs_progress (&xdr, &message);
595 g->progress_cb (g, g->progress_cb_data,
596 message.proc, message.serial,
597 message.position, message.total);
603 /* Process next message. */
604 return guestfs___recv_from_daemon (g, size_rtn, buf_rtn);
610 /* This is very much like recv_from_daemon above, but g->sock is
611 * a listening socket and we are accepting a new connection on
612 * that socket instead of reading anything. Returns the newly
616 guestfs___accept_from_daemon (guestfs_h *g)
622 "accept_from_daemon: %p g->state = %d\n", g, g->state);
626 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
627 FD_SET (g->sock, &rset); /* Read socket for accept. */
629 int max_fd = MAX (g->sock, g->fd[1]);
633 /* If the qemu process has died, clean up the zombie (RHBZ#579155).
634 * By partially polling in the select below we ensure that this
635 * function will be called eventually.
637 waitpid (g->pid, NULL, WNOHANG);
641 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
642 int r = select (max_fd+1, &rset2, NULL, NULL, &tv);
644 if (errno == EINTR || errno == EAGAIN)
646 perrorf (g, "select");
650 if (FD_ISSET (g->fd[1], &rset2)) {
651 if (read_log_message_or_eof (g, g->fd[1], 1) == -1)
654 if (FD_ISSET (g->sock, &rset2)) {
655 sock = accept (g->sock, NULL, NULL);
657 if (errno == EINTR || errno == EAGAIN)
659 perrorf (g, "accept");
669 guestfs___send (guestfs_h *g, int proc_nr, uint64_t progress_hint,
670 xdrproc_t xdrp, char *args)
672 struct guestfs_message_header hdr;
675 int serial = g->msg_next_serial++;
680 if (g->state != BUSY) {
681 error (g, _("guestfs___send: state %d != BUSY"), g->state);
685 /* We have to allocate this message buffer on the heap because
686 * it is quite large (although will be mostly unused). We
687 * can't allocate it on the stack because in some environments
688 * we have quite limited stack space available, notably when
689 * running in the JVM.
691 msg_out = safe_malloc (g, GUESTFS_MESSAGE_MAX + 4);
692 xdrmem_create (&xdr, msg_out + 4, GUESTFS_MESSAGE_MAX, XDR_ENCODE);
694 /* Serialize the header. */
695 hdr.prog = GUESTFS_PROGRAM;
696 hdr.vers = GUESTFS_PROTOCOL_VERSION;
698 hdr.direction = GUESTFS_DIRECTION_CALL;
700 hdr.status = GUESTFS_STATUS_OK;
701 hdr.progress_hint = progress_hint;
702 hdr.optargs_bitmask = 0;
704 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
705 error (g, _("xdr_guestfs_message_header failed"));
709 /* Serialize the args. If any, because some message types
710 * have no parameters.
713 if (!(*xdrp) (&xdr, args)) {
714 error (g, _("dispatch failed to marshal args"));
719 /* Get the actual length of the message, resize the buffer to match
720 * the actual length, and write the length word at the beginning.
722 len = xdr_getpos (&xdr);
725 msg_out = safe_realloc (g, msg_out, len + 4);
726 msg_out_size = len + 4;
728 xdrmem_create (&xdr, msg_out, 4, XDR_ENCODE);
729 xdr_uint32_t (&xdr, &len);
732 r = guestfs___send_to_daemon (g, msg_out, msg_out_size);
733 if (r == -2) /* Ignore stray daemon cancellations. */
746 static int cancel = 0; /* XXX Implement file cancellation. */
747 static int send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t len);
748 static int send_file_data (guestfs_h *g, const char *buf, size_t len);
749 static int send_file_cancellation (guestfs_h *g);
750 static int send_file_complete (guestfs_h *g);
756 * -2 daemon cancelled (we must read the error message)
759 guestfs___send_file (guestfs_h *g, const char *filename)
761 char buf[GUESTFS_MAX_CHUNK_SIZE];
764 fd = open (filename, O_RDONLY);
766 perrorf (g, "open: %s", filename);
767 send_file_cancellation (g);
768 /* Daemon sees cancellation and won't reply, so caller can
774 /* Send file in chunked encoding. */
776 r = read (fd, buf, sizeof buf);
777 if (r == -1 && (errno == EINTR || errno == EAGAIN))
780 err = send_file_data (g, buf, r);
782 if (err == -2) /* daemon sent cancellation */
783 send_file_cancellation (g);
788 if (cancel) { /* cancel from either end */
789 send_file_cancellation (g);
794 perrorf (g, "read: %s", filename);
795 send_file_cancellation (g);
799 /* End of file, but before we send that, we need to close
800 * the file and check for errors.
802 if (close (fd) == -1) {
803 perrorf (g, "close: %s", filename);
804 send_file_cancellation (g);
808 return send_file_complete (g);
811 /* Send a chunk of file data. */
813 send_file_data (guestfs_h *g, const char *buf, size_t len)
815 return send_file_chunk (g, 0, buf, len);
818 /* Send a cancellation message. */
820 send_file_cancellation (guestfs_h *g)
822 return send_file_chunk (g, 1, NULL, 0);
825 /* Send a file complete chunk. */
827 send_file_complete (guestfs_h *g)
830 return send_file_chunk (g, 0, buf, 0);
834 send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t buflen)
843 if (g->state != BUSY) {
844 error (g, _("send_file_chunk: state %d != READY"), g->state);
848 /* Allocate the chunk buffer. Don't use the stack to avoid
849 * excessive stack usage and unnecessary copies.
851 msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
852 xdrmem_create (&xdr, msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
854 /* Serialize the chunk. */
855 chunk.cancel = cancel;
856 chunk.data.data_len = buflen;
857 chunk.data.data_val = (char *) buf;
859 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
860 error (g, _("xdr_guestfs_chunk failed (buf = %p, buflen = %zu)"),
866 len = xdr_getpos (&xdr);
869 /* Reduce the size of the outgoing message buffer to the real length. */
870 msg_out = safe_realloc (g, msg_out, len + 4);
871 msg_out_size = len + 4;
873 xdrmem_create (&xdr, msg_out, 4, XDR_ENCODE);
874 xdr_uint32_t (&xdr, &len);
876 r = guestfs___send_to_daemon (g, msg_out, msg_out_size);
878 /* Did the daemon send a cancellation message? */
881 fprintf (stderr, "got daemon cancellation\n");
897 /* Receive a reply. */
899 guestfs___recv (guestfs_h *g, const char *fn,
900 guestfs_message_header *hdr,
901 guestfs_message_error *err,
902 xdrproc_t xdrp, char *ret)
910 r = guestfs___recv_from_daemon (g, &size, &buf);
914 /* This can happen if a cancellation happens right at the end
915 * of us sending a FileIn parameter to the daemon. Discard. The
916 * daemon should send us an error message next.
918 if (size == GUESTFS_CANCEL_FLAG)
921 if (size == GUESTFS_LAUNCH_FLAG) {
922 error (g, "%s: received unexpected launch flag from daemon when expecting reply", fn);
926 xdrmem_create (&xdr, buf, size, XDR_DECODE);
928 if (!xdr_guestfs_message_header (&xdr, hdr)) {
929 error (g, "%s: failed to parse reply header", fn);
934 if (hdr->status == GUESTFS_STATUS_ERROR) {
935 if (!xdr_guestfs_message_error (&xdr, err)) {
936 error (g, "%s: failed to parse reply error", fn);
942 if (xdrp && ret && !xdrp (&xdr, ret)) {
943 error (g, "%s: failed to parse reply", fn);
955 /* Receive a file. */
957 /* Returns -1 = error, 0 = EOF, > 0 = more data */
958 static ssize_t receive_file_data (guestfs_h *g, void **buf);
961 guestfs___recv_file (guestfs_h *g, const char *filename)
966 fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
968 perrorf (g, "open: %s", filename);
972 /* Receive the file in chunked encoding. */
973 while ((r = receive_file_data (g, &buf)) > 0) {
974 if (xwrite (fd, buf, r) == -1) {
975 perrorf (g, "%s: write", filename);
983 error (g, _("%s: error in chunked encoding"), filename);
987 if (close (fd) == -1) {
988 perrorf (g, "close: %s", filename);
995 /* Send cancellation message to daemon, then wait until it
996 * cancels (just throwing away data).
1000 uint32_t flag = GUESTFS_CANCEL_FLAG;
1003 fprintf (stderr, "%s: waiting for daemon to acknowledge cancellation\n",
1006 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
1007 xdr_uint32_t (&xdr, &flag);
1010 if (xwrite (g->sock, fbuf, sizeof fbuf) == -1) {
1011 perrorf (g, _("write to daemon socket"));
1015 while (receive_file_data (g, NULL) > 0)
1016 ; /* just discard it */
1021 /* Receive a chunk of file data. */
1022 /* Returns -1 = error, 0 = EOF, > 0 = more data */
1024 receive_file_data (guestfs_h *g, void **buf_r)
1030 guestfs_chunk chunk;
1032 r = guestfs___recv_from_daemon (g, &len, &buf);
1034 error (g, _("receive_file_data: parse error in reply callback"));
1038 if (len == GUESTFS_LAUNCH_FLAG || len == GUESTFS_CANCEL_FLAG) {
1039 error (g, _("receive_file_data: unexpected flag received when reading file chunks"));
1043 memset (&chunk, 0, sizeof chunk);
1045 xdrmem_create (&xdr, buf, len, XDR_DECODE);
1046 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
1047 error (g, _("failed to parse file chunk"));
1052 /* After decoding, the original buffer is no longer used. */
1056 error (g, _("file receive cancelled by daemon"));
1057 free (chunk.data.data_val);
1061 if (chunk.data.data_len == 0) { /* end of transfer */
1062 free (chunk.data.data_val);
1066 if (buf_r) *buf_r = chunk.data.data_val;
1067 else free (chunk.data.data_val); /* else caller frees */
1069 return chunk.data.data_len;