aboutsummaryrefslogtreecommitdiff
path: root/kms/client
diff options
context:
space:
mode:
Diffstat (limited to 'kms/client')
-rw-r--r--kms/client/kms_client.c87
1 files changed, 58 insertions, 29 deletions
diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c
index d3e68dd..9fadf05 100644
--- a/kms/client/kms_client.c
+++ b/kms/client/kms_client.c
@@ -1,4 +1,5 @@
#include "kms_client.h"
+#include "../../include/utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -12,7 +13,6 @@
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/capability.h>
-#include <sys/random.h>
#define GSR_SOCKET_PAIR_LOCAL 0
#define GSR_SOCKET_PAIR_REMOTE 1
@@ -20,21 +20,6 @@
static void cleanup_socket(gsr_kms_client *self, bool kill_server);
static int gsr_kms_client_replace_connection(gsr_kms_client *self);
-static bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size) {
- /* TODO: Use other functions on other platforms than linux */
- if(getrandom(buffer, buffer_size, 0) < buffer_size) {
- fprintf(stderr, "Failed to get random bytes, error: %s\n", strerror(errno));
- return false;
- }
-
- for(int i = 0; i < buffer_size; ++i) {
- unsigned char c = *(unsigned char*)&buffer[i];
- buffer[i] = alphabet[c % alphabet_size];
- }
-
- return true;
-}
-
static void close_fds(gsr_kms_response *response) {
for(int i = 0; i < response->num_items; ++i) {
for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
@@ -139,20 +124,48 @@ static bool create_socket_path(char *output_path, size_t output_path_size) {
char random_characters[11];
random_characters[10] = '\0';
- if(!generate_random_characters(random_characters, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62))
+ if(!generate_random_characters_standard_alphabet(random_characters, 10))
return false;
snprintf(output_path, output_path_size, "%s/.gsr-kms-socket-%s", home, random_characters);
return true;
}
-static void string_copy(char *dst, const char *src, int len) {
- int src_len = strlen(src);
- int min_len = src_len;
- if(len - 1 < min_len)
- min_len = len - 1;
- memcpy(dst, src, min_len);
- dst[min_len] = '\0';
+static bool readlink_realpath(const char *filepath, char *buffer) {
+ char symlinked_path[PATH_MAX];
+ ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1);
+ if(bytes_written == -1 && errno == EINVAL) {
+ /* Not a symlink */
+ snprintf(symlinked_path, sizeof(symlinked_path), "%s", filepath);
+ } else if(bytes_written == -1) {
+ return false;
+ } else {
+ symlinked_path[bytes_written] = '\0';
+ }
+
+ if(!realpath(symlinked_path, buffer))
+ return false;
+
+ return true;
+}
+
+static bool strcat_safe(char *str, int size, const char *str_to_add) {
+ const int str_len = strlen(str);
+ const int str_to_add_len = strlen(str_to_add);
+ if(str_len + str_to_add_len + 1 >= size)
+ return false;
+
+ memcpy(str + str_len, str_to_add, str_to_add_len);
+ str[str_len + str_to_add_len] = '\0';
+ return true;
+}
+
+static void file_get_directory(char *filepath) {
+ char *end = strrchr(filepath, '/');
+ if(end == NULL)
+ filepath[0] = '\0';
+ else
+ *end = '\0';
}
static bool find_program_in_path(const char *program_name, char *filepath, int filepath_len) {
@@ -206,10 +219,26 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
char server_filepath[PATH_MAX];
- if(!find_program_in_path("gsr-kms-server", server_filepath, sizeof(server_filepath))) {
- fprintf(stderr, "gsr error: gsr_kms_client_init: gsr-kms-server is not installed\n");
+ if(!readlink_realpath("/proc/self/exe", server_filepath)) {
+ fprintf(stderr, "gsr error: gsr_kms_client_init: failed to resolve /proc/self/exe\n");
return -1;
}
+ file_get_directory(server_filepath);
+
+ if(!strcat_safe(server_filepath, sizeof(server_filepath), "/gsr-kms-server")) {
+ fprintf(stderr, "gsr error: gsr_kms_client_init: gsr-kms-server path too long\n");
+ return -1;
+ }
+
+ if(access(server_filepath, F_OK) != 0) {
+ fprintf(stderr, "gsr info: gsr_kms_client_init: gsr-kms-server is not installed in the same directory as gpu-screen-recorder (%s not found), looking for gsr-kms-server in PATH instead\n", server_filepath);
+ if(!find_program_in_path("gsr-kms-server", server_filepath, sizeof(server_filepath)) || access(server_filepath, F_OK) != 0) {
+ fprintf(stderr, "gsr error: gsr_kms_client_init: gsr-kms-server was not found in PATH. Please install gpu-screen-recorder properly\n");
+ return -1;
+ }
+ }
+
+ fprintf(stderr, "gsr info: gsr_kms_client_init: setting up connection to %s\n", server_filepath);
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
const char *home = getenv("HOME");
@@ -251,7 +280,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
local_addr.sun_family = AF_UNIX;
- string_copy(local_addr.sun_path, self->initial_socket_path, sizeof(local_addr.sun_path));
+ snprintf(local_addr.sun_path, sizeof(local_addr.sun_path), "%s", (const char*)self->initial_socket_path);
const mode_t prev_mask = umask(0000);
const int bind_res = bind(self->initial_socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path));
@@ -400,7 +429,7 @@ int gsr_kms_client_replace_connection(gsr_kms_client *self) {
}
if(response.version != GSR_KMS_PROTOCOL_VERSION) {
- fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: expected gsr-kms-server protocol version to be %u, but it's %u\n", GSR_KMS_PROTOCOL_VERSION, response.version);
+ fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: expected gsr-kms-server protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, response.version);
/*close_fds(response);*/
return -1;
}
@@ -435,7 +464,7 @@ int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) {
}
if(response->version != GSR_KMS_PROTOCOL_VERSION) {
- fprintf(stderr, "gsr error: gsr_kms_client_get_kms: expected gsr-kms-server protocol version to be %u, but it's %u\n", GSR_KMS_PROTOCOL_VERSION, response->version);
+ fprintf(stderr, "gsr error: gsr_kms_client_get_kms: expected gsr-kms-server protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, response->version);
/*close_fds(response);*/
strcpy(response->err_msg, "mismatching protocol version");
return -1;