#include <unistd.h>
#include <errno.h>
#include <sys/param.h> /* defines MIN */
+#include <sys/select.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
#include "c-ctype.h"
#include "ignore-value.h"
exit (EXIT_FAILURE);
xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
- xdr_uint32_t (&xdr, &len);
+ xdr_u_int (&xdr, &len);
xdr_destroy (&xdr);
if (len > GUESTFS_MESSAGE_MAX) {
goto cont;
}
- /* Now start to process this message. */
proc_nr = hdr.proc;
serial = hdr.serial;
+
+ /* Clear errors before we call the stub functions. This is just
+ * to ensure that we can accurately report errors in cases where
+ * error handling paths don't set errno correctly.
+ */
+ errno = 0;
+#ifdef WIN32
+ SetLastError (0);
+ WSASetLastError (0);
+#endif
+
+ /* Now start to process this message. */
dispatch_incoming_message (&xdr);
/* Note that dispatch_incoming_message will also send a reply. */
}
void
-reply_with_perror (const char *fs, ...)
+reply_with_perror_errno (int err, const char *fs, ...)
{
char buf1[GUESTFS_ERROR_LEN];
char buf2[GUESTFS_ERROR_LEN];
va_list args;
- int err = errno;
va_start (args, fs);
vsnprintf (buf1, sizeof buf1, fs, args);
xdr_destroy (&xdr);
xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
- xdr_uint32_t (&xdr, &len);
+ xdr_u_int (&xdr, &len);
xdr_destroy (&xdr);
if (xwrite (sock, lenbuf, 4) == -1) {
* we want to return an error message instead. (RHBZ#509597).
*/
if (!(*xdrp) (&xdr, ret)) {
- reply_with_perror ("guestfsd: failed to encode reply body\n");
+ reply_with_error ("guestfsd: failed to encode reply body\n(maybe the reply exceeds the maximum message size in the protocol?)");
xdr_destroy (&xdr);
return;
}
xdr_destroy (&xdr);
xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
- xdr_uint32_t (&xdr, &len);
+ xdr_u_int (&xdr, &len);
xdr_destroy (&xdr);
if (xwrite (sock, lenbuf, 4) == -1) {
uint32_t len;
for (;;) {
+ if (verbose)
+ fprintf (stderr, "receive_file: reading length word\n");
+
/* Read the length word. */
if (xread (sock, lenbuf, 4) == -1)
exit (EXIT_FAILURE);
xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
- xdr_uint32_t (&xdr, &len);
+ xdr_u_int (&xdr, &len);
xdr_destroy (&xdr);
if (len == GUESTFS_CANCEL_FLAG)
free (buf);
if (verbose)
- printf ("receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
- chunk.cancel, chunk.data.data_len, chunk.data.data_val);
+ fprintf (stderr, "receive_file: got chunk: cancel = %d, len = %d, buf = %p\n",
+ chunk.cancel, chunk.data.data_len, chunk.data.data_val);
if (chunk.cancel) {
- fprintf (stderr, "receive_file: received cancellation from library\n");
+ if (verbose)
+ fprintf (stderr, "receive_file: received cancellation from library\n");
xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
return -2;
}
if (chunk.data.data_len == 0) {
+ if (verbose)
+ fprintf (stderr, "receive_file: end of file, leaving function\n");
xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
return 0; /* end of file */
}
r = 0;
xdr_free ((xdrproc_t) xdr_guestfs_chunk, (char *) &chunk);
- if (r == -1) /* write error */
+ if (r == -1) { /* write error */
+ if (verbose)
+ fprintf (stderr, "receive_file: write error\n");
return -1;
+ }
}
}
uint32_t flag = GUESTFS_CANCEL_FLAG;
xdrmem_create (&xdr, fbuf, sizeof fbuf, XDR_ENCODE);
- xdr_uint32_t (&xdr, &flag);
+ xdr_u_int (&xdr, &flag);
xdr_destroy (&xdr);
if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
return 0;
xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
- xdr_uint32_t (&xdr, &flag);
+ xdr_u_int (&xdr, &flag);
xdr_destroy (&xdr);
if (flag != GUESTFS_CANCEL_FLAG) {
xdr_destroy (&xdr);
xdrmem_create (&xdr, lenbuf, 4, XDR_ENCODE);
- xdr_uint32_t (&xdr, &len);
+ xdr_u_int (&xdr, &len);
xdr_destroy (&xdr);
int err = (xwrite (sock, lenbuf, 4) == 0