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