Home | History | Annotate | Line # | Download | only in kern
sys_sig.c revision 1.26.2.2
      1  1.26.2.1  uebayasi /*	$NetBSD: sys_sig.c,v 1.26.2.2 2010/10/22 07:22:29 uebayasi 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.26.2.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.26.2.2 2010/10/22 07:22:29 uebayasi 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.2        ad int
    217       1.9       dsl sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
    218       1.2        ad {
    219       1.9       dsl 	/* {
    220       1.2        ad 		syscallarg(int)	pid;
    221       1.2        ad 		syscallarg(int)	signum;
    222       1.9       dsl 	} */
    223       1.2        ad 	struct proc	*p;
    224       1.2        ad 	ksiginfo_t	ksi;
    225       1.2        ad 	int signum = SCARG(uap, signum);
    226       1.2        ad 	int error;
    227       1.2        ad 
    228       1.2        ad 	if ((u_int)signum >= NSIG)
    229      1.25     rmind 		return EINVAL;
    230       1.2        ad 	KSI_INIT(&ksi);
    231       1.2        ad 	ksi.ksi_signo = signum;
    232       1.2        ad 	ksi.ksi_code = SI_USER;
    233       1.2        ad 	ksi.ksi_pid = l->l_proc->p_pid;
    234       1.2        ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    235       1.2        ad 	if (SCARG(uap, pid) > 0) {
    236       1.2        ad 		/* kill single process */
    237      1.13        ad 		mutex_enter(proc_lock);
    238  1.26.2.1  uebayasi 		p = proc_find(SCARG(uap, pid));
    239  1.26.2.1  uebayasi 		if (p == NULL) {
    240      1.13        ad 			mutex_exit(proc_lock);
    241      1.25     rmind 			return ESRCH;
    242      1.13        ad 		}
    243      1.14        ad 		mutex_enter(p->p_lock);
    244       1.2        ad 		error = kauth_authorize_process(l->l_cred,
    245      1.11      elad 		    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum),
    246       1.2        ad 		    NULL, NULL);
    247       1.2        ad 		if (!error && signum) {
    248       1.2        ad 			kpsignal2(p, &ksi);
    249       1.2        ad 		}
    250      1.14        ad 		mutex_exit(p->p_lock);
    251      1.13        ad 		mutex_exit(proc_lock);
    252      1.25     rmind 		return error;
    253       1.2        ad 	}
    254       1.2        ad 	switch (SCARG(uap, pid)) {
    255       1.2        ad 	case -1:		/* broadcast signal */
    256      1.25     rmind 		return killpg1(l, &ksi, 0, 1);
    257       1.2        ad 	case 0:			/* signal own process group */
    258      1.25     rmind 		return killpg1(l, &ksi, 0, 0);
    259       1.2        ad 	default:		/* negative explicit process group */
    260      1.25     rmind 		return killpg1(l, &ksi, -SCARG(uap, pid), 0);
    261       1.2        ad 	}
    262       1.2        ad 	/* NOTREACHED */
    263       1.2        ad }
    264       1.2        ad 
    265       1.2        ad int
    266      1.25     rmind sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap,
    267      1.25     rmind     register_t *retval)
    268       1.2        ad {
    269       1.9       dsl 	/* {
    270       1.2        ad 		syscallarg(struct __ucontext *) ucp;
    271       1.9       dsl 	} */
    272       1.2        ad 	struct proc *p = l->l_proc;
    273       1.2        ad 	ucontext_t uc;
    274       1.2        ad 
    275      1.14        ad 	mutex_enter(p->p_lock);
    276       1.2        ad 	getucontext(l, &uc);
    277      1.14        ad 	mutex_exit(p->p_lock);
    278       1.2        ad 
    279      1.25     rmind 	return copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp)));
    280       1.2        ad }
    281       1.2        ad 
    282       1.2        ad int
    283      1.25     rmind sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap,
    284      1.25     rmind     register_t *retval)
    285       1.2        ad {
    286       1.9       dsl 	/* {
    287       1.2        ad 		syscallarg(const ucontext_t *) ucp;
    288       1.9       dsl 	} */
    289       1.2        ad 	struct proc *p = l->l_proc;
    290       1.2        ad 	ucontext_t uc;
    291       1.2        ad 	int error;
    292       1.2        ad 
    293       1.2        ad 	error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
    294       1.2        ad 	if (error)
    295      1.25     rmind 		return error;
    296      1.25     rmind 	if ((uc.uc_flags & _UC_CPU) == 0)
    297      1.25     rmind 		return EINVAL;
    298      1.14        ad 	mutex_enter(p->p_lock);
    299       1.2        ad 	error = setucontext(l, &uc);
    300      1.14        ad 	mutex_exit(p->p_lock);
    301       1.2        ad 	if (error)
    302      1.25     rmind  		return error;
    303       1.2        ad 
    304      1.25     rmind 	return EJUSTRETURN;
    305       1.2        ad }
    306       1.2        ad 
    307       1.2        ad /*
    308       1.2        ad  * sigtimedwait(2) system call, used also for implementation
    309       1.2        ad  * of sigwaitinfo() and sigwait().
    310       1.2        ad  *
    311       1.2        ad  * This only handles single LWP in signal wait. libpthread provides
    312       1.2        ad  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
    313       1.2        ad  */
    314       1.2        ad int
    315      1.21  christos sys_____sigtimedwait50(struct lwp *l,
    316      1.21  christos     const struct sys_____sigtimedwait50_args *uap, register_t *retval)
    317       1.2        ad {
    318       1.2        ad 
    319      1.26     pooka 	return sigtimedwait1(l, uap, retval, copyout, copyin, copyout);
    320       1.2        ad }
    321       1.2        ad 
    322       1.2        ad int
    323       1.2        ad sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
    324       1.2        ad 	struct sigaction *osa, const void *tramp, int vers)
    325       1.2        ad {
    326       1.2        ad 	struct proc *p;
    327       1.2        ad 	struct sigacts *ps;
    328       1.2        ad 	sigset_t tset;
    329       1.2        ad 	int prop, error;
    330       1.2        ad 	ksiginfoq_t kq;
    331      1.20        ad 	static bool v0v1valid;
    332       1.2        ad 
    333       1.2        ad 	if (signum <= 0 || signum >= NSIG)
    334      1.25     rmind 		return EINVAL;
    335       1.2        ad 
    336       1.2        ad 	p = l->l_proc;
    337       1.2        ad 	error = 0;
    338       1.2        ad 	ksiginfo_queue_init(&kq);
    339       1.2        ad 
    340       1.2        ad 	/*
    341       1.2        ad 	 * Trampoline ABI version 0 is reserved for the legacy kernel
    342       1.2        ad 	 * provided on-stack trampoline.  Conversely, if we are using a
    343       1.2        ad 	 * non-0 ABI version, we must have a trampoline.  Only validate the
    344       1.2        ad 	 * vers if a new sigaction was supplied. Emulations use legacy
    345       1.2        ad 	 * kernel trampolines with version 0, alternatively check for that
    346       1.2        ad 	 * too.
    347      1.19        ad 	 *
    348      1.19        ad 	 * If version < 2, we try to autoload the compat module.  Note
    349      1.19        ad 	 * that we interlock with the unload check in compat_modcmd()
    350  1.26.2.2  uebayasi 	 * using kernconfig_lock.  If the autoload fails, we don't try it
    351      1.19        ad 	 * again for this process.
    352      1.19        ad 	 */
    353      1.20        ad 	if (nsa != NULL) {
    354      1.20        ad 		if (__predict_false(vers < 2) &&
    355      1.20        ad 		    (p->p_lflag & PL_SIGCOMPAT) == 0) {
    356  1.26.2.2  uebayasi 			kernconfig_lock();
    357      1.20        ad 			if (sendsig_sigcontext_vec == NULL) {
    358      1.20        ad 				(void)module_autoload("compat",
    359      1.20        ad 				    MODULE_CLASS_ANY);
    360      1.20        ad 			}
    361      1.20        ad 			if (sendsig_sigcontext_vec != NULL) {
    362      1.20        ad 				/*
    363      1.20        ad 				 * We need to remember if the
    364      1.20        ad 				 * sigcontext method may be useable,
    365      1.20        ad 				 * because libc may use it even
    366      1.20        ad 				 * if siginfo is available.
    367      1.20        ad 				 */
    368      1.20        ad 				v0v1valid = true;
    369      1.20        ad 			}
    370      1.20        ad 			mutex_enter(proc_lock);
    371      1.20        ad 			/*
    372      1.20        ad 			 * Prevent unload of compat module while
    373      1.20        ad 			 * this process remains.
    374      1.20        ad 			 */
    375      1.20        ad 			p->p_lflag |= PL_SIGCOMPAT;
    376      1.20        ad 			mutex_exit(proc_lock);
    377  1.26.2.2  uebayasi 			kernconfig_unlock();
    378      1.19        ad 		}
    379      1.19        ad 
    380      1.20        ad 		switch (vers) {
    381      1.20        ad 		case 0:
    382      1.20        ad 			/* sigcontext, kernel supplied trampoline. */
    383      1.20        ad 			if (tramp != NULL || !v0v1valid) {
    384      1.20        ad 				return EINVAL;
    385      1.20        ad 			}
    386      1.20        ad 			break;
    387      1.20        ad 		case 1:
    388      1.20        ad 			/* sigcontext, user supplied trampoline. */
    389      1.20        ad 			if (tramp == NULL || !v0v1valid) {
    390      1.20        ad 				return EINVAL;
    391      1.20        ad 			}
    392      1.20        ad 			break;
    393      1.20        ad 		case 2:
    394      1.20        ad 		case 3:
    395      1.20        ad 			/* siginfo, user supplied trampoline. */
    396      1.20        ad 			if (tramp == NULL) {
    397      1.20        ad 				return EINVAL;
    398      1.20        ad 			}
    399      1.20        ad 			break;
    400      1.20        ad 		default:
    401      1.20        ad 			return EINVAL;
    402      1.20        ad 		}
    403       1.2        ad 	}
    404       1.2        ad 
    405      1.14        ad 	mutex_enter(p->p_lock);
    406       1.2        ad 
    407       1.2        ad 	ps = p->p_sigacts;
    408       1.2        ad 	if (osa)
    409       1.2        ad 		*osa = SIGACTION_PS(ps, signum);
    410       1.2        ad 	if (!nsa)
    411       1.2        ad 		goto out;
    412       1.2        ad 
    413       1.2        ad 	prop = sigprop[signum];
    414       1.2        ad 	if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
    415       1.2        ad 		error = EINVAL;
    416       1.2        ad 		goto out;
    417       1.2        ad 	}
    418       1.2        ad 
    419       1.2        ad 	SIGACTION_PS(ps, signum) = *nsa;
    420       1.2        ad 	ps->sa_sigdesc[signum].sd_tramp = tramp;
    421       1.2        ad 	ps->sa_sigdesc[signum].sd_vers = vers;
    422       1.2        ad 	sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
    423       1.2        ad 
    424       1.2        ad 	if ((prop & SA_NORESET) != 0)
    425       1.2        ad 		SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
    426       1.2        ad 
    427       1.2        ad 	if (signum == SIGCHLD) {
    428       1.2        ad 		if (nsa->sa_flags & SA_NOCLDSTOP)
    429       1.2        ad 			p->p_sflag |= PS_NOCLDSTOP;
    430       1.2        ad 		else
    431       1.2        ad 			p->p_sflag &= ~PS_NOCLDSTOP;
    432       1.2        ad 		if (nsa->sa_flags & SA_NOCLDWAIT) {
    433       1.2        ad 			/*
    434       1.2        ad 			 * Paranoia: since SA_NOCLDWAIT is implemented by
    435       1.2        ad 			 * reparenting the dying child to PID 1 (and trust
    436       1.2        ad 			 * it to reap the zombie), PID 1 itself is forbidden
    437       1.2        ad 			 * to set SA_NOCLDWAIT.
    438       1.2        ad 			 */
    439       1.2        ad 			if (p->p_pid == 1)
    440       1.4     pavel 				p->p_flag &= ~PK_NOCLDWAIT;
    441       1.2        ad 			else
    442       1.4     pavel 				p->p_flag |= PK_NOCLDWAIT;
    443       1.2        ad 		} else
    444       1.4     pavel 			p->p_flag &= ~PK_NOCLDWAIT;
    445       1.2        ad 
    446       1.2        ad 		if (nsa->sa_handler == SIG_IGN) {
    447       1.2        ad 			/*
    448       1.2        ad 			 * Paranoia: same as above.
    449       1.2        ad 			 */
    450       1.2        ad 			if (p->p_pid == 1)
    451       1.4     pavel 				p->p_flag &= ~PK_CLDSIGIGN;
    452       1.2        ad 			else
    453       1.4     pavel 				p->p_flag |= PK_CLDSIGIGN;
    454       1.2        ad 		} else
    455       1.4     pavel 			p->p_flag &= ~PK_CLDSIGIGN;
    456       1.2        ad 	}
    457       1.2        ad 
    458       1.2        ad 	if ((nsa->sa_flags & SA_NODEFER) == 0)
    459       1.2        ad 		sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    460       1.2        ad 	else
    461       1.2        ad 		sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
    462       1.2        ad 
    463       1.2        ad 	/*
    464       1.2        ad 	 * Set bit in p_sigctx.ps_sigignore for signals that are set to
    465       1.2        ad 	 * SIG_IGN, and for signals set to SIG_DFL where the default is to
    466       1.2        ad 	 * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
    467       1.2        ad 	 * we have to restart the process.
    468       1.2        ad 	 */
    469       1.2        ad 	if (nsa->sa_handler == SIG_IGN ||
    470       1.2        ad 	    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
    471       1.2        ad 		/* Never to be seen again. */
    472       1.2        ad 		sigemptyset(&tset);
    473       1.2        ad 		sigaddset(&tset, signum);
    474       1.2        ad 		sigclearall(p, &tset, &kq);
    475       1.2        ad 		if (signum != SIGCONT) {
    476       1.2        ad 			/* Easier in psignal */
    477       1.2        ad 			sigaddset(&p->p_sigctx.ps_sigignore, signum);
    478       1.2        ad 		}
    479       1.2        ad 		sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    480       1.2        ad 	} else {
    481       1.2        ad 		sigdelset(&p->p_sigctx.ps_sigignore, signum);
    482       1.2        ad 		if (nsa->sa_handler == SIG_DFL)
    483       1.2        ad 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
    484       1.2        ad 		else
    485       1.2        ad 			sigaddset(&p->p_sigctx.ps_sigcatch, signum);
    486       1.2        ad 	}
    487       1.2        ad 
    488       1.2        ad 	/*
    489       1.2        ad 	 * Previously held signals may now have become visible.  Ensure that
    490       1.2        ad 	 * we check for them before returning to userspace.
    491       1.2        ad 	 */
    492       1.6        ad 	if (sigispending(l, 0)) {
    493       1.6        ad 		lwp_lock(l);
    494       1.6        ad 		l->l_flag |= LW_PENDSIG;
    495       1.6        ad 		lwp_unlock(l);
    496       1.6        ad 	}
    497      1.25     rmind out:
    498      1.14        ad 	mutex_exit(p->p_lock);
    499       1.2        ad 	ksiginfo_queue_drain(&kq);
    500       1.2        ad 
    501      1.25     rmind 	return error;
    502       1.2        ad }
    503       1.2        ad 
    504       1.2        ad int
    505       1.2        ad sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
    506       1.2        ad {
    507       1.2        ad 	int more;
    508      1.17  wrstuden 	struct proc *p = l->l_proc;
    509      1.17  wrstuden 	sigset_t *mask;
    510      1.17  wrstuden 	mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
    511       1.2        ad 
    512      1.17  wrstuden 	KASSERT(mutex_owned(p->p_lock));
    513       1.2        ad 
    514       1.2        ad 	if (oss)
    515      1.17  wrstuden 		*oss = *mask;
    516       1.2        ad 	if (nss) {
    517       1.2        ad 		switch (how) {
    518       1.2        ad 		case SIG_BLOCK:
    519      1.17  wrstuden 			sigplusset(nss, mask);
    520       1.2        ad 			more = 0;
    521       1.2        ad 			break;
    522       1.2        ad 		case SIG_UNBLOCK:
    523      1.17  wrstuden 			sigminusset(nss, mask);
    524       1.2        ad 			more = 1;
    525       1.2        ad 			break;
    526       1.2        ad 		case SIG_SETMASK:
    527      1.17  wrstuden 			*mask = *nss;
    528       1.2        ad 			more = 1;
    529       1.2        ad 			break;
    530       1.2        ad 		default:
    531       1.2        ad 			return (EINVAL);
    532       1.2        ad 		}
    533      1.17  wrstuden 		sigminusset(&sigcantmask, mask);
    534       1.6        ad 		if (more && sigispending(l, 0)) {
    535       1.2        ad 			/*
    536       1.2        ad 			 * Check for pending signals on return to user.
    537       1.2        ad 			 */
    538       1.2        ad 			lwp_lock(l);
    539       1.4     pavel 			l->l_flag |= LW_PENDSIG;
    540       1.2        ad 			lwp_unlock(l);
    541       1.2        ad 		}
    542       1.2        ad 	}
    543       1.2        ad 
    544      1.25     rmind 	return 0;
    545       1.2        ad }
    546       1.2        ad 
    547       1.2        ad void
    548       1.2        ad sigpending1(struct lwp *l, sigset_t *ss)
    549       1.2        ad {
    550       1.2        ad 	struct proc *p = l->l_proc;
    551       1.2        ad 
    552      1.14        ad 	mutex_enter(p->p_lock);
    553       1.2        ad 	*ss = l->l_sigpend.sp_set;
    554       1.2        ad 	sigplusset(&p->p_sigpend.sp_set, ss);
    555      1.14        ad 	mutex_exit(p->p_lock);
    556       1.2        ad }
    557       1.2        ad 
    558       1.2        ad int
    559       1.2        ad sigsuspend1(struct lwp *l, const sigset_t *ss)
    560       1.2        ad {
    561      1.25     rmind 	struct proc *p = l->l_proc;
    562       1.2        ad 
    563       1.2        ad 	if (ss) {
    564       1.2        ad 		/*
    565      1.12      yamt 		 * When returning from sigsuspend, we want
    566       1.2        ad 		 * the old mask to be restored after the
    567       1.2        ad 		 * signal handler has finished.  Thus, we
    568       1.2        ad 		 * save it here and mark the sigctx structure
    569       1.2        ad 		 * to indicate this.
    570       1.2        ad 		 */
    571      1.14        ad 		mutex_enter(p->p_lock);
    572       1.2        ad 		l->l_sigrestore = 1;
    573       1.2        ad 		l->l_sigoldmask = l->l_sigmask;
    574       1.2        ad 		l->l_sigmask = *ss;
    575       1.2        ad 		sigminusset(&sigcantmask, &l->l_sigmask);
    576       1.2        ad 
    577       1.2        ad 		/* Check for pending signals when sleeping. */
    578       1.6        ad 		if (sigispending(l, 0)) {
    579       1.6        ad 			lwp_lock(l);
    580       1.6        ad 			l->l_flag |= LW_PENDSIG;
    581       1.6        ad 			lwp_unlock(l);
    582       1.6        ad 		}
    583      1.14        ad 		mutex_exit(p->p_lock);
    584       1.2        ad 	}
    585       1.2        ad 
    586       1.5   thorpej 	while (kpause("pause", true, 0, NULL) == 0)
    587       1.2        ad 		;
    588       1.2        ad 
    589       1.2        ad 	/* always return EINTR rather than ERESTART... */
    590      1.25     rmind 	return EINTR;
    591       1.2        ad }
    592       1.2        ad 
    593       1.2        ad int
    594       1.2        ad sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
    595      1.25     rmind     struct sigaltstack *oss)
    596       1.2        ad {
    597       1.2        ad 	struct proc *p = l->l_proc;
    598       1.2        ad 	int error = 0;
    599       1.2        ad 
    600      1.14        ad 	mutex_enter(p->p_lock);
    601       1.2        ad 
    602       1.2        ad 	if (oss)
    603       1.2        ad 		*oss = l->l_sigstk;
    604       1.2        ad 
    605       1.2        ad 	if (nss) {
    606       1.2        ad 		if (nss->ss_flags & ~SS_ALLBITS)
    607       1.2        ad 			error = EINVAL;
    608       1.2        ad 		else if (nss->ss_flags & SS_DISABLE) {
    609       1.2        ad 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    610       1.2        ad 				error = EINVAL;
    611       1.2        ad 		} else if (nss->ss_size < MINSIGSTKSZ)
    612       1.2        ad 			error = ENOMEM;
    613       1.2        ad 
    614       1.2        ad 		if (!error)
    615       1.2        ad 			l->l_sigstk = *nss;
    616       1.2        ad 	}
    617       1.2        ad 
    618      1.14        ad 	mutex_exit(p->p_lock);
    619       1.2        ad 
    620      1.25     rmind 	return error;
    621       1.2        ad }
    622       1.2        ad 
    623       1.2        ad int
    624      1.26     pooka sigtimedwait1(struct lwp *l, const struct sys_____sigtimedwait50_args *uap,
    625      1.25     rmind     register_t *retval, copyout_t storeinf, copyin_t fetchts, copyout_t storets)
    626       1.2        ad {
    627       1.9       dsl 	/* {
    628       1.2        ad 		syscallarg(const sigset_t *) set;
    629       1.2        ad 		syscallarg(siginfo_t *) info;
    630       1.2        ad 		syscallarg(struct timespec *) timeout;
    631       1.9       dsl 	} */
    632       1.2        ad 	struct proc *p = l->l_proc;
    633      1.25     rmind 	int error, signum, timo;
    634       1.2        ad 	struct timespec ts, tsstart, tsnow;
    635      1.24     rmind 	ksiginfo_t ksi;
    636       1.2        ad 
    637       1.2        ad 	/*
    638       1.2        ad 	 * Calculate timeout, if it was specified.
    639       1.2        ad 	 */
    640       1.2        ad 	if (SCARG(uap, timeout)) {
    641      1.25     rmind 		error = (*fetchts)(SCARG(uap, timeout), &ts, sizeof(ts));
    642      1.23  christos 		if (error)
    643      1.23  christos 			return error;
    644       1.2        ad 
    645      1.23  christos 		if ((error = itimespecfix(&ts)) != 0)
    646      1.23  christos 			return error;
    647       1.2        ad 
    648      1.23  christos 		timo = tstohz(&ts);
    649      1.23  christos 		if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec != 0)
    650      1.23  christos 			timo++;
    651       1.2        ad 
    652       1.2        ad 		/*
    653       1.2        ad 		 * Remember current uptime, it would be used in
    654       1.2        ad 		 * ECANCELED/ERESTART case.
    655       1.2        ad 		 */
    656       1.2        ad 		getnanouptime(&tsstart);
    657      1.25     rmind 	} else {
    658      1.25     rmind 		memset(&tsstart, 0, sizeof(tsstart)); /* XXXgcc */
    659      1.25     rmind 		timo = 0;
    660       1.2        ad 	}
    661       1.2        ad 
    662       1.2        ad 	error = copyin(SCARG(uap, set), &l->l_sigwaitset,
    663       1.2        ad 	    sizeof(l->l_sigwaitset));
    664      1.25     rmind 	if (error)
    665      1.25     rmind 		return error;
    666       1.2        ad 
    667       1.2        ad 	/*
    668       1.2        ad 	 * Silently ignore SA_CANTMASK signals. psignal1() would ignore
    669       1.2        ad 	 * SA_CANTMASK signals in waitset, we do this only for the below
    670       1.2        ad 	 * siglist check.
    671       1.2        ad 	 */
    672       1.2        ad 	sigminusset(&sigcantmask, &l->l_sigwaitset);
    673       1.2        ad 
    674      1.14        ad 	mutex_enter(p->p_lock);
    675       1.2        ad 
    676      1.25     rmind 	/* SA processes can have no more than 1 sigwaiter. */
    677      1.17  wrstuden 	if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
    678      1.17  wrstuden 		mutex_exit(p->p_lock);
    679      1.17  wrstuden 		error = EINVAL;
    680      1.17  wrstuden 		goto out;
    681      1.17  wrstuden 	}
    682      1.17  wrstuden 
    683      1.25     rmind 	/* Check for pending signals in the process, if no - then in LWP. */
    684      1.24     rmind 	if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
    685      1.24     rmind 		signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
    686       1.2        ad 
    687       1.2        ad 	if (signum != 0) {
    688      1.25     rmind 		/* If found a pending signal, just copy it out to the user. */
    689      1.14        ad 		mutex_exit(p->p_lock);
    690       1.2        ad 		goto out;
    691       1.2        ad 	}
    692       1.2        ad 
    693       1.2        ad 	/*
    694      1.25     rmind 	 * Set up the sigwait list and wait for signal to arrive.
    695      1.25     rmind 	 * We can either be woken up or time out.
    696       1.2        ad 	 */
    697      1.24     rmind 	l->l_sigwaited = &ksi;
    698       1.2        ad 	LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
    699      1.14        ad 	error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
    700       1.2        ad 
    701       1.2        ad 	/*
    702      1.25     rmind 	 * Need to find out if we woke as a result of _lwp_wakeup() or a
    703       1.2        ad 	 * signal outside our wait set.
    704       1.2        ad 	 */
    705       1.2        ad 	if (l->l_sigwaited != NULL) {
    706       1.2        ad 		if (error == EINTR) {
    707      1.25     rmind 			/* Wakeup via _lwp_wakeup(). */
    708       1.2        ad 			error = ECANCELED;
    709       1.2        ad 		} else if (!error) {
    710      1.25     rmind 			/* Spurious wakeup - arrange for syscall restart. */
    711       1.2        ad 			error = ERESTART;
    712       1.2        ad 		}
    713       1.2        ad 		l->l_sigwaited = NULL;
    714       1.2        ad 		LIST_REMOVE(l, l_sigwaiter);
    715       1.2        ad 	}
    716      1.14        ad 	mutex_exit(p->p_lock);
    717       1.2        ad 
    718       1.2        ad 	/*
    719       1.2        ad 	 * If the sleep was interrupted (either by signal or wakeup), update
    720       1.2        ad 	 * the timeout and copyout new value back.  It would be used when
    721       1.2        ad 	 * the syscall would be restarted or called again.
    722       1.2        ad 	 */
    723       1.2        ad 	if (timo && (error == ERESTART || error == ECANCELED)) {
    724       1.2        ad 		getnanouptime(&tsnow);
    725       1.2        ad 
    726      1.25     rmind 		/* Compute how much time has passed since start. */
    727       1.2        ad 		timespecsub(&tsnow, &tsstart, &tsnow);
    728      1.25     rmind 
    729      1.25     rmind 		/* Substract passed time from timeout. */
    730       1.2        ad 		timespecsub(&ts, &tsnow, &ts);
    731       1.2        ad 
    732       1.2        ad 		if (ts.tv_sec < 0)
    733       1.2        ad 			error = EAGAIN;
    734       1.2        ad 		else {
    735      1.25     rmind 			/* Copy updated timeout to userland. */
    736      1.25     rmind 			error = (*storets)(&ts, SCARG(uap, timeout),
    737       1.2        ad 			    sizeof(ts));
    738       1.2        ad 		}
    739       1.2        ad 	}
    740      1.25     rmind out:
    741       1.2        ad 	/*
    742       1.2        ad 	 * If a signal from the wait set arrived, copy it to userland.
    743       1.2        ad 	 * Copy only the used part of siginfo, the padding part is
    744       1.2        ad 	 * left unchanged (userland is not supposed to touch it anyway).
    745       1.2        ad 	 */
    746  1.26.2.1  uebayasi 	if (error == 0 && SCARG(uap, info)) {
    747      1.25     rmind 		error = (*storeinf)(&ksi.ksi_info, SCARG(uap, info),
    748      1.24     rmind 		    sizeof(ksi.ksi_info));
    749      1.25     rmind 	}
    750  1.26.2.1  uebayasi 	if (error == 0)
    751  1.26.2.1  uebayasi 		*retval = ksi.ksi_info._signo;
    752       1.2        ad 	return error;
    753       1.2        ad }
    754