Add protocol support for an arbitrary number of command line arguments

Make it more efficient as well, don't pass a lot of potentially
padded space, pass only the exact amount required.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/client.c b/client.c
index 8f8dc90..874f0ae 100644
--- a/client.c
+++ b/client.c
@@ -111,32 +111,25 @@
 	nr_clients--;
 }
 
-static int __fio_client_add_cmd_option(struct fio_client *client,
-				       const char *opt)
+static void __fio_client_add_cmd_option(struct fio_client *client,
+					const char *opt)
 {
 	int index;
 
-	if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) {
-		log_err("fio: max cmd line number reached.\n");
-		log_err("fio: cmd line <%s> has been ignored.\n", opt);
-		return 1;
-	}
-
 	index = client->argc++;
 	client->argv = realloc(client->argv, sizeof(char *) * client->argc);
 	client->argv[index] = strdup(opt);
 	dprint(FD_NET, "client: add cmd %d: %s\n", index, opt);
-	return 0;
 }
 
-int fio_client_add_cmd_option(void *cookie, const char *opt)
+void fio_client_add_cmd_option(void *cookie, const char *opt)
 {
 	struct fio_client *client = cookie;
 
 	if (!client || !opt)
-		return 0;
+		return;
 
-	return __fio_client_add_cmd_option(client, opt);
+	__fio_client_add_cmd_option(client, opt);
 }
 
 int fio_client_add(const char *hostname, void **cookie)
@@ -280,17 +273,41 @@
 
 static int send_client_cmd_line(struct fio_client *client)
 {
-	struct cmd_line_pdu *pdu;
+	struct cmd_single_line_pdu *cslp;
+	struct cmd_line_pdu *clp;
+	unsigned long offset;
+	void *pdu;
+	size_t mem;
 	int i, ret;
 
 	dprint(FD_NET, "client: send cmdline %d\n", client->argc);
 
-	pdu = malloc(sizeof(*pdu));
-	for (i = 0; i < client->argc; i++)
-		strcpy((char *) pdu->argv[i], client->argv[i]);
+	/*
+	 * Find out how much mem we need
+	 */
+	for (i = 0, mem = 0; i < client->argc; i++)
+		mem += strlen(client->argv[i]) + 1;
 
-	pdu->argc = cpu_to_le16(client->argc);
-	ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu));
+	/*
+	 * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu
+	 */
+	mem += sizeof(*clp) + (client->argc * sizeof(*cslp));
+
+	pdu = malloc(mem);
+	clp = pdu;
+	offset = sizeof(*clp);
+
+	for (i = 0; i < client->argc; i++) {
+		uint16_t arg_len = strlen(client->argv[i]) + 1;
+
+		cslp = pdu + offset;
+		strcpy((char *) cslp->text, client->argv[i]);
+		cslp->len = cpu_to_le16(arg_len);
+		offset += sizeof(*cslp) + arg_len;
+	}
+
+	clp->lines = cpu_to_le16(client->argc);
+	ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem);
 	free(pdu);
 	return ret;
 }
diff --git a/init.c b/init.c
index e3ff39c..e2f7bab 100644
--- a/init.c
+++ b/init.c
@@ -1243,9 +1243,9 @@
 	return 0;
 }
 
-int parse_cmd_client(void *client, char *opt)
+void parse_cmd_client(void *client, char *opt)
 {
-	return fio_client_add_cmd_option(client, opt);
+	fio_client_add_cmd_option(client, opt);
 }
 
 int parse_cmd_line(int argc, char *argv[])
@@ -1267,11 +1267,7 @@
 		did_arg = 1;
 
 		if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
-			if (parse_cmd_client(cur_client, argv[optind - 1])) {
-				exit_val = 1;
-				do_exit++;
-				break;
-			}
+			parse_cmd_client(cur_client, argv[optind - 1]);
 			c &= ~FIO_CLIENT_FLAG;
 		}
 
diff --git a/server.c b/server.c
index f9c3c57..a54db7b 100644
--- a/server.c
+++ b/server.c
@@ -102,7 +102,7 @@
 	cmd->pdu_len	= le32_to_cpu(cmd->pdu_len);
 
 	switch (cmd->version) {
-	case FIO_SERVER_VER3:
+	case FIO_SERVER_VER:
 		break;
 	default:
 		log_err("fio: bad server cmd version %d\n", cmd->version);
@@ -270,24 +270,36 @@
 
 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
 {
-	struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload;
-	char *argv[FIO_NET_CMD_JOBLINE_ARGV];
+	void *pdu = cmd->payload;
+	struct cmd_single_line_pdu *cslp;
+	struct cmd_line_pdu *clp;
+	unsigned long offset;
+	char **argv;
 	int ret, i;
 
-	pdu->argc = le16_to_cpu(pdu->argc);
+	clp = pdu;
+	clp->lines = le16_to_cpu(clp->lines);
+	argv = malloc(clp->lines * sizeof(char *));
+	offset = sizeof(*clp);
 
-	dprint(FD_NET, "server: %d command line args\n", pdu->argc);
+	dprint(FD_NET, "server: %d command line args\n", clp->lines);
 
-	for (i = 0; i < pdu->argc; i++) {
-		argv[i] = (char *) pdu->argv[i];
+	for (i = 0; i < clp->lines; i++) {
+		cslp = pdu + offset;
+		argv[i] = (char *) cslp->text;
+
+		offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
 		dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
 	}
 
-	if (parse_cmd_line(pdu->argc, argv)) {
+	if (parse_cmd_line(clp->lines, argv)) {
 		fio_server_send_quit_cmd();
+		free(argv);
 		return -1;
 	}
 
+	free(argv);
+
 	fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
 
 	ret = exec_run();
diff --git a/server.h b/server.h
index c6e9ce2..d68cbf5 100644
--- a/server.h
+++ b/server.h
@@ -26,8 +26,7 @@
 };
 
 enum {
-	FIO_SERVER_VER		= 3,
-	FIO_SERVER_VER3		= 3,
+	FIO_SERVER_VER		= 4,
 
 	FIO_SERVER_MAX_PDU	= 1024,
 
@@ -48,8 +47,6 @@
 	/* crc does not include the crc fields */
 	FIO_NET_CMD_CRC_SZ	= sizeof(struct fio_net_cmd) -
 					2 * sizeof(uint16_t),
-
-	FIO_NET_CMD_JOBLINE_ARGV	= 128,
 };
 
 struct cmd_ts_pdu {
@@ -67,9 +64,14 @@
 	uint8_t arch;
 };
 
+struct cmd_single_line_pdu {
+	uint16_t len;
+	uint8_t text[0];
+};
+
 struct cmd_line_pdu {
-	uint16_t argc;
-	uint8_t argv[FIO_NET_CMD_JOBLINE_ARGV][64];
+	uint16_t lines;
+	struct cmd_single_line_pdu options[0];
 };
 
 extern int fio_start_server(int);
@@ -91,7 +93,7 @@
 extern int fio_clients_send_ini(const char *);
 extern int fio_handle_clients(void);
 extern int fio_client_add(const char *, void **);
-extern int fio_client_add_cmd_option(void *, const char *);
+extern void fio_client_add_cmd_option(void *, 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);
@@ -106,7 +108,7 @@
 {
 	memset(cmd, 0, sizeof(*cmd));
 
-	cmd->version	= __cpu_to_le16(FIO_SERVER_VER3);
+	cmd->version	= __cpu_to_le16(FIO_SERVER_VER);
 	cmd->opcode	= cpu_to_le16(opcode);
 
 	if (pdu) {