protocol: add a few more options for XDG_RUNTIME_DIR

PIPEWIRE_CORE can be used to specify a server name.
PIPEWIRE_REMOTE can be used to specify what server name to
connect to.

Either use the absolute path of the name to create and connect
to a server, or use a relative path. For a relative path, the
server name will be completed by prefixing the following paths
in order:

PIPEWIRE_RUNTIME_DIR environment variable,
XDG_RUNTIME_DIR environment variable,
HOME environment variable,
USERPROFILE environment variable,
home directory as stored in the password database.

Fixes #259
This commit is contained in:
Wim Taymans 2020-07-31 12:46:58 +02:00
parent 5f40bc6d4e
commit 6f2e274b15
4 changed files with 98 additions and 24 deletions

View file

@ -22,6 +22,8 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
@ -31,13 +33,13 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#if HAVE_PWD_H
#include <pwd.h>
#endif
#include <spa/pod/iter.h>
#include <spa/utils/result.h>
#include "config.h"
#ifdef HAVE_SYSTEMD_DAEMON
#include <systemd/sd-daemon.h>
#endif
@ -432,6 +434,28 @@ exit:
return NULL;
}
static const char *
get_runtime_dir(void)
{
const char *runtime_dir;
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) {
struct passwd pwd, *result = NULL;
char buffer[4096];
if (getpwuid_r(getuid(), &pwd, buffer, sizeof(buffer), &result) == 0)
runtime_dir = result ? result->pw_dir : NULL;
}
return runtime_dir;
}
static int init_socket_name(struct server *s, const char *name)
{
int name_size;
@ -440,9 +464,14 @@ static int init_socket_name(struct server *s, const char *name)
path_is_absolute = name[0] == '/';
runtime_dir = getenv("XDG_RUNTIME_DIR");
runtime_dir = get_runtime_dir();
pw_log_debug("name:%s runtime_dir:%s", name, runtime_dir);
if (runtime_dir == NULL && !path_is_absolute) {
pw_log_error("server %p: XDG_RUNTIME_DIR not set in the environment", s);
pw_log_error("server %p: name %s is not an absolute path and no runtime dir found."
"set one of PIPEWIRE_RUNTIME_DIR, XDG_RUNTIME_DIR, HOME or "
"USERPROFILE in the environment", s, name);
return -ENOENT;
}