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> |
| 31 | |
| 32 | static pthread_mutex_t gAtForkListMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; |
| 33 | |
| 34 | struct 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 | |
| 43 | struct atfork_list_t { |
| 44 | atfork_t* first; |
| 45 | atfork_t* last; |
| 46 | }; |
| 47 | |
| 48 | static atfork_list_t gAtForkList = { NULL, NULL }; |
| 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 | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 57 | pthread_mutex_lock(&gAtForkListMutex); |
| 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. |
| 62 | for (atfork_t* it = gAtForkList.last; it != NULL; it = it->prev) { |
| 63 | if (it->prepare != NULL) { |
| 64 | it->prepare(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void __bionic_atfork_run_child() { |
| 70 | for (atfork_t* it = gAtForkList.first; it != NULL; it = it->next) { |
| 71 | if (it->child != NULL) { |
| 72 | it->child(); |
| 73 | } |
| 74 | } |
| 75 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 76 | gAtForkListMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void __bionic_atfork_run_parent() { |
| 80 | for (atfork_t* it = gAtForkList.first; it != NULL; it = it->next) { |
| 81 | if (it->parent != NULL) { |
| 82 | it->parent(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pthread_mutex_unlock(&gAtForkListMutex); |
| 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 | |
| 99 | pthread_mutex_lock(&gAtForkListMutex); |
| 100 | |
| 101 | // Append 'entry' to the list. |
| 102 | entry->next = NULL; |
| 103 | entry->prev = gAtForkList.last; |
| 104 | if (entry->prev != NULL) { |
| 105 | entry->prev->next = entry; |
| 106 | } |
| 107 | if (gAtForkList.first == NULL) { |
| 108 | gAtForkList.first = entry; |
| 109 | } |
| 110 | gAtForkList.last = entry; |
| 111 | |
| 112 | pthread_mutex_unlock(&gAtForkListMutex); |
| 113 | |
| 114 | return 0; |
| 115 | } |