Home | History | Annotate | Line # | Download | only in common
linux_signal.c revision 1.65
      1  1.65     njoly /*	$NetBSD: linux_signal.c,v 1.65 2008/10/21 13:36:28 njoly 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.65     njoly __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.65 2008/10/21 13:36:28 njoly 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.1      fvdl 
     67   1.1      fvdl #include <sys/syscallargs.h>
     68   1.1      fvdl 
     69  1.15  christos #include <compat/linux/common/linux_types.h>
     70  1.15  christos #include <compat/linux/common/linux_signal.h>
     71  1.49      manu #include <compat/linux/common/linux_exec.h> /* For emul_linux */
     72  1.49      manu #include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
     73  1.49      manu #include <compat/linux/common/linux_emuldata.h> /* for linux_emuldata */
     74  1.17       erh #include <compat/linux/common/linux_siginfo.h>
     75  1.45      fvdl #include <compat/linux/common/linux_sigevent.h>
     76  1.15  christos #include <compat/linux/common/linux_util.h>
     77  1.55     njoly #include <compat/linux/common/linux_ipc.h>
     78  1.55     njoly #include <compat/linux/common/linux_sem.h>
     79  1.15  christos 
     80   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     81   1.1      fvdl 
     82  1.14       erh /* Locally used defines (in bsd<->linux conversion functions): */
     83  1.11     perry #define	linux_sigemptyset(s)	memset((s), 0, sizeof(*(s)))
     84  1.20     itohy #define	linux_sigismember(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     85  1.63     njoly 					& (1L << ((n) - 1) % LINUX__NSIG_BPW))
     86  1.20     itohy #define	linux_sigaddset(s, n)	((s)->sig[((n) - 1) / LINUX__NSIG_BPW]	\
     87  1.63     njoly 					|= (1L << ((n) - 1) % LINUX__NSIG_BPW))
     88   1.6   mycroft 
     89  1.36  christos #ifdef DEBUG_LINUX
     90  1.36  christos #define DPRINTF(a)	uprintf a
     91  1.36  christos #else
     92  1.36  christos #define DPRINTF(a)
     93  1.36  christos #endif
     94  1.36  christos 
     95  1.37  christos extern const int native_to_linux_signo[];
     96  1.37  christos extern const int linux_to_native_signo[];
     97   1.6   mycroft 
     98  1.14       erh /*
     99  1.20     itohy  * Convert between Linux and BSD signal sets.
    100  1.14       erh  */
    101  1.20     itohy #if LINUX__NSIG_WORDS > 1
    102   1.6   mycroft void
    103  1.57       dsl linux_old_extra_to_native_sigset(sigset_t *bss, const linux_old_sigset_t *lss, const unsigned long *extra)
    104  1.20     itohy {
    105  1.20     itohy 	linux_sigset_t lsnew;
    106  1.20     itohy 
    107  1.20     itohy 	/* convert old sigset to new sigset */
    108  1.20     itohy 	linux_sigemptyset(&lsnew);
    109  1.20     itohy 	lsnew.sig[0] = *lss;
    110  1.20     itohy 	if (extra)
    111  1.33  christos 		memcpy(&lsnew.sig[1], extra,
    112  1.33  christos 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    113  1.20     itohy 
    114  1.33  christos 	linux_to_native_sigset(bss, &lsnew);
    115  1.20     itohy }
    116  1.20     itohy 
    117  1.20     itohy void
    118  1.57       dsl native_to_linux_old_extra_sigset(linux_old_sigset_t *lss, unsigned long *extra, const sigset_t *bss)
    119  1.20     itohy {
    120  1.20     itohy 	linux_sigset_t lsnew;
    121  1.20     itohy 
    122  1.33  christos 	native_to_linux_sigset(&lsnew, bss);
    123  1.20     itohy 
    124  1.20     itohy 	/* convert new sigset to old sigset */
    125  1.20     itohy 	*lss = lsnew.sig[0];
    126  1.20     itohy 	if (extra)
    127  1.33  christos 		memcpy(extra, &lsnew.sig[1],
    128  1.33  christos 		    sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t));
    129  1.20     itohy }
    130  1.43      manu #endif /* LINUX__NSIG_WORDS > 1 */
    131  1.20     itohy 
    132  1.20     itohy void
    133  1.57       dsl linux_to_native_sigset(sigset_t *bss, const linux_sigset_t *lss)
    134   1.1      fvdl {
    135   1.1      fvdl 	int i, newsig;
    136   1.1      fvdl 
    137   1.6   mycroft 	sigemptyset(bss);
    138  1.20     itohy 	for (i = 1; i < LINUX__NSIG; i++) {
    139   1.6   mycroft 		if (linux_sigismember(lss, i)) {
    140  1.37  christos 			newsig = linux_to_native_signo[i];
    141   1.1      fvdl 			if (newsig)
    142   1.6   mycroft 				sigaddset(bss, newsig);
    143   1.1      fvdl 		}
    144   1.1      fvdl 	}
    145   1.1      fvdl }
    146   1.1      fvdl 
    147   1.1      fvdl void
    148  1.57       dsl native_to_linux_sigset(linux_sigset_t *lss, const sigset_t *bss)
    149   1.1      fvdl {
    150   1.1      fvdl 	int i, newsig;
    151  1.20     itohy 
    152   1.6   mycroft 	linux_sigemptyset(lss);
    153   1.6   mycroft 	for (i = 1; i < NSIG; i++) {
    154   1.6   mycroft 		if (sigismember(bss, i)) {
    155  1.37  christos 			newsig = native_to_linux_signo[i];
    156   1.1      fvdl 			if (newsig)
    157   1.6   mycroft 				linux_sigaddset(lss, newsig);
    158   1.1      fvdl 		}
    159   1.1      fvdl 	}
    160   1.1      fvdl }
    161   1.1      fvdl 
    162  1.34  christos unsigned int
    163  1.57       dsl native_to_linux_sigflags(const int bsf)
    164  1.33  christos {
    165  1.34  christos 	unsigned int lsf = 0;
    166  1.34  christos 	if ((bsf & SA_NOCLDSTOP) != 0)
    167  1.34  christos 		lsf |= LINUX_SA_NOCLDSTOP;
    168  1.34  christos 	if ((bsf & SA_NOCLDWAIT) != 0)
    169  1.34  christos 		lsf |= LINUX_SA_NOCLDWAIT;
    170  1.34  christos 	if ((bsf & SA_ONSTACK) != 0)
    171  1.34  christos 		lsf |= LINUX_SA_ONSTACK;
    172  1.34  christos 	if ((bsf & SA_RESTART) != 0)
    173  1.34  christos 		lsf |= LINUX_SA_RESTART;
    174  1.34  christos 	if ((bsf & SA_NODEFER) != 0)
    175  1.34  christos 		lsf |= LINUX_SA_NOMASK;
    176  1.34  christos 	if ((bsf & SA_RESETHAND) != 0)
    177  1.34  christos 		lsf |= LINUX_SA_ONESHOT;
    178  1.34  christos 	if ((bsf & SA_SIGINFO) != 0)
    179  1.34  christos 		lsf |= LINUX_SA_SIGINFO;
    180  1.34  christos 	return lsf;
    181  1.33  christos }
    182  1.33  christos 
    183  1.34  christos int
    184  1.57       dsl linux_to_native_sigflags(const unsigned long lsf)
    185  1.33  christos {
    186  1.34  christos 	int bsf = 0;
    187  1.34  christos 	if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
    188  1.34  christos 		bsf |= SA_NOCLDSTOP;
    189  1.34  christos 	if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
    190  1.34  christos 		bsf |= SA_NOCLDWAIT;
    191  1.34  christos 	if ((lsf & LINUX_SA_ONSTACK) != 0)
    192  1.34  christos 		bsf |= SA_ONSTACK;
    193  1.34  christos 	if ((lsf & LINUX_SA_RESTART) != 0)
    194  1.34  christos 		bsf |= SA_RESTART;
    195  1.34  christos 	if ((lsf & LINUX_SA_ONESHOT) != 0)
    196  1.34  christos 		bsf |= SA_RESETHAND;
    197  1.34  christos 	if ((lsf & LINUX_SA_NOMASK) != 0)
    198  1.34  christos 		bsf |= SA_NODEFER;
    199  1.34  christos 	if ((lsf & LINUX_SA_SIGINFO) != 0)
    200  1.34  christos 		bsf |= SA_SIGINFO;
    201  1.50  christos 	if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
    202  1.36  christos 		DPRINTF(("linux_old_to_native_sigflags: "
    203  1.36  christos 		    "%lx extra bits ignored\n", lsf));
    204  1.50  christos 	}
    205  1.34  christos 	return bsf;
    206  1.33  christos }
    207  1.33  christos 
    208   1.1      fvdl /*
    209  1.46      manu  * Convert between Linux and BSD sigaction structures.
    210   1.1      fvdl  */
    211   1.1      fvdl void
    212  1.57       dsl linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
    213  1.14       erh {
    214  1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    215  1.39  christos 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    216  1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    217  1.14       erh }
    218  1.14       erh 
    219  1.14       erh void
    220  1.57       dsl native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
    221  1.14       erh {
    222  1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    223  1.39  christos 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    224  1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    225  1.34  christos #ifndef __alpha__
    226  1.39  christos 	lsa->linux_sa_restorer = NULL;
    227  1.34  christos #endif
    228  1.14       erh }
    229  1.14       erh 
    230  1.14       erh /* ...and the new sigaction conversion funcs. */
    231  1.14       erh void
    232  1.57       dsl linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
    233   1.1      fvdl {
    234  1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    235  1.39  christos 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    236  1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    237   1.1      fvdl }
    238   1.1      fvdl 
    239   1.1      fvdl void
    240  1.57       dsl native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
    241   1.1      fvdl {
    242  1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    243  1.39  christos 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    244  1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    245  1.34  christos #ifndef __alpha__
    246  1.39  christos 	lsa->linux_sa_restorer = NULL;
    247  1.34  christos #endif
    248   1.1      fvdl }
    249   1.1      fvdl 
    250  1.14       erh /* ----------------------------------------------------------------------- */
    251   1.1      fvdl 
    252   1.1      fvdl /*
    253   1.1      fvdl  * The Linux sigaction() system call. Do the usual conversions,
    254   1.1      fvdl  * and just call sigaction(). Some flags and values are silently
    255   1.1      fvdl  * ignored (see above).
    256   1.1      fvdl  */
    257   1.1      fvdl int
    258  1.58       dsl linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
    259   1.8   thorpej {
    260  1.58       dsl 	/* {
    261   1.1      fvdl 		syscallarg(int) signum;
    262  1.12   mycroft 		syscallarg(const struct linux_sigaction *) nsa;
    263   1.1      fvdl 		syscallarg(struct linux_sigaction *) osa;
    264  1.14       erh 		syscallarg(size_t) sigsetsize;
    265  1.58       dsl 	} */
    266  1.12   mycroft 	struct linux_sigaction nlsa, olsa;
    267  1.12   mycroft 	struct sigaction nbsa, obsa;
    268  1.25      tron 	int error, sig;
    269  1.46      manu 	void *tramp = NULL;
    270  1.46      manu 	int vers = 0;
    271  1.47       mrg #if defined __amd64__
    272  1.53        ad 	struct sigacts *ps = l->l_proc->p_sigacts;
    273  1.46      manu #endif
    274   1.1      fvdl 
    275  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    276  1.14       erh 		return (EINVAL);
    277  1.14       erh 
    278  1.12   mycroft 	if (SCARG(uap, nsa)) {
    279  1.12   mycroft 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
    280  1.12   mycroft 		if (error)
    281  1.12   mycroft 			return (error);
    282  1.33  christos 		linux_to_native_sigaction(&nbsa, &nlsa);
    283  1.12   mycroft 	}
    284  1.46      manu 
    285  1.25      tron 	sig = SCARG(uap, signum);
    286  1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    287  1.25      tron 		return (EINVAL);
    288  1.37  christos 	if (sig > 0 && !linux_to_native_signo[sig]) {
    289  1.29        tv 		/* Pretend that we did something useful for unknown signals. */
    290  1.29        tv 		obsa.sa_handler = SIG_IGN;
    291  1.29        tv 		sigemptyset(&obsa.sa_mask);
    292  1.29        tv 		obsa.sa_flags = 0;
    293  1.29        tv 	} else {
    294  1.46      manu #if defined __amd64__
    295  1.46      manu 		if (nlsa.linux_sa_flags & LINUX_SA_RESTORER) {
    296  1.46      manu 			if ((tramp = nlsa.linux_sa_restorer) != NULL)
    297  1.46      manu 				vers = 2; /* XXX arch dependant */
    298  1.46      manu 		}
    299  1.46      manu #endif
    300  1.46      manu 
    301  1.53        ad 		error = sigaction1(l, linux_to_native_signo[sig],
    302  1.38   thorpej 		    SCARG(uap, nsa) ? &nbsa : NULL,
    303  1.38   thorpej 		    SCARG(uap, osa) ? &obsa : NULL,
    304  1.46      manu 		    tramp, vers);
    305  1.29        tv 		if (error)
    306  1.29        tv 			return (error);
    307  1.29        tv 	}
    308  1.12   mycroft 	if (SCARG(uap, osa)) {
    309  1.33  christos 		native_to_linux_sigaction(&olsa, &obsa);
    310  1.46      manu 
    311  1.46      manu #if defined __amd64__
    312  1.46      manu 		if (ps->sa_sigdesc[sig].sd_vers != 0) {
    313  1.46      manu 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
    314  1.46      manu 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
    315  1.46      manu 		}
    316  1.46      manu #endif
    317  1.46      manu 
    318  1.12   mycroft 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
    319  1.12   mycroft 		if (error)
    320  1.12   mycroft 			return (error);
    321   1.1      fvdl 	}
    322  1.12   mycroft 	return (0);
    323   1.1      fvdl }
    324   1.1      fvdl 
    325   1.1      fvdl int
    326  1.57       dsl linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
    327   1.8   thorpej {
    328  1.53        ad 	struct proc *p = l->l_proc;
    329  1.14       erh 	linux_old_sigset_t nlss, olss;
    330  1.12   mycroft 	sigset_t nbss, obss;
    331  1.12   mycroft 	int error;
    332   1.1      fvdl 
    333  1.17       erh 	switch (how) {
    334   1.1      fvdl 	case LINUX_SIG_BLOCK:
    335  1.12   mycroft 		how = SIG_BLOCK;
    336   1.1      fvdl 		break;
    337   1.1      fvdl 	case LINUX_SIG_UNBLOCK:
    338  1.12   mycroft 		how = SIG_UNBLOCK;
    339   1.1      fvdl 		break;
    340   1.1      fvdl 	case LINUX_SIG_SETMASK:
    341  1.12   mycroft 		how = SIG_SETMASK;
    342   1.1      fvdl 		break;
    343   1.1      fvdl 	default:
    344  1.12   mycroft 		return (EINVAL);
    345   1.1      fvdl 	}
    346   1.1      fvdl 
    347  1.17       erh 	if (set) {
    348  1.17       erh 		error = copyin(set, &nlss, sizeof(nlss));
    349  1.12   mycroft 		if (error)
    350  1.12   mycroft 			return (error);
    351  1.33  christos 		linux_old_to_native_sigset(&nbss, &nlss);
    352  1.12   mycroft 	}
    353  1.61        ad 	mutex_enter(p->p_lock);
    354  1.53        ad 	error = sigprocmask1(l, how,
    355  1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    356  1.61        ad 	mutex_exit(p->p_lock);
    357  1.12   mycroft 	if (error)
    358  1.42     perry 		return (error);
    359  1.17       erh 	if (oset) {
    360  1.33  christos 		native_to_linux_old_sigset(&olss, &obss);
    361  1.17       erh 		error = copyout(&olss, oset, sizeof(olss));
    362  1.12   mycroft 		if (error)
    363  1.12   mycroft 			return (error);
    364  1.42     perry 	}
    365  1.12   mycroft 	return (error);
    366  1.17       erh }
    367  1.17       erh 
    368  1.17       erh int
    369  1.58       dsl linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
    370  1.17       erh {
    371  1.58       dsl 	/* {
    372  1.17       erh 		syscallarg(int) how;
    373  1.17       erh 		syscallarg(const linux_sigset_t *) set;
    374  1.17       erh 		syscallarg(linux_sigset_t *) oset;
    375  1.17       erh 		syscallarg(size_t) sigsetsize;
    376  1.58       dsl 	} */
    377  1.24      fvdl 	linux_sigset_t nlss, olss, *oset;
    378  1.24      fvdl 	const linux_sigset_t *set;
    379  1.53        ad 	struct proc *p = l->l_proc;
    380  1.24      fvdl 	sigset_t nbss, obss;
    381  1.24      fvdl 	int error, how;
    382  1.24      fvdl 
    383  1.24      fvdl 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    384  1.24      fvdl 		return (EINVAL);
    385  1.24      fvdl 
    386  1.24      fvdl 	switch (SCARG(uap, how)) {
    387  1.24      fvdl 	case LINUX_SIG_BLOCK:
    388  1.24      fvdl 		how = SIG_BLOCK;
    389  1.24      fvdl 		break;
    390  1.24      fvdl 	case LINUX_SIG_UNBLOCK:
    391  1.24      fvdl 		how = SIG_UNBLOCK;
    392  1.24      fvdl 		break;
    393  1.24      fvdl 	case LINUX_SIG_SETMASK:
    394  1.24      fvdl 		how = SIG_SETMASK;
    395  1.24      fvdl 		break;
    396  1.24      fvdl 	default:
    397  1.24      fvdl 		return (EINVAL);
    398  1.17       erh 	}
    399  1.17       erh 
    400  1.24      fvdl 	set = SCARG(uap, set);
    401  1.24      fvdl 	oset = SCARG(uap, oset);
    402  1.24      fvdl 
    403  1.24      fvdl 	if (set) {
    404  1.24      fvdl 		error = copyin(set, &nlss, sizeof(nlss));
    405  1.24      fvdl 		if (error)
    406  1.24      fvdl 			return (error);
    407  1.33  christos 		linux_to_native_sigset(&nbss, &nlss);
    408  1.24      fvdl 	}
    409  1.61        ad 	mutex_enter(p->p_lock);
    410  1.53        ad 	error = sigprocmask1(l, how,
    411  1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    412  1.61        ad 	mutex_exit(p->p_lock);
    413  1.28      tron 	if (!error && oset) {
    414  1.33  christos 		native_to_linux_sigset(&olss, &obss);
    415  1.24      fvdl 		error = copyout(&olss, oset, sizeof(olss));
    416  1.42     perry 	}
    417  1.24      fvdl 	return (error);
    418   1.1      fvdl }
    419   1.1      fvdl 
    420   1.1      fvdl int
    421  1.58       dsl linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
    422   1.1      fvdl {
    423  1.58       dsl 	/* {
    424  1.14       erh 		syscallarg(linux_sigset_t *) set;
    425  1.14       erh 		syscallarg(size_t) sigsetsize;
    426  1.58       dsl 	} */
    427  1.12   mycroft 	sigset_t bss;
    428  1.12   mycroft 	linux_sigset_t lss;
    429   1.6   mycroft 
    430  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    431  1.14       erh 		return (EINVAL);
    432  1.14       erh 
    433  1.53        ad 	sigpending1(l, &bss);
    434  1.33  christos 	native_to_linux_sigset(&lss, &bss);
    435  1.14       erh 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    436   1.1      fvdl }
    437  1.24      fvdl 
    438  1.43      manu #ifndef __amd64__
    439   1.1      fvdl int
    440  1.58       dsl linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
    441   1.8   thorpej {
    442  1.58       dsl 	/* {
    443  1.14       erh 		syscallarg(linux_old_sigset_t *) mask;
    444  1.58       dsl 	} */
    445  1.12   mycroft 	sigset_t bss;
    446  1.14       erh 	linux_old_sigset_t lss;
    447   1.1      fvdl 
    448  1.53        ad 	sigpending1(l, &bss);
    449  1.33  christos 	native_to_linux_old_sigset(&lss, &bss);
    450  1.12   mycroft 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    451   1.1      fvdl }
    452   1.1      fvdl 
    453   1.1      fvdl int
    454  1.58       dsl linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
    455   1.8   thorpej {
    456  1.58       dsl 	/* {
    457  1.54  christos 		syscallarg(void *) restart;
    458   1.3      fvdl 		syscallarg(int) oldmask;
    459   1.1      fvdl 		syscallarg(int) mask;
    460  1.58       dsl 	} */
    461  1.14       erh 	linux_old_sigset_t lss;
    462  1.14       erh 	sigset_t bss;
    463  1.14       erh 
    464  1.14       erh 	lss = SCARG(uap, mask);
    465  1.33  christos 	linux_old_to_native_sigset(&bss, &lss);
    466  1.53        ad 	return (sigsuspend1(l, &bss));
    467  1.14       erh }
    468  1.43      manu #endif /* __amd64__ */
    469  1.43      manu 
    470  1.14       erh int
    471  1.58       dsl linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
    472  1.14       erh {
    473  1.58       dsl 	/* {
    474  1.14       erh 		syscallarg(linux_sigset_t *) unewset;
    475  1.14       erh 		syscallarg(size_t) sigsetsize;
    476  1.58       dsl 	} */
    477  1.12   mycroft 	linux_sigset_t lss;
    478  1.12   mycroft 	sigset_t bss;
    479  1.14       erh 	int error;
    480  1.14       erh 
    481  1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    482  1.14       erh 		return (EINVAL);
    483  1.14       erh 
    484  1.14       erh 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
    485  1.14       erh 	if (error)
    486  1.14       erh 		return (error);
    487  1.14       erh 
    488  1.33  christos 	linux_to_native_sigset(&bss, &lss);
    489  1.14       erh 
    490  1.53        ad 	return (sigsuspend1(l, &bss));
    491   1.3      fvdl }
    492   1.3      fvdl 
    493   1.3      fvdl /*
    494  1.14       erh  * Once more: only a signal conversion is needed.
    495  1.14       erh  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
    496   1.3      fvdl  */
    497   1.3      fvdl int
    498  1.58       dsl linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
    499  1.14       erh {
    500  1.14       erh 	/* XXX XAX This isn't this really int, int, siginfo_t *, is it? */
    501  1.14       erh #if 0
    502  1.14       erh 	struct linux_sys_rt_queueinfo_args /* {
    503  1.14       erh 		syscallarg(int) pid;
    504  1.14       erh 		syscallarg(int) signum;
    505  1.14       erh 		syscallarg(siginfo_t *) uinfo;
    506  1.14       erh 	} */ *uap = v;
    507  1.14       erh #endif
    508   1.3      fvdl 
    509  1.14       erh 	/* XXX To really implement this we need to	*/
    510  1.14       erh 	/* XXX keep a list of queued signals somewhere.	*/
    511  1.58       dsl 	return (linux_sys_kill(l, (const void *)uap, retval));
    512   1.1      fvdl }
    513   1.1      fvdl 
    514   1.1      fvdl int
    515  1.58       dsl linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
    516   1.8   thorpej {
    517  1.58       dsl 	/* {
    518   1.1      fvdl 		syscallarg(int) pid;
    519   1.1      fvdl 		syscallarg(int) signum;
    520  1.58       dsl 	} */
    521  1.40   thorpej 
    522   1.9   mycroft 	struct sys_kill_args ka;
    523  1.25      tron 	int sig;
    524   1.6   mycroft 
    525   1.6   mycroft 	SCARG(&ka, pid) = SCARG(uap, pid);
    526  1.25      tron 	sig = SCARG(uap, signum);
    527  1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    528  1.25      tron 		return (EINVAL);
    529  1.37  christos 	SCARG(&ka, signum) = linux_to_native_signo[sig];
    530  1.40   thorpej 	return sys_kill(l, &ka, retval);
    531   1.1      fvdl }
    532  1.30  christos 
    533  1.30  christos #ifdef LINUX_SS_ONSTACK
    534  1.56       dsl static void linux_to_native_sigaltstack(struct sigaltstack *,
    535  1.56       dsl     const struct linux_sigaltstack *);
    536  1.30  christos 
    537  1.30  christos static void
    538  1.57       dsl linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss)
    539  1.30  christos {
    540  1.30  christos 	bss->ss_sp = lss->ss_sp;
    541  1.30  christos 	bss->ss_size = lss->ss_size;
    542  1.30  christos 	if (lss->ss_flags & LINUX_SS_ONSTACK)
    543  1.30  christos 	    bss->ss_flags = SS_ONSTACK;
    544  1.30  christos 	else if (lss->ss_flags & LINUX_SS_DISABLE)
    545  1.30  christos 	    bss->ss_flags = SS_DISABLE;
    546  1.30  christos 	else
    547  1.30  christos 	    bss->ss_flags = 0;
    548  1.30  christos }
    549  1.30  christos 
    550  1.41  christos void
    551  1.57       dsl native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss)
    552  1.30  christos {
    553  1.30  christos 	lss->ss_sp = bss->ss_sp;
    554  1.30  christos 	lss->ss_size = bss->ss_size;
    555  1.30  christos 	if (bss->ss_flags & SS_ONSTACK)
    556  1.30  christos 	    lss->ss_flags = LINUX_SS_ONSTACK;
    557  1.30  christos 	else if (bss->ss_flags & SS_DISABLE)
    558  1.30  christos 	    lss->ss_flags = LINUX_SS_DISABLE;
    559  1.30  christos 	else
    560  1.30  christos 	    lss->ss_flags = 0;
    561  1.30  christos }
    562  1.30  christos 
    563  1.30  christos int
    564  1.58       dsl linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
    565  1.30  christos {
    566  1.58       dsl 	/* {
    567  1.30  christos 		syscallarg(const struct linux_sigaltstack *) ss;
    568  1.30  christos 		syscallarg(struct linux_sigaltstack *) oss;
    569  1.58       dsl 	} */
    570  1.30  christos 	struct linux_sigaltstack ss;
    571  1.48  christos 	struct sigaltstack nss;
    572  1.53        ad 	struct proc *p = l->l_proc;
    573  1.53        ad 	int error = 0;
    574  1.30  christos 
    575  1.48  christos 	if (SCARG(uap, oss)) {
    576  1.53        ad 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
    577  1.48  christos 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
    578  1.48  christos 			return error;
    579  1.48  christos 	}
    580  1.48  christos 
    581  1.30  christos 	if (SCARG(uap, ss) != NULL) {
    582  1.30  christos 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
    583  1.30  christos 			return error;
    584  1.30  christos 		linux_to_native_sigaltstack(&nss, &ss);
    585  1.30  christos 
    586  1.61        ad 		mutex_enter(p->p_lock);
    587  1.53        ad 
    588  1.48  christos 		if (nss.ss_flags & ~SS_ALLBITS)
    589  1.53        ad 			error = EINVAL;
    590  1.53        ad 		else if (nss.ss_flags & SS_DISABLE) {
    591  1.53        ad 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    592  1.53        ad 				error = EINVAL;
    593  1.53        ad 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
    594  1.53        ad 			error = ENOMEM;
    595  1.30  christos 
    596  1.53        ad 		if (error == 0)
    597  1.53        ad 			l->l_sigstk = nss;
    598  1.53        ad 
    599  1.61        ad 		mutex_exit(p->p_lock);
    600  1.30  christos 	}
    601  1.48  christos 
    602  1.53        ad 	return error;
    603  1.30  christos }
    604  1.44       jmc #endif /* LINUX_SS_ONSTACK */
    605  1.49      manu 
    606  1.49      manu #ifdef LINUX_NPTL
    607  1.65     njoly static int
    608  1.65     njoly linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
    609  1.65     njoly {
    610  1.65     njoly 	struct proc *p;
    611  1.65     njoly 	int error;
    612  1.65     njoly 	ksiginfo_t ksi;
    613  1.65     njoly 	struct linux_emuldata *led;
    614  1.65     njoly 
    615  1.65     njoly 	if (signum < 0 || signum >= LINUX__NSIG)
    616  1.65     njoly 		return EINVAL;
    617  1.65     njoly 	signum = linux_to_native_signo[signum];
    618  1.65     njoly 
    619  1.65     njoly 	KSI_INIT(&ksi);
    620  1.65     njoly 	ksi.ksi_signo = signum;
    621  1.65     njoly 	ksi.ksi_code = SI_LWP;
    622  1.65     njoly 	ksi.ksi_pid = l->l_proc->p_pid;
    623  1.65     njoly 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    624  1.65     njoly 
    625  1.65     njoly 	mutex_enter(proc_lock);
    626  1.65     njoly 	if ((p = p_find(tid, PFIND_LOCKED)) == NULL) {
    627  1.65     njoly 		mutex_exit(proc_lock);
    628  1.65     njoly 		return ESRCH;
    629  1.65     njoly 	}
    630  1.65     njoly 	led = p->p_emuldata;
    631  1.65     njoly 	if (tgid > 0 && led->s->group_pid != tgid) {
    632  1.65     njoly 		mutex_exit(proc_lock);
    633  1.65     njoly 		return ESRCH;
    634  1.65     njoly 	}
    635  1.65     njoly 	mutex_enter(p->p_lock);
    636  1.65     njoly 	error = kauth_authorize_process(l->l_cred,
    637  1.65     njoly 	    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
    638  1.65     njoly 	if (!error && signum)
    639  1.65     njoly 		kpsignal2(p, &ksi);
    640  1.65     njoly 	mutex_exit(p->p_lock);
    641  1.65     njoly 	mutex_exit(proc_lock);
    642  1.65     njoly 
    643  1.65     njoly 	return error;
    644  1.65     njoly }
    645  1.65     njoly 
    646  1.49      manu int
    647  1.58       dsl linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
    648  1.49      manu {
    649  1.58       dsl 	/* {
    650  1.49      manu 		syscallarg(int) tid;
    651  1.49      manu 		syscallarg(int) sig;
    652  1.58       dsl 	} */
    653  1.49      manu 
    654  1.65     njoly 	if (SCARG(uap, tid) <= 0)
    655  1.65     njoly 		return EINVAL;
    656  1.49      manu 
    657  1.65     njoly 	return linux_do_tkill(l, 0, SCARG(uap, tid), SCARG(uap, sig));
    658  1.49      manu }
    659  1.49      manu 
    660  1.49      manu int
    661  1.58       dsl linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
    662  1.49      manu {
    663  1.58       dsl 	/* {
    664  1.49      manu 		syscallarg(int) tgid;
    665  1.49      manu 		syscallarg(int) tid;
    666  1.49      manu 		syscallarg(int) sig;
    667  1.58       dsl 	} */
    668  1.49      manu 
    669  1.65     njoly 	if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) <= 0)
    670  1.65     njoly 		return EINVAL;
    671  1.49      manu 
    672  1.65     njoly 	return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
    673  1.49      manu }
    674  1.49      manu #endif /* LINUX_NPTL */
    675  1.64     njoly 
    676  1.64     njoly int
    677  1.64     njoly native_to_linux_si_code(int code)
    678  1.64     njoly {
    679  1.64     njoly 	int si_codes[] = {
    680  1.64     njoly 	    LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
    681  1.64     njoly 	    LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
    682  1.64     njoly 	};
    683  1.64     njoly 
    684  1.64     njoly 	if (code <= 0 && -code < __arraycount(si_codes))
    685  1.64     njoly 		return si_codes[-code];
    686  1.64     njoly 
    687  1.64     njoly 	return code;
    688  1.64     njoly }
    689