server: malloc/free fix
[fio.git] / client.c
index 054043d4eb423f913009644434c062fe77b0959b..05bba38d60d54d1a8f9facb7433da41ab07d0a7c 100644 (file)
--- a/client.c
+++ b/client.c
@@ -61,7 +61,7 @@ static struct fio_client *find_client_by_name(const char *name)
 
 static void remove_client(struct fio_client *client)
 {
-       printf("remove client %s\n", client->hostname);
+       dprint(FD_NET, "removed client <%s>\n", client->hostname);
        flist_del(&client->list);
        nr_clients--;
        free(client->hostname);
@@ -72,6 +72,7 @@ void fio_client_add(const char *hostname)
 {
        struct fio_client *client;
 
+       dprint(FD_NET, "added client <%s>\n", hostname);
        client = malloc(sizeof(*client));
        memset(client, 0, sizeof(*client));
        client->hostname = strdup(hostname);
@@ -84,6 +85,8 @@ static int fio_client_connect(struct fio_client *client)
 {
        int fd;
 
+       dprint(FD_NET, "connect to host %s\n", client->hostname);
+
        memset(&client->addr, 0, sizeof(client->addr));
        client->addr.sin_family = AF_INET;
        client->addr.sin_port = htons(fio_net_port);
@@ -103,13 +106,11 @@ static int fio_client_connect(struct fio_client *client)
        fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0) {
                log_err("fio: socket: %s\n", strerror(errno));
-               free(client);
                return 1;
        }
 
        if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
                log_err("fio: connect: %s\n", strerror(errno));
-               free(client);
                return 1;
        }
 
@@ -150,6 +151,8 @@ static int fio_client_send_ini(struct fio_client *client, const char *filename)
        off_t len;
        int fd, ret;
 
+       dprint(FD_NET, "send ini %s to %s\n", filename, client->hostname);
+
        fd = open(filename, O_RDONLY);
        if (fd < 0) {
                log_err("fio: job file open: %s\n", strerror(errno));
@@ -204,6 +207,9 @@ static int handle_client(struct fio_client *client)
        struct fio_net_cmd *cmd;
 
        while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
+               dprint(FD_NET, "%s: got cmd op %d\n", client->hostname,
+                                                       cmd->opcode);
+
                if (cmd->opcode == FIO_NET_CMD_ACK) {
                        free(cmd);
                        continue;
@@ -231,19 +237,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)
@@ -267,6 +276,5 @@ int fio_handle_clients(void)
        }
 
        free(pfds);
-
        return 0;
 }