Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libc_r / uthread / uthread_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: src/lib/libc_r/uthread/uthread_sigwait.c,v 1.13.2.5 2002/10/22 14:44:03 fjoe Exp $
33  * $DragonFly: src/lib/libc_r/uthread/uthread_sigwait.c,v 1.2 2003/06/17 04:26:48 dillon Exp $
34  */
35 #include <signal.h>
36 #include <sys/param.h>
37 #include <sys/signalvar.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include "pthread_private.h"
41
42 int
43 sigwait(const sigset_t *set, int *sig)
44 {
45         struct pthread  *curthread = _get_curthread();
46         int             ret = 0;
47         int             i;
48         sigset_t        tempset, waitset;
49         struct sigaction act;
50         
51         _thread_enter_cancellation_point();
52         /*
53          * Specify the thread kernel signal handler.
54          */
55         act.sa_handler = (void (*) ()) _thread_sig_handler;
56         act.sa_flags = SA_SIGINFO | SA_RESTART;
57         /* Ensure the signal handler cannot be interrupted by other signals: */
58         sigfillset(&act.sa_mask);
59
60         /*
61          * Initialize the set of signals that will be waited on:
62          */
63         waitset = *set;
64
65         /* These signals can't be waited on. */
66         sigdelset(&waitset, SIGKILL);
67         sigdelset(&waitset, SIGSTOP);
68         sigdelset(&waitset, _SCHED_SIGNAL);
69         sigdelset(&waitset, SIGCHLD);
70         sigdelset(&waitset, SIGINFO);
71
72         /* Check to see if a pending signal is in the wait mask. */
73         tempset = curthread->sigpend;
74         SIGSETOR(tempset, _process_sigpending);
75         SIGSETAND(tempset, waitset);
76         if (SIGNOTEMPTY(tempset)) {
77                 /* Enter a loop to find a pending signal: */
78                 for (i = 1; i < NSIG; i++) {
79                         if (sigismember (&tempset, i))
80                                 break;
81                 }
82
83                 /* Clear the pending signal: */
84                 if (sigismember(&curthread->sigpend,i))
85                         sigdelset(&curthread->sigpend,i);
86                 else
87                         sigdelset(&_process_sigpending,i);
88
89                 /* Return the signal number to the caller: */
90                 *sig = i;
91
92                 _thread_leave_cancellation_point();
93                 return (0);
94         }
95
96         /*
97          * Access the _thread_dfl_count array under the protection of signal
98          * deferral.
99          */
100         _thread_kern_sig_defer();
101
102         /*
103          * Enter a loop to find the signals that are SIG_DFL.  For
104          * these signals we must install a dummy signal handler in
105          * order for the kernel to pass them in to us.  POSIX says
106          * that the _application_ must explicitly install a dummy
107          * handler for signals that are SIG_IGN in order to sigwait
108          * on them.  Note that SIG_IGN signals are left in the
109          * mask because a subsequent sigaction could enable an
110          * ignored signal.
111          */
112         sigemptyset(&tempset);
113         for (i = 1; i < NSIG; i++) {
114                 if (sigismember(&waitset, i) &&
115                     (_thread_sigact[i - 1].sa_handler == SIG_DFL)) {
116                         _thread_dfl_count[i]++;
117                         sigaddset(&tempset, i);
118                         if (_thread_dfl_count[i] == 1) {
119                                 if (__sys_sigaction(i,&act,NULL) != 0)
120                                         ret = -1;
121                         }
122                 }
123         }
124         /* Done accessing _thread_dfl_count for now. */
125         _thread_kern_sig_undefer();
126
127         if (ret == 0) {
128                 /*
129                  * Save the wait signal mask.  The wait signal
130                  * mask is independent of the threads signal mask
131                  * and requires separate storage.
132                  */
133                 curthread->data.sigwait = &waitset;
134
135                 /* Wait for a signal: */
136                 _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__);
137
138                 /* Return the signal number to the caller: */
139                 *sig = curthread->signo;
140
141                 /*
142                  * Probably unnecessary, but since it's in a union struct
143                  * we don't know how it could be used in the future.
144                  */
145                 curthread->data.sigwait = NULL;
146         }
147
148         /*
149          * Access the _thread_dfl_count array under the protection of signal
150          * deferral.
151          */
152         _thread_kern_sig_defer();
153
154         /* Restore the sigactions: */
155         act.sa_handler = SIG_DFL;
156         for (i = 1; i < NSIG; i++) {
157                 if (sigismember(&tempset, i)) {
158                         _thread_dfl_count[i]--;
159                         if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) &&
160                             (_thread_dfl_count[i] == 0)) {
161                                 if (__sys_sigaction(i,&act,NULL) != 0)
162                                         ret = -1;
163                         }
164                 }
165         }
166         /* Done accessing _thread_dfl_count. */
167         _thread_kern_sig_undefer();
168
169         _thread_leave_cancellation_point();
170         
171         /* Return the completion status: */
172         return (ret);
173 }