Patch: close-on-exec files
Tom Tromey
tromey@redhat.com
Fri Mar 8 23:50:00 GMT 2002
This patch changes libgcj so that all new file descriptors are marked
as close-on-exec. The idea here is that we don't want new processes
to be started with a bunch of random open files or connections.
I'm not checking this in yet since I don't know whether it will work
on Windows. This seems to be a drawback of our current java.net code
-- it is hard to change in a platform-specific way.
Adam, please advise. Will this work on Windows? Is it even
necessary?
I suppose I could introduce a new _Jv_platform_close_on_exec function
and then add a null implementation for non-POSIX platforms. What do
people think of that?
Tom
Index: ChangeLog
from Tom Tromey <tromey@redhat.com>
* java/net/natPlainSocketImpl.cc (create): Set close-on-exec
flag.
(accept): Likewise.
* java/net/natPlainDatagramSocketImpl.cc (create): Set
close-on-exec flag.
* java/io/natFileDescriptorPosix.cc (open): Set close-on-exec
flag.
Index: java/net/natPlainDatagramSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainDatagramSocketImpl.cc,v
retrieving revision 1.33
diff -u -r1.33 natPlainDatagramSocketImpl.cc
--- java/net/natPlainDatagramSocketImpl.cc 2002/02/25 05:21:45 1.33
+++ java/net/natPlainDatagramSocketImpl.cc 2002/03/08 22:50:10
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999, 2000 Free Software Foundation
+/* Copyright (C) 1999, 2000, 2002 Free Software Foundation
This file is part of libgcj.
@@ -28,6 +28,7 @@
#endif
#include <errno.h>
#include <string.h>
+#include <fcntl.h>
#endif /* WIN32 */
#if HAVE_BSTRING_H
@@ -185,6 +186,11 @@
char* strerr = strerror (errno);
throw new java::net::SocketException (JvNewStringUTF (strerr));
}
+
+ // We don't want this fd to be open in processes we might exec.
+ // However, we ignore errors as this is not critical.
+ fcntl (sock, F_SETFD, FD_CLOEXEC);
+
fnum = sock;
fd = new java::io::FileDescriptor (sock);
}
Index: java/net/natPlainSocketImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/natPlainSocketImpl.cc,v
retrieving revision 1.32
diff -u -r1.32 natPlainSocketImpl.cc
--- java/net/natPlainSocketImpl.cc 2002/03/08 01:46:34 1.32
+++ java/net/natPlainSocketImpl.cc 2002/03/08 22:50:10
@@ -48,6 +48,7 @@
#include <netinet/tcp.h>
#include <errno.h>
#include <string.h>
+#include <fcntl.h>
#endif /* WIN32 */
#endif /* DISABLE_JAVA_NET */
@@ -229,6 +230,11 @@
char* strerr = strerror (errno);
throw new java::io::IOException (JvNewStringUTF (strerr));
}
+
+ // We don't want this fd to be open in processes we might exec.
+ // However, we ignore errors as this is not critical.
+ fcntl (sock, F_SETFD, FD_CLOEXEC);
+
fnum = sock;
fd = new java::io::FileDescriptor (sock);
}
@@ -371,6 +377,11 @@
new_socket = _Jv_accept (fnum, (sockaddr*) &u, &addrlen);
if (new_socket < 0)
goto error;
+
+ // We don't want this fd to be open in processes we might exec.
+ // However, we ignore errors as this is not critical.
+ fcntl (new_socket, F_SETFD, FD_CLOEXEC);
+
jbyteArray raddr;
jint rport;
if (u.address.sin_family == AF_INET)
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.18
diff -u -r1.18 natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc 2002/03/06 23:23:34 1.18
+++ java/io/natFileDescriptorPosix.cc 2002/03/08 22:50:11
@@ -122,6 +122,11 @@
sprintf (msg, "%s (%s)", buf, strerror (errno));
throw new FileNotFoundException (JvNewStringLatin1 (msg));
}
+
+ // We don't want this fd to be open in processes we might exec.
+ // However, we ignore errors as this is not critical.
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
+
return fd;
}
More information about the Java-patches
mailing list