You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ji...@apache.org on 2014/04/28 19:24:21 UTC
svn commit: r1590714 - in /apr/apr/branches/1.6.x: ./ build/ include/
include/arch/unix/ network_io/unix/ test/
Author: jim
Date: Mon Apr 28 17:24:21 2014
New Revision: 1590714
URL: http://svn.apache.org/r1590714
Log:
Merge r745763 from trunk:
Enable unix domain (AF_UNIX) sockets if supported by the OS
Submitted by: mturk
Reviewed/backported by: jim
Modified:
apr/apr/branches/1.6.x/ (props changed)
apr/apr/branches/1.6.x/CHANGES
apr/apr/branches/1.6.x/build/apr_network.m4
apr/apr/branches/1.6.x/configure.in
apr/apr/branches/1.6.x/include/apr.h.in
apr/apr/branches/1.6.x/include/apr.hnw
apr/apr/branches/1.6.x/include/apr.hw
apr/apr/branches/1.6.x/include/apr_network_io.h
apr/apr/branches/1.6.x/include/arch/unix/apr_arch_networkio.h
apr/apr/branches/1.6.x/network_io/unix/sockaddr.c
apr/apr/branches/1.6.x/network_io/unix/sockets.c
apr/apr/branches/1.6.x/network_io/unix/sockopt.c
apr/apr/branches/1.6.x/test/sockchild.c
apr/apr/branches/1.6.x/test/testsock.c
Propchange: apr/apr/branches/1.6.x/
------------------------------------------------------------------------------
Merged /apr/apr/trunk:r745763
Modified: apr/apr/branches/1.6.x/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/CHANGES?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/CHANGES [utf-8] (original)
+++ apr/apr/branches/1.6.x/CHANGES [utf-8] Mon Apr 28 17:24:21 2014
@@ -1,6 +1,9 @@
-*- coding: utf-8 -*-
Changes for APR 1.6.0
+ *) Added Unix domain socket support.
+ [Mladen Turk]
+
*) Add apr_shm_delete() to compliment apr_shm_remove().
[Jim Jagielski]
Modified: apr/apr/branches/1.6.x/build/apr_network.m4
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/build/apr_network.m4?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/build/apr_network.m4 (original)
+++ apr/apr/branches/1.6.x/build/apr_network.m4 Mon Apr 28 17:24:21 2014
@@ -824,6 +824,36 @@ else
fi
])
+dnl Check for presence of struct sockaddr_un.
+AC_DEFUN([APR_CHECK_SOCKADDR_UN], [
+AC_CACHE_CHECK(for sockaddr_un, ac_cv_define_sockaddr_un,[
+AC_TRY_COMPILE([
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+],[
+struct sockaddr_un sa;
+],[
+ ac_cv_define_sockaddr_un=yes
+],[
+ ac_cv_define_sockaddr_un=no
+])
+])
+
+if test "$ac_cv_define_sockaddr_un" = "yes"; then
+ have_sockaddr_un=1
+else
+ have_sockaddr_un=0
+fi
+AC_SUBST(have_sockaddr_un)
+])
+
dnl
dnl APR_H_ERRNO_COMPILE_CHECK
dnl
Modified: apr/apr/branches/1.6.x/configure.in
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/configure.in?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/configure.in (original)
+++ apr/apr/branches/1.6.x/configure.in Mon Apr 28 17:24:21 2014
@@ -2666,6 +2666,7 @@ APR_CHECK_NEGATIVE_EAI
APR_CHECK_WORKING_GETNAMEINFO
APR_CHECK_SOCKADDR_IN6
APR_CHECK_SOCKADDR_STORAGE
+APR_CHECK_SOCKADDR_UN
have_ipv6="0"
if test "$user_disabled_ipv6" = 1; then
Modified: apr/apr/branches/1.6.x/include/apr.h.in
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/apr.h.in?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/apr.h.in (original)
+++ apr/apr/branches/1.6.x/include/apr.h.in Mon Apr 28 17:24:21 2014
@@ -245,6 +245,7 @@ extern "C" {
#define APR_HAVE_INET_ADDR @have_inet_addr@
#define APR_HAVE_INET_NETWORK @have_inet_network@
#define APR_HAVE_IPV6 @have_ipv6@
+#define APR_HAVE_SOCKADDR_UN @have_sockaddr_un@
#define APR_HAVE_MEMMOVE @have_memmove@
#define APR_HAVE_SETRLIMIT @have_setrlimit@
#define APR_HAVE_SIGACTION @have_sigaction@
Modified: apr/apr/branches/1.6.x/include/apr.hnw
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/apr.hnw?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/apr.hnw (original)
+++ apr/apr/branches/1.6.x/include/apr.hnw Mon Apr 28 17:24:21 2014
@@ -180,6 +180,7 @@ extern "C" {
#else
#define APR_HAVE_IPV6 0
#endif
+#define APR_HAVE_SOCKADDR_UN 0
#define APR_HAVE_MEMCHR 1
#define APR_HAVE_MEMMOVE 1
#define APR_HAVE_SETRLIMIT 0
Modified: apr/apr/branches/1.6.x/include/apr.hw
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/apr.hw?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/apr.hw (original)
+++ apr/apr/branches/1.6.x/include/apr.hw Mon Apr 28 17:24:21 2014
@@ -283,6 +283,7 @@ extern "C" {
#define APR_HAVE_INET_ADDR 1
#define APR_HAVE_INET_NETWORK 0
#define APR_HAVE_IPV6 0
+#define APR_HAVE_SOCKADDR_UN 0
#define APR_HAVE_MEMMOVE 1
#define APR_HAVE_SETRLIMIT 0
#define APR_HAVE_SIGACTION 0
Modified: apr/apr/branches/1.6.x/include/apr_network_io.h
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/apr_network_io.h?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/apr_network_io.h (original)
+++ apr/apr/branches/1.6.x/include/apr_network_io.h Mon Apr 28 17:24:21 2014
@@ -26,10 +26,14 @@
#include "apr_file_io.h"
#include "apr_errno.h"
#include "apr_inherit.h"
+#include "apr_perms_set.h"
#if APR_HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
+#if APR_HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
#ifdef __cplusplus
extern "C" {
@@ -156,6 +160,25 @@ struct in_addr {
#define APR_INET6 AF_INET6
#endif
+#if APR_HAVE_SOCKADDR_UN
+#if defined (AF_UNIX)
+#define APR_UNIX AF_UNIX
+#elif defined(AF_LOCAL)
+#define APR_UNIX AF_LOCAL
+#else
+#error "Neither AF_UNIX nor AF_LOCAL is defined"
+#endif
+#else /* !APR_HAVE_SOCKADDR_UN */
+#if defined (AF_UNIX)
+#define APR_UNIX AF_UNIX
+#elif defined(AF_LOCAL)
+#define APR_UNIX AF_LOCAL
+#else
+/* TODO: Use a smarter way to detect unique APR_UNIX value */
+#define APR_UNIX 1234
+#endif
+#endif
+
/**
* @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
* @{
@@ -247,6 +270,10 @@ struct apr_sockaddr_t {
* dependent on whether APR_HAVE_IPV6 is defined. */
struct sockaddr_storage sas;
#endif
+#if APR_HAVE_SOCKADDR_UN
+ /** Unix domain socket sockaddr structure */
+ struct sockaddr_un unx;
+#endif
} sa;
};
@@ -375,6 +402,7 @@ APR_DECLARE(apr_status_t) apr_socket_atr
* @param sa The new apr_sockaddr_t.
* @param hostname The hostname or numeric address string to resolve/parse, or
* NULL to build an address that corresponds to 0.0.0.0 or ::
+ * or in case of APR_UNIX family it is absolute socket filename.
* @param family The address family to use, or APR_UNSPEC if the system should
* decide.
* @param port The port number.
@@ -807,6 +835,11 @@ APR_DECLARE_INHERIT_SET(socket);
APR_DECLARE_INHERIT_UNSET(socket);
/**
+ * Set socket permissions.
+ */
+APR_PERMS_SET_IMPLEMENT(global_mutex);
+
+/**
* @defgroup apr_mcast IP Multicast
* @{
*/
Modified: apr/apr/branches/1.6.x/include/arch/unix/apr_arch_networkio.h
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/arch/unix/apr_arch_networkio.h?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/arch/unix/apr_arch_networkio.h (original)
+++ apr/apr/branches/1.6.x/include/arch/unix/apr_arch_networkio.h Mon Apr 28 17:24:21 2014
@@ -111,6 +111,9 @@ struct apr_socket_t {
#ifndef HAVE_POLL
int connected;
#endif
+#if APR_HAVE_SOCKADDR_UN
+ int bound;
+#endif
int local_port_unknown;
int local_interface_unknown;
int remote_addr_unknown;
Modified: apr/apr/branches/1.6.x/network_io/unix/sockaddr.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/network_io/unix/sockaddr.c?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/network_io/unix/sockaddr.c (original)
+++ apr/apr/branches/1.6.x/network_io/unix/sockaddr.c Mon Apr 28 17:24:21 2014
@@ -167,6 +167,14 @@ void apr_sockaddr_vars_set(apr_sockaddr_
addr->ipaddr_len = sizeof(struct in6_addr);
}
#endif
+#if APR_HAVE_SOCKADDR_UN
+ else if (family == APR_UNIX) {
+ addr->salen = sizeof(struct sockaddr_un);
+ addr->addr_str_len = sizeof(addr->sa.unx.sun_path);;
+ addr->ipaddr_ptr = &(addr->sa.unx.sun_path);
+ addr->ipaddr_len = addr->addr_str_len;
+ }
+#endif
}
APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
@@ -612,6 +620,33 @@ APR_DECLARE(apr_status_t) apr_sockaddr_i
}
#endif
}
+ if (family == APR_UNSPEC && hostname && *hostname == '/') {
+ family = APR_UNIX;
+ }
+ if (family == APR_UNIX) {
+#if APR_HAVE_SOCKADDR_UN
+ if (hostname && *hostname == '/') {
+ *sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
+ (*sa)->pool = p;
+ apr_cpystrn((*sa)->sa.unx.sun_path, hostname,
+ sizeof((*sa)->sa.unx.sun_path));
+ (*sa)->hostname = apr_pstrdup(p, hostname);
+ (*sa)->family = APR_UNIX;
+ (*sa)->sa.unx.sun_family = APR_UNIX;
+ (*sa)->salen = sizeof(struct sockaddr_un);
+ (*sa)->addr_str_len = sizeof((*sa)->sa.unx.sun_path);
+ (*sa)->ipaddr_ptr = &((*sa)->sa.unx.sun_path);
+ (*sa)->ipaddr_len = (*sa)->addr_str_len;
+
+ return APR_SUCCESS;
+ }
+ else
+#endif
+ {
+ *sa = NULL;
+ return APR_ENOTIMPL;
+ }
+ }
#if !APR_HAVE_IPV6
/* What may happen is that APR is not IPv6-enabled, but we're still
* going to call getaddrinfo(), so we have to tell the OS we only
@@ -664,6 +699,12 @@ APR_DECLARE(apr_status_t) apr_getnameinf
tmphostname, sizeof(tmphostname), NULL, 0,
flags != 0 ? flags : NI_NAMEREQD);
}
+#if APR_HAVE_SOCKADDR_UN
+ else if (sockaddr->family == APR_UNIX) {
+ *hostname = sockaddr->hostname;
+ return APR_SUCCESS;
+ }
+#endif
else
#endif
rc = getnameinfo((const struct sockaddr *)&sockaddr->sa, sockaddr->salen,
Modified: apr/apr/branches/1.6.x/network_io/unix/sockets.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/network_io/unix/sockets.c?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/network_io/unix/sockets.c (original)
+++ apr/apr/branches/1.6.x/network_io/unix/sockets.c Mon Apr 28 17:24:21 2014
@@ -26,13 +26,26 @@
#define close closesocket
#endif /* BEOS_R5 */
-static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */
+#if APR_HAVE_SOCKADDR_UN
+#define GENERIC_INADDR_ANY_LEN sizeof(struct sockaddr_un)
+#else
+#define GENERIC_INADDR_ANY_LEN 16
+#endif
+
+/* big enough for IPv4, IPv6 and optionaly sun_path */
+static char generic_inaddr_any[GENERIC_INADDR_ANY_LEN] = {0};
static apr_status_t socket_cleanup(void *sock)
{
apr_socket_t *thesocket = sock;
int sd = thesocket->socketdes;
+#if APR_HAVE_SOCKADDR_UN
+ if (thesocket->bound && thesocket->local_addr->family == APR_UNIX) {
+ /* XXX: Check for return values ? */
+ unlink(thesocket->local_addr->hostname);
+ }
+#endif
/* Set socket descriptor to -1 before close(), so that there is no
* chance of returning an already closed FD from apr_os_sock_get().
*/
@@ -49,6 +62,18 @@ static apr_status_t socket_cleanup(void
}
}
+static apr_status_t socket_child_cleanup(void *sock)
+{
+ apr_socket_t *thesocket = sock;
+ if (close(thesocket->socketdes) == 0) {
+ thesocket->socketdes = -1;
+ return APR_SUCCESS;
+ }
+ else {
+ return errno;
+ }
+}
+
static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
{
sock->type = type;
@@ -92,6 +117,7 @@ apr_status_t apr_socket_create(apr_socke
int protocol, apr_pool_t *cont)
{
int family = ofamily, flags = 0;
+ int oprotocol = protocol;
#ifdef HAVE_SOCK_CLOEXEC
flags |= SOCK_CLOEXEC;
@@ -104,7 +130,11 @@ apr_status_t apr_socket_create(apr_socke
family = APR_INET;
#endif
}
-
+#if APR_HAVE_SOCKADDR_UN
+ if (family == APR_UNIX) {
+ protocol = 0;
+ }
+#endif
alloc_socket(new, cont);
#ifndef BEOS_R5
@@ -140,7 +170,7 @@ apr_status_t apr_socket_create(apr_socke
if ((*new)->socketdes < 0) {
return errno;
}
- set_socket_vars(*new, family, type, protocol);
+ set_socket_vars(*new, family, type, oprotocol);
#ifndef HAVE_SOCK_CLOEXEC
{
@@ -158,7 +188,7 @@ apr_status_t apr_socket_create(apr_socke
(*new)->timeout = -1;
(*new)->inherit = 0;
apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
- socket_cleanup);
+ socket_child_cleanup);
return APR_SUCCESS;
}
@@ -183,6 +213,13 @@ apr_status_t apr_socket_bind(apr_socket_
else {
sock->local_addr = sa;
/* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
+#if APR_HAVE_SOCKADDR_UN
+ if (sock->local_addr->family == APR_UNIX) {
+ sock->bound = 1;
+ sock->local_port_unknown = 1;
+ }
+ else
+#endif
if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
}
@@ -273,6 +310,14 @@ apr_status_t apr_socket_accept(apr_socke
(*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
}
#endif
+#if APR_HAVE_SOCKADDR_UN
+ else if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
+ *(*new)->remote_addr = *sock->local_addr;
+ (*new)->local_addr->ipaddr_ptr = &((*new)->local_addr->sa.unx.sun_path);
+ (*new)->remote_addr->ipaddr_ptr = &((*new)->remote_addr->sa.unx.sun_path);
+ }
+ if (sock->local_addr->sa.sin.sin_family != AF_UNIX)
+#endif
(*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
if (sock->local_port_unknown) {
/* not likely for a listening socket, but theoretically possible :) */
@@ -357,7 +402,6 @@ apr_status_t apr_socket_connect(apr_sock
#endif /* SO_ERROR */
}
-
if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) {
/* A real remote address was passed in. If the unspecified
* address was used, the actual remote addr will have to be
@@ -375,6 +419,13 @@ apr_status_t apr_socket_connect(apr_sock
/* connect() got us an ephemeral port */
sock->local_port_unknown = 1;
}
+#if APR_HAVE_SOCKADDR_UN
+ if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
+ /* Assign connect address as local. */
+ sock->local_addr = sa;
+ }
+ else
+#endif
if (!memcmp(sock->local_addr->ipaddr_ptr,
generic_inaddr_any,
sock->local_addr->ipaddr_len)) {
Modified: apr/apr/branches/1.6.x/network_io/unix/sockopt.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/network_io/unix/sockopt.c?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/network_io/unix/sockopt.c (original)
+++ apr/apr/branches/1.6.x/network_io/unix/sockopt.c Mon Apr 28 17:24:21 2014
@@ -428,3 +428,24 @@ apr_status_t apr_socket_accept_filter(ap
return APR_SUCCESS;
}
#endif
+
+APR_PERMS_SET_IMPLEMENT(socket)
+{
+#if APR_HAVE_SOCKADDR_UN
+ apr_status_t rv = APR_SUCCESS;
+ apr_socket_t *socket = (apr_socket_t *)thesocket;
+
+ if (socket->local_addr->family == APR_UNIX) {
+ if (!(perms & APR_FPROT_GSETID))
+ gid = -1;
+ if (fchown(socket->socketdes, uid, gid) < 0) {
+ rv = errno;
+ }
+ }
+ else
+ rv = APR_EINVAL;
+ return rv;
+#else
+ return APR_ENOTIMPL;
+#endif
+}
Modified: apr/apr/branches/1.6.x/test/sockchild.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/test/sockchild.c?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/test/sockchild.c (original)
+++ apr/apr/branches/1.6.x/test/sockchild.c Mon Apr 28 17:24:21 2014
@@ -30,11 +30,11 @@ int main(int argc, char *argv[])
atexit(apr_terminate);
apr_pool_create(&p, NULL);
- if (argc < 2) {
+ if (argc < 3) {
exit(-1);
}
- rv = apr_sockaddr_info_get(&remote_sa, "127.0.0.1", APR_UNSPEC, 8021, 0, p);
+ rv = apr_sockaddr_info_get(&remote_sa, argv[2], APR_UNSPEC, 8021, 0, p);
if (rv != APR_SUCCESS) {
exit(-1);
}
Modified: apr/apr/branches/1.6.x/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/test/testsock.c?rev=1590714&r1=1590713&r2=1590714&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/test/testsock.c (original)
+++ apr/apr/branches/1.6.x/test/testsock.c Mon Apr 28 17:24:21 2014
@@ -26,10 +26,15 @@
#define APR_WANT_BYTEFUNC
#include "apr_want.h"
+#define UNIX_SOCKET_NAME "/tmp/apr-socket"
+#define IPV4_SOCKET_NAME "127.0.0.1"
+static char *socket_name = NULL;
+static int socket_type = APR_INET;
+
static void launch_child(abts_case *tc, apr_proc_t *proc, const char *arg1, apr_pool_t *p)
{
apr_procattr_t *procattr;
- const char *args[3];
+ const char *args[4];
apr_status_t rv;
rv = apr_procattr_create(&procattr, p);
@@ -47,7 +52,8 @@ static void launch_child(abts_case *tc,
args[0] = "sockchild" EXTENSION;
args[1] = arg1;
- args[2] = NULL;
+ args[2] = socket_name;
+ args[3] = NULL;
rv = apr_proc_create(proc, TESTBINPATH "sockchild" EXTENSION, args, NULL,
procattr, p);
APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
@@ -117,7 +123,7 @@ static apr_socket_t *setup_socket(abts_c
apr_sockaddr_t *sa;
apr_socket_t *sock;
- rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_INET, 8021, 0, p);
+ rv = apr_sockaddr_info_get(&sa, socket_name, socket_type, 8021, 0, p);
APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, APR_PROTO_TCP, p);
@@ -491,7 +497,7 @@ static void test_nonblock_inheritance(ab
abts_suite *testsock(abts_suite *suite)
{
suite = ADD_SUITE(suite)
-
+ socket_name = IPV4_SOCKET_NAME;
abts_run_test(suite, test_addr_info, NULL);
abts_run_test(suite, test_serv_by_name, NULL);
abts_run_test(suite, test_create_bind_listen, NULL);
@@ -503,6 +509,14 @@ abts_suite *testsock(abts_suite *suite)
abts_run_test(suite, test_get_addr, NULL);
abts_run_test(suite, test_nonblock_inheritance, NULL);
+#if APR_HAVE_SOCKADDR_UN
+ socket_name = UNIX_SOCKET_NAME;
+ socket_type = APR_UNIX;
+ abts_run_test(suite, test_create_bind_listen, NULL);
+ abts_run_test(suite, test_send, NULL);
+ abts_run_test(suite, test_recv, NULL);
+ abts_run_test(suite, test_timeout, NULL);
+#endif
return suite;
}