]> xenbits.xensource.com Git - libvirt.git/commitdiff
safe{read,write}: Document usage with nonblocking FD
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 17 Jan 2013 07:35:53 +0000 (08:35 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 21 Jan 2013 19:18:28 +0000 (20:18 +0100)
Currently, whenever somebody calls saferead() on nonblocking FD
(safewrite() is totally interchangeable for purpose of this message)
he might get wrong return value. For instance, in the first iteration
some data is read. The number of bytes read is stored into local
variable 'nread'. However, in next iterations we can get -1 from
read() with errno == EAGAIN, in which case the -1 is returned despite
fact some data has already been read. So the caller gets confused.
Bare read() should be used for nonblocking FD.

src/util/virutil.c

index b36e9d39dc7055d089d9a0d29c4caf72856cd10c..24ba9549497fef03bb560d2ff589083e62cfb36d 100644 (file)
@@ -98,7 +98,11 @@ verify(sizeof(gid_t) <= sizeof(unsigned int) &&
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
-/* Like read(), but restarts after EINTR */
+/* Like read(), but restarts after EINTR.  Doesn't play
+ * nicely with nonblocking FD and EAGAIN, in which case
+ * you want to use bare read(). Or even use virSocket()
+ * if the FD is related to a socket rather than a plain
+ * file or pipe. */
 ssize_t
 saferead(int fd, void *buf, size_t count)
 {
@@ -118,7 +122,11 @@ saferead(int fd, void *buf, size_t count)
     return nread;
 }
 
-/* Like write(), but restarts after EINTR */
+/* Like write(), but restarts after EINTR. Doesn't play
+ * nicely with nonblocking FD and EAGAIN, in which case
+ * you want to use bare write(). Or even use virSocket()
+ * if the FD is related to a socket rather than a plain
+ * file or pipe. */
 ssize_t
 safewrite(int fd, const void *buf, size_t count)
 {