Home | History | Annotate | Line # | Download | only in rumpkern
signals.c revision 1.6
      1  1.6  pooka /*	$NetBSD: signals.c,v 1.6 2011/01/08 14:01:04 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka #include <sys/cdefs.h>
     29  1.6  pooka __KERNEL_RCSID(0, "$NetBSD: signals.c,v 1.6 2011/01/08 14:01:04 pooka Exp $");
     30  1.1  pooka 
     31  1.1  pooka #include <sys/param.h>
     32  1.1  pooka #include <sys/atomic.h>
     33  1.1  pooka #include <sys/event.h>
     34  1.1  pooka #include <sys/proc.h>
     35  1.1  pooka #include <sys/signal.h>
     36  1.1  pooka 
     37  1.1  pooka #include <rump/rump.h>
     38  1.1  pooka #include <rump/rumpuser.h>
     39  1.1  pooka 
     40  1.1  pooka #include "rumpkern_if_priv.h"
     41  1.1  pooka 
     42  1.5  pooka const struct filterops sig_filtops = {
     43  1.5  pooka 	.f_attach = (void *)eopnotsupp,
     44  1.5  pooka };
     45  1.5  pooka 
     46  1.2  pooka sigset_t sigcantmask;
     47  1.1  pooka 
     48  1.1  pooka /* RUMP_SIGMODEL_PANIC */
     49  1.1  pooka 
     50  1.1  pooka static void
     51  1.1  pooka rumpsig_panic(pid_t target, int signo)
     52  1.1  pooka {
     53  1.1  pooka 
     54  1.1  pooka 	switch (signo) {
     55  1.1  pooka 	case SIGSYS:
     56  1.6  pooka 	case SIGPIPE:
     57  1.1  pooka 		break;
     58  1.1  pooka 	default:
     59  1.1  pooka 		panic("unhandled signal %d", signo);
     60  1.1  pooka 	}
     61  1.1  pooka }
     62  1.1  pooka 
     63  1.1  pooka /* RUMP_SIGMODEL_IGNORE */
     64  1.1  pooka 
     65  1.1  pooka static void
     66  1.1  pooka rumpsig_ignore(pid_t target, int signo)
     67  1.1  pooka {
     68  1.1  pooka 
     69  1.1  pooka 	return;
     70  1.1  pooka }
     71  1.1  pooka 
     72  1.1  pooka /* RUMP_SIGMODEL_HOST */
     73  1.1  pooka 
     74  1.1  pooka static void
     75  1.1  pooka rumpsig_host(pid_t target, int signo)
     76  1.1  pooka {
     77  1.1  pooka 	int error;
     78  1.1  pooka 
     79  1.1  pooka 	rumpuser_kill(target, signo, &error);
     80  1.1  pooka }
     81  1.1  pooka 
     82  1.1  pooka /* RUMP_SIGMODEL_RAISE */
     83  1.1  pooka 
     84  1.1  pooka static void
     85  1.1  pooka rumpsig_raise(pid_t target, int signo)
     86  1.1  pooka {
     87  1.1  pooka 	int error;
     88  1.1  pooka 
     89  1.1  pooka 	rumpuser_kill(RUMPUSER_PID_SELF, signo, &error);
     90  1.1  pooka }
     91  1.1  pooka 
     92  1.4  pooka static void
     93  1.4  pooka rumpsig_record(pid_t target, int sig)
     94  1.4  pooka {
     95  1.4  pooka 	struct proc *p = NULL;
     96  1.4  pooka 	struct pgrp *pgrp = NULL;
     97  1.4  pooka 
     98  1.4  pooka 	/* well this is a little silly */
     99  1.4  pooka 	mutex_enter(proc_lock);
    100  1.4  pooka 	if (target >= 0)
    101  1.4  pooka 		p = proc_find_raw(target);
    102  1.4  pooka 	else
    103  1.4  pooka 		pgrp = pgrp_find(target);
    104  1.4  pooka 
    105  1.4  pooka 	if (p) {
    106  1.4  pooka 		mutex_enter(p->p_lock);
    107  1.4  pooka 		if (!sigismember(&p->p_sigctx.ps_sigignore, sig)) {
    108  1.4  pooka 			sigaddset(&p->p_sigpend.sp_set, sig);
    109  1.4  pooka 		}
    110  1.4  pooka 		mutex_exit(p->p_lock);
    111  1.4  pooka 	} else if (pgrp) {
    112  1.4  pooka 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    113  1.4  pooka 			mutex_enter(p->p_lock);
    114  1.4  pooka 			if (!sigismember(&p->p_sigctx.ps_sigignore, sig)) {
    115  1.4  pooka 				sigaddset(&p->p_sigpend.sp_set, sig);
    116  1.4  pooka 			}
    117  1.4  pooka 			mutex_exit(p->p_lock);
    118  1.4  pooka 		}
    119  1.4  pooka 	}
    120  1.4  pooka 	mutex_exit(proc_lock);
    121  1.4  pooka }
    122  1.4  pooka 
    123  1.1  pooka typedef void (*rumpsig_fn)(pid_t pid, int sig);
    124  1.1  pooka 
    125  1.1  pooka rumpsig_fn rumpsig = rumpsig_panic;
    126  1.1  pooka 
    127  1.1  pooka /*
    128  1.1  pooka  * Set signal delivery model.  It would be nice if we could
    129  1.1  pooka  * take a functional argument.  But then we'd need some
    130  1.1  pooka  * OS independent way to represent a signal number and also
    131  1.1  pooka  * a method for easy processing (parsing is boring).
    132  1.1  pooka  *
    133  1.1  pooka  * Plus, upcalls from the rump kernel into process space except
    134  1.1  pooka  * via rumpuser is a somewhat gray area now.
    135  1.1  pooka  */
    136  1.1  pooka void
    137  1.1  pooka rump_boot_setsigmodel(enum rump_sigmodel model)
    138  1.1  pooka {
    139  1.1  pooka 
    140  1.1  pooka 	switch (model) {
    141  1.1  pooka 	case RUMP_SIGMODEL_PANIC:
    142  1.4  pooka 		rumpsig = rumpsig_panic;
    143  1.1  pooka 		break;
    144  1.1  pooka 	case RUMP_SIGMODEL_IGNORE:
    145  1.4  pooka 		rumpsig = rumpsig_ignore;
    146  1.1  pooka 		break;
    147  1.1  pooka 	case RUMP_SIGMODEL_HOST:
    148  1.4  pooka 		rumpsig = rumpsig_host;
    149  1.1  pooka 		break;
    150  1.1  pooka 	case RUMP_SIGMODEL_RAISE:
    151  1.4  pooka 		rumpsig = rumpsig_raise;
    152  1.4  pooka 		break;
    153  1.4  pooka 	case RUMP_SIGMODEL_RECORD:
    154  1.4  pooka 		rumpsig = rumpsig_record;
    155  1.1  pooka 		break;
    156  1.1  pooka 	}
    157  1.1  pooka }
    158  1.1  pooka 
    159  1.1  pooka void
    160  1.1  pooka psignal(struct proc *p, int sig)
    161  1.1  pooka {
    162  1.1  pooka 
    163  1.1  pooka 	rumpsig(p->p_pid, sig);
    164  1.1  pooka }
    165  1.1  pooka 
    166  1.1  pooka void
    167  1.1  pooka pgsignal(struct pgrp *pgrp, int sig, int checktty)
    168  1.1  pooka {
    169  1.1  pooka 
    170  1.1  pooka 	rumpsig(-pgrp->pg_id, sig);
    171  1.1  pooka }
    172  1.1  pooka 
    173  1.1  pooka void
    174  1.1  pooka kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    175  1.1  pooka {
    176  1.1  pooka 
    177  1.1  pooka 	rumpsig(p->p_pid, ksi->ksi_signo);
    178  1.1  pooka }
    179  1.1  pooka 
    180  1.1  pooka void
    181  1.1  pooka kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    182  1.1  pooka {
    183  1.1  pooka 
    184  1.1  pooka 	rumpsig(-pgrp->pg_id, ksi->ksi_signo);
    185  1.1  pooka }
    186  1.1  pooka 
    187  1.1  pooka int
    188  1.1  pooka sigispending(struct lwp *l, int signo)
    189  1.1  pooka {
    190  1.4  pooka 	struct proc *p = l->l_proc;
    191  1.4  pooka 	sigset_t tset;
    192  1.4  pooka 
    193  1.4  pooka 	tset = p->p_sigpend.sp_set;
    194  1.1  pooka 
    195  1.4  pooka 	if (signo == 0) {
    196  1.4  pooka 		if (firstsig(&tset) != 0)
    197  1.4  pooka 			return EINTR;
    198  1.4  pooka 	} else if (sigismember(&tset, signo))
    199  1.4  pooka 		return EINTR;
    200  1.1  pooka 	return 0;
    201  1.1  pooka }
    202  1.1  pooka 
    203  1.1  pooka void
    204  1.1  pooka sigpending1(struct lwp *l, sigset_t *ss)
    205  1.1  pooka {
    206  1.4  pooka 	struct proc *p = l->l_proc;
    207  1.1  pooka 
    208  1.4  pooka 	mutex_enter(p->p_lock);
    209  1.4  pooka 	*ss = l->l_proc->p_sigpend.sp_set;
    210  1.4  pooka 	mutex_exit(p->p_lock);
    211  1.1  pooka }
    212  1.1  pooka 
    213  1.1  pooka int
    214  1.1  pooka sigismasked(struct lwp *l, int sig)
    215  1.1  pooka {
    216  1.1  pooka 
    217  1.4  pooka 	return sigismember(&l->l_proc->p_sigctx.ps_sigignore, sig);
    218  1.4  pooka }
    219  1.4  pooka 
    220  1.4  pooka void
    221  1.4  pooka sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
    222  1.4  pooka {
    223  1.4  pooka 
    224  1.4  pooka 	if (mask == NULL)
    225  1.4  pooka 		sigemptyset(&sp->sp_set);
    226  1.4  pooka 	else
    227  1.4  pooka 		sigminusset(mask, &sp->sp_set);
    228  1.1  pooka }
    229  1.1  pooka 
    230  1.1  pooka void
    231  1.1  pooka sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
    232  1.1  pooka {
    233  1.1  pooka 
    234  1.4  pooka 	/* don't assert proc lock, hence callable from user context */
    235  1.4  pooka 	sigclear(&p->p_sigpend, mask, kq);
    236  1.1  pooka }
    237  1.1  pooka 
    238  1.1  pooka void
    239  1.1  pooka ksiginfo_queue_drain0(ksiginfoq_t *kq)
    240  1.1  pooka {
    241  1.1  pooka 
    242  1.1  pooka 	if (!(CIRCLEQ_EMPTY(kq)))
    243  1.1  pooka 		panic("how did that get there?");
    244  1.1  pooka }
    245  1.3  pooka 
    246  1.4  pooka int
    247  1.4  pooka sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
    248  1.4  pooka {
    249  1.4  pooka 	sigset_t *mask = &l->l_proc->p_sigctx.ps_sigignore;
    250  1.4  pooka 
    251  1.4  pooka 	KASSERT(mutex_owned(l->l_proc->p_lock));
    252  1.4  pooka 
    253  1.4  pooka 	if (oss)
    254  1.4  pooka 		*oss = *mask;
    255  1.4  pooka 
    256  1.4  pooka 	if (nss) {
    257  1.4  pooka 		switch (how) {
    258  1.4  pooka 		case SIG_BLOCK:
    259  1.4  pooka 			sigplusset(nss, mask);
    260  1.4  pooka 			break;
    261  1.4  pooka 		case SIG_UNBLOCK:
    262  1.4  pooka 			sigminusset(nss, mask);
    263  1.4  pooka 			break;
    264  1.4  pooka 		case SIG_SETMASK:
    265  1.4  pooka 			*mask = *nss;
    266  1.4  pooka 			break;
    267  1.4  pooka 		default:
    268  1.4  pooka 			return EINVAL;
    269  1.4  pooka 		}
    270  1.4  pooka 	}
    271  1.4  pooka 
    272  1.4  pooka 	return 0;
    273  1.4  pooka }
    274  1.4  pooka 
    275  1.3  pooka void
    276  1.3  pooka siginit(struct proc *p)
    277  1.3  pooka {
    278  1.3  pooka 
    279  1.4  pooka 	sigemptyset(&p->p_sigctx.ps_sigignore);
    280  1.4  pooka 	sigemptyset(&p->p_sigpend.sp_set);
    281  1.3  pooka }
    282