aboutsummaryrefslogtreecommitdiff
path: root/kms/client/kms_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'kms/client/kms_client.c')
-rw-r--r--kms/client/kms_client.c205
1 files changed, 141 insertions, 64 deletions
diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c
index 57c6ccf..d7ddc78 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>
@@ -10,8 +11,14 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
+#include <poll.h>
#include <sys/stat.h>
+#ifdef __linux__
#include <sys/capability.h>
+#endif
+#ifdef __FreeBSD__
+#include <sys/sysctl.h>
+#endif
#define GSR_SOCKET_PAIR_LOCAL 0
#define GSR_SOCKET_PAIR_REMOTE 1
@@ -19,34 +26,18 @@
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) {
- int fd = open("/dev/urandom", O_RDONLY);
- if(fd == -1) {
- perror("/dev/urandom");
- return false;
- }
-
- if(read(fd, buffer, buffer_size) < buffer_size) {
- fprintf(stderr, "Failed to read %d bytes from /dev/urandom\n", buffer_size);
- close(fd);
- return false;
- }
-
- for(int i = 0; i < buffer_size; ++i) {
- unsigned char c = *(unsigned char*)&buffer[i];
- buffer[i] = alphabet[c % alphabet_size];
- }
-
- close(fd);
- return true;
-}
-
static void close_fds(gsr_kms_response *response) {
- for(int i = 0; i < response->num_fds; ++i) {
- if(response->fds[i].fd > 0)
- close(response->fds[i].fd);
- response->fds[i].fd = 0;
+ for(int i = 0; i < response->num_items; ++i) {
+ for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
+ gsr_kms_response_dma_buf *dma_buf = &response->items[i].dma_buf[j];
+ if(dma_buf->fd > 0) {
+ close(dma_buf->fd);
+ dma_buf->fd = -1;
+ }
+ }
+ response->items[i].num_dma_bufs = 0;
}
+ response->num_items = 0;
}
static int send_msg_to_server(int server_fd, gsr_kms_request *request) {
@@ -79,7 +70,7 @@ static int send_msg_to_server(int server_fd, gsr_kms_request *request) {
return sendmsg(server_fd, &response_message, 0);
}
-static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
+static int recv_msg_from_server(int server_pid, int server_fd, gsr_kms_response *response) {
struct iovec iov;
iov.iov_base = response;
iov.iov_len = sizeof(*response);
@@ -88,21 +79,40 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
response_message.msg_iov = &iov;
response_message.msg_iovlen = 1;
- char cmsgbuf[CMSG_SPACE(sizeof(int) * GSR_KMS_MAX_PLANES)];
+ char cmsgbuf[CMSG_SPACE(sizeof(int) * GSR_KMS_MAX_ITEMS * GSR_KMS_MAX_DMA_BUFS)];
memset(cmsgbuf, 0, sizeof(cmsgbuf));
response_message.msg_control = cmsgbuf;
response_message.msg_controllen = sizeof(cmsgbuf);
- int res = recvmsg(server_fd, &response_message, MSG_WAITALL);
- if(res <= 0)
- return res;
+ int res = 0;
+ for(;;) {
+ res = recvmsg(server_fd, &response_message, MSG_DONTWAIT);
+ if(res <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ // If we are replacing the connection and closing the application at the same time
+ // then recvmsg can get stuck (because the server died), so we prevent that by doing
+ // non-blocking recvmsg and checking if the server died
+ int status = 0;
+ int wait_result = waitpid(server_pid, &status, WNOHANG);
+ if(wait_result != 0) {
+ res = -1;
+ break;
+ }
+ usleep(1000);
+ } else {
+ break;
+ }
+ }
- if(response->num_fds > 0) {
+ if(res > 0 && response->num_items > 0) {
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
if(cmsg) {
int *fds = (int*)CMSG_DATA(cmsg);
- for(int i = 0; i < response->num_fds; ++i) {
- response->fds[i].fd = fds[i];
+ int fd_index = 0;
+ for(int i = 0; i < response->num_items; ++i) {
+ for(int j = 0; j < response->items[i].num_dma_bufs; ++j) {
+ gsr_kms_response_dma_buf *dma_buf = &response->items[i].dma_buf[j];
+ dma_buf->fd = fds[fd_index++];
+ }
}
} else {
close_fds(response);
@@ -114,26 +124,59 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
/* 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) {
+ const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
const char *home = getenv("HOME");
- if(!home)
+ // Portable home with AppImage can cause the socket path to be longer than 108 characters (unix domain socket path max length).
+ // Using gsr-kms-socket in $HOME is only needed in flatpak, so use /tmp everywhere else instead.
+ if(!home || !inside_flatpak)
home = "/tmp";
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 strncpy_safe(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';
+#ifdef __linux__
+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;
+}
+#endif
+
+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) {
@@ -187,11 +230,41 @@ 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");
+#ifdef __linux__
+ 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;
}
+#elif defined(__FreeBSD__)
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
+ size_t size = PATH_MAX;
+
+ if (sysctl(mib, 4, server_filepath, &size, NULL, 0) != 0) {
+ fprintf(stderr, "gsr error: gsr_kms_client_init: failed to resolve pathname using sysctl\n");
+ return -1;
+ }
+
+#else
+#error "Implement it by yourself"
+#endif
+ 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");
if(!home)
@@ -201,6 +274,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
if(geteuid() == 0) {
has_perm = true;
} else {
+#ifdef __linux__
cap_t kms_server_cap = cap_get_file(server_filepath);
if(kms_server_cap) {
cap_flag_value_t res = CAP_CLEAR;
@@ -218,6 +292,9 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
else
fprintf(stderr, "gsr info: gsr_kms_client_init: failed to get cap\n");
}
+#else
+ fprintf(stderr, "gsr info: gsr_kms_client_init: platform doesn't support cap\n");
+#endif
}
if(socketpair(AF_UNIX, SOCK_STREAM, 0, self->socket_pair) == -1) {
@@ -232,7 +309,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
local_addr.sun_family = AF_UNIX;
- strncpy_safe(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));
@@ -263,24 +340,23 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
const char *args[] = { "pkexec", server_filepath, self->initial_socket_path, card_path, NULL };
execvp(args[0], (char *const*)args);
}
- fprintf(stderr, "gsr error: gsr_kms_client_init: execvp failed, error: %s\n", strerror(errno));
+ fprintf(stderr, "gsr error: gsr_kms_client_init: failed to launch \"gsr-kms-server\", error: %s\n", strerror(errno));
_exit(127);
} else { /* parent */
self->kms_server_pid = pid;
}
+ // We need this dumb-shit retardation with unix domain socket and then replace it with socketpair because
+ // pkexec doesn't work with socketpair................
fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for server to connect\n");
+ struct pollfd poll_fd = {
+ .fd = self->initial_socket_fd,
+ .events = POLLIN,
+ .revents = 0
+ };
for(;;) {
- struct timeval tv;
- fd_set rfds;
- FD_ZERO(&rfds);
- FD_SET(self->initial_socket_fd, &rfds);
-
- tv.tv_sec = 0;
- tv.tv_usec = 100 * 1000; // 100 ms
-
- int select_res = select(1 + self->initial_socket_fd, &rfds, NULL, NULL, &tv);
- if(select_res > 0) {
+ int poll_res = poll(&poll_fd, 1, 100);
+ if(poll_res > 0 && (poll_fd.revents & POLLIN)) {
socklen_t sock_len = 0;
self->initial_client_fd = accept(self->initial_socket_fd, (struct sockaddr*)&remote_addr, &sock_len);
if(self->initial_client_fd == -1) {
@@ -320,12 +396,12 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
void cleanup_socket(gsr_kms_client *self, bool kill_server) {
- if(self->initial_client_fd != -1) {
+ if(self->initial_client_fd > 0) {
close(self->initial_client_fd);
self->initial_client_fd = -1;
}
- if(self->initial_socket_fd != -1) {
+ if(self->initial_socket_fd > 0) {
close(self->initial_socket_fd);
self->initial_socket_fd = -1;
}
@@ -339,8 +415,9 @@ void cleanup_socket(gsr_kms_client *self, bool kill_server) {
}
}
- if(kill_server && self->kms_server_pid != -1) {
+ if(kill_server && self->kms_server_pid > 0) {
kill(self->kms_server_pid, SIGKILL);
+ // TODO:
//int status;
//waitpid(self->kms_server_pid, &status, 0);
self->kms_server_pid = -1;
@@ -371,7 +448,7 @@ int gsr_kms_client_replace_connection(gsr_kms_client *self) {
return -1;
}
- const int recv_res = recv_msg_from_server(self->socket_pair[GSR_SOCKET_PAIR_LOCAL], &response);
+ const int recv_res = recv_msg_from_server(self->kms_server_pid, self->socket_pair[GSR_SOCKET_PAIR_LOCAL], &response);
if(recv_res == 0) {
fprintf(stderr, "gsr warning: gsr_kms_client_replace_connection: kms server shut down\n");
return -1;
@@ -381,7 +458,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;
}
@@ -404,7 +481,7 @@ int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) {
return -1;
}
- const int recv_res = recv_msg_from_server(self->socket_pair[GSR_SOCKET_PAIR_LOCAL], response);
+ const int recv_res = recv_msg_from_server(self->kms_server_pid, self->socket_pair[GSR_SOCKET_PAIR_LOCAL], response);
if(recv_res == 0) {
fprintf(stderr, "gsr warning: gsr_kms_client_get_kms: kms server shut down\n");
strcpy(response->err_msg, "failed to receive");
@@ -416,7 +493,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;