xref: /netbsd-src/lib/libc/gen/pthread_atfork.3 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: pthread_atfork.3,v 1.3 2003/04/16 13:34:38 wiz Exp $
2.\"
3.\" Copyright (c) 2003 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Nathan J. Williams.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd February 12, 2003
38.Dt PTHREAD_ATFORK 3
39.Os
40.Sh NAME
41.Nm pthread_atfork
42.Nd register handlers to be called when process forks
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In pthread.h
47.Ft int
48.Fn pthread_atfork "void (*prepare)(void)" "void (*parent)(void)" "void (*child)(void)"
49.Sh DESCRIPTION
50The
51.Fn pthread_atfork
52function registers the provided handler functions to be called when the
53.Xr fork 2
54function is called.
55Each of the three handlers is called at a different place in the
56.Xr fork 2
57sequence.
58The
59.Ar prepare
60handler is called in the parent process before the fork happens, the
61.Ar parent
62handler is called in the parent process after the fork has happened, and the
63.Ar child
64handler is called in the child process after the fork has happened.
65The
66.Ar parent
67and
68.Ar child
69handlers are called in the order in which they were registered, while the
70.Ar prepare
71handlers are called in reverse of the order in which they were registerd.
72.Pp
73Any of the handlers given may be
74.Dv NULL .
75.Pp
76The intended use of
77.Fn pthread_atfork
78is to provide a consistent state to a child process from a multithreaded parent
79process where locks may be acquired and released asynchronously with respect to the
80.Xr fork 2
81call.
82Each subsystem with locks that are used in a child process should register
83handlers with
84.Fn pthread_atfork
85that acquires those locks in the
86.Ar prepare
87handler and releases them in the
88.Ar parent
89handler.
90.Sh RETURN VALUES
91The
92.Fn pthread_atfork
93function returns 0 on success and an error number on failure.
94.Sh ERRORS
95The following error code may be returned:
96.Bl -tag -width Er
97.It Bq Er ENOMEM
98Insufficent memory exists to register the fork handlers.
99.El
100.Sh SEE ALSO
101.Xr fork 2
102.Sh STANDARDS
103The
104.Fn pthread_atfork
105function conforms to
106.St -p1003.1c-95 .
107.Sh HISTORY
108The
109.Fn pthread_atfork
110function first appeared in
111.Nx 2.0 .
112.Sh CAVEATS
113After calling
114.Xr fork 2
115from a multithreaded process, it is only safe to call
116async-signal-safe functions until calling one of the
117.Xr exec 3
118functions.
119The
120.Fn pthread_*
121functions are not async-signal-safe, so it is not safe to use such functions
122in the
123.Ar child
124handler.
125.Sh BUGS
126There is no way to unregister a handler registered with
127.Fn pthread_atfork .
128