1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "it_pthread_test.h"
32 #include <time.h>
33
34 extern int nanosleep(const struct timespec *req, struct timespec *rem);
35
36 static pthread_mutex_t g_mux = PTHREAD_MUTEX_INITIALIZER;
37 static volatile int g_testAtforkCount = 0;
38 static int g_testAtforkPrepare = 0;
39 static int g_testAtforkParent = 0;
40
Doit(void * arg)41 static void *Doit(void *arg)
42 {
43 int err;
44 struct timespec ts = { 2, 0 }; // 2, set time 2s.
45
46 ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT);
47
48 err = pthread_mutex_lock(&g_mux);
49 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
50
51 (void)nanosleep(&ts, NULL);
52 err = pthread_mutex_unlock(&g_mux);
53 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
54
55 EXIT:
56 return NULL;
57 }
58
Doit1(void * arg)59 static void *Doit1(void *arg)
60 {
61 int err;
62 struct timespec ts = { 2, 0 }; // 2, set time 2s.
63
64 ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT);
65
66 err = pthread_mutex_lock(&g_mux);
67 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
68
69 (void)nanosleep(&ts, NULL);
70 err = pthread_mutex_unlock(&g_mux);
71 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
72
73 EXIT:
74 return NULL;
75 }
76
Prepare(void)77 static void Prepare(void)
78 {
79 int err;
80
81 err = pthread_mutex_unlock(&g_mux);
82 ICUNIT_ASSERT_EQUAL_VOID(err, 0, err);
83 g_testAtforkPrepare++;
84 }
85
Parent(void)86 static void Parent(void)
87 {
88 int err = pthread_mutex_lock(&g_mux);
89 ICUNIT_ASSERT_EQUAL_VOID(err, 0, err);
90 g_testAtforkParent++;
91 }
92
PthreadAtforkTest(void * arg)93 static void *PthreadAtforkTest(void *arg)
94 {
95 int err, ret;
96 int pid;
97 int status = 0;
98 struct timespec ts = { 1, 0 };
99 pthread_t tid;
100
101 g_testAtforkCount = 0;
102 g_testAtforkPrepare = 0;
103 g_testAtforkParent = 0;
104
105 err = pthread_atfork(Prepare, Parent, NULL);
106 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
107 g_testAtforkCount++;
108 err = pthread_create(&tid, NULL, Doit, NULL);
109 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
110
111 nanosleep(&ts, NULL);
112 pid = fork();
113 ICUNIT_GOTO_EQUAL(g_testAtforkPrepare, 1, g_testAtforkPrepare, EXIT);
114 if (pid == 0) {
115 Doit1(NULL);
116 exit(10); // 10, set exit status.
117 }
118
119 ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT_WAIT); // 100000, The pid will never exceed 100000.
120 ICUNIT_GOTO_EQUAL(g_testAtforkParent, 1, g_testAtforkParent, EXIT_WAIT);
121
122 err = pthread_join(tid, NULL);
123 ICUNIT_GOTO_EQUAL(err, 0, err, EXIT_WAIT);
124
125
126 err = waitpid(pid, &status, 0);
127 status = WEXITSTATUS(status);
128 ICUNIT_GOTO_EQUAL(err, pid, err, EXIT);
129 ICUNIT_GOTO_EQUAL(status, 10, status, EXIT); // 10, get exit status.
130
131 EXIT:
132 return NULL;
133
134 EXIT_WAIT:
135 (void)waitpid(pid, 0, 0);
136 return NULL;
137 }
138
Testcase(void)139 static int Testcase(void)
140 {
141 int ret;
142 pthread_t newPthread;
143 int curThreadPri, curThreadPolicy;
144 pthread_attr_t a = { 0 };
145 struct sched_param param = { 0 };
146
147 ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, ¶m);
148 ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
149
150 curThreadPri = param.sched_priority;
151
152 ret = pthread_attr_init(&a);
153 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
154 param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
155 pthread_attr_setschedparam(&a, ¶m);
156 ret = pthread_create(&newPthread, &a, PthreadAtforkTest, 0);
157 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
158
159 ret = pthread_join(newPthread, NULL);
160 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
161
162 return 0;
163 }
164
ItTestPthreadAtfork002(void)165 void ItTestPthreadAtfork002(void)
166 {
167 TEST_ADD_CASE("IT_PTHREAD_ATFORK_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
168 }
169