1 /* Basic tests for Linux epoll_* wrappers.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <support/check.h>
22 #include <support/support.h>
23 #include <support/xsignal.h>
24 #include <support/xunistd.h>
25 #include <support/xtime.h>
27 #include <sys/epoll.h>
29 /* The test focus on checking if the timeout argument is correctly handled
37 typedef int (*epoll_wait_check_t
) (int, struct epoll_event
*, int,
38 int, const sigset_t
*);
41 test_epoll_basic (epoll_wait_check_t epoll_wait_check
)
45 sigemptyset (&ss_usr1
);
46 sigaddset (&ss_usr1
, SIGUSR1
);
47 TEST_COMPARE (sigprocmask (SIG_BLOCK
, &ss_usr1
, NULL
), 0);
55 TEST_COMPARE (sigprocmask (SIG_SETMASK
, NULL
, &ss
), 0);
56 sigdelset (&ss
, SIGUSR1
);
58 int efd
= epoll_create1 (0);
59 TEST_VERIFY_EXIT (efd
!= -1);
61 struct epoll_event event
;
63 event
.data
.fd
= fds
[1][0];
64 event
.events
= EPOLLIN
| EPOLLET
;
65 TEST_COMPARE (epoll_ctl (efd
, EPOLL_CTL_ADD
, fds
[1][0], &event
), 0);
67 pid_t parent
= getpid ();
74 event
.data
.fd
= fds
[0][0];
75 event
.events
= EPOLLIN
| EPOLLET
;
76 TEST_COMPARE (epoll_ctl (efd
, EPOLL_CTL_ADD
, fds
[0][0], &event
), 0);
81 if (getppid () != parent
)
82 FAIL_EXIT1 ("getppid()=%d != parent=%d", getppid(), parent
);
85 e
= epoll_wait_check (efd
, &event
, 1, 500, &ss
);
90 TEST_COMPARE (errno
, EINTR
);
92 TEMP_FAILURE_RETRY (write (fds
[1][1], "foo", 3));
100 /* Wait some time so child is blocked on the syscall. */
101 nanosleep (&(struct timespec
) {0, 10000000}, NULL
);
102 TEST_COMPARE (kill (p
, SIGUSR1
), 0);
104 int e
= epoll_wait_check (efd
, &event
, 1, 500000000, &ss
);
106 TEST_VERIFY (event
.events
& EPOLLIN
);
115 test_epoll_large_timeout (epoll_wait_check_t epoll_wait_check
)
117 timer_t t
= support_create_timer (0, 100000000, true, NULL
);
124 FD_SET (fds
[0], &rfds
);
127 TEST_COMPARE (sigprocmask (SIG_SETMASK
, NULL
, &ss
), 0);
128 sigdelset (&ss
, SIGALRM
);
130 int efd
= epoll_create1 (0);
131 TEST_VERIFY_EXIT (efd
!= -1);
133 struct epoll_event event
;
134 event
.data
.fd
= fds
[0];
135 event
.events
= EPOLLIN
| EPOLLET
;
136 TEST_COMPARE (epoll_ctl (efd
, EPOLL_CTL_ADD
, fds
[0], &event
), 0);
138 int tmo
= TYPE_MAXIMUM (int);
139 TEST_COMPARE (epoll_wait_check (efd
, &event
, 1, tmo
, &ss
), -1);
140 TEST_VERIFY (errno
== EINTR
|| errno
== EOVERFLOW
);
142 TEST_COMPARE (epoll_wait_check (efd
, &event
, 1, -1, &ss
), -1);
143 TEST_VERIFY (errno
== EINTR
|| errno
== EOVERFLOW
);
145 support_delete_timer (t
);
154 epoll_wait_check (int epfd
, struct epoll_event
*ev
, int maxev
, int tmo
,
158 TEST_COMPARE (sigprocmask (SIG_SETMASK
, ss
, &orig
), 0);
159 int r
= epoll_wait (epfd
, ev
, maxev
, tmo
);
160 TEST_COMPARE (sigprocmask (SIG_SETMASK
, &orig
, NULL
), 0);
165 epoll_pwait_check (int epfd
, struct epoll_event
*ev
, int maxev
, int tmo
,
168 return epoll_pwait (epfd
, ev
, maxev
, tmo
, ss
);
172 epoll_pwait2_check (int epfd
, struct epoll_event
*ev
, int maxev
, int tmo
,
175 time_t s
= tmo
== -1 ? TYPE_MAXIMUM (time_t) : tmo
/ 1000;
176 long int ns
= tmo
== -1 ? 0 : (tmo
% 1000) * 1000000;
177 return epoll_pwait2 (epfd
, ev
, maxev
, &(struct timespec
) { s
, ns
}, ss
);
185 sa
.sa_handler
= handler
;
187 sigemptyset (&sa
.sa_mask
);
188 xsigaction (SIGUSR1
, &sa
, NULL
);
190 sa
.sa_handler
= SIG_IGN
;
191 xsigaction (SIGCHLD
, &sa
, NULL
);
194 int r
= epoll_pwait2 (-1, NULL
, 0, NULL
, NULL
);
195 TEST_COMPARE (r
, -1);
196 bool pwait2_supported
= errno
!= ENOSYS
;
198 test_epoll_basic (epoll_wait_check
);
199 test_epoll_basic (epoll_pwait_check
);
200 if (pwait2_supported
)
201 test_epoll_basic (epoll_pwait2_check
);
203 test_epoll_large_timeout (epoll_wait_check
);
204 test_epoll_large_timeout (epoll_pwait_check
);
205 if (pwait2_supported
)
206 test_epoll_large_timeout (epoll_pwait2_check
);
211 #include <support/test-driver.c>