socket-label: tweak socket_address_listen() a bit
authorLennart Poettering <lennart@poettering.net>
Wed, 27 Dec 2017 15:59:44 +0000 (16:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 5 Jan 2018 12:55:08 +0000 (13:55 +0100)
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

index 448265b..e67a5cf 100644 (file)
@@ -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 {