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