Author: mturk
Date: Thu Jul 7 13:58:27 2011
New Revision: 1143830
URL: http://svn.apache.org/viewvc?rev=1143830&view=rev
Log:
For local windows sockets create a file holding pid and port
Modified:
commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c
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=1143830&r1=1143829&r2=1143830&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 13:58:27 2011
@@ -280,4 +280,6 @@ wchar_t *dirname_w(const wchar_t *
int symlink_w(const wchar_t *target, const wchar_t *lnkname);
ssize_t readlink_w(const wchar_t *lnkname, wchar_t *buf, size_t len);
+PSECURITY_ATTRIBUTES GetSaWithNullDacl(JNI_STDENV, jboolean inherit);
+
#endif /* _ACR_ARCH_OPTS_H_ */
Modified: 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=1143830&r1=1143829&r2=1143830&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c Thu Jul
7 13:58:27 2011
@@ -41,17 +41,24 @@
#define SOCKADDR_RELEASE(BA, SA) \
AcrReleaseArrayCritical(env, (BA), (SA))
+typedef struct wls_fd_t {
+ acr_fd_t fd;
+ HANDLE fh;
+
+} wls_fd_t;
+
ACR_NET_EXPORT(jint, LocalDescriptor, close0)(JNI_STDARGS, jlong fp)
{
int rc = 0;
- acr_fd_t *fd = J2P(fp, acr_fd_t *);
+ wls_fd_t *wd = J2P(fp, wls_fd_t *);
- if (fd == 0)
+ if (wd == 0)
return ACR_EBADF;
- if (AcrAtomic32Dec(&fd->refs) == 0) {
- if (closesocket(fd->u.s) == SOCKET_ERROR)
+ if (AcrAtomic32Dec(&wd->fd.refs) == 0) {
+ SAFE_CLOSE_HANDLE(wd->fh);
+ if (closesocket(wd->fd.u.s) == SOCKET_ERROR)
rc = ACR_GET_NETOS_ERROR();
- AcrFree(fd);
+ AcrFree(wd);
}
return rc;
}
@@ -73,7 +80,7 @@ ACR_NET_EXPORT(jlong, LocalDescriptor, s
SOCKET sd;
int rc = 0;
int type = ACR_DT_LSOCK;
- acr_fd_t *sp;
+ wls_fd_t *sp;
switch (stype) {
case 1:
@@ -93,7 +100,7 @@ ACR_NET_EXPORT(jlong, LocalDescriptor, s
if (block == JNI_FALSE && rc == 0) {
u_long one = 1;
if (ioctlsocket(sd, FIONBIO, &one) == SOCKET_ERROR) {
- rc = ACR_GET_NETOS_ERROR();
+ rc = ACR_GET_NETOS_ERROR();
closesocket(sd);
}
type |= ACR_DT_NBLOCK;
@@ -102,13 +109,14 @@ ACR_NET_EXPORT(jlong, LocalDescriptor, s
ACR_THROW_NET_ERROR(rc);
return 0;
}
- if ((sp = ACR_TALLOC(acr_fd_t)) == 0) {
+ if ((sp = ACR_TALLOC(wls_fd_t)) == 0) {
closesocket(sd);
return 0;
}
- sp->type = type;
- sp->refs = 1;
- sp->u.s = sd;
+ sp->fd.type = type;
+ sp->fd.refs = 1;
+ sp->fd.u.s = sd;
+ sp->fh = 0;
return P2J(sp);
}
@@ -136,26 +144,61 @@ ACR_NET_EXPORT(jint, LocalEndpoint, conn
jbyteArray cb, jint timeout)
{
int rc;
+ WCHAR sname[NI_MAXHOST];
+ char ssbuf[64];
+ HANDLE sfh = 0;
+ DWORD pid, nrd, port;
+ struct sockaddr_in sa;
+ int sas = ISIZEOF(sa);
acr_sockaddr_t *ca = SOCKADDR_CAST(cb);
- acr_fd_t *fd = J2P(fp, acr_fd_t *);
+ wls_fd_t *wd = J2P(fp, wls_fd_t *);
- rc = connect(fd->u.s, (const struct sockaddr *)&ca->sa.sin, ca->salen);
+ if (MultiByteToWideChar(CP_UTF8, 0, ca->hostname, -1, sname, NI_MAXHOST)
== 0) {
+ rc = GetLastError();
+ SOCKADDR_RELEASE(cb, ca);
+ return rc;
+ }
+ SOCKADDR_RELEASE(cb, ca);
+ sfh = CreateFileW(sname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
0, 0);
+ if (IS_INVALID_HANDLE(sfh)) {
+ rc = GetLastError();
+ return rc;
+ }
+ if (!ReadFile(sfh, ssbuf, 63, &nrd, 0)) {
+ rc = GetLastError();
+ goto finally;
+ }
+ ssbuf[nrd] = 0;
+ if (sscanf(ssbuf, "%u %d", &pid, &port) != 2) {
+ /* Bad format ?
+ */
+ rc = ACR_ENOTSOCK;
+ goto finally;
+ }
+ /* TODO: Check if process is running
+ */
+ sa.sin_port = htons((u_short)port);
+ sa.sin_family = AF_INET;
+ sa.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+ rc = connect(wd->fd.u.s, (SOCKADDR *)&sa, sas);
if (rc == SOCKET_ERROR)
rc = ACR_GET_NETOS_ERROR();
- SOCKADDR_RELEASE(cb, ca);
- if (rc != 0 && (fd->type & ACR_DT_NBLOCK)) {
+ if (rc != 0 && (wd->fd.type & ACR_DT_NBLOCK)) {
if (timeout > 0 && (rc == WSAEINPROGRESS || rc == WSAEALREADY || rc ==
WSAEWOULDBLOCK)) {
- rc = AcrWaitIO(fd->u.s, timeout, FD_WRITE);
+ rc = AcrWaitIO(wd->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);
+ if (getsockopt(wd->fd.u.s, SOL_SOCKET, SO_ERROR, (char *)&err,
&len) == -1);
rc = ACR_GET_NETOS_ERROR();
if (err != 0)
rc = err;
}
}
}
+finally:
+ SAFE_CLOSE_HANDLE(sfh);
return rc;
}
@@ -164,13 +207,59 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint
jint backlog)
{
int rc = 0;
+ WCHAR sname[NI_MAXHOST];
+ char ssbuf[64];
+ HANDLE sfh = 0;
+ DWORD swr, nwr;
+ struct sockaddr_in sa;
+ int sas = ISIZEOF(sa);
+
acr_sockaddr_t *aa = SOCKADDR_CAST(ba);
- acr_fd_t *fd = J2P(fp, acr_fd_t *);
+ wls_fd_t *wd = J2P(fp, wls_fd_t *);
+
+ if (MultiByteToWideChar(CP_UTF8, 0, aa->hostname, -1, sname, NI_MAXHOST)
== 0) {
+ rc = GetLastError();
+ SOCKADDR_RELEASE(ba, aa);
+ return rc;
+ }
+ sfh = CreateFileW(sname, GENERIC_WRITE, FILE_SHARE_READ,
+ GetSaWithNullDacl(env, JNI_FALSE), CREATE_NEW,
+ FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_SYSTEM |
FILE_FLAG_DELETE_ON_CLOSE,
+ 0);
+ if (IS_INVALID_HANDLE(sfh)) {
+ rc = GetLastError();
+ goto failed;
+ }
+ sa.sin_port = 0;
+ sa.sin_family = AF_INET;
+ sa.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+ if (bind(wd->fd.u.s, (SOCKADDR *)&sa, sas) == -1) {
+ rc = ACR_GET_NETOS_ERROR();
+ goto failed;
+ }
+ if (getsockname(wd->fd.u.s, (SOCKADDR *)&sa, &sas) == SOCKET_ERROR) {
+ rc = ACR_GET_NETOS_ERROR();
+ goto failed;
+ }
+ swr = _snprintf(ssbuf, sizeof(ssbuf), "%u %d\n",
+ GetCurrentProcessId(),
+ ntohs(sa.sin_port));
+ if (!WriteFile(sfh, ssbuf, swr, &nwr, 0)) {
+ rc = GetLastError();
+ goto failed;
+ }
+ if (listen(wd->fd.u.s, backlog) == SOCKET_ERROR) {
+ rc = ACR_GET_NETOS_ERROR();
+ goto failed;
+ }
+
+ SOCKADDR_RELEASE(ba, aa);
+ wd->fh = sfh;
+ return 0;
- if (bind(fd->u.s, (const struct sockaddr *)&aa->sa, aa->salen) == -1)
- rc = errno;
+failed:
SOCKADDR_RELEASE(ba, aa);
- if (rc == 0 && listen(fd->u.s, backlog) == -1)
- rc = errno;
+ SAFE_CLOSE_HANDLE(sfh);
return rc;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c?rev=1143830&r1=1143829&r2=1143830&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c Thu Jul
7 13:58:27 2011
@@ -46,7 +46,7 @@ ACR_WIN_EXPORT(jlong, Security, Allocate
return P2J(sid);
}
-static PSECURITY_ATTRIBUTES GetSaWithNullDacl(JNI_STDENV, jboolean inherit)
+PSECURITY_ATTRIBUTES GetSaWithNullDacl(JNI_STDENV, jboolean inherit)
{
DWORD rc = 0;
PSECURITY_DESCRIPTOR pSD = 0;