Make use of unix sockets optional.

pull/5755/head
ahedberg 9 years ago
parent f21b0d1436
commit 803931d230
  1. 4
      include/grpc/impl/codegen/port_platform.h
  2. 4
      src/core/client_config/resolvers/sockaddr_resolver.c
  3. 2
      src/core/iomgr/endpoint_pair_posix.c
  4. 5
      src/core/iomgr/resolve_address_posix.c
  5. 6
      src/core/iomgr/sockaddr_utils.c
  6. 10
      src/core/iomgr/tcp_client_posix.c
  7. 21
      src/core/iomgr/tcp_server_posix.c
  8. 10
      src/core/iomgr/udp_server.c

@ -133,6 +133,7 @@
#define GPR_POSIX_TIME 1 #define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1 #define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_MSG_NOSIGNAL 1 #define GPR_HAVE_MSG_NOSIGNAL 1
#define GPR_HAVE_UNIX_SOCKET 1
#elif defined(__linux__) #elif defined(__linux__)
#define GPR_POSIX_CRASH_HANDLER 1 #define GPR_POSIX_CRASH_HANDLER 1
#define GPR_PLATFORM_STRING "linux" #define GPR_PLATFORM_STRING "linux"
@ -154,6 +155,7 @@
#define GPR_POSIX_WAKEUP_FD 1 #define GPR_POSIX_WAKEUP_FD 1
#define GPR_POSIX_SOCKET 1 #define GPR_POSIX_SOCKET 1
#define GPR_POSIX_SOCKETADDR 1 #define GPR_POSIX_SOCKETADDR 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef __GLIBC_PREREQ #ifdef __GLIBC_PREREQ
#if __GLIBC_PREREQ(2, 9) #if __GLIBC_PREREQ(2, 9)
#define GPR_LINUX_EVENTFD 1 #define GPR_LINUX_EVENTFD 1
@ -214,6 +216,7 @@
#define GPR_POSIX_TIME 1 #define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1 #define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_SO_NOSIGPIPE 1 #define GPR_HAVE_SO_NOSIGPIPE 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef _LP64 #ifdef _LP64
#define GPR_ARCH_64 1 #define GPR_ARCH_64 1
#else /* _LP64 */ #else /* _LP64 */
@ -242,6 +245,7 @@
#define GPR_POSIX_TIME 1 #define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1 #define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_SO_NOSIGPIPE 1 #define GPR_HAVE_SO_NOSIGPIPE 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef _LP64 #ifdef _LP64
#define GPR_ARCH_64 1 #define GPR_ARCH_64 1
#else /* _LP64 */ #else /* _LP64 */

@ -37,7 +37,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifdef GPR_POSIX_SOCKET #ifdef GPR_HAVE_UNIX_SOCKET
#include <sys/un.h> #include <sys/un.h>
#endif #endif
@ -168,7 +168,7 @@ static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
gpr_free(r); gpr_free(r);
} }
#ifdef GPR_POSIX_SOCKET #ifdef GPR_HAVE_UNIX_SOCKET
static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr,
size_t *len) { size_t *len) {
struct sockaddr_un *un = (struct sockaddr_un *)addr; struct sockaddr_un *un = (struct sockaddr_un *)addr;

@ -52,7 +52,9 @@
static void create_sockets(int sv[2]) { static void create_sockets(int sv[2]) {
int flags; int flags;
#ifdef GPR_HAVE_UNIX_SOCKET
GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
#endif
flags = fcntl(sv[0], F_GETFL, 0); flags = fcntl(sv[0], F_GETFL, 0);
GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0); GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
flags = fcntl(sv[1], F_GETFL, 0); flags = fcntl(sv[1], F_GETFL, 0);

@ -39,7 +39,10 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef GPR_HAVE_UNIX_SOCKET
#include <sys/un.h> #include <sys/un.h>
#endif
#include <string.h>
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/host_port.h> #include <grpc/support/host_port.h>
@ -71,6 +74,7 @@ static grpc_resolved_addresses *blocking_resolve_address_impl(
int s; int s;
size_t i; size_t i;
grpc_resolved_addresses *addrs = NULL; grpc_resolved_addresses *addrs = NULL;
#ifdef GPR_HAVE_UNIX_SOCKET
struct sockaddr_un *un; struct sockaddr_un *un;
if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' && if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
@ -84,6 +88,7 @@ static grpc_resolved_addresses *blocking_resolve_address_impl(
addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
return addrs; return addrs;
} }
#endif
/* parse name, splitting it into host and port parts */ /* parse name, splitting it into host and port parts */
gpr_split_host_port(name, &host, &port); gpr_split_host_port(name, &host, &port);

@ -36,7 +36,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#ifdef GPR_POSIX_SOCKET #ifdef GPR_HAVE_UNIX_SOCKET
#include <sys/un.h> #include <sys/un.h>
#endif #endif
@ -191,7 +191,7 @@ char *grpc_sockaddr_to_uri(const struct sockaddr *addr) {
gpr_asprintf(&result, "ipv6:%s", temp); gpr_asprintf(&result, "ipv6:%s", temp);
gpr_free(temp); gpr_free(temp);
return result; return result;
#ifdef GPR_POSIX_SOCKET #ifdef GPR_HAVE_UNIX_SOCKET
case AF_UNIX: case AF_UNIX:
gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path);
return result; return result;
@ -207,8 +207,10 @@ int grpc_sockaddr_get_port(const struct sockaddr *addr) {
return ntohs(((struct sockaddr_in *)addr)->sin_port); return ntohs(((struct sockaddr_in *)addr)->sin_port);
case AF_INET6: case AF_INET6:
return ntohs(((struct sockaddr_in6 *)addr)->sin6_port); return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
#ifdef GPR_HAVE_UNIX_SOCKET
case AF_UNIX: case AF_UNIX:
return 1; return 1;
#endif
default: default:
gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port",
addr->sa_family); addr->sa_family);

@ -76,6 +76,7 @@ static int prepare_socket(const struct sockaddr *addr, int fd) {
goto error; goto error;
} }
#ifdef GPR_HAVE_UNIX_SOCKET
if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || 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)) || (addr->sa_family != AF_UNIX && !grpc_set_socket_low_latency(fd, 1)) ||
!grpc_set_socket_no_sigpipe_if_possible(fd)) { !grpc_set_socket_no_sigpipe_if_possible(fd)) {
@ -83,6 +84,15 @@ static int prepare_socket(const struct sockaddr *addr, int fd) {
strerror(errno)); strerror(errno));
goto error; 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_no_sigpipe_if_possible(fd)) {
gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
strerror(errno));
goto error;
}
#endif
return 1; return 1;

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

@ -52,7 +52,9 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef GPR_HAVE_UNIX_SOCKET
#include <sys/un.h> #include <sys/un.h>
#endif
#include <unistd.h> #include <unistd.h>
#include "src/core/iomgr/fd_posix.h" #include "src/core/iomgr/fd_posix.h"
@ -77,7 +79,9 @@ typedef struct {
union { union {
uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE]; uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE];
struct sockaddr sockaddr; struct sockaddr sockaddr;
#ifdef GPR_HAVE_UNIX_SOCKET
struct sockaddr_un un; struct sockaddr_un un;
#endif
} addr; } addr;
size_t addr_len; size_t addr_len;
grpc_closure read_closure; grpc_closure read_closure;
@ -85,6 +89,7 @@ typedef struct {
grpc_udp_server_read_cb read_cb; grpc_udp_server_read_cb read_cb;
} server_port; } server_port;
#ifdef GPR_HAVE_UNIX_SOCKET
static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
struct stat st; struct stat st;
@ -92,6 +97,7 @@ static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
unlink(un->sun_path); unlink(un->sun_path);
} }
} }
#endif
/* the overall server */ /* the overall server */
struct grpc_udp_server { struct grpc_udp_server {
@ -176,9 +182,11 @@ static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_udp_server *s) {
if (s->nports) { if (s->nports) {
for (i = 0; i < s->nports; i++) { for (i = 0; i < s->nports; i++) {
server_port *sp = &s->ports[i]; server_port *sp = &s->ports[i];
#ifdef GPR_HAVE_UNIX_SOCKET
if (sp->addr.sockaddr.sa_family == AF_UNIX) { if (sp->addr.sockaddr.sa_family == AF_UNIX) {
unlink_if_unix_domain_socket(&sp->addr.un); unlink_if_unix_domain_socket(&sp->addr.un);
} }
#endif
sp->destroyed_closure.cb = destroyed_port; sp->destroyed_closure.cb = destroyed_port;
sp->destroyed_closure.cb_arg = s; sp->destroyed_closure.cb_arg = s;
grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL, grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL,
@ -336,9 +344,11 @@ int grpc_udp_server_add_port(grpc_udp_server *s, const void *addr,
socklen_t sockname_len; socklen_t sockname_len;
int port; int port;
#ifdef GPR_HAVE_UNIX_SOCKET
if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
unlink_if_unix_domain_socket(addr); unlink_if_unix_domain_socket(addr);
} }
#endif
/* Check if this is a wildcard port, and if so, try to keep the port the same /* Check if this is a wildcard port, and if so, try to keep the port the same
as some previously created listener. */ as some previously created listener. */

Loading…
Cancel
Save