check for negative bytes in seguse update; increment version number
[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 *
9e97623a 7 * @(#)kern_sig.c 7.50 (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 "acct.h"
d86a61ec 22#include "file.h"
94368568 23#include "kernel.h"
f4af9a5b 24#include "wait.h"
22585170 25#include "ktrace.h"
687880f9 26
8e6cd807
MK
27#include "machine/cpu.h"
28
35493b0f
MK
29#include "vm/vm.h"
30#include "kinfo_proc.h"
31#include "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;
8429d022 373 (void) tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0);
22585170 374 /* always return EINTR rather than ERESTART... */
d9c2f47f 375 return (EINTR);
4147b3f6
BJ
376}
377
0c1cfb60 378#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
9e97623a
CT
379struct osigstack_args {
380 struct sigstack *nss;
381 struct sigstack *oss;
382};
e635a749 383/* ARGSUSED */
ebf08548 384osigstack(p, uap, retval)
e635a749 385 struct proc *p;
9e97623a 386 register struct osigstack_args *uap;
e635a749
MK
387 int *retval;
388{
457aa395 389 struct sigstack ss;
ebf08548 390 struct sigacts *psp;
f4af9a5b
MK
391 int error = 0;
392
ebf08548
KM
393 psp = p->p_sigacts;
394 ss.ss_sp = psp->ps_sigstk.ss_base;
395 ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
396 if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
397 sizeof (struct sigstack))))
d9c2f47f 398 return (error);
f4af9a5b 399 if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
ebf08548
KM
400 sizeof (ss))) == 0) {
401 psp->ps_sigstk.ss_base = ss.ss_sp;
402 psp->ps_sigstk.ss_size = 0;
403 psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
404 psp->ps_flags |= SAS_ALTSTACK;
405 }
d9c2f47f 406 return (error);
4147b3f6 407}
0c1cfb60 408#endif /* COMPAT_43 || COMPAT_SUNOS */
ebf08548 409
9e97623a
CT
410struct sigaltstack_args {
411 struct sigaltstack *nss;
412 struct sigaltstack *oss;
413};
ebf08548
KM
414/* ARGSUSED */
415sigaltstack(p, uap, retval)
416 struct proc *p;
9e97623a 417 register struct sigaltstack_args *uap;
ebf08548
KM
418 int *retval;
419{
420 struct sigacts *psp;
421 struct sigaltstack ss;
422 int error;
423
424 psp = p->p_sigacts;
425 if ((psp->ps_flags & SAS_ALTSTACK) == 0)
426 psp->ps_sigstk.ss_flags |= SA_DISABLE;
427 if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
428 (caddr_t)uap->oss, sizeof (struct sigaltstack))))
429 return (error);
d9a7fa42
KM
430 if (uap->nss == 0)
431 return (0);
432 if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))
ebf08548
KM
433 return (error);
434 if (ss.ss_flags & SA_DISABLE) {
435 if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
436 return (EINVAL);
437 psp->ps_flags &= ~SAS_ALTSTACK;
438 psp->ps_sigstk.ss_flags = ss.ss_flags;
439 return (0);
440 }
441 if (ss.ss_size < MINSIGSTKSZ)
442 return (ENOMEM);
443 psp->ps_flags |= SAS_ALTSTACK;
444 psp->ps_sigstk= ss;
445 return (0);
446}
4147b3f6 447
9e97623a
CT
448struct kill_args {
449 int pid;
450 int signo;
451};
e635a749
MK
452/* ARGSUSED */
453kill(cp, uap, retval)
454 register struct proc *cp;
9e97623a 455 register struct kill_args *uap;
e635a749
MK
456 int *retval;
457{
7713be67 458 register struct proc *p;
8429d022 459 register struct pcred *pc = cp->p_cred;
1e3738da 460
f4af9a5b 461 if ((unsigned) uap->signo >= NSIG)
d9c2f47f 462 return (EINVAL);
7713be67
KM
463 if (uap->pid > 0) {
464 /* kill single process */
465 p = pfind(uap->pid);
f4af9a5b 466 if (p == 0)
d9c2f47f 467 return (ESRCH);
8429d022 468 if (!CANSIGNAL(cp, pc, p, uap->signo))
d9c2f47f 469 return (EPERM);
f4af9a5b 470 if (uap->signo)
7713be67 471 psignal(p, uap->signo);
d9c2f47f 472 return (0);
7713be67
KM
473 }
474 switch (uap->pid) {
475 case -1: /* broadcast signal */
d9c2f47f 476 return (killpg1(cp, uap->signo, 0, 1));
7713be67 477 case 0: /* signal own process group */
d9c2f47f 478 return (killpg1(cp, uap->signo, 0, 0));
7713be67 479 default: /* negative explicit process group */
d9c2f47f 480 return (killpg1(cp, uap->signo, -uap->pid, 0));
7713be67 481 }
f4af9a5b 482 /* NOTREACHED */
1e3738da
BJ
483}
484
0cbc4437 485#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
9e97623a
CT
486struct okillpg_args {
487 int pgid;
488 int signo;
489};
e635a749
MK
490/* ARGSUSED */
491okillpg(p, uap, retval)
492 struct proc *p;
9e97623a 493 register struct okillpg_args *uap;
e635a749
MK
494 int *retval;
495{
1e3738da 496
f4af9a5b 497 if ((unsigned) uap->signo >= NSIG)
d9c2f47f
MK
498 return (EINVAL);
499 return (killpg1(p, uap->signo, uap->pgid, 0));
1e3738da 500}
0c1cfb60 501#endif /* COMPAT_43 || COMPAT_SUNOS */
dd012d1e 502
e635a749
MK
503/*
504 * Common code for kill process group/broadcast kill.
505 * cp is calling process.
506 */
507killpg1(cp, signo, pgid, all)
508 register struct proc *cp;
8fe87cbb 509 int signo, pgid, all;
3b4f6e10
SL
510{
511 register struct proc *p;
8429d022 512 register struct pcred *pc = cp->p_cred;
8fe87cbb 513 struct pgrp *pgrp;
8429d022 514 int nfound = 0;
8fe87cbb
MT
515
516 if (all)
517 /*
518 * broadcast
687880f9 519 */
80a8e5e6 520 for (p = (struct proc *)allproc; p != NULL; p = p->p_nxt) {
8429d022
MK
521 if (p->p_pid <= 1 || p->p_flag&SSYS ||
522 p == cp || !CANSIGNAL(cp, pc, p, signo))
8fe87cbb 523 continue;
8429d022 524 nfound++;
8fe87cbb
MT
525 if (signo)
526 psignal(p, signo);
527 }
528 else {
529 if (pgid == 0)
530 /*
531 * zero pgid means send to my process group.
532 */
8429d022 533 pgrp = cp->p_pgrp;
8fe87cbb
MT
534 else {
535 pgrp = pgfind(pgid);
536 if (pgrp == NULL)
f4af9a5b 537 return (ESRCH);
8fe87cbb 538 }
8fe87cbb 539 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
8429d022 540 if (p->p_pid <= 1 || p->p_flag&SSYS ||
e3448cc4 541 p->p_stat == SZOMB || !CANSIGNAL(cp, pc, p, signo))
8fe87cbb 542 continue;
8429d022 543 nfound++;
8fe87cbb
MT
544 if (signo)
545 psignal(p, signo);
7713be67 546 }
687880f9 547 }
8429d022 548 return (nfound ? 0 : ESRCH);
4147b3f6 549}
687880f9 550
e635a749 551/*
687880f9 552 * Send the specified signal to
8fe87cbb 553 * all processes with 'pgid' as
687880f9 554 * process group.
687880f9 555 */
8429d022 556void
8fe87cbb 557gsignal(pgid, sig)
8429d022 558 int pgid, sig;
8fe87cbb 559{
f4af9a5b 560 struct pgrp *pgrp;
8fe87cbb 561
f4af9a5b 562 if (pgid && (pgrp = pgfind(pgid)))
c697d0b9 563 pgsignal(pgrp, sig, 0);
8fe87cbb 564}
e635a749 565
22585170 566/*
c697d0b9
MT
567 * Send sig to every member of a process group.
568 * If checktty is 1, limit to members which have a controlling
569 * terminal.
22585170 570 */
8429d022 571void
c697d0b9 572pgsignal(pgrp, sig, checkctty)
f4af9a5b 573 struct pgrp *pgrp;
8429d022 574 int sig, checkctty;
687880f9
BJ
575{
576 register struct proc *p;
577
22585170
MT
578 if (pgrp)
579 for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
c697d0b9
MT
580 if (checkctty == 0 || p->p_flag&SCTTY)
581 psignal(p, sig);
f4af9a5b
MK
582}
583
584/*
585 * Send a signal caused by a trap to the current process.
586 * If it will be caught immediately, deliver it with correct code.
587 * Otherwise, post it normally.
588 */
8429d022
MK
589void
590trapsignal(p, sig, code)
591 struct proc *p;
f4af9a5b
MK
592 register int sig;
593 unsigned code;
594{
8429d022 595 register struct sigacts *ps = p->p_sigacts;
f4af9a5b
MK
596 int mask;
597
598 mask = sigmask(sig);
599 if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
600 (p->p_sigmask & mask) == 0) {
8429d022 601 p->p_stats->p_ru.ru_nsignals++;
22585170
MT
602#ifdef KTRACE
603 if (KTRPOINT(p, KTR_PSIG))
8429d022 604 ktrpsig(p->p_tracep, sig, ps->ps_sigact[sig],
22585170
MT
605 p->p_sigmask, code);
606#endif
8429d022 607 sendsig(ps->ps_sigact[sig], sig, p->p_sigmask, code);
8429d022 608 p->p_sigmask |= ps->ps_catchmask[sig] | mask;
f4af9a5b 609 } else {
8429d022 610 ps->ps_code = code; /* XXX for core dump/debugger */
8fe87cbb 611 psignal(p, sig);
f4af9a5b 612 }
687880f9
BJ
613}
614
615/*
22585170 616 * Send the specified signal to the specified process.
8429d022
MK
617 * If the signal has an action, the action is usually performed
618 * by the target process rather than the caller; we simply add
619 * the signal to the set of pending signals for the process.
22585170
MT
620 * Exceptions:
621 * o When a stop signal is sent to a sleeping process that takes the default
622 * action, the process is stopped without awakening it.
623 * o SIGCONT restarts stopped processes (or puts them back to sleep)
624 * regardless of the signal action (eg, blocked or ignored).
625 * Other ignored signals are discarded immediately.
687880f9 626 */
8429d022 627void
687880f9
BJ
628psignal(p, sig)
629 register struct proc *p;
630 register int sig;
631{
8429d022 632 register int s, prop;
f4af9a5b 633 register sig_t action;
feff6b5a 634 int mask;
687880f9 635
f4af9a5b
MK
636 if ((unsigned)sig >= NSIG || sig == 0)
637 panic("psignal sig");
feff6b5a 638 mask = sigmask(sig);
8429d022 639 prop = sigprop[sig];
687880f9
BJ
640
641 /*
642 * If proc is traced, always give parent a chance.
687880f9
BJ
643 */
644 if (p->p_flag & STRC)
645 action = SIG_DFL;
646 else {
687880f9 647 /*
dd012d1e
SL
648 * If the signal is being ignored,
649 * then we forget about it immediately.
f4af9a5b
MK
650 * (Note: we don't set SIGCONT in p_sigignore,
651 * and if it is set to SIG_IGN,
652 * action will be SIG_DFL here.)
687880f9 653 */
feff6b5a 654 if (p->p_sigignore & mask)
687880f9 655 return;
feff6b5a 656 if (p->p_sigmask & mask)
dd012d1e 657 action = SIG_HOLD;
feff6b5a 658 else if (p->p_sigcatch & mask)
dd012d1e 659 action = SIG_CATCH;
e51c8e4a 660 else
dd012d1e 661 action = SIG_DFL;
687880f9 662 }
687880f9 663
8429d022
MK
664 if (p->p_nice > NZERO && (sig == SIGKILL ||
665 sig == SIGTERM && (p->p_flag&STRC || action != SIG_DFL)))
666 p->p_nice = NZERO;
687880f9 667
8429d022 668 if (prop & SA_CONT)
f4af9a5b 669 p->p_sig &= ~stopsigmask;
f4af9a5b 670
8429d022 671 if (prop & SA_STOP) {
faf9c963
MK
672 /*
673 * If sending a tty stop signal to a member of an orphaned
674 * process group, discard the signal here if the action
675 * is default; don't stop the process below if sleeping,
676 * and don't clear any pending SIGCONT.
677 */
8429d022
MK
678 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
679 action == SIG_DFL)
9db58063 680 return;
8429d022 681 p->p_sig &= ~contsigmask;
687880f9 682 }
f4af9a5b
MK
683 p->p_sig |= mask;
684
687880f9 685 /*
f4af9a5b
MK
686 * Defer further processing for signals which are held,
687 * except that stopped processes must be continued by SIGCONT.
687880f9 688 */
8429d022 689 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
687880f9 690 return;
feff6b5a 691 s = splhigh();
687880f9
BJ
692 switch (p->p_stat) {
693
694 case SSLEEP:
695 /*
22585170 696 * If process is sleeping uninterruptibly
687880f9
BJ
697 * we can't interrupt the sleep... the signal will
698 * be noticed when the process returns through
699 * trap() or syscall().
700 */
22585170 701 if ((p->p_flag & SSINTR) == 0)
687880f9
BJ
702 goto out;
703 /*
704 * Process is sleeping and traced... make it runnable
705 * so it can discover the signal in issig() and stop
706 * for the parent.
707 */
708 if (p->p_flag&STRC)
709 goto run;
f4af9a5b
MK
710 /*
711 * When a sleeping process receives a stop
712 * signal, process immediately if possible.
713 * All other (caught or default) signals
714 * cause the process to run.
715 */
8429d022 716 if (prop & SA_STOP) {
687880f9 717 if (action != SIG_DFL)
f4af9a5b 718 goto runfast;
687880f9 719 /*
8429d022
MK
720 * If a child holding parent blocked,
721 * stopping could cause deadlock.
687880f9 722 */
8429d022 723 if (p->p_flag&SPPWAIT)
687880f9 724 goto out;
feff6b5a 725 p->p_sig &= ~mask;
19dec745 726 p->p_xstat = sig;
f4af9a5b
MK
727 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
728 psignal(p->p_pptr, SIGCHLD);
687880f9
BJ
729 stop(p);
730 goto out;
f4af9a5b
MK
731 } else
732 goto runfast;
687880f9
BJ
733 /*NOTREACHED*/
734
735 case SSTOP:
736 /*
737 * If traced process is already stopped,
738 * then no further action is necessary.
739 */
740 if (p->p_flag&STRC)
741 goto out;
687880f9 742
8429d022
MK
743 /*
744 * Kill signal always sets processes running.
745 */
746 if (sig == SIGKILL)
f4af9a5b 747 goto runfast;
687880f9 748
8429d022 749 if (prop & SA_CONT) {
687880f9 750 /*
f4af9a5b
MK
751 * If SIGCONT is default (or ignored), we continue
752 * the process but don't leave the signal in p_sig,
753 * as it has no further action. If SIGCONT is held,
754 * continue the process and leave the signal in p_sig.
687880f9
BJ
755 * If the process catches SIGCONT, let it handle
756 * the signal itself. If it isn't waiting on
757 * an event, then it goes back to run state.
758 * Otherwise, process goes back to sleep state.
759 */
f4af9a5b
MK
760 if (action == SIG_DFL)
761 p->p_sig &= ~mask;
762 if (action == SIG_CATCH)
763 goto runfast;
764 if (p->p_wchan == 0)
687880f9
BJ
765 goto run;
766 p->p_stat = SSLEEP;
767 goto out;
8429d022 768 }
687880f9 769
8429d022 770 if (prop & SA_STOP) {
687880f9
BJ
771 /*
772 * Already stopped, don't need to stop again.
773 * (If we did the shell could get confused.)
774 */
feff6b5a 775 p->p_sig &= ~mask; /* take it away */
687880f9 776 goto out;
687880f9 777 }
8429d022
MK
778
779 /*
780 * If process is sleeping interruptibly, then
781 * simulate a wakeup so that when it is continued,
782 * it will be made runnable and can look at the signal.
783 * But don't setrun the process, leave it stopped.
784 */
785 if (p->p_wchan && p->p_flag & SSINTR)
786 unsleep(p);
787 goto out;
687880f9
BJ
788
789 default:
790 /*
791 * SRUN, SIDL, SZOMB do nothing with the signal,
792 * other than kicking ourselves if we are running.
793 * It will either never be noticed, or noticed very soon.
794 */
35493b0f 795 if (p == curproc)
8e6cd807 796 signotify(p);
687880f9
BJ
797 goto out;
798 }
799 /*NOTREACHED*/
f4af9a5b
MK
800
801runfast:
687880f9
BJ
802 /*
803 * Raise priority to at least PUSER.
804 */
805 if (p->p_pri > PUSER)
e4f89871 806 p->p_pri = PUSER;
f4af9a5b 807run:
687880f9
BJ
808 setrun(p);
809out:
810 splx(s);
811}
812
813/*
22585170
MT
814 * If the current process has a signal to process (should be caught
815 * or cause termination, should interrupt current syscall),
816 * return the signal number. Stop signals with default action
817 * are processed immediately, then cleared; they aren't returned.
8429d022
MK
818 * This is checked after each entry to the system for a syscall
819 * or trap (though this can usually be done without actually calling
820 * issig by checking the pending signal masks in the CURSIG macro.)
821 * The normal call sequence is
822 *
823 * while (sig = CURSIG(curproc))
824 * psig(sig);
687880f9 825 */
8429d022
MK
826issig(p)
827 register struct proc *p;
687880f9 828{
8429d022 829 register int sig, mask, prop;
687880f9 830
687880f9 831 for (;;) {
f4af9a5b 832 mask = p->p_sig &~ p->p_sigmask;
8429d022 833 if (p->p_flag&SPPWAIT)
f4af9a5b 834 mask &= ~stopsigmask;
22585170
MT
835 if (mask == 0) /* no signal to send */
836 return (0);
f4af9a5b 837 sig = ffs((long)mask);
feff6b5a 838 mask = sigmask(sig);
8429d022 839 prop = sigprop[sig];
22585170
MT
840 /*
841 * We should see pending but ignored signals
842 * only if STRC was on when they were posted.
843 */
844 if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
845 p->p_sig &= ~mask;
846 continue;
847 }
8429d022 848 if (p->p_flag&STRC && (p->p_flag&SPPWAIT) == 0) {
687880f9
BJ
849 /*
850 * If traced, always stop, and stay
851 * stopped until released by the parent.
852 */
19dec745 853 p->p_xstat = sig;
ad6c75cb 854 psignal(p->p_pptr, SIGCHLD);
687880f9
BJ
855 do {
856 stop(p);
857 swtch();
1bfd8b20 858 } while (!procxmt(p) && p->p_flag&STRC);
687880f9
BJ
859
860 /*
f99e4a3a 861 * If the traced bit got turned off,
22585170 862 * go back up to the top to rescan signals.
35493b0f
MK
863 * This ensures that p_sig* and ps_sigact
864 * are consistent.
687880f9 865 */
22585170 866 if ((p->p_flag&STRC) == 0)
687880f9 867 continue;
687880f9
BJ
868
869 /*
870 * If parent wants us to take the signal,
19dec745 871 * then it will leave it in p->p_xstat;
687880f9
BJ
872 * otherwise we just look for signals again.
873 */
22585170 874 p->p_sig &= ~mask; /* clear the old signal */
19dec745 875 sig = p->p_xstat;
687880f9
BJ
876 if (sig == 0)
877 continue;
f99e4a3a
SL
878
879 /*
22585170
MT
880 * Put the new signal into p_sig.
881 * If signal is being masked,
882 * look for other signals.
f99e4a3a 883 */
feff6b5a 884 mask = sigmask(sig);
22585170
MT
885 p->p_sig |= mask;
886 if (p->p_sigmask & mask)
f99e4a3a 887 continue;
687880f9 888 }
22585170
MT
889
890 /*
891 * Decide whether the signal should be returned.
892 * Return the signal's number, or fall through
893 * to clear it from the pending mask.
894 */
8429d022 895 switch ((int)p->p_sigacts->ps_sigact[sig]) {
687880f9
BJ
896
897 case SIG_DFL:
898 /*
899 * Don't take default actions on system processes.
900 */
fc080d8d
RC
901 if (p->p_pid <= 1) {
902#ifdef DIAGNOSTIC
903 /*
904 * Are you sure you want to ignore SIGSEGV
905 * in init? XXX
906 */
907 printf("Process (pid %d) got signal %d\n",
908 p->p_pid, sig);
909#endif
22585170 910 break; /* == ignore */
fc080d8d 911 }
22585170
MT
912 /*
913 * If there is a pending stop signal to process
914 * with default action, stop here,
e51c8e4a
MK
915 * then clear the signal. However,
916 * if process is member of an orphaned
917 * process group, ignore tty stop signals.
22585170 918 */
8429d022 919 if (prop & SA_STOP) {
e51c8e4a
MK
920 if (p->p_flag&STRC ||
921 (p->p_pgrp->pg_jobc == 0 &&
8429d022 922 prop & SA_TTYSTOP))
22585170 923 break; /* == ignore */
19dec745 924 p->p_xstat = sig;
687880f9 925 stop(p);
f4af9a5b
MK
926 if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
927 psignal(p->p_pptr, SIGCHLD);
687880f9 928 swtch();
22585170 929 break;
8429d022 930 } else if (prop & SA_IGNORE) {
687880f9 931 /*
f4af9a5b
MK
932 * Except for SIGCONT, shouldn't get here.
933 * Default action is to ignore; drop it.
687880f9 934 */
22585170 935 break; /* == ignore */
f4af9a5b 936 } else
22585170 937 return (sig);
687880f9
BJ
938 /*NOTREACHED*/
939
687880f9
BJ
940 case SIG_IGN:
941 /*
f4af9a5b
MK
942 * Masking above should prevent us ever trying
943 * to take action on an ignored signal other
944 * than SIGCONT, unless process is traced.
687880f9 945 */
8429d022 946 if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
687880f9 947 printf("issig\n");
22585170 948 break; /* == ignore */
687880f9
BJ
949
950 default:
951 /*
952 * This signal has an action, let
953 * psig process it.
954 */
22585170 955 return (sig);
687880f9 956 }
22585170 957 p->p_sig &= ~mask; /* take the signal! */
687880f9 958 }
22585170 959 /* NOTREACHED */
687880f9
BJ
960}
961
687880f9
BJ
962/*
963 * Put the argument process into the stopped
ad6c75cb
MK
964 * state and notify the parent via wakeup.
965 * Signals are handled elsewhere.
22585170 966 * The process must not be on the run queue.
687880f9
BJ
967 */
968stop(p)
969 register struct proc *p;
970{
971
972 p->p_stat = SSTOP;
973 p->p_flag &= ~SWTED;
974 wakeup((caddr_t)p->p_pptr);
687880f9
BJ
975}
976
977/*
8429d022
MK
978 * Take the action for the specified signal
979 * from the current set of pending signals.
687880f9 980 */
8429d022 981void
22585170
MT
982psig(sig)
983 register int sig;
687880f9 984{
8429d022
MK
985 register struct proc *p = curproc;
986 register struct sigacts *ps = p->p_sigacts;
f4af9a5b 987 register sig_t action;
8429d022 988 int mask, returnmask;
f4af9a5b 989
22585170 990#ifdef DIAGNOSTIC
8429d022
MK
991 if (sig == 0)
992 panic("psig");
22585170 993#endif
8429d022
MK
994 mask = sigmask(sig);
995 p->p_sig &= ~mask;
996 action = ps->ps_sigact[sig];
22585170 997#ifdef KTRACE
8429d022 998 if (KTRPOINT(p, KTR_PSIG))
ebf08548 999 ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SAS_OLDMASK ?
8429d022 1000 ps->ps_oldmask : p->p_sigmask, 0);
22585170 1001#endif
8429d022
MK
1002 if (action == SIG_DFL) {
1003 /*
1004 * Default action, where the default is to kill
1005 * the process. (Other cases were ignored above.)
8429d022 1006 */
35493b0f 1007 sigexit(p, sig);
8429d022
MK
1008 /* NOTREACHED */
1009 } else {
1010 /*
1011 * If we get here, the signal must be caught.
1012 */
f4af9a5b 1013#ifdef DIAGNOSTIC
8429d022
MK
1014 if (action == SIG_IGN || (p->p_sigmask & mask))
1015 panic("psig action");
f4af9a5b 1016#endif
8429d022
MK
1017 /*
1018 * Set the new mask value and also defer further
1019 * occurences of this signal.
1020 *
1021 * Special case: user has done a sigpause. Here the
1022 * current mask is not of interest, but rather the
1023 * mask from before the sigpause is what we want
1024 * restored after the signal processing is completed.
1025 */
1026 (void) splhigh();
ebf08548 1027 if (ps->ps_flags & SAS_OLDMASK) {
8429d022 1028 returnmask = ps->ps_oldmask;
ebf08548 1029 ps->ps_flags &= ~SAS_OLDMASK;
8429d022
MK
1030 } else
1031 returnmask = p->p_sigmask;
1032 p->p_sigmask |= ps->ps_catchmask[sig] | mask;
1033 (void) spl0();
1034 p->p_stats->p_ru.ru_nsignals++;
8429d022 1035 sendsig(action, sig, returnmask, 0);
8429d022 1036 }
687880f9
BJ
1037}
1038
35493b0f
MK
1039/*
1040 * Force the current process to exit with the specified
1041 * signal, dumping core if appropriate. We bypass the normal
1042 * tests for masked and caught signals, allowing unrecoverable
1043 * failures to terminate the process without changing signal state.
1044 * Mark the accounting record with the signal termination.
1045 * If dumping core, save the signal number for the debugger.
1046 * Calls exit and does not return.
1047 */
1048sigexit(p, sig)
1049 register struct proc *p;
1050 int sig;
1051{
1052
1053 p->p_acflag |= AXSIG;
1054 if (sigprop[sig] & SA_CORE) {
1055 p->p_sigacts->ps_sig = sig;
1056 if (coredump(p) == 0)
1057 sig |= WCOREFLAG;
1058 }
1059 exit(p, W_EXITCODE(0, sig));
1060 /* NOTREACHED */
1061}
1062
687880f9 1063/*
8429d022 1064 * Create a core dump.
bdb4bae9 1065 * The file name is "core.progname".
a91cddfd 1066 * Core dumps are not created if the process is setuid.
687880f9 1067 */
8429d022
MK
1068coredump(p)
1069 register struct proc *p;
687880f9 1070{
c4ec2128 1071 register struct vnode *vp;
8429d022
MK
1072 register struct pcred *pcred = p->p_cred;
1073 register struct ucred *cred = pcred->pc_ucred;
1074 register struct vmspace *vm = p->p_vmspace;
d86a61ec 1075 struct vattr vattr;
79583d0d 1076 int error, error1;
8429d022 1077 struct nameidata nd;
bdb4bae9 1078 char name[MAXCOMLEN+6]; /* core.progname */
687880f9 1079
8429d022
MK
1080 if (pcred->p_svuid != pcred->p_ruid ||
1081 pcred->p_svgid != pcred->p_rgid)
d86a61ec 1082 return (EFAULT);
8429d022
MK
1083 if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
1084 p->p_rlimit[RLIMIT_CORE].rlim_cur)
d86a61ec 1085 return (EFAULT);
bdb4bae9 1086 sprintf(name, "core.%s", p->p_comm);
0c9db509
JH
1087 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1088 if (error = vn_open(&nd, O_CREAT|FWRITE, 0644))
d86a61ec 1089 return (error);
8429d022 1090 vp = nd.ni_vp;
6bcdc4f3 1091 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
d86a61ec 1092 vattr.va_nlink != 1) {
79583d0d
KM
1093 error = EFAULT;
1094 goto out;
7b0cb7cb 1095 }
3ee1461b 1096 VATTR_NULL(&vattr);
d86a61ec 1097 vattr.va_size = 0;
4fcc40ae 1098 LEASE_CHECK(vp, p, cred, LEASE_WRITE);
6bcdc4f3 1099 VOP_SETATTR(vp, &vattr, cred, p);
8429d022 1100 p->p_acflag |= ACORE;
8e6cd807
MK
1101 bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1102 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
7b2c7fa0 1103 error = cpu_coredump(p, vp, cred);
d86a61ec 1104 if (error == 0)
8429d022
MK
1105 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1106 (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
8e6cd807 1107 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
d86a61ec
KM
1108 if (error == 0)
1109 error = vn_rdwr(UIO_WRITE, vp,
8e6cd807 1110 (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
8429d022
MK
1111 round_page(ctob(vm->vm_ssize)),
1112 (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
8e6cd807 1113 IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
79583d0d
KM
1114out:
1115 VOP_UNLOCK(vp);
1116 error1 = vn_close(vp, FWRITE, cred, p);
bdb4bae9 1117 if (error == 0)
79583d0d 1118 error = error1;
d86a61ec 1119 return (error);
687880f9 1120}
f4af9a5b
MK
1121
1122/*
1123 * Nonexistent system call-- signal process (may want to handle it).
1124 * Flag error in case process won't see signal immediately (blocked or ignored).
1125 */
9e97623a
CT
1126struct nosys_args {
1127 int dummy;
1128};
8eea23a6
KM
1129/* ARGSUSED */
1130nosys(p, args, retval)
1131 struct proc *p;
9e97623a 1132 struct nosys_args *args;
8eea23a6 1133 int *retval;
f4af9a5b
MK
1134{
1135
8eea23a6 1136 psignal(p, SIGSYS);
d9c2f47f 1137 return (EINVAL);
f4af9a5b 1138}