From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 16 22:39:35 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A53D1106564A; Thu, 16 Aug 2012 22:39:35 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) by mx1.freebsd.org (Postfix) with ESMTP id 39C8E8FC14; Thu, 16 Aug 2012 22:39:35 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 0E9781DD59C; Fri, 17 Aug 2012 00:39:34 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id E92512847B; Fri, 17 Aug 2012 00:39:33 +0200 (CEST) Date: Fri, 17 Aug 2012 00:39:33 +0200 From: Jilles Tjoelker To: Konstantin Belousov Message-ID: <20120816223933.GA19925@stack.nl> References: <5029D727.2090105@freebsd.org> <20120814081830.GA5883@deviant.kiev.zoral.com.ua> <502A1788.9090702@freebsd.org> <20120814094111.GB5883@deviant.kiev.zoral.com.ua> <502A6B7A.6070504@gmail.com> <20120814210911.GA90640@stack.nl> <502AE1D4.4060308@gmail.com> <20120815174942.GN5883@deviant.kiev.zoral.com.ua> <502C3D8B.4060008@gmail.com> <20120816114426.GR5883@deviant.kiev.zoral.com.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120816114426.GR5883@deviant.kiev.zoral.com.ua> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: freebsd-hackers@freebsd.org, davidxu@freebsd.org Subject: Re: system() using vfork() or posix_spawn() and libthr X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Aug 2012 22:39:35 -0000 On Thu, Aug 16, 2012 at 02:44:26PM +0300, Konstantin Belousov wrote: > My point is that the fact that fork() is called from dynamic context > that was identified as the signal handler does not mean anything. > It can be mis-identified for many reasons, which both me and you > tried to partially enumerate above. > The really important thing is the atomicity of the current context, > e.g. synchronous SIGSEGV caused by a language runtime GC is very > much safe place to call atfork handlers, since runtimes usually cause > signal generations only at the safe place. > I do not think that such approach as you described can be completed, > the _Fork() seems the only robust way. Agreed, that way (detecting signal handler) lies madness. If need be, _Fork() is easily implemented and used. > BTW, returning to Jilles proposal, can we call vfork() only for > single-threaded parent ? I think it gives good boost for single-threaded > case, and also eliminates the concerns of non-safety. This would probably fix the safety issues but at a price. There is a correlation between processes so large that they benefit greatly from vfork and threaded processes. On the other hand, I think direct calls to vfork() in applications are risky and it may not be possible to support them safely in all circumstances. However, if libc is calling vfork() such as via popen(), system() or posix_spawn(), it should be possible even in a multi-threaded process. In that case, the rtld and libthr problems can be avoided by using aliases with hidden visibility for all functions the vforked child needs to call (or any other method that prevents interposition and hard-codes a displacement into libc.so). There may still be a problem in programs that install signal handlers because the set of async-signal-safe functions is larger than what can be done in a vforked child. Userland can avoid this by masking affected signals before calling vfork() and resetting them to SIG_DFL before unmasking them. This will add many syscalls if the code does not know which signals are affected (such as libc). Alternatively, the kernel could map caught signals to the default action for processes with P_PPWAIT (just like it does not stop such processes because of signals or TTY job control). -- Jilles Tjoelker