Kms server timeout, move back kms unix domain socket to HOME because flatpak cant access it otherwise

This commit is contained in:
dec05eba 2023-04-17 23:53:18 +02:00
parent c0ebae365d
commit df2509c0aa
2 changed files with 35 additions and 25 deletions

View File

@ -79,33 +79,18 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
return res; 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) { 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 const char *home = getenv("HOME");
char runtime_dir_path[PATH_MAX]; if(!home)
const char *runtime_dir = getenv("XDG_RUNTIME_DIR"); home = "/tmp";
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");
char random_characters[11]; char random_characters[11];
random_characters[10] = '\0'; random_characters[10] = '\0';
if(!generate_random_characters(random_characters, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62)) if(!generate_random_characters(random_characters, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62))
return false; 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; 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); int wait_result = waitpid(self->kms_server_pid, &status, WNOHANG);
if(wait_result != 0) { if(wait_result != 0) {
fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, error: %s\n", strerror(errno)); 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; goto err;
} }
} }

View File

@ -10,6 +10,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <time.h>
#include <xf86drm.h> #include <xf86drm.h>
#include <xf86drmMode.h> #include <xf86drmMode.h>
@ -188,6 +189,14 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) {
return result; 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) { int main(int argc, char **argv) {
if(argc != 3) { if(argc != 3) {
fprintf(stderr, "usage: kms_server <domain_socket_path> <card_path>\n"); fprintf(stderr, "usage: kms_server <domain_socket_path> <card_path>\n");
@ -217,23 +226,38 @@ int main(int argc, char **argv) {
} }
fprintf(stderr, "kms server info: connecting to the client\n"); 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}; struct sockaddr_un remote_addr = {0};
remote_addr.sun_family = AF_UNIX; remote_addr.sun_family = AF_UNIX;
strncpy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path)); strncpy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path));
// TODO: Check if parent disconnected // 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(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) {
if(errno == ECONNREFUSED || errno == ENOENT) if(errno == ECONNREFUSED || errno == ENOENT) {
continue; // Host not ready yet? TODO: sleep goto next;
if(errno == EISCONN) // TODO? } else if(errno == EISCONN) {
connected = true;
break; break;
}
fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno); fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno);
close(drm.drmfd); close(drm.drmfd);
return 2; 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; int res = 0;
for(;;) { for(;;) {