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