commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1143761 - in /commons/sandbox/runtime/trunk/src/main: native/ native/include/acr/ native/os/unix/ native/os/win32/ native/shared/ test/org/apache/commons/runtime/
Date Thu, 07 Jul 2011 11:10:49 GMT
Author: mturk
Date: Thu Jul  7 11:10:49 2011
New Revision: 1143761

URL: http://svn.apache.org/viewvc?rev=1143761&view=rev
Log:
Add windows localsock.c initial code and required support fixes:

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Thu Jul  7 11:10:49 2011
@@ -81,6 +81,7 @@ WIN32_SOURCES=\
 	$(TOPDIR)\os\win32\execmem.c \
 	$(TOPDIR)\os\win32\inetsock.c \
 	$(TOPDIR)\os\win32\init.c \
+	$(TOPDIR)\os\win32\localsock.c \
 	$(TOPDIR)\os\win32\os.c \
 	$(TOPDIR)\os\win32\path.c \
 	$(TOPDIR)\os\win32\platform.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h Thu Jul  7 11:10:49
2011
@@ -19,6 +19,12 @@
 
 #include "acr/jniapi.h"
 
+#define ACR_DT_NBLOCK       0x0001
+#define ACR_DT_MASK         0xfff0
+#define ACR_DT_SOCKET       0x0010
+#define ACR_DT_LSOCK        0x0020
+#define ACR_DT_PIPE         0x0040
+
 typedef struct acr_fd_t acr_fd_t;
 struct acr_fd_t {
     int                     type;   /**< Descriptor type    */

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=1143761&r1=1143760&r2=1143761&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 Jul  7 11:10:49 2011
@@ -109,6 +109,7 @@ ACR_NET_EXPORT(jlong, LocalDescriptor, s
         r_close(sd);
         return 0;
     }
+    sp->type = ACR_DT_LSOCK;
     sp->refs = 1;
     sp->u.s  = sd;
     return P2J(sp);
@@ -263,6 +264,7 @@ ACR_NET_EXPORT(jlong, LocalServerEndpoin
         r_close(sd);
         return 0;
     }
+    sp->type = ACR_DT_LSOCK;
     sp->refs = 1;
     sp->u.s  = sd;
     return P2J(sp);

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h Thu Jul  7 11:10:49
2011
@@ -270,7 +270,9 @@ HANDLE  AcrNullPipe(int flags, int fd);
 int     AcrPipePair(HANDLE *rd, HANDLE *wr, int flags, char *name);
 int     AcrSocketPair(SOCKET *rd, SOCKET *wr, int nonblocking);
 int     AcrDrainSocket(SOCKET sd);
-int     AcrNonblock(int sd, int on);
+int     AcrNonblock(SOCKET sd, int on);
+int     AcrWaitIO(SOCKET sd, int timeout, int events);
+
 size_t  wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz);
 size_t  wcslcat(wchar_t *dst, const wchar_t *src, size_t siz);
 const wchar_t *basename_w(const wchar_t *path);

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c?rev=1143761&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c Thu Jul  7 11:10:49
2011
@@ -0,0 +1,176 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/jnitypes.h"
+#include "acr/error.h"
+#include "acr/memory.h"
+#include "acr/netapi.h"
+#include "acr/unsafe.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+#include "arch_sync.h"
+
+/* Windows doesn't have local sockets
+ * so instead we create a 127.0.0.1 loopack
+ * adapter wrappers.
+ * The socket file is a place where the
+ * server sockets stores its pid and
+ * listening socket port in the following form:
+ * <pid> <port> <future use>
+ * The client opens the file, parses the
+ * pid and port and makes a connection.
+ * In server mode if the file exists and the pid doesn't
+ * exist, the file is overwritten.
+ */
+
+#define SOCKADDR_CAST(BA) \
+    (acr_sockaddr_t *)AcrGetArrayCritical(env, (BA))
+#define SOCKADDR_RELEASE(BA, SA) \
+    AcrReleaseArrayCritical(env, (BA), (SA))
+
+ACR_NET_EXPORT(jint, LocalDescriptor, close0)(JNI_STDARGS, jlong fp)
+{
+    int rc = 0;
+    acr_fd_t *fd = J2P(fp, acr_fd_t *);
+
+    if (fd == 0)
+        return ACR_EBADF;
+    if (AcrAtomic32Dec(&fd->refs) == 0) {
+        if (closesocket(fd->u.s) == SOCKET_ERROR)
+            rc = ACR_GET_NETOS_ERROR();
+        AcrFree(fd);
+    }
+    return rc;
+}
+
+ACR_NET_EXPORT(jint, LocalDescriptor, sendz0)(JNI_STDARGS, jlong fp)
+{
+    char dummy = 0;
+    acr_fd_t *fd = J2P(fp, acr_fd_t *);
+
+    if (send(fd->u.s, &dummy, 0, 0) == SOCKET_ERROR)
+        return ACR_GET_NETOS_ERROR();
+    else
+        return 0;
+}
+
+ACR_NET_EXPORT(jlong, LocalDescriptor, socket0)(JNI_STDARGS, jint stype,
+                                                jboolean block)
+{
+    SOCKET sd;
+    int rc   = 0;
+    int type = ACR_DT_LSOCK;
+    acr_fd_t *sp;
+
+    switch (stype) {
+        case 1:
+            type = SOCK_STREAM;
+        break;
+        case 2:
+            type = SOCK_DGRAM;
+        break;
+        case 3:
+            type = SOCK_RAW;
+        break;
+
+    }
+    sd = socket(AF_INET, type, 0);
+    if (sd == -1)
+        rc = ACR_GET_NETOS_ERROR();
+    if (block == JNI_FALSE && rc == 0) {
+        u_long one = 1;
+        if (ioctlsocket(sd, FIONBIO, &one) == SOCKET_ERROR) {
+            rc = ACR_GET_NETOS_ERROR(); 
+            closesocket(sd);
+        }
+        type |= ACR_DT_NBLOCK;
+    }
+    if (rc != 0) {
+        ACR_THROW_NET_ERROR(rc);
+        return 0;
+    }
+    if ((sp = ACR_TALLOC(acr_fd_t)) == 0) {
+        closesocket(sd);
+        return 0;
+    }
+    sp->type = type;
+    sp->refs = 1;
+    sp->u.s  = sd;
+    return P2J(sp);
+}
+
+ACR_NET_EXPORT(jint, LocalDescriptor, block0)(JNI_STDARGS, jlong fp, jboolean on)
+{
+    acr_fd_t *fd = J2P(fp, acr_fd_t *);
+    u_long   set = (u_long)on;
+
+    if (ioctlsocket(fd->u.s, FIONBIO, &set) == SOCKET_ERROR)
+        return ACR_GET_NETOS_ERROR();
+    if (on)
+        fd->type |=  ACR_DT_NBLOCK;
+    else
+        fd->type &= ~ACR_DT_NBLOCK;
+    return 0;
+}
+
+ACR_NET_EXPORT(jboolean, LocalDescriptor, isBlocking0)(JNI_STDARGS, jlong fp)
+{
+    acr_fd_t *fd = J2P(fp, acr_fd_t *);
+    return fd->type & ACR_DT_NBLOCK ? JNI_FALSE : JNI_TRUE;
+}
+
+ACR_NET_EXPORT(jint, LocalEndpoint, connect0)(JNI_STDARGS, jlong fp,
+                                              jbyteArray cb, jint timeout)
+{
+    int rc;
+    acr_sockaddr_t *ca = SOCKADDR_CAST(cb);
+    acr_fd_t *fd       = J2P(fp, acr_fd_t *);
+
+    rc = connect(fd->u.s, (const struct sockaddr *)&ca->sa.sin, ca->salen);
+    if (rc == SOCKET_ERROR)
+        rc = ACR_GET_NETOS_ERROR();
+    SOCKADDR_RELEASE(cb, ca);
+    if (rc != 0 && (fd->type & ACR_DT_NBLOCK)) {
+        if (timeout > 0 && (rc == WSAEINPROGRESS || rc == WSAEALREADY || rc ==
WSAEWOULDBLOCK)) {
+            rc = AcrWaitIO(fd->u.s, timeout, FD_WRITE);
+            if (rc == 0) {
+                int       err;
+                socklen_t len = sizeof(err);
+                if (getsockopt(fd->u.s, SOL_SOCKET, SO_ERROR, (char *)&err, &len)
== -1);
+                    rc = ACR_GET_NETOS_ERROR();
+                if (err != 0)
+                    rc = err;
+            }
+        }
+    }
+    return rc;
+}
+
+ACR_NET_EXPORT(jint, LocalServerEndpoint, bind0)(JNI_STDARGS, jlong fp,
+                                                 jbyteArray ba,
+                                                 jint backlog)
+{
+    int rc = 0;
+    acr_sockaddr_t *aa = SOCKADDR_CAST(ba);
+    acr_fd_t *fd       = J2P(fp, acr_fd_t *);
+
+    if (bind(fd->u.s, (const struct sockaddr *)&aa->sa, aa->salen) == -1)
+        rc = errno;
+    SOCKADDR_RELEASE(ba, aa);
+    if (rc == 0 && listen(fd->u.s, backlog) == -1)
+        rc = errno;
+    return rc;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/util.c Thu Jul  7 11:10:49 2011
@@ -18,6 +18,7 @@
 #include "acr/memory.h"
 #include "acr/iodefs.h"
 #include "acr/port.h"
+#include "acr/time.h"
 #include "arch_opts.h"
 
 #define PIPE_FMT    "\\\\.\\pipe\\00000000-0000-0000-0000-000000000000"
@@ -320,13 +321,13 @@ AcrPipePair(HANDLE *rd, HANDLE *wr, int 
 }
 
 int
-AcrNonblock(int sd, int on)
+AcrNonblock(SOCKET sd, int on)
 {
     u_long set = on;
 
     if (sd == -1)
         return ACR_EBADF;
-    if (ioctlsocket((SOCKET)sd, FIONBIO, &set) == SOCKET_ERROR)
+    if (ioctlsocket(sd, FIONBIO, &set) == SOCKET_ERROR)
         return ACR_GET_NETOS_ERROR();
     else
         return 0;
@@ -492,3 +493,33 @@ AcrDrainSocket(SOCKET sd)
     else
         return 0;
 }
+
+int
+AcrWaitIO(SOCKET sd, int timeout, int events)
+{
+    int            rc;
+    struct timeval  tv;
+    struct timeval *tp = 0;
+    fd_set rdset, wrset;
+
+    FD_ZERO(&rdset);
+    FD_ZERO(&wrset);
+
+    if (events & FD_READ) {
+        FD_SET(sd, &rdset);
+    }
+    if (events & FD_WRITE) {
+        FD_SET(sd, &wrset);
+    }
+    if (timeout > 0) {
+        tp = &tv;
+        tp->tv_sec  = (long)(timeout / 1000);
+        tp->tv_usec = (long)((timeout * 1000LL) % ACR_USEC_PER_SEC);
+    }
+    rc = select(1, &rdset, &wrset, 0, tp);
+    if (rc == SOCKET_ERROR)
+        return ACR_GET_NETOS_ERROR();
+    else if (rc == 0)
+        return ACR_TIMEUP;
+    return 0;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Thu Jul  7 11:10:49 2011
@@ -823,21 +823,19 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
 {
     jbyteArray     ba;
     acr_sockaddr_t sa;
-    char *np;
     int   rc = ACR_ENOTIMPL;
 
     memset(&sa, 0, sizeof(acr_sockaddr_t));
-    WITH_CSTR(hostname) {
+#if defined(WINDOWS)
+    WITH_WSTR(hostname)
+#else
+    WITH_CSTR(hostname)
+#endif
+    {
 #if HAVE_SYS_UN_H
         if (*J2S(hostname) != '/') {
-            size_t i = (int)strlcpy(sa.hostname, VAR_RUN_PATH "/", sizeof(sa.hostname));
+            strlcpy(sa.hostname, VAR_RUN_PATH "/", sizeof(sa.hostname));
             strlcat(sa.hostname, J2S(hostname), sizeof(sa.hostname));
-            np = sa.hostname + i;
-            while (*np != '\0') {
-                if (*np == '/' || *np == ' ')
-                    *np = '_';
-                np++;
-            }
         }
         else
             strlcpy(sa.hostname, J2S(hostname), sizeof(sa.hostname));
@@ -849,18 +847,28 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
         sa.iplen   = sa.addrlen;
         rc = 0;
 #elif defined(WINDOWS)
-        strlcpy(sa.hostname, "\\\\.\\pipe\\", sizeof(sa.hostname));
-        strlcat(sa.hostname, J2S(hostname), sizeof(sa.hostname));
-        np = sa.hostname + 9;
-        while (*np != '\0') {
-            if (*np == '/' || *np == '\\'|| *np == ' ')
-                *np = '_';
-            np++;
+        WCHAR buf[NI_MAXHOST];
+        /* Get the full path name in case the
+         * current directory gets changed.
+         */
+        DWORD nch = GetFullPathNameW(J2S(hostname), NI_MAXHOST, buf, 0);
+        if (nch == 0 || nch > NI_MAXHOST) {
+            /* Error or lack of space */
+            rc = nch == 0 ? GetLastError() : ACR_EOVERFLOW;
+        }
+        else {
+            if (WideCharToMultiByte(CP_UTF8, 0, buf, -1, sa.hostname,
+                                    NI_MAXHOST, 0, 0) == 0) {
+                /* COnversion failed. */
+                rc = GetLastError();
+            }
+            else {
+                sa.family  = AF_LOCAL;
+                sa.addrlen = ISIZEOF(sa.hostname);
+                sa.iplen   = sa.addrlen;
+                rc = 0;
+            }
         }
-        sa.family  = AF_LOCAL;
-        sa.addrlen = ISIZEOF(sa.hostname);
-        sa.iplen   = sa.addrlen;
-        rc = 0;
 #endif
     } DONE_WITH_STR(hostname);
 

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java?rev=1143761&r1=1143760&r2=1143761&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestAddress.java
Thu Jul  7 11:10:49 2011
@@ -30,7 +30,7 @@ public class TestAddress extends Assert
     public void posixLocalAddress()
         throws IOException
     {
-        LocalEndpointAddress la = new LocalEndpointAddress("foo/bar");
+        LocalEndpointAddress la = new LocalEndpointAddress("foo_bar");
         assertNotNull(la);
         assertEquals(la.toString(), "/var/run/foo_bar");
         assertEquals(la.getFamily(), AddressFamily.LOCAL);
@@ -42,9 +42,9 @@ public class TestAddress extends Assert
     public void windowsLocalAddress()
         throws IOException
     {
-        LocalEndpointAddress la = new LocalEndpointAddress("foo/bar");
+        LocalEndpointAddress la = new LocalEndpointAddress("foo_bar");
         assertNotNull(la);
-        assertEquals(la.toString(), "\\\\.\\pipe\\foo_bar");
+        assertEquals(la.toString(), (new File("foo_bar")).getAbsolutePath());
         la = null;
         System.gc();
     }



Mime
View raw message