Home | History | Annotate | Line # | Download | only in common
linux32_signal.c revision 1.5
      1  1.5   dsl /*	$NetBSD: linux32_signal.c,v 1.5 2007/12/08 18:36:12 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.5   dsl linux32_to_native_sigset(sigset_t *bss, const linux32_sigset_t *lss)
     57  1.1  manu {
     58  1.1  manu 	int i, newsig;
     59  1.1  manu 
     60  1.1  manu 	sigemptyset(bss);
     61  1.1  manu 	for (i = 1; i < LINUX32__NSIG; i++) {
     62  1.1  manu 		if (linux32_sigismember(lss, i)) {
     63  1.1  manu 			newsig = linux32_to_native_signo[i];
     64  1.1  manu 			if (newsig)
     65  1.1  manu 				sigaddset(bss, newsig);
     66  1.1  manu 		}
     67  1.1  manu 	}
     68  1.1  manu }
     69  1.1  manu 
     70  1.1  manu void
     71  1.5   dsl native_to_linux32_sigset(linux32_sigset_t *lss, const sigset_t *bss)
     72  1.1  manu {
     73  1.1  manu 	int i, newsig;
     74  1.1  manu 
     75  1.1  manu 	linux32_sigemptyset(lss);
     76  1.1  manu 	for (i = 1; i < NSIG; i++) {
     77  1.1  manu 		if (sigismember(bss, i)) {
     78  1.1  manu 			newsig = native_to_linux32_signo[i];
     79  1.1  manu 			if (newsig)
     80  1.1  manu 				linux32_sigaddset(lss, newsig);
     81  1.1  manu 		}
     82  1.1  manu 	}
     83  1.1  manu }
     84  1.1  manu 
     85  1.1  manu unsigned int
     86  1.5   dsl native_to_linux32_sigflags(const int bsf)
     87  1.1  manu {
     88  1.1  manu 	unsigned int lsf = 0;
     89  1.1  manu 	if ((bsf & SA_NOCLDSTOP) != 0)
     90  1.1  manu 		lsf |= LINUX32_SA_NOCLDSTOP;
     91  1.1  manu 	if ((bsf & SA_NOCLDWAIT) != 0)
     92  1.1  manu 		lsf |= LINUX32_SA_NOCLDWAIT;
     93  1.1  manu 	if ((bsf & SA_ONSTACK) != 0)
     94  1.1  manu 		lsf |= LINUX32_SA_ONSTACK;
     95  1.1  manu 	if ((bsf & SA_RESTART) != 0)
     96  1.1  manu 		lsf |= LINUX32_SA_RESTART;
     97  1.1  manu 	if ((bsf & SA_NODEFER) != 0)
     98  1.1  manu 		lsf |= LINUX32_SA_NOMASK;
     99  1.1  manu 	if ((bsf & SA_RESETHAND) != 0)
    100  1.1  manu 		lsf |= LINUX32_SA_ONESHOT;
    101  1.1  manu 	if ((bsf & SA_SIGINFO) != 0)
    102  1.1  manu 		lsf |= LINUX32_SA_SIGINFO;
    103  1.1  manu 	return lsf;
    104  1.1  manu }
    105  1.1  manu 
    106  1.1  manu int
    107  1.5   dsl linux32_to_native_sigflags(const unsigned long lsf)
    108  1.1  manu {
    109  1.1  manu 	int bsf = 0;
    110  1.1  manu 	if ((lsf & LINUX32_SA_NOCLDSTOP) != 0)
    111  1.1  manu 		bsf |= SA_NOCLDSTOP;
    112  1.1  manu 	if ((lsf & LINUX32_SA_NOCLDWAIT) != 0)
    113  1.1  manu 		bsf |= SA_NOCLDWAIT;
    114  1.1  manu 	if ((lsf & LINUX32_SA_ONSTACK) != 0)
    115  1.1  manu 		bsf |= SA_ONSTACK;
    116  1.1  manu 	if ((lsf & LINUX32_SA_RESTART) != 0)
    117  1.1  manu 		bsf |= SA_RESTART;
    118  1.1  manu 	if ((lsf & LINUX32_SA_ONESHOT) != 0)
    119  1.1  manu 		bsf |= SA_RESETHAND;
    120  1.1  manu 	if ((lsf & LINUX32_SA_NOMASK) != 0)
    121  1.1  manu 		bsf |= SA_NODEFER;
    122  1.1  manu 	if ((lsf & LINUX32_SA_SIGINFO) != 0)
    123  1.1  manu 		bsf |= SA_SIGINFO;
    124  1.1  manu 	if ((lsf & ~LINUX32_SA_ALLBITS) != 0) {
    125  1.1  manu #ifdef DEBUG_LINUX
    126  1.1  manu 		printf("linux32_old_to_native_sigflags: "
    127  1.1  manu 		    "%lx extra bits ignored\n", lsf);
    128  1.1  manu #endif
    129  1.1  manu 	}
    130  1.1  manu 	return bsf;
    131  1.1  manu }
    132  1.1  manu 
    133  1.1  manu void
    134  1.5   dsl linux32_to_native_sigaction(struct sigaction *bsa, const struct linux32_sigaction *lsa)
    135  1.1  manu {
    136  1.1  manu 	bsa->sa_handler = NETBSD32PTR64(lsa->linux_sa_handler);
    137  1.1  manu 	linux32_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask);
    138  1.1  manu 	bsa->sa_flags = linux32_to_native_sigflags(lsa->linux_sa_flags);
    139  1.1  manu }
    140  1.1  manu 
    141  1.1  manu void
    142  1.5   dsl native_to_linux32_sigaction(struct linux32_sigaction *lsa, const struct sigaction *bsa)
    143  1.1  manu {
    144  1.3   dsl 	NETBSD32PTR32(lsa->linux_sa_handler, bsa->sa_handler);
    145  1.1  manu 	native_to_linux32_sigset(&lsa->linux_sa_mask, &bsa->sa_mask);
    146  1.1  manu 	lsa->linux_sa_flags = native_to_linux32_sigflags(bsa->sa_flags);
    147  1.3   dsl 	NETBSD32PTR32(lsa->linux_sa_restorer, NULL);
    148  1.1  manu }
    149  1.1  manu 
    150  1.1  manu void
    151  1.5   dsl native_to_linux32_sigaltstack(struct linux32_sigaltstack *lss, const struct sigaltstack *bss)
    152  1.1  manu {
    153  1.3   dsl 	NETBSD32PTR32(lss->ss_sp, bss->ss_sp);
    154  1.1  manu 	lss->ss_size = bss->ss_size;
    155  1.1  manu 	if (bss->ss_flags & SS_ONSTACK)
    156  1.1  manu 	    lss->ss_flags = LINUX32_SS_ONSTACK;
    157  1.1  manu 	else if (bss->ss_flags & SS_DISABLE)
    158  1.1  manu 	    lss->ss_flags = LINUX32_SS_DISABLE;
    159  1.1  manu 	else
    160  1.1  manu 	    lss->ss_flags = 0;
    161  1.1  manu }
    162  1.1  manu 
    163  1.1  manu 
    164  1.1  manu void
    165  1.5   dsl native_to_linux32_old_sigset(linux32_old_sigset_t *lss, const sigset_t *bss)
    166  1.1  manu {
    167  1.1  manu 	linux32_sigset_t lsnew;
    168  1.1  manu 
    169  1.1  manu 	native_to_linux32_sigset(&lsnew, bss);
    170  1.1  manu 
    171  1.1  manu 	/* convert new sigset to old sigset */
    172  1.1  manu 	*lss = lsnew.sig[0];
    173  1.1  manu }
    174  1.1  manu 
    175  1.1  manu void
    176  1.5   dsl linux32_old_to_native_sigset(sigset_t *bss, const linux32_old_sigset_t *lss)
    177  1.1  manu {
    178  1.1  manu 	linux32_sigset_t ls;
    179  1.1  manu 
    180  1.1  manu 	bzero(&ls, sizeof(ls));
    181  1.1  manu 	ls.sig[0] = *lss;
    182  1.1  manu 
    183  1.1  manu 	linux32_to_native_sigset(bss, &ls);
    184  1.1  manu }
    185  1.1  manu 
    186  1.1  manu int
    187  1.5   dsl linux32_sys_rt_sigaction(struct lwp *l, void *v, register_t *retval)
    188  1.1  manu {
    189  1.1  manu 	struct linux32_sys_rt_sigaction_args /* {
    190  1.1  manu 		syscallarg(int) signum;
    191  1.1  manu 		syscallarg(const linux32_sigactionp_t) nsa;
    192  1.1  manu 		syscallarg(linux32_sigactionp_t) osa;
    193  1.1  manu 		syscallarg(netbsd32_size_t) sigsetsize;
    194  1.1  manu 	} */ *uap = v;
    195  1.1  manu 	struct linux32_sigaction nls32;
    196  1.1  manu 	struct linux32_sigaction ols32;
    197  1.1  manu 	struct sigaction ns;
    198  1.1  manu 	struct sigaction os;
    199  1.1  manu 	int error;
    200  1.1  manu 	int sig;
    201  1.1  manu 	int vers = 0;
    202  1.1  manu 	void *tramp = NULL;
    203  1.1  manu 
    204  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    205  1.1  manu 		return EINVAL;
    206  1.1  manu 
    207  1.4   dsl 	if (SCARG_P32(uap, nsa) != NULL) {
    208  1.4   dsl 		if ((error = copyin(SCARG_P32(uap, nsa),
    209  1.1  manu 		    &nls32, sizeof(nls32))) != 0)
    210  1.1  manu 			return error;
    211  1.1  manu 		linux32_to_native_sigaction(&ns, &nls32);
    212  1.1  manu 	}
    213  1.1  manu 
    214  1.1  manu 	sig = SCARG(uap, signum);
    215  1.1  manu 	if (sig < 0 || sig >= LINUX32__NSIG)
    216  1.1  manu 		return EINVAL;
    217  1.1  manu 	if (sig > 0 && !linux32_to_native_signo[sig]) {
    218  1.1  manu 		/* unknown signal... */
    219  1.1  manu 		os.sa_handler = SIG_IGN;
    220  1.1  manu 		sigemptyset(&os.sa_mask);
    221  1.1  manu 		os.sa_flags = 0;
    222  1.1  manu 	} else {
    223  1.2    ad 		if ((error = sigaction1(l,
    224  1.1  manu 		    linux32_to_native_signo[sig],
    225  1.4   dsl 		    SCARG_P32(uap, nsa) ? &ns : NULL,
    226  1.4   dsl 		    SCARG_P32(uap, osa) ? &os : NULL,
    227  1.1  manu 		    tramp, vers)) != 0)
    228  1.1  manu 			return error;
    229  1.1  manu 	}
    230  1.1  manu 
    231  1.4   dsl 	if (SCARG_P32(uap, osa) != NULL) {
    232  1.1  manu 		native_to_linux32_sigaction(&ols32, &os);
    233  1.1  manu 
    234  1.4   dsl 		if ((error = copyout(&ols32, SCARG_P32(uap, osa),
    235  1.1  manu 		    sizeof(ols32))) != 0)
    236  1.1  manu 			return error;
    237  1.1  manu 	}
    238  1.1  manu 
    239  1.1  manu 	return 0;
    240  1.1  manu }
    241  1.1  manu 
    242  1.1  manu int
    243  1.5   dsl linux32_sys_rt_sigprocmask(struct lwp *l, void *v, register_t *retval)
    244  1.1  manu {
    245  1.1  manu 	struct linux32_sys_rt_sigprocmask_args /* {
    246  1.1  manu 		syscallarg(int) how;
    247  1.1  manu 		syscallarg(const linux32_sigsetp_t) set;
    248  1.1  manu 		syscallarg(linux32_sigsetp_t) oset;
    249  1.1  manu 		syscallarg(netbsd32_size_t) sigsetsize;
    250  1.1  manu 	} */ *uap = v;
    251  1.1  manu 	struct proc *p = l->l_proc;
    252  1.1  manu 	linux32_sigset_t nls32, ols32;
    253  1.1  manu 	sigset_t ns, os;
    254  1.1  manu 	int error;
    255  1.1  manu 	int how;
    256  1.1  manu 
    257  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    258  1.1  manu 		return EINVAL;
    259  1.1  manu 
    260  1.1  manu 	switch (SCARG(uap, how)) {
    261  1.1  manu 	case LINUX32_SIG_BLOCK:
    262  1.1  manu 		how = SIG_BLOCK;
    263  1.1  manu 		break;
    264  1.1  manu 	case LINUX32_SIG_UNBLOCK:
    265  1.1  manu 		how = SIG_UNBLOCK;
    266  1.1  manu 		break;
    267  1.1  manu 	case LINUX32_SIG_SETMASK:
    268  1.1  manu 		how = SIG_SETMASK;
    269  1.1  manu 		break;
    270  1.1  manu 	default:
    271  1.1  manu 		return EINVAL;
    272  1.1  manu 		break;
    273  1.1  manu 	}
    274  1.1  manu 
    275  1.4   dsl 	if (SCARG_P32(uap, set) != NULL) {
    276  1.4   dsl 		if ((error = copyin(SCARG_P32(uap, set),
    277  1.1  manu 		    &nls32, sizeof(nls32))) != 0)
    278  1.1  manu 			return error;
    279  1.1  manu 		linux32_to_native_sigset(&ns, &nls32);
    280  1.1  manu 	}
    281  1.1  manu 
    282  1.2    ad 	mutex_enter(&p->p_smutex);
    283  1.2    ad 	error = sigprocmask1(l, how,
    284  1.4   dsl 	    SCARG_P32(uap, set) ? &ns : NULL,
    285  1.4   dsl 	    SCARG_P32(uap, oset) ? &os : NULL);
    286  1.2    ad 	mutex_exit(&p->p_smutex);
    287  1.2    ad 
    288  1.2    ad         if (error != 0)
    289  1.1  manu 		return error;
    290  1.1  manu 
    291  1.4   dsl 	if (SCARG_P32(uap, oset) != NULL) {
    292  1.1  manu 		native_to_linux32_sigset(&ols32, &os);
    293  1.1  manu 		if ((error = copyout(&ols32,
    294  1.4   dsl 		    SCARG_P32(uap, oset), sizeof(ols32))) != 0)
    295  1.1  manu 			return error;
    296  1.1  manu 	}
    297  1.1  manu 
    298  1.1  manu 	return 0;
    299  1.1  manu }
    300  1.1  manu 
    301  1.1  manu int
    302  1.1  manu linux32_sys_kill(l, v, retval)
    303  1.1  manu 	struct lwp *l;
    304  1.1  manu 	void *v;
    305  1.1  manu 	register_t *retval;
    306  1.1  manu {
    307  1.1  manu 	struct linux32_sys_kill_args /* {
    308  1.1  manu 		syscallarg(int) pid;
    309  1.1  manu 		syscallarg(int) signum;
    310  1.1  manu 	} */ *uap = v;
    311  1.1  manu 
    312  1.1  manu 	struct sys_kill_args ka;
    313  1.1  manu 	int sig;
    314  1.1  manu 
    315  1.1  manu 	SCARG(&ka, pid) = SCARG(uap, pid);
    316  1.1  manu 	sig = SCARG(uap, signum);
    317  1.1  manu 	if (sig < 0 || sig >= LINUX32__NSIG)
    318  1.1  manu 		return (EINVAL);
    319  1.1  manu 	SCARG(&ka, signum) = linux32_to_native_signo[sig];
    320  1.1  manu 	return sys_kill(l, &ka, retval);
    321  1.1  manu }
    322  1.1  manu 
    323  1.1  manu int
    324  1.1  manu linux32_sys_rt_sigsuspend(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_rt_sigsuspend_args /* {
    330  1.1  manu 		syscallarg(linux32_sigsetp_t) unewset;
    331  1.1  manu                 syscallarg(netbsd32_size_t) sigsetsize;
    332  1.1  manu 	} */ *uap = v;
    333  1.1  manu 	linux32_sigset_t lss;
    334  1.1  manu 	sigset_t bss;
    335  1.1  manu 	int error;
    336  1.1  manu 
    337  1.1  manu 	if (SCARG(uap, sigsetsize) != sizeof(linux32_sigset_t))
    338  1.1  manu 		return EINVAL;
    339  1.1  manu 
    340  1.4   dsl 	if ((error = copyin(SCARG_P32(uap, unewset),
    341  1.1  manu 	    &lss, sizeof(linux32_sigset_t))) != 0)
    342  1.1  manu 		return error;
    343  1.1  manu 
    344  1.1  manu 	linux32_to_native_sigset(&bss, &lss);
    345  1.1  manu 
    346  1.2    ad 	return sigsuspend1(l, &bss);
    347  1.1  manu }
    348  1.1  manu 
    349  1.1  manu int
    350  1.5   dsl linux32_sys_signal(struct lwp *l, void *v, register_t *retval)
    351  1.1  manu {
    352  1.1  manu 	struct linux32_sys_signal_args /* {
    353  1.1  manu 		syscallarg(int) signum;
    354  1.1  manu 		syscallarg(linux32_handler_t) handler;
    355  1.1  manu 	} */ *uap = v;
    356  1.1  manu         struct sigaction nbsa, obsa;
    357  1.1  manu         int error, sig;
    358  1.1  manu 
    359  1.1  manu         *retval = -1;
    360  1.1  manu 
    361  1.1  manu         sig = SCARG(uap, signum);
    362  1.1  manu         if (sig < 0 || sig >= LINUX32__NSIG)
    363  1.1  manu                 return EINVAL;
    364  1.1  manu 
    365  1.4   dsl         nbsa.sa_handler = SCARG_P32(uap, handler);
    366  1.1  manu         sigemptyset(&nbsa.sa_mask);
    367  1.1  manu         nbsa.sa_flags = SA_RESETHAND | SA_NODEFER;
    368  1.1  manu 
    369  1.2    ad         if ((error = sigaction1(l, linux32_to_native_signo[sig],
    370  1.1  manu             &nbsa, &obsa, NULL, 0)) != 0)
    371  1.1  manu 		return error;
    372  1.1  manu 
    373  1.1  manu         *retval = (int)(long)obsa.sa_handler;
    374  1.1  manu         return 0;
    375  1.1  manu }
    376