Cleanups for 4.4BSD-Lite
[unix-history] / usr / src / sys / kern / kern_sig.c
CommitLineData
da7c5cc6 1/*
c34daa85
KB
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
da7c5cc6 4 *
dbf0c423 5 * %sccs.include.redist.c%
d86a61ec 6 *
802236f4 7 * @(#)kern_sig.c 8.2 (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>
802236f4 27#include <sys/stat.h>
687880f9 28
38a01dbe 29#include <machine/cpu.h>
8e6cd807 30
38a01dbe 31#include <vm/vm.h>
38a01dbe 32#include <sys/user.h> /* for coredump */
d301d150 33
2e143991 34/*
802236f4 35 * Can process p, with pcred pc, send the signal signum to process q?
2e143991 36 */
802236f4 37#define CANSIGNAL(p, pc, q, signum) \
8429d022
MK
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 || \
802236f4 43 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
e635a749 44
9e97623a 45struct sigaction_args {
802236f4 46 int signum;
9e97623a
CT
47 struct sigaction *nsa;
48 struct sigaction *osa;
49};
e635a749
MK
50/* ARGSUSED */
51sigaction(p, uap, retval)
52 struct proc *p;
9e97623a 53 register struct sigaction_args *uap;
e635a749
MK
54 int *retval;
55{
f4af9a5b
MK
56 struct sigaction vec;
57 register struct sigaction *sa;
8429d022 58 register struct sigacts *ps = p->p_sigacts;
802236f4 59 register int signum;
f4af9a5b 60 int bit, error;
dd012d1e 61
802236f4
KB
62 signum = uap->signum;
63 if (signum <= 0 || signum >= NSIG ||
64 signum == SIGKILL || signum == SIGSTOP)
d9c2f47f 65 return (EINVAL);
f4af9a5b
MK
66 sa = &vec;
67 if (uap->osa) {
802236f4
KB
68 sa->sa_handler = ps->ps_sigact[signum];
69 sa->sa_mask = ps->ps_catchmask[signum];
70 bit = sigmask(signum);
f4af9a5b 71 sa->sa_flags = 0;
8429d022 72 if ((ps->ps_sigonstack & bit) != 0)
f4af9a5b 73 sa->sa_flags |= SA_ONSTACK;
8429d022 74 if ((ps->ps_sigintr & bit) == 0)
f4af9a5b 75 sa->sa_flags |= SA_RESTART;
e635a749 76 if (p->p_flag & SNOCLDSTOP)
f4af9a5b
MK
77 sa->sa_flags |= SA_NOCLDSTOP;
78 if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
79 sizeof (vec)))
d9c2f47f 80 return (error);
457aa395 81 }
f4af9a5b
MK
82 if (uap->nsa) {
83 if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
84 sizeof (vec)))
d9c2f47f 85 return (error);
802236f4 86 setsigvec(p, signum, sa);
457aa395 87 }
d9c2f47f 88 return (0);
dd012d1e 89}
4147b3f6 90
802236f4 91setsigvec(p, signum, sa)
e635a749 92 register struct proc *p;
802236f4 93 int signum;
f4af9a5b 94 register struct sigaction *sa;
dd012d1e 95{
8429d022 96 register struct sigacts *ps = p->p_sigacts;
457aa395 97 register int bit;
dd012d1e 98
802236f4 99 bit = sigmask(signum);
dd012d1e
SL
100 /*
101 * Change setting atomically.
102 */
feff6b5a 103 (void) splhigh();
802236f4
KB
104 ps->ps_sigact[signum] = sa->sa_handler;
105 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
f4af9a5b 106 if ((sa->sa_flags & SA_RESTART) == 0)
8429d022 107 ps->ps_sigintr |= bit;
fe7f81f4 108 else
8429d022 109 ps->ps_sigintr &= ~bit;
f4af9a5b 110 if (sa->sa_flags & SA_ONSTACK)
8429d022 111 ps->ps_sigonstack |= bit;
457aa395 112 else
8429d022 113 ps->ps_sigonstack &= ~bit;
0cbc4437
CT
114#ifdef COMPAT_SUNOS
115 if (sa->sa_flags & SA_USERTRAMP)
116 ps->ps_usertramp |= bit;
117 else
118 ps->ps_usertramp &= ~bit;
119#endif
802236f4 120 if (signum == SIGCHLD) {
f4af9a5b
MK
121 if (sa->sa_flags & SA_NOCLDSTOP)
122 p->p_flag |= SNOCLDSTOP;
123 else
124 p->p_flag &= ~SNOCLDSTOP;
125 }
126 /*
127 * Set bit in p_sigignore for signals that are set to SIG_IGN,
128 * and for signals set to SIG_DFL where the default is to ignore.
129 * However, don't put SIGCONT in p_sigignore,
130 * as we have to restart the process.
131 */
132 if (sa->sa_handler == SIG_IGN ||
802236f4 133 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
457aa395 134 p->p_sig &= ~bit; /* never to be seen again */
802236f4 135 if (signum != SIGCONT)
f4af9a5b 136 p->p_sigignore |= bit; /* easier in psignal */
457aa395 137 p->p_sigcatch &= ~bit;
dd012d1e 138 } else {
457aa395 139 p->p_sigignore &= ~bit;
f4af9a5b 140 if (sa->sa_handler == SIG_DFL)
457aa395 141 p->p_sigcatch &= ~bit;
dd012d1e 142 else
457aa395 143 p->p_sigcatch |= bit;
dd012d1e
SL
144 }
145 (void) spl0();
4147b3f6
BJ
146}
147
f4af9a5b
MK
148/*
149 * Initialize signal state for process 0;
150 * set to ignore signals that are ignored by default.
151 */
8429d022 152void
f4af9a5b
MK
153siginit(p)
154 struct proc *p;
155{
8429d022 156 register int i;
f4af9a5b 157
8429d022
MK
158 for (i = 0; i < NSIG; i++)
159 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
160 p->p_sigignore |= sigmask(i);
f4af9a5b
MK
161}
162
163/*
164 * Reset signals for an exec of the specified process.
165 */
8429d022 166void
f4af9a5b
MK
167execsigs(p)
168 register struct proc *p;
169{
8429d022 170 register struct sigacts *ps = p->p_sigacts;
f4af9a5b
MK
171 register int nc, mask;
172
173 /*
174 * Reset caught signals. Held signals remain held
175 * through p_sigmask (unless they were caught,
176 * and are now ignored by default).
177 */
178 while (p->p_sigcatch) {
179 nc = ffs((long)p->p_sigcatch);
180 mask = sigmask(nc);
181 p->p_sigcatch &= ~mask;
8429d022 182 if (sigprop[nc] & SA_IGNORE) {
f4af9a5b
MK
183 if (nc != SIGCONT)
184 p->p_sigignore |= mask;
185 p->p_sig &= ~mask;
186 }
8429d022 187 ps->ps_sigact[nc] = SIG_DFL;
f4af9a5b
MK
188 }
189 /*
190 * Reset stack state to the user stack.
191 * Clear set of signals caught on the signal stack.
192 */
ebf08548
KM
193 ps->ps_sigstk.ss_flags = SA_DISABLE;
194 ps->ps_sigstk.ss_size = 0;
195 ps->ps_sigstk.ss_base = 0;
196 ps->ps_flags = 0;
f4af9a5b
MK
197}
198
199/*
200 * Manipulate signal mask.
201 * Note that we receive new mask, not pointer,
202 * and return old mask as return value;
203 * the library stub does the rest.
204 */
9e97623a
CT
205struct sigprocmask_args {
206 int how;
207 sigset_t mask;
208};
e635a749
MK
209sigprocmask(p, uap, retval)
210 register struct proc *p;
9e97623a 211 struct sigprocmask_args *uap;
e635a749
MK
212 int *retval;
213{
f4af9a5b
MK
214 int error = 0;
215
e635a749 216 *retval = p->p_sigmask;
f4af9a5b
MK
217 (void) splhigh();
218
219 switch (uap->how) {
220 case SIG_BLOCK:
221 p->p_sigmask |= uap->mask &~ sigcantmask;
222 break;
223
224 case SIG_UNBLOCK:
225 p->p_sigmask &= ~uap->mask;
226 break;
227
228 case SIG_SETMASK:
229 p->p_sigmask = uap->mask &~ sigcantmask;
230 break;
231
232 default:
233 error = EINVAL;
234 break;
235 }
236 (void) spl0();
d9c2f47f 237 return (error);
f4af9a5b
MK
238}
239
9e97623a
CT
240struct sigpending_args {
241 int dummy;
242};
e635a749
MK
243/* ARGSUSED */
244sigpending(p, uap, retval)
245 struct proc *p;
9e97623a 246 struct sigpending_args *uap;
e635a749 247 int *retval;
f4af9a5b
MK
248{
249
e635a749 250 *retval = p->p_sig;
d9c2f47f 251 return (0);
f4af9a5b
MK
252}
253
0cbc4437 254#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
f4af9a5b
MK
255/*
256 * Generalized interface signal handler, 4.3-compatible.
257 */
9e97623a 258struct osigvec_args {
802236f4 259 int signum;
9e97623a
CT
260 struct sigvec *nsv;
261 struct sigvec *osv;
262};
e635a749
MK
263/* ARGSUSED */
264osigvec(p, uap, retval)
265 struct proc *p;
9e97623a 266 register struct osigvec_args *uap;
e635a749
MK
267 int *retval;
268{
f4af9a5b 269 struct sigvec vec;
8429d022 270 register struct sigacts *ps = p->p_sigacts;
f4af9a5b 271 register struct sigvec *sv;
802236f4 272 register int signum;
f4af9a5b
MK
273 int bit, error;
274
802236f4
KB
275 signum = uap->signum;
276 if (signum <= 0 || signum >= NSIG ||
277 signum == SIGKILL || signum == SIGSTOP)
d9c2f47f 278 return (EINVAL);
f4af9a5b
MK
279 sv = &vec;
280 if (uap->osv) {
802236f4
KB
281 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
282 sv->sv_mask = ps->ps_catchmask[signum];
283 bit = sigmask(signum);
f4af9a5b 284 sv->sv_flags = 0;
8429d022 285 if ((ps->ps_sigonstack & bit) != 0)
f4af9a5b 286 sv->sv_flags |= SV_ONSTACK;
8429d022 287 if ((ps->ps_sigintr & bit) != 0)
f4af9a5b 288 sv->sv_flags |= SV_INTERRUPT;
0cbc4437 289#ifndef COMPAT_SUNOS
e635a749 290 if (p->p_flag & SNOCLDSTOP)
f4af9a5b 291 sv->sv_flags |= SA_NOCLDSTOP;
0cbc4437 292#endif
f4af9a5b
MK
293 if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
294 sizeof (vec)))
d9c2f47f 295 return (error);
f4af9a5b
MK
296 }
297 if (uap->nsv) {
298 if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
299 sizeof (vec)))
d9c2f47f 300 return (error);
0cbc4437
CT
301#ifdef COMPAT_SUNOS
302 /*
0c1cfb60
KM
303 * SunOS uses this bit (4, aka SA_DISABLE) as SV_RESETHAND,
304 * `reset to SIG_DFL on delivery'. We have no such option
305 * now or ever!
0cbc4437 306 */
0c1cfb60 307 if (sv->sv_flags & SA_DISABLE)
0cbc4437
CT
308 return (EINVAL);
309 sv->sv_flags |= SA_USERTRAMP;
310#endif
f4af9a5b 311 sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
802236f4 312 setsigvec(p, signum, (struct sigaction *)sv);
f4af9a5b 313 }
d9c2f47f 314 return (0);
f4af9a5b
MK
315}
316
9e97623a
CT
317struct osigblock_args {
318 int mask;
319};
e635a749
MK
320osigblock(p, uap, retval)
321 register struct proc *p;
9e97623a 322 struct osigblock_args *uap;
e635a749
MK
323 int *retval;
324{
4147b3f6 325
feff6b5a 326 (void) splhigh();
e635a749 327 *retval = p->p_sigmask;
f4af9a5b 328 p->p_sigmask |= uap->mask &~ sigcantmask;
dd012d1e 329 (void) spl0();
d9c2f47f 330 return (0);
4147b3f6
BJ
331}
332
9e97623a
CT
333struct osigsetmask_args {
334 int mask;
335};
e635a749
MK
336osigsetmask(p, uap, retval)
337 struct proc *p;
9e97623a 338 struct osigsetmask_args *uap;
e635a749
MK
339 int *retval;
340{
687880f9 341
feff6b5a 342 (void) splhigh();
e635a749 343 *retval = p->p_sigmask;
f4af9a5b 344 p->p_sigmask = uap->mask &~ sigcantmask;
dd012d1e 345 (void) spl0();
d9c2f47f 346 return (0);
687880f9 347}
0c1cfb60 348#endif /* COMPAT_43 || COMPAT_SUNOS */
687880f9 349
f4af9a5b
MK
350/*
351 * Suspend process until signal, providing mask to be set
352 * in the meantime. Note nonstandard calling convention:
353 * libc stub passes mask, not pointer, to save a copyin.
354 */
9e97623a
CT
355struct sigsuspend_args {
356 sigset_t mask;
357};
e635a749
MK
358/* ARGSUSED */
359sigsuspend(p, uap, retval)
360 register struct proc *p;
9e97623a 361 struct sigsuspend_args *uap;
e635a749
MK
362 int *retval;
363{
8429d022 364 register struct sigacts *ps = p->p_sigacts;
4147b3f6 365
dd012d1e
SL
366 /*
367 * When returning from sigpause, we want
368 * the old mask to be restored after the
369 * signal handler has finished. Thus, we
5077c7b9
MK
370 * save it here and mark the sigacts structure
371 * to indicate this.
dd012d1e 372 */
8429d022 373 ps->ps_oldmask = p->p_sigmask;
ebf08548 374 ps->ps_flags |= SAS_OLDMASK;
f4af9a5b 375 p->p_sigmask = uap->mask &~ sigcantmask;
92900edc
KM
376 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
377 /* void */;
22585170 378 /* always return EINTR rather than ERESTART... */
d9c2f47f 379 return (EINTR);
4147b3f6
BJ
380}
381
0c1cfb60 382#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
9e97623a
CT
383struct osigstack_args {
384 struct sigstack *nss;
385 struct sigstack *oss;
386};
e635a749 387/* ARGSUSED */
ebf08548 388osigstack(p, uap, retval)
e635a749 389 struct proc *p;
9e97623a 390 register struct osigstack_args *uap;
e635a749
MK
391 int *retval;
392{
457aa395 393 struct sigstack ss;
ebf08548 394 struct sigacts *psp;
f4af9a5b
MK
395 int error = 0;
396
ebf08548
KM
397 psp = p->p_sigacts;
398 ss.ss_sp = psp->ps_sigstk.ss_base;
399 ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
400 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
401 sizeof (struct sigstack))))
d9c2f47f 402 return (error);
f4af9a5b 403 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
ebf08548
KM
404 sizeof (ss))) == 0) {
405 psp->ps_sigstk.ss_base = ss.ss_sp;
406 psp->ps_sigstk.ss_size = 0;
407 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
408 psp->ps_flags |= SAS_ALTSTACK;
409 }
d9c2f47f 410 return (error);
4147b3f6 411}
0c1cfb60 412#endif /* COMPAT_43 || COMPAT_SUNOS */
ebf08548 413
9e97623a
CT
414struct sigaltstack_args {
415 struct sigaltstack *nss;
416 struct sigaltstack *oss;
417};
ebf08548
KM
418/* ARGSUSED */
419sigaltstack(p, uap, retval)
420 struct proc *p;
9e97623a 421 register struct sigaltstack_args *uap;
ebf08548
KM
422 int *retval;
423{
424 struct sigacts *psp;
425 struct sigaltstack ss;
426 int error;
427
428 psp = p->p_sigacts;
429 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
430 psp->ps_sigstk.ss_flags |= SA_DISABLE;
431 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
432 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
433 return (error);
d9a7fa42
KM
434 if (uap->nss == 0)
435 return (0);
436 if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))
ebf08548
KM
437 return (error);
438 if (ss.ss_flags & SA_DISABLE) {
439 if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
440 return (EINVAL);
441 psp->ps_flags &= ~SAS_ALTSTACK;
442 psp->ps_sigstk.ss_flags = ss.ss_flags;
443 return (0);
444 }
445 if (ss.ss_size < MINSIGSTKSZ)
446 return (ENOMEM);
447 psp->ps_flags |= SAS_ALTSTACK;
448 psp->ps_sigstk= ss;
449 return (0);
450}
4147b3f6 451
9e97623a
CT
452struct kill_args {
453 int pid;
802236f4 454 int signum;
9e97623a 455};
e635a749
MK
456/* ARGSUSED */
457kill(cp, uap, retval)
458 register struct proc *cp;
9e97623a 459 register struct kill_args *uap;
e635a749
MK
460 int *retval;
461{
7713be67 462 register struct proc *p;
8429d022 463 register struct pcred *pc = cp->p_cred;
1e3738da 464
802236f4 465 if ((u_int)uap->signum >= NSIG)
d9c2f47f 466 return (EINVAL);
7713be67
KM
467 if (uap->pid > 0) {
468 /* kill single process */
802236f4 469 if ((p = pfind(uap->pid)) == NULL)
d9c2f47f 470 return (ESRCH);
802236f4 471 if (!CANSIGNAL(cp, pc, p, uap->signum))
d9c2f47f 472 return (EPERM);
802236f4
KB
473 if (uap->signum)
474 psignal(p, uap->signum);
d9c2f47f 475 return (0);
7713be67
KM
476 }
477 switch (uap->pid) {
478 case -1: /* broadcast signal */
802236f4 479 return (killpg1(cp, uap->signum, 0, 1));
7713be67 480 case 0: /* signal own process group */
802236f4 481 return (killpg1(cp, uap->signum, 0, 0));
7713be67 482 default: /* negative explicit process group */
802236f4 483 return (killpg1(cp, uap->signum, -uap->pid, 0));
7713be67 484 }
f4af9a5b 485 /* NOTREACHED */
1e3738da
BJ
486}
487
0cbc4437 488#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
9e97623a
CT
489struct okillpg_args {
490 int pgid;
802236f4 491 int signum;
9e97623a 492};
e635a749
MK
493/* ARGSUSED */
494okillpg(p, uap, retval)
495 struct proc *p;
9e97623a 496 register struct okillpg_args *uap;
e635a749
MK
497 int *retval;
498{
1e3738da 499
802236f4 500 if ((u_int)uap->signum >= NSIG)
d9c2f47f 501 return (EINVAL);
802236f4 502 return (killpg1(p, uap->signum, uap->pgid, 0));
1e3738da 503}
0c1cfb60 504#endif /* COMPAT_43 || COMPAT_SUNOS */
dd012d1e 505
e635a749
MK
506/*
507 * Common code for kill process group/broadcast kill.
508 * cp is calling process.
509 */
802236f4 510killpg1(cp, signum, pgid, all)
e635a749 511 register struct proc *cp;
802236f4 512 int signum, pgid, all;
3b4f6e10
SL
513{
514 register struct proc *p;
8429d022 515 register struct pcred *pc = cp->p_cred;
8fe87cbb 516 struct pgrp *pgrp;
8429d022 517 int nfound = 0;
8fe87cbb
MT
518
519 if (all)
520 /*
521 * broadcast
687880f9 522 */
80a8e5e6 523 for (p = (struct proc *)allproc; p != NULL; p = p->p_nxt) {
90f62fff 524 if (p->p_pid <= 1 || p->p_flag & SSYS ||
802236f4 525 p == cp || !CANSIGNAL(cp, pc, p, signum))
8fe87cbb 526 continue;
8429d022 527 nfound++;
802236f4
KB
528 if (signum)
529 psignal(p, signum);
8fe87cbb
MT
530 }
531 else {
532 if (pgid == 0)
533 /*
534 * zero pgid means send to my process group.
535 */
8429d022 536 pgrp = cp->p_pgrp;
8fe87cbb
MT
537 else {
538 pgrp = pgfind(pgid);
539 if (pgrp == NULL)
f4af9a5b 540 return (ESRCH);
8fe87cbb 541 }
8fe87cbb 542 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
90f62fff 543 if (p->p_pid <= 1 || p->p_flag & SSYS ||
802236f4
KB
544 p->p_stat == SZOMB ||
545 !CANSIGNAL(cp, pc, p, signum))
8fe87cbb 546 continue;
8429d022 547 nfound++;
802236f4
KB
548 if (signum)
549 psignal(p, signum);
7713be67 550 }
687880f9 551 }
8429d022 552 return (nfound ? 0 : ESRCH);
4147b3f6 553}
687880f9 554
e635a749 555/*
802236f4 556 * Send a signal to a process group.
687880f9 557 */
8429d022 558void
802236f4
KB
559gsignal(pgid, signum)
560 int pgid, signum;
8fe87cbb 561{
f4af9a5b 562 struct pgrp *pgrp;
8fe87cbb 563
f4af9a5b 564 if (pgid && (pgrp = pgfind(pgid)))
802236f4 565 pgsignal(pgrp, signum, 0);
8fe87cbb 566}
e635a749 567
22585170 568/*
802236f4
KB
569 * Send a signal to a process group. If checktty is 1,
570 * limit to members which have a controlling terminal.
22585170 571 */
8429d022 572void
802236f4 573pgsignal(pgrp, signum, checkctty)
f4af9a5b 574 struct pgrp *pgrp;
802236f4 575 int signum, 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)
802236f4 582 psignal(p, signum);
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 590void
802236f4 591trapsignal(p, signum, code)
8429d022 592 struct proc *p;
802236f4
KB
593 register int signum;
594 u_int code;
f4af9a5b 595{
8429d022 596 register struct sigacts *ps = p->p_sigacts;
f4af9a5b
MK
597 int mask;
598
802236f4 599 mask = sigmask(signum);
f4af9a5b
MK
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))
802236f4 605 ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
22585170
MT
606 p->p_sigmask, code);
607#endif
802236f4
KB
608 sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, code);
609 p->p_sigmask |= ps->ps_catchmask[signum] | mask;
f4af9a5b 610 } else {
8429d022 611 ps->ps_code = code; /* XXX for core dump/debugger */
802236f4 612 psignal(p, signum);
f4af9a5b 613 }
687880f9
BJ
614}
615
616/*
802236f4
KB
617 * Send the signal to the process. If the signal has an action, the action
618 * is usually performed by the target process rather than the caller; we add
8429d022 619 * the signal to the set of pending signals for the process.
802236f4 620 *
22585170 621 * Exceptions:
802236f4
KB
622 * o When a stop signal is sent to a sleeping process that takes the
623 * default action, the process is stopped without awakening it.
22585170
MT
624 * o SIGCONT restarts stopped processes (or puts them back to sleep)
625 * regardless of the signal action (eg, blocked or ignored).
802236f4 626 *
22585170 627 * Other ignored signals are discarded immediately.
687880f9 628 */
8429d022 629void
802236f4 630psignal(p, signum)
687880f9 631 register struct proc *p;
802236f4 632 register int signum;
687880f9 633{
8429d022 634 register int s, prop;
f4af9a5b 635 register sig_t action;
feff6b5a 636 int mask;
687880f9 637
802236f4
KB
638 if ((u_int)signum >= NSIG || signum == 0)
639 panic("psignal signal number");
640 mask = sigmask(signum);
641 prop = sigprop[signum];
687880f9
BJ
642
643 /*
644 * If proc is traced, always give parent a chance.
687880f9
BJ
645 */
646 if (p->p_flag & STRC)
647 action = SIG_DFL;
648 else {
687880f9 649 /*
dd012d1e
SL
650 * If the signal is being ignored,
651 * then we forget about it immediately.
f4af9a5b
MK
652 * (Note: we don't set SIGCONT in p_sigignore,
653 * and if it is set to SIG_IGN,
654 * action will be SIG_DFL here.)
687880f9 655 */
feff6b5a 656 if (p->p_sigignore & mask)
687880f9 657 return;
feff6b5a 658 if (p->p_sigmask & mask)
dd012d1e 659 action = SIG_HOLD;
feff6b5a 660 else if (p->p_sigcatch & mask)
dd012d1e 661 action = SIG_CATCH;
e51c8e4a 662 else
dd012d1e 663 action = SIG_DFL;
687880f9 664 }
687880f9 665
90f62fff
KM
666 if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
667 (p->p_flag & STRC) == 0)
8429d022 668 p->p_nice = NZERO;
687880f9 669
8429d022 670 if (prop & SA_CONT)
f4af9a5b 671 p->p_sig &= ~stopsigmask;
f4af9a5b 672
8429d022 673 if (prop & SA_STOP) {
faf9c963
MK
674 /*
675 * If sending a tty stop signal to a member of an orphaned
676 * process group, discard the signal here if the action
677 * is default; don't stop the process below if sleeping,
678 * and don't clear any pending SIGCONT.
679 */
8429d022
MK
680 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
681 action == SIG_DFL)
9db58063 682 return;
8429d022 683 p->p_sig &= ~contsigmask;
687880f9 684 }
f4af9a5b
MK
685 p->p_sig |= mask;
686
687880f9 687 /*
f4af9a5b
MK
688 * Defer further processing for signals which are held,
689 * except that stopped processes must be continued by SIGCONT.
687880f9 690 */
8429d022 691 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
687880f9 692 return;
feff6b5a 693 s = splhigh();
687880f9
BJ
694 switch (p->p_stat) {
695
696 case SSLEEP:
697 /*
22585170 698 * If process is sleeping uninterruptibly
687880f9
BJ
699 * we can't interrupt the sleep... the signal will
700 * be noticed when the process returns through
701 * trap() or syscall().
702 */
22585170 703 if ((p->p_flag & SSINTR) == 0)
687880f9
BJ
704 goto out;
705 /*
706 * Process is sleeping and traced... make it runnable
707 * so it can discover the signal in issig() and stop
708 * for the parent.
709 */
90f62fff 710 if (p->p_flag & STRC)
687880f9 711 goto run;
90f62fff
KM
712 /*
713 * If SIGCONT is default (or ignored) and process is
714 * asleep, we are finished; the process should not
715 * be awakened.
716 */
717 if ((prop & SA_CONT) && action == SIG_DFL) {
718 p->p_sig &= ~mask;
719 goto out;
720 }
f4af9a5b
MK
721 /*
722 * When a sleeping process receives a stop
723 * signal, process immediately if possible.
724 * All other (caught or default) signals
725 * cause the process to run.
726 */
8429d022 727 if (prop & SA_STOP) {
687880f9 728 if (action != SIG_DFL)
f4af9a5b 729 goto runfast;
687880f9 730 /*
8429d022
MK
731 * If a child holding parent blocked,
732 * stopping could cause deadlock.
687880f9 733 */
90f62fff 734 if (p->p_flag & SPPWAIT)
687880f9 735 goto out;
feff6b5a 736 p->p_sig &= ~mask;
802236f4 737 p->p_xstat = signum;
f4af9a5b
MK
738 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
739 psignal(p->p_pptr, SIGCHLD);
687880f9
BJ
740 stop(p);
741 goto out;
f4af9a5b
MK
742 } else
743 goto runfast;
687880f9
BJ
744 /*NOTREACHED*/
745
746 case SSTOP:
747 /*
748 * If traced process is already stopped,
749 * then no further action is necessary.
750 */
90f62fff 751 if (p->p_flag & STRC)
687880f9 752 goto out;
687880f9 753
8429d022
MK
754 /*
755 * Kill signal always sets processes running.
756 */
802236f4 757 if (signum == SIGKILL)
f4af9a5b 758 goto runfast;
687880f9 759
8429d022 760 if (prop & SA_CONT) {
687880f9 761 /*
f4af9a5b
MK
762 * If SIGCONT is default (or ignored), we continue
763 * the process but don't leave the signal in p_sig,
764 * as it has no further action. If SIGCONT is held,
765 * continue the process and leave the signal in p_sig.
687880f9
BJ
766 * If the process catches SIGCONT, let it handle
767 * the signal itself. If it isn't waiting on
768 * an event, then it goes back to run state.
769 * Otherwise, process goes back to sleep state.
770 */
f4af9a5b
MK
771 if (action == SIG_DFL)
772 p->p_sig &= ~mask;
773 if (action == SIG_CATCH)
774 goto runfast;
775 if (p->p_wchan == 0)
687880f9
BJ
776 goto run;
777 p->p_stat = SSLEEP;
778 goto out;
8429d022 779 }
687880f9 780
8429d022 781 if (prop & SA_STOP) {
687880f9
BJ
782 /*
783 * Already stopped, don't need to stop again.
784 * (If we did the shell could get confused.)
785 */
feff6b5a 786 p->p_sig &= ~mask; /* take it away */
687880f9 787 goto out;
687880f9 788 }
8429d022
MK
789
790 /*
791 * If process is sleeping interruptibly, then
792 * simulate a wakeup so that when it is continued,
793 * it will be made runnable and can look at the signal.
794 * But don't setrun the process, leave it stopped.
795 */
796 if (p->p_wchan && p->p_flag & SSINTR)
797 unsleep(p);
798 goto out;
687880f9
BJ
799
800 default:
801 /*
802 * SRUN, SIDL, SZOMB do nothing with the signal,
803 * other than kicking ourselves if we are running.
804 * It will either never be noticed, or noticed very soon.
805 */
35493b0f 806 if (p == curproc)
8e6cd807 807 signotify(p);
687880f9
BJ
808 goto out;
809 }
810 /*NOTREACHED*/
f4af9a5b
MK
811
812runfast:
687880f9
BJ
813 /*
814 * Raise priority to at least PUSER.
815 */
816 if (p->p_pri > PUSER)
e4f89871 817 p->p_pri = PUSER;
f4af9a5b 818run:
687880f9
BJ
819 setrun(p);
820out:
821 splx(s);
822}
823
824/*
802236f4
KB
825 * If the current process has received a signal (should be caught or cause
826 * termination, should interrupt current syscall), return the signal number.
827 * Stop signals with default action are processed immediately, then cleared;
828 * they aren't returned. This is checked after each entry to the system for
829 * a syscall or trap (though this can usually be done without calling issig
830 * by checking the pending signal masks in the CURSIG macro.) The normal call
831 * sequence is
8429d022 832 *
802236f4
KB
833 * while (signum = CURSIG(curproc))
834 * psig(signum);
687880f9 835 */
8429d022
MK
836issig(p)
837 register struct proc *p;
687880f9 838{
802236f4 839 register int signum, 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);
802236f4
KB
847 signum = ffs((long)mask);
848 mask = sigmask(signum);
849 prop = sigprop[signum];
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 */
802236f4 863 p->p_xstat = signum;
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 */
802236f4
KB
885 signum = p->p_xstat;
886 if (signum == 0)
687880f9 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 */
802236f4 894 mask = sigmask(signum);
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 */
802236f4 905 switch ((int)p->p_sigacts->ps_sigact[signum]) {
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",
802236f4 918 p->p_pid, signum);
fc080d8d 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 */
802236f4 934 p->p_xstat = signum;
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
802236f4 947 return (signum);
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 */
802236f4 965 return (signum);
687880f9 966 }
22585170 967 p->p_sig &= ~mask; /* take the signal! */
687880f9 968 }
22585170 969 /* NOTREACHED */
687880f9
BJ
970}
971
687880f9 972/*
802236f4
KB
973 * Put the argument process into the stopped state and notify the parent
974 * via wakeup. Signals are handled elsewhere. The process must not be
975 * on the run queue.
687880f9
BJ
976 */
977stop(p)
978 register struct proc *p;
979{
980
981 p->p_stat = SSTOP;
982 p->p_flag &= ~SWTED;
983 wakeup((caddr_t)p->p_pptr);
687880f9
BJ
984}
985
986/*
8429d022
MK
987 * Take the action for the specified signal
988 * from the current set of pending signals.
687880f9 989 */
8429d022 990void
802236f4
KB
991psig(signum)
992 register int signum;
687880f9 993{
8429d022
MK
994 register struct proc *p = curproc;
995 register struct sigacts *ps = p->p_sigacts;
f4af9a5b 996 register sig_t action;
8429d022 997 int mask, returnmask;
f4af9a5b 998
22585170 999#ifdef DIAGNOSTIC
802236f4 1000 if (signum == 0)
8429d022 1001 panic("psig");
22585170 1002#endif
802236f4 1003 mask = sigmask(signum);
8429d022 1004 p->p_sig &= ~mask;
802236f4 1005 action = ps->ps_sigact[signum];
22585170 1006#ifdef KTRACE
8429d022 1007 if (KTRPOINT(p, KTR_PSIG))
802236f4
KB
1008 ktrpsig(p->p_tracep,
1009 signum, 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 */
802236f4 1017 sigexit(p, signum);
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;
802236f4 1042 p->p_sigmask |= ps->ps_catchmask[signum] | mask;
8429d022
MK
1043 (void) spl0();
1044 p->p_stats->p_ru.ru_nsignals++;
802236f4 1045 sendsig(action, signum, 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 1062/*
802236f4
KB
1063 * Force the current process to exit with the specified signal, dumping core
1064 * if appropriate. We bypass the normal tests for masked and caught signals,
1065 * allowing unrecoverable failures to terminate the process without changing
1066 * signal state. Mark the accounting record with the signal termination.
1067 * If dumping core, save the signal number for the debugger. Calls exit and
1068 * does not return.
35493b0f 1069 */
802236f4 1070sigexit(p, signum)
35493b0f 1071 register struct proc *p;
802236f4 1072 int signum;
35493b0f
MK
1073{
1074
1075 p->p_acflag |= AXSIG;
802236f4
KB
1076 if (sigprop[signum] & SA_CORE) {
1077 p->p_sigacts->ps_sig = signum;
35493b0f 1078 if (coredump(p) == 0)
802236f4 1079 signum |= WCOREFLAG;
35493b0f 1080 }
802236f4 1081 exit1(p, W_EXITCODE(0, signum));
35493b0f
MK
1082 /* NOTREACHED */
1083}
1084
687880f9 1085/*
802236f4
KB
1086 * Dump core, into a file named "core.progname".
1087 * Do not drop core if the process was setuid/setgid.
687880f9 1088 */
8429d022
MK
1089coredump(p)
1090 register struct proc *p;
687880f9 1091{
c4ec2128 1092 register struct vnode *vp;
8429d022
MK
1093 register struct pcred *pcred = p->p_cred;
1094 register struct ucred *cred = pcred->pc_ucred;
1095 register struct vmspace *vm = p->p_vmspace;
d86a61ec 1096 struct vattr vattr;
79583d0d 1097 int error, error1;
8429d022 1098 struct nameidata nd;
802236f4 1099 char name[MAXCOMLEN+6]; /* progname.core */
687880f9 1100
802236f4 1101 if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
d86a61ec 1102 return (EFAULT);
8429d022
MK
1103 if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1104 p->p_rlimit[RLIMIT_CORE].rlim_cur)
d86a61ec 1105 return (EFAULT);
802236f4 1106 sprintf(name, "%s.core", p->p_comm);
0c9db509 1107 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
802236f4
KB
1108 if (error = vn_open(&nd,
1109 O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
d86a61ec 1110 return (error);
8429d022 1111 vp = nd.ni_vp;
802236f4
KB
1112
1113 /* Don't dump to non-regular files or files with links. */
1114 if (vp->v_type != VREG ||
1115 VOP_GETATTR(vp, &vattr, cred, p) || 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}