1/*
2 * This file is part of rt_sigsuspend 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_sigsuspend
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_sigsuspend(const sigset_t *const set, const unsigned long size)
46{
47	return syscall(__NR_rt_sigsuspend, set, size);
48}
49
50static void
51iterate(const char *const text, const int sig,
52	const void *const set, unsigned int size)
53{
54	const void *mask;
55
56	for (mask = set;; size >>= 1, mask += size) {
57		raise(sig);
58		assert(k_sigsuspend(mask, size) == -1);
59		if (EINTR == errno) {
60			tprintf("rt_sigsuspend(%s, %u) = ? ERESTARTNOHAND"
61				" (To be restarted if no handler)\n",
62				text, size);
63		} else {
64			if (size < sizeof(long))
65				tprintf("rt_sigsuspend(%p, %u)"
66					" = -1 EINVAL (%m)\n",
67					mask, size);
68			else
69				tprintf("rt_sigsuspend(%s, %u)"
70					" = -1 EINVAL (%m)\n",
71					set == mask ? text : "~[]", size);
72		}
73		if (!size)
74			break;
75	}
76}
77
78static void
79handler(int signo)
80{
81}
82
83int
84main(void)
85{
86	tprintf("%s", "");
87
88	const unsigned int big_size = 1024 / 8;
89	void *k_set = tail_alloc(big_size);
90	memset(k_set, 0, big_size);
91
92	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);
93	sigemptyset(libc_set);
94	sigaddset(libc_set, SIGUSR1);
95	if (sigprocmask(SIG_SETMASK, libc_set, NULL))
96		perror_msg_and_fail("sigprocmask");
97
98	const struct sigaction sa = {
99		.sa_handler = handler
100	};
101	if (sigaction(SIGUSR1, &sa, NULL))
102		perror_msg_and_fail("sigaction");
103
104	raise(SIGUSR1);
105	unsigned int set_size = big_size;
106	for (; set_size; set_size >>= 1, k_set += set_size) {
107		assert(k_sigsuspend(k_set, set_size) == -1);
108		if (EINTR == errno)
109			break;
110		tprintf("rt_sigsuspend(%p, %u) = -1 EINVAL (%m)\n",
111			k_set, set_size);
112	}
113	if (!set_size)
114		perror_msg_and_fail("rt_sigsuspend");
115	tprintf("rt_sigsuspend([], %u) = ? ERESTARTNOHAND"
116		" (To be restarted if no handler)\n", set_size);
117
118	sigemptyset(libc_set);
119	sigaddset(libc_set, SIGUSR2);
120	memcpy(k_set, libc_set, set_size);
121	raise(SIGUSR1);
122	assert(k_sigsuspend(k_set, set_size) == -1);
123	assert(EINTR == errno);
124	tprintf("rt_sigsuspend([USR2], %u) = ? ERESTARTNOHAND"
125		" (To be restarted if no handler)\n", set_size);
126
127	sigaddset(libc_set, SIGHUP);
128	memcpy(k_set, libc_set, set_size);
129	raise(SIGUSR1);
130	assert(k_sigsuspend(k_set, set_size) == -1);
131	assert(EINTR == errno);
132	tprintf("rt_sigsuspend([HUP USR2], %u) = ? ERESTARTNOHAND"
133		" (To be restarted if no handler)\n", set_size);
134
135	sigaddset(libc_set, SIGINT);
136	memcpy(k_set, libc_set, set_size);
137	raise(SIGUSR1);
138	assert(k_sigsuspend(k_set, set_size) == -1);
139	assert(EINTR == errno);
140	tprintf("rt_sigsuspend([HUP INT USR2], %u) = ? ERESTARTNOHAND"
141		" (To be restarted if no handler)\n", set_size);
142
143	memset(libc_set, -1, sizeof(*libc_set));
144	sigdelset(libc_set, SIGUSR1);
145	memcpy(k_set, libc_set, set_size);
146	raise(SIGUSR1);
147	assert(k_sigsuspend(k_set, set_size) == -1);
148	assert(EINTR == errno);
149	tprintf("rt_sigsuspend(~[USR1], %u) = ? ERESTARTNOHAND"
150		" (To be restarted if no handler)\n", set_size);
151
152	assert(k_sigsuspend(k_set - set_size, set_size << 1) == -1);
153	tprintf("rt_sigsuspend(%p, %u) = -1 EINVAL (%m)\n",
154		k_set - set_size, set_size << 1);
155
156	iterate("~[USR1]", SIGUSR1, k_set, set_size >> 1);
157
158	tprintf("+++ exited with 0 +++\n");
159	return 0;
160}
161
162#else
163
164SKIP_MAIN_UNDEFINED("__NR_rt_sigsuspend")
165
166#endif
167