From 706d7c27ad12098a44d83330eab1aef8f8ed12dc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 27 Dec 2017 16:59:44 +0100 Subject: [PATCH] socket-label: tweak socket_address_listen() a bit This changes two things when binding to AF_UNIX file system sockets: 1. When wethe socket already exists in the fs, and unlink() on it fails, don't bother to bind() a second time: since nothing changed it won't work either. 2. Also use SELinux-aware bind() for the second attempt. --- src/basic/socket-label.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/basic/socket-label.c b/src/basic/socket-label.c index 448265b..e67a5cf 100644 --- a/src/basic/socket-label.c +++ b/src/basic/socket-label.c @@ -124,10 +124,13 @@ int socket_address_listen( r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size); if (r == -EADDRINUSE) { /* Unlink and try again */ - (void) unlink(p); - if (bind(fd, &a->sockaddr.sa, a->size) < 0) - return -errno; - } else if (r < 0) + + if (unlink(p) < 0) + return r; /* didn't work, return original error */ + + r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size); + } + if (r < 0) return r; } } else { -- 2.7.4