client: More robust handling of client links

This commit is contained in:
Kenny Levinsen 2020-11-23 01:03:37 +01:00
parent df8494af61
commit e0782a825e
3 changed files with 6 additions and 14 deletions

View file

@ -70,6 +70,7 @@ struct client *client_create(struct server *server, int client_fd) {
client->server = server; client->server = server;
client->connection.fd = client_fd; client->connection.fd = client_fd;
linked_list_init(&client->devices); linked_list_init(&client->devices);
linked_list_insert(&server->idle_clients, &client->link);
return client; return client;
} }
@ -80,14 +81,9 @@ void client_destroy(struct client *client) {
close(client->connection.fd); close(client->connection.fd);
client->connection.fd = -1; client->connection.fd = -1;
} }
linked_list_remove(&client->link);
if (client->seat != NULL) { if (client->seat != NULL) {
// This should also close and remove all devices. This unlinks
// the client.
seat_remove_client(client); seat_remove_client(client);
} else {
// If we are not a member of a seat, we will be on the idle
// clients list, so unlink the client manually.
linked_list_remove(&client->link);
} }
if (client->event_source != NULL) { if (client->event_source != NULL) {
event_source_fd_destroy(client->event_source); event_source_fd_destroy(client->event_source);
@ -149,6 +145,7 @@ static int handle_open_seat(struct client *client) {
log_errorf("unable to add client to target seat: %s", strerror(errno)); log_errorf("unable to add client to target seat: %s", strerror(errno));
return -1; return -1;
} }
linked_list_insert(&seat->clients, &client->link);
size_t seat_name_len = strlen(seat_name); size_t seat_name_len = strlen(seat_name);
@ -177,6 +174,7 @@ static int handle_close_seat(struct client *client) {
return -1; return -1;
} }
linked_list_remove(&client->link);
if (seat_remove_client(client) == -1) { if (seat_remove_client(client) == -1) {
log_error("unable to remove client from seat"); log_error("unable to remove client from seat");
return -1; return -1;

View file

@ -149,9 +149,8 @@ int seat_add_client(struct seat *seat, struct client *client) {
log_debugf("registered client %p as session %d", (void *)client, client->session); log_debugf("registered client %p as session %d", (void *)client, client->session);
client->seat = seat; client->seat = seat;
linked_list_insert(&seat->clients, &client->link);
log_debug("added client"); log_debug("added client");
return 0; return 0;
} }
@ -160,10 +159,6 @@ int seat_remove_client(struct client *client) {
assert(client->seat); assert(client->seat);
struct seat *seat = client->seat; struct seat *seat = client->seat;
// We must first remove the client to avoid reactivation
linked_list_remove(&client->link);
if (seat->next_client == client) { if (seat->next_client == client) {
seat->next_client = NULL; seat->next_client = NULL;
} }

View file

@ -132,13 +132,12 @@ int server_add_client(struct server *server, int fd) {
client->event_source = client->event_source =
poller_add_fd(&server->poller, fd, EVENT_READABLE, client_handle_connection, client); poller_add_fd(&server->poller, fd, EVENT_READABLE, client_handle_connection, client);
if (client->event_source == NULL) { if (client->event_source == NULL) {
client_destroy(client);
log_errorf("could not add client socket to poller: %s", strerror(errno)); log_errorf("could not add client socket to poller: %s", strerror(errno));
client_destroy(client);
return -1; return -1;
} }
log_infof("new client connected (pid: %d, uid: %d, gid: %d)", client->pid, client->uid, log_infof("new client connected (pid: %d, uid: %d, gid: %d)", client->pid, client->uid,
client->gid); client->gid);
linked_list_insert(&server->idle_clients, &client->link);
return 0; return 0;
} }