close-on-exec
Emil Velikov
emil.l.velikov at gmail.com
Thu Feb 26 08:04:39 PST 2015
On 25/02/15 22:27, Alan Coopersmith wrote:
> On 02/25/15 01:24 PM, Thomas Klausner wrote:
>> I don't know what the proper way to do this portably, but this is
>> the diff we currently have in NetBSD's xsrc for xsm to close file
>> descriptors on exec.
>>
>> I see that glibc added fopen's "e" flag for 2.7 (in 2007). No idea
>> about other operating systems, the flag was new to me too :)
>
> Solaris has the ancient support for setting the FD_CLOEXEC flag via
> fcntl() in all versions, and the more recent support for the O_CLOEXEC
> flag to open() in Solaris 11 and later, but does not yet support the
> "e" flag to fopen(). (I've filed an enhancement request to add it to
> our libc in the future, but that doesn't help today.)
>
> I believe the most portable way to do this is:
>
> if (!(addfp = fopen (addAuthFile, "w")))
> goto bad;
> else
> fcntl(fileno(addfp), F_SETFD, FD_CLOEXEC);
>
> That would be subject to race conditions in multi-threaded processes, as
> described in https://udrepper.livejournal.com/20407.html, so not safe in
> libraries or programs which deliberately start multiple threads, but since
> xsm is not one of those, it should be mostly safe. (Not completely, because
> we can't be sure nothing we called in a library didn't start a thread
> behind
> the scenes to handle one of our requests, but without an "e" flag
> everywhere,
> I'm not sure what more we can do. I have no idea how to write a configure
> check to test for fopen(..., "e") support either.)
>
Something like the following should be ok, but I haven't tested it.
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#define _POSIX_SOURCE
#include <stdio.h>
int main () {
FILE *fp;
fp = fopen("/tmp/foo", "e");
return 0;
}]])], AC_DEFINE([HAVE_FOPEN_E], [1],
[Define if fopen(... , "e") exists.])
)
-Emil
More information about the xorg-devel
mailing list