kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.6 2003/08/07 21:17:18 dillon 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
39 #include <emulation/linux/machine/linux.h>
40 #include <emulation/linux/machine/linux_proto.h>
41 #include "linux_signal.h"
42 #include "linux_util.h"
43
44 void
45 linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss)
46 {
47         int b, l;
48
49         SIGEMPTYSET(*bss);
50         bss->__bits[0] = lss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
51         bss->__bits[1] = lss->__bits[1];
52         for (l = 1; l <= LINUX_SIGTBLSZ; l++) {
53                 if (LINUX_SIGISMEMBER(*lss, l)) {
54 #ifdef __alpha__
55                         b = _SIG_IDX(l);
56 #else
57                         b = linux_to_bsd_signal[_SIG_IDX(l)];
58 #endif
59                         if (b)
60                                 SIGADDSET(*bss, b);
61                 }
62         }
63 }
64
65 void
66 bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss)
67 {
68         int b, l;
69
70         LINUX_SIGEMPTYSET(*lss);
71         lss->__bits[0] = bss->__bits[0] & ~((1U << LINUX_SIGTBLSZ) - 1);
72         lss->__bits[1] = bss->__bits[1];
73         for (b = 1; b <= LINUX_SIGTBLSZ; b++) {
74                 if (SIGISMEMBER(*bss, b)) {
75 #if __alpha__
76                         l = _SIG_IDX(b);
77 #else
78                         l = bsd_to_linux_signal[_SIG_IDX(b)];
79 #endif
80                         if (l)
81                                 LINUX_SIGADDSET(*lss, l);
82                 }
83         }
84 }
85
86 static void
87 linux_to_bsd_sigaction(l_sigaction_t *lsa, struct sigaction *bsa)
88 {
89
90         linux_to_bsd_sigset(&lsa->lsa_mask, &bsa->sa_mask);
91         bsa->sa_handler = lsa->lsa_handler;
92         bsa->sa_flags = 0;
93         if (lsa->lsa_flags & LINUX_SA_NOCLDSTOP)
94                 bsa->sa_flags |= SA_NOCLDSTOP;
95         if (lsa->lsa_flags & LINUX_SA_NOCLDWAIT)
96                 bsa->sa_flags |= SA_NOCLDWAIT;
97         if (lsa->lsa_flags & LINUX_SA_SIGINFO)
98                 bsa->sa_flags |= SA_SIGINFO;
99         if (lsa->lsa_flags & LINUX_SA_ONSTACK)
100                 bsa->sa_flags |= SA_ONSTACK;
101         if (lsa->lsa_flags & LINUX_SA_RESTART)
102                 bsa->sa_flags |= SA_RESTART;
103         if (lsa->lsa_flags & LINUX_SA_ONESHOT)
104                 bsa->sa_flags |= SA_RESETHAND;
105         if (lsa->lsa_flags & LINUX_SA_NOMASK)
106                 bsa->sa_flags |= SA_NODEFER;
107 }
108
109 static void
110 bsd_to_linux_sigaction(struct sigaction *bsa, l_sigaction_t *lsa)
111 {
112
113         bsd_to_linux_sigset(&bsa->sa_mask, &lsa->lsa_mask);
114         lsa->lsa_handler = bsa->sa_handler;
115         lsa->lsa_restorer = NULL;       /* unsupported */
116         lsa->lsa_flags = 0;
117         if (bsa->sa_flags & SA_NOCLDSTOP)
118                 lsa->lsa_flags |= LINUX_SA_NOCLDSTOP;
119         if (bsa->sa_flags & SA_NOCLDWAIT)
120                 lsa->lsa_flags |= LINUX_SA_NOCLDWAIT;
121         if (bsa->sa_flags & SA_SIGINFO)
122                 lsa->lsa_flags |= LINUX_SA_SIGINFO;
123         if (bsa->sa_flags & SA_ONSTACK)
124                 lsa->lsa_flags |= LINUX_SA_ONSTACK;
125         if (bsa->sa_flags & SA_RESTART)
126                 lsa->lsa_flags |= LINUX_SA_RESTART;
127         if (bsa->sa_flags & SA_RESETHAND)
128                 lsa->lsa_flags |= LINUX_SA_ONESHOT;
129         if (bsa->sa_flags & SA_NODEFER)
130                 lsa->lsa_flags |= LINUX_SA_NOMASK;
131 }
132
133 int
134 linux_do_sigaction(int linux_sig, l_sigaction_t *linux_nsa,
135                    l_sigaction_t *linux_osa, int *res)
136 {
137         struct sigaction *nsa, *osa;
138         struct sigaction_args sa_args;
139         int error;
140         caddr_t sg = stackgap_init();
141
142         if (linux_sig <= 0 || linux_sig > LINUX_NSIG)
143                 return (EINVAL);
144
145         if (linux_osa != NULL)
146                 osa = stackgap_alloc(&sg, sizeof(struct sigaction));
147         else
148                 osa = NULL;
149
150         if (linux_nsa != NULL) {
151                 nsa = stackgap_alloc(&sg, sizeof(struct sigaction));
152                 linux_to_bsd_sigaction(linux_nsa, nsa);
153         }
154         else
155                 nsa = NULL;
156
157 #ifndef __alpha__
158         if (linux_sig <= LINUX_SIGTBLSZ)
159                 sa_args.sig = linux_to_bsd_signal[_SIG_IDX(linux_sig)];
160         else
161 #endif
162                 sa_args.sig = linux_sig;
163
164         sa_args.act = nsa;
165         sa_args.oact = osa;
166         sa_args.sysmsg_result = 0;
167         error = sigaction(&sa_args);
168         if (error)
169                 return (error);
170         *res = sa_args.sysmsg_result;
171
172         if (linux_osa != NULL)
173                 bsd_to_linux_sigaction(osa, linux_osa);
174
175         return (0);
176 }
177
178
179 #ifndef __alpha__
180 int
181 linux_signal(struct linux_signal_args *args)
182 {
183         l_sigaction_t nsa, osa;
184         int error;
185         int dummy;
186
187 #ifdef DEBUG
188         if (ldebug(signal))
189                 printf(ARGS(signal, "%d, %p"),
190                     args->sig, (void *)args->handler);
191 #endif
192
193         nsa.lsa_handler = args->handler;
194         nsa.lsa_flags = LINUX_SA_ONESHOT | LINUX_SA_NOMASK;
195         LINUX_SIGEMPTYSET(nsa.lsa_mask);
196
197         error = linux_do_sigaction(args->sig, &nsa, &osa, &dummy);
198         args->sysmsg_result = (int)osa.lsa_handler;
199
200         return (error);
201 }
202 #endif  /*!__alpha__*/
203
204 int
205 linux_rt_sigaction(struct linux_rt_sigaction_args *args)
206 {
207         l_sigaction_t nsa, osa;
208         int error;
209
210 #ifdef DEBUG
211         if (ldebug(rt_sigaction))
212                 printf(ARGS(rt_sigaction, "%ld, %p, %p, %ld"),
213                     (long)args->sig, (void *)args->act,
214                     (void *)args->oact, (long)args->sigsetsize);
215 #endif
216
217         if (args->sigsetsize != sizeof(l_sigset_t))
218                 return (EINVAL);
219
220         if (args->act != NULL) {
221                 error = copyin(args->act, &nsa, sizeof(l_sigaction_t));
222                 if (error)
223                         return (error);
224         }
225
226         error = linux_do_sigaction(args->sig,
227                                    args->act ? &nsa : NULL,
228                                    args->oact ? &osa : NULL,
229                                    &args->sysmsg_result);
230
231         if (args->oact != NULL && !error) {
232                 error = copyout(&osa, args->oact, sizeof(l_sigaction_t));
233         }
234
235         return (error);
236 }
237
238 static int
239 linux_do_sigprocmask(int how, l_sigset_t *new, l_sigset_t *old, int *res)
240 {
241         struct proc *p = curproc;
242         int error;
243         sigset_t mask;
244
245         error = 0;
246         *res = 0;
247
248         if (old != NULL)
249                 bsd_to_linux_sigset(&p->p_sigmask, old);
250
251         if (new != NULL) {
252                 linux_to_bsd_sigset(new, &mask);
253
254                 switch (how) {
255                 case LINUX_SIG_BLOCK:
256                         SIGSETOR(p->p_sigmask, mask);
257                         SIG_CANTMASK(p->p_sigmask);
258                         break;
259                 case LINUX_SIG_UNBLOCK:
260                         SIGSETNAND(p->p_sigmask, mask);
261                         break;
262                 case LINUX_SIG_SETMASK:
263                         p->p_sigmask = mask;
264                         SIG_CANTMASK(p->p_sigmask);
265                         break;
266                 default:
267                         error = EINVAL;
268                         break;
269                 }
270         }
271
272         return (error);
273 }
274
275 #ifndef __alpha__
276 int
277 linux_sigprocmask(struct linux_sigprocmask_args *args)
278 {
279         l_osigset_t mask;
280         l_sigset_t set, oset;
281         int error;
282
283 #ifdef DEBUG
284         if (ldebug(sigprocmask))
285                 printf(ARGS(sigprocmask, "%d, *, *"), args->how);
286 #endif
287
288         if (args->mask != NULL) {
289                 error = copyin(args->mask, &mask, sizeof(l_osigset_t));
290                 if (error)
291                         return (error);
292                 LINUX_SIGEMPTYSET(set);
293                 set.__bits[0] = mask;
294         }
295
296         error = linux_do_sigprocmask(args->how,
297                                      args->mask ? &set : NULL,
298                                      args->omask ? &oset : NULL,
299                                      &args->sysmsg_result);
300
301         if (args->omask != NULL && !error) {
302                 mask = oset.__bits[0];
303                 error = copyout(&mask, args->omask, sizeof(l_osigset_t));
304         }
305
306         return (error);
307 }
308 #endif  /*!__alpha__*/
309
310 int
311 linux_rt_sigprocmask(struct linux_rt_sigprocmask_args *args)
312 {
313         l_sigset_t set, oset;
314         int error;
315
316 #ifdef DEBUG
317         if (ldebug(rt_sigprocmask))
318                 printf(ARGS(rt_sigprocmask, "%d, %p, %p, %ld"),
319                     args->how, (void *)args->mask,
320                     (void *)args->omask, (long)args->sigsetsize);
321 #endif
322
323         if (args->sigsetsize != sizeof(l_sigset_t))
324                 return EINVAL;
325
326         if (args->mask != NULL) {
327                 error = copyin(args->mask, &set, sizeof(l_sigset_t));
328                 if (error)
329                         return (error);
330         }
331
332         error = linux_do_sigprocmask(args->how,
333                                      args->mask ? &set : NULL,
334                                      args->omask ? &oset : NULL,
335                                      &args->sysmsg_result);
336
337         if (args->omask != NULL && !error) {
338                 error = copyout(&oset, args->omask, sizeof(l_sigset_t));
339         }
340
341         return (error);
342 }
343
344 #ifndef __alpha__
345 int
346 linux_sgetmask(struct linux_sgetmask_args *args)
347 {
348         struct proc *p = curproc;
349         l_sigset_t mask;
350
351 #ifdef DEBUG
352         if (ldebug(sgetmask))
353                 printf(ARGS(sgetmask, ""));
354 #endif
355
356         bsd_to_linux_sigset(&p->p_sigmask, &mask);
357         args->sysmsg_result = mask.__bits[0];
358         return (0);
359 }
360
361 int
362 linux_ssetmask(struct linux_ssetmask_args *args)
363 {
364         struct proc *p = curproc;
365         l_sigset_t lset;
366         sigset_t bset;
367
368 #ifdef DEBUG
369         if (ldebug(ssetmask))
370                 printf(ARGS(ssetmask, "%08lx"), (unsigned long)args->mask);
371 #endif
372
373         bsd_to_linux_sigset(&p->p_sigmask, &lset);
374         args->sysmsg_result = lset.__bits[0];
375         LINUX_SIGEMPTYSET(lset);
376         lset.__bits[0] = args->mask;
377         linux_to_bsd_sigset(&lset, &bset);
378         p->p_sigmask = bset;
379         SIG_CANTMASK(p->p_sigmask);
380         return (0);
381 }
382
383 int
384 linux_sigpending(struct linux_sigpending_args *args)
385 {
386         struct proc *p = curproc;
387         sigset_t bset;
388         l_sigset_t lset;
389         l_osigset_t mask;
390
391 #ifdef DEBUG
392         if (ldebug(sigpending))
393                 printf(ARGS(sigpending, "*"));
394 #endif
395
396         bset = p->p_siglist;
397         SIGSETAND(bset, p->p_sigmask);
398         bsd_to_linux_sigset(&bset, &lset);
399         mask = lset.__bits[0];
400         return (copyout(&mask, args->mask, sizeof(mask)));
401 }
402 #endif  /*!__alpha__*/
403
404 int
405 linux_kill(struct linux_kill_args *args)
406 {
407         struct kill_args ka;
408         int error;
409
410 #ifdef DEBUG
411         if (ldebug(kill))
412                 printf(ARGS(kill, "%d, %d"), args->pid, args->signum);
413 #endif
414
415         /*
416          * Allow signal 0 as a means to check for privileges
417          */
418         if (args->signum < 0 || args->signum > LINUX_NSIG)
419                 return EINVAL;
420
421 #ifndef __alpha__
422         if (args->signum > 0 && args->signum <= LINUX_SIGTBLSZ)
423                 ka.signum = linux_to_bsd_signal[_SIG_IDX(args->signum)];
424         else
425 #endif
426                 ka.signum = args->signum;
427
428         ka.pid = args->pid;
429         ka.sysmsg_result = 0;
430         error = kill(&ka);
431         args->sysmsg_result = ka.sysmsg_result;
432         return(error);
433 }
434