From aa31da4ffe0703292a9d76b0c543a0fb7e60f0a1 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 17 Feb 2015 16:33:35 -0800 Subject: [PATCH] UDS Fix Remove existing UDS listeners IFF they are a socket before trying to create a new socket. --- src/core/iomgr/tcp_server_posix.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 6a961637188..9e5076efc71 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -42,17 +42,18 @@ #include "src/core/iomgr/tcp_server.h" -#include +#include #include +#include #include #include #include +#include +#include +#include #include #include -#include #include -#include -#include #include "src/core/iomgr/pollset_posix.h" #include "src/core/iomgr/resolve_address.h" @@ -83,6 +84,14 @@ typedef struct { int addr_len; } server_port; +static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { + struct stat st; + + if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { + unlink(un->sun_path); + } +} + /* the overall server */ struct grpc_tcp_server { grpc_tcp_server_cb cb; @@ -130,7 +139,7 @@ void grpc_tcp_server_destroy(grpc_tcp_server *s) { for (i = 0; i < s->nports; i++) { server_port *sp = &s->ports[i]; if (sp->addr.sockaddr.sa_family == AF_UNIX) { - unlink(sp->addr.un.sun_path); + unlink_if_unix_domain_socket(&sp->addr.un); } grpc_fd_orphan(sp->emfd, NULL, NULL); } @@ -301,6 +310,10 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, socklen_t sockname_len; int port; + if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { + unlink_if_unix_domain_socket(addr); + } + /* Check if this is a wildcard port, and if so, try to keep the port the same as some previously created listener. */ if (grpc_sockaddr_get_port(addr) == 0) {