From owner-svn-src-head@freebsd.org Sun May 22 13:32:46 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id CDD8EB45DD5; Sun, 22 May 2016 13:32:46 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 85ED61F3B; Sun, 22 May 2016 13:32:46 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4MDWjUI091278; Sun, 22 May 2016 13:32:45 GMT (envelope-from jilles@FreeBSD.org) Received: (from jilles@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4MDWjGn091277; Sun, 22 May 2016 13:32:45 GMT (envelope-from jilles@FreeBSD.org) Message-Id: <201605221332.u4MDWjGn091277@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jilles set sender to jilles@FreeBSD.org using -f From: Jilles Tjoelker Date: Sun, 22 May 2016 13:32:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r300420 - head/lib/libc/sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 May 2016 13:32:46 -0000 Author: jilles Date: Sun May 22 13:32:45 2016 New Revision: 300420 URL: https://svnweb.freebsd.org/changeset/base/300420 Log: vfork(2): Mention some risks of calling vfork() from application code. MFC after: 1 week Modified: head/lib/libc/sys/vfork.2 Modified: head/lib/libc/sys/vfork.2 ============================================================================== --- head/lib/libc/sys/vfork.2 Sun May 22 13:10:48 2016 (r300419) +++ head/lib/libc/sys/vfork.2 Sun May 22 13:32:45 2016 (r300420) @@ -28,7 +28,7 @@ .\" @(#)vfork.2 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd April 6, 2015 +.Dd May 22, 2016 .Dt VFORK 2 .Os .Sh NAME @@ -41,11 +41,20 @@ .Ft pid_t .Fn vfork void .Sh DESCRIPTION +.Bf -symbolic +Since this function is hard to use correctly from application software, +it is recommended to use +.Xr posix_spawn 3 +or +.Xr fork 2 +instead. +.Ef +.Pp The .Fn vfork system call can be used to create new processes without fully copying the address -space of the old process, which is horrendously inefficient in a paged +space of the old process, which is inefficient in a paged environment. It is useful when the purpose of .Xr fork 2 @@ -56,13 +65,15 @@ The system call differs from .Xr fork 2 -in that the child borrows the parent's memory and thread of -control until a call to +in that the child borrows the parent process's address space and the +calling thread's stack +until a call to .Xr execve 2 or an exit (either by a call to .Xr _exit 2 or abnormally). -The parent process is suspended while the child is using its resources. +The calling thread is suspended while the child is using its resources. +Other threads continue to run. .Pp The .Fn vfork @@ -70,17 +81,21 @@ system call returns 0 in the child's context and (later) the pid of the child in the parent's context. .Pp -The -.Fn vfork -system call -can normally be used just like -.Xr fork 2 . -It does not work, however, to return while running in the child's context +Many problems can occur when replacing +.Xr fork 2 +with +.Fn vfork . +For example, it does not work to return while running in the child's context from the procedure that called .Fn vfork since the eventual return from .Fn vfork would then return to a no longer existent stack frame. +Also, changing process state which is partially implemented in user space +such as signal handlers with +.Xr libthr 3 +will corrupt the parent's state. +.Pp Be careful, also, to call .Xr _exit 2 rather than @@ -106,7 +121,8 @@ Same as for .Xr rfork 2 , .Xr sigaction 2 , .Xr wait 2 , -.Xr exit 3 +.Xr exit 3 , +.Xr posix_spawn 3 .Sh HISTORY The .Fn vfork