blob: 045aca0e9ba350a44b716ab6c56f8d8d01d3909a [file] [log] [blame]
Dmitry V. Levin446e5b12016-02-16 00:52:43 +00001/*
2 * This file is part of poll strace test.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 */
29
30#include "tests.h"
31#include <sys/syscall.h>
32
33#ifdef __NR_poll
34
35# include <assert.h>
36# include <errno.h>
37# include <poll.h>
38# include <stdio.h>
39# include <stdlib.h>
40# include <unistd.h>
41
42#define PRINT_EVENT(flag, member) \
43 if (member & flag) { \
44 if (member != pfd->member) \
45 tprintf("|"); \
46 tprintf(#flag); \
47 member &= ~flag; \
48 }
49
50static void
51print_pollfd_entering(const struct pollfd *const pfd)
52{
53 tprintf("{fd=%d", pfd->fd);
54 if (pfd->fd >= 0) {
55 tprintf(", events=");
56 short events = pfd->events;
57
58 if (pfd->events) {
59 PRINT_EVENT(POLLIN, events)
60 PRINT_EVENT(POLLPRI, events)
61 PRINT_EVENT(POLLOUT, events)
62#ifdef POLLRDNORM
63 PRINT_EVENT(POLLRDNORM, events)
64#endif
65#ifdef POLLWRNORM
66 PRINT_EVENT(POLLWRNORM, events)
67#endif
68#ifdef POLLRDBAND
69 PRINT_EVENT(POLLRDBAND, events)
70#endif
71#ifdef POLLWRBAND
72 PRINT_EVENT(POLLWRBAND, events)
73#endif
74 PRINT_EVENT(POLLERR, events)
75 PRINT_EVENT(POLLHUP, events)
76 PRINT_EVENT(POLLNVAL, events)
77 } else
78 tprintf("0");
79 }
80 tprintf("}");
81}
82
83static void
84print_pollfd_array_entering(const struct pollfd *const pfd,
85 const unsigned int size,
86 const unsigned int valid,
87 const unsigned int abbrev)
88{
89 tprintf("[");
90 unsigned int i;
91 for (i = 0; i < size; ++i) {
92 if (i)
93 tprintf(", ");
94 if (i >= abbrev) {
95 tprintf("...");
96 break;
97 }
98 if (i < valid)
99 print_pollfd_entering(&pfd[i]);
100 else {
101 tprintf("%p", &pfd[i]);
102 break;
103 }
104 }
105 tprintf("]");
106}
107
108static void
109print_pollfd_exiting(const struct pollfd *const pfd,
110 unsigned int *const seen,
111 const unsigned int abbrev)
112{
113 if (!pfd->revents || pfd->fd < 0 || *seen > abbrev)
114 return;
115
116 if (*seen)
117 tprintf(", ");
118 ++(*seen);
119
120 if (*seen > abbrev) {
121 tprintf("...");
122 return;
123 }
124 tprintf("{fd=%d, revents=", pfd->fd);
125 short revents = pfd->revents;
126
127 PRINT_EVENT(POLLIN, revents)
128 PRINT_EVENT(POLLPRI, revents)
129 PRINT_EVENT(POLLOUT, revents)
130#ifdef POLLRDNORM
131 PRINT_EVENT(POLLRDNORM, revents)
132#endif
133#ifdef POLLWRNORM
134 PRINT_EVENT(POLLWRNORM, revents)
135#endif
136#ifdef POLLRDBAND
137 PRINT_EVENT(POLLRDBAND, revents)
138#endif
139#ifdef POLLWRBAND
140 PRINT_EVENT(POLLWRBAND, revents)
141#endif
142 PRINT_EVENT(POLLERR, revents)
143 PRINT_EVENT(POLLHUP, revents)
144 PRINT_EVENT(POLLNVAL, revents)
145 tprintf("}");
146}
147
148static void
149print_pollfd_array_exiting(const struct pollfd *const pfd,
150 const unsigned int size,
151 const unsigned int abbrev)
152{
153 tprintf("[");
154 unsigned int seen = 0;
155 unsigned int i;
156 for (i = 0; i < size; ++i)
157 print_pollfd_exiting(&pfd[i], &seen, abbrev);
158 tprintf("]");
159}
160
161int
162main(int ac, char **av)
163{
164 tprintf("%s", "");
165
166 assert(syscall(__NR_poll, NULL, 42, 0) == -1);
167 if (ENOSYS == errno)
168 perror_msg_and_skip("poll");
169 tprintf("poll(NULL, 42, 0) = -1 EFAULT (%m)\n");
170
171 int fds[2];
172 if (pipe(fds) || pipe(fds))
173 perror_msg_and_fail("pipe");
174
175 const unsigned int abbrev = (ac > 1) ? atoi(av[1]) : -1;
176 const struct pollfd pfds0[] = {
177 { .fd = 0, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
178 { .fd = 1, .events = POLLOUT | POLLWRNORM | POLLWRBAND },
179 { .fd = fds[0], .events = POLLIN | POLLPRI },
180 { .fd = fds[1], .events = POLLOUT },
181 { .fd = 2, .events = POLLOUT | POLLWRBAND }
182 };
183 struct pollfd *const tail_fds0 = tail_memdup(pfds0, sizeof(pfds0));
184 const int timeout = 42;
185 int rc = syscall(__NR_poll, tail_fds0, 0, timeout);
186 assert(rc == 0);
187
188 tprintf("poll(%p, 0, %d) = %d (Timeout)\n", tail_fds0, timeout, rc);
189
190 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
191 assert(rc == 3);
192
193 tprintf("poll(");
194 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
195 ARRAY_SIZE(pfds0), abbrev);
196 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
197 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
198 tprintf(")\n");
199
200 tail_fds0[0].fd = -1;
201 tail_fds0[2].fd = -3;
202 tail_fds0[4].events = 0;
203 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
204 assert(rc == 2);
205
206 tprintf("poll(");
207 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
208 ARRAY_SIZE(pfds0), abbrev);
209 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
210 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
211 tprintf(")\n");
212
213 tail_fds0[1].fd = -2;
214 tail_fds0[4].fd = -5;
215 rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
216 assert(rc == 1);
217
218 tprintf("poll(");
219 print_pollfd_array_entering(tail_fds0, ARRAY_SIZE(pfds0),
220 ARRAY_SIZE(pfds0), abbrev);
221 tprintf(", %u, %d) = %d (", ARRAY_SIZE(pfds0), timeout, rc);
222 print_pollfd_array_exiting(tail_fds0, ARRAY_SIZE(pfds0), abbrev);
223 tprintf(")\n");
224
225 struct pollfd pfds1[] = {
226 { .fd = 1, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
227 { .fd = 0, .events = POLLOUT | POLLWRNORM | POLLWRBAND }
228 };
229 struct pollfd *const tail_fds1 = tail_memdup(pfds1, sizeof(pfds1));
230 rc = syscall(__NR_poll, tail_fds1, ARRAY_SIZE(pfds1), timeout);
231 assert(rc == 0);
232
233 tprintf("poll(");
234 print_pollfd_array_entering(tail_fds1, ARRAY_SIZE(pfds1),
235 ARRAY_SIZE(pfds1), abbrev);
236 tprintf(", %u, %d) = %d (Timeout)\n", ARRAY_SIZE(pfds1), timeout, rc);
237
238 const void *const efault = tail_fds0 + ARRAY_SIZE(pfds0);
239 rc = syscall(__NR_poll, efault, 1, 0);
240 assert(rc == -1);
241 tprintf("poll(%p, 1, 0) = -1 EFAULT (%m)\n", efault);
242
243 const unsigned int valid = 1;
244 const void *const epfds = tail_fds0 + ARRAY_SIZE(pfds0) - valid;
245 rc = syscall(__NR_poll, epfds, valid + 1, 0);
246 assert(rc == -1);
247 tprintf("poll(");
248 print_pollfd_array_entering(epfds, valid + 1, valid, abbrev);
249 errno = EFAULT;
250 tprintf(", %u, 0) = -1 EFAULT (%m)\n", valid + 1);
251
252 tprintf("+++ exited with 0 +++\n");
253 return 0;
254}
255
256#else
257
258SKIP_MAIN_UNDEFINED("__NR_poll")
259
260#endif