1*1b191cb5SApple OSS Distributions #include <stdlib.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <string.h>
4*1b191cb5SApple OSS Distributions #include <err.h>
5*1b191cb5SApple OSS Distributions #include <errno.h>
6*1b191cb5SApple OSS Distributions #include <pthread.h>
7*1b191cb5SApple OSS Distributions #include <fcntl.h>
8*1b191cb5SApple OSS Distributions #include <poll.h>
9*1b191cb5SApple OSS Distributions #include <sys/socket.h>
10*1b191cb5SApple OSS Distributions #include <darwintest.h>
11*1b191cb5SApple OSS Distributions /*
12*1b191cb5SApple OSS Distributions * Tests that a full UNIX domain socket buffer
13*1b191cb5SApple OSS Distributions * always reports the right poll() event.
14*1b191cb5SApple OSS Distributions */
15*1b191cb5SApple OSS Distributions
16*1b191cb5SApple OSS Distributions static const int MSG1_LEN = 16384;
17*1b191cb5SApple OSS Distributions
18*1b191cb5SApple OSS Distributions static void
do_recv(int sk,char * buf,size_t size)19*1b191cb5SApple OSS Distributions do_recv(int sk, char *buf, size_t size)
20*1b191cb5SApple OSS Distributions {
21*1b191cb5SApple OSS Distributions do{
22*1b191cb5SApple OSS Distributions struct iovec iov[1];
23*1b191cb5SApple OSS Distributions struct msghdr msg;
24*1b191cb5SApple OSS Distributions
25*1b191cb5SApple OSS Distributions struct pollfd pfd[1] = { { sk, POLLIN, 0 } };
26*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(poll(pfd, 1, -1), "poll");
27*1b191cb5SApple OSS Distributions memset(&msg, 0, sizeof(msg));
28*1b191cb5SApple OSS Distributions iov->iov_base = buf;
29*1b191cb5SApple OSS Distributions iov->iov_len = size;
30*1b191cb5SApple OSS Distributions msg.msg_iov = iov;
31*1b191cb5SApple OSS Distributions msg.msg_iovlen = 1;
32*1b191cb5SApple OSS Distributions ssize_t res = recvmsg(sk, &msg, 0);
33*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(res, "recvmsg");
34*1b191cb5SApple OSS Distributions buf += res;
35*1b191cb5SApple OSS Distributions size -= (size_t)res;
36*1b191cb5SApple OSS Distributions } while (size);
37*1b191cb5SApple OSS Distributions }
38*1b191cb5SApple OSS Distributions
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions static void *
receiver(void * arg)41*1b191cb5SApple OSS Distributions receiver(void *arg)
42*1b191cb5SApple OSS Distributions {
43*1b191cb5SApple OSS Distributions int sk = (int)arg;
44*1b191cb5SApple OSS Distributions char *buf = malloc(MSG1_LEN);
45*1b191cb5SApple OSS Distributions for (;;) {
46*1b191cb5SApple OSS Distributions do_recv(sk, buf, MSG1_LEN);
47*1b191cb5SApple OSS Distributions }
48*1b191cb5SApple OSS Distributions }
49*1b191cb5SApple OSS Distributions
50*1b191cb5SApple OSS Distributions static void
do_send(int sk,char * buf,size_t size)51*1b191cb5SApple OSS Distributions do_send(int sk, char *buf, size_t size)
52*1b191cb5SApple OSS Distributions {
53*1b191cb5SApple OSS Distributions do{
54*1b191cb5SApple OSS Distributions struct iovec iov[1];
55*1b191cb5SApple OSS Distributions struct msghdr msg;
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions struct pollfd pfd[1] = { { sk, POLLOUT, 0 } };
58*1b191cb5SApple OSS Distributions int res = poll(pfd, 1, -1);
59*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(res, "poll");
60*1b191cb5SApple OSS Distributions if (res == 0) {
61*1b191cb5SApple OSS Distributions continue;
62*1b191cb5SApple OSS Distributions }
63*1b191cb5SApple OSS Distributions if (!(pfd[0].revents & POLLOUT)) {
64*1b191cb5SApple OSS Distributions T_FAIL("POLLOUT not set");
65*1b191cb5SApple OSS Distributions }
66*1b191cb5SApple OSS Distributions memset(&msg, 0, sizeof(msg));
67*1b191cb5SApple OSS Distributions iov->iov_base = buf;
68*1b191cb5SApple OSS Distributions iov->iov_len = size;
69*1b191cb5SApple OSS Distributions msg.msg_iov = iov;
70*1b191cb5SApple OSS Distributions msg.msg_iovlen = 1;
71*1b191cb5SApple OSS Distributions ssize_t res_sendmsg = sendmsg(sk, &msg, 0);
72*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(res_sendmsg, "sendmsg");
73*1b191cb5SApple OSS Distributions buf += res_sendmsg;
74*1b191cb5SApple OSS Distributions size -= (size_t)res_sendmsg;
75*1b191cb5SApple OSS Distributions } while (size);
76*1b191cb5SApple OSS Distributions }
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions
79*1b191cb5SApple OSS Distributions static void
cfg_sk(int sk)80*1b191cb5SApple OSS Distributions cfg_sk(int sk)
81*1b191cb5SApple OSS Distributions {
82*1b191cb5SApple OSS Distributions int newSndBufSz = MSG1_LEN * 2;
83*1b191cb5SApple OSS Distributions socklen_t optLen = sizeof(newSndBufSz);
84*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(sk, SOL_SOCKET, SO_SNDBUF,
85*1b191cb5SApple OSS Distributions &newSndBufSz, optLen),
86*1b191cb5SApple OSS Distributions "setsockopt");
87*1b191cb5SApple OSS Distributions newSndBufSz = MSG1_LEN * 2;
88*1b191cb5SApple OSS Distributions optLen = sizeof(newSndBufSz);
89*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(sk, SOL_SOCKET, SO_RCVBUF,
90*1b191cb5SApple OSS Distributions &newSndBufSz, optLen),
91*1b191cb5SApple OSS Distributions "setsockopt");
92*1b191cb5SApple OSS Distributions int flags = fcntl(sk, F_GETFL, 0);
93*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(flags, "fcntl");
94*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(sk, F_SETFL, flags | O_NONBLOCK),
95*1b191cb5SApple OSS Distributions "fcntl");
96*1b191cb5SApple OSS Distributions }
97*1b191cb5SApple OSS Distributions
98*1b191cb5SApple OSS Distributions T_DECL(uipc_sosendcheck, "Tests the UNIX Domain poll filter", T_META_CHECK_LEAKS(false))
99*1b191cb5SApple OSS Distributions {
100*1b191cb5SApple OSS Distributions int s[2];
101*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_STREAM, 0, s),
102*1b191cb5SApple OSS Distributions "socketpair");
103*1b191cb5SApple OSS Distributions cfg_sk(s[0]);
104*1b191cb5SApple OSS Distributions cfg_sk(s[1]);
105*1b191cb5SApple OSS Distributions char *buf = malloc(MSG1_LEN);
106*1b191cb5SApple OSS Distributions
107*1b191cb5SApple OSS Distributions pthread_t receiver_th;
108*1b191cb5SApple OSS Distributions if (pthread_create(&receiver_th, 0, receiver, (void *)(uintptr_t)s[1])) {
109*1b191cb5SApple OSS Distributions T_FAIL("pthread_create failed");
110*1b191cb5SApple OSS Distributions }
111*1b191cb5SApple OSS Distributions
112*1b191cb5SApple OSS Distributions for (unsigned int i = 0; i < 90000; i++) {
113*1b191cb5SApple OSS Distributions do_send(s[0], buf, 5);
114*1b191cb5SApple OSS Distributions do_send(s[0], buf, MSG1_LEN);
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions }
117