xref: /netbsd-src/tests/lib/libpthread/t_sigmask.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /* $NetBSD: t_sigmask.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_sigmask.c,v 1.2 2010/11/03 16:10:22 christos Exp $");
33 
34 /*
35  * Regression test for pthread_sigmask when SA upcalls aren't started yet.
36  *
37  * Written by Christian Limpach <cl@NetBSD.org>, December 2003.
38  * Public domain.
39  */
40 
41 #include <errno.h>
42 #include <pthread.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 
47 #include <sys/resource.h>
48 
49 #include <atf-c.h>
50 
51 #include "h_common.h"
52 
53 static volatile sig_atomic_t flag;
54 static volatile sig_atomic_t flag2;
55 
56 static volatile pthread_t thr_usr1;
57 static volatile pthread_t thr_usr2;
58 
59 static sig_atomic_t count = 0;
60 
61 ATF_TC(upcalls_not_started);
62 ATF_TC_HEAD(upcalls_not_started, tc)
63 {
64 	atf_tc_set_md_var(tc, "descr", "Checks pthread_sigmask when SA upcalls "
65 	    "aren't started yet");
66 }
67 ATF_TC_BODY(upcalls_not_started, tc)
68 {
69 	sigset_t nset;
70 	struct rlimit rlim;
71 
72 	rlim.rlim_cur = rlim.rlim_max = 0;
73 	(void) setrlimit(RLIMIT_CORE, &rlim);
74 
75 	sigemptyset(&nset);
76 	sigaddset(&nset, SIGFPE);
77 	pthread_sigmask(SIG_BLOCK, &nset, NULL);
78 
79 	kill(getpid(), SIGFPE);
80 }
81 
82 static void
83 upcalls_not_started_handler1(int sig, siginfo_t *info, void *ctx)
84 {
85 
86 	kill(getpid(), SIGUSR2);
87 	/*
88 	 * If the mask is properly set, SIGUSR2 will not be handled
89 	 * until this handler returns.
90 	 */
91 	flag = 1;
92 }
93 
94 static void
95 upcalls_not_started_handler2(int sig, siginfo_t *info, void *ctx)
96 {
97 	if (flag == 1)
98 		flag = 2;
99 }
100 
101 ATF_TC(before_threads);
102 ATF_TC_HEAD(before_threads, tc)
103 {
104 	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
105 	    "before threads are started");
106 }
107 ATF_TC_BODY(before_threads, tc)
108 {
109 	struct sigaction act;
110 	int ret;
111 
112 	act.sa_sigaction = upcalls_not_started_handler1;
113 	sigemptyset(&act.sa_mask);
114 	sigaddset(&act.sa_mask, SIGUSR2);
115 	act.sa_flags = SA_SIGINFO;
116 
117 	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
118 
119 	act.sa_sigaction = upcalls_not_started_handler2;
120 	sigemptyset(&act.sa_mask);
121 	act.sa_flags = SA_SIGINFO;
122 	ret = sigaction(SIGUSR2, &act, NULL);
123 
124 	kill(getpid(), SIGUSR1);
125 
126 	ATF_REQUIRE_EQ(flag, 2);
127 	printf("Success: Both handlers ran in order\n");
128 }
129 
130 static void
131 respected_while_running_handler1(int sig, siginfo_t *info, void *ctx)
132 {
133 
134 	kill(getpid(), SIGUSR2);
135 	/*
136 	 * If the mask is properly set, SIGUSR2 will not be handled
137 	 * by the current thread until this handler returns.
138 	 */
139 	flag = 1;
140 	thr_usr1 = pthread_self();
141 }
142 
143 static void
144 respected_while_running_handler2(int sig, siginfo_t *info, void *ctx)
145 {
146 	if (flag == 1)
147 		flag = 2;
148 	flag2 = 1;
149 	thr_usr2 = pthread_self();
150 }
151 
152 static void *
153 respected_while_running_threadroutine(void *arg)
154 {
155 
156 	kill(getpid(), SIGUSR1);
157 	sleep(1);
158 
159 	if (flag == 2)
160 		printf("Success: Both handlers ran in order\n");
161 	else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2)
162 		printf("Success: Handlers were invoked by different threads\n");
163 	else {
164 		printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
165 			(int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2);
166 		atf_tc_fail("failure");
167 	}
168 
169 	return NULL;
170 }
171 
172 ATF_TC(respected_while_running);
173 ATF_TC_HEAD(respected_while_running, tc)
174 {
175 	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
176 	    "while threads are running");
177 }
178 ATF_TC_BODY(respected_while_running, tc)
179 {
180 	struct sigaction act;
181 	pthread_t thread;
182 	int rv;
183 
184 	act.sa_sigaction = respected_while_running_handler1;
185 	sigemptyset(&act.sa_mask);
186 	sigaddset(&act.sa_mask, SIGUSR2);
187 	act.sa_flags = SA_SIGINFO;
188 
189 	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
190 
191 	act.sa_sigaction = respected_while_running_handler2;
192 	sigemptyset(&act.sa_mask);
193 	act.sa_flags = SA_SIGINFO;
194 	rv = sigaction(SIGUSR2, &act, NULL);
195 
196 	PTHREAD_REQUIRE(pthread_create(&thread, NULL,
197 	    respected_while_running_threadroutine, NULL));
198 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
199 }
200 
201 static void
202 incorrect_mask_bug_handler(int sig)
203 {
204 	count++;
205 }
206 
207 static void *
208 incorrect_mask_bug_sleeper(void* arg)
209 {
210 	int i;
211 	for (i = 0; i < 10; i++)
212 		sleep(1);
213 
214 	atf_tc_fail("sleeper");
215 }
216 
217 ATF_TC(incorrect_mask_bug);
218 ATF_TC_HEAD(incorrect_mask_bug, tc)
219 {
220 	atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where "
221 	    "incorrect signal mask was used");
222 }
223 ATF_TC_BODY(incorrect_mask_bug, tc)
224 {
225 	pthread_t id;
226 	struct sigaction act;
227 
228 	act.sa_sigaction = NULL;
229 	sigemptyset(&act.sa_mask);
230 	act.sa_flags = 0;
231 	act.sa_handler = incorrect_mask_bug_handler;
232 
233 	ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s",
234 	    strerror(errno));
235 
236 	sigaddset(&act.sa_mask, SIGALRM);
237 	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
238 
239 	PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper,
240 	    NULL));
241 	sleep(1);
242 
243 	sigemptyset(&act.sa_mask);
244 	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
245 
246 	for (;;) {
247 		alarm(1);
248 		if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR)
249 			if (count == 2)
250 				return;
251 	}
252 }
253 
254 ATF_TP_ADD_TCS(tp)
255 {
256 
257 	ATF_TP_ADD_TC(tp, upcalls_not_started);
258 	ATF_TP_ADD_TC(tp, before_threads);
259 	ATF_TP_ADD_TC(tp, respected_while_running);
260 	ATF_TP_ADD_TC(tp, incorrect_mask_bug);
261 
262 	return atf_no_error();
263 }
264