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