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
21static int handler_count = 0;
22
23void handler(int sig)
24{
25    handler_count++;
26}
27
28/**
29 * @tc.name      : sighold_0100
30 * @tc.desc      : add sig to the signal mask of the calling process
31 * @tc.level     : Level 0
32 */
33void sighold_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) {
67        t_error("%s failed: result = %d\n", __func__, result);
68    }
69
70    if (handler_count != 0) {
71        t_error("%s failed: handler_count = %d\n", __func__, handler_count);
72    }
73}
74
75/**
76 * @tc.name      : sighold_0200
77 * @tc.desc      : add an invalid sig to the signal mask of the calling process
78 * @tc.level     : Level 2
79 */
80void sighold_0200(void)
81{
82    errno = 0;
83    int sig = 99999;
84    int result = sighold(sig);
85    if (result == 0) {
86        t_error("%s failed: result = %d\n", __func__, result);
87    }
88
89    if (errno == 0) {
90        t_error("%s failed: errno = %d\n", __func__, errno);
91    }
92}
93
94int main(int argc, char *argv[])
95{
96    sighold_0100();
97    sighold_0200();
98
99    return t_status;
100}
101