client: continue support for multiple connections

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/client.c b/client.c
index 368ad9e..054043d 100644
--- a/client.c
+++ b/client.c
@@ -26,7 +26,6 @@
 };
 
 static FLIST_HEAD(client_list);
-static unsigned int nr_clients;
 
 static struct fio_client *find_client_by_fd(int fd)
 {
@@ -43,6 +42,7 @@
 	return NULL;
 }
 
+#if 0
 static struct fio_client *find_client_by_name(const char *name)
 {
 	struct fio_client *client;
@@ -57,6 +57,7 @@
 
 	return NULL;
 }
+#endif
 
 static void remove_client(struct fio_client *client)
 {
@@ -67,21 +68,30 @@
 	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 +113,27 @@
 		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 +143,13 @@
  * 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 +184,21 @@
 	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;
diff --git a/fio.c b/fio.c
index bd566e8..0f12888 100644
--- a/fio.c
+++ b/fio.c
@@ -1694,7 +1694,7 @@
 
 int exec_run(void)
 {
-	if (is_client)
+	if (nr_clients)
 		return fio_handle_clients();
 	if (exec_profile && load_profile(exec_profile))
 		return 1;
diff --git a/fio.h b/fio.h
index e46a1a3..a6ef937 100644
--- a/fio.h
+++ b/fio.h
@@ -652,7 +652,7 @@
 extern int warnings_fatal;
 extern int terse_version;
 extern int is_backend;
-extern int is_client;
+extern int nr_clients;
 
 extern struct thread_data *threads;
 
diff --git a/init.c b/init.c
index 19f94e5..1dcb490 100644
--- a/init.c
+++ b/init.c
@@ -46,8 +46,7 @@
 int warnings_fatal = 0;
 int terse_version = 2;
 int is_backend = 0;
-int is_client = 0;
-char *client;
+int nr_clients = 0;
 
 int write_bw_log = 0;
 int read_only = 0;
@@ -1309,7 +1308,7 @@
 			}
 			break;
 		case 'S':
-			if (is_client) {
+			if (nr_clients) {
 				log_err("fio: can't be both client and server\n");
 				do_exit++;
 				exit_val = 1;
@@ -1327,8 +1326,7 @@
 				exit_val = 1;
 				break;
 			}
-			is_client = 1;
-			client = strdup(optarg);
+			fio_client_add(optarg);
 			break;
 		default:
 			do_exit++;
@@ -1340,7 +1338,7 @@
 	if (do_exit)
 		exit(exit_val);
 
-	if (is_client && fio_client_connect(client)) {
+	if (nr_clients && fio_clients_connect()) {
 		do_exit++;
 		exit_val = 1;
 		return 1;
@@ -1384,8 +1382,8 @@
 	for (i = 0; i < job_files; i++) {
 		if (fill_def_thread())
 			return 1;
-		if (is_client) {
-			if (fio_client_send_ini(client, ini_file[i]))
+		if (nr_clients) {
+			if (fio_clients_send_ini(ini_file[i]))
 				return 1;
 		} else {
 			if (parse_jobs_ini(ini_file[i], 0, i))
@@ -1402,7 +1400,7 @@
 			return 0;
 		if (exec_profile)
 			return 0;
-		if (is_backend || is_client)
+		if (is_backend || nr_clients)
 			return 0;
 
 		log_err("No jobs(s) defined\n\n");
diff --git a/server.h b/server.h
index 0f57ae8..8e6852a 100644
--- a/server.h
+++ b/server.h
@@ -43,9 +43,10 @@
 extern int fio_server_log(const char *format, ...);
 extern int fio_net_send_cmd(int, uint16_t, const char *, off_t);
 
-extern int fio_client_connect(const char *);
-extern int fio_client_send_ini(const char *, const char *);
+extern int fio_clients_connect(void);
+extern int fio_clients_send_ini(const char *);
 extern int fio_handle_clients(void);
+extern void fio_client_add(const char *);
 
 extern int fio_recv_data(int sk, void *p, unsigned int len);
 extern int fio_send_data(int sk, const void *p, unsigned int len);