Subject: Re: bug#33050: 27.0.50; [macOS] Problem with process input with process-connection-type nil

From: Filipp Gunbin <[email-masked]>
Date: Thu, 25 Oct 2018 20:10:06 +0300

On 25/10/2018 19:24 +0300, Eli Zaretskii wrote:

>> From: Filipp Gunbin <[email-masked]>
>> Cc: Eli Zaretskii <[email-masked]>,  [email-masked]
>> Date: Thu, 25 Oct 2018 18:51:17 +0300
>>
>> With process-connection-type nil, we call vfork() and then setsid()
>> apparently fails (at least to reset child's controlling terminal).
>>
>> I tried to change the above to just use fork(), and got the correct
>> behavior.
>>
>> If only I knew how to fix this correctly, without reverting to fork()
>> :-) Does anyone have ideas?  I'll try to look into this further myself,
>> and will write back.
>
> I'm probably missing something: why are you still trying to find a
> solution, when one was already found?  What's wrong with setting
> process-connection-type non-nil in this case, at least for Darwin?

Yes, setting p-c-t to t worked from the start, but it's just a
workaround - there's still problem with p-c-t nil.

I'm currently running with this patch, which is based on Alan's fix in
callproc.c (call_process() had the same problem on Darwin).

Filipp

diff --git a/src/process.c b/src/process.c
index 6cda4f27ac..1f8810927d 100644
--- a/src/process.c
+++ b/src/process.c
@@ -2066,21 +2066,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
   int volatile forkerr_volatile = forkerr;
   struct Lisp_Process *p_volatile = p;
 
-#ifdef DARWIN_OS
-  /* Darwin doesn't let us run setsid after a vfork, so use fork when
-     necessary.  Also, reset SIGCHLD handling after a vfork, as
-     apparently macOS can mistakenly deliver SIGCHLD to the child.  */
-  if (pty_flag)
-    pid = fork ();
-  else
-    {
-      pid = vfork ();
-      if (pid == 0)
-	signal (SIGCHLD, SIG_DFL);
-    }
-#else
   pid = vfork ();
-#endif
 
   current_dir = current_dir_volatile;
   lisp_pty_name = lisp_pty_name_volatile;
@@ -2091,15 +2077,35 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
   p = p_volatile;
 
   pty_flag = p->pty_flag;
-
   if (pid == 0)
 #endif /* not WINDOWSNT */
     {
+#ifdef DARWIN_OS
+      /* Work around a macOS bug, where SIGCHLD is apparently
+	 delivered to a vforked child instead of to its parent.  See:
+	 https://lists.gnu.org/r/emacs-devel/2017-05/msg00342.html
+      */
+      signal (SIGCHLD, SIG_DFL);
+#endif
+
       /* Make the pty be the controlling terminal of the process.  */
 #ifdef HAVE_PTYS
       /* First, disconnect its current controlling terminal.
 	 Do this even if !PTY_FLAG; see Bug#30762.  */
+#ifdef DARWIN_OS
+      /* Darwin doesn't let us run setsid after a vfork, so use
+         TIOCNOTTY when necessary. */
+      {
+	int j = emacs_open (DEV_TTY, O_RDWR, 0);
+	if (j >= 0)
+	  {
+	    ioctl (j, TIOCNOTTY, 0);
+	    emacs_close (j);
+	  }
+      }
+#else
       setsid ();
+#endif
       /* Make the pty's terminal the controlling terminal.  */
       if (pty_flag && forkin >= 0)
 	{