kern_sig.c revision 1.108 1 /* $NetBSD: kern_sig.c,v 1.108 2000/11/05 15:37:09 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 "opt_ktrace.h"
44 #include "opt_compat_sunos.h"
45 #include "opt_compat_netbsd32.h"
46
47 #define SIGPROP /* include signal properties table */
48 #include <sys/param.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/timeb.h>
56 #include <sys/times.h>
57 #include <sys/buf.h>
58 #include <sys/acct.h>
59 #include <sys/file.h>
60 #include <sys/kernel.h>
61 #include <sys/wait.h>
62 #include <sys/ktrace.h>
63 #include <sys/syslog.h>
64 #include <sys/stat.h>
65 #include <sys/core.h>
66 #include <sys/ptrace.h>
67 #include <sys/filedesc.h>
68 #include <sys/malloc.h>
69 #include <sys/pool.h>
70
71 #include <sys/mount.h>
72 #include <sys/syscallargs.h>
73
74 #include <machine/cpu.h>
75
76 #include <sys/user.h> /* for coredump */
77
78 #include <uvm/uvm_extern.h>
79
80 static void proc_stop __P((struct proc *p));
81 void killproc __P((struct proc *, char *));
82 static int build_corename __P((struct proc *, char [MAXPATHLEN]));
83 #if COMPAT_NETBSD32
84 static int coredump32 __P((struct proc *, struct vnode *));
85 #endif
86 sigset_t contsigmask, stopsigmask, sigcantmask;
87
88 struct pool sigacts_pool; /* memory pool for sigacts structures */
89
90 /*
91 * Can process p, with pcred pc, send the signal signum to process q?
92 */
93 #define CANSIGNAL(p, pc, q, signum) \
94 ((pc)->pc_ucred->cr_uid == 0 || \
95 (pc)->p_ruid == (q)->p_cred->p_ruid || \
96 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
97 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
98 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
99 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
100
101 /*
102 * Initialize signal-related data structures.
103 */
104 void
105 signal_init()
106 {
107
108 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
109 0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
110 }
111
112 /*
113 * Create an initial sigacts structure, using the same signal state
114 * as p.
115 */
116 struct sigacts *
117 sigactsinit(p)
118 struct proc *p;
119 {
120 struct sigacts *ps;
121
122 ps = pool_get(&sigacts_pool, PR_WAITOK);
123 memcpy(ps, p->p_sigacts, sizeof(struct sigacts));
124 ps->ps_refcnt = 1;
125 return (ps);
126 }
127
128 /*
129 * Make p2 share p1's sigacts.
130 */
131 void
132 sigactsshare(p1, p2)
133 struct proc *p1, *p2;
134 {
135
136 p2->p_sigacts = p1->p_sigacts;
137 p1->p_sigacts->ps_refcnt++;
138 }
139
140 /*
141 * Make this process not share its sigacts, maintaining all
142 * signal state.
143 */
144 void
145 sigactsunshare(p)
146 struct proc *p;
147 {
148 struct sigacts *newps;
149
150 if (p->p_sigacts->ps_refcnt == 1)
151 return;
152
153 newps = sigactsinit(p);
154 sigactsfree(p);
155 p->p_sigacts = newps;
156 }
157
158 /*
159 * Release a sigacts structure.
160 */
161 void
162 sigactsfree(p)
163 struct proc *p;
164 {
165 struct sigacts *ps = p->p_sigacts;
166
167 if (--ps->ps_refcnt > 0)
168 return;
169
170 p->p_sigacts = NULL;
171
172 pool_put(&sigacts_pool, ps);
173 }
174
175 int
176 sigaction1(p, signum, nsa, osa)
177 struct proc *p;
178 int signum;
179 const struct sigaction *nsa;
180 struct sigaction *osa;
181 {
182 struct sigacts *ps = p->p_sigacts;
183 int prop;
184
185 if (signum <= 0 || signum >= NSIG)
186 return (EINVAL);
187
188 if (osa)
189 *osa = ps->ps_sigact[signum];
190
191 if (nsa) {
192 if (nsa->sa_flags & ~SA_ALLBITS)
193 return (EINVAL);
194
195 prop = sigprop[signum];
196 if (prop & SA_CANTMASK)
197 return (EINVAL);
198
199 (void) splsched(); /* XXXSMP */
200 ps->ps_sigact[signum] = *nsa;
201 sigminusset(&sigcantmask, &ps->ps_sigact[signum].sa_mask);
202 if ((prop & SA_NORESET) != 0)
203 ps->ps_sigact[signum].sa_flags &= ~SA_RESETHAND;
204 if (signum == SIGCHLD) {
205 if (nsa->sa_flags & SA_NOCLDSTOP)
206 p->p_flag |= P_NOCLDSTOP;
207 else
208 p->p_flag &= ~P_NOCLDSTOP;
209 if (nsa->sa_flags & SA_NOCLDWAIT) {
210 /*
211 * Paranoia: since SA_NOCLDWAIT is implemented
212 * by reparenting the dying child to PID 1 (and
213 * trust it to reap the zombie), PID 1 itself is
214 * forbidden to set SA_NOCLDWAIT.
215 */
216 if (p->p_pid == 1)
217 p->p_flag &= ~P_NOCLDWAIT;
218 else
219 p->p_flag |= P_NOCLDWAIT;
220 } else
221 p->p_flag &= ~P_NOCLDWAIT;
222 }
223 if ((nsa->sa_flags & SA_NODEFER) == 0)
224 sigaddset(&ps->ps_sigact[signum].sa_mask, signum);
225 else
226 sigdelset(&ps->ps_sigact[signum].sa_mask, signum);
227 /*
228 * Set bit in p_sigignore for signals that are set to SIG_IGN,
229 * and for signals set to SIG_DFL where the default is to ignore.
230 * However, don't put SIGCONT in p_sigignore,
231 * as we have to restart the process.
232 */
233 if (nsa->sa_handler == SIG_IGN ||
234 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
235 sigdelset(&p->p_siglist, signum); /* never to be seen again */
236 if (signum != SIGCONT)
237 sigaddset(&p->p_sigignore, signum); /* easier in psignal */
238 sigdelset(&p->p_sigcatch, signum);
239 } else {
240 sigdelset(&p->p_sigignore, signum);
241 if (nsa->sa_handler == SIG_DFL)
242 sigdelset(&p->p_sigcatch, signum);
243 else
244 sigaddset(&p->p_sigcatch, signum);
245 }
246 (void) spl0();
247 }
248
249 return (0);
250 }
251
252 /* ARGSUSED */
253 int
254 sys___sigaction14(p, v, retval)
255 struct proc *p;
256 void *v;
257 register_t *retval;
258 {
259 struct sys___sigaction14_args /* {
260 syscallarg(int) signum;
261 syscallarg(const struct sigaction *) nsa;
262 syscallarg(struct sigaction *) osa;
263 } */ *uap = v;
264 struct sigaction nsa, osa;
265 int error;
266
267 if (SCARG(uap, nsa)) {
268 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
269 if (error)
270 return (error);
271 }
272 error = sigaction1(p, SCARG(uap, signum),
273 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
274 if (error)
275 return (error);
276 if (SCARG(uap, osa)) {
277 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
278 if (error)
279 return (error);
280 }
281 return (0);
282 }
283
284 /*
285 * Initialize signal state for process 0;
286 * set to ignore signals that are ignored by default and disable the signal
287 * stack.
288 */
289 void
290 siginit(p)
291 struct proc *p;
292 {
293 struct sigacts *ps = p->p_sigacts;
294 int signum;
295 int prop;
296
297 sigemptyset(&contsigmask);
298 sigemptyset(&stopsigmask);
299 sigemptyset(&sigcantmask);
300 for (signum = 1; signum < NSIG; signum++) {
301 prop = sigprop[signum];
302 if (prop & SA_CONT)
303 sigaddset(&contsigmask, signum);
304 if (prop & SA_STOP)
305 sigaddset(&stopsigmask, signum);
306 if (prop & SA_CANTMASK)
307 sigaddset(&sigcantmask, signum);
308 if (prop & SA_IGNORE && signum != SIGCONT)
309 sigaddset(&p->p_sigignore, signum);
310 sigemptyset(&ps->ps_sigact[signum].sa_mask);
311 ps->ps_sigact[signum].sa_flags = SA_RESTART;
312 }
313 sigemptyset(&p->p_sigcatch);
314 p->p_flag &= ~P_NOCLDSTOP;
315
316 /*
317 * Reset stack state to the user stack.
318 */
319 ps->ps_sigstk.ss_flags = SS_DISABLE;
320 ps->ps_sigstk.ss_size = 0;
321 ps->ps_sigstk.ss_sp = 0;
322
323 /* One reference. */
324 ps->ps_refcnt = 1;
325 }
326
327 /*
328 * Reset signals for an exec of the specified process.
329 */
330 void
331 execsigs(p)
332 struct proc *p;
333 {
334 struct sigacts *ps = p->p_sigacts;
335 int signum;
336 int prop;
337
338 /*
339 * Reset caught signals. Held signals remain held
340 * through p_sigmask (unless they were caught,
341 * and are now ignored by default).
342 */
343 for (signum = 1; signum < NSIG; signum++) {
344 if (sigismember(&p->p_sigcatch, signum)) {
345 prop = sigprop[signum];
346 if (prop & SA_IGNORE) {
347 if ((prop & SA_CONT) == 0)
348 sigaddset(&p->p_sigignore, signum);
349 sigdelset(&p->p_siglist, signum);
350 }
351 ps->ps_sigact[signum].sa_handler = SIG_DFL;
352 }
353 sigemptyset(&ps->ps_sigact[signum].sa_mask);
354 ps->ps_sigact[signum].sa_flags = SA_RESTART;
355 }
356 sigemptyset(&p->p_sigcatch);
357 p->p_flag &= ~P_NOCLDSTOP;
358
359 /*
360 * Reset stack state to the user stack.
361 */
362 ps->ps_sigstk.ss_flags = SS_DISABLE;
363 ps->ps_sigstk.ss_size = 0;
364 ps->ps_sigstk.ss_sp = 0;
365 }
366
367 int
368 sigprocmask1(p, how, nss, oss)
369 struct proc *p;
370 int how;
371 const sigset_t *nss;
372 sigset_t *oss;
373 {
374
375 if (oss)
376 *oss = p->p_sigmask;
377
378 if (nss) {
379 (void)splsched(); /* XXXSMP */
380 switch (how) {
381 case SIG_BLOCK:
382 sigplusset(nss, &p->p_sigmask);
383 break;
384 case SIG_UNBLOCK:
385 sigminusset(nss, &p->p_sigmask);
386 p->p_sigcheck = 1;
387 break;
388 case SIG_SETMASK:
389 p->p_sigmask = *nss;
390 p->p_sigcheck = 1;
391 break;
392 default:
393 (void)spl0(); /* XXXSMP */
394 return (EINVAL);
395 }
396 sigminusset(&sigcantmask, &p->p_sigmask);
397 (void)spl0(); /* XXXSMP */
398 }
399
400 return (0);
401 }
402
403 /*
404 * Manipulate signal mask.
405 * Note that we receive new mask, not pointer,
406 * and return old mask as return value;
407 * the library stub does the rest.
408 */
409 int
410 sys___sigprocmask14(p, v, retval)
411 struct proc *p;
412 void *v;
413 register_t *retval;
414 {
415 struct sys___sigprocmask14_args /* {
416 syscallarg(int) how;
417 syscallarg(const sigset_t *) set;
418 syscallarg(sigset_t *) oset;
419 } */ *uap = v;
420 sigset_t nss, oss;
421 int error;
422
423 if (SCARG(uap, set)) {
424 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
425 if (error)
426 return (error);
427 }
428 error = sigprocmask1(p, SCARG(uap, how),
429 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
430 if (error)
431 return (error);
432 if (SCARG(uap, oset)) {
433 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
434 if (error)
435 return (error);
436 }
437 return (0);
438 }
439
440 void
441 sigpending1(p, ss)
442 struct proc *p;
443 sigset_t *ss;
444 {
445
446 *ss = p->p_siglist;
447 sigminusset(&p->p_sigmask, ss);
448 }
449
450 /* ARGSUSED */
451 int
452 sys___sigpending14(p, v, retval)
453 struct proc *p;
454 void *v;
455 register_t *retval;
456 {
457 struct sys___sigpending14_args /* {
458 syscallarg(sigset_t *) set;
459 } */ *uap = v;
460 sigset_t ss;
461
462 sigpending1(p, &ss);
463 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
464 }
465
466 int
467 sigsuspend1(p, ss)
468 struct proc *p;
469 const sigset_t *ss;
470 {
471 struct sigacts *ps = p->p_sigacts;
472
473 if (ss) {
474 /*
475 * When returning from sigpause, we want
476 * the old mask to be restored after the
477 * signal handler has finished. Thus, we
478 * save it here and mark the sigacts structure
479 * to indicate this.
480 */
481 ps->ps_oldmask = p->p_sigmask;
482 ps->ps_flags |= SAS_OLDMASK;
483 (void) splsched(); /* XXXSMP */
484 p->p_sigmask = *ss;
485 p->p_sigcheck = 1;
486 sigminusset(&sigcantmask, &p->p_sigmask);
487 (void) spl0(); /* XXXSMP */
488 }
489
490 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
491 /* void */;
492 /* always return EINTR rather than ERESTART... */
493 return (EINTR);
494 }
495
496 /*
497 * Suspend process until signal, providing mask to be set
498 * in the meantime. Note nonstandard calling convention:
499 * libc stub passes mask, not pointer, to save a copyin.
500 */
501 /* ARGSUSED */
502 int
503 sys___sigsuspend14(p, v, retval)
504 struct proc *p;
505 void *v;
506 register_t *retval;
507 {
508 struct sys___sigsuspend14_args /* {
509 syscallarg(const sigset_t *) set;
510 } */ *uap = v;
511 sigset_t ss;
512 int error;
513
514 if (SCARG(uap, set)) {
515 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
516 if (error)
517 return (error);
518 }
519
520 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
521 }
522
523 int
524 sigaltstack1(p, nss, oss)
525 struct proc *p;
526 const struct sigaltstack *nss;
527 struct sigaltstack *oss;
528 {
529 struct sigacts *ps = p->p_sigacts;
530
531 if (oss)
532 *oss = ps->ps_sigstk;
533
534 if (nss) {
535 if (nss->ss_flags & ~SS_ALLBITS)
536 return (EINVAL);
537
538 if (nss->ss_flags & SS_DISABLE) {
539 if (ps->ps_sigstk.ss_flags & SS_ONSTACK)
540 return (EINVAL);
541 } else {
542 if (nss->ss_size < MINSIGSTKSZ)
543 return (ENOMEM);
544 }
545 ps->ps_sigstk = *nss;
546 }
547
548 return (0);
549 }
550
551 /* ARGSUSED */
552 int
553 sys___sigaltstack14(p, v, retval)
554 struct proc *p;
555 void *v;
556 register_t *retval;
557 {
558 struct sys___sigaltstack14_args /* {
559 syscallarg(const struct sigaltstack *) nss;
560 syscallarg(struct sigaltstack *) oss;
561 } */ *uap = v;
562 struct sigaltstack nss, oss;
563 int error;
564
565 if (SCARG(uap, nss)) {
566 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
567 if (error)
568 return (error);
569 }
570 error = sigaltstack1(p,
571 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
572 if (error)
573 return (error);
574 if (SCARG(uap, oss)) {
575 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
576 if (error)
577 return (error);
578 }
579 return (0);
580 }
581
582 /* ARGSUSED */
583 int
584 sys_kill(cp, v, retval)
585 struct proc *cp;
586 void *v;
587 register_t *retval;
588 {
589 struct sys_kill_args /* {
590 syscallarg(int) pid;
591 syscallarg(int) signum;
592 } */ *uap = v;
593 struct proc *p;
594 struct pcred *pc = cp->p_cred;
595
596 if ((u_int)SCARG(uap, signum) >= NSIG)
597 return (EINVAL);
598 if (SCARG(uap, pid) > 0) {
599 /* kill single process */
600 if ((p = pfind(SCARG(uap, pid))) == NULL)
601 return (ESRCH);
602 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
603 return (EPERM);
604 if (SCARG(uap, signum))
605 psignal(p, SCARG(uap, signum));
606 return (0);
607 }
608 switch (SCARG(uap, pid)) {
609 case -1: /* broadcast signal */
610 return (killpg1(cp, SCARG(uap, signum), 0, 1));
611 case 0: /* signal own process group */
612 return (killpg1(cp, SCARG(uap, signum), 0, 0));
613 default: /* negative explicit process group */
614 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
615 }
616 /* NOTREACHED */
617 }
618
619 /*
620 * Common code for kill process group/broadcast kill.
621 * cp is calling process.
622 */
623 int
624 killpg1(cp, signum, pgid, all)
625 struct proc *cp;
626 int signum, pgid, all;
627 {
628 struct proc *p;
629 struct pcred *pc = cp->p_cred;
630 struct pgrp *pgrp;
631 int nfound = 0;
632
633 if (all) {
634 /*
635 * broadcast
636 */
637 proclist_lock_read();
638 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
639 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
640 p == cp || !CANSIGNAL(cp, pc, p, signum))
641 continue;
642 nfound++;
643 if (signum)
644 psignal(p, signum);
645 }
646 proclist_unlock_read();
647 } else {
648 if (pgid == 0)
649 /*
650 * zero pgid means send to my process group.
651 */
652 pgrp = cp->p_pgrp;
653 else {
654 pgrp = pgfind(pgid);
655 if (pgrp == NULL)
656 return (ESRCH);
657 }
658 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
659 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
660 !CANSIGNAL(cp, pc, p, signum))
661 continue;
662 nfound++;
663 if (signum && P_ZOMBIE(p) == 0)
664 psignal(p, signum);
665 }
666 }
667 return (nfound ? 0 : ESRCH);
668 }
669
670 /*
671 * Send a signal to a process group.
672 */
673 void
674 gsignal(pgid, signum)
675 int pgid, signum;
676 {
677 struct pgrp *pgrp;
678
679 if (pgid && (pgrp = pgfind(pgid)))
680 pgsignal(pgrp, signum, 0);
681 }
682
683 /*
684 * Send a signal to a process group. If checktty is 1,
685 * limit to members which have a controlling terminal.
686 */
687 void
688 pgsignal(pgrp, signum, checkctty)
689 struct pgrp *pgrp;
690 int signum, checkctty;
691 {
692 struct proc *p;
693
694 if (pgrp)
695 for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
696 if (checkctty == 0 || p->p_flag & P_CONTROLT)
697 psignal(p, signum);
698 }
699
700 /*
701 * Send a signal caused by a trap to the current process.
702 * If it will be caught immediately, deliver it with correct code.
703 * Otherwise, post it normally.
704 */
705 void
706 trapsignal(p, signum, code)
707 struct proc *p;
708 int signum;
709 u_long code;
710 {
711 struct sigacts *ps = p->p_sigacts;
712
713 if ((p->p_flag & P_TRACED) == 0 &&
714 sigismember(&p->p_sigcatch, signum) &&
715 !sigismember(&p->p_sigmask, signum)) {
716 p->p_stats->p_ru.ru_nsignals++;
717 #ifdef KTRACE
718 if (KTRPOINT(p, KTR_PSIG))
719 ktrpsig(p, signum,
720 ps->ps_sigact[signum].sa_handler, &p->p_sigmask,
721 code);
722 #endif
723 (*p->p_emul->e_sendsig)(ps->ps_sigact[signum].sa_handler,
724 signum, &p->p_sigmask, code);
725 (void) splsched(); /* XXXSMP */
726 sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
727 if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
728 sigdelset(&p->p_sigcatch, signum);
729 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
730 sigaddset(&p->p_sigignore, signum);
731 ps->ps_sigact[signum].sa_handler = SIG_DFL;
732 }
733 (void) spl0(); /* XXXSMP */
734 } else {
735 ps->ps_code = code; /* XXX for core dump/debugger */
736 ps->ps_sig = signum; /* XXX to verify code */
737 psignal(p, signum);
738 }
739 }
740
741 /*
742 * Send the signal to the process. If the signal has an action, the action
743 * is usually performed by the target process rather than the caller; we add
744 * the signal to the set of pending signals for the process.
745 *
746 * Exceptions:
747 * o When a stop signal is sent to a sleeping process that takes the
748 * default action, the process is stopped without awakening it.
749 * o SIGCONT restarts stopped processes (or puts them back to sleep)
750 * regardless of the signal action (eg, blocked or ignored).
751 *
752 * Other ignored signals are discarded immediately.
753 *
754 * XXXSMP: Invoked as psignal() or sched_psignal().
755 */
756 void
757 psignal1(p, signum, dolock)
758 struct proc *p;
759 int signum;
760 int dolock; /* XXXSMP: works, but icky */
761 {
762 int s, prop;
763 sig_t action;
764
765 #ifdef DIAGNOSTIC
766 if (signum <= 0 || signum >= NSIG)
767 panic("psignal signal number");
768
769 /* XXXSMP: works, but icky */
770 if (dolock)
771 SCHED_ASSERT_UNLOCKED();
772 else
773 SCHED_ASSERT_LOCKED();
774 #endif
775 prop = sigprop[signum];
776
777 /*
778 * If proc is traced, always give parent a chance.
779 */
780 if (p->p_flag & P_TRACED)
781 action = SIG_DFL;
782 else {
783 /*
784 * If the signal is being ignored,
785 * then we forget about it immediately.
786 * (Note: we don't set SIGCONT in p_sigignore,
787 * and if it is set to SIG_IGN,
788 * action will be SIG_DFL here.)
789 */
790 if (sigismember(&p->p_sigignore, signum))
791 return;
792 if (sigismember(&p->p_sigmask, signum))
793 action = SIG_HOLD;
794 else if (sigismember(&p->p_sigcatch, signum))
795 action = SIG_CATCH;
796 else {
797 action = SIG_DFL;
798
799 if (prop & SA_KILL && p->p_nice > NZERO)
800 p->p_nice = NZERO;
801
802 /*
803 * If sending a tty stop signal to a member of an
804 * orphaned process group, discard the signal here if
805 * the action is default; don't stop the process below
806 * if sleeping, and don't clear any pending SIGCONT.
807 */
808 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
809 return;
810 }
811 }
812
813 if (prop & SA_CONT)
814 sigminusset(&stopsigmask, &p->p_siglist);
815
816 if (prop & SA_STOP)
817 sigminusset(&contsigmask, &p->p_siglist);
818
819 sigaddset(&p->p_siglist, signum);
820 p->p_sigcheck = 1;
821
822 /*
823 * Defer further processing for signals which are held,
824 * except that stopped processes must be continued by SIGCONT.
825 */
826 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
827 return;
828
829 /* XXXSMP: works, but icky */
830 if (dolock)
831 SCHED_LOCK(s);
832
833 switch (p->p_stat) {
834 case SSLEEP:
835 /*
836 * If process is sleeping uninterruptibly
837 * we can't interrupt the sleep... the signal will
838 * be noticed when the process returns through
839 * trap() or syscall().
840 */
841 if ((p->p_flag & P_SINTR) == 0)
842 goto out;
843 /*
844 * Process is sleeping and traced... make it runnable
845 * so it can discover the signal in issignal() and stop
846 * for the parent.
847 */
848 if (p->p_flag & P_TRACED)
849 goto run;
850 /*
851 * If SIGCONT is default (or ignored) and process is
852 * asleep, we are finished; the process should not
853 * be awakened.
854 */
855 if ((prop & SA_CONT) && action == SIG_DFL) {
856 sigdelset(&p->p_siglist, signum);
857 goto out;
858 }
859 /*
860 * When a sleeping process receives a stop
861 * signal, process immediately if possible.
862 */
863 if ((prop & SA_STOP) && action == SIG_DFL) {
864 /*
865 * If a child holding parent blocked,
866 * stopping could cause deadlock.
867 */
868 if (p->p_flag & P_PPWAIT)
869 goto out;
870 sigdelset(&p->p_siglist, signum);
871 p->p_xstat = signum;
872 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
873 /*
874 * XXXSMP: recursive call; don't lock
875 * the second time around.
876 */
877 sched_psignal(p->p_pptr, SIGCHLD);
878 }
879 proc_stop(p); /* XXXSMP: recurse? */
880 goto out;
881 }
882 /*
883 * All other (caught or default) signals
884 * cause the process to run.
885 */
886 goto runfast;
887 /*NOTREACHED*/
888
889 case SSTOP:
890 /*
891 * If traced process is already stopped,
892 * then no further action is necessary.
893 */
894 if (p->p_flag & P_TRACED)
895 goto out;
896
897 /*
898 * Kill signal always sets processes running.
899 */
900 if (signum == SIGKILL)
901 goto runfast;
902
903 if (prop & SA_CONT) {
904 /*
905 * If SIGCONT is default (or ignored), we continue the
906 * process but don't leave the signal in p_siglist, as
907 * it has no further action. If SIGCONT is held, we
908 * continue the process and leave the signal in
909 * p_siglist. If the process catches SIGCONT, let it
910 * handle the signal itself. If it isn't waiting on
911 * an event, then it goes back to run state.
912 * Otherwise, process goes back to sleep state.
913 */
914 if (action == SIG_DFL)
915 sigdelset(&p->p_siglist, signum);
916 if (action == SIG_CATCH)
917 goto runfast;
918 if (p->p_wchan == 0)
919 goto run;
920 p->p_stat = SSLEEP;
921 goto out;
922 }
923
924 if (prop & SA_STOP) {
925 /*
926 * Already stopped, don't need to stop again.
927 * (If we did the shell could get confused.)
928 */
929 sigdelset(&p->p_siglist, signum);
930 goto out;
931 }
932
933 /*
934 * If process is sleeping interruptibly, then simulate a
935 * wakeup so that when it is continued, it will be made
936 * runnable and can look at the signal. But don't make
937 * the process runnable, leave it stopped.
938 */
939 if (p->p_wchan && p->p_flag & P_SINTR)
940 unsleep(p);
941 goto out;
942
943 case SONPROC:
944 /*
945 * We're running; notice the signal.
946 */
947 signotify(p);
948 goto out;
949
950 default:
951 /*
952 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
953 * It will either never be noticed, or noticed very soon.
954 */
955 goto out;
956 }
957 /*NOTREACHED*/
958
959 runfast:
960 /*
961 * Raise priority to at least PUSER.
962 */
963 if (p->p_priority > PUSER)
964 p->p_priority = PUSER;
965 run:
966 setrunnable(p); /* XXXSMP: recurse? */
967 out:
968 /* XXXSMP: works, but icky */
969 if (dolock)
970 SCHED_UNLOCK(s);
971 }
972
973 static __inline int firstsig __P((const sigset_t *));
974
975 static __inline int
976 firstsig(ss)
977 const sigset_t *ss;
978 {
979 int sig;
980
981 sig = ffs(ss->__bits[0]);
982 if (sig != 0)
983 return (sig);
984 #if NSIG > 33
985 sig = ffs(ss->__bits[1]);
986 if (sig != 0)
987 return (sig + 32);
988 #endif
989 #if NSIG > 65
990 sig = ffs(ss->__bits[2]);
991 if (sig != 0)
992 return (sig + 64);
993 #endif
994 #if NSIG > 97
995 sig = ffs(ss->__bits[3]);
996 if (sig != 0)
997 return (sig + 96);
998 #endif
999 return (0);
1000 }
1001
1002 /*
1003 * If the current process has received a signal (should be caught or cause
1004 * termination, should interrupt current syscall), return the signal number.
1005 * Stop signals with default action are processed immediately, then cleared;
1006 * they aren't returned. This is checked after each entry to the system for
1007 * a syscall or trap (though this can usually be done without calling issignal
1008 * by checking the pending signal masks in the CURSIG macro.) The normal call
1009 * sequence is
1010 *
1011 * while (signum = CURSIG(curproc))
1012 * postsig(signum);
1013 */
1014 int
1015 issignal(p)
1016 struct proc *p;
1017 {
1018 int s, signum, prop;
1019 sigset_t ss;
1020
1021 for (;;) {
1022 sigpending1(p, &ss);
1023 if (p->p_flag & P_PPWAIT)
1024 sigminusset(&stopsigmask, &ss);
1025 signum = firstsig(&ss);
1026 if (signum == 0) { /* no signal to send */
1027 p->p_sigcheck = 0;
1028 return (0);
1029 }
1030 sigdelset(&p->p_siglist, signum); /* take the signal! */
1031
1032 /*
1033 * We should see pending but ignored signals
1034 * only if P_TRACED was on when they were posted.
1035 */
1036 if (sigismember(&p->p_sigignore, signum) &&
1037 (p->p_flag & P_TRACED) == 0)
1038 continue;
1039
1040 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1041 /*
1042 * If traced, always stop, and stay
1043 * stopped until released by the debugger.
1044 */
1045 p->p_xstat = signum;
1046 if ((p->p_flag & P_FSTRACE) == 0)
1047 psignal(p->p_pptr, SIGCHLD);
1048 do {
1049 SCHED_LOCK(s);
1050 proc_stop(p);
1051 mi_switch(p);
1052 SCHED_ASSERT_UNLOCKED();
1053 splx(s);
1054 } while (!trace_req(p) && p->p_flag & P_TRACED);
1055
1056 /*
1057 * If we are no longer being traced, or the parent
1058 * didn't give us a signal, look for more signals.
1059 */
1060 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1061 continue;
1062
1063 /*
1064 * If the new signal is being masked, look for other
1065 * signals.
1066 */
1067 signum = p->p_xstat;
1068 /* `p->p_siglist |= mask' is done in setrunnable(). */
1069 if (sigismember(&p->p_sigmask, signum))
1070 continue;
1071 sigdelset(&p->p_siglist, signum); /* take the signal! */
1072 }
1073
1074 prop = sigprop[signum];
1075
1076 /*
1077 * Decide whether the signal should be returned.
1078 * Return the signal's number, or fall through
1079 * to clear it from the pending mask.
1080 */
1081 switch ((long)p->p_sigacts->ps_sigact[signum].sa_handler) {
1082
1083 case (long)SIG_DFL:
1084 /*
1085 * Don't take default actions on system processes.
1086 */
1087 if (p->p_pid <= 1) {
1088 #ifdef DIAGNOSTIC
1089 /*
1090 * Are you sure you want to ignore SIGSEGV
1091 * in init? XXX
1092 */
1093 printf("Process (pid %d) got signal %d\n",
1094 p->p_pid, signum);
1095 #endif
1096 break; /* == ignore */
1097 }
1098 /*
1099 * If there is a pending stop signal to process
1100 * with default action, stop here,
1101 * then clear the signal. However,
1102 * if process is member of an orphaned
1103 * process group, ignore tty stop signals.
1104 */
1105 if (prop & SA_STOP) {
1106 if (p->p_flag & P_TRACED ||
1107 (p->p_pgrp->pg_jobc == 0 &&
1108 prop & SA_TTYSTOP))
1109 break; /* == ignore */
1110 p->p_xstat = signum;
1111 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1112 psignal(p->p_pptr, SIGCHLD);
1113 SCHED_LOCK(s);
1114 proc_stop(p);
1115 mi_switch(p);
1116 SCHED_ASSERT_UNLOCKED();
1117 splx(s);
1118 break;
1119 } else if (prop & SA_IGNORE) {
1120 /*
1121 * Except for SIGCONT, shouldn't get here.
1122 * Default action is to ignore; drop it.
1123 */
1124 break; /* == ignore */
1125 } else
1126 goto keep;
1127 /*NOTREACHED*/
1128
1129 case (long)SIG_IGN:
1130 /*
1131 * Masking above should prevent us ever trying
1132 * to take action on an ignored signal other
1133 * than SIGCONT, unless process is traced.
1134 */
1135 if ((prop & SA_CONT) == 0 &&
1136 (p->p_flag & P_TRACED) == 0)
1137 printf("issignal\n");
1138 break; /* == ignore */
1139
1140 default:
1141 /*
1142 * This signal has an action, let
1143 * postsig() process it.
1144 */
1145 goto keep;
1146 }
1147 }
1148 /* NOTREACHED */
1149
1150 keep:
1151 sigaddset(&p->p_siglist, signum); /* leave the signal for later */
1152 p->p_sigcheck = 1;
1153 return (signum);
1154 }
1155
1156 /*
1157 * Put the argument process into the stopped state and notify the parent
1158 * via wakeup. Signals are handled elsewhere. The process must not be
1159 * on the run queue.
1160 */
1161 static void
1162 proc_stop(p)
1163 struct proc *p;
1164 {
1165
1166 SCHED_ASSERT_LOCKED();
1167
1168 p->p_stat = SSTOP;
1169 p->p_flag &= ~P_WAITED;
1170 sched_wakeup((caddr_t)p->p_pptr);
1171 }
1172
1173 /*
1174 * Take the action for the specified signal
1175 * from the current set of pending signals.
1176 */
1177 void
1178 postsig(signum)
1179 int signum;
1180 {
1181 struct proc *p = curproc;
1182 struct sigacts *ps = p->p_sigacts;
1183 sig_t action;
1184 u_long code;
1185 sigset_t *returnmask;
1186
1187 #ifdef DIAGNOSTIC
1188 if (signum == 0)
1189 panic("postsig");
1190 #endif
1191
1192 KERNEL_PROC_LOCK(p);
1193
1194 sigdelset(&p->p_siglist, signum);
1195 action = ps->ps_sigact[signum].sa_handler;
1196 #ifdef KTRACE
1197 if (KTRPOINT(p, KTR_PSIG))
1198 ktrpsig(p,
1199 signum, action, ps->ps_flags & SAS_OLDMASK ?
1200 &ps->ps_oldmask : &p->p_sigmask, 0);
1201 #endif
1202 if (action == SIG_DFL) {
1203 /*
1204 * Default action, where the default is to kill
1205 * the process. (Other cases were ignored above.)
1206 */
1207 sigexit(p, signum);
1208 /* NOTREACHED */
1209 } else {
1210 /*
1211 * If we get here, the signal must be caught.
1212 */
1213 #ifdef DIAGNOSTIC
1214 if (action == SIG_IGN || sigismember(&p->p_sigmask, signum))
1215 panic("postsig action");
1216 #endif
1217 /*
1218 * Set the new mask value and also defer further
1219 * occurences of this signal.
1220 *
1221 * Special case: user has done a sigpause. Here the
1222 * current mask is not of interest, but rather the
1223 * mask from before the sigpause is what we want
1224 * restored after the signal processing is completed.
1225 */
1226 if (ps->ps_flags & SAS_OLDMASK) {
1227 returnmask = &ps->ps_oldmask;
1228 ps->ps_flags &= ~SAS_OLDMASK;
1229 } else
1230 returnmask = &p->p_sigmask;
1231 p->p_stats->p_ru.ru_nsignals++;
1232 if (ps->ps_sig != signum) {
1233 code = 0;
1234 } else {
1235 code = ps->ps_code;
1236 ps->ps_code = 0;
1237 ps->ps_sig = 0;
1238 }
1239 (*p->p_emul->e_sendsig)(action, signum, returnmask, code);
1240 (void) splsched(); /* XXXSMP */
1241 sigplusset(&ps->ps_sigact[signum].sa_mask, &p->p_sigmask);
1242 if (ps->ps_sigact[signum].sa_flags & SA_RESETHAND) {
1243 sigdelset(&p->p_sigcatch, signum);
1244 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1245 sigaddset(&p->p_sigignore, signum);
1246 ps->ps_sigact[signum].sa_handler = SIG_DFL;
1247 }
1248 (void) spl0(); /* XXXSMP */
1249 }
1250
1251 KERNEL_PROC_UNLOCK(p);
1252 }
1253
1254 /*
1255 * Kill the current process for stated reason.
1256 */
1257 void
1258 killproc(p, why)
1259 struct proc *p;
1260 char *why;
1261 {
1262
1263 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1264 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1265 psignal(p, SIGKILL);
1266 }
1267
1268 /*
1269 * Force the current process to exit with the specified signal, dumping core
1270 * if appropriate. We bypass the normal tests for masked and caught signals,
1271 * allowing unrecoverable failures to terminate the process without changing
1272 * signal state. Mark the accounting record with the signal termination.
1273 * If dumping core, save the signal number for the debugger. Calls exit and
1274 * does not return.
1275 */
1276
1277 #if defined(DEBUG)
1278 int kern_logsigexit = 1; /* not static to make public for sysctl */
1279 #else
1280 int kern_logsigexit = 0; /* not static to make public for sysctl */
1281 #endif
1282
1283 static const char logcoredump[] =
1284 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1285 static const char lognocoredump[] =
1286 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1287
1288 void
1289 sigexit(p, signum)
1290 struct proc *p;
1291 int signum;
1292 {
1293 int error;
1294 int exitsig = signum;
1295
1296 p->p_acflag |= AXSIG;
1297 if (sigprop[signum] & SA_CORE) {
1298 p->p_sigacts->ps_sig = signum;
1299 if ((error = coredump(p)) == 0)
1300 exitsig |= WCOREFLAG;
1301
1302 if (kern_logsigexit) {
1303 int uid = p->p_cred && p->p_ucred ?
1304 p->p_ucred->cr_uid : -1;
1305
1306 if (error)
1307 log(LOG_INFO, lognocoredump, p->p_pid,
1308 p->p_comm, uid, signum, error);
1309 else
1310 log(LOG_INFO, logcoredump, p->p_pid,
1311 p->p_comm, uid, signum);
1312 }
1313
1314 }
1315
1316 exit1(p, W_EXITCODE(0, exitsig));
1317 /* NOTREACHED */
1318 }
1319
1320 /*
1321 * Dump core, into a file named "progname.core" or "core" (depending on the
1322 * value of shortcorename), unless the process was setuid/setgid.
1323 */
1324 int
1325 coredump(p)
1326 struct proc *p;
1327 {
1328 struct vnode *vp;
1329 struct vmspace *vm = p->p_vmspace;
1330 struct ucred *cred = p->p_cred->pc_ucred;
1331 struct nameidata nd;
1332 struct vattr vattr;
1333 int error, error1;
1334 char name[MAXPATHLEN];
1335 struct core core;
1336
1337 /*
1338 * Make sure the process has not set-id, to prevent data leaks.
1339 */
1340 if (p->p_flag & P_SUGID)
1341 return (EPERM);
1342
1343 /*
1344 * Refuse to core if the data + stack + user size is larger than
1345 * the core dump limit. XXX THIS IS WRONG, because of mapped
1346 * data.
1347 */
1348 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1349 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1350 return (EFBIG); /* better error code? */
1351
1352 /*
1353 * The core dump will go in the current working directory. Make
1354 * sure that the directory is still there and that the mount flags
1355 * allow us to write core dumps there.
1356 */
1357 vp = p->p_cwdi->cwdi_cdir;
1358 if (vp->v_mount == NULL ||
1359 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1360 return (EPERM);
1361
1362 error = build_corename(p, name);
1363 if (error)
1364 return error;
1365
1366 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1367 error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
1368 if (error)
1369 return (error);
1370 vp = nd.ni_vp;
1371
1372 /* Don't dump to non-regular files or files with links. */
1373 if (vp->v_type != VREG ||
1374 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1375 error = EINVAL;
1376 goto out;
1377 }
1378 VATTR_NULL(&vattr);
1379 vattr.va_size = 0;
1380 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1381 VOP_SETATTR(vp, &vattr, cred, p);
1382 p->p_acflag |= ACORE;
1383
1384 #if COMPAT_NETBSD32
1385 if (p->p_flag & P_32)
1386 return (coredump32(p, vp));
1387 #endif
1388 #if 0
1389 /*
1390 * XXX
1391 * It would be nice if we at least dumped the signal state (and made it
1392 * available at run time to the debugger, as well), but this code
1393 * hasn't actually had any effect for a long time, since we don't dump
1394 * the user area. For now, it's dead.
1395 */
1396 memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
1397 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1398 #endif
1399
1400 core.c_midmag = 0;
1401 strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1402 core.c_nseg = 0;
1403 core.c_signo = p->p_sigacts->ps_sig;
1404 core.c_ucode = p->p_sigacts->ps_code;
1405 core.c_cpusize = 0;
1406 core.c_tsize = (u_long)ctob(vm->vm_tsize);
1407 core.c_dsize = (u_long)ctob(vm->vm_dsize);
1408 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1409 error = cpu_coredump(p, vp, cred, &core);
1410 if (error)
1411 goto out;
1412 if (core.c_midmag == 0) {
1413 /* XXX
1414 * cpu_coredump() didn't bother to set the magic; assume
1415 * this is a request to do a traditional dump. cpu_coredump()
1416 * is still responsible for setting sensible values in
1417 * the core header.
1418 */
1419 if (core.c_cpusize == 0)
1420 core.c_cpusize = USPACE; /* Just in case */
1421 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1422 (int)core.c_dsize,
1423 (off_t)core.c_cpusize, UIO_USERSPACE,
1424 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1425 if (error)
1426 goto out;
1427 error = vn_rdwr(UIO_WRITE, vp,
1428 (caddr_t)(u_long)trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1429 core.c_ssize,
1430 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1431 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1432 } else {
1433 /*
1434 * uvm_coredump() spits out all appropriate segments.
1435 * All that's left to do is to write the core header.
1436 */
1437 error = uvm_coredump(p, vp, cred, &core);
1438 if (error)
1439 goto out;
1440 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1441 (int)core.c_hdrsize, (off_t)0,
1442 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1443 }
1444 out:
1445 VOP_UNLOCK(vp, 0);
1446 error1 = vn_close(vp, FWRITE, cred, p);
1447 if (error == 0)
1448 error = error1;
1449 return (error);
1450 }
1451
1452 #if COMPAT_NETBSD32
1453 /*
1454 * Same as coredump, but generates a 32-bit image.
1455 */
1456 int
1457 coredump32(p, vp)
1458 struct proc *p;
1459 struct vnode *vp;
1460 {
1461 struct vmspace *vm = p->p_vmspace;
1462 struct ucred *cred = p->p_cred->pc_ucred;
1463 int error, error1;
1464 struct core32 core;
1465
1466 #if 0
1467 /*
1468 * XXX
1469 * It would be nice if we at least dumped the signal state (and made it
1470 * available at run time to the debugger, as well), but this code
1471 * hasn't actually had any effect for a long time, since we don't dump
1472 * the user area. For now, it's dead.
1473 */
1474 memcpy(&p->p_addr->u_kproc.kp_proc, p, sizeof(struct proc));
1475 fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1476 #endif
1477
1478 core.c_midmag = 0;
1479 strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1480 core.c_nseg = 0;
1481 core.c_signo = p->p_sigacts->ps_sig;
1482 core.c_ucode = p->p_sigacts->ps_code;
1483 core.c_cpusize = 0;
1484 core.c_tsize = (u_long)ctob(vm->vm_tsize);
1485 core.c_dsize = (u_long)ctob(vm->vm_dsize);
1486 core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1487 error = cpu_coredump32(p, vp, cred, &core);
1488 if (error)
1489 goto out;
1490 if (core.c_midmag == 0) {
1491 /* XXX
1492 * cpu_coredump() didn't bother to set the magic; assume
1493 * this is a request to do a traditional dump. cpu_coredump()
1494 * is still responsible for setting sensible values in
1495 * the core header.
1496 */
1497 if (core.c_cpusize == 0)
1498 core.c_cpusize = USPACE; /* Just in case */
1499 error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1500 (int)core.c_dsize,
1501 (off_t)core.c_cpusize, UIO_USERSPACE,
1502 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1503 if (error)
1504 goto out;
1505 error = vn_rdwr(UIO_WRITE, vp,
1506 (caddr_t)(u_long)trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1507 core.c_ssize,
1508 (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1509 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1510 } else {
1511 /*
1512 * uvm_coredump() spits out all appropriate segments.
1513 * All that's left to do is to write the core header.
1514 */
1515 error = uvm_coredump32(p, vp, cred, &core);
1516 if (error)
1517 goto out;
1518 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1519 (int)core.c_hdrsize, (off_t)0,
1520 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1521 }
1522 out:
1523 VOP_UNLOCK(vp, 0);
1524 error1 = vn_close(vp, FWRITE, cred, p);
1525 if (error == 0)
1526 error = error1;
1527 return (error);
1528 }
1529 #endif
1530
1531 /*
1532 * Nonexistent system call-- signal process (may want to handle it).
1533 * Flag error in case process won't see signal immediately (blocked or ignored).
1534 */
1535 /* ARGSUSED */
1536 int
1537 sys_nosys(p, v, retval)
1538 struct proc *p;
1539 void *v;
1540 register_t *retval;
1541 {
1542
1543 psignal(p, SIGSYS);
1544 return (ENOSYS);
1545 }
1546
1547 static int
1548 build_corename(p, dst)
1549 struct proc *p;
1550 char dst[MAXPATHLEN];
1551 {
1552 const char *s;
1553 char *d, *end;
1554 int i;
1555
1556 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1557 *s != '\0'; s++) {
1558 if (*s == '%') {
1559 switch (*(s + 1)) {
1560 case 'n':
1561 i = snprintf(d, end - d, "%s", p->p_comm);
1562 break;
1563 case 'p':
1564 i = snprintf(d, end - d, "%d", p->p_pid);
1565 break;
1566 case 'u':
1567 i = snprintf(d, end - d, "%s",
1568 p->p_pgrp->pg_session->s_login);
1569 break;
1570 case 't':
1571 i = snprintf(d, end - d, "%ld",
1572 p->p_stats->p_start.tv_sec);
1573 break;
1574 default:
1575 goto copy;
1576 }
1577 d += i;
1578 s++;
1579 } else {
1580 copy: *d = *s;
1581 d++;
1582 }
1583 if (d >= end)
1584 return (ENAMETOOLONG);
1585 }
1586 *d = '\0';
1587 return (0);
1588 }
1589
1590 /*
1591 * Returns true if signal is ignored or masked for passed process.
1592 */
1593 int
1594 sigismasked(p, sig)
1595 struct proc *p;
1596 int sig;
1597 {
1598 return sigismember(&p->p_sigignore, SIGTTOU)
1599 || sigismember(&p->p_sigmask, SIGTTOU);
1600 }
1601