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