1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <pthread.h>
31 
32 struct atfork_t {
33   atfork_t* next;
34   atfork_t* prev;
35 
36   void (*prepare)(void);
37   void (*child)(void);
38   void (*parent)(void);
39 };
40 
41 struct atfork_list_t {
42   atfork_t* first;
43   atfork_t* last;
44 };
45 
46 static pthread_mutex_t g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
47 static atfork_list_t g_atfork_list = { NULL, NULL };
48 
__bionic_atfork_run_prepare()49 void __bionic_atfork_run_prepare() {
50   // We lock the atfork list here, unlock it in the parent, and reset it in the child.
51   // This ensures that nobody can modify the handler array between the calls
52   // to the prepare and parent/child handlers.
53   //
54   // TODO: If a handler tries to mutate the list, they'll block. We should probably copy
55   // the list before forking, and have prepare, parent, and child all work on the consistent copy.
56   pthread_mutex_lock(&g_atfork_list_mutex);
57 
58   // Call pthread_atfork() prepare handlers. POSIX states that the prepare
59   // handlers should be called in the reverse order of the parent/child
60   // handlers, so we iterate backwards.
61   for (atfork_t* it = g_atfork_list.last; it != NULL; it = it->prev) {
62     if (it->prepare != NULL) {
63       it->prepare();
64     }
65   }
66 }
67 
__bionic_atfork_run_child()68 void __bionic_atfork_run_child() {
69   for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
70     if (it->child != NULL) {
71       it->child();
72     }
73   }
74 
75   g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
76 }
77 
__bionic_atfork_run_parent()78 void __bionic_atfork_run_parent() {
79   for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
80     if (it->parent != NULL) {
81       it->parent();
82     }
83   }
84 
85   pthread_mutex_unlock(&g_atfork_list_mutex);
86 }
87 
pthread_atfork(void (* prepare)(void),void (* parent)(void),void (* child)(void))88 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void)) {
89   atfork_t* entry = reinterpret_cast<atfork_t*>(malloc(sizeof(atfork_t)));
90   if (entry == NULL) {
91     return ENOMEM;
92   }
93 
94   entry->prepare = prepare;
95   entry->parent = parent;
96   entry->child = child;
97 
98   pthread_mutex_lock(&g_atfork_list_mutex);
99 
100   // Append 'entry' to the list.
101   entry->next = NULL;
102   entry->prev = g_atfork_list.last;
103   if (entry->prev != NULL) {
104     entry->prev->next = entry;
105   }
106   if (g_atfork_list.first == NULL) {
107     g_atfork_list.first = entry;
108   }
109   g_atfork_list.last = entry;
110 
111   pthread_mutex_unlock(&g_atfork_list_mutex);
112 
113   return 0;
114 }
115