1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <pthread.h>
17 #include <stdlib.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21
22 #define TEST_THREAD_SLEEP 3
23
24 pthread_mutex_t glock = PTHREAD_MUTEX_INITIALIZER;
25 static int gprepare_flag = 0;
26 static int gparent_flag = 0;
27 static int gchild_flag = 0;
28
reset_data(void)29 void reset_data(void)
30 {
31 gprepare_flag = 0;
32 gparent_flag = 0;
33 gchild_flag = 0;
34 }
35
prepare(void)36 static void prepare(void)
37 {
38 int ret = pthread_mutex_lock(&glock);
39 EXPECT_EQ("pthread_atfork prepare", ret, 0);
40 gprepare_flag++;
41 }
42
parent(void)43 static void parent(void)
44 {
45 int ret = pthread_mutex_unlock(&glock);
46 EXPECT_EQ("pthread_atfork prepare", ret, 0);
47 gparent_flag++;
48 }
49
child(void)50 static void child(void)
51 {
52 int ret = pthread_mutex_unlock(&glock);
53 EXPECT_EQ("pthread_atfork prepare", ret, 0);
54 gchild_flag++;
55 }
56
thread_fun(void * arg)57 static void *thread_fun(void *arg)
58 {
59 while (1) {
60 pthread_testcancel();
61 pthread_mutex_lock(&glock);
62 sleep(TEST_THREAD_SLEEP);
63 pthread_mutex_unlock(&glock);
64 sleep(1);
65 }
66 return NULL;
67 }
68
69 /**
70 * @tc.name : pthread_atfork_0100
71 * @tc.desc : Provide a callback to test the relationship between pthread_atfork and fork
72 * @tc.level : Level 1
73 */
pthread_atfork_0100(void)74 void pthread_atfork_0100(void)
75 {
76 reset_data();
77 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
78 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
79
80 pthread_t tid;
81 int ret = pthread_create(&tid, NULL, thread_fun, NULL);
82 EXPECT_EQ("pthread_atfork_0100", ret, CMPFLAG);
83 if (ret != 0) {
84 return;
85 }
86
87 ret = pthread_atfork(prepare, parent, child);
88 EXPECT_EQ("pthread_atfork_0100", ret, CMPFLAG);
89 if (ret != 0) {
90 return;
91 }
92 sleep(1);
93
94 pid_t pid = fork();
95 if (pid == 0) {
96 pthread_mutex_lock(&glock);
97 sleep(1);
98 pthread_mutex_unlock(&glock);
99 EXPECT_EQ("pthread_atfork_0100", gchild_flag, 1);
100 _exit(0);
101 } else if (pid > 0) {
102 wait(NULL);
103 pthread_cancel(tid);
104 pthread_join(tid, NULL);
105
106 EXPECT_EQ("pthread_atfork_0100", gprepare_flag, 1);
107 EXPECT_EQ("pthread_atfork_0100", gparent_flag, 1);
108 } else {
109 EXPECT_FALSE("pthread_atfork_0100", 1);
110 }
111 }
112
113 /**
114 * @tc.name : pthread_atfork_0200
115 * @tc.desc : No callback is provided, test the relationship between pthread_atfork and fork
116 * @tc.level : Level 2
117 */
pthread_atfork_0200(void)118 void pthread_atfork_0200(void)
119 {
120 reset_data();
121 int ret = pthread_atfork(NULL, NULL, NULL);
122 EXPECT_EQ("pthread_atfork_0200", ret, CMPFLAG);
123 if (ret != 0) {
124 return;
125 }
126
127 pid_t pid = fork();
128 if (pid == 0) {
129 EXPECT_EQ("pthread_atfork_0200", gchild_flag, 0);
130 _exit(0);
131 } else if (pid > 0) {
132 wait(NULL);
133
134 EXPECT_EQ("pthread_atfork_0200", gprepare_flag, 0);
135 EXPECT_EQ("pthread_atfork_0200", gparent_flag, 0);
136 } else {
137 EXPECT_FALSE("pthread_atfork_0200", 1);
138 }
139 }
140
main(void)141 int main(void)
142 {
143 pthread_atfork_0200();
144 pthread_atfork_0100();
145 return t_status;
146 }