blob: 5bf63fb41e994378cf660aedf11f96043bbeaceb [file] [log] [blame]
Elliott Hughesc3f11402013-10-30 14:40:09 -07001/*
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
32static pthread_mutex_t gAtForkListMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
33
34struct atfork_t {
35 atfork_t* next;
36 atfork_t* prev;
37
38 void (*prepare)(void);
39 void (*child)(void);
40 void (*parent)(void);
41};
42
43struct atfork_list_t {
44 atfork_t* first;
45 atfork_t* last;
46};
47
48static atfork_list_t gAtForkList = { NULL, NULL };
49
50void __bionic_atfork_run_prepare() {
51 // We will lock this here, and unlock it in the parent and child functions.
52 // This ensures that nobody can modify the handler array between the calls
53 // to the prepare and parent/child handlers.
54 //
55 // TODO: If a handler mucks with the list, it could cause problems. Right
56 // now it's ok because all they can do is add new items to the end
57 // of the list, but if/when we implement cleanup in dlclose() things
58 // will get more interesting...
59 pthread_mutex_lock(&gAtForkListMutex);
60
61 // Call pthread_atfork() prepare handlers. POSIX states that the prepare
62 // handlers should be called in the reverse order of the parent/child
63 // handlers, so we iterate backwards.
64 for (atfork_t* it = gAtForkList.last; it != NULL; it = it->prev) {
65 if (it->prepare != NULL) {
66 it->prepare();
67 }
68 }
69}
70
71void __bionic_atfork_run_child() {
72 for (atfork_t* it = gAtForkList.first; it != NULL; it = it->next) {
73 if (it->child != NULL) {
74 it->child();
75 }
76 }
77
78 pthread_mutexattr_t attr;
79 pthread_mutexattr_init(&attr);
80 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
81 pthread_mutex_init(&gAtForkListMutex, &attr);
82}
83
84void __bionic_atfork_run_parent() {
85 for (atfork_t* it = gAtForkList.first; it != NULL; it = it->next) {
86 if (it->parent != NULL) {
87 it->parent();
88 }
89 }
90
91 pthread_mutex_unlock(&gAtForkListMutex);
92}
93
94int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void)) {
95 atfork_t* entry = reinterpret_cast<atfork_t*>(malloc(sizeof(atfork_t)));
96 if (entry == NULL) {
97 return ENOMEM;
98 }
99
100 entry->prepare = prepare;
101 entry->parent = parent;
102 entry->child = child;
103
104 pthread_mutex_lock(&gAtForkListMutex);
105
106 // Append 'entry' to the list.
107 entry->next = NULL;
108 entry->prev = gAtForkList.last;
109 if (entry->prev != NULL) {
110 entry->prev->next = entry;
111 }
112 if (gAtForkList.first == NULL) {
113 gAtForkList.first = entry;
114 }
115 gAtForkList.last = entry;
116
117 pthread_mutex_unlock(&gAtForkListMutex);
118
119 return 0;
120}