Introduce free_ipc_client

This commit is contained in:
Orestis Floros 2018-04-23 01:02:44 +03:00
parent 715cea61af
commit b0bbe53d04
No known key found for this signature in database
GPG Key ID: E9AD9F32E401E38F
1 changed files with 19 additions and 18 deletions

View File

@ -38,6 +38,16 @@ static void set_nonblock(int sockfd) {
err(-1, "Could not set O_NONBLOCK"); err(-1, "Could not set O_NONBLOCK");
} }
static void free_ipc_client(ipc_client *client) {
close(client->fd);
for (int i = 0; i < client->num_events; i++){
free(client->events[i]);
}
free(client->events);
TAILQ_REMOVE(&all_clients, client, clients);
free(client);
}
/* /*
* Sends the specified event to all IPC clients which are currently connected * Sends the specified event to all IPC clients which are currently connected
* and subscribed to this kind of event. * and subscribed to this kind of event.
@ -99,12 +109,7 @@ void ipc_shutdown(shutdown_reason_t reason) {
while (!TAILQ_EMPTY(&all_clients)) { while (!TAILQ_EMPTY(&all_clients)) {
current = TAILQ_FIRST(&all_clients); current = TAILQ_FIRST(&all_clients);
shutdown(current->fd, SHUT_RDWR); shutdown(current->fd, SHUT_RDWR);
close(current->fd); free_ipc_client(current);
for (int i = 0; i < current->num_events; i++)
free(current->events[i]);
free(current->events);
TAILQ_REMOVE(&all_clients, current, clients);
free(current);
} }
} }
@ -1247,25 +1252,21 @@ static void ipc_receive_message(EV_P_ struct ev_io *w, int revents) {
return; return;
} }
/* If not, there was some kind of error. We dont bother /* If not, there was some kind of error. We dont bother and close the
* and close the connection */ * connection. Delete the client from the list of clients. */
close(w->fd); bool closed = false;
/* Delete the client from the list of clients */
ipc_client *current; ipc_client *current;
TAILQ_FOREACH(current, &all_clients, clients) { TAILQ_FOREACH(current, &all_clients, clients) {
if (current->fd != w->fd) if (current->fd != w->fd)
continue; continue;
for (int i = 0; i < current->num_events; i++) free_ipc_client(current);
free(current->events[i]); closed = true;
free(current->events);
/* We can call TAILQ_REMOVE because we break out of the
* TAILQ_FOREACH afterwards */
TAILQ_REMOVE(&all_clients, current, clients);
free(current);
break; break;
} }
if (!closed) {
close(w->fd);
}
ev_io_stop(EV_A_ w); ev_io_stop(EV_A_ w);
free(w); free(w);