- server->bind_successful = 1;
-
- if (listen(server->fd, 2) < 0) {
- avahi_log_warn("listen(): %s", strerror(errno));
- goto fail;
+ if (n == 1) {
+ int r;
+
+ if ((r = sd_is_socket(AF_LOCAL, SOCK_STREAM, 1, 0)) < 0) {
+ avahi_log_warn("Passed systemd file descriptor is of wrong type: %s", strerror(-r));
+ goto fail;
+ }
+
+ server->fd = SD_LISTEN_FDS_START;
+
+ } else {
+
+ if ((server->fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
+ avahi_log_warn("socket(AF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
+ goto fail;
+ }
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_LOCAL;
+ strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1);
+
+ /* We simply remove existing UNIX sockets under this name. The
+ Avahi daemon makes sure that it runs only once on a host,
+ therefore sockets that already exist are stale and may be
+ removed without any ill effects */
+
+ unlink(AVAHI_SOCKET);
+
+ if (bind(server->fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
+ avahi_log_warn("bind(): %s", strerror(errno));
+ goto fail;
+ }
+
+ server->remove_socket = 1;
+
+ if (listen(server->fd, SOMAXCONN) < 0) {
+ avahi_log_warn("listen(): %s", strerror(errno));
+ goto fail;
+ }