You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/06/09 08:49:04 UTC

svn commit: r1133679 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/LocalServerEndpoint.java native/configure native/include/acr/error.h native/os/unix/usock.c native/os/win32/config.hw native/shared/error.c

Author: mturk
Date: Thu Jun  9 06:49:04 2011
New Revision: 1133679

URL: http://svn.apache.org/viewvc?rev=1133679&view=rev
Log:
Allow inheriting parent endpoint blocking mode

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
    commons/sandbox/runtime/trunk/src/main/native/configure
    commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw
    commons/sandbox/runtime/trunk/src/main/native/shared/error.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java Thu Jun  9 06:49:04 2011
@@ -43,7 +43,8 @@ public class LocalServerEndpoint extends
     private final LocalDescriptor       sd;
     private SelectionKeyImpl            key;
     private EndpointAddress             sa;
-    private boolean                     bound = false;
+    private boolean                     bound    = false;
+    private boolean                     blocking = true;
 
     private static native void          unlink0(byte[] addr);
 
@@ -78,18 +79,22 @@ public class LocalServerEndpoint extends
     public boolean isBlocking()
         throws IOException
     {
-        return sd.isBlocking();
+        return blocking;
     }
 
     @Override
     public Descriptor configureBlocking(boolean block)
-        throws ClosedDescriptorException,
-               IllegalBlockingModeException,
+        throws IllegalBlockingModeException,
                IOException
     {
         if (key != null && key.selected)
             throw new IllegalBlockingModeException();
-        return sd.configureBlocking(block);
+        // Blocking mode must be set before the
+        // actual server endpoint is created.
+        if (sd.valid())
+            throw new IllegalBlockingModeException();
+        blocking = block;
+        return sd;
     }
 
 
@@ -150,7 +155,7 @@ public class LocalServerEndpoint extends
     }
 
     private static native int           bind0(int fd, byte[] sa, int backlog);
-    private static native int           accept0(int fd, byte[] sa)
+    private static native int           accept0(int fd, byte[] sa, boolean block)
         throws SocketException;
 
     @Override
@@ -160,7 +165,7 @@ public class LocalServerEndpoint extends
         if (bound)
             throw new IOException(Local.sm.get("endpoint.EBOUND"));
         if (sd.closed())
-            sd.create(SocketType.STREAM);
+            sd.create(SocketType.STREAM, blocking);
         if (backlog < LISTEN_BACKLOG)
             backlog = LISTEN_BACKLOG;
         int rc = bind0(sd.fd(), endpoint.sockaddr(), backlog);
@@ -178,7 +183,7 @@ public class LocalServerEndpoint extends
     {
         if (sd.closed())
             throw new ClosedDescriptorException();
-        int fd = accept0(sd.fd(), sa.sockaddr());
+        int fd = accept0(sd.fd(), sa.sockaddr(), blocking);
         LocalDescriptor ad = new LocalDescriptor(fd);
         return new LocalEndpoint(ad, sa);
     }

Modified: commons/sandbox/runtime/trunk/src/main/native/configure
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/configure (original)
+++ commons/sandbox/runtime/trunk/src/main/native/configure Thu Jun  9 06:49:04 2011
@@ -1115,6 +1115,90 @@ EOF
     echo $rc
 }
 
