From 312864d9e90cc3e15eb26eed31d14bfb6fdc5200 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 30 Aug 2019 18:08:00 +0200 Subject: [PATCH] protocol-native: attempt to remove socket After we grab the lockfile we should remove the socket when it exists so that we can bind again. This should solve startup problems after a crash, which left the socket around and caused bind failures. --- src/modules/module-protocol-native.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/modules/module-protocol-native.c b/src/modules/module-protocol-native.c index ecfcf5f45..3c769a534 100644 --- a/src/modules/module-protocol-native.c +++ b/src/modules/module-protocol-native.c @@ -480,10 +480,23 @@ static int add_socket(struct pw_protocol *protocol, struct server *s) #endif if (fd < 0) { + struct stat socket_stat; + if ((fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) < 0) { res = -errno; goto error; } + if (stat(s->addr.sun_path, &socket_stat) < 0) { + 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) { + pw_log_warn("removing stale socket"); + unlink(s->addr.sun_path); + } size = offsetof(struct sockaddr_un, sun_path) + strlen(s->addr.sun_path); if (bind(fd, (struct sockaddr *) &s->addr, size) < 0) {