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 * $FreeBSD: releng/5.1/sys/i386/ibcs2/ibcs2_signal.c 113859 2003-04-22 18:23:49Z jhb $
29 */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/signalvar.h>
36 #include <sys/syscallsubr.h>
37 #include <sys/sysproto.h>
38
39 #include <i386/ibcs2/ibcs2_types.h>
40 #include <i386/ibcs2/ibcs2_signal.h>
41 #include <i386/ibcs2/ibcs2_proto.h>
42 #include <i386/ibcs2/ibcs2_xenix.h>
43 #include <i386/ibcs2/ibcs2_util.h>
44
45 #define sigemptyset(s) SIGEMPTYSET(*(s))
46 #define sigismember(s, n) SIGISMEMBER(*(s), n)
47 #define sigaddset(s, n) SIGADDSET(*(s), n)
48
49 #define ibcs2_sigmask(n) (1 << ((n) - 1))
50 #define ibcs2_sigemptyset(s) bzero((s), sizeof(*(s)))
51 #define ibcs2_sigismember(s, n) (*(s) & ibcs2_sigmask(n))
52 #define ibcs2_sigaddset(s, n) (*(s) |= ibcs2_sigmask(n))
53
54 static void ibcs2_to_bsd_sigset(const ibcs2_sigset_t *, sigset_t *);
55 static void bsd_to_ibcs2_sigset(const sigset_t *, ibcs2_sigset_t *);
56 static void ibcs2_to_bsd_sigaction(struct ibcs2_sigaction *,
57 struct sigaction *);
58 static void bsd_to_ibcs2_sigaction(struct sigaction *,
59 struct ibcs2_sigaction *);
60
61 int bsd_to_ibcs2_sig[IBCS2_SIGTBLSZ] = {
62 IBCS2_SIGHUP, /* 1 */
63 IBCS2_SIGINT, /* 2 */
64 IBCS2_SIGQUIT, /* 3 */
65 IBCS2_SIGILL, /* 4 */
66 IBCS2_SIGTRAP, /* 5 */
67 IBCS2_SIGABRT, /* 6 */
68 IBCS2_SIGEMT, /* 7 */
69 IBCS2_SIGFPE, /* 8 */
70 IBCS2_SIGKILL, /* 9 */
71 IBCS2_SIGBUS, /* 10 */
72 IBCS2_SIGSEGV, /* 11 */
73 IBCS2_SIGSYS, /* 12 */
74 IBCS2_SIGPIPE, /* 13 */
75 IBCS2_SIGALRM, /* 14 */
76 IBCS2_SIGTERM, /* 15 */
77 0, /* 16 - SIGURG */
78 IBCS2_SIGSTOP, /* 17 */
79 IBCS2_SIGTSTP, /* 18 */
80 IBCS2_SIGCONT, /* 19 */
81 IBCS2_SIGCLD, /* 20 */
82 IBCS2_SIGTTIN, /* 21 */
83 IBCS2_SIGTTOU, /* 22 */
84 IBCS2_SIGPOLL, /* 23 */
85 0, /* 24 - SIGXCPU */
86 0, /* 25 - SIGXFSZ */
87 IBCS2_SIGVTALRM, /* 26 */
88 IBCS2_SIGPROF, /* 27 */
89 IBCS2_SIGWINCH, /* 28 */
90 0, /* 29 */
91 IBCS2_SIGUSR1, /* 30 */
92 IBCS2_SIGUSR2, /* 31 */
93 0 /* 32 */
94 };
95
96 static int ibcs2_to_bsd_sig[IBCS2_SIGTBLSZ] = {
97 SIGHUP, /* 1 */
98 SIGINT, /* 2 */
99 SIGQUIT, /* 3 */
100 SIGILL, /* 4 */
101 SIGTRAP, /* 5 */
102 SIGABRT, /* 6 */
103 SIGEMT, /* 7 */
104 SIGFPE, /* 8 */
105 SIGKILL, /* 9 */
106 SIGBUS, /* 10 */
107 SIGSEGV, /* 11 */
108 SIGSYS, /* 12 */
109 SIGPIPE, /* 13 */
110 SIGALRM, /* 14 */
111 SIGTERM, /* 15 */
112 SIGUSR1, /* 16 */
113 SIGUSR2, /* 17 */
114 SIGCHLD, /* 18 */
115 0, /* 19 - SIGPWR */
116 SIGWINCH, /* 20 */
117 0, /* 21 */
118 SIGIO, /* 22 */
119 SIGSTOP, /* 23 */
120 SIGTSTP, /* 24 */
121 SIGCONT, /* 25 */
122 SIGTTIN, /* 26 */
123 SIGTTOU, /* 27 */
124 SIGVTALRM, /* 28 */
125 SIGPROF, /* 29 */
126 0, /* 30 */
127 0, /* 31 */
128 0 /* 32 */
129 };
130
131 void
132 ibcs2_to_bsd_sigset(iss, bss)
133 const ibcs2_sigset_t *iss;
134 sigset_t *bss;
135 {
136 int i, newsig;
137
138 sigemptyset(bss);
139 for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
140 if (ibcs2_sigismember(iss, i)) {
141 newsig = ibcs2_to_bsd_sig[_SIG_IDX(i)];
142 if (newsig)
143 sigaddset(bss, newsig);
144 }
145 }
146 }
147
148 static void
149 bsd_to_ibcs2_sigset(bss, iss)
150 const sigset_t *bss;
151 ibcs2_sigset_t *iss;
152 {
153 int i, newsig;
154
155 ibcs2_sigemptyset(iss);
156 for (i = 1; i <= IBCS2_SIGTBLSZ; i++) {
157 if (sigismember(bss, i)) {
158 newsig = bsd_to_ibcs2_sig[_SIG_IDX(i)];
159 if (newsig)
160 ibcs2_sigaddset(iss, newsig);
161 }
162 }
163 }
164
165 static void
166 ibcs2_to_bsd_sigaction(isa, bsa)
167 struct ibcs2_sigaction *isa;
168 struct sigaction *bsa;
169 {
170
171 bsa->sa_handler = isa->isa_handler;
172 ibcs2_to_bsd_sigset(&isa->isa_mask, &bsa->sa_mask);
173 bsa->sa_flags = 0; /* ??? SA_NODEFER */
174 if ((isa->isa_flags & IBCS2_SA_NOCLDSTOP) != 0)
175 bsa->sa_flags |= SA_NOCLDSTOP;
176 }
177
178 static void
179 bsd_to_ibcs2_sigaction(bsa, isa)
180 struct sigaction *bsa;
181 struct ibcs2_sigaction *isa;
182 {
183
184 isa->isa_handler = bsa->sa_handler;
185 bsd_to_ibcs2_sigset(&bsa->sa_mask, &isa->isa_mask);
186 isa->isa_flags = 0;
187 if ((bsa->sa_flags & SA_NOCLDSTOP) != 0)
188 isa->isa_flags |= IBCS2_SA_NOCLDSTOP;
189 }
190
191 int
192 ibcs2_sigaction(td, uap)
193 register struct thread *td;
194 struct ibcs2_sigaction_args *uap;
195 {
196 struct ibcs2_sigaction isa;
197 struct sigaction nbsa, obsa;
198 struct sigaction *nbsap;
199 int error;
200
201 if (uap->act != NULL) {
202 if ((error = copyin(uap->act, &isa, sizeof(isa))) != 0)
203 return (error);
204 ibcs2_to_bsd_sigaction(&isa, &nbsa);
205 nbsap = &nbsa;
206 } else
207 nbsap = NULL;
208 error = kern_sigaction(td, ibcs2_to_bsd_sig[_SIG_IDX(uap->sig)], &nbsa,
209 &obsa, 0);
210 if (error == 0 && uap->oact != NULL) {
211 bsd_to_ibcs2_sigaction(&obsa, &isa);
212 error = copyout(&isa, uap->oact, sizeof(isa));
213 }
214 return (error);
215 }
216
217 int
218 ibcs2_sigsys(td, uap)
219 register struct thread *td;
220 struct ibcs2_sigsys_args *uap;
221 {
222 struct proc *p = td->td_proc;
223 struct sigaction sa;
224 int signum = ibcs2_to_bsd_sig[_SIG_IDX(IBCS2_SIGNO(uap->sig))];
225 int error;
226
227 if (signum <= 0 || signum >= IBCS2_NSIG) {
228 if (IBCS2_SIGCALL(uap->sig) == IBCS2_SIGNAL_MASK ||
229 IBCS2_SIGCALL(uap->sig) == IBCS2_SIGSET_MASK)
230 td->td_retval[0] = (int)IBCS2_SIG_ERR;
231 return EINVAL;
232 }
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(td, uap)
341 register struct thread *td;
342 struct ibcs2_sigprocmask_args *uap;
343 {
344 ibcs2_sigset_t iss;
345 sigset_t oss, nss;
346 sigset_t *nssp;
347 int error, how;
348
349 switch (uap->how) {
350 case IBCS2_SIG_BLOCK:
351 how = SIG_BLOCK;
352 break;
353 case IBCS2_SIG_UNBLOCK:
354 how = SIG_UNBLOCK;
355 break;
356 case IBCS2_SIG_SETMASK:
357 how = SIG_SETMASK;
358 break;
359 default:
360 return (EINVAL);
361 }
362 if (uap->set != NULL) {
363 if ((error = copyin(uap->set, &iss, sizeof(iss))) != 0)
364 return error;
365 ibcs2_to_bsd_sigset(&iss, &nss);
366 nssp = &nss;
367 } else
368 nssp = NULL;
369 error = kern_sigprocmask(td, how, nssp, &oss, 0);
370 if (error == 0 && uap->oset != NULL) {
371 bsd_to_ibcs2_sigset(&oss, &iss);
372 error = copyout(&iss, uap->oset, sizeof(iss));
373 }
374 return (error);
375 }
376
377 int
378 ibcs2_sigpending(td, uap)
379 register struct thread *td;
380 struct ibcs2_sigpending_args *uap;
381 {
382 struct proc *p = td->td_proc;
383 sigset_t bss;
384 ibcs2_sigset_t iss;
385
386 PROC_LOCK(p);
387 bss = td->td_siglist;
388 SIGSETOR(bss, p->p_siglist);
389 SIGSETAND(bss, td->td_sigmask);
390 PROC_UNLOCK(p);
391 bsd_to_ibcs2_sigset(&bss, &iss);
392
393 return copyout(&iss, uap->mask, sizeof(iss));
394 }
395
396 int
397 ibcs2_sigsuspend(td, uap)
398 register struct thread *td;
399 struct ibcs2_sigsuspend_args *uap;
400 {
401 ibcs2_sigset_t sss;
402 sigset_t bss;
403 int error;
404
405 if ((error = copyin(uap->mask, &sss, sizeof(sss))) != 0)
406 return error;
407
408 ibcs2_to_bsd_sigset(&sss, &bss);
409 return kern_sigsuspend(td, bss);
410 }
411
412 int
413 ibcs2_pause(td, uap)
414 register struct thread *td;
415 struct ibcs2_pause_args *uap;
416 {
417 sigset_t mask;
418
419 PROC_LOCK(td->td_proc);
420 mask = td->td_sigmask;
421 PROC_UNLOCK(td->td_proc);
422 return kern_sigsuspend(td, mask);
423 }
424
425 int
426 ibcs2_kill(td, uap)
427 register struct thread *td;
428 struct ibcs2_kill_args *uap;
429 {
430 struct kill_args ka;
431
432 ka.pid = uap->pid;
433 ka.signum = ibcs2_to_bsd_sig[_SIG_IDX(uap->signo)];
434 return kill(td, &ka);
435 }
Cache object: e956ed99bf08c417941088f851ad2b15
|