1 /* $NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 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.3 2013/10/19 17:45:01 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);
ATF_TC_HEAD(upcalls_not_started,tc)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 }
ATF_TC_BODY(upcalls_not_started,tc)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
upcalls_not_started_handler1(int sig,siginfo_t * info,void * ctx)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
upcalls_not_started_handler2(int sig,siginfo_t * info,void * ctx)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);
ATF_TC_HEAD(before_threads,tc)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 }
ATF_TC_BODY(before_threads,tc)107 ATF_TC_BODY(before_threads, tc)
108 {
109           struct sigaction act;
110 
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;
115 
116           ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
117 
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);
122 
123           kill(getpid(), SIGUSR1);
124 
125           ATF_REQUIRE_EQ(flag, 2);
126           printf("Success: Both handlers ran in order\n");
127 }
128 
129 static void
respected_while_running_handler1(int sig,siginfo_t * info,void * ctx)130 respected_while_running_handler1(int sig, siginfo_t *info, void *ctx)
131 {
132 
133           kill(getpid(), SIGUSR2);
134           /*
135            * If the mask is properly set, SIGUSR2 will not be handled
136            * by the current thread until this handler returns.
137            */
138           flag = 1;
139           thr_usr1 = pthread_self();
140 }
141 
142 static void
respected_while_running_handler2(int sig,siginfo_t * info,void * ctx)143 respected_while_running_handler2(int sig, siginfo_t *info, void *ctx)
144 {
145           if (flag == 1)
146                     flag = 2;
147           flag2 = 1;
148           thr_usr2 = pthread_self();
149 }
150 
151 static void *
respected_while_running_threadroutine(void * arg)152 respected_while_running_threadroutine(void *arg)
153 {
154 
155           kill(getpid(), SIGUSR1);
156           sleep(1);
157 
158           if (flag == 2)
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");
162           else {
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");
166           }
167 
168           return NULL;
169 }
170 
171 ATF_TC(respected_while_running);
ATF_TC_HEAD(respected_while_running,tc)172 ATF_TC_HEAD(respected_while_running, tc)
173 {
174           atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
175               "while threads are running");
176 }
ATF_TC_BODY(respected_while_running,tc)177 ATF_TC_BODY(respected_while_running, tc)
178 {
179           struct sigaction act;
180           pthread_t thread;
181 
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;
186 
187           ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
188 
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);
193 
194           PTHREAD_REQUIRE(pthread_create(&thread, NULL,
195               respected_while_running_threadroutine, NULL));
196           PTHREAD_REQUIRE(pthread_join(thread, NULL));
197 }
198 
199 static void
incorrect_mask_bug_handler(int sig)200 incorrect_mask_bug_handler(int sig)
201 {
202           count++;
203 }
204 
205 static void *
incorrect_mask_bug_sleeper(void * arg)206 incorrect_mask_bug_sleeper(void* arg)
207 {
208           int i;
209           for (i = 0; i < 10; i++)
210                     sleep(1);
211 
212           atf_tc_fail("sleeper");
213 }
214 
215 ATF_TC(incorrect_mask_bug);
ATF_TC_HEAD(incorrect_mask_bug,tc)216 ATF_TC_HEAD(incorrect_mask_bug, tc)
217 {
218           atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where "
219               "incorrect signal mask was used");
220 }
ATF_TC_BODY(incorrect_mask_bug,tc)221 ATF_TC_BODY(incorrect_mask_bug, tc)
222 {
223           pthread_t id;
224           struct sigaction act;
225 
226           act.sa_sigaction = NULL;
227           sigemptyset(&act.sa_mask);
228           act.sa_flags = 0;
229           act.sa_handler = incorrect_mask_bug_handler;
230 
231           ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s",
232               strerror(errno));
233 
234           sigaddset(&act.sa_mask, SIGALRM);
235           PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
236 
237           PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper,
238               NULL));
239           sleep(1);
240 
241           sigemptyset(&act.sa_mask);
242           PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
243 
244           for (;;) {
245                     alarm(1);
246                     if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR)
247                               if (count == 2)
248                                         return;
249           }
250 }
251 
ATF_TP_ADD_TCS(tp)252 ATF_TP_ADD_TCS(tp)
253 {
254 
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);
259 
260           return atf_no_error();
261 }
262