protocol-native: support abstract socket addresses

They start with @ and do not create a file in the file system.
This commit is contained in:
Wim Taymans 2025-02-17 16:50:16 +01:00
parent cfc8d414a9
commit 62035963a3
2 changed files with 25 additions and 11 deletions

View file

@ -746,7 +746,7 @@ static int init_socket_name(struct server *s, const char *name)
const char *runtime_dir; const char *runtime_dir;
bool path_is_absolute; bool path_is_absolute;
path_is_absolute = name[0] == '/'; path_is_absolute = name[0] == '/' || name[0] == '@';
runtime_dir = get_runtime_dir(); runtime_dir = get_runtime_dir();
@ -784,6 +784,9 @@ static int lock_socket(struct server *s)
{ {
int res; int res;
if (s->addr.sun_path[0] == '\0')
return 0;
snprintf(s->lock_addr, sizeof(s->lock_addr), "%s%s", s->addr.sun_path, LOCK_SUFFIX); snprintf(s->lock_addr, sizeof(s->lock_addr), "%s%s", s->addr.sun_path, LOCK_SUFFIX);
s->fd_lock = open(s->lock_addr, O_CREAT | O_CLOEXEC, s->fd_lock = open(s->lock_addr, O_CREAT | O_CLOEXEC,
@ -939,18 +942,24 @@ static int add_socket(struct pw_protocol *protocol, struct server *s, struct soc
res = -errno; res = -errno;
goto error; goto error;
} }
if (stat(s->addr.sun_path, &socket_stat) < 0) { if (s->addr.sun_path[0] == '@') {
if (errno != ENOENT) { s->addr.sun_path[0] = 0;
res = -errno; size = (socklen_t) (strlen(&s->addr.sun_path[1]) + 1);
pw_log_error("server %p: stat %s failed with error: %m", } else {
s, s->addr.sun_path); if (stat(s->addr.sun_path, &socket_stat) < 0) {
goto error_close; if (errno != ENOENT) {
res = -errno;
pw_log_error("server %p: stat %s failed with error: %m",
s, s->addr.sun_path);
goto error_close;
}
} else if (socket_stat.st_mode & S_IWUSR || socket_stat.st_mode & S_IWGRP) {
unlink(s->addr.sun_path);
} }
} else if (socket_stat.st_mode & S_IWUSR || socket_stat.st_mode & S_IWGRP) { size = (socklen_t) (strlen(s->addr.sun_path) + 1);
unlink(s->addr.sun_path);
} }
size = offsetof(struct sockaddr_un, sun_path) + strlen(s->addr.sun_path); size += offsetof(struct sockaddr_un, sun_path);
if (bind(fd, (struct sockaddr *) &s->addr, size) < 0) { if (bind(fd, (struct sockaddr *) &s->addr, size) < 0) {
res = -errno; res = -errno;
pw_log_error("server %p: bind() failed with error: %m", s); pw_log_error("server %p: bind() failed with error: %m", s);

View file

@ -83,6 +83,11 @@ static int try_connect(struct pw_protocol_client *client,
else else
name_size = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", runtime_dir, name) + 1; name_size = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", runtime_dir, name) + 1;
if (addr.sun_path[0] == '@') {
addr.sun_path[0] = '\0';
name_size--;
}
if (name_size > (int) sizeof addr.sun_path) { if (name_size > (int) sizeof addr.sun_path) {
if (runtime_dir == NULL) if (runtime_dir == NULL)
pw_log_error("client %p: socket path \"%s\" plus null terminator exceeds %i bytes", pw_log_error("client %p: socket path \"%s\" plus null terminator exceeds %i bytes",
@ -136,7 +141,7 @@ static int try_connect_name(struct pw_protocol_client *client,
if (res >= 0) if (res >= 0)
return res; return res;
} }
if (name[0] == '/') { if (name[0] == '/' || name[0] == '@') {
return try_connect(client, NULL, name, done_callback, data); return try_connect(client, NULL, name, done_callback, data);
} else { } else {
runtime_dir = get_runtime_dir(); runtime_dir = get_runtime_dir();