This is the mail archive of the binutils@sourceware.org mailing list for the binutils 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]

Patch: use close-on-exec


It turns out that when gdb execs an inferior, the new process inherits
a number of open file descriptors.  This is a bit unfriendly.

I decided to attack the problem at its source, and mark file
descriptors as close-on-exec when they are created.  This patch
updates BFD to do so.

I don't really remember most of my portability foo.  I just check for
fileno and F_GETFD.  I hope that is sufficient; if not it is at least
easily fixed.

I built and regression tested this (ld, gas, and binutils) on x86 F9.

Ok?

Tom

2008-10-27  Tom Tromey  <tromey@redhat.com>

	* configure, config.in: Rebuild.
	* configure.in: Check for fileno.
	* bfdio.c (close_on_exec): New function.
	(real_fopen): Use it.

Index: bfd/bfdio.c
===================================================================
RCS file: /cvs/src/src/bfd/bfdio.c,v
retrieving revision 1.17
diff -u -r1.17 bfdio.c
--- bfd/bfdio.c	20 Feb 2008 17:42:35 -0000	1.17
+++ bfd/bfdio.c	27 Oct 2008 22:43:56 -0000
@@ -62,13 +62,30 @@
 #endif
 }
 
+/* Mark FILE as close-on-exec.  Return FILE.  FILE may be NULL, in
+   which case nothing is done.  */
+static FILE *
+close_on_exec (FILE *file)
+{
+#if defined (HAVE_FILENO) && defined (F_GETFD)
+  if (file)
+    {
+      int fd = fileno (file);
+      int old = fcntl (fd, F_GETFD, 0);
+      if (old >= 0)
+	fcntl (fd, F_SETFD, old | FD_CLOEXEC);
+    }
+#endif
+  return file;
+}
+
 FILE *
 real_fopen (const char *filename, const char *modes)
 {
 #if defined (HAVE_FOPEN64)
-  return fopen64 (filename, modes);
+  return close_on_exec (fopen64 (filename, modes));
 #else
-  return fopen (filename, modes);
+  return close_on_exec (fopen (filename, modes));
 #endif
 }
 
Index: bfd/configure.in
===================================================================
RCS file: /cvs/src/src/bfd/configure.in,v
retrieving revision 1.246
diff -u -r1.246 configure.in
--- bfd/configure.in	8 Oct 2008 15:58:25 -0000	1.246
+++ bfd/configure.in	27 Oct 2008 22:43:58 -0000
@@ -179,7 +179,7 @@
 AC_HEADER_TIME
 AC_HEADER_DIRENT
 ACX_HEADER_STRING
-AC_CHECK_FUNCS(fcntl getpagesize setitimer sysconf fdopen getuid getgid)
+AC_CHECK_FUNCS(fcntl getpagesize setitimer sysconf fdopen getuid getgid fileno)
 AC_CHECK_FUNCS(strtoull)
 
 AC_CHECK_DECLS(basename)


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