client: re-setup poll fd array in case a client disappears
[fio.git] / client.c
index 368ad9e7bed81a9ac550fc1eb7f5efef182c65c3..c080c17d03fadaf36466398bb7c062c0bb15b5d0 100644 (file)
--- a/client.c
+++ b/client.c
@@ -26,7 +26,6 @@ struct fio_client {
 };
 
 static FLIST_HEAD(client_list);
-static unsigned int nr_clients;
 
 static struct fio_client *find_client_by_fd(int fd)
 {
@@ -43,6 +42,7 @@ static struct fio_client *find_client_by_fd(int fd)
        return NULL;
 }
 
+#if 0
 static struct fio_client *find_client_by_name(const char *name)
 {
        struct fio_client *client;
@@ -57,31 +57,40 @@ static struct fio_client *find_client_by_name(const char *name)
 
        return NULL;
 }
+#endif
 
 static void remove_client(struct fio_client *client)
 {
-       printf("remove client %s\n", client->hostname);
        flist_del(&client->list);
        nr_clients--;
        free(client->hostname);
        free(client);
 }
 
-int fio_client_connect(const char *host)
+void fio_client_add(const char *hostname)
 {
        struct fio_client *client;
-       int fd;
 
        client = malloc(sizeof(*client));
+       memset(client, 0, sizeof(*client));
+       client->hostname = strdup(hostname);
+       client->fd = -1;
+       flist_add(&client->list, &client_list);
+       nr_clients++;
+}
+
+static int fio_client_connect(struct fio_client *client)
+{
+       int fd;
 
        memset(&client->addr, 0, sizeof(client->addr));
        client->addr.sin_family = AF_INET;
        client->addr.sin_port = htons(fio_net_port);
 
-       if (inet_aton(host, &client->addr.sin_addr) != 1) {
+       if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) {
                struct hostent *hent;
 
-               hent = gethostbyname(host);
+               hent = gethostbyname(client->hostname);
                if (!hent) {
                        log_err("fio: gethostbyname: %s\n", strerror(errno));
                        return 1;
@@ -103,13 +112,27 @@ int fio_client_connect(const char *host)
                return 1;
        }
 
-       client->hostname = strdup(host);
-       flist_add(&client->list, &client_list);
-       nr_clients++;
        client->fd = fd;
        return 0;
 }
 
+int fio_clients_connect(void)
+{
+       struct fio_client *client;
+       struct flist_head *entry, *tmp;
+       int ret;
+
+       flist_for_each_safe(entry, tmp, &client_list) {
+               client = flist_entry(entry, struct fio_client, list);
+
+               ret = fio_client_connect(client);
+               if (ret)
+                       remove_client(client);
+       }
+
+       return !nr_clients;
+}
+
 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
 {
        return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
@@ -119,20 +142,13 @@ static int send_file_buf(struct fio_client *client, char *buf, off_t size)
  * Send file contents to server backend. We could use sendfile(), but to remain
  * more portable lets just read/write the darn thing.
  */
-int fio_client_send_ini(const char *hostname, const char *filename)
+static int fio_client_send_ini(struct fio_client *client, const char *filename)
 {
-       struct fio_client *client;
        struct stat sb;
        char *p, *buf;
        off_t len;
        int fd, ret;
 
-       client = find_client_by_name(hostname);
-       if (!client) {
-               log_err("fio: can't find client for host %s\n", hostname);
-               return 1;
-       }
-
        fd = open(filename, O_RDONLY);
        if (fd < 0) {
                log_err("fio: job file open: %s\n", strerror(errno));
@@ -167,6 +183,21 @@ int fio_client_send_ini(const char *hostname, const char *filename)
        return ret;
 }
 
+int fio_clients_send_ini(const char *filename)
+{
+       struct fio_client *client;
+       struct flist_head *entry, *tmp;
+
+       flist_for_each_safe(entry, tmp, &client_list) {
+               client = flist_entry(entry, struct fio_client, list);
+
+               if (fio_client_send_ini(client, filename))
+                       remove_client(client);
+       }
+
+       return !nr_clients;
+}
+
 static int handle_client(struct fio_client *client)
 {
        struct fio_net_cmd *cmd;
@@ -199,19 +230,22 @@ int fio_handle_clients(void)
        struct fio_client *client;
        struct flist_head *entry;
        struct pollfd *pfds;
-       int i = 0, ret = 0;
+       int i, ret = 0;
 
        pfds = malloc(nr_clients * sizeof(struct pollfd));
 
-       flist_for_each(entry, &client_list) {
-               client = flist_entry(entry, struct fio_client, list);
+       while (!exit_backend && nr_clients) {
+               i = 0;
+               flist_for_each(entry, &client_list) {
+                       client = flist_entry(entry, struct fio_client, list);
 
-               pfds[i].fd = client->fd;
-               pfds[i].events = POLLIN;
-               i++;
-       }
+                       pfds[i].fd = client->fd;
+                       pfds[i].events = POLLIN;
+                       i++;
+               }
+
+               assert(i == nr_clients);
 
-       while (!exit_backend && nr_clients) {
                ret = poll(pfds, nr_clients, 100);
                if (ret < 0) {
                        if (errno == EINTR)