close-on-exec

Alan Coopersmith alan.coopersmith at oracle.com
Wed Feb 25 14:27:30 PST 2015


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.)

-- 
	-Alan Coopersmith-              alan.coopersmith at oracle.com
	 Oracle Solaris Engineering - http://blogs.oracle.com/alanc


More information about the xorg-devel mailing list