xref: /illumos-gate/usr/src/lib/libc/port/gen/atfork.c (revision fea9cb91bd8e12d84069b4dab1268363668b4bff)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
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 #pragma weak _private_pthread_atfork = _pthread_atfork
49 #pragma weak pthread_atfork = _pthread_atfork
50 int
51 _pthread_atfork(void (*prepare)(void),
52 	void (*parent)(void), void (*child)(void))
53 {
54 	ulwp_t *self = curthread;
55 	uberdata_t *udp = self->ul_uberdata;
56 	atfork_t *atfp;
57 	atfork_t *head;
58 	int error;
59 
60 	if ((error = fork_lock_enter("pthread_atfork")) != 0) {
61 		/*
62 		 * Cannot call pthread_atfork() from a fork handler.
63 		 */
64 		fork_lock_exit();
65 		return (error);
66 	}
67 	if ((atfp = lmalloc(sizeof (atfork_t))) == NULL) {
68 		fork_lock_exit();
69 		return (ENOMEM);
70 	}
71 	atfp->prepare = prepare;
72 	atfp->parent = parent;
73 	atfp->child = child;
74 	if ((head = udp->atforklist) == NULL) {
75 		udp->atforklist = atfp;
76 		atfp->forw = atfp->back = atfp;
77 	} else {
78 		head->back->forw = atfp;
79 		atfp->forw = head;
80 		atfp->back = head->back;
81 		head->back = atfp;
82 	}
83 	fork_lock_exit();
84 	return (0);
85 }
86 
87 /*
88  * _prefork_handler() is called by fork1() before it starts processing.
89  * It executes the user installed "prepare" routines in LIFO order (POSIX)
90  */
91 void
92 _prefork_handler(void)
93 {
94 	uberdata_t *udp = curthread->ul_uberdata;
95 	atfork_t *atfork_q;
96 	atfork_t *atfp;
97 
98 	if ((atfork_q = udp->atforklist) != NULL) {
99 		atfp = atfork_q = atfork_q->back;
100 		do {
101 			if (atfp->prepare)
102 				(*atfp->prepare)();
103 		} while ((atfp = atfp->back) != atfork_q);
104 	}
105 }
106 
107 /*
108  * _postfork_parent_handler() is called by fork1() after it retuns as parent.
109  * It executes the user installed "parent" routines in FIFO order (POSIX).
110  */
111 void
112 _postfork_parent_handler(void)
113 {
114 	uberdata_t *udp = curthread->ul_uberdata;
115 	atfork_t *atfork_q;
116 	atfork_t *atfp;
117 
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 
138 	if ((atfork_q = udp->atforklist) != NULL) {
139 		atfp = atfork_q;
140 		do {
141 			if (atfp->child)
142 				(*atfp->child)();
143 		} while ((atfp = atfp->forw) != atfork_q);
144 	}
145 }
146