1 //depot/projects/kse/lib/libpthread/thread/thr_sigwait.c#1 - branch change 15154 (text+ko)
2 /*
3 * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
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. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: stable/10/lib/libkse/thread/thr_sigwait.c 174689 2007-12-16 23:29:57Z deischen $
31 */
32
33 #include "namespace.h"
34 #include <signal.h>
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
37 #include <errno.h>
38 #include <pthread.h>
39 #include "un-namespace.h"
40 #include "thr_private.h"
41
42 int __sigtimedwait(const sigset_t *set, siginfo_t *info,
43 const struct timespec *timeout);
44 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
45 int __sigwait(const sigset_t *set, int *sig);
46 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
47 const struct timespec *timeout);
48 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
49 int _sigwait(const sigset_t *set, int *sig);
50
51 __weak_reference(__sigwait, sigwait);
52 __weak_reference(__sigtimedwait, sigtimedwait);
53 __weak_reference(__sigwaitinfo, sigwaitinfo);
54
55 static int
lib_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)56 lib_sigtimedwait(const sigset_t *set, siginfo_t *info,
57 const struct timespec *timeout)
58 {
59 struct pthread *curthread = _get_curthread();
60 int ret = 0;
61 int i;
62 struct sigwait_data waitdata;
63 sigset_t waitset;
64 kse_critical_t crit;
65 siginfo_t siginfo;
66
67 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
68 if (info == NULL)
69 info = &siginfo;
70 return (__sys_sigtimedwait(set, info, timeout));
71 }
72
73 /*
74 * Initialize the set of signals that will be waited on:
75 */
76 waitset = *set;
77
78 /* These signals can't be waited on. */
79 SIGDELSET(waitset, SIGKILL);
80 SIGDELSET(waitset, SIGSTOP);
81
82 /*
83 * POSIX says that the _application_ must explicitly install
84 * a dummy handler for signals that are SIG_IGN in order
85 * to sigwait on them. Note that SIG_IGN signals are left in
86 * the mask because a subsequent sigaction could enable an
87 * ignored signal.
88 */
89
90 crit = _kse_critical_enter();
91 KSE_SCHED_LOCK(curthread->kse, curthread->kseg);
92 for (i = 1; i <= _SIG_MAXSIG; ++i) {
93 if (SIGISMEMBER(waitset, i) &&
94 SIGISMEMBER(curthread->sigpend, i)) {
95 SIGDELSET(curthread->sigpend, i);
96 siginfo = curthread->siginfo[i - 1];
97 KSE_SCHED_UNLOCK(curthread->kse,
98 curthread->kseg);
99 _kse_critical_leave(crit);
100 ret = i;
101 goto OUT;
102 }
103 }
104 curthread->timeout = 0;
105 curthread->interrupted = 0;
106 _thr_set_timeout(timeout);
107 /* Wait for a signal: */
108 siginfo.si_signo = 0;
109 waitdata.waitset = &waitset;
110 waitdata.siginfo = &siginfo;
111 curthread->data.sigwait = &waitdata;
112 THR_SET_STATE(curthread, PS_SIGWAIT);
113 _thr_sched_switch_unlocked(curthread);
114 /*
115 * Return the signal number to the caller:
116 */
117 if (siginfo.si_signo > 0) {
118 ret = siginfo.si_signo;
119 } else {
120 if (curthread->interrupted)
121 errno = EINTR;
122 else if (curthread->timeout)
123 errno = EAGAIN;
124 ret = -1;
125 }
126 curthread->timeout = 0;
127 curthread->interrupted = 0;
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 curthread->data.sigwait = NULL;
133
134 OUT:
135 if (ret > 0 && info != NULL)
136 *info = siginfo;
137
138 return (ret);
139 }
140
141 int
__sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)142 __sigtimedwait(const sigset_t *set, siginfo_t *info,
143 const struct timespec * timeout)
144 {
145 struct pthread *curthread = _get_curthread();
146 int ret;
147
148 _thr_cancel_enter(curthread);
149 ret = lib_sigtimedwait(set, info, timeout);
150 _thr_cancel_leave(curthread, 1);
151 return (ret);
152 }
153
_sigtimedwait(const sigset_t * set,siginfo_t * info,const struct timespec * timeout)154 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
155 const struct timespec * timeout)
156 {
157 return lib_sigtimedwait(set, info, timeout);
158 }
159
160 int
__sigwaitinfo(const sigset_t * set,siginfo_t * info)161 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
162 {
163 struct pthread *curthread = _get_curthread();
164 int ret;
165
166 _thr_cancel_enter(curthread);
167 ret = lib_sigtimedwait(set, info, NULL);
168 _thr_cancel_leave(curthread, 1);
169 return (ret);
170 }
171
172 int
_sigwaitinfo(const sigset_t * set,siginfo_t * info)173 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
174 {
175 return lib_sigtimedwait(set, info, NULL);
176 }
177
178 int
__sigwait(const sigset_t * set,int * sig)179 __sigwait(const sigset_t *set, int *sig)
180 {
181 struct pthread *curthread = _get_curthread();
182 int ret;
183
184 _thr_cancel_enter(curthread);
185 ret = lib_sigtimedwait(set, NULL, NULL);
186 if (ret > 0) {
187 *sig = ret;
188 ret = 0;
189 } else {
190 ret = errno;
191 }
192 _thr_cancel_leave(curthread, 1);
193 return (ret);
194 }
195
196 int
_sigwait(const sigset_t * set,int * sig)197 _sigwait(const sigset_t *set, int *sig)
198 {
199 int ret;
200
201 ret = lib_sigtimedwait(set, NULL, NULL);
202 if (ret > 0) {
203 *sig = ret;
204 ret = 0;
205 } else {
206 ret = errno;
207 }
208 return (ret);
209 }
210
211