* Implement POSIX (XSI)'s header file re_comp.h
[dragonfly.git] / sys / emulation / linux / linux_signal.c
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 withough 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  * $FreeBSD: src/sys/compat/linux/linux_signal.c,v 1.23.2.3 2001/11/05 19:08:23 marcel Exp $
29  * $DragonFly: src/sys/emulation/linux/linux_signal.c,v 1.9 2003/11/16 01:50:54 daver Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/proc.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysproto.h>
38 #include <sys/kern_syscall.h>
39
40 #include <arch_linux/linux.h>
41 #include <arch_linux/linux_proto.h>
42 #include "linux_signal.h"
43 #include "linux_util.h"
44
45 void
46 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
47 {
48         int b, l;
49
50         SIGEMPTYSET(*bss);
51         bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
52         bss->__bits[1] = lss->__bits[1];
53         for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
54                 if (LINUX_SIGISMEMBER(*lss, l)) {
55 #ifdef __alpha__
56                         b = _SIG_IDX(l);
57 #else
58                         b = linux_to_bsd_signal[_SIG_IDX(l)];
59 #endif
60                         if (b)
61                                 SIGADDSET(*bss, b);
62                 }
63         }
64 }
65
66 void
67 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
68 {
69         int b, l;
70
71         LINUX_SIGEMPTYSET(*lss);
72         lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
73         lss->__bits[1] = bss->__bits[1];
74         for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
75                 if (SIGISMEMBER(*bss, b)) {
76 #if __alpha__
77                         l = _SIG_IDX(b);
78 #else
79                         l = bsd_to_linux_signal[_SIG_IDX(b)];
80 #endif
81                         if (l)
82                                 LINUX_SIGADDSET(*lss, l);
83                 }
84         }
85 }
86
87 void
88 linux_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 = 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
110 void
111 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
112 {
113
114         bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
115         lsa->lsa_handler = bsa->sa_handler;
116         lsa->lsa_restorer = NULL;       /* unsupported */
117         lsa->lsa_flags = 0;
118         if (bsa->sa_flags & SA_NOCLDSTOP)
119                 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
120         if (bsa->sa_flags & SA_NOCLDWAIT)
121                 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
122         if (bsa->sa_flags & SA_SIGINFO)
123                 lsa->lsa_flags |= LINUX_SA_SIGINFO;
124         if (bsa->sa_flags & SA_ONSTACK)
125                 lsa->lsa_flags |= LINUX_SA_ONSTACK;
126         if (bsa->sa_flags & SA_RESTART)
127                 lsa->lsa_flags |= LINUX_SA_RESTART;
128         if (bsa->sa_flags & SA_RESETHAND)
129                 lsa->lsa_flags |= LINUX_SA_ONESHOT;
130         if (bsa->sa_flags & SA_NODEFER)
131                 lsa->lsa_flags |= LINUX_SA_NOMASK;
132 }
133
134 #ifndef __alpha__
135 int
136 linux_signal(struct linux_signal_args *args)
137 {
138         l_sigaction_t linux_nsa, linux_osa;
139         struct sigaction nsa, osa;
140         int error, sig;
141
142 #ifdef DEBUG
143         if (ldebug(signal))
144                 printf(ARGS(signal, "%d, %p"),
145                     args->sig, (void *)args->handler);
146 #endif
147         linux_nsa.lsa_handler = args->handler;
148         linux_nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
149         LINUX_SIGEMPTYSET(linux_nsa.lsa_mask);
150         linux_to_bsd_sigaction(&linux_nsa, &nsa);
151         if (args->sig <= LINUX_SIGTBLSZ) {
152                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
153         } else {
154                 sig = args->sig;
155         }
156
157         error = kern_sigaction(sig, &nsa, &osa);
158
159         bsd_to_linux_sigaction(&osa, &linux_osa);
160         args->sysmsg_result = (int) linux_osa.lsa_handler;
161         return (error);
162 }
163 #endif  /*!__alpha__*/
164
165 int
166 linux_rt_sigaction(struct linux_rt_sigaction_args *args)
167 {
168         l_sigaction_t linux_nsa, linux_osa;
169         struct sigaction nsa, osa;
170         int error, sig;
171
172 #ifdef DEBUG
173         if (ldebug(rt_sigaction))
174                 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
175                     (long)args->sig, (void *)args->act,
176                     (void *)args->oact, (long)args->sigsetsize);
177 #endif
178         if (args->sigsetsize != sizeof(l_sigset_t))
179                 return (EINVAL);
180
181         if (args->act) {
182                 error = copyin(args->act, &linux_nsa, sizeof(linux_nsa));
183                 if (error)
184                         return (error);
185                 linux_to_bsd_sigaction(&linux_nsa, &nsa);
186         }
187         if (args->sig <= LINUX_SIGTBLSZ) {
188                 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
189         } else {
190                 sig = args->sig;
191         }
192
193         error = kern_sigaction(sig, args->act ? &nsa : NULL,
194             args->oact ? &osa : NULL);
195
196         if (error == 0 && args->oact) {
197                 bsd_to_linux_sigaction(&osa, &linux_osa);
198                 error = copyout(&linux_osa, args->oact, sizeof(linux_osa));
199         }
200
201         return (error);
202 }
203
204 static int
205 linux_to_bsd_sigprocmask(int how)
206 {
207         switch (how) {
208         case LINUX_SIG_BLOCK:
209                 return SIG_BLOCK;
210         case LINUX_SIG_UNBLOCK:
211                 return SIG_UNBLOCK;
212         case LINUX_SIG_SETMASK:
213                 return SIG_SETMASK;
214         default:
215                 return (-1);
216         }
217 }
218
219 #ifndef __alpha__
220 int
221 linux_sigprocmask(struct linux_sigprocmask_args *args)
222 {
223         l_osigset_t mask;
224         l_sigset_t linux_set, linux_oset;
225         sigset_t set, oset;
226         int error, how;
227
228 #ifdef DEBUG
229         if (ldebug(sigprocmask))
230                 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
231 #endif
232
233         if (args->mask) {
234                 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
235                 if (error)
236                         return (error);
237                 LINUX_SIGEMPTYSET(linux_set);
238                 linux_set.__bits[0] = mask;
239                 linux_to_bsd_sigset(&linux_set, &set);
240         }
241         how = linux_to_bsd_sigprocmask(args->how);
242
243         error = kern_sigprocmask(how, args->mask ? &set : NULL,
244             args->omask ? &oset : NULL);
245
246         if (error == 0 && args->omask) {
247                 bsd_to_linux_sigset(&oset, &linux_oset);
248                 mask = linux_oset.__bits[0];
249                 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
250         }
251         return (error);
252 }
253 #endif  /*!__alpha__*/
254
255 int
256 linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
257 {
258         l_sigset_t linux_set, linux_oset;
259         sigset_t set, oset;
260         int error, how;
261
262 #ifdef DEBUG
263         if (ldebug(rt_sigprocmask))
264                 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
265                     args->how, (void *)args->mask,
266                     (void *)args->omask, (long)args->sigsetsize);
267 #endif
268
269         if (args->sigsetsize != sizeof(l_sigset_t))
270                 return EINVAL;
271
272         if (args->mask) {
273                 error = copyin(args->mask, &linux_set, sizeof(l_sigset_t));
274                 if (error)
275                         return (error);
276                 linux_to_bsd_sigset(&linux_set, &set);
277         }
278         how = linux_to_bsd_sigprocmask(args->how);
279
280         error = kern_sigprocmask(how, args->mask ? &set : NULL,
281             args->omask ? &oset : NULL);
282
283         if (error == 0 && args->omask) {
284                 bsd_to_linux_sigset(&oset, &linux_oset);
285                 error = copyout(&linux_oset, args->omask, sizeof(l_sigset_t));
286         }
287
288         return (error);
289 }
290
291 #ifndef __alpha__
292 int
293 linux_sgetmask(struct linux_sgetmask_args *args)
294 {
295         struct proc *p = curproc;
296         l_sigset_t mask;
297
298 #ifdef DEBUG
299         if (ldebug(sgetmask))
300                 printf(ARGS(sgetmask, ""));
301 #endif
302
303         bsd_to_linux_sigset(&p->p_sigmask, &mask);
304         args->sysmsg_result = mask.__bits[0];
305         return (0);
306 }
307
308 int
309 linux_ssetmask(struct linux_ssetmask_args *args)
310 {
311         struct proc *p = curproc;
312         l_sigset_t lset;
313         sigset_t bset;
314
315 #ifdef DEBUG
316         if (ldebug(ssetmask))
317                 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
318 #endif
319
320         bsd_to_linux_sigset(&p->p_sigmask, &lset);
321         args->sysmsg_result = lset.__bits[0];
322         LINUX_SIGEMPTYSET(lset);
323         lset.__bits[0] = args->mask;
324         linux_to_bsd_sigset(&lset, &bset);
325         p->p_sigmask = bset;
326         SIG_CANTMASK(p->p_sigmask);
327         return (0);
328 }
329
330 int
331 linux_sigpending(struct linux_sigpending_args *args)
332 {
333         struct thread *td = curthread;
334         struct proc *p = td->td_proc;
335         sigset_t set;
336         l_sigset_t linux_set;
337         l_osigset_t mask;
338         int error;
339
340 #ifdef DEBUG
341         if (ldebug(sigpending))
342                 printf(ARGS(sigpending, "*"));
343 #endif
344
345         error = kern_sigpending(&set);
346
347         if (error == 0) {
348                 SIGSETAND(set, p->p_sigmask);
349                 bsd_to_linux_sigset(&set, &linux_set);
350                 mask = linux_set.__bits[0];
351                 error = copyout(&mask, args->mask, sizeof(mask));
352         }
353         return (error);
354 }
355 #endif  /*!__alpha__*/
356
357 int
358 linux_kill(struct linux_kill_args *args)
359 {
360         int error, sig;
361
362 #ifdef DEBUG
363         if (ldebug(kill))
364                 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
365 #endif
366
367         /*
368          * Allow signal 0 as a means to check for privileges
369          */
370         if (args->signum < 0 || args->signum > LINUX_NSIG)
371                 return EINVAL;
372
373 #ifndef __alpha__
374         if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
375                 sig = linux_to_bsd_signal[_SIG_IDX(args->signum)];
376         else
377 #endif
378                 sig = args->signum;
379
380         error = kern_kill(sig, args->pid);
381
382         return(error);
383 }
384