xref: /minix3/tests/lib/libpthread/t_sigmask.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /* $NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc  *
1611be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc  */
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include <sys/cdefs.h>
3011be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
3111be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
32*84d9c625SLionel Sambuc __RCSID("$NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc /*
3511be35a1SLionel Sambuc  * Regression test for pthread_sigmask when SA upcalls aren't started yet.
3611be35a1SLionel Sambuc  *
3711be35a1SLionel Sambuc  * Written by Christian Limpach <cl@NetBSD.org>, December 2003.
3811be35a1SLionel Sambuc  * Public domain.
3911be35a1SLionel Sambuc  */
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc #include <errno.h>
4211be35a1SLionel Sambuc #include <pthread.h>
4311be35a1SLionel Sambuc #include <signal.h>
4411be35a1SLionel Sambuc #include <stdio.h>
4511be35a1SLionel Sambuc #include <unistd.h>
4611be35a1SLionel Sambuc 
4711be35a1SLionel Sambuc #include <sys/resource.h>
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc #include <atf-c.h>
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc #include "h_common.h"
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc static volatile sig_atomic_t flag;
5411be35a1SLionel Sambuc static volatile sig_atomic_t flag2;
5511be35a1SLionel Sambuc 
5611be35a1SLionel Sambuc static volatile pthread_t thr_usr1;
5711be35a1SLionel Sambuc static volatile pthread_t thr_usr2;
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc static sig_atomic_t count = 0;
6011be35a1SLionel Sambuc 
6111be35a1SLionel Sambuc ATF_TC(upcalls_not_started);
ATF_TC_HEAD(upcalls_not_started,tc)6211be35a1SLionel Sambuc ATF_TC_HEAD(upcalls_not_started, tc)
6311be35a1SLionel Sambuc {
6411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_sigmask when SA upcalls "
6511be35a1SLionel Sambuc 	    "aren't started yet");
6611be35a1SLionel Sambuc }
ATF_TC_BODY(upcalls_not_started,tc)6711be35a1SLionel Sambuc ATF_TC_BODY(upcalls_not_started, tc)
6811be35a1SLionel Sambuc {
6911be35a1SLionel Sambuc 	sigset_t nset;
7011be35a1SLionel Sambuc 	struct rlimit rlim;
7111be35a1SLionel Sambuc 
7211be35a1SLionel Sambuc 	rlim.rlim_cur = rlim.rlim_max = 0;
7311be35a1SLionel Sambuc 	(void) setrlimit(RLIMIT_CORE, &rlim);
7411be35a1SLionel Sambuc 
7511be35a1SLionel Sambuc 	sigemptyset(&nset);
7611be35a1SLionel Sambuc 	sigaddset(&nset, SIGFPE);
7711be35a1SLionel Sambuc 	pthread_sigmask(SIG_BLOCK, &nset, NULL);
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc 	kill(getpid(), SIGFPE);
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc 
8211be35a1SLionel Sambuc static void
upcalls_not_started_handler1(int sig,siginfo_t * info,void * ctx)8311be35a1SLionel Sambuc upcalls_not_started_handler1(int sig, siginfo_t *info, void *ctx)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc 
8611be35a1SLionel Sambuc 	kill(getpid(), SIGUSR2);
8711be35a1SLionel Sambuc 	/*
8811be35a1SLionel Sambuc 	 * If the mask is properly set, SIGUSR2 will not be handled
8911be35a1SLionel Sambuc 	 * until this handler returns.
9011be35a1SLionel Sambuc 	 */
9111be35a1SLionel Sambuc 	flag = 1;
9211be35a1SLionel Sambuc }
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc static void
upcalls_not_started_handler2(int sig,siginfo_t * info,void * ctx)9511be35a1SLionel Sambuc upcalls_not_started_handler2(int sig, siginfo_t *info, void *ctx)
9611be35a1SLionel Sambuc {
9711be35a1SLionel Sambuc 	if (flag == 1)
9811be35a1SLionel Sambuc 		flag = 2;
9911be35a1SLionel Sambuc }
10011be35a1SLionel Sambuc 
10111be35a1SLionel Sambuc ATF_TC(before_threads);
ATF_TC_HEAD(before_threads,tc)10211be35a1SLionel Sambuc ATF_TC_HEAD(before_threads, tc)
10311be35a1SLionel Sambuc {
10411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
10511be35a1SLionel Sambuc 	    "before threads are started");
10611be35a1SLionel Sambuc }
ATF_TC_BODY(before_threads,tc)10711be35a1SLionel Sambuc ATF_TC_BODY(before_threads, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc 	struct sigaction act;
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc 	act.sa_sigaction = upcalls_not_started_handler1;
11211be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
11311be35a1SLionel Sambuc 	sigaddset(&act.sa_mask, SIGUSR2);
11411be35a1SLionel Sambuc 	act.sa_flags = SA_SIGINFO;
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	act.sa_sigaction = upcalls_not_started_handler2;
11911be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
12011be35a1SLionel Sambuc 	act.sa_flags = SA_SIGINFO;
121*84d9c625SLionel Sambuc 	(void)sigaction(SIGUSR2, &act, NULL);
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc 	kill(getpid(), SIGUSR1);
12411be35a1SLionel Sambuc 
12511be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(flag, 2);
12611be35a1SLionel Sambuc 	printf("Success: Both handlers ran in order\n");
12711be35a1SLionel Sambuc }
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc static void
respected_while_running_handler1(int sig,siginfo_t * info,void * ctx)13011be35a1SLionel Sambuc respected_while_running_handler1(int sig, siginfo_t *info, void *ctx)
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc 	kill(getpid(), SIGUSR2);
13411be35a1SLionel Sambuc 	/*
13511be35a1SLionel Sambuc 	 * If the mask is properly set, SIGUSR2 will not be handled
13611be35a1SLionel Sambuc 	 * by the current thread until this handler returns.
13711be35a1SLionel Sambuc 	 */
13811be35a1SLionel Sambuc 	flag = 1;
13911be35a1SLionel Sambuc 	thr_usr1 = pthread_self();
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc static void
respected_while_running_handler2(int sig,siginfo_t * info,void * ctx)14311be35a1SLionel Sambuc respected_while_running_handler2(int sig, siginfo_t *info, void *ctx)
14411be35a1SLionel Sambuc {
14511be35a1SLionel Sambuc 	if (flag == 1)
14611be35a1SLionel Sambuc 		flag = 2;
14711be35a1SLionel Sambuc 	flag2 = 1;
14811be35a1SLionel Sambuc 	thr_usr2 = pthread_self();
14911be35a1SLionel Sambuc }
15011be35a1SLionel Sambuc 
15111be35a1SLionel Sambuc static void *
respected_while_running_threadroutine(void * arg)15211be35a1SLionel Sambuc respected_while_running_threadroutine(void *arg)
15311be35a1SLionel Sambuc {
15411be35a1SLionel Sambuc 
15511be35a1SLionel Sambuc 	kill(getpid(), SIGUSR1);
15611be35a1SLionel Sambuc 	sleep(1);
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc 	if (flag == 2)
15911be35a1SLionel Sambuc 		printf("Success: Both handlers ran in order\n");
16011be35a1SLionel Sambuc 	else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2)
16111be35a1SLionel Sambuc 		printf("Success: Handlers were invoked by different threads\n");
16211be35a1SLionel Sambuc 	else {
16311be35a1SLionel Sambuc 		printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
16411be35a1SLionel Sambuc 			(int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2);
16511be35a1SLionel Sambuc 		atf_tc_fail("failure");
16611be35a1SLionel Sambuc 	}
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc 	return NULL;
16911be35a1SLionel Sambuc }
17011be35a1SLionel Sambuc 
17111be35a1SLionel Sambuc ATF_TC(respected_while_running);
ATF_TC_HEAD(respected_while_running,tc)17211be35a1SLionel Sambuc ATF_TC_HEAD(respected_while_running, tc)
17311be35a1SLionel Sambuc {
17411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
17511be35a1SLionel Sambuc 	    "while threads are running");
17611be35a1SLionel Sambuc }
ATF_TC_BODY(respected_while_running,tc)17711be35a1SLionel Sambuc ATF_TC_BODY(respected_while_running, tc)
17811be35a1SLionel Sambuc {
17911be35a1SLionel Sambuc 	struct sigaction act;
18011be35a1SLionel Sambuc 	pthread_t thread;
18111be35a1SLionel Sambuc 
18211be35a1SLionel Sambuc 	act.sa_sigaction = respected_while_running_handler1;
18311be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
18411be35a1SLionel Sambuc 	sigaddset(&act.sa_mask, SIGUSR2);
18511be35a1SLionel Sambuc 	act.sa_flags = SA_SIGINFO;
18611be35a1SLionel Sambuc 
18711be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
18811be35a1SLionel Sambuc 
18911be35a1SLionel Sambuc 	act.sa_sigaction = respected_while_running_handler2;
19011be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
19111be35a1SLionel Sambuc 	act.sa_flags = SA_SIGINFO;
192*84d9c625SLionel Sambuc 	(void)sigaction(SIGUSR2, &act, NULL);
19311be35a1SLionel Sambuc 
19411be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_create(&thread, NULL,
19511be35a1SLionel Sambuc 	    respected_while_running_threadroutine, NULL));
19611be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
19711be35a1SLionel Sambuc }
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc static void
incorrect_mask_bug_handler(int sig)20011be35a1SLionel Sambuc incorrect_mask_bug_handler(int sig)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc 	count++;
20311be35a1SLionel Sambuc }
20411be35a1SLionel Sambuc 
20511be35a1SLionel Sambuc static void *
incorrect_mask_bug_sleeper(void * arg)20611be35a1SLionel Sambuc incorrect_mask_bug_sleeper(void* arg)
20711be35a1SLionel Sambuc {
20811be35a1SLionel Sambuc 	int i;
20911be35a1SLionel Sambuc 	for (i = 0; i < 10; i++)
21011be35a1SLionel Sambuc 		sleep(1);
21111be35a1SLionel Sambuc 
21211be35a1SLionel Sambuc 	atf_tc_fail("sleeper");
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc 
21511be35a1SLionel Sambuc ATF_TC(incorrect_mask_bug);
ATF_TC_HEAD(incorrect_mask_bug,tc)21611be35a1SLionel Sambuc ATF_TC_HEAD(incorrect_mask_bug, tc)
21711be35a1SLionel Sambuc {
21811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where "
21911be35a1SLionel Sambuc 	    "incorrect signal mask was used");
22011be35a1SLionel Sambuc }
ATF_TC_BODY(incorrect_mask_bug,tc)22111be35a1SLionel Sambuc ATF_TC_BODY(incorrect_mask_bug, tc)
22211be35a1SLionel Sambuc {
22311be35a1SLionel Sambuc 	pthread_t id;
22411be35a1SLionel Sambuc 	struct sigaction act;
22511be35a1SLionel Sambuc 
22611be35a1SLionel Sambuc 	act.sa_sigaction = NULL;
22711be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
22811be35a1SLionel Sambuc 	act.sa_flags = 0;
22911be35a1SLionel Sambuc 	act.sa_handler = incorrect_mask_bug_handler;
23011be35a1SLionel Sambuc 
23111be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s",
23211be35a1SLionel Sambuc 	    strerror(errno));
23311be35a1SLionel Sambuc 
23411be35a1SLionel Sambuc 	sigaddset(&act.sa_mask, SIGALRM);
23511be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
23611be35a1SLionel Sambuc 
23711be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper,
23811be35a1SLionel Sambuc 	    NULL));
23911be35a1SLionel Sambuc 	sleep(1);
24011be35a1SLionel Sambuc 
24111be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
24211be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
24311be35a1SLionel Sambuc 
24411be35a1SLionel Sambuc 	for (;;) {
24511be35a1SLionel Sambuc 		alarm(1);
24611be35a1SLionel Sambuc 		if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR)
24711be35a1SLionel Sambuc 			if (count == 2)
24811be35a1SLionel Sambuc 				return;
24911be35a1SLionel Sambuc 	}
25011be35a1SLionel Sambuc }
25111be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)25211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
25311be35a1SLionel Sambuc {
25411be35a1SLionel Sambuc 
25511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, upcalls_not_started);
25611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, before_threads);
25711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, respected_while_running);
25811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, incorrect_mask_bug);
25911be35a1SLionel Sambuc 
26011be35a1SLionel Sambuc 	return atf_no_error();
26111be35a1SLionel Sambuc }
262