1272343Sngie/* $NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008, 2010\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $");
33272343Sngie
34272343Sngie/*
35272343Sngie * Regression test for pthread_sigmask when SA upcalls aren't started yet.
36272343Sngie *
37272343Sngie * Written by Christian Limpach <cl@NetBSD.org>, December 2003.
38272343Sngie * Public domain.
39272343Sngie */
40272343Sngie
41272343Sngie#include <errno.h>
42272343Sngie#include <pthread.h>
43272343Sngie#include <signal.h>
44272343Sngie#include <stdio.h>
45272343Sngie#include <unistd.h>
46272343Sngie
47272343Sngie#include <sys/resource.h>
48272343Sngie
49272343Sngie#include <atf-c.h>
50272343Sngie
51272343Sngie#include "h_common.h"
52272343Sngie
53272343Sngiestatic volatile sig_atomic_t flag;
54272343Sngiestatic volatile sig_atomic_t flag2;
55272343Sngie
56272343Sngiestatic volatile pthread_t thr_usr1;
57272343Sngiestatic volatile pthread_t thr_usr2;
58272343Sngie
59272343Sngiestatic sig_atomic_t count = 0;
60272343Sngie
61272343SngieATF_TC(upcalls_not_started);
62272343SngieATF_TC_HEAD(upcalls_not_started, tc)
63272343Sngie{
64272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_sigmask when SA upcalls "
65272343Sngie	    "aren't started yet");
66272343Sngie}
67272343SngieATF_TC_BODY(upcalls_not_started, tc)
68272343Sngie{
69272343Sngie	sigset_t nset;
70272343Sngie	struct rlimit rlim;
71272343Sngie
72272343Sngie	rlim.rlim_cur = rlim.rlim_max = 0;
73272343Sngie	(void) setrlimit(RLIMIT_CORE, &rlim);
74272343Sngie
75272343Sngie	sigemptyset(&nset);
76272343Sngie	sigaddset(&nset, SIGFPE);
77272343Sngie	pthread_sigmask(SIG_BLOCK, &nset, NULL);
78272343Sngie
79272343Sngie	kill(getpid(), SIGFPE);
80272343Sngie}
81272343Sngie
82272343Sngiestatic void
83272343Sngieupcalls_not_started_handler1(int sig, siginfo_t *info, void *ctx)
84272343Sngie{
85272343Sngie
86272343Sngie	kill(getpid(), SIGUSR2);
87272343Sngie	/*
88272343Sngie	 * If the mask is properly set, SIGUSR2 will not be handled
89272343Sngie	 * until this handler returns.
90272343Sngie	 */
91272343Sngie	flag = 1;
92272343Sngie}
93272343Sngie
94272343Sngiestatic void
95272343Sngieupcalls_not_started_handler2(int sig, siginfo_t *info, void *ctx)
96272343Sngie{
97272343Sngie	if (flag == 1)
98272343Sngie		flag = 2;
99272343Sngie}
100272343Sngie
101272343SngieATF_TC(before_threads);
102272343SngieATF_TC_HEAD(before_threads, tc)
103272343Sngie{
104272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
105272343Sngie	    "before threads are started");
106272343Sngie}
107272343SngieATF_TC_BODY(before_threads, tc)
108272343Sngie{
109272343Sngie	struct sigaction act;
110272343Sngie
111272343Sngie	act.sa_sigaction = upcalls_not_started_handler1;
112272343Sngie	sigemptyset(&act.sa_mask);
113272343Sngie	sigaddset(&act.sa_mask, SIGUSR2);
114272343Sngie	act.sa_flags = SA_SIGINFO;
115272343Sngie
116272343Sngie	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
117272343Sngie
118272343Sngie	act.sa_sigaction = upcalls_not_started_handler2;
119272343Sngie	sigemptyset(&act.sa_mask);
120272343Sngie	act.sa_flags = SA_SIGINFO;
121272343Sngie	(void)sigaction(SIGUSR2, &act, NULL);
122272343Sngie
123272343Sngie	kill(getpid(), SIGUSR1);
124272343Sngie
125272343Sngie	ATF_REQUIRE_EQ(flag, 2);
126272343Sngie	printf("Success: Both handlers ran in order\n");
127272343Sngie}
128272343Sngie
129272343Sngiestatic void
130272343Sngierespected_while_running_handler1(int sig, siginfo_t *info, void *ctx)
131272343Sngie{
132272343Sngie
133272343Sngie	kill(getpid(), SIGUSR2);
134272343Sngie	/*
135272343Sngie	 * If the mask is properly set, SIGUSR2 will not be handled
136272343Sngie	 * by the current thread until this handler returns.
137272343Sngie	 */
138272343Sngie	flag = 1;
139272343Sngie	thr_usr1 = pthread_self();
140272343Sngie}
141272343Sngie
142272343Sngiestatic void
143272343Sngierespected_while_running_handler2(int sig, siginfo_t *info, void *ctx)
144272343Sngie{
145272343Sngie	if (flag == 1)
146272343Sngie		flag = 2;
147272343Sngie	flag2 = 1;
148272343Sngie	thr_usr2 = pthread_self();
149272343Sngie}
150272343Sngie
151272343Sngiestatic void *
152272343Sngierespected_while_running_threadroutine(void *arg)
153272343Sngie{
154272343Sngie
155272343Sngie	kill(getpid(), SIGUSR1);
156272343Sngie	sleep(1);
157272343Sngie
158272343Sngie	if (flag == 2)
159272343Sngie		printf("Success: Both handlers ran in order\n");
160272343Sngie	else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2)
161272343Sngie		printf("Success: Handlers were invoked by different threads\n");
162272343Sngie	else {
163272343Sngie		printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
164272343Sngie			(int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2);
165272343Sngie		atf_tc_fail("failure");
166272343Sngie	}
167272343Sngie
168272343Sngie	return NULL;
169272343Sngie}
170272343Sngie
171272343SngieATF_TC(respected_while_running);
172272343SngieATF_TC_HEAD(respected_while_running, tc)
173272343Sngie{
174272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
175272343Sngie	    "while threads are running");
176272343Sngie}
177272343SngieATF_TC_BODY(respected_while_running, tc)
178272343Sngie{
179272343Sngie	struct sigaction act;
180272343Sngie	pthread_t thread;
181272343Sngie
182272343Sngie	act.sa_sigaction = respected_while_running_handler1;
183272343Sngie	sigemptyset(&act.sa_mask);
184272343Sngie	sigaddset(&act.sa_mask, SIGUSR2);
185272343Sngie	act.sa_flags = SA_SIGINFO;
186272343Sngie
187272343Sngie	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
188272343Sngie
189272343Sngie	act.sa_sigaction = respected_while_running_handler2;
190272343Sngie	sigemptyset(&act.sa_mask);
191272343Sngie	act.sa_flags = SA_SIGINFO;
192272343Sngie	(void)sigaction(SIGUSR2, &act, NULL);
193272343Sngie
194272343Sngie	PTHREAD_REQUIRE(pthread_create(&thread, NULL,
195272343Sngie	    respected_while_running_threadroutine, NULL));
196272343Sngie	PTHREAD_REQUIRE(pthread_join(thread, NULL));
197272343Sngie}
198272343Sngie
199272343Sngiestatic void
200272343Sngieincorrect_mask_bug_handler(int sig)
201272343Sngie{
202272343Sngie	count++;
203272343Sngie}
204272343Sngie
205272343Sngiestatic void *
206272343Sngieincorrect_mask_bug_sleeper(void* arg)
207272343Sngie{
208272343Sngie	int i;
209272343Sngie	for (i = 0; i < 10; i++)
210272343Sngie		sleep(1);
211272343Sngie
212272343Sngie	atf_tc_fail("sleeper");
213272343Sngie}
214272343Sngie
215272343SngieATF_TC(incorrect_mask_bug);
216272343SngieATF_TC_HEAD(incorrect_mask_bug, tc)
217272343Sngie{
218272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where "
219272343Sngie	    "incorrect signal mask was used");
220272343Sngie}
221272343SngieATF_TC_BODY(incorrect_mask_bug, tc)
222272343Sngie{
223272343Sngie	pthread_t id;
224272343Sngie	struct sigaction act;
225272343Sngie
226272343Sngie	act.sa_sigaction = NULL;
227272343Sngie	sigemptyset(&act.sa_mask);
228272343Sngie	act.sa_flags = 0;
229272343Sngie	act.sa_handler = incorrect_mask_bug_handler;
230272343Sngie
231272343Sngie	ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s",
232272343Sngie	    strerror(errno));
233272343Sngie
234272343Sngie	sigaddset(&act.sa_mask, SIGALRM);
235272343Sngie	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
236272343Sngie
237272343Sngie	PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper,
238272343Sngie	    NULL));
239272343Sngie	sleep(1);
240272343Sngie
241272343Sngie	sigemptyset(&act.sa_mask);
242272343Sngie	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
243272343Sngie
244272343Sngie	for (;;) {
245272343Sngie		alarm(1);
246272343Sngie		if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR)
247272343Sngie			if (count == 2)
248272343Sngie				return;
249272343Sngie	}
250272343Sngie}
251272343Sngie
252272343SngieATF_TP_ADD_TCS(tp)
253272343Sngie{
254272343Sngie
255272343Sngie	ATF_TP_ADD_TC(tp, upcalls_not_started);
256272343Sngie	ATF_TP_ADD_TC(tp, before_threads);
257272343Sngie	ATF_TP_ADD_TC(tp, respected_while_running);
258272343Sngie	ATF_TP_ADD_TC(tp, incorrect_mask_bug);
259272343Sngie
260272343Sngie	return atf_no_error();
261272343Sngie}
262