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