signals.c revision 1.5 1 1.5 pooka /* $NetBSD: signals.c,v 1.5 2011/01/03 14:57:06 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.5 pooka __KERNEL_RCSID(0, "$NetBSD: signals.c,v 1.5 2011/01/03 14:57:06 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.1 pooka break;
57 1.1 pooka default:
58 1.1 pooka panic("unhandled signal %d", signo);
59 1.1 pooka }
60 1.1 pooka }
61 1.1 pooka
62 1.1 pooka /* RUMP_SIGMODEL_IGNORE */
63 1.1 pooka
64 1.1 pooka static void
65 1.1 pooka rumpsig_ignore(pid_t target, int signo)
66 1.1 pooka {
67 1.1 pooka
68 1.1 pooka return;
69 1.1 pooka }
70 1.1 pooka
71 1.1 pooka /* RUMP_SIGMODEL_HOST */
72 1.1 pooka
73 1.1 pooka static void
74 1.1 pooka rumpsig_host(pid_t target, int signo)
75 1.1 pooka {
76 1.1 pooka int error;
77 1.1 pooka
78 1.1 pooka rumpuser_kill(target, signo, &error);
79 1.1 pooka }
80 1.1 pooka
81 1.1 pooka /* RUMP_SIGMODEL_RAISE */
82 1.1 pooka
83 1.1 pooka static void
84 1.1 pooka rumpsig_raise(pid_t target, int signo)
85 1.1 pooka {
86 1.1 pooka int error;
87 1.1 pooka
88 1.1 pooka rumpuser_kill(RUMPUSER_PID_SELF, signo, &error);
89 1.1 pooka }
90 1.1 pooka
91 1.4 pooka static void
92 1.4 pooka rumpsig_record(pid_t target, int sig)
93 1.4 pooka {
94 1.4 pooka struct proc *p = NULL;
95 1.4 pooka struct pgrp *pgrp = NULL;
96 1.4 pooka
97 1.4 pooka /* well this is a little silly */
98 1.4 pooka mutex_enter(proc_lock);
99 1.4 pooka if (target >= 0)
100 1.4 pooka p = proc_find_raw(target);
101 1.4 pooka else
102 1.4 pooka pgrp = pgrp_find(target);
103 1.4 pooka
104 1.4 pooka if (p) {
105 1.4 pooka mutex_enter(p->p_lock);
106 1.4 pooka if (!sigismember(&p->p_sigctx.ps_sigignore, sig)) {
107 1.4 pooka sigaddset(&p->p_sigpend.sp_set, sig);
108 1.4 pooka }
109 1.4 pooka mutex_exit(p->p_lock);
110 1.4 pooka } else if (pgrp) {
111 1.4 pooka LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
112 1.4 pooka mutex_enter(p->p_lock);
113 1.4 pooka if (!sigismember(&p->p_sigctx.ps_sigignore, sig)) {
114 1.4 pooka sigaddset(&p->p_sigpend.sp_set, sig);
115 1.4 pooka }
116 1.4 pooka mutex_exit(p->p_lock);
117 1.4 pooka }
118 1.4 pooka }
119 1.4 pooka mutex_exit(proc_lock);
120 1.4 pooka }
121 1.4 pooka
122 1.1 pooka typedef void (*rumpsig_fn)(pid_t pid, int sig);
123 1.1 pooka
124 1.1 pooka rumpsig_fn rumpsig = rumpsig_panic;
125 1.1 pooka
126 1.1 pooka /*
127 1.1 pooka * Set signal delivery model. It would be nice if we could
128 1.1 pooka * take a functional argument. But then we'd need some
129 1.1 pooka * OS independent way to represent a signal number and also
130 1.1 pooka * a method for easy processing (parsing is boring).
131 1.1 pooka *
132 1.1 pooka * Plus, upcalls from the rump kernel into process space except
133 1.1 pooka * via rumpuser is a somewhat gray area now.
134 1.1 pooka */
135 1.1 pooka void
136 1.1 pooka rump_boot_setsigmodel(enum rump_sigmodel model)
137 1.1 pooka {
138 1.1 pooka
139 1.1 pooka switch (model) {
140 1.1 pooka case RUMP_SIGMODEL_PANIC:
141 1.4 pooka rumpsig = rumpsig_panic;
142 1.1 pooka break;
143 1.1 pooka case RUMP_SIGMODEL_IGNORE:
144 1.4 pooka rumpsig = rumpsig_ignore;
145 1.1 pooka break;
146 1.1 pooka case RUMP_SIGMODEL_HOST:
147 1.4 pooka rumpsig = rumpsig_host;
148 1.1 pooka break;
149 1.1 pooka case RUMP_SIGMODEL_RAISE:
150 1.4 pooka rumpsig = rumpsig_raise;
151 1.4 pooka break;
152 1.4 pooka case RUMP_SIGMODEL_RECORD:
153 1.4 pooka rumpsig = rumpsig_record;
154 1.1 pooka break;
155 1.1 pooka }
156 1.1 pooka }
157 1.1 pooka
158 1.1 pooka void
159 1.1 pooka psignal(struct proc *p, int sig)
160 1.1 pooka {
161 1.1 pooka
162 1.1 pooka rumpsig(p->p_pid, sig);
163 1.1 pooka }
164 1.1 pooka
165 1.1 pooka void
166 1.1 pooka pgsignal(struct pgrp *pgrp, int sig, int checktty)
167 1.1 pooka {
168 1.1 pooka
169 1.1 pooka rumpsig(-pgrp->pg_id, sig);
170 1.1 pooka }
171 1.1 pooka
172 1.1 pooka void
173 1.1 pooka kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
174 1.1 pooka {
175 1.1 pooka
176 1.1 pooka rumpsig(p->p_pid, ksi->ksi_signo);
177 1.1 pooka }
178 1.1 pooka
179 1.1 pooka void
180 1.1 pooka kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
181 1.1 pooka {
182 1.1 pooka
183 1.1 pooka rumpsig(-pgrp->pg_id, ksi->ksi_signo);
184 1.1 pooka }
185 1.1 pooka
186 1.1 pooka int
187 1.1 pooka sigispending(struct lwp *l, int signo)
188 1.1 pooka {
189 1.4 pooka struct proc *p = l->l_proc;
190 1.4 pooka sigset_t tset;
191 1.4 pooka
192 1.4 pooka tset = p->p_sigpend.sp_set;
193 1.1 pooka
194 1.4 pooka if (signo == 0) {
195 1.4 pooka if (firstsig(&tset) != 0)
196 1.4 pooka return EINTR;
197 1.4 pooka } else if (sigismember(&tset, signo))
198 1.4 pooka return EINTR;
199 1.1 pooka return 0;
200 1.1 pooka }
201 1.1 pooka
202 1.1 pooka void
203 1.1 pooka sigpending1(struct lwp *l, sigset_t *ss)
204 1.1 pooka {
205 1.4 pooka struct proc *p = l->l_proc;
206 1.1 pooka
207 1.4 pooka mutex_enter(p->p_lock);
208 1.4 pooka *ss = l->l_proc->p_sigpend.sp_set;
209 1.4 pooka mutex_exit(p->p_lock);
210 1.1 pooka }
211 1.1 pooka
212 1.1 pooka int
213 1.1 pooka sigismasked(struct lwp *l, int sig)
214 1.1 pooka {
215 1.1 pooka
216 1.4 pooka return sigismember(&l->l_proc->p_sigctx.ps_sigignore, sig);
217 1.4 pooka }
218 1.4 pooka
219 1.4 pooka void
220 1.4 pooka sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
221 1.4 pooka {
222 1.4 pooka
223 1.4 pooka if (mask == NULL)
224 1.4 pooka sigemptyset(&sp->sp_set);
225 1.4 pooka else
226 1.4 pooka sigminusset(mask, &sp->sp_set);
227 1.1 pooka }
228 1.1 pooka
229 1.1 pooka void
230 1.1 pooka sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
231 1.1 pooka {
232 1.1 pooka
233 1.4 pooka /* don't assert proc lock, hence callable from user context */
234 1.4 pooka sigclear(&p->p_sigpend, mask, kq);
235 1.1 pooka }
236 1.1 pooka
237 1.1 pooka void
238 1.1 pooka ksiginfo_queue_drain0(ksiginfoq_t *kq)
239 1.1 pooka {
240 1.1 pooka
241 1.1 pooka if (!(CIRCLEQ_EMPTY(kq)))
242 1.1 pooka panic("how did that get there?");
243 1.1 pooka }
244 1.3 pooka
245 1.4 pooka int
246 1.4 pooka sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
247 1.4 pooka {
248 1.4 pooka sigset_t *mask = &l->l_proc->p_sigctx.ps_sigignore;
249 1.4 pooka
250 1.4 pooka KASSERT(mutex_owned(l->l_proc->p_lock));
251 1.4 pooka
252 1.4 pooka if (oss)
253 1.4 pooka *oss = *mask;
254 1.4 pooka
255 1.4 pooka if (nss) {
256 1.4 pooka switch (how) {
257 1.4 pooka case SIG_BLOCK:
258 1.4 pooka sigplusset(nss, mask);
259 1.4 pooka break;
260 1.4 pooka case SIG_UNBLOCK:
261 1.4 pooka sigminusset(nss, mask);
262 1.4 pooka break;
263 1.4 pooka case SIG_SETMASK:
264 1.4 pooka *mask = *nss;
265 1.4 pooka break;
266 1.4 pooka default:
267 1.4 pooka return EINVAL;
268 1.4 pooka }
269 1.4 pooka }
270 1.4 pooka
271 1.4 pooka return 0;
272 1.4 pooka }
273 1.4 pooka
274 1.3 pooka void
275 1.3 pooka siginit(struct proc *p)
276 1.3 pooka {
277 1.3 pooka
278 1.4 pooka sigemptyset(&p->p_sigctx.ps_sigignore);
279 1.4 pooka sigemptyset(&p->p_sigpend.sp_set);
280 1.3 pooka }
281