1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "SignalTest.h"
17f6603c60Sopenharmony_ci#include <unistd.h>
18f6603c60Sopenharmony_ci#include <string.h>
19f6603c60Sopenharmony_ci#include <stdlib.h>
20f6603c60Sopenharmony_ci#include <fcntl.h>
21f6603c60Sopenharmony_ci#include <sys/shm.h>
22f6603c60Sopenharmony_ci#include <sys/types.h>
23f6603c60Sopenharmony_ci#include <sys/stat.h>
24f6603c60Sopenharmony_ci#include "log.h"
25f6603c60Sopenharmony_ci#include "utils.h"
26f6603c60Sopenharmony_ci#include "KernelConstants.h"
27f6603c60Sopenharmony_ci
28f6603c60Sopenharmony_ciusing namespace testing::ext;
29f6603c60Sopenharmony_ci
30f6603c60Sopenharmony_ci/**
31f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ALL_0100
32f6603c60Sopenharmony_ci * @tc.name   test the default action when receive a signal
33f6603c60Sopenharmony_ci              default action: http://man7.org/linux/man-pages/man7/signal.7.html
34f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
35f6603c60Sopenharmony_ci */
36f6603c60Sopenharmony_ciHWTEST_P(IpcSignalTest, testAllSigDefaultAction, Function | MediumTest | Level1)
37f6603c60Sopenharmony_ci{
38f6603c60Sopenharmony_ci    int index = GetParam();
39f6603c60Sopenharmony_ci    SignalAction a = ALL_SIGNALS[index].action;
40f6603c60Sopenharmony_ci    bool expectStop, coredump;
41f6603c60Sopenharmony_ci    if (a == STOP) { // SIGSTOP should has no effect in liteos
42f6603c60Sopenharmony_ci        expectStop = false;
43f6603c60Sopenharmony_ci        coredump = false;
44f6603c60Sopenharmony_ci    } else if (a == COREDUMP) {
45f6603c60Sopenharmony_ci        expectStop = true;
46f6603c60Sopenharmony_ci        coredump = true;
47f6603c60Sopenharmony_ci    } else if (a == TERMINATE) {
48f6603c60Sopenharmony_ci        expectStop = true;
49f6603c60Sopenharmony_ci        coredump = false;
50f6603c60Sopenharmony_ci    } else {
51f6603c60Sopenharmony_ci        // CONTINUE's default action is ignore, if the process is not Stopped
52f6603c60Sopenharmony_ci        expectStop = false;
53f6603c60Sopenharmony_ci        coredump = false;
54f6603c60Sopenharmony_ci    }
55f6603c60Sopenharmony_ci    LOG("all supported signal DefaultActionTest test %d: %s, expectStop=%d",
56f6603c60Sopenharmony_ci        index, ALL_SIGNALS[index].signame, expectStop);
57f6603c60Sopenharmony_ci    DefaultActionTest(index, expectStop, coredump);
58f6603c60Sopenharmony_ci    Msleep(100); // don't test too fast
59f6603c60Sopenharmony_ci}
60f6603c60Sopenharmony_ci
61f6603c60Sopenharmony_ci/**
62f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ALL_0200
63f6603c60Sopenharmony_ci * @tc.name   test all supported signal's send and receive. SIGKILL, SIGSTOP SIGCONT is special, not tested here
64f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
65f6603c60Sopenharmony_ci */
66f6603c60Sopenharmony_ciHWTEST_P(IpcSignalTest, testAllSigSendAndRecv, Function | MediumTest | Level1)
67f6603c60Sopenharmony_ci{
68f6603c60Sopenharmony_ci    int index = GetParam();
69f6603c60Sopenharmony_ci    // SIGKILL/SIGSTOP cannot be caught, blocked, or ignored, can not test here
70f6603c60Sopenharmony_ci    if (index==SIGKILL || index==SIGSTOP || index==SIGCONT) {
71f6603c60Sopenharmony_ci        return;
72f6603c60Sopenharmony_ci    }
73f6603c60Sopenharmony_ci    LOG("all supported signal SendAndRecv test %d: %s", index, ALL_SIGNALS[index].signame);
74f6603c60Sopenharmony_ci    SendAndRecvTest(index);
75f6603c60Sopenharmony_ci    Msleep(100); // don't test too fast
76f6603c60Sopenharmony_ci}
77f6603c60Sopenharmony_ci
78f6603c60Sopenharmony_ci/**
79f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ALL_0250
80f6603c60Sopenharmony_ci * @tc.name   test all signal's description string
81f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
82f6603c60Sopenharmony_ci */
83f6603c60Sopenharmony_ciHWTEST_P(IpcSignalTest, testAllSigDescription, Function | MediumTest | Level3)
84f6603c60Sopenharmony_ci{
85f6603c60Sopenharmony_ci    int signo = GetParam();
86f6603c60Sopenharmony_ci    const char* descipt = ALL_SIGNALS[signo].description;
87f6603c60Sopenharmony_ci    LOG("all supported signal's description string test: %d(%s)", signo, ALL_SIGNALS[signo].signame);
88f6603c60Sopenharmony_ci
89f6603c60Sopenharmony_ci    // we use child to do the test here, cause 'restore of stderr' is not work by now.
90f6603c60Sopenharmony_ci    pid_t pid = fork();
91f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
92f6603c60Sopenharmony_ci    if (pid == 0) { // child
93f6603c60Sopenharmony_ci        // redirect stderr to a file, so we can check the content.
94f6603c60Sopenharmony_ci        char* outfile = WRITABLE_TEST_DIR "stderr.txt";
95f6603c60Sopenharmony_ci        if (freopen(outfile, "w", stderr) == NULL) {
96f6603c60Sopenharmony_ci            LOG("redirect stderr fail, freopen errno=%d\n", errno);
97f6603c60Sopenharmony_ci            exit(1);
98f6603c60Sopenharmony_ci        }
99f6603c60Sopenharmony_ci        psignal(signo, nullptr);
100f6603c60Sopenharmony_ci        int rt = CheckSigString(outfile, descipt);
101f6603c60Sopenharmony_ci        exit(rt);
102f6603c60Sopenharmony_ci    } else { // parent
103f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
104f6603c60Sopenharmony_ci    }
105f6603c60Sopenharmony_ci    Msleep(100); // don't test too fast
106f6603c60Sopenharmony_ci}
107f6603c60Sopenharmony_ciINSTANTIATE_TEST_CASE_P(AllSignalTest, IpcSignalTest, testing::Range(1, MAX_SIGNAL));
108f6603c60Sopenharmony_ci
109f6603c60Sopenharmony_ci/**
110f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ALL_0300
111f6603c60Sopenharmony_ci * @tc.name   test SIGKILL can't blocked. SIGSTOP can't be blocked too, but not supported by liteos
112f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
113f6603c60Sopenharmony_ci */
114f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testBlockSIGKILL, Function | MediumTest | Level2)
115f6603c60Sopenharmony_ci{
116f6603c60Sopenharmony_ci    pid_t pid = fork();
117f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
118f6603c60Sopenharmony_ci    if (pid > 0) { // parent
119f6603c60Sopenharmony_ci        LOG("parent in");
120f6603c60Sopenharmony_ci        Msleep(50);
121f6603c60Sopenharmony_ci        LOGD("parent: before kill");
122f6603c60Sopenharmony_ci        kill(pid, SIGKILL);
123f6603c60Sopenharmony_ci        Msleep(150); // wait to exit
124f6603c60Sopenharmony_ci        WaitProcKilled(pid, SIGKILL);
125f6603c60Sopenharmony_ci    } else { // child
126f6603c60Sopenharmony_ci        LOG("child in");
127f6603c60Sopenharmony_ci        sigset_t set;
128f6603c60Sopenharmony_ci        sigemptyset(&set);
129f6603c60Sopenharmony_ci        sigaddset(&set, SIGKILL);
130f6603c60Sopenharmony_ci        int rt = sigprocmask(SIG_BLOCK, &set, nullptr);
131f6603c60Sopenharmony_ci        LOG("sigprocmask rt = %d", rt);
132f6603c60Sopenharmony_ci        KeepRun(100);
133f6603c60Sopenharmony_ci        LOG("child exit 0");
134f6603c60Sopenharmony_ci        exit(0);
135f6603c60Sopenharmony_ci    }
136f6603c60Sopenharmony_ci}
137f6603c60Sopenharmony_ci
138f6603c60Sopenharmony_ci/**
139f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ALL_0400
140f6603c60Sopenharmony_ci * @tc.name   test SIGKILL can't suspended.
141f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
142f6603c60Sopenharmony_ci */
143f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSuspendSIGKILL, Function | MediumTest | Level3)
144f6603c60Sopenharmony_ci{
145f6603c60Sopenharmony_ci    pid_t pid = fork();
146f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
147f6603c60Sopenharmony_ci    if (pid > 0) { // parent
148f6603c60Sopenharmony_ci        Msleep(20);
149f6603c60Sopenharmony_ci        kill(pid, SIGKILL);
150f6603c60Sopenharmony_ci        Msleep(150); // wait to exit
151f6603c60Sopenharmony_ci        WaitProcKilled(pid, SIGKILL);
152f6603c60Sopenharmony_ci    } else { // child
153f6603c60Sopenharmony_ci        sigset_t set;
154f6603c60Sopenharmony_ci        sigemptyset(&set);
155f6603c60Sopenharmony_ci        sigaddset(&set, SIGKILL);
156f6603c60Sopenharmony_ci        LOG("before sigsuspend");
157f6603c60Sopenharmony_ci        int rt = sigsuspend(&set);
158f6603c60Sopenharmony_ci        LOG("sigsuspend rt = %d", rt);
159f6603c60Sopenharmony_ci        KeepRun(100);
160f6603c60Sopenharmony_ci        exit(0);
161f6603c60Sopenharmony_ci    }
162f6603c60Sopenharmony_ci}
163f6603c60Sopenharmony_ci
164f6603c60Sopenharmony_ci/**
165f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGNAL_0100
166f6603c60Sopenharmony_ci * @tc.name   test SIGKILL and SIGSTOP's action can not changed by signal
167f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
168f6603c60Sopenharmony_ci */
169f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSignalFailSig, Function | MediumTest | Level2)
170f6603c60Sopenharmony_ci{
171f6603c60Sopenharmony_ci    SignalFailTest(SIGKILL, SignalHandler);
172f6603c60Sopenharmony_ci    SignalFailTest(SIGKILL, SIG_IGN);
173f6603c60Sopenharmony_ci    SignalFailTest(SIGSTOP, SignalHandler);
174f6603c60Sopenharmony_ci    SignalFailTest(SIGSTOP, SIG_IGN);
175f6603c60Sopenharmony_ci    SignalFailTest(31, SignalHandler);
176f6603c60Sopenharmony_ci    SignalFailTest(32, SignalHandler);
177f6603c60Sopenharmony_ci    SignalFailTest(33, SignalHandler);
178f6603c60Sopenharmony_ci}
179f6603c60Sopenharmony_ci
180f6603c60Sopenharmony_ci/**
181f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGNAL_0200
182f6603c60Sopenharmony_ci * @tc.name   test SIG_IGN and SIG_DFL
183f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
184f6603c60Sopenharmony_ci */
185f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSignal_DFL_IGN, Function | MediumTest | Level2)
186f6603c60Sopenharmony_ci{
187f6603c60Sopenharmony_ci    pid_t pid = fork();
188f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
189f6603c60Sopenharmony_ci    if (pid == 0) { // child
190f6603c60Sopenharmony_ci        int exitCode = 0;
191f6603c60Sopenharmony_ci        LOG("change handler to SIG_IGN");
192f6603c60Sopenharmony_ci        signal(SIGURG, SIG_IGN);
193f6603c60Sopenharmony_ci        Msleep(40);
194f6603c60Sopenharmony_ci        if (mReceivedSignal != 0) {
195f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected 0, but get %d", mReceivedSignal);
196f6603c60Sopenharmony_ci            exitCode = 1;
197f6603c60Sopenharmony_ci        }
198f6603c60Sopenharmony_ci
199f6603c60Sopenharmony_ci        LOG("change handler to SignalHandler");
200f6603c60Sopenharmony_ci        signal(SIGURG, SignalHandler);
201f6603c60Sopenharmony_ci        mReceivedSignal = 0;
202f6603c60Sopenharmony_ci        Msleep(50);
203f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGURG) {
204f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected %d, but get %d", SIGURG, mReceivedSignal);
205f6603c60Sopenharmony_ci            exitCode = 1;
206f6603c60Sopenharmony_ci        }
207f6603c60Sopenharmony_ci
208f6603c60Sopenharmony_ci        LOG("change handler to SIG_DFL");
209f6603c60Sopenharmony_ci        signal(SIGURG, SIG_DFL);
210f6603c60Sopenharmony_ci        mReceivedSignal = 0;
211f6603c60Sopenharmony_ci        Msleep(50);
212f6603c60Sopenharmony_ci        if (mReceivedSignal != 0) {
213f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected 0, but get %d", mReceivedSignal);
214f6603c60Sopenharmony_ci            exitCode = 1;
215f6603c60Sopenharmony_ci        }
216f6603c60Sopenharmony_ci        exit(exitCode);
217f6603c60Sopenharmony_ci    } else { // parent
218f6603c60Sopenharmony_ci        Msleep(20);
219f6603c60Sopenharmony_ci        LOG("------ Send SIGURG 1");
220f6603c60Sopenharmony_ci        kill(pid, SIGURG);
221f6603c60Sopenharmony_ci        Msleep(50);
222f6603c60Sopenharmony_ci        LOG("------ Send SIGURG 2");
223f6603c60Sopenharmony_ci        kill(pid, SIGURG);
224f6603c60Sopenharmony_ci        Msleep(50);
225f6603c60Sopenharmony_ci        LOG("------ Send SIGURG 3");
226f6603c60Sopenharmony_ci        kill(pid, SIGURG);
227f6603c60Sopenharmony_ci        Msleep(50);
228f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
229f6603c60Sopenharmony_ci    }
230f6603c60Sopenharmony_ci}
231f6603c60Sopenharmony_ci
232f6603c60Sopenharmony_ci/**
233f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_RAISE_0100
234f6603c60Sopenharmony_ci * @tc.name   raise basic function test
235f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
236f6603c60Sopenharmony_ci */
237f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testRaiseBasic, Function | MediumTest | Level1)
238f6603c60Sopenharmony_ci{
239f6603c60Sopenharmony_ci    pid_t pid = fork();
240f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
241f6603c60Sopenharmony_ci    if (pid == 0) { // child
242f6603c60Sopenharmony_ci        int exitCode = 0;
243f6603c60Sopenharmony_ci        signal(SIGQUIT, SignalHandler);
244f6603c60Sopenharmony_ci        raise(SIGQUIT);
245f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGQUIT) {
246f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGQUIT);
247f6603c60Sopenharmony_ci            exitCode = 1;
248f6603c60Sopenharmony_ci        }
249f6603c60Sopenharmony_ci        exit(exitCode);
250f6603c60Sopenharmony_ci    } else { // parent
251f6603c60Sopenharmony_ci        Msleep(30);
252f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
253f6603c60Sopenharmony_ci    }
254f6603c60Sopenharmony_ci}
255f6603c60Sopenharmony_ci
256f6603c60Sopenharmony_ci/**
257f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_ABORT_0100
258f6603c60Sopenharmony_ci * @tc.name   abort function test: SIGABRT handler
259f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
260f6603c60Sopenharmony_ci */
261f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testAbortHandler, Function | MediumTest | Level1)
262f6603c60Sopenharmony_ci{
263f6603c60Sopenharmony_ci    const int memSize = 32;
264f6603c60Sopenharmony_ci    mShmid = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
265f6603c60Sopenharmony_ci    if (mShmid == -1){
266f6603c60Sopenharmony_ci        LOG("shmget errno = %d\n", errno);
267f6603c60Sopenharmony_ci        ADD_FAILURE();
268f6603c60Sopenharmony_ci    }
269f6603c60Sopenharmony_ci
270f6603c60Sopenharmony_ci    pid_t pid = fork();
271f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
272f6603c60Sopenharmony_ci    if (pid == 0) { // child
273f6603c60Sopenharmony_ci        Msleep(30);
274f6603c60Sopenharmony_ci        signal(SIGABRT, SigAbortHandler);
275f6603c60Sopenharmony_ci        // try BLOCK SIGABRT
276f6603c60Sopenharmony_ci        sigset_t set;
277f6603c60Sopenharmony_ci        sigemptyset(&set);
278f6603c60Sopenharmony_ci        sigaddset(&set, SIGABRT);
279f6603c60Sopenharmony_ci        sigprocmask(SIG_BLOCK, &set, nullptr);
280f6603c60Sopenharmony_ci
281f6603c60Sopenharmony_ci        LOG("before abort");
282f6603c60Sopenharmony_ci        abort();
283f6603c60Sopenharmony_ci        // should never get here
284f6603c60Sopenharmony_ci        LOG("after abort");
285f6603c60Sopenharmony_ci        exit(1);
286f6603c60Sopenharmony_ci    } else { // parent
287f6603c60Sopenharmony_ci        Msleep(50);
288f6603c60Sopenharmony_ci        WaitProcKilled(pid, SIGABRT);
289f6603c60Sopenharmony_ci
290f6603c60Sopenharmony_ci        int *shared = static_cast<int*>(shmat(mShmid, NULL, 0));
291f6603c60Sopenharmony_ci        if (shared == reinterpret_cast<int*>(-1)) {
292f6603c60Sopenharmony_ci            LOG("shmat fail, errno = %d", errno);
293f6603c60Sopenharmony_ci            ADD_FAILURE();
294f6603c60Sopenharmony_ci        } else if (*shared != SIGABRT) {
295f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGABRT);
296f6603c60Sopenharmony_ci            ADD_FAILURE();
297f6603c60Sopenharmony_ci        }
298f6603c60Sopenharmony_ci        shmdt(shared);
299f6603c60Sopenharmony_ci        shmctl(mShmid, IPC_RMID, nullptr);
300f6603c60Sopenharmony_ci    }
301f6603c60Sopenharmony_ci}
302f6603c60Sopenharmony_ci
303f6603c60Sopenharmony_ci/**
304f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGACTION_0100
305f6603c60Sopenharmony_ci * @tc.name   sigaction function test: read siginfo
306f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
307f6603c60Sopenharmony_ci */
308f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigactionSiginfo, Function | MediumTest | Level1)
309f6603c60Sopenharmony_ci{
310f6603c60Sopenharmony_ci    pid_t pid = fork();
311f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
312f6603c60Sopenharmony_ci    if (pid == 0) { // child
313f6603c60Sopenharmony_ci        int exitCode = 0;
314f6603c60Sopenharmony_ci        struct sigaction act = {0};
315f6603c60Sopenharmony_ci        act.sa_sigaction = SigactionHandler;
316f6603c60Sopenharmony_ci        sigemptyset(&act.sa_mask);
317f6603c60Sopenharmony_ci        act.sa_flags = SA_SIGINFO;
318f6603c60Sopenharmony_ci        int rt = sigaction(SIGALRM, &act, nullptr);
319f6603c60Sopenharmony_ci        if (rt != 0) {
320f6603c60Sopenharmony_ci            LOG("sigaction return fail, rt=%d", rt);
321f6603c60Sopenharmony_ci            exit(1);
322f6603c60Sopenharmony_ci        }
323f6603c60Sopenharmony_ci        alarm(1);
324f6603c60Sopenharmony_ci        Msleep(1100);
325f6603c60Sopenharmony_ci
326f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGALRM) {
327f6603c60Sopenharmony_ci            LOG("SigactionHandler check fail, expected signal:%d, actual:%d", SIGALRM, mReceivedSignal);
328f6603c60Sopenharmony_ci            exit(1);
329f6603c60Sopenharmony_ci        }
330f6603c60Sopenharmony_ci        // other area of siginfo_t is not supported yet, test code deleted
331f6603c60Sopenharmony_ci        LOG("child exited with code=%d", exitCode);
332f6603c60Sopenharmony_ci        exit(exitCode);
333f6603c60Sopenharmony_ci    } else { // parent
334f6603c60Sopenharmony_ci        Msleep(1200);
335f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
336f6603c60Sopenharmony_ci    }
337f6603c60Sopenharmony_ci}
338f6603c60Sopenharmony_ci
339f6603c60Sopenharmony_ci/**
340f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGWAIT_0100
341f6603c60Sopenharmony_ci * @tc.name   sigwait basic function test
342f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
343f6603c60Sopenharmony_ci */
344f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigwaitBasic, Function | MediumTest | Level1)
345f6603c60Sopenharmony_ci{
346f6603c60Sopenharmony_ci    pid_t pid = fork();
347f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
348f6603c60Sopenharmony_ci    if (pid == 0) { // child
349f6603c60Sopenharmony_ci        int rt = 0;
350f6603c60Sopenharmony_ci        int exitCode = 0;
351f6603c60Sopenharmony_ci        sigset_t set;
352f6603c60Sopenharmony_ci        int sig;
353f6603c60Sopenharmony_ci        sigemptyset(&set);
354f6603c60Sopenharmony_ci        sigaddset(&set, SIGALRM);
355f6603c60Sopenharmony_ci        sigaddset(&set, SIGTERM);
356f6603c60Sopenharmony_ci        signal(SIGTERM, SignalHandler);
357f6603c60Sopenharmony_ci        signal(SIGALRM, SignalHandler);
358f6603c60Sopenharmony_ci
359f6603c60Sopenharmony_ci        rt = sigwait(&set, &sig);
360f6603c60Sopenharmony_ci        LOG("sigwait1 returned: %d, signo=%d, mReceivedSignal1: %d", rt, sig, mReceivedSignal);
361f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGALRM  || sig != SIGALRM) {
362f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGALRM);
363f6603c60Sopenharmony_ci            exitCode = 1;
364f6603c60Sopenharmony_ci        }
365f6603c60Sopenharmony_ci
366f6603c60Sopenharmony_ci        rt = sigwait(&set, &sig);
367f6603c60Sopenharmony_ci        LOG("sigwait2 returned: %d, signo=%d, mReceivedSignal1: %d", rt, sig, mReceivedSignal);
368f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGTERM  || sig != SIGTERM) {
369f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGALRM);
370f6603c60Sopenharmony_ci            exitCode = 1;
371f6603c60Sopenharmony_ci        }
372f6603c60Sopenharmony_ci
373f6603c60Sopenharmony_ci        rt = sigwait(&set, &sig);
374f6603c60Sopenharmony_ci        LOG("sigwait3 returned: %d, signo=%d, mReceivedSignal3: %d", rt, sig, mReceivedSignal);
375f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGALRM  || sig != SIGALRM) {
376f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGALRM);
377f6603c60Sopenharmony_ci            exitCode = 1;
378f6603c60Sopenharmony_ci        }
379f6603c60Sopenharmony_ci
380f6603c60Sopenharmony_ci        LOG("child exited with code=%d", exitCode);
381f6603c60Sopenharmony_ci        exit(exitCode);
382f6603c60Sopenharmony_ci    } else { // parent
383f6603c60Sopenharmony_ci        Msleep(30);
384f6603c60Sopenharmony_ci        LOG("calling kill 1, signo=%d", SIGALRM);
385f6603c60Sopenharmony_ci        kill(pid, SIGALRM);
386f6603c60Sopenharmony_ci        Msleep(50);
387f6603c60Sopenharmony_ci        LOG("calling kill 2, signo=%d", SIGTERM);
388f6603c60Sopenharmony_ci        kill(pid, SIGTERM);
389f6603c60Sopenharmony_ci        Msleep(50);
390f6603c60Sopenharmony_ci        LOG("calling kill 3, signo=%d", SIGALRM);
391f6603c60Sopenharmony_ci        kill(pid, SIGALRM);
392f6603c60Sopenharmony_ci        Msleep(100);
393f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
394f6603c60Sopenharmony_ci    }
395f6603c60Sopenharmony_ci}
396f6603c60Sopenharmony_ci/**
397f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGWAIT_0200
398f6603c60Sopenharmony_ci * @tc.name   test that 'sigwait' should not return if a not-in-set signal is received
399f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
400f6603c60Sopenharmony_ci */
401f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigwaitNotInSet, Function | MediumTest | Level2)
402f6603c60Sopenharmony_ci{
403f6603c60Sopenharmony_ci    pid_t pid = fork();
404f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
405f6603c60Sopenharmony_ci    if (pid == 0) { // child
406f6603c60Sopenharmony_ci        int rt = 0;
407f6603c60Sopenharmony_ci        int exitCode = 0;
408f6603c60Sopenharmony_ci        sigset_t set;
409f6603c60Sopenharmony_ci        int sig;
410f6603c60Sopenharmony_ci        sigemptyset(&set);
411f6603c60Sopenharmony_ci        sigaddset(&set, SIGTERM);
412f6603c60Sopenharmony_ci        signal(SIGTERM, SignalHandler);
413f6603c60Sopenharmony_ci        signal(SIGALRM, SignalHandler);
414f6603c60Sopenharmony_ci
415f6603c60Sopenharmony_ci        rt = sigwait(&set, &sig);
416f6603c60Sopenharmony_ci        LOG("sigwait1 returned: %d, signo=%d, mReceivedSignal1: %d", rt, sig, mReceivedSignal);
417f6603c60Sopenharmony_ci        LOG("child exiting with code=%d", exitCode);
418f6603c60Sopenharmony_ci        exit(exitCode);
419f6603c60Sopenharmony_ci    } else { // parent
420f6603c60Sopenharmony_ci        LOG("parent pid is %d, child pid is %d", getpid(), pid);
421f6603c60Sopenharmony_ci        Msleep(30);
422f6603c60Sopenharmony_ci        LOG("calling kill 1, signo=%d", SIGALRM);
423f6603c60Sopenharmony_ci        kill(pid, SIGALRM);
424f6603c60Sopenharmony_ci        Msleep(50);
425f6603c60Sopenharmony_ci        AssertProcAlive(pid);
426f6603c60Sopenharmony_ci
427f6603c60Sopenharmony_ci        LOG("calling kill 2, signo=%d", SIGKILL);
428f6603c60Sopenharmony_ci        kill(pid, SIGKILL);
429f6603c60Sopenharmony_ci        Msleep(100);
430f6603c60Sopenharmony_ci        AssertProcKilled(pid, SIGKILL);
431f6603c60Sopenharmony_ci    }
432f6603c60Sopenharmony_ci}
433f6603c60Sopenharmony_ci
434f6603c60Sopenharmony_ci/**
435f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGTIMEDWAIT_0100
436f6603c60Sopenharmony_ci * @tc.name   sigtimedwait still work even blocked by sigprocmask
437f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
438f6603c60Sopenharmony_ci */
439f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigtimedwaitBlock, Function | MediumTest | Level1)
440f6603c60Sopenharmony_ci{
441f6603c60Sopenharmony_ci    pid_t pid = fork();
442f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
443f6603c60Sopenharmony_ci    if (pid == 0) { // child
444f6603c60Sopenharmony_ci        int exitCode = 0;
445f6603c60Sopenharmony_ci        signal(SIGSEGV, SignalHandler);
446f6603c60Sopenharmony_ci        struct timespec time1 = {0, 100*1000000};
447f6603c60Sopenharmony_ci        sigset_t sigmask, timeset;
448f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
449f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGINT);
450f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGSEGV);
451f6603c60Sopenharmony_ci        sigemptyset(&timeset);
452f6603c60Sopenharmony_ci        sigaddset(&timeset, SIGSEGV);
453f6603c60Sopenharmony_ci
454f6603c60Sopenharmony_ci        sigprocmask(SIG_BLOCK, &sigmask, nullptr);
455f6603c60Sopenharmony_ci        Msleep(80);
456f6603c60Sopenharmony_ci
457f6603c60Sopenharmony_ci        int rt = sigtimedwait(&timeset, nullptr, &time1);
458f6603c60Sopenharmony_ci        if (rt != SIGSEGV) {
459f6603c60Sopenharmony_ci            LOG("sigtimedwait return fail, expected:%d, actual:%d", SIGSEGV, rt);
460f6603c60Sopenharmony_ci            exitCode = 1;
461f6603c60Sopenharmony_ci        }
462f6603c60Sopenharmony_ci
463f6603c60Sopenharmony_ci        // check the sigprocmask set not changed
464f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
465f6603c60Sopenharmony_ci        sigprocmask(SIG_UNBLOCK, nullptr, &sigmask);
466f6603c60Sopenharmony_ci        if (sigismember(&sigmask, SIGINT) != 1) {
467f6603c60Sopenharmony_ci            LOG("SIGINT should still in block set!");
468f6603c60Sopenharmony_ci            exitCode = 1;
469f6603c60Sopenharmony_ci        }
470f6603c60Sopenharmony_ci        if (sigismember(&sigmask, SIGSEGV) != 1) {
471f6603c60Sopenharmony_ci            LOG("SIGSEGV should still in block set!");
472f6603c60Sopenharmony_ci            exitCode = 1;
473f6603c60Sopenharmony_ci        }
474f6603c60Sopenharmony_ci        exit(exitCode);
475f6603c60Sopenharmony_ci    } else { // parent
476f6603c60Sopenharmony_ci        Msleep(40);
477f6603c60Sopenharmony_ci        kill(pid, SIGSEGV);
478f6603c60Sopenharmony_ci        Msleep(200);
479f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
480f6603c60Sopenharmony_ci    }
481f6603c60Sopenharmony_ci    sigset_t pending;
482f6603c60Sopenharmony_ci    sigemptyset(&pending);
483f6603c60Sopenharmony_ci    sigpending(&pending);
484f6603c60Sopenharmony_ci    if (sigisemptyset(&pending)) {
485f6603c60Sopenharmony_ci        LOG("pending set empty");
486f6603c60Sopenharmony_ci        return;
487f6603c60Sopenharmony_ci    }
488f6603c60Sopenharmony_ci    LOG("========pending set not empty=========");
489f6603c60Sopenharmony_ci    if (sigismember(&pending, SIGCHLD)) {
490f6603c60Sopenharmony_ci        LOG("pending set is SIGCHLD");
491f6603c60Sopenharmony_ci        return;
492f6603c60Sopenharmony_ci    } else {
493f6603c60Sopenharmony_ci        LOG("pending set is not SIGCHLD!");
494f6603c60Sopenharmony_ci    }
495f6603c60Sopenharmony_ci}
496f6603c60Sopenharmony_ci
497f6603c60Sopenharmony_ci/**
498f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGTIMEDWAIT_0200
499f6603c60Sopenharmony_ci * @tc.name   sigtimedwait error test: timeout or interrupted.
500f6603c60Sopenharmony_ci *            by now, liteos sigtimedwait cannot interrupted
501f6603c60Sopenharmony_ci *            so the interrupted-check-code will run only when 'POSIX_TEST' is set
502f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
503f6603c60Sopenharmony_ci */
504f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigtimedwaitTimeout, Function | MediumTest | Level2)
505f6603c60Sopenharmony_ci{
506f6603c60Sopenharmony_ci    pid_t pid = fork();
507f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
508f6603c60Sopenharmony_ci    if (pid == 0) { // child
509f6603c60Sopenharmony_ci        int rt;
510f6603c60Sopenharmony_ci        int exitCode = 0;
511f6603c60Sopenharmony_ci        sigset_t set;
512f6603c60Sopenharmony_ci        siginfo_t si;
513f6603c60Sopenharmony_ci        struct timespec ts = {0, 500*1000000};
514f6603c60Sopenharmony_ci        sigemptyset(&set);
515f6603c60Sopenharmony_ci        sigaddset(&set, SIGUSR1);
516f6603c60Sopenharmony_ci
517f6603c60Sopenharmony_ci        struct timespec time1 = {0, 0};
518f6603c60Sopenharmony_ci        struct timespec time2 = {0, 0};
519f6603c60Sopenharmony_ci        errno = 0;
520f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time1);
521f6603c60Sopenharmony_ci        rt = sigtimedwait(&set, &si, &ts);
522f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time2);
523f6603c60Sopenharmony_ci        LOG("sigtimedwait returned: %d", rt);
524f6603c60Sopenharmony_ci        if (rt != -1 || errno != EAGAIN) {
525f6603c60Sopenharmony_ci            LOG("sigtimedwait error check fail, expected errno=%d(EAGAIN), actual=%d", EAGAIN, errno);
526f6603c60Sopenharmony_ci            exitCode = 1;
527f6603c60Sopenharmony_ci        }
528f6603c60Sopenharmony_ci        double duration = (time2.tv_sec - time1.tv_sec)*1000.0 + (time2.tv_nsec - time1.tv_nsec)/1000000.0;
529f6603c60Sopenharmony_ci        LOG("clock_gettime1 : tv_sec=%ld, tv_nsec=%ld", time1.tv_sec, time1.tv_nsec);
530f6603c60Sopenharmony_ci        LOG("clock_gettime2 : tv_sec=%ld, tv_nsec=%ld", time2.tv_sec, time2.tv_nsec);
531f6603c60Sopenharmony_ci        LOG("duration : %f ms", duration);
532f6603c60Sopenharmony_ci        if (CheckValueClose(1000, duration)) {
533f6603c60Sopenharmony_ci            LOG("Timeout value accuracy check fail, expected=1000, actual=%f", duration);
534f6603c60Sopenharmony_ci            exitCode = 1;
535f6603c60Sopenharmony_ci        }
536f6603c60Sopenharmony_ci
537f6603c60Sopenharmony_ci        LOG("child exited with code=%d", exitCode);
538f6603c60Sopenharmony_ci        exit(exitCode);
539f6603c60Sopenharmony_ci    } else { // parent
540f6603c60Sopenharmony_ci        sleep(1);
541f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
542f6603c60Sopenharmony_ci    }
543f6603c60Sopenharmony_ci}
544f6603c60Sopenharmony_ci
545f6603c60Sopenharmony_ci
546f6603c60Sopenharmony_ci/**
547f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGTIMEDWAIT_0300
548f6603c60Sopenharmony_ci * @tc.name   sigtimedwait siginfo_t test
549f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
550f6603c60Sopenharmony_ci */
551f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigtimedwaitSiginfo, Function | MediumTest | Level2)
552f6603c60Sopenharmony_ci{
553f6603c60Sopenharmony_ci    pid_t pid = fork();
554f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
555f6603c60Sopenharmony_ci    if (pid == 0) { // child
556f6603c60Sopenharmony_ci        int exitCode = 0;
557f6603c60Sopenharmony_ci        signal(SIGINT, SignalHandler);
558f6603c60Sopenharmony_ci        struct timespec time1 = {0, 100*1000000};
559f6603c60Sopenharmony_ci        sigset_t set;
560f6603c60Sopenharmony_ci        siginfo_t si;
561f6603c60Sopenharmony_ci        sigemptyset(&set);
562f6603c60Sopenharmony_ci        sigaddset(&set, SIGINT);
563f6603c60Sopenharmony_ci        int rt = sigtimedwait(&set, &si, &time1);
564f6603c60Sopenharmony_ci        if (rt != SIGINT) {
565f6603c60Sopenharmony_ci            LOG("sigtimedwait should return the signal, expected:%d, actual:%d", SIGINT, rt);
566f6603c60Sopenharmony_ci            exitCode = 1;
567f6603c60Sopenharmony_ci        }
568f6603c60Sopenharmony_ci        LOGD("si: %d,%d,%d,%p", si.si_signo, si.si_code, si.si_value.sival_int, si.si_value.sival_int);
569f6603c60Sopenharmony_ci        if (si.si_signo != SIGINT) {
570f6603c60Sopenharmony_ci            LOG("sigtimedwait set siginfo_t fail, si_signo=%d", si.si_signo);
571f6603c60Sopenharmony_ci            exitCode = 1;
572f6603c60Sopenharmony_ci        }
573f6603c60Sopenharmony_ci        // other area of siginfo_t is not supported yet, test code deleted
574f6603c60Sopenharmony_ci        LOG("child exited with code=%d", exitCode);
575f6603c60Sopenharmony_ci        exit(exitCode);
576f6603c60Sopenharmony_ci    } else { // parent
577f6603c60Sopenharmony_ci        Msleep(20);
578f6603c60Sopenharmony_ci        kill(pid, SIGINT);
579f6603c60Sopenharmony_ci        Msleep(150);
580f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
581f6603c60Sopenharmony_ci    }
582f6603c60Sopenharmony_ci}
583f6603c60Sopenharmony_ci
584f6603c60Sopenharmony_ci/**
585f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGWAITINFO_0100
586f6603c60Sopenharmony_ci * @tc.name   sigwaitinfo basic test: a pending signal should cause sigwaitinfo return immediately
587f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
588f6603c60Sopenharmony_ci */
589f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigwaitinfoBasic, Function | MediumTest | Level1)
590f6603c60Sopenharmony_ci{
591f6603c60Sopenharmony_ci    pid_t pid = fork();
592f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
593f6603c60Sopenharmony_ci    if (pid == 0) { // child
594f6603c60Sopenharmony_ci        int exitCode = 0;
595f6603c60Sopenharmony_ci        sigset_t set, pending;
596f6603c60Sopenharmony_ci        sigemptyset(&pending);
597f6603c60Sopenharmony_ci        sigemptyset(&set);
598f6603c60Sopenharmony_ci        sigaddset(&set, SIGALRM);
599f6603c60Sopenharmony_ci        signal(SIGALRM, SignalHandler);
600f6603c60Sopenharmony_ci        if (sigprocmask(SIG_BLOCK, &set, nullptr) == -1) {
601f6603c60Sopenharmony_ci            LOG("sigprocmask failed");
602f6603c60Sopenharmony_ci            exit(1);
603f6603c60Sopenharmony_ci        }
604f6603c60Sopenharmony_ci        LOG("raise SIGALRM");
605f6603c60Sopenharmony_ci        raise(SIGALRM);
606f6603c60Sopenharmony_ci
607f6603c60Sopenharmony_ci        sigpending(&pending);
608f6603c60Sopenharmony_ci        if (!sigismember(&pending, SIGALRM)) {
609f6603c60Sopenharmony_ci            LOG("SIGALRM is not in pending set");
610f6603c60Sopenharmony_ci            exit(1);
611f6603c60Sopenharmony_ci        }
612f6603c60Sopenharmony_ci
613f6603c60Sopenharmony_ci        LOGD("before sigwaitinfo");
614f6603c60Sopenharmony_ci        struct timespec time1 = {0, 0};
615f6603c60Sopenharmony_ci        struct timespec time2 = {0, 0};
616f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time1);
617f6603c60Sopenharmony_ci        int rt = sigwaitinfo(&set, nullptr);
618f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time2);
619f6603c60Sopenharmony_ci
620f6603c60Sopenharmony_ci        LOGD("after sigwaitinfo");
621f6603c60Sopenharmony_ci        double duration = (time2.tv_sec - time1.tv_sec)*1000.0 + (time2.tv_nsec - time1.tv_nsec)/1000000.0;
622f6603c60Sopenharmony_ci        LOG("duration: %f ms", duration);
623f6603c60Sopenharmony_ci        if (CheckValueClose(0.1, duration)) {
624f6603c60Sopenharmony_ci            LOG("sigwaitinfo should return immediately, but %f ms used", duration);
625f6603c60Sopenharmony_ci            exitCode = 1;
626f6603c60Sopenharmony_ci        }
627f6603c60Sopenharmony_ci        if (rt != SIGALRM) {
628f6603c60Sopenharmony_ci            LOG("sigwaitinfo should return the signal, expected:%d, actual:%d", SIGALRM, rt);
629f6603c60Sopenharmony_ci            exitCode = 1;
630f6603c60Sopenharmony_ci        }
631f6603c60Sopenharmony_ci        LOG("child exited with code=%d", exitCode);
632f6603c60Sopenharmony_ci        exit(exitCode);
633f6603c60Sopenharmony_ci    } else { // parent
634f6603c60Sopenharmony_ci        sleep(1);
635f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
636f6603c60Sopenharmony_ci    }
637f6603c60Sopenharmony_ci}
638f6603c60Sopenharmony_ci
639f6603c60Sopenharmony_ci/**
640f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PAUSE_0100
641f6603c60Sopenharmony_ci * @tc.name   pause basic function test
642f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
643f6603c60Sopenharmony_ci */
644f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPauseBasic, Function | MediumTest | Level1)
645f6603c60Sopenharmony_ci{
646f6603c60Sopenharmony_ci    pid_t pid = fork();
647f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
648f6603c60Sopenharmony_ci    if (pid == 0) { // child
649f6603c60Sopenharmony_ci        struct timespec time1 = {0};
650f6603c60Sopenharmony_ci        struct timespec time2 = {0};
651f6603c60Sopenharmony_ci        int exitCode = 0;
652f6603c60Sopenharmony_ci        signal(SIGQUIT, SignalHandler);
653f6603c60Sopenharmony_ci        Msleep(100);
654f6603c60Sopenharmony_ci
655f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time1);
656f6603c60Sopenharmony_ci        int rt = pause();
657f6603c60Sopenharmony_ci        clock_gettime(CLOCK_MONOTONIC, &time2);
658f6603c60Sopenharmony_ci        if (rt != -1) {
659f6603c60Sopenharmony_ci            LOG("pause should return -1, but rt=%d", rt);
660f6603c60Sopenharmony_ci            exitCode = 1;
661f6603c60Sopenharmony_ci        }
662f6603c60Sopenharmony_ci        if (errno != EINTR) {
663f6603c60Sopenharmony_ci            LOG("pause should set errno to 4(EINTR),but get %d", errno);
664f6603c60Sopenharmony_ci            exitCode = 1;
665f6603c60Sopenharmony_ci        }
666f6603c60Sopenharmony_ci
667f6603c60Sopenharmony_ci        long duration = (time2.tv_sec - time1.tv_sec)*1000 + (time2.tv_nsec - time1.tv_nsec)/1000000;
668f6603c60Sopenharmony_ci        LOG("paused time: %ld ms", duration);
669f6603c60Sopenharmony_ci        if (! CheckValueClose(100, duration, 0.2)) {
670f6603c60Sopenharmony_ci            LOG("paused time check error.");
671f6603c60Sopenharmony_ci            exitCode = 1;
672f6603c60Sopenharmony_ci        }
673f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGQUIT) {
674f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGQUIT);
675f6603c60Sopenharmony_ci            exitCode = 1;
676f6603c60Sopenharmony_ci        }
677f6603c60Sopenharmony_ci        exit(exitCode);
678f6603c60Sopenharmony_ci    } else { // parent
679f6603c60Sopenharmony_ci        Msleep(200);
680f6603c60Sopenharmony_ci        kill(pid, SIGQUIT);
681f6603c60Sopenharmony_ci        Msleep(200);
682f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
683f6603c60Sopenharmony_ci    }
684f6603c60Sopenharmony_ci}
685f6603c60Sopenharmony_ci/**
686f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PAUSE_0200
687f6603c60Sopenharmony_ci * @tc.name   pause and mask test
688f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
689f6603c60Sopenharmony_ci */
690f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPauseAndMask, Function | MediumTest | Level1)
691f6603c60Sopenharmony_ci{
692f6603c60Sopenharmony_ci    pid_t pid = fork();
693f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
694f6603c60Sopenharmony_ci    if (pid == 0) { // child
695f6603c60Sopenharmony_ci        int exitCode = 0;
696f6603c60Sopenharmony_ci        signal(SIGINT, SignalHandler);
697f6603c60Sopenharmony_ci        signal(SIGTRAP, SignalHandler);
698f6603c60Sopenharmony_ci
699f6603c60Sopenharmony_ci        sigset_t sigmask;
700f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
701f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGINT);
702f6603c60Sopenharmony_ci        int rt = sigprocmask(SIG_BLOCK, &sigmask, nullptr);
703f6603c60Sopenharmony_ci        if (rt != 0) {
704f6603c60Sopenharmony_ci            LOG("sigprocmask fail, rt=%d, errno=%d", rt, errno);
705f6603c60Sopenharmony_ci            exit(1);
706f6603c60Sopenharmony_ci        }
707f6603c60Sopenharmony_ci        LOG("before pause");
708f6603c60Sopenharmony_ci        rt = pause();
709f6603c60Sopenharmony_ci        if (rt != -1) {
710f6603c60Sopenharmony_ci            LOG("pause should return -1, but rt=%d", rt);
711f6603c60Sopenharmony_ci            exitCode = 1;
712f6603c60Sopenharmony_ci        }
713f6603c60Sopenharmony_ci        if (errno != EINTR) {
714f6603c60Sopenharmony_ci            LOG("pause should set errno to 4(EINTR),but get %d", errno);
715f6603c60Sopenharmony_ci            exitCode = 1;
716f6603c60Sopenharmony_ci        }
717f6603c60Sopenharmony_ci        LOG("after pause");
718f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGTRAP) {
719f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected %d,but get %d", SIGINT, mReceivedSignal);
720f6603c60Sopenharmony_ci            exitCode = 1;
721f6603c60Sopenharmony_ci        }
722f6603c60Sopenharmony_ci        exit(exitCode);
723f6603c60Sopenharmony_ci    } else { // parent
724f6603c60Sopenharmony_ci        Msleep(20);
725f6603c60Sopenharmony_ci        kill(pid, SIGINT);
726f6603c60Sopenharmony_ci        Msleep(20);
727f6603c60Sopenharmony_ci        AssertProcAlive(pid);
728f6603c60Sopenharmony_ci        kill(pid, SIGTRAP);
729f6603c60Sopenharmony_ci        Msleep(20);
730f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
731f6603c60Sopenharmony_ci    }
732f6603c60Sopenharmony_ci    sigset_t pending;
733f6603c60Sopenharmony_ci    sigemptyset(&pending);
734f6603c60Sopenharmony_ci    sigpending(&pending);
735f6603c60Sopenharmony_ci    if (sigisemptyset(&pending)) {
736f6603c60Sopenharmony_ci        LOG("pending set empty");
737f6603c60Sopenharmony_ci    } else {
738f6603c60Sopenharmony_ci        LOG("========pending set not empty=========");
739f6603c60Sopenharmony_ci    }
740f6603c60Sopenharmony_ci}
741f6603c60Sopenharmony_ci
742f6603c60Sopenharmony_ci/**
743f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPENDING_0100
744f6603c60Sopenharmony_ci * @tc.name   sigpending basic function test, also test pending-signal-set should not inherited after fork,
745f6603c60Sopenharmony_ci              but signal mask set should inherited, and the change of child's pending set should not effect parent's.
746f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
747f6603c60Sopenharmony_ci */
748f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigpendingBasic, Function | MediumTest | Level1)
749f6603c60Sopenharmony_ci{
750f6603c60Sopenharmony_ci    int rt;
751f6603c60Sopenharmony_ci    sigset_t sigmask, oldmask;
752f6603c60Sopenharmony_ci    sigset_t pending;
753f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
754f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
755f6603c60Sopenharmony_ci    sigemptyset(&pending);
756f6603c60Sopenharmony_ci    sigpending(&pending);
757f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigisemptyset(&pending)) << "initial pending set should empty";
758f6603c60Sopenharmony_ci
759f6603c60Sopenharmony_ci    signal(SIGALRM, SignalHandler);
760f6603c60Sopenharmony_ci
761f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGALRM);
762f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGUSR1);
763f6603c60Sopenharmony_ci    sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
764f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigisemptyset(&oldmask)) << "initial sig mask set should empty";
765f6603c60Sopenharmony_ci    rt = sigpending(&pending);
766f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
767f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigisemptyset(&pending)) << "SIG_BLOCK set should not effect on pending set";
768f6603c60Sopenharmony_ci
769f6603c60Sopenharmony_ci    LOGD("raise SIGALRM");
770f6603c60Sopenharmony_ci    raise(SIGALRM);
771f6603c60Sopenharmony_ci    sigpending(&pending);
772f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&pending, SIGALRM));
773f6603c60Sopenharmony_ci
774f6603c60Sopenharmony_ci    pid_t pid = fork();
775f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
776f6603c60Sopenharmony_ci    if (pid == 0) { // child
777f6603c60Sopenharmony_ci        int exitCode = 0;
778f6603c60Sopenharmony_ci        sigemptyset(&pending);
779f6603c60Sopenharmony_ci        sigpending(&pending);
780f6603c60Sopenharmony_ci        if (sigisemptyset(&pending) != 1) {
781f6603c60Sopenharmony_ci            LOG("pending signal set should not reserved via fork");
782f6603c60Sopenharmony_ci            exitCode = 1;
783f6603c60Sopenharmony_ci        }
784f6603c60Sopenharmony_ci
785f6603c60Sopenharmony_ci        sigemptyset(&oldmask);
786f6603c60Sopenharmony_ci        sigprocmask(SIG_BLOCK, nullptr, &oldmask); // read mask
787f6603c60Sopenharmony_ci        if ((sigismember(&oldmask, SIGALRM) != 1) || (sigismember(&oldmask, SIGUSR1) != 1)) {
788f6603c60Sopenharmony_ci            LOG("signal mask set should reserved via fork");
789f6603c60Sopenharmony_ci            exitCode = 1;
790f6603c60Sopenharmony_ci        }
791f6603c60Sopenharmony_ci
792f6603c60Sopenharmony_ci        LOG("unblock SIGALRM.");
793f6603c60Sopenharmony_ci        rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
794f6603c60Sopenharmony_ci        sigemptyset(&pending);
795f6603c60Sopenharmony_ci        sigpending(&pending);
796f6603c60Sopenharmony_ci        if (sigisemptyset(&pending) != 1) {
797f6603c60Sopenharmony_ci            LOG("pending signal set is not empty after unblock");
798f6603c60Sopenharmony_ci            exitCode = 1;
799f6603c60Sopenharmony_ci        }
800f6603c60Sopenharmony_ci
801f6603c60Sopenharmony_ci        LOG("child exit(%d).", exitCode);
802f6603c60Sopenharmony_ci        exit(exitCode);
803f6603c60Sopenharmony_ci    } else { // parent
804f6603c60Sopenharmony_ci        Msleep(80);
805f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
806f6603c60Sopenharmony_ci        // check child's pending set should not effect parent's
807f6603c60Sopenharmony_ci        sigemptyset(&pending);
808f6603c60Sopenharmony_ci        sigpending(&pending);
809f6603c60Sopenharmony_ci        EXPECT_EQ(1, sigismember(&pending, SIGALRM)) << "parent's pending set is changed!";
810f6603c60Sopenharmony_ci    }
811f6603c60Sopenharmony_ci}
812f6603c60Sopenharmony_ci
813f6603c60Sopenharmony_ci/**
814f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPROCMASK_0100
815f6603c60Sopenharmony_ci * @tc.name   sigprocmask function test: use raise and kill to send signal
816f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
817f6603c60Sopenharmony_ci */
818f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigprocmaskBasic, Function | MediumTest | Level1)
819f6603c60Sopenharmony_ci{
820f6603c60Sopenharmony_ci    int rt;
821f6603c60Sopenharmony_ci    sigset_t sigmask, oldmask;
822f6603c60Sopenharmony_ci    sigset_t pending;
823f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
824f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
825f6603c60Sopenharmony_ci    sigemptyset(&pending);
826f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGINT);
827f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGUSR1);
828f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
829f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
830f6603c60Sopenharmony_ci
831f6603c60Sopenharmony_ci    signal(SIGINT, SignalHandler);
832f6603c60Sopenharmony_ci    signal(SIGUSR1, SignalHandler);
833f6603c60Sopenharmony_ci    ASSERT_EQ(mReceivedSignal, 0);
834f6603c60Sopenharmony_ci
835f6603c60Sopenharmony_ci    pid_t pid = fork();
836f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
837f6603c60Sopenharmony_ci    if (pid == 0) { // child
838f6603c60Sopenharmony_ci        int exitCode = 0;
839f6603c60Sopenharmony_ci        raise(SIGINT);
840f6603c60Sopenharmony_ci        if (mReceivedSignal != 0) {
841f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected=%d, actual=%d", 0, mReceivedSignal);
842f6603c60Sopenharmony_ci            LOG("SIGINT(%d) should has blocked!", SIGINT);
843f6603c60Sopenharmony_ci            exit(1);
844f6603c60Sopenharmony_ci        }
845f6603c60Sopenharmony_ci
846f6603c60Sopenharmony_ci        LOG("unblock SIGINT");
847f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
848f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGINT);
849f6603c60Sopenharmony_ci        rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
850f6603c60Sopenharmony_ci        EXPECT_EQ(rt, 0);
851f6603c60Sopenharmony_ci
852f6603c60Sopenharmony_ci        // check the new block set
853f6603c60Sopenharmony_ci        sigemptyset(&oldmask);
854f6603c60Sopenharmony_ci        sigprocmask(SIG_UNBLOCK, nullptr, &oldmask);
855f6603c60Sopenharmony_ci        if (sigismember(&oldmask, SIGINT) == 1) {
856f6603c60Sopenharmony_ci            LOG("SIGINT should has deleted from block set!");
857f6603c60Sopenharmony_ci            exitCode = 1;
858f6603c60Sopenharmony_ci        }
859f6603c60Sopenharmony_ci        if (sigismember(&oldmask, SIGUSR1) != 1) {
860f6603c60Sopenharmony_ci            LOG("SIGUSR1 should still in block set!");
861f6603c60Sopenharmony_ci            exitCode = 1;
862f6603c60Sopenharmony_ci        }
863f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGINT) {
864f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected=%d, actual=%d", SIGINT, mReceivedSignal);
865f6603c60Sopenharmony_ci            LOG("SIGALRM should deliver after unblock!");
866f6603c60Sopenharmony_ci            exitCode = 1;
867f6603c60Sopenharmony_ci        }
868f6603c60Sopenharmony_ci
869f6603c60Sopenharmony_ci        // test kill
870f6603c60Sopenharmony_ci        mReceivedSignal = 0;
871f6603c60Sopenharmony_ci        Msleep(80);
872f6603c60Sopenharmony_ci        sigpending(&pending);
873f6603c60Sopenharmony_ci        if (!sigismember(&pending, SIGUSR1)) {
874f6603c60Sopenharmony_ci            LOG("SIGUSR1 is not in pending set!");
875f6603c60Sopenharmony_ci            exitCode = 1;
876f6603c60Sopenharmony_ci        } else {
877f6603c60Sopenharmony_ci            LOG("SIGUSR1 is in pending set.");
878f6603c60Sopenharmony_ci        }
879f6603c60Sopenharmony_ci
880f6603c60Sopenharmony_ci        if (mReceivedSignal != 0) {
881f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected=%d, actual=%d", 0, mReceivedSignal);
882f6603c60Sopenharmony_ci            LOG("SIGUSR1(%d) should has blocked!", SIGUSR1);
883f6603c60Sopenharmony_ci            exit(1);
884f6603c60Sopenharmony_ci        }
885f6603c60Sopenharmony_ci
886f6603c60Sopenharmony_ci        LOG("unblock SIGUSR1");
887f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
888f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGUSR1);
889f6603c60Sopenharmony_ci        sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
890f6603c60Sopenharmony_ci
891f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGUSR1) {
892f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected=%d, actual=%d", SIGUSR1, mReceivedSignal);
893f6603c60Sopenharmony_ci            LOG("SIGUSR1 should deliver after unblock!");
894f6603c60Sopenharmony_ci            exitCode = 1;
895f6603c60Sopenharmony_ci        }
896f6603c60Sopenharmony_ci
897f6603c60Sopenharmony_ci        LOG("child exit(%d).", exitCode);
898f6603c60Sopenharmony_ci        exit(exitCode);
899f6603c60Sopenharmony_ci    } else { // parent
900f6603c60Sopenharmony_ci        Msleep(50);
901f6603c60Sopenharmony_ci        kill(pid, SIGUSR1);
902f6603c60Sopenharmony_ci        Msleep(50);
903f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
904f6603c60Sopenharmony_ci    }
905f6603c60Sopenharmony_ci    // restore default config
906f6603c60Sopenharmony_ci    signal(SIGINT, SIG_DFL);
907f6603c60Sopenharmony_ci    signal(SIGUSR1, SIG_DFL);
908f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
909f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
910f6603c60Sopenharmony_ci}
911f6603c60Sopenharmony_ci
912f6603c60Sopenharmony_ci/**
913f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPROCMASK_0200
914f6603c60Sopenharmony_ci * @tc.name   sigprocmask function test: use alarm to send the signal
915f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
916f6603c60Sopenharmony_ci */
917f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigprocmaskAlarm, Function | MediumTest | Level2)
918f6603c60Sopenharmony_ci{
919f6603c60Sopenharmony_ci    int rt;
920f6603c60Sopenharmony_ci    sigset_t sigmask, oldmask;
921f6603c60Sopenharmony_ci    sigset_t pending;
922f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
923f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
924f6603c60Sopenharmony_ci    sigemptyset(&pending);
925f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGALRM);
926f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
927f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
928f6603c60Sopenharmony_ci
929f6603c60Sopenharmony_ci    signal(SIGALRM, SignalHandler);
930f6603c60Sopenharmony_ci    ASSERT_EQ(mReceivedSignal, 0);
931f6603c60Sopenharmony_ci    alarm(1);
932f6603c60Sopenharmony_ci    Msleep(1100);
933f6603c60Sopenharmony_ci
934f6603c60Sopenharmony_ci    sigpending(&pending);
935f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&pending, SIGALRM));
936f6603c60Sopenharmony_ci    EXPECT_EQ(mReceivedSignal, 0);
937f6603c60Sopenharmony_ci
938f6603c60Sopenharmony_ci    LOG("unblock SIGALRM.");
939f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
940f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
941f6603c60Sopenharmony_ci    ASSERT_EQ(mReceivedSignal, SIGALRM);
942f6603c60Sopenharmony_ci
943f6603c60Sopenharmony_ci    signal(SIGALRM, SIG_DFL);
944f6603c60Sopenharmony_ci}
945f6603c60Sopenharmony_ci
946f6603c60Sopenharmony_ci/**
947f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPROCMASK_0300
948f6603c60Sopenharmony_ci * @tc.name   sigprocmask function test: mask operation
949f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
950f6603c60Sopenharmony_ci */
951f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigprocmaskSetMask, Function | MediumTest | Level2)
952f6603c60Sopenharmony_ci{
953f6603c60Sopenharmony_ci    int rt;
954f6603c60Sopenharmony_ci    sigset_t sigmask, oldmask, pending;
955f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
956f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
957f6603c60Sopenharmony_ci    sigemptyset(&pending);
958f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGALRM);
959f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, &sigmask, nullptr);
960f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
961f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
962f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGINT);
963f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, &sigmask, nullptr);
964f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
965f6603c60Sopenharmony_ci
966f6603c60Sopenharmony_ci    LOG("add a new sig to block set");
967f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, nullptr, &oldmask); // check
968f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
969f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&oldmask, SIGALRM));
970f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&oldmask, SIGINT));
971f6603c60Sopenharmony_ci
972f6603c60Sopenharmony_ci    LOG("unblock a not-in-set sig");
973f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
974f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGTRAP);
975f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
976f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
977f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, nullptr, &oldmask); // check
978f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&oldmask, SIGALRM));
979f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&oldmask, SIGINT));
980f6603c60Sopenharmony_ci    EXPECT_EQ(0, sigismember(&oldmask, SIGTRAP));
981f6603c60Sopenharmony_ci
982f6603c60Sopenharmony_ci    LOG("set new block set, clear old one");
983f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_SETMASK, &sigmask, nullptr);
984f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
985f6603c60Sopenharmony_ci    rt = sigprocmask(10, nullptr, &oldmask); // check
986f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
987f6603c60Sopenharmony_ci    EXPECT_EQ(0, sigismember(&oldmask, SIGALRM));
988f6603c60Sopenharmony_ci    EXPECT_EQ(0, sigismember(&oldmask, SIGINT));
989f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigismember(&oldmask, SIGTRAP));
990f6603c60Sopenharmony_ci
991f6603c60Sopenharmony_ci    LOG("unblock git, clear the block set");
992f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
993f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
994f6603c60Sopenharmony_ci    rt = sigprocmask(100, nullptr, &oldmask); // check
995f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
996f6603c60Sopenharmony_ci    EXPECT_EQ(1, sigisemptyset(&oldmask));
997f6603c60Sopenharmony_ci}
998f6603c60Sopenharmony_ci/**
999f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPROCMASK_0400
1000f6603c60Sopenharmony_ci * @tc.name   sigprocmask function test: don't block signal that not in block-set with caught
1001f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1002f6603c60Sopenharmony_ci */
1003f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigprocmaskNotinSet, Function | MediumTest | Level2)
1004f6603c60Sopenharmony_ci{
1005f6603c60Sopenharmony_ci    LOG("Test: not-in-set signal could be caught.");
1006f6603c60Sopenharmony_ci    pid_t pid = fork();
1007f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1008f6603c60Sopenharmony_ci    if (pid == 0) { // child
1009f6603c60Sopenharmony_ci        sigset_t sigmask;
1010f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
1011f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGTRAP);
1012f6603c60Sopenharmony_ci        int rt = sigprocmask(SIG_BLOCK, &sigmask, nullptr);
1013f6603c60Sopenharmony_ci        if (rt != 0) {
1014f6603c60Sopenharmony_ci            LOG("sigprocmask fail, rt=%d, errno=%d", rt, errno);
1015f6603c60Sopenharmony_ci            exit(1);
1016f6603c60Sopenharmony_ci        }
1017f6603c60Sopenharmony_ci        signal(SIGINT, SignalHandler);
1018f6603c60Sopenharmony_ci        signal(SIGTRAP, SignalHandler);
1019f6603c60Sopenharmony_ci
1020f6603c60Sopenharmony_ci        Msleep(80);
1021f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGINT) {
1022f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected=SIGINT, actual=%d", mReceivedSignal);
1023f6603c60Sopenharmony_ci            exit(1);
1024f6603c60Sopenharmony_ci        }
1025f6603c60Sopenharmony_ci        exit(0);
1026f6603c60Sopenharmony_ci    } else { // parent
1027f6603c60Sopenharmony_ci        Msleep(30);
1028f6603c60Sopenharmony_ci        kill(pid, SIGINT);
1029f6603c60Sopenharmony_ci        Msleep(50);
1030f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
1031f6603c60Sopenharmony_ci    }
1032f6603c60Sopenharmony_ci}
1033f6603c60Sopenharmony_ci
1034f6603c60Sopenharmony_ci/**
1035f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGPROCMASK_0800
1036f6603c60Sopenharmony_ci * @tc.name   sigprocmask function test: don't block signal that not in block-set with terminate
1037f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1038f6603c60Sopenharmony_ci */
1039f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigprocmaskNotinSet0800, Function | MediumTest | Level2)
1040f6603c60Sopenharmony_ci{
1041f6603c60Sopenharmony_ci    LOG("Test: not-in-set signal could terminate the process.");
1042f6603c60Sopenharmony_ci    pid_t pid = fork();
1043f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1044f6603c60Sopenharmony_ci    if (pid == 0) { // child
1045f6603c60Sopenharmony_ci        sigset_t sigmask;
1046f6603c60Sopenharmony_ci        sigemptyset(&sigmask);
1047f6603c60Sopenharmony_ci        sigaddset(&sigmask, SIGTRAP);
1048f6603c60Sopenharmony_ci        int rt = sigprocmask(SIG_BLOCK, &sigmask, nullptr);
1049f6603c60Sopenharmony_ci        if (rt != 0) {
1050f6603c60Sopenharmony_ci            LOG("sigprocmask fail, rt=%d, errno=%d", rt, errno);
1051f6603c60Sopenharmony_ci            exit(1);
1052f6603c60Sopenharmony_ci        }
1053f6603c60Sopenharmony_ci        Msleep(60);
1054f6603c60Sopenharmony_ci        exit(0);
1055f6603c60Sopenharmony_ci    } else { // parent
1056f6603c60Sopenharmony_ci        Msleep(30);
1057f6603c60Sopenharmony_ci        kill(pid, SIGINT);
1058f6603c60Sopenharmony_ci        Msleep(30);
1059f6603c60Sopenharmony_ci        WaitProcKilled(pid, SIGINT);
1060f6603c60Sopenharmony_ci    }
1061f6603c60Sopenharmony_ci}
1062f6603c60Sopenharmony_ci
1063f6603c60Sopenharmony_ci/**
1064f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGSUSPEND_0100
1065f6603c60Sopenharmony_ci * @tc.name   sigsuspend basic function test1: the sigsuspend-signal terminates the process
1066f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1067f6603c60Sopenharmony_ci */
1068f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsuspendTerminate, Function | MediumTest | Level1)
1069f6603c60Sopenharmony_ci{
1070f6603c60Sopenharmony_ci    pid_t pid = fork();
1071f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1072f6603c60Sopenharmony_ci    if (pid == 0) { // child
1073f6603c60Sopenharmony_ci        signal(SIGTRAP, SignalHandler);
1074f6603c60Sopenharmony_ci        sigset_t set;
1075f6603c60Sopenharmony_ci        sigemptyset(&set);
1076f6603c60Sopenharmony_ci        sigaddset(&set, SIGHUP);
1077f6603c60Sopenharmony_ci        int rt = sigsuspend(&set);
1078f6603c60Sopenharmony_ci        // should not get here:
1079f6603c60Sopenharmony_ci        LOG("sigsuspend rt = %d, errno=%d", rt, errno);
1080f6603c60Sopenharmony_ci        exit(1);
1081f6603c60Sopenharmony_ci    } else { // parent
1082f6603c60Sopenharmony_ci        Msleep(20);
1083f6603c60Sopenharmony_ci
1084f6603c60Sopenharmony_ci        kill(pid, SIGHUP); // this should blocked by sigsuspend
1085f6603c60Sopenharmony_ci        Msleep(20);
1086f6603c60Sopenharmony_ci        AssertProcAlive(pid);
1087f6603c60Sopenharmony_ci
1088f6603c60Sopenharmony_ci        kill(pid, SIGTRAP); // this should interrupt sigsuspend
1089f6603c60Sopenharmony_ci        Msleep(100);
1090f6603c60Sopenharmony_ci        WaitProcKilled(pid, SIGHUP);
1091f6603c60Sopenharmony_ci    }
1092f6603c60Sopenharmony_ci}
1093f6603c60Sopenharmony_ci/**
1094f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGSUSPEND_0200
1095f6603c60Sopenharmony_ci * @tc.name   sigsuspend basic function test2: the sigsuspend-signal caught, and process continue run
1096f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1097f6603c60Sopenharmony_ci */
1098f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsuspendContinue, Function | MediumTest | Level2)
1099f6603c60Sopenharmony_ci{
1100f6603c60Sopenharmony_ci    pid_t pid = fork();
1101f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1102f6603c60Sopenharmony_ci    if (pid == 0) { // child
1103f6603c60Sopenharmony_ci        int exitCode = 0;
1104f6603c60Sopenharmony_ci        signal(SIGTRAP, SignalHandler);
1105f6603c60Sopenharmony_ci        signal(SIGHUP, SignalHandler);
1106f6603c60Sopenharmony_ci        sigset_t set;
1107f6603c60Sopenharmony_ci        sigemptyset(&set);
1108f6603c60Sopenharmony_ci        sigaddset(&set, SIGHUP);
1109f6603c60Sopenharmony_ci        int rt = sigsuspend(&set);
1110f6603c60Sopenharmony_ci        if (rt != -1) {
1111f6603c60Sopenharmony_ci            LOG("sigsuspend should return -1, but rt=%d", rt);
1112f6603c60Sopenharmony_ci            exitCode = 1;
1113f6603c60Sopenharmony_ci        }
1114f6603c60Sopenharmony_ci        if (errno != EINTR) {
1115f6603c60Sopenharmony_ci            LOG("sigsuspend should set errno to 4(EINTR),but get %d", errno);
1116f6603c60Sopenharmony_ci            exitCode = 1;
1117f6603c60Sopenharmony_ci        }
1118f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGHUP) {
1119f6603c60Sopenharmony_ci            LOG("Received signal check fail, expected signal=%d", SIGHUP);
1120f6603c60Sopenharmony_ci            exitCode = 1;
1121f6603c60Sopenharmony_ci        }
1122f6603c60Sopenharmony_ci        exit(exitCode);
1123f6603c60Sopenharmony_ci    } else { // parent
1124f6603c60Sopenharmony_ci        Msleep(20);
1125f6603c60Sopenharmony_ci
1126f6603c60Sopenharmony_ci        kill(pid, SIGHUP);
1127f6603c60Sopenharmony_ci        Msleep(20);
1128f6603c60Sopenharmony_ci        AssertProcAlive(pid);
1129f6603c60Sopenharmony_ci
1130f6603c60Sopenharmony_ci        kill(pid, SIGTRAP);
1131f6603c60Sopenharmony_ci        Msleep(100);
1132f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
1133f6603c60Sopenharmony_ci    }
1134f6603c60Sopenharmony_ci}
1135f6603c60Sopenharmony_ci
1136f6603c60Sopenharmony_ci/**
1137f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIGSUSPEND_0300
1138f6603c60Sopenharmony_ci * @tc.name   sigsuspend test with sigprocmask
1139f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1140f6603c60Sopenharmony_ci */
1141f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsuspendAndMask, Function | MediumTest | Level1)
1142f6603c60Sopenharmony_ci{
1143f6603c60Sopenharmony_ci    pid_t pid = fork();
1144f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1145f6603c60Sopenharmony_ci    if (pid == 0) { // child
1146f6603c60Sopenharmony_ci        int exitCode = 0;
1147f6603c60Sopenharmony_ci        sigset_t procMask, suspendMask;
1148f6603c60Sopenharmony_ci        sigemptyset(&suspendMask);
1149f6603c60Sopenharmony_ci        sigemptyset(&procMask);
1150f6603c60Sopenharmony_ci        sigaddset(&suspendMask, SIGQUIT);
1151f6603c60Sopenharmony_ci        sigaddset(&procMask, SIGINT);
1152f6603c60Sopenharmony_ci
1153f6603c60Sopenharmony_ci        signal(SIGINT, SignalHandler);
1154f6603c60Sopenharmony_ci        signal(SIGQUIT, SignalHandler);
1155f6603c60Sopenharmony_ci
1156f6603c60Sopenharmony_ci        LOG("Block SIGINT...");
1157f6603c60Sopenharmony_ci        int rt = sigprocmask(SIG_SETMASK, &procMask, nullptr);
1158f6603c60Sopenharmony_ci        if (rt != 0) {
1159f6603c60Sopenharmony_ci            LOG("sigprocmask fail, rt=%d, errno=%d", rt, errno);
1160f6603c60Sopenharmony_ci            exit(1);
1161f6603c60Sopenharmony_ci        }
1162f6603c60Sopenharmony_ci        LOG("Suspend SIGQUIT...");
1163f6603c60Sopenharmony_ci        rt = sigsuspend(&suspendMask);
1164f6603c60Sopenharmony_ci        if (rt != -1) {
1165f6603c60Sopenharmony_ci            LOG("sigsuspend should return -1, but rt=%d", rt);
1166f6603c60Sopenharmony_ci            exitCode = 1;
1167f6603c60Sopenharmony_ci        }
1168f6603c60Sopenharmony_ci        // signal in procmask should received 1st
1169f6603c60Sopenharmony_ci        // need change to check SIGQUIT only if multi-signal issue fixed
1170f6603c60Sopenharmony_ci        if ((mReceivedSignal != SIGQUIT) && (mReceivedSignal != SIGINT)) {
1171f6603c60Sopenharmony_ci            LOG("Received signal check fail, get %d", mReceivedSignal);
1172f6603c60Sopenharmony_ci            exitCode = 1;
1173f6603c60Sopenharmony_ci        }
1174f6603c60Sopenharmony_ci
1175f6603c60Sopenharmony_ci        LOG("Check current mask...");
1176f6603c60Sopenharmony_ci        sigemptyset(&procMask);
1177f6603c60Sopenharmony_ci        sigprocmask(10, nullptr, &procMask);
1178f6603c60Sopenharmony_ci        if (!sigismember(&procMask, SIGINT)) {
1179f6603c60Sopenharmony_ci            LOG("SIGINT should in block set!");
1180f6603c60Sopenharmony_ci            exitCode = 1;
1181f6603c60Sopenharmony_ci        }
1182f6603c60Sopenharmony_ci        if (sigismember(&procMask, SIGQUIT)) {
1183f6603c60Sopenharmony_ci            LOG("SIGQUIT should not in block set!");
1184f6603c60Sopenharmony_ci            exitCode = 1;
1185f6603c60Sopenharmony_ci        }
1186f6603c60Sopenharmony_ci        exit(exitCode);
1187f6603c60Sopenharmony_ci    } else { // parent
1188f6603c60Sopenharmony_ci        Msleep(20);
1189f6603c60Sopenharmony_ci        kill(pid, SIGQUIT);
1190f6603c60Sopenharmony_ci        Msleep(20);
1191f6603c60Sopenharmony_ci        AssertProcAlive(pid);
1192f6603c60Sopenharmony_ci
1193f6603c60Sopenharmony_ci        kill(pid, SIGINT); // this should interrupt sigsuspend
1194f6603c60Sopenharmony_ci        Msleep(100);
1195f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
1196f6603c60Sopenharmony_ci    }
1197f6603c60Sopenharmony_ci}
1198f6603c60Sopenharmony_ci
1199f6603c60Sopenharmony_ci/**
1200f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PTDKILL_0100
1201f6603c60Sopenharmony_ci * @tc.name   pthread_kill function test: basic
1202f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1203f6603c60Sopenharmony_ci */
1204f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPthreadkill, Function | MediumTest | Level1)
1205f6603c60Sopenharmony_ci{
1206f6603c60Sopenharmony_ci    pthread_t tid;
1207f6603c60Sopenharmony_ci    int sigNo = SIGXCPU;
1208f6603c60Sopenharmony_ci    int ret = pthread_create(&tid, NULL, ThreadFunc1, (void*)sigNo);
1209f6603c60Sopenharmony_ci    ASSERT_EQ(ret, 0) << "pthread_create failed, errno=" << ret;
1210f6603c60Sopenharmony_ci    Msleep(30);
1211f6603c60Sopenharmony_ci    ret = pthread_kill(tid, sigNo);
1212f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_kill failed, errno=" << ret;
1213f6603c60Sopenharmony_ci    ret = pthread_join(tid, NULL);
1214f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret;
1215f6603c60Sopenharmony_ci    EXPECT_EQ(mReceivedSignal, sigNo) << "Thread received signal check fail";
1216f6603c60Sopenharmony_ci    // restore handler
1217f6603c60Sopenharmony_ci    handler_type rt = signal(sigNo, SIG_DFL);
1218f6603c60Sopenharmony_ci    ASSERT_NE(rt, SIG_ERR) << "restore signal failed, errno=" << errno;
1219f6603c60Sopenharmony_ci}
1220f6603c60Sopenharmony_ci
1221f6603c60Sopenharmony_ci/**
1222f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PTDKILL_0200
1223f6603c60Sopenharmony_ci * @tc.name   pthread_kill function test: try kill main thread
1224f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1225f6603c60Sopenharmony_ci */
1226f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPthreadkillMain, Function | MediumTest | Level2)
1227f6603c60Sopenharmony_ci{
1228f6603c60Sopenharmony_ci    pid_t pid = fork();
1229f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1230f6603c60Sopenharmony_ci    if (pid == 0) { // child
1231f6603c60Sopenharmony_ci        pthread_t mainThread = pthread_self();
1232f6603c60Sopenharmony_ci        signal(SIGINT, SignalHandler);
1233f6603c60Sopenharmony_ci        pthread_t tid;
1234f6603c60Sopenharmony_ci        int ret = pthread_create(&tid, NULL, ThreadFunc2, &mainThread);
1235f6603c60Sopenharmony_ci        if (ret != 0) {
1236f6603c60Sopenharmony_ci            LOG("pthread_create failed, errno=%d", ret);
1237f6603c60Sopenharmony_ci            exit(1);
1238f6603c60Sopenharmony_ci        }
1239f6603c60Sopenharmony_ci
1240f6603c60Sopenharmony_ci        void* threadExitCode = nullptr;
1241f6603c60Sopenharmony_ci        ret = pthread_join(tid, &threadExitCode);
1242f6603c60Sopenharmony_ci        if (ret != 0) {
1243f6603c60Sopenharmony_ci            LOG("pthread_join failed, errno=%d", ret);
1244f6603c60Sopenharmony_ci            exit(1);
1245f6603c60Sopenharmony_ci        }
1246f6603c60Sopenharmony_ci        int c = (int)((uintptr_t)threadExitCode);
1247f6603c60Sopenharmony_ci        if (c != 0) {
1248f6603c60Sopenharmony_ci            LOG("sub thread exited failed, code=%d", c);
1249f6603c60Sopenharmony_ci            exit(1);
1250f6603c60Sopenharmony_ci        }
1251f6603c60Sopenharmony_ci
1252f6603c60Sopenharmony_ci        if (mReceivedSignal != SIGINT) {
1253f6603c60Sopenharmony_ci            LOG("SignalHandler check fail, expected signal:%d, actual:%d", SIGINT, mReceivedSignal);
1254f6603c60Sopenharmony_ci            exit(1);
1255f6603c60Sopenharmony_ci        }
1256f6603c60Sopenharmony_ci        exit(0);
1257f6603c60Sopenharmony_ci    } else { // parent
1258f6603c60Sopenharmony_ci        Msleep(100);
1259f6603c60Sopenharmony_ci        AssertProcExitedOK(pid);
1260f6603c60Sopenharmony_ci    }
1261f6603c60Sopenharmony_ci}
1262f6603c60Sopenharmony_ci
1263f6603c60Sopenharmony_ci/**
1264f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PTDSIGMASK_0100
1265f6603c60Sopenharmony_ci * @tc.name   pthread_sigmask function test: basic
1266f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1267f6603c60Sopenharmony_ci */
1268f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPthreadSigmask, Function | MediumTest | Level1)
1269f6603c60Sopenharmony_ci{
1270f6603c60Sopenharmony_ci    pthread_t tid;
1271f6603c60Sopenharmony_ci    int ret = pthread_create(&tid, NULL, ThreadFuncForSigmask1, (void*)1);
1272f6603c60Sopenharmony_ci    ASSERT_EQ(ret, 0) << "pthread_create failed, errno=" << ret;
1273f6603c60Sopenharmony_ci    ret = pthread_join(tid, NULL);
1274f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret;
1275f6603c60Sopenharmony_ci}
1276f6603c60Sopenharmony_ci
1277f6603c60Sopenharmony_ci/**
1278f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PTDSIGMASK_0200
1279f6603c60Sopenharmony_ci * @tc.name   pthread_sigmask function test: handler
1280f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1281f6603c60Sopenharmony_ci */
1282f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPthreadSigmaskHandler, Function | MediumTest | Level1)
1283f6603c60Sopenharmony_ci{
1284f6603c60Sopenharmony_ci    signal(SIGINT, SignalHandler);
1285f6603c60Sopenharmony_ci    pthread_t tid;
1286f6603c60Sopenharmony_ci    int ret = pthread_create(&tid, NULL, ThreadFuncForSigmask1, (void*)2);
1287f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_create failed, errno=" << ret;
1288f6603c60Sopenharmony_ci
1289f6603c60Sopenharmony_ci    ret = pthread_join(tid, NULL);
1290f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret;
1291f6603c60Sopenharmony_ci
1292f6603c60Sopenharmony_ci    signal(SIGINT, SIG_DFL);
1293f6603c60Sopenharmony_ci}
1294f6603c60Sopenharmony_ci
1295f6603c60Sopenharmony_ci/**
1296f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PTDSIGMASK_0300
1297f6603c60Sopenharmony_ci * @tc.name   test that the signal-mask should inherited from the creator
1298f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1299f6603c60Sopenharmony_ci */
1300f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPthreadSigmaskInherit, Function | MediumTest | Level3)
1301f6603c60Sopenharmony_ci{
1302f6603c60Sopenharmony_ci    sigset_t sigmask, oldmask;
1303f6603c60Sopenharmony_ci    sigemptyset(&sigmask);
1304f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
1305f6603c60Sopenharmony_ci
1306f6603c60Sopenharmony_ci    LOG("add SIGUSR1 to block set");
1307f6603c60Sopenharmony_ci    sigaddset(&sigmask, SIGUSR1);
1308f6603c60Sopenharmony_ci    int rt = sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
1309f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1310f6603c60Sopenharmony_ci
1311f6603c60Sopenharmony_ci    pthread_t tid;
1312f6603c60Sopenharmony_ci    int ret = pthread_create(&tid, NULL, ThreadFuncForSigmask2, NULL);
1313f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_create failed, errno=" << ret;
1314f6603c60Sopenharmony_ci
1315f6603c60Sopenharmony_ci    void* threadExitCode = nullptr;
1316f6603c60Sopenharmony_ci    ret = pthread_join(tid, &threadExitCode);
1317f6603c60Sopenharmony_ci    EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret;
1318f6603c60Sopenharmony_ci    int code = (int)((uintptr_t)threadExitCode);
1319f6603c60Sopenharmony_ci    EXPECT_EQ(code, 0);
1320f6603c60Sopenharmony_ci
1321f6603c60Sopenharmony_ci    LOG("check the block set is not effected");
1322f6603c60Sopenharmony_ci    sigprocmask(SIG_UNBLOCK, nullptr, &oldmask);
1323f6603c60Sopenharmony_ci
1324f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&oldmask, SIGINT), 0) << "SIGINT should not in block set!";
1325f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&oldmask, SIGUSR1), 1) << "SIGUSR1 should still in block set!";
1326f6603c60Sopenharmony_ci
1327f6603c60Sopenharmony_ci    LOG("restore default config");
1328f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &sigmask, nullptr);
1329f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1330f6603c60Sopenharmony_ci}
1331f6603c60Sopenharmony_ci
1332f6603c60Sopenharmony_ci/**
1333f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0100
1334f6603c60Sopenharmony_ci * @tc.name   test sig set operator APIs:sigemptyset,sigaddset,sigisemptyset,sigismember
1335f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1336f6603c60Sopenharmony_ci */
1337f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetBasic, Function | MediumTest | Level2)
1338f6603c60Sopenharmony_ci{
1339f6603c60Sopenharmony_ci    sigset_t set;
1340f6603c60Sopenharmony_ci    int rt = sigemptyset(&set);
1341f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigandset failed";
1342f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&set), 1) << "set should empty";
1343f6603c60Sopenharmony_ci
1344f6603c60Sopenharmony_ci    rt = sigaddset(&set, SIGINT);
1345f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigaddset failed, errno=" << errno;
1346f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGINT), 1) << "SIGINT should in set";
1347f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 0) << "SIGQUIT should not in set";
1348f6603c60Sopenharmony_ci
1349f6603c60Sopenharmony_ci    rt = sigaddset(&set, SIGQUIT);
1350f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigaddset failed, errno=" << errno;
1351f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 1) << "SIGQUIT should in set";
1352f6603c60Sopenharmony_ci
1353f6603c60Sopenharmony_ci    rt = sigaddset(&set, -1);
1354f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1355f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1356f6603c60Sopenharmony_ci
1357f6603c60Sopenharmony_ci    rt = sigaddset(&set, MAX_SIGNAL_NUMBER + 1);
1358f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1359f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1360f6603c60Sopenharmony_ci
1361f6603c60Sopenharmony_ci    LOG("check that set not changed after failed-sigadd");
1362f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGINT), 1) << "SIGINT should in set";
1363f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 1) << "SIGQUIT should in set";
1364f6603c60Sopenharmony_ci
1365f6603c60Sopenharmony_ci    sigemptyset(&set);
1366f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&set), 1) << "set should empty";
1367f6603c60Sopenharmony_ci}
1368f6603c60Sopenharmony_ci
1369f6603c60Sopenharmony_ci/**
1370f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0200
1371f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigandset
1372f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1373f6603c60Sopenharmony_ci */
1374f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetAnd, Function | MediumTest | Level2)
1375f6603c60Sopenharmony_ci{
1376f6603c60Sopenharmony_ci    sigset_t set1, set2, setAnd;
1377f6603c60Sopenharmony_ci    sigemptyset(&set1);
1378f6603c60Sopenharmony_ci    sigemptyset(&set2);
1379f6603c60Sopenharmony_ci    sigemptyset(&setAnd);
1380f6603c60Sopenharmony_ci    int rt;
1381f6603c60Sopenharmony_ci
1382f6603c60Sopenharmony_ci    LOG("test and-set with empty set:");
1383f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1384f6603c60Sopenharmony_ci    rt = sigandset(&setAnd, &set1, &set2);
1385f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigandset failed, errno=" << errno;
1386f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&setAnd), 1) << "setAnd should empty";
1387f6603c60Sopenharmony_ci}
1388f6603c60Sopenharmony_ci
1389f6603c60Sopenharmony_ci/**
1390f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0300
1391f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigorset
1392f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1393f6603c60Sopenharmony_ci */
1394f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetOr, Function | MediumTest | Level2)
1395f6603c60Sopenharmony_ci{
1396f6603c60Sopenharmony_ci    sigset_t set1, set2, setOr;
1397f6603c60Sopenharmony_ci    sigemptyset(&set1);
1398f6603c60Sopenharmony_ci    sigemptyset(&set2);
1399f6603c60Sopenharmony_ci    sigemptyset(&setOr);
1400f6603c60Sopenharmony_ci    int rt;
1401f6603c60Sopenharmony_ci
1402f6603c60Sopenharmony_ci    LOG("test or-set with empty set:");
1403f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1404f6603c60Sopenharmony_ci    rt = sigorset(&setOr, &set1, &set2);
1405f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigorset failed, errno=" << errno;
1406f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGINT), 1) << "SIGINT should in setOr";
1407f6603c60Sopenharmony_ci}
1408f6603c60Sopenharmony_ci
1409f6603c60Sopenharmony_ci/**
1410f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0400
1411f6603c60Sopenharmony_ci * @tc.name   test sig set operator APIs: sigdelset
1412f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1413f6603c60Sopenharmony_ci */
1414f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetDelete, Function | MediumTest | Level2)
1415f6603c60Sopenharmony_ci{
1416f6603c60Sopenharmony_ci    sigset_t set;
1417f6603c60Sopenharmony_ci    sigemptyset(&set);
1418f6603c60Sopenharmony_ci    sigaddset(&set, SIGINT);
1419f6603c60Sopenharmony_ci
1420f6603c60Sopenharmony_ci    LOG("delete in-set sig:");
1421f6603c60Sopenharmony_ci    int rt = sigdelset(&set, SIGINT);
1422f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigdelset failed, errno=" << errno;
1423f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&set), 1);
1424f6603c60Sopenharmony_ci
1425f6603c60Sopenharmony_ci    sigaddset(&set, SIGINT);
1426f6603c60Sopenharmony_ci    sigaddset(&set, SIGQUIT);
1427f6603c60Sopenharmony_ci    rt = sigdelset(&set, SIGINT);
1428f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigdelset failed, errno=" << errno;
1429f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 1) << "SIGQUIT should in set";
1430f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGINT), 0) << "SIGINT should not in set";
1431f6603c60Sopenharmony_ci}
1432f6603c60Sopenharmony_ci
1433f6603c60Sopenharmony_ci/**
1434f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0500
1435f6603c60Sopenharmony_ci * @tc.name   test sig set operator APIs: sigfillset
1436f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1437f6603c60Sopenharmony_ci */
1438f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetFill, Function | MediumTest | Level2)
1439f6603c60Sopenharmony_ci{
1440f6603c60Sopenharmony_ci    sigset_t set;
1441f6603c60Sopenharmony_ci    sigemptyset(&set);
1442f6603c60Sopenharmony_ci    sigaddset(&set, SIGINT);
1443f6603c60Sopenharmony_ci
1444f6603c60Sopenharmony_ci    int rt = sigfillset(&set);
1445f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigfillset failed, errno=" << errno;
1446f6603c60Sopenharmony_ci    for (int i = 1; i <= MAX_SIGNAL; i++) {
1447f6603c60Sopenharmony_ci        EXPECT_EQ(sigismember(&set, i), 1) << i << " not in set!";
1448f6603c60Sopenharmony_ci    }
1449f6603c60Sopenharmony_ci
1450f6603c60Sopenharmony_ci    sigdelset(&set, SIGINT);
1451f6603c60Sopenharmony_ci    for (int i = 1; i <= MAX_SIGNAL; i++) {
1452f6603c60Sopenharmony_ci        if (i == SIGINT) {
1453f6603c60Sopenharmony_ci            EXPECT_EQ(sigismember(&set, i), 0) << "SIGINT should not in set!";
1454f6603c60Sopenharmony_ci        } else {
1455f6603c60Sopenharmony_ci            EXPECT_EQ(sigismember(&set, i), 1) << i << " not in set!";
1456f6603c60Sopenharmony_ci        }
1457f6603c60Sopenharmony_ci    }
1458f6603c60Sopenharmony_ci}
1459f6603c60Sopenharmony_ci
1460f6603c60Sopenharmony_ci/**
1461f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0600
1462f6603c60Sopenharmony_ci * @tc.name   test sigdelset operator APIs: sigdelset
1463f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1464f6603c60Sopenharmony_ci */
1465f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetDelete0600, Function | MediumTest | Level2)
1466f6603c60Sopenharmony_ci{
1467f6603c60Sopenharmony_ci    int rt;
1468f6603c60Sopenharmony_ci    sigset_t set;
1469f6603c60Sopenharmony_ci    sigemptyset(&set);
1470f6603c60Sopenharmony_ci    sigaddset(&set, SIGINT);
1471f6603c60Sopenharmony_ci
1472f6603c60Sopenharmony_ci    LOG("delete not-in-set sig:");
1473f6603c60Sopenharmony_ci    sigemptyset(&set);
1474f6603c60Sopenharmony_ci    sigaddset(&set, SIGQUIT);
1475f6603c60Sopenharmony_ci    rt = sigdelset(&set, SIGINT);
1476f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigdelset failed, errno=" << errno;
1477f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 1) << "SIGQUIT should in set";
1478f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGINT), 0) << "SIGINT should not in set";
1479f6603c60Sopenharmony_ci}
1480f6603c60Sopenharmony_ci
1481f6603c60Sopenharmony_ci/**
1482f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_0700
1483f6603c60Sopenharmony_ci * @tc.name   test sig set operator APIs: sigdelset with invalid
1484f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1485f6603c60Sopenharmony_ci */
1486f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetDelete0700, Function | MediumTest | Level2)
1487f6603c60Sopenharmony_ci{
1488f6603c60Sopenharmony_ci    int rt;
1489f6603c60Sopenharmony_ci    sigset_t set;
1490f6603c60Sopenharmony_ci    sigemptyset(&set);
1491f6603c60Sopenharmony_ci    sigaddset(&set, SIGINT);
1492f6603c60Sopenharmony_ci
1493f6603c60Sopenharmony_ci    LOG("delete invalid sig:");
1494f6603c60Sopenharmony_ci    rt = sigdelset(&set, -1);
1495f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1496f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1497f6603c60Sopenharmony_ci
1498f6603c60Sopenharmony_ci    rt = sigdelset(&set, MAX_SIGNAL_NUMBER + 1);
1499f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1500f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1501f6603c60Sopenharmony_ci
1502f6603c60Sopenharmony_ci    LOG("check that set not changed after failed-delete");
1503f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGQUIT), 0) << "SIGQUIT should in set";
1504f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&set, SIGINT), 1) << "SIGINT should not in set";
1505f6603c60Sopenharmony_ci}
1506f6603c60Sopenharmony_ci
1507f6603c60Sopenharmony_ci/**
1508f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_1000
1509f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigorset with non-overlap
1510f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1511f6603c60Sopenharmony_ci */
1512f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetOr1000, Function | MediumTest | Level2)
1513f6603c60Sopenharmony_ci{
1514f6603c60Sopenharmony_ci    sigset_t set1, set2, setOr;
1515f6603c60Sopenharmony_ci    sigemptyset(&set1);
1516f6603c60Sopenharmony_ci    sigemptyset(&set2);
1517f6603c60Sopenharmony_ci    sigemptyset(&setOr);
1518f6603c60Sopenharmony_ci    int rt;
1519f6603c60Sopenharmony_ci
1520f6603c60Sopenharmony_ci    LOG("test or-set with non-overlap sets:");
1521f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1522f6603c60Sopenharmony_ci    sigaddset(&set2, SIGABRT);
1523f6603c60Sopenharmony_ci    rt = sigorset(&setOr, &set1, &set2);
1524f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigorset failed, errno=" << errno;
1525f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGINT), 1) << "SIGINT should in setOr";
1526f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGABRT), 1) << "SIGABRT should in setOr";
1527f6603c60Sopenharmony_ci}
1528f6603c60Sopenharmony_ci
1529f6603c60Sopenharmony_ci/**
1530f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_1100
1531f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigorset with overlapping
1532f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1533f6603c60Sopenharmony_ci */
1534f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetOr1100, Function | MediumTest | Level2)
1535f6603c60Sopenharmony_ci{
1536f6603c60Sopenharmony_ci    sigset_t set1, set2, setOr;
1537f6603c60Sopenharmony_ci    sigemptyset(&set1);
1538f6603c60Sopenharmony_ci    sigemptyset(&set2);
1539f6603c60Sopenharmony_ci    sigemptyset(&setOr);
1540f6603c60Sopenharmony_ci    int rt;
1541f6603c60Sopenharmony_ci
1542f6603c60Sopenharmony_ci    LOG("test or-set with overlapping sets:");
1543f6603c60Sopenharmony_ci    sigaddset(&setOr, SIGHUP);  // original siganl in setOr
1544f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1545f6603c60Sopenharmony_ci    sigaddset(&set2, SIGABRT);
1546f6603c60Sopenharmony_ci    sigaddset(&set1, SIGQUIT);
1547f6603c60Sopenharmony_ci    sigaddset(&set2, SIGQUIT);
1548f6603c60Sopenharmony_ci    rt = sigorset(&setOr, &set1, &set2);
1549f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigorset failed, errno=" << errno;
1550f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGINT), 1) << "SIGINT should in setOr";
1551f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGABRT), 1) << "SIGABRT should in setOr";
1552f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGQUIT), 1) << "SIGQUIT should in setOr";
1553f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setOr, SIGHUP), 0) << "original siganl in setOr should deleted";
1554f6603c60Sopenharmony_ci}
1555f6603c60Sopenharmony_ci
1556f6603c60Sopenharmony_ci
1557f6603c60Sopenharmony_ci/**
1558f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_1200
1559f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigandset with non-overlap
1560f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1561f6603c60Sopenharmony_ci */
1562f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetAnd1200, Function | MediumTest | Level2)
1563f6603c60Sopenharmony_ci{
1564f6603c60Sopenharmony_ci    sigset_t set1, set2, setAnd;
1565f6603c60Sopenharmony_ci    sigemptyset(&set1);
1566f6603c60Sopenharmony_ci    sigemptyset(&set2);
1567f6603c60Sopenharmony_ci    sigemptyset(&setAnd);
1568f6603c60Sopenharmony_ci    int rt;
1569f6603c60Sopenharmony_ci
1570f6603c60Sopenharmony_ci    LOG("test and-set with non-overlap sets:");
1571f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1572f6603c60Sopenharmony_ci    sigaddset(&set2, SIGABRT);
1573f6603c60Sopenharmony_ci    rt = sigandset(&setAnd, &set1, &set2);
1574f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigandset failed, errno=" << errno;
1575f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&setAnd), 1) << "setAnd should empty";
1576f6603c60Sopenharmony_ci}
1577f6603c60Sopenharmony_ci
1578f6603c60Sopenharmony_ci/**
1579f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_1300
1580f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigandset with overlapping
1581f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1582f6603c60Sopenharmony_ci */
1583f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetAnd1300, Function | MediumTest | Level2)
1584f6603c60Sopenharmony_ci{
1585f6603c60Sopenharmony_ci    sigset_t set1, set2, setAnd;
1586f6603c60Sopenharmony_ci    sigemptyset(&set1);
1587f6603c60Sopenharmony_ci    sigemptyset(&set2);
1588f6603c60Sopenharmony_ci    sigemptyset(&setAnd);
1589f6603c60Sopenharmony_ci    int rt;
1590f6603c60Sopenharmony_ci
1591f6603c60Sopenharmony_ci    LOG("test and-set with overlapping sets:");
1592f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1593f6603c60Sopenharmony_ci    sigaddset(&set2, SIGABRT);
1594f6603c60Sopenharmony_ci    sigaddset(&set1, SIGQUIT);
1595f6603c60Sopenharmony_ci    sigaddset(&set2, SIGQUIT);
1596f6603c60Sopenharmony_ci    sigaddset(&setAnd, SIGHUP);  // original siganl in setAnd
1597f6603c60Sopenharmony_ci    rt = sigandset(&setAnd, &set1, &set2);
1598f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigandset failed, errno=" << errno;
1599f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGQUIT), 1) << "SIGQUIT should in setAnd";
1600f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGINT), 0) << "SIGINT should not in setAnd";
1601f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGABRT), 0) << "SIGABRT should not in setAnd";
1602f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGHUP), 0) << "original siganl in setAnd should deleted";
1603f6603c60Sopenharmony_ci}
1604f6603c60Sopenharmony_ci
1605f6603c60Sopenharmony_ci/**
1606f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SETOP_1400
1607f6603c60Sopenharmony_ci * @tc.name   test sig set operator API: sigandset with multi-overlapping
1608f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1609f6603c60Sopenharmony_ci */
1610f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigsetAnd1400, Function | MediumTest | Level2)
1611f6603c60Sopenharmony_ci{
1612f6603c60Sopenharmony_ci    sigset_t set1, set2, setAnd;
1613f6603c60Sopenharmony_ci    sigemptyset(&set1);
1614f6603c60Sopenharmony_ci    sigemptyset(&set2);
1615f6603c60Sopenharmony_ci    sigemptyset(&setAnd);
1616f6603c60Sopenharmony_ci    int rt;
1617f6603c60Sopenharmony_ci    LOG("test and-set with multi-overlapping sets:");
1618f6603c60Sopenharmony_ci    sigaddset(&set1, SIGINT);
1619f6603c60Sopenharmony_ci    sigaddset(&set2, SIGABRT);
1620f6603c60Sopenharmony_ci    sigaddset(&set1, SIGQUIT);
1621f6603c60Sopenharmony_ci    sigaddset(&set2, SIGQUIT);
1622f6603c60Sopenharmony_ci    sigaddset(&setAnd, SIGHUP);  // original siganl in setAnd
1623f6603c60Sopenharmony_ci    sigaddset(&set1, SIGUSR1);
1624f6603c60Sopenharmony_ci    sigaddset(&set2, SIGUSR1);
1625f6603c60Sopenharmony_ci    rt = sigandset(&setAnd, &set1, &set2);
1626f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "sigandset failed, errno=" << errno;
1627f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGQUIT), 1) << "SIGQUIT should in setAnd";
1628f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGUSR1), 1) << "SIGUSR1 should in setAnd";
1629f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGINT), 0) << "SIGINT should not in setAnd";
1630f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&setAnd, SIGABRT), 0) << "SIGABRT should not in setAnd";
1631f6603c60Sopenharmony_ci}
1632f6603c60Sopenharmony_ci
1633f6603c60Sopenharmony_ci/**
1634f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SIGHOLD_0100
1635f6603c60Sopenharmony_ci * @tc.name   sighold function test
1636f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1637f6603c60Sopenharmony_ci */
1638f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigHold, Function | MediumTest | Level2)
1639f6603c60Sopenharmony_ci{
1640f6603c60Sopenharmony_ci    int sigNo = SIGINT;
1641f6603c60Sopenharmony_ci    int rt = sighold(sigNo);
1642f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "sighold failed, errno=" << errno;
1643f6603c60Sopenharmony_ci
1644f6603c60Sopenharmony_ci    LOG("check that the signal is added to the mask");
1645f6603c60Sopenharmony_ci    sigset_t oldmask;
1646f6603c60Sopenharmony_ci    sigemptyset(&oldmask);
1647f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, nullptr, &oldmask);
1648f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1649f6603c60Sopenharmony_ci    EXPECT_EQ(sigismember(&oldmask, sigNo), 1);
1650f6603c60Sopenharmony_ci
1651f6603c60Sopenharmony_ci    // restore default config
1652f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_UNBLOCK, &oldmask, nullptr);
1653f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1654f6603c60Sopenharmony_ci}
1655f6603c60Sopenharmony_ci
1656f6603c60Sopenharmony_ci/**
1657f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SIGHOLD_0200
1658f6603c60Sopenharmony_ci * @tc.name   sighold function error test
1659f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1660f6603c60Sopenharmony_ci */
1661f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigHoldError, Function | MediumTest | Level2)
1662f6603c60Sopenharmony_ci{
1663f6603c60Sopenharmony_ci    int rt = sighold(-1);
1664f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1665f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1666f6603c60Sopenharmony_ci
1667f6603c60Sopenharmony_ci    rt = sighold(MAX_SIGNAL_NUMBER + 1);
1668f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1669f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1670f6603c60Sopenharmony_ci}
1671f6603c60Sopenharmony_ci
1672f6603c60Sopenharmony_ci/**
1673f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SIGRELSE_0100
1674f6603c60Sopenharmony_ci * @tc.name   sigrelse function test
1675f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1676f6603c60Sopenharmony_ci */
1677f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigRelse, Function | MediumTest | Level2)
1678f6603c60Sopenharmony_ci{
1679f6603c60Sopenharmony_ci    LOG("add sig to mask");
1680f6603c60Sopenharmony_ci    sigset_t mask;
1681f6603c60Sopenharmony_ci    sigemptyset(&mask);
1682f6603c60Sopenharmony_ci    sigaddset(&mask, SIGINT);
1683f6603c60Sopenharmony_ci    int rt = sigprocmask(SIG_BLOCK, &mask, nullptr);
1684f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
1685f6603c60Sopenharmony_ci
1686f6603c60Sopenharmony_ci    LOG("delete signal from the mask");
1687f6603c60Sopenharmony_ci    rt = sigrelse(SIGINT);
1688f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1689f6603c60Sopenharmony_ci
1690f6603c60Sopenharmony_ci    LOG("check that the signal is deleted");
1691f6603c60Sopenharmony_ci    sigemptyset(&mask);
1692f6603c60Sopenharmony_ci    rt = sigprocmask(SIG_BLOCK, nullptr, &mask);
1693f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
1694f6603c60Sopenharmony_ci    EXPECT_EQ(sigisemptyset(&mask), 1);
1695f6603c60Sopenharmony_ci}
1696f6603c60Sopenharmony_ci
1697f6603c60Sopenharmony_ci/**
1698f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_SIGRELSE_0200
1699f6603c60Sopenharmony_ci * @tc.name   sigrelse function error test
1700f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1701f6603c60Sopenharmony_ci */
1702f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testSigRelseError, Function | MediumTest | Level2)
1703f6603c60Sopenharmony_ci{
1704f6603c60Sopenharmony_ci    int rt = sigrelse(-1);
1705f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1706f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1707f6603c60Sopenharmony_ci
1708f6603c60Sopenharmony_ci    rt = sigrelse(MAX_SIGNAL_NUMBER + 1);
1709f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
1710f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1711f6603c60Sopenharmony_ci}
1712f6603c60Sopenharmony_ci
1713f6603c60Sopenharmony_ci/**
1714f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PSIGNAL_0100
1715f6603c60Sopenharmony_ci * @tc.name   psignal function test
1716f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1717f6603c60Sopenharmony_ci */
1718f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPsignal, Function | MediumTest | Level2)
1719f6603c60Sopenharmony_ci{
1720f6603c60Sopenharmony_ci    // we use child to do the test here, cause 'restore of stderr' is not work by now.
1721f6603c60Sopenharmony_ci    pid_t pid = fork();
1722f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1723f6603c60Sopenharmony_ci    if (pid == 0) { // child
1724f6603c60Sopenharmony_ci        // redirect stderr to a file, so we can check the content.
1725f6603c60Sopenharmony_ci        char* outfile = WRITABLE_TEST_DIR "stderr.txt";
1726f6603c60Sopenharmony_ci        if (freopen(outfile, "w", stderr) == NULL) {
1727f6603c60Sopenharmony_ci            LOG("redirect stderr fail, freopen errno=%d\n", errno);
1728f6603c60Sopenharmony_ci            exit(1);
1729f6603c60Sopenharmony_ci        }
1730f6603c60Sopenharmony_ci        psignal(SIGFPE, "SIGFPE");
1731f6603c60Sopenharmony_ci        int rt = CheckSigString(outfile, "SIGFPE: Arithmetic exception");
1732f6603c60Sopenharmony_ci        exit(rt);
1733f6603c60Sopenharmony_ci    } else { // parent
1734f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
1735f6603c60Sopenharmony_ci    }
1736f6603c60Sopenharmony_ci}
1737f6603c60Sopenharmony_ci
1738f6603c60Sopenharmony_ci/**
1739f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PSIGNAL_0200
1740f6603c60Sopenharmony_ci * @tc.name   psignal function error test1
1741f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1742f6603c60Sopenharmony_ci */
1743f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPsignalError1, Function | MediumTest | Level3)
1744f6603c60Sopenharmony_ci{
1745f6603c60Sopenharmony_ci    pid_t pid = fork();
1746f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1747f6603c60Sopenharmony_ci    if (pid == 0) { // child
1748f6603c60Sopenharmony_ci        char* outfile = WRITABLE_TEST_DIR "stderr.txt";
1749f6603c60Sopenharmony_ci        if (freopen(outfile, "w", stderr) == NULL) {
1750f6603c60Sopenharmony_ci            LOG("redirect stderr fail, freopen errno=%d\n", errno);
1751f6603c60Sopenharmony_ci            exit(1);
1752f6603c60Sopenharmony_ci        }
1753f6603c60Sopenharmony_ci        psignal(-1, "===");
1754f6603c60Sopenharmony_ci        int rt = CheckSigString(outfile, "===: Unknown signal");
1755f6603c60Sopenharmony_ci        exit(rt);
1756f6603c60Sopenharmony_ci    } else { // parent
1757f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
1758f6603c60Sopenharmony_ci    }
1759f6603c60Sopenharmony_ci}
1760f6603c60Sopenharmony_ci
1761f6603c60Sopenharmony_ci/**
1762f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_SIG_PSIGNAL_0300
1763f6603c60Sopenharmony_ci * @tc.name   psignal function error test2
1764f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
1765f6603c60Sopenharmony_ci */
1766f6603c60Sopenharmony_ciHWTEST_F(IpcSignalTest, testPsignalError2, Function | MediumTest | Level3)
1767f6603c60Sopenharmony_ci{
1768f6603c60Sopenharmony_ci    pid_t pid = fork();
1769f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1770f6603c60Sopenharmony_ci    if (pid == 0) { // child
1771f6603c60Sopenharmony_ci        char* outfile = WRITABLE_TEST_DIR "stderr.txt";
1772f6603c60Sopenharmony_ci        if (freopen(outfile, "w", stderr) == NULL) {
1773f6603c60Sopenharmony_ci            LOG("redirect stderr fail, freopen errno=%d\n", errno);
1774f6603c60Sopenharmony_ci            exit(1);
1775f6603c60Sopenharmony_ci        }
1776f6603c60Sopenharmony_ci        psignal(MAX_SIGNAL_NUMBER + 1, "***");
1777f6603c60Sopenharmony_ci        int rt = CheckSigString(outfile, "***: Unknown signal");
1778f6603c60Sopenharmony_ci        exit(rt);
1779f6603c60Sopenharmony_ci    } else { // parent
1780f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
1781f6603c60Sopenharmony_ci    }
1782f6603c60Sopenharmony_ci}
1783