<sys/poll.h>
[minix3.git] / tests / lib / libpthread / t_sigmask.c
blobe29152ccaf92a327ac327f765ba445a96379cd39
1 /* $NetBSD: t_sigmask.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */
3 /*
4 * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.2 2010/11/03 16:10:22 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.
38 * Public domain.
41 #include <errno.h>
42 #include <pthread.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <unistd.h>
47 #include <sys/resource.h>
49 #include <atf-c.h>
51 #include "h_common.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)
69 sigset_t nset;
70 struct rlimit rlim;
72 rlim.rlim_cur = rlim.rlim_max = 0;
73 (void) setrlimit(RLIMIT_CORE, &rlim);
75 sigemptyset(&nset);
76 sigaddset(&nset, SIGFPE);
77 pthread_sigmask(SIG_BLOCK, &nset, NULL);
79 kill(getpid(), SIGFPE);
82 static void
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.
91 flag = 1;
94 static void
95 upcalls_not_started_handler2(int sig, siginfo_t *info, void *ctx)
97 if (flag == 1)
98 flag = 2;
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;
110 int ret;
112 act.sa_sigaction = upcalls_not_started_handler1;
113 sigemptyset(&act.sa_mask);
114 sigaddset(&act.sa_mask, SIGUSR2);
115 act.sa_flags = SA_SIGINFO;
117 ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
119 act.sa_sigaction = upcalls_not_started_handler2;
120 sigemptyset(&act.sa_mask);
121 act.sa_flags = SA_SIGINFO;
122 ret = sigaction(SIGUSR2, &act, NULL);
124 kill(getpid(), SIGUSR1);
126 ATF_REQUIRE_EQ(flag, 2);
127 printf("Success: Both handlers ran in order\n");
130 static void
131 respected_while_running_handler1(int sig, siginfo_t *info, void *ctx)
134 kill(getpid(), SIGUSR2);
136 * If the mask is properly set, SIGUSR2 will not be handled
137 * by the current thread until this handler returns.
139 flag = 1;
140 thr_usr1 = pthread_self();
143 static void
144 respected_while_running_handler2(int sig, siginfo_t *info, void *ctx)
146 if (flag == 1)
147 flag = 2;
148 flag2 = 1;
149 thr_usr2 = pthread_self();
152 static void *
153 respected_while_running_threadroutine(void *arg)
156 kill(getpid(), SIGUSR1);
157 sleep(1);
159 if (flag == 2)
160 printf("Success: Both handlers ran in order\n");
161 else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2)
162 printf("Success: Handlers were invoked by different threads\n");
163 else {
164 printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n",
165 (int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2);
166 atf_tc_fail("failure");
169 return NULL;
172 ATF_TC(respected_while_running);
173 ATF_TC_HEAD(respected_while_running, tc)
175 atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected "
176 "while threads are running");
178 ATF_TC_BODY(respected_while_running, tc)
180 struct sigaction act;
181 pthread_t thread;
182 int rv;
184 act.sa_sigaction = respected_while_running_handler1;
185 sigemptyset(&act.sa_mask);
186 sigaddset(&act.sa_mask, SIGUSR2);
187 act.sa_flags = SA_SIGINFO;
189 ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
191 act.sa_sigaction = respected_while_running_handler2;
192 sigemptyset(&act.sa_mask);
193 act.sa_flags = SA_SIGINFO;
194 rv = sigaction(SIGUSR2, &act, NULL);
196 PTHREAD_REQUIRE(pthread_create(&thread, NULL,
197 respected_while_running_threadroutine, NULL));
198 PTHREAD_REQUIRE(pthread_join(thread, NULL));
201 static void
202 incorrect_mask_bug_handler(int sig)
204 count++;
207 static void *
208 incorrect_mask_bug_sleeper(void* arg)
210 int i;
211 for (i = 0; i < 10; i++)
212 sleep(1);
214 atf_tc_fail("sleeper");
217 ATF_TC(incorrect_mask_bug);
218 ATF_TC_HEAD(incorrect_mask_bug, tc)
220 atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where "
221 "incorrect signal mask was used");
223 ATF_TC_BODY(incorrect_mask_bug, tc)
225 pthread_t id;
226 struct sigaction act;
228 act.sa_sigaction = NULL;
229 sigemptyset(&act.sa_mask);
230 act.sa_flags = 0;
231 act.sa_handler = incorrect_mask_bug_handler;
233 ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s",
234 strerror(errno));
236 sigaddset(&act.sa_mask, SIGALRM);
237 PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
239 PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper,
240 NULL));
241 sleep(1);
243 sigemptyset(&act.sa_mask);
244 PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL));
246 for (;;) {
247 alarm(1);
248 if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR)
249 if (count == 2)
250 return;
254 ATF_TP_ADD_TCS(tp)
257 ATF_TP_ADD_TC(tp, upcalls_not_started);
258 ATF_TP_ADD_TC(tp, before_threads);
259 ATF_TP_ADD_TC(tp, respected_while_running);
260 ATF_TP_ADD_TC(tp, incorrect_mask_bug);
262 return atf_no_error();