Print this page
OS-4470 lxbrand unblocking signals in new threads must be atomic
Reviewed by: Robert Mustacchi <rm@joyent.com>
OS-3561 lxbrand emulation library should execute on alternate stack
OS-3558 lxbrand add support for full in-kernel syscall handling
OS-3545 lx_syscall_regs should not walk stack
OS-3868 many LTP testcases now hang
OS-3901 lxbrand lx_recvmsg fails to translate control messages when 64-bit
Reviewed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
Reviewed by: Bryan Cantrill <bryan@joyent.com>
OS-2844 lx brand should support 64-bit user-land
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libc/port/threads/sigaction.c
+++ new/usr/src/lib/libc/port/threads/sigaction.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 + * Copyright 2015 Joyent, Inc.
25 26 */
26 27
27 28 #include "lint.h"
28 29 #include <sys/feature_tests.h>
29 30 /*
30 31 * setcontext() really can return, if UC_CPU is not specified.
31 32 * Make the compiler shut up about it.
32 33 */
33 34 #if defined(__NORETURN)
34 35 #undef __NORETURN
35 36 #endif
36 37 #define __NORETURN
37 38 #include "thr_uberdata.h"
38 39 #include "asyncio.h"
39 40 #include <signal.h>
40 41 #include <siginfo.h>
41 42 #include <sys/systm.h>
42 43
43 44 /* maskable signals */
44 45 const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
45 46
46 47 /*
47 48 * Return true if the valid signal bits in both sets are the same.
48 49 */
49 50 int
50 51 sigequalset(const sigset_t *s1, const sigset_t *s2)
51 52 {
52 53 /*
53 54 * We only test valid signal bits, not rubbish following MAXSIG
54 55 * (for speed). Algorithm:
55 56 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
56 57 */
57 58 /* see lib/libc/inc/thr_uberdata.h for why this must be true */
58 59 #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32))
59 60 return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
60 61 (s1->__sigbits[1] ^ s2->__sigbits[1]) |
61 62 ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2)));
62 63 #else
63 64 #error "fix me: MAXSIG out of bounds"
64 65 #endif
65 66 }
66 67
67 68 /*
68 69 * Common code for calling the user-specified signal handler.
69 70 */
70 71 void
71 72 call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
72 73 {
73 74 ulwp_t *self = curthread;
74 75 uberdata_t *udp = self->ul_uberdata;
75 76 struct sigaction uact;
76 77 volatile struct sigaction *sap;
77 78
78 79 /*
79 80 * If we are taking a signal while parked or about to be parked
80 81 * on __lwp_park() then remove ourself from the sleep queue so
81 82 * that we can grab locks. The code in mutex_lock_queue() and
82 83 * cond_wait_common() will detect this and deal with it when
83 84 * __lwp_park() returns.
84 85 */
85 86 unsleep_self();
86 87 set_parking_flag(self, 0);
87 88
88 89 if (__td_event_report(self, TD_CATCHSIG, udp)) {
89 90 self->ul_td_evbuf.eventnum = TD_CATCHSIG;
90 91 self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
91 92 tdb_event(TD_CATCHSIG, udp);
92 93 }
93 94
94 95 /*
95 96 * Get a self-consistent set of flags, handler, and mask
96 97 * while holding the sig's sig_lock for the least possible time.
97 98 * We must acquire the sig's sig_lock because some thread running
98 99 * in sigaction() might be establishing a new signal handler.
99 100 * The code in sigaction() acquires the writer lock; here
100 101 * we acquire the readers lock to ehance concurrency in the
101 102 * face of heavy signal traffic, such as generated by java.
102 103 *
103 104 * Locking exceptions:
104 105 * No locking for a child of vfork().
105 106 * If the signal is SIGPROF with an si_code of PROF_SIG,
106 107 * then we assume that this signal was generated by
107 108 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
108 109 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
109 110 * then we assume that the signal was generated by
110 111 * a hardware performance counter overflow.
111 112 * In these cases, assume that we need no locking. It is the
112 113 * monitoring program's responsibility to ensure correctness.
113 114 */
114 115 sap = &udp->siguaction[sig].sig_uaction;
115 116 if (self->ul_vfork ||
116 117 (sip != NULL &&
117 118 ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
118 119 (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
119 120 /* we wish this assignment could be atomic */
120 121 (void) memcpy(&uact, (void *)sap, sizeof (uact));
121 122 } else {
122 123 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
123 124 lrw_rdlock(rwlp);
124 125 (void) memcpy(&uact, (void *)sap, sizeof (uact));
125 126 if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
126 127 (sap->sa_flags & SA_RESETHAND))
127 128 sap->sa_sigaction = SIG_DFL;
128 129 lrw_unlock(rwlp);
129 130 }
130 131
131 132 /*
132 133 * Set the proper signal mask and call the user's signal handler.
133 134 * (We overrode the user-requested signal mask with maskset
134 135 * so we currently have all blockable signals blocked.)
135 136 *
136 137 * We would like to ASSERT() that the signal is not a member of the
137 138 * signal mask at the previous level (ucp->uc_sigmask) or the specified
138 139 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
139 140 * /proc can override this via PCSSIG, so we don't bother.
140 141 *
141 142 * We would also like to ASSERT() that the signal mask at the previous
142 143 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
143 144 * but /proc can change the thread's signal mask via PCSHOLD, so we
144 145 * don't bother with that either.
145 146 */
146 147 ASSERT(ucp->uc_flags & UC_SIGMASK);
147 148 if (self->ul_sigsuspend) {
148 149 ucp->uc_sigmask = self->ul_sigmask;
149 150 self->ul_sigsuspend = 0;
150 151 /* the sigsuspend() or pollsys() signal mask */
151 152 sigorset(&uact.sa_mask, &self->ul_tmpmask);
152 153 } else {
153 154 /* the signal mask at the previous level */
154 155 sigorset(&uact.sa_mask, &ucp->uc_sigmask);
155 156 }
156 157 if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */
157 158 (void) sigaddset(&uact.sa_mask, sig);
158 159 self->ul_sigmask = uact.sa_mask;
159 160 self->ul_siglink = ucp;
160 161 (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask);
161 162
162 163 /*
163 164 * If this thread has been sent SIGCANCEL from the kernel
164 165 * or from pthread_cancel(), it is being asked to exit.
165 166 * The kernel may send SIGCANCEL without a siginfo struct.
166 167 * If the SIGCANCEL is process-directed (from kill() or
167 168 * sigqueue()), treat it as an ordinary signal.
168 169 */
169 170 if (sig == SIGCANCEL) {
170 171 if (sip == NULL || SI_FROMKERNEL(sip) ||
171 172 sip->si_code == SI_LWP) {
172 173 do_sigcancel();
173 174 goto out;
174 175 }
175 176 /* SIGCANCEL is ignored by default */
176 177 if (uact.sa_sigaction == SIG_DFL ||
177 178 uact.sa_sigaction == SIG_IGN)
178 179 goto out;
179 180 }
180 181
181 182 /*
182 183 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
183 184 * we are an aio worker thread, cancel the aio request.
184 185 */
185 186 if (sig == SIGAIOCANCEL) {
186 187 aio_worker_t *aiowp = pthread_getspecific(_aio_key);
187 188
188 189 if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
189 190 siglongjmp(aiowp->work_jmp_buf, 1);
190 191 /* SIGLWP is ignored by default */
191 192 if (uact.sa_sigaction == SIG_DFL ||
192 193 uact.sa_sigaction == SIG_IGN)
193 194 goto out;
194 195 }
195 196
196 197 if (!(uact.sa_flags & SA_SIGINFO))
197 198 sip = NULL;
198 199 __sighndlr(sig, sip, ucp, uact.sa_sigaction);
199 200
200 201 #if defined(sparc) || defined(__sparc)
201 202 /*
202 203 * If this is a floating point exception and the queue
203 204 * is non-empty, pop the top entry from the queue. This
204 205 * is to maintain expected behavior.
205 206 */
206 207 if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
207 208 fpregset_t *fp = &ucp->uc_mcontext.fpregs;
208 209
209 210 if (--fp->fpu_qcnt > 0) {
210 211 unsigned char i;
211 212 struct _fq *fqp;
212 213
213 214 fqp = fp->fpu_q;
214 215 for (i = 0; i < fp->fpu_qcnt; i++)
215 216 fqp[i] = fqp[i+1];
216 217 }
217 218 }
218 219 #endif /* sparc */
219 220
220 221 out:
221 222 (void) setcontext(ucp);
222 223 thr_panic("call_user_handler(): setcontext() returned");
223 224 }
224 225
225 226 /*
226 227 * take_deferred_signal() is called when ul_critical and ul_sigdefer become
227 228 * zero and a deferred signal has been recorded on the current thread.
228 229 * We are out of the critical region and are ready to take a signal.
229 230 * The kernel has all signals blocked on this lwp, but our value of
230 231 * ul_sigmask is the correct signal mask for the previous context.
231 232 *
232 233 * We call __sigresend() to atomically restore the signal mask and
233 234 * cause the signal to be sent again with the remembered siginfo.
234 235 * We will not return successfully from __sigresend() until the
235 236 * application's signal handler has been run via sigacthandler().
236 237 */
237 238 void
238 239 take_deferred_signal(int sig)
239 240 {
240 241 extern int __sigresend(int, siginfo_t *, sigset_t *);
241 242 ulwp_t *self = curthread;
242 243 siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
243 244 siginfo_t *sip;
244 245 int error;
245 246
246 247 ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
247 248
248 249 /*
249 250 * If the signal handler was established with SA_RESETHAND,
250 251 * the kernel has reset the handler to SIG_DFL, so we have
251 252 * to reestablish the handler now so that it will be entered
252 253 * again when we call __sigresend(), below.
253 254 *
254 255 * Logically, we should acquire and release the signal's
255 256 * sig_lock around this operation to protect the integrity
256 257 * of the signal action while we copy it, as is done below
257 258 * in _libc_sigaction(). However, we may be on a user-level
258 259 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
259 260 * might attempt to sleep on a different sleep queue and
260 261 * that would corrupt the entire sleep queue mechanism.
261 262 *
262 263 * If we are on a sleep queue we will remove ourself from
263 264 * it in call_user_handler(), called from sigacthandler(),
264 265 * before entering the application's signal handler.
265 266 * In the meantime, we must not acquire any locks.
266 267 */
267 268 if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
268 269 struct sigaction tact = suap->sig_uaction;
269 270 tact.sa_flags &= ~SA_NODEFER;
270 271 tact.sa_sigaction = self->ul_uberdata->sigacthandler;
271 272 tact.sa_mask = maskset;
272 273 (void) __sigaction(sig, &tact, NULL);
273 274 }
274 275
275 276 if (self->ul_siginfo.si_signo == 0)
276 277 sip = NULL;
|
↓ open down ↓ |
242 lines elided |
↑ open up ↑ |
277 278 else
278 279 sip = &self->ul_siginfo;
279 280
280 281 /* EAGAIN can happen only for a pending SIGSTOP signal */
281 282 while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
282 283 continue;
283 284 if (error)
284 285 thr_panic("take_deferred_signal(): __sigresend() failed");
285 286 }
286 287
288 +/*
289 + * sigacthandler() attempts to clean up dangling uc_link pointers in
290 + * signal handling contexts when libc believes us to have escaped
291 + * a signal handler incorrectly in the past.
292 + *
293 + * Branded processes have a legitimate use for a chain including contexts
294 + * other than those used for signal handling when tracking emulation
295 + * requests from the kernel. We allow them to disable this cleanup
296 + * behaviour.
297 + */
298 +static int escaped_context_cleanup = 1;
299 +
287 300 void
301 +set_escaped_context_cleanup(int on)
302 +{
303 + escaped_context_cleanup = on;
304 +}
305 +
306 +void
288 307 sigacthandler(int sig, siginfo_t *sip, void *uvp)
289 308 {
290 309 ucontext_t *ucp = uvp;
291 310 ulwp_t *self = curthread;
292 311
293 312 /*
294 313 * Do this in case we took a signal while in a cancelable system call.
295 314 * It does no harm if we were not in such a system call.
296 315 */
297 316 self->ul_sp = 0;
298 317 if (sig != SIGCANCEL)
|
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
299 318 self->ul_cancel_async = self->ul_save_async;
300 319
301 320 /*
302 321 * If this thread has performed a longjmp() from a signal handler
303 322 * back to main level some time in the past, it has left the kernel
304 323 * thinking that it is still in the signal context. We repair this
305 324 * possible damage by setting ucp->uc_link to NULL if we know that
306 325 * we are actually executing at main level (self->ul_siglink == NULL).
307 326 * See the code for setjmp()/longjmp() for more details.
308 327 */
309 - if (self->ul_siglink == NULL)
328 + if (escaped_context_cleanup && self->ul_siglink == NULL)
310 329 ucp->uc_link = NULL;
311 330
312 331 /*
313 332 * If we are not in a critical region and are
314 333 * not deferring signals, take the signal now.
315 334 */
316 335 if ((self->ul_critical + self->ul_sigdefer) == 0) {
317 336 call_user_handler(sig, sip, ucp);
318 337 /*
319 338 * On the surface, the following call seems redundant
320 339 * because call_user_handler() cannot return. However,
321 340 * we don't want to return from here because the compiler
322 341 * might recycle our frame. We want to keep it on the
323 342 * stack to assist debuggers such as pstack in identifying
324 343 * signal frames. The call to thr_panic() serves to prevent
325 344 * tail-call optimisation here.
326 345 */
327 346 thr_panic("sigacthandler(): call_user_handler() returned");
328 347 }
329 348
330 349 /*
331 350 * We are in a critical region or we are deferring signals. When
332 351 * we emerge from the region we will call take_deferred_signal().
333 352 */
334 353 ASSERT(self->ul_cursig == 0);
335 354 self->ul_cursig = (char)sig;
336 355 if (sip != NULL)
337 356 (void) memcpy(&self->ul_siginfo,
338 357 sip, sizeof (siginfo_t));
339 358 else
340 359 self->ul_siginfo.si_signo = 0;
341 360
342 361 /*
343 362 * Make sure that if we return to a call to __lwp_park()
344 363 * or ___lwp_cond_wait() that it returns right away
345 364 * (giving us a spurious wakeup but not a deadlock).
346 365 */
347 366 set_parking_flag(self, 0);
348 367
349 368 /*
350 369 * Return to the previous context with all signals blocked.
351 370 * We will restore the signal mask in take_deferred_signal().
352 371 * Note that we are calling the system call trap here, not
353 372 * the setcontext() wrapper. We don't want to change the
354 373 * thread's ul_sigmask by this operation.
355 374 */
356 375 ucp->uc_sigmask = maskset;
357 376 (void) __setcontext(ucp);
358 377 thr_panic("sigacthandler(): __setcontext() returned");
359 378 }
360 379
361 380 #pragma weak _sigaction = sigaction
362 381 int
363 382 sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
364 383 {
365 384 ulwp_t *self = curthread;
366 385 uberdata_t *udp = self->ul_uberdata;
367 386 struct sigaction oaction;
368 387 struct sigaction tact;
369 388 struct sigaction *tactp = NULL;
370 389 int rv;
371 390
372 391 if (sig <= 0 || sig >= NSIG) {
373 392 errno = EINVAL;
374 393 return (-1);
375 394 }
376 395
377 396 if (!self->ul_vfork)
378 397 lrw_wrlock(&udp->siguaction[sig].sig_lock);
379 398
380 399 oaction = udp->siguaction[sig].sig_uaction;
381 400
382 401 if (nact != NULL) {
383 402 tact = *nact; /* make a copy so we can modify it */
384 403 tactp = &tact;
385 404 delete_reserved_signals(&tact.sa_mask);
386 405
387 406 #if !defined(_LP64)
388 407 tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */
389 408 #endif
390 409 /*
391 410 * To be compatible with the behavior of SunOS 4.x:
392 411 * If the new signal handler is SIG_IGN or SIG_DFL, do
393 412 * not change the signal's entry in the siguaction array.
394 413 * This allows a child of vfork(2) to set signal handlers
395 414 * to SIG_IGN or SIG_DFL without affecting the parent.
396 415 *
397 416 * This also covers a race condition with some thread
398 417 * setting the signal action to SIG_DFL or SIG_IGN
399 418 * when the thread has also received and deferred
400 419 * that signal. When the thread takes the deferred
401 420 * signal, even though it has set the action to SIG_DFL
402 421 * or SIG_IGN, it will execute the old signal handler
403 422 * anyway. This is an inherent signaling race condition
404 423 * and is not a bug.
405 424 *
406 425 * A child of vfork() is not allowed to change signal
407 426 * handlers to anything other than SIG_DFL or SIG_IGN.
408 427 */
409 428 if (self->ul_vfork) {
410 429 if (tact.sa_sigaction != SIG_IGN)
411 430 tact.sa_sigaction = SIG_DFL;
412 431 } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
413 432 /*
414 433 * Always catch these signals.
415 434 * We need SIGCANCEL for pthread_cancel() to work.
416 435 * We need SIGAIOCANCEL for aio_cancel() to work.
417 436 */
418 437 udp->siguaction[sig].sig_uaction = tact;
419 438 if (tact.sa_sigaction == SIG_DFL ||
420 439 tact.sa_sigaction == SIG_IGN)
421 440 tact.sa_flags = SA_SIGINFO;
422 441 else {
423 442 tact.sa_flags |= SA_SIGINFO;
424 443 tact.sa_flags &=
425 444 ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
426 445 }
427 446 tact.sa_sigaction = udp->sigacthandler;
428 447 tact.sa_mask = maskset;
429 448 } else if (tact.sa_sigaction != SIG_DFL &&
430 449 tact.sa_sigaction != SIG_IGN) {
431 450 udp->siguaction[sig].sig_uaction = tact;
432 451 tact.sa_flags &= ~SA_NODEFER;
433 452 tact.sa_sigaction = udp->sigacthandler;
434 453 tact.sa_mask = maskset;
435 454 }
436 455 }
437 456
438 457 if ((rv = __sigaction(sig, tactp, oact)) != 0)
439 458 udp->siguaction[sig].sig_uaction = oaction;
440 459 else if (oact != NULL &&
441 460 oact->sa_sigaction != SIG_DFL &&
442 461 oact->sa_sigaction != SIG_IGN)
443 462 *oact = oaction;
444 463
445 464 /*
446 465 * We detect setting the disposition of SIGIO just to set the
447 466 * _sigio_enabled flag for the asynchronous i/o (aio) code.
448 467 */
449 468 if (sig == SIGIO && rv == 0 && tactp != NULL) {
450 469 _sigio_enabled =
|
↓ open down ↓ |
131 lines elided |
↑ open up ↑ |
451 470 (tactp->sa_handler != SIG_DFL &&
452 471 tactp->sa_handler != SIG_IGN);
453 472 }
454 473
455 474 if (!self->ul_vfork)
456 475 lrw_unlock(&udp->siguaction[sig].sig_lock);
457 476 return (rv);
458 477 }
459 478
460 479 /*
461 - * This is a private interface for the linux brand interface.
480 + * This is a private interface for the lx brand.
462 481 */
463 482 void
464 483 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
465 - void (**osigacthandler)(int, siginfo_t *, void *))
484 + void (**osigacthandler)(int, siginfo_t *, void *),
485 + int (*brsetctxt)(const ucontext_t *))
466 486 {
467 487 ulwp_t *self = curthread;
468 488 uberdata_t *udp = self->ul_uberdata;
469 489
470 490 if (osigacthandler != NULL)
471 491 *osigacthandler = udp->sigacthandler;
472 492
473 493 udp->sigacthandler = nsigacthandler;
494 +
495 + if (brsetctxt != NULL)
496 + udp->setctxt = brsetctxt;
474 497 }
475 498
476 499 /*
477 500 * Tell the kernel to block all signals.
478 501 * Use the schedctl interface, or failing that, use __lwp_sigmask().
479 502 * This action can be rescinded only by making a system call that
480 503 * sets the signal mask:
481 504 * __lwp_sigmask(), __sigprocmask(), __setcontext(),
482 505 * __sigsuspend() or __pollsys().
483 506 * In particular, this action cannot be reversed by assigning
484 507 * scp->sc_sigblock = 0. That would be a way to lose signals.
485 508 * See the definition of restore_signals(self).
486 509 */
487 510 void
488 511 block_all_signals(ulwp_t *self)
489 512 {
490 513 volatile sc_shared_t *scp;
491 514
492 515 enter_critical(self);
493 516 if ((scp = self->ul_schedctl) != NULL ||
494 517 (scp = setup_schedctl()) != NULL)
495 518 scp->sc_sigblock = 1;
496 519 else
497 520 (void) __lwp_sigmask(SIG_SETMASK, &maskset);
498 521 exit_critical(self);
499 522 }
500 523
501 524 /*
502 525 * setcontext() has code that forcibly restores the curthread
503 526 * pointer in a context passed to the setcontext(2) syscall.
504 527 *
505 528 * Certain processes may need to disable this feature, so these routines
506 529 * provide the mechanism to do so.
507 530 *
508 531 * (As an example, branded 32-bit x86 processes may use %gs for their own
509 532 * purposes, so they need to be able to specify a %gs value to be restored
|
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
510 533 * on return from a signal handler via the passed ucontext_t.)
511 534 */
512 535 static int setcontext_enforcement = 1;
513 536
514 537 void
515 538 set_setcontext_enforcement(int on)
516 539 {
517 540 setcontext_enforcement = on;
518 541 }
519 542
543 +/*
544 + * The LX brand emulation library implements an operation that is analogous to
545 + * setcontext(), but takes a different path in to the kernel. So that it can
546 + * correctly restore a signal mask, we expose just the signal mask handling
547 + * part of the regular setcontext() routine as a private interface.
548 + */
549 +void
550 +setcontext_sigmask(ucontext_t *ucp)
551 +{
552 + ulwp_t *self = curthread;
553 +
554 + if (ucp->uc_flags & UC_SIGMASK) {
555 + block_all_signals(self);
556 + delete_reserved_signals(&ucp->uc_sigmask);
557 + self->ul_sigmask = ucp->uc_sigmask;
558 + if (self->ul_cursig) {
559 + /*
560 + * We have a deferred signal present.
561 + * The signal mask will be set when the
562 + * signal is taken in take_deferred_signal().
563 + */
564 + ASSERT(self->ul_critical + self->ul_sigdefer != 0);
565 + ucp->uc_flags &= ~UC_SIGMASK;
566 + }
567 + }
568 +}
569 +
520 570 #pragma weak _setcontext = setcontext
521 571 int
522 572 setcontext(const ucontext_t *ucp)
523 573 {
524 574 ulwp_t *self = curthread;
575 + uberdata_t *udp = self->ul_uberdata;
525 576 int ret;
526 577 ucontext_t uc;
527 578
528 579 /*
529 580 * Returning from the main context (uc_link == NULL) causes
530 581 * the thread to exit. See setcontext(2) and makecontext(3C).
531 582 */
532 583 if (ucp == NULL)
533 584 thr_exit(NULL);
534 585 (void) memcpy(&uc, ucp, sizeof (uc));
535 586
536 587 /*
537 588 * Restore previous signal mask and context link.
538 589 */
539 - if (uc.uc_flags & UC_SIGMASK) {
540 - block_all_signals(self);
541 - delete_reserved_signals(&uc.uc_sigmask);
542 - self->ul_sigmask = uc.uc_sigmask;
543 - if (self->ul_cursig) {
544 - /*
545 - * We have a deferred signal present.
546 - * The signal mask will be set when the
547 - * signal is taken in take_deferred_signal().
548 - */
549 - ASSERT(self->ul_critical + self->ul_sigdefer != 0);
550 - uc.uc_flags &= ~UC_SIGMASK;
551 - }
552 - }
590 + setcontext_sigmask(&uc);
553 591 self->ul_siglink = uc.uc_link;
554 592
555 593 /*
556 594 * We don't know where this context structure has been.
557 595 * Preserve the curthread pointer, at least.
558 596 *
559 597 * Allow this feature to be disabled if a particular process
560 598 * requests it.
561 599 */
562 600 if (setcontext_enforcement) {
563 601 #if defined(__sparc)
564 602 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
565 603 #elif defined(__amd64)
566 604 uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
567 605 #elif defined(__i386)
568 606 uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
569 607 #else
570 608 #error "none of __sparc, __amd64, __i386 defined"
|
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
571 609 #endif
572 610 }
573 611
574 612 /*
575 613 * Make sure that if we return to a call to __lwp_park()
576 614 * or ___lwp_cond_wait() that it returns right away
577 615 * (giving us a spurious wakeup but not a deadlock).
578 616 */
579 617 set_parking_flag(self, 0);
580 618 self->ul_sp = 0;
581 - ret = __setcontext(&uc);
619 + ret = udp->setctxt(&uc);
582 620
583 621 /*
584 622 * It is OK for setcontext() to return if the user has not specified
585 623 * UC_CPU.
586 624 */
587 625 if (uc.uc_flags & UC_CPU)
588 626 thr_panic("setcontext(): __setcontext() returned");
589 627 return (ret);
590 628 }
591 629
592 630 #pragma weak _thr_sigsetmask = thr_sigsetmask
593 631 int
594 632 thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
595 633 {
596 634 ulwp_t *self = curthread;
597 635 sigset_t saveset;
598 636
599 637 if (set == NULL) {
600 638 enter_critical(self);
601 639 if (oset != NULL)
602 640 *oset = self->ul_sigmask;
603 641 exit_critical(self);
604 642 } else {
605 643 switch (how) {
606 644 case SIG_BLOCK:
607 645 case SIG_UNBLOCK:
608 646 case SIG_SETMASK:
609 647 break;
610 648 default:
611 649 return (EINVAL);
612 650 }
613 651
614 652 /*
615 653 * The assignments to self->ul_sigmask must be protected from
616 654 * signals. The nuances of this code are subtle. Be careful.
617 655 */
618 656 block_all_signals(self);
619 657 if (oset != NULL)
620 658 saveset = self->ul_sigmask;
621 659 switch (how) {
622 660 case SIG_BLOCK:
623 661 self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
624 662 self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
625 663 self->ul_sigmask.__sigbits[2] |= set->__sigbits[2];
626 664 self->ul_sigmask.__sigbits[3] |= set->__sigbits[3];
627 665 break;
628 666 case SIG_UNBLOCK:
629 667 self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
630 668 self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
631 669 self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2];
632 670 self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3];
633 671 break;
634 672 case SIG_SETMASK:
635 673 self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
636 674 self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
637 675 self->ul_sigmask.__sigbits[2] = set->__sigbits[2];
638 676 self->ul_sigmask.__sigbits[3] = set->__sigbits[3];
639 677 break;
640 678 }
641 679 delete_reserved_signals(&self->ul_sigmask);
642 680 if (oset != NULL)
643 681 *oset = saveset;
644 682 restore_signals(self);
645 683 }
646 684
647 685 return (0);
648 686 }
649 687
650 688 #pragma weak _pthread_sigmask = pthread_sigmask
651 689 int
652 690 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
653 691 {
654 692 return (thr_sigsetmask(how, set, oset));
655 693 }
656 694
657 695 #pragma weak _sigprocmask = sigprocmask
658 696 int
659 697 sigprocmask(int how, const sigset_t *set, sigset_t *oset)
660 698 {
661 699 int error;
662 700
663 701 /*
664 702 * Guard against children of vfork().
665 703 */
666 704 if (curthread->ul_vfork)
667 705 return (__sigprocmask(how, set, oset));
668 706
669 707 if ((error = thr_sigsetmask(how, set, oset)) != 0) {
670 708 errno = error;
671 709 return (-1);
672 710 }
673 711
674 712 return (0);
675 713 }
676 714
677 715 /*
678 716 * Called at library initialization to set up signal handling.
679 717 * All we really do is initialize the sig_lock rwlocks.
680 718 * All signal handlers are either SIG_DFL or SIG_IGN on exec().
681 719 * However, if any signal handlers were established on alternate
682 720 * link maps before the primary link map has been initialized,
683 721 * then inform the kernel of the new sigacthandler.
684 722 */
685 723 void
686 724 signal_init()
687 725 {
688 726 uberdata_t *udp = curthread->ul_uberdata;
689 727 struct sigaction *sap;
690 728 struct sigaction act;
691 729 rwlock_t *rwlp;
692 730 int sig;
693 731
694 732 for (sig = 0; sig < NSIG; sig++) {
695 733 rwlp = &udp->siguaction[sig].sig_lock;
696 734 rwlp->rwlock_magic = RWL_MAGIC;
697 735 rwlp->mutex.mutex_flag = LOCK_INITED;
698 736 rwlp->mutex.mutex_magic = MUTEX_MAGIC;
699 737 sap = &udp->siguaction[sig].sig_uaction;
700 738 if (sap->sa_sigaction != SIG_DFL &&
701 739 sap->sa_sigaction != SIG_IGN &&
702 740 __sigaction(sig, NULL, &act) == 0 &&
703 741 act.sa_sigaction != SIG_DFL &&
704 742 act.sa_sigaction != SIG_IGN) {
705 743 act = *sap;
706 744 act.sa_flags &= ~SA_NODEFER;
707 745 act.sa_sigaction = udp->sigacthandler;
708 746 act.sa_mask = maskset;
709 747 (void) __sigaction(sig, &act, NULL);
710 748 }
711 749 }
712 750 }
713 751
714 752 /*
715 753 * Common code for cancelling self in _sigcancel() and pthread_cancel().
716 754 * First record the fact that a cancellation is pending.
717 755 * Then, if cancellation is disabled or if we are holding unprotected
718 756 * libc locks, just return to defer the cancellation.
719 757 * Then, if we are at a cancellation point (ul_cancelable) just
720 758 * return and let _canceloff() do the exit.
721 759 * Else exit immediately if async mode is in effect.
722 760 */
723 761 void
724 762 do_sigcancel(void)
725 763 {
726 764 ulwp_t *self = curthread;
727 765
728 766 ASSERT(self->ul_critical == 0);
729 767 ASSERT(self->ul_sigdefer == 0);
730 768 self->ul_cancel_pending = 1;
731 769 if (self->ul_cancel_async &&
732 770 !self->ul_cancel_disabled &&
733 771 self->ul_libc_locks == 0 &&
734 772 !self->ul_cancelable)
735 773 pthread_exit(PTHREAD_CANCELED);
736 774 set_cancel_pending_flag(self, 0);
737 775 }
738 776
739 777 /*
740 778 * Set up the SIGCANCEL handler for threads cancellation,
741 779 * needed only when we have more than one thread,
742 780 * or the SIGAIOCANCEL handler for aio cancellation,
743 781 * called when aio is initialized, in __uaio_init().
744 782 */
745 783 void
746 784 setup_cancelsig(int sig)
747 785 {
748 786 uberdata_t *udp = curthread->ul_uberdata;
749 787 rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
750 788 struct sigaction act;
751 789
752 790 ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
753 791 lrw_rdlock(rwlp);
754 792 act = udp->siguaction[sig].sig_uaction;
755 793 lrw_unlock(rwlp);
756 794 if (act.sa_sigaction == SIG_DFL ||
757 795 act.sa_sigaction == SIG_IGN)
758 796 act.sa_flags = SA_SIGINFO;
759 797 else {
760 798 act.sa_flags |= SA_SIGINFO;
761 799 act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
762 800 }
763 801 act.sa_sigaction = udp->sigacthandler;
764 802 act.sa_mask = maskset;
765 803 (void) __sigaction(sig, &act, NULL);
766 804 }
|
↓ open down ↓ |
175 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX