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