1 /* $NetBSD: svr4_signal.c,v 1.56 2006/11/16 01:32:44 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas and by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: svr4_signal.c,v 1.56 2006/11/16 01:32:44 christos Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/filedesc.h>
47 #include <sys/ioctl.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
50 #include <sys/signal.h>
51 #include <sys/signalvar.h>
52 #include <sys/malloc.h>
53 #include <sys/wait.h>
54
55 #include <sys/sa.h>
56 #include <sys/syscallargs.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <compat/svr4/svr4_types.h>
61 #include <compat/svr4/svr4_signal.h>
62 #include <compat/svr4/svr4_lwp.h>
63 #include <compat/svr4/svr4_ucontext.h>
64 #include <compat/svr4/svr4_syscallargs.h>
65 #include <compat/svr4/svr4_util.h>
66 #include <compat/svr4/svr4_ucontext.h>
67
68 #define svr4_sigmask(n) (1 << (((n) - 1) & 31))
69 #define svr4_sigword(n) (((n) - 1) >> 5)
70 #define svr4_sigemptyset(s) memset((s), 0, sizeof(*(s)))
71 #define svr4_sigismember(s, n) ((s)->bits[svr4_sigword(n)] & svr4_sigmask(n))
72 #define svr4_sigaddset(s, n) ((s)->bits[svr4_sigword(n)] |= svr4_sigmask(n))
73
74 static inline void svr4_sigfillset __P((svr4_sigset_t *));
75 void svr4_to_native_sigaction __P((const struct svr4_sigaction *,
76 struct sigaction *));
77 void native_to_svr4_sigaction __P((const struct sigaction *,
78 struct svr4_sigaction *));
79
80 extern const int native_to_svr4_signo[];
81 extern const int svr4_to_native_signo[];
82
83 static inline void
84 svr4_sigfillset(s)
85 svr4_sigset_t *s;
86 {
87 int i;
88
89 svr4_sigemptyset(s);
90 for (i = 1; i < SVR4_NSIG; i++)
91 if (svr4_to_native_signo[i] != 0)
92 svr4_sigaddset(s, i);
93 }
94
95 void
96 svr4_to_native_sigset(sss, bss)
97 const svr4_sigset_t *sss;
98 sigset_t *bss;
99 {
100 int i, newsig;
101
102 sigemptyset(bss);
103 for (i = 1; i < SVR4_NSIG; i++) {
104 if (svr4_sigismember(sss, i)) {
105 newsig = svr4_to_native_signo[i];
106 if (newsig)
107 sigaddset(bss, newsig);
108 }
109 }
110 }
111
112
113 void
114 native_to_svr4_sigset(bss, sss)
115 const sigset_t *bss;
116 svr4_sigset_t *sss;
117 {
118 int i, newsig;
119
120 svr4_sigemptyset(sss);
121 for (i = 1; i < NSIG; i++) {
122 if (sigismember(bss, i)) {
123 newsig = native_to_svr4_signo[i];
124 if (newsig)
125 svr4_sigaddset(sss, newsig);
126 }
127 }
128 }
129
130 /*
131 * XXX: Only a subset of the flags is currently implemented.
132 */
133 void
134 svr4_to_native_sigaction(ssa, bsa)
135 const struct svr4_sigaction *ssa;
136 struct sigaction *bsa;
137 {
138
139 bsa->sa_handler = (sig_t) ssa->svr4_sa_handler;
140 svr4_to_native_sigset(&ssa->svr4_sa_mask, &bsa->sa_mask);
141 bsa->sa_flags = 0;
142 if ((ssa->svr4_sa_flags & SVR4_SA_ONSTACK) != 0)
143 bsa->sa_flags |= SA_ONSTACK;
144 if ((ssa->svr4_sa_flags & SVR4_SA_RESETHAND) != 0)
145 bsa->sa_flags |= SA_RESETHAND;
146 if ((ssa->svr4_sa_flags & SVR4_SA_RESTART) != 0)
147 bsa->sa_flags |= SA_RESTART;
148 if ((ssa->svr4_sa_flags & SVR4_SA_SIGINFO) != 0)
149 bsa->sa_flags |= SA_SIGINFO;
150 if ((ssa->svr4_sa_flags & SVR4_SA_NODEFER) != 0)
151 bsa->sa_flags |= SA_NODEFER;
152 if ((ssa->svr4_sa_flags & SVR4_SA_NOCLDWAIT) != 0)
153 bsa->sa_flags |= SA_NOCLDWAIT;
154 if ((ssa->svr4_sa_flags & SVR4_SA_NOCLDSTOP) != 0)
155 bsa->sa_flags |= SA_NOCLDSTOP;
156 if ((ssa->svr4_sa_flags & ~SVR4_SA_ALLBITS) != 0) {
157 DPRINTF(("svr4_to_native_sigaction: extra bits %x ignored\n",
158 ssa->svr4_sa_flags & ~SVR4_SA_ALLBITS));
159 }
160 }
161
162 void
163 native_to_svr4_sigaction(bsa, ssa)
164 const struct sigaction *bsa;
165 struct svr4_sigaction *ssa;
166 {
167
168 ssa->svr4_sa_handler = (svr4_sig_t) bsa->sa_handler;
169 native_to_svr4_sigset(&bsa->sa_mask, &ssa->svr4_sa_mask);
170 ssa->svr4_sa_flags = 0;
171 if ((bsa->sa_flags & SA_ONSTACK) != 0)
172 ssa->svr4_sa_flags |= SVR4_SA_ONSTACK;
173 if ((bsa->sa_flags & SA_RESETHAND) != 0)
174 ssa->svr4_sa_flags |= SVR4_SA_RESETHAND;
175 if ((bsa->sa_flags & SA_RESTART) != 0)
176 ssa->svr4_sa_flags |= SVR4_SA_RESTART;
177 if ((bsa->sa_flags & SA_NODEFER) != 0)
178 ssa->svr4_sa_flags |= SVR4_SA_NODEFER;
179 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
180 ssa->svr4_sa_flags |= SVR4_SA_NOCLDSTOP;
181 }
182
183 void
184 svr4_to_native_sigaltstack(sss, bss)
185 const struct svr4_sigaltstack *sss;
186 struct sigaltstack *bss;
187 {
188
189 bss->ss_sp = sss->ss_sp;
190 bss->ss_size = sss->ss_size;
191 bss->ss_flags = 0;
192 if ((sss->ss_flags & SVR4_SS_DISABLE) != 0)
193 bss->ss_flags |= SS_DISABLE;
194 if ((sss->ss_flags & SVR4_SS_ONSTACK) != 0)
195 bss->ss_flags |= SS_ONSTACK;
196 if ((sss->ss_flags & ~SVR4_SS_ALLBITS) != 0)
197 /*XXX*/ printf("svr4_to_native_sigaltstack: extra bits %x ignored\n",
198 sss->ss_flags & ~SVR4_SS_ALLBITS);
199 }
200
201 void
202 native_to_svr4_sigaltstack(bss, sss)
203 const struct sigaltstack *bss;
204 struct svr4_sigaltstack *sss;
205 {
206
207 sss->ss_sp = bss->ss_sp;
208 sss->ss_size = bss->ss_size;
209 sss->ss_flags = 0;
210 if ((bss->ss_flags & SS_DISABLE) != 0)
211 sss->ss_flags |= SVR4_SS_DISABLE;
212 if ((bss->ss_flags & SS_ONSTACK) != 0)
213 sss->ss_flags |= SVR4_SS_ONSTACK;
214 }
215
216 int
217 svr4_sys_sigaction(struct lwp *l, void *v, register_t *retval)
218 {
219 struct svr4_sys_sigaction_args /* {
220 syscallarg(int) signum;
221 syscallarg(const struct svr4_sigaction *) nsa;
222 syscallarg(struct svr4_sigaction *) osa;
223 } */ *uap = v;
224 struct proc *p = l->l_proc;
225 struct svr4_sigaction nssa, ossa;
226 struct sigaction nbsa, obsa;
227 int error;
228
229 if (SCARG(uap, nsa)) {
230 error = copyin(SCARG(uap, nsa), &nssa, sizeof(nssa));
231 if (error)
232 return (error);
233 svr4_to_native_sigaction(&nssa, &nbsa);
234 }
235 error = sigaction1(p, svr4_to_native_signo[SVR4_SIGNO(SCARG(uap, signum))],
236 SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
237 NULL, 0);
238 if (error)
239 return (error);
240 if (SCARG(uap, osa)) {
241 native_to_svr4_sigaction(&obsa, &ossa);
242 error = copyout(&ossa, SCARG(uap, osa), sizeof(ossa));
243 if (error)
244 return (error);
245 }
246 return (0);
247 }
248
249 int
250 svr4_sys_sigaltstack(struct lwp *l, void *v, register_t *retval)
251 {
252 struct svr4_sys_sigaltstack_args /* {
253 syscallarg(const struct svr4_sigaltstack *) nss;
254 syscallarg(struct svr4_sigaltstack *) oss;
255 } */ *uap = v;
256 struct svr4_sigaltstack nsss, osss;
257 struct sigaltstack nbss, obss;
258 int error;
259
260 if (SCARG(uap, nss)) {
261 error = copyin(SCARG(uap, nss), &nsss, sizeof(nsss));
262 if (error)
263 return (error);
264 svr4_to_native_sigaltstack(&nsss, &nbss);
265 }
266 error = sigaltstack1(l->l_proc,
267 SCARG(uap, nss) ? &nbss : 0, SCARG(uap, oss) ? &obss : 0);
268 if (error)
269 return (error);
270 if (SCARG(uap, oss)) {
271 native_to_svr4_sigaltstack(&obss, &osss);
272 error = copyout(&osss, SCARG(uap, oss), sizeof(osss));
273 if (error)
274 return (error);
275 }
276 return (0);
277 }
278
279 /*
280 * Stolen from the ibcs2 one
281 */
282 int
283 svr4_sys_signal(l, v, retval)
284 struct lwp *l;
285 void *v;
286 register_t *retval;
287 {
288 struct svr4_sys_signal_args /* {
289 syscallarg(int) signum;
290 syscallarg(svr4_sig_t) handler;
291 } */ *uap = v;
292 struct proc *p = l->l_proc;
293 int signum = svr4_to_native_signo[SVR4_SIGNO(SCARG(uap, signum))];
294 struct sigaction nbsa, obsa;
295 sigset_t ss;
296 int error;
297
298 if (signum <= 0 || signum >= SVR4_NSIG)
299 return (EINVAL);
300
301 switch (SVR4_SIGCALL(SCARG(uap, signum))) {
302 case SVR4_SIGDEFER_MASK:
303 if (SCARG(uap, handler) == SVR4_SIG_HOLD)
304 goto sighold;
305 /* FALLTHROUGH */
306
307 case SVR4_SIGNAL_MASK:
308 nbsa.sa_handler = (sig_t)SCARG(uap, handler);
309 sigemptyset(&nbsa.sa_mask);
310 nbsa.sa_flags = 0;
311 error = sigaction1(p, signum, &nbsa, &obsa, NULL, 0);
312 if (error)
313 return (error);
314 *retval = (u_int)(u_long)obsa.sa_handler;
315 return (0);
316
317 case SVR4_SIGHOLD_MASK:
318 sighold:
319 sigemptyset(&ss);
320 sigaddset(&ss, signum);
321 return (sigprocmask1(p, SIG_BLOCK, &ss, 0));
322
323 case SVR4_SIGRELSE_MASK:
324 sigemptyset(&ss);
325 sigaddset(&ss, signum);
326 return (sigprocmask1(p, SIG_UNBLOCK, &ss, 0));
327
328 case SVR4_SIGIGNORE_MASK:
329 nbsa.sa_handler = SIG_IGN;
330 sigemptyset(&nbsa.sa_mask);
331 nbsa.sa_flags = 0;
332 return (sigaction1(p, signum, &nbsa, 0, NULL, 0));
333
334 case SVR4_SIGPAUSE_MASK:
335 ss = p->p_sigctx.ps_sigmask;
336 sigdelset(&ss, signum);
337 return (sigsuspend1(p, &ss));
338
339 default:
340 return (ENOSYS);
341 }
342 }
343
344 int
345 svr4_sys_sigprocmask(struct lwp *l, void *v, register_t *retval)
346 {
347 struct svr4_sys_sigprocmask_args /* {
348 syscallarg(int) how;
349 syscallarg(const svr4_sigset_t *) set;
350 syscallarg(svr4_sigset_t *) oset;
351 } */ *uap = v;
352 svr4_sigset_t nsss, osss;
353 sigset_t nbss, obss;
354 int how;
355 int error;
356
357 /*
358 * Initialize how to 0 to avoid a compiler warning. Note that
359 * this is safe because of the check in the default: case.
360 */
361 how = 0;
362
363 switch (SCARG(uap, how)) {
364 case SVR4_SIG_BLOCK:
365 how = SIG_BLOCK;
366 break;
367 case SVR4_SIG_UNBLOCK:
368 how = SIG_UNBLOCK;
369 break;
370 case SVR4_SIG_SETMASK:
371 how = SIG_SETMASK;
372 break;
373 default:
374 if (SCARG(uap, set))
375 return EINVAL;
376 break;
377 }
378
379 if (SCARG(uap, set)) {
380 error = copyin(SCARG(uap, set), &nsss, sizeof(nsss));
381 if (error)
382 return error;
383 svr4_to_native_sigset(&nsss, &nbss);
384 }
385 error = sigprocmask1(l->l_proc, how,
386 SCARG(uap, set) ? &nbss : NULL, SCARG(uap, oset) ? &obss : NULL);
387 if (error)
388 return error;
389 if (SCARG(uap, oset)) {
390 native_to_svr4_sigset(&obss, &osss);
391 error = copyout(&osss, SCARG(uap, oset), sizeof(osss));
392 if (error)
393 return error;
394 }
395 return 0;
396 }
397
398 int
399 svr4_sys_sigpending(struct lwp *l, void *v, register_t *retval)
400 {
401 struct svr4_sys_sigpending_args /* {
402 syscallarg(int) what;
403 syscallarg(svr4_sigset_t *) set;
404 } */ *uap = v;
405 sigset_t bss;
406 svr4_sigset_t sss;
407
408 switch (SCARG(uap, what)) {
409 case 1: /* sigpending */
410 sigpending1(l->l_proc, &bss);
411 native_to_svr4_sigset(&bss, &sss);
412 break;
413
414 case 2: /* sigfillset */
415 svr4_sigfillset(&sss);
416 break;
417
418 default:
419 return (EINVAL);
420 }
421 return (copyout(&sss, SCARG(uap, set), sizeof(sss)));
422 }
423
424 int
425 svr4_sys_sigsuspend(struct lwp *l, void *v, register_t *retval)
426 {
427 struct svr4_sys_sigsuspend_args /* {
428 syscallarg(const svr4_sigset_t *) set;
429 } */ *uap = v;
430 svr4_sigset_t sss;
431 sigset_t bss;
432 int error;
433
434 if (SCARG(uap, set)) {
435 error = copyin(SCARG(uap, set), &sss, sizeof(sss));
436 if (error)
437 return (error);
438 svr4_to_native_sigset(&sss, &bss);
439 }
440
441 return (sigsuspend1(l->l_proc, SCARG(uap, set) ? &bss : 0));
442 }
443
444 int
445 svr4_sys_pause(struct lwp *l, void *v, register_t *retval)
446 {
447
448 return (sigsuspend1(l->l_proc, 0));
449 }
450
451 int
452 svr4_sys_kill(l, v, retval)
453 struct lwp *l;
454 void *v;
455 register_t *retval;
456 {
457 struct svr4_sys_kill_args /* {
458 syscallarg(int) pid;
459 syscallarg(int) signum;
460 } */ *uap = v;
461 struct sys_kill_args ka;
462
463 SCARG(&ka, pid) = SCARG(uap, pid);
464 SCARG(&ka, signum) = svr4_to_native_signo[SVR4_SIGNO(SCARG(uap, signum))];
465 return sys_kill(l, &ka, retval);
466 }
467
468 void
469 svr4_getcontext(l, uc)
470 struct lwp *l;
471 struct svr4_ucontext *uc;
472 {
473 sigset_t mask;
474 struct proc *p = l->l_proc;
475
476 svr4_getmcontext(l, &uc->uc_mcontext, &uc->uc_flags);
477 uc->uc_link = l->l_ctxlink;
478 /*
479 * The (unsupplied) definition of the `current execution stack'
480 * in the System V Interface Definition appears to allow returning
481 * the main context stack.
482 */
483 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
484 uc->uc_stack.ss_sp = (void *)USRSTACK;
485 uc->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
486 uc->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
487 } else {
488 /* Simply copy alternate signal execution stack. */
489 uc->uc_stack.ss_sp = p->p_sigctx.ps_sigstk.ss_sp;
490 uc->uc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
491 uc->uc_stack.ss_flags = p->p_sigctx.ps_sigstk.ss_flags;
492 }
493
494 (void)sigprocmask1(p, 0, NULL, &mask);
495 native_to_svr4_sigset(&mask, &uc->uc_sigmask);
496
497 uc->uc_flags |= _UC_SIGMASK | _UC_STACK;
498 }
499
500
501 int
502 svr4_setcontext(l, uc)
503 struct lwp *l;
504 struct svr4_ucontext *uc;
505 {
506 sigset_t mask;
507 struct proc *p = l->l_proc;
508
509 if (uc->uc_flags & _UC_SIGMASK) {
510 svr4_to_native_sigset(&uc->uc_sigmask, &mask);
511 sigprocmask1(p, SIG_SETMASK, &mask, NULL);
512 }
513
514 /* Ignore the stack; see comment in svr4_getcontext. */
515
516 l->l_ctxlink = uc->uc_link;
517 svr4_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
518
519 return EJUSTRETURN;
520 }
521
522 int
523 svr4_sys_context(l, v, retval)
524 struct lwp *l;
525 void *v;
526 register_t *retval;
527 {
528 struct svr4_sys_context_args /* {
529 syscallarg(int) func;
530 syscallarg(struct svr4_ucontext *) uc;
531 } */ *uap = v;
532 int error;
533 svr4_ucontext_t uc;
534 *retval = 0;
535
536 switch (SCARG(uap, func)) {
537 case SVR4_GETCONTEXT:
538 DPRINTF(("getcontext(%p)\n", SCARG(uap, uc)));
539 svr4_getcontext(l, &uc);
540 return (copyout(&uc, SCARG(uap, uc), sizeof (*SCARG(uap, uc))));
541
542
543 case SVR4_SETCONTEXT:
544 DPRINTF(("setcontext(%p)\n", SCARG(uap, uc)));
545 error = copyin(SCARG(uap, uc), &uc, sizeof (uc));
546 if (error)
547 return (error);
548 svr4_setcontext(l, &uc);
549 return EJUSTRETURN;
550
551 default:
552 DPRINTF(("context(%d, %p)\n", SCARG(uap, func),
553 SCARG(uap, uc)));
554 return ENOSYS;
555 }
556 }
Cache object: 05491cfbd927702ab5e8be8d1dd9a42b
|