1 /* $NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $ */
4 * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.3 2013/10/19 17:45:01 christos Exp $");
35 * Regression test for pthread_sigmask when SA upcalls aren't started yet.
37 * Written by Christian Limpach <cl@NetBSD.org>, December 2003.
47 #include <sys/resource.h>
53 static volatile sig_atomic_t flag
;
54 static volatile sig_atomic_t flag2
;
56 static volatile pthread_t thr_usr1
;
57 static volatile pthread_t thr_usr2
;
59 static sig_atomic_t count
= 0;
61 ATF_TC(upcalls_not_started
);
62 ATF_TC_HEAD(upcalls_not_started
, tc
)
64 atf_tc_set_md_var(tc
, "descr", "Checks pthread_sigmask when SA upcalls "
65 "aren't started yet");
67 ATF_TC_BODY(upcalls_not_started
, tc
)
72 rlim
.rlim_cur
= rlim
.rlim_max
= 0;
73 (void) setrlimit(RLIMIT_CORE
, &rlim
);
76 sigaddset(&nset
, SIGFPE
);
77 pthread_sigmask(SIG_BLOCK
, &nset
, NULL
);
79 kill(getpid(), SIGFPE
);
83 upcalls_not_started_handler1(int sig
, siginfo_t
*info
, void *ctx
)
86 kill(getpid(), SIGUSR2
);
88 * If the mask is properly set, SIGUSR2 will not be handled
89 * until this handler returns.
95 upcalls_not_started_handler2(int sig
, siginfo_t
*info
, void *ctx
)
101 ATF_TC(before_threads
);
102 ATF_TC_HEAD(before_threads
, tc
)
104 atf_tc_set_md_var(tc
, "descr", "Checks that signal masks are respected "
105 "before threads are started");
107 ATF_TC_BODY(before_threads
, tc
)
109 struct sigaction act
;
111 act
.sa_sigaction
= upcalls_not_started_handler1
;
112 sigemptyset(&act
.sa_mask
);
113 sigaddset(&act
.sa_mask
, SIGUSR2
);
114 act
.sa_flags
= SA_SIGINFO
;
116 ATF_REQUIRE_EQ(sigaction(SIGUSR1
, &act
, NULL
), 0);
118 act
.sa_sigaction
= upcalls_not_started_handler2
;
119 sigemptyset(&act
.sa_mask
);
120 act
.sa_flags
= SA_SIGINFO
;
121 (void)sigaction(SIGUSR2
, &act
, NULL
);
123 kill(getpid(), SIGUSR1
);
125 ATF_REQUIRE_EQ(flag
, 2);
126 printf("Success: Both handlers ran in order\n");
130 respected_while_running_handler1(int sig
, siginfo_t
*info
, void *ctx
)
133 kill(getpid(), SIGUSR2
);
135 * If the mask is properly set, SIGUSR2 will not be handled
136 * by the current thread until this handler returns.
139 thr_usr1
= pthread_self();
143 respected_while_running_handler2(int sig
, siginfo_t
*info
, void *ctx
)
148 thr_usr2
= pthread_self();
152 respected_while_running_threadroutine(void *arg
)
155 kill(getpid(), SIGUSR1
);
159 printf("Success: Both handlers ran in order\n");
160 else if (flag
== 1 && flag2
== 1 && thr_usr1
!= thr_usr2
)
161 printf("Success: Handlers were invoked by different threads\n");
163 printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
164 (int)flag
, (int)flag2
, (void *)thr_usr1
, (void *)thr_usr2
);
165 atf_tc_fail("failure");
171 ATF_TC(respected_while_running
);
172 ATF_TC_HEAD(respected_while_running
, tc
)
174 atf_tc_set_md_var(tc
, "descr", "Checks that signal masks are respected "
175 "while threads are running");
177 ATF_TC_BODY(respected_while_running
, tc
)
179 struct sigaction act
;
182 act
.sa_sigaction
= respected_while_running_handler1
;
183 sigemptyset(&act
.sa_mask
);
184 sigaddset(&act
.sa_mask
, SIGUSR2
);
185 act
.sa_flags
= SA_SIGINFO
;
187 ATF_REQUIRE_EQ(sigaction(SIGUSR1
, &act
, NULL
), 0);
189 act
.sa_sigaction
= respected_while_running_handler2
;
190 sigemptyset(&act
.sa_mask
);
191 act
.sa_flags
= SA_SIGINFO
;
192 (void)sigaction(SIGUSR2
, &act
, NULL
);
194 PTHREAD_REQUIRE(pthread_create(&thread
, NULL
,
195 respected_while_running_threadroutine
, NULL
));
196 PTHREAD_REQUIRE(pthread_join(thread
, NULL
));
200 incorrect_mask_bug_handler(int sig
)
206 incorrect_mask_bug_sleeper(void* arg
)
209 for (i
= 0; i
< 10; i
++)
212 atf_tc_fail("sleeper");
215 ATF_TC(incorrect_mask_bug
);
216 ATF_TC_HEAD(incorrect_mask_bug
, tc
)
218 atf_tc_set_md_var(tc
, "descr", "Checks for bug in libpthread where "
219 "incorrect signal mask was used");
221 ATF_TC_BODY(incorrect_mask_bug
, tc
)
224 struct sigaction act
;
226 act
.sa_sigaction
= NULL
;
227 sigemptyset(&act
.sa_mask
);
229 act
.sa_handler
= incorrect_mask_bug_handler
;
231 ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM
, &act
, NULL
), 0, "%s",
234 sigaddset(&act
.sa_mask
, SIGALRM
);
235 PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK
, &act
.sa_mask
, NULL
));
237 PTHREAD_REQUIRE(pthread_create(&id
, NULL
, incorrect_mask_bug_sleeper
,
241 sigemptyset(&act
.sa_mask
);
242 PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK
, &act
.sa_mask
, NULL
));
246 if (select(1, NULL
, NULL
, NULL
, NULL
) == -1 && errno
== EINTR
)
255 ATF_TP_ADD_TC(tp
, upcalls_not_started
);
256 ATF_TP_ADD_TC(tp
, before_threads
);
257 ATF_TP_ADD_TC(tp
, respected_while_running
);
258 ATF_TP_ADD_TC(tp
, incorrect_mask_bug
);
260 return atf_no_error();