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 20#include "utils.h" 21 22static int g_atfork_prepare_calls = 0; 23static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } 24static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; } 25static void AtForkPrepare3() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 3; } 26static void AtForkPrepare4() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 4; } 27 28static int g_atfork_parent_calls = 0; 29static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } 30static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; } 31static void AtForkParent3() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 3; } 32static void AtForkParent4() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 4; } 33 34static int g_atfork_child_calls = 0; 35static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } 36static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; } 37static void AtForkChild3() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 3; } 38static void AtForkChild4() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 4; } 39 40TEST(pthread, pthread_atfork_with_dlclose) { 41 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); 42 43 void* handle = dlopen("libtest_pthread_atfork.so", RTLD_NOW | RTLD_LOCAL); 44 ASSERT_TRUE(handle != nullptr) << dlerror(); 45 typedef int (*fn_t)(void (*)(void), void (*)(void), void (*)(void)); 46 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "proxy_pthread_atfork")); 47 ASSERT_TRUE(fn != nullptr) << dlerror(); 48 // the library registers 2 additional atfork handlers in a constructor 49 ASSERT_EQ(0, fn(AtForkPrepare2, AtForkParent2, AtForkChild2)); 50 ASSERT_EQ(0, fn(AtForkPrepare3, AtForkParent3, AtForkChild3)); 51 52 ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4)); 53 54 pid_t pid = fork(); 55 56 ASSERT_NE(-1, pid) << strerror(errno); 57 58 if (pid == 0) { 59 ASSERT_EQ(1234, g_atfork_child_calls); 60 _exit(0); 61 } 62 63 ASSERT_EQ(1234, g_atfork_parent_calls); 64 ASSERT_EQ(4321, g_atfork_prepare_calls); 65 66 EXPECT_EQ(0, dlclose(handle)); 67 g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; 68 69 AssertChildExited(pid, 0); 70 71 pid = fork(); 72 73 ASSERT_NE(-1, pid) << strerror(errno); 74 75 if (pid == 0) { 76 ASSERT_EQ(14, g_atfork_child_calls); 77 _exit(0); 78 } 79 80 ASSERT_EQ(14, g_atfork_parent_calls); 81 ASSERT_EQ(41, g_atfork_prepare_calls); 82 83 AssertChildExited(pid, 0); 84} 85