aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenny Levinsen <kl@kl.wtf>2021-02-27 15:13:18 +0100
committerKenny Levinsen <kl@kl.wtf>2021-02-27 15:51:13 +0100
commit45bab8b258b1cb0ba68eff3bfc3913e1e5d84b7d (patch)
treeb5270751bd11f7a04154dc292b6c022a252924b3
parentb1f7ec1c4d753d0135cb88347efbc11c7039b2cb (diff)
client: Replace pending_disable with state enum
This simplifies logic in seat handling.
-rw-r--r--include/client.h10
-rw-r--r--seatd/client.c1
-rw-r--r--seatd/seat.c41
3 files changed, 30 insertions, 22 deletions
diff --git a/include/client.h b/include/client.h
index 3b1f105..8746bea 100644
--- a/include/client.h
+++ b/include/client.h
@@ -10,6 +10,14 @@
struct server;
+enum client_state {
+ CLIENT_NEW,
+ CLIENT_ACTIVE,
+ CLIENT_PENDING_DISABLE,
+ CLIENT_DISABLED,
+ CLIENT_CLOSED
+};
+
struct client {
struct linked_list link; // seat::clients
struct server *server;
@@ -22,7 +30,7 @@ struct client {
struct seat *seat;
int session;
- bool pending_disable;
+ enum client_state state;
struct linked_list devices;
};
diff --git a/seatd/client.c b/seatd/client.c
index a5a11ab..949bbd8 100644
--- a/seatd/client.c
+++ b/seatd/client.c
@@ -73,6 +73,7 @@ struct client *client_create(struct server *server, int client_fd) {
client->session = -1;
client->server = server;
client->connection.fd = client_fd;
+ client->state = CLIENT_NEW;
linked_list_init(&client->devices);
linked_list_insert(&server->idle_clients, &client->link);
return client;
diff --git a/seatd/seat.c b/seatd/seat.c
index a14eddd..0893db7 100644
--- a/seatd/seat.c
+++ b/seatd/seat.c
@@ -200,15 +200,12 @@ struct seat_device *seat_open_device(struct client *client, const char *path) {
assert(strlen(path) > 0);
struct seat *seat = client->seat;
- if (client != seat->active_client) {
- errno = EPERM;
- return NULL;
- }
-
- if (client->pending_disable) {
+ if (client->state != CLIENT_ACTIVE) {
+ log_error("client is not active");
errno = EPERM;
return NULL;
}
+ assert(seat->active_client == client);
char sanitized_path[PATH_MAX];
if (realpath(path, sanitized_path) == NULL) {
@@ -419,10 +416,15 @@ done:
int seat_open_client(struct seat *seat, struct client *client) {
assert(seat);
assert(client);
- assert(!client->pending_disable);
+
+ if (client->state != CLIENT_NEW && client->state != CLIENT_DISABLED) {
+ log_error("client is not new or disabled");
+ errno = EALREADY;
+ return -1;
+ }
if (seat->active_client != NULL) {
- log_error("client already active");
+ log_error("seat already has active client");
errno = EBUSY;
return -1;
}
@@ -439,6 +441,7 @@ int seat_open_client(struct seat *seat, struct client *client) {
}
}
+ client->state = CLIENT_ACTIVE;
seat->active_client = client;
if (client_send_enable_seat(client) == -1) {
log_error("could not send enable signal");
@@ -474,7 +477,7 @@ int seat_close_client(struct client *client) {
}
}
- client->pending_disable = false;
+ client->state = CLIENT_CLOSED;
seat->active_client = NULL;
log_debug("closed client");
@@ -491,17 +494,12 @@ static int seat_disable_client(struct client *client) {
struct seat *seat = client->seat;
- if (seat->active_client != client) {
+ if (client->state != CLIENT_ACTIVE) {
log_error("client not active");
errno = EBUSY;
return -1;
}
-
- if (client->pending_disable) {
- log_error("client already pending disable");
- errno = EBUSY;
- return -1;
- }
+ assert(seat->active_client = client);
// We *deactivate* all remaining fds. These may later be reactivated.
// The reason we cannot just close them is that certain device fds, such
@@ -515,7 +513,7 @@ static int seat_disable_client(struct client *client) {
}
}
- client->pending_disable = true;
+ client->state = CLIENT_PENDING_DISABLE;
if (client_send_disable_seat(seat->active_client) == -1) {
log_error("could not send disable event");
return -1;
@@ -530,13 +528,13 @@ int seat_ack_disable_client(struct client *client) {
assert(client->seat);
struct seat *seat = client->seat;
- if (!client->pending_disable) {
+ if (client->state != CLIENT_PENDING_DISABLE) {
log_error("client not pending disable");
errno = EBUSY;
return -1;
}
- client->pending_disable = false;
+ client->state = CLIENT_DISABLED;
log_debug("disabled client");
if (seat->active_client != client) {
@@ -558,11 +556,12 @@ int seat_set_next_session(struct client *client, int session) {
struct seat *seat = client->seat;
- if (seat->active_client != client || client->pending_disable) {
- log_error("client not active or pending disable");
+ if (client->state != CLIENT_ACTIVE) {
+ log_error("client is not active");
errno = EPERM;
return -1;
}
+ assert(seat->active_client == client);
if (session <= 0) {
log_errorf("invalid session value: %d", session);