From: Sjoerd Simons <sjoerd@luon.net>
Date: Mon, 1 Dec 2014 22:34:34 +0100
Subject: core: Make binding notify & private dbus socket more robust

Use mkdir + unlink right before calling bind on private unix sockets to
prevent issues with stray sockets. This patch is based on a serie of
upstream commits with the net code change in this patch as a result
  0c3f25e0c1f028d4da9cc5253abf0322230e6835
  e7bc519620cb7bcdbe2166fc2a446453769d827e
  498e87d6b7ef025fef2e089931f355b5cd3c7dad
  f0e62e89970b8c38eb07a9beebd277ce13a5fcc2

Bug-Debian: https://bugs.debian.org/761306
---
 src/core/dbus.c    | 5 ++---
 src/core/manager.c | 3 +++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/core/dbus.c b/src/core/dbus.c
index fb8e496..1c9c0aa 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -974,11 +974,10 @@ static int bus_init_private(Manager *m) {
                 left = strpcpy(&p, left, "/systemd/private");
 
                 salen = sizeof(sa.un) - left;
-
-                mkdir_parents_label(sa.un.sun_path, 0755);
         }
 
-        unlink(sa.un.sun_path);
+        (void) mkdir_parents_label(sa.un.sun_path, 0755);
+        (void) unlink(sa.un.sun_path);
 
         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
         if (fd < 0) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 9c96750..c99f6ec 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -551,6 +551,9 @@ static int manager_setup_notify(Manager *m) {
                 if (!m->notify_socket)
                         return log_oom();
 
+                (void) mkdir_parents_label(m->notify_socket, 0755);
+                (void) unlink(m->notify_socket);
+
                 strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1);
                 r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
                 if (r < 0) {
