1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
4 * Author: Ma Xinjian <maxj.fnst@fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify that signalfd(2) fails with:
11 *
12 * - EBADF when fd is invalid
13 * - EINVAL when fd is not a valid signalfd file descriptor
14 * - EINVAL when flags are invalid
15 */
16
17 #include <sys/signalfd.h>
18 #include "tst_test.h"
19
20 #define SIGNAL_FILE "signal_file"
21
22 static int fd_ebadf = -2;
23 static int fd_einval1;
24 static int fd_einval2 = -1;
25
26 static sigset_t *mask;
27
28 static struct test_case_t {
29 int *fd;
30 int flags;
31 int expected_errno;
32 char *desc;
33 } tcases[] = {
34 {&fd_ebadf, 0, EBADF, "fd is invalid"},
35 {&fd_einval1, 0, EINVAL,
36 "fd is not a valid signalfd file descriptor"},
37 {&fd_einval2, -1, EINVAL, "flags are invalid"},
38 };
39
setup(void)40 static void setup(void)
41 {
42 SAFE_SIGEMPTYSET(mask);
43 SAFE_SIGADDSET(mask, SIGUSR1);
44 SAFE_SIGPROCMASK(SIG_BLOCK, mask, NULL);
45
46 fd_einval1 = SAFE_OPEN(SIGNAL_FILE, O_CREAT, 0777);
47 }
48
cleanup(void)49 static void cleanup(void)
50 {
51 if (fd_einval1 > 0)
52 SAFE_CLOSE(fd_einval1);
53 }
54
verify_signalfd(unsigned int i)55 static void verify_signalfd(unsigned int i)
56 {
57 struct test_case_t *tc = &tcases[i];
58
59 TST_EXP_FAIL2(signalfd(*(tc->fd), mask, tc->flags),
60 tc->expected_errno, "%s", tc->desc);
61 }
62
63 static struct tst_test test = {
64 .tcnt = ARRAY_SIZE(tcases),
65 .test = verify_signalfd,
66 .setup = setup,
67 .cleanup = cleanup,
68 .needs_tmpdir = 1,
69 .bufs = (struct tst_buffers []) {
70 {&mask, .size = sizeof(sigset_t)},
71 {}
72 }
73 };
74