]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/ibcs2/ibcs2_signal.c
MFV r326007: less v529.
[FreeBSD/FreeBSD.git] / sys / i386 / ibcs2 / ibcs2_signal.c
1 /*-
2  * Copyright (c) 1995 Scott Bartram
3  * Copyright (c) 1995 Steven Wallace
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/signalvar.h>
37 #include <sys/syscallsubr.h>
38 #include <sys/sysproto.h>
39
40 #include <i386/ibcs2/ibcs2_types.h>
41 #include <i386/ibcs2/ibcs2_signal.h>
42 #include <i386/ibcs2/ibcs2_proto.h>
43 #include <i386/ibcs2/ibcs2_xenix.h>
44 #include <i386/ibcs2/ibcs2_util.h>
45
46 #define sigemptyset(s)          SIGEMPTYSET(*(s))
47 #define sigismember(s, n)       SIGISMEMBER(*(s), n)
48 #define sigaddset(s, n)         SIGADDSET(*(s), n)
49
50 #define ibcs2_sigmask(n)        (1 << ((n) - 1))
51 #define ibcs2_sigemptyset(s)    bzero((s), sizeof(*(s)))
52 #define ibcs2_sigismember(s, n) (*(s) & ibcs2_sigmask(n))
53 #define ibcs2_sigaddset(s, n)   (*(s) |= ibcs2_sigmask(n))
54
55 static void ibcs2_to_bsd_sigset(const ibcs2_sigset_t *, sigset_t *);
56 static void bsd_to_ibcs2_sigset(const sigset_t *, ibcs2_sigset_t *);
57 static void ibcs2_to_bsd_sigaction(struct ibcs2_sigaction *,
58                                         struct sigaction *);
59 static void bsd_to_ibcs2_sigaction(struct sigaction *,
60                                         struct ibcs2_sigaction *);
61
62 int bsd_to_ibcs2_sig[IBCS2_SIGTBLSZ] = {
63         IBCS2_SIGHUP,           /* 1 */
64         IBCS2_SIGINT,           /* 2 */
65         IBCS2_SIGQUIT,          /* 3 */
66         IBCS2_SIGILL,           /* 4 */
67         IBCS2_SIGTRAP,          /* 5 */
68         IBCS2_SIGABRT,          /* 6 */
69         IBCS2_SIGEMT,           /* 7 */
70         IBCS2_SIGFPE,           /* 8 */
71         IBCS2_SIGKILL,          /* 9 */
72         IBCS2_SIGBUS,           /* 10 */
73         IBCS2_SIGSEGV,          /* 11 */
74         IBCS2_SIGSYS,           /* 12 */
75         IBCS2_SIGPIPE,          /* 13 */
76         IBCS2_SIGALRM,          /* 14 */
77         IBCS2_SIGTERM,          /* 15 */
78         0,                      /* 16 - SIGURG */
79         IBCS2_SIGSTOP,          /* 17 */
80         IBCS2_SIGTSTP,          /* 18 */
81         IBCS2_SIGCONT,          /* 19 */
82         IBCS2_SIGCLD,           /* 20 */
83         IBCS2_SIGTTIN,          /* 21 */
84         IBCS2_SIGTTOU,          /* 22 */
85         IBCS2_SIGPOLL,          /* 23 */
86         0,                      /* 24 - SIGXCPU */
87         0,                      /* 25 - SIGXFSZ */
88         IBCS2_SIGVTALRM,        /* 26 */
89         IBCS2_SIGPROF,          /* 27 */
90         IBCS2_SIGWINCH,         /* 28 */
91         0,                      /* 29 */
92         IBCS2_SIGUSR1,          /* 30 */
93         IBCS2_SIGUSR2,          /* 31 */
94         0                       /* 32 */
95 };
96
97 static int ibcs2_to_bsd_sig[IBCS2_SIGTBLSZ] = {
98         SIGHUP,                 /* 1 */
99         SIGINT,                 /* 2 */
100         SIGQUIT,                /* 3 */
101         SIGILL,                 /* 4 */
102         SIGTRAP,                /* 5 */
103         SIGABRT,                /* 6 */
104         SIGEMT,                 /* 7 */
105         SIGFPE,                 /* 8 */
106         SIGKILL,                /* 9 */
107         SIGBUS,                 /* 10 */
108         SIGSEGV,                /* 11 */
109         SIGSYS,                 /* 12 */
110         SIGPIPE,                /* 13 */
111         SIGALRM,                /* 14 */
112         SIGTERM,                /* 15 */
113         SIGUSR1,                /* 16 */
114         SIGUSR2,                /* 17 */
115         SIGCHLD,                /* 18 */
116         0,                      /* 19 - SIGPWR */
117         SIGWINCH,               /* 20 */
118         0,                      /* 21 */
119         SIGIO,                  /* 22 */
120         SIGSTOP,                /* 23 */
121         SIGTSTP,                /* 24 */
122         SIGCONT,                /* 25 */
123         SIGTTIN,                /* 26 */
124         SIGTTOU,                /* 27 */
125         SIGVTALRM,              /* 28 */
126         SIGPROF,                /* 29 */
127         0,                      /* 30 */
128         0,                      /* 31 */
129         0                       /* 32 */
130 };
131
132 void
133 ibcs2_to_bsd_sigset(iss, bss)
134         const ibcs2_sigset_t *iss;
135         sigset_t *bss;
136 {
137         int i, newsig;
138
139         sigemptyset(bss);
140         for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
141                 if (ibcs2_sigismember(iss, i)) {
142                         newsig = ibcs2_to_bsd_sig[_SIG_IDX(i)];
143                         if (newsig)
144                                 sigaddset(bss, newsig);
145                 }
146         }
147 }
148
149 static void
150 bsd_to_ibcs2_sigset(bss, iss)
151         const sigset_t *bss;
152         ibcs2_sigset_t *iss;
153 {
154         int i, newsig;
155
156         ibcs2_sigemptyset(iss);
157         for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
158                 if (sigismember(bss, i)) {
159                         newsig = bsd_to_ibcs2_sig[_SIG_IDX(i)];
160                         if (newsig)
161                                 ibcs2_sigaddset(iss, newsig);
162                 }
163         }
164 }
165
166 static void
167 ibcs2_to_bsd_sigaction(isa, bsa)
168         struct ibcs2_sigaction *isa;
169         struct sigaction *bsa;
170 {
171
172         bsa->sa_handler = isa->isa_handler;
173         ibcs2_to_bsd_sigset(&isa->isa_mask, &bsa->sa_mask);
174         bsa->sa_flags = 0;      /* ??? SA_NODEFER */
175         if ((isa->isa_flags & IBCS2_SA_NOCLDSTOP) != 0)
176                 bsa->sa_flags |= SA_NOCLDSTOP;
177 }
178
179 static void
180 bsd_to_ibcs2_sigaction(bsa, isa)
181         struct sigaction *bsa;
182         struct ibcs2_sigaction *isa;
183 {
184
185         isa->isa_handler = bsa->sa_handler;
186         bsd_to_ibcs2_sigset(&bsa->sa_mask, &isa->isa_mask);
187         isa->isa_flags = 0;
188         if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
189                 isa->isa_flags |= IBCS2_SA_NOCLDSTOP;
190 }
191
192 int
193 ibcs2_sigaction(struct thread *td, struct ibcs2_sigaction_args *uap)
194 {
195         struct ibcs2_sigaction isa;
196         struct sigaction nbsa, obsa;
197         struct sigaction *nbsap;
198         int error;
199
200         if (uap->act != NULL) {
201                 if ((error = copyin(uap->act, &isa, sizeof(isa))) != 0)
202                         return (error);
203                 ibcs2_to_bsd_sigaction(&isa, &nbsa);
204                 nbsap = &nbsa;
205         } else
206                 nbsap = NULL;
207         if (uap->sig <= 0 || uap->sig > IBCS2_NSIG)
208                 return (EINVAL);
209         error = kern_sigaction(td, ibcs2_to_bsd_sig[_SIG_IDX(uap->sig)], &nbsa,
210             &obsa, 0);
211         if (error == 0 && uap->oact != NULL) {
212                 bsd_to_ibcs2_sigaction(&obsa, &isa);
213                 error = copyout(&isa, uap->oact, sizeof(isa));
214         }
215         return (error);
216 }
217
218 int
219 ibcs2_sigsys(struct thread *td, struct ibcs2_sigsys_args *uap)
220 {
221         struct proc *p = td->td_proc;
222         struct sigaction sa;
223         int signum = IBCS2_SIGNO(uap->sig);
224         int error;
225
226         if (signum <= 0 || signum > IBCS2_NSIG) {
227                 if (IBCS2_SIGCALL(uap->sig) == IBCS2_SIGNAL_MASK ||
228                     IBCS2_SIGCALL(uap->sig) == IBCS2_SIGSET_MASK)
229                         td->td_retval[0] = (int)IBCS2_SIG_ERR;
230                 return EINVAL;
231         }
232         signum = ibcs2_to_bsd_sig[_SIG_IDX(signum)];
233         
234         switch (IBCS2_SIGCALL(uap->sig)) {
235         case IBCS2_SIGSET_MASK:
236                 /*
237                  * Check for SIG_HOLD action.
238                  * Otherwise, perform signal() except with different sa_flags.
239                  */
240                 if (uap->fp != IBCS2_SIG_HOLD) {
241                         /* add sig to mask before exececuting signal handler */
242                         sa.sa_flags = 0;
243                         goto ibcs2_sigset;
244                 }
245                 /* else FALLTHROUGH to sighold */
246
247         case IBCS2_SIGHOLD_MASK:
248                 {
249                         sigset_t mask;
250
251                         SIGEMPTYSET(mask);
252                         SIGADDSET(mask, signum);
253                         return (kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
254                                     0));
255                 }
256                 
257         case IBCS2_SIGNAL_MASK:
258                 {
259                         struct sigaction osa;
260
261                         /* do not automatically block signal */
262                         sa.sa_flags = SA_NODEFER;
263 #ifdef SA_RESETHAND
264                         if((signum != IBCS2_SIGILL) &&
265                            (signum != IBCS2_SIGTRAP) &&
266                            (signum != IBCS2_SIGPWR))
267                                 /* set to SIG_DFL before executing handler */
268                                 sa.sa_flags |= SA_RESETHAND;
269 #endif
270                 ibcs2_sigset:
271                         sa.sa_handler = uap->fp;
272                         sigemptyset(&sa.sa_mask);
273 #if 0
274                         if (signum != SIGALRM)
275                                 sa.sa_flags |= SA_RESTART;
276 #endif
277                         error = kern_sigaction(td, signum, &sa, &osa, 0);
278                         if (error != 0) {
279                                 DPRINTF(("signal: sigaction failed: %d\n",
280                                          error));
281                                 td->td_retval[0] = (int)IBCS2_SIG_ERR;
282                                 return (error);
283                         }
284                         td->td_retval[0] = (int)osa.sa_handler;
285
286                         /* special sigset() check */
287                         if(IBCS2_SIGCALL(uap->sig) == IBCS2_SIGSET_MASK) {
288                                 PROC_LOCK(p);
289                                 /* check to make sure signal is not blocked */
290                                 if(sigismember(&td->td_sigmask, signum)) {
291                                         /* return SIG_HOLD and unblock signal*/
292                                         td->td_retval[0] = (int)IBCS2_SIG_HOLD;
293                                         SIGDELSET(td->td_sigmask, signum);
294                                         signotify(td);
295                                 }
296                                 PROC_UNLOCK(p);
297                         }
298                                 
299                         return 0;
300                 }
301                 
302         case IBCS2_SIGRELSE_MASK:
303                 {
304                         sigset_t mask;
305
306                         SIGEMPTYSET(mask);
307                         SIGADDSET(mask, signum);
308                         return (kern_sigprocmask(td, SIG_UNBLOCK, &mask, NULL,
309                                     0));
310                 }
311                 
312         case IBCS2_SIGIGNORE_MASK:
313                 {
314                         sa.sa_handler = SIG_IGN;
315                         sigemptyset(&sa.sa_mask);
316                         sa.sa_flags = 0;
317                         error = kern_sigaction(td, signum, &sa, NULL, 0);
318                         if (error != 0)
319                                 DPRINTF(("sigignore: sigaction failed\n"));
320                         return (error);
321                 }
322                 
323         case IBCS2_SIGPAUSE_MASK:
324                 {
325                         sigset_t mask;
326
327                         PROC_LOCK(p);
328                         mask = td->td_sigmask;
329                         PROC_UNLOCK(p);
330                         SIGDELSET(mask, signum);
331                         return kern_sigsuspend(td, mask);
332                 }
333                 
334         default:
335                 return ENOSYS;
336         }
337 }
338
339 int
340 ibcs2_sigprocmask(struct thread *td, struct ibcs2_sigprocmask_args *uap)
341 {
342         ibcs2_sigset_t iss;
343         sigset_t oss, nss;
344         sigset_t *nssp;
345         int error, how;
346
347         switch (uap->how) {
348         case IBCS2_SIG_BLOCK:
349                 how = SIG_BLOCK;
350                 break;
351         case IBCS2_SIG_UNBLOCK:
352                 how = SIG_UNBLOCK;
353                 break;
354         case IBCS2_SIG_SETMASK:
355                 how = SIG_SETMASK;
356                 break;
357         default:
358                 return (EINVAL);
359         }
360         if (uap->set != NULL) {
361                 if ((error = copyin(uap->set, &iss, sizeof(iss))) != 0)
362                         return error;
363                 ibcs2_to_bsd_sigset(&iss, &nss);
364                 nssp = &nss;
365         } else
366                 nssp = NULL;
367         error = kern_sigprocmask(td, how, nssp, &oss, 0);
368         if (error == 0 && uap->oset != NULL) {
369                 bsd_to_ibcs2_sigset(&oss, &iss);
370                 error = copyout(&iss, uap->oset, sizeof(iss));
371         }
372         return (error);
373 }
374
375 int
376 ibcs2_sigpending(struct thread *td, struct ibcs2_sigpending_args *uap)
377 {
378         struct proc *p = td->td_proc;
379         sigset_t bss;
380         ibcs2_sigset_t iss;
381
382         PROC_LOCK(p);
383         bss = td->td_siglist;
384         SIGSETOR(bss, p->p_siglist);
385         SIGSETAND(bss, td->td_sigmask);
386         PROC_UNLOCK(p);
387         bsd_to_ibcs2_sigset(&bss, &iss);
388
389         return copyout(&iss, uap->mask, sizeof(iss));
390 }
391
392 int
393 ibcs2_sigsuspend(struct thread *td, struct ibcs2_sigsuspend_args *uap)
394 {
395         ibcs2_sigset_t sss;
396         sigset_t bss;
397         int error;
398
399         if ((error = copyin(uap->mask, &sss, sizeof(sss))) != 0)
400                 return error;
401
402         ibcs2_to_bsd_sigset(&sss, &bss);
403         return kern_sigsuspend(td, bss);
404 }
405
406 int
407 ibcs2_pause(struct thread *td, struct ibcs2_pause_args *uap)
408 {
409         sigset_t mask;
410
411         PROC_LOCK(td->td_proc);
412         mask = td->td_sigmask;
413         PROC_UNLOCK(td->td_proc);
414         return kern_sigsuspend(td, mask);
415 }
416
417 int
418 ibcs2_kill(struct thread *td, struct ibcs2_kill_args *uap)
419 {
420         struct kill_args ka;
421
422         if (uap->signo <= 0 || uap->signo > IBCS2_NSIG)
423                 return (EINVAL);
424         ka.pid = uap->pid;
425         ka.signum = ibcs2_to_bsd_sig[_SIG_IDX(uap->signo)];
426         return sys_kill(td, &ka);
427 }