diff options
-rw-r--r-- | lib/private.h | 5 | ||||
-rw-r--r-- | lib/receiver.c | 11 | ||||
-rw-r--r-- | lib/sender.c | 7 | ||||
-rw-r--r-- | lib/utils.c | 42 | ||||
-rw-r--r-- | tests/client-server.c | 2 | ||||
-rw-r--r-- | tests/sender-receiver.c | 2 |
6 files changed, 57 insertions, 12 deletions
diff --git a/lib/private.h b/lib/private.h index 55d87a3..06f601a 100644 --- a/lib/private.h +++ b/lib/private.h @@ -48,6 +48,11 @@ icipc_log (enum icipc_log_level level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); +/* socket path */ + +int +icipc_construct_socket_path (const char *name, char *buf, size_t buf_size); + /* socket */ ssize_t diff --git a/lib/receiver.c b/lib/receiver.c index 4592916..b58e8be 100644 --- a/lib/receiver.c +++ b/lib/receiver.c @@ -107,14 +107,12 @@ icipc_receiver_new (const char *path, size_t user_size) { struct icipc_receiver *self; - int name_size; + int res; /* check params */ if (path == NULL || buffer_size == 0) return NULL; - unlink (path); - self = calloc (1, sizeof (struct icipc_receiver) + user_size); if (self == NULL) return NULL; @@ -123,11 +121,12 @@ icipc_receiver_new (const char *path, /* set address */ self->addr.sun_family = AF_LOCAL; - name_size = snprintf(self->addr.sun_path, sizeof(self->addr.sun_path), "%s", - path) + 1; - if (name_size > (int) sizeof(self->addr.sun_path)) + res = icipc_construct_socket_path (path, self->addr.sun_path, sizeof(self->addr.sun_path)); + if (res < 0) goto error; + unlink (self->addr.sun_path); + /* create socket */ self->socket_fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); diff --git a/lib/sender.c b/lib/sender.c index 7b5b0c9..7d2cd04 100644 --- a/lib/sender.c +++ b/lib/sender.c @@ -118,7 +118,7 @@ icipc_sender_new (const char *path, size_t user_size) { struct icipc_sender *self; - int name_size; + int res; if (path == NULL) return NULL; @@ -131,9 +131,8 @@ icipc_sender_new (const char *path, /* set address */ self->addr.sun_family = AF_LOCAL; - name_size = snprintf(self->addr.sun_path, sizeof(self->addr.sun_path), "%s", - path) + 1; - if (name_size > (int) sizeof(self->addr.sun_path)) + res = icipc_construct_socket_path (path, self->addr.sun_path, sizeof(self->addr.sun_path)); + if (res < 0) goto error; /* create socket */ diff --git a/lib/utils.c b/lib/utils.c index 7c683d1..6390ec5 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -18,6 +18,7 @@ #include <time.h> #include <errno.h> #include <assert.h> +#include <pwd.h> #include "private.h" @@ -89,6 +90,47 @@ icipc_log (enum icipc_log_level level, const char *fmt, ...) va_end (args); } +/* socket path */ + +int +icipc_construct_socket_path (const char *name, char *buf, size_t buf_size) +{ + bool path_is_absolute; + const char *runtime_dir = NULL; + struct passwd pwd, *result = NULL; + char buffer[4096]; + int name_size; + + path_is_absolute = name[0] == '/'; + + if (!path_is_absolute) { + runtime_dir = getenv("PIPEWIRE_RUNTIME_DIR"); + if (runtime_dir == NULL) + runtime_dir = getenv("XDG_RUNTIME_DIR"); + if (runtime_dir == NULL) + runtime_dir = getenv("HOME"); + if (runtime_dir == NULL) + runtime_dir = getenv("USERPROFILE"); + if (runtime_dir == NULL) { + if (getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &result) == 0) + runtime_dir = result ? result->pw_dir : NULL; + } + } + + if (runtime_dir == NULL && !path_is_absolute) + return -ENOENT; + + if (path_is_absolute) + name_size = snprintf (buf, buf_size, "%s", name) + 1; + else + name_size = snprintf (buf, buf_size, "%s/%s", runtime_dir, name) + 1; + + if (name_size > (int) buf_size) + return -ENAMETOOLONG; + + return 0; +} + /* socket */ ssize_t diff --git a/tests/client-server.c b/tests/client-server.c index ca6d1c3..241804c 100644 --- a/tests/client-server.c +++ b/tests/client-server.c @@ -11,7 +11,7 @@ #include <spa/pod/parser.h> #include <icipc/icipc.h> -#define TEST_ADDRESS "/tmp/icipc-client-server" +#define TEST_ADDRESS "icipc-client-server" static bool increment_request_handler (struct icipc_server *self, int client_fd, diff --git a/tests/sender-receiver.c b/tests/sender-receiver.c index bd8721d..4d96496 100644 --- a/tests/sender-receiver.c +++ b/tests/sender-receiver.c @@ -9,7 +9,7 @@ #include <glib.h> #include <icipc/icipc.h> -#define TEST_ADDRESS "/tmp/icipc-sender-receiver" +#define TEST_ADDRESS "icipc-sender-receiver" struct event_data { const uint8_t * expected_data; |