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