emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] src/print.c: Check for __GLIBC__ rather than GNU_LINUX


From: Eli Zaretskii
Subject: Re: [PATCH] src/print.c: Check for __GLIBC__ rather than GNU_LINUX
Date: Sat, 02 Apr 2016 11:35:41 +0300

> Cc: address@hidden, address@hidden, address@hidden
> From: Paul Eggert <address@hidden>
> Date: Fri, 1 Apr 2016 00:46:52 -0700
> 
> > After that, temacs seems to hang during loadup.  Looks like it hangs
> > inside the libc function 'close'.  I don't have enough free time now
> > to do anything serious on master.
> 
> No rush. When you get time, if you happen to know who's calling 'close' I 
> could 
> ifdef that call out on MS-Windows.

It turns out the call to dup2 with 2 identical arguments has a strange
side effect with MS implementation: the subsequent attempt to fclose
the corresponding stdio stream hangs.  I was unable to understand why
that happens, since according to my references, such a dup2 call
should just validate the original file descriptor and do nothing else.
But since we already have a sys_dup2 function that wraps the MS dup2,
I added a workaround there, see the patch below.

For the record, what init_standard_fds does is unnecessary on
MS-Windows, since we already have the equivalent code in init_ntproc
(for reasons explained in the comments there).  So an alternative
would be to make init_standard_fds a no-op for WINDOWSNT.  I preferred
to have less #ifdef's in mainline Emacs code, but if you think the
alternative is better, it's fine with me.

Btw, are changes like the one below safe?

     /* Manipulate tty.  */
     if (hide_char)
       {
  -      etty_valid = emacs_get_tty (fileno (stdin), &etty) == 0;
  +      etty_valid = emacs_get_tty (STDIN_FILENO, &etty) == 0;
         if (etty_valid)
  -     set_binary_mode (fileno (stdin), O_BINARY);
  -      suppress_echo_on_tty (fileno (stdin));
  +     set_binary_mode (STDIN_FILENO, O_BINARY);
  +      suppress_echo_on_tty (STDIN_FILENO);
       }

Are we sure fileno(stdin) will necessarily return 0, what with all the
redirections and stuff?  Why not keep the original code?

So to sum it up, these additional changes are required for MS-Windows:

diff --git a/src/print.c b/src/print.c
index 2b53d75..ee60f57 100644
--- a/src/print.c
+++ b/src/print.c
@@ -38,6 +38,10 @@ along with GNU Emacs.  If not, see 
<http://www.gnu.org/licenses/>.  */
 #include <float.h>
 #include <ftoastr.h>
 
+#ifdef WINDOWSNT
+#include <sys/socket.h>                /* for F_DUPFD_CLOEXEC */
+#endif
+
 struct terminal;
 
 /* Avoid actual stack overflow in print.  */
diff --git a/src/w32.c b/src/w32.c
index 3f4ac88..94aa7d8 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -8181,17 +8181,33 @@ sys_dup2 (int src, int dst)
       return -1;
     }
 
-  /* make sure we close the destination first if it's a pipe or socket */
-  if (src != dst && fd_info[dst].flags != 0)
+  /* MS _dup2 seems to have weird side effect when invoked with 2
+     identical arguments: an attempt to fclose the corresponding stdio
+     stream after that hangs (we do close standard streams in
+     init_ntproc).  Attempt to avoid that by not calling _dup2 that
+     way: if SRC is valid, we know that dup2 should be a no-op, so do
+     nothing and return DST.  */
+  if (src == dst)
+    {
+      if ((HANDLE)_get_osfhandle (src) == INVALID_HANDLE_VALUE)
+       {
+         errno = EBADF;
+         return -1;
+       }
+      return dst;
+    }
+
+  /* Make sure we close the destination first if it's a pipe or socket.  */
+  if (fd_info[dst].flags != 0)
     sys_close (dst);
 
   rc = _dup2 (src, dst);
   if (rc == 0)
     {
-      /* duplicate our internal info as well */
+      /* Duplicate our internal info as well.  */
       fd_info[dst] = fd_info[src];
     }
-  return rc;
+  return rc == 0 ? dst : rc;
 }
 
 int



reply via email to

[Prev in Thread] Current Thread [Next in Thread]