Daemon: fix handling of errors from xread and xwrite.
authorRichard Jones <rjones@trick.home.annexia.org>
Thu, 17 Sep 2009 14:28:41 +0000 (15:28 +0100)
committerRichard Jones <rjones@trick.home.annexia.org>
Thu, 17 Sep 2009 15:05:39 +0000 (16:05 +0100)
If xread or xwrite returns -1, that indicates an error and we
should exit.  Note that xread/xwrite has already printed the
error message.

daemon/daemon.h
daemon/guestfsd.c
daemon/proto.c

index 83c9672..86c6876 100644 (file)
@@ -36,8 +36,10 @@ extern int sysroot_len;
 
 extern char *sysroot_path (const char *path);
 
 
 extern char *sysroot_path (const char *path);
 
-extern int xwrite (int sock, const void *buf, size_t len);
-extern int xread (int sock, void *buf, size_t len);
+extern int xwrite (int sock, const void *buf, size_t len)
+  __attribute__((__warn_unused_result__));
+extern int xread (int sock, void *buf, size_t len)
+  __attribute__((__warn_unused_result__));
 
 extern int add_string (char ***argv, int *size, int *alloc, const char *str);
 extern int count_strings (char *const *argv);
 
 extern int add_string (char ***argv, int *size, int *alloc, const char *str);
 extern int count_strings (char *const *argv);
index af554bf..7c4e72e 100644 (file)
@@ -233,7 +233,8 @@ main (int argc, char *argv[])
     exit (1);
   }
 
     exit (1);
   }
 
-  (void) xwrite (sock, buf, xdr_getpos (&xdr));
+  if (xwrite (sock, buf, xdr_getpos (&xdr)) == -1)
+    exit (1);
 
   xdr_destroy (&xdr);
 
 
   xdr_destroy (&xdr);
 
index d03a048..c0e3927 100644 (file)
@@ -62,7 +62,9 @@ main_loop (int _sock)
 #endif
 
     /* Read the length word. */
 #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);
     xdrmem_create (&xdr, lenbuf, 4, XDR_DECODE);
     xdr_uint32_t (&xdr, &len);
     xdr_destroy (&xdr);
@@ -79,7 +81,8 @@ main_loop (int _sock)
       continue;
     }
 
       continue;
     }
 
-    xread (sock, buf, len);
+    if (xread (sock, buf, len) == -1)
+      exit (1);
 
 #ifdef ENABLE_PACKET_DUMP
     if (verbose) {
 
 #ifdef ENABLE_PACKET_DUMP
     if (verbose) {
@@ -307,7 +310,9 @@ receive_file (receive_cb cb, void *opaque)
 
   for (;;) {
     /* Read the length word. */
 
   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);
     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;
     }
 
       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);
 
     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);
 
   /* Read the message from the daemon. */
   r = xread (sock, buf, sizeof buf);
-  if (r == -1) {
-    perror ("read");
+  if (r == -1)
     return 0;
     return 0;
-  }
 
   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
   xdr_uint32_t (&xdr, &flag);
 
   xdrmem_create (&xdr, buf, sizeof buf, XDR_DECODE);
   xdr_uint32_t (&xdr, &flag);