• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <errno.h>
17 #include <signal.h>
18 
19 #include "test.h"
20 
21 static int handler_count = 0;
22 
handler(int sig)23 void handler(int sig)
24 {
25     handler_count++;
26 }
27 
28 /**
29  * @tc.name      : sigrelse_0100
30  * @tc.desc      : removes sig from the calling process's signal mask
31  * @tc.level     : Level 0
32  */
sigrelse_0100(void)33 void sigrelse_0100(void)
34 {
35     handler_count = 0;
36 
37     int sig = SIGALRM;
38     struct sigaction act = {.sa_flags = 0, .sa_handler = handler};
39     struct sigaction old_act = {0};
40     int result = sigaction(sig, &act, &old_act);
41     if (result != 0) {
42         t_error("%s failed: result = %d\n", __func__, result);
43         return;
44     }
45 
46     result = sighold(sig);
47     if (result != 0) {
48         t_error("%s failed: result = %d\n", __func__, result);
49         return;
50     }
51 
52     sigset_t set = {0};
53     result = sigprocmask(SIG_SETMASK, NULL, &set);
54     if (result != 0) {
55         t_error("%s failed: result = %d\n", __func__, result);
56         return;
57     }
58 
59     result = sigismember(&set, sig);
60     if (result == 0) {
61         t_error("%s failed: result = %d\n", __func__, result);
62         return;
63     }
64 
65     result = raise(sig);
66     if (result != 0 || handler_count != 0) {
67         t_error("%s failed: result = %d\n", __func__, result);
68         t_error("%s failed: handler_count = %d\n", __func__, handler_count);
69     }
70 
71     errno = 0;
72     result = sigpause(sig);
73     if (result != -1 || errno != EINTR) {
74         t_error("%s failed: result = %d\n", __func__, result);
75         t_error("%s failed: errno = %d\n", __func__, errno);
76     }
77 
78     if (handler_count != 1) {
79         t_error("%s failed: handler_count = %d\n", __func__, handler_count);
80     }
81 
82     result = sigprocmask(SIG_SETMASK, NULL, &set);
83     if (result != 0) {
84         t_error("%s failed: result = %d\n", __func__, result);
85     }
86 
87     result = sigismember(&set, sig);
88     if (result != 1) {
89         t_error("%s failed: result = %d\n", __func__, result);
90     }
91 
92     result = sigrelse(sig);
93     if (result != 0) {
94         t_error("%s failed: result = %d\n", __func__, result);
95     }
96 
97     result = sigprocmask(SIG_SETMASK, NULL, &set);
98     if (result != 0) {
99         t_error("%s failed: result = %d\n", __func__, result);
100     }
101 
102     result = sigismember(&set, sig);
103     if (result != 0) {
104         t_error("%s failed: result = %d\n", __func__, result);
105     }
106 }
107 
108 /**
109  * @tc.name      : sigrelse_0200
110  * @tc.desc      : removes an invalid sig from the calling process's signal mask
111  * @tc.level     : Level 2
112  */
sigrelse_0200(void)113 void sigrelse_0200(void)
114 {
115     errno = 0;
116     int sig = 99999;
117     int result = sigrelse(sig);
118     if (result == 0) {
119         t_error("%s failed: result = %d\n", __func__, result);
120     }
121 
122     if (errno == 0) {
123         t_error("%s failed: errno = %d\n", __func__, errno);
124     }
125 }
126 
main(int argc,char * argv[])127 int main(int argc, char *argv[])
128 {
129     sigrelse_0100();
130     sigrelse_0200();
131 
132     return t_status;
133 }
134