server: fix for non zero appended strings
authorJens Axboe <axboe@kernel.dk>
Tue, 4 Oct 2011 08:31:53 +0000 (10:31 +0200)
committerJens Axboe <axboe@kernel.dk>
Tue, 4 Oct 2011 08:31:53 +0000 (10:31 +0200)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
client.c
server.c

index 464dce6..536ff31 100644 (file)
--- a/client.c
+++ b/client.c
@@ -27,6 +27,8 @@ struct fio_client {
 
 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 @@ static void client_signal_handler(void)
        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 @@ int fio_clients_connect(void)
                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 @@ static int fio_client_send_ini(struct fio_client *client, const char *filename)
                        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 @@ static void handle_probe(struct fio_net_cmd *cmd)
 {
        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 @@ static int handle_client(struct fio_client *client)
                        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 @@ static int handle_client(struct fio_client *client)
                        break;
                }
 
-               if (done)
+               if (done || one)
                        break;
        }
 
@@ -462,7 +480,7 @@ int fio_handle_clients(void)
                                log_err("fio: unknown client\n");
                                continue;
                        }
-                       handle_client(client);
+                       handle_client(client, 0);
                }
        }
 
index a48842e..a8b8258 100644 (file)
--- a/server.c
+++ b/server.c
@@ -160,9 +160,11 @@ struct fio_net_cmd *fio_net_recv_cmd(int sk, int block)
                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 @@ struct fio_net_cmd *fio_net_recv_cmd(int sk, int block)
        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 @@ static int send_quit_command(void)
        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 @@ static int handle_command(struct fio_net_cmd *cmd)
                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);