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
22 static int g_atfork_prepare_calls = 0;
AtForkPrepare1()23 static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
AtForkPrepare2()24 static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
AtForkPrepare3()25 static void AtForkPrepare3() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 3; }
AtForkPrepare4()26 static void AtForkPrepare4() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 4; }
27
28 static int g_atfork_parent_calls = 0;
AtForkParent1()29 static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
AtForkParent2()30 static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
AtForkParent3()31 static void AtForkParent3() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 3; }
AtForkParent4()32 static void AtForkParent4() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 4; }
33
34 static int g_atfork_child_calls = 0;
AtForkChild1()35 static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
AtForkChild2()36 static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
AtForkChild3()37 static void AtForkChild3() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 3; }
AtForkChild4()38 static void AtForkChild4() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 4; }
39
TEST(pthread,pthread_atfork_with_dlclose)40 TEST(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