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