chiark / gitweb /
io-util: make flush_fd() return how many bytes where flushed
authorLennart Poettering <lennart@poettering.net>
Tue, 12 Dec 2017 22:21:09 +0000 (23:21 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:49:54 +0000 (07:49 +0200)
This is useful so that callers know whether anything at all and how much
was flushed.

This patches through users of this functions to ensure that the return
values > 0 which may be returned now are not propagated in public APIs.

Also, users that ignore the return value are changed to do so explicitly
now.

src/basic/io-util.c
src/libelogind/sd-login/sd-login.c

index 77c9bdc739d424076493ecfdc850fa2dc1f2e6f2..08ad42ed952468df310df00972cdecb3a051bfb5 100644 (file)
@@ -33,6 +33,7 @@ int flush_fd(int fd) {
                 .fd = fd,
                 .events = POLLIN,
         };
+        int count = 0;
 
         /* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
          * read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read
@@ -52,7 +53,7 @@ int flush_fd(int fd) {
                         return -errno;
 
                 } else if (r == 0)
-                        return 0;
+                        return count;
 
                 l = read(fd, buf, sizeof(buf));
                 if (l < 0) {
@@ -61,11 +62,13 @@ int flush_fd(int fd) {
                                 continue;
 
                         if (errno == EAGAIN)
-                                return 0;
+                                return count;
 
                         return -errno;
                 } else if (l == 0)
-                        return 0;
+                        return count;
+
+                count += (int) l;
         }
 }
 
index a7de8f1c1b9842e24e931d1b08af2276aeeeac36..bc5f83eb3069fe3f134b36f328e5e3a01877d247 100644 (file)
@@ -1091,10 +1091,15 @@ _public_ sd_login_monitor* sd_login_monitor_unref(sd_login_monitor *m) {
 }
 
 _public_ int sd_login_monitor_flush(sd_login_monitor *m) {
+        int r;
 
         assert_return(m, -EINVAL);
 
-        return flush_fd(MONITOR_TO_FD(m));
+        r = flush_fd(MONITOR_TO_FD(m));
+        if (r < 0)
+                return r;
+
+        return 0;
 }
 
 _public_ int sd_login_monitor_get_fd(sd_login_monitor *m) {