1 /* $OpenBSD: sigwait.c,v 1.3 2002/10/12 03:00:11 marc Exp $ */ 2 /* 3 * Copyright (c) 1998 Daniel M. Eischen <eischen@vigrid.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Daniel M. Eischen. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY DANIEL M. EISCHEN AND CONTRIBUTORS ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 #include <stdlib.h> 35 #include <unistd.h> 36 37 #include <errno.h> 38 #include <pthread.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <string.h> 42 43 #include <pthread_np.h> 44 #include "test.h" 45 46 static int sigcounts[NSIG + 1]; 47 static sigset_t wait_mask; 48 static pthread_mutex_t waiter_mutex; 49 50 51 static void * 52 sigwaiter (void *arg) 53 { 54 int signo; 55 56 SET_NAME("sigwaiter"); 57 58 /* Block all of the signals that the function will wait for */ 59 CHECKe(sigprocmask (SIG_BLOCK, &wait_mask, NULL)); 60 61 while (sigcounts[SIGINT] == 0) { 62 printf("Sigwait waiting (thread %p)\n", pthread_self()); 63 CHECKe(sigwait (&wait_mask, &signo)); 64 sigcounts[signo]++; 65 printf ("Sigwait caught signal %d (%s)\n", signo, 66 strsignal(signo)); 67 68 /* Allow the main thread to prevent the sigwait. */ 69 CHECKr(pthread_mutex_lock (&waiter_mutex)); 70 CHECKr(pthread_mutex_unlock (&waiter_mutex)); 71 } 72 73 return (arg); 74 } 75 76 77 static void 78 sighandler (int signo) 79 { 80 int save_errno = errno; 81 char buf[8192]; 82 83 snprintf(buf, sizeof buf, 84 " -> Signal handler caught signal %d (%s) in thread %p\n", 85 signo, strsignal(signo), pthread_self()); 86 write(STDOUT_FILENO, buf, strlen(buf)); 87 88 if ((signo >= 0) && (signo <= NSIG)) 89 sigcounts[signo]++; 90 errno = save_errno; 91 } 92 93 int main (int argc, char *argv[]) 94 { 95 pthread_mutexattr_t mattr; 96 pthread_attr_t pattr; 97 pthread_t tid; 98 struct sigaction act; 99 100 /* Initialize our signal counts. */ 101 memset ((void *) sigcounts, 0, NSIG * sizeof (int)); 102 103 /* Setupt our wait mask. */ 104 sigemptyset (&wait_mask); /* Default action */ 105 sigaddset (&wait_mask, SIGHUP); /* terminate */ 106 sigaddset (&wait_mask, SIGINT); /* terminate */ 107 sigaddset (&wait_mask, SIGQUIT); /* create core image */ 108 sigaddset (&wait_mask, SIGURG); /* ignore */ 109 sigaddset (&wait_mask, SIGIO); /* ignore */ 110 sigaddset (&wait_mask, SIGUSR1); /* terminate */ 111 112 /* Ignore signals SIGHUP and SIGIO. */ 113 sigemptyset (&act.sa_mask); 114 sigaddset (&act.sa_mask, SIGHUP); 115 sigaddset (&act.sa_mask, SIGIO); 116 act.sa_handler = SIG_IGN; 117 act.sa_flags = 0; 118 CHECKe(sigaction (SIGHUP, &act, NULL)); 119 CHECKe(sigaction (SIGIO, &act, NULL)); 120 121 /* Install a signal handler for SIGURG */ 122 sigemptyset (&act.sa_mask); 123 sigaddset (&act.sa_mask, SIGURG); 124 act.sa_handler = sighandler; 125 act.sa_flags = SA_RESTART; 126 CHECKe(sigaction (SIGURG, &act, NULL)); 127 128 /* Install a signal handler for SIGXCPU */ 129 sigemptyset (&act.sa_mask); 130 sigaddset (&act.sa_mask, SIGXCPU); 131 CHECKe(sigaction (SIGXCPU, &act, NULL)); 132 133 /* 134 * Initialize the thread attribute. 135 */ 136 CHECKr(pthread_attr_init (&pattr)); 137 CHECKr(pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE)); 138 139 /* 140 * Initialize and create a mutex. 141 */ 142 CHECKr(pthread_mutexattr_init (&mattr)); 143 CHECKr(pthread_mutex_init (&waiter_mutex, &mattr)); 144 145 /* 146 * Create the sigwaiter thread. 147 */ 148 CHECKr(pthread_create (&tid, &pattr, sigwaiter, NULL)); 149 150 /* 151 * Verify that an ignored signal doesn't cause a wakeup. 152 * We don't have a handler installed for SIGIO. 153 */ 154 CHECKr(pthread_kill (tid, SIGIO)); 155 sleep (1); 156 CHECKe(kill(getpid(), SIGIO)); 157 sleep (1); 158 /* sigwait should not wake up for ignored signal SIGIO */ 159 ASSERT(sigcounts[SIGIO] == 0); 160 161 /* 162 * Verify that a signal with a default action of ignore, for 163 * which we have a signal handler installed, will release a sigwait. 164 */ 165 CHECKr(pthread_kill (tid, SIGURG)); 166 sleep (1); 167 CHECKe(kill(getpid(), SIGURG)); 168 sleep (1); 169 /* sigwait should wake up for SIGURG */ 170 ASSERT(sigcounts[SIGURG] == 2); 171 172 /* 173 * Verify that a signal with a default action that terminates 174 * the process will release a sigwait. 175 */ 176 CHECKr(pthread_kill (tid, SIGUSR1)); 177 sleep (1); 178 CHECKe(kill(getpid(), SIGUSR1)); 179 sleep (1); 180 if (sigcounts[SIGUSR1] != 2) 181 printf ("FAIL: sigwait doesn't wake up for SIGUSR1.\n"); 182 183 /* 184 * Verify that if we install a signal handler for a previously 185 * ignored signal, an occurrence of this signal will release 186 * the (already waiting) sigwait. 187 */ 188 189 /* Install a signal handler for SIGHUP. */ 190 sigemptyset (&act.sa_mask); 191 sigaddset (&act.sa_mask, SIGHUP); 192 act.sa_handler = sighandler; 193 act.sa_flags = SA_RESTART; 194 CHECKe(sigaction (SIGHUP, &act, NULL)); 195 196 /* Sending SIGHUP should release the sigwait. */ 197 CHECKe(kill(getpid(), SIGHUP)); 198 sleep (1); 199 CHECKr(pthread_kill (tid, SIGHUP)); 200 sleep (1); 201 /* sigwait should wake up for SIGHUP */ 202 ASSERT(sigcounts[SIGHUP] == 2); 203 204 /* 205 * Verify that a pending signal in the waiters mask will 206 * cause sigwait to return the pending signal. We do this 207 * by taking the waiters mutex and signaling the waiter to 208 * release him from the sigwait. The waiter will block 209 * on taking the mutex, and we can then send the waiter a 210 * signal which should be added to his pending signals. 211 * The next time the waiter does a sigwait, he should 212 * return with the pending signal. 213 */ 214 sigcounts[SIGHUP] = 0; 215 CHECKr(pthread_mutex_lock (&waiter_mutex)); 216 /* Release the waiter from sigwait. */ 217 CHECKe(kill(getpid(), SIGHUP)); 218 sleep (1); 219 /* signal handler should wake up for SIGHUP */ 220 ASSERT(sigcounts[SIGHUP] == 1); 221 /* 222 * Add SIGHUP to all threads pending signals. Since there is 223 * a signal handler installed for SIGHUP and this signal is 224 * blocked from the waiter thread and unblocked in the main 225 * thread, the signal handler should be called once for SIGHUP. 226 */ 227 CHECKe(kill(getpid(), SIGHUP)); 228 /* Release the waiter thread and allow him to run. */ 229 CHECKr(pthread_mutex_unlock (&waiter_mutex)); 230 sleep (1); 231 /* 232 * sigwait should NOT return for pending SIGHUP. Nothing is pending 233 * because the signal was processed by the SIGHUP signal handler. 234 */ 235 ASSERT(sigcounts[SIGHUP] == 2); 236 237 /* 238 * Repeat the above test using pthread_kill and SIGUSR1 239 */ 240 sigcounts[SIGUSR1] = 0; 241 CHECKr(pthread_mutex_lock (&waiter_mutex)); 242 /* Release the waiter from sigwait. */ 243 CHECKr(pthread_kill (tid, SIGUSR1)); 244 sleep (1); 245 /* sigwait should wake up for SIGUSR1 */ 246 ASSERT(sigcounts[SIGUSR1] == 1); 247 /* Add SIGUSR1 to the waiters pending signals. */ 248 CHECKr(pthread_kill (tid, SIGUSR1)); 249 /* Release the waiter thread and allow him to run. */ 250 CHECKe(pthread_mutex_unlock (&waiter_mutex)); 251 sleep (1); 252 /* sigwait should return for pending SIGUSR1 */ 253 ASSERT(sigcounts[SIGUSR1] == 2); 254 255 #if 0 256 /* 257 * Verify that we can still kill the process for a signal 258 * not being waited on by sigwait. 259 */ 260 CHECKe(kill(getpid(), SIGPIPE)); 261 PANIC("SIGPIPE did not terminate process"); 262 263 /* 264 * Wait for the thread to finish. 265 */ 266 CHECKr(pthread_join (tid, NULL)); 267 #endif 268 269 SUCCEED; 270 } 271