2 * test_utils_compat_poll.c
4 * Unit tests for the compatibility layer of poll/epoll API.
6 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
8 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
21 #include <common/compat/poll.hpp>
22 #include <common/readwrite.hpp>
23 #include <common/pipe.hpp>
24 #include <common/dynamic-array.hpp>
26 /* Verification without trashing test order in the child process */
27 #define childok(e, test, ...) do { \
29 diag(test, ## __VA_ARGS__); \
30 _exit(EXIT_FAILURE); \
35 int lttng_opt_quiet
= 1;
36 int lttng_opt_verbose
;
40 * Non-zero 8-bits arbitrary value below 0x7f to ensure no sign extension
41 * occurs. Used to verify that the value is properly propagated through the
44 #define MAGIC_VALUE ((char) 0x5A)
53 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
54 #define CLOE_VALUE EPOLL_CLOEXEC
56 #define CLOE_VALUE FD_CLOEXEC
60 void test_epoll_compat(void)
63 * Type conversion present to disable warning of anonymous enum from
66 ok((int) LTTNG_CLOEXEC
== (int) CLOE_VALUE
, "epoll's CLOEXEC value");
70 static void test_alloc(void)
72 struct lttng_poll_event poll_events
;
74 lttng_poll_init(&poll_events
);
77 ok(lttng_poll_create(NULL
, 1, 0) != 0, "Create over NULL pointer fails");
79 ok(lttng_poll_create(&poll_events
, 0, 0) != 0, "Create with size 0 fails");
81 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
83 * lttng_poll_event structure untested due to incompatibility across
84 * sublayers. lttng_poll_clean cannot be tested. There is no success
85 * criteria. Verify set's max size cases.
87 lttng_poll_clean(&poll_events
);
90 /* Tests stuff related to what would be handled with epoll_ctl. */
91 static void test_add_del(void)
93 struct lttng_poll_event poll_events
;
95 lttng_poll_init(&poll_events
);
96 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
97 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) != 0, "Adding to uninitialized structure fails");
98 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
100 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create a poll set succeeds");
101 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set created empty");
103 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
104 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
105 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
106 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
108 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) == 0, "Adding valid FD succeeds");
109 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Nb of elements incremented");
111 ok(lttng_poll_del(NULL
, 1) != 0, "Removing from NULL set fails");
112 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
114 ok(lttng_poll_del(&poll_events
, -1) != 0, "Removing from negative FD fails");
115 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
117 ok(lttng_poll_del(&poll_events
, 2) == 0, "Removing invalid FD still succeeds");
118 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of elements unchanged");
120 ok(lttng_poll_del(&poll_events
, 1) == 0, "Removing valid FD succeeds");
121 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements decremented");
123 ok(lttng_poll_del(&poll_events
, 1) != 0, "Removing from empty set fails");
124 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements unchanged");
126 lttng_poll_clean(&poll_events
);
129 static void test_mod_wait(void)
131 struct lttng_poll_event poll_events
;
132 struct lttng_poll_event cpoll_events
;
136 char rbuf
= 0, tbuf
= MAGIC_VALUE
;
139 lttng_poll_init(&poll_events
);
140 lttng_poll_init(&cpoll_events
);
142 ok(pipe(hupfd
) != -1, "pipe function succeeds");
143 ok(pipe(infd
) != -1, "pipe function succeeds");
147 childok(lttng_poll_create(&cpoll_events
, 1, 0) == 0, "Create valid poll set succeeds");
148 childok(lttng_poll_mod(NULL
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
149 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
150 childok(lttng_poll_add(&cpoll_events
, infd
[0], LPOLLIN
) == 0, "Add valid FD succeeds");
151 childok(lttng_poll_mod(&cpoll_events
, -1, LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
152 childok(lttng_poll_mod(&cpoll_events
, hupfd
[0], LPOLLIN
) == 0, "lttng_poll_mod on unincluded FD goes on");
153 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == 0, "Modify event type succeeds");
154 childok(close(infd
[1]) == 0, "Close valid FD succeeds");
155 childok(lttng_poll_wait(&cpoll_events
, -1) == 1, "Wait on close times out");
156 childok(lttng_read(infd
[0], &rbuf
, 1) == 1, "Data is present in the pipe");
157 childok(rbuf
== MAGIC_VALUE
, "Received data is consistent with transmitted data");
158 childok(lttng_poll_del(&cpoll_events
, infd
[0]) == 0, "Removing valid FD succeeds");
159 childok(close(infd
[0]) == 0, "Close valid FD succeeds");
160 childok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
161 childok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
162 lttng_poll_clean(&cpoll_events
);
165 ok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
166 ok(close(infd
[0]) == 0, "Close valid FD succeeds");
168 ok(lttng_poll_wait(NULL
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
170 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
171 ok(lttng_poll_wait(&poll_events
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
172 ok(lttng_poll_add(&poll_events
, hupfd
[0], LPOLLIN
) == 0, "Add valid FD succeeds");
173 ok(lttng_write(infd
[1], &tbuf
, 1) == 1, "Write to pipe succeeds");
174 ok(lttng_poll_wait(&poll_events
, -1) == 1, "Wakes up on one event");
175 ok(lttng_poll_del(&poll_events
, hupfd
[0]) == 0, "Removing valid FD succeeds");
176 ok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
177 ok(close(infd
[1]) == 0, "Close valid FD succeeds");
178 lttng_poll_clean(&poll_events
);
179 ok(waitpid(cpid
, &wstatus
, 0) == cpid
, "Wait for child exit");
180 ok(WIFEXITED(wstatus
) == 1, "Child process exited");
181 ok(WEXITSTATUS(wstatus
) == EXIT_SUCCESS
, "Child process exited with EXIT_SUCCESS");
185 static void destroy_pipe(void *pipe
)
187 lttng_pipe_destroy((lttng_pipe
*) pipe
);
190 static int run_active_set_combination(unsigned int fd_count
,
191 unsigned int active_fds_mask
)
195 const unsigned int active_fds_count
= __builtin_popcount(active_fds_mask
);
196 struct lttng_poll_event poll_events
;
197 struct lttng_dynamic_pointer_array pipes
;
198 struct lttng_pipe
*pipe
= NULL
;
200 lttng_poll_init(&poll_events
);
201 lttng_dynamic_pointer_array_init(&pipes
, destroy_pipe
);
203 ret
= lttng_poll_create(&poll_events
, fd_count
, 0);
205 diag("Failed to create poll set for %u file descriptors",
210 for (i
= 0; i
< fd_count
; i
++) {
211 pipe
= lttng_pipe_open(0);
214 diag("Failed to allocate pipe");
219 ret
= lttng_poll_add(&poll_events
, lttng_pipe_get_readfd(pipe
),
222 diag("Failed to add file descriptor to poll set");
227 ret
= lttng_dynamic_pointer_array_add_pointer(&pipes
, pipe
);
229 diag("Failed to add pipe to pipes array");
234 /* Ownership transferred to the pointer array. */
238 /* Write one byte for all active fds that should be active. */
239 for (i
= 0; i
< fd_count
; i
++) {
240 struct lttng_pipe
*borrowed_pipe
;
242 /* Should this fd be made active? */
243 if (!(active_fds_mask
& (1 << i
))) {
248 (lttng_pipe
*) lttng_dynamic_pointer_array_get_pointer(
252 ret
= lttng_pipe_write(
253 borrowed_pipe
, &c
, sizeof(char));
254 if (ret
!= sizeof(char)) {
255 diag("Failed to write to pipe");
261 ret
= lttng_poll_wait(&poll_events
, 0);
262 if (ret
!= active_fds_count
) {
263 diag("lttng_poll_wait returned %d, expected %u active file descriptors",
264 ret
, active_fds_count
);
273 lttng_dynamic_pointer_array_reset(&pipes
);
274 lttng_poll_clean(&poll_events
);
275 lttng_pipe_destroy(pipe
);
279 static void test_active_set_combinations(unsigned int fd_count
)
281 unsigned int i
, all_active_mask
= 0;
283 /* Do you really want to test more than 4,294,967,295 combinations? */
284 LTTNG_ASSERT(fd_count
<= 32);
286 for (i
= 0; i
< fd_count
; i
++) {
287 all_active_mask
|= (1 << i
);
290 for (i
= 0; i
<= all_active_mask
; i
++) {
291 const int ret
= run_active_set_combination(fd_count
, i
);
298 pass("Test all combinations of active file descriptors for %u file descriptors", fd_count
);
301 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count
);
304 static void test_func_def(void)
306 #ifdef LTTNG_POLL_GETFD
312 #ifdef LTTNG_POLL_GETEV
318 #ifdef LTTNG_POLL_GETSZ
324 #ifdef LTTNG_POLL_GET_PREV_FD
325 #define PASS_GET_PREV_FD 1
327 #define PASS_GET_PREV_FD 0
330 ok(lttng_poll_reset
== lttng_poll_reset
, "lttng_poll_reset is defined");
331 ok(lttng_poll_init
== lttng_poll_init
, "lttng_poll_init is defined");
332 ok(PASS_GETFD
, "GETFD is defined");
333 ok(PASS_GETEV
, "GETEV is defined");
334 ok(PASS_GETSZ
, "GETSZ is defined");
335 ok(PASS_GET_PREV_FD
, "GET_PREV_FD is defined");
340 plan_tests(NUM_TESTS
);
348 test_active_set_combinations(8);
349 return exit_status();