close on exec atomics

Eric Blake ebb9@byu.net
Tue Jul 28 10:14:00 GMT 2009


Corinna Vinschen <vinschen <at> redhat.com> writes:

> > Should I go ahead and prepare a patch for the newlib side?
> 
> It's not that easy to implement, especially not the fcntl which requires
> to redefine the dup2 methods throughout to allow atomic operation on the
> target OS handles.  open (O_CLOEXEC) is much easier.  Anyway, can we
> wait until after Cygwin 1.7.1?

No problem waiting for the cygwin side of things.  On the newlib side, the 
usage will have to be guarded appropriately, so that it will just start working 
when the OS catches up.  For example, __sflags (used by fopen to parse the 
mode) will have to do: 

if 'e' found in mode string:
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
# define __O_CLOEXEC_REQUESTED <some unused bit>
#endif
#if O_CLOEXEC
  flags |= O_CLOEXEC;
#elif HAVE_FCNTL
  flags |= __O_CLOEXEC_REQUESTED;
#else
  /* Silently ignored, since we can't support it.  */
#endif

then fopen will have to do:
/* Atomic if supported by OS.  */
f = _open_r (, flags & ~__O_CLOEXEC_REQUESTED);
#if HAVE_FCNTL
/* Racy, but still provides convenience.  */
if (flags & __O_CLOEXEC_REQUESTED)
  fcntl (f, F_SETFD, FD_CLOEXEC | fcntl (f, F_GETFD));
#else
/* Silently ignored, since we can't support it.  */  
#endif

-- 
Eric Blake




More information about the Newlib mailing list