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