1*0a6a1f1dSLionel Sambuc.\" $NetBSD: pthread_atfork.3,v 1.7 2014/07/19 14:58:50 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 2003 The NetBSD Foundation, Inc. 42fe8fb19SBen Gras.\" All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation 72fe8fb19SBen Gras.\" by Nathan J. Williams. 82fe8fb19SBen Gras.\" 92fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 102fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 112fe8fb19SBen Gras.\" are met: 122fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 132fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 142fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 152fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 162fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 192fe8fb19SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 202fe8fb19SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 212fe8fb19SBen Gras.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 222fe8fb19SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 232fe8fb19SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 242fe8fb19SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 252fe8fb19SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 262fe8fb19SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 272fe8fb19SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 282fe8fb19SBen Gras.\" POSSIBILITY OF SUCH DAMAGE. 292fe8fb19SBen Gras.\" 30*0a6a1f1dSLionel Sambuc.Dd July 18, 2014 312fe8fb19SBen Gras.Dt PTHREAD_ATFORK 3 322fe8fb19SBen Gras.Os 332fe8fb19SBen Gras.Sh NAME 342fe8fb19SBen Gras.Nm pthread_atfork 352fe8fb19SBen Gras.Nd register handlers to be called when process forks 362fe8fb19SBen Gras.Sh LIBRARY 372fe8fb19SBen Gras.Lb libc 382fe8fb19SBen Gras.Sh SYNOPSIS 392fe8fb19SBen Gras.In pthread.h 402fe8fb19SBen Gras.Ft int 412fe8fb19SBen Gras.Fn pthread_atfork "void (*prepare)(void)" "void (*parent)(void)" "void (*child)(void)" 422fe8fb19SBen Gras.Sh DESCRIPTION 432fe8fb19SBen GrasThe 442fe8fb19SBen Gras.Fn pthread_atfork 452fe8fb19SBen Grasfunction registers the provided handler functions to be called when the 462fe8fb19SBen Gras.Xr fork 2 472fe8fb19SBen Grasfunction is called. 482fe8fb19SBen GrasEach of the three handlers is called at a different place in the 492fe8fb19SBen Gras.Xr fork 2 502fe8fb19SBen Grassequence. 512fe8fb19SBen GrasThe 522fe8fb19SBen Gras.Ar prepare 532fe8fb19SBen Grashandler is called in the parent process before the fork happens, the 542fe8fb19SBen Gras.Ar parent 552fe8fb19SBen Grashandler is called in the parent process after the fork has happened, and the 562fe8fb19SBen Gras.Ar child 572fe8fb19SBen Grashandler is called in the child process after the fork has happened. 582fe8fb19SBen GrasThe 592fe8fb19SBen Gras.Ar parent 602fe8fb19SBen Grasand 612fe8fb19SBen Gras.Ar child 622fe8fb19SBen Grashandlers are called in the order in which they were registered, while the 632fe8fb19SBen Gras.Ar prepare 642fe8fb19SBen Grashandlers are called in reverse of the order in which they were registered. 652fe8fb19SBen Gras.Pp 662fe8fb19SBen GrasAny of the handlers given may be 672fe8fb19SBen Gras.Dv NULL . 682fe8fb19SBen Gras.Pp 692fe8fb19SBen GrasThe intended use of 702fe8fb19SBen Gras.Fn pthread_atfork 712fe8fb19SBen Grasis to provide a consistent state to a child process from a multithreaded parent 722fe8fb19SBen Grasprocess where locks may be acquired and released asynchronously with respect to the 732fe8fb19SBen Gras.Xr fork 2 742fe8fb19SBen Grascall. 752fe8fb19SBen GrasEach subsystem with locks that are used in a child process should register 762fe8fb19SBen Grashandlers with 772fe8fb19SBen Gras.Fn pthread_atfork 782fe8fb19SBen Grasthat acquires those locks in the 792fe8fb19SBen Gras.Ar prepare 802fe8fb19SBen Grashandler and releases them in the 812fe8fb19SBen Gras.Ar parent 822fe8fb19SBen Grashandler. 832fe8fb19SBen Gras.Sh RETURN VALUES 842fe8fb19SBen GrasThe 852fe8fb19SBen Gras.Fn pthread_atfork 862fe8fb19SBen Grasfunction returns 0 on success and an error number on failure. 872fe8fb19SBen Gras.Sh ERRORS 882fe8fb19SBen GrasThe following error code may be returned: 892fe8fb19SBen Gras.Bl -tag -width Er 902fe8fb19SBen Gras.It Bq Er ENOMEM 912fe8fb19SBen GrasInsufficient memory exists to register the fork handlers. 922fe8fb19SBen Gras.El 932fe8fb19SBen Gras.Sh SEE ALSO 94*0a6a1f1dSLionel Sambuc.Xr fork 2 , 95*0a6a1f1dSLionel Sambuc.Xr pthread_mutex 3 , 96*0a6a1f1dSLionel Sambuc.Xr signal 7 972fe8fb19SBen Gras.Sh STANDARDS 982fe8fb19SBen GrasThe 992fe8fb19SBen Gras.Fn pthread_atfork 1002fe8fb19SBen Grasfunction conforms to 1012fe8fb19SBen Gras.St -p1003.1c-95 . 1022fe8fb19SBen Gras.Sh HISTORY 1032fe8fb19SBen GrasThe 1042fe8fb19SBen Gras.Fn pthread_atfork 1052fe8fb19SBen Grasfunction first appeared in 1062fe8fb19SBen Gras.Nx 2.0 . 1072fe8fb19SBen Gras.Sh CAVEATS 1082fe8fb19SBen GrasAfter calling 1092fe8fb19SBen Gras.Xr fork 2 1102fe8fb19SBen Grasfrom a multithreaded process, it is only safe to call 1112fe8fb19SBen Grasasync-signal-safe functions until calling one of the 1122fe8fb19SBen Gras.Xr exec 3 1132fe8fb19SBen Grasfunctions. 1142fe8fb19SBen GrasThe 1152fe8fb19SBen Gras.Fn pthread_* 1162fe8fb19SBen Grasfunctions are not async-signal-safe, so it is not safe to use such functions 1172fe8fb19SBen Grasin the 1182fe8fb19SBen Gras.Ar child 1192fe8fb19SBen Grashandler. 120*0a6a1f1dSLionel SambucPOSIX does not mandate that 121*0a6a1f1dSLionel Sambuc.Fn pthread_mutex_unlock 122*0a6a1f1dSLionel Sambucbe async-signal-safe, but it is in 123*0a6a1f1dSLionel Sambuc.Nx 124*0a6a1f1dSLionel Sambucand thus safe to use within the 125*0a6a1f1dSLionel Sambuc.Ar child 126*0a6a1f1dSLionel Sambuchandler. 1272fe8fb19SBen Gras.Sh BUGS 1282fe8fb19SBen GrasThere is no way to unregister a handler registered with 1292fe8fb19SBen Gras.Fn pthread_atfork . 130