Based on e7bc519620cb7bcdbe2166fc2a446453769d827e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Thu, 16 Oct 2014 19:15:38 -0500
Subject: [PATCH] systemd: try harder to bind to notify socket

Without the socket open we are going to crash and burn. If for
whatever reason we fail during deserialization we will fail when
trying to open the socket. In this case it is better to unlink the old
socket and maybe lose some messages, than to continue without the
notification socket.

Of course this situation should not happen, but we should handle
it as gracefully as possible anyway.

https://bugzilla.redhat.com/show_bug.cgi?id=1099299
---
 src/core/manager.c |   16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

--- src/core/manager.c
+++ src/core/manager.c	2014-10-20 13:47:21.035837897 +0000
@@ -572,7 +572,21 @@ static int manager_setup_notify(Manager
                 r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
                 if (r < 0) {
                         log_error("bind(@%s) failed: %m", sa.un.sun_path+1);
-                        return -errno;
+                        if (errno == EADDRINUSE) {
+                                log_notice("Removing %s socket and trying again.", m->notify_socket);
+                                r = unlink(m->notify_socket);
+                                if (r < 0) {
+                                        log_error("Failed to remove %s: %m", m->notify_socket);
+                                        return -EADDRINUSE;
+                                }
+
+                                r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
+                                if (r < 0) {
+                                        log_error("bind(@%s) failed: %m", sa.un.sun_path+1);
+                                        return -errno;
+                                }
+                        } else
+                                return -errno;
                 }
 
                 r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
