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