discover: destroy client if message read failed
authorJeremy Kerr <jk@ozlabs.org>
Thu, 16 May 2013 08:09:37 +0000 (16:09 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 21 May 2013 07:28:33 +0000 (15:28 +0800)
When a client disconnects, the read from the client's fd will return
EOF. We should destroy the client in this situation.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/discover-server.c

index ca20113a22267d8ca23b0bd25c472234bd3682b7..138551ab2ddfa841a6e1533a884ee69541c02b30 100644 (file)
@@ -31,6 +31,7 @@ struct discover_server {
 struct client {
        struct discover_server *server;
        struct list_item list;
 struct client {
        struct discover_server *server;
        struct list_item list;
+       struct waiter *waiter;
        int fd;
 };
 
        int fd;
 };
 
@@ -55,6 +56,9 @@ static int client_destructor(void *arg)
        if (client->fd >= 0)
                close(client->fd);
 
        if (client->fd >= 0)
                close(client->fd);
 
+       if (client->waiter)
+               waiter_remove(client->waiter);
+
        list_remove(&client->list);
 
        return 0;
        list_remove(&client->list);
 
        return 0;
@@ -172,8 +176,11 @@ static int discover_server_process_message(void *arg)
 
        message = pb_protocol_read_message(client, client->fd);
 
 
        message = pb_protocol_read_message(client, client->fd);
 
-       if (!message)
+       if (!message) {
+               talloc_free(client);
                return 0;
                return 0;
+       }
+
 
        if (message->action != PB_PROTOCOL_ACTION_BOOT) {
                pb_log("%s: invalid action %d\n", __func__, message->action);
 
        if (message->action != PB_PROTOCOL_ACTION_BOOT) {
                pb_log("%s: invalid action %d\n", __func__, message->action);
@@ -207,7 +214,7 @@ static int discover_server_process_connection(void *arg)
        }
 
        /* add to our list of clients */
        }
 
        /* add to our list of clients */
-       client = talloc(server, struct client);
+       client = talloc_zero(server, struct client);
        list_add(&server->clients, &client->list);
 
        talloc_set_destructor(client, client_destructor);
        list_add(&server->clients, &client->list);
 
        talloc_set_destructor(client, client_destructor);
@@ -229,8 +236,8 @@ static int discover_server_process_connection(void *arg)
                                        opt->option);
        }
 
                                        opt->option);
        }
 
-       waiter_register(server->waitset, client->fd, WAIT_IN,
-                       discover_server_process_message, client);
+       client->waiter = waiter_register(server->waitset, client->fd, WAIT_IN,
+                               discover_server_process_message, client);
 
        return 0;
 }
 
        return 0;
 }