Fix address truncation on Windows
[fio.git] / client.c
index ace70d357b97a3e882bacfea811e265779a85b44..dd75882c54f3714db8d8ca37e7119b91be742a66 100644 (file)
--- a/client.c
+++ b/client.c
@@ -37,6 +37,7 @@ struct fio_client {
        char *hostname;
        int port;
        int fd;
+       unsigned int refs;
 
        char *name;
 
@@ -48,6 +49,7 @@ struct fio_client {
        unsigned int jobs;
        int error;
        int ipv6;
+       int sent_job;
 
        struct flist_head eta_list;
        struct client_eta *eta_in_flight;
@@ -118,8 +120,10 @@ static struct fio_client *find_client_by_fd(int fd)
        flist_for_each(entry, &client_hash[bucket]) {
                client = flist_entry(entry, struct fio_client, hash_list);
 
-               if (client->fd == fd)
+               if (client->fd == fd) {
+                       client->refs++;
                        return client;
+               }
        }
 
        return NULL;
@@ -127,6 +131,11 @@ static struct fio_client *find_client_by_fd(int fd)
 
 static void remove_client(struct fio_client *client)
 {
+       assert(client->refs);
+
+       if (--client->refs)
+               return;
+
        dprint(FD_NET, "client: removed <%s>\n", client->hostname);
        flist_del(&client->list);
 
@@ -148,6 +157,11 @@ static void remove_client(struct fio_client *client)
        sum_stat_clients--;
 }
 
+static void put_client(struct fio_client *client)
+{
+       remove_client(client);
+}
+
 static void __fio_client_add_cmd_option(struct fio_client *client,
                                        const char *opt)
 {
@@ -214,6 +228,7 @@ int fio_client_add(const char *hostname, void **cookie)
                return -1;
 
        client->fd = -1;
+       client->refs = 1;
 
        __fio_client_add_cmd_option(client, "fio");
 
@@ -481,6 +496,7 @@ static int fio_client_send_ini(struct fio_client *client, const char *filename)
                return 1;
        }
 
+       client->sent_job = 1;
        ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, sb.st_size, 0);
        free(buf);
        close(fd);
@@ -497,6 +513,8 @@ int fio_clients_send_ini(const char *filename)
 
                if (fio_client_send_ini(client, filename))
                        remove_client(client);
+
+               client->sent_job = 1;
        }
 
        return !nr_clients;
@@ -978,8 +996,6 @@ static int fio_client_timed_out(void)
 
 int fio_handle_clients(void)
 {
-       struct fio_client *client;
-       struct flist_head *entry;
        struct pollfd *pfds;
        int i, ret = 0, retval = 0;
 
@@ -992,15 +1008,27 @@ int fio_handle_clients(void)
        init_group_run_stat(&client_gs);
 
        while (!exit_backend && nr_clients) {
+               struct flist_head *entry, *tmp;
+               struct fio_client *client;
+
                i = 0;
-               flist_for_each(entry, &client_list) {
+               flist_for_each_safe(entry, tmp, &client_list) {
                        client = flist_entry(entry, struct fio_client, list);
 
+                       if (!client->sent_job &&
+                           flist_empty(&client->cmd_list)) {
+                               remove_client(client);
+                               continue;
+                       }
+
                        pfds[i].fd = client->fd;
                        pfds[i].events = POLLIN;
                        i++;
                }
 
+               if (!nr_clients)
+                       break;
+
                assert(i == nr_clients);
 
                do {
@@ -1041,6 +1069,7 @@ int fio_handle_clients(void)
                                retval = 1;
                        } else if (client->error)
                                retval = 1;
+                       put_client(client);
                }
        }