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