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