telnet line mode changes
[unix-history] / usr / src / sys / kern / kern_sig.c
CommitLineData
da7c5cc6 1/*
d86a61ec
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
d86a61ec
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
e51c8e4a 17 * @(#)kern_sig.c 7.17 (Berkeley) %G%
da7c5cc6 18 */
961945a8 19
94368568
JB
20#include "param.h"
21#include "systm.h"
f4af9a5b 22#include "syscontext.h" /* XXX */
d86a61ec 23#include "vnode.h"
94368568
JB
24#include "proc.h"
25#include "timeb.h"
26#include "times.h"
94368568 27#include "buf.h"
94368568
JB
28#include "text.h"
29#include "seg.h"
30#include "vm.h"
31#include "acct.h"
32#include "uio.h"
d86a61ec 33#include "file.h"
94368568 34#include "kernel.h"
f4af9a5b 35#include "wait.h"
22585170 36#include "ktrace.h"
687880f9 37
d301d150
KM
38#include "machine/reg.h"
39#include "machine/pte.h"
40#include "machine/psl.h"
41#include "machine/mtpr.h"
42
e51c8e4a
MK
43#define ttystopsigmask (sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU))
44#define stopsigmask (sigmask(SIGSTOP)|ttystopsigmask)
f4af9a5b
MK
45#define defaultignmask (sigmask(SIGCONT)|sigmask(SIGIO)|sigmask(SIGURG)| \
46 sigmask(SIGCHLD)|sigmask(SIGWINCH)|sigmask(SIGINFO))
457aa395 47
2e143991 48/*
f4af9a5b
MK
49 * Can the current process (u.u_procp) send the specified signal
50 * to the specified process?
2e143991 51 */
f4af9a5b
MK
52#define CANSIGNAL(p, signo) \
53 (u.u_uid == 0 || \
54 u.u_uid == (p)->p_uid || u.u_uid == (p)->p_ruid || \
55 u.u_procp->p_ruid == (p)->p_uid || \
56 u.u_procp->p_ruid == (p)->p_ruid || \
57 ((signo) == SIGCONT && (p)->p_session == u.u_procp->p_session))
58
59sigaction()
4147b3f6 60{
457aa395 61 register struct a {
dd012d1e 62 int signo;
f4af9a5b
MK
63 struct sigaction *nsa;
64 struct sigaction *osa;
dd012d1e 65 } *uap = (struct a *)u.u_ap;
f4af9a5b
MK
66 struct sigaction vec;
67 register struct sigaction *sa;
dd012d1e 68 register int sig;
f4af9a5b 69 int bit, error;
dd012d1e
SL
70
71 sig = uap->signo;
f4af9a5b
MK
72 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
73 RETURN (EINVAL);
74 sa = &vec;
75 if (uap->osa) {
76 sa->sa_handler = u.u_signal[sig];
77 sa->sa_mask = u.u_sigmask[sig];
fe7f81f4 78 bit = sigmask(sig);
f4af9a5b 79 sa->sa_flags = 0;
fe7f81f4 80 if ((u.u_sigonstack & bit) != 0)
f4af9a5b
MK
81 sa->sa_flags |= SA_ONSTACK;
82 if ((u.u_sigintr & bit) == 0)
83 sa->sa_flags |= SA_RESTART;
84 if (u.u_procp->p_flag & SNOCLDSTOP)
85 sa->sa_flags |= SA_NOCLDSTOP;
86 if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
87 sizeof (vec)))
88 RETURN (error);
457aa395 89 }
f4af9a5b
MK
90 if (uap->nsa) {
91 if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
92 sizeof (vec)))
93 RETURN (error);
94 setsigvec(sig, sa);
457aa395 95 }
f4af9a5b 96 RETURN (0);
dd012d1e 97}
4147b3f6 98
f4af9a5b 99setsigvec(sig, sa)
457aa395 100 int sig;
f4af9a5b 101 register struct sigaction *sa;
dd012d1e
SL
102{
103 register struct proc *p;
457aa395 104 register int bit;
dd012d1e 105
feff6b5a 106 bit = sigmask(sig);
dd012d1e
SL
107 p = u.u_procp;
108 /*
109 * Change setting atomically.
110 */
feff6b5a 111 (void) splhigh();
f4af9a5b
MK
112 u.u_signal[sig] = sa->sa_handler;
113 u.u_sigmask[sig] = sa->sa_mask &~ sigcantmask;
114 if ((sa->sa_flags & SA_RESTART) == 0)
fe7f81f4
KM
115 u.u_sigintr |= bit;
116 else
117 u.u_sigintr &= ~bit;
f4af9a5b 118 if (sa->sa_flags & SA_ONSTACK)
457aa395
SL
119 u.u_sigonstack |= bit;
120 else
121 u.u_sigonstack &= ~bit;
f4af9a5b
MK
122 if (sig == SIGCHLD) {
123 if (sa->sa_flags & SA_NOCLDSTOP)
124 p->p_flag |= SNOCLDSTOP;
125 else
126 p->p_flag &= ~SNOCLDSTOP;
127 }
128 /*
129 * Set bit in p_sigignore for signals that are set to SIG_IGN,
130 * and for signals set to SIG_DFL where the default is to ignore.
131 * However, don't put SIGCONT in p_sigignore,
132 * as we have to restart the process.
133 */
134 if (sa->sa_handler == SIG_IGN ||
135 (bit & defaultignmask && sa->sa_handler == SIG_DFL)) {
457aa395 136 p->p_sig &= ~bit; /* never to be seen again */
f4af9a5b
MK
137 if (sig != SIGCONT)
138 p->p_sigignore |= bit; /* easier in psignal */
457aa395 139 p->p_sigcatch &= ~bit;
dd012d1e 140 } else {
457aa395 141 p->p_sigignore &= ~bit;
f4af9a5b 142 if (sa->sa_handler == SIG_DFL)
457aa395 143 p->p_sigcatch &= ~bit;
dd012d1e 144 else
457aa395 145 p->p_sigcatch |= bit;
dd012d1e
SL
146 }
147 (void) spl0();
4147b3f6
BJ
148}
149
f4af9a5b
MK
150/*
151 * Initialize signal state for process 0;
152 * set to ignore signals that are ignored by default.
153 */
154siginit(p)
155 struct proc *p;
156{
157
158 p->p_sigignore = defaultignmask &~ sigmask(SIGCONT);
159}
160
161/*
162 * Reset signals for an exec of the specified process.
163 */
164execsigs(p)
165 register struct proc *p;
166{
167 register int nc, mask;
168
169 /*
170 * Reset caught signals. Held signals remain held
171 * through p_sigmask (unless they were caught,
172 * and are now ignored by default).
173 */
174 while (p->p_sigcatch) {
175 nc = ffs((long)p->p_sigcatch);
176 mask = sigmask(nc);
177 p->p_sigcatch &= ~mask;
178 if (mask & defaultignmask) {
179 if (nc != SIGCONT)
180 p->p_sigignore |= mask;
181 p->p_sig &= ~mask;
182 }
183 u.u_signal[nc] = SIG_DFL;
184 }
185 /*
186 * Reset stack state to the user stack.
187 * Clear set of signals caught on the signal stack.
188 */
189 u.u_onstack = 0;
190 u.u_sigsp = 0;
191 u.u_sigonstack = 0;
192}
193
194/*
195 * Manipulate signal mask.
196 * Note that we receive new mask, not pointer,
197 * and return old mask as return value;
198 * the library stub does the rest.
199 */
200sigprocmask()
201{
202 struct a {
203 int how;
204 sigset_t mask;
205 } *uap = (struct a *)u.u_ap;
206 register struct proc *p = u.u_procp;
207 int error = 0;
208
209 u.u_r.r_val1 = p->p_sigmask;
210 (void) splhigh();
211
212 switch (uap->how) {
213 case SIG_BLOCK:
214 p->p_sigmask |= uap->mask &~ sigcantmask;
215 break;
216
217 case SIG_UNBLOCK:
218 p->p_sigmask &= ~uap->mask;
219 break;
220
221 case SIG_SETMASK:
222 p->p_sigmask = uap->mask &~ sigcantmask;
223 break;
224
225 default:
226 error = EINVAL;
227 break;
228 }
229 (void) spl0();
230 RETURN (error);
231}
232
233sigpending()
234{
235
236 u.u_r.r_val1 = u.u_procp->p_sig;
237 RETURN (0);
238}
239
240#ifdef COMPAT_43
241/*
242 * Generalized interface signal handler, 4.3-compatible.
243 */
244osigvec()
245{
246 register struct a {
247 int signo;
248 struct sigvec *nsv;
249 struct sigvec *osv;
250 } *uap = (struct a *)u.u_ap;
251 struct sigvec vec;
252 register struct sigvec *sv;
253 register int sig;
254 int bit, error;
255
256 sig = uap->signo;
257 if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
258 RETURN (EINVAL);
259 sv = &vec;
260 if (uap->osv) {
261 *(sig_t *)&sv->sv_handler = u.u_signal[sig];
262 sv->sv_mask = u.u_sigmask[sig];
263 bit = sigmask(sig);
264 sv->sv_flags = 0;
265 if ((u.u_sigonstack & bit) != 0)
266 sv->sv_flags |= SV_ONSTACK;
267 if ((u.u_sigintr & bit) != 0)
268 sv->sv_flags |= SV_INTERRUPT;
269 if (u.u_procp->p_flag & SNOCLDSTOP)
270 sv->sv_flags |= SA_NOCLDSTOP;
271 if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
272 sizeof (vec)))
273 RETURN (error);
274 }
275 if (uap->nsv) {
276 if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
277 sizeof (vec)))
278 RETURN (error);
279 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
8f949d44 280 setsigvec(sig, (struct sigaction *)sv);
f4af9a5b
MK
281 }
282 RETURN (0);
283}
284
285osigblock()
4147b3f6 286{
dd012d1e 287 struct a {
feff6b5a 288 int mask;
dd012d1e 289 } *uap = (struct a *)u.u_ap;
457aa395 290 register struct proc *p = u.u_procp;
4147b3f6 291
feff6b5a 292 (void) splhigh();
dd012d1e 293 u.u_r.r_val1 = p->p_sigmask;
f4af9a5b 294 p->p_sigmask |= uap->mask &~ sigcantmask;
dd012d1e 295 (void) spl0();
f4af9a5b 296 RETURN (0);
4147b3f6
BJ
297}
298
f4af9a5b 299osigsetmask()
687880f9 300{
dd012d1e 301 struct a {
feff6b5a 302 int mask;
dd012d1e
SL
303 } *uap = (struct a *)u.u_ap;
304 register struct proc *p = u.u_procp;
687880f9 305
feff6b5a 306 (void) splhigh();
dd012d1e 307 u.u_r.r_val1 = p->p_sigmask;
f4af9a5b 308 p->p_sigmask = uap->mask &~ sigcantmask;
dd012d1e 309 (void) spl0();
f4af9a5b 310 RETURN (0);
687880f9 311}
f4af9a5b 312#endif
687880f9 313
f4af9a5b
MK
314/*
315 * Suspend process until signal, providing mask to be set
316 * in the meantime. Note nonstandard calling convention:
317 * libc stub passes mask, not pointer, to save a copyin.
318 */
319sigsuspend()
4147b3f6 320{
dd012d1e 321 struct a {
f4af9a5b 322 sigset_t mask;
dd012d1e
SL
323 } *uap = (struct a *)u.u_ap;
324 register struct proc *p = u.u_procp;
4147b3f6 325
dd012d1e
SL
326 /*
327 * When returning from sigpause, we want
328 * the old mask to be restored after the
329 * signal handler has finished. Thus, we
330 * save it here and mark the proc structure
331 * to indicate this (should be in u.).
332 */
333 u.u_oldmask = p->p_sigmask;
334 p->p_flag |= SOMASK;
f4af9a5b 335 p->p_sigmask = uap->mask &~ sigcantmask;
22585170
MT
336 (void) tsleep((caddr_t)&u, PPAUSE | PCATCH, "pause", 0);
337 /* always return EINTR rather than ERESTART... */
338 RETURN (EINTR);
4147b3f6
BJ
339}
340
341sigstack()
342{
457aa395 343 register struct a {
457aa395
SL
344 struct sigstack *nss;
345 struct sigstack *oss;
dd012d1e 346 } *uap = (struct a *)u.u_ap;
457aa395 347 struct sigstack ss;
f4af9a5b
MK
348 int error = 0;
349
350 if (uap->oss && (error = copyout((caddr_t)&u.u_sigstack,
351 (caddr_t)uap->oss, sizeof (struct sigstack))))
352 RETURN (error);
353 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
354 sizeof (ss))) == 0)
355 u.u_sigstack = ss;
356 RETURN (error);
4147b3f6
BJ
357}
358
1e3738da
BJ
359kill()
360{
dd012d1e
SL
361 register struct a {
362 int pid;
363 int signo;
364 } *uap = (struct a *)u.u_ap;
7713be67 365 register struct proc *p;
1e3738da 366
f4af9a5b
MK
367 if ((unsigned) uap->signo >= NSIG)
368 RETURN (EINVAL);
7713be67
KM
369 if (uap->pid > 0) {
370 /* kill single process */
371 p = pfind(uap->pid);
f4af9a5b
MK
372 if (p == 0)
373 RETURN (ESRCH);
374 if (!CANSIGNAL(p, uap->signo))
375 RETURN (EPERM);
376 if (uap->signo)
7713be67 377 psignal(p, uap->signo);
f4af9a5b 378 RETURN (0);
7713be67
KM
379 }
380 switch (uap->pid) {
381 case -1: /* broadcast signal */
f4af9a5b 382 RETURN (killpg1(uap->signo, 0, 1));
7713be67 383 case 0: /* signal own process group */
f4af9a5b 384 RETURN (killpg1(uap->signo, 0, 0));
7713be67 385 default: /* negative explicit process group */
f4af9a5b 386 RETURN (killpg1(uap->signo, -uap->pid, 0));
7713be67 387 }
f4af9a5b 388 /* NOTREACHED */
1e3738da
BJ
389}
390
f4af9a5b
MK
391#ifdef COMPAT_43
392okillpg()
1e3738da 393{
3b4f6e10 394 register struct a {
8fe87cbb 395 int pgid;
3b4f6e10
SL
396 int signo;
397 } *uap = (struct a *)u.u_ap;
1e3738da 398
f4af9a5b
MK
399 if ((unsigned) uap->signo >= NSIG)
400 RETURN (EINVAL);
401 RETURN (killpg1(uap->signo, uap->pgid, 0));
1e3738da 402}
f4af9a5b 403#endif
dd012d1e 404
8fe87cbb
MT
405killpg1(signo, pgid, all)
406 int signo, pgid, all;
3b4f6e10
SL
407{
408 register struct proc *p;
8fe87cbb 409 struct pgrp *pgrp;
f4af9a5b 410 int f = 0, error = ESRCH;
8fe87cbb
MT
411
412 if (all)
413 /*
414 * broadcast
687880f9 415 */
8fe87cbb
MT
416 for (p = allproc; p != NULL; p = p->p_nxt) {
417 if (p->p_ppid == 0 || p->p_flag&SSYS ||
f4af9a5b 418 p == u.u_procp || !CANSIGNAL(p, signo))
8fe87cbb
MT
419 continue;
420 f++;
421 if (signo)
422 psignal(p, signo);
423 }
424 else {
425 if (pgid == 0)
426 /*
427 * zero pgid means send to my process group.
428 */
429 pgrp = u.u_procp->p_pgrp;
430 else {
431 pgrp = pgfind(pgid);
432 if (pgrp == NULL)
f4af9a5b 433 return (ESRCH);
8fe87cbb 434 }
8fe87cbb 435 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
f4af9a5b
MK
436 if (p->p_ppid == 0 || p->p_flag&SSYS ||
437 !CANSIGNAL(p, signo))
8fe87cbb 438 continue;
8fe87cbb
MT
439 f++;
440 if (signo)
441 psignal(p, signo);
7713be67 442 }
687880f9 443 }
f4af9a5b 444 return (f ? 0 : error);
4147b3f6 445}
687880f9 446
22585170 447/* XXX - to be removed, as soon as sockets are changed to operate on pgrps
687880f9 448 * Send the specified signal to
8fe87cbb 449 * all processes with 'pgid' as
687880f9 450 * process group.
687880f9 451 */
8fe87cbb
MT
452gsignal(pgid, sig)
453{
f4af9a5b 454 struct pgrp *pgrp;
8fe87cbb 455
f4af9a5b 456 if (pgid && (pgrp = pgfind(pgid)))
c697d0b9 457 pgsignal(pgrp, sig, 0);
8fe87cbb 458}
22585170 459/*
c697d0b9
MT
460 * Send sig to every member of a process group.
461 * If checktty is 1, limit to members which have a controlling
462 * terminal.
22585170 463 */
c697d0b9 464pgsignal(pgrp, sig, checkctty)
f4af9a5b 465 struct pgrp *pgrp;
687880f9
BJ
466{
467 register struct proc *p;
468
22585170
MT
469 if (pgrp)
470 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
c697d0b9
MT
471 if (checkctty == 0 || p->p_flag&SCTTY)
472 psignal(p, sig);
f4af9a5b
MK
473}
474
475/*
476 * Send a signal caused by a trap to the current process.
477 * If it will be caught immediately, deliver it with correct code.
478 * Otherwise, post it normally.
479 */
480trapsignal(sig, code)
481 register int sig;
482 unsigned code;
483{
484 register struct proc *p = u.u_procp;
485 int mask;
486
487 mask = sigmask(sig);
488 if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
489 (p->p_sigmask & mask) == 0) {
490 u.u_ru.ru_nsignals++;
22585170
MT
491#ifdef KTRACE
492 if (KTRPOINT(p, KTR_PSIG))
493 ktrpsig(p->p_tracep, sig, u.u_signal[sig],
494 p->p_sigmask, code);
495#endif
f4af9a5b
MK
496 sendsig(u.u_signal[sig], sig, p->p_sigmask, code);
497 p->p_sigmask |= u.u_sigmask[sig] | mask;
498 } else {
499 u.u_arg[1] = code; /* XXX for core dump/debugger */
8fe87cbb 500 psignal(p, sig);
f4af9a5b 501 }
687880f9
BJ
502}
503
504/*
22585170
MT
505 * Send the specified signal to the specified process.
506 * Most signals do not do anything directly to a process;
507 * they set a flag that asks the process to do something to itself.
508 * Exceptions:
509 * o When a stop signal is sent to a sleeping process that takes the default
510 * action, the process is stopped without awakening it.
511 * o SIGCONT restarts stopped processes (or puts them back to sleep)
512 * regardless of the signal action (eg, blocked or ignored).
513 * Other ignored signals are discarded immediately.
687880f9
BJ
514 */
515psignal(p, sig)
516 register struct proc *p;
517 register int sig;
518{
519 register int s;
f4af9a5b 520 register sig_t action;
feff6b5a 521 int mask;
687880f9 522
f4af9a5b
MK
523 if ((unsigned)sig >= NSIG || sig == 0)
524 panic("psignal sig");
feff6b5a 525 mask = sigmask(sig);
687880f9
BJ
526
527 /*
528 * If proc is traced, always give parent a chance.
687880f9
BJ
529 */
530 if (p->p_flag & STRC)
531 action = SIG_DFL;
532 else {
687880f9 533 /*
dd012d1e
SL
534 * If the signal is being ignored,
535 * then we forget about it immediately.
f4af9a5b
MK
536 * (Note: we don't set SIGCONT in p_sigignore,
537 * and if it is set to SIG_IGN,
538 * action will be SIG_DFL here.)
687880f9 539 */
feff6b5a 540 if (p->p_sigignore & mask)
687880f9 541 return;
feff6b5a 542 if (p->p_sigmask & mask)
dd012d1e 543 action = SIG_HOLD;
feff6b5a 544 else if (p->p_sigcatch & mask)
dd012d1e 545 action = SIG_CATCH;
e51c8e4a 546 else
dd012d1e 547 action = SIG_DFL;
687880f9 548 }
f4af9a5b 549 switch (sig) {
687880f9 550
f4af9a5b
MK
551 case SIGTERM:
552 if ((p->p_flag&STRC) || action != SIG_DFL)
687880f9 553 break;
f4af9a5b 554 /* FALLTHROUGH */
687880f9 555
f4af9a5b
MK
556 case SIGKILL:
557 if (p->p_nice > NZERO)
558 p->p_nice = NZERO;
559 break;
687880f9 560
f4af9a5b
MK
561 case SIGCONT:
562 p->p_sig &= ~stopsigmask;
563 break;
564
565 case SIGTSTP:
566 case SIGTTIN:
567 case SIGTTOU:
568 case SIGSTOP:
569 p->p_sig &= ~sigmask(SIGCONT);
570 break;
687880f9 571 }
f4af9a5b
MK
572 p->p_sig |= mask;
573
687880f9 574 /*
f4af9a5b
MK
575 * Defer further processing for signals which are held,
576 * except that stopped processes must be continued by SIGCONT.
687880f9 577 */
f4af9a5b 578 if (action == SIG_HOLD && (sig != SIGCONT || p->p_stat != SSTOP))
687880f9 579 return;
feff6b5a 580 s = splhigh();
687880f9
BJ
581 switch (p->p_stat) {
582
583 case SSLEEP:
584 /*
22585170 585 * If process is sleeping uninterruptibly
687880f9
BJ
586 * we can't interrupt the sleep... the signal will
587 * be noticed when the process returns through
588 * trap() or syscall().
589 */
22585170 590 if ((p->p_flag & SSINTR) == 0)
687880f9
BJ
591 goto out;
592 /*
593 * Process is sleeping and traced... make it runnable
594 * so it can discover the signal in issig() and stop
595 * for the parent.
596 */
597 if (p->p_flag&STRC)
598 goto run;
f4af9a5b
MK
599 /*
600 * When a sleeping process receives a stop
601 * signal, process immediately if possible.
602 * All other (caught or default) signals
603 * cause the process to run.
604 */
605 if (mask & stopsigmask) {
687880f9 606 if (action != SIG_DFL)
f4af9a5b 607 goto runfast;
687880f9
BJ
608 /*
609 * If a child in vfork(), stopping could
610 * cause deadlock.
611 */
612 if (p->p_flag&SVFORK)
613 goto out;
feff6b5a 614 p->p_sig &= ~mask;
687880f9 615 p->p_cursig = sig;
f4af9a5b
MK
616 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
617 psignal(p->p_pptr, SIGCHLD);
687880f9
BJ
618 stop(p);
619 goto out;
f4af9a5b
MK
620 } else
621 goto runfast;
687880f9
BJ
622 /*NOTREACHED*/
623
624 case SSTOP:
625 /*
626 * If traced process is already stopped,
627 * then no further action is necessary.
628 */
629 if (p->p_flag&STRC)
630 goto out;
631 switch (sig) {
632
633 case SIGKILL:
634 /*
635 * Kill signal always sets processes running.
636 */
f4af9a5b 637 goto runfast;
687880f9
BJ
638
639 case SIGCONT:
640 /*
f4af9a5b
MK
641 * If SIGCONT is default (or ignored), we continue
642 * the process but don't leave the signal in p_sig,
643 * as it has no further action. If SIGCONT is held,
644 * continue the process and leave the signal in p_sig.
687880f9
BJ
645 * If the process catches SIGCONT, let it handle
646 * the signal itself. If it isn't waiting on
647 * an event, then it goes back to run state.
648 * Otherwise, process goes back to sleep state.
649 */
f4af9a5b
MK
650 p->p_cursig = 0; /* ??? XXX */
651 if (action == SIG_DFL)
652 p->p_sig &= ~mask;
653 if (action == SIG_CATCH)
654 goto runfast;
655 if (p->p_wchan == 0)
687880f9
BJ
656 goto run;
657 p->p_stat = SSLEEP;
658 goto out;
659
660 case SIGSTOP:
661 case SIGTSTP:
662 case SIGTTIN:
663 case SIGTTOU:
664 /*
665 * Already stopped, don't need to stop again.
666 * (If we did the shell could get confused.)
667 */
feff6b5a 668 p->p_sig &= ~mask; /* take it away */
687880f9
BJ
669 goto out;
670
671 default:
672 /*
673 * If process is sleeping interruptibly, then
22585170
MT
674 * simulate a wakeup so that when it is continued,
675 * it will be made runnable and can look at the signal.
676 * But don't setrun the process, leave it stopped.
687880f9 677 */
22585170 678 if (p->p_wchan && p->p_flag & SSINTR)
687880f9
BJ
679 unsleep(p);
680 goto out;
681 }
682 /*NOTREACHED*/
683
684 default:
685 /*
686 * SRUN, SIDL, SZOMB do nothing with the signal,
687 * other than kicking ourselves if we are running.
688 * It will either never be noticed, or noticed very soon.
689 */
690 if (p == u.u_procp && !noproc)
691 aston();
692 goto out;
693 }
694 /*NOTREACHED*/
f4af9a5b
MK
695
696runfast:
687880f9
BJ
697 /*
698 * Raise priority to at least PUSER.
699 */
700 if (p->p_pri > PUSER)
e4f89871 701 p->p_pri = PUSER;
f4af9a5b 702run:
687880f9
BJ
703 setrun(p);
704out:
705 splx(s);
706}
707
708/*
22585170
MT
709 * If the current process has a signal to process (should be caught
710 * or cause termination, should interrupt current syscall),
711 * return the signal number. Stop signals with default action
712 * are processed immediately, then cleared; they aren't returned.
687880f9
BJ
713 * This is asked at least once each time a process enters the
714 * system (though this can usually be done without actually
715 * calling issig by checking the pending signal masks.)
687880f9
BJ
716 */
717issig()
718{
719 register struct proc *p;
f4af9a5b 720 register int sig, mask;
687880f9
BJ
721
722 p = u.u_procp;
723 for (;;) {
f4af9a5b 724 mask = p->p_sig &~ p->p_sigmask;
687880f9 725 if (p->p_flag&SVFORK)
f4af9a5b 726 mask &= ~stopsigmask;
22585170
MT
727 if (mask == 0) /* no signal to send */
728 return (0);
f4af9a5b 729 sig = ffs((long)mask);
feff6b5a 730 mask = sigmask(sig);
22585170
MT
731 /*
732 * We should see pending but ignored signals
733 * only if STRC was on when they were posted.
734 */
735 if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
736 p->p_sig &= ~mask;
737 continue;
738 }
dd012d1e 739 if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
687880f9
BJ
740 /*
741 * If traced, always stop, and stay
742 * stopped until released by the parent.
743 */
22585170 744 p->p_cursig = sig;
ad6c75cb 745 psignal(p->p_pptr, SIGCHLD);
687880f9
BJ
746 do {
747 stop(p);
748 swtch();
749 } while (!procxmt() && p->p_flag&STRC);
750
751 /*
f99e4a3a 752 * If the traced bit got turned off,
22585170 753 * go back up to the top to rescan signals.
f99e4a3a 754 * This ensures that p_sig* and u_signal are consistent.
687880f9 755 */
22585170 756 if ((p->p_flag&STRC) == 0)
687880f9 757 continue;
687880f9
BJ
758
759 /*
760 * If parent wants us to take the signal,
761 * then it will leave it in p->p_cursig;
762 * otherwise we just look for signals again.
763 */
22585170 764 p->p_sig &= ~mask; /* clear the old signal */
687880f9
BJ
765 sig = p->p_cursig;
766 if (sig == 0)
767 continue;
f99e4a3a
SL
768
769 /*
22585170
MT
770 * Put the new signal into p_sig.
771 * If signal is being masked,
772 * look for other signals.
f99e4a3a 773 */
feff6b5a 774 mask = sigmask(sig);
22585170
MT
775 p->p_sig |= mask;
776 if (p->p_sigmask & mask)
f99e4a3a 777 continue;
687880f9 778 }
22585170
MT
779
780 /*
781 * Decide whether the signal should be returned.
782 * Return the signal's number, or fall through
783 * to clear it from the pending mask.
784 */
a488db1a 785 switch ((int)u.u_signal[sig]) {
687880f9
BJ
786
787 case SIG_DFL:
788 /*
789 * Don't take default actions on system processes.
790 */
791 if (p->p_ppid == 0)
22585170
MT
792 break; /* == ignore */
793 /*
794 * If there is a pending stop signal to process
795 * with default action, stop here,
e51c8e4a
MK
796 * then clear the signal. However,
797 * if process is member of an orphaned
798 * process group, ignore tty stop signals.
22585170 799 */
f4af9a5b 800 if (mask & stopsigmask) {
e51c8e4a
MK
801 if (p->p_flag&STRC ||
802 (p->p_pgrp->pg_jobc == 0 &&
803 mask & ttystopsigmask))
22585170
MT
804 break; /* == ignore */
805 p->p_cursig = sig;
687880f9 806 stop(p);
f4af9a5b
MK
807 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
808 psignal(p->p_pptr, SIGCHLD);
687880f9 809 swtch();
22585170 810 break;
f4af9a5b 811 } else if (mask & defaultignmask) {
687880f9 812 /*
f4af9a5b
MK
813 * Except for SIGCONT, shouldn't get here.
814 * Default action is to ignore; drop it.
687880f9 815 */
22585170 816 break; /* == ignore */
f4af9a5b 817 } else
22585170 818 return (sig);
687880f9
BJ
819 /*NOTREACHED*/
820
687880f9
BJ
821 case SIG_IGN:
822 /*
f4af9a5b
MK
823 * Masking above should prevent us ever trying
824 * to take action on an ignored signal other
825 * than SIGCONT, unless process is traced.
687880f9 826 */
f4af9a5b 827 if (sig != SIGCONT && (p->p_flag&STRC) == 0)
687880f9 828 printf("issig\n");
22585170 829 break; /* == ignore */
687880f9
BJ
830
831 default:
832 /*
833 * This signal has an action, let
834 * psig process it.
835 */
22585170 836 return (sig);
687880f9 837 }
22585170 838 p->p_sig &= ~mask; /* take the signal! */
687880f9 839 }
22585170 840 /* NOTREACHED */
687880f9
BJ
841}
842
687880f9
BJ
843/*
844 * Put the argument process into the stopped
ad6c75cb
MK
845 * state and notify the parent via wakeup.
846 * Signals are handled elsewhere.
22585170 847 * The process must not be on the run queue.
687880f9
BJ
848 */
849stop(p)
850 register struct proc *p;
851{
852
853 p->p_stat = SSTOP;
854 p->p_flag &= ~SWTED;
855 wakeup((caddr_t)p->p_pptr);
687880f9
BJ
856}
857
858/*
22585170 859 * Perform the action specified by the current signal.
687880f9 860 * The usual sequence is:
22585170
MT
861 * if (sig = CURSIG(p))
862 * psig(sig);
687880f9 863 */
22585170
MT
864psig(sig)
865 register int sig;
687880f9 866{
dd012d1e 867 register struct proc *p = u.u_procp;
f4af9a5b
MK
868 int mask, returnmask;
869 register sig_t action;
870
871 do {
22585170 872#ifdef DIAGNOSTIC
f4af9a5b
MK
873 if (sig == 0)
874 panic("psig");
22585170
MT
875#endif
876 mask = sigmask(sig);
877 p->p_sig &= ~mask;
f4af9a5b 878 action = u.u_signal[sig];
22585170
MT
879#ifdef KTRACE
880 if (KTRPOINT(p, KTR_PSIG))
881 ktrpsig(p->p_tracep, sig, action, p->p_flag & SOMASK ?
882 u.u_oldmask : p->p_sigmask, 0);
883#endif
f4af9a5b
MK
884 if (action != SIG_DFL) {
885#ifdef DIAGNOSTIC
886 if (action == SIG_IGN || (p->p_sigmask & mask))
887 panic("psig action");
888#endif
889 u.u_error = 0;
890 /*
891 * Set the new mask value and also defer further
892 * occurences of this signal.
893 *
894 * Special case: user has done a sigpause. Here the
895 * current mask is not of interest, but rather the
896 * mask from before the sigpause is what we want
897 * restored after the signal processing is completed.
898 */
899 (void) splhigh();
900 if (p->p_flag & SOMASK) {
901 returnmask = u.u_oldmask;
902 p->p_flag &= ~SOMASK;
903 } else
904 returnmask = p->p_sigmask;
905 p->p_sigmask |= u.u_sigmask[sig] | mask;
906 (void) spl0();
907 u.u_ru.ru_nsignals++;
908 sendsig(action, sig, returnmask, 0);
f4af9a5b 909 continue;
687880f9 910 }
f4af9a5b
MK
911 u.u_acflag |= AXSIG;
912 switch (sig) {
687880f9 913
f4af9a5b
MK
914 case SIGILL:
915 case SIGIOT:
916 case SIGBUS:
917 case SIGQUIT:
918 case SIGTRAP:
919 case SIGEMT:
920 case SIGFPE:
921 case SIGSEGV:
922 case SIGSYS:
923 u.u_arg[0] = sig;
924 if (core() == 0)
925 sig |= WCOREFLAG;
926 }
927 exit(W_EXITCODE(0, sig));
928 /* NOTREACHED */
22585170 929 } while (sig = CURSIG(p));
687880f9
BJ
930}
931
687880f9 932/*
f4af9a5b 933 * Create a core image on the file "core".
687880f9
BJ
934 * It writes UPAGES block of the
935 * user.h area followed by the entire
936 * data+stack segments.
937 */
938core()
939{
c4ec2128 940 register struct vnode *vp;
f4af9a5b 941 register struct proc *p = u.u_procp;
715baff1 942 register struct nameidata *ndp = &u.u_nd;
d86a61ec
KM
943 struct vattr vattr;
944 int error;
687880f9 945
f4af9a5b 946 if (p->p_svuid != p->p_ruid || p->p_svgid != p->p_rgid)
d86a61ec
KM
947 return (EFAULT);
948 if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
1e3738da 949 u.u_rlimit[RLIMIT_CORE].rlim_cur)
d86a61ec 950 return (EFAULT);
f4af9a5b
MK
951 if (p->p_textp) {
952 VOP_LOCK(p->p_textp->x_vptr);
953 error = VOP_ACCESS(p->p_textp->x_vptr, VREAD, u.u_cred);
954 VOP_UNLOCK(p->p_textp->x_vptr);
d86a61ec
KM
955 if (error)
956 return (EFAULT);
957 }
715baff1
KM
958 ndp->ni_segflg = UIO_SYSSPACE;
959 ndp->ni_dirp = "core";
d86a61ec
KM
960 if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
961 return (error);
962 vp = ndp->ni_vp;
4493085b 963 VOP_LOCK(vp);
d86a61ec 964 if (vp->v_type != VREG ||
c4ec2128 965 VOP_GETATTR(vp, &vattr, u.u_cred) ||
d86a61ec 966 vattr.va_nlink != 1) {
4493085b
KM
967 vput(vp);
968 return (EFAULT);
7b0cb7cb 969 }
8f949d44
KM
970#ifdef MAPMEM
971 mmcore();
728df0db 972#endif
3ee1461b 973 VATTR_NULL(&vattr);
d86a61ec 974 vattr.va_size = 0;
c4ec2128 975 VOP_SETATTR(vp, &vattr, u.u_cred);
7b0cb7cb 976 u.u_acflag |= ACORE;
8f949d44
KM
977#ifdef HPUXCOMPAT
978 /*
979 * BLETCH! If we loaded from an HPUX format binary file
980 * we have to dump an HPUX style user struct so that the
981 * HPUX debuggers can grok it.
982 */
983 if (u.u_pcb.pcb_flags & PCB_HPUXBIN)
984 error = hpuxdumpu(vp, ndp->ni_cred);
985 else
986#endif
d86a61ec 987 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
4493085b 988 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
d86a61ec
KM
989 if (error == 0)
990 error = vn_rdwr(UIO_WRITE, vp,
f4af9a5b 991 (caddr_t)ctob(dptov(p, 0)),
4493085b
KM
992 (int)ctob(u.u_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
993 IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
d86a61ec
KM
994 if (error == 0)
995 error = vn_rdwr(UIO_WRITE, vp,
f4af9a5b 996 (caddr_t)ctob(sptov(p, u.u_ssize - 1)),
8011f5df 997 (int)ctob(u.u_ssize),
4493085b
KM
998 (off_t)ctob(UPAGES) + ctob(u.u_dsize), UIO_USERSPACE,
999 IO_NODELOCKED|IO_UNIT, ndp->ni_cred, (int *)0);
1000 vput(vp);
d86a61ec 1001 return (error);
687880f9 1002}
f4af9a5b
MK
1003
1004/*
1005 * Nonexistent system call-- signal process (may want to handle it).
1006 * Flag error in case process won't see signal immediately (blocked or ignored).
1007 */
1008nosys()
1009{
1010
1011 psignal(u.u_procp, SIGSYS);
1012 u.u_error = EINVAL;
1013}