+have_nonblock_inherited()
+{
+    do_printf 'Checking for %-32s' "nonblock inherited"
+    cat > $cccsrc.c << EOF
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <fcntl.h>
+#include <unistd.h>
+int main(void) {
+    int listen_s, connected_s, client_s;
+    int listen_port, rc;
+    struct sockaddr_in sa;
+    socklen_t sa_len;
+
+    listen_s = socket(AF_INET, SOCK_STREAM, 0);
+    if (listen_s < 0) {
+        perror("socket");
+        exit(1);
+    }
+    memset(&sa, 0, sizeof sa);
+    sa.sin_family = AF_INET;
+    /* leave port 0 to get ephemeral */
+    rc = bind(listen_s, (struct sockaddr *)&sa, sizeof sa);
+    if (rc < 0) {
+        perror("bind for ephemeral port");
+        exit(1);
+    }
+    /* find ephemeral port */
+    sa_len = sizeof(sa);
+    rc = getsockname(listen_s, (struct sockaddr *)&sa, &sa_len);
+    if (rc < 0) {
+        perror("getsockname");
+        exit(1);
+    }
+    listen_port = sa.sin_port;
+    rc = listen(listen_s, 5);
+    if (rc < 0) {
+        perror("listen");
+        exit(1);
+    }
+    rc = fcntl(listen_s, F_SETFL, O_NONBLOCK);
+    if (rc < 0) {
+        perror("fcntl(F_SETFL)");
+        exit(1);
+    }
+    client_s = socket(AF_INET, SOCK_STREAM, 0);
+    if (client_s < 0) {
+        perror("socket");
+        exit(1);
+    }
+    memset(&sa, 0, sizeof sa);
+    sa.sin_family = AF_INET;
+    sa.sin_port   = listen_port;
+    /* leave sin_addr all zeros to use loopback */
+    rc = connect(client_s, (struct sockaddr *)&sa, sizeof sa);
+    if (rc < 0) {
+        perror("connect");
+        exit(1);
+    }
+    sa_len = sizeof sa;
+    connected_s = accept(listen_s, (struct sockaddr *)&sa, &sa_len);
+    if (connected_s < 0) {
+        perror("accept");
+        exit(1);
+    }
+    rc = fcntl(connected_s, F_GETFL, 0);
+    if (rc < 0) {
+        perror("fcntl(F_GETFL)");
+        exit(1);
+    }
+    if (!(rc & O_NONBLOCK)) {
+        fprintf(stderr, "O_NONBLOCK is not set in the child.\n");
+        exit(1);
+    }
+    return 0;
+}
+EOF
+    rc=`test_compile z 0`
+    echo $rc
+}
+
 check_sizeof()
 {
     do_printf 'Checking for sizeof %-25s' "$1"
@@ -1380,6 +1464,7 @@ extern "C" {
 #define HAVE_GETIFADDRS         `have_function x getifaddrs`
 #define HAVE_SOCK_CLOEXEC       `have_socket AF_INET 'SOCK_STREAM|SOCK_CLOEXEC' SOCK_CLOEXEC`
 #define HAVE_SOCK_NONBLOCK      `have_socket AF_INET 'SOCK_STREAM|SOCK_NONBLOCK' SOCK_NONBLOCK`
+#define HAVE_NONBLOCK_INHERITED `have_nonblock_inherited`
 #define HAVE_FILE_CLOEXEC       `have_defined O_CLOEXEC`
 #define HAVE_SO_ACCEPTFILTER    `have_defined SO_ACCEPTFILTER`
 #define HAVE_TM_TM_GMTOFF       `have_strcut_member time 'struct tm' tm_gmtoff`

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h Thu Jun  9 06:49:04 2011
@@ -56,6 +56,7 @@ enum {
     ACR_EX_EPERM,           /* OperationNotPermittedException */
     ACR_EX_EINPROGRESS,     /* OperationInProgressException */
     ACR_EX_EWOULDBLOCK,     /* OperationWouldBlockException */
+    ACR_EX_EILLEGALBLOCK,   /* IllegalBlockingModeException */
     ACR_EX_TIMEOUT,         /* TimeoutException */
     ACR_EX_ESYS,            /* SystemException */
     ACR_EX_ENET,            /* NetworkException */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Thu Jun  9 06:49:04 2011
@@ -185,19 +185,27 @@ ACR_NET_EXPORT(void, LocalServerEndpoint
 }
 
 ACR_NET_EXPORT(jint, LocalServerEndpoint, accept0)(JNI_STDARGS, jint fd,
-                                                   jbyteArray ba)
+                                                   jbyteArray ba,
+                                                   jboolean block)
 {
     int sd;
     acr_sockaddr_t  aa;
     socklen_t       aalen;
+#if HAVE_ACCEPT4
+    int flags = SOCK_CLOEXEC;
+#endif
+#if HAVE_NONBLOCK_INHERITED
+    if (block == JNI_FALSE)
+        flags |= SOCK_NONBLOCK;
+#endif
 
     memset(&aa, 0, sizeof(aa));
     aalen = ISIZEOF(struct sockaddr_un);
     do {
-#if !HAVE_ACCEPT4
-        sd = accept(fd,  (struct sockaddr *)&aa.sa, &aalen);
+#if HAVE_ACCEPT4
+        sd = accept4(fd, (struct sockaddr *)&aa.sa, &aalen, flags);
 #else
-        sd = accept4(fd, (struct sockaddr *)&aa.sa, &aalen, SOCK_CLOEXEC);
+        sd = accept(fd,  (struct sockaddr *)&aa.sa, &aalen);
 #endif
     } while (sd == -1 && errno == EINTR);
 
@@ -215,6 +223,16 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint
         }
     }
 #endif
+#if !HAVE_NONBLOCK_INHERITED
+    if (block == JNI_FALSE) {
+        int rc = AcrNonblock(sd, 1);
+        if (rc != 0) {
+            r_close(sd);
+            ACR_THROW_NET_ERROR(rc);
+            return -1;
+        }
+    }
+#endif
 #if defined(DEBUG) || defined(_DEBUG)
     if (aa.sa.unx.sun_family != AF_LOCAL) {
         fprintf(stderr, "Expected AF_LOCAL but found %d\n", aa.sa.unx.sun_family);

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/config.hw Thu Jun  9 06:49:04 2011
@@ -118,6 +118,7 @@
 #define HAVE_GETIFADDRS         0
 #define HAVE_SOCK_CLOEXEC       0
 #define HAVE_SOCK_NONBLOCK      0
+#define HAVE_NONBLOCK_INHERITED 1
 #define HAVE_FILE_CLOEXEC       0
 #define HAVE_SO_ACCEPTFILTER    0
 #define HAVE_TM_TM_GMTOFF       0

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1133679&r1=1133678&r2=1133679&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Thu Jun  9 06:49:04 2011
@@ -48,8 +48,9 @@ static struct {
     { 0, ACR_CLASS_PATH "NoSuchObjectException"                 }, /* ENOENT    */
     { 0, ACR_CLASS_PATH "OperationNotImplementedException"      }, /* ENOTIMPL  */
     { 0, ACR_CLASS_PATH "OperationNotPermittedException"        }, /* EPERM     */
-    { 0, ACR_IO_CP      "OperationInProgressException"          }, /* EINPROGRESS  */
-    { 0, ACR_IO_CP      "OperationWouldBlockException"          }, /* EWOULDBLOCK  */
+    { 0, ACR_IO_CP      "OperationInProgressException"          }, /* EINPROGRESS   */
+    { 0, ACR_IO_CP      "OperationWouldBlockException"          }, /* EWOULDBLOCK   */
+    { 0, ACR_NET_CP     "IllegalBlockingModeException"          }, /* EILLEGALBLOCK */
     { 0, ACR_CLASS_PATH "TimeoutException"                      }, /* ETIMEOUT  */
     { 0, ACR_CLASS_PATH "SystemException"                       }, /* EX_ESYS   */
     { 0, ACR_NET_CP     "NetworkException"                      }, /* EX_ENET   */