Debian Codegraph @

src/util/test-dispatch.c

raw
1 /*
2 * Test Dispatcher
3 */
4
5 #undef NDEBUG
6 #include <c-stdaux.h>
7 #include <linux/sockios.h>
8 #include <stdlib.h>
9 #include <sys/epoll.h>
10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
12 #include "util/dispatch.h"
13
14 static void q_assert(int s, bool has_in, bool has_out) {
15 int r, v;
16
17 /*
18 * Verify that the IN/OUT queues are (non-)empty, depending on what is
19 * given as @has_in and @has_out.
20 */
21
22 r = ioctl(s, SIOCINQ, &v);
23 c_assert(r >= 0);
24 c_assert(!v == !has_in);
25
26 r = ioctl(s, SIOCOUTQ, &v);
27 c_assert(r >= 0);
28 c_assert(!v == !has_out);
29 }
30
31 /*
32 * This test verifies that we get EPOLLOUT from UDS sockets whenever the
33 * outgoing queue runs *EMPTY*. That is, when we send data to a socket, we rely
34 * on the kernel to re-notify us of EPOLLOUT whenever the other side cleared
35 * its incoming queue.
36 *
37 * Additionally, we do the same test *after* we shutdown both read and write
38 * side of the connection. That is, even if both channels are down and EPOLLHUP
39 * is signalled, we still want to be notified of queues running empty.
40 *
41 * Both of these invariants is relied on by our socket implementation for
42 * accounting reasons. Hence, they better be granted by the kernel.
43 */
44 static void test_uds_edge(unsigned int run) {
45 _c_cleanup_(dispatch_context_deinit) DispatchContext c = DISPATCH_CONTEXT_NULL(c);
46 DispatchFile f = DISPATCH_FILE_NULL(f);
47 char b[] = { "foobar" };
48 int r, s[2];
49
50 /* setup */
51
52 r = dispatch_context_init(&c);
53 c_assert(!r);
54
55 r = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, s);
56 c_assert(!r);
57
58 r = dispatch_file_init(&f, &c, NULL, s[0], EPOLLOUT, 0);
59 c_assert(!r);
60
61 dispatch_file_select(&f, EPOLLOUT);
62
63 /* both queues empty, EPOLLOUT ready */
64
65 q_assert(s[0], false, false);
66 q_assert(s[1], false, false);
67
68 r = dispatch_context_poll(&c, 0);
69 c_assert(!r);
70 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
71
72 /* send message and verify sockets signal data */
73
74 r = send(s[0], b, sizeof(b), MSG_DONTWAIT | MSG_NOSIGNAL);
75 c_assert(r == sizeof(b));
76
77 q_assert(s[0], false, true);
78 q_assert(s[1], true, false);
79
80 r = dispatch_context_poll(&c, 0);
81 c_assert(!r);
82 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
83
84 /* clear EPOLLOUT (but keep it selected), verify it is not signalled */
85
86 dispatch_file_clear(&f, EPOLLOUT);
87
88 r = dispatch_context_poll(&c, 0);
89 c_assert(!r);
90 c_assert(!c_list_is_linked(&f.ready_link) && !(f.events & EPOLLOUT));
91
92 /* receive data and verify socket becomes pollable */
93
94 r = recv(s[1], b, sizeof(b), MSG_DONTWAIT);
95 c_assert(r == sizeof(b));
96
97 q_assert(s[0], false, false);
98 q_assert(s[1], false, false);
99
100 r = dispatch_context_poll(&c, 0);
101 c_assert(!r);
102 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
103
104 /*
105 * The first test succeeded. We now repeat this test, but before
106 * dequeuing the data, we shutdown the socket.
107 *
108 * This simulates a server queueing data on a client that now decided
109 * to disconnect. We want the server to be able to keep the connection
110 * up and running, and still be notified of events, even though it was
111 * shutdown. That is, we want to be notified of the queue running
112 * empty, either via following recv(2) calls or a teardown of the
113 * remote socket via the last close(2) call.
114 */
115
116 /* send message again and verify sockets signal data */
117
118 r = send(s[0], b, sizeof(b), MSG_DONTWAIT | MSG_NOSIGNAL);
119 c_assert(r == sizeof(b));
120
121 q_assert(s[0], false, true);
122 q_assert(s[1], true, false);
123
124 r = dispatch_context_poll(&c, 0);
125 c_assert(!r);
126 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
127
128 /* clear EPOLLOUT (but keep it selected), verify it is not signalled */
129
130 dispatch_file_clear(&f, EPOLLOUT);
131
132 r = dispatch_context_poll(&c, 0);
133 c_assert(!r);
134 c_assert(!c_list_is_linked(&f.ready_link) && !(f.events & EPOLLOUT));
135
136 /* trigger remote shutdown and verify queue state did not change */
137
138 r = shutdown(s[1], SHUT_RDWR);
139 c_assert(!r);
140
141 q_assert(s[0], false, true);
142 q_assert(s[1], true, false);
143
144 /*
145 * Verify that EPOLLHUP is set. We do *NOT* use the dispatcher for
146 * that. We want to make sure we are woken up for EPOLLOUT, not
147 * EPOLLHUP, hence we better not select EPOLLHUP in the epoll-set. This
148 * tests a fast-path the socket-layer uses to pass events to epoll
149 * without requiring a callback into ->poll(). Furthermore, epoll
150 * correctly ignores wake-ups for anything that does not provide our
151 * events.
152 * This is in no way a reliable way to just get woken up for EPOLLOUT.
153 * But it decreases false-positives, so lets do it nevertheless.
154 *
155 * Note that we fetch EPOLLOUT here, since shutdown(2) might trigger
156 * it. However, we explicitly clear is and fetch events again,
157 * verifying we're no longer woken up for it.
158 */
159
160 r = send(s[0], b, sizeof(b), MSG_DONTWAIT | MSG_NOSIGNAL);
161 c_assert(r < 0 && errno == EPIPE);
162
163 /* fetch EPOLLOUT which was set by shutdown(2) and clear it */
164
165 r = dispatch_context_poll(&c, 0);
166 c_assert(!r);
167 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
168
169 dispatch_file_clear(&f, EPOLLOUT);
170
171 r = dispatch_context_poll(&c, 0);
172 c_assert(!r);
173 c_assert(!(f.events & EPOLLOUT));
174
175 /* receive data and verify socket becomes pollable */
176
177 switch (run) {
178 case 0:
179 /* if @run is 0, we use recv(2) to dequeue data */
180 r = recv(s[1], b, sizeof(b), MSG_DONTWAIT);
181 c_assert(r == sizeof(b));
182
183 q_assert(s[0], false, false);
184 q_assert(s[1], false, false);
185 break;
186 case 1:
187 /* if @run is 1, we use close(2) to flush queues */
188 r = close(s[1]);
189 c_assert(!r);
190 s[1] = -1;
191
192 q_assert(s[0], false, false);
193 /* s[1] is obviously invalid here */
194 break;
195 default:
196 c_assert(0);
197 }
198
199 r = dispatch_context_poll(&c, 0);
200 c_assert(!r);
201 c_assert(c_list_is_linked(&f.ready_link) && (f.events & EPOLLOUT));
202
203 /* cleanup */
204
205 dispatch_file_deinit(&f);
206 c_close(s[1]);
207 c_close(s[0]);
208 }
209
210 int main(int argc, char **argv) {
211 test_uds_edge(0);
212 test_uds_edge(1);
213 return 0;
214 }