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 action_count = 0;
22
action(int sig,siginfo_t * info,void * ctx)23 void action(int sig, siginfo_t *info, void *ctx)
24 {
25 if (sig != SIGALRM) {
26 t_error("%s failed: sig = %d\n", __func__, sig);
27 }
28
29 if (info->si_signo != SIGALRM) {
30 t_error("%s failed: info->si_signo = %d\n", __func__, info->si_signo);
31 }
32
33 if (info->si_code != SI_QUEUE) {
34 t_error("%s failed: info->si_code = %d\n", __func__, info->si_code);
35 }
36
37 if (info->si_value.sival_int != 1) {
38 t_error("%s failed: info->si_value.sival_int = %d\n", __func__, info->si_value.sival_int);
39 }
40
41 action_count++;
42 }
43
44 /**
45 * @tc.name : sigqueue_0100
46 * @tc.desc : queue a signal
47 * @tc.level : Level 0
48 */
sigqueue_0100(void)49 void sigqueue_0100(void)
50 {
51 action_count = 0;
52
53 int sig = SIGALRM;
54 int flags = SA_SIGINFO;
55 struct sigaction act = {.sa_flags = flags, .sa_sigaction = action};
56 struct sigaction old_act = {0};
57 int result = sigaction(sig, &act, &old_act);
58 if (result != 0) {
59 t_error("%s failed: result = %d\n", __func__, result);
60 return;
61 }
62
63 union sigval sigval = {.sival_int = 1};
64
65 errno = 0;
66 result = sigqueue(getpid(), sig, sigval);
67 if (result != 0) {
68 t_error("%s failed: result = %d\n", __func__, result);
69 }
70
71 if (errno != 0) {
72 t_error("%s failed: errno = %d\n", __func__, errno);
73 }
74
75 if (action_count != 1) {
76 t_error("%s failed: action_count = %d\n", __func__, action_count);
77 }
78 }
79
80 /**
81 * @tc.name : sigqueue_0200
82 * @tc.desc : queue a signal with an invalid sig
83 * @tc.level : Level 2
84 */
sigqueue_0200(void)85 void sigqueue_0200(void)
86 {
87 int sig = 99999;
88 union sigval sigval = {.sival_int = 1};
89
90 errno = 0;
91 int result = sigqueue(getpid(), sig, sigval);
92 if (result == 0) {
93 t_error("%s failed: result = %d\n", __func__, result);
94 }
95
96 if (errno == 0) {
97 t_error("%s failed: errno = %d\n", __func__, errno);
98 }
99 }
100
main(int argc,char * argv[])101 int main(int argc, char *argv[])
102 {
103 sigqueue_0100();
104 sigqueue_0200();
105
106 return t_status;
107 }
108