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