This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] [MinGW]: _Jv_close_on_exec


Hi People,

>This attempts to provide an implementation for _Jv_close_on_exec,
>which for POSIX is implemented via ::fcntl (fd, F_SETFD, FD_CLOEXEC).

This variant reflects the current state of CVS. I'll let this age
on the list for awhile and check it in in a week or so if no one
screams beforehand.

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/

ChangeLog
2003-10-30  Mohan Embar  <gnustuff@thisiscool.com>

	* include/win32.h (_Jv_platform_close_on_exec): Changed
	signature and declared extern.
	* win32.cc (_Jv_platform_close_on_exec): Implemented.
	* gnu/java/net/natPlainDatagramSocketImplWin32.cc
	(create): Use new signature of _Jv_platform_close_on_exec.
	* gnu/java/net/natPlainSocketImplWin32.cc 
	(create): Eliminated a few typecasts
	Use new signature of _Jv_platform_close_on_exec.
	(accept): Eliminated a few typecasts
	Use new signature of _Jv_platform_close_on_exec.
	* java/io/natFileDescriptorWin32.cc (open): Use
	_Jv_platform_close_on_exec.

Index: include/win32.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/win32.h,v
retrieving revision 1.23
diff -u -2 -r1.23 win32.h
--- include/win32.h	17 Oct 2003 18:44:42 -0000	1.23
+++ include/win32.h	31 Oct 2003 03:42:14 -0000
@@ -98,9 +98,6 @@
 extern int _Jv_pipe (int filedes[2]);
 
-inline void
-_Jv_platform_close_on_exec (jint)
-{
-  // Ignore.
-}
+extern void
+_Jv_platform_close_on_exec (HANDLE h);
 
 #ifdef JV_HASH_SYNCHRONIZATION
Index: win32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/win32.cc,v
retrieving revision 1.18
diff -u -2 -r1.18 win32.cc
--- win32.cc	31 Oct 2003 03:31:54 -0000	1.18
+++ win32.cc	31 Oct 2003 03:42:09 -0000
@@ -364,2 +364,10 @@
   return _pipe (filedes, 4096, _O_BINARY);
 }
+
+void
+_Jv_platform_close_on_exec (HANDLE h)
+{
+  // Mark the handle as non-inheritable. This has
+  // no effect under Win9X.
+  SetHandleInformation (h, HANDLE_FLAG_INHERIT, 0);
+}
Index: gnu/java/net/natPlainDatagramSocketImplWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainDatagramSocketImplWin32.cc,v
retrieving revision 1.3
diff -u -2 -r1.3 natPlainDatagramSocketImplWin32.cc
--- gnu/java/net/natPlainDatagramSocketImplWin32.cc	21 Oct 2003 12:29:43 -0000	1.3
+++ gnu/java/net/natPlainDatagramSocketImplWin32.cc	31 Oct 2003 03:42:12 -0000
@@ -70,9 +70,12 @@
     }
 
-  _Jv_platform_close_on_exec (sock);
+  // Cast this to a HANDLE so we can make
+  // it non-inheritable via _Jv_platform_close_on_exec.
+  HANDLE hSocket = (HANDLE) sock;
+  _Jv_platform_close_on_exec (hSocket);
 
   // We use native_fd in place of fd here.  From leaving fd null we avoid
   // the double close problem in FileDescriptor.finalize.
-  native_fd = (int) sock;
+  native_fd = (jint) hSocket;
 }
 
Index: gnu/java/net/natPlainSocketImplWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/natPlainSocketImplWin32.cc,v
retrieving revision 1.6
diff -u -2 -r1.6 natPlainSocketImplWin32.cc
--- gnu/java/net/natPlainSocketImplWin32.cc	31 Oct 2003 03:19:51 -0000	1.6
+++ gnu/java/net/natPlainSocketImplWin32.cc	31 Oct 2003 03:42:13 -0000
@@ -46,16 +46,19 @@
 gnu::java::net::PlainSocketImpl::create (jboolean stream)
 {
-  int sock = ::socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
+  SOCKET sock = ::socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
 
-  if (sock == int(INVALID_SOCKET))
+  if (sock == INVALID_SOCKET)
     {
       _Jv_ThrowIOException ();
     }
 
-  _Jv_platform_close_on_exec (sock);
+  // Cast this to a HANDLE so we can make
+  // it non-inheritable via _Jv_platform_close_on_exec.
+  HANDLE hSocket = (HANDLE) sock;
+  _Jv_platform_close_on_exec (hSocket);
 
   // We use native_fd in place of fd here.  From leaving fd null we avoid
   // the double close problem in FileDescriptor.finalize.
-  native_fd = sock;
+  native_fd = (jint) hSocket;
 }
 
@@ -231,5 +234,6 @@
   union SockAddr u;
   socklen_t addrlen = sizeof(u);
-  int new_socket = 0;
+  HANDLE hSocket = 0;
+  SOCKET new_socket = 0;
 
   if (timeout > 0)
@@ -246,5 +250,5 @@
         new_socket = ::accept (native_fd, (sockaddr*) &u, &addrlen);
 
-        if (new_socket != int(INVALID_SOCKET))
+        if (new_socket != INVALID_SOCKET)
         {
           // This new child socket is nonblocking because the parent
@@ -285,8 +289,11 @@
     }
 
-  if (new_socket == int(INVALID_SOCKET))
+  if (new_socket == INVALID_SOCKET)
     goto error;
 
-  _Jv_platform_close_on_exec (new_socket);
+  // Cast this to a HANDLE so we can make
+  // it non-inheritable via _Jv_platform_close_on_exec.
+  hSocket = (HANDLE) new_socket;
+  _Jv_platform_close_on_exec (hSocket);
 
   jbyteArray raddr;
@@ -309,5 +316,5 @@
     throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
 
-  s->native_fd = new_socket;
+  s->native_fd = (jint) hSocket;
   s->localport = localport;
   s->address = new ::java::net::InetAddress (raddr, NULL);
Index: java/io/natFileDescriptorWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorWin32.cc,v
retrieving revision 1.15
diff -u -2 -r1.15 natFileDescriptorWin32.cc
--- java/io/natFileDescriptorWin32.cc	29 Aug 2003 04:21:00 -0000	1.15
+++ java/io/natFileDescriptorWin32.cc	31 Oct 2003 03:42:16 -0000
@@ -134,5 +134,11 @@
       }
     }
-  return (jint)handle;
+    
+  // Make this handle non-inheritable so that child
+  // processes don't inadvertently prevent us from
+  // closing this file.
+  _Jv_platform_close_on_exec (handle);
+
+  return (jint) handle;
 }
 




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]