1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "thr_uberdata.h"
#include "mtlib.h"
/*
* fork handlers are run in LIFO order.
* The libc fork handler is expected to be the first handler installed,
* hence would be the last fork handler run in preparation for fork1().
* It is essential that this be so, for other libraries depend on libc
* and may grab their own locks before calling into libc. By special
* arrangement, the loader runs libc's init section (libc_init()) first.
*/
/*
* pthread_atfork(): installs handlers to be called during fork1().
* There is no POSIX API that provides for deletion of atfork handlers.
* Collaboration between the loader and libc ensures that atfork
* handlers installed by a library are deleted when that library
* is unloaded (see _preexec_atfork_unload() in atexit.c).
*/
int
pthread_atfork(void (*prepare)(void),
void (*parent)(void), void (*child)(void))
{
ulwp_t *self = curthread;
uberdata_t *udp = self->ul_uberdata;
atfork_t *atfp;
atfork_t *head;
int error = 0;
(void) mutex_lock(&udp->atfork_lock);
if (self->ul_fork) {
/*
* Cannot call pthread_atfork() from a fork handler.
*/
error = EDEADLK;
} else if ((atfp = lmalloc(sizeof (atfork_t))) == NULL) {
error = ENOMEM;
} else {
atfp->prepare = prepare;
atfp->parent = parent;
atfp->child = child;
if ((head = udp->atforklist) == NULL) {
udp->atforklist = atfp;
atfp->forw = atfp->back = atfp;
} else {
head->back->forw = atfp;
atfp->forw = head;
atfp->back = head->back;
head->back = atfp;
}
}
(void) mutex_unlock(&udp->atfork_lock);
return (error);
}
/*
* _prefork_handler() is called by fork1() before it starts processing.
* It executes the user installed "prepare" routines in LIFO order (POSIX)
*/
void
_prefork_handler(void)
{
uberdata_t *udp = curthread->ul_uberdata;
atfork_t *atfork_q;
atfork_t *atfp;
ASSERT(MUTEX_OWNED(&udp->atfork_lock, curthread));
if ((atfork_q = udp->atforklist) != NULL) {
atfp = atfork_q = atfork_q->back;
do {
if (atfp->prepare)
(*atfp->prepare)();
} while ((atfp = atfp->back) != atfork_q);
}
}
/*
* _postfork_parent_handler() is called by fork1() after it retuns as parent.
* It executes the user installed "parent" routines in FIFO order (POSIX).
*/
void
_postfork_parent_handler(void)
{
uberdata_t *udp = curthread->ul_uberdata;
atfork_t *atfork_q;
atfork_t *atfp;
ASSERT(MUTEX_OWNED(&udp->atfork_lock, curthread));
if ((atfork_q = udp->atforklist) != NULL) {
atfp = atfork_q;
do {
if (atfp->parent)
(*atfp->parent)();
} while ((atfp = atfp->forw) != atfork_q);
}
}
/*
* _postfork_child_handler() is called by fork1() after it returns as child.
* It executes the user installed "child" routines in FIFO order (POSIX).
*/
void
_postfork_child_handler(void)
{
uberdata_t *udp = curthread->ul_uberdata;
atfork_t *atfork_q;
atfork_t *atfp;
ASSERT(MUTEX_OWNED(&udp->atfork_lock, curthread));
if ((atfork_q = udp->atforklist) != NULL) {
atfp = atfork_q;
do {
if (atfp->child)
(*atfp->child)();
} while ((atfp = atfp->forw) != atfork_q);
}
}
|