diff options
author | George Kiagiadakis <george.kiagiadakis@collabora.com> | 2021-05-06 14:05:59 +0300 |
---|---|---|
committer | George Kiagiadakis <george.kiagiadakis@collabora.com> | 2021-07-28 13:19:02 +0300 |
commit | 2409371e084c6f34e21b27e533ee7d6feca3f471 (patch) | |
tree | 16371850dc6a12bcb50b1700201e0e39ed2e0055 /lib | |
parent | a189af3e9d74c0cca520e6913f68d5f5f614befc (diff) |
wpipc: place sockets in the same runtime directory as pipewire
Signed-off-by: George Kiagiadakis <george.kiagiadakis@collabora.com>
Diffstat (limited to 'lib')
-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 |
4 files changed, 55 insertions, 10 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 |