kern_sig.c revision 1.135 1 /* $NetBSD: kern_sig.c,v 1.135 2003/02/15 20:54:39 jdolecek 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.135 2003/02/15 20:54:39 jdolecek 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 setrunnable(l);
1423 cantake = 1;
1424 }
1425 }
1426
1427 return lr;
1428 }
1429
1430 /*
1431 * Take the action for the specified signal
1432 * from the current set of pending signals.
1433 */
1434 void
1435 postsig(int signum)
1436 {
1437 struct lwp *l;
1438 struct proc *p;
1439 struct sigacts *ps;
1440 sig_t action;
1441 u_long code;
1442 sigset_t *returnmask;
1443
1444 l = curlwp;
1445 p = l->l_proc;
1446 ps = p->p_sigacts;
1447 #ifdef DIAGNOSTIC
1448 if (signum == 0)
1449 panic("postsig");
1450 #endif
1451
1452 KERNEL_PROC_LOCK(l);
1453
1454 sigdelset(&p->p_sigctx.ps_siglist, signum);
1455 action = SIGACTION_PS(ps, signum).sa_handler;
1456 #ifdef KTRACE
1457 if (KTRPOINT(p, KTR_PSIG))
1458 ktrpsig(p,
1459 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1460 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1461 #endif
1462 if (action == SIG_DFL) {
1463 /*
1464 * Default action, where the default is to kill
1465 * the process. (Other cases were ignored above.)
1466 */
1467 sigexit(l, signum);
1468 /* NOTREACHED */
1469 } else {
1470 /*
1471 * If we get here, the signal must be caught.
1472 */
1473 #ifdef DIAGNOSTIC
1474 if (action == SIG_IGN ||
1475 sigismember(&p->p_sigctx.ps_sigmask, signum))
1476 panic("postsig action");
1477 #endif
1478 /*
1479 * Set the new mask value and also defer further
1480 * occurences of this signal.
1481 *
1482 * Special case: user has done a sigpause. Here the
1483 * current mask is not of interest, but rather the
1484 * mask from before the sigpause is what we want
1485 * restored after the signal processing is completed.
1486 */
1487 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1488 returnmask = &p->p_sigctx.ps_oldmask;
1489 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1490 } else
1491 returnmask = &p->p_sigctx.ps_sigmask;
1492 p->p_stats->p_ru.ru_nsignals++;
1493 if (p->p_sigctx.ps_sig != signum) {
1494 code = 0;
1495 } else {
1496 code = p->p_sigctx.ps_code;
1497 p->p_sigctx.ps_code = 0;
1498 p->p_sigctx.ps_sig = 0;
1499 }
1500 psendsig(l, signum, returnmask, code);
1501 (void) splsched(); /* XXXSMP */
1502 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1503 &p->p_sigctx.ps_sigmask);
1504 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1505 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1506 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1507 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1508 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1509 }
1510 (void) spl0(); /* XXXSMP */
1511 }
1512
1513 KERNEL_PROC_UNLOCK(l);
1514 }
1515
1516 /*
1517 * Kill the current process for stated reason.
1518 */
1519 void
1520 killproc(struct proc *p, const char *why)
1521 {
1522
1523 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1524 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1525 psignal(p, SIGKILL);
1526 }
1527
1528 /*
1529 * Force the current process to exit with the specified signal, dumping core
1530 * if appropriate. We bypass the normal tests for masked and caught signals,
1531 * allowing unrecoverable failures to terminate the process without changing
1532 * signal state. Mark the accounting record with the signal termination.
1533 * If dumping core, save the signal number for the debugger. Calls exit and
1534 * does not return.
1535 */
1536
1537 #if defined(DEBUG)
1538 int kern_logsigexit = 1; /* not static to make public for sysctl */
1539 #else
1540 int kern_logsigexit = 0; /* not static to make public for sysctl */
1541 #endif
1542
1543 static const char logcoredump[] =
1544 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1545 static const char lognocoredump[] =
1546 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1547
1548 /* Wrapper function for use in p_userret */
1549 static void
1550 lwp_coredump_hook(struct lwp *l, void *arg)
1551 {
1552 int s;
1553
1554 /*
1555 * Suspend ourselves, so that the kernel stack and therefore
1556 * the userland registers saved in the trapframe are around
1557 * for coredump() to write them out.
1558 */
1559 KERNEL_PROC_LOCK(l);
1560 l->l_flag &= ~L_DETACHED;
1561 SCHED_LOCK(s);
1562 l->l_stat = LSSUSPENDED;
1563 l->l_proc->p_nrlwps--;
1564 /* XXX NJWLWP check if this makes sense here: */
1565 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1566 mi_switch(l, NULL);
1567 SCHED_ASSERT_UNLOCKED();
1568 splx(s);
1569
1570 lwp_exit(l);
1571 }
1572
1573 void
1574 sigexit(struct lwp *l, int signum)
1575 {
1576 struct proc *p;
1577 int error, exitsig;
1578
1579 p = l->l_proc;
1580
1581 /*
1582 * Don't permit coredump() or exit1() multiple times
1583 * in the same process.
1584 */
1585 if (p->p_flag & P_WEXIT)
1586 (*p->p_userret)(l, p->p_userret_arg);
1587 p->p_flag |= P_WEXIT;
1588 /* We don't want to switch away from exiting. */
1589 /* XXX multiprocessor: stop LWPs on other processors. */
1590 if (l->l_flag & L_SA) {
1591 l->l_flag &= ~L_SA;
1592 p->p_flag &= ~P_SA;
1593 }
1594
1595 /* Make other LWPs stick around long enough to be dumped */
1596 p->p_userret = lwp_coredump_hook;
1597 p->p_userret_arg = NULL;
1598
1599 exitsig = signum;
1600 p->p_acflag |= AXSIG;
1601 if (sigprop[signum] & SA_CORE) {
1602 p->p_sigctx.ps_sig = signum;
1603 if ((error = coredump(l)) == 0)
1604 exitsig |= WCOREFLAG;
1605
1606 if (kern_logsigexit) {
1607 /* XXX What if we ever have really large UIDs? */
1608 int uid = p->p_cred && p->p_ucred ?
1609 (int) p->p_ucred->cr_uid : -1;
1610
1611 if (error)
1612 log(LOG_INFO, lognocoredump, p->p_pid,
1613 p->p_comm, uid, signum, error);
1614 else
1615 log(LOG_INFO, logcoredump, p->p_pid,
1616 p->p_comm, uid, signum);
1617 }
1618
1619 }
1620
1621 exit1(l, W_EXITCODE(0, exitsig));
1622 /* NOTREACHED */
1623 }
1624
1625 /*
1626 * Dump core, into a file named "progname.core" or "core" (depending on the
1627 * value of shortcorename), unless the process was setuid/setgid.
1628 */
1629 int
1630 coredump(struct lwp *l)
1631 {
1632 struct vnode *vp;
1633 struct proc *p;
1634 struct vmspace *vm;
1635 struct ucred *cred;
1636 struct nameidata nd;
1637 struct vattr vattr;
1638 int error, error1;
1639 char name[MAXPATHLEN];
1640
1641 p = l->l_proc;
1642 vm = p->p_vmspace;
1643 cred = p->p_cred->pc_ucred;
1644
1645 /*
1646 * Make sure the process has not set-id, to prevent data leaks.
1647 */
1648 if (p->p_flag & P_SUGID)
1649 return (EPERM);
1650
1651 /*
1652 * Refuse to core if the data + stack + user size is larger than
1653 * the core dump limit. XXX THIS IS WRONG, because of mapped
1654 * data.
1655 */
1656 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1657 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1658 return (EFBIG); /* better error code? */
1659
1660 /*
1661 * The core dump will go in the current working directory. Make
1662 * sure that the directory is still there and that the mount flags
1663 * allow us to write core dumps there.
1664 */
1665 vp = p->p_cwdi->cwdi_cdir;
1666 if (vp->v_mount == NULL ||
1667 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1668 return (EPERM);
1669
1670 error = build_corename(p, name);
1671 if (error)
1672 return error;
1673
1674 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1675 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1676 if (error)
1677 return (error);
1678 vp = nd.ni_vp;
1679
1680 /* Don't dump to non-regular files or files with links. */
1681 if (vp->v_type != VREG ||
1682 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1683 error = EINVAL;
1684 goto out;
1685 }
1686 VATTR_NULL(&vattr);
1687 vattr.va_size = 0;
1688 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1689 VOP_SETATTR(vp, &vattr, cred, p);
1690 p->p_acflag |= ACORE;
1691
1692 /* Now dump the actual core file. */
1693 error = (*p->p_execsw->es_coredump)(l, vp, cred);
1694 out:
1695 VOP_UNLOCK(vp, 0);
1696 error1 = vn_close(vp, FWRITE, cred, p);
1697 if (error == 0)
1698 error = error1;
1699 return (error);
1700 }
1701
1702 /*
1703 * Nonexistent system call-- signal process (may want to handle it).
1704 * Flag error in case process won't see signal immediately (blocked or ignored).
1705 */
1706 /* ARGSUSED */
1707 int
1708 sys_nosys(struct lwp *l, void *v, register_t *retval)
1709 {
1710 struct proc *p;
1711
1712 p = l->l_proc;
1713 psignal(p, SIGSYS);
1714 return (ENOSYS);
1715 }
1716
1717 static int
1718 build_corename(struct proc *p, char dst[MAXPATHLEN])
1719 {
1720 const char *s;
1721 char *d, *end;
1722 int i;
1723
1724 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1725 *s != '\0'; s++) {
1726 if (*s == '%') {
1727 switch (*(s + 1)) {
1728 case 'n':
1729 i = snprintf(d, end - d, "%s", p->p_comm);
1730 break;
1731 case 'p':
1732 i = snprintf(d, end - d, "%d", p->p_pid);
1733 break;
1734 case 'u':
1735 i = snprintf(d, end - d, "%.*s",
1736 (int)sizeof p->p_pgrp->pg_session->s_login,
1737 p->p_pgrp->pg_session->s_login);
1738 break;
1739 case 't':
1740 i = snprintf(d, end - d, "%ld",
1741 p->p_stats->p_start.tv_sec);
1742 break;
1743 default:
1744 goto copy;
1745 }
1746 d += i;
1747 s++;
1748 } else {
1749 copy: *d = *s;
1750 d++;
1751 }
1752 if (d >= end)
1753 return (ENAMETOOLONG);
1754 }
1755 *d = '\0';
1756 return 0;
1757 }
1758
1759 void
1760 getucontext(struct lwp *l, ucontext_t *ucp)
1761 {
1762 struct proc *p;
1763
1764 p = l->l_proc;
1765
1766 ucp->uc_flags = 0;
1767 ucp->uc_link = l->l_ctxlink;
1768
1769 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
1770 ucp->uc_flags |= _UC_SIGMASK;
1771
1772 /*
1773 * The (unsupplied) definition of the `current execution stack'
1774 * in the System V Interface Definition appears to allow returning
1775 * the main context stack.
1776 */
1777 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
1778 ucp->uc_stack.ss_sp = (void *)USRSTACK;
1779 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
1780 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
1781 } else {
1782 /* Simply copy alternate signal execution stack. */
1783 ucp->uc_stack = p->p_sigctx.ps_sigstk;
1784 }
1785 ucp->uc_flags |= _UC_STACK;
1786
1787 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1788 }
1789
1790 /* ARGSUSED */
1791 int
1792 sys_getcontext(struct lwp *l, void *v, register_t *retval)
1793 {
1794 struct sys_getcontext_args /* {
1795 syscallarg(struct __ucontext *) ucp;
1796 } */ *uap = v;
1797 ucontext_t uc;
1798
1799 getucontext(l, &uc);
1800
1801 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
1802 }
1803
1804 int
1805 setucontext(struct lwp *l, const ucontext_t *ucp)
1806 {
1807 struct proc *p;
1808 int error;
1809
1810 p = l->l_proc;
1811 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
1812 return (error);
1813 l->l_ctxlink = ucp->uc_link;
1814 /*
1815 * We might want to take care of the stack portion here but currently
1816 * don't; see the comment in getucontext().
1817 */
1818 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
1819 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
1820
1821 return 0;
1822 }
1823
1824 /* ARGSUSED */
1825 int
1826 sys_setcontext(struct lwp *l, void *v, register_t *retval)
1827 {
1828 struct sys_setcontext_args /* {
1829 syscallarg(const ucontext_t *) ucp;
1830 } */ *uap = v;
1831 ucontext_t uc;
1832 int error;
1833
1834 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
1835 exit1(l, W_EXITCODE(0, 0));
1836 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
1837 (error = setucontext(l, &uc)) != 0)
1838 return (error);
1839
1840 return (EJUSTRETURN);
1841 }
1842
1843 /*
1844 * sigtimedwait(2) system call, used also for implementation
1845 * of sigwaitinfo() and sigwait().
1846 *
1847 * This only handles single LWP in signal wait. libpthread provides
1848 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
1849 *
1850 * XXX no support for queued signals, si_code is always SI_USER.
1851 */
1852 int
1853 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
1854 {
1855 struct sys___sigtimedwait_args /* {
1856 syscallarg(const sigset_t *) set;
1857 syscallarg(siginfo_t *) info;
1858 syscallarg(struct timespec *) timeout;
1859 } */ *uap = v;
1860 sigset_t waitset, twaitset;
1861 struct proc *p = l->l_proc;
1862 int error, signum, s;
1863 int timo = 0;
1864 struct timeval tvstart;
1865 struct timespec ts;
1866
1867 if ((error = copyin(SCARG(uap, set), &waitset, sizeof(waitset))))
1868 return (error);
1869
1870 /*
1871 * Silently ignore SA_CANTMASK signals. psignal1() would
1872 * ignore SA_CANTMASK signals in waitset, we do this
1873 * only for the below siglist check.
1874 */
1875 sigminusset(&sigcantmask, &waitset);
1876
1877 /*
1878 * First scan siglist and check if there is signal from
1879 * our waitset already pending.
1880 */
1881 twaitset = waitset;
1882 __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
1883 if ((signum = firstsig(&twaitset))) {
1884 /* found pending signal */
1885 sigdelset(&p->p_sigctx.ps_siglist, signum);
1886 goto sig;
1887 }
1888
1889 /*
1890 * Calculate timeout, if it was specified.
1891 */
1892 if (SCARG(uap, timeout)) {
1893 uint64_t ms;
1894
1895 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
1896 return (error);
1897
1898 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
1899 timo = mstohz(ms);
1900 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
1901 timo = 1;
1902 if (timo <= 0)
1903 return (EAGAIN);
1904
1905 /*
1906 * Remember current mono_time, it would be used in
1907 * ECANCELED/ERESTART case.
1908 */
1909 s = splclock();
1910 tvstart = mono_time;
1911 splx(s);
1912 }
1913
1914 /*
1915 * Setup ps_sigwait list.
1916 */
1917 p->p_sigctx.ps_sigwaited = -1;
1918 p->p_sigctx.ps_sigwait = waitset;
1919
1920 /*
1921 * Wait for signal to arrive. We can either be woken up or
1922 * time out.
1923 */
1924 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
1925
1926 /*
1927 * Check if a signal from our wait set has arrived, or if it
1928 * was mere wakeup.
1929 */
1930 if (!error) {
1931 if ((signum = p->p_sigctx.ps_sigwaited) <= 0) {
1932 /* wakeup via _lwp_wakeup() */
1933 error = ECANCELED;
1934 }
1935 }
1936
1937 /*
1938 * On error, clear sigwait indication. psignal1() sets it
1939 * in !error case.
1940 */
1941 if (error) {
1942 p->p_sigctx.ps_sigwaited = 0;
1943
1944 /*
1945 * If the sleep was interrupted (either by signal or wakeup),
1946 * update the timeout and copyout new value back.
1947 * It would be used when the syscall would be restarted
1948 * or called again.
1949 */
1950 if (timo && (error == ERESTART || error == ECANCELED)) {
1951 struct timeval tvnow, tvtimo;
1952 int err;
1953
1954 s = splclock();
1955 tvnow = mono_time;
1956 splx(s);
1957
1958 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
1959
1960 /* compute how much time has passed since start */
1961 timersub(&tvnow, &tvstart, &tvnow);
1962 /* substract passed time from timeout */
1963 timersub(&tvtimo, &tvnow, &tvtimo);
1964
1965 if (tvtimo.tv_sec < 0)
1966 return (EAGAIN);
1967
1968 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
1969
1970 /* copy updated timeout to userland */
1971 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts))))
1972 return (err);
1973 }
1974
1975 return (error);
1976 }
1977
1978 /*
1979 * If a signal from the wait set arrived, copy it to userland.
1980 * XXX no queued signals for now
1981 */
1982 if (signum > 0) {
1983 siginfo_t si;
1984
1985 sig:
1986 memset(&si, 0, sizeof(si));
1987 si.si_signo = signum;
1988 si.si_code = SI_USER;
1989
1990 error = copyout(&si, SCARG(uap, info), sizeof(si));
1991 if (error)
1992 return (error);
1993 }
1994
1995 return (0);
1996 }
1997
1998 /*
1999 * Returns true if signal is ignored or masked for passed process.
2000 */
2001 int
2002 sigismasked(struct proc *p, int sig)
2003 {
2004
2005 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2006 sigismember(&p->p_sigctx.ps_sigmask, sig));
2007 }
2008
2009 static int
2010 filt_sigattach(struct knote *kn)
2011 {
2012 struct proc *p = curproc;
2013
2014 kn->kn_ptr.p_proc = p;
2015 kn->kn_flags |= EV_CLEAR; /* automatically set */
2016
2017 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2018
2019 return (0);
2020 }
2021
2022 static void
2023 filt_sigdetach(struct knote *kn)
2024 {
2025 struct proc *p = kn->kn_ptr.p_proc;
2026
2027 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2028 }
2029
2030 /*
2031 * signal knotes are shared with proc knotes, so we apply a mask to
2032 * the hint in order to differentiate them from process hints. This
2033 * could be avoided by using a signal-specific knote list, but probably
2034 * isn't worth the trouble.
2035 */
2036 static int
2037 filt_signal(struct knote *kn, long hint)
2038 {
2039
2040 if (hint & NOTE_SIGNAL) {
2041 hint &= ~NOTE_SIGNAL;
2042
2043 if (kn->kn_id == hint)
2044 kn->kn_data++;
2045 }
2046 return (kn->kn_data != 0);
2047 }
2048
2049 const struct filterops sig_filtops = {
2050 0, filt_sigattach, filt_sigdetach, filt_signal
2051 };
2052