Home | History | Annotate | Line # | Download | only in common
linux_signal.c revision 1.72
      1  1.72       wiz /*	$NetBSD: linux_signal.c,v 1.72 2011/06/30 20:09:39 wiz Exp $	*/
      2  1.53        ad 
      3  1.16      fvdl /*-
      4  1.16      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5   1.1      fvdl  * All rights reserved.
      6   1.1      fvdl  *
      7  1.16      fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8  1.16      fvdl  * by Frank van der Linden and Eric Haszlakiewicz.
      9  1.16      fvdl  *
     10   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     11   1.1      fvdl  * modification, are permitted provided that the following conditions
     12   1.1      fvdl  * are met:
     13   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     14   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     15   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     18   1.1      fvdl  *
     19  1.16      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.16      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.16      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.16      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.16      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.16      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.16      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.16      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.16      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.16      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.16      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     30  1.16      fvdl  */
     31  1.16      fvdl /*
     32   1.1      fvdl  * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp
     33   1.1      fvdl  */
     34   1.1      fvdl 
     35  1.14       erh /*
     36  1.14       erh  *   Functions in multiarch:
     37  1.14       erh  *	linux_sys_signal	: linux_sig_notalpha.c
     38  1.14       erh  *	linux_sys_siggetmask	: linux_sig_notalpha.c
     39  1.14       erh  *	linux_sys_sigsetmask	: linux_sig_notalpha.c
     40  1.14       erh  *	linux_sys_pause		: linux_sig_notalpha.c
     41  1.14       erh  *	linux_sys_sigaction	: linux_sigaction.c
     42  1.14       erh  *
     43  1.14       erh  */
     44  1.14       erh 
     45  1.14       erh /*
     46  1.14       erh  *   Unimplemented:
     47  1.14       erh  *	linux_sys_rt_sigtimedwait	: sigsuspend w/timeout.
     48  1.14       erh  */
     49  1.32     lukem 
     50  1.32     lukem #include <sys/cdefs.h>
     51  1.72       wiz __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.72 2011/06/30 20:09:39 wiz Exp $");
     52  1.21  drochner 
     53  1.21  drochner #define COMPAT_LINUX 1
     54  1.14       erh 
     55   1.1      fvdl #include <sys/param.h>
     56   1.1      fvdl #include <sys/systm.h>
     57   1.1      fvdl #include <sys/namei.h>
     58   1.1      fvdl #include <sys/proc.h>
     59   1.1      fvdl #include <sys/filedesc.h>
     60   1.1      fvdl #include <sys/ioctl.h>
     61   1.1      fvdl #include <sys/mount.h>
     62   1.1      fvdl #include <sys/kernel.h>
     63   1.1      fvdl #include <sys/signal.h>
     64   1.1      fvdl #include <sys/signalvar.h>
     65   1.1      fvdl #include <sys/malloc.h>
     66  1.68     njoly #include <sys/wait.h>
     67   1.1      fvdl 
     68   1.1      fvdl #include <sys/syscallargs.h>
     69   1.1      fvdl 
     70  1.15  christos #include <compat/linux/common/linux_types.h>
     71  1.15  christos #include <compat/linux/common/linux_signal.h>
     72  1.71       chs #include <compat/linux/common/linux_emuldata.h>
     73  1.17       erh #include <compat/linux/common/linux_siginfo.h>
     74  1.45      fvdl #include <compat/linux/common/linux_sigevent.h>
     75  1.15  christos #include <compat/linux/common/linux_util.h>
     76  1.55     njoly #include <compat/linux/common/linux_ipc.h>
     77  1.55     njoly #include <compat/linux/common/linux_sem.h>
     78  1.15  christos 
     79   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     80   1.1      fvdl 
     81  1.14       erh /* Locally used defines (in bsd<->linux conversion functions): */
     82  1.11     perry #define	linux_sigemptyset(s)	memset((s), 0, sizeof(*(s)))
     83  1.20     itohy #define	linux_sigismember(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     84  1.63     njoly 					& (1L << ((n) - 1) % LINUX__NSIG_BPW))
     85  1.20     itohy #define	linux_sigaddset(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     86  1.63     njoly 					|= (1L << ((n) - 1) % LINUX__NSIG_BPW))
     87   1.6   mycroft 
     88  1.36  christos #ifdef DEBUG_LINUX
     89  1.36  christos #define DPRINTF(a)	uprintf a
     90  1.36  christos #else
     91  1.36  christos #define DPRINTF(a)
     92  1.36  christos #endif
     93  1.36  christos 
     94  1.37  christos extern const int native_to_linux_signo[];
     95  1.37  christos extern const int linux_to_native_signo[];
     96   1.6   mycroft 
     97  1.14       erh /*
     98  1.20     itohy  * Convert between Linux and BSD signal sets.
     99  1.14       erh  */
    100  1.20     itohy #if LINUX__NSIG_WORDS > 1
    101   1.6   mycroft void
    102  1.57       dsl linux_old_extra_to_native_sigset(sigset_t *bss, const linux_old_sigset_t *lss, const unsigned long *extra)
    103  1.20     itohy {
    104  1.20     itohy 	linux_sigset_t lsnew;
    105  1.20     itohy 
    106  1.20     itohy 	/* convert old sigset to new sigset */
    107  1.20     itohy 	linux_sigemptyset(&lsnew);
    108  1.20     itohy 	lsnew.sig[0] = *lss;
    109  1.20     itohy 	if (extra)
    110  1.33  christos 		memcpy(&lsnew.sig[1], extra,
    111  1.33  christos 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    112  1.20     itohy 
    113  1.33  christos 	linux_to_native_sigset(bss, &lsnew);
    114  1.20     itohy }
    115  1.20     itohy 
    116  1.20     itohy void
    117  1.57       dsl native_to_linux_old_extra_sigset(linux_old_sigset_t *lss, unsigned long *extra, const sigset_t *bss)
    118  1.20     itohy {
    119  1.20     itohy 	linux_sigset_t lsnew;
    120  1.20     itohy 
    121  1.33  christos 	native_to_linux_sigset(&lsnew, bss);
    122  1.20     itohy 
    123  1.20     itohy 	/* convert new sigset to old sigset */
    124  1.20     itohy 	*lss = lsnew.sig[0];
    125  1.20     itohy 	if (extra)
    126  1.33  christos 		memcpy(extra, &lsnew.sig[1],
    127  1.33  christos 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    128  1.20     itohy }
    129  1.43      manu #endif /* LINUX__NSIG_WORDS > 1 */
    130  1.20     itohy 
    131  1.20     itohy void
    132  1.57       dsl linux_to_native_sigset(sigset_t *bss, const linux_sigset_t *lss)
    133   1.1      fvdl {
    134   1.1      fvdl 	int i, newsig;
    135   1.1      fvdl 
    136   1.6   mycroft 	sigemptyset(bss);
    137  1.20     itohy 	for (i = 1; i < LINUX__NSIG; i++) {
    138   1.6   mycroft 		if (linux_sigismember(lss, i)) {
    139  1.37  christos 			newsig = linux_to_native_signo[i];
    140   1.1      fvdl 			if (newsig)
    141   1.6   mycroft 				sigaddset(bss, newsig);
    142   1.1      fvdl 		}
    143   1.1      fvdl 	}
    144   1.1      fvdl }
    145   1.1      fvdl 
    146   1.1      fvdl void
    147  1.57       dsl native_to_linux_sigset(linux_sigset_t *lss, const sigset_t *bss)
    148   1.1      fvdl {
    149   1.1      fvdl 	int i, newsig;
    150  1.20     itohy 
    151   1.6   mycroft 	linux_sigemptyset(lss);
    152   1.6   mycroft 	for (i = 1; i < NSIG; i++) {
    153   1.6   mycroft 		if (sigismember(bss, i)) {
    154  1.37  christos 			newsig = native_to_linux_signo[i];
    155   1.1      fvdl 			if (newsig)
    156   1.6   mycroft 				linux_sigaddset(lss, newsig);
    157   1.1      fvdl 		}
    158   1.1      fvdl 	}
    159   1.1      fvdl }
    160   1.1      fvdl 
    161  1.34  christos unsigned int
    162  1.57       dsl native_to_linux_sigflags(const int bsf)
    163  1.33  christos {
    164  1.34  christos 	unsigned int lsf = 0;
    165  1.34  christos 	if ((bsf & SA_NOCLDSTOP) != 0)
    166  1.34  christos 		lsf |= LINUX_SA_NOCLDSTOP;
    167  1.34  christos 	if ((bsf & SA_NOCLDWAIT) != 0)
    168  1.34  christos 		lsf |= LINUX_SA_NOCLDWAIT;
    169  1.34  christos 	if ((bsf & SA_ONSTACK) != 0)
    170  1.34  christos 		lsf |= LINUX_SA_ONSTACK;
    171  1.34  christos 	if ((bsf & SA_RESTART) != 0)
    172  1.34  christos 		lsf |= LINUX_SA_RESTART;
    173  1.34  christos 	if ((bsf & SA_NODEFER) != 0)
    174  1.34  christos 		lsf |= LINUX_SA_NOMASK;
    175  1.34  christos 	if ((bsf & SA_RESETHAND) != 0)
    176  1.34  christos 		lsf |= LINUX_SA_ONESHOT;
    177  1.34  christos 	if ((bsf & SA_SIGINFO) != 0)
    178  1.34  christos 		lsf |= LINUX_SA_SIGINFO;
    179  1.34  christos 	return lsf;
    180  1.33  christos }
    181  1.33  christos 
    182  1.34  christos int
    183  1.57       dsl linux_to_native_sigflags(const unsigned long lsf)
    184  1.33  christos {
    185  1.34  christos 	int bsf = 0;
    186  1.34  christos 	if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
    187  1.34  christos 		bsf |= SA_NOCLDSTOP;
    188  1.34  christos 	if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
    189  1.34  christos 		bsf |= SA_NOCLDWAIT;
    190  1.34  christos 	if ((lsf & LINUX_SA_ONSTACK) != 0)
    191  1.34  christos 		bsf |= SA_ONSTACK;
    192  1.34  christos 	if ((lsf & LINUX_SA_RESTART) != 0)
    193  1.34  christos 		bsf |= SA_RESTART;
    194  1.34  christos 	if ((lsf & LINUX_SA_ONESHOT) != 0)
    195  1.34  christos 		bsf |= SA_RESETHAND;
    196  1.34  christos 	if ((lsf & LINUX_SA_NOMASK) != 0)
    197  1.34  christos 		bsf |= SA_NODEFER;
    198  1.34  christos 	if ((lsf & LINUX_SA_SIGINFO) != 0)
    199  1.34  christos 		bsf |= SA_SIGINFO;
    200  1.50  christos 	if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
    201  1.36  christos 		DPRINTF(("linux_old_to_native_sigflags: "
    202  1.36  christos 		    "%lx extra bits ignored\n", lsf));
    203  1.50  christos 	}
    204  1.34  christos 	return bsf;
    205  1.33  christos }
    206  1.33  christos 
    207   1.1      fvdl /*
    208  1.46      manu  * Convert between Linux and BSD sigaction structures.
    209   1.1      fvdl  */
    210   1.1      fvdl void
    211  1.57       dsl linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
    212  1.14       erh {
    213  1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    214  1.39  christos 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    215  1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    216  1.14       erh }
    217  1.14       erh 
    218  1.14       erh void
    219  1.57       dsl native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
    220  1.14       erh {
    221  1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    222  1.39  christos 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    223  1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    224  1.34  christos #ifndef __alpha__
    225  1.39  christos 	lsa->linux_sa_restorer = NULL;
    226  1.34  christos #endif
    227  1.14       erh }
    228  1.14       erh 
    229  1.14       erh /* ...and the new sigaction conversion funcs. */
    230  1.14       erh void
    231  1.57       dsl linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
    232   1.1      fvdl {
    233  1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    234  1.39  christos 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    235  1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    236   1.1      fvdl }
    237   1.1      fvdl 
    238   1.1      fvdl void
    239  1.57       dsl native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
    240   1.1      fvdl {
    241  1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    242  1.39  christos 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    243  1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    244  1.34  christos #ifndef __alpha__
    245  1.39  christos 	lsa->linux_sa_restorer = NULL;
    246  1.34  christos #endif
    247   1.1      fvdl }
    248   1.1      fvdl 
    249  1.14       erh /* ----------------------------------------------------------------------- */
    250   1.1      fvdl 
    251   1.1      fvdl /*
    252   1.1      fvdl  * The Linux sigaction() system call. Do the usual conversions,
    253   1.1      fvdl  * and just call sigaction(). Some flags and values are silently
    254   1.1      fvdl  * ignored (see above).
    255   1.1      fvdl  */
    256   1.1      fvdl int
    257  1.58       dsl linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
    258   1.8   thorpej {
    259  1.58       dsl 	/* {
    260   1.1      fvdl 		syscallarg(int) signum;
    261  1.12   mycroft 		syscallarg(const struct linux_sigaction *) nsa;
    262   1.1      fvdl 		syscallarg(struct linux_sigaction *) osa;
    263  1.14       erh 		syscallarg(size_t) sigsetsize;
    264  1.58       dsl 	} */
    265  1.12   mycroft 	struct linux_sigaction nlsa, olsa;
    266  1.12   mycroft 	struct sigaction nbsa, obsa;
    267  1.25      tron 	int error, sig;
    268  1.46      manu 	void *tramp = NULL;
    269  1.46      manu 	int vers = 0;
    270  1.47       mrg #if defined __amd64__
    271  1.53        ad 	struct sigacts *ps = l->l_proc->p_sigacts;
    272  1.46      manu #endif
    273   1.1      fvdl 
    274  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    275  1.14       erh 		return (EINVAL);
    276  1.14       erh 
    277  1.12   mycroft 	if (SCARG(uap, nsa)) {
    278  1.12   mycroft 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
    279  1.12   mycroft 		if (error)
    280  1.12   mycroft 			return (error);
    281  1.33  christos 		linux_to_native_sigaction(&nbsa, &nlsa);
    282  1.12   mycroft 	}
    283  1.46      manu 
    284  1.25      tron 	sig = SCARG(uap, signum);
    285  1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    286  1.25      tron 		return (EINVAL);
    287  1.37  christos 	if (sig > 0 && !linux_to_native_signo[sig]) {
    288  1.29        tv 		/* Pretend that we did something useful for unknown signals. */
    289  1.29        tv 		obsa.sa_handler = SIG_IGN;
    290  1.29        tv 		sigemptyset(&obsa.sa_mask);
    291  1.29        tv 		obsa.sa_flags = 0;
    292  1.29        tv 	} else {
    293  1.46      manu #if defined __amd64__
    294  1.46      manu 		if (nlsa.linux_sa_flags & LINUX_SA_RESTORER) {
    295  1.46      manu 			if ((tramp = nlsa.linux_sa_restorer) != NULL)
    296  1.72       wiz 				vers = 2; /* XXX arch dependent */
    297  1.46      manu 		}
    298  1.46      manu #endif
    299  1.46      manu 
    300  1.53        ad 		error = sigaction1(l, linux_to_native_signo[sig],
    301  1.38   thorpej 		    SCARG(uap, nsa) ? &nbsa : NULL,
    302  1.38   thorpej 		    SCARG(uap, osa) ? &obsa : NULL,
    303  1.46      manu 		    tramp, vers);
    304  1.29        tv 		if (error)
    305  1.29        tv 			return (error);
    306  1.29        tv 	}
    307  1.12   mycroft 	if (SCARG(uap, osa)) {
    308  1.33  christos 		native_to_linux_sigaction(&olsa, &obsa);
    309  1.46      manu 
    310  1.46      manu #if defined __amd64__
    311  1.46      manu 		if (ps->sa_sigdesc[sig].sd_vers != 0) {
    312  1.46      manu 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
    313  1.46      manu 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
    314  1.46      manu 		}
    315  1.46      manu #endif
    316  1.46      manu 
    317  1.12   mycroft 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
    318  1.12   mycroft 		if (error)
    319  1.12   mycroft 			return (error);
    320   1.1      fvdl 	}
    321  1.12   mycroft 	return (0);
    322   1.1      fvdl }
    323   1.1      fvdl 
    324   1.1      fvdl int
    325  1.57       dsl linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
    326   1.8   thorpej {
    327  1.53        ad 	struct proc *p = l->l_proc;
    328  1.14       erh 	linux_old_sigset_t nlss, olss;
    329  1.12   mycroft 	sigset_t nbss, obss;
    330  1.12   mycroft 	int error;
    331   1.1      fvdl 
    332  1.17       erh 	switch (how) {
    333   1.1      fvdl 	case LINUX_SIG_BLOCK:
    334  1.12   mycroft 		how = SIG_BLOCK;
    335   1.1      fvdl 		break;
    336   1.1      fvdl 	case LINUX_SIG_UNBLOCK:
    337  1.12   mycroft 		how = SIG_UNBLOCK;
    338   1.1      fvdl 		break;
    339   1.1      fvdl 	case LINUX_SIG_SETMASK:
    340  1.12   mycroft 		how = SIG_SETMASK;
    341   1.1      fvdl 		break;
    342   1.1      fvdl 	default:
    343  1.12   mycroft 		return (EINVAL);
    344   1.1      fvdl 	}
    345   1.1      fvdl 
    346  1.17       erh 	if (set) {
    347  1.17       erh 		error = copyin(set, &nlss, sizeof(nlss));
    348  1.12   mycroft 		if (error)
    349  1.12   mycroft 			return (error);
    350  1.33  christos 		linux_old_to_native_sigset(&nbss, &nlss);
    351  1.12   mycroft 	}
    352  1.61        ad 	mutex_enter(p->p_lock);
    353  1.53        ad 	error = sigprocmask1(l, how,
    354  1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    355  1.61        ad 	mutex_exit(p->p_lock);
    356  1.12   mycroft 	if (error)
    357  1.42     perry 		return (error);
    358  1.17       erh 	if (oset) {
    359  1.33  christos 		native_to_linux_old_sigset(&olss, &obss);
    360  1.17       erh 		error = copyout(&olss, oset, sizeof(olss));
    361  1.12   mycroft 		if (error)
    362  1.12   mycroft 			return (error);
    363  1.42     perry 	}
    364  1.12   mycroft 	return (error);
    365  1.17       erh }
    366  1.17       erh 
    367  1.17       erh int
    368  1.58       dsl linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
    369  1.17       erh {
    370  1.58       dsl 	/* {
    371  1.17       erh 		syscallarg(int) how;
    372  1.17       erh 		syscallarg(const linux_sigset_t *) set;
    373  1.17       erh 		syscallarg(linux_sigset_t *) oset;
    374  1.17       erh 		syscallarg(size_t) sigsetsize;
    375  1.58       dsl 	} */
    376  1.24      fvdl 	linux_sigset_t nlss, olss, *oset;
    377  1.24      fvdl 	const linux_sigset_t *set;
    378  1.53        ad 	struct proc *p = l->l_proc;
    379  1.24      fvdl 	sigset_t nbss, obss;
    380  1.24      fvdl 	int error, how;
    381  1.24      fvdl 
    382  1.24      fvdl 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    383  1.24      fvdl 		return (EINVAL);
    384  1.24      fvdl 
    385  1.24      fvdl 	switch (SCARG(uap, how)) {
    386  1.24      fvdl 	case LINUX_SIG_BLOCK:
    387  1.24      fvdl 		how = SIG_BLOCK;
    388  1.24      fvdl 		break;
    389  1.24      fvdl 	case LINUX_SIG_UNBLOCK:
    390  1.24      fvdl 		how = SIG_UNBLOCK;
    391  1.24      fvdl 		break;
    392  1.24      fvdl 	case LINUX_SIG_SETMASK:
    393  1.24      fvdl 		how = SIG_SETMASK;
    394  1.24      fvdl 		break;
    395  1.24      fvdl 	default:
    396  1.24      fvdl 		return (EINVAL);
    397  1.17       erh 	}
    398  1.17       erh 
    399  1.24      fvdl 	set = SCARG(uap, set);
    400  1.24      fvdl 	oset = SCARG(uap, oset);
    401  1.24      fvdl 
    402  1.24      fvdl 	if (set) {
    403  1.24      fvdl 		error = copyin(set, &nlss, sizeof(nlss));
    404  1.24      fvdl 		if (error)
    405  1.24      fvdl 			return (error);
    406  1.33  christos 		linux_to_native_sigset(&nbss, &nlss);
    407  1.24      fvdl 	}
    408  1.61        ad 	mutex_enter(p->p_lock);
    409  1.53        ad 	error = sigprocmask1(l, how,
    410  1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    411  1.61        ad 	mutex_exit(p->p_lock);
    412  1.28      tron 	if (!error && oset) {
    413  1.33  christos 		native_to_linux_sigset(&olss, &obss);
    414  1.24      fvdl 		error = copyout(&olss, oset, sizeof(olss));
    415  1.42     perry 	}
    416  1.24      fvdl 	return (error);
    417   1.1      fvdl }
    418   1.1      fvdl 
    419   1.1      fvdl int
    420  1.58       dsl linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
    421   1.1      fvdl {
    422  1.58       dsl 	/* {
    423  1.14       erh 		syscallarg(linux_sigset_t *) set;
    424  1.14       erh 		syscallarg(size_t) sigsetsize;
    425  1.58       dsl 	} */
    426  1.12   mycroft 	sigset_t bss;
    427  1.12   mycroft 	linux_sigset_t lss;
    428   1.6   mycroft 
    429  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    430  1.14       erh 		return (EINVAL);
    431  1.14       erh 
    432  1.53        ad 	sigpending1(l, &bss);
    433  1.33  christos 	native_to_linux_sigset(&lss, &bss);
    434  1.14       erh 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    435   1.1      fvdl }
    436  1.24      fvdl 
    437  1.43      manu #ifndef __amd64__
    438   1.1      fvdl int
    439  1.58       dsl linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
    440   1.8   thorpej {
    441  1.58       dsl 	/* {
    442  1.14       erh 		syscallarg(linux_old_sigset_t *) mask;
    443  1.58       dsl 	} */
    444  1.12   mycroft 	sigset_t bss;
    445  1.14       erh 	linux_old_sigset_t lss;
    446   1.1      fvdl 
    447  1.53        ad 	sigpending1(l, &bss);
    448  1.33  christos 	native_to_linux_old_sigset(&lss, &bss);
    449  1.12   mycroft 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    450   1.1      fvdl }
    451   1.1      fvdl 
    452   1.1      fvdl int
    453  1.58       dsl linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
    454   1.8   thorpej {
    455  1.58       dsl 	/* {
    456  1.54  christos 		syscallarg(void *) restart;
    457   1.3      fvdl 		syscallarg(int) oldmask;
    458   1.1      fvdl 		syscallarg(int) mask;
    459  1.58       dsl 	} */
    460  1.14       erh 	linux_old_sigset_t lss;
    461  1.14       erh 	sigset_t bss;
    462  1.14       erh 
    463  1.14       erh 	lss = SCARG(uap, mask);
    464  1.33  christos 	linux_old_to_native_sigset(&bss, &lss);
    465  1.53        ad 	return (sigsuspend1(l, &bss));
    466  1.14       erh }
    467  1.43      manu #endif /* __amd64__ */
    468  1.43      manu 
    469  1.14       erh int
    470  1.58       dsl linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
    471  1.14       erh {
    472  1.58       dsl 	/* {
    473  1.14       erh 		syscallarg(linux_sigset_t *) unewset;
    474  1.14       erh 		syscallarg(size_t) sigsetsize;
    475  1.58       dsl 	} */
    476  1.12   mycroft 	linux_sigset_t lss;
    477  1.12   mycroft 	sigset_t bss;
    478  1.14       erh 	int error;
    479  1.14       erh 
    480  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    481  1.14       erh 		return (EINVAL);
    482  1.14       erh 
    483  1.14       erh 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
    484  1.14       erh 	if (error)
    485  1.14       erh 		return (error);
    486  1.14       erh 
    487  1.33  christos 	linux_to_native_sigset(&bss, &lss);
    488  1.14       erh 
    489  1.53        ad 	return (sigsuspend1(l, &bss));
    490   1.3      fvdl }
    491   1.3      fvdl 
    492   1.3      fvdl /*
    493  1.14       erh  * Once more: only a signal conversion is needed.
    494  1.14       erh  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
    495   1.3      fvdl  */
    496   1.3      fvdl int
    497  1.58       dsl linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
    498  1.14       erh {
    499  1.69     njoly 	/*
    500  1.14       erh 		syscallarg(int) pid;
    501  1.14       erh 		syscallarg(int) signum;
    502  1.69     njoly 		syscallarg(linix_siginfo_t *) uinfo;
    503  1.69     njoly 	*/
    504  1.69     njoly 	int error;
    505  1.69     njoly 	linux_siginfo_t info;
    506  1.69     njoly 
    507  1.69     njoly 	error = copyin(SCARG(uap, uinfo), &info, sizeof(info));
    508  1.69     njoly 	if (error)
    509  1.69     njoly 		return error;
    510  1.69     njoly 	if (info.lsi_code >= 0)
    511  1.69     njoly 		return EPERM;
    512   1.3      fvdl 
    513  1.14       erh 	/* XXX To really implement this we need to	*/
    514  1.14       erh 	/* XXX keep a list of queued signals somewhere.	*/
    515  1.58       dsl 	return (linux_sys_kill(l, (const void *)uap, retval));
    516   1.1      fvdl }
    517   1.1      fvdl 
    518   1.1      fvdl int
    519  1.58       dsl linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
    520   1.8   thorpej {
    521  1.58       dsl 	/* {
    522   1.1      fvdl 		syscallarg(int) pid;
    523   1.1      fvdl 		syscallarg(int) signum;
    524  1.58       dsl 	} */
    525  1.40   thorpej 
    526   1.9   mycroft 	struct sys_kill_args ka;
    527  1.25      tron 	int sig;
    528   1.6   mycroft 
    529   1.6   mycroft 	SCARG(&ka, pid) = SCARG(uap, pid);
    530  1.25      tron 	sig = SCARG(uap, signum);
    531  1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    532  1.25      tron 		return (EINVAL);
    533  1.37  christos 	SCARG(&ka, signum) = linux_to_native_signo[sig];
    534  1.40   thorpej 	return sys_kill(l, &ka, retval);
    535   1.1      fvdl }
    536  1.30  christos 
    537  1.30  christos #ifdef LINUX_SS_ONSTACK
    538  1.56       dsl static void linux_to_native_sigaltstack(struct sigaltstack *,
    539  1.56       dsl     const struct linux_sigaltstack *);
    540  1.30  christos 
    541  1.30  christos static void
    542  1.57       dsl linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss)
    543  1.30  christos {
    544  1.30  christos 	bss->ss_sp = lss->ss_sp;
    545  1.30  christos 	bss->ss_size = lss->ss_size;
    546  1.30  christos 	if (lss->ss_flags & LINUX_SS_ONSTACK)
    547  1.30  christos 	    bss->ss_flags = SS_ONSTACK;
    548  1.30  christos 	else if (lss->ss_flags & LINUX_SS_DISABLE)
    549  1.30  christos 	    bss->ss_flags = SS_DISABLE;
    550  1.30  christos 	else
    551  1.30  christos 	    bss->ss_flags = 0;
    552  1.30  christos }
    553  1.30  christos 
    554  1.41  christos void
    555  1.57       dsl native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss)
    556  1.30  christos {
    557  1.30  christos 	lss->ss_sp = bss->ss_sp;
    558  1.30  christos 	lss->ss_size = bss->ss_size;
    559  1.30  christos 	if (bss->ss_flags & SS_ONSTACK)
    560  1.30  christos 	    lss->ss_flags = LINUX_SS_ONSTACK;
    561  1.30  christos 	else if (bss->ss_flags & SS_DISABLE)
    562  1.30  christos 	    lss->ss_flags = LINUX_SS_DISABLE;
    563  1.30  christos 	else
    564  1.30  christos 	    lss->ss_flags = 0;
    565  1.30  christos }
    566  1.30  christos 
    567  1.30  christos int
    568  1.58       dsl linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
    569  1.30  christos {
    570  1.58       dsl 	/* {
    571  1.30  christos 		syscallarg(const struct linux_sigaltstack *) ss;
    572  1.30  christos 		syscallarg(struct linux_sigaltstack *) oss;
    573  1.58       dsl 	} */
    574  1.30  christos 	struct linux_sigaltstack ss;
    575  1.48  christos 	struct sigaltstack nss;
    576  1.53        ad 	struct proc *p = l->l_proc;
    577  1.53        ad 	int error = 0;
    578  1.30  christos 
    579  1.48  christos 	if (SCARG(uap, oss)) {
    580  1.53        ad 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
    581  1.48  christos 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
    582  1.48  christos 			return error;
    583  1.48  christos 	}
    584  1.48  christos 
    585  1.30  christos 	if (SCARG(uap, ss) != NULL) {
    586  1.30  christos 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
    587  1.30  christos 			return error;
    588  1.30  christos 		linux_to_native_sigaltstack(&nss, &ss);
    589  1.30  christos 
    590  1.61        ad 		mutex_enter(p->p_lock);
    591  1.53        ad 
    592  1.48  christos 		if (nss.ss_flags & ~SS_ALLBITS)
    593  1.53        ad 			error = EINVAL;
    594  1.53        ad 		else if (nss.ss_flags & SS_DISABLE) {
    595  1.53        ad 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    596  1.53        ad 				error = EINVAL;
    597  1.53        ad 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
    598  1.53        ad 			error = ENOMEM;
    599  1.30  christos 
    600  1.53        ad 		if (error == 0)
    601  1.53        ad 			l->l_sigstk = nss;
    602  1.53        ad 
    603  1.61        ad 		mutex_exit(p->p_lock);
    604  1.30  christos 	}
    605  1.48  christos 
    606  1.53        ad 	return error;
    607  1.30  christos }
    608  1.44       jmc #endif /* LINUX_SS_ONSTACK */
    609  1.49      manu 
    610  1.65     njoly static int
    611  1.65     njoly linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
    612  1.65     njoly {
    613  1.65     njoly 	struct proc *p;
    614  1.71       chs 	struct lwp *t;
    615  1.71       chs 	ksiginfo_t ksi;
    616  1.65     njoly 	int error;
    617  1.65     njoly 
    618  1.65     njoly 	if (signum < 0 || signum >= LINUX__NSIG)
    619  1.65     njoly 		return EINVAL;
    620  1.65     njoly 	signum = linux_to_native_signo[signum];
    621  1.65     njoly 
    622  1.71       chs 	if (tgid == -1) {
    623  1.71       chs 		tgid = tid;
    624  1.71       chs 	}
    625  1.71       chs 
    626  1.65     njoly 	KSI_INIT(&ksi);
    627  1.65     njoly 	ksi.ksi_signo = signum;
    628  1.65     njoly 	ksi.ksi_code = SI_LWP;
    629  1.65     njoly 	ksi.ksi_pid = l->l_proc->p_pid;
    630  1.65     njoly 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    631  1.71       chs 	ksi.ksi_lid = tid;
    632  1.65     njoly 
    633  1.65     njoly 	mutex_enter(proc_lock);
    634  1.71       chs 	p = proc_find(tgid);
    635  1.71       chs 	if (p == NULL) {
    636  1.65     njoly 		mutex_exit(proc_lock);
    637  1.65     njoly 		return ESRCH;
    638  1.65     njoly 	}
    639  1.65     njoly 	mutex_enter(p->p_lock);
    640  1.65     njoly 	error = kauth_authorize_process(l->l_cred,
    641  1.65     njoly 	    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
    642  1.71       chs 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    643  1.71       chs 		error = ESRCH;
    644  1.71       chs 	else if (signum != 0)
    645  1.65     njoly 		kpsignal2(p, &ksi);
    646  1.65     njoly 	mutex_exit(p->p_lock);
    647  1.65     njoly 	mutex_exit(proc_lock);
    648  1.65     njoly 
    649  1.65     njoly 	return error;
    650  1.65     njoly }
    651  1.65     njoly 
    652  1.49      manu int
    653  1.58       dsl linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
    654  1.49      manu {
    655  1.58       dsl 	/* {
    656  1.49      manu 		syscallarg(int) tid;
    657  1.49      manu 		syscallarg(int) sig;
    658  1.58       dsl 	} */
    659  1.49      manu 
    660  1.65     njoly 	if (SCARG(uap, tid) <= 0)
    661  1.65     njoly 		return EINVAL;
    662  1.49      manu 
    663  1.71       chs 	return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig));
    664  1.49      manu }
    665  1.49      manu 
    666  1.49      manu int
    667  1.58       dsl linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
    668  1.49      manu {
    669  1.58       dsl 	/* {
    670  1.49      manu 		syscallarg(int) tgid;
    671  1.49      manu 		syscallarg(int) tid;
    672  1.49      manu 		syscallarg(int) sig;
    673  1.58       dsl 	} */
    674  1.49      manu 
    675  1.71       chs 	if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1)
    676  1.65     njoly 		return EINVAL;
    677  1.49      manu 
    678  1.65     njoly 	return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
    679  1.49      manu }
    680  1.64     njoly 
    681  1.64     njoly int
    682  1.64     njoly native_to_linux_si_code(int code)
    683  1.64     njoly {
    684  1.64     njoly 	int si_codes[] = {
    685  1.64     njoly 	    LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
    686  1.64     njoly 	    LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
    687  1.64     njoly 	};
    688  1.64     njoly 
    689  1.64     njoly 	if (code <= 0 && -code < __arraycount(si_codes))
    690  1.64     njoly 		return si_codes[-code];
    691  1.64     njoly 
    692  1.64     njoly 	return code;
    693  1.64     njoly }
    694  1.68     njoly 
    695  1.68     njoly int
    696  1.68     njoly native_to_linux_si_status(int code, int status)
    697  1.68     njoly {
    698  1.68     njoly 	int sts;
    699  1.68     njoly 
    700  1.68     njoly 	switch (code) {
    701  1.68     njoly 	case CLD_CONTINUED:
    702  1.68     njoly 		sts = LINUX_SIGCONT;
    703  1.68     njoly 		break;
    704  1.68     njoly 	case CLD_EXITED:
    705  1.68     njoly 		sts = WEXITSTATUS(status);
    706  1.68     njoly 		break;
    707  1.68     njoly 	case CLD_STOPPED:
    708  1.68     njoly 	case CLD_TRAPPED:
    709  1.68     njoly 	case CLD_DUMPED:
    710  1.68     njoly 	case CLD_KILLED:
    711  1.68     njoly 	default:
    712  1.68     njoly 		sts = native_to_linux_signo[WTERMSIG(status)];
    713  1.68     njoly 		break;
    714  1.68     njoly 	}
    715  1.68     njoly 
    716  1.68     njoly 	return sts;
    717  1.68     njoly }
    718