Error check fcntl() calls
authorJens Axboe <axboe@fb.com>
Mon, 14 Apr 2014 16:04:21 +0000 (10:04 -0600)
committerJens Axboe <axboe@fb.com>
Mon, 14 Apr 2014 16:04:21 +0000 (10:04 -0600)
Add a helper to mark an fd as non-blocking, so we can check
and complain in one place.

Signed-off-by: Jens Axboe <axboe@fb.com>
engines/binject.c
engines/sg.c
fio.h
libfio.c
server.c

index fb1906206a039be8e13f5cc00118307b6febd1dd..0264d0a32c0d62e0f3d5bb3c534ee942c0057f6b 100644 (file)
@@ -109,10 +109,9 @@ static int fio_binject_getevents(struct thread_data *td, unsigned int min,
                /*
                 * don't block for min events == 0
                 */
-               if (!min) {
-                       bd->fd_flags[i] = fcntl(bf->fd, F_GETFL);
-                       fcntl(bf->fd, F_SETFL, bd->fd_flags[i] | O_NONBLOCK);
-               }
+               if (!min)
+                       fio_set_fd_nonblocking(bf->fd, "binject");
+
                bd->pfds[i].fd = bf->fd;
                bd->pfds[i].events = POLLIN;
        }
index 88d91258d22c89e78298c6084e7dd277b51aa23e..fcd9c41397a8bf20644b04336c2020bdc63b2215 100644 (file)
@@ -77,10 +77,9 @@ static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
                /*
                 * don't block for min events == 0
                 */
-               if (!min) {
-                       sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
-                       fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
-               }
+               if (!min)
+                       fio_set_fd_nonblocking(f->fd, "sg");
+
                sd->pfds[i].fd = f->fd;
                sd->pfds[i].events = POLLIN;
        }
diff --git a/fio.h b/fio.h
index 144591347c1367e83541237e1509215b6f254609..25f6052b966a93d7bbef5f87275083b33d79de1f 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -441,6 +441,7 @@ extern char *num2str(unsigned long, int, int, int, int);
 extern int ioengine_load(struct thread_data *);
 extern int parse_dryrun(void);
 extern int fio_running_or_pending_io_threads(void);
+extern void fio_set_fd_nonblocking(int, const char *);
 
 extern uintptr_t page_mask;
 extern uintptr_t page_size;
index 3fde492c91af727f6d96abf39c308d3ca4b657c7..5ed8c6073603d4edb0b381fb356694cce600e04b 100644 (file)
--- a/libfio.c
+++ b/libfio.c
@@ -27,6 +27,7 @@
 #include <signal.h>
 #include <stdint.h>
 #include <locale.h>
+#include <fcntl.h>
 
 #include "fio.h"
 #include "smalloc.h"
@@ -233,6 +234,21 @@ int fio_running_or_pending_io_threads(void)
        return 0;
 }
 
+void fio_set_fd_nonblocking(int fd, const char *who)
+{
+       int flags;
+
+       flags = fcntl(fd, F_GETFL);
+       if (flags < 0)
+               log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
+       else {
+               flags |= O_NONBLOCK;
+               flags = fcntl(fd, F_SETFL, flags);
+               if (flags < 0)
+                       log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
+       }
+}
+
 static int endian_check(void)
 {
        union {
index ab3bece1f61f47e3f7e8dd704a5acf64ac50223d..1c4c494abcc0318028003dedb2b2d4e0c47dc7d1 100644 (file)
--- a/server.c
+++ b/server.c
@@ -4,7 +4,6 @@
 #include <unistd.h>
 #include <limits.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <sys/poll.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -843,14 +842,12 @@ static int accept_loop(int listen_sk)
        struct sockaddr_in6 addr6;
        socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
        struct pollfd pfd;
-       int ret = 0, sk, flags, exitval = 0;
+       int ret = 0, sk, exitval = 0;
        FLIST_HEAD(conn_list);
 
        dprint(FD_NET, "server enter accept loop\n");
 
-       flags = fcntl(listen_sk, F_GETFL);
-       flags |= O_NONBLOCK;
-       fcntl(listen_sk, F_SETFL, flags);
+       fio_set_fd_nonblocking(listen_sk, "server");
 
        while (!exit_backend) {
                const char *from;