From 8a34ec8c059ddc18fcd11b647f6a7642889a6c45 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 13 Aug 2023 13:53:52 +0900 Subject: [PATCH] Make socket filename unique per user --- src/client.c | 7 ++++++- src/common_constants.h | 2 +- src/socket.c | 13 ++++++++----- src/socket.h | 1 + 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/client.c b/src/client.c index 246cb5c..43b80b1 100644 --- a/src/client.c +++ b/src/client.c @@ -1,6 +1,7 @@ // standard library includes #include #include +#include // unix includes #include @@ -50,6 +51,10 @@ int main(int argc, char **argv) { return 1; } + char socket_filename[108]; + snprintf(socket_filename, sizeof(socket_filename), + UNIX_SOCKET_HANDLER_SOCKET_FMT_STRING, getenv("HOME")); + struct sockaddr_un addr; int ret; __attribute__((cleanup(cleanup_data_socket))) int data_socket = -1; @@ -68,7 +73,7 @@ int main(int argc, char **argv) { addr.sun_family = AF_UNIX; strncpy(addr.sun_path, - UNIX_SOCKET_HANDLER_SOCKET_NAME, + socket_filename, sizeof(addr.sun_path) - 1); ret = connect(data_socket, (const struct sockaddr*) &addr, sizeof(addr)); diff --git a/src/common_constants.h b/src/common_constants.h index b21f2c6..9a7a4cb 100644 --- a/src/common_constants.h +++ b/src/common_constants.h @@ -1,7 +1,7 @@ #ifndef OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_COMMON_CONSTANTS_H_ #define OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_COMMON_CONSTANTS_H_ -#define UNIX_SOCKET_HANDLER_SOCKET_NAME "/tmp/obs-studio-plugin-unix-socket-handler-socket" +#define UNIX_SOCKET_HANDLER_SOCKET_FMT_STRING "/tmp/%s_obs-studio-plugin-unix-socket-handler-socket" typedef enum UnixSocketEventType { UNIX_SOCKET_EVENT_NOP = 0, diff --git a/src/socket.c b/src/socket.c index 09a7df4..8e2f8fd 100644 --- a/src/socket.c +++ b/src/socket.c @@ -113,6 +113,9 @@ int unix_socket_handler_thread_function(void *ud) { void init_unix_socket_handler(UnixSocketHandler *handler) { memset(handler, 0, sizeof(UnixSocketHandler)); + snprintf(handler->socket_filename, sizeof(handler->socket_filename), + UNIX_SOCKET_HANDLER_SOCKET_FMT_STRING, getenv("HOME")); + umask(S_IRWXO); // Set up unix socket. @@ -132,7 +135,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) { handler->name.sun_family = AF_UNIX; strncpy(handler->name.sun_path, - UNIX_SOCKET_HANDLER_SOCKET_NAME, + handler->socket_filename, sizeof(handler->name.sun_path) - 1); ret = bind(handler->socket_descriptor, @@ -149,7 +152,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) { if (ret == -1) { close(handler->socket_descriptor); handler->socket_descriptor = -1; - unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); + unlink(handler->socket_filename); handler->flags = 0xFFFFFFFFFFFFFFFF; return; } @@ -160,7 +163,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) { if (ret != thrd_success) { close(handler->socket_descriptor); handler->socket_descriptor = -1; - unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); + unlink(handler->socket_filename); handler->flags = 0xFFFFFFFFFFFFFFFF; return; } @@ -172,7 +175,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) { if (ret != thrd_success) { close(handler->socket_descriptor); handler->socket_descriptor = -1; - unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); + unlink(handler->socket_filename); mtx_destroy(handler->mutex); handler->flags = 0xFFFFFFFFFFFFFFFF; return; @@ -189,7 +192,7 @@ void cleanup_unix_socket_handler(UnixSocketHandler *handler) { if (handler->socket_descriptor >= 0) { close(handler->socket_descriptor); handler->socket_descriptor = -1; - unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); + unlink(handler->socket_filename); } if (handler->mutex) { mtx_lock(handler->mutex); diff --git a/src/socket.h b/src/socket.h index b19a4c7..d756119 100644 --- a/src/socket.h +++ b/src/socket.h @@ -24,6 +24,7 @@ typedef struct UnixSocketHandler { * ???? 0001 - thread should stop */ uint64_t ccflags; + char socket_filename[108]; int socket_descriptor; } UnixSocketHandler;