| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #include <gtest/gtest.h> | 
|  | 17 |  | 
|  | 18 | #include <dlfcn.h> | 
|  | 19 |  | 
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 20 | #include "utils.h" | 
|  | 21 |  | 
| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 22 | static int g_atfork_prepare_calls = 0; | 
|  | 23 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } | 
|  | 24 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; } | 
|  | 25 | static void AtForkPrepare3() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 3; } | 
|  | 26 | static void AtForkPrepare4() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 4; } | 
|  | 27 |  | 
|  | 28 | static int g_atfork_parent_calls = 0; | 
|  | 29 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } | 
|  | 30 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; } | 
|  | 31 | static void AtForkParent3() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 3; } | 
|  | 32 | static void AtForkParent4() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 4; } | 
|  | 33 |  | 
|  | 34 | static int g_atfork_child_calls = 0; | 
|  | 35 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } | 
|  | 36 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; } | 
|  | 37 | static void AtForkChild3() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 3; } | 
|  | 38 | static void AtForkChild4() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 4; } | 
|  | 39 |  | 
| Mingwei Shi | f6a21bf | 2016-11-03 14:53:45 +0000 | [diff] [blame] | 40 | static void* g_atfork_test_handle = nullptr; | 
|  | 41 | static void AtForkPrepare() {} | 
|  | 42 | static void AtForkParent() {} | 
|  | 43 | static void AtForkChild() { dlclose(g_atfork_test_handle); g_atfork_test_handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); } | 
|  | 44 |  | 
| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 45 | TEST(pthread, pthread_atfork_with_dlclose) { | 
|  | 46 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); | 
|  | 47 |  | 
|  | 48 | void* handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 49 | ASSERT_TRUE(handle != nullptr) << dlerror(); | 
|  | 50 | typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void)); | 
|  | 51 | fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "proxy_pthread_atfork")); | 
|  | 52 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 53 | // the library registers 2 additional atfork handlers in a constructor | 
|  | 54 | ASSERT_EQ(0, fn(AtForkPrepare2, AtForkParent2, AtForkChild2)); | 
|  | 55 | ASSERT_EQ(0, fn(AtForkPrepare3, AtForkParent3, AtForkChild3)); | 
|  | 56 |  | 
|  | 57 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4)); | 
|  | 58 |  | 
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 59 | pid_t pid = fork(); | 
| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | ASSERT_NE(-1, pid) << strerror(errno); | 
|  | 62 |  | 
|  | 63 | if (pid == 0) { | 
|  | 64 | ASSERT_EQ(1234, g_atfork_child_calls); | 
|  | 65 | _exit(0); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | ASSERT_EQ(1234, g_atfork_parent_calls); | 
|  | 69 | ASSERT_EQ(4321, g_atfork_prepare_calls); | 
|  | 70 |  | 
|  | 71 | EXPECT_EQ(0, dlclose(handle)); | 
|  | 72 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; | 
|  | 73 |  | 
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 74 | AssertChildExited(pid, 0); | 
| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | pid = fork(); | 
|  | 77 |  | 
|  | 78 | ASSERT_NE(-1, pid) << strerror(errno); | 
|  | 79 |  | 
|  | 80 | if (pid == 0) { | 
|  | 81 | ASSERT_EQ(14, g_atfork_child_calls); | 
|  | 82 | _exit(0); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | ASSERT_EQ(14, g_atfork_parent_calls); | 
|  | 86 | ASSERT_EQ(41, g_atfork_prepare_calls); | 
|  | 87 |  | 
| Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 88 | AssertChildExited(pid, 0); | 
| Dmitriy Ivanov | a60fd09 | 2015-05-05 16:29:28 -0700 | [diff] [blame] | 89 | } | 
| Mingwei Shi | f6a21bf | 2016-11-03 14:53:45 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | TEST(pthread, pthread_atfork_child_with_dlclose) { | 
|  | 92 |  | 
|  | 93 | g_atfork_test_handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); | 
|  | 94 | ASSERT_TRUE(g_atfork_test_handle != nullptr) << dlerror(); | 
|  | 95 | typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void)); | 
|  | 96 | fn_t fn = reinterpret_cast<fn_t>(dlsym(g_atfork_test_handle, "proxy_pthread_atfork")); | 
|  | 97 | ASSERT_TRUE(fn != nullptr) << dlerror(); | 
|  | 98 | // the library registers 2 additional atfork handlers in a constructor | 
|  | 99 |  | 
|  | 100 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare, AtForkParent, AtForkChild)); | 
|  | 101 |  | 
|  | 102 | pid_t pid = fork(); | 
|  | 103 |  | 
|  | 104 | ASSERT_NE(-1, pid) << strerror(errno); | 
|  | 105 |  | 
|  | 106 | if (pid == 0) { | 
|  | 107 | _exit(0); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | AssertChildExited(pid, 0); | 
|  | 111 |  | 
|  | 112 | EXPECT_EQ(0, dlclose(g_atfork_test_handle)); | 
|  | 113 | g_atfork_test_handle = nullptr; | 
|  | 114 | } |