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