Skip to content

Commit

Permalink
Merge pull request grpc#5755 from ahedberg/unix_sockets_optional
Browse files Browse the repository at this point in the history
Make unix sockets optional
  • Loading branch information
jtattermusch committed Mar 23, 2016
2 parents e3af44e + 6753ae0 commit f1050f6
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 88 deletions.
9 changes: 9 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ cc_library(
"src/core/iomgr/timer.h",
"src/core/iomgr/timer_heap.h",
"src/core/iomgr/udp_server.h",
"src/core/iomgr/unix_sockets_posix.h",
"src/core/iomgr/wakeup_fd_pipe.h",
"src/core/iomgr/wakeup_fd_posix.h",
"src/core/iomgr/workqueue.h",
Expand Down Expand Up @@ -368,6 +369,8 @@ cc_library(
"src/core/iomgr/timer.c",
"src/core/iomgr/timer_heap.c",
"src/core/iomgr/udp_server.c",
"src/core/iomgr/unix_sockets_posix.c",
"src/core/iomgr/unix_sockets_posix_noop.c",
"src/core/iomgr/wakeup_fd_eventfd.c",
"src/core/iomgr/wakeup_fd_nospecial.c",
"src/core/iomgr/wakeup_fd_pipe.c",
Expand Down Expand Up @@ -596,6 +599,7 @@ cc_library(
"src/core/iomgr/timer.h",
"src/core/iomgr/timer_heap.h",
"src/core/iomgr/udp_server.h",
"src/core/iomgr/unix_sockets_posix.h",
"src/core/iomgr/wakeup_fd_pipe.h",
"src/core/iomgr/wakeup_fd_posix.h",
"src/core/iomgr/workqueue.h",
Expand Down Expand Up @@ -727,6 +731,8 @@ cc_library(
"src/core/iomgr/timer.c",
"src/core/iomgr/timer_heap.c",
"src/core/iomgr/udp_server.c",
"src/core/iomgr/unix_sockets_posix.c",
"src/core/iomgr/unix_sockets_posix_noop.c",
"src/core/iomgr/wakeup_fd_eventfd.c",
"src/core/iomgr/wakeup_fd_nospecial.c",
"src/core/iomgr/wakeup_fd_pipe.c",
Expand Down Expand Up @@ -1427,6 +1433,8 @@ objc_library(
"src/core/iomgr/timer.c",
"src/core/iomgr/timer_heap.c",
"src/core/iomgr/udp_server.c",
"src/core/iomgr/unix_sockets_posix.c",
"src/core/iomgr/unix_sockets_posix_noop.c",
"src/core/iomgr/wakeup_fd_eventfd.c",
"src/core/iomgr/wakeup_fd_nospecial.c",
"src/core/iomgr/wakeup_fd_pipe.c",
Expand Down Expand Up @@ -1600,6 +1608,7 @@ objc_library(
"src/core/iomgr/timer.h",
"src/core/iomgr/timer_heap.h",
"src/core/iomgr/udp_server.h",
"src/core/iomgr/unix_sockets_posix.h",
"src/core/iomgr/wakeup_fd_pipe.h",
"src/core/iomgr/wakeup_fd_posix.h",
"src/core/iomgr/workqueue.h",
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,8 @@ LIBGRPC_SRC = \
src/core/iomgr/timer.c \
src/core/iomgr/timer_heap.c \
src/core/iomgr/udp_server.c \
src/core/iomgr/unix_sockets_posix.c \
src/core/iomgr/unix_sockets_posix_noop.c \
src/core/iomgr/wakeup_fd_eventfd.c \
src/core/iomgr/wakeup_fd_nospecial.c \
src/core/iomgr/wakeup_fd_pipe.c \
Expand Down Expand Up @@ -2834,6 +2836,8 @@ LIBGRPC_UNSECURE_SRC = \
src/core/iomgr/timer.c \
src/core/iomgr/timer_heap.c \
src/core/iomgr/udp_server.c \
src/core/iomgr/unix_sockets_posix.c \
src/core/iomgr/unix_sockets_posix_noop.c \
src/core/iomgr/wakeup_fd_eventfd.c \
src/core/iomgr/wakeup_fd_nospecial.c \
src/core/iomgr/wakeup_fd_pipe.c \
Expand Down
2 changes: 2 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@
'src/core/iomgr/timer.c',
'src/core/iomgr/timer_heap.c',
'src/core/iomgr/udp_server.c',
'src/core/iomgr/unix_sockets_posix.c',
'src/core/iomgr/unix_sockets_posix_noop.c',
'src/core/iomgr/wakeup_fd_eventfd.c',
'src/core/iomgr/wakeup_fd_nospecial.c',
'src/core/iomgr/wakeup_fd_pipe.c',
Expand Down
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ filegroups:
- src/core/iomgr/timer.h
- src/core/iomgr/timer_heap.h
- src/core/iomgr/udp_server.h
- src/core/iomgr/unix_sockets_posix.h
- src/core/iomgr/wakeup_fd_pipe.h
- src/core/iomgr/wakeup_fd_posix.h
- src/core/iomgr/workqueue.h
Expand Down Expand Up @@ -438,6 +439,8 @@ filegroups:
- src/core/iomgr/timer.c
- src/core/iomgr/timer_heap.c
- src/core/iomgr/udp_server.c
- src/core/iomgr/unix_sockets_posix.c
- src/core/iomgr/unix_sockets_posix_noop.c
- src/core/iomgr/wakeup_fd_eventfd.c
- src/core/iomgr/wakeup_fd_nospecial.c
- src/core/iomgr/wakeup_fd_pipe.c
Expand Down
2 changes: 2 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ if test "$PHP_GRPC" != "no"; then
src/core/iomgr/timer.c \
src/core/iomgr/timer_heap.c \
src/core/iomgr/udp_server.c \
src/core/iomgr/unix_sockets_posix.c \
src/core/iomgr/unix_sockets_posix_noop.c \
src/core/iomgr/wakeup_fd_eventfd.c \
src/core/iomgr/wakeup_fd_nospecial.c \
src/core/iomgr/wakeup_fd_pipe.c \
Expand Down
4 changes: 4 additions & 0 deletions gRPC.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Pod::Spec.new do |s|
'src/core/iomgr/timer.h',
'src/core/iomgr/timer_heap.h',
'src/core/iomgr/udp_server.h',
'src/core/iomgr/unix_sockets_posix.h',
'src/core/iomgr/wakeup_fd_pipe.h',
'src/core/iomgr/wakeup_fd_posix.h',
'src/core/iomgr/workqueue.h',
Expand Down Expand Up @@ -385,6 +386,8 @@ Pod::Spec.new do |s|
'src/core/iomgr/timer.c',
'src/core/iomgr/timer_heap.c',
'src/core/iomgr/udp_server.c',
'src/core/iomgr/unix_sockets_posix.c',
'src/core/iomgr/unix_sockets_posix_noop.c',
'src/core/iomgr/wakeup_fd_eventfd.c',
'src/core/iomgr/wakeup_fd_nospecial.c',
'src/core/iomgr/wakeup_fd_pipe.c',
Expand Down Expand Up @@ -556,6 +559,7 @@ Pod::Spec.new do |s|
'src/core/iomgr/timer.h',
'src/core/iomgr/timer_heap.h',
'src/core/iomgr/udp_server.h',
'src/core/iomgr/unix_sockets_posix.h',
'src/core/iomgr/wakeup_fd_pipe.h',
'src/core/iomgr/wakeup_fd_posix.h',
'src/core/iomgr/workqueue.h',
Expand Down
3 changes: 3 additions & 0 deletions grpc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/iomgr/timer.h )
s.files += %w( src/core/iomgr/timer_heap.h )
s.files += %w( src/core/iomgr/udp_server.h )
s.files += %w( src/core/iomgr/unix_sockets_posix.h )
s.files += %w( src/core/iomgr/wakeup_fd_pipe.h )
s.files += %w( src/core/iomgr/wakeup_fd_posix.h )
s.files += %w( src/core/iomgr/workqueue.h )
Expand Down Expand Up @@ -368,6 +369,8 @@ Gem::Specification.new do |s|
s.files += %w( src/core/iomgr/timer.c )
s.files += %w( src/core/iomgr/timer_heap.c )
s.files += %w( src/core/iomgr/udp_server.c )
s.files += %w( src/core/iomgr/unix_sockets_posix.c )
s.files += %w( src/core/iomgr/unix_sockets_posix_noop.c )
s.files += %w( src/core/iomgr/wakeup_fd_eventfd.c )
s.files += %w( src/core/iomgr/wakeup_fd_nospecial.c )
s.files += %w( src/core/iomgr/wakeup_fd_pipe.c )
Expand Down
4 changes: 4 additions & 0 deletions include/grpc/impl/codegen/port_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_MSG_NOSIGNAL 1
#define GPR_HAVE_UNIX_SOCKET 1
#elif defined(__linux__)
#define GPR_POSIX_CRASH_HANDLER 1
#define GPR_PLATFORM_STRING "linux"
Expand All @@ -154,6 +155,7 @@
#define GPR_POSIX_WAKEUP_FD 1
#define GPR_POSIX_SOCKET 1
#define GPR_POSIX_SOCKETADDR 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef __GLIBC_PREREQ
#if __GLIBC_PREREQ(2, 9)
#define GPR_LINUX_EVENTFD 1
Expand Down Expand Up @@ -214,6 +216,7 @@
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_SO_NOSIGPIPE 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef _LP64
#define GPR_ARCH_64 1
#else /* _LP64 */
Expand Down Expand Up @@ -242,6 +245,7 @@
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
#define GPR_HAVE_SO_NOSIGPIPE 1
#define GPR_HAVE_UNIX_SOCKET 1
#ifdef _LP64
#define GPR_ARCH_64 1
#else /* _LP64 */
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"src/core/iomgr/timer.h",
"src/core/iomgr/timer_heap.h",
"src/core/iomgr/udp_server.h",
"src/core/iomgr/unix_sockets_posix.h",
"src/core/iomgr/wakeup_fd_pipe.h",
"src/core/iomgr/wakeup_fd_posix.h",
"src/core/iomgr/workqueue.h",
Expand Down Expand Up @@ -310,6 +311,8 @@
"src/core/iomgr/timer.c",
"src/core/iomgr/timer_heap.c",
"src/core/iomgr/udp_server.c",
"src/core/iomgr/unix_sockets_posix.c",
"src/core/iomgr/unix_sockets_posix_noop.c",
"src/core/iomgr/wakeup_fd_eventfd.c",
"src/core/iomgr/wakeup_fd_nospecial.c",
"src/core/iomgr/wakeup_fd_pipe.c",
Expand Down
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<file baseinstalldir="/" name="src/core/iomgr/timer.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/timer_heap.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/udp_server.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/unix_sockets_posix.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/wakeup_fd_pipe.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/wakeup_fd_posix.h" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/workqueue.h" role="src" />
Expand Down Expand Up @@ -372,6 +373,8 @@
<file baseinstalldir="/" name="src/core/iomgr/timer.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/timer_heap.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/udp_server.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/unix_sockets_posix.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/unix_sockets_posix_noop.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/wakeup_fd_eventfd.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/wakeup_fd_nospecial.c" role="src" />
<file baseinstalldir="/" name="src/core/iomgr/wakeup_fd_pipe.c" role="src" />
Expand Down
35 changes: 8 additions & 27 deletions src/core/client_config/resolvers/sockaddr_resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@

#include <stdio.h>
#include <string.h>
#ifdef GPR_POSIX_SOCKET
#include <sys/un.h>
#endif

#include <grpc/support/alloc.h>
#include <grpc/support/host_port.h>
#include <grpc/support/string_util.h>

#include "src/core/client_config/lb_policy_registry.h"
#include "src/core/iomgr/resolve_address.h"
#include "src/core/iomgr/unix_sockets_posix.h"
#include "src/core/support/string.h"

typedef struct {
Expand Down Expand Up @@ -168,24 +166,6 @@ static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
gpr_free(r);
}

#ifdef GPR_POSIX_SOCKET
static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr,
size_t *len) {
struct sockaddr_un *un = (struct sockaddr_un *)addr;

un->sun_family = AF_UNIX;
strcpy(un->sun_path, uri->path);
*len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;

return 1;
}

static char *unix_get_default_authority(grpc_resolver_factory *factory,
grpc_uri *uri) {
return gpr_strdup("localhost");
}
#endif

static char *ip_get_default_authority(grpc_uri *uri) {
const char *path = uri->path;
if (path[0] == '/') ++path;
Expand Down Expand Up @@ -371,21 +351,22 @@ static void sockaddr_factory_ref(grpc_resolver_factory *factory) {}

static void sockaddr_factory_unref(grpc_resolver_factory *factory) {}

#define DECL_FACTORY(name) \
#define DECL_FACTORY(name, prefix) \
static grpc_resolver *name##_factory_create_resolver( \
grpc_resolver_factory *factory, grpc_resolver_args *args) { \
return sockaddr_create(args, "pick_first", parse_##name); \
return sockaddr_create(args, "pick_first", prefix##parse_##name); \
} \
static const grpc_resolver_factory_vtable name##_factory_vtable = { \
sockaddr_factory_ref, sockaddr_factory_unref, \
name##_factory_create_resolver, name##_get_default_authority, #name}; \
name##_factory_create_resolver, prefix##name##_get_default_authority, \
#name}; \
static grpc_resolver_factory name##_resolver_factory = { \
&name##_factory_vtable}; \
grpc_resolver_factory *grpc_##name##_resolver_factory_create() { \
return &name##_resolver_factory; \
}

#ifdef GPR_POSIX_SOCKET
DECL_FACTORY(unix)
#ifdef GPR_HAVE_UNIX_SOCKET
DECL_FACTORY(unix, grpc_)
#endif
DECL_FACTORY(ipv4) DECL_FACTORY(ipv6)
DECL_FACTORY(ipv4, ) DECL_FACTORY(ipv6, )
5 changes: 3 additions & 2 deletions src/core/iomgr/endpoint_pair_posix.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright 2015, Google Inc.
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -37,6 +37,7 @@

#include "src/core/iomgr/endpoint_pair.h"
#include "src/core/iomgr/socket_utils_posix.h"
#include "src/core/iomgr/unix_sockets_posix.h"

#include <errno.h>
#include <fcntl.h>
Expand All @@ -52,7 +53,7 @@

static void create_sockets(int sv[2]) {
int flags;
GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
grpc_create_socketpair_if_unix(sv);
flags = fcntl(sv[0], F_GETFL, 0);
GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
flags = fcntl(sv[1], F_GETFL, 0);
Expand Down
12 changes: 2 additions & 10 deletions src/core/iomgr/resolve_address_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include <string.h>
#include <sys/types.h>
#include <sys/un.h>

#include <grpc/support/alloc.h>
#include <grpc/support/host_port.h>
Expand All @@ -51,6 +50,7 @@
#include "src/core/iomgr/executor.h"
#include "src/core/iomgr/iomgr_internal.h"
#include "src/core/iomgr/sockaddr_utils.h"
#include "src/core/iomgr/unix_sockets_posix.h"
#include "src/core/support/block_annotate.h"
#include "src/core/support/string.h"

Expand All @@ -71,18 +71,10 @@ static grpc_resolved_addresses *blocking_resolve_address_impl(
int s;
size_t i;
grpc_resolved_addresses *addrs = NULL;
struct sockaddr_un *un;

if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
name[4] == ':' && name[5] != 0) {
addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
addrs->naddrs = 1;
addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address));
un = (struct sockaddr_un *)addrs->addrs->addr;
un->sun_family = AF_UNIX;
strcpy(un->sun_path, name + 5);
addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
return addrs;
return grpc_resolve_unix_domain_address(name + 5);
}

/* parse name, splitting it into host and port parts */
Expand Down
Loading

0 comments on commit f1050f6

Please sign in to comment.