Change protocol to send Linux errno from daemon to library.
[libguestfs.git] / daemon / proto.c
index 2231037..628e86c 100644 (file)
 #include <rpc/types.h>
 #include <rpc/xdr.h>
 
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
 #include "c-ctype.h"
 #include "ignore-value.h"
 
@@ -137,9 +141,20 @@ main_loop (int _sock)
       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. */
 
@@ -164,7 +179,7 @@ main_loop (int _sock)
   }
 }
 
-static void send_error (const char *msg);
+static void send_error (int errnum, const char *msg);
 
 void
 reply_with_error (const char *fs, ...)
@@ -176,16 +191,15 @@ reply_with_error (const char *fs, ...)
   vsnprintf (err, sizeof err, fs, args);
   va_end (args);
 
-  send_error (err);
+  send_error (0, err);
 }
 
 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);
@@ -193,11 +207,11 @@ reply_with_perror (const char *fs, ...)
 
   snprintf (buf2, sizeof buf2, "%s: %s", buf1, strerror (err));
 
-  send_error (buf2);
+  send_error (err, buf2);
 }
 
 static void
-send_error (const char *msg)
+send_error (int errnum, const char *msg)
 {
   XDR xdr;
   char buf[GUESTFS_ERROR_LEN + 200];
@@ -222,6 +236,7 @@ send_error (const char *msg)
     exit (EXIT_FAILURE);
   }
 
+  err.linux_errno = errnum;
   err.error_message = (char *) msg;
 
   if (!xdr_guestfs_message_error (&xdr, &err)) {
@@ -275,7 +290,7 @@ reply (xdrproc_t xdrp, char *ret)
      * 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;
     }
@@ -310,6 +325,9 @@ receive_file (receive_cb cb, void *opaque)
   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);
@@ -347,15 +365,18 @@ receive_file (receive_cb cb, void *opaque)
     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 */
     }
@@ -366,13 +387,16 @@ receive_file (receive_cb cb, void *opaque)
       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;
+    }
   }
 }
 
 /* Send a cancellation flag back to the library. */
-void
+int
 cancel_receive (void)
 {
   XDR xdr;
@@ -385,11 +409,11 @@ cancel_receive (void)
 
   if (xwrite (sock, fbuf, sizeof fbuf) == -1) {
     perror ("write to socket");
-    return;
+    return -1;
   }
 
   /* Keep receiving chunks and discarding, until library sees cancel. */
-  (void) receive_file (NULL, NULL);
+  return receive_file (NULL, NULL);
 }
 
 static int check_for_library_cancellation (void);