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 "sys/syscall.h"
33 
child1(void)34 void child1(void)
35 {
36     int count = 0;
37     int policy, pri, newPolicy;
38     struct sched_param param = { 0 };
39     pthread_t pthread = pthread_self();
40     int tid = Syscall(SYS_gettid, 0, 0, 0, 0);
41 
42     int ret = pthread_getschedparam(pthread, &policy, &param);
43 
44     pri = param.sched_priority;
45 
46     while (1) {
47         ret = pthread_getschedparam(pthread, &newPolicy, &param);
48         if (ret != 0) {
49             printf("pthread_getschedparam failed  ! %d error: %d\n", __LINE__, errno);
50             exit(255); // 255, set a special exit code.
51         }
52 
53         if (newPolicy != policy || pri != param.sched_priority) {
54             printf("pthread_getschedparam failed  ! %d policy %d newPolicy %d pri %d param.sched_priority :%d\n",
55                    __LINE__, policy, newPolicy, pri, param.sched_priority);
56             exit(255); // 255, set a special exit code.
57         }
58 
59         count++;
60     };
61 
62     exit(0);
63 }
64 
child(void)65 void child(void)
66 {
67     int ret;
68     int loop = 0;
69     int status = 0;
70     const int loopNum = 50;
71     const int countNum = 128;
72     pid_t pid = fork();
73     if (pid == 0) {
74         child1();
75     }
76 
77     int tid = Syscall(SYS_gettid, 0, 0, 0, 0);
78 
79     for (int count = 0; count < countNum; count++) {
80         if (count == tid) {
81             continue;
82         }
83 
84         ret = Syscall(SYS_pthread_join, count, 0, 0, 0);
85         if (ret == 0) {
86             exit(1);
87         }
88 
89         ret = Syscall(SYS_pthread_set_detach, count, 0, 0, 0);
90         if (ret == 0) {
91             exit(1);
92         }
93 
94         ret = Syscall(SYS_pthread_deatch, count, 0, 0, 0);
95         if (ret == 0) {
96             exit(1);
97         }
98 
99         ret = Syscall(SYS_sched_setparam, count, 20, -1, 0); // 20, param test
100         if (ret == 0) {
101             exit(1);
102         }
103 
104         ret = Syscall(SYS_sched_getparam, count, -1, 0, 0);
105         if (ret == 0) {
106             exit(1);
107         }
108 
109         ret = Syscall(SYS_sched_setscheduler, count, SCHED_RR, 20, -1); // 20, param test
110         if (ret == 0) {
111             exit(1);
112         }
113 
114         ret = Syscall(SYS_sched_getscheduler, count, -1, 0, 0);
115         if (ret == 0) {
116             exit(1);
117         }
118 
119         ret = Syscall(SYS_tkill, count, SIGPWR, 0, 0);
120         if (ret == 0) {
121             exit(1);
122         }
123 
124         if (count == countNum - 1) {
125             loop++;
126             if (loop == loopNum) {
127                 break;
128             } else {
129                 count = 0;
130             }
131         }
132     }
133 
134     kill(pid, SIGKILL);
135 
136     ret = waitpid(pid, &status, 0);
137     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
138 
139     exit(255); // 255, set a special exit code.
140 EXIT:
141     exit(0);
142 }
143 
Testcase(void)144 static int Testcase(void)
145 {
146     int ret;
147     int status = 0;
148     pid_t pid = fork();
149     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, The pid will never exceed 100000.
150     if (pid == 0) {
151         child();
152         printf("[Failed] - [Errline : %d RetCode : 0x%x\n", g_iCunitErrLineNo, g_iCunitErrCode);
153         exit(0);
154     }
155     ret = waitpid(pid, &status, 0);
156     ICUNIT_GOTO_EQUAL(ret, pid, ret, EXIT);
157     status = WEXITSTATUS(status);
158     ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert exit code.
159     return 0;
160 EXIT:
161     return 1;
162 }
163 
ItTestPthread011(void)164 void ItTestPthread011(void)
165 {
166     TEST_ADD_CASE("IT_POSIX_PTHREAD_011", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
167 }
168