Deleted Added
full compact
linux_signal.c (165869) linux_signal.c (184058)
1/*-
2 * Copyright (c) 1994-1995 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1994-1995 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/compat/linux/linux_signal.c 165869 2007-01-07 19:14:06Z netchild $");
30__FBSDID("$FreeBSD: head/sys/compat/linux/linux_signal.c 184058 2008-10-19 10:02:26Z kib $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/sx.h>
37#include <sys/proc.h>
38#include <sys/signalvar.h>
39#include <sys/syscallsubr.h>
40#include <sys/sysproto.h>
41
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/sx.h>
37#include <sys/proc.h>
38#include <sys/signalvar.h>
39#include <sys/syscallsubr.h>
40#include <sys/sysproto.h>
41
42#include <security/audit/audit.h>
43
42#include "opt_compat.h"
43
44#ifdef COMPAT_LINUX32
45#include <machine/../linux32/linux.h>
46#include <machine/../linux32/linux32_proto.h>
47#else
48#include <machine/../linux/linux.h>
49#include <machine/../linux/linux_proto.h>
50#endif
51#include <compat/linux/linux_signal.h>
52#include <compat/linux/linux_util.h>
53#include <compat/linux/linux_emul.h>
54
55void
56linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
57{
58 int b, l;
59
60 SIGEMPTYSET(*bss);
61 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
62 bss->__bits[1] = lss->__bits[1];
63 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
64 if (LINUX_SIGISMEMBER(*lss, l)) {
65 b = linux_to_bsd_signal[_SIG_IDX(l)];
66 if (b)
67 SIGADDSET(*bss, b);
68 }
69 }
70}
71
72void
73bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
74{
75 int b, l;
76
77 LINUX_SIGEMPTYSET(*lss);
78 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
79 lss->__bits[1] = bss->__bits[1];
80 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
81 if (SIGISMEMBER(*bss, b)) {
82 l = bsd_to_linux_signal[_SIG_IDX(b)];
83 if (l)
84 LINUX_SIGADDSET(*lss, l);
85 }
86 }
87}
88
89static void
90linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
91{
92
93 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
94 bsa->sa_handler = PTRIN(lsa->lsa_handler);
95 bsa->sa_flags = 0;
96 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
97 bsa->sa_flags |= SA_NOCLDSTOP;
98 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
99 bsa->sa_flags |= SA_NOCLDWAIT;
100 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
101 bsa->sa_flags |= SA_SIGINFO;
102 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
103 bsa->sa_flags |= SA_ONSTACK;
104 if (lsa->lsa_flags & LINUX_SA_RESTART)
105 bsa->sa_flags |= SA_RESTART;
106 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
107 bsa->sa_flags |= SA_RESETHAND;
108 if (lsa->lsa_flags & LINUX_SA_NOMASK)
109 bsa->sa_flags |= SA_NODEFER;
110}
111
112static void
113bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
114{
115
116 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
117#ifdef COMPAT_LINUX32
118 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
119#else
120 lsa->lsa_handler = bsa->sa_handler;
121#endif
122 lsa->lsa_restorer = 0; /* unsupported */
123 lsa->lsa_flags = 0;
124 if (bsa->sa_flags & SA_NOCLDSTOP)
125 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
126 if (bsa->sa_flags & SA_NOCLDWAIT)
127 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
128 if (bsa->sa_flags & SA_SIGINFO)
129 lsa->lsa_flags |= LINUX_SA_SIGINFO;
130 if (bsa->sa_flags & SA_ONSTACK)
131 lsa->lsa_flags |= LINUX_SA_ONSTACK;
132 if (bsa->sa_flags & SA_RESTART)
133 lsa->lsa_flags |= LINUX_SA_RESTART;
134 if (bsa->sa_flags & SA_RESETHAND)
135 lsa->lsa_flags |= LINUX_SA_ONESHOT;
136 if (bsa->sa_flags & SA_NODEFER)
137 lsa->lsa_flags |= LINUX_SA_NOMASK;
138}
139
140int
141linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
142 l_sigaction_t *linux_osa)
143{
144 struct sigaction act, oact, *nsa, *osa;
145 int error, sig;
146
147 if (!LINUX_SIG_VALID(linux_sig))
148 return (EINVAL);
149
150 osa = (linux_osa != NULL) ? &oact : NULL;
151 if (linux_nsa != NULL) {
152 nsa = &act;
153 linux_to_bsd_sigaction(linux_nsa, nsa);
154 } else
155 nsa = NULL;
156
157 if (linux_sig <= LINUX_SIGTBLSZ)
158 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
159 else
160 sig = linux_sig;
161
162 error = kern_sigaction(td, sig, nsa, osa, 0);
163 if (error)
164 return (error);
165
166 if (linux_osa != NULL)
167 bsd_to_linux_sigaction(osa, linux_osa);
168
169 return (0);
170}
171
172
173int
174linux_signal(struct thread *td, struct linux_signal_args *args)
175{
176 l_sigaction_t nsa, osa;
177 int error;
178
179#ifdef DEBUG
180 if (ldebug(signal))
181 printf(ARGS(signal, "%d, %p"),
182 args->sig, (void *)(uintptr_t)args->handler);
183#endif
184
185 nsa.lsa_handler = args->handler;
186 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
187 LINUX_SIGEMPTYSET(nsa.lsa_mask);
188
189 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
190 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
191
192 return (error);
193}
194
195int
196linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
197{
198 l_sigaction_t nsa, osa;
199 int error;
200
201#ifdef DEBUG
202 if (ldebug(rt_sigaction))
203 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
204 (long)args->sig, (void *)args->act,
205 (void *)args->oact, (long)args->sigsetsize);
206#endif
207
208 if (args->sigsetsize != sizeof(l_sigset_t))
209 return (EINVAL);
210
211 if (args->act != NULL) {
212 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
213 if (error)
214 return (error);
215 }
216
217 error = linux_do_sigaction(td, args->sig,
218 args->act ? &nsa : NULL,
219 args->oact ? &osa : NULL);
220
221 if (args->oact != NULL && !error) {
222 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
223 }
224
225 return (error);
226}
227
228static int
229linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
230 l_sigset_t *old)
231{
232 sigset_t omask, nmask;
233 sigset_t *nmaskp;
234 int error;
235
236 td->td_retval[0] = 0;
237
238 switch (how) {
239 case LINUX_SIG_BLOCK:
240 how = SIG_BLOCK;
241 break;
242 case LINUX_SIG_UNBLOCK:
243 how = SIG_UNBLOCK;
244 break;
245 case LINUX_SIG_SETMASK:
246 how = SIG_SETMASK;
247 break;
248 default:
249 return (EINVAL);
250 }
251 if (new != NULL) {
252 linux_to_bsd_sigset(new, &nmask);
253 nmaskp = &nmask;
254 } else
255 nmaskp = NULL;
256 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
257 if (error == 0 && old != NULL)
258 bsd_to_linux_sigset(&omask, old);
259
260 return (error);
261}
262
263int
264linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
265{
266 l_osigset_t mask;
267 l_sigset_t set, oset;
268 int error;
269
270#ifdef DEBUG
271 if (ldebug(sigprocmask))
272 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
273#endif
274
275 if (args->mask != NULL) {
276 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
277 if (error)
278 return (error);
279 LINUX_SIGEMPTYSET(set);
280 set.__bits[0] = mask;
281 }
282
283 error = linux_do_sigprocmask(td, args->how,
284 args->mask ? &set : NULL,
285 args->omask ? &oset : NULL);
286
287 if (args->omask != NULL && !error) {
288 mask = oset.__bits[0];
289 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
290 }
291
292 return (error);
293}
294
295int
296linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
297{
298 l_sigset_t set, oset;
299 int error;
300
301#ifdef DEBUG
302 if (ldebug(rt_sigprocmask))
303 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
304 args->how, (void *)args->mask,
305 (void *)args->omask, (long)args->sigsetsize);
306#endif
307
308 if (args->sigsetsize != sizeof(l_sigset_t))
309 return EINVAL;
310
311 if (args->mask != NULL) {
312 error = copyin(args->mask, &set, sizeof(l_sigset_t));
313 if (error)
314 return (error);
315 }
316
317 error = linux_do_sigprocmask(td, args->how,
318 args->mask ? &set : NULL,
319 args->omask ? &oset : NULL);
320
321 if (args->omask != NULL && !error) {
322 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
323 }
324
325 return (error);
326}
327
328int
329linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
330{
331 struct proc *p = td->td_proc;
332 l_sigset_t mask;
333
334#ifdef DEBUG
335 if (ldebug(sgetmask))
336 printf(ARGS(sgetmask, ""));
337#endif
338
339 PROC_LOCK(p);
340 bsd_to_linux_sigset(&td->td_sigmask, &mask);
341 PROC_UNLOCK(p);
342 td->td_retval[0] = mask.__bits[0];
343 return (0);
344}
345
346int
347linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
348{
349 struct proc *p = td->td_proc;
350 l_sigset_t lset;
351 sigset_t bset;
352
353#ifdef DEBUG
354 if (ldebug(ssetmask))
355 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
356#endif
357
358 PROC_LOCK(p);
359 bsd_to_linux_sigset(&td->td_sigmask, &lset);
360 td->td_retval[0] = lset.__bits[0];
361 LINUX_SIGEMPTYSET(lset);
362 lset.__bits[0] = args->mask;
363 linux_to_bsd_sigset(&lset, &bset);
364 td->td_sigmask = bset;
365 SIG_CANTMASK(td->td_sigmask);
366 signotify(td);
367 PROC_UNLOCK(p);
368 return (0);
369}
370
371/*
372 * MPSAFE
373 */
374int
375linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
376{
377 struct proc *p = td->td_proc;
378 sigset_t bset;
379 l_sigset_t lset;
380 l_osigset_t mask;
381
382#ifdef DEBUG
383 if (ldebug(sigpending))
384 printf(ARGS(sigpending, "*"));
385#endif
386
387 PROC_LOCK(p);
388 bset = p->p_siglist;
389 SIGSETOR(bset, td->td_siglist);
390 SIGSETAND(bset, td->td_sigmask);
391 PROC_UNLOCK(p);
392 bsd_to_linux_sigset(&bset, &lset);
393 mask = lset.__bits[0];
394 return (copyout(&mask, args->mask, sizeof(mask)));
395}
396
397/*
398 * MPSAFE
399 */
400int
401linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
402{
403 struct proc *p = td->td_proc;
404 sigset_t bset;
405 l_sigset_t lset;
406
407 if (args->sigsetsize > sizeof(lset))
408 return EINVAL;
409 /* NOT REACHED */
410
411#ifdef DEBUG
412 if (ldebug(rt_sigpending))
413 printf(ARGS(rt_sigpending, "*"));
414#endif
415
416 PROC_LOCK(p);
417 bset = p->p_siglist;
418 SIGSETOR(bset, td->td_siglist);
419 SIGSETAND(bset, td->td_sigmask);
420 PROC_UNLOCK(p);
421 bsd_to_linux_sigset(&bset, &lset);
422 return (copyout(&lset, args->set, args->sigsetsize));
423}
424
425/*
426 * MPSAFE
427 */
428int
429linux_rt_sigtimedwait(struct thread *td,
430 struct linux_rt_sigtimedwait_args *args)
431{
432 int error;
433 l_timeval ltv;
434 struct timeval tv;
435 struct timespec ts, *tsa;
436 l_sigset_t lset;
437 sigset_t bset;
438 l_siginfo_t linfo;
439 ksiginfo_t info;
440
441#ifdef DEBUG
442 if (ldebug(rt_sigtimedwait))
443 printf(ARGS(rt_sigtimedwait, "*"));
444#endif
445 if (args->sigsetsize != sizeof(l_sigset_t))
446 return (EINVAL);
447
448 if ((error = copyin(args->mask, &lset, sizeof(lset))))
449 return (error);
450 linux_to_bsd_sigset(&lset, &bset);
451
452 tsa = NULL;
453 if (args->timeout) {
454 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
455 return (error);
456#ifdef DEBUG
457 if (ldebug(rt_sigtimedwait))
458 printf(LMSG("linux_rt_sigtimedwait: incoming timeout (%d/%d)\n"),
459 ltv.tv_sec, ltv.tv_usec);
460#endif
461 tv.tv_sec = (long)ltv.tv_sec;
462 tv.tv_usec = (suseconds_t)ltv.tv_usec;
463 if (itimerfix(&tv)) {
464 /*
465 * The timeout was invalid. Convert it to something
466 * valid that will act as it does under Linux.
467 */
468 tv.tv_sec += tv.tv_usec / 1000000;
469 tv.tv_usec %= 1000000;
470 if (tv.tv_usec < 0) {
471 tv.tv_sec -= 1;
472 tv.tv_usec += 1000000;
473 }
474 if (tv.tv_sec < 0)
475 timevalclear(&tv);
476#ifdef DEBUG
477 if (ldebug(rt_sigtimedwait))
478 printf(LMSG("linux_rt_sigtimedwait: converted timeout (%jd/%ld)\n"),
479 (intmax_t)tv.tv_sec, tv.tv_usec);
480#endif
481 }
482 TIMEVAL_TO_TIMESPEC(&tv, &ts);
483 tsa = &ts;
484 }
485 error = kern_sigtimedwait(td, bset, &info, tsa);
486#ifdef DEBUG
487 if (ldebug(rt_sigtimedwait))
488 printf(LMSG("linux_rt_sigtimedwait: sigtimedwait returning (%d)\n"), error);
489#endif
490 if (error)
491 return (error);
492
493 if (args->ptr) {
494 memset(&linfo, 0, sizeof(linfo));
495 linfo.lsi_signo = info.ksi_signo;
496 error = copyout(&linfo, args->ptr, sizeof(linfo));
497 }
498
499 /* Repost if we got an error. */
500 if (error && info.ksi_signo) {
501 PROC_LOCK(td->td_proc);
502 tdsignal(td->td_proc, td, info.ksi_signo, &info);
503 PROC_UNLOCK(td->td_proc);
504 } else
505 td->td_retval[0] = info.ksi_signo;
506
507 return (error);
508}
509
510int
511linux_kill(struct thread *td, struct linux_kill_args *args)
512{
513 struct kill_args /* {
514 int pid;
515 int signum;
516 } */ tmp;
517
518#ifdef DEBUG
519 if (ldebug(kill))
520 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
521#endif
522
523 /*
524 * Allow signal 0 as a means to check for privileges
525 */
526 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
527 return EINVAL;
528
529 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
530 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
531 else
532 tmp.signum = args->signum;
533
534 tmp.pid = args->pid;
535 return (kill(td, &tmp));
536}
537
44#include "opt_compat.h"
45
46#ifdef COMPAT_LINUX32
47#include <machine/../linux32/linux.h>
48#include <machine/../linux32/linux32_proto.h>
49#else
50#include <machine/../linux/linux.h>
51#include <machine/../linux/linux_proto.h>
52#endif
53#include <compat/linux/linux_signal.h>
54#include <compat/linux/linux_util.h>
55#include <compat/linux/linux_emul.h>
56
57void
58linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
59{
60 int b, l;
61
62 SIGEMPTYSET(*bss);
63 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
64 bss->__bits[1] = lss->__bits[1];
65 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
66 if (LINUX_SIGISMEMBER(*lss, l)) {
67 b = linux_to_bsd_signal[_SIG_IDX(l)];
68 if (b)
69 SIGADDSET(*bss, b);
70 }
71 }
72}
73
74void
75bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
76{
77 int b, l;
78
79 LINUX_SIGEMPTYSET(*lss);
80 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
81 lss->__bits[1] = bss->__bits[1];
82 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
83 if (SIGISMEMBER(*bss, b)) {
84 l = bsd_to_linux_signal[_SIG_IDX(b)];
85 if (l)
86 LINUX_SIGADDSET(*lss, l);
87 }
88 }
89}
90
91static void
92linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
93{
94
95 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
96 bsa->sa_handler = PTRIN(lsa->lsa_handler);
97 bsa->sa_flags = 0;
98 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
99 bsa->sa_flags |= SA_NOCLDSTOP;
100 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
101 bsa->sa_flags |= SA_NOCLDWAIT;
102 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
103 bsa->sa_flags |= SA_SIGINFO;
104 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
105 bsa->sa_flags |= SA_ONSTACK;
106 if (lsa->lsa_flags & LINUX_SA_RESTART)
107 bsa->sa_flags |= SA_RESTART;
108 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
109 bsa->sa_flags |= SA_RESETHAND;
110 if (lsa->lsa_flags & LINUX_SA_NOMASK)
111 bsa->sa_flags |= SA_NODEFER;
112}
113
114static void
115bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
116{
117
118 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
119#ifdef COMPAT_LINUX32
120 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
121#else
122 lsa->lsa_handler = bsa->sa_handler;
123#endif
124 lsa->lsa_restorer = 0; /* unsupported */
125 lsa->lsa_flags = 0;
126 if (bsa->sa_flags & SA_NOCLDSTOP)
127 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
128 if (bsa->sa_flags & SA_NOCLDWAIT)
129 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
130 if (bsa->sa_flags & SA_SIGINFO)
131 lsa->lsa_flags |= LINUX_SA_SIGINFO;
132 if (bsa->sa_flags & SA_ONSTACK)
133 lsa->lsa_flags |= LINUX_SA_ONSTACK;
134 if (bsa->sa_flags & SA_RESTART)
135 lsa->lsa_flags |= LINUX_SA_RESTART;
136 if (bsa->sa_flags & SA_RESETHAND)
137 lsa->lsa_flags |= LINUX_SA_ONESHOT;
138 if (bsa->sa_flags & SA_NODEFER)
139 lsa->lsa_flags |= LINUX_SA_NOMASK;
140}
141
142int
143linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
144 l_sigaction_t *linux_osa)
145{
146 struct sigaction act, oact, *nsa, *osa;
147 int error, sig;
148
149 if (!LINUX_SIG_VALID(linux_sig))
150 return (EINVAL);
151
152 osa = (linux_osa != NULL) ? &oact : NULL;
153 if (linux_nsa != NULL) {
154 nsa = &act;
155 linux_to_bsd_sigaction(linux_nsa, nsa);
156 } else
157 nsa = NULL;
158
159 if (linux_sig <= LINUX_SIGTBLSZ)
160 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
161 else
162 sig = linux_sig;
163
164 error = kern_sigaction(td, sig, nsa, osa, 0);
165 if (error)
166 return (error);
167
168 if (linux_osa != NULL)
169 bsd_to_linux_sigaction(osa, linux_osa);
170
171 return (0);
172}
173
174
175int
176linux_signal(struct thread *td, struct linux_signal_args *args)
177{
178 l_sigaction_t nsa, osa;
179 int error;
180
181#ifdef DEBUG
182 if (ldebug(signal))
183 printf(ARGS(signal, "%d, %p"),
184 args->sig, (void *)(uintptr_t)args->handler);
185#endif
186
187 nsa.lsa_handler = args->handler;
188 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
189 LINUX_SIGEMPTYSET(nsa.lsa_mask);
190
191 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
192 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
193
194 return (error);
195}
196
197int
198linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
199{
200 l_sigaction_t nsa, osa;
201 int error;
202
203#ifdef DEBUG
204 if (ldebug(rt_sigaction))
205 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
206 (long)args->sig, (void *)args->act,
207 (void *)args->oact, (long)args->sigsetsize);
208#endif
209
210 if (args->sigsetsize != sizeof(l_sigset_t))
211 return (EINVAL);
212
213 if (args->act != NULL) {
214 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
215 if (error)
216 return (error);
217 }
218
219 error = linux_do_sigaction(td, args->sig,
220 args->act ? &nsa : NULL,
221 args->oact ? &osa : NULL);
222
223 if (args->oact != NULL && !error) {
224 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
225 }
226
227 return (error);
228}
229
230static int
231linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
232 l_sigset_t *old)
233{
234 sigset_t omask, nmask;
235 sigset_t *nmaskp;
236 int error;
237
238 td->td_retval[0] = 0;
239
240 switch (how) {
241 case LINUX_SIG_BLOCK:
242 how = SIG_BLOCK;
243 break;
244 case LINUX_SIG_UNBLOCK:
245 how = SIG_UNBLOCK;
246 break;
247 case LINUX_SIG_SETMASK:
248 how = SIG_SETMASK;
249 break;
250 default:
251 return (EINVAL);
252 }
253 if (new != NULL) {
254 linux_to_bsd_sigset(new, &nmask);
255 nmaskp = &nmask;
256 } else
257 nmaskp = NULL;
258 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
259 if (error == 0 && old != NULL)
260 bsd_to_linux_sigset(&omask, old);
261
262 return (error);
263}
264
265int
266linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
267{
268 l_osigset_t mask;
269 l_sigset_t set, oset;
270 int error;
271
272#ifdef DEBUG
273 if (ldebug(sigprocmask))
274 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
275#endif
276
277 if (args->mask != NULL) {
278 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
279 if (error)
280 return (error);
281 LINUX_SIGEMPTYSET(set);
282 set.__bits[0] = mask;
283 }
284
285 error = linux_do_sigprocmask(td, args->how,
286 args->mask ? &set : NULL,
287 args->omask ? &oset : NULL);
288
289 if (args->omask != NULL && !error) {
290 mask = oset.__bits[0];
291 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
292 }
293
294 return (error);
295}
296
297int
298linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
299{
300 l_sigset_t set, oset;
301 int error;
302
303#ifdef DEBUG
304 if (ldebug(rt_sigprocmask))
305 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
306 args->how, (void *)args->mask,
307 (void *)args->omask, (long)args->sigsetsize);
308#endif
309
310 if (args->sigsetsize != sizeof(l_sigset_t))
311 return EINVAL;
312
313 if (args->mask != NULL) {
314 error = copyin(args->mask, &set, sizeof(l_sigset_t));
315 if (error)
316 return (error);
317 }
318
319 error = linux_do_sigprocmask(td, args->how,
320 args->mask ? &set : NULL,
321 args->omask ? &oset : NULL);
322
323 if (args->omask != NULL && !error) {
324 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
325 }
326
327 return (error);
328}
329
330int
331linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
332{
333 struct proc *p = td->td_proc;
334 l_sigset_t mask;
335
336#ifdef DEBUG
337 if (ldebug(sgetmask))
338 printf(ARGS(sgetmask, ""));
339#endif
340
341 PROC_LOCK(p);
342 bsd_to_linux_sigset(&td->td_sigmask, &mask);
343 PROC_UNLOCK(p);
344 td->td_retval[0] = mask.__bits[0];
345 return (0);
346}
347
348int
349linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
350{
351 struct proc *p = td->td_proc;
352 l_sigset_t lset;
353 sigset_t bset;
354
355#ifdef DEBUG
356 if (ldebug(ssetmask))
357 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
358#endif
359
360 PROC_LOCK(p);
361 bsd_to_linux_sigset(&td->td_sigmask, &lset);
362 td->td_retval[0] = lset.__bits[0];
363 LINUX_SIGEMPTYSET(lset);
364 lset.__bits[0] = args->mask;
365 linux_to_bsd_sigset(&lset, &bset);
366 td->td_sigmask = bset;
367 SIG_CANTMASK(td->td_sigmask);
368 signotify(td);
369 PROC_UNLOCK(p);
370 return (0);
371}
372
373/*
374 * MPSAFE
375 */
376int
377linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
378{
379 struct proc *p = td->td_proc;
380 sigset_t bset;
381 l_sigset_t lset;
382 l_osigset_t mask;
383
384#ifdef DEBUG
385 if (ldebug(sigpending))
386 printf(ARGS(sigpending, "*"));
387#endif
388
389 PROC_LOCK(p);
390 bset = p->p_siglist;
391 SIGSETOR(bset, td->td_siglist);
392 SIGSETAND(bset, td->td_sigmask);
393 PROC_UNLOCK(p);
394 bsd_to_linux_sigset(&bset, &lset);
395 mask = lset.__bits[0];
396 return (copyout(&mask, args->mask, sizeof(mask)));
397}
398
399/*
400 * MPSAFE
401 */
402int
403linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
404{
405 struct proc *p = td->td_proc;
406 sigset_t bset;
407 l_sigset_t lset;
408
409 if (args->sigsetsize > sizeof(lset))
410 return EINVAL;
411 /* NOT REACHED */
412
413#ifdef DEBUG
414 if (ldebug(rt_sigpending))
415 printf(ARGS(rt_sigpending, "*"));
416#endif
417
418 PROC_LOCK(p);
419 bset = p->p_siglist;
420 SIGSETOR(bset, td->td_siglist);
421 SIGSETAND(bset, td->td_sigmask);
422 PROC_UNLOCK(p);
423 bsd_to_linux_sigset(&bset, &lset);
424 return (copyout(&lset, args->set, args->sigsetsize));
425}
426
427/*
428 * MPSAFE
429 */
430int
431linux_rt_sigtimedwait(struct thread *td,
432 struct linux_rt_sigtimedwait_args *args)
433{
434 int error;
435 l_timeval ltv;
436 struct timeval tv;
437 struct timespec ts, *tsa;
438 l_sigset_t lset;
439 sigset_t bset;
440 l_siginfo_t linfo;
441 ksiginfo_t info;
442
443#ifdef DEBUG
444 if (ldebug(rt_sigtimedwait))
445 printf(ARGS(rt_sigtimedwait, "*"));
446#endif
447 if (args->sigsetsize != sizeof(l_sigset_t))
448 return (EINVAL);
449
450 if ((error = copyin(args->mask, &lset, sizeof(lset))))
451 return (error);
452 linux_to_bsd_sigset(&lset, &bset);
453
454 tsa = NULL;
455 if (args->timeout) {
456 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
457 return (error);
458#ifdef DEBUG
459 if (ldebug(rt_sigtimedwait))
460 printf(LMSG("linux_rt_sigtimedwait: incoming timeout (%d/%d)\n"),
461 ltv.tv_sec, ltv.tv_usec);
462#endif
463 tv.tv_sec = (long)ltv.tv_sec;
464 tv.tv_usec = (suseconds_t)ltv.tv_usec;
465 if (itimerfix(&tv)) {
466 /*
467 * The timeout was invalid. Convert it to something
468 * valid that will act as it does under Linux.
469 */
470 tv.tv_sec += tv.tv_usec / 1000000;
471 tv.tv_usec %= 1000000;
472 if (tv.tv_usec < 0) {
473 tv.tv_sec -= 1;
474 tv.tv_usec += 1000000;
475 }
476 if (tv.tv_sec < 0)
477 timevalclear(&tv);
478#ifdef DEBUG
479 if (ldebug(rt_sigtimedwait))
480 printf(LMSG("linux_rt_sigtimedwait: converted timeout (%jd/%ld)\n"),
481 (intmax_t)tv.tv_sec, tv.tv_usec);
482#endif
483 }
484 TIMEVAL_TO_TIMESPEC(&tv, &ts);
485 tsa = &ts;
486 }
487 error = kern_sigtimedwait(td, bset, &info, tsa);
488#ifdef DEBUG
489 if (ldebug(rt_sigtimedwait))
490 printf(LMSG("linux_rt_sigtimedwait: sigtimedwait returning (%d)\n"), error);
491#endif
492 if (error)
493 return (error);
494
495 if (args->ptr) {
496 memset(&linfo, 0, sizeof(linfo));
497 linfo.lsi_signo = info.ksi_signo;
498 error = copyout(&linfo, args->ptr, sizeof(linfo));
499 }
500
501 /* Repost if we got an error. */
502 if (error && info.ksi_signo) {
503 PROC_LOCK(td->td_proc);
504 tdsignal(td->td_proc, td, info.ksi_signo, &info);
505 PROC_UNLOCK(td->td_proc);
506 } else
507 td->td_retval[0] = info.ksi_signo;
508
509 return (error);
510}
511
512int
513linux_kill(struct thread *td, struct linux_kill_args *args)
514{
515 struct kill_args /* {
516 int pid;
517 int signum;
518 } */ tmp;
519
520#ifdef DEBUG
521 if (ldebug(kill))
522 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
523#endif
524
525 /*
526 * Allow signal 0 as a means to check for privileges
527 */
528 if (!LINUX_SIG_VALID(args->signum) && args->signum != 0)
529 return EINVAL;
530
531 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
532 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
533 else
534 tmp.signum = args->signum;
535
536 tmp.pid = args->pid;
537 return (kill(td, &tmp));
538}
539
538int
539linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
540static int
541linux_do_tkill(struct thread *td, l_int tgid, l_int pid, l_int signum)
540{
542{
541 struct linux_emuldata *em;
542 struct linux_kill_args ka;
543 struct proc *proc = td->td_proc;
544 struct linux_emuldata *em;
543 struct proc *p;
545 struct proc *p;
546 ksiginfo_t ksi;
547 int error;
544
548
545#ifdef DEBUG
546 if (ldebug(tgkill))
547 printf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
548#endif
549 AUDIT_ARG(signum, signum);
550 AUDIT_ARG(pid, pid);
549
551
550 ka.pid = args->pid;
551 ka.signum = args->sig;
552 /*
553 * Allow signal 0 as a means to check for privileges
554 */
555 if (!LINUX_SIG_VALID(signum) && signum != 0)
556 return (EINVAL);
552
557
553 if (args->tgid == -1)
554 return linux_kill(td, &ka);
558 if (signum > 0 && signum <= LINUX_SIGTBLSZ)
559 signum = linux_to_bsd_signal[_SIG_IDX(signum)];
555
560
556 if ((p = pfind(args->pid)) == NULL)
557 return ESRCH;
561 if ((p = pfind(pid)) == NULL) {
562 if ((p = zpfind(pid)) == NULL)
563 return (ESRCH);
564 }
558
565
559 if (p->p_sysent != &elf_linux_sysvec)
560 return ESRCH;
566 AUDIT_ARG(process, p);
567 error = p_cansignal(td, p, signum);
568 if (error)
569 goto out;
561
570
562 PROC_UNLOCK(p);
563
571 error = ESRCH;
564 em = em_find(p, EMUL_DONTLOCK);
565
566 if (em == NULL) {
567#ifdef DEBUG
572 em = em_find(p, EMUL_DONTLOCK);
573
574 if (em == NULL) {
575#ifdef DEBUG
568 printf("emuldata not found in tgkill.\n");
576 printf("emuldata not found in do_tkill.\n");
569#endif
577#endif
570 return ESRCH;
578 goto out;
571 }
579 }
580 if (tgid > 0 && em->shared->group_pid != tgid)
581 goto out;
572
582
573 if (em->shared->group_pid != args->tgid)
574 return ESRCH;
583 ksiginfo_init(&ksi);
584 ksi.ksi_signo = signum;
585 ksi.ksi_code = LINUX_SI_TKILL;
586 ksi.ksi_errno = 0;
587 ksi.ksi_pid = proc->p_pid;
588 ksi.ksi_uid = proc->p_ucred->cr_ruid;
575
589
576 return linux_kill(td, &ka);
590 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
591
592out:
593 PROC_UNLOCK(p);
594 return (error);
577}
578
579int
595}
596
597int
598linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
599{
600
601#ifdef DEBUG
602 if (ldebug(tgkill))
603 printf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
604#endif
605 if (args->pid <= 0 || args->tgid <=0)
606 return (EINVAL);
607
608 return (linux_do_tkill(td, args->tgid, args->pid, args->sig));
609}
610
611int
580linux_tkill(struct thread *td, struct linux_tkill_args *args)
581{
582#ifdef DEBUG
583 if (ldebug(tkill))
584 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
585#endif
612linux_tkill(struct thread *td, struct linux_tkill_args *args)
613{
614#ifdef DEBUG
615 if (ldebug(tkill))
616 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
617#endif
618 if (args->tid <= 0)
619 return (EINVAL);
586
620
587 return (linux_kill(td, (struct linux_kill_args *) args));
621 return (linux_do_tkill(td, 0, args->tid, args->sig));
588}
622}
623
624void
625ksiginfo_to_lsiginfo(ksiginfo_t *ksi, l_siginfo_t *lsi, l_int sig)
626{
627
628 lsi->lsi_signo = sig;
629 lsi->lsi_code = ksi->ksi_code;
630
631 switch (sig) {
632 case LINUX_SIGPOLL:
633 /* XXX si_fd? */
634 lsi->lsi_band = ksi->ksi_band;
635 break;
636 case LINUX_SIGCHLD:
637 lsi->lsi_pid = ksi->ksi_pid;
638 lsi->lsi_uid = ksi->ksi_uid;
639 lsi->lsi_status = ksi->ksi_status;
640 break;
641 case LINUX_SIGBUS:
642 case LINUX_SIGILL:
643 case LINUX_SIGFPE:
644 case LINUX_SIGSEGV:
645 lsi->lsi_addr = PTROUT(ksi->ksi_addr);
646 break;
647 default:
648 /* XXX SI_TIMER etc... */
649 lsi->lsi_pid = ksi->ksi_pid;
650 lsi->lsi_uid = ksi->ksi_uid;
651 break;
652 }
653}