Deleted Added
full compact
linux_signal.c (158415) linux_signal.c (161310)
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 158415 2006-05-10 20:38:16Z netchild $");
30__FBSDID("$FreeBSD: head/sys/compat/linux/linux_signal.c 161310 2006-08-15 12:54:30Z netchild $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/proc.h>
37#include <sys/signalvar.h>
38#include <sys/syscallsubr.h>
39#include <sys/sysproto.h>
40
41#include "opt_compat.h"
42
43#ifdef COMPAT_LINUX32
44#include <machine/../linux32/linux.h>
45#include <machine/../linux32/linux32_proto.h>
46#else
47#include <machine/../linux/linux.h>
48#include <machine/../linux/linux_proto.h>
49#endif
50#include <compat/linux/linux_signal.h>
51#include <compat/linux/linux_util.h>
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/proc.h>
37#include <sys/signalvar.h>
38#include <sys/syscallsubr.h>
39#include <sys/sysproto.h>
40
41#include "opt_compat.h"
42
43#ifdef COMPAT_LINUX32
44#include <machine/../linux32/linux.h>
45#include <machine/../linux32/linux32_proto.h>
46#else
47#include <machine/../linux/linux.h>
48#include <machine/../linux/linux_proto.h>
49#endif
50#include <compat/linux/linux_signal.h>
51#include <compat/linux/linux_util.h>
52#include <compat/linux/linux_emul.h>
52
53
54extern struct sx emul_shared_lock;
55extern struct sx emul_lock;
56
53void
54linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
55{
56 int b, l;
57
58 SIGEMPTYSET(*bss);
59 bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
60 bss->__bits[1] = lss->__bits[1];
61 for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
62 if (LINUX_SIGISMEMBER(*lss, l)) {
63 b = linux_to_bsd_signal[_SIG_IDX(l)];
64 if (b)
65 SIGADDSET(*bss, b);
66 }
67 }
68}
69
70void
71bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
72{
73 int b, l;
74
75 LINUX_SIGEMPTYSET(*lss);
76 lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
77 lss->__bits[1] = bss->__bits[1];
78 for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
79 if (SIGISMEMBER(*bss, b)) {
80 l = bsd_to_linux_signal[_SIG_IDX(b)];
81 if (l)
82 LINUX_SIGADDSET(*lss, l);
83 }
84 }
85}
86
87static void
88linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
89{
90
91 linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
92 bsa->sa_handler = PTRIN(lsa->lsa_handler);
93 bsa->sa_flags = 0;
94 if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
95 bsa->sa_flags |= SA_NOCLDSTOP;
96 if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
97 bsa->sa_flags |= SA_NOCLDWAIT;
98 if (lsa->lsa_flags & LINUX_SA_SIGINFO)
99 bsa->sa_flags |= SA_SIGINFO;
100 if (lsa->lsa_flags & LINUX_SA_ONSTACK)
101 bsa->sa_flags |= SA_ONSTACK;
102 if (lsa->lsa_flags & LINUX_SA_RESTART)
103 bsa->sa_flags |= SA_RESTART;
104 if (lsa->lsa_flags & LINUX_SA_ONESHOT)
105 bsa->sa_flags |= SA_RESETHAND;
106 if (lsa->lsa_flags & LINUX_SA_NOMASK)
107 bsa->sa_flags |= SA_NODEFER;
108}
109
110static void
111bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
112{
113
114 bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
115#ifdef COMPAT_LINUX32
116 lsa->lsa_handler = (uintptr_t)bsa->sa_handler;
117#else
118 lsa->lsa_handler = bsa->sa_handler;
119#endif
120 lsa->lsa_restorer = 0; /* unsupported */
121 lsa->lsa_flags = 0;
122 if (bsa->sa_flags & SA_NOCLDSTOP)
123 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
124 if (bsa->sa_flags & SA_NOCLDWAIT)
125 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
126 if (bsa->sa_flags & SA_SIGINFO)
127 lsa->lsa_flags |= LINUX_SA_SIGINFO;
128 if (bsa->sa_flags & SA_ONSTACK)
129 lsa->lsa_flags |= LINUX_SA_ONSTACK;
130 if (bsa->sa_flags & SA_RESTART)
131 lsa->lsa_flags |= LINUX_SA_RESTART;
132 if (bsa->sa_flags & SA_RESETHAND)
133 lsa->lsa_flags |= LINUX_SA_ONESHOT;
134 if (bsa->sa_flags & SA_NODEFER)
135 lsa->lsa_flags |= LINUX_SA_NOMASK;
136}
137
138int
139linux_do_sigaction(struct thread *td, int linux_sig, l_sigaction_t *linux_nsa,
140 l_sigaction_t *linux_osa)
141{
142 struct sigaction act, oact, *nsa, *osa;
143 int error, sig;
144
145 if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
146 return (EINVAL);
147
148 osa = (linux_osa != NULL) ? &oact : NULL;
149 if (linux_nsa != NULL) {
150 nsa = &act;
151 linux_to_bsd_sigaction(linux_nsa, nsa);
152 } else
153 nsa = NULL;
154
155 if (linux_sig <= LINUX_SIGTBLSZ)
156 sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
157 else
158 sig = linux_sig;
159
160 error = kern_sigaction(td, sig, nsa, osa, 0);
161 if (error)
162 return (error);
163
164 if (linux_osa != NULL)
165 bsd_to_linux_sigaction(osa, linux_osa);
166
167 return (0);
168}
169
170
171int
172linux_signal(struct thread *td, struct linux_signal_args *args)
173{
174 l_sigaction_t nsa, osa;
175 int error;
176
177#ifdef DEBUG
178 if (ldebug(signal))
179 printf(ARGS(signal, "%d, %p"),
180 args->sig, (void *)(uintptr_t)args->handler);
181#endif
182
183 nsa.lsa_handler = args->handler;
184 nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
185 LINUX_SIGEMPTYSET(nsa.lsa_mask);
186
187 error = linux_do_sigaction(td, args->sig, &nsa, &osa);
188 td->td_retval[0] = (int)(intptr_t)osa.lsa_handler;
189
190 return (error);
191}
192
193int
194linux_rt_sigaction(struct thread *td, struct linux_rt_sigaction_args *args)
195{
196 l_sigaction_t nsa, osa;
197 int error;
198
199#ifdef DEBUG
200 if (ldebug(rt_sigaction))
201 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
202 (long)args->sig, (void *)args->act,
203 (void *)args->oact, (long)args->sigsetsize);
204#endif
205
206 if (args->sigsetsize != sizeof(l_sigset_t))
207 return (EINVAL);
208
209 if (args->act != NULL) {
210 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
211 if (error)
212 return (error);
213 }
214
215 error = linux_do_sigaction(td, args->sig,
216 args->act ? &nsa : NULL,
217 args->oact ? &osa : NULL);
218
219 if (args->oact != NULL && !error) {
220 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
221 }
222
223 return (error);
224}
225
226static int
227linux_do_sigprocmask(struct thread *td, int how, l_sigset_t *new,
228 l_sigset_t *old)
229{
230 sigset_t omask, nmask;
231 sigset_t *nmaskp;
232 int error;
233
234 td->td_retval[0] = 0;
235
236 switch (how) {
237 case LINUX_SIG_BLOCK:
238 how = SIG_BLOCK;
239 break;
240 case LINUX_SIG_UNBLOCK:
241 how = SIG_UNBLOCK;
242 break;
243 case LINUX_SIG_SETMASK:
244 how = SIG_SETMASK;
245 break;
246 default:
247 return (EINVAL);
248 }
249 if (new != NULL) {
250 linux_to_bsd_sigset(new, &nmask);
251 nmaskp = &nmask;
252 } else
253 nmaskp = NULL;
254 error = kern_sigprocmask(td, how, nmaskp, &omask, 0);
255 if (error == 0 && old != NULL)
256 bsd_to_linux_sigset(&omask, old);
257
258 return (error);
259}
260
261int
262linux_sigprocmask(struct thread *td, struct linux_sigprocmask_args *args)
263{
264 l_osigset_t mask;
265 l_sigset_t set, oset;
266 int error;
267
268#ifdef DEBUG
269 if (ldebug(sigprocmask))
270 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
271#endif
272
273 if (args->mask != NULL) {
274 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
275 if (error)
276 return (error);
277 LINUX_SIGEMPTYSET(set);
278 set.__bits[0] = mask;
279 }
280
281 error = linux_do_sigprocmask(td, args->how,
282 args->mask ? &set : NULL,
283 args->omask ? &oset : NULL);
284
285 if (args->omask != NULL && !error) {
286 mask = oset.__bits[0];
287 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
288 }
289
290 return (error);
291}
292
293int
294linux_rt_sigprocmask(struct thread *td, struct linux_rt_sigprocmask_args *args)
295{
296 l_sigset_t set, oset;
297 int error;
298
299#ifdef DEBUG
300 if (ldebug(rt_sigprocmask))
301 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
302 args->how, (void *)args->mask,
303 (void *)args->omask, (long)args->sigsetsize);
304#endif
305
306 if (args->sigsetsize != sizeof(l_sigset_t))
307 return EINVAL;
308
309 if (args->mask != NULL) {
310 error = copyin(args->mask, &set, sizeof(l_sigset_t));
311 if (error)
312 return (error);
313 }
314
315 error = linux_do_sigprocmask(td, args->how,
316 args->mask ? &set : NULL,
317 args->omask ? &oset : NULL);
318
319 if (args->omask != NULL && !error) {
320 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
321 }
322
323 return (error);
324}
325
326int
327linux_sgetmask(struct thread *td, struct linux_sgetmask_args *args)
328{
329 struct proc *p = td->td_proc;
330 l_sigset_t mask;
331
332#ifdef DEBUG
333 if (ldebug(sgetmask))
334 printf(ARGS(sgetmask, ""));
335#endif
336
337 PROC_LOCK(p);
338 bsd_to_linux_sigset(&td->td_sigmask, &mask);
339 PROC_UNLOCK(p);
340 td->td_retval[0] = mask.__bits[0];
341 return (0);
342}
343
344int
345linux_ssetmask(struct thread *td, struct linux_ssetmask_args *args)
346{
347 struct proc *p = td->td_proc;
348 l_sigset_t lset;
349 sigset_t bset;
350
351#ifdef DEBUG
352 if (ldebug(ssetmask))
353 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
354#endif
355
356 PROC_LOCK(p);
357 bsd_to_linux_sigset(&td->td_sigmask, &lset);
358 td->td_retval[0] = lset.__bits[0];
359 LINUX_SIGEMPTYSET(lset);
360 lset.__bits[0] = args->mask;
361 linux_to_bsd_sigset(&lset, &bset);
362 td->td_sigmask = bset;
363 SIG_CANTMASK(td->td_sigmask);
364 signotify(td);
365 PROC_UNLOCK(p);
366 return (0);
367}
368
369/*
370 * MPSAFE
371 */
372int
373linux_sigpending(struct thread *td, struct linux_sigpending_args *args)
374{
375 struct proc *p = td->td_proc;
376 sigset_t bset;
377 l_sigset_t lset;
378 l_osigset_t mask;
379
380#ifdef DEBUG
381 if (ldebug(sigpending))
382 printf(ARGS(sigpending, "*"));
383#endif
384
385 PROC_LOCK(p);
386 bset = p->p_siglist;
387 SIGSETOR(bset, td->td_siglist);
388 SIGSETAND(bset, td->td_sigmask);
389 PROC_UNLOCK(p);
390 bsd_to_linux_sigset(&bset, &lset);
391 mask = lset.__bits[0];
392 return (copyout(&mask, args->mask, sizeof(mask)));
393}
394
395/*
396 * MPSAFE
397 */
398int
399linux_rt_sigpending(struct thread *td, struct linux_rt_sigpending_args *args)
400{
401 struct proc *p = td->td_proc;
402 sigset_t bset;
403 l_sigset_t lset;
404
405 if (args->sigsetsize > sizeof(lset))
406 return EINVAL;
407 /* NOT REACHED */
408
409#ifdef DEBUG
410 if (ldebug(rt_sigpending))
411 printf(ARGS(rt_sigpending, "*"));
412#endif
413
414 PROC_LOCK(p);
415 bset = p->p_siglist;
416 SIGSETOR(bset, td->td_siglist);
417 SIGSETAND(bset, td->td_sigmask);
418 PROC_UNLOCK(p);
419 bsd_to_linux_sigset(&bset, &lset);
420 return (copyout(&lset, args->set, args->sigsetsize));
421}
422
423int
424linux_kill(struct thread *td, struct linux_kill_args *args)
425{
426 struct kill_args /* {
427 int pid;
428 int signum;
429 } */ tmp;
430
431#ifdef DEBUG
432 if (ldebug(kill))
433 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
434#endif
435
436 /*
437 * Allow signal 0 as a means to check for privileges
438 */
439 if (args->signum < 0 || args->signum > LINUX_NSIG)
440 return EINVAL;
441
442 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
443 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
444 else
445 tmp.signum = args->signum;
446
447 tmp.pid = args->pid;
448 return (kill(td, &tmp));
449}
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 <= 0 || linux_sig > LINUX_NSIG)
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
427int
428linux_kill(struct thread *td, struct linux_kill_args *args)
429{
430 struct kill_args /* {
431 int pid;
432 int signum;
433 } */ tmp;
434
435#ifdef DEBUG
436 if (ldebug(kill))
437 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
438#endif
439
440 /*
441 * Allow signal 0 as a means to check for privileges
442 */
443 if (args->signum < 0 || args->signum > LINUX_NSIG)
444 return EINVAL;
445
446 if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
447 tmp.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
448 else
449 tmp.signum = args->signum;
450
451 tmp.pid = args->pid;
452 return (kill(td, &tmp));
453}
454
455int
456linux_tgkill(struct thread *td, struct linux_tgkill_args *args)
457{
458 struct linux_emuldata *em;
459 struct linux_kill_args ka;
460 struct proc *p;
461
462#ifdef DEBUG
463 if (ldebug(tgkill))
464 printf(ARGS(tgkill, "%d, %d, %d"), args->tgid, args->pid, args->sig);
465#endif
466
467 ka.pid = args->pid;
468 ka.signum = args->sig;
469
470 if (args->tgid == -1)
471 return linux_kill(td, &ka);
472
473 if ((p = pfind(args->pid)) == NULL)
474 return ESRCH;
475
476 if (p->p_sysent != &elf_linux_sysvec)
477 return ESRCH;
478
479 PROC_UNLOCK(p);
480
481 em = em_find(p, EMUL_UNLOCKED);
482
483 if (em == NULL) {
484#ifdef DEBUG
485 printf("emuldata not found in tgkill.\n");
486#endif
487 return ESRCH;
488 }
489
490 if (em->shared->group_pid != args->tgid)
491 return ESRCH;
492
493 EMUL_UNLOCK(&emul_lock);
494
495 return linux_kill(td, &ka);
496}
497
498int
499linux_tkill(struct thread *td, struct linux_tkill_args *args)
500{
501#ifdef DEBUG
502 if (ldebug(tkill))
503 printf(ARGS(tkill, "%i, %i"), args->tid, args->sig);
504#endif
505
506 return (linux_kill(td, (struct linux_kill_args *) args));
507}