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) \
43 if (member & flag) { \
44 if (member != pfd->member) \
51 print_pollfd_entering(const struct pollfd *const pfd)
53 tprintf("{fd=%d", pfd->fd);
56 short events = pfd->events;
59 PRINT_EVENT(POLLIN, events)
60 PRINT_EVENT(POLLPRI, events)
61 PRINT_EVENT(POLLOUT, events)
63 PRINT_EVENT(POLLRDNORM, events)
66 PRINT_EVENT(POLLWRNORM, events)
69 PRINT_EVENT(POLLRDBAND, events)
72 PRINT_EVENT(POLLWRBAND, events)
74 PRINT_EVENT(POLLERR, events)
75 PRINT_EVENT(POLLHUP, events)
76 PRINT_EVENT(POLLNVAL, events)
84 print_pollfd_array_entering(const struct pollfd *const pfd,
85 const unsigned int size,
86 const unsigned int valid,
87 const unsigned int abbrev)
91 for (i = 0; i < size; ++i) {
95 tprintf("%p", &pfd[i]);
102 print_pollfd_entering(&pfd[i]);
108 print_pollfd_exiting(const struct pollfd *const pfd,
109 unsigned int *const seen,
110 const unsigned int abbrev)
112 if (!pfd->revents || pfd->fd < 0 || *seen > abbrev)
119 if (*seen > abbrev) {
123 tprintf("{fd=%d, revents=", pfd->fd);
124 short revents = pfd->revents;
126 PRINT_EVENT(POLLIN, revents)
127 PRINT_EVENT(POLLPRI, revents)
128 PRINT_EVENT(POLLOUT, revents)
130 PRINT_EVENT(POLLRDNORM, revents)
133 PRINT_EVENT(POLLWRNORM, revents)
136 PRINT_EVENT(POLLRDBAND, revents)
139 PRINT_EVENT(POLLWRBAND, revents)
141 PRINT_EVENT(POLLERR, revents)
142 PRINT_EVENT(POLLHUP, revents)
143 PRINT_EVENT(POLLNVAL, revents)
148 print_pollfd_array_exiting(const struct pollfd *const pfd,
149 const unsigned int size,
150 const unsigned int abbrev)
153 unsigned int seen = 0;
155 for (i = 0; i < size; ++i)
156 print_pollfd_exiting(&pfd[i], &seen, abbrev);
161 main(int ac, char **av)
165 assert(syscall(__NR_poll, NULL, 42, 0) == -1);
167 perror_msg_and_skip("poll");
168 tprintf("poll(NULL, 42, 0) = -1 EFAULT (%m)\n");
171 if (pipe(fds) || pipe(fds))
172 perror_msg_and_fail("pipe");
174 const unsigned int abbrev = (ac > 1) ? atoi(av[1]) : -1;
175 const struct pollfd pfds0[] = {
176 { .fd = 0, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
177 { .fd = 1, .events = POLLOUT | POLLWRNORM | POLLWRBAND },
178 { .fd = fds[0], .events = POLLIN | POLLPRI },
179 { .fd = fds[1], .events = POLLOUT },
180 { .fd = 2, .events = POLLOUT | POLLWRBAND }
182 struct pollfd *const tail_fds0 = tail_memdup(pfds0, sizeof(pfds0));
183 const int timeout = 42;
184 int rc = syscall(__NR_poll, tail_fds0, 0, timeout);
187 tprintf("poll([], 0, %d) = %d (Timeout)\n", timeout, rc);
189 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
193 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
194 ARRAY_SIZE(pfds0), abbrev);
195 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
196 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
199 tail_fds0[0].fd = -1;
200 tail_fds0[2].fd = -3;
201 tail_fds0[4].events = 0;
202 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
206 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
207 ARRAY_SIZE(pfds0), abbrev);
208 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
209 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
212 tail_fds0[1].fd = -2;
213 tail_fds0[4].fd = -5;
214 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
218 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
219 ARRAY_SIZE(pfds0), abbrev);
220 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
221 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
224 struct pollfd pfds1[] = {
225 { .fd = 1, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
226 { .fd = 0, .events = POLLOUT | POLLWRNORM | POLLWRBAND }
228 struct pollfd *const tail_fds1 = tail_memdup(pfds1, sizeof(pfds1));
229 rc = syscall(__NR_poll, tail_fds1, ARRAY_SIZE(pfds1), timeout);
233 print_pollfd_array_entering(tail_fds1, ARRAY_SIZE(pfds1),
234 ARRAY_SIZE(pfds1), abbrev);
235 tprintf(", %u, %d) = %d (Timeout)\n", ARRAY_SIZE(pfds1), timeout, rc);
237 const void *const efault = tail_fds0 + ARRAY_SIZE(pfds0);
238 rc = syscall(__NR_poll, efault, 1, 0);
240 tprintf("poll(%p, 1, 0) = -1 EFAULT (%m)\n", efault);
242 const unsigned int valid = 1;
243 const void *const epfds = tail_fds0 + ARRAY_SIZE(pfds0) - valid;
244 rc = syscall(__NR_poll, epfds, valid + 1, 0);
247 print_pollfd_array_entering(epfds, valid + 1, valid, abbrev);
249 tprintf(", %u, 0) = -1 EFAULT (%m)\n", valid + 1);
251 tprintf("+++ exited with 0 +++\n");
257 SKIP_MAIN_UNDEFINED("__NR_poll")