This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Patch: use close-on-exec
- From: Tom Tromey <tromey at redhat dot com>
- To: Andreas Schwab <schwab at suse dot de>
- Cc: Binutils Development <binutils at sourceware dot org>
- Date: Tue, 28 Oct 2008 12:07:05 -0600
- Subject: Re: Patch: use close-on-exec
- References: <m3wsftpse5.fsf@fleche.redhat.com> <jezlkp9htl.fsf@sykes.suse.de>
- Reply-to: Tom Tromey <tromey at redhat dot com>
>>>>> "Andreas" == Andreas Schwab <schwab@suse.de> writes:
>> 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.
Andreas> You might want to check for FD_CLOEXEC and use 1 instead if not
Andreas> defined.
Here's an update.
Tom
2008-10-28 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.
(FD_CLOEXEC): New define.
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 28 Oct 2008 18:05:34 -0000
@@ -38,6 +38,10 @@
#define S_IXOTH 0001 /* Execute by others. */
#endif
+#ifndef FD_CLOEXEC
+#define FD_CLOEXEC 1
+#endif
+
file_ptr
real_ftell (FILE *file)
{
@@ -62,13 +66,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/config.in
===================================================================
RCS file: /cvs/src/src/bfd/config.in,v
retrieving revision 1.41
diff -u -r1.41 config.in
--- bfd/config.in 11 Sep 2008 19:02:01 -0000 1.41
+++ bfd/config.in 28 Oct 2008 18:05:34 -0000
@@ -78,6 +78,9 @@
/* Define to 1 if you have the `fdopen' function. */
#undef HAVE_FDOPEN
+/* Define to 1 if you have the `fileno' function. */
+#undef HAVE_FILENO
+
/* Define to 1 if you have the `fopen64' function. */
#undef HAVE_FOPEN64
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 28 Oct 2008 18:05:35 -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)