[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen master] libvchan: tidy up usages of fcntl in select-type sample application



commit 098ef0c4d4e0b867e86ddbb9ebc813bb8c2ad617
Author:     Matthew Daley <mattjd@xxxxxxxxx>
AuthorDate: Fri Nov 1 13:33:23 2013 +1300
Commit:     Ian Campbell <ian.campbell@xxxxxxxxxx>
CommitDate: Mon Nov 4 17:13:38 2013 +0000

    libvchan: tidy up usages of fcntl in select-type sample application
    
    Namely, don't overwrite all the other flags when twiddling O_NONBLOCK,
    and add basic error handling.
    
    Coverity-ID: 1055041
    Signed-off-by: Matthew Daley <mattjd@xxxxxxxxx>
    Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
    Reviewed-by: Daniel De Graaf <dgdegra@xxxxxxxxxxxxx>
---
 tools/libvchan/node-select.c |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/tools/libvchan/node-select.c b/tools/libvchan/node-select.c
index 6c6c19e..13c5822 100644
--- a/tools/libvchan/node-select.c
+++ b/tools/libvchan/node-select.c
@@ -80,6 +80,22 @@ void stdout_wr() {
        }
 }
 
+static int set_nonblocking(int fd, int nonblocking) {
+       int flags = fcntl(fd, F_GETFL);
+       if (flags == -1)
+               return -1;
+
+       if (nonblocking)
+               flags |= O_NONBLOCK;
+       else
+               flags &= ~O_NONBLOCK;
+
+       if (fcntl(fd, F_SETFL, flags) == -1)
+               return -1;
+
+       return 0;
+}
+
 /**
     Simple libxenvchan application, both client and server.
        Both sides may write and read, both from the libxenvchan and from 
@@ -105,8 +121,10 @@ int main(int argc, char **argv)
                exit(1);
        }
 
-       fcntl(0, F_SETFL, O_NONBLOCK);
-       fcntl(1, F_SETFL, O_NONBLOCK);
+       if (set_nonblocking(0, 1) || set_nonblocking(1, 1)) {
+               perror("set_nonblocking");
+               exit(1);
+       }
 
        libxenvchan_fd = libxenvchan_fd_for_select(ctrl);
        for (;;) {
@@ -153,7 +171,10 @@ int main(int argc, char **argv)
                        stdout_wr();
                }
                if (!libxenvchan_is_open(ctrl)) {
-                       fcntl(1, F_SETFL, 0);
+                       if (set_nonblocking(1, 0)) {
+                               perror("set_nonblocking");
+                               exit(1);
+                       }
                        while (outsiz)
                                stdout_wr();
                        return 0;
--
generated by git-patchbot for /home/xen/git/xen.git#master

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.