blob: d1c4ad0c4ef32f9bfdb4c0de8f35aefaee6edebd [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>
Dan Albert75ef63d2014-11-21 00:18:07 -080031#include <stdlib.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070032
Elliott Hughesc3f11402013-10-30 14:40:09 -070033struct atfork_t {
34 atfork_t* next;
35 atfork_t* prev;
36
37 void (*prepare)(void);
38 void (*child)(void);
39 void (*parent)(void);
40};
41
42struct atfork_list_t {
43 atfork_t* first;
44 atfork_t* last;
45};
46
Elliott Hughes212e0e32014-12-01 16:43:51 -080047static pthread_mutex_t g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughes1728b232014-05-14 10:02:03 -070048static atfork_list_t g_atfork_list = { NULL, NULL };
Elliott Hughesc3f11402013-10-30 14:40:09 -070049
50void __bionic_atfork_run_prepare() {
Elliott Hughes4b558f52014-03-04 15:58:02 -080051 // We lock the atfork list here, unlock it in the parent, and reset it in the child.
Elliott Hughesc3f11402013-10-30 14:40:09 -070052 // This ensures that nobody can modify the handler array between the calls
53 // to the prepare and parent/child handlers.
54 //
Elliott Hughes4b558f52014-03-04 15:58:02 -080055 // TODO: If a handler tries to mutate the list, they'll block. We should probably copy
56 // the list before forking, and have prepare, parent, and child all work on the consistent copy.
Elliott Hughes1728b232014-05-14 10:02:03 -070057 pthread_mutex_lock(&g_atfork_list_mutex);
Elliott Hughesc3f11402013-10-30 14:40:09 -070058
59 // Call pthread_atfork() prepare handlers. POSIX states that the prepare
60 // handlers should be called in the reverse order of the parent/child
61 // handlers, so we iterate backwards.
Elliott Hughes1728b232014-05-14 10:02:03 -070062 for (atfork_t* it = g_atfork_list.last; it != NULL; it = it->prev) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070063 if (it->prepare != NULL) {
64 it->prepare();
65 }
66 }
67}
68
69void __bionic_atfork_run_child() {
Elliott Hughes1728b232014-05-14 10:02:03 -070070 for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070071 if (it->child != NULL) {
72 it->child();
73 }
74 }
75
Elliott Hughes212e0e32014-12-01 16:43:51 -080076 g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesc3f11402013-10-30 14:40:09 -070077}
78
79void __bionic_atfork_run_parent() {
Elliott Hughes1728b232014-05-14 10:02:03 -070080 for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070081 if (it->parent != NULL) {
82 it->parent();
83 }
84 }
85
Elliott Hughes1728b232014-05-14 10:02:03 -070086 pthread_mutex_unlock(&g_atfork_list_mutex);
Elliott Hughesc3f11402013-10-30 14:40:09 -070087}
88
89int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void)) {
90 atfork_t* entry = reinterpret_cast<atfork_t*>(malloc(sizeof(atfork_t)));
91 if (entry == NULL) {
92 return ENOMEM;
93 }
94
95 entry->prepare = prepare;
96 entry->parent = parent;
97 entry->child = child;
98
Elliott Hughes1728b232014-05-14 10:02:03 -070099 pthread_mutex_lock(&g_atfork_list_mutex);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700100
101 // Append 'entry' to the list.
102 entry->next = NULL;
Elliott Hughes1728b232014-05-14 10:02:03 -0700103 entry->prev = g_atfork_list.last;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700104 if (entry->prev != NULL) {
105 entry->prev->next = entry;
106 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700107 if (g_atfork_list.first == NULL) {
108 g_atfork_list.first = entry;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700109 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700110 g_atfork_list.last = entry;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700111
Elliott Hughes1728b232014-05-14 10:02:03 -0700112 pthread_mutex_unlock(&g_atfork_list_mutex);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700113
114 return 0;
115}