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
SigPrint(int sig)34 static void SigPrint(int sig)
35 {
36 (void)sig;
37 printf("%s%d\n", __FUNCTION__, __LINE__);
38 return;
39 }
40
SigPrint1(int sig)41 static void SigPrint1(int sig)
42 {
43 (void)sig;
44 printf("%s%d\n", __FUNCTION__, __LINE__);
45 return;
46 }
47
48 static int g_sigCount = 0;
SigPrint2(int sig)49 static void SigPrint2(int sig)
50 {
51 (void)sig;
52 g_sigCount = 1;
53 printf("%s, count = %d\n", __FUNCTION__, g_sigCount);
54 return;
55 }
56
ThreadSetFunc2(void * arg)57 static void *ThreadSetFunc2(void *arg)
58 {
59 (void)arg;
60 void (*retSig)(int);
61 retSig = signal(SIGALRM, SigPrint2);
62 ICUNIT_GOTO_NOT_EQUAL(retSig, SIG_ERR, retSig, EXIT);
63 pthread_exit((void *)NULL);
64 return NULL;
65 EXIT:
66 pthread_exit(reinterpret_cast<void *>(-1));
67 return reinterpret_cast<void *>(-1);
68 }
69
ThreadSetDfl(void * arg)70 static void *ThreadSetDfl(void *arg)
71 {
72 (void)arg;
73 void (*retSig)(int);
74 retSig = signal(SIGALRM, SIG_DFL);
75 ICUNIT_GOTO_NOT_EQUAL(retSig, SIG_ERR, retSig, EXIT);
76 pthread_exit((void *)NULL);
77 return NULL;
78 EXIT:
79 pthread_exit(reinterpret_cast<void *>(-1));
80 return reinterpret_cast<void *>(-1);
81 }
82
ThreadKill(void * arg)83 static void *ThreadKill(void *arg)
84 {
85 (void)arg;
86 int retValue;
87
88 retValue = raise(SIGALRM);
89 ICUNIT_GOTO_EQUAL(retValue, 0, retValue, EXIT);
90 pthread_exit((void *)NULL);
91 return NULL;
92 EXIT:
93 pthread_exit(reinterpret_cast<void *>(-1));
94 return reinterpret_cast<void *>(-1);
95 }
96
TestSigMultiPthread(void)97 static int TestSigMultiPthread(void)
98 {
99 int fpid;
100 int status;
101 int *status1 = nullptr;
102 int ret;
103 int count;
104 pthread_t thread, thread1, thread2;
105
106 fpid = fork();
107 ICUNIT_ASSERT_WITHIN_EQUAL(fpid, 0, UINT_MAX, fpid);
108 if (fpid == 0) {
109 ret = pthread_create(&thread1, NULL, ThreadSetDfl, 0);
110 if (ret != 0) {
111 exit(ret);
112 }
113 ret = pthread_create(&thread2, NULL, ThreadSetFunc2, 0);
114 if (ret != 0) {
115 exit(ret);
116 }
117 ret = pthread_create(&thread, NULL, ThreadKill, 0);
118 if (ret != 0) {
119 exit(ret);
120 }
121
122 pthread_join(thread, reinterpret_cast<void **>(&status1));
123 if ((int)(intptr_t)status1 != 0) {
124 exit(-1);
125 }
126 pthread_join(thread1, reinterpret_cast<void **>(&status1));
127 if ((int)(intptr_t)status1 != 0) {
128 exit(-1);
129 }
130 pthread_join(thread2, reinterpret_cast<void **>(&status1));
131 if ((int)(intptr_t)status1 != 0) {
132 exit(-1);
133 }
134 if (g_sigCount != 1) {
135 exit(g_sigCount);
136 }
137 exit(0);
138 }
139
140 ret = waitpid(fpid, &status, 0);
141 ICUNIT_ASSERT_EQUAL(ret, fpid, ret);
142 ICUNIT_ASSERT_EQUAL(WEXITSTATUS(status), 0, WEXITSTATUS(status));
143 return 0;
144 }
145
ItPosixSignal013(void)146 void ItPosixSignal013(void)
147 {
148 TEST_ADD_CASE(__FUNCTION__, TestSigMultiPthread, TEST_POSIX, TEST_SIGNAL, TEST_LEVEL0, TEST_FUNCTION);
149 }
150