1 /*
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 <asm/unistd.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 
50 static void
print_pollfd_entering(const struct pollfd * const pfd)51 print_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 
83 static void
print_pollfd_array_entering(const struct pollfd * const pfd,const unsigned int size,const unsigned int valid,const unsigned int abbrev)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)
88 {
89 	tprintf("[");
90 	unsigned int i;
91 	for (i = 0; i < size; ++i) {
92 		if (i)
93 			tprintf(", ");
94 		if (i >= valid) {
95 			tprintf("%p", &pfd[i]);
96 			break;
97 		}
98 		if (i >= abbrev) {
99 			tprintf("...");
100 			break;
101 		}
102 		print_pollfd_entering(&pfd[i]);
103 	}
104 	tprintf("]");
105 }
106 
107 static void
print_pollfd_exiting(const struct pollfd * const pfd,unsigned int * const seen,const unsigned int abbrev)108 print_pollfd_exiting(const struct pollfd *const pfd,
109 		     unsigned int *const seen,
110 		     const unsigned int abbrev)
111 {
112 	if (!pfd->revents || pfd->fd < 0 || *seen > abbrev)
113 		return;
114 
115 	if (*seen)
116 		tprintf(", ");
117 	++(*seen);
118 
119 	if (*seen > abbrev) {
120 		tprintf("...");
121 		return;
122 	}
123 	tprintf("{fd=%d, revents=", pfd->fd);
124 	short revents = pfd->revents;
125 
126 	PRINT_EVENT(POLLIN, revents)
127 	PRINT_EVENT(POLLPRI, revents)
128 	PRINT_EVENT(POLLOUT, revents)
129 #ifdef POLLRDNORM
130 	PRINT_EVENT(POLLRDNORM, revents)
131 #endif
132 #ifdef POLLWRNORM
133 	PRINT_EVENT(POLLWRNORM, revents)
134 #endif
135 #ifdef POLLRDBAND
136 	PRINT_EVENT(POLLRDBAND, revents)
137 #endif
138 #ifdef POLLWRBAND
139 	PRINT_EVENT(POLLWRBAND, revents)
140 #endif
141 	PRINT_EVENT(POLLERR, revents)
142 	PRINT_EVENT(POLLHUP, revents)
143 	PRINT_EVENT(POLLNVAL, revents)
144 	tprintf("}");
145 }
146 
147 static void
print_pollfd_array_exiting(const struct pollfd * const pfd,const unsigned int size,const unsigned int abbrev)148 print_pollfd_array_exiting(const struct pollfd *const pfd,
149 			   const unsigned int size,
150 			   const unsigned int abbrev)
151 {
152 	tprintf("[");
153 	unsigned int seen = 0;
154 	unsigned int i;
155 	for (i = 0; i < size; ++i)
156 		print_pollfd_exiting(&pfd[i], &seen, abbrev);
157 	tprintf("]");
158 }
159 
160 int
main(int ac,char ** av)161 main(int ac, char **av)
162 {
163 	tprintf("%s", "");
164 
165 	assert(syscall(__NR_poll, NULL, 42, 0) == -1);
166 	if (ENOSYS == errno)
167 		perror_msg_and_skip("poll");
168 	tprintf("poll(NULL, 42, 0) = -1 EFAULT (%m)\n");
169 
170 	int fds[2];
171 	if (pipe(fds) || pipe(fds))
172 		perror_msg_and_fail("pipe");
173 
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 }
181 	};
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);
185 	assert(rc == 0);
186 
187 	tprintf("poll([], 0, %d) = %d (Timeout)\n", timeout, rc);
188 
189 	rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
190 	assert(rc == 3);
191 
192 	tprintf("poll(");
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);
197 	tprintf(")\n");
198 
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);
203 	assert(rc == 2);
204 
205 	tprintf("poll(");
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);
210 	tprintf(")\n");
211 
212 	tail_fds0[1].fd = -2;
213 	tail_fds0[4].fd = -5;
214 	rc = syscall(__NR_poll, tail_fds0, ARRAY_SIZE(pfds0), timeout);
215 	assert(rc == 1);
216 
217 	tprintf("poll(");
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);
222 	tprintf(")\n");
223 
224 	struct pollfd pfds1[] = {
225 		{ .fd = 1, .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
226 		{ .fd = 0, .events = POLLOUT | POLLWRNORM | POLLWRBAND }
227 	};
228 	struct pollfd *const tail_fds1 = tail_memdup(pfds1, sizeof(pfds1));
229 	rc = syscall(__NR_poll, tail_fds1, ARRAY_SIZE(pfds1), timeout);
230 	assert(rc == 0);
231 
232 	tprintf("poll(");
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);
236 
237 	const void *const efault = tail_fds0 + ARRAY_SIZE(pfds0);
238 	rc = syscall(__NR_poll, efault, 1, 0);
239 	assert(rc == -1);
240 	tprintf("poll(%p, 1, 0) = -1 EFAULT (%m)\n", efault);
241 
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);
245 	assert(rc == -1);
246 	tprintf("poll(");
247 	print_pollfd_array_entering(epfds, valid + 1, valid, abbrev);
248 	errno = EFAULT;
249 	tprintf(", %u, 0) = -1 EFAULT (%m)\n", valid + 1);
250 
251 	tprintf("+++ exited with 0 +++\n");
252 	return 0;
253 }
254 
255 #else
256 
257 SKIP_MAIN_UNDEFINED("__NR_poll")
258 
259 #endif
260