kern_sig.c revision 1.137 1 /* $NetBSD: kern_sig.c,v 1.137 2003/03/06 15:31:14 darrenr Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.137 2003/03/06 15:31:14 darrenr Exp $");
45
46 #include "opt_ktrace.h"
47 #include "opt_compat_sunos.h"
48 #include "opt_compat_netbsd32.h"
49
50 #define SIGPROP /* include signal properties table */
51 #include <sys/param.h>
52 #include <sys/signalvar.h>
53 #include <sys/resourcevar.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/proc.h>
57 #include <sys/systm.h>
58 #include <sys/timeb.h>
59 #include <sys/times.h>
60 #include <sys/buf.h>
61 #include <sys/acct.h>
62 #include <sys/file.h>
63 #include <sys/kernel.h>
64 #include <sys/wait.h>
65 #include <sys/ktrace.h>
66 #include <sys/syslog.h>
67 #include <sys/stat.h>
68 #include <sys/core.h>
69 #include <sys/filedesc.h>
70 #include <sys/malloc.h>
71 #include <sys/pool.h>
72 #include <sys/ucontext.h>
73 #include <sys/sa.h>
74 #include <sys/savar.h>
75 #include <sys/exec.h>
76
77 #include <sys/mount.h>
78 #include <sys/syscallargs.h>
79
80 #include <machine/cpu.h>
81
82 #include <sys/user.h> /* for coredump */
83
84 #include <uvm/uvm_extern.h>
85
86 static void proc_stop(struct proc *p);
87 static int build_corename(struct proc *, char [MAXPATHLEN]);
88 sigset_t contsigmask, stopsigmask, sigcantmask;
89
90 struct pool sigacts_pool; /* memory pool for sigacts structures */
91 struct pool siginfo_pool; /* memory pool for siginfo structures */
92
93 /*
94 * Can process p, with pcred pc, send the signal signum to process q?
95 */
96 #define CANSIGNAL(p, pc, q, signum) \
97 ((pc)->pc_ucred->cr_uid == 0 || \
98 (pc)->p_ruid == (q)->p_cred->p_ruid || \
99 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
100 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
101 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
102 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
103
104 /*
105 * Initialize signal-related data structures.
106 */
107 void
108 signal_init(void)
109 {
110
111 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
112 &pool_allocator_nointr);
113 pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
114 &pool_allocator_nointr);
115 }
116
117 /*
118 * Create an initial sigctx structure, using the same signal state
119 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
120 * copy it from parent.
121 */
122 void
123 sigactsinit(struct proc *np, struct proc *pp, int share)
124 {
125 struct sigacts *ps;
126
127 if (share) {
128 np->p_sigacts = pp->p_sigacts;
129 pp->p_sigacts->sa_refcnt++;
130 } else {
131 ps = pool_get(&sigacts_pool, PR_WAITOK);
132 if (pp)
133 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
134 else
135 memset(ps, '\0', sizeof(struct sigacts));
136 ps->sa_refcnt = 1;
137 np->p_sigacts = ps;
138 }
139 }
140
141 /*
142 * Make this process not share its sigctx, maintaining all
143 * signal state.
144 */
145 void
146 sigactsunshare(struct proc *p)
147 {
148 struct sigacts *oldps;
149
150 if (p->p_sigacts->sa_refcnt == 1)
151 return;
152
153 oldps = p->p_sigacts;
154 sigactsinit(p, NULL, 0);
155
156 if (--oldps->sa_refcnt == 0)
157 pool_put(&sigacts_pool, oldps);
158 }
159
160 /*
161 * Release a sigctx structure.
162 */
163 void
164 sigactsfree(struct proc *p)
165 {
166 struct sigacts *ps;
167
168 ps = p->p_sigacts;
169 if (--ps->sa_refcnt > 0)
170 return;
171
172 pool_put(&sigacts_pool, ps);
173 }
174
175 int
176 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
177 struct sigaction *osa, void *tramp, int vers)
178 {
179 struct sigacts *ps;
180 int prop;
181
182 ps = p->p_sigacts;
183 if (signum <= 0 || signum >= NSIG)
184 return (EINVAL);
185
186 /*
187 * Trampoline ABI version 0 is reserved for the legacy
188 * kernel-provided on-stack trampoline. Conversely, if
189 * we are using a non-0 ABI version, we must have a
190 * trampoline.
191 */
192 if ((vers != 0 && tramp == NULL) ||
193 (vers == 0 && tramp != NULL))
194 return (EINVAL);
195
196 if (osa)
197 *osa = SIGACTION_PS(ps, signum);
198
199 if (nsa) {
200 if (nsa->sa_flags & ~SA_ALLBITS)
201 return (EINVAL);
202
203 prop = sigprop[signum];
204 if (prop & SA_CANTMASK)
205 return (EINVAL);
206
207 (void) splsched(); /* XXXSMP */
208 SIGACTION_PS(ps, signum) = *nsa;
209 ps->sa_sigdesc[signum].sd_tramp = tramp;
210 ps->sa_sigdesc[signum].sd_vers = vers;
211 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
212 if ((prop & SA_NORESET) != 0)
213 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
214 if (signum == SIGCHLD) {
215 if (nsa->sa_flags & SA_NOCLDSTOP)
216 p->p_flag |= P_NOCLDSTOP;
217 else
218 p->p_flag &= ~P_NOCLDSTOP;
219 if (nsa->sa_flags & SA_NOCLDWAIT) {
220 /*
221 * Paranoia: since SA_NOCLDWAIT is implemented
222 * by reparenting the dying child to PID 1 (and
223 * trust it to reap the zombie), PID 1 itself
224 * is forbidden to set SA_NOCLDWAIT.
225 */
226 if (p->p_pid == 1)
227 p->p_flag &= ~P_NOCLDWAIT;
228 else
229 p->p_flag |= P_NOCLDWAIT;
230 } else
231 p->p_flag &= ~P_NOCLDWAIT;
232 }
233 if ((nsa->sa_flags & SA_NODEFER) == 0)
234 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
235 else
236 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
237 /*
238 * Set bit in p_sigctx.ps_sigignore for signals that are set to
239 * SIG_IGN, and for signals set to SIG_DFL where the default is
240 * to ignore. However, don't put SIGCONT in
241 * p_sigctx.ps_sigignore, as we have to restart the process.
242 */
243 if (nsa->sa_handler == SIG_IGN ||
244 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
245 /* never to be seen again */
246 sigdelset(&p->p_sigctx.ps_siglist, signum);
247 if (signum != SIGCONT) {
248 /* easier in psignal */
249 sigaddset(&p->p_sigctx.ps_sigignore, signum);
250 }
251 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
252 } else {
253 sigdelset(&p->p_sigctx.ps_sigignore, signum);
254 if (nsa->sa_handler == SIG_DFL)
255 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
256 else
257 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
258 }
259 (void) spl0();
260 }
261
262 return (0);
263 }
264
265 /* ARGSUSED */
266 int
267 sys___sigaction14(struct lwp *l, void *v, register_t *retval)
268 {
269 struct sys___sigaction14_args /* {
270 syscallarg(int) signum;
271 syscallarg(const struct sigaction *) nsa;
272 syscallarg(struct sigaction *) osa;
273 } */ *uap = v;
274 struct proc *p;
275 struct sigaction nsa, osa;
276 int error;
277
278 if (SCARG(uap, nsa)) {
279 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
280 if (error)
281 return (error);
282 }
283 p = l->l_proc;
284 error = sigaction1(p, SCARG(uap, signum),
285 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
286 NULL, 0);
287 if (error)
288 return (error);
289 if (SCARG(uap, osa)) {
290 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
291 if (error)
292 return (error);
293 }
294 return (0);
295 }
296
297 /* ARGSUSED */
298 int
299 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
300 {
301 struct sys___sigaction_sigtramp_args /* {
302 syscallarg(int) signum;
303 syscallarg(const struct sigaction *) nsa;
304 syscallarg(struct sigaction *) osa;
305 syscallarg(void *) tramp;
306 syscallarg(int) vers;
307 } */ *uap = v;
308 struct proc *p = l->l_proc;
309 struct sigaction nsa, osa;
310 int error;
311
312 if (SCARG(uap, nsa)) {
313 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
314 if (error)
315 return (error);
316 }
317 error = sigaction1(p, SCARG(uap, signum),
318 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
319 SCARG(uap, tramp), SCARG(uap, vers));
320 if (error)
321 return (error);
322 if (SCARG(uap, osa)) {
323 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
324 if (error)
325 return (error);
326 }
327 return (0);
328 }
329
330 /*
331 * Initialize signal state for process 0;
332 * set to ignore signals that are ignored by default and disable the signal
333 * stack.
334 */
335 void
336 siginit(struct proc *p)
337 {
338 struct sigacts *ps;
339 int signum, prop;
340
341 ps = p->p_sigacts;
342 sigemptyset(&contsigmask);
343 sigemptyset(&stopsigmask);
344 sigemptyset(&sigcantmask);
345 for (signum = 1; signum < NSIG; signum++) {
346 prop = sigprop[signum];
347 if (prop & SA_CONT)
348 sigaddset(&contsigmask, signum);
349 if (prop & SA_STOP)
350 sigaddset(&stopsigmask, signum);
351 if (prop & SA_CANTMASK)
352 sigaddset(&sigcantmask, signum);
353 if (prop & SA_IGNORE && signum != SIGCONT)
354 sigaddset(&p->p_sigctx.ps_sigignore, signum);
355 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
356 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
357 }
358 sigemptyset(&p->p_sigctx.ps_sigcatch);
359 p->p_sigctx.ps_sigwaited = 0;
360 p->p_flag &= ~P_NOCLDSTOP;
361
362 /*
363 * Reset stack state to the user stack.
364 */
365 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
366 p->p_sigctx.ps_sigstk.ss_size = 0;
367 p->p_sigctx.ps_sigstk.ss_sp = 0;
368
369 /* One reference. */
370 ps->sa_refcnt = 1;
371 }
372
373 /*
374 * Reset signals for an exec of the specified process.
375 */
376 void
377 execsigs(struct proc *p)
378 {
379 struct sigacts *ps;
380 int signum, prop;
381
382 sigactsunshare(p);
383
384 ps = p->p_sigacts;
385
386 /*
387 * Reset caught signals. Held signals remain held
388 * through p_sigctx.ps_sigmask (unless they were caught,
389 * and are now ignored by default).
390 */
391 for (signum = 1; signum < NSIG; signum++) {
392 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
393 prop = sigprop[signum];
394 if (prop & SA_IGNORE) {
395 if ((prop & SA_CONT) == 0)
396 sigaddset(&p->p_sigctx.ps_sigignore,
397 signum);
398 sigdelset(&p->p_sigctx.ps_siglist, signum);
399 }
400 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
401 }
402 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
403 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
404 }
405 sigemptyset(&p->p_sigctx.ps_sigcatch);
406 p->p_sigctx.ps_sigwaited = 0;
407 p->p_flag &= ~P_NOCLDSTOP;
408
409 /*
410 * Reset stack state to the user stack.
411 */
412 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
413 p->p_sigctx.ps_sigstk.ss_size = 0;
414 p->p_sigctx.ps_sigstk.ss_sp = 0;
415 }
416
417 int
418 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
419 {
420
421 if (oss)
422 *oss = p->p_sigctx.ps_sigmask;
423
424 if (nss) {
425 (void)splsched(); /* XXXSMP */
426 switch (how) {
427 case SIG_BLOCK:
428 sigplusset(nss, &p->p_sigctx.ps_sigmask);
429 break;
430 case SIG_UNBLOCK:
431 sigminusset(nss, &p->p_sigctx.ps_sigmask);
432 CHECKSIGS(p);
433 break;
434 case SIG_SETMASK:
435 p->p_sigctx.ps_sigmask = *nss;
436 CHECKSIGS(p);
437 break;
438 default:
439 (void)spl0(); /* XXXSMP */
440 return (EINVAL);
441 }
442 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
443 (void)spl0(); /* XXXSMP */
444 }
445
446 return (0);
447 }
448
449 /*
450 * Manipulate signal mask.
451 * Note that we receive new mask, not pointer,
452 * and return old mask as return value;
453 * the library stub does the rest.
454 */
455 int
456 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
457 {
458 struct sys___sigprocmask14_args /* {
459 syscallarg(int) how;
460 syscallarg(const sigset_t *) set;
461 syscallarg(sigset_t *) oset;
462 } */ *uap = v;
463 struct proc *p;
464 sigset_t nss, oss;
465 int error;
466
467 if (SCARG(uap, set)) {
468 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
469 if (error)
470 return (error);
471 }
472 p = l->l_proc;
473 error = sigprocmask1(p, SCARG(uap, how),
474 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
475 if (error)
476 return (error);
477 if (SCARG(uap, oset)) {
478 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
479 if (error)
480 return (error);
481 }
482 return (0);
483 }
484
485 void
486 sigpending1(struct proc *p, sigset_t *ss)
487 {
488
489 *ss = p->p_sigctx.ps_siglist;
490 sigminusset(&p->p_sigctx.ps_sigmask, ss);
491 }
492
493 /* ARGSUSED */
494 int
495 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
496 {
497 struct sys___sigpending14_args /* {
498 syscallarg(sigset_t *) set;
499 } */ *uap = v;
500 struct proc *p;
501 sigset_t ss;
502
503 p = l->l_proc;
504 sigpending1(p, &ss);
505 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
506 }
507
508 int
509 sigsuspend1(struct proc *p, const sigset_t *ss)
510 {
511 struct sigacts *ps;
512
513 ps = p->p_sigacts;
514 if (ss) {
515 /*
516 * When returning from sigpause, we want
517 * the old mask to be restored after the
518 * signal handler has finished. Thus, we
519 * save it here and mark the sigctx structure
520 * to indicate this.
521 */
522 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
523 p->p_sigctx.ps_flags |= SAS_OLDMASK;
524 (void) splsched(); /* XXXSMP */
525 p->p_sigctx.ps_sigmask = *ss;
526 CHECKSIGS(p);
527 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
528 (void) spl0(); /* XXXSMP */
529 }
530
531 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
532 /* void */;
533 /* always return EINTR rather than ERESTART... */
534 return (EINTR);
535 }
536
537 /*
538 * Suspend process until signal, providing mask to be set
539 * in the meantime. Note nonstandard calling convention:
540 * libc stub passes mask, not pointer, to save a copyin.
541 */
542 /* ARGSUSED */
543 int
544 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
545 {
546 struct sys___sigsuspend14_args /* {
547 syscallarg(const sigset_t *) set;
548 } */ *uap = v;
549 struct proc *p;
550 sigset_t ss;
551 int error;
552
553 if (SCARG(uap, set)) {
554 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
555 if (error)
556 return (error);
557 }
558
559 p = l->l_proc;
560 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
561 }
562
563 int
564 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
565 struct sigaltstack *oss)
566 {
567
568 if (oss)
569 *oss = p->p_sigctx.ps_sigstk;
570
571 if (nss) {
572 if (nss->ss_flags & ~SS_ALLBITS)
573 return (EINVAL);
574
575 if (nss->ss_flags & SS_DISABLE) {
576 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
577 return (EINVAL);
578 } else {
579 if (nss->ss_size < MINSIGSTKSZ)
580 return (ENOMEM);
581 }
582 p->p_sigctx.ps_sigstk = *nss;
583 }
584
585 return (0);
586 }
587
588 /* ARGSUSED */
589 int
590 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
591 {
592 struct sys___sigaltstack14_args /* {
593 syscallarg(const struct sigaltstack *) nss;
594 syscallarg(struct sigaltstack *) oss;
595 } */ *uap = v;
596 struct proc *p;
597 struct sigaltstack nss, oss;
598 int error;
599
600 if (SCARG(uap, nss)) {
601 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
602 if (error)
603 return (error);
604 }
605 p = l->l_proc;
606 error = sigaltstack1(p,
607 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
608 if (error)
609 return (error);
610 if (SCARG(uap, oss)) {
611 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
612 if (error)
613 return (error);
614 }
615 return (0);
616 }
617
618 /* ARGSUSED */
619 int
620 sys_kill(struct lwp *l, void *v, register_t *retval)
621 {
622 struct sys_kill_args /* {
623 syscallarg(int) pid;
624 syscallarg(int) signum;
625 } */ *uap = v;
626 struct proc *cp, *p;
627 struct pcred *pc;
628
629 cp = l->l_proc;
630 pc = cp->p_cred;
631 if ((u_int)SCARG(uap, signum) >= NSIG)
632 return (EINVAL);
633 if (SCARG(uap, pid) > 0) {
634 /* kill single process */
635 if ((p = pfind(SCARG(uap, pid))) == NULL)
636 return (ESRCH);
637 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
638 return (EPERM);
639 if (SCARG(uap, signum))
640 psignal(p, SCARG(uap, signum));
641 return (0);
642 }
643 switch (SCARG(uap, pid)) {
644 case -1: /* broadcast signal */
645 return (killpg1(cp, SCARG(uap, signum), 0, 1));
646 case 0: /* signal own process group */
647 return (killpg1(cp, SCARG(uap, signum), 0, 0));
648 default: /* negative explicit process group */
649 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
650 }
651 /* NOTREACHED */
652 }
653
654 /*
655 * Common code for kill process group/broadcast kill.
656 * cp is calling process.
657 */
658 int
659 killpg1(struct proc *cp, int signum, int pgid, int all)
660 {
661 struct proc *p;
662 struct pcred *pc;
663 struct pgrp *pgrp;
664 int nfound;
665
666 pc = cp->p_cred;
667 nfound = 0;
668 if (all) {
669 /*
670 * broadcast
671 */
672 proclist_lock_read();
673 LIST_FOREACH(p, &allproc, p_list) {
674 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
675 p == cp || !CANSIGNAL(cp, pc, p, signum))
676 continue;
677 nfound++;
678 if (signum)
679 psignal(p, signum);
680 }
681 proclist_unlock_read();
682 } else {
683 if (pgid == 0)
684 /*
685 * zero pgid means send to my process group.
686 */
687 pgrp = cp->p_pgrp;
688 else {
689 pgrp = pgfind(pgid);
690 if (pgrp == NULL)
691 return (ESRCH);
692 }
693 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
694 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
695 !CANSIGNAL(cp, pc, p, signum))
696 continue;
697 nfound++;
698 if (signum && P_ZOMBIE(p) == 0)
699 psignal(p, signum);
700 }
701 }
702 return (nfound ? 0 : ESRCH);
703 }
704
705 /*
706 * Send a signal to a process group.
707 */
708 void
709 gsignal(int pgid, int signum)
710 {
711 struct pgrp *pgrp;
712
713 if (pgid && (pgrp = pgfind(pgid)))
714 pgsignal(pgrp, signum, 0);
715 }
716
717 /*
718 * Send a signal to a process group. If checktty is 1,
719 * limit to members which have a controlling terminal.
720 */
721 void
722 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
723 {
724 struct proc *p;
725
726 if (pgrp)
727 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
728 if (checkctty == 0 || p->p_flag & P_CONTROLT)
729 psignal(p, signum);
730 }
731
732 /*
733 * Send a signal caused by a trap to the current process.
734 * If it will be caught immediately, deliver it with correct code.
735 * Otherwise, post it normally.
736 */
737 void
738 trapsignal(struct lwp *l, int signum, u_long code)
739 {
740 struct proc *p;
741 struct sigacts *ps;
742
743 p = l->l_proc;
744 ps = p->p_sigacts;
745 if ((p->p_flag & P_TRACED) == 0 &&
746 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
747 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
748 p->p_stats->p_ru.ru_nsignals++;
749 #ifdef KTRACE
750 if (KTRPOINT(p, KTR_PSIG))
751 ktrpsig(p, signum,
752 SIGACTION_PS(ps, signum).sa_handler,
753 &p->p_sigctx.ps_sigmask, code);
754 #endif
755 psendsig(l, signum, &p->p_sigctx.ps_sigmask, code);
756 (void) splsched(); /* XXXSMP */
757 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
758 &p->p_sigctx.ps_sigmask);
759 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
760 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
761 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
762 sigaddset(&p->p_sigctx.ps_sigignore, signum);
763 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
764 }
765 (void) spl0(); /* XXXSMP */
766 } else {
767 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */
768 p->p_sigctx.ps_sig = signum; /* XXX to verify code */
769 psignal(p, signum);
770 }
771 }
772
773 /*
774 * Send the signal to the process. If the signal has an action, the action
775 * is usually performed by the target process rather than the caller; we add
776 * the signal to the set of pending signals for the process.
777 *
778 * Exceptions:
779 * o When a stop signal is sent to a sleeping process that takes the
780 * default action, the process is stopped without awakening it.
781 * o SIGCONT restarts stopped processes (or puts them back to sleep)
782 * regardless of the signal action (eg, blocked or ignored).
783 *
784 * Other ignored signals are discarded immediately.
785 *
786 * XXXSMP: Invoked as psignal() or sched_psignal().
787 */
788 void
789 psignal1(struct proc *p, int signum,
790 int dolock) /* XXXSMP: works, but icky */
791 {
792 struct lwp *l, *suspended;
793 int s = 0, prop, allsusp;
794 sig_t action;
795
796 #ifdef DIAGNOSTIC
797 if (signum <= 0 || signum >= NSIG)
798 panic("psignal signal number");
799
800 /* XXXSMP: works, but icky */
801 if (dolock)
802 SCHED_ASSERT_UNLOCKED();
803 else
804 SCHED_ASSERT_LOCKED();
805 #endif
806 /*
807 * Notify any interested parties in the signal.
808 */
809 KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
810
811 prop = sigprop[signum];
812
813 /*
814 * If proc is traced, always give parent a chance.
815 */
816 if (p->p_flag & P_TRACED)
817 action = SIG_DFL;
818 else {
819 /*
820 * If the signal is being ignored,
821 * then we forget about it immediately.
822 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
823 * and if it is set to SIG_IGN,
824 * action will be SIG_DFL here.)
825 */
826 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
827 return;
828 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
829 action = SIG_HOLD;
830 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
831 action = SIG_CATCH;
832 else {
833 action = SIG_DFL;
834
835 if (prop & SA_KILL && p->p_nice > NZERO)
836 p->p_nice = NZERO;
837
838 /*
839 * If sending a tty stop signal to a member of an
840 * orphaned process group, discard the signal here if
841 * the action is default; don't stop the process below
842 * if sleeping, and don't clear any pending SIGCONT.
843 */
844 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
845 return;
846 }
847 }
848
849 if (prop & SA_CONT)
850 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
851
852 if (prop & SA_STOP)
853 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
854
855 sigaddset(&p->p_sigctx.ps_siglist, signum);
856
857 /* CHECKSIGS() is "inlined" here. */
858 p->p_sigctx.ps_sigcheck = 1;
859
860 /*
861 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
862 * please!), check if anything waits on it. If yes, clear the
863 * pending signal from siglist set, save it to ps_sigwaited,
864 * clear sigwait list, and wakeup any sigwaiters.
865 * The signal won't be processed further here.
866 */
867 if ((prop & SA_CANTMASK) == 0
868 && p->p_sigctx.ps_sigwaited < 0
869 && sigismember(&p->p_sigctx.ps_sigwait, signum)) {
870 sigdelset(&p->p_sigctx.ps_siglist, signum);
871 p->p_sigctx.ps_sigwaited = signum;
872 sigemptyset(&p->p_sigctx.ps_sigwait);
873
874 if (dolock)
875 wakeup_one(&p->p_sigctx.ps_sigwait);
876 else
877 sched_wakeup(&p->p_sigctx.ps_sigwait);
878 return;
879 }
880
881 /*
882 * Defer further processing for signals which are held,
883 * except that stopped processes must be continued by SIGCONT.
884 */
885 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
886 return;
887 /* XXXSMP: works, but icky */
888 if (dolock)
889 SCHED_LOCK(s);
890
891 if (p->p_nrlwps > 0) {
892 /*
893 * At least one LWP is running or on a run queue.
894 * The signal will be noticed when one of them returns
895 * to userspace.
896 */
897 signotify(p);
898 /*
899 * The signal will be noticed very soon.
900 */
901 goto out;
902 } else {
903 /* Process is sleeping or stopped */
904 if (p->p_flag & P_SA) {
905 l = p->p_sa->sa_idle;
906 } else {
907 /*
908 * Find out if any of the sleeps are interruptable,
909 * and if all the live LWPs remaining are suspended.
910 */
911 allsusp = 1;
912 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
913 if (l->l_stat == LSSLEEP &&
914 l->l_flag & L_SINTR)
915 break;
916 if (l->l_stat == LSSUSPENDED)
917 suspended = l;
918 else if ((l->l_stat != LSZOMB) &&
919 (l->l_stat != LSDEAD))
920 allsusp = 0;
921 }
922 }
923 if (p->p_stat == SACTIVE) {
924 /* All LWPs must be sleeping */
925 KDASSERT(((p->p_flag & P_SA) == 0) || (l != NULL));
926
927 if (l != NULL && (p->p_flag & P_TRACED))
928 goto run;
929
930 /*
931 * If SIGCONT is default (or ignored) and process is
932 * asleep, we are finished; the process should not
933 * be awakened.
934 */
935 if ((prop & SA_CONT) && action == SIG_DFL) {
936 sigdelset(&p->p_sigctx.ps_siglist, signum);
937 goto out;
938 }
939
940 /*
941 * When a sleeping process receives a stop
942 * signal, process immediately if possible.
943 */
944 if ((prop & SA_STOP) && action == SIG_DFL) {
945 /*
946 * If a child holding parent blocked,
947 * stopping could cause deadlock.
948 */
949 if (p->p_flag & P_PPWAIT)
950 goto out;
951 sigdelset(&p->p_sigctx.ps_siglist, signum);
952 p->p_xstat = signum;
953 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
954 /*
955 * XXXSMP: recursive call; don't lock
956 * the second time around.
957 */
958 sched_psignal(p->p_pptr, SIGCHLD);
959 }
960 proc_stop(p); /* XXXSMP: recurse? */
961 goto out;
962 }
963
964 if (l == NULL) {
965 /*
966 * Special case: SIGKILL of a process
967 * which is entirely composed of
968 * suspended LWPs should succeed. We
969 * make this happen by unsuspending one of
970 * them.
971 */
972 if (allsusp && (signum == SIGKILL))
973 lwp_continue(suspended);
974 goto out;
975 }
976 /*
977 * All other (caught or default) signals
978 * cause the process to run.
979 */
980 goto runfast;
981 /*NOTREACHED*/
982 } else if (p->p_stat == SSTOP) {
983 /* Process is stopped */
984 /*
985 * If traced process is already stopped,
986 * then no further action is necessary.
987 */
988 if (p->p_flag & P_TRACED)
989 goto out;
990
991 /*
992 * Kill signal always sets processes running,
993 * if possible.
994 */
995 if (signum == SIGKILL) {
996 l = proc_unstop(p);
997 if (l)
998 goto runfast;
999 goto out;
1000 }
1001
1002 if (prop & SA_CONT) {
1003 /*
1004 * If SIGCONT is default (or ignored),
1005 * we continue the process but don't
1006 * leave the signal in ps_siglist, as
1007 * it has no further action. If
1008 * SIGCONT is held, we continue the
1009 * process and leave the signal in
1010 * ps_siglist. If the process catches
1011 * SIGCONT, let it handle the signal
1012 * itself. If it isn't waiting on an
1013 * event, then it goes back to run
1014 * state. Otherwise, process goes
1015 * back to sleep state.
1016 */
1017 if (action == SIG_DFL)
1018 sigdelset(&p->p_sigctx.ps_siglist,
1019 signum);
1020 l = proc_unstop(p);
1021 if (l && (action == SIG_CATCH))
1022 goto runfast;
1023 goto out;
1024 }
1025
1026 if (prop & SA_STOP) {
1027 /*
1028 * Already stopped, don't need to stop again.
1029 * (If we did the shell could get confused.)
1030 */
1031 sigdelset(&p->p_sigctx.ps_siglist, signum);
1032 goto out;
1033 }
1034
1035 /*
1036 * If a lwp is sleeping interruptibly, then
1037 * wake it up; it will run until the kernel
1038 * boundary, where it will stop in issignal(),
1039 * since p->p_stat is still SSTOP. When the
1040 * process is continued, it will be made
1041 * runnable and can look at the signal.
1042 */
1043 if (l)
1044 goto run;
1045 goto out;
1046 } else {
1047 /* Else what? */
1048 panic("psignal: Invalid process state %d.",
1049 p->p_stat);
1050 }
1051 }
1052 /*NOTREACHED*/
1053
1054 runfast:
1055 /*
1056 * Raise priority to at least PUSER.
1057 */
1058 if (l->l_priority > PUSER)
1059 l->l_priority = PUSER;
1060 run:
1061 setrunnable(l); /* XXXSMP: recurse? */
1062 out:
1063 /* XXXSMP: works, but icky */
1064 if (dolock)
1065 SCHED_UNLOCK(s);
1066 }
1067
1068 void
1069 psendsig(struct lwp *l, int sig, sigset_t *mask, u_long code)
1070 {
1071 struct proc *p = l->l_proc;
1072 struct lwp *le, *li;
1073 siginfo_t *si;
1074
1075 if (p->p_flag & P_SA) {
1076 si = pool_get(&siginfo_pool, PR_WAITOK);
1077 si->si_signo = sig;
1078 si->si_errno = 0;
1079 si->si_code = code;
1080 le = li = NULL;
1081 if (code)
1082 le = l;
1083 else
1084 li = l;
1085
1086 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1087 sizeof(siginfo_t), si);
1088 return;
1089 }
1090
1091 (*p->p_emul->e_sendsig)(sig, mask, code);
1092 }
1093
1094 static __inline int firstsig(const sigset_t *);
1095
1096 static __inline int
1097 firstsig(const sigset_t *ss)
1098 {
1099 int sig;
1100
1101 sig = ffs(ss->__bits[0]);
1102 if (sig != 0)
1103 return (sig);
1104 #if NSIG > 33
1105 sig = ffs(ss->__bits[1]);
1106 if (sig != 0)
1107 return (sig + 32);
1108 #endif
1109 #if NSIG > 65
1110 sig = ffs(ss->__bits[2]);
1111 if (sig != 0)
1112 return (sig + 64);
1113 #endif
1114 #if NSIG > 97
1115 sig = ffs(ss->__bits[3]);
1116 if (sig != 0)
1117 return (sig + 96);
1118 #endif
1119 return (0);
1120 }
1121
1122 /*
1123 * If the current process has received a signal (should be caught or cause
1124 * termination, should interrupt current syscall), return the signal number.
1125 * Stop signals with default action are processed immediately, then cleared;
1126 * they aren't returned. This is checked after each entry to the system for
1127 * a syscall or trap (though this can usually be done without calling issignal
1128 * by checking the pending signal masks in the CURSIG macro.) The normal call
1129 * sequence is
1130 *
1131 * while (signum = CURSIG(curlwp))
1132 * postsig(signum);
1133 */
1134 int
1135 issignal(struct lwp *l)
1136 {
1137 struct proc *p = l->l_proc;
1138 int s = 0, signum, prop;
1139 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1140 sigset_t ss;
1141
1142 if (p->p_stat == SSTOP) {
1143 /*
1144 * The process is stopped/stopping. Stop ourselves now that
1145 * we're on the kernel/userspace boundary.
1146 */
1147 if (dolock)
1148 SCHED_LOCK(s);
1149 l->l_stat = LSSTOP;
1150 p->p_nrlwps--;
1151 if (p->p_flag & P_TRACED)
1152 goto sigtraceswitch;
1153 else
1154 goto sigswitch;
1155 }
1156 for (;;) {
1157 sigpending1(p, &ss);
1158 if (p->p_flag & P_PPWAIT)
1159 sigminusset(&stopsigmask, &ss);
1160 signum = firstsig(&ss);
1161 if (signum == 0) { /* no signal to send */
1162 p->p_sigctx.ps_sigcheck = 0;
1163 if (locked && dolock)
1164 SCHED_LOCK(s);
1165 return (0);
1166 }
1167 /* take the signal! */
1168 sigdelset(&p->p_sigctx.ps_siglist, signum);
1169
1170 /*
1171 * We should see pending but ignored signals
1172 * only if P_TRACED was on when they were posted.
1173 */
1174 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1175 (p->p_flag & P_TRACED) == 0)
1176 continue;
1177
1178 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1179 /*
1180 * If traced, always stop, and stay
1181 * stopped until released by the debugger.
1182 */
1183 p->p_xstat = signum;
1184 if ((p->p_flag & P_FSTRACE) == 0)
1185 psignal1(p->p_pptr, SIGCHLD, dolock);
1186 if (dolock)
1187 SCHED_LOCK(s);
1188 proc_stop(p);
1189 sigtraceswitch:
1190 mi_switch(l, NULL);
1191 SCHED_ASSERT_UNLOCKED();
1192 if (dolock)
1193 splx(s);
1194 else
1195 dolock = 1;
1196
1197 /*
1198 * If we are no longer being traced, or the parent
1199 * didn't give us a signal, look for more signals.
1200 */
1201 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1202 continue;
1203
1204 /*
1205 * If the new signal is being masked, look for other
1206 * signals.
1207 */
1208 signum = p->p_xstat;
1209 p->p_xstat = 0;
1210 /*
1211 * `p->p_sigctx.ps_siglist |= mask' is done
1212 * in setrunnable().
1213 */
1214 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1215 continue;
1216 /* take the signal! */
1217 sigdelset(&p->p_sigctx.ps_siglist, signum);
1218 }
1219
1220 prop = sigprop[signum];
1221
1222 /*
1223 * Decide whether the signal should be returned.
1224 * Return the signal's number, or fall through
1225 * to clear it from the pending mask.
1226 */
1227 switch ((long)SIGACTION(p, signum).sa_handler) {
1228
1229 case (long)SIG_DFL:
1230 /*
1231 * Don't take default actions on system processes.
1232 */
1233 if (p->p_pid <= 1) {
1234 #ifdef DIAGNOSTIC
1235 /*
1236 * Are you sure you want to ignore SIGSEGV
1237 * in init? XXX
1238 */
1239 printf("Process (pid %d) got signal %d\n",
1240 p->p_pid, signum);
1241 #endif
1242 break; /* == ignore */
1243 }
1244 /*
1245 * If there is a pending stop signal to process
1246 * with default action, stop here,
1247 * then clear the signal. However,
1248 * if process is member of an orphaned
1249 * process group, ignore tty stop signals.
1250 */
1251 if (prop & SA_STOP) {
1252 if (p->p_flag & P_TRACED ||
1253 (p->p_pgrp->pg_jobc == 0 &&
1254 prop & SA_TTYSTOP))
1255 break; /* == ignore */
1256 p->p_xstat = signum;
1257 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1258 psignal1(p->p_pptr, SIGCHLD, dolock);
1259 if (dolock)
1260 SCHED_LOCK(s);
1261 proc_stop(p);
1262 sigswitch:
1263 mi_switch(l, NULL);
1264 SCHED_ASSERT_UNLOCKED();
1265 if (dolock)
1266 splx(s);
1267 else
1268 dolock = 1;
1269 break;
1270 } else if (prop & SA_IGNORE) {
1271 /*
1272 * Except for SIGCONT, shouldn't get here.
1273 * Default action is to ignore; drop it.
1274 */
1275 break; /* == ignore */
1276 } else
1277 goto keep;
1278 /*NOTREACHED*/
1279
1280 case (long)SIG_IGN:
1281 /*
1282 * Masking above should prevent us ever trying
1283 * to take action on an ignored signal other
1284 * than SIGCONT, unless process is traced.
1285 */
1286 #ifdef DEBUG_ISSIGNAL
1287 if ((prop & SA_CONT) == 0 &&
1288 (p->p_flag & P_TRACED) == 0)
1289 printf("issignal\n");
1290 #endif
1291 break; /* == ignore */
1292
1293 default:
1294 /*
1295 * This signal has an action, let
1296 * postsig() process it.
1297 */
1298 goto keep;
1299 }
1300 }
1301 /* NOTREACHED */
1302
1303 keep:
1304 /* leave the signal for later */
1305 sigaddset(&p->p_sigctx.ps_siglist, signum);
1306 CHECKSIGS(p);
1307 if (locked && dolock)
1308 SCHED_LOCK(s);
1309 return (signum);
1310 }
1311
1312 /*
1313 * Put the argument process into the stopped state and notify the parent
1314 * via wakeup. Signals are handled elsewhere. The process must not be
1315 * on the run queue.
1316 */
1317 static void
1318 proc_stop(struct proc *p)
1319 {
1320 struct lwp *l;
1321
1322 SCHED_ASSERT_LOCKED();
1323
1324 /* XXX lock process LWP state */
1325 p->p_stat = SSTOP;
1326 p->p_flag &= ~P_WAITED;
1327
1328 /*
1329 * Put as many LWP's as possible in stopped state.
1330 * Sleeping ones will notice the stopped state as they try to
1331 * return to userspace.
1332 */
1333
1334 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1335 if (l->l_stat == LSONPROC) {
1336 /* XXX SMP this assumes that a LWP that is LSONPROC
1337 * is curlwp and hence is about to be mi_switched
1338 * away; the only callers of proc_stop() are:
1339 * - psignal
1340 * - issignal()
1341 * For the former, proc_stop() is only called when
1342 * no processes are running, so we don't worry.
1343 * For the latter, proc_stop() is called right
1344 * before mi_switch().
1345 */
1346 l->l_stat = LSSTOP;
1347 p->p_nrlwps--;
1348 } else if (l->l_stat == LSRUN) {
1349 /* Remove LWP from the run queue */
1350 remrunqueue(l);
1351 l->l_stat = LSSTOP;
1352 p->p_nrlwps--;
1353 } else if ((l->l_stat == LSSLEEP) ||
1354 (l->l_stat == LSSUSPENDED) ||
1355 (l->l_stat == LSZOMB) ||
1356 (l->l_stat == LSDEAD)) {
1357 /*
1358 * Don't do anything; let sleeping LWPs
1359 * discover the stopped state of the process
1360 * on their way out of the kernel; otherwise,
1361 * things like NFS threads that sleep with
1362 * locks will block the rest of the system
1363 * from getting any work done.
1364 *
1365 * Suspended/dead/zombie LWPs aren't going
1366 * anywhere, so we don't need to touch them.
1367 */
1368 }
1369 #ifdef DIAGNOSTIC
1370 else {
1371 panic("proc_stop: process %d lwp %d "
1372 "in unstoppable state %d.\n",
1373 p->p_pid, l->l_lid, l->l_stat);
1374 }
1375 #endif
1376 }
1377 /* XXX unlock process LWP state */
1378
1379 sched_wakeup((caddr_t)p->p_pptr);
1380 }
1381
1382 /*
1383 * Given a process in state SSTOP, set the state back to SACTIVE and
1384 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1385 *
1386 * If no LWPs ended up runnable (and therefore able to take a signal),
1387 * return a LWP that is sleeping interruptably. The caller can wake
1388 * that LWP up to take a signal.
1389 */
1390 struct lwp *
1391 proc_unstop(p)
1392 struct proc *p;
1393 {
1394 struct lwp *l, *lr = NULL;
1395 int cantake = 0;
1396
1397 SCHED_ASSERT_LOCKED();
1398
1399 /*
1400 * Our caller will want to invoke setrunnable() on whatever we return,
1401 * provided that it isn't NULL.
1402 */
1403
1404 p->p_stat = SACTIVE;
1405 if (p->p_flag & P_SA) {
1406 lr = p->p_sa->sa_idle; /* OK if this is NULL. */
1407 cantake = 1;
1408 }
1409 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1410 if (l->l_stat == LSRUN)
1411 cantake = 1;
1412 if (l->l_stat != LSSTOP)
1413 continue;
1414
1415 if (l->l_wchan != NULL) {
1416 l->l_stat = LSSLEEP;
1417 if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1418 lr = l;
1419 cantake = 1;
1420 }
1421 } else {
1422 if (l != lr)
1423 setrunnable(l);
1424 cantake = 1;
1425 }
1426 }
1427
1428 return lr;
1429 }
1430
1431 /*
1432 * Take the action for the specified signal
1433 * from the current set of pending signals.
1434 */
1435 void
1436 postsig(int signum)
1437 {
1438 struct lwp *l;
1439 struct proc *p;
1440 struct sigacts *ps;
1441 sig_t action;
1442 u_long code;
1443 sigset_t *returnmask;
1444
1445 l = curlwp;
1446 p = l->l_proc;
1447 ps = p->p_sigacts;
1448 #ifdef DIAGNOSTIC
1449 if (signum == 0)
1450 panic("postsig");
1451 #endif
1452
1453 KERNEL_PROC_LOCK(l);
1454
1455 sigdelset(&p->p_sigctx.ps_siglist, signum);
1456 action = SIGACTION_PS(ps, signum).sa_handler;
1457 #ifdef KTRACE
1458 if (KTRPOINT(p, KTR_PSIG))
1459 ktrpsig(p,
1460 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1461 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1462 #endif
1463 if (action == SIG_DFL) {
1464 /*
1465 * Default action, where the default is to kill
1466 * the process. (Other cases were ignored above.)
1467 */
1468 sigexit(l, signum);
1469 /* NOTREACHED */
1470 } else {
1471 /*
1472 * If we get here, the signal must be caught.
1473 */
1474 #ifdef DIAGNOSTIC
1475 if (action == SIG_IGN ||
1476 sigismember(&p->p_sigctx.ps_sigmask, signum))
1477 panic("postsig action");
1478 #endif
1479 /*
1480 * Set the new mask value and also defer further
1481 * occurences of this signal.
1482 *
1483 * Special case: user has done a sigpause. Here the
1484 * current mask is not of interest, but rather the
1485 * mask from before the sigpause is what we want
1486 * restored after the signal processing is completed.
1487 */
1488 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1489 returnmask = &p->p_sigctx.ps_oldmask;
1490 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1491 } else
1492 returnmask = &p->p_sigctx.ps_sigmask;
1493 p->p_stats->p_ru.ru_nsignals++;
1494 if (p->p_sigctx.ps_sig != signum) {
1495 code = 0;
1496 } else {
1497 code = p->p_sigctx.ps_code;
1498 p->p_sigctx.ps_code = 0;
1499 p->p_sigctx.ps_sig = 0;
1500 }
1501 psendsig(l, signum, returnmask, code);
1502 (void) splsched(); /* XXXSMP */
1503 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1504 &p->p_sigctx.ps_sigmask);
1505 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1506 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1507 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1508 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1509 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1510 }
1511 (void) spl0(); /* XXXSMP */
1512 }
1513
1514 KERNEL_PROC_UNLOCK(l);
1515 }
1516
1517 /*
1518 * Kill the current process for stated reason.
1519 */
1520 void
1521 killproc(struct proc *p, const char *why)
1522 {
1523
1524 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1525 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1526 psignal(p, SIGKILL);
1527 }
1528
1529 /*
1530 * Force the current process to exit with the specified signal, dumping core
1531 * if appropriate. We bypass the normal tests for masked and caught signals,
1532 * allowing unrecoverable failures to terminate the process without changing
1533 * signal state. Mark the accounting record with the signal termination.
1534 * If dumping core, save the signal number for the debugger. Calls exit and
1535 * does not return.
1536 */
1537
1538 #if defined(DEBUG)
1539 int kern_logsigexit = 1; /* not static to make public for sysctl */
1540 #else
1541 int kern_logsigexit = 0; /* not static to make public for sysctl */
1542 #endif
1543
1544 static const char logcoredump[] =
1545 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1546 static const char lognocoredump[] =
1547 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1548
1549 /* Wrapper function for use in p_userret */
1550 static void
1551 lwp_coredump_hook(struct lwp *l, void *arg)
1552 {
1553 int s;
1554
1555 /*
1556 * Suspend ourselves, so that the kernel stack and therefore
1557 * the userland registers saved in the trapframe are around
1558 * for coredump() to write them out.
1559 */
1560 KERNEL_PROC_LOCK(l);
1561 l->l_flag &= ~L_DETACHED;
1562 SCHED_LOCK(s);
1563 l->l_stat = LSSUSPENDED;
1564 l->l_proc->p_nrlwps--;
1565 /* XXX NJWLWP check if this makes sense here: */
1566 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1567 mi_switch(l, NULL);
1568 SCHED_ASSERT_UNLOCKED();
1569 splx(s);
1570
1571 lwp_exit(l);
1572 }
1573
1574 void
1575 sigexit(struct lwp *l, int signum)
1576 {
1577 struct proc *p;
1578 struct lwp *l2;
1579 int error, exitsig;
1580
1581 p = l->l_proc;
1582
1583 /*
1584 * Don't permit coredump() or exit1() multiple times
1585 * in the same process.
1586 */
1587 if (p->p_flag & P_WEXIT) {
1588 KERNEL_PROC_UNLOCK(l);
1589 (*p->p_userret)(l, p->p_userret_arg);
1590 }
1591 p->p_flag |= P_WEXIT;
1592 /* We don't want to switch away from exiting. */
1593 /* XXX multiprocessor: stop LWPs on other processors. */
1594 if (p->p_flag & P_SA) {
1595 LIST_FOREACH(l2, &p->p_lwps, l_sibling)
1596 l2->l_flag &= ~L_SA;
1597 p->p_flag &= ~P_SA;
1598 }
1599
1600 /* Make other LWPs stick around long enough to be dumped */
1601 p->p_userret = lwp_coredump_hook;
1602 p->p_userret_arg = NULL;
1603
1604 exitsig = signum;
1605 p->p_acflag |= AXSIG;
1606 if (sigprop[signum] & SA_CORE) {
1607 p->p_sigctx.ps_sig = signum;
1608 if ((error = coredump(l)) == 0)
1609 exitsig |= WCOREFLAG;
1610
1611 if (kern_logsigexit) {
1612 /* XXX What if we ever have really large UIDs? */
1613 int uid = p->p_cred && p->p_ucred ?
1614 (int) p->p_ucred->cr_uid : -1;
1615
1616 if (error)
1617 log(LOG_INFO, lognocoredump, p->p_pid,
1618 p->p_comm, uid, signum, error);
1619 else
1620 log(LOG_INFO, logcoredump, p->p_pid,
1621 p->p_comm, uid, signum);
1622 }
1623
1624 }
1625
1626 exit1(l, W_EXITCODE(0, exitsig));
1627 /* NOTREACHED */
1628 }
1629
1630 /*
1631 * Dump core, into a file named "progname.core" or "core" (depending on the
1632 * value of shortcorename), unless the process was setuid/setgid.
1633 */
1634 int
1635 coredump(struct lwp *l)
1636 {
1637 struct vnode *vp;
1638 struct proc *p;
1639 struct vmspace *vm;
1640 struct ucred *cred;
1641 struct nameidata nd;
1642 struct vattr vattr;
1643 int error, error1;
1644 char name[MAXPATHLEN];
1645
1646 p = l->l_proc;
1647 vm = p->p_vmspace;
1648 cred = p->p_cred->pc_ucred;
1649
1650 /*
1651 * Make sure the process has not set-id, to prevent data leaks.
1652 */
1653 if (p->p_flag & P_SUGID)
1654 return (EPERM);
1655
1656 /*
1657 * Refuse to core if the data + stack + user size is larger than
1658 * the core dump limit. XXX THIS IS WRONG, because of mapped
1659 * data.
1660 */
1661 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1662 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1663 return (EFBIG); /* better error code? */
1664
1665 /*
1666 * The core dump will go in the current working directory. Make
1667 * sure that the directory is still there and that the mount flags
1668 * allow us to write core dumps there.
1669 */
1670 vp = p->p_cwdi->cwdi_cdir;
1671 if (vp->v_mount == NULL ||
1672 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1673 return (EPERM);
1674
1675 error = build_corename(p, name);
1676 if (error)
1677 return error;
1678
1679 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1680 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1681 if (error)
1682 return (error);
1683 vp = nd.ni_vp;
1684
1685 /* Don't dump to non-regular files or files with links. */
1686 if (vp->v_type != VREG ||
1687 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1688 error = EINVAL;
1689 goto out;
1690 }
1691 VATTR_NULL(&vattr);
1692 vattr.va_size = 0;
1693 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1694 VOP_SETATTR(vp, &vattr, cred, p);
1695 p->p_acflag |= ACORE;
1696
1697 /* Now dump the actual core file. */
1698 error = (*p->p_execsw->es_coredump)(l, vp, cred);
1699 out:
1700 VOP_UNLOCK(vp, 0);
1701 error1 = vn_close(vp, FWRITE, cred, p);
1702 if (error == 0)
1703 error = error1;
1704 return (error);
1705 }
1706
1707 /*
1708 * Nonexistent system call-- signal process (may want to handle it).
1709 * Flag error in case process won't see signal immediately (blocked or ignored).
1710 */
1711 /* ARGSUSED */
1712 int
1713 sys_nosys(struct lwp *l, void *v, register_t *retval)
1714 {
1715 struct proc *p;
1716
1717 p = l->l_proc;
1718 psignal(p, SIGSYS);
1719 return (ENOSYS);
1720 }
1721
1722 static int
1723 build_corename(struct proc *p, char dst[MAXPATHLEN])
1724 {
1725 const char *s;
1726 char *d, *end;
1727 int i;
1728
1729 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1730 *s != '\0'; s++) {
1731 if (*s == '%') {
1732 switch (*(s + 1)) {
1733 case 'n':
1734 i = snprintf(d, end - d, "%s", p->p_comm);
1735 break;
1736 case 'p':
1737 i = snprintf(d, end - d, "%d", p->p_pid);
1738 break;
1739 case 'u':
1740 i = snprintf(d, end - d, "%.*s",
1741 (int)sizeof p->p_pgrp->pg_session->s_login,
1742 p->p_pgrp->pg_session->s_login);
1743 break;
1744 case 't':
1745 i = snprintf(d, end - d, "%ld",
1746 p->p_stats->p_start.tv_sec);
1747 break;
1748 default:
1749 goto copy;
1750 }
1751 d += i;
1752 s++;
1753 } else {
1754 copy: *d = *s;
1755 d++;
1756 }
1757 if (d >= end)
1758 return (ENAMETOOLONG);
1759 }
1760 *d = '\0';
1761 return 0;
1762 }
1763
1764 void
1765 getucontext(struct lwp *l, ucontext_t *ucp)
1766 {
1767 struct proc *p;
1768
1769 p = l->l_proc;
1770
1771 ucp->uc_flags = 0;
1772 ucp->uc_link = l->l_ctxlink;
1773
1774 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
1775 ucp->uc_flags |= _UC_SIGMASK;
1776
1777 /*
1778 * The (unsupplied) definition of the `current execution stack'
1779 * in the System V Interface Definition appears to allow returning
1780 * the main context stack.
1781 */
1782 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
1783 ucp->uc_stack.ss_sp = (void *)USRSTACK;
1784 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
1785 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
1786 } else {
1787 /* Simply copy alternate signal execution stack. */
1788 ucp->uc_stack = p->p_sigctx.ps_sigstk;
1789 }
1790 ucp->uc_flags |= _UC_STACK;
1791
1792 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1793 }
1794
1795 /* ARGSUSED */
1796 int
1797 sys_getcontext(struct lwp *l, void *v, register_t *retval)
1798 {
1799 struct sys_getcontext_args /* {
1800 syscallarg(struct __ucontext *) ucp;
1801 } */ *uap = v;
1802 ucontext_t uc;
1803
1804 getucontext(l, &uc);
1805
1806 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
1807 }
1808
1809 int
1810 setucontext(struct lwp *l, const ucontext_t *ucp)
1811 {
1812 struct proc *p;
1813 int error;
1814
1815 p = l->l_proc;
1816 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
1817 return (error);
1818 l->l_ctxlink = ucp->uc_link;
1819 /*
1820 * We might want to take care of the stack portion here but currently
1821 * don't; see the comment in getucontext().
1822 */
1823 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
1824 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
1825
1826 return 0;
1827 }
1828
1829 /* ARGSUSED */
1830 int
1831 sys_setcontext(struct lwp *l, void *v, register_t *retval)
1832 {
1833 struct sys_setcontext_args /* {
1834 syscallarg(const ucontext_t *) ucp;
1835 } */ *uap = v;
1836 ucontext_t uc;
1837 int error;
1838
1839 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
1840 exit1(l, W_EXITCODE(0, 0));
1841 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
1842 (error = setucontext(l, &uc)) != 0)
1843 return (error);
1844
1845 return (EJUSTRETURN);
1846 }
1847
1848 /*
1849 * sigtimedwait(2) system call, used also for implementation
1850 * of sigwaitinfo() and sigwait().
1851 *
1852 * This only handles single LWP in signal wait. libpthread provides
1853 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
1854 *
1855 * XXX no support for queued signals, si_code is always SI_USER.
1856 */
1857 int
1858 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
1859 {
1860 struct sys___sigtimedwait_args /* {
1861 syscallarg(const sigset_t *) set;
1862 syscallarg(siginfo_t *) info;
1863 syscallarg(struct timespec *) timeout;
1864 } */ *uap = v;
1865 sigset_t waitset, twaitset;
1866 struct proc *p = l->l_proc;
1867 int error, signum, s;
1868 int timo = 0;
1869 struct timeval tvstart;
1870 struct timespec ts;
1871
1872 if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset))))
1873 return (error);
1874
1875 /*
1876 * Silently ignore SA_CANTMASK signals. psignal1() would
1877 * ignore SA_CANTMASK signals in waitset, we do this
1878 * only for the below siglist check.
1879 */
1880 sigminusset(&sigcantmask, &waitset);
1881
1882 /*
1883 * First scan siglist and check if there is signal from
1884 * our waitset already pending.
1885 */
1886 twaitset = waitset;
1887 __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
1888 if ((signum = firstsig(&twaitset))) {
1889 /* found pending signal */
1890 sigdelset(&p->p_sigctx.ps_siglist, signum);
1891 goto sig;
1892 }
1893
1894 /*
1895 * Calculate timeout, if it was specified.
1896 */
1897 if (SCARG(uap, timeout)) {
1898 uint64_t ms;
1899
1900 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
1901 return (error);
1902
1903 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
1904 timo = mstohz(ms);
1905 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
1906 timo = 1;
1907 if (timo <= 0)
1908 return (EAGAIN);
1909
1910 /*
1911 * Remember current mono_time, it would be used in
1912 * ECANCELED/ERESTART case.
1913 */
1914 s = splclock();
1915 tvstart = mono_time;
1916 splx(s);
1917 }
1918
1919 /*
1920 * Setup ps_sigwait list.
1921 */
1922 p->p_sigctx.ps_sigwaited = -1;
1923 p->p_sigctx.ps_sigwait = waitset;
1924
1925 /*
1926 * Wait for signal to arrive. We can either be woken up or
1927 * time out.
1928 */
1929 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
1930
1931 /*
1932 * Check if a signal from our wait set has arrived, or if it
1933 * was mere wakeup.
1934 */
1935 if (!error) {
1936 if ((signum = p->p_sigctx.ps_sigwaited) <= 0) {
1937 /* wakeup via _lwp_wakeup() */
1938 error = ECANCELED;
1939 }
1940 }
1941
1942 /*
1943 * On error, clear sigwait indication. psignal1() sets it
1944 * in !error case.
1945 */
1946 if (error) {
1947 p->p_sigctx.ps_sigwaited = 0;
1948
1949 /*
1950 * If the sleep was interrupted (either by signal or wakeup),
1951 * update the timeout and copyout new value back.
1952 * It would be used when the syscall would be restarted
1953 * or called again.
1954 */
1955 if (timo && (error == ERESTART || error == ECANCELED)) {
1956 struct timeval tvnow, tvtimo;
1957 int err;
1958
1959 s = splclock();
1960 tvnow = mono_time;
1961 splx(s);
1962
1963 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
1964
1965 /* compute how much time has passed since start */
1966 timersub(&tvnow, &tvstart, &tvnow);
1967 /* substract passed time from timeout */
1968 timersub(&tvtimo, &tvnow, &tvtimo);
1969
1970 if (tvtimo.tv_sec < 0)
1971 return (EAGAIN);
1972
1973 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
1974
1975 /* copy updated timeout to userland */
1976 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts))))
1977 return (err);
1978 }
1979
1980 return (error);
1981 }
1982
1983 /*
1984 * If a signal from the wait set arrived, copy it to userland.
1985 * XXX no queued signals for now
1986 */
1987 if (signum > 0) {
1988 siginfo_t si;
1989
1990 sig:
1991 memset(&si, 0, sizeof(si));
1992 si.si_signo = signum;
1993 si.si_code = SI_USER;
1994
1995 error = copyout(&si, SCARG(uap, info), sizeof(si));
1996 if (error)
1997 return (error);
1998 }
1999
2000 return (0);
2001 }
2002
2003 /*
2004 * Returns true if signal is ignored or masked for passed process.
2005 */
2006 int
2007 sigismasked(struct proc *p, int sig)
2008 {
2009
2010 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2011 sigismember(&p->p_sigctx.ps_sigmask, sig));
2012 }
2013
2014 static int
2015 filt_sigattach(struct knote *kn)
2016 {
2017 struct proc *p = curproc;
2018
2019 kn->kn_ptr.p_proc = p;
2020 kn->kn_flags |= EV_CLEAR; /* automatically set */
2021
2022 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2023
2024 return (0);
2025 }
2026
2027 static void
2028 filt_sigdetach(struct knote *kn)
2029 {
2030 struct proc *p = kn->kn_ptr.p_proc;
2031
2032 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2033 }
2034
2035 /*
2036 * signal knotes are shared with proc knotes, so we apply a mask to
2037 * the hint in order to differentiate them from process hints. This
2038 * could be avoided by using a signal-specific knote list, but probably
2039 * isn't worth the trouble.
2040 */
2041 static int
2042 filt_signal(struct knote *kn, long hint)
2043 {
2044
2045 if (hint & NOTE_SIGNAL) {
2046 hint &= ~NOTE_SIGNAL;
2047
2048 if (kn->kn_id == hint)
2049 kn->kn_data++;
2050 }
2051 return (kn->kn_data != 0);
2052 }
2053
2054 const struct filterops sig_filtops = {
2055 0, filt_sigattach, filt_sigdetach, filt_signal
2056 };
2057