Use poll() for connect loop
authorJens Axboe <axboe@kernel.dk>
Fri, 30 Sep 2011 00:27:02 +0000 (18:27 -0600)
committerJens Axboe <axboe@kernel.dk>
Fri, 30 Sep 2011 00:27:02 +0000 (18:27 -0600)
So we can exit nicely when asked to.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
fio.c
server.c
server.h

diff --git a/fio.c b/fio.c
index 5e4d0e6..a15080b 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -46,6 +46,7 @@
 #include "profile.h"
 #include "lib/rand.h"
 #include "memalign.h"
+#include "server.h"
 
 unsigned long page_mask;
 unsigned long page_size;
@@ -122,6 +123,7 @@ static void sig_int(int sig)
 {
        if (threads) {
                log_info("\nfio: terminating on signal %d\n", sig);
+               exit_backend = 1;
                fflush(stdout);
                exit_value = 128;
                terminate_threads(TERMINATE_ALL);
index 541216c..38f690b 100644 (file)
--- a/server.c
+++ b/server.c
 
 static int net_port = 8765;
 
+int exit_backend = 0;
+
 static int accept_loop(int listen_sk)
 {
        struct sockaddr addr;
        unsigned int len = sizeof(addr);
-       int sk, do_exit = 0;
+       struct pollfd pfd;
+       int ret, sk, flags;
 
+       flags = fcntl(listen_sk, F_GETFL);
+       flags |= O_NONBLOCK;
+       fcntl(listen_sk, F_SETFL, flags);
 again:
+       pfd.fd = listen_sk;
+       pfd.events = POLLIN;
+       do {
+               ret = poll(&pfd, 1, 100);
+               if (ret < 0) {
+                       if (errno == EINTR)
+                               break;
+                       perror("poll");
+                       goto out;
+               } else if (!ret)
+                       continue;
+
+               if (pfd.revents & POLLIN)
+                       break;
+       } while (!exit_backend);
+
+       if (exit_backend)
+               goto out;
+
        sk = accept(listen_sk, &addr, &len);
        if (sk < 0) {
                log_err("fio: accept failed\n");
@@ -35,14 +60,13 @@ again:
        }
 
        /* read forever */
-       while (!do_exit) {
+       while (!exit_backend) {
                char buf[131072];
-               int ret;
 
                ret = recv(sk, buf, 4096, 0);
                if (ret > 0) {
                        if (!strncmp("FIO_QUIT", buf, 8)) {
-                               do_exit = 1;
+                               exit_backend = 1;
                                break;
                        }
                        parse_jobs_ini(buf, 1, 0);
@@ -58,9 +82,10 @@ again:
 
        close(sk);
 
-       if (!do_exit)
+       if (!exit_backend)
                goto again;
 
+out:
        return 0;
 }
 
index 879faab..aed61b2 100644 (file)
--- a/server.h
+++ b/server.h
@@ -2,5 +2,6 @@
 #define FIO_SERVER_H
 
 extern int fio_server(void);
+extern int exit_backend;
 
 #endif