Use poll() for connect loop
[fio.git] / server.c
index bd49b9c5a5cd4100adc7bb03a1a685c61a5fe8bd..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;
+       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);
-       printf("got a hit\n");
        if (sk < 0) {
                log_err("fio: accept failed\n");
                return -1;
        }
 
        /* read forever */
-       for (;;) {
+       while (!exit_backend) {
                char buf[131072];
-               int ret;
 
                ret = recv(sk, buf, 4096, 0);
                if (ret > 0) {
+                       if (!strncmp("FIO_QUIT", buf, 8)) {
+                               exit_backend = 1;
+                               break;
+                       }
                        parse_jobs_ini(buf, 1, 0);
                        exec_run();
                        reset_fio_state();
@@ -54,8 +81,12 @@ again:
        }
 
        close(sk);
-       printf("closed\n");
-       goto again;
+
+       if (!exit_backend)
+               goto again;
+
+out:
+       return 0;
 }
 
 int fio_server(void)