xref: /netbsd-src/sys/kern/sys_sig.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /*	$NetBSD: sys_sig.c,v 1.54 2021/11/01 05:07:17 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  * (c) UNIX System Laboratories, Inc.
36  * All or some portions of this file are derived from material licensed
37  * to the University of California by American Telephone and Telegraph
38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39  * the permission of UNIX System Laboratories, Inc.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)kern_sig.c	8.14 (Berkeley) 5/14/95
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.54 2021/11/01 05:07:17 thorpej Exp $");
70 
71 #include "opt_dtrace.h"
72 
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/signalvar.h>
76 #include <sys/proc.h>
77 #include <sys/pool.h>
78 #include <sys/syscallargs.h>
79 #include <sys/kauth.h>
80 #include <sys/wait.h>
81 #include <sys/kmem.h>
82 #include <sys/module.h>
83 #include <sys/sdt.h>
84 #include <sys/compat_stub.h>
85 
86 SDT_PROVIDER_DECLARE(proc);
87 SDT_PROBE_DEFINE2(proc, kernel, , signal__clear,
88     "int", 		/* signal */
89     "ksiginfo_t *");	/* signal-info */
90 
91 int
92 sys___sigaction_sigtramp(struct lwp *l,
93     const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
94 {
95 	/* {
96 		syscallarg(int)				signum;
97 		syscallarg(const struct sigaction *)	nsa;
98 		syscallarg(struct sigaction *)		osa;
99 		syscallarg(void *)			tramp;
100 		syscallarg(int)				vers;
101 	} */
102 	struct sigaction nsa, osa;
103 	int error;
104 
105 	if (SCARG(uap, nsa)) {
106 		error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
107 		if (error)
108 			return (error);
109 	}
110 	error = sigaction1(l, SCARG(uap, signum),
111 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
112 	    SCARG(uap, tramp), SCARG(uap, vers));
113 	if (error)
114 		return (error);
115 	if (SCARG(uap, osa)) {
116 		error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
117 		if (error)
118 			return (error);
119 	}
120 	return 0;
121 }
122 
123 /*
124  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
125  * return old mask as return value; the library stub does the rest.
126  */
127 int
128 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap,
129     register_t *retval)
130 {
131 	/* {
132 		syscallarg(int)			how;
133 		syscallarg(const sigset_t *)	set;
134 		syscallarg(sigset_t *)		oset;
135 	} */
136 	struct proc	*p = l->l_proc;
137 	sigset_t	nss, oss;
138 	int		error;
139 
140 	if (SCARG(uap, set)) {
141 		error = copyin(SCARG(uap, set), &nss, sizeof(nss));
142 		if (error)
143 			return error;
144 	}
145 	mutex_enter(p->p_lock);
146 	error = sigprocmask1(l, SCARG(uap, how),
147 	    SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
148 	mutex_exit(p->p_lock);
149 	if (error)
150 		return error;
151 	if (SCARG(uap, oset)) {
152 		error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
153 		if (error)
154 			return error;
155 	}
156 	return 0;
157 }
158 
159 int
160 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap,
161     register_t *retval)
162 {
163 	/* {
164 		syscallarg(sigset_t *)	set;
165 	} */
166 	sigset_t ss;
167 
168 	sigpending1(l, &ss);
169 	return copyout(&ss, SCARG(uap, set), sizeof(ss));
170 }
171 
172 /*
173  * Suspend process until signal, providing mask to be set in the meantime.
174  * Note nonstandard calling convention: libc stub passes mask, not pointer,
175  * to save a copyin.
176  */
177 int
178 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap,
179     register_t *retval)
180 {
181 	/* {
182 		syscallarg(const sigset_t *)	set;
183 	} */
184 	sigset_t	ss;
185 	int		error;
186 
187 	if (SCARG(uap, set)) {
188 		error = copyin(SCARG(uap, set), &ss, sizeof(ss));
189 		if (error)
190 			return error;
191 	}
192 	return sigsuspend1(l, SCARG(uap, set) ? &ss : 0);
193 }
194 
195 int
196 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap,
197     register_t *retval)
198 {
199 	/* {
200 		syscallarg(const struct sigaltstack *)	nss;
201 		syscallarg(struct sigaltstack *)	oss;
202 	} */
203 	stack_t	nss, oss;
204 	int	error;
205 
206 	if (SCARG(uap, nss)) {
207 		error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
208 		if (error)
209 			return error;
210 	}
211 	error = sigaltstack1(l,
212 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
213 	if (error)
214 		return error;
215 	if (SCARG(uap, oss)) {
216 		error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
217 		if (error)
218 			return error;
219 	}
220 	return 0;
221 }
222 
223 int
224 kill1(struct lwp *l, pid_t pid, ksiginfo_t *ksi, register_t *retval)
225 {
226 	int error;
227 	struct proc *p;
228 
229 	if ((u_int)ksi->ksi_signo >= NSIG)
230 		return EINVAL;
231 
232 	if (pid != l->l_proc->p_pid) {
233 		if (ksi->ksi_pid != l->l_proc->p_pid)
234 			return EPERM;
235 
236 		if (ksi->ksi_uid != kauth_cred_geteuid(l->l_cred))
237 			return EPERM;
238 
239 		switch (ksi->ksi_code) {
240 		case SI_USER:
241 		case SI_QUEUE:
242 			break;
243 		default:
244 			return EPERM;
245 		}
246 	}
247 
248 	if (pid > 0) {
249 		/* kill single process */
250 		mutex_enter(&proc_lock);
251 		p = proc_find_raw(pid);
252 		if (p == NULL || (p->p_stat != SACTIVE && p->p_stat != SSTOP)) {
253 			mutex_exit(&proc_lock);
254 			/* IEEE Std 1003.1-2001: return success for zombies */
255 			return p ? 0 : ESRCH;
256 		}
257 		mutex_enter(p->p_lock);
258 		error = kauth_authorize_process(l->l_cred,
259 		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(ksi->ksi_signo),
260 		    NULL, NULL);
261 		if (!error && ksi->ksi_signo) {
262 			error = kpsignal2(p, ksi);
263 		}
264 		mutex_exit(p->p_lock);
265 		mutex_exit(&proc_lock);
266 		return error;
267 	}
268 
269 	switch (pid) {
270 	case -1:		/* broadcast signal */
271 		return killpg1(l, ksi, 0, 1);
272 	case 0:			/* signal own process group */
273 		return killpg1(l, ksi, 0, 0);
274 	default:		/* negative explicit process group */
275 		return killpg1(l, ksi, -pid, 0);
276 	}
277 	/* NOTREACHED */
278 }
279 
280 int
281 sys_sigqueueinfo(struct lwp *l, const struct sys_sigqueueinfo_args *uap,
282     register_t *retval)
283 {
284 	/* {
285 		syscallarg(pid_t int)	pid;
286 		syscallarg(const siginfo_t *)	info;
287 	} */
288 	ksiginfo_t	ksi;
289 	int error;
290 
291 	KSI_INIT(&ksi);
292 
293 	if ((error = copyin(&SCARG(uap, info)->_info, &ksi.ksi_info,
294 	    sizeof(ksi.ksi_info))) != 0)
295 		return error;
296 
297 	return kill1(l, SCARG(uap, pid), &ksi, retval);
298 }
299 
300 int
301 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
302 {
303 	/* {
304 		syscallarg(pid_t)	pid;
305 		syscallarg(int)	signum;
306 	} */
307 	ksiginfo_t	ksi;
308 
309 	KSI_INIT(&ksi);
310 
311 	ksi.ksi_signo = SCARG(uap, signum);
312 	ksi.ksi_code = SI_USER;
313 	ksi.ksi_pid = l->l_proc->p_pid;
314 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
315 
316 	return kill1(l, SCARG(uap, pid), &ksi, retval);
317 }
318 
319 int
320 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap,
321     register_t *retval)
322 {
323 	/* {
324 		syscallarg(struct __ucontext *) ucp;
325 	} */
326 	struct proc *p = l->l_proc;
327 	ucontext_t uc;
328 
329 	memset(&uc, 0, sizeof(uc));
330 
331 	mutex_enter(p->p_lock);
332 	getucontext(l, &uc);
333 	mutex_exit(p->p_lock);
334 
335 	return copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)));
336 }
337 
338 int
339 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap,
340     register_t *retval)
341 {
342 	/* {
343 		syscallarg(const ucontext_t *) ucp;
344 	} */
345 	struct proc *p = l->l_proc;
346 	ucontext_t uc;
347 	int error;
348 
349 	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
350 	if (error)
351 		return error;
352 	if ((uc.uc_flags & _UC_CPU) == 0)
353 		return EINVAL;
354 	mutex_enter(p->p_lock);
355 	error = setucontext(l, &uc);
356 	mutex_exit(p->p_lock);
357 	if (error)
358  		return error;
359 
360 	return EJUSTRETURN;
361 }
362 
363 /*
364  * sigtimedwait(2) system call, used also for implementation
365  * of sigwaitinfo() and sigwait().
366  *
367  * This only handles single LWP in signal wait. libpthread provides
368  * its own sigtimedwait() wrapper to DTRT WRT individual threads.
369  */
370 int
371 sys_____sigtimedwait50(struct lwp *l,
372     const struct sys_____sigtimedwait50_args *uap, register_t *retval)
373 {
374 
375 	return sigtimedwait1(l, uap, retval, copyin, copyout, copyin, copyout);
376 }
377 
378 int
379 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
380 	struct sigaction *osa, const void *tramp, int vers)
381 {
382 	struct proc *p;
383 	struct sigacts *ps;
384 	sigset_t tset;
385 	int prop, error;
386 	ksiginfoq_t kq;
387 	static bool v0v1valid;
388 
389 	if (signum <= 0 || signum >= NSIG)
390 		return EINVAL;
391 
392 	p = l->l_proc;
393 	error = 0;
394 	ksiginfo_queue_init(&kq);
395 
396 	/*
397 	 * Trampoline ABI version __SIGTRAMP_SIGCODE_VERSION (0) is reserved
398 	 * for the legacy kernel provided on-stack trampoline.  Conversely,
399 	 * if we are using a non-0 ABI version, we must have a trampoline.
400 	 * Only validate the vers if a new sigaction was supplied and there
401 	 * was an actual handler specified (not SIG_IGN or SIG_DFL), which
402 	 * don't require a trampoline. Emulations use legacy kernel
403 	 * trampolines with version 0, alternatively check for that too.
404 	 *
405 	 * If version < __SIGTRAMP_SIGINFO_VERSION_MIN (usually 2), we try
406 	 * to autoload the compat module.  Note that we interlock with the
407 	 * unload check in compat_modcmd() using kernconfig_lock.  If the
408 	 * autoload fails, we don't try it again for this process.
409 	 */
410 	if (nsa != NULL && nsa->sa_handler != SIG_IGN
411 	    && nsa->sa_handler != SIG_DFL) {
412 		if (__predict_false(vers < __SIGTRAMP_SIGINFO_VERSION_MIN)) {
413 			if (vers == __SIGTRAMP_SIGCODE_VERSION &&
414 			    p->p_sigctx.ps_sigcode != NULL) {
415 				/*
416 				 * if sigcode is used for this emulation,
417 				 * version 0 is allowed.
418 				 */
419 			}
420 #ifdef __HAVE_STRUCT_SIGCONTEXT
421 			else if (p->p_flag & PK_32) {
422 				v0v1valid = true;
423 			} else if ((p->p_lflag & PL_SIGCOMPAT) == 0) {
424 				kernconfig_lock();
425 				(void)module_autoload("compat_16",
426 				    MODULE_CLASS_ANY);
427 				if (sendsig_sigcontext_16_hook.hooked) {
428 					/*
429 					 * We need to remember if the
430 					 * sigcontext method may be useable,
431 					 * because libc may use it even
432 					 * if siginfo is available.
433 					 */
434 					v0v1valid = true;
435 				}
436 				mutex_enter(&proc_lock);
437 				/*
438 				 * Prevent unload of compat module while
439 				 * this process remains.
440 				 */
441 				p->p_lflag |= PL_SIGCOMPAT;
442 				mutex_exit(&proc_lock);
443 				kernconfig_unlock();
444 			}
445 #endif /* __HAVE_STRUCT_SIGCONTEXT */
446 		}
447 
448 		switch (vers) {
449 		case __SIGTRAMP_SIGCODE_VERSION:
450 			/* kernel supplied trampoline. */
451 			if (tramp != NULL ||
452 			    (p->p_sigctx.ps_sigcode == NULL && !v0v1valid)) {
453 				return EINVAL;
454 			}
455 			break;
456 #ifdef __HAVE_STRUCT_SIGCONTEXT
457 		case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
458 		     __SIGTRAMP_SIGCONTEXT_VERSION_MAX:
459 			/* sigcontext, user supplied trampoline. */
460 			if (tramp == NULL || !v0v1valid) {
461 				return EINVAL;
462 			}
463 			break;
464 #endif /* __HAVE_STRUCT_SIGCONTEXT */
465 		case __SIGTRAMP_SIGINFO_VERSION_MIN ...
466 		     __SIGTRAMP_SIGINFO_VERSION_MAX:
467 			/* siginfo, user supplied trampoline. */
468 			if (tramp == NULL) {
469 				return EINVAL;
470 			}
471 			break;
472 		default:
473 			/* Invalid trampoline version. */
474 			return EINVAL;
475 		}
476 	}
477 
478 	mutex_enter(p->p_lock);
479 
480 	ps = p->p_sigacts;
481 	if (osa)
482 		sigaction_copy(osa, &SIGACTION_PS(ps, signum));
483 	if (!nsa)
484 		goto out;
485 
486 	prop = sigprop[signum];
487 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
488 		error = EINVAL;
489 		goto out;
490 	}
491 
492 	sigaction_copy(&SIGACTION_PS(ps, signum), nsa);
493 	ps->sa_sigdesc[signum].sd_tramp = tramp;
494 	ps->sa_sigdesc[signum].sd_vers = vers;
495 	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
496 
497 	if ((prop & SA_NORESET) != 0)
498 		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
499 
500 	if (signum == SIGCHLD) {
501 		if (nsa->sa_flags & SA_NOCLDSTOP)
502 			p->p_sflag |= PS_NOCLDSTOP;
503 		else
504 			p->p_sflag &= ~PS_NOCLDSTOP;
505 		if (nsa->sa_flags & SA_NOCLDWAIT) {
506 			/*
507 			 * Paranoia: since SA_NOCLDWAIT is implemented by
508 			 * reparenting the dying child to PID 1 (and trust
509 			 * it to reap the zombie), PID 1 itself is forbidden
510 			 * to set SA_NOCLDWAIT.
511 			 */
512 			if (p->p_pid == 1)
513 				p->p_flag &= ~PK_NOCLDWAIT;
514 			else
515 				p->p_flag |= PK_NOCLDWAIT;
516 		} else
517 			p->p_flag &= ~PK_NOCLDWAIT;
518 
519 		if (nsa->sa_handler == SIG_IGN) {
520 			/*
521 			 * Paranoia: same as above.
522 			 */
523 			if (p->p_pid == 1)
524 				p->p_flag &= ~PK_CLDSIGIGN;
525 			else
526 				p->p_flag |= PK_CLDSIGIGN;
527 		} else
528 			p->p_flag &= ~PK_CLDSIGIGN;
529 	}
530 
531 	if ((nsa->sa_flags & SA_NODEFER) == 0)
532 		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
533 	else
534 		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
535 
536 	/*
537 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
538 	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
539 	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
540 	 * we have to restart the process.
541 	 */
542 	if (nsa->sa_handler == SIG_IGN ||
543 	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
544 		/* Never to be seen again. */
545 		sigemptyset(&tset);
546 		sigaddset(&tset, signum);
547 		sigclearall(p, &tset, &kq);
548 		if (signum != SIGCONT) {
549 			/* Easier in psignal */
550 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
551 		}
552 		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
553 	} else {
554 		sigdelset(&p->p_sigctx.ps_sigignore, signum);
555 		if (nsa->sa_handler == SIG_DFL)
556 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
557 		else
558 			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
559 	}
560 
561 	/*
562 	 * Previously held signals may now have become visible.  Ensure that
563 	 * we check for them before returning to userspace.
564 	 */
565 	if (sigispending(l, 0)) {
566 		lwp_lock(l);
567 		l->l_flag |= LW_PENDSIG;
568 		lwp_unlock(l);
569 	}
570 out:
571 	mutex_exit(p->p_lock);
572 	ksiginfo_queue_drain(&kq);
573 
574 	return error;
575 }
576 
577 int
578 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
579 {
580 	sigset_t *mask = &l->l_sigmask;
581 	bool more;
582 
583 	KASSERT(mutex_owned(l->l_proc->p_lock));
584 
585 	if (oss) {
586 		*oss = *mask;
587 	}
588 
589 	if (nss == NULL) {
590 		return 0;
591 	}
592 
593 	switch (how) {
594 	case SIG_BLOCK:
595 		sigplusset(nss, mask);
596 		more = false;
597 		break;
598 	case SIG_UNBLOCK:
599 		sigminusset(nss, mask);
600 		more = true;
601 		break;
602 	case SIG_SETMASK:
603 		*mask = *nss;
604 		more = true;
605 		break;
606 	default:
607 		return EINVAL;
608 	}
609 	sigminusset(&sigcantmask, mask);
610 	if (more && sigispending(l, 0)) {
611 		/*
612 		 * Check for pending signals on return to user.
613 		 */
614 		lwp_lock(l);
615 		l->l_flag |= LW_PENDSIG;
616 		lwp_unlock(l);
617 	}
618 	return 0;
619 }
620 
621 void
622 sigpending1(struct lwp *l, sigset_t *ss)
623 {
624 	struct proc *p = l->l_proc;
625 
626 	mutex_enter(p->p_lock);
627 	*ss = l->l_sigpend.sp_set;
628 	sigplusset(&p->p_sigpend.sp_set, ss);
629 	mutex_exit(p->p_lock);
630 }
631 
632 void
633 sigsuspendsetup(struct lwp *l, const sigset_t *ss)
634 {
635 	struct proc *p = l->l_proc;
636 
637 	/*
638 	 * When returning from sigsuspend/pselect/pollts, we want
639 	 * the old mask to be restored after the
640 	 * signal handler has finished.  Thus, we
641 	 * save it here and mark the sigctx structure
642 	 * to indicate this.
643 	 */
644 	mutex_enter(p->p_lock);
645 	l->l_sigrestore = 1;
646 	l->l_sigoldmask = l->l_sigmask;
647 	l->l_sigmask = *ss;
648 	sigminusset(&sigcantmask, &l->l_sigmask);
649 
650 	/* Check for pending signals when sleeping. */
651 	if (sigispending(l, 0)) {
652 		lwp_lock(l);
653 		l->l_flag |= LW_PENDSIG;
654 		lwp_unlock(l);
655 	}
656 	mutex_exit(p->p_lock);
657 }
658 
659 void
660 sigsuspendteardown(struct lwp *l)
661 {
662 	struct proc *p = l->l_proc;
663 
664 	mutex_enter(p->p_lock);
665 	/* Check for pending signals when sleeping. */
666 	if (l->l_sigrestore) {
667 		if (sigispending(l, 0)) {
668 			lwp_lock(l);
669 			l->l_flag |= LW_PENDSIG;
670 			lwp_unlock(l);
671 		} else {
672 			l->l_sigrestore = 0;
673 			l->l_sigmask = l->l_sigoldmask;
674 		}
675 	}
676 	mutex_exit(p->p_lock);
677 }
678 
679 int
680 sigsuspend1(struct lwp *l, const sigset_t *ss)
681 {
682 
683 	if (ss)
684 		sigsuspendsetup(l, ss);
685 
686 	while (kpause("pause", true, 0, NULL) == 0)
687 		;
688 
689 	/* always return EINTR rather than ERESTART... */
690 	return EINTR;
691 }
692 
693 int
694 sigaltstack1(struct lwp *l, const stack_t *nss, stack_t *oss)
695 {
696 	struct proc *p = l->l_proc;
697 	int error = 0;
698 
699 	mutex_enter(p->p_lock);
700 
701 	if (oss)
702 		*oss = l->l_sigstk;
703 
704 	if (nss) {
705 		if (nss->ss_flags & ~SS_ALLBITS)
706 			error = EINVAL;
707 		else if (nss->ss_flags & SS_DISABLE) {
708 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
709 				error = EINVAL;
710 		} else if (nss->ss_size < MINSIGSTKSZ)
711 			error = ENOMEM;
712 
713 		if (!error)
714 			l->l_sigstk = *nss;
715 	}
716 
717 	mutex_exit(p->p_lock);
718 
719 	return error;
720 }
721 
722 int
723 sigtimedwait1(struct lwp *l, const struct sys_____sigtimedwait50_args *uap,
724     register_t *retval, copyin_t fetchss, copyout_t storeinf, copyin_t fetchts,
725     copyout_t storets)
726 {
727 	/* {
728 		syscallarg(const sigset_t *) set;
729 		syscallarg(siginfo_t *) info;
730 		syscallarg(struct timespec *) timeout;
731 	} */
732 	struct proc *p = l->l_proc;
733 	int error, signum, timo;
734 	struct timespec ts, tsstart, tsnow;
735 	ksiginfo_t ksi;
736 
737 	/*
738 	 * Calculate timeout, if it was specified.
739 	 *
740 	 * NULL pointer means an infinite timeout.
741 	 * {.tv_sec = 0, .tv_nsec = 0} means do not block.
742 	 */
743 	if (SCARG(uap, timeout)) {
744 		error = (*fetchts)(SCARG(uap, timeout), &ts, sizeof(ts));
745 		if (error)
746 			return error;
747 
748 		if ((error = itimespecfix(&ts)) != 0)
749 			return error;
750 
751 		timo = tstohz(&ts);
752 		if (timo == 0) {
753 			if (ts.tv_sec == 0 && ts.tv_nsec == 0)
754 				timo = -1; /* do not block */
755 			else
756 				timo = 1; /* the shortest possible timeout */
757 		}
758 
759 		/*
760 		 * Remember current uptime, it would be used in
761 		 * ECANCELED/ERESTART case.
762 		 */
763 		getnanouptime(&tsstart);
764 	} else {
765 		memset(&tsstart, 0, sizeof(tsstart)); /* XXXgcc */
766 		timo = 0; /* infinite timeout */
767 	}
768 
769 	error = (*fetchss)(SCARG(uap, set), &l->l_sigwaitset,
770 	    sizeof(l->l_sigwaitset));
771 	if (error)
772 		return error;
773 
774 	/*
775 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
776 	 * SA_CANTMASK signals in waitset, we do this only for the below
777 	 * siglist check.
778 	 */
779 	sigminusset(&sigcantmask, &l->l_sigwaitset);
780 
781 	memset(&ksi.ksi_info, 0, sizeof(ksi.ksi_info));
782 
783 	mutex_enter(p->p_lock);
784 
785 	/* Check for pending signals in the process, if no - then in LWP. */
786 	if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
787 		signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
788 
789 	if (signum != 0) {
790 		/* If found a pending signal, just copy it out to the user. */
791 		mutex_exit(p->p_lock);
792 		goto out;
793 	}
794 
795 	if (timo < 0) {
796 		/* If not allowed to block, return an error */
797 		mutex_exit(p->p_lock);
798 		return EAGAIN;
799 	}
800 
801 	/*
802 	 * Set up the sigwait list and wait for signal to arrive.
803 	 * We can either be woken up or time out.
804 	 */
805 	l->l_sigwaited = &ksi;
806 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
807 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
808 
809 	/*
810 	 * Need to find out if we woke as a result of _lwp_wakeup() or a
811 	 * signal outside our wait set.
812 	 */
813 	if (l->l_sigwaited != NULL) {
814 		if (error == EINTR) {
815 			/* Wakeup via _lwp_wakeup(). */
816 			error = ECANCELED;
817 		} else if (!error) {
818 			/* Spurious wakeup - arrange for syscall restart. */
819 			error = ERESTART;
820 		}
821 		l->l_sigwaited = NULL;
822 		LIST_REMOVE(l, l_sigwaiter);
823 	}
824 	mutex_exit(p->p_lock);
825 
826 	/*
827 	 * If the sleep was interrupted (either by signal or wakeup), update
828 	 * the timeout and copyout new value back.  It would be used when
829 	 * the syscall would be restarted or called again.
830 	 */
831 	if (timo && (error == ERESTART || error == ECANCELED)) {
832 		getnanouptime(&tsnow);
833 
834 		/* Compute how much time has passed since start. */
835 		timespecsub(&tsnow, &tsstart, &tsnow);
836 
837 		/* Substract passed time from timeout. */
838 		timespecsub(&ts, &tsnow, &ts);
839 
840 		if (ts.tv_sec < 0)
841 			error = EAGAIN;
842 		else {
843 			/* Copy updated timeout to userland. */
844 			error = (*storets)(&ts, SCARG(uap, timeout),
845 			    sizeof(ts));
846 		}
847 	}
848 out:
849 	/*
850 	 * If a signal from the wait set arrived, copy it to userland.
851 	 * Copy only the used part of siginfo, the padding part is
852 	 * left unchanged (userland is not supposed to touch it anyway).
853 	 */
854 	if (error == 0 && SCARG(uap, info)) {
855 		error = (*storeinf)(&ksi.ksi_info, SCARG(uap, info),
856 		    sizeof(ksi.ksi_info));
857 	}
858 	if (error == 0) {
859 		*retval = ksi.ksi_info._signo;
860 		SDT_PROBE(proc, kernel, , signal__clear, *retval,
861 		    &ksi, 0, 0, 0);
862 	}
863 	return error;
864 }
865