1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2023 Oxide Computer Company
14 */
15
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19
20 #include "common.h"
21
22 int
main(void)23 main(void)
24 {
25 int res, err, fd;
26
27 /* Open with a bad signal mask pointer */
28 res = signalfd(-1, NULL, 0);
29 err = errno;
30 if (res != -1 || err != EFAULT) {
31 test_fail("expected EFAULT for NULL signal mask"
32 ", found res=%d errno=%d", res, err);
33 }
34
35 sigset_t mask;
36 assert(sigemptyset(&mask) == 0);
37
38 /* Open with bad flags */
39 res = signalfd(-1, &mask, ~0);
40 err = errno;
41 if (res != -1 || err != EINVAL) {
42 test_fail("expected EINVAL bad flags"
43 ", found res=%d errno=%d", res, err);
44 }
45
46 /* Open basic instance and confirm empty flags */
47 res = signalfd(-1, &mask, 0);
48 err = errno;
49 if (res < 0) {
50 test_fail("failed to open signalfd, found res=%d errno=%d",
51 res, err);
52 }
53 fd = res;
54 res = fcntl(fd, F_GETFL, 0);
55 assert(res >= 0);
56 if ((res & O_NONBLOCK) != 0) {
57 test_fail("expected no O_NONBLOCK, found flags=0x%x", res);
58 }
59 res = fcntl(fd, F_GETFD, 0);
60 assert(res >= 0);
61 if ((res & FD_CLOEXEC) != 0) {
62 test_fail("expected no FD_CLOEXEC, found fdflags=0x%x", res);
63 }
64 (void) close(fd);
65
66 /* Open with NONBLOCK and CLOEXEC, and confirm flags */
67 res = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
68 err = errno;
69 if (res < 0) {
70 test_fail("failed to open signalfd, found res=%d errno=%d",
71 res, err);
72 }
73 fd = res;
74 res = fcntl(fd, F_GETFL, 0);
75 assert(res >= 0);
76 if ((res & O_NONBLOCK) == 0) {
77 test_fail("missing O_NONBLOCK, found flags=0x%x", res);
78 }
79 res = fcntl(fd, F_GETFD, 0);
80 assert(res >= 0);
81 if ((res & FD_CLOEXEC) == 0) {
82 test_fail("missing FD_CLOEXEC, found fdflags=0x%x", res);
83 }
84 (void) close(fd);
85
86 test_pass();
87 }
88