2 * This file is part of poll strace test.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <asm/unistd.h>
42 #define PRINT_EVENT(flag, member) \
44 if (member & flag) { \
45 if (member != pfd->member) \
53 print_pollfd_entering(const struct pollfd *const pfd)
55 tprintf("{fd=%d", pfd->fd);
58 short events = pfd->events;
61 PRINT_EVENT(POLLIN, events);
62 PRINT_EVENT(POLLPRI, events);
63 PRINT_EVENT(POLLOUT, events);
65 PRINT_EVENT(POLLRDNORM, events);
68 PRINT_EVENT(POLLWRNORM, events);
71 PRINT_EVENT(POLLRDBAND, events);
74 PRINT_EVENT(POLLWRBAND, events);
76 PRINT_EVENT(POLLERR, events);
77 PRINT_EVENT(POLLHUP, events);
78 PRINT_EVENT(POLLNVAL, events);
86 print_pollfd_array_entering(const struct pollfd *const pfd,
87 const unsigned int size,
88 const unsigned int valid,
89 const unsigned int abbrev)
93 for (i = 0; i < size; ++i) {
97 tprintf("%p", &pfd[i]);
104 print_pollfd_entering(&pfd[i]);
110 print_pollfd_exiting(const struct pollfd *const pfd,
111 unsigned int *const seen,
112 const unsigned int abbrev)
114 if (!pfd->revents || pfd->fd < 0 || *seen > abbrev)
121 if (*seen > abbrev) {
125 tprintf("{fd=%d, revents=", pfd->fd);
126 short revents = pfd->revents;
128 PRINT_EVENT(POLLIN, revents);
129 PRINT_EVENT(POLLPRI, revents);
130 PRINT_EVENT(POLLOUT, revents);
132 PRINT_EVENT(POLLRDNORM, revents);
135 PRINT_EVENT(POLLWRNORM, revents);
138 PRINT_EVENT(POLLRDBAND, revents);
141 PRINT_EVENT(POLLWRBAND, revents);
143 PRINT_EVENT(POLLERR, revents);
144 PRINT_EVENT(POLLHUP, revents);
145 PRINT_EVENT(POLLNVAL, revents);
150 print_pollfd_array_exiting(const struct pollfd *const pfd,
151 const unsigned int size,
152 const unsigned int abbrev)
155 unsigned int seen = 0;
157 for (i = 0; i < size; ++i)
158 print_pollfd_exiting(&pfd[i], &seen, abbrev);
163 main(int ac, char **av)
167 assert(syscall(__NR_poll, NULL, 42, 0) == -1);
169 perror_msg_and_skip("poll");
170 tprintf("poll(NULL, 42, 0) = -1 EFAULT (%m)\n");
173 if (pipe(fds) || pipe(fds))
174 perror_msg_and_fail("pipe");
176 const unsigned int abbrev = (ac > 1) ? atoi(av[1]) : -1;
177 const struct pollfd pfds0[] = {
178 { .fd = 0, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
179 { .fd = 1, .events = POLLOUT | POLLWRNORM | POLLWRBAND },
180 { .fd = fds[0], .events = POLLIN | POLLPRI },
181 { .fd = fds[1], .events = POLLOUT },
182 { .fd = 2, .events = POLLOUT | POLLWRBAND }
184 struct pollfd *const tail_fds0 = tail_memdup(pfds0, sizeof(pfds0));
185 const int timeout = 42;
186 int rc = syscall(__NR_poll, tail_fds0, 0, timeout);
189 tprintf("poll([], 0, %d) = %d (Timeout)\n", timeout, rc);
191 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
195 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
196 ARRAY_SIZE(pfds0), abbrev);
197 tprintf(", %u, %d) = %d (",
198 (unsigned int) ARRAY_SIZE(pfds0), timeout, rc);
199 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
202 tail_fds0[0].fd = -1;
203 tail_fds0[2].fd = -3;
204 tail_fds0[4].events = 0;
205 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
209 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
210 ARRAY_SIZE(pfds0), abbrev);
211 tprintf(", %u, %d) = %d (",
212 (unsigned int) ARRAY_SIZE(pfds0), timeout, rc);
213 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
216 tail_fds0[1].fd = -2;
217 tail_fds0[4].fd = -5;
218 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
222 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
223 ARRAY_SIZE(pfds0), abbrev);
224 tprintf(", %u, %d) = %d (",
225 (unsigned int) ARRAY_SIZE(pfds0), timeout, rc);
226 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
229 struct pollfd pfds1[] = {
230 { .fd = 1, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
231 { .fd = 0, .events = POLLOUT | POLLWRNORM | POLLWRBAND }
233 struct pollfd *const tail_fds1 = tail_memdup(pfds1, sizeof(pfds1));
234 rc = syscall(__NR_poll, tail_fds1, ARRAY_SIZE(pfds1), timeout);
238 print_pollfd_array_entering(tail_fds1, ARRAY_SIZE(pfds1),
239 ARRAY_SIZE(pfds1), abbrev);
240 tprintf(", %u, %d) = %d (Timeout)\n",
241 (unsigned int) ARRAY_SIZE(pfds1), timeout, rc);
243 const void *const efault = tail_fds0 + ARRAY_SIZE(pfds0);
244 rc = syscall(__NR_poll, efault, 1, 0);
246 tprintf("poll(%p, 1, 0) = -1 EFAULT (%m)\n", efault);
248 const unsigned int valid = 1;
249 const void *const epfds = tail_fds0 + ARRAY_SIZE(pfds0) - valid;
250 rc = syscall(__NR_poll, epfds, valid + 1, 0);
253 print_pollfd_array_entering(epfds, valid + 1, valid, abbrev);
255 tprintf(", %u, 0) = -1 EFAULT (%m)\n", valid + 1);
257 tprintf("+++ exited with 0 +++\n");
263 SKIP_MAIN_UNDEFINED("__NR_poll")