]> git.saurik.com Git - apple/xnu.git/blame_incremental - tests/fd_send.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / fd_send.c
... / ...
CommitLineData
1#include <darwintest.h>
2#include <darwintest_utils.h>
3#include <dispatch/dispatch.h>
4#include <mach/mach.h>
5#include <signal.h>
6#include <sys/socket.h>
7
8T_GLOBAL_META(
9 T_META_NAMESPACE("xnu.fd"),
10 T_META_RUN_CONCURRENTLY(true));
11
12
13#define SOCKETPAIR(pair) \
14 T_ASSERT_POSIX_SUCCESS(socketpair(PF_LOCAL, SOCK_STREAM, 0, pair), "socketpair")
15
16
17static errno_t
18send_fd(int sock, int fd)
19{
20 struct iovec iovec[1];
21 struct msghdr msg;
22 struct cmsghdr *cmsghdrp;
23 char buf[CMSG_SPACE(sizeof(int))];
24
25 iovec[0].iov_base = "";
26 iovec[0].iov_len = 1;
27 msg.msg_name = 0;
28 msg.msg_namelen = 0;
29 msg.msg_iov = iovec;
30 msg.msg_iovlen = 1;
31 msg.msg_control = buf;
32 msg.msg_controllen = CMSG_SPACE(sizeof(int));
33
34 cmsghdrp = CMSG_FIRSTHDR(&msg);
35 cmsghdrp->cmsg_len = CMSG_LEN(sizeof(int));
36 cmsghdrp->cmsg_level = SOL_SOCKET;
37 cmsghdrp->cmsg_type = SCM_RIGHTS;
38
39 memcpy(CMSG_DATA(cmsghdrp), &fd, sizeof(fd));
40
41 if (sendmsg(sock, &msg, 0) < 0) {
42 return errno;
43 }
44
45 return 0;
46}
47
48static errno_t
49recv_fd(int sock, int *fdp)
50{
51 u_char c;
52 struct iovec iovec[1];
53 struct msghdr msg;
54 struct cmsghdr *cmsghdrp;
55 char buf[CMSG_SPACE(sizeof(int))];
56
57 iovec[0].iov_base = &c;
58 iovec[0].iov_len = 1;
59
60 msg.msg_name = 0;
61 msg.msg_namelen = 0;
62 msg.msg_iov = iovec;
63 msg.msg_iovlen = 1;
64 msg.msg_control = buf;
65 msg.msg_controllen = CMSG_SPACE(sizeof(int));
66 msg.msg_flags = 0;
67
68 if (recvmsg(sock, &msg, 0) < 0) {
69 return errno;
70 }
71
72 cmsghdrp = CMSG_FIRSTHDR(&msg);
73 if (cmsghdrp == NULL) {
74 return ENOENT;
75 }
76
77 if (cmsghdrp->cmsg_len != CMSG_LEN(sizeof(int))) {
78 return ENOENT;
79 }
80 if (cmsghdrp->cmsg_level != SOL_SOCKET) {
81 return ENOENT;
82 }
83 if (cmsghdrp->cmsg_type != SCM_RIGHTS) {
84 return ENOENT;
85 }
86
87 memcpy(fdp, CMSG_DATA(cmsghdrp), sizeof(*fdp));
88 return 0;
89}
90
91T_DECL(send, "test for 30465592")
92{
93 int pair[2], fd, status;
94 pid_t child;
95
96 T_ASSERT_POSIX_SUCCESS(socketpair(PF_LOCAL, SOCK_STREAM, 0, pair),
97 "socketpair");
98
99 child = fork();
100 if (child != 0) {
101 fd = open("/dev/null", O_RDWR);
102 T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)");
103
104 T_ASSERT_EQ(send_fd(pair[0], fd), 0, "send_fd");
105 T_ASSERT_POSIX_SUCCESS(close(fd), "close(fd)");
106
107 T_EXPECT_POSIX_SUCCESS(waitpid(child, &status, 0), "waitpid");
108 } else {
109 T_QUIET; T_ASSERT_EQ(recv_fd(pair[1], &fd), 0, "recv_fd");
110 T_QUIET; T_ASSERT_NE(fd, -1, "received a proper fd");
111 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(fd), "close(fd)");
112 raise(SIGKILL); /* do not confuse the test system */
113 }
114}
115
116T_DECL(send_kill, "test for 30465592")
117{
118 int pair[2], fd, status;
119 pid_t child;
120
121 T_QUIET; SOCKETPAIR(pair);
122
123 child = fork();
124 if (child != 0) {
125 fd = open("/dev/null", O_RDWR);
126 T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)");
127
128 T_ASSERT_EQ(send_fd(pair[0], fd), 0, "send_fd");
129 T_ASSERT_POSIX_SUCCESS(close(fd), "close(fd)");
130
131 T_EXPECT_POSIX_SUCCESS(kill(child, SIGKILL), "kill(child)");
132
133 T_EXPECT_POSIX_SUCCESS(waitpid(child, &status, 0), "waitpid");
134 } else {
135 T_QUIET; T_ASSERT_EQ(recv_fd(pair[1], &fd), 0, "recv_fd");
136 T_QUIET; T_ASSERT_NE(fd, -1, "received a proper fd");
137 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(fd), "close(fd)");
138 raise(SIGKILL); /* do not confuse the test system */
139 }
140}
141
142T_DECL(send_sock, "test for 30465592")
143{
144 int pair[2], fd, status;
145 pid_t child;
146
147 T_QUIET; SOCKETPAIR(pair);
148
149 child = fork();
150 if (child != 0) {
151 int sock[2];
152
153 T_QUIET; SOCKETPAIR(sock);
154
155 T_ASSERT_EQ(send_fd(pair[0], sock[0]), 0, "send_fd");
156 T_ASSERT_POSIX_SUCCESS(close(sock[0]), "close(sock[0])");
157 T_ASSERT_POSIX_SUCCESS(close(sock[1]), "close(sock[1])");
158
159 T_EXPECT_POSIX_SUCCESS(waitpid(child, &status, 0), "waitpid");
160 } else {
161 T_QUIET; T_ASSERT_EQ(recv_fd(pair[1], &fd), 0, "recv_fd");
162 T_QUIET; T_ASSERT_NE(fd, -1, "received a proper fd");
163 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(fd), "close(fd)");
164 raise(SIGKILL); /* do not confuse the test system */
165 }
166}
167
168T_DECL(send_stress, "test for 67133384")
169{
170 int fd;
171
172 fd = open("/dev/null", O_RDWR);
173 T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)");
174
175 dispatch_apply(10, NULL, ^(size_t worker) {
176 dispatch_queue_t q = dispatch_queue_create("receiver", NULL);
177 dispatch_group_t g = dispatch_group_create();
178 int pairbuf[2], *pair = pairbuf;
179 int n = 1000;
180
181 SOCKETPAIR(pair);
182
183 dispatch_group_async(g, q, ^{
184 int tmp;
185
186 for (int i = 0; i < n; i++) {
187 T_QUIET; T_ASSERT_EQ(recv_fd(pair[1], &tmp), 0, "recv_fd");
188 T_QUIET; T_ASSERT_NE(tmp, -1, "received a proper fd");
189 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(tmp), "close(tmp)");
190 }
191 });
192 dispatch_release(q);
193
194 for (int i = 0; i < n; i++) {
195 int tmp = dup(fd);
196 T_QUIET; T_ASSERT_POSIX_SUCCESS(tmp, "dup");
197 T_QUIET; T_ASSERT_EQ(send_fd(pair[0], tmp), 0, "send_fd");
198 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(tmp), "close(tmp)");
199 }
200 dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
201
202 T_PASS("sent and received %d fds in worker %zd", n, worker);
203
204 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(pair[0]), "close(pair[0])");
205 T_QUIET; T_EXPECT_POSIX_SUCCESS(close(pair[1]), "close(pair[1])");
206 });
207}