mirror of
https://repo.dec05eba.com/gpu-screen-recorder
synced 2026-03-31 09:07:13 +09:00
Revert "Reverse kms server connect direction to fix umask issue"
This reverts commit e844421f6d.
This commit is contained in:
@@ -174,10 +174,12 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
|
|||||||
int result = -1;
|
int result = -1;
|
||||||
self->kms_server_pid = -1;
|
self->kms_server_pid = -1;
|
||||||
self->initial_socket_fd = -1;
|
self->initial_socket_fd = -1;
|
||||||
|
self->initial_client_fd = -1;
|
||||||
self->initial_socket_path[0] = '\0';
|
self->initial_socket_path[0] = '\0';
|
||||||
self->socket_pair[0] = -1;
|
self->socket_pair[0] = -1;
|
||||||
self->socket_pair[1] = -1;
|
self->socket_pair[1] = -1;
|
||||||
struct sockaddr_un local_addr = {0};
|
struct sockaddr_un local_addr = {0};
|
||||||
|
struct sockaddr_un remote_addr = {0};
|
||||||
|
|
||||||
if(!create_socket_path(self->initial_socket_path, sizeof(self->initial_socket_path))) {
|
if(!create_socket_path(self->initial_socket_path, sizeof(self->initial_socket_path))) {
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to create path to kms socket\n");
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to create path to kms socket\n");
|
||||||
@@ -229,6 +231,20 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
|
|||||||
local_addr.sun_family = AF_UNIX;
|
local_addr.sun_family = AF_UNIX;
|
||||||
strncpy_safe(local_addr.sun_path, self->initial_socket_path, sizeof(local_addr.sun_path));
|
strncpy_safe(local_addr.sun_path, self->initial_socket_path, sizeof(local_addr.sun_path));
|
||||||
|
|
||||||
|
const mode_t prev_mask = umask(0000);
|
||||||
|
int bind_res = bind(self->initial_socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path));
|
||||||
|
umask(prev_mask);
|
||||||
|
|
||||||
|
if(bind_res == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to bind socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(listen(self->initial_socket_fd, 1) == -1) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to listen on socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if(pid == -1) {
|
if(pid == -1) {
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_init: fork failed, error: %s\n", strerror(errno));
|
fprintf(stderr, "gsr error: gsr_kms_client_init: fork failed, error: %s\n", strerror(errno));
|
||||||
@@ -255,50 +271,41 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
|
|||||||
self->kms_server_pid = pid;
|
self->kms_server_pid = pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "gsr info: connecting to the server\n");
|
fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for server to connect\n");
|
||||||
bool connected = false;
|
for(;;) {
|
||||||
while(true) {
|
struct timeval tv;
|
||||||
struct sockaddr_un remote_addr = {0};
|
fd_set rfds;
|
||||||
remote_addr.sun_family = AF_UNIX;
|
FD_ZERO(&rfds);
|
||||||
strncpy_safe(remote_addr.sun_path, self->initial_socket_path, sizeof(remote_addr.sun_path));
|
FD_SET(self->initial_socket_fd, &rfds);
|
||||||
// TODO: Check if parent disconnected
|
|
||||||
if(connect(self->initial_socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) {
|
tv.tv_sec = 0;
|
||||||
if(errno == ECONNREFUSED || errno == ENOENT) {
|
tv.tv_usec = 100 * 1000; // 100 ms
|
||||||
goto next;
|
|
||||||
} else if(errno == EISCONN) {
|
int select_res = select(1 + self->initial_socket_fd, &rfds, NULL, NULL, &tv);
|
||||||
connected = true;
|
if(select_res > 0) {
|
||||||
break;
|
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) {
|
||||||
|
fprintf(stderr, "gsr error: gsr_kms_client_init: accept failed on socket, error: %s\n", strerror(errno));
|
||||||
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_init: connect failed, error: %s (%d)\n", strerror(errno), errno);
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
next:
|
|
||||||
if(connected)
|
|
||||||
break;
|
break;
|
||||||
|
} else {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
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) {
|
||||||
int exit_code = -1;
|
int exit_code = -1;
|
||||||
if(WIFEXITED(status))
|
if(WIFEXITED(status))
|
||||||
exit_code = WEXITSTATUS(status);
|
exit_code = WEXITSTATUS(status);
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, exit code: %d\n", exit_code);
|
fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, exit code: %d\n", exit_code);
|
||||||
self->kms_server_pid = -1;
|
self->kms_server_pid = -1;
|
||||||
if(exit_code != 0)
|
if(exit_code != 0)
|
||||||
result = exit_code;
|
result = exit_code;
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
usleep(30 * 1000); // 30 milliseconds
|
|
||||||
}
|
|
||||||
|
|
||||||
if(connected) {
|
|
||||||
fprintf(stderr, "gsr info: connected to the server\n");
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_init: failed to connect to the server\n");
|
|
||||||
goto err;
|
|
||||||
}
|
}
|
||||||
|
fprintf(stderr, "gsr info: gsr_kms_client_init: server connected\n");
|
||||||
|
|
||||||
fprintf(stderr, "gsr info: replacing file-backed unix domain socket with socketpair\n");
|
fprintf(stderr, "gsr info: replacing file-backed unix domain socket with socketpair\n");
|
||||||
if(gsr_kms_client_replace_connection(self) != 0)
|
if(gsr_kms_client_replace_connection(self) != 0)
|
||||||
@@ -315,6 +322,11 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cleanup_socket(gsr_kms_client *self, bool kill_server) {
|
void cleanup_socket(gsr_kms_client *self, bool kill_server) {
|
||||||
|
if(self->initial_client_fd != -1) {
|
||||||
|
close(self->initial_client_fd);
|
||||||
|
self->initial_client_fd = -1;
|
||||||
|
}
|
||||||
|
|
||||||
if(self->initial_socket_fd != -1) {
|
if(self->initial_socket_fd != -1) {
|
||||||
close(self->initial_socket_fd);
|
close(self->initial_socket_fd);
|
||||||
self->initial_socket_fd = -1;
|
self->initial_socket_fd = -1;
|
||||||
@@ -354,7 +366,7 @@ int gsr_kms_client_replace_connection(gsr_kms_client *self) {
|
|||||||
request.version = GSR_KMS_PROTOCOL_VERSION;
|
request.version = GSR_KMS_PROTOCOL_VERSION;
|
||||||
request.type = KMS_REQUEST_TYPE_REPLACE_CONNECTION;
|
request.type = KMS_REQUEST_TYPE_REPLACE_CONNECTION;
|
||||||
request.new_connection_fd = self->socket_pair[GSR_SOCKET_PAIR_REMOTE];
|
request.new_connection_fd = self->socket_pair[GSR_SOCKET_PAIR_REMOTE];
|
||||||
if(send_msg_to_server(self->initial_socket_fd, &request) == -1) {
|
if(send_msg_to_server(self->initial_client_fd, &request) == -1) {
|
||||||
fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to send request message to server\n");
|
fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to send request message to server\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
pid_t kms_server_pid;
|
pid_t kms_server_pid;
|
||||||
int initial_socket_fd;
|
int initial_socket_fd;
|
||||||
|
int initial_client_fd;
|
||||||
char initial_socket_path[PATH_MAX];
|
char initial_socket_path[PATH_MAX];
|
||||||
int socket_pair[2];
|
int socket_pair[2];
|
||||||
} gsr_kms_client;
|
} gsr_kms_client;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define GSR_KMS_PROTOCOL_VERSION 2
|
#define GSR_KMS_PROTOCOL_VERSION 1
|
||||||
#define GSR_KMS_MAX_PLANES 32
|
#define GSR_KMS_MAX_PLANES 32
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <xf86drm.h>
|
#include <xf86drm.h>
|
||||||
@@ -362,7 +361,6 @@ static void strncpy_safe(char *dst, const char *src, int len) {
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
int socket_fd = 0;
|
int socket_fd = 0;
|
||||||
int client_socket_fd = 0;
|
|
||||||
gsr_drm drm;
|
gsr_drm drm;
|
||||||
drm.drmfd = 0;
|
drm.drmfd = 0;
|
||||||
drm.planes = NULL;
|
drm.planes = NULL;
|
||||||
@@ -409,59 +407,36 @@ int main(int argc, char **argv) {
|
|||||||
c2crtc_map.num_maps = 0;
|
c2crtc_map.num_maps = 0;
|
||||||
map_crtc_to_connector_ids(&drm, &c2crtc_map);
|
map_crtc_to_connector_ids(&drm, &c2crtc_map);
|
||||||
|
|
||||||
struct sockaddr_un local_addr = {0};
|
fprintf(stderr, "kms server info: connecting to the client\n");
|
||||||
local_addr.sun_family = AF_UNIX;
|
|
||||||
strncpy_safe(local_addr.sun_path, domain_socket_path, sizeof(local_addr.sun_path));
|
|
||||||
|
|
||||||
const mode_t prev_mask = umask(0000);
|
|
||||||
const int bind_res = bind(socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path));
|
|
||||||
umask(prev_mask);
|
|
||||||
|
|
||||||
if(bind_res == -1) {
|
|
||||||
fprintf(stderr, "kms server error: failed to bind socket, error: %s\n", strerror(errno));
|
|
||||||
res = 2;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(listen(socket_fd, 1) == -1) {
|
|
||||||
fprintf(stderr, "kms server error: failed to listen on socket, error: %s\n", strerror(errno));
|
|
||||||
res = 2;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stderr, "kms server info: waiting for client to connect\n");
|
|
||||||
bool connected = false;
|
bool connected = false;
|
||||||
const double connect_timeout_sec = 5.0;
|
const double connect_timeout_sec = 5.0;
|
||||||
const double start_time = clock_get_monotonic_seconds();
|
const double start_time = clock_get_monotonic_seconds();
|
||||||
while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) {
|
while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) {
|
||||||
struct timeval tv;
|
struct sockaddr_un remote_addr = {0};
|
||||||
fd_set rfds;
|
remote_addr.sun_family = AF_UNIX;
|
||||||
FD_ZERO(&rfds);
|
strncpy_safe(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path));
|
||||||
FD_SET(socket_fd, &rfds);
|
// TODO: Check if parent disconnected
|
||||||
|
if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) {
|
||||||
tv.tv_sec = 0;
|
if(errno == ECONNREFUSED || errno == ENOENT) {
|
||||||
tv.tv_usec = 100 * 1000; // 100 ms
|
goto next;
|
||||||
|
} else if(errno == EISCONN) {
|
||||||
int select_res = select(1 + socket_fd, &rfds, NULL, NULL, &tv);
|
connected = true;
|
||||||
if(select_res > 0) {
|
break;
|
||||||
struct sockaddr_un remote_addr = {0};
|
|
||||||
socklen_t sock_len = 0;
|
|
||||||
client_socket_fd = accept(socket_fd, (struct sockaddr*)&remote_addr, &sock_len);
|
|
||||||
if(client_socket_fd == -1) {
|
|
||||||
fprintf(stderr, "kms server error: accept failed on socket, error: %s\n", strerror(errno));
|
|
||||||
res = 2;
|
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connected = true;
|
fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno);
|
||||||
break;
|
res = 2;
|
||||||
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
next:
|
||||||
|
usleep(30 * 1000); // 30 milliseconds
|
||||||
}
|
}
|
||||||
|
|
||||||
if(connected) {
|
if(connected) {
|
||||||
fprintf(stderr, "kms server info: client connected\n");
|
fprintf(stderr, "kms server info: connected to the client\n");
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "kms server error: client failed to connect in %f seconds\n", connect_timeout_sec);
|
fprintf(stderr, "kms server error: failed to connect to the client in %f seconds\n", connect_timeout_sec);
|
||||||
res = 2;
|
res = 2;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@@ -472,7 +447,7 @@ int main(int argc, char **argv) {
|
|||||||
request.type = -1;
|
request.type = -1;
|
||||||
request.new_connection_fd = 0;
|
request.new_connection_fd = 0;
|
||||||
|
|
||||||
const int recv_res = recv_msg_from_client(client_socket_fd, &request);
|
const int recv_res = recv_msg_from_client(socket_fd, &request);
|
||||||
if(recv_res == 0) {
|
if(recv_res == 0) {
|
||||||
fprintf(stderr, "kms server info: kms client shutdown, shutting down the server\n");
|
fprintf(stderr, "kms server info: kms client shutdown, shutting down the server\n");
|
||||||
res = 3;
|
res = 3;
|
||||||
@@ -504,29 +479,18 @@ int main(int argc, char **argv) {
|
|||||||
response.num_fds = 0;
|
response.num_fds = 0;
|
||||||
|
|
||||||
if(request.new_connection_fd > 0) {
|
if(request.new_connection_fd > 0) {
|
||||||
if(socket_fd > 0) {
|
if(socket_fd > 0)
|
||||||
close(socket_fd);
|
close(socket_fd);
|
||||||
socket_fd = -1;
|
socket_fd = request.new_connection_fd;
|
||||||
}
|
|
||||||
|
|
||||||
if(client_socket_fd > 0)
|
|
||||||
close(client_socket_fd);
|
|
||||||
|
|
||||||
client_socket_fd = request.new_connection_fd;
|
|
||||||
|
|
||||||
response.result = KMS_RESULT_OK;
|
response.result = KMS_RESULT_OK;
|
||||||
if(send_msg_to_client(client_socket_fd, &response) == -1)
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_REPLACE_CONNECTION request\n");
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_REPLACE_CONNECTION request\n");
|
||||||
|
|
||||||
if(domain_socket_path) {
|
|
||||||
remove(domain_socket_path);
|
|
||||||
domain_socket_path = NULL;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
response.result = KMS_RESULT_INVALID_REQUEST;
|
response.result = KMS_RESULT_INVALID_REQUEST;
|
||||||
snprintf(response.err_msg, sizeof(response.err_msg), "received invalid connection fd");
|
snprintf(response.err_msg, sizeof(response.err_msg), "received invalid connection fd");
|
||||||
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
||||||
if(send_msg_to_client(client_socket_fd, &response) == -1)
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,10 +502,10 @@ int main(int argc, char **argv) {
|
|||||||
response.num_fds = 0;
|
response.num_fds = 0;
|
||||||
|
|
||||||
if(kms_get_fb(&drm, &response, &c2crtc_map) == 0) {
|
if(kms_get_fb(&drm, &response, &c2crtc_map) == 0) {
|
||||||
if(send_msg_to_client(client_socket_fd, &response) == -1)
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
||||||
} else {
|
} else {
|
||||||
if(send_msg_to_client(client_socket_fd, &response) == -1)
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -559,7 +523,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
snprintf(response.err_msg, sizeof(response.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS");
|
snprintf(response.err_msg, sizeof(response.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS");
|
||||||
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
fprintf(stderr, "kms server error: %s\n", response.err_msg);
|
||||||
if(send_msg_to_client(client_socket_fd, &response) == -1)
|
if(send_msg_to_client(socket_fd, &response) == -1)
|
||||||
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
fprintf(stderr, "kms server error: failed to respond to client request\n");
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -572,11 +536,7 @@ int main(int argc, char **argv) {
|
|||||||
drmModeFreePlaneResources(drm.planes);
|
drmModeFreePlaneResources(drm.planes);
|
||||||
if(drm.drmfd > 0)
|
if(drm.drmfd > 0)
|
||||||
close(drm.drmfd);
|
close(drm.drmfd);
|
||||||
if(client_socket_fd > 0)
|
|
||||||
close(client_socket_fd);
|
|
||||||
if(socket_fd > 0)
|
if(socket_fd > 0)
|
||||||
close(socket_fd);
|
close(socket_fd);
|
||||||
if(domain_socket_path)
|
|
||||||
remove(domain_socket_path);
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user