From 9b799f6e45c5e7a44432824f69d9632ff5848e6c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 2 Aug 2026 01:35:27 +0200 Subject: Only remove the ipc socket path when an unused socket exists there The socket that a killed GPU Screen Recorder instance leaves behind is removed so that the same ipc socket path can be used again, but this was done for any type of file, so using the path of a regular file as the ipc socket path deleted that file. --- src/cli/ipc.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cli/ipc.c b/src/cli/ipc.c index 6d555c0..5a43283 100644 --- a/src/cli/ipc.c +++ b/src/cli/ipc.c @@ -412,13 +412,32 @@ static bool ipc_socket_filepath_in_use(const char *socket_filepath) { return in_use; } +/* Removes the socket that a GPU Screen Recorder instance that was killed left behind, so the same ipc socket path can be used again */ +static bool ipc_remove_unused_socket(const char *socket_filepath) { + struct stat file_stat; + if(lstat(socket_filepath, &file_stat) == -1) + return true; + + if(!S_ISSOCK(file_stat.st_mode)) { + gsr_log(GSR_LOG_LEVEL_ERROR, "gsr_ipc_init: can't use \"%s\" as the ipc socket path because a file that isn't a socket already exists there", socket_filepath); + return false; + } + + if(ipc_socket_filepath_in_use(socket_filepath)) { + gsr_log(GSR_LOG_LEVEL_ERROR, "gsr_ipc_init: another program is already listening on \"%s\"", socket_filepath); + return false; + } + + unlink(socket_filepath); + return true; +} + static bool ipc_bind(gsr_ipc *self, const struct sockaddr_un *addr) { + if(!ipc_remove_unused_socket(self->socket_filepath)) + return false; + const mode_t prev_mask = umask(0777 & ~GSR_IPC_SOCKET_MODE); - int bind_result = bind(self->socket_fd, (const struct sockaddr*)addr, sizeof(*addr)); - if(bind_result == -1 && errno == EADDRINUSE && !ipc_socket_filepath_in_use(self->socket_filepath)) { - unlink(self->socket_filepath); - bind_result = bind(self->socket_fd, (const struct sockaddr*)addr, sizeof(*addr)); - } + const int bind_result = bind(self->socket_fd, (const struct sockaddr*)addr, sizeof(*addr)); const int bind_error = errno; umask(prev_mask); -- cgit v1.2.3