+static int manager_setup_notify(Manager *m) {
+ int r;
+
+ if (m->test_run)
+ return 0;
+
+ if (m->notify_fd < 0) {
+ _cleanup_close_ int fd = -1;
+ union sockaddr_union sa = {
+ .sa.sa_family = AF_UNIX,
+ };
+ static const int one = 1;
+
+ /* First free all secondary fields */
+ free(m->notify_socket);
+ m->notify_socket = NULL;
+ m->notify_event_source = sd_event_source_unref(m->notify_event_source);
+
+ fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
+ if (fd < 0) {
+ log_error("Failed to allocate notification socket: %m");
+ return -errno;
+ }
+
+ if (m->running_as == SYSTEMD_SYSTEM)
+ m->notify_socket = strdup("/run/systemd/notify");
+ else {
+ const char *e;
+
+ e = getenv("XDG_RUNTIME_DIR");
+ if (!e) {
+ log_error("XDG_RUNTIME_DIR is not set: %m");
+ return -EINVAL;
+ }
+
+ m->notify_socket = strappend(e, "/systemd/notify");
+ }
+ if (!m->notify_socket)
+ return log_oom();
+
+ 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) {
+ log_error("bind(%s) failed: %m", sa.un.sun_path);
+ 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);
+ return -errno;
+ }
+ } else
+ return -errno;
+ }
+
+ r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
+ if (r < 0) {
+ log_error("SO_PASSCRED failed: %m");
+ return -errno;
+ }
+
+ m->notify_fd = fd;
+ fd = -1;
+
+ log_debug("Using notification socket %s", m->notify_socket);
+ }
+
+ if (!m->notify_event_source) {
+ r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
+ if (r < 0) {
+ log_error("Failed to allocate notify event source: %s", strerror(-r));
+ return -errno;
+ }
+
+ /* Process signals a bit earlier than SIGCHLD, so that we can
+ * still identify to which service an exit message belongs */
+ r = sd_event_source_set_priority(m->notify_event_source, -7);
+ if (r < 0) {
+ log_error("Failed to set priority of notify event source: %s", strerror(-r));
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int manager_setup_kdbus(Manager *m) {
+#ifdef ENABLE_KDBUS
+ _cleanup_free_ char *p = NULL;
+
+ assert(m);
+
+ if (m->test_run || m->kdbus_fd >= 0)
+ return 0;
+
+ m->kdbus_fd = bus_kernel_create_bus(m->running_as == SYSTEMD_SYSTEM ? "system" : "user", m->running_as == SYSTEMD_SYSTEM, &p);
+ if (m->kdbus_fd < 0) {
+ log_debug("Failed to set up kdbus: %s", strerror(-m->kdbus_fd));
+ return m->kdbus_fd;
+ }
+
+ log_debug("Successfully set up kdbus on %s", p);
+
+ /* Create the namespace directory here, so that the contents
+ * of that directory is not visible to non-root users. This is
+ * necessary to ensure that users cannot get access to busses
+ * of virtualized users when no UID namespacing is used. */
+ if (m->running_as == SYSTEMD_SYSTEM)
+ mkdir_p_label("/dev/kdbus/domain", 0700);
+#endif
+
+ return 0;
+}
+
+static int manager_connect_bus(Manager *m, bool reexecuting) {
+ bool try_bus_connect;
+
+ assert(m);
+
+ if (m->test_run)
+ return 0;
+
+ try_bus_connect =
+ m->kdbus_fd >= 0 ||
+ reexecuting ||
+ (m->running_as == SYSTEMD_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
+
+ /* Try to connect to the busses, if possible. */
+ return bus_init(m, try_bus_connect);
+}
+