diff options
| author | dec05eba <dec05eba@protonmail.com> | 2026-08-02 01:35:27 +0200 |
|---|---|---|
| committer | dec05eba <dec05eba@protonmail.com> | 2026-08-02 01:35:27 +0200 |
| commit | 9b799f6e45c5e7a44432824f69d9632ff5848e6c (patch) | |
| tree | 54e1a08470de0164ea6d5e95d9820dfde22637d5 /src/cli | |
| parent | d72113be395891f4289f0d86a32a37a99a4d5b38 (diff) | |
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.
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/ipc.c | 29 |
1 files 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); |
