diff src/lib/network.c @ 8219:ec83f6dcb585 HEAD

Added net_listen_unix_unlink_stale() and use it where needed to avoid code duplication.
author Timo Sirainen <tss@iki.fi>
date Sat, 27 Sep 2008 12:20:26 +0300
parents 423b8e3fedbb
children b9faf4db2a9f
line wrap: on
line diff
--- a/src/lib/network.c	Wed Sep 24 23:33:59 2008 +0300
+++ b/src/lib/network.c	Sat Sep 27 12:20:26 2008 +0300
@@ -395,6 +395,33 @@
 	return -1;
 }
 
+int net_listen_unix_unlink_stale(const char *path, int backlog)
+{
+	unsigned int i = 0;
+	int fd;
+
+	while ((fd = net_listen_unix(path, backlog)) == -1) {
+		if (errno != EADDRINUSE || ++i == 2)
+			return -1;
+
+		/* see if it really exists */
+		fd = net_connect_unix(path);
+		if (fd != -1 || errno != ECONNREFUSED) {
+			if (fd != -1) (void)close(fd);
+			errno = EADDRINUSE;
+			return -1;
+		}
+
+		/* delete and try again */
+		if (unlink(path) < 0 && errno != ENOENT) {
+			i_error("unlink(%s) failed: %m", path);
+			errno = EADDRINUSE;
+			return -1;
+		}
+	}
+	return fd;
+}
+
 int net_accept(int fd, struct ip_addr *addr, unsigned int *port)
 {
 	union sockaddr_union so;