From df2509c0aaf7ce33de48535b03fc677574b93e14 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 17 Apr 2023 23:53:18 +0200 Subject: [PATCH] Kms server timeout, move back kms unix domain socket to HOME because flatpak cant access it otherwise --- kms/client/kms_client.c | 26 ++++++-------------------- kms/server/kms_server.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c index 9c17f80..2d1cd0f 100644 --- a/kms/client/kms_client.c +++ b/kms/client/kms_client.c @@ -79,33 +79,18 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) { return res; } +/* We have to use $HOME because in flatpak there is no simple path that is accessible, read and write, that multiple flatpak instances can access */ static bool create_socket_path(char *output_path, size_t output_path_size) { - // Can't use /tmp because of flatpak, but fallback to it if we fail to get a valid runtime dir - char runtime_dir_path[PATH_MAX]; - const char *runtime_dir = getenv("XDG_RUNTIME_DIR"); - const char *flatpak_id = getenv("FLATPAK_ID"); - if(runtime_dir) { - if(flatpak_id) - snprintf(runtime_dir_path, sizeof(runtime_dir_path), "%s/app/%s", runtime_dir, flatpak_id); - else - strcpy(runtime_dir_path, runtime_dir); - } else { - const uint32_t uid = getuid(); - if(flatpak_id) - snprintf(runtime_dir_path, sizeof(runtime_dir_path), "/run/user/%u/app/%s", uid, flatpak_id); - else - snprintf(runtime_dir_path, sizeof(runtime_dir_path), "/run/user/%u", uid); - } - - if(access(runtime_dir_path, F_OK) != 0) - strcpy(runtime_dir_path, "/tmp"); + const char *home = getenv("HOME"); + if(!home) + home = "/tmp"; char random_characters[11]; random_characters[10] = '\0'; if(!generate_random_characters(random_characters, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62)) return false; - snprintf(output_path, output_path_size, "%s/gsr-kms-socket-%s", runtime_dir_path, random_characters); + snprintf(output_path, output_path_size, "%s/.gsr-kms-socket-%s", home, random_characters); return true; } @@ -221,6 +206,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) { int wait_result = waitpid(self->kms_server_pid, &status, WNOHANG); if(wait_result != 0) { fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, error: %s\n", strerror(errno)); + self->kms_server_pid = -1; goto err; } } diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c index 7d0ed5c..dac7c31 100644 --- a/kms/server/kms_server.c +++ b/kms/server/kms_server.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -188,6 +189,14 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { return result; } +static double clock_get_monotonic_seconds(void) { + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 0; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001; +} + int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "usage: kms_server \n"); @@ -217,23 +226,38 @@ int main(int argc, char **argv) { } fprintf(stderr, "kms server info: connecting to the client\n"); - for(;;) { + bool connected = false; + const double connect_timeout_sec = 5.0; + const double start_time = clock_get_monotonic_seconds(); + while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) { struct sockaddr_un remote_addr = {0}; remote_addr.sun_family = AF_UNIX; strncpy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path)); // TODO: Check if parent disconnected if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) { - if(errno == ECONNREFUSED || errno == ENOENT) - continue; // Host not ready yet? TODO: sleep - if(errno == EISCONN) // TODO? + if(errno == ECONNREFUSED || errno == ENOENT) { + goto next; + } else if(errno == EISCONN) { + connected = true; break; + } fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno); close(drm.drmfd); return 2; } + + next: + usleep(30 * 1000); // 30 milliseconds + } + + if(connected) { + fprintf(stderr, "kms server info: connected to the client\n"); + } else { + fprintf(stderr, "kms server error: failed to connect to the client in %f seconds\n", connect_timeout_sec); + close(drm.drmfd); + return 2; } - fprintf(stderr, "kms server info: connected to the client\n"); int res = 0; for(;;) {