| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 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> | 
| Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 31 | #include <stdlib.h> | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 32 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 33 | struct 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 |  | 
|  | 42 | struct atfork_list_t { | 
|  | 43 | atfork_t* first; | 
|  | 44 | atfork_t* last; | 
|  | 45 | }; | 
|  | 46 |  | 
| Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 47 | static pthread_mutex_t g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 48 | static atfork_list_t g_atfork_list = { NULL, NULL }; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 49 |  | 
|  | 50 | void __bionic_atfork_run_prepare() { | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 51 | // We lock the atfork list here, unlock it in the parent, and reset it in the child. | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 52 | // This ensures that nobody can modify the handler array between the calls | 
|  | 53 | // to the prepare and parent/child handlers. | 
|  | 54 | // | 
| Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 55 | // 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 Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 57 | pthread_mutex_lock(&g_atfork_list_mutex); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 58 |  | 
|  | 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 Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 62 | for (atfork_t* it = g_atfork_list.last; it != NULL; it = it->prev) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 63 | if (it->prepare != NULL) { | 
|  | 64 | it->prepare(); | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | void __bionic_atfork_run_child() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 70 | for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 71 | if (it->child != NULL) { | 
|  | 72 | it->child(); | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
| Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 76 | g_atfork_list_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | void __bionic_atfork_run_parent() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 80 | for (atfork_t* it = g_atfork_list.first; it != NULL; it = it->next) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 81 | if (it->parent != NULL) { | 
|  | 82 | it->parent(); | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 86 | pthread_mutex_unlock(&g_atfork_list_mutex); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | int 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 Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 99 | pthread_mutex_lock(&g_atfork_list_mutex); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | // Append 'entry' to the list. | 
|  | 102 | entry->next = NULL; | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 103 | entry->prev = g_atfork_list.last; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 104 | if (entry->prev != NULL) { | 
|  | 105 | entry->prev->next = entry; | 
|  | 106 | } | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 107 | if (g_atfork_list.first == NULL) { | 
|  | 108 | g_atfork_list.first = entry; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 109 | } | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 110 | g_atfork_list.last = entry; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 111 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 112 | pthread_mutex_unlock(&g_atfork_list_mutex); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | return 0; | 
|  | 115 | } |