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