Home | History | Annotate | Line # | Download | only in common
linux32_signal.c revision 1.4
      1  1.4   dsl /*	$NetBSD: linux32_signal.c,v 1.4 2007/03/18 21:38:32 dsl Exp $ */
      2  1.1  manu 
      3  1.1  manu /*-
      4  1.1  manu  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5  1.1  manu  *
      6  1.1  manu  * Redistribution and use in source and binary forms, with or without
      7  1.1  manu  * modification, are permitted provided that the following conditions
      8  1.1  manu  * are met:
      9  1.1  manu  * 1. Redistributions of source code must retain the above copyright
     10  1.1  manu  *    notice, this list of conditions and the following disclaimer.
     11  1.1  manu  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  manu  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  manu  *    documentation and/or other materials provided with the distribution.
     14  1.1  manu  * 3. All advertising materials mentioning features or use of this software
     15  1.1  manu  *    must display the following acknowledgement:
     16  1.1  manu  *	This product includes software developed by Emmanuel Dreyfus
     17  1.1  manu  * 4. The name of the author may not be used to endorse or promote
     18  1.1  manu  *    products derived from this software without specific prior written
     19  1.1  manu  *    permission.
     20  1.1  manu  *
     21  1.1  manu  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22  1.1  manu  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23  1.1  manu  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1  manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25  1.1  manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1  manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.1  manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1  manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1  manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1  manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1  manu  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1  manu  */
     33  1.1  manu #include <sys/param.h>
     34  1.1  manu #include <sys/ucred.h>
     35  1.1  manu #include <sys/signalvar.h>
     36  1.1  manu #include <sys/lwp.h>
     37  1.1  manu #include <sys/time.h>
     38  1.2    ad #include <sys/proc.h>
     39  1.1  manu 
     40  1.1  manu #include <compat/netbsd32/netbsd32.h>
     41  1.1  manu 
     42  1.1  manu #include <compat/linux32/common/linux32_types.h>
     43  1.1  manu #include <compat/linux32/common/linux32_signal.h>
     44  1.1  manu #include <compat/linux32/linux32_syscallargs.h>
     45  1.1  manu 
     46  1.1  manu #define linux32_sigemptyset(s)    memset((s), 0, sizeof(*(s)))
     47  1.1  manu #define linux32_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX32__NSIG_BPW]  \
     48  1.1  manu 				    & (1 << ((n) - 1) % LINUX32__NSIG_BPW))
     49  1.1  manu #define linux32_sigaddset(s, n)   ((s)->sig[((n) - 1) / LINUX32__NSIG_BPW]  \
     50  1.1  manu 				    |= (1 << ((n) - 1) % LINUX32__NSIG_BPW))
     51  1.1  manu 
     52  1.1  manu extern const int native_to_linux32_signo[];
     53  1.1  manu extern const int linux32_to_native_signo[];
     54  1.1  manu 
     55  1.1  manu void
     56  1.1  manu linux32_to_native_sigset(bss, lss)
     57  1.1  manu 	sigset_t *bss;
     58  1.1  manu 	const linux32_sigset_t *lss;
     59  1.1  manu {
     60  1.1  manu 	int i, newsig;
     61  1.1  manu 
     62  1.1  manu 	sigemptyset(bss);
     63  1.1  manu 	for (i = 1; i < LINUX32__NSIG; i++) {
     64  1.1  manu 		if (linux32_sigismember(lss, i)) {
     65  1.1  manu 			newsig = linux32_to_native_signo[i];
     66  1.1  manu 			if (newsig)
     67  1.1  manu 				sigaddset(bss, newsig);
     68  1.1  manu 		}
     69  1.1  manu 	}
     70  1.1  manu }
     71  1.1  manu 
     72  1.1  manu void
     73  1.1  manu native_to_linux32_sigset(lss, bss)
     74  1.1  manu 	linux32_sigset_t *lss;
     75  1.1  manu 	const sigset_t *bss;
     76  1.1  manu {
     77  1.1  manu 	int i, newsig;
     78  1.1  manu 
     79  1.1  manu 	linux32_sigemptyset(lss);
     80  1.1  manu 	for (i = 1; i < NSIG; i++) {
     81  1.1  manu 		if (sigismember(bss, i)) {
     82  1.1  manu 			newsig = native_to_linux32_signo[i];
     83  1.1  manu 			if (newsig)
     84  1.1  manu 				linux32_sigaddset(lss, newsig);
     85  1.1  manu 		}
     86  1.1  manu 	}
     87  1.1  manu }
     88  1.1  manu 
     89  1.1  manu unsigned int
     90  1.1  manu native_to_linux32_sigflags(bsf)
     91  1.1  manu 	const int bsf;
     92  1.1  manu {
     93  1.1  manu 	unsigned int lsf = 0;
     94  1.1  manu 	if ((bsf & SA_NOCLDSTOP) != 0)
     95  1.1  manu 		lsf |= LINUX32_SA_NOCLDSTOP;
     96  1.1  manu 	if ((bsf & SA_NOCLDWAIT) != 0)
     97  1.1  manu 		lsf |= LINUX32_SA_NOCLDWAIT;
     98  1.1  manu 	if ((bsf & SA_ONSTACK) != 0)
     99  1.1  manu 		lsf |= LINUX32_SA_ONSTACK;
    100  1.1  manu 	if ((bsf & SA_RESTART) != 0)
    101  1.1  manu 		lsf |= LINUX32_SA_RESTART;
    102  1.1  manu 	if ((bsf & SA_NODEFER) != 0)
    103  1.1  manu 		lsf |= LINUX32_SA_NOMASK;
    104  1.1  manu 	if ((bsf & SA_RESETHAND) != 0)
    105  1.1  manu 		lsf |= LINUX32_SA_ONESHOT;
    106  1.1  manu 	if ((bsf & SA_SIGINFO) != 0)
    107  1.1  manu 		lsf |= LINUX32_SA_SIGINFO;
    108  1.1  manu 	return lsf;
    109  1.1  manu }
    110  1.1  manu 
    111  1.1  manu int
    112  1.1  manu linux32_to_native_sigflags(lsf)
    113  1.1  manu 	const unsigned long lsf;
    114  1.1  manu {
    115  1.1  manu 	int bsf = 0;
    116  1.1  manu 	if ((lsf & LINUX32_SA_NOCLDSTOP) != 0)
    117  1.1  manu 		bsf |= SA_NOCLDSTOP;
    118  1.1  manu 	if ((lsf & LINUX32_SA_NOCLDWAIT) != 0)
    119  1.1  manu 		bsf |= SA_NOCLDWAIT;
    120  1.1  manu 	if ((lsf & LINUX32_SA_ONSTACK) != 0)
    121  1.1  manu 		bsf |= SA_ONSTACK;
    122  1.1  manu 	if ((lsf & LINUX32_SA_RESTART) != 0)
    123  1.1  manu 		bsf |= SA_RESTART;
    124  1.1  manu 	if ((lsf & LINUX32_SA_ONESHOT) != 0)
    125  1.1  manu 		bsf |= SA_RESETHAND;
    126  1.1  manu 	if ((lsf & LINUX32_SA_NOMASK) != 0)
    127  1.1  manu 		bsf |= SA_NODEFER;
    128  1.1  manu 	if ((lsf & LINUX32_SA_SIGINFO) != 0)
    129  1.1  manu 		bsf |= SA_SIGINFO;
    130  1.1  manu 	if ((lsf & ~LINUX32_SA_ALLBITS) != 0) {
    131  1.1  manu #ifdef DEBUG_LINUX
    132  1.1  manu 		printf("linux32_old_to_native_sigflags: "
    133  1.1  manu 		    "%lx extra bits ignored\n", lsf);
    134  1.1  manu #endif
    135  1.1  manu 	}
    136  1.1  manu 	return bsf;
    137  1.1  manu }
    138  1.1  manu 
    139  1.1  manu void
    140  1.1  manu linux32_to_native_sigaction(bsa, lsa)
    141  1.1  manu 	struct sigaction *bsa;
    142  1.1  manu 	const struct linux32_sigaction *lsa;
    143  1.1  manu {
    144  1.1  manu 	bsa->sa_handler = NETBSD32PTR64(lsa->linux_sa_handler);
    145  1.1  manu 	linux32_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    146  1.1  manu 	bsa->sa_flags = linux32_to_native_sigflags(lsa->linux_sa_flags);
    147  1.1  manu }
    148  1.1  manu 
    149  1.1  manu void
    150  1.1  manu native_to_linux32_sigaction(lsa, bsa)
    151  1.1  manu 	struct linux32_sigaction *lsa;
    152  1.1  manu 	const struct sigaction *bsa;
    153  1.1  manu {
    154  1.3   dsl 	NETBSD32PTR32(lsa->linux_sa_handler, bsa->sa_handler);
    155  1.1  manu 	native_to_linux32_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    156  1.1  manu 	lsa->linux_sa_flags = native_to_linux32_sigflags(bsa->sa_flags);
    157  1.3   dsl 	NETBSD32PTR32(lsa->linux_sa_restorer, NULL);
    158  1.1  manu }
    159  1.1  manu 
    160  1.1  manu void
    161  1.1  manu native_to_linux32_sigaltstack(lss, bss)
    162  1.1  manu 	struct linux32_sigaltstack *lss;
    163  1.1  manu 	const struct sigaltstack *bss;
    164  1.1  manu {
    165  1.3   dsl 	NETBSD32PTR32(lss->ss_sp, bss->ss_sp);
    166  1.1  manu 	lss->ss_size = bss->ss_size;
    167  1.1  manu 	if (bss->ss_flags & SS_ONSTACK)
    168  1.1  manu 	    lss->ss_flags = LINUX32_SS_ONSTACK;
    169  1.1  manu 	else if (bss->ss_flags & SS_DISABLE)
    170  1.1  manu 	    lss->ss_flags = LINUX32_SS_DISABLE;
    171  1.1  manu 	else
    172  1.1  manu 	    lss->ss_flags = 0;
    173  1.1  manu }
    174  1.1  manu 
    175  1.1  manu 
    176  1.1  manu void
    177  1.1  manu native_to_linux32_old_sigset(lss, bss)
    178  1.1  manu 	linux32_old_sigset_t *lss;
    179  1.1  manu 	const sigset_t *bss;
    180  1.1  manu {
    181  1.1  manu 	linux32_sigset_t lsnew;
    182  1.1  manu 
    183  1.1  manu 	native_to_linux32_sigset(&lsnew, bss);
    184  1.1  manu 
    185  1.1  manu 	/* convert new sigset to old sigset */
    186  1.1  manu 	*lss = lsnew.sig[0];
    187  1.1  manu }
    188  1.1  manu 
    189  1.1  manu void
    190  1.1  manu linux32_old_to_native_sigset(bss, lss)
    191  1.1  manu 	sigset_t *bss;
    192  1.1  manu 	const linux32_old_sigset_t *lss;
    193  1.1  manu {
    194  1.1  manu 	linux32_sigset_t ls;
    195  1.1  manu 
    196  1.1  manu 	bzero(&ls, sizeof(ls));
    197  1.1  manu 	ls.sig[0] = *lss;
    198  1.1  manu 
    199  1.1  manu 	linux32_to_native_sigset(bss, &ls);
    200  1.1  manu }
    201  1.1  manu 
    202  1.1  manu int
    203  1.1  manu linux32_sys_rt_sigaction(l, v, retval)
    204  1.1  manu 	struct lwp *l;
    205  1.1  manu 	void *v;
    206  1.1  manu 	register_t *retval;
    207  1.1  manu {
    208  1.1  manu 	struct linux32_sys_rt_sigaction_args /* {
    209  1.1  manu 		syscallarg(int) signum;
    210  1.1  manu 		syscallarg(const linux32_sigactionp_t) nsa;
    211  1.1  manu 		syscallarg(linux32_sigactionp_t) osa;
    212  1.1  manu 		syscallarg(netbsd32_size_t) sigsetsize;
    213  1.1  manu 	} */ *uap = v;
    214  1.1  manu 	struct linux32_sigaction nls32;
    215  1.1  manu 	struct linux32_sigaction ols32;
    216  1.1  manu 	struct sigaction ns;
    217  1.1  manu 	struct sigaction os;
    218  1.1  manu 	int error;
    219  1.1  manu 	int sig;
    220  1.1  manu 	int vers = 0;
    221  1.1  manu 	void *tramp = NULL;
    222  1.1  manu 
    223  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    224  1.1  manu 		return EINVAL;
    225  1.1  manu 
    226  1.4   dsl 	if (SCARG_P32(uap, nsa) != NULL) {
    227  1.4   dsl 		if ((error = copyin(SCARG_P32(uap, nsa),
    228  1.1  manu 		    &nls32, sizeof(nls32))) != 0)
    229  1.1  manu 			return error;
    230  1.1  manu 		linux32_to_native_sigaction(&ns, &nls32);
    231  1.1  manu 	}
    232  1.1  manu 
    233  1.1  manu 	sig = SCARG(uap, signum);
    234  1.1  manu 	if (sig < 0 || sig >= LINUX32__NSIG)
    235  1.1  manu 		return EINVAL;
    236  1.1  manu 	if (sig > 0 && !linux32_to_native_signo[sig]) {
    237  1.1  manu 		/* unknown signal... */
    238  1.1  manu 		os.sa_handler = SIG_IGN;
    239  1.1  manu 		sigemptyset(&os.sa_mask);
    240  1.1  manu 		os.sa_flags = 0;
    241  1.1  manu 	} else {
    242  1.2    ad 		if ((error = sigaction1(l,
    243  1.1  manu 		    linux32_to_native_signo[sig],
    244  1.4   dsl 		    SCARG_P32(uap, nsa) ? &ns : NULL,
    245  1.4   dsl 		    SCARG_P32(uap, osa) ? &os : NULL,
    246  1.1  manu 		    tramp, vers)) != 0)
    247  1.1  manu 			return error;
    248  1.1  manu 	}
    249  1.1  manu 
    250  1.4   dsl 	if (SCARG_P32(uap, osa) != NULL) {
    251  1.1  manu 		native_to_linux32_sigaction(&ols32, &os);
    252  1.1  manu 
    253  1.4   dsl 		if ((error = copyout(&ols32, SCARG_P32(uap, osa),
    254  1.1  manu 		    sizeof(ols32))) != 0)
    255  1.1  manu 			return error;
    256  1.1  manu 	}
    257  1.1  manu 
    258  1.1  manu 	return 0;
    259  1.1  manu }
    260  1.1  manu 
    261  1.1  manu int
    262  1.1  manu linux32_sys_rt_sigprocmask(l, v, retval)
    263  1.1  manu 	struct lwp *l;
    264  1.1  manu 	void *v;
    265  1.1  manu 	register_t *retval;
    266  1.1  manu {
    267  1.1  manu 	struct linux32_sys_rt_sigprocmask_args /* {
    268  1.1  manu 		syscallarg(int) how;
    269  1.1  manu 		syscallarg(const linux32_sigsetp_t) set;
    270  1.1  manu 		syscallarg(linux32_sigsetp_t) oset;
    271  1.1  manu 		syscallarg(netbsd32_size_t) sigsetsize;
    272  1.1  manu 	} */ *uap = v;
    273  1.1  manu 	struct proc *p = l->l_proc;
    274  1.1  manu 	linux32_sigset_t nls32, ols32;
    275  1.1  manu 	sigset_t ns, os;
    276  1.1  manu 	int error;
    277  1.1  manu 	int how;
    278  1.1  manu 
    279  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    280  1.1  manu 		return EINVAL;
    281  1.1  manu 
    282  1.1  manu 	switch (SCARG(uap, how)) {
    283  1.1  manu 	case LINUX32_SIG_BLOCK:
    284  1.1  manu 		how = SIG_BLOCK;
    285  1.1  manu 		break;
    286  1.1  manu 	case LINUX32_SIG_UNBLOCK:
    287  1.1  manu 		how = SIG_UNBLOCK;
    288  1.1  manu 		break;
    289  1.1  manu 	case LINUX32_SIG_SETMASK:
    290  1.1  manu 		how = SIG_SETMASK;
    291  1.1  manu 		break;
    292  1.1  manu 	default:
    293  1.1  manu 		return EINVAL;
    294  1.1  manu 		break;
    295  1.1  manu 	}
    296  1.1  manu 
    297  1.4   dsl 	if (SCARG_P32(uap, set) != NULL) {
    298  1.4   dsl 		if ((error = copyin(SCARG_P32(uap, set),
    299  1.1  manu 		    &nls32, sizeof(nls32))) != 0)
    300  1.1  manu 			return error;
    301  1.1  manu 		linux32_to_native_sigset(&ns, &nls32);
    302  1.1  manu 	}
    303  1.1  manu 
    304  1.2    ad 	mutex_enter(&p->p_smutex);
    305  1.2    ad 	error = sigprocmask1(l, how,
    306  1.4   dsl 	    SCARG_P32(uap, set) ? &ns : NULL,
    307  1.4   dsl 	    SCARG_P32(uap, oset) ? &os : NULL);
    308  1.2    ad 	mutex_exit(&p->p_smutex);
    309  1.2    ad 
    310  1.2    ad         if (error != 0)
    311  1.1  manu 		return error;
    312  1.1  manu 
    313  1.4   dsl 	if (SCARG_P32(uap, oset) != NULL) {
    314  1.1  manu 		native_to_linux32_sigset(&ols32, &os);
    315  1.1  manu 		if ((error = copyout(&ols32,
    316  1.4   dsl 		    SCARG_P32(uap, oset), sizeof(ols32))) != 0)
    317  1.1  manu 			return error;
    318  1.1  manu 	}
    319  1.1  manu 
    320  1.1  manu 	return 0;
    321  1.1  manu }
    322  1.1  manu 
    323  1.1  manu int
    324  1.1  manu linux32_sys_kill(l, v, retval)
    325  1.1  manu 	struct lwp *l;
    326  1.1  manu 	void *v;
    327  1.1  manu 	register_t *retval;
    328  1.1  manu {
    329  1.1  manu 	struct linux32_sys_kill_args /* {
    330  1.1  manu 		syscallarg(int) pid;
    331  1.1  manu 		syscallarg(int) signum;
    332  1.1  manu 	} */ *uap = v;
    333  1.1  manu 
    334  1.1  manu 	struct sys_kill_args ka;
    335  1.1  manu 	int sig;
    336  1.1  manu 
    337  1.1  manu 	SCARG(&ka, pid) = SCARG(uap, pid);
    338  1.1  manu 	sig = SCARG(uap, signum);
    339  1.1  manu 	if (sig < 0 || sig >= LINUX32__NSIG)
    340  1.1  manu 		return (EINVAL);
    341  1.1  manu 	SCARG(&ka, signum) = linux32_to_native_signo[sig];
    342  1.1  manu 	return sys_kill(l, &ka, retval);
    343  1.1  manu }
    344  1.1  manu 
    345  1.1  manu int
    346  1.1  manu linux32_sys_rt_sigsuspend(l, v, retval)
    347  1.1  manu 	struct lwp *l;
    348  1.1  manu 	void *v;
    349  1.1  manu 	register_t *retval;
    350  1.1  manu {
    351  1.1  manu 	struct linux32_sys_rt_sigsuspend_args /* {
    352  1.1  manu 		syscallarg(linux32_sigsetp_t) unewset;
    353  1.1  manu                 syscallarg(netbsd32_size_t) sigsetsize;
    354  1.1  manu 	} */ *uap = v;
    355  1.1  manu 	linux32_sigset_t lss;
    356  1.1  manu 	sigset_t bss;
    357  1.1  manu 	int error;
    358  1.1  manu 
    359  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    360  1.1  manu 		return EINVAL;
    361  1.1  manu 
    362  1.4   dsl 	if ((error = copyin(SCARG_P32(uap, unewset),
    363  1.1  manu 	    &lss, sizeof(linux32_sigset_t))) != 0)
    364  1.1  manu 		return error;
    365  1.1  manu 
    366  1.1  manu 	linux32_to_native_sigset(&bss, &lss);
    367  1.1  manu 
    368  1.2    ad 	return sigsuspend1(l, &bss);
    369  1.1  manu }
    370  1.1  manu 
    371  1.1  manu int
    372  1.1  manu linux32_sys_signal(l, v, retval)
    373  1.1  manu 	struct lwp *l;
    374  1.1  manu 	void *v;
    375  1.1  manu 	register_t *retval;
    376  1.1  manu {
    377  1.1  manu 	struct linux32_sys_signal_args /* {
    378  1.1  manu 		syscallarg(int) signum;
    379  1.1  manu 		syscallarg(linux32_handler_t) handler;
    380  1.1  manu 	} */ *uap = v;
    381  1.1  manu         struct sigaction nbsa, obsa;
    382  1.1  manu         int error, sig;
    383  1.1  manu 
    384  1.1  manu         *retval = -1;
    385  1.1  manu 
    386  1.1  manu         sig = SCARG(uap, signum);
    387  1.1  manu         if (sig < 0 || sig >= LINUX32__NSIG)
    388  1.1  manu                 return EINVAL;
    389  1.1  manu 
    390  1.4   dsl         nbsa.sa_handler = SCARG_P32(uap, handler);
    391  1.1  manu         sigemptyset(&nbsa.sa_mask);
    392  1.1  manu         nbsa.sa_flags = SA_RESETHAND | SA_NODEFER;
    393  1.1  manu 
    394  1.2    ad         if ((error = sigaction1(l, linux32_to_native_signo[sig],
    395  1.1  manu             &nbsa, &obsa, NULL, 0)) != 0)
    396  1.1  manu 		return error;
    397  1.1  manu 
    398  1.1  manu         *retval = (int)(long)obsa.sa_handler;
    399  1.1  manu         return 0;
    400  1.1  manu }
    401