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