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