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