Home | History | Annotate | Line # | Download | only in common
linux_signal.c revision 1.89.6.1
      1  1.89.6.1  perseant /*	$NetBSD: linux_signal.c,v 1.89.6.1 2025/08/02 05:56:27 perseant 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.89.6.1  perseant __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.89.6.1 2025/08/02 05:56:27 perseant 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.68     njoly #include <sys/wait.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.71       chs #include <compat/linux/common/linux_emuldata.h>
     72      1.17       erh #include <compat/linux/common/linux_siginfo.h>
     73      1.45      fvdl #include <compat/linux/common/linux_sigevent.h>
     74      1.15  christos #include <compat/linux/common/linux_util.h>
     75      1.55     njoly #include <compat/linux/common/linux_ipc.h>
     76      1.55     njoly #include <compat/linux/common/linux_sem.h>
     77      1.73  christos #include <compat/linux/common/linux_errno.h>
     78      1.73  christos #include <compat/linux/common/linux_sched.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.73  christos void
    163      1.73  christos native_to_linux_siginfo(linux_siginfo_t *lsi, const struct _ksiginfo *ksi)
    164      1.73  christos {
    165      1.73  christos 	memset(lsi, 0, sizeof(*lsi));
    166      1.73  christos 
    167      1.73  christos 	lsi->lsi_signo = native_to_linux_signo[ksi->_signo];
    168      1.73  christos 	lsi->lsi_errno = native_to_linux_errno[ksi->_errno];
    169      1.73  christos 	lsi->lsi_code = native_to_linux_si_code(ksi->_code);
    170      1.73  christos 
    171      1.73  christos 	switch (ksi->_code) {
    172      1.73  christos 	case SI_NOINFO:
    173      1.73  christos 		break;
    174      1.73  christos 
    175      1.73  christos 	case SI_USER:
    176      1.73  christos 		lsi->lsi_pid = ksi->_reason._rt._pid;
    177      1.73  christos 		lsi->lsi_uid = ksi->_reason._rt._uid;
    178      1.73  christos 		if (lsi->lsi_signo == LINUX_SIGALRM ||
    179      1.73  christos 		    lsi->lsi_signo >= LINUX_SIGRTMIN)
    180      1.73  christos 			lsi->lsi_value.sival_ptr =
    181      1.73  christos 			    ksi->_reason._rt._value.sival_ptr;
    182      1.73  christos 		break;
    183      1.73  christos 
    184      1.73  christos 	case SI_TIMER:
    185      1.73  christos 	case SI_QUEUE:
    186      1.73  christos 		lsi->lsi_uid = ksi->_reason._rt._uid;
    187      1.73  christos 		lsi->lsi_uid = ksi->_reason._rt._uid;
    188      1.73  christos 		lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr;
    189      1.73  christos 		break;
    190      1.73  christos 
    191      1.73  christos 	case SI_ASYNCIO:
    192      1.73  christos 	case SI_MESGQ:
    193      1.73  christos 		lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr;
    194      1.73  christos 		break;
    195      1.73  christos 
    196      1.73  christos 	default:
    197      1.73  christos 		switch (ksi->_signo) {
    198      1.73  christos 		case SIGCHLD:
    199      1.73  christos 			lsi->lsi_uid = ksi->_reason._child._uid;
    200      1.73  christos 			lsi->lsi_pid = ksi->_reason._child._pid;
    201      1.73  christos 			lsi->lsi_status = native_to_linux_si_status(
    202      1.73  christos 			    ksi->_code, ksi->_reason._child._status);
    203      1.73  christos 			lsi->lsi_utime = ksi->_reason._child._utime;
    204      1.73  christos 			lsi->lsi_stime = ksi->_reason._child._stime;
    205      1.73  christos 			break;
    206      1.73  christos 
    207      1.73  christos 		case SIGILL:
    208      1.73  christos 		case SIGFPE:
    209      1.73  christos 		case SIGSEGV:
    210      1.73  christos 		case SIGBUS:
    211      1.73  christos 		case SIGTRAP:
    212      1.73  christos 			lsi->lsi_addr = ksi->_reason._fault._addr;
    213      1.73  christos 			break;
    214      1.73  christos 
    215      1.73  christos 		case SIGIO:
    216      1.73  christos 			lsi->lsi_fd = ksi->_reason._poll._fd;
    217      1.73  christos 			lsi->lsi_band = ksi->_reason._poll._band;
    218      1.73  christos 			break;
    219      1.73  christos 		default:
    220      1.73  christos 			break;
    221      1.73  christos 		}
    222      1.73  christos 	}
    223      1.73  christos }
    224      1.73  christos 
    225      1.34  christos unsigned int
    226      1.57       dsl native_to_linux_sigflags(const int bsf)
    227      1.33  christos {
    228      1.34  christos 	unsigned int lsf = 0;
    229      1.34  christos 	if ((bsf & SA_NOCLDSTOP) != 0)
    230      1.34  christos 		lsf |= LINUX_SA_NOCLDSTOP;
    231      1.34  christos 	if ((bsf & SA_NOCLDWAIT) != 0)
    232      1.34  christos 		lsf |= LINUX_SA_NOCLDWAIT;
    233      1.34  christos 	if ((bsf & SA_ONSTACK) != 0)
    234      1.34  christos 		lsf |= LINUX_SA_ONSTACK;
    235      1.34  christos 	if ((bsf & SA_RESTART) != 0)
    236      1.34  christos 		lsf |= LINUX_SA_RESTART;
    237      1.34  christos 	if ((bsf & SA_NODEFER) != 0)
    238      1.34  christos 		lsf |= LINUX_SA_NOMASK;
    239      1.34  christos 	if ((bsf & SA_RESETHAND) != 0)
    240      1.34  christos 		lsf |= LINUX_SA_ONESHOT;
    241      1.34  christos 	if ((bsf & SA_SIGINFO) != 0)
    242      1.34  christos 		lsf |= LINUX_SA_SIGINFO;
    243      1.34  christos 	return lsf;
    244      1.33  christos }
    245      1.33  christos 
    246      1.34  christos int
    247      1.57       dsl linux_to_native_sigflags(const unsigned long lsf)
    248      1.33  christos {
    249      1.34  christos 	int bsf = 0;
    250      1.34  christos 	if ((lsf & LINUX_SA_NOCLDSTOP) != 0)
    251      1.34  christos 		bsf |= SA_NOCLDSTOP;
    252      1.34  christos 	if ((lsf & LINUX_SA_NOCLDWAIT) != 0)
    253      1.34  christos 		bsf |= SA_NOCLDWAIT;
    254      1.34  christos 	if ((lsf & LINUX_SA_ONSTACK) != 0)
    255      1.34  christos 		bsf |= SA_ONSTACK;
    256      1.34  christos 	if ((lsf & LINUX_SA_RESTART) != 0)
    257      1.34  christos 		bsf |= SA_RESTART;
    258      1.34  christos 	if ((lsf & LINUX_SA_ONESHOT) != 0)
    259      1.34  christos 		bsf |= SA_RESETHAND;
    260      1.34  christos 	if ((lsf & LINUX_SA_NOMASK) != 0)
    261      1.34  christos 		bsf |= SA_NODEFER;
    262      1.34  christos 	if ((lsf & LINUX_SA_SIGINFO) != 0)
    263      1.34  christos 		bsf |= SA_SIGINFO;
    264      1.50  christos 	if ((lsf & ~LINUX_SA_ALLBITS) != 0) {
    265      1.36  christos 		DPRINTF(("linux_old_to_native_sigflags: "
    266      1.36  christos 		    "%lx extra bits ignored\n", lsf));
    267      1.50  christos 	}
    268      1.34  christos 	return bsf;
    269      1.33  christos }
    270      1.33  christos 
    271      1.86       ryo #if !defined(__aarch64__)
    272       1.1      fvdl /*
    273      1.46      manu  * Convert between Linux and BSD sigaction structures.
    274       1.1      fvdl  */
    275       1.1      fvdl void
    276      1.57       dsl linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa)
    277      1.14       erh {
    278      1.84  riastrad 
    279      1.84  riastrad 	memset(bsa, 0, sizeof(*bsa));
    280      1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    281      1.39  christos 	linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    282      1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    283      1.14       erh }
    284      1.14       erh 
    285      1.14       erh void
    286      1.57       dsl native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa)
    287      1.14       erh {
    288      1.84  riastrad 
    289      1.84  riastrad 	memset(lsa, 0, sizeof(*lsa));
    290      1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    291      1.39  christos 	native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    292      1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    293      1.34  christos #ifndef __alpha__
    294      1.39  christos 	lsa->linux_sa_restorer = NULL;
    295      1.34  christos #endif
    296      1.14       erh }
    297      1.86       ryo #endif
    298      1.14       erh 
    299      1.14       erh /* ...and the new sigaction conversion funcs. */
    300      1.14       erh void
    301      1.57       dsl linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa)
    302       1.1      fvdl {
    303      1.84  riastrad 
    304      1.84  riastrad 	memset(bsa, 0, sizeof(*bsa));
    305      1.39  christos 	bsa->sa_handler = lsa->linux_sa_handler;
    306      1.39  christos 	linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    307      1.39  christos 	bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags);
    308       1.1      fvdl }
    309       1.1      fvdl 
    310       1.1      fvdl void
    311      1.57       dsl native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa)
    312       1.1      fvdl {
    313      1.84  riastrad 
    314      1.84  riastrad 	memset(lsa, 0, sizeof(*lsa));
    315      1.39  christos 	lsa->linux_sa_handler = bsa->sa_handler;
    316      1.39  christos 	native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    317      1.39  christos 	lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags);
    318      1.34  christos #ifndef __alpha__
    319      1.39  christos 	lsa->linux_sa_restorer = NULL;
    320      1.34  christos #endif
    321       1.1      fvdl }
    322       1.1      fvdl 
    323      1.14       erh /* ----------------------------------------------------------------------- */
    324       1.1      fvdl 
    325       1.1      fvdl /*
    326       1.1      fvdl  * The Linux sigaction() system call. Do the usual conversions,
    327       1.1      fvdl  * and just call sigaction(). Some flags and values are silently
    328       1.1      fvdl  * ignored (see above).
    329       1.1      fvdl  */
    330       1.1      fvdl int
    331      1.58       dsl linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval)
    332       1.8   thorpej {
    333      1.58       dsl 	/* {
    334       1.1      fvdl 		syscallarg(int) signum;
    335      1.12   mycroft 		syscallarg(const struct linux_sigaction *) nsa;
    336       1.1      fvdl 		syscallarg(struct linux_sigaction *) osa;
    337      1.14       erh 		syscallarg(size_t) sigsetsize;
    338      1.58       dsl 	} */
    339      1.12   mycroft 	struct linux_sigaction nlsa, olsa;
    340      1.12   mycroft 	struct sigaction nbsa, obsa;
    341      1.25      tron 	int error, sig;
    342      1.46      manu 	void *tramp = NULL;
    343      1.46      manu 	int vers = 0;
    344      1.77  christos #ifdef LINUX_SA_RESTORER
    345      1.53        ad 	struct sigacts *ps = l->l_proc->p_sigacts;
    346      1.46      manu #endif
    347       1.1      fvdl 
    348      1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    349      1.79       rin 		return EINVAL;
    350      1.14       erh 
    351      1.12   mycroft 	if (SCARG(uap, nsa)) {
    352      1.12   mycroft 		error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa));
    353      1.12   mycroft 		if (error)
    354      1.79       rin 			return error;
    355      1.33  christos 		linux_to_native_sigaction(&nbsa, &nlsa);
    356      1.12   mycroft 	}
    357      1.46      manu 
    358      1.25      tron 	sig = SCARG(uap, signum);
    359      1.80  christos 	/*
    360      1.80  christos 	 * XXX: Linux has 33 realtime signals, the go binary wants to
    361      1.80  christos 	 * reset all of them; nothing else uses the last RT signal, so for
    362      1.80  christos 	 * now ignore it.
    363      1.80  christos 	 */
    364      1.80  christos 	if (sig == LINUX__NSIG) {
    365      1.80  christos 		uprintf("%s: setting signal %d ignored\n", __func__, sig);
    366      1.80  christos 		sig--;	/* back to 63 which is ignored */
    367      1.80  christos 	}
    368      1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    369      1.79       rin 		return EINVAL;
    370      1.37  christos 	if (sig > 0 && !linux_to_native_signo[sig]) {
    371      1.29        tv 		/* Pretend that we did something useful for unknown signals. */
    372      1.29        tv 		obsa.sa_handler = SIG_IGN;
    373      1.29        tv 		sigemptyset(&obsa.sa_mask);
    374      1.29        tv 		obsa.sa_flags = 0;
    375      1.29        tv 	} else {
    376      1.77  christos #ifdef LINUX_SA_RESTORER
    377      1.78       rin 		if (SCARG(uap, nsa) &&
    378      1.78       rin 		    (nlsa.linux_sa_flags & LINUX_SA_RESTORER) &&
    379      1.77  christos 		    (tramp = nlsa.linux_sa_restorer) != NULL)
    380      1.79       rin 			vers = 2;
    381      1.46      manu #endif
    382      1.46      manu 
    383      1.53        ad 		error = sigaction1(l, linux_to_native_signo[sig],
    384      1.38   thorpej 		    SCARG(uap, nsa) ? &nbsa : NULL,
    385      1.38   thorpej 		    SCARG(uap, osa) ? &obsa : NULL,
    386      1.46      manu 		    tramp, vers);
    387      1.29        tv 		if (error)
    388      1.79       rin 			return error;
    389      1.29        tv 	}
    390      1.12   mycroft 	if (SCARG(uap, osa)) {
    391      1.33  christos 		native_to_linux_sigaction(&olsa, &obsa);
    392      1.46      manu 
    393      1.77  christos #ifdef LINUX_SA_RESTORER
    394      1.87   thorpej 		if (ps->sa_sigdesc[sig].sd_vers != __SIGTRAMP_SIGCODE_VERSION) {
    395      1.46      manu 			olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp;
    396      1.46      manu 			olsa.linux_sa_flags |= LINUX_SA_RESTORER;
    397      1.46      manu 		}
    398      1.46      manu #endif
    399      1.46      manu 
    400      1.12   mycroft 		error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa));
    401      1.12   mycroft 		if (error)
    402      1.79       rin 			return error;
    403       1.1      fvdl 	}
    404      1.79       rin 	return 0;
    405       1.1      fvdl }
    406       1.1      fvdl 
    407      1.86       ryo #if !defined(__aarch64__)
    408       1.1      fvdl int
    409      1.57       dsl linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset)
    410       1.8   thorpej {
    411      1.53        ad 	struct proc *p = l->l_proc;
    412      1.14       erh 	linux_old_sigset_t nlss, olss;
    413      1.12   mycroft 	sigset_t nbss, obss;
    414      1.12   mycroft 	int error;
    415       1.1      fvdl 
    416      1.17       erh 	switch (how) {
    417       1.1      fvdl 	case LINUX_SIG_BLOCK:
    418      1.12   mycroft 		how = SIG_BLOCK;
    419       1.1      fvdl 		break;
    420       1.1      fvdl 	case LINUX_SIG_UNBLOCK:
    421      1.12   mycroft 		how = SIG_UNBLOCK;
    422       1.1      fvdl 		break;
    423       1.1      fvdl 	case LINUX_SIG_SETMASK:
    424      1.12   mycroft 		how = SIG_SETMASK;
    425       1.1      fvdl 		break;
    426       1.1      fvdl 	default:
    427      1.79       rin 		return EINVAL;
    428       1.1      fvdl 	}
    429       1.1      fvdl 
    430      1.17       erh 	if (set) {
    431      1.17       erh 		error = copyin(set, &nlss, sizeof(nlss));
    432      1.12   mycroft 		if (error)
    433      1.79       rin 			return error;
    434      1.33  christos 		linux_old_to_native_sigset(&nbss, &nlss);
    435      1.12   mycroft 	}
    436      1.61        ad 	mutex_enter(p->p_lock);
    437      1.53        ad 	error = sigprocmask1(l, how,
    438      1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    439      1.61        ad 	mutex_exit(p->p_lock);
    440      1.12   mycroft 	if (error)
    441      1.79       rin 		return error;
    442      1.17       erh 	if (oset) {
    443      1.33  christos 		native_to_linux_old_sigset(&olss, &obss);
    444      1.17       erh 		error = copyout(&olss, oset, sizeof(olss));
    445      1.12   mycroft 		if (error)
    446      1.79       rin 			return error;
    447      1.42     perry 	}
    448      1.79       rin 	return error;
    449      1.17       erh }
    450      1.86       ryo #endif
    451      1.17       erh 
    452      1.17       erh int
    453      1.58       dsl linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval)
    454      1.17       erh {
    455      1.58       dsl 	/* {
    456      1.17       erh 		syscallarg(int) how;
    457      1.17       erh 		syscallarg(const linux_sigset_t *) set;
    458      1.17       erh 		syscallarg(linux_sigset_t *) oset;
    459      1.17       erh 		syscallarg(size_t) sigsetsize;
    460      1.58       dsl 	} */
    461      1.24      fvdl 	linux_sigset_t nlss, olss, *oset;
    462      1.24      fvdl 	const linux_sigset_t *set;
    463      1.53        ad 	struct proc *p = l->l_proc;
    464      1.24      fvdl 	sigset_t nbss, obss;
    465      1.24      fvdl 	int error, how;
    466      1.24      fvdl 
    467      1.24      fvdl 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    468      1.79       rin 		return EINVAL;
    469      1.24      fvdl 
    470      1.24      fvdl 	switch (SCARG(uap, how)) {
    471      1.24      fvdl 	case LINUX_SIG_BLOCK:
    472      1.24      fvdl 		how = SIG_BLOCK;
    473      1.24      fvdl 		break;
    474      1.24      fvdl 	case LINUX_SIG_UNBLOCK:
    475      1.24      fvdl 		how = SIG_UNBLOCK;
    476      1.24      fvdl 		break;
    477      1.24      fvdl 	case LINUX_SIG_SETMASK:
    478      1.24      fvdl 		how = SIG_SETMASK;
    479      1.24      fvdl 		break;
    480      1.24      fvdl 	default:
    481      1.79       rin 		return EINVAL;
    482      1.17       erh 	}
    483      1.17       erh 
    484      1.24      fvdl 	set = SCARG(uap, set);
    485      1.24      fvdl 	oset = SCARG(uap, oset);
    486      1.24      fvdl 
    487      1.24      fvdl 	if (set) {
    488      1.24      fvdl 		error = copyin(set, &nlss, sizeof(nlss));
    489      1.24      fvdl 		if (error)
    490      1.79       rin 			return error;
    491      1.33  christos 		linux_to_native_sigset(&nbss, &nlss);
    492      1.24      fvdl 	}
    493      1.61        ad 	mutex_enter(p->p_lock);
    494      1.53        ad 	error = sigprocmask1(l, how,
    495      1.27      tron 	    set ? &nbss : NULL, oset ? &obss : NULL);
    496      1.61        ad 	mutex_exit(p->p_lock);
    497      1.28      tron 	if (!error && oset) {
    498      1.33  christos 		native_to_linux_sigset(&olss, &obss);
    499      1.24      fvdl 		error = copyout(&olss, oset, sizeof(olss));
    500      1.42     perry 	}
    501      1.79       rin 	return error;
    502       1.1      fvdl }
    503       1.1      fvdl 
    504       1.1      fvdl int
    505      1.58       dsl linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval)
    506       1.1      fvdl {
    507      1.58       dsl 	/* {
    508      1.14       erh 		syscallarg(linux_sigset_t *) set;
    509      1.14       erh 		syscallarg(size_t) sigsetsize;
    510      1.58       dsl 	} */
    511      1.12   mycroft 	sigset_t bss;
    512      1.12   mycroft 	linux_sigset_t lss;
    513       1.6   mycroft 
    514      1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    515      1.79       rin 		return EINVAL;
    516      1.14       erh 
    517      1.53        ad 	sigpending1(l, &bss);
    518      1.33  christos 	native_to_linux_sigset(&lss, &bss);
    519      1.14       erh 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    520       1.1      fvdl }
    521      1.24      fvdl 
    522      1.86       ryo #if !defined(__aarch64__) && !defined(__amd64__)
    523       1.1      fvdl int
    524      1.58       dsl linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval)
    525       1.8   thorpej {
    526      1.58       dsl 	/* {
    527      1.14       erh 		syscallarg(linux_old_sigset_t *) mask;
    528      1.58       dsl 	} */
    529      1.12   mycroft 	sigset_t bss;
    530      1.14       erh 	linux_old_sigset_t lss;
    531       1.1      fvdl 
    532      1.53        ad 	sigpending1(l, &bss);
    533      1.33  christos 	native_to_linux_old_sigset(&lss, &bss);
    534      1.12   mycroft 	return copyout(&lss, SCARG(uap, set), sizeof(lss));
    535       1.1      fvdl }
    536       1.1      fvdl 
    537       1.1      fvdl int
    538      1.58       dsl linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval)
    539       1.8   thorpej {
    540      1.58       dsl 	/* {
    541      1.54  christos 		syscallarg(void *) restart;
    542       1.3      fvdl 		syscallarg(int) oldmask;
    543       1.1      fvdl 		syscallarg(int) mask;
    544      1.58       dsl 	} */
    545      1.14       erh 	linux_old_sigset_t lss;
    546      1.14       erh 	sigset_t bss;
    547      1.14       erh 
    548      1.14       erh 	lss = SCARG(uap, mask);
    549      1.33  christos 	linux_old_to_native_sigset(&bss, &lss);
    550      1.79       rin 	return sigsuspend1(l, &bss);
    551      1.14       erh }
    552      1.86       ryo #endif /* !__aarch64__ && !__amd64__ */
    553      1.43      manu 
    554      1.14       erh int
    555      1.58       dsl linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval)
    556      1.14       erh {
    557      1.58       dsl 	/* {
    558      1.14       erh 		syscallarg(linux_sigset_t *) unewset;
    559      1.14       erh 		syscallarg(size_t) sigsetsize;
    560      1.58       dsl 	} */
    561      1.12   mycroft 	linux_sigset_t lss;
    562      1.12   mycroft 	sigset_t bss;
    563      1.14       erh 	int error;
    564      1.14       erh 
    565      1.14       erh 	if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t))
    566      1.79       rin 		return EINVAL;
    567      1.14       erh 
    568      1.14       erh 	error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t));
    569      1.14       erh 	if (error)
    570      1.79       rin 		return error;
    571      1.14       erh 
    572      1.33  christos 	linux_to_native_sigset(&bss, &lss);
    573      1.14       erh 
    574      1.79       rin 	return sigsuspend1(l, &bss);
    575       1.3      fvdl }
    576      1.75  christos 
    577      1.73  christos static int
    578      1.73  christos fetchss(const void *u, void *s, size_t len)
    579      1.73  christos {
    580      1.73  christos 	int error;
    581      1.73  christos 	linux_sigset_t lss;
    582  1.89.6.1  perseant 
    583      1.73  christos 	if ((error = copyin(u, &lss, sizeof(lss))) != 0)
    584      1.73  christos 		return error;
    585      1.73  christos 
    586      1.73  christos 	linux_to_native_sigset(s, &lss);
    587      1.73  christos 	return 0;
    588      1.73  christos }
    589      1.73  christos 
    590      1.73  christos static int
    591      1.73  christos fetchts(const void *u, void *s, size_t len)
    592      1.73  christos {
    593      1.73  christos 	int error;
    594      1.73  christos 	struct linux_timespec lts;
    595  1.89.6.1  perseant 
    596      1.73  christos 	if ((error = copyin(u, &lts, sizeof(lts))) != 0)
    597      1.73  christos 		return error;
    598      1.73  christos 
    599      1.73  christos 	linux_to_native_timespec(s, &lts);
    600      1.73  christos 	return 0;
    601      1.73  christos }
    602      1.73  christos 
    603      1.73  christos static int
    604      1.73  christos fakestorets(const void *u, void *s, size_t len)
    605      1.73  christos {
    606      1.73  christos 	/* Do nothing, sigtimedwait does not alter timeout like ours */
    607      1.73  christos 	return 0;
    608      1.73  christos }
    609      1.73  christos 
    610      1.73  christos static int
    611      1.73  christos storeinfo(const void *s, void *u, size_t len)
    612      1.73  christos {
    613      1.73  christos 	struct linux_siginfo lsi;
    614      1.73  christos 
    615      1.73  christos 	native_to_linux_siginfo(&lsi, &((const siginfo_t *)s)->_info);
    616      1.73  christos 	return copyout(&lsi, u, sizeof(lsi));
    617      1.73  christos }
    618      1.73  christos 
    619      1.73  christos int
    620      1.73  christos linux_sys_rt_sigtimedwait(struct lwp *l,
    621      1.73  christos     const struct linux_sys_rt_sigtimedwait_args *uap, register_t *retval)
    622      1.73  christos {
    623      1.73  christos 	/* {
    624      1.73  christos 		syscallarg(const linux_sigset_t *) set;
    625      1.73  christos 		syscallarg(linux_siginfo_t *) info);
    626      1.73  christos 		syscallarg(const struct linux_timespec *) timeout;
    627      1.73  christos 	} */
    628      1.73  christos 
    629      1.73  christos 	return sigtimedwait1(l, (const struct sys_____sigtimedwait50_args *)uap,
    630      1.73  christos 	    retval, fetchss, storeinfo, fetchts, fakestorets);
    631      1.73  christos }
    632       1.3      fvdl 
    633      1.86       ryo #if !defined(__aarch64__)
    634       1.3      fvdl /*
    635      1.14       erh  * Once more: only a signal conversion is needed.
    636      1.14       erh  * Note: also used as sys_rt_queueinfo.  The info field is ignored.
    637       1.3      fvdl  */
    638       1.3      fvdl int
    639      1.58       dsl linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval)
    640      1.14       erh {
    641      1.69     njoly 	/*
    642      1.14       erh 		syscallarg(int) pid;
    643      1.14       erh 		syscallarg(int) signum;
    644      1.69     njoly 		syscallarg(linix_siginfo_t *) uinfo;
    645      1.69     njoly 	*/
    646      1.69     njoly 	int error;
    647      1.69     njoly 	linux_siginfo_t info;
    648      1.69     njoly 
    649      1.69     njoly 	error = copyin(SCARG(uap, uinfo), &info, sizeof(info));
    650      1.69     njoly 	if (error)
    651      1.69     njoly 		return error;
    652      1.69     njoly 	if (info.lsi_code >= 0)
    653      1.69     njoly 		return EPERM;
    654       1.3      fvdl 
    655      1.14       erh 	/* XXX To really implement this we need to	*/
    656      1.14       erh 	/* XXX keep a list of queued signals somewhere.	*/
    657      1.79       rin 	return linux_sys_kill(l, (const void *)uap, retval);
    658       1.1      fvdl }
    659      1.86       ryo #endif
    660       1.1      fvdl 
    661       1.1      fvdl int
    662      1.58       dsl linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval)
    663       1.8   thorpej {
    664      1.58       dsl 	/* {
    665       1.1      fvdl 		syscallarg(int) pid;
    666       1.1      fvdl 		syscallarg(int) signum;
    667      1.58       dsl 	} */
    668      1.40   thorpej 
    669       1.9   mycroft 	struct sys_kill_args ka;
    670      1.25      tron 	int sig;
    671       1.6   mycroft 
    672       1.6   mycroft 	SCARG(&ka, pid) = SCARG(uap, pid);
    673      1.25      tron 	sig = SCARG(uap, signum);
    674      1.25      tron 	if (sig < 0 || sig >= LINUX__NSIG)
    675      1.79       rin 		return EINVAL;
    676      1.37  christos 	SCARG(&ka, signum) = linux_to_native_signo[sig];
    677      1.40   thorpej 	return sys_kill(l, &ka, retval);
    678       1.1      fvdl }
    679      1.30  christos 
    680      1.30  christos #ifdef LINUX_SS_ONSTACK
    681      1.88   thorpej static void linux_to_native_sigaltstack(stack_t *,
    682      1.56       dsl     const struct linux_sigaltstack *);
    683      1.30  christos 
    684      1.30  christos static void
    685      1.88   thorpej linux_to_native_sigaltstack(stack_t *bss, const struct linux_sigaltstack *lss)
    686      1.30  christos {
    687      1.30  christos 	bss->ss_sp = lss->ss_sp;
    688      1.30  christos 	bss->ss_size = lss->ss_size;
    689      1.30  christos 	if (lss->ss_flags & LINUX_SS_ONSTACK)
    690      1.30  christos 	    bss->ss_flags = SS_ONSTACK;
    691      1.30  christos 	else if (lss->ss_flags & LINUX_SS_DISABLE)
    692      1.30  christos 	    bss->ss_flags = SS_DISABLE;
    693      1.30  christos 	else
    694      1.30  christos 	    bss->ss_flags = 0;
    695      1.30  christos }
    696      1.30  christos 
    697      1.41  christos void
    698      1.88   thorpej native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const stack_t *bss)
    699      1.30  christos {
    700      1.81      maxv 	memset(lss, 0, sizeof(*lss));
    701      1.30  christos 	lss->ss_sp = bss->ss_sp;
    702      1.30  christos 	lss->ss_size = bss->ss_size;
    703      1.30  christos 	if (bss->ss_flags & SS_ONSTACK)
    704      1.30  christos 	    lss->ss_flags = LINUX_SS_ONSTACK;
    705      1.30  christos 	else if (bss->ss_flags & SS_DISABLE)
    706      1.30  christos 	    lss->ss_flags = LINUX_SS_DISABLE;
    707      1.30  christos 	else
    708      1.30  christos 	    lss->ss_flags = 0;
    709      1.30  christos }
    710      1.30  christos 
    711      1.30  christos int
    712      1.58       dsl linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval)
    713      1.30  christos {
    714      1.58       dsl 	/* {
    715      1.30  christos 		syscallarg(const struct linux_sigaltstack *) ss;
    716      1.30  christos 		syscallarg(struct linux_sigaltstack *) oss;
    717      1.58       dsl 	} */
    718      1.30  christos 	struct linux_sigaltstack ss;
    719      1.88   thorpej 	stack_t nss;
    720      1.53        ad 	struct proc *p = l->l_proc;
    721      1.53        ad 	int error = 0;
    722      1.30  christos 
    723      1.48  christos 	if (SCARG(uap, oss)) {
    724      1.53        ad 		native_to_linux_sigaltstack(&ss, &l->l_sigstk);
    725      1.48  christos 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0)
    726      1.48  christos 			return error;
    727      1.48  christos 	}
    728      1.48  christos 
    729      1.30  christos 	if (SCARG(uap, ss) != NULL) {
    730      1.30  christos 		if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0)
    731      1.30  christos 			return error;
    732      1.30  christos 		linux_to_native_sigaltstack(&nss, &ss);
    733      1.30  christos 
    734      1.61        ad 		mutex_enter(p->p_lock);
    735      1.53        ad 
    736      1.48  christos 		if (nss.ss_flags & ~SS_ALLBITS)
    737      1.53        ad 			error = EINVAL;
    738      1.53        ad 		else if (nss.ss_flags & SS_DISABLE) {
    739      1.53        ad 			if (l->l_sigstk.ss_flags & SS_ONSTACK)
    740      1.53        ad 				error = EINVAL;
    741      1.53        ad 		} else if (nss.ss_size < LINUX_MINSIGSTKSZ)
    742      1.53        ad 			error = ENOMEM;
    743      1.30  christos 
    744      1.53        ad 		if (error == 0)
    745      1.53        ad 			l->l_sigstk = nss;
    746      1.53        ad 
    747      1.61        ad 		mutex_exit(p->p_lock);
    748      1.30  christos 	}
    749      1.48  christos 
    750      1.53        ad 	return error;
    751      1.30  christos }
    752      1.44       jmc #endif /* LINUX_SS_ONSTACK */
    753      1.49      manu 
    754      1.65     njoly static int
    755      1.65     njoly linux_do_tkill(struct lwp *l, int tgid, int tid, int signum)
    756      1.65     njoly {
    757      1.65     njoly 	struct proc *p;
    758      1.71       chs 	struct lwp *t;
    759      1.71       chs 	ksiginfo_t ksi;
    760      1.65     njoly 	int error;
    761      1.65     njoly 
    762      1.65     njoly 	if (signum < 0 || signum >= LINUX__NSIG)
    763      1.65     njoly 		return EINVAL;
    764      1.65     njoly 	signum = linux_to_native_signo[signum];
    765      1.65     njoly 
    766      1.65     njoly 	KSI_INIT(&ksi);
    767      1.65     njoly 	ksi.ksi_signo = signum;
    768      1.65     njoly 	ksi.ksi_code = SI_LWP;
    769      1.65     njoly 	ksi.ksi_pid = l->l_proc->p_pid;
    770      1.65     njoly 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    771      1.71       chs 	ksi.ksi_lid = tid;
    772      1.65     njoly 
    773      1.83        ad 	mutex_enter(&proc_lock);
    774      1.82   thorpej 	if (tgid != -1)
    775      1.82   thorpej 		p = proc_find(tgid);
    776      1.82   thorpej 	else
    777      1.82   thorpej 		p = proc_find_lwpid(tid);
    778      1.71       chs 	if (p == NULL) {
    779      1.83        ad 		mutex_exit(&proc_lock);
    780      1.65     njoly 		return ESRCH;
    781      1.65     njoly 	}
    782      1.65     njoly 	mutex_enter(p->p_lock);
    783      1.65     njoly 	error = kauth_authorize_process(l->l_cred,
    784      1.65     njoly 	    KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL);
    785      1.71       chs 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    786      1.71       chs 		error = ESRCH;
    787      1.71       chs 	else if (signum != 0)
    788      1.65     njoly 		kpsignal2(p, &ksi);
    789      1.65     njoly 	mutex_exit(p->p_lock);
    790      1.83        ad 	mutex_exit(&proc_lock);
    791      1.65     njoly 
    792      1.65     njoly 	return error;
    793      1.65     njoly }
    794      1.65     njoly 
    795      1.49      manu int
    796      1.58       dsl linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval)
    797      1.49      manu {
    798      1.58       dsl 	/* {
    799      1.49      manu 		syscallarg(int) tid;
    800      1.49      manu 		syscallarg(int) sig;
    801      1.58       dsl 	} */
    802      1.49      manu 
    803      1.65     njoly 	if (SCARG(uap, tid) <= 0)
    804      1.65     njoly 		return EINVAL;
    805      1.49      manu 
    806      1.71       chs 	return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig));
    807      1.49      manu }
    808      1.49      manu 
    809      1.49      manu int
    810      1.58       dsl linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval)
    811      1.49      manu {
    812      1.58       dsl 	/* {
    813      1.49      manu 		syscallarg(int) tgid;
    814      1.49      manu 		syscallarg(int) tid;
    815      1.49      manu 		syscallarg(int) sig;
    816      1.58       dsl 	} */
    817      1.49      manu 
    818      1.71       chs 	if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1)
    819      1.65     njoly 		return EINVAL;
    820      1.49      manu 
    821      1.65     njoly 	return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig));
    822      1.49      manu }
    823      1.64     njoly 
    824      1.64     njoly int
    825      1.64     njoly native_to_linux_si_code(int code)
    826      1.64     njoly {
    827      1.64     njoly 	int si_codes[] = {
    828      1.64     njoly 	    LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO,
    829      1.64     njoly 	    LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */
    830      1.64     njoly 	};
    831      1.64     njoly 
    832      1.64     njoly 	if (code <= 0 && -code < __arraycount(si_codes))
    833      1.64     njoly 		return si_codes[-code];
    834      1.64     njoly 
    835      1.64     njoly 	return code;
    836      1.64     njoly }
    837      1.68     njoly 
    838      1.68     njoly int
    839      1.68     njoly native_to_linux_si_status(int code, int status)
    840      1.68     njoly {
    841      1.68     njoly 	int sts;
    842      1.68     njoly 
    843      1.68     njoly 	switch (code) {
    844      1.68     njoly 	case CLD_CONTINUED:
    845      1.68     njoly 		sts = LINUX_SIGCONT;
    846      1.68     njoly 		break;
    847      1.68     njoly 	case CLD_EXITED:
    848      1.89  christos 		sts = status;
    849      1.68     njoly 		break;
    850      1.68     njoly 	case CLD_STOPPED:
    851      1.68     njoly 	case CLD_TRAPPED:
    852      1.68     njoly 	case CLD_DUMPED:
    853      1.68     njoly 	case CLD_KILLED:
    854      1.68     njoly 	default:
    855      1.68     njoly 		sts = native_to_linux_signo[WTERMSIG(status)];
    856      1.68     njoly 		break;
    857      1.68     njoly 	}
    858      1.68     njoly 
    859      1.68     njoly 	return sts;
    860      1.68     njoly }
    861      1.85   thorpej 
    862      1.85   thorpej int
    863      1.85   thorpej linux_to_native_sigevent(struct sigevent *nsep,
    864      1.85   thorpej     const struct linux_sigevent *lsep)
    865      1.85   thorpej {
    866      1.85   thorpej 	memset(nsep, 0, sizeof(*nsep));
    867      1.85   thorpej 
    868      1.85   thorpej 	switch (lsep->sigev_notify) {
    869      1.85   thorpej 	case LINUX_SIGEV_SIGNAL:
    870      1.85   thorpej 		nsep->sigev_notify = SIGEV_SIGNAL;
    871      1.85   thorpej 		break;
    872      1.85   thorpej 
    873      1.85   thorpej 	case LINUX_SIGEV_NONE:
    874      1.85   thorpej 		nsep->sigev_notify = SIGEV_NONE;
    875      1.85   thorpej 		break;
    876      1.85   thorpej 
    877      1.85   thorpej 	case LINUX_SIGEV_THREAD:
    878      1.85   thorpej 	case LINUX_SIGEV_THREAD_ID:
    879      1.85   thorpej 	default:
    880      1.85   thorpej 		return ENOTSUP;
    881      1.85   thorpej 	}
    882      1.85   thorpej 
    883      1.85   thorpej 	nsep->sigev_value = lsep->sigev_value;
    884      1.85   thorpej 	if (lsep->sigev_signo < 0 || lsep->sigev_signo >= LINUX__NSIG) {
    885      1.85   thorpej 		return EINVAL;
    886      1.85   thorpej 	}
    887      1.85   thorpej 	nsep->sigev_signo = linux_to_native_signo[lsep->sigev_signo];
    888      1.85   thorpej 
    889      1.85   thorpej 	return 0;
    890      1.85   thorpej }
    891      1.85   thorpej 
    892      1.85   thorpej int
    893      1.85   thorpej linux_sigevent_copyin(const void *src, void *dst, size_t size)
    894      1.85   thorpej {
    895      1.85   thorpej 	struct linux_sigevent lse;
    896      1.85   thorpej 	struct sigevent *sep = dst;
    897      1.85   thorpej 	int error;
    898      1.85   thorpej 
    899      1.85   thorpej 	KASSERT(size == sizeof(*sep));
    900      1.85   thorpej 
    901      1.85   thorpej 	error = copyin(src, &lse, sizeof(lse));
    902      1.85   thorpej 	if (error) {
    903      1.85   thorpej 		return error;
    904      1.85   thorpej 	}
    905      1.85   thorpej 
    906      1.85   thorpej 	return linux_to_native_sigevent(sep, &lse);
    907      1.85   thorpej }
    908