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