Daemon: fix handling of errors from xread and xwrite.
[libguestfs.git] / daemon / proto.c
index 709f978..c0e3927 100644 (file)
@@ -45,7 +45,7 @@ main_loop (int _sock)
   XDR xdr;
   char *buf;
   char lenbuf[4];
-  unsigned len;
+  uint32_t len;
   struct guestfs_message_header hdr;
   struct timeval start_t, end_t;
   int64_t start_us, end_us, elapsed_us;
@@ -62,7 +62,9 @@ main_loop (int _sock)
 #endif
 
     /* Read the length word. */
-    xread (sock, lenbuf, 4);
+    if (xread (sock, lenbuf, 4) == -1)
+      exit (1);
+
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     xdr_uint32_t (&xdr, &len);
     xdr_destroy (&xdr);
@@ -79,14 +81,15 @@ main_loop (int _sock)
       continue;
     }
 
-    xread (sock, buf, len);
+    if (xread (sock, buf, len) == -1)
+      exit (1);
 
-#if 0
+#ifdef ENABLE_PACKET_DUMP
     if (verbose) {
-      int i, j;
+      size_t i, j;
 
       for (i = 0; i < len; i += 16) {
-        printf ("%04x: ", i);
+        printf ("%04zx: ", i);
         for (j = i; j < MIN (i+16, len); ++j)
           printf ("%02x ", (unsigned char) buf[j]);
         for (; j < i+16; ++j)
@@ -288,7 +291,7 @@ reply (xdrproc_t xdrp, char *ret)
     fprintf (stderr, "xwrite failed\n");
     exit (1);
   }
-  if (xwrite (sock, buf, len) == len) {
+  if (xwrite (sock, buf, len) == -1) {
     fprintf (stderr, "xwrite failed\n");
     exit (1);
   }
@@ -307,7 +310,9 @@ receive_file (receive_cb cb, void *opaque)
 
   for (;;) {
     /* Read the length word. */
-    xread (sock, lenbuf, 4);
+    if (xread (sock, lenbuf, 4) == -1)
+      exit (1);
+
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     xdr_uint32_t (&xdr, &len);
     xdr_destroy (&xdr);
@@ -327,7 +332,8 @@ receive_file (receive_cb cb, void *opaque)
       return -1;
     }
 
-    xread (sock, buf, len);
+    if (xread (sock, buf, len) == -1)
+      exit (1);
 
     xdrmem_create (&xdr, buf, len, XDR_DECODE);
     memset (&chunk, 0, sizeof chunk);
@@ -444,10 +450,8 @@ check_for_library_cancellation (void)
 
   /* Read the message from the daemon. */
   r = xread (sock, buf, sizeof buf);
-  if (r == -1) {
-    perror ("read");
+  if (r == -1)
     return 0;
-  }
 
   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
   xdr_uint32_t (&xdr, &flag);
@@ -462,7 +466,7 @@ check_for_library_cancellation (void)
   return 1;
 }
 
-void
+int
 send_file_end (int cancel)
 {
   guestfs_chunk chunk;
@@ -470,7 +474,7 @@ send_file_end (int cancel)
   chunk.cancel = cancel;
   chunk.data.data_len = 0;
   chunk.data.data_val = NULL;
-  send_chunk (&chunk);
+  return send_chunk (&chunk);
 }
 
 static int
@@ -495,8 +499,12 @@ send_chunk (const guestfs_chunk *chunk)
   xdr_uint32_t (&xdr, &len);
   xdr_destroy (&xdr);
 
-  (void) xwrite (sock, lenbuf, 4);
-  (void) xwrite (sock, buf, len);
+  int err = (xwrite (sock, lenbuf, 4) == 0
+             && xwrite (sock, buf, len) == 0 ? 0 : -1);
+  if (err) {
+    fprintf (stderr, "send_chunk: write failed\n");
+    exit (1);
+  }
 
-  return 0;
+  return err;
 }