From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: bug#33154: 27.0.50; create_process on Darwin should not invoke setsid() after vfork() [PATCH] Date: Tue, 6 Nov 2018 23:41:46 -0800 Organization: UCLA Computer Science Department Message-ID: References: <8237cee2-9e65-4093-8077-a458cf58c911@cs.ucla.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2943A0983D181D379495110C" X-Trace: blaine.gmane.org 1541576436 9411 195.159.176.226 (7 Nov 2018 07:40:36 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 7 Nov 2018 07:40:36 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 Cc: Alan Third , 33154@debbugs.gnu.org To: Filipp Gunbin Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Nov 07 08:40:31 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gKIRx-0002LX-KB for geb-bug-gnu-emacs@m.gmane.org; Wed, 07 Nov 2018 08:40:29 +0100 Original-Received: from localhost ([::1]:46199 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKIU3-0006ML-Qt for geb-bug-gnu-emacs@m.gmane.org; Wed, 07 Nov 2018 02:42:39 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52220) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKITv-0006Ln-1w for bug-gnu-emacs@gnu.org; Wed, 07 Nov 2018 02:42:35 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gKITe-0005mf-4f for bug-gnu-emacs@gnu.org; Wed, 07 Nov 2018 02:42:21 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:33806) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gKITS-0005hN-Bc for bug-gnu-emacs@gnu.org; Wed, 07 Nov 2018 02:42:06 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gKITS-0001Dk-6D for bug-gnu-emacs@gnu.org; Wed, 07 Nov 2018 02:42:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Paul Eggert Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 07 Nov 2018 07:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33154 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 33154-submit@debbugs.gnu.org id=B33154.15415765164676 (code B ref 33154); Wed, 07 Nov 2018 07:42:02 +0000 Original-Received: (at 33154) by debbugs.gnu.org; 7 Nov 2018 07:41:56 +0000 Original-Received: from localhost ([127.0.0.1]:38064 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gKITM-0001DM-18 for submit@debbugs.gnu.org; Wed, 07 Nov 2018 02:41:56 -0500 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:38100) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gKITK-0001D6-1f for 33154@debbugs.gnu.org; Wed, 07 Nov 2018 02:41:54 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id B88C11600A5; Tue, 6 Nov 2018 23:41:47 -0800 (PST) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id pag2SdarmWuo; Tue, 6 Nov 2018 23:41:46 -0800 (PST) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id C14871600AB; Tue, 6 Nov 2018 23:41:46 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id PHgC5XKaZ1G1; Tue, 6 Nov 2018 23:41:46 -0800 (PST) Original-Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 919C11600A5; Tue, 6 Nov 2018 23:41:46 -0800 (PST) In-Reply-To: Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:152119 Archived-At: This is a multi-part message in MIME format. --------------2943A0983D181D379495110C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Filipp Gunbin wrote: > In your patch, we don't detach from current (Emacs's) controlling > terminal before doing TIOCSCTTY. Ah, OK. I see also that vfork won't work on Darwin if pty mode is used, since Emacs wants to create a new session and Darwin setsid always fails in a vforked child that has not yet execed. However, your patch introduces another duplicate of the open/TIOCNOTTY/close fallback code, making three duplicates in all. How about if we coalesce these duplicates into a function and then call that function? Also, I think we can call the function from just two places (not three). Furthermore, I think it'd be more robust if Emacs does setsid everywhere (with a fallback to open/TIOCNOTTY/close everywhere TIOCNOTTY is available), not just Darwin. Proposed patch (against master) attached. --------------2943A0983D181D379495110C Content-Type: text/x-patch; name="0001-Dissociate-controlling-tty-better-on-Darwin.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Dissociate-controlling-tty-better-on-Darwin.patch" >From cff3581f79d5eeb251fb683250bf48da3f68895a Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 6 Nov 2018 23:30:26 -0800 Subject: [PATCH] Dissociate controlling tty better on Darwin * src/process.c (dissociate_controlling_tty): New function. (create_process): Use it to dissociate controlling tty if setsid fails, which happens on Darwin after a vfork (Bug#33154). Do this on all platforms, not just on Darwin, as a similar problem is plausible elsewhere. * src/callproc.c (call_process): Use the new function here, too, for consistency and to avoid duplicate code. --- src/callproc.c | 14 +------------- src/process.c | 40 ++++++++++++++++++++++------------------ src/process.h | 1 + 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/callproc.c b/src/callproc.c index a2cfd2e94d..9f47c79b81 100644 --- a/src/callproc.c +++ b/src/callproc.c @@ -643,19 +643,7 @@ call_process (ptrdiff_t nargs, Lisp_Object *args, int filefd, #endif unblock_child_signal (&oldset); - -#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 + dissociate_controlling_tty (); /* Emacs ignores SIGPIPE, but the child should not. */ signal (SIGPIPE, SIG_DFL); diff --git a/src/process.c b/src/process.c index 6cda4f27ac..7e78e172d3 100644 --- a/src/process.c +++ b/src/process.c @@ -1949,6 +1949,26 @@ close_process_fd (int *fd_addr) } } +void +dissociate_controlling_tty (void) +{ + if (setsid () < 0) + { +#ifdef TIOCNOTTY + /* Needed on Darwin after vfork, since setsid fails in a vforked + child that has not execed. + I wonder: would just ioctl (fd, TIOCNOTTY, 0) work here, for + some fd that the caller already has? */ + int ttyfd = emacs_open (DEV_TTY, O_RDWR, 0); + if (0 <= ttyfd) + { + ioctl (ttyfd, TIOCNOTTY, 0); + emacs_close (ttyfd); + } +#endif + } +} + /* Indexes of file descriptors in open_fds. */ enum { @@ -2097,9 +2117,8 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) { /* 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. */ - setsid (); + dissociate_controlling_tty (); + /* Make the pty's terminal the controlling terminal. */ if (pty_flag && forkin >= 0) { @@ -2128,21 +2147,6 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) } #endif #endif -#ifdef TIOCNOTTY - /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you - can do TIOCSPGRP only to the process's controlling tty. */ - if (pty_flag) - { - /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? - I can't test it since I don't have 4.3. */ - int j = emacs_open (DEV_TTY, O_RDWR, 0); - if (j >= 0) - { - ioctl (j, TIOCNOTTY, 0); - emacs_close (j); - } - } -#endif /* TIOCNOTTY */ #if !defined (DONT_REOPEN_PTY) /*** There is a suggestion that this ought to be a diff --git a/src/process.h b/src/process.h index 3c6dd7b91f..67b783400d 100644 --- a/src/process.h +++ b/src/process.h @@ -300,6 +300,7 @@ extern Lisp_Object network_interface_info (Lisp_Object); extern Lisp_Object remove_slash_colon (Lisp_Object); extern void update_processes_for_thread_death (Lisp_Object); +extern void dissociate_controlling_tty (void); INLINE_HEADER_END -- 2.17.1 --------------2943A0983D181D379495110C--