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 /* Size of guestfs_progress message on the wire. */
74 #define PROGRESS_MESSAGE_SIZE 24
76 /* This is the code used to send and receive RPC messages and (for
77 * certain types of message) to perform file transfers. This code is
78 * driven from the generated actions (src/actions.c). There
79 * are five different cases to consider:
81 * (1) A non-daemon function. There is no RPC involved at all, it's
82 * all handled inside the library.
84 * (2) A simple RPC (eg. "mount"). We write the request, then read
85 * the reply. The sequence of calls is:
92 * (3) An RPC with FileOut parameters (eg. "upload"). We write the
93 * request, then write the file(s), then read the reply. The sequence
98 * guestfs___send_file (possibly multiple times)
102 * (4) An RPC with FileIn parameters (eg. "download"). We write the
103 * request, then read the reply, then read the file(s). The sequence
109 * guestfs___recv_file (possibly multiple times)
112 * (5) Both FileOut and FileIn parameters. There are no calls like
113 * this in the current API, but they would be implemented as a
114 * combination of cases (3) and (4).
116 * During all writes and reads, we also select(2) on qemu stdout
117 * looking for messages (guestfsd stderr and guest kernel dmesg), and
118 * anything received is passed up through the log_message_cb. This is
119 * also the reason why all the sockets are non-blocking. We also have
120 * to check for EOF (qemu died). All of this is handled by the
121 * functions send_to_daemon and recv_from_daemon.
125 xwrite (int fd, const void *v_buf, size_t len)
127 const char *buf = v_buf;
131 r = write (fd, buf, len);
143 guestfs___set_busy (guestfs_h *g)
145 if (g->state != READY) {
146 error (g, _("guestfs_set_busy: called when in state %d != READY"),
155 guestfs___end_busy (guestfs_h *g)
169 error (g, _("guestfs_end_busy: called when in state %d"), g->state);
175 /* This is called if we detect EOF, ie. qemu died. */
177 child_cleanup (guestfs_h *g)
180 fprintf (stderr, "child_cleanup: %p: child process died\n", g);
182 /*if (g->pid > 0) kill (g->pid, SIGTERM);*/
183 if (g->recoverypid > 0) kill (g->recoverypid, 9);
184 waitpid (g->pid, NULL, 0);
185 if (g->recoverypid > 0) waitpid (g->recoverypid, NULL, 0);
194 memset (&g->launch_t, 0, sizeof g->launch_t);
196 if (g->subprocess_quit_cb)
197 g->subprocess_quit_cb (g, g->subprocess_quit_cb_data);
201 read_log_message_or_eof (guestfs_h *g, int fd, int error_if_eof)
209 "read_log_message_or_eof: %p g->state = %d, fd = %d\n",
213 /* QEMU's console emulates a 16550A serial port. The real 16550A
214 * device has a small FIFO buffer (16 bytes) which means here we see
215 * lots of small reads of 1-16 bytes in length, usually single
218 n = read (fd, buf, sizeof buf);
220 /* Hopefully this indicates the qemu child process has died. */
224 /* We weren't expecting eof here (called from launch) so place
225 * something in the error buffer. RHBZ#588851.
227 error (g, "child process died unexpectedly");
233 if (errno == EINTR || errno == EAGAIN)
240 /* In verbose mode, copy all log messages to stderr. */
242 ignore_value (write (STDERR_FILENO, buf, n));
244 /* It's an actual log message, send it upwards if anyone is listening. */
245 if (g->log_message_cb)
246 g->log_message_cb (g, g->log_message_cb_data, buf, n);
251 /* Read 'n' bytes, setting the socket to blocking temporarily so
252 * that we really read the number of bytes requested.
253 * Returns: 0 == EOF while reading
254 * -1 == error, error() function has been called
255 * n == read 'n' bytes in full
258 really_read_from_socket (guestfs_h *g, int sock, char *buf, size_t n)
264 /* Set socket to blocking. */
265 flags = fcntl (sock, F_GETFL);
267 perrorf (g, "fcntl");
270 if (fcntl (sock, F_SETFL, flags & ~O_NONBLOCK) == -1) {
271 perrorf (g, "fcntl");
277 r = read (sock, &buf[got], n-got);
287 /* Restore original socket flags. */
288 if (fcntl (sock, F_SETFL, flags) == -1) {
289 perrorf (g, "fcntl");
293 return (ssize_t) got;
297 check_for_daemon_cancellation_or_eof (guestfs_h *g, int fd)
306 "check_for_daemon_cancellation_or_eof: %p g->state = %d, fd = %d\n",
309 n = really_read_from_socket (g, fd, buf, 4);
313 /* Hopefully this indicates the qemu child process has died. */
318 xdrmem_create (&xdr, buf, 4, XDR_DECODE);
319 xdr_uint32_t (&xdr, &flag);
322 /* Read and process progress messages that happen during FileIn. */
323 if (flag == GUESTFS_PROGRESS_FLAG) {
324 char buf[PROGRESS_MESSAGE_SIZE];
326 n = really_read_from_socket (g, fd, buf, PROGRESS_MESSAGE_SIZE);
334 if (g->state == BUSY && g->progress_cb) {
335 guestfs_progress message;
337 xdrmem_create (&xdr, buf, PROGRESS_MESSAGE_SIZE, XDR_DECODE);
338 xdr_guestfs_progress (&xdr, &message);
341 g->progress_cb (g, g->progress_cb_data,
342 message.proc, message.serial,
343 message.position, message.total);
349 if (flag != GUESTFS_CANCEL_FLAG) {
350 error (g, _("check_for_daemon_cancellation_or_eof: read 0x%x from daemon, expected 0x%x\n"),
351 flag, GUESTFS_CANCEL_FLAG);
358 /* This writes the whole N bytes of BUF to the daemon socket.
360 * If the whole write is successful, it returns 0.
361 * If there was an error, it returns -1.
362 * If the daemon sent a cancellation message, it returns -2.
364 * It also checks qemu stdout for log messages and passes those up
365 * through log_message_cb.
367 * It also checks for EOF (qemu died) and passes that up through the
368 * child_cleanup function above.
371 guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n)
373 const char *buf = v_buf;
379 "send_to_daemon: %p g->state = %d, n = %zu\n", g, g->state, n);
384 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
385 FD_SET (g->sock, &rset); /* Read socket for cancellation & EOF. */
386 FD_SET (g->sock, &wset); /* Write to socket to send the data. */
388 int max_fd = MAX (g->sock, g->fd[1]);
393 int r = select (max_fd+1, &rset2, &wset2, NULL, NULL);
395 if (errno == EINTR || errno == EAGAIN)
397 perrorf (g, "select");
401 if (FD_ISSET (g->fd[1], &rset2)) {
402 if (read_log_message_or_eof (g, g->fd[1], 0) == -1)
405 if (FD_ISSET (g->sock, &rset2)) {
406 r = check_for_daemon_cancellation_or_eof (g, g->sock);
410 /* Daemon sent cancel message. But to maintain
411 * synchronization we must write out the remainder of the
412 * write buffer before we return (RHBZ#576879).
414 if (xwrite (g->sock, buf, n) == -1) {
415 perrorf (g, "write");
418 return -2; /* cancelled */
421 if (FD_ISSET (g->sock, &wset2)) {
422 r = write (g->sock, buf, n);
424 if (errno == EINTR || errno == EAGAIN)
426 perrorf (g, "write");
427 if (errno == EPIPE) /* Disconnected from guest (RHBZ#508713). */
439 /* This reads a single message, file chunk, launch flag or
440 * cancellation flag from the daemon. If something was read, it
441 * returns 0, otherwise -1.
443 * Both size_rtn and buf_rtn must be passed by the caller as non-NULL.
445 * *size_rtn returns the size of the returned message or it may be
446 * GUESTFS_LAUNCH_FLAG or GUESTFS_CANCEL_FLAG.
448 * *buf_rtn is returned containing the message (if any) or will be set
449 * to NULL. *buf_rtn must be freed by the caller.
451 * It also checks qemu stdout for log messages and passes those up
452 * through log_message_cb.
454 * It also checks for EOF (qemu died) and passes that up through the
455 * child_cleanup function above.
457 * Progress notifications are handled transparently by this function.
458 * If the callback exists, it is called. The caller of this function
459 * will not see GUESTFS_PROGRESS_FLAG.
463 unexpected_end_of_file_from_daemon_error (guestfs_h *g)
465 #define UNEXPEOF_ERROR "unexpected end of file when reading from daemon.\n"
466 #define UNEXPEOF_TEST_TOOL \
467 "Or you can run 'libguestfs-test-tool' and post the complete output into\n" \
468 "a bug report or message to the libguestfs mailing list."
470 error (g, _(UNEXPEOF_ERROR
471 "This usually means the libguestfs appliance failed to start up. Please\n"
472 "enable debugging (LIBGUESTFS_DEBUG=1) and rerun the command, then look at\n"
473 "the debug messages output prior to this error.\n"
474 UNEXPEOF_TEST_TOOL));
476 error (g, _(UNEXPEOF_ERROR
477 "See earlier debug messages.\n"
478 UNEXPEOF_TEST_TOOL));
482 guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn)
488 "recv_from_daemon: %p g->state = %d, size_rtn = %p, buf_rtn = %p\n",
489 g, g->state, size_rtn, buf_rtn);
493 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
494 FD_SET (g->sock, &rset); /* Read socket for data & EOF. */
496 int max_fd = MAX (g->sock, g->fd[1]);
502 /* nr is the size of the message, but we prime it as -4 because we
503 * have to read the message length word first.
508 ssize_t message_size =
509 *size_rtn != GUESTFS_PROGRESS_FLAG ?
510 *size_rtn : PROGRESS_MESSAGE_SIZE;
511 if (nr >= message_size)
515 int r = select (max_fd+1, &rset2, NULL, NULL, NULL);
517 if (errno == EINTR || errno == EAGAIN)
519 perrorf (g, "select");
525 if (FD_ISSET (g->fd[1], &rset2)) {
526 if (read_log_message_or_eof (g, g->fd[1], 0) == -1) {
532 if (FD_ISSET (g->sock, &rset2)) {
533 if (nr < 0) { /* Have we read the message length word yet? */
534 r = read (g->sock, lenbuf+nr+4, -nr);
536 if (errno == EINTR || errno == EAGAIN)
540 /* Under some circumstances we see "Connection reset by peer"
541 * here when the child dies suddenly. Catch this and call
542 * the cleanup function, same as for EOF.
544 if (err == ECONNRESET)
549 unexpected_end_of_file_from_daemon_error (g);
555 if (nr < 0) /* Still not got the whole length word. */
559 xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
560 xdr_uint32_t (&xdr, size_rtn);
563 /* *size_rtn changed, recalculate message_size */
565 *size_rtn != GUESTFS_PROGRESS_FLAG ?
566 *size_rtn : PROGRESS_MESSAGE_SIZE;
568 if (*size_rtn == GUESTFS_LAUNCH_FLAG) {
569 if (g->state != LAUNCHING)
570 error (g, _("received magic signature from guestfsd, but in state %d"),
574 if (g->launch_done_cb)
575 g->launch_done_cb (g, g->launch_done_cb_data);
579 else if (*size_rtn == GUESTFS_CANCEL_FLAG)
581 else if (*size_rtn == GUESTFS_PROGRESS_FLAG)
583 /* If this happens, it's pretty bad and we've probably lost
586 else if (*size_rtn > GUESTFS_MESSAGE_MAX) {
587 error (g, _("message length (%u) > maximum possible size (%d)"),
588 (unsigned) *size_rtn, GUESTFS_MESSAGE_MAX);
592 /* Allocate the complete buffer, size now known. */
593 *buf_rtn = safe_malloc (g, message_size);
597 size_t sizetoread = message_size - nr;
598 if (sizetoread > BUFSIZ) sizetoread = BUFSIZ;
600 r = read (g->sock, (char *) (*buf_rtn) + nr, sizetoread);
602 if (errno == EINTR || errno == EAGAIN)
610 unexpected_end_of_file_from_daemon_error (g);
620 /* Got the full message, caller can start processing it. */
621 #ifdef ENABLE_PACKET_DUMP
625 for (i = 0; i < nr; i += 16) {
626 printf ("%04zx: ", i);
627 for (j = i; j < MIN (i+16, nr); ++j)
628 printf ("%02x ", (*(unsigned char **)buf_rtn)[j]);
629 for (; j < i+16; ++j)
632 for (j = i; j < MIN (i+16, nr); ++j)
633 if (c_isprint ((*(char **)buf_rtn)[j]))
634 printf ("%c", (*(char **)buf_rtn)[j]);
637 for (; j < i+16; ++j)
644 if (*size_rtn == GUESTFS_PROGRESS_FLAG) {
645 if (g->state == BUSY && g->progress_cb) {
646 guestfs_progress message;
648 xdrmem_create (&xdr, *buf_rtn, PROGRESS_MESSAGE_SIZE, XDR_DECODE);
649 xdr_guestfs_progress (&xdr, &message);
652 g->progress_cb (g, g->progress_cb_data,
653 message.proc, message.serial,
654 message.position, message.total);
660 /* Process next message. */
661 return guestfs___recv_from_daemon (g, size_rtn, buf_rtn);
667 /* This is very much like recv_from_daemon above, but g->sock is
668 * a listening socket and we are accepting a new connection on
669 * that socket instead of reading anything. Returns the newly
673 guestfs___accept_from_daemon (guestfs_h *g)
679 "accept_from_daemon: %p g->state = %d\n", g, g->state);
683 FD_SET (g->fd[1], &rset); /* Read qemu stdout for log messages & EOF. */
684 FD_SET (g->sock, &rset); /* Read socket for accept. */
686 int max_fd = MAX (g->sock, g->fd[1]);
690 /* If the qemu process has died, clean up the zombie (RHBZ#579155).
691 * By partially polling in the select below we ensure that this
692 * function will be called eventually.
694 waitpid (g->pid, NULL, WNOHANG);
698 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
699 int r = select (max_fd+1, &rset2, NULL, NULL, &tv);
701 if (errno == EINTR || errno == EAGAIN)
703 perrorf (g, "select");
707 if (FD_ISSET (g->fd[1], &rset2)) {
708 if (read_log_message_or_eof (g, g->fd[1], 1) == -1)
711 if (FD_ISSET (g->sock, &rset2)) {
712 sock = accept (g->sock, NULL, NULL);
714 if (errno == EINTR || errno == EAGAIN)
716 perrorf (g, "accept");
726 guestfs___send (guestfs_h *g, int proc_nr,
727 uint64_t progress_hint, uint64_t optargs_bitmask,
728 xdrproc_t xdrp, char *args)
730 struct guestfs_message_header hdr;
733 int serial = g->msg_next_serial++;
738 if (g->state != BUSY) {
739 error (g, _("guestfs___send: state %d != BUSY"), g->state);
743 /* We have to allocate this message buffer on the heap because
744 * it is quite large (although will be mostly unused). We
745 * can't allocate it on the stack because in some environments
746 * we have quite limited stack space available, notably when
747 * running in the JVM.
749 msg_out = safe_malloc (g, GUESTFS_MESSAGE_MAX + 4);
750 xdrmem_create (&xdr, msg_out + 4, GUESTFS_MESSAGE_MAX, XDR_ENCODE);
752 /* Serialize the header. */
753 hdr.prog = GUESTFS_PROGRAM;
754 hdr.vers = GUESTFS_PROTOCOL_VERSION;
756 hdr.direction = GUESTFS_DIRECTION_CALL;
758 hdr.status = GUESTFS_STATUS_OK;
759 hdr.progress_hint = progress_hint;
760 hdr.optargs_bitmask = optargs_bitmask;
762 if (!xdr_guestfs_message_header (&xdr, &hdr)) {
763 error (g, _("xdr_guestfs_message_header failed"));
767 /* Serialize the args. If any, because some message types
768 * have no parameters.
771 if (!(*xdrp) (&xdr, args)) {
772 error (g, _("dispatch failed to marshal args"));
777 /* Get the actual length of the message, resize the buffer to match
778 * the actual length, and write the length word at the beginning.
780 len = xdr_getpos (&xdr);
783 msg_out = safe_realloc (g, msg_out, len + 4);
784 msg_out_size = len + 4;
786 xdrmem_create (&xdr, msg_out, 4, XDR_ENCODE);
787 xdr_uint32_t (&xdr, &len);
790 r = guestfs___send_to_daemon (g, msg_out, msg_out_size);
791 if (r == -2) /* Ignore stray daemon cancellations. */
804 static int cancel = 0; /* XXX Implement file cancellation. */
805 static int send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t len);
806 static int send_file_data (guestfs_h *g, const char *buf, size_t len);
807 static int send_file_cancellation (guestfs_h *g);
808 static int send_file_complete (guestfs_h *g);
814 * -2 daemon cancelled (we must read the error message)
817 guestfs___send_file (guestfs_h *g, const char *filename)
819 char buf[GUESTFS_MAX_CHUNK_SIZE];
822 fd = open (filename, O_RDONLY);
824 perrorf (g, "open: %s", filename);
825 send_file_cancellation (g);
829 /* Send file in chunked encoding. */
831 r = read (fd, buf, sizeof buf);
832 if (r == -1 && (errno == EINTR || errno == EAGAIN))
835 err = send_file_data (g, buf, r);
837 if (err == -2) /* daemon sent cancellation */
838 send_file_cancellation (g);
843 if (cancel) { /* cancel from either end */
844 send_file_cancellation (g);
849 perrorf (g, "read: %s", filename);
850 send_file_cancellation (g);
854 /* End of file, but before we send that, we need to close
855 * the file and check for errors.
857 if (close (fd) == -1) {
858 perrorf (g, "close: %s", filename);
859 send_file_cancellation (g);
863 return send_file_complete (g);
866 /* Send a chunk of file data. */
868 send_file_data (guestfs_h *g, const char *buf, size_t len)
870 return send_file_chunk (g, 0, buf, len);
873 /* Send a cancellation message. */
875 send_file_cancellation (guestfs_h *g)
877 return send_file_chunk (g, 1, NULL, 0);
880 /* Send a file complete chunk. */
882 send_file_complete (guestfs_h *g)
885 return send_file_chunk (g, 0, buf, 0);
889 send_file_chunk (guestfs_h *g, int cancel, const char *buf, size_t buflen)
898 if (g->state != BUSY) {
899 error (g, _("send_file_chunk: state %d != READY"), g->state);
903 /* Allocate the chunk buffer. Don't use the stack to avoid
904 * excessive stack usage and unnecessary copies.
906 msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
907 xdrmem_create (&xdr, msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
909 /* Serialize the chunk. */
910 chunk.cancel = cancel;
911 chunk.data.data_len = buflen;
912 chunk.data.data_val = (char *) buf;
914 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
915 error (g, _("xdr_guestfs_chunk failed (buf = %p, buflen = %zu)"),
921 len = xdr_getpos (&xdr);
924 /* Reduce the size of the outgoing message buffer to the real length. */
925 msg_out = safe_realloc (g, msg_out, len + 4);
926 msg_out_size = len + 4;
928 xdrmem_create (&xdr, msg_out, 4, XDR_ENCODE);
929 xdr_uint32_t (&xdr, &len);
931 r = guestfs___send_to_daemon (g, msg_out, msg_out_size);
933 /* Did the daemon send a cancellation message? */
936 fprintf (stderr, "got daemon cancellation\n");
952 /* Receive a reply. */
954 guestfs___recv (guestfs_h *g, const char *fn,
955 guestfs_message_header *hdr,
956 guestfs_message_error *err,
957 xdrproc_t xdrp, char *ret)
965 r = guestfs___recv_from_daemon (g, &size, &buf);
969 /* This can happen if a cancellation happens right at the end
970 * of us sending a FileIn parameter to the daemon. Discard. The
971 * daemon should send us an error message next.
973 if (size == GUESTFS_CANCEL_FLAG)
976 if (size == GUESTFS_LAUNCH_FLAG) {
977 error (g, "%s: received unexpected launch flag from daemon when expecting reply", fn);
981 xdrmem_create (&xdr, buf, size, XDR_DECODE);
983 if (!xdr_guestfs_message_header (&xdr, hdr)) {
984 error (g, "%s: failed to parse reply header", fn);
989 if (hdr->status == GUESTFS_STATUS_ERROR) {
990 if (!xdr_guestfs_message_error (&xdr, err)) {
991 error (g, "%s: failed to parse reply error", fn);
997 if (xdrp && ret && !xdrp (&xdr, ret)) {
998 error (g, "%s: failed to parse reply", fn);
1010 /* Same as guestfs___recv, but it discards the reply message. */
1012 guestfs___recv_discard (guestfs_h *g, const char *fn)
1019 r = guestfs___recv_from_daemon (g, &size, &buf);
1023 /* This can happen if a cancellation happens right at the end
1024 * of us sending a FileIn parameter to the daemon. Discard. The
1025 * daemon should send us an error message next.
1027 if (size == GUESTFS_CANCEL_FLAG)
1030 if (size == GUESTFS_LAUNCH_FLAG) {
1031 error (g, "%s: received unexpected launch flag from daemon when expecting reply", fn);
1038 /* Receive a file. */
1040 /* Returns -1 = error, 0 = EOF, > 0 = more data */
1041 static ssize_t receive_file_data (guestfs_h *g, void **buf);
1044 guestfs___recv_file (guestfs_h *g, const char *filename)
1049 fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
1051 perrorf (g, "open: %s", filename);
1055 /* Receive the file in chunked encoding. */
1056 while ((r = receive_file_data (g, &buf)) > 0) {
1057 if (xwrite (fd, buf, r) == -1) {
1058 perrorf (g, "%s: write", filename);
1066 error (g, _("%s: error in chunked encoding"), filename);
1070 if (close (fd) == -1) {
1071 perrorf (g, "close: %s", filename);
1078 /* Send cancellation message to daemon, then wait until it
1079 * cancels (just throwing away data).
1083 uint32_t flag = GUESTFS_CANCEL_FLAG;
1086 fprintf (stderr, "%s: waiting for daemon to acknowledge cancellation\n",
1089 xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
1090 xdr_uint32_t (&xdr, &flag);
1093 if (xwrite (g->sock, fbuf, sizeof fbuf) == -1) {
1094 perrorf (g, _("write to daemon socket"));
1098 while (receive_file_data (g, NULL) > 0)
1099 ; /* just discard it */
1104 /* Receive a chunk of file data. */
1105 /* Returns -1 = error, 0 = EOF, > 0 = more data */
1107 receive_file_data (guestfs_h *g, void **buf_r)
1113 guestfs_chunk chunk;
1115 r = guestfs___recv_from_daemon (g, &len, &buf);
1117 error (g, _("receive_file_data: parse error in reply callback"));
1121 if (len == GUESTFS_LAUNCH_FLAG || len == GUESTFS_CANCEL_FLAG) {
1122 error (g, _("receive_file_data: unexpected flag received when reading file chunks"));
1126 memset (&chunk, 0, sizeof chunk);
1128 xdrmem_create (&xdr, buf, len, XDR_DECODE);
1129 if (!xdr_guestfs_chunk (&xdr, &chunk)) {
1130 error (g, _("failed to parse file chunk"));
1135 /* After decoding, the original buffer is no longer used. */
1139 error (g, _("file receive cancelled by daemon"));
1140 free (chunk.data.data_val);
1144 if (chunk.data.data_len == 0) { /* end of transfer */
1145 free (chunk.data.data_val);
1149 if (buf_r) *buf_r = chunk.data.data_val;
1150 else free (chunk.data.data_val); /* else caller frees */
1152 return chunk.data.data_len;