Make socket filename unique per user

This commit is contained in:
Stephen Seo 2023-08-13 13:53:52 +09:00
parent 6574341870
commit 8a34ec8c05
4 changed files with 16 additions and 7 deletions

View file

@ -1,6 +1,7 @@
// standard library includes // standard library includes
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
// unix includes // unix includes
#include <sys/socket.h> #include <sys/socket.h>
@ -50,6 +51,10 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
char socket_filename[108];
snprintf(socket_filename, sizeof(socket_filename),
UNIX_SOCKET_HANDLER_SOCKET_FMT_STRING, getenv("HOME"));
struct sockaddr_un addr; struct sockaddr_un addr;
int ret; int ret;
__attribute__((cleanup(cleanup_data_socket))) int data_socket = -1; __attribute__((cleanup(cleanup_data_socket))) int data_socket = -1;
@ -68,7 +73,7 @@ int main(int argc, char **argv) {
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, strncpy(addr.sun_path,
UNIX_SOCKET_HANDLER_SOCKET_NAME, socket_filename,
sizeof(addr.sun_path) - 1); sizeof(addr.sun_path) - 1);
ret = connect(data_socket, (const struct sockaddr*) &addr, sizeof(addr)); ret = connect(data_socket, (const struct sockaddr*) &addr, sizeof(addr));

View file

@ -1,7 +1,7 @@
#ifndef OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_COMMON_CONSTANTS_H_ #ifndef OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_COMMON_CONSTANTS_H_
#define 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 { typedef enum UnixSocketEventType {
UNIX_SOCKET_EVENT_NOP = 0, UNIX_SOCKET_EVENT_NOP = 0,

View file

@ -113,6 +113,9 @@ int unix_socket_handler_thread_function(void *ud) {
void init_unix_socket_handler(UnixSocketHandler *handler) { void init_unix_socket_handler(UnixSocketHandler *handler) {
memset(handler, 0, sizeof(UnixSocketHandler)); memset(handler, 0, sizeof(UnixSocketHandler));
snprintf(handler->socket_filename, sizeof(handler->socket_filename),
UNIX_SOCKET_HANDLER_SOCKET_FMT_STRING, getenv("HOME"));
umask(S_IRWXO); umask(S_IRWXO);
// Set up unix socket. // Set up unix socket.
@ -132,7 +135,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) {
handler->name.sun_family = AF_UNIX; handler->name.sun_family = AF_UNIX;
strncpy(handler->name.sun_path, strncpy(handler->name.sun_path,
UNIX_SOCKET_HANDLER_SOCKET_NAME, handler->socket_filename,
sizeof(handler->name.sun_path) - 1); sizeof(handler->name.sun_path) - 1);
ret = bind(handler->socket_descriptor, ret = bind(handler->socket_descriptor,
@ -149,7 +152,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) {
if (ret == -1) { if (ret == -1) {
close(handler->socket_descriptor); close(handler->socket_descriptor);
handler->socket_descriptor = -1; handler->socket_descriptor = -1;
unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); unlink(handler->socket_filename);
handler->flags = 0xFFFFFFFFFFFFFFFF; handler->flags = 0xFFFFFFFFFFFFFFFF;
return; return;
} }
@ -160,7 +163,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) {
if (ret != thrd_success) { if (ret != thrd_success) {
close(handler->socket_descriptor); close(handler->socket_descriptor);
handler->socket_descriptor = -1; handler->socket_descriptor = -1;
unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); unlink(handler->socket_filename);
handler->flags = 0xFFFFFFFFFFFFFFFF; handler->flags = 0xFFFFFFFFFFFFFFFF;
return; return;
} }
@ -172,7 +175,7 @@ void init_unix_socket_handler(UnixSocketHandler *handler) {
if (ret != thrd_success) { if (ret != thrd_success) {
close(handler->socket_descriptor); close(handler->socket_descriptor);
handler->socket_descriptor = -1; handler->socket_descriptor = -1;
unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); unlink(handler->socket_filename);
mtx_destroy(handler->mutex); mtx_destroy(handler->mutex);
handler->flags = 0xFFFFFFFFFFFFFFFF; handler->flags = 0xFFFFFFFFFFFFFFFF;
return; return;
@ -189,7 +192,7 @@ void cleanup_unix_socket_handler(UnixSocketHandler *handler) {
if (handler->socket_descriptor >= 0) { if (handler->socket_descriptor >= 0) {
close(handler->socket_descriptor); close(handler->socket_descriptor);
handler->socket_descriptor = -1; handler->socket_descriptor = -1;
unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME); unlink(handler->socket_filename);
} }
if (handler->mutex) { if (handler->mutex) {
mtx_lock(handler->mutex); mtx_lock(handler->mutex);

View file

@ -24,6 +24,7 @@ typedef struct UnixSocketHandler {
* ???? 0001 - thread should stop * ???? 0001 - thread should stop
*/ */
uint64_t ccflags; uint64_t ccflags;
char socket_filename[108];
int socket_descriptor; int socket_descriptor;
} UnixSocketHandler; } UnixSocketHandler;