server: fix for non zero appended strings

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/client.c b/client.c
index 464dce6..536ff31 100644
--- a/client.c
+++ b/client.c
@@ -27,6 +27,8 @@
 
 static FLIST_HEAD(client_list);
 
+static int handle_client(struct fio_client *client, int one);
+
 static struct fio_client *find_client_by_fd(int fd)
 {
 	struct fio_client *client;
@@ -151,6 +153,12 @@
 	sigaction(SIGTERM, &act, NULL);
 }
 
+static void probe_client(struct fio_client *client)
+{
+	fio_net_send_simple_cmd(client->fd, FIO_NET_CMD_PROBE, 0);
+	handle_client(client, 1);
+}
+
 int fio_clients_connect(void)
 {
 	struct fio_client *client;
@@ -163,8 +171,12 @@
 		client = flist_entry(entry, struct fio_client, list);
 
 		ret = fio_client_connect(client);
-		if (ret)
+		if (ret) {
 			remove_client(client);
+			continue;
+		}
+
+		probe_client(client);
 	}
 
 	return !nr_clients;
@@ -217,6 +229,11 @@
 			continue;
 	} while (1);
 
+	if (len) {
+		log_err("fio: failed reading job file %s\n", filename);
+		return 1;
+	}
+
 	ret = send_file_buf(client, buf, sb.st_size);
 	free(buf);
 	return ret;
@@ -370,11 +387,11 @@
 {
 	struct cmd_probe_pdu *probe = (struct cmd_probe_pdu *) cmd->payload;
 
-	log_info("Probe: %s: %u.%u.%u\n", probe->hostname, probe->fio_major,
-					probe->fio_minor, probe->fio_patch);
+	log_info("Probe: hostname=%s, fio ver %u.%u.%u\n", probe->hostname,
+			probe->fio_major, probe->fio_minor, probe->fio_patch);
 }
 
-static int handle_client(struct fio_client *client)
+static int handle_client(struct fio_client *client, int one)
 {
 	struct fio_net_cmd *cmd;
 	int done = 0;
@@ -390,8 +407,9 @@
 			done = 1;
 			break;
 		case FIO_NET_CMD_TEXT:
-			fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
-			fflush(stdout);
+			fprintf(f_out, "Client <%s>: ", client->hostname);
+			fwrite(cmd->payload, cmd->pdu_len, 1, f_out);
+			fflush(f_out);
 			free(cmd);
 			break;
 		case FIO_NET_CMD_TS:
@@ -416,7 +434,7 @@
 			break;
 		}
 
-		if (done)
+		if (done || one)
 			break;
 	}
 
@@ -462,7 +480,7 @@
 				log_err("fio: unknown client\n");
 				continue;
 			}
-			handle_client(client);
+			handle_client(client, 0);
 		}
 	}
 
diff --git a/server.c b/server.c
index a48842e..a8b8258 100644
--- a/server.c
+++ b/server.c
@@ -160,9 +160,11 @@
 		if (ret)
 			break;
 
-		if (first)
-			cmd_size = sizeof(cmd) + cmd.pdu_len;
-		else
+		if (first) {
+			/* if this is text, add room for \0 at the end */
+			cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
+			assert(!cmdret);
+		} else
 			cmd_size += cmd.pdu_len;
 
 		cmdret = realloc(cmdret, cmd_size);
@@ -199,8 +201,17 @@
 	if (ret) {
 		free(cmdret);
 		cmdret = NULL;
-	} else if (cmdret)
+	} else if (cmdret) {
+		/* zero-terminate text input */
+		if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
+		    cmdret->opcode == FIO_NET_CMD_JOB)) {
+			char *buf = (char *) cmdret->payload;
+
+			buf[cmdret->pdu_len ] = '\0';
+		}
+		/* frag flag is internal */
 		cmdret->flags &= ~FIO_NET_CMD_F_MORE;
+	}
 
 	return cmdret;
 }
@@ -264,9 +275,9 @@
 	return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
 }
 
-static int handle_cur_job(struct fio_net_cmd *cmd)
+static int handle_job_cmd(struct fio_net_cmd *cmd)
 {
-	void *buf = cmd->payload;
+	char *buf = (char *) cmd->payload;
 	int ret;
 
 	parse_jobs_ini(buf, 1, 0);
@@ -303,7 +314,7 @@
 		exit_backend = 1;
 		return -1;
 	case FIO_NET_CMD_JOB:
-		ret = handle_cur_job(cmd);
+		ret = handle_job_cmd(cmd);
 		break;
 	case FIO_NET_CMD_PROBE:
 		ret = handle_probe_cmd(cmd);