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#include <sys/signalfd.h>
19
20#include "test.h"
21
22const int sig = SIGALRM;
23
24/**
25 * @tc.name      : signalfd_0100
26 * @tc.desc      : create a file descriptor for accepting signals
27 * @tc.level     : Level 0
28 */
29void signalfd_0100(void)
30{
31    sigset_t mask = {};
32
33    int result = sigaddset(&mask, sig);
34    if (result != 0) {
35        t_error("%s failed: result = %d\n", __func__, result);
36    }
37
38    result = sigprocmask(SIG_SETMASK, &mask, NULL);
39    if (result != 0) {
40        t_error("%s failed: result = %d\n", __func__, result);
41    }
42
43    errno = 0;
44    int fd = signalfd(-1, &mask, SFD_CLOEXEC);
45    if (fd <= 0) {
46        t_error("%s failed: fd = %d\n", __func__, fd);
47    }
48
49    if (errno != 0) {
50        t_error("%s failed: errno = %d\n", __func__, errno);
51    }
52
53    raise(sig);
54
55    struct signalfd_siginfo sfd_si = {0};
56    ssize_t size = read(fd, &sfd_si, sizeof(sfd_si));
57    if (size <= 0) {
58        t_error("%s failed: size = %ld\n", __func__, size);
59    }
60
61    if (sfd_si.ssi_signo != sig) {
62        t_error("%s failed: sfd_si.ssi_signo = %d\n", __func__, sfd_si.ssi_signo);
63    }
64
65    close(fd);
66}
67
68/**
69 * @tc.name      : signalfd_0200
70 * @tc.desc      : create a file descriptor for accepting signals with invalid parameters
71 * @tc.level     : Level 2
72 */
73void signalfd_0200(void)
74{
75    errno = 0;
76    int fd = signalfd(-1, NULL, -1);
77    if (fd > 0) {
78        t_error("%s failed: fd = %d\n", __func__, fd);
79    }
80
81    if (errno == 0) {
82        t_error("%s failed: errno = %d\n", __func__, errno);
83    }
84}
85
86int main(int argc, char *argv[])
87{
88    signalfd_0100();
89    signalfd_0200();
90
91    return t_status;
92}
93