1/*
2 * This file is part of rt_sigtimedwait strace test.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2017 The strace developers.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "tests.h"
32#include <asm/unistd.h>
33
34#ifdef __NR_rt_sigtimedwait
35
36# include <assert.h>
37# include <errno.h>
38# include <signal.h>
39# include <stdio.h>
40# include <stdint.h>
41# include <string.h>
42# include <unistd.h>
43
44static long
45k_sigtimedwait(const sigset_t *const set, siginfo_t *const info,
46	       const struct timespec *const timeout, const unsigned long size)
47{
48	return syscall(__NR_rt_sigtimedwait, set, info, timeout, size);
49}
50
51static void
52iterate(const char *const text, const void *set,
53	const struct timespec *const timeout, unsigned int size)
54{
55	for (;;) {
56		assert(k_sigtimedwait(set, NULL, timeout, size) == -1);
57		if (EINTR == errno) {
58			tprintf("rt_sigtimedwait(%s, NULL"
59				", {tv_sec=%lld, tv_nsec=%llu}, %u)"
60				" = -1 EAGAIN (%m)\n", text,
61				(long long) timeout->tv_sec,
62				zero_extend_signed_to_ull(timeout->tv_nsec),
63				size);
64		} else {
65			if (size < sizeof(long))
66				tprintf("rt_sigtimedwait(%p, NULL"
67					", {tv_sec=%lld, tv_nsec=%llu}"
68					", %u) = -1 EINVAL (%m)\n",
69					set, (long long) timeout->tv_sec,
70					zero_extend_signed_to_ull(timeout->tv_nsec),
71					size);
72			else
73				tprintf("rt_sigtimedwait(%s, NULL"
74					", {tv_sec=%lld, tv_nsec=%llu}"
75					", %u) = -1 EINVAL (%m)\n",
76					text, (long long) timeout->tv_sec,
77					zero_extend_signed_to_ull(timeout->tv_nsec),
78					size);
79		}
80		if (!size)
81			break;
82		size >>= 1;
83		set += size;
84	}
85}
86
87int
88main(void)
89{
90	tprintf("%s", "");
91
92	TAIL_ALLOC_OBJECT_CONST_PTR(siginfo_t, info);
93	TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, timeout);
94	timeout->tv_sec = 0;
95	timeout->tv_nsec = 42;
96
97	const unsigned int big_size = 1024 / 8;
98	void *k_set = tail_alloc(big_size);
99	memset(k_set, 0, big_size);
100
101	unsigned int set_size = big_size;
102	for (; set_size; set_size >>= 1, k_set += set_size) {
103		assert(k_sigtimedwait(k_set, NULL, timeout, set_size) == -1);
104		if (EAGAIN == errno)
105			break;
106		tprintf("rt_sigtimedwait(%p, NULL, {tv_sec=%lld, tv_nsec=%llu}"
107			", %u) = -1 EINVAL (%m)\n",
108			k_set, (long long) timeout->tv_sec,
109			zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
110	}
111	if (!set_size)
112		perror_msg_and_fail("rt_sigtimedwait");
113	tprintf("rt_sigtimedwait([], NULL, {tv_sec=%lld, tv_nsec=%llu}, %u)"
114		" = -1 EAGAIN (%m)\n",
115		(long long) timeout->tv_sec,
116		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
117
118	timeout->tv_sec = 0xdeadbeefU;
119	timeout->tv_nsec = 0xfacefeedU;
120	assert(k_sigtimedwait(k_set, NULL, timeout, set_size) == -1);
121	tprintf("rt_sigtimedwait([], NULL, {tv_sec=%lld, tv_nsec=%llu}"
122		", %u) = -1 EINVAL (%m)\n",
123		(long long) timeout->tv_sec,
124		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
125
126	timeout->tv_sec = (time_t) 0xcafef00ddeadbeefLL;
127	timeout->tv_nsec = (long) 0xbadc0dedfacefeedLL;
128	assert(k_sigtimedwait(k_set, NULL, timeout, set_size) == -1);
129	tprintf("rt_sigtimedwait([], NULL, {tv_sec=%lld, tv_nsec=%llu}"
130		", %u) = -1 EINVAL (%m)\n",
131		(long long) timeout->tv_sec,
132		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
133
134	timeout->tv_sec = 0;
135	timeout->tv_nsec = 42;
136
137	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);
138	sigemptyset(libc_set);
139	sigaddset(libc_set, SIGHUP);
140	memcpy(k_set, libc_set, set_size);
141
142	assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
143	assert(EAGAIN == errno);
144	tprintf("rt_sigtimedwait([HUP], %p, {tv_sec=%lld, tv_nsec=%llu}, %u)"
145		" = -1 EAGAIN (%m)\n",
146		info, (long long) timeout->tv_sec,
147		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
148
149	sigaddset(libc_set, SIGINT);
150	memcpy(k_set, libc_set, set_size);
151
152	assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
153	assert(EAGAIN == errno);
154	tprintf("rt_sigtimedwait([HUP INT], %p, {tv_sec=%lld, tv_nsec=%llu}, %u)"
155		" = -1 EAGAIN (%m)\n",
156		info, (long long) timeout->tv_sec,
157		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
158
159	sigaddset(libc_set, SIGQUIT);
160	sigaddset(libc_set, SIGALRM);
161	sigaddset(libc_set, SIGTERM);
162	memcpy(k_set, libc_set, set_size);
163
164	assert(k_sigtimedwait(k_set, info, timeout, set_size) == -1);
165	assert(EAGAIN == errno);
166	tprintf("rt_sigtimedwait(%s, %p, {tv_sec=%lld, tv_nsec=%llu}, %u)"
167		" = -1 EAGAIN (%m)\n",
168		"[HUP INT QUIT ALRM TERM]",
169		info, (long long) timeout->tv_sec,
170		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
171
172	memset(k_set - set_size, -1, set_size);
173	assert(k_sigtimedwait(k_set - set_size, info, timeout, set_size) == -1);
174	assert(EAGAIN == errno);
175	tprintf("rt_sigtimedwait(~[], %p, {tv_sec=%lld, tv_nsec=%llu}, %u)"
176		" = -1 EAGAIN (%m)\n",
177		info, (long long) timeout->tv_sec,
178		zero_extend_signed_to_ull(timeout->tv_nsec), set_size);
179
180	if (sigprocmask(SIG_SETMASK, libc_set, NULL))
181		perror_msg_and_fail("sigprocmask");
182
183	assert(k_sigtimedwait(k_set - set_size, info, NULL, set_size << 1) == -1);
184	tprintf("rt_sigtimedwait(%p, %p, NULL, %u) = -1 EINVAL (%m)\n",
185		k_set - set_size, info, set_size << 1);
186
187	iterate("~[]", k_set - set_size, timeout, set_size >> 1);
188
189	timeout->tv_sec = 1;
190	raise(SIGALRM);
191	assert(k_sigtimedwait(k_set, info, timeout, set_size) == SIGALRM);
192	tprintf("rt_sigtimedwait(%s, {si_signo=%s, si_code=SI_TKILL"
193		", si_pid=%d, si_uid=%d}, {tv_sec=%lld, tv_nsec=%llu}, %u)"
194		" = %d (%s)\n",
195		"[HUP INT QUIT ALRM TERM]", "SIGALRM", getpid(), getuid(),
196		(long long) timeout->tv_sec,
197		zero_extend_signed_to_ull(timeout->tv_nsec),
198		set_size, SIGALRM, "SIGALRM");
199
200	raise(SIGALRM);
201	assert(k_sigtimedwait(k_set, NULL, NULL, set_size) == SIGALRM);
202	tprintf("rt_sigtimedwait(%s, NULL, NULL, %u) = %d (%s)\n",
203		"[HUP INT QUIT ALRM TERM]", set_size, SIGALRM, "SIGALRM");
204
205	tprintf("+++ exited with 0 +++\n");
206	return 0;
207}
208
209#else
210
211SKIP_MAIN_UNDEFINED("__NR_rt_sigtimedwait")
212
213#endif
214