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