|
|
|
@ -52,7 +52,9 @@ |
|
|
|
|
#include <sys/socket.h> |
|
|
|
|
#include <sys/stat.h> |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
#include <sys/un.h> |
|
|
|
|
#endif |
|
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
|
#include "src/core/iomgr/pollset_posix.h" |
|
|
|
@ -81,7 +83,9 @@ struct grpc_tcp_listener { |
|
|
|
|
union { |
|
|
|
|
uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE]; |
|
|
|
|
struct sockaddr sockaddr; |
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
struct sockaddr_un un; |
|
|
|
|
#endif |
|
|
|
|
} addr; |
|
|
|
|
size_t addr_len; |
|
|
|
|
int port; |
|
|
|
@ -98,6 +102,7 @@ struct grpc_tcp_listener { |
|
|
|
|
int is_sibling; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { |
|
|
|
|
struct stat st; |
|
|
|
|
|
|
|
|
@ -105,6 +110,7 @@ static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { |
|
|
|
|
unlink(un->sun_path); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/* the overall server */ |
|
|
|
|
struct grpc_tcp_server { |
|
|
|
@ -203,9 +209,11 @@ static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
|
|
|
|
if (s->head) { |
|
|
|
|
grpc_tcp_listener *sp; |
|
|
|
|
for (sp = s->head; sp; sp = sp->next) { |
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
if (sp->addr.sockaddr.sa_family == AF_UNIX) { |
|
|
|
|
unlink_if_unix_domain_socket(&sp->addr.un); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
sp->destroyed_closure.cb = destroyed_port; |
|
|
|
|
sp->destroyed_closure.cb_arg = s; |
|
|
|
|
grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL, |
|
|
|
@ -280,6 +288,7 @@ static int prepare_socket(int fd, const struct sockaddr *addr, |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
|
|
|
|
(addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) || |
|
|
|
|
!grpc_set_socket_reuse_addr(fd, 1))) || |
|
|
|
@ -288,6 +297,16 @@ static int prepare_socket(int fd, const struct sockaddr *addr, |
|
|
|
|
strerror(errno)); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
#else |
|
|
|
|
if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
|
|
|
|
!grpc_set_socket_low_latency(fd, 1) || |
|
|
|
|
!grpc_set_socket_reuse_addr(fd, 1) || |
|
|
|
|
!grpc_set_socket_no_sigpipe_if_possible(fd)) { |
|
|
|
|
gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
|
|
|
|
strerror(errno)); |
|
|
|
|
goto error; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
GPR_ASSERT(addr_len < ~(socklen_t)0); |
|
|
|
|
if (bind(fd, addr, (socklen_t)addr_len) < 0) { |
|
|
|
@ -451,9 +470,11 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
|
|
|
|
if (s->tail != NULL) { |
|
|
|
|
port_index = s->tail->port_index + 1; |
|
|
|
|
} |
|
|
|
|
#ifdef GPR_HAVE_UNIX_SOCKET |
|
|
|
|
if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { |
|
|
|
|
unlink_if_unix_domain_socket(addr); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/* Check if this is a wildcard port, and if so, try to keep the port the same
|
|
|
|
|
as some previously created listener. */ |
|
|
|
|