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
17 #include <sys/types.h>
22 #include <common/compat/poll.h>
23 #include <common/readwrite.h>
24 #include <common/pipe.h>
25 #include <common/dynamic-array.h>
27 /* Verification without trashing test order in the child process */
28 #define childok(e, test, ...) do { \
30 diag(test, ## __VA_ARGS__); \
31 _exit(EXIT_FAILURE); \
36 int lttng_opt_quiet
= 1;
37 int lttng_opt_verbose
;
41 * Non-zero 8-bits arbitrary value below 0x7f to ensure no sign extension
42 * occurs. Used to verify that the value is properly propagated through the
45 #define MAGIC_VALUE ((char) 0x5A)
54 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
55 #define CLOE_VALUE EPOLL_CLOEXEC
57 #define CLOE_VALUE FD_CLOEXEC
61 void test_epoll_compat(void)
64 * Type conversion present to disable warning of anonymous enum from
67 ok((int) LTTNG_CLOEXEC
== (int) CLOE_VALUE
, "epoll's CLOEXEC value");
71 static void test_alloc(void)
73 struct lttng_poll_event poll_events
;
75 lttng_poll_init(&poll_events
);
78 ok(lttng_poll_create(NULL
, 1, 0) != 0, "Create over NULL pointer fails");
80 ok(lttng_poll_create(&poll_events
, 0, 0) != 0, "Create with size 0 fails");
82 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
84 * lttng_poll_event structure untested due to incompatibility across
85 * sublayers. lttng_poll_clean cannot be tested. There is no success
86 * criteria. Verify set's max size cases.
88 lttng_poll_clean(&poll_events
);
91 /* Tests stuff related to what would be handled with epoll_ctl. */
92 static void test_add_del(void)
94 struct lttng_poll_event poll_events
;
96 lttng_poll_init(&poll_events
);
97 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
98 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) != 0, "Adding to uninitialized structure fails");
99 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
101 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create a poll set succeeds");
102 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set created empty");
104 ok(lttng_poll_add(NULL
, 1, LPOLLIN
) != 0, "Adding to NULL set fails");
105 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
106 ok(lttng_poll_add(&poll_events
, -1, LPOLLIN
) != 0, "Adding invalid FD fails");
107 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Set still empty");
109 ok(lttng_poll_add(&poll_events
, 1, LPOLLIN
) == 0, "Adding valid FD succeeds");
110 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Nb of elements incremented");
112 ok(lttng_poll_del(NULL
, 1) != 0, "Removing from NULL set fails");
113 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
115 ok(lttng_poll_del(&poll_events
, -1) != 0, "Removing from negative FD fails");
116 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of FD in set unchanged");
118 ok(lttng_poll_del(&poll_events
, 2) == 0, "Removing invalid FD still succeeds");
119 ok(LTTNG_POLL_GETNB(&poll_events
) == 1, "Number of elements unchanged");
121 ok(lttng_poll_del(&poll_events
, 1) == 0, "Removing valid FD succeeds");
122 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements decremented");
124 ok(lttng_poll_del(&poll_events
, 1) != 0, "Removing from empty set fails");
125 ok(LTTNG_POLL_GETNB(&poll_events
) == 0, "Nb of elements unchanged");
127 lttng_poll_clean(&poll_events
);
130 static void test_mod_wait(void)
132 struct lttng_poll_event poll_events
;
133 struct lttng_poll_event cpoll_events
;
137 char rbuf
= 0, tbuf
= MAGIC_VALUE
;
140 lttng_poll_init(&poll_events
);
141 lttng_poll_init(&cpoll_events
);
143 ok(pipe(hupfd
) != -1, "pipe function succeeds");
144 ok(pipe(infd
) != -1, "pipe function succeeds");
148 childok(lttng_poll_create(&cpoll_events
, 1, 0) == 0, "Create valid poll set succeeds");
149 childok(lttng_poll_mod(NULL
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
150 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
151 childok(lttng_poll_add(&cpoll_events
, infd
[0], LPOLLHUP
) == 0, "Add valid FD succeeds");
152 childok(lttng_poll_mod(&cpoll_events
, -1, LPOLLIN
) == -1, "lttng_poll_mod with invalid input returns an error");
153 childok(lttng_poll_mod(&cpoll_events
, hupfd
[0], LPOLLIN
) == 0, "lttng_poll_mod on unincluded FD goes on");
154 childok(lttng_poll_mod(&cpoll_events
, infd
[0], LPOLLIN
) == 0, "Modify event type succeeds");
155 childok(close(infd
[1]) == 0, "Close valid FD succeeds");
156 childok(lttng_poll_wait(&cpoll_events
, -1) == 1, "Wait on close times out");
157 childok(lttng_read(infd
[0], &rbuf
, 1) == 1, "Data is present in the pipe");
158 childok(rbuf
== MAGIC_VALUE
, "Received data is consistent with transmitted data");
159 childok(lttng_poll_del(&cpoll_events
, infd
[0]) == 0, "Removing valid FD succeeds");
160 childok(close(infd
[0]) == 0, "Close valid FD succeeds");
161 childok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
162 childok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
163 lttng_poll_clean(&cpoll_events
);
166 ok(close(hupfd
[1]) == 0, "Close valid FD succeeds");
167 ok(close(infd
[0]) == 0, "Close valid FD succeeds");
169 ok(lttng_poll_wait(NULL
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
171 ok(lttng_poll_create(&poll_events
, 1, 0) == 0, "Create valid poll set succeeds");
172 ok(lttng_poll_wait(&poll_events
, -1) == -1, "lttng_poll_wait call with invalid input returns error");
173 ok(lttng_poll_add(&poll_events
, hupfd
[0], LPOLLHUP
) == 0, "Add valid FD succeeds");
174 ok(lttng_write(infd
[1], &tbuf
, 1) == 1, "Write to pipe succeeds");
175 ok(lttng_poll_wait(&poll_events
, -1) == 1, "Wakes up on one event");
176 ok(lttng_poll_del(&poll_events
, hupfd
[0]) == 0, "Removing valid FD succeeds");
177 ok(close(hupfd
[0]) == 0, "Close valid FD succeeds");
178 ok(close(infd
[1]) == 0, "Close valid FD succeeds");
179 lttng_poll_clean(&poll_events
);
180 ok(waitpid(cpid
, &wstatus
, 0) == cpid
, "Wait for child exit");
181 ok(WIFEXITED(wstatus
) == 1, "Child process exited");
182 ok(WEXITSTATUS(wstatus
) == EXIT_SUCCESS
, "Child process exited with EXIT_SUCCESS");
186 static void destroy_pipe(void *pipe
)
188 lttng_pipe_destroy(pipe
);
191 static int run_active_set_combination(unsigned int fd_count
,
192 unsigned int active_fds_mask
)
196 const unsigned int active_fds_count
= __builtin_popcount(active_fds_mask
);
197 struct lttng_poll_event poll_events
;
198 struct lttng_dynamic_pointer_array pipes
;
199 struct lttng_pipe
*pipe
= NULL
;
201 lttng_poll_init(&poll_events
);
202 lttng_dynamic_pointer_array_init(&pipes
, destroy_pipe
);
204 ret
= lttng_poll_create(&poll_events
, fd_count
, 0);
206 diag("Failed to create poll set for %u file descriptors",
211 for (i
= 0; i
< fd_count
; i
++) {
212 pipe
= lttng_pipe_open(0);
215 diag("Failed to allocate pipe");
220 ret
= lttng_poll_add(&poll_events
, lttng_pipe_get_readfd(pipe
),
223 diag("Failed to add file descriptor to poll set");
228 ret
= lttng_dynamic_pointer_array_add_pointer(&pipes
, pipe
);
230 diag("Failed to add pipe to pipes array");
235 /* Ownership transferred to the pointer array. */
239 /* Write one byte for all active fds that should be active. */
240 for (i
= 0; i
< fd_count
; i
++) {
241 struct lttng_pipe
*pipe
;
243 /* Should this fd be made active? */
244 if (!(active_fds_mask
& (1 << i
))) {
248 pipe
= lttng_dynamic_pointer_array_get_pointer(&pipes
, i
);
250 ret
= lttng_pipe_write(pipe
, &(char) {'a'}, sizeof(char));
251 if (ret
!= sizeof(char)) {
252 diag("Failed to write to pipe");
258 ret
= lttng_poll_wait(&poll_events
, 0);
259 if (ret
!= active_fds_count
) {
260 diag("lttng_poll_wait returned %d, expected %u active file descriptors",
261 ret
, active_fds_count
);
270 lttng_dynamic_pointer_array_reset(&pipes
);
271 lttng_poll_clean(&poll_events
);
272 lttng_pipe_destroy(pipe
);
276 static void test_active_set_combinations(unsigned int fd_count
)
278 unsigned int i
, all_active_mask
= 0;
280 /* Do you really want to test more than 4,294,967,295 combinations? */
281 assert(fd_count
<= 32);
283 for (i
= 0; i
< fd_count
; i
++) {
284 all_active_mask
|= (1 << i
);
287 for (i
= 0; i
<= all_active_mask
; i
++) {
288 const int ret
= run_active_set_combination(fd_count
, i
);
295 pass("Test all combinations of active file descriptors for %u file descriptors", fd_count
);
298 fail("Test all combinations of active file descriptors for %u file descriptors", fd_count
);
301 static void test_func_def(void)
303 #ifdef LTTNG_POLL_GETFD
309 #ifdef LTTNG_POLL_GETEV
315 #ifdef LTTNG_POLL_GETSZ
321 #ifdef LTTNG_POLL_GET_PREV_FD
322 #define PASS_GET_PREV_FD 1
324 #define PASS_GET_PREV_FD 0
327 ok(lttng_poll_reset
== lttng_poll_reset
, "lttng_poll_reset is defined");
328 ok(lttng_poll_init
== lttng_poll_init
, "lttng_poll_init is defined");
329 ok(PASS_GETFD
, "GETFD is defined");
330 ok(PASS_GETEV
, "GETEV is defined");
331 ok(PASS_GETSZ
, "GETSZ is defined");
332 ok(PASS_GET_PREV_FD
, "GET_PREV_FD is defined");
337 plan_tests(NUM_TESTS
);
345 test_active_set_combinations(8);
346 return exit_status();