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 5e4d0e60244ac3c2e6d73ae1c3858eafead8d760..a15080b4a26e89bebea6edf5def458e701e0cddd 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 541216ca6a77f41d9001ded8ecc3e51ef54f17dc..38f690bd9ca81b886c3b90a6631a0548cb3962a9 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 879faaba31f3a3a2093b5fe0b4a94d3505df9410..aed61b2c0b7cf509756ae9f0c29a859c6dc50f38 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