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