Subject: Re: bug#14803: Setting close-on-exec flag consistently

From: Paul Eggert <[email-masked]>
Date: Sat, 06 Jul 2013 14:43:26 -0700

On 07/06/2013 10:54 AM, Eli Zaretskii wrote:
>> Date: Sat, 06 Jul 2013 10:18:07 -0700
>> From: Paul Eggert <[email-masked]>
>> On systems like w32 that lack SOCK_CLOEXEC, the code creates
>> sockets just as it did before (using plain 'socket' and 'accept'
>> rather than 'socket' and 'accept4' with SOCK_CLOEXEC).
> 
> Given that w32 redirects calls to 'socket' to 'sys_socket', which is
> implemented on w32.c, will the above still work?

Yes, the idea is that it does the same calls as before,
to the same code in w32.c as before.

> I assume that lib/fcntl.c and
> lib/pipe2.c will not be compiled for w32, due to some configure test?

Yes, though that needs to be arranged.  The following further
patch should do it:

=== modified file 'ChangeLog'
--- ChangeLog	2013-07-06 09:44:23 +0000
+++ ChangeLog	2013-07-06 21:39:31 +0000
@@ -1,7 +1,9 @@
 2013-07-06  Paul Eggert  <[email-masked]>
 
 	Make file descriptors close-on-exec when possible (Bug#14803).
-	* configure.ac (mkostemp): New function to check for.
+	* configure.ac (ac_cv_func_fcntl, gl_cv_func_fcntl_f_dupfd_cloexec)
+	(gl_cv_func_fcntl_f_dupfd_works, ac_cv_func_pipe2): Hotwire for MinGW.
+	(mkostemp): New function to check for.
 	(PTY_OPEN): Pass O_CLOEXEC to posix_openpt.
 	* lib/fcntl.c, lib/getdtablesize.c, lib/pipe2.c, m4/fcntl.m4:
 	* m4/getdtablesize.m4, m4/pipe2.m4: New files, taken from gnulib.

=== modified file 'configure.ac'
--- configure.ac	2013-07-06 07:04:07 +0000
+++ configure.ac	2013-07-06 21:33:56 +0000
@@ -634,6 +634,19 @@
 # Avoid gnulib's threadlib module, as we do threads our own way.
 AC_DEFUN([gl_THREADLIB])
 
+# On MinGW, Emacs supplies its own substitutes for some missing functions,
+# so hotwire the cache to pretend to Gnulib that the functions work;
+# that way, 'configure' won't arrange to build the Gnulib substitutes.
+if test "$opsys" = mingw32; then
+  # fcntl
+  ac_cv_func_fcntl=emulated # Any single word other than 'no' will do.
+  gl_cv_func_fcntl_f_dupfd_cloexec=yes # Actually 'no', but only 'yes' works.
+  gl_cv_func_fcntl_f_dupfd_works='no, but Emacs emulates yes'
+
+  # pipe2
+  ac_cv_func_pipe2='no, but Emacs emulates yes'
+fi
+
 # Initialize gnulib right after choosing the compiler.
 dnl Amongst other things, this sets AR and ARFLAGS.
 gl_EARLY