• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_signal.h"
32 #include "signal.h"
33 
34 static int g_sigCount = 0;
35 static int g_sigCount1 = 0;
SigPrint(int sig)36 static void SigPrint(int sig)
37 {
38     g_sigCount++;
39     printf("signal receive success\n");
40 }
41 
SigPrint1(int sig)42 static void SigPrint1(int sig)
43 {
44     g_sigCount1++;
45     printf("signal receive success\n");
46 }
47 
TestCase(VOID)48 static UINT32 TestCase(VOID)
49 {
50     int pid = 0;
51     int ret = 0;
52     void (*retptr)(int);
53     sigset_t newset, oldset;
54 
55     pid = fork();
56     if (pid < 0) {
57         printf("Fork error\n");
58         return LOS_NOK;
59     } else if (pid == 0) {
60         printf("test032 raise before\n");
61         sigemptyset(&newset);
62         sigaddset(&newset, SIGUSR1);
63         sigaddset(&newset, SIGUSR2);
64         ret = sigprocmask(SIG_BLOCK, &newset, &oldset);
65         if (ret != 0) {
66             printf("error:sigprocmask\n");
67             exit(LOS_NOK);
68         }
69         retptr = signal(SIGUSR1, SigPrint);
70         if (retptr == SIG_ERR) {
71             printf("error:signal\n");
72             exit(LOS_NOK);
73         }
74         retptr = signal(SIGUSR2, SigPrint1);
75         if (retptr == SIG_ERR) {
76             printf("error:signal\n");
77             exit(LOS_NOK);
78         }
79         raise(SIGUSR1);
80         raise(SIGUSR2);
81         sigdelset(&newset, SIGUSR2);
82         ret = sigprocmask(SIG_UNBLOCK, &newset, &oldset);
83         if (ret != 0) {
84             printf("error:sigprocmask\n");
85             exit(LOS_NOK);
86         }
87         sleep(1);
88         if (!(g_sigCount == 1 && g_sigCount1 == 0)) {
89             printf("Assert failed in line %d\n", __LINE__);
90             printf("g_sigCount = %d, g_sigCount1 = %d\n", g_sigCount, g_sigCount1);
91             exit(LOS_NOK);
92         }
93         sigdelset(&newset, SIGUSR1);
94         sigaddset(&newset, SIGUSR2);
95         ret = sigprocmask(SIG_UNBLOCK, &newset, &oldset);
96         if (ret != 0) {
97             printf("error:sigprocmask\n");
98             exit(LOS_NOK);
99         }
100         sleep(1);
101         if (!(g_sigCount == 1 && g_sigCount1 == 1)) {
102             printf("Assert failed in line %d\n", __LINE__);
103             printf("g_sigCount = %d, g_sigCount1 = %d\n", g_sigCount, g_sigCount1);
104             exit(LOS_NOK);
105         }
106         printf("test032 raise after\n");
107         exit(LOS_OK);
108     }
109 
110     wait(&ret);
111     ICUNIT_ASSERT_EQUAL(WEXITSTATUS(ret), LOS_OK, WEXITSTATUS(ret));
112     return LOS_OK;
113 }
114 
ItPosixSignal033(void)115 void ItPosixSignal033(void)
116 {
117     TEST_ADD_CASE(__FUNCTION__, TestCase, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
118 }