]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpthread/thread/thr_sigwait.c
This commit was generated by cvs2svn to compensate for changes in r54820,
[FreeBSD/FreeBSD.git] / lib / libpthread / thread / thr_sigwait.c
1 /*
2  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <signal.h>
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
37 #include <errno.h>
38 #ifdef _THREAD_SAFE
39 #include <pthread.h>
40 #include "pthread_private.h"
41
42 int
43 sigwait(const sigset_t * set, int *sig)
44 {
45         int             ret = 0;
46         int             i;
47         sigset_t        tempset, waitset;
48         struct sigaction act;
49         
50         _thread_enter_cancellation_point();
51         /*
52          * Specify the thread kernel signal handler.
53          */
54         act.sa_handler = (void (*) ()) _thread_sig_handler;
55         act.sa_flags = SA_RESTART;
56         act.sa_mask = *set;
57
58         /* Ensure the scheduling signal is masked: */
59         sigaddset(&act.sa_mask, _SCHED_SIGNAL);
60
61         /*
62          * Initialize the set of signals that will be waited on:
63          */
64         waitset = *set;
65
66         /* These signals can't be waited on. */
67         sigdelset(&waitset, SIGKILL);
68         sigdelset(&waitset, SIGSTOP);
69         sigdelset(&waitset, _SCHED_SIGNAL);
70         sigdelset(&waitset, SIGCHLD);
71         sigdelset(&waitset, SIGINFO);
72
73         /* Check to see if a pending signal is in the wait mask. */
74         tempset = _thread_run->sigpend;
75         SIGSETOR(tempset, _process_sigpending);
76         SIGSETAND(tempset, waitset);
77         if (SIGNOTEMPTY(tempset)) {
78                 /* Enter a loop to find a pending signal: */
79                 for (i = 1; i < NSIG; i++) {
80                         if (sigismember (&tempset, i))
81                                 break;
82                 }
83
84                 /* Clear the pending signal: */
85                 if (sigismember(&_thread_run->sigpend,i))
86                         sigdelset(&_thread_run->sigpend,i);
87                 else
88                         sigdelset(&_process_sigpending,i);
89
90                 /* Return the signal number to the caller: */
91                 *sig = i;
92
93                 _thread_leave_cancellation_point();
94                 return (0);
95         }
96
97         /*
98          * Enter a loop to find the signals that are SIG_DFL.  For
99          * these signals we must install a dummy signal handler in
100          * order for the kernel to pass them in to us.  POSIX says
101          * that the _application_ must explicitly install a dummy
102          * handler for signals that are SIG_IGN in order to sigwait
103          * on them.  Note that SIG_IGN signals are left in the
104          * mask because a subsequent sigaction could enable an
105          * ignored signal.
106          */
107         for (i = 1; i < NSIG; i++) {
108                 if (sigismember(&waitset, i) &&
109                     (_thread_sigact[i - 1].sa_handler == SIG_DFL)) {
110                         if (_thread_sys_sigaction(i,&act,NULL) != 0)
111                                 ret = -1;
112                 }
113         }
114         if (ret == 0) {
115                 /*
116                  * Save the wait signal mask.  The wait signal
117                  * mask is independent of the threads signal mask
118                  * and requires separate storage.
119                  */
120                 _thread_run->data.sigwait = &waitset;
121
122                 /* Wait for a signal: */
123                 _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__);
124
125                 /* Return the signal number to the caller: */
126                 *sig = _thread_run->signo;
127
128                 /*
129                  * Probably unnecessary, but since it's in a union struct
130                  * we don't know how it could be used in the future.
131                  */
132                 _thread_run->data.sigwait = NULL;
133         }
134
135         /* Restore the sigactions: */
136         act.sa_handler = SIG_DFL;
137         for (i = 1; i < NSIG; i++) {
138                 if (sigismember(&waitset, i) &&
139                     (_thread_sigact[i - 1].sa_handler == SIG_DFL)) {
140                         if (_thread_sys_sigaction(i,&act,NULL) != 0)
141                                 ret = -1;
142                 }
143         }
144
145         _thread_leave_cancellation_point();
146         /* Return the completion status: */
147         return (ret);
148 }
149 #endif