summaryrefslogtreecommitdiffstats
path: root/lib/utils.c
diff options
context:
space:
mode:
authorGeorge Kiagiadakis <george.kiagiadakis@collabora.com>2021-05-06 14:05:59 +0300
committerGeorge Kiagiadakis <george.kiagiadakis@collabora.com>2021-07-28 13:19:02 +0300
commit2409371e084c6f34e21b27e533ee7d6feca3f471 (patch)
tree16371850dc6a12bcb50b1700201e0e39ed2e0055 /lib/utils.c
parenta189af3e9d74c0cca520e6913f68d5f5f614befc (diff)
wpipc: place sockets in the same runtime directory as pipewire
Signed-off-by: George Kiagiadakis <george.kiagiadakis@collabora.com>
Diffstat (limited to 'lib/utils.c')
-rw-r--r--lib/utils.c42
1 files changed, 42 insertions, 0 deletions
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