summaryrefslogtreecommitdiff
path: root/tests/ppoll.c
blob: bf3426fbf6473e37dd904eb0e6761e3a6b3bb304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Check decoding of ppoll syscall.
 *
 * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2015-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"

#ifdef __NR_ppoll

# include <errno.h>
# include <poll.h>
# include <signal.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>

# ifndef PATH_TRACING_FD
#  define PATH_TRACING_FD 0
# endif
# ifndef TRACING_FDS
#  define TRACING_FDS 0
# endif
# ifndef TRACING_FD1
#  define TRACING_FD1 0
# endif
# ifndef TRACING_FD2
#  define TRACING_FD2 0
# endif
# ifndef TRACE_FD1
#  define TRACE_FD1 (!PATH_TRACING_FD && (!TRACING_FDS || TRACING_FD1))
# endif
# ifndef TRACE_FD2
#  define TRACE_FD2 (!PATH_TRACING_FD && (!TRACING_FDS || TRACING_FD2))
# endif
# ifndef TRACE_OTHER_FDS
#  define TRACE_OTHER_FDS (!PATH_TRACING_FD && !TRACING_FDS)
# endif

static const char *errstr;

static long
sys_ppoll(const kernel_ulong_t ufds,
	  const kernel_ulong_t nfds,
	  const kernel_ulong_t tsp,
	  const kernel_ulong_t sigmask,
	  const kernel_ulong_t sigsetsize)
{
	long rc = syscall(__NR_ppoll, ufds, nfds, tsp, sigmask, sigsetsize);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
# if PATH_TRACING_FD
	skip_if_unavailable("/proc/self/fd/");
# endif

	static const kernel_ulong_t bogus_nfds =
		(kernel_ulong_t) 0xdeadbeeffacefeedULL;
	static const kernel_ulong_t bogus_sigsetsize =
		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;
	static const char *const POLLWRNORM_str =
		(POLLWRNORM == POLLOUT) ? "" : "|POLLWRNORM";
	static const char *const USR2_CHLD_str =
		(SIGUSR2 < SIGCHLD) ? "USR2 CHLD" : "CHLD USR2";
	void *const efault = tail_alloc(1024) + 1024;
	TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
	const unsigned int sigset_size = get_sigset_size();
	void *const sigmask = tail_alloc(sigset_size);
	struct pollfd *fds;
	sigset_t mask;
	int pipe_fd[4];
	long rc;

	sys_ppoll(0, bogus_nfds, 0, 0, bogus_sigsetsize);
	if (ENOSYS == errno)
		perror_msg_and_skip("ppoll");
# if !PATH_TRACING_FD && !TRACING_FDS
	printf("ppoll(NULL, %u, NULL, NULL, %llu) = %s\n",
	       (unsigned) bogus_nfds, (unsigned long long) bogus_sigsetsize,
	       errstr);
# endif

	sys_ppoll((unsigned long) efault, 42, (unsigned long) efault + 8,
		  (unsigned long) efault + 16, sigset_size);
# if !PATH_TRACING_FD && !TRACING_FDS
	printf("ppoll(%p, %u, %p, %p, %u) = %s\n",
	       efault, 42, efault + 8, efault + 16, sigset_size, errstr);
# endif

	ts->tv_sec = 0xdeadbeefU;
	ts->tv_nsec = 0xfacefeedU;
	sys_ppoll(0, 0, (unsigned long) ts, 0, sigset_size);
# if !PATH_TRACING_FD && !TRACING_FDS
	printf("ppoll(NULL, 0, {tv_sec=%lld, tv_nsec=%llu}, NULL, %u) = %s\n",
	       (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec),
	       sigset_size, errstr);
# endif

	ts->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
	ts->tv_nsec = (long) 0xbadc0dedfacefeedL;
	sys_ppoll(0, 0, (unsigned long) ts, 0, sigset_size);
# if !PATH_TRACING_FD && !TRACING_FDS
	printf("ppoll(NULL, 0, {tv_sec=%lld, tv_nsec=%llu}, NULL, %u) = %s\n",
	       (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec),
	       sigset_size, errstr);
# endif

	if (pipe(pipe_fd) || pipe(pipe_fd + 2))
		perror_msg_and_fail("pipe");

# if TRACING_FD1
	if (dup2(pipe_fd[3], TRACING_FD1) < 0)
		perror_msg_and_fail("dup2 1");
	close(pipe_fd[3]);
	pipe_fd[3] = TRACING_FD1;
# endif

# if TRACING_FD2
	if (dup2(pipe_fd[1], TRACING_FD2) < 0)
		perror_msg_and_fail("dup2 2");
	close(pipe_fd[1]);
	pipe_fd[1] = TRACING_FD2;
# endif

	ts->tv_sec = 42;
	ts->tv_nsec = 999999999;

	const struct pollfd fds1[] = {
		{ .fd = pipe_fd[0], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
		{ .fd = pipe_fd[1], .events = POLLOUT | POLLWRNORM | POLLWRBAND },
		{ .fd = pipe_fd[2], .events = POLLIN | POLLPRI },
		{ .fd = pipe_fd[3], .events = POLLOUT }
	};
	fds = efault - sizeof(fds1);
	memcpy(fds, fds1, sizeof(fds1));

	sigemptyset(&mask);
	sigaddset(&mask, SIGUSR2);
	sigaddset(&mask, SIGCHLD);
	memcpy(sigmask, &mask, sigset_size);

	rc = sys_ppoll((unsigned long) fds,
		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
		       (unsigned long) sigmask, sigset_size);
	if (rc != 2)
		perror_msg_and_fail("ppoll 1");
# if TRACE_FD1 || TRACE_FD2 || TRACE_OTHER_FDS
	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
#  if VERBOSE
	       ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
#  else
	       ", ...]"
#  endif
	       ", %u, {tv_sec=42, tv_nsec=999999999}, [%s], %u) = %ld"
	       " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
	       ", left {tv_sec=%u, tv_nsec=%u})\n",
	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
#  if VERBOSE
	       pipe_fd[2], pipe_fd[3],
#  endif
	       (unsigned int) ARRAY_SIZE(fds1), USR2_CHLD_str,
	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
	       pipe_fd[3], (unsigned int) ts->tv_sec,
	       (unsigned int) ts->tv_nsec);
# endif /* !PATH_TRACING_FDS */

	ts->tv_sec = 23;
	ts->tv_nsec = 123456789;

	rc = sys_ppoll((unsigned long) fds,
		       F8ILL_KULONG_MASK | (ARRAY_SIZE(fds1) - 2),
		       (unsigned long) ts,
		       (unsigned long) sigmask, sigset_size);
	if (rc != 1)
		perror_msg_and_fail("ppoll 1.5");
# if TRACE_FD2 || TRACE_OTHER_FDS
	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}]"
	       ", %u, {tv_sec=23, tv_nsec=123456789}, [%s], %u) = %ld"
	       " ([{fd=%d, revents=POLLOUT%s}]"
	       ", left {tv_sec=%u, tv_nsec=%u})\n",
	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
	       (unsigned int) ARRAY_SIZE(fds1) - 2, USR2_CHLD_str,
	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
	       (unsigned int) ts->tv_sec, (unsigned int) ts->tv_nsec);
# endif /* !PATH_TRACING_FDS */

# if PATH_TRACING_FD
	ts->tv_sec = 123;
	ts->tv_nsec = 987654321;
	fds[3].fd = PATH_TRACING_FD;

	rc = sys_ppoll((unsigned long) fds,
		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
		       (unsigned long) sigmask, sigset_size);
	if (rc != 2)
		perror_msg_and_fail("ppoll -P");
	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
#  if VERBOSE
	       ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
#  else
	       ", ...]"
#  endif
	       ", %u, {tv_sec=123, tv_nsec=987654321}, [%s], %u) = %ld"
	       " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
	       ", left {tv_sec=%u, tv_nsec=%u})\n",
	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
#  if VERBOSE
	       pipe_fd[2], PATH_TRACING_FD,
#  endif
	       (unsigned int) ARRAY_SIZE(fds1), USR2_CHLD_str,
	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
	       PATH_TRACING_FD, (unsigned int) ts->tv_sec,
	       (unsigned int) ts->tv_nsec);
# endif /* PATH_TRACING_FD */

	ts->tv_sec = 0;
	ts->tv_nsec = 999;
	const struct pollfd fds2[] = {
		{ .fd = pipe_fd[1], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
		{ .fd = pipe_fd[0], .events = POLLOUT | POLLWRNORM | POLLWRBAND }
	};
	fds = efault - sizeof(fds2);
	memcpy(fds, fds2, sizeof(fds2));

	memset(&mask, -1, sizeof(mask));
	sigdelset(&mask, SIGHUP);
	sigdelset(&mask, SIGKILL);
	sigdelset(&mask, SIGSTOP);
	memcpy(sigmask, &mask, sigset_size);

	rc = sys_ppoll((unsigned long) fds,
		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds2), (unsigned long) ts,
		       (unsigned long) sigmask, sigset_size);
	if (rc != 0)
		perror_msg_and_fail("ppoll 2");
# if TRACE_FD2 || TRACE_OTHER_FDS
	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}], %u"
	       ", {tv_sec=0, tv_nsec=999}, ~[HUP KILL STOP], %u)"
	       " = %ld (Timeout)\n",
	       pipe_fd[1], pipe_fd[0], POLLWRNORM_str,
	       (unsigned) ARRAY_SIZE(fds2), sigset_size, rc);
# endif /* !PATH_TRACING_FD */

	ts->tv_sec = 0;
	ts->tv_nsec = 1;

	rc = sys_ppoll((unsigned long) fds,
		       F8ILL_KULONG_MASK | 1, (unsigned long) ts,
		       (unsigned long) sigmask, sigset_size);
	if (rc != 0)
		perror_msg_and_fail("ppoll 2");
# if !PATH_TRACING_FD && TRACE_FD2
	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1"
	       ", {tv_sec=0, tv_nsec=1}, ~[HUP KILL STOP], %u)"
	       " = %ld (Timeout)\n",
	       pipe_fd[1], sigset_size, rc);
# endif /* !PATH_TRACING_FD */

	if (F8ILL_KULONG_SUPPORTED) {
		sys_ppoll(f8ill_ptr_to_kulong(fds), ARRAY_SIZE(fds2),
			  f8ill_ptr_to_kulong(ts), f8ill_ptr_to_kulong(sigmask),
			  sigset_size);
# if !PATH_TRACING_FD && !TRACING_FDS
		printf("ppoll(%#llx, %u, %#llx, %#llx, %u) = %s\n",
		       (unsigned long long) f8ill_ptr_to_kulong(fds),
		       (unsigned) ARRAY_SIZE(fds2),
		       (unsigned long long) f8ill_ptr_to_kulong(ts),
		       (unsigned long long) f8ill_ptr_to_kulong(sigmask),
		       (unsigned) sigset_size, errstr);
# endif /* !PATH_TRACING_FD */
	}

	puts("+++ exited with 0 +++");
	return 0;
}

#else

SKIP_MAIN_UNDEFINED("__NR_ppoll")

#endif