all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: emacs-devel@gnu.org, somasis@exherbo.org, somasissounds@gmail.com
Subject: Re: [PATCH] src/print.c: Check for __GLIBC__ rather than GNU_LINUX
Date: Sat, 02 Apr 2016 11:35:41 +0300	[thread overview]
Message-ID: <83pou8s90i.fsf@gnu.org> (raw)
In-Reply-To: <56FE276C.2050306@cs.ucla.edu> (message from Paul Eggert on Fri,  1 Apr 2016 00:46:52 -0700)

> Cc: somasissounds@gmail.com, emacs-devel@gnu.org, somasis@exherbo.org
> From: Paul Eggert <eggert@cs.ucla.edu>
> 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	other threads:[~2016-04-02  8:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-30  5:22 [PATCH] src/print.c: Check for __GLIBC__ rather than GNU_LINUX Kylie McClain
2016-03-31 20:37 ` Paul Eggert
2016-03-31 21:26   ` Kylie McClain
2016-04-01  6:43   ` Eli Zaretskii
2016-04-01  7:46     ` Paul Eggert
2016-04-02  8:35       ` Eli Zaretskii [this message]
2016-04-03  8:24         ` Paul Eggert
2016-04-04 16:45         ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83pou8s90i.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=emacs-devel@gnu.org \
    --cc=somasis@exherbo.org \
    --cc=somasissounds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.