Fix invalid use of NULL in epoll_pwait2(2) test
[glibc.git] / sysdeps / unix / sysv / linux / tst-epoll.c
blobabda45c4275a1dbe7b35e2d04eeba2fb545f6e0d
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/>. */
19 #include <errno.h>
20 #include <intprops.h>
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>
26 #include <stdlib.h>
27 #include <sys/epoll.h>
29 /* The test focus on checking if the timeout argument is correctly handled
30 by glibc wrappers. */
32 static void
33 handler (int sig)
37 typedef int (*epoll_wait_check_t) (int, struct epoll_event *, int,
38 int, const sigset_t *);
40 static void
41 test_epoll_basic (epoll_wait_check_t epoll_wait_check)
44 sigset_t ss_usr1;
45 sigemptyset (&ss_usr1);
46 sigaddset (&ss_usr1, SIGUSR1);
47 TEST_COMPARE (sigprocmask (SIG_BLOCK, &ss_usr1, NULL), 0);
50 int fds[2][2];
51 xpipe (fds[0]);
52 xpipe (fds[1]);
54 sigset_t ss;
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 ();
68 pid_t p = xfork ();
69 if (p == 0)
71 xclose (fds[0][1]);
72 xclose (fds[1][0]);
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);
78 int e;
81 if (getppid () != parent)
82 FAIL_EXIT1 ("getppid()=%d != parent=%d", getppid(), parent);
84 errno = 0;
85 e = epoll_wait_check (efd, &event, 1, 500, &ss);
87 while (e == 0);
89 TEST_COMPARE (e, -1);
90 TEST_COMPARE (errno, EINTR);
92 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
94 exit (0);
97 xclose (fds[0][0]);
98 xclose (fds[1][1]);
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);
105 TEST_COMPARE (e, 1);
106 TEST_VERIFY (event.events & EPOLLIN);
108 xclose (fds[0][1]);
109 xclose (fds[1][0]);
110 xclose (efd);
114 static void
115 test_epoll_large_timeout (epoll_wait_check_t epoll_wait_check)
117 timer_t t = support_create_timer (0, 100000000, true, NULL);
119 int fds[2];
120 xpipe (fds);
122 fd_set rfds;
123 FD_ZERO (&rfds);
124 FD_SET (fds[0], &rfds);
126 sigset_t ss;
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);
147 xclose (fds[0]);
148 xclose (fds[1]);
149 xclose (efd);
153 static int
154 epoll_wait_check (int epfd, struct epoll_event *ev, int maxev, int tmo,
155 const sigset_t *ss)
157 sigset_t orig;
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);
161 return r;
164 static int
165 epoll_pwait_check (int epfd, struct epoll_event *ev, int maxev, int tmo,
166 const sigset_t *ss)
168 return epoll_pwait (epfd, ev, maxev, tmo, ss);
171 static int
172 epoll_pwait2_check (int epfd, struct epoll_event *ev, int maxev, int tmo,
173 const sigset_t *ss)
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);
180 static int
181 do_test (void)
183 struct epoll_event ev;
186 struct sigaction sa;
187 sa.sa_handler = handler;
188 sa.sa_flags = 0;
189 sigemptyset (&sa.sa_mask);
190 xsigaction (SIGUSR1, &sa, NULL);
192 sa.sa_handler = SIG_IGN;
193 xsigaction (SIGCHLD, &sa, NULL);
196 int r = epoll_pwait2 (-1, &ev, 0, NULL, NULL);
197 TEST_COMPARE (r, -1);
198 bool pwait2_supported = errno != ENOSYS;
200 test_epoll_basic (epoll_wait_check);
201 test_epoll_basic (epoll_pwait_check);
202 if (pwait2_supported)
203 test_epoll_basic (epoll_pwait2_check);
205 test_epoll_large_timeout (epoll_wait_check);
206 test_epoll_large_timeout (epoll_pwait_check);
207 if (pwait2_supported)
208 test_epoll_large_timeout (epoll_pwait2_check);
210 return 0;
213 #include <support/test-driver.c>