1/*
2 * This file is part of rt_sigprocmask 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_sigprocmask
35
36# include <assert.h>
37# include <signal.h>
38# include <stdio.h>
39# include <string.h>
40# include <unistd.h>
41
42static long
43k_sigprocmask(const unsigned long how, void *const new_set,
44	      void *const old_set, const unsigned long size)
45{
46	return syscall(__NR_rt_sigprocmask, how, new_set, old_set, size);
47}
48
49static void
50iterate(const char *const text, void *set, void *old, unsigned int size)
51{
52	for (;;) {
53		if (k_sigprocmask(SIG_UNBLOCK, set, old, size)) {
54			if (size < sizeof(long))
55				tprintf("rt_sigprocmask(SIG_UNBLOCK"
56					", %p, %p, %u) = -1 EINVAL (%m)\n",
57					set, old, size);
58			else
59				tprintf("rt_sigprocmask(SIG_UNBLOCK"
60					", %s, %p, %u) = -1 EINVAL (%m)\n",
61					text, old, size);
62		} else {
63			tprintf("rt_sigprocmask(SIG_UNBLOCK, %s, [], %u)"
64				" = 0\n", text, size);
65		}
66		if (!size)
67			break;
68		size >>= 1;
69		set += size;
70		old += size;
71	}
72}
73
74int
75main(void)
76{
77	tprintf("%s", "");
78
79	const unsigned int big_size = 1024 / 8;
80	unsigned int set_size;
81
82	for (set_size = big_size; set_size; set_size >>= 1) {
83		if (!k_sigprocmask(SIG_SETMASK, NULL, NULL, set_size))
84			break;
85		tprintf("rt_sigprocmask(SIG_SETMASK, NULL, NULL, %u)"
86			" = -1 EINVAL (%m)\n", set_size);
87	}
88	if (!set_size)
89		perror_msg_and_fail("rt_sigprocmask");
90	tprintf("rt_sigprocmask(SIG_SETMASK, NULL, NULL, %u) = 0\n",
91		set_size);
92
93	void *const k_set = tail_alloc(set_size);
94	void *const old_set = tail_alloc(set_size);
95	TAIL_ALLOC_OBJECT_CONST_PTR(sigset_t, libc_set);
96
97	memset(k_set, 0, set_size);
98	if (k_sigprocmask(SIG_SETMASK, k_set, NULL, set_size))
99		perror_msg_and_fail("rt_sigprocmask");
100	tprintf("rt_sigprocmask(SIG_SETMASK, [], NULL, %u) = 0\n", set_size);
101
102	if (k_sigprocmask(SIG_UNBLOCK, k_set - set_size, old_set, set_size))
103		perror_msg_and_fail("rt_sigprocmask");
104	tprintf("rt_sigprocmask(SIG_UNBLOCK, ~[], [], %u) = 0\n", set_size);
105
106	assert(k_sigprocmask(SIG_SETMASK, k_set - set_size,
107			     old_set, set_size << 1) == -1);
108	tprintf("rt_sigprocmask(SIG_SETMASK, %p, %p, %u) = -1 EINVAL (%m)\n",
109		k_set - set_size, old_set, set_size << 1);
110
111	iterate("~[]", k_set - set_size, old_set, set_size >> 1);
112
113	sigemptyset(libc_set);
114	sigaddset(libc_set, SIGHUP);
115	memcpy(k_set, libc_set, set_size);
116
117	if (k_sigprocmask(SIG_BLOCK, k_set, old_set, set_size))
118		perror_msg_and_fail("rt_sigprocmask");
119	tprintf("rt_sigprocmask(SIG_BLOCK, [HUP], [], %u) = 0\n", set_size);
120
121	memset(libc_set, -1, sizeof(sigset_t));
122	sigdelset(libc_set, SIGHUP);
123	memcpy(k_set, libc_set, set_size);
124
125	if (k_sigprocmask(SIG_UNBLOCK, k_set, old_set, set_size))
126		perror_msg_and_fail("rt_sigprocmask");
127	tprintf("rt_sigprocmask(SIG_UNBLOCK, ~[HUP], [HUP], %u) = 0\n",
128		set_size);
129
130	sigdelset(libc_set, SIGKILL);
131	memcpy(k_set, libc_set, set_size);
132
133	if (k_sigprocmask(SIG_UNBLOCK, k_set, old_set, set_size))
134		perror_msg_and_fail("rt_sigprocmask");
135	tprintf("rt_sigprocmask(SIG_UNBLOCK, ~[HUP KILL], [HUP], %u) = 0\n",
136		set_size);
137
138	sigemptyset(libc_set);
139	sigaddset(libc_set, SIGHUP);
140	sigaddset(libc_set, SIGINT);
141	sigaddset(libc_set, SIGQUIT);
142	sigaddset(libc_set, SIGALRM);
143	sigaddset(libc_set, SIGTERM);
144	memcpy(k_set, libc_set, set_size);
145
146	if (k_sigprocmask(SIG_BLOCK, k_set, old_set, set_size))
147		perror_msg_and_fail("rt_sigprocmask");
148	tprintf("rt_sigprocmask(SIG_BLOCK, %s, [HUP], %u) = 0\n",
149		"[HUP INT QUIT ALRM TERM]", set_size);
150
151	if (k_sigprocmask(SIG_SETMASK, NULL, old_set, set_size))
152		perror_msg_and_fail("rt_sigprocmask");
153	tprintf("rt_sigprocmask(SIG_SETMASK, NULL, %s, %u) = 0\n",
154		"[HUP INT QUIT ALRM TERM]", set_size);
155
156	assert(k_sigprocmask(SIG_SETMASK, k_set + (set_size >> 1), NULL,
157			     set_size) == -1);
158	tprintf("rt_sigprocmask(SIG_SETMASK, %p, NULL, %u) = -1 EFAULT (%m)\n",
159		k_set + (set_size >> 1), set_size);
160
161	assert(k_sigprocmask(SIG_SETMASK, k_set, old_set + (set_size >> 1),
162			     set_size) == -1);
163	tprintf("rt_sigprocmask(SIG_SETMASK, %s, %p, %u) = -1 EFAULT (%m)\n",
164		"[HUP INT QUIT ALRM TERM]",
165		old_set + (set_size >> 1), set_size);
166
167	tprintf("+++ exited with 0 +++\n");
168	return 0;
169}
170
171#else
172
173SKIP_MAIN_UNDEFINED("__NR_rt_sigprocmask")
174
175#endif
176