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