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