+ char err[GUESTFS_ERROR_LEN];
+ va_list args;
+
+ va_start (args, fs);
+ vsnprintf (err, sizeof err, fs, args);
+ va_end (args);
+
+ send_error (err);
+}
+
+void
+reply_with_perror_errno (int err, const char *fs, ...)
+{
+ char buf1[GUESTFS_ERROR_LEN];
+ char buf2[GUESTFS_ERROR_LEN];
+ va_list args;
+
+ va_start (args, fs);
+ vsnprintf (buf1, sizeof buf1, fs, args);
+ va_end (args);
+
+ snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
+
+ send_error (buf2);
+}
+
+static void
+send_error (const char *msg)
+{
+ XDR xdr;
+ char buf[GUESTFS_ERROR_LEN + 200];
+ char lenbuf[4];
+ struct guestfs_message_header hdr;
+ struct guestfs_message_error err;
+ unsigned len;
+
+ fprintf (stderr, "guestfsd: error: %s\n", msg);
+
+ xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
+
+ hdr.prog = GUESTFS_PROGRAM;
+ hdr.vers = GUESTFS_PROTOCOL_VERSION;
+ hdr.direction = GUESTFS_DIRECTION_REPLY;
+ hdr.status = GUESTFS_STATUS_ERROR;
+ hdr.proc = proc_nr;
+ hdr.serial = serial;
+
+ if (!xdr_guestfs_message_header (&xdr, &hdr)) {
+ fprintf (stderr, "guestfsd: failed to encode error message header\n");
+ exit (EXIT_FAILURE);
+ }
+
+ err.error_message = (char *) msg;
+
+ if (!xdr_guestfs_message_error (&xdr, &err)) {
+ fprintf (stderr, "guestfsd: failed to encode error message body\n");
+ exit (EXIT_FAILURE);
+ }
+
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
+ xdr_u_int (&xdr, &len);
+ xdr_destroy (&xdr);
+
+ if (xwrite (sock, lenbuf, 4) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+ if (xwrite (sock, buf, len) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+}
+
+void
+reply (xdrproc_t xdrp, char *ret)
+{
+ XDR xdr;
+ char buf[GUESTFS_MESSAGE_MAX];
+ char lenbuf[4];
+ struct guestfs_message_header hdr;
+ unsigned len;
+
+ xdrmem_create (&xdr, buf, sizeof buf, XDR_ENCODE);
+
+ hdr.prog = GUESTFS_PROGRAM;
+ hdr.vers = GUESTFS_PROTOCOL_VERSION;
+ hdr.direction = GUESTFS_DIRECTION_REPLY;
+ hdr.status = GUESTFS_STATUS_OK;
+ hdr.proc = proc_nr;
+ hdr.serial = serial;
+
+ if (!xdr_guestfs_message_header (&xdr, &hdr)) {
+ fprintf (stderr, "guestfsd: failed to encode reply header\n");
+ exit (EXIT_FAILURE);
+ }
+
+ if (xdrp) {
+ /* This can fail if the reply body is too large, for example
+ * if it exceeds the maximum message size. In that case
+ * we want to return an error message instead. (RHBZ#509597).
+ */
+ if (!(*xdrp) (&xdr, ret)) {
+ reply_with_perror ("guestfsd: failed to encode reply body\n");
+ xdr_destroy (&xdr);
+ return;
+ }
+ }
+
+ len = xdr_getpos (&xdr);
+ xdr_destroy (&xdr);
+
+ xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
+ xdr_u_int (&xdr, &len);
+ xdr_destroy (&xdr);
+
+ if (xwrite (sock, lenbuf, 4) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+ if (xwrite (sock, buf, len) == -1) {
+ fprintf (stderr, "xwrite failed\n");
+ exit (EXIT_FAILURE);
+ }
+}
+
+/* Receive file chunks, repeatedly calling 'cb'. */
+int
+receive_file (receive_cb cb, void *opaque)
+{
+ guestfs_chunk chunk;
+ char lenbuf[4];
+ char *buf;
+ XDR xdr;
+ int r;
+ uint32_t len;
+
+ for (;;) {
+ /* Read the length word. */
+ if (xread (sock, lenbuf, 4) == -1)
+ exit (EXIT_FAILURE);
+
+ xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
+ xdr_u_int (&xdr, &len);
+ xdr_destroy (&xdr);
+
+ if (len == GUESTFS_CANCEL_FLAG)
+ continue; /* Just ignore it. */
+
+ if (len > GUESTFS_MESSAGE_MAX) {
+ fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n",
+ len);
+ exit (EXIT_FAILURE);
+ }
+
+ buf = malloc (len);
+ if (!buf) {
+ perror ("malloc");
+ return -1;
+ }
+
+ if (xread (sock, buf, len) == -1)
+ exit (EXIT_FAILURE);