1*d8b80295SApple OSS Distributions /*
2*d8b80295SApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved.
3*d8b80295SApple OSS Distributions */
4*d8b80295SApple OSS Distributions #include <stdio.h>
5*d8b80295SApple OSS Distributions #include <errno.h>
6*d8b80295SApple OSS Distributions #include <stdio.h>
7*d8b80295SApple OSS Distributions #include <string.h>
8*d8b80295SApple OSS Distributions #include <stdlib.h>
9*d8b80295SApple OSS Distributions #include <unistd.h>
10*d8b80295SApple OSS Distributions #include <fcntl.h>
11*d8b80295SApple OSS Distributions
12*d8b80295SApple OSS Distributions #include <sys/types.h>
13*d8b80295SApple OSS Distributions #include <sys/event.h>
14*d8b80295SApple OSS Distributions #include <sys/time.h>
15*d8b80295SApple OSS Distributions
16*d8b80295SApple OSS Distributions #include <arpa/inet.h>
17*d8b80295SApple OSS Distributions #include <net/if_var.h>
18*d8b80295SApple OSS Distributions #include <netinet/ip6.h>
19*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
20*d8b80295SApple OSS Distributions #include <darwintest.h>
21*d8b80295SApple OSS Distributions
22*d8b80295SApple OSS Distributions /*
23*d8b80295SApple OSS Distributions * Tests that filling up the socket buffer doesn't cause
24*d8b80295SApple OSS Distributions * kevent to return "writeable".
25*d8b80295SApple OSS Distributions */
26*d8b80295SApple OSS Distributions static void __unused
test_kevent(int type)27*d8b80295SApple OSS Distributions test_kevent(int type)
28*d8b80295SApple OSS Distributions {
29*d8b80295SApple OSS Distributions int sockets[2] = { -1 };
30*d8b80295SApple OSS Distributions int kq = -1;
31*d8b80295SApple OSS Distributions struct kevent evlist = { 0 };
32*d8b80295SApple OSS Distributions struct kevent chlist = { 0 };
33*d8b80295SApple OSS Distributions
34*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((kq = kqueue()), "kqueue");
35*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, type, 0, sockets), "socketpair");
36*d8b80295SApple OSS Distributions int flags = fcntl(sockets[0], F_GETFL);
37*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(sockets[0], F_SETFL, flags | O_NONBLOCK), "fcntl");
38*d8b80295SApple OSS Distributions
39*d8b80295SApple OSS Distributions EV_SET(&chlist, sockets[0], EVFILT_WRITE, EV_ADD | EV_ERROR, 0, 0, 0);
40*d8b80295SApple OSS Distributions ssize_t result = kevent(kq, &chlist, 1, &evlist, 1, NULL);
41*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 1, "should be able to write");
42*d8b80295SApple OSS Distributions
43*d8b80295SApple OSS Distributions // Fill the socket buffer
44*d8b80295SApple OSS Distributions char buf[1] = { 0x55 };
45*d8b80295SApple OSS Distributions while (write(sockets[0], buf, sizeof(buf)) > 0) {
46*d8b80295SApple OSS Distributions ;
47*d8b80295SApple OSS Distributions }
48*d8b80295SApple OSS Distributions
49*d8b80295SApple OSS Distributions result = write(sockets[0], buf, sizeof(buf));
50*d8b80295SApple OSS Distributions if (type == SOCK_STREAM) {
51*d8b80295SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EWOULDBLOCK, "should block");
52*d8b80295SApple OSS Distributions } else {
53*d8b80295SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ENOBUFS, "should block");
54*d8b80295SApple OSS Distributions }
55*d8b80295SApple OSS Distributions
56*d8b80295SApple OSS Distributions struct timespec ts = { .tv_sec = 1, .tv_nsec = 0 };
57*d8b80295SApple OSS Distributions result = kevent(kq, &chlist, 1, &evlist, 1, &ts);
58*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 0, "should timeout");
59*d8b80295SApple OSS Distributions close(sockets[0]);
60*d8b80295SApple OSS Distributions close(sockets[1]);
61*d8b80295SApple OSS Distributions close(kq);
62*d8b80295SApple OSS Distributions }
63*d8b80295SApple OSS Distributions
64*d8b80295SApple OSS Distributions static void __unused
test_kevent_lowat(int type)65*d8b80295SApple OSS Distributions test_kevent_lowat(int type)
66*d8b80295SApple OSS Distributions {
67*d8b80295SApple OSS Distributions int sockets[2] = { -1 };
68*d8b80295SApple OSS Distributions int kq = -1;
69*d8b80295SApple OSS Distributions struct kevent evlist = { 0 };
70*d8b80295SApple OSS Distributions struct kevent chlist = { 0 };
71*d8b80295SApple OSS Distributions
72*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((kq = kqueue()), "kqueue");
73*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, type, 0, sockets), "socketpair");
74*d8b80295SApple OSS Distributions int flags = fcntl(sockets[0], F_GETFL);
75*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(sockets[0], F_SETFL, flags | O_NONBLOCK), "fcntl");
76*d8b80295SApple OSS Distributions
77*d8b80295SApple OSS Distributions EV_SET(&chlist, sockets[0], EVFILT_WRITE, EV_ADD | EV_ERROR, 0, 0, 0);
78*d8b80295SApple OSS Distributions ssize_t result = kevent(kq, &chlist, 1, &evlist, 1, NULL);
79*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 1, "should be able to write");
80*d8b80295SApple OSS Distributions
81*d8b80295SApple OSS Distributions // Almost fill the socket buffer but leave 2K available.
82*d8b80295SApple OSS Distributions char buf[1] = { 0x55 };
83*d8b80295SApple OSS Distributions int max_writes = type == SOCK_STREAM ? 6000 : 30;
84*d8b80295SApple OSS Distributions for (int i = 0; i < max_writes; i++) {
85*d8b80295SApple OSS Distributions write(sockets[0], buf, sizeof(buf));
86*d8b80295SApple OSS Distributions }
87*d8b80295SApple OSS Distributions
88*d8b80295SApple OSS Distributions result = kevent(kq, &chlist, 1, &evlist, 1, NULL);
89*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 1, "should be able to write again");
90*d8b80295SApple OSS Distributions
91*d8b80295SApple OSS Distributions char large_buf[4096] = { };
92*d8b80295SApple OSS Distributions
93*d8b80295SApple OSS Distributions if (type == SOCK_STREAM) {
94*d8b80295SApple OSS Distributions // Write 2KB.
95*d8b80295SApple OSS Distributions result = write(sockets[0], large_buf, 2 * 1024);
96*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "write 2KB");
97*d8b80295SApple OSS Distributions // Write 4KB, should fail.
98*d8b80295SApple OSS Distributions result = write(sockets[0], large_buf, sizeof(large_buf));
99*d8b80295SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EWOULDBLOCK, "should block (EWOULDBLOCK)");
100*d8b80295SApple OSS Distributions } else {
101*d8b80295SApple OSS Distributions // Write 512B.
102*d8b80295SApple OSS Distributions result = write(sockets[0], large_buf, 512);
103*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "write 512B");
104*d8b80295SApple OSS Distributions // Write 2KB, should fail.
105*d8b80295SApple OSS Distributions result = write(sockets[0], large_buf, 2048);
106*d8b80295SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ENOBUFS, "should block (ENOBUFS)");
107*d8b80295SApple OSS Distributions }
108*d8b80295SApple OSS Distributions
109*d8b80295SApple OSS Distributions // Ask kqueue to wake us up when we can write 100 bytes.
110*d8b80295SApple OSS Distributions EV_SET(&chlist, sockets[0], EVFILT_WRITE, EV_ADD | EV_ERROR, NOTE_LOWAT, 100, 0);
111*d8b80295SApple OSS Distributions
112*d8b80295SApple OSS Distributions struct timespec ts = { .tv_sec = 1, .tv_nsec = 0 };
113*d8b80295SApple OSS Distributions result = kevent(kq, &chlist, 1, &evlist, 1, &ts);
114*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 0, "should timeout (note_lowat)");
115*d8b80295SApple OSS Distributions
116*d8b80295SApple OSS Distributions // Set the send buffer low water mark.
117*d8b80295SApple OSS Distributions int lowat = type == SOCK_STREAM ? 100 : 10;
118*d8b80295SApple OSS Distributions result = setsockopt(sockets[0], SOL_SOCKET, SO_SNDLOWAT, &lowat, sizeof(lowat));
119*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "setsockopt");
120*d8b80295SApple OSS Distributions
121*d8b80295SApple OSS Distributions if (type == SOCK_STREAM) {
122*d8b80295SApple OSS Distributions // Write 100 bytes.
123*d8b80295SApple OSS Distributions result = write(sockets[0], large_buf, 100);
124*d8b80295SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "write 100B");
125*d8b80295SApple OSS Distributions }
126*d8b80295SApple OSS Distributions
127*d8b80295SApple OSS Distributions // Reset the event and kqueue should respect SO_SNDLOWAT.
128*d8b80295SApple OSS Distributions EV_SET(&chlist, sockets[0], EVFILT_WRITE, EV_ADD | EV_ERROR, 0, 0, 0);
129*d8b80295SApple OSS Distributions result = kevent(kq, &chlist, 1, &evlist, 1, &ts);
130*d8b80295SApple OSS Distributions T_ASSERT_EQ(result, 0, "should timeout (sndlowat)");
131*d8b80295SApple OSS Distributions
132*d8b80295SApple OSS Distributions close(sockets[0]);
133*d8b80295SApple OSS Distributions close(sockets[1]);
134*d8b80295SApple OSS Distributions close(kq);
135*d8b80295SApple OSS Distributions }
136*d8b80295SApple OSS Distributions
137*d8b80295SApple OSS Distributions T_DECL(uipc_kevent, "Tests the UNIX Domain kevent filter", T_META_CHECK_LEAKS(false))
138*d8b80295SApple OSS Distributions {
139*d8b80295SApple OSS Distributions #if 0
140*d8b80295SApple OSS Distributions test_kevent(SOCK_STREAM);
141*d8b80295SApple OSS Distributions test_kevent(SOCK_DGRAM);
142*d8b80295SApple OSS Distributions test_kevent_lowat(SOCK_STREAM);
143*d8b80295SApple OSS Distributions test_kevent_lowat(SOCK_DGRAM);
144*d8b80295SApple OSS Distributions #else
145*d8b80295SApple OSS Distributions T_SKIP("Test is unstable");
146*d8b80295SApple OSS Distributions #endif
147*d8b80295SApple OSS Distributions }
148