xref: /freebsd/sys/kern/kern_sig.c (revision 5203edcdc553fda6caa1da8826a89b1a02dad1bf)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/acct.h>
48 #include <sys/condvar.h>
49 #include <sys/event.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/kse.h>
53 #include <sys/ktr.h>
54 #include <sys/ktrace.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/pioctl.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sleepqueue.h>
63 #include <sys/smp.h>
64 #include <sys/stat.h>
65 #include <sys/sx.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysent.h>
69 #include <sys/syslog.h>
70 #include <sys/sysproto.h>
71 #include <sys/unistd.h>
72 #include <sys/wait.h>
73 
74 #include <machine/cpu.h>
75 
76 #if defined (__alpha__) && !defined(COMPAT_43)
77 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
78 #endif
79 
80 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
81 
82 static int	coredump(struct thread *);
83 static char	*expand_name(const char *, uid_t, pid_t);
84 static int	killpg1(struct thread *td, int sig, int pgid, int all);
85 static int	issignal(struct thread *p);
86 static int	sigprop(int sig);
87 static void	stop(struct proc *);
88 static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89 static int	filt_sigattach(struct knote *kn);
90 static void	filt_sigdetach(struct knote *kn);
91 static int	filt_signal(struct knote *kn, long hint);
92 static struct thread *sigtd(struct proc *p, int sig, int prop);
93 static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94 				siginfo_t *info, struct timespec *timeout);
95 static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
96 
97 struct filterops sig_filtops =
98 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99 
100 static int	kern_logsigexit = 1;
101 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
102     &kern_logsigexit, 0,
103     "Log processes quitting on abnormal signals to syslog(3)");
104 
105 /*
106  * Policy -- Can ucred cr1 send SIGIO to process cr2?
107  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
108  * in the right situations.
109  */
110 #define CANSIGIO(cr1, cr2) \
111 	((cr1)->cr_uid == 0 || \
112 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
113 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
114 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
115 	    (cr1)->cr_uid == (cr2)->cr_uid)
116 
117 int sugid_coredump;
118 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
119     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120 
121 static int	do_coredump = 1;
122 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123 	&do_coredump, 0, "Enable/Disable coredumps");
124 
125 /*
126  * Signal properties and actions.
127  * The array below categorizes the signals and their default actions
128  * according to the following properties:
129  */
130 #define	SA_KILL		0x01		/* terminates process by default */
131 #define	SA_CORE		0x02		/* ditto and coredumps */
132 #define	SA_STOP		0x04		/* suspend process */
133 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
134 #define	SA_IGNORE	0x10		/* ignore by default */
135 #define	SA_CONT		0x20		/* continue if suspended */
136 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
137 #define	SA_PROC		0x80		/* deliverable to any thread */
138 
139 static int sigproptbl[NSIG] = {
140         SA_KILL|SA_PROC,		/* SIGHUP */
141         SA_KILL|SA_PROC,		/* SIGINT */
142         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
143         SA_KILL|SA_CORE,		/* SIGILL */
144         SA_KILL|SA_CORE,		/* SIGTRAP */
145         SA_KILL|SA_CORE,		/* SIGABRT */
146         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
147         SA_KILL|SA_CORE,		/* SIGFPE */
148         SA_KILL|SA_PROC,		/* SIGKILL */
149         SA_KILL|SA_CORE,		/* SIGBUS */
150         SA_KILL|SA_CORE,		/* SIGSEGV */
151         SA_KILL|SA_CORE,		/* SIGSYS */
152         SA_KILL|SA_PROC,		/* SIGPIPE */
153         SA_KILL|SA_PROC,		/* SIGALRM */
154         SA_KILL|SA_PROC,		/* SIGTERM */
155         SA_IGNORE|SA_PROC,		/* SIGURG */
156         SA_STOP|SA_PROC,		/* SIGSTOP */
157         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
158         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
159         SA_IGNORE|SA_PROC,		/* SIGCHLD */
160         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
161         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
162         SA_IGNORE|SA_PROC,		/* SIGIO */
163         SA_KILL,			/* SIGXCPU */
164         SA_KILL,			/* SIGXFSZ */
165         SA_KILL|SA_PROC,		/* SIGVTALRM */
166         SA_KILL|SA_PROC,		/* SIGPROF */
167         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
168         SA_IGNORE|SA_PROC,		/* SIGINFO */
169         SA_KILL|SA_PROC,		/* SIGUSR1 */
170         SA_KILL|SA_PROC,		/* SIGUSR2 */
171 };
172 
173 /*
174  * Determine signal that should be delivered to process p, the current
175  * process, 0 if none.  If there is a pending stop signal with default
176  * action, the process stops in issignal().
177  * XXXKSE   the check for a pending stop is not done under KSE
178  *
179  * MP SAFE.
180  */
181 int
182 cursig(struct thread *td)
183 {
184 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
185 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
186 	mtx_assert(&sched_lock, MA_NOTOWNED);
187 	return (SIGPENDING(td) ? issignal(td) : 0);
188 }
189 
190 /*
191  * Arrange for ast() to handle unmasked pending signals on return to user
192  * mode.  This must be called whenever a signal is added to td_siglist or
193  * unmasked in td_sigmask.
194  */
195 void
196 signotify(struct thread *td)
197 {
198 	struct proc *p;
199 	sigset_t set, saved;
200 
201 	p = td->td_proc;
202 
203 	PROC_LOCK_ASSERT(p, MA_OWNED);
204 
205 	/*
206 	 * If our mask changed we may have to move signal that were
207 	 * previously masked by all threads to our siglist.
208 	 */
209 	set = p->p_siglist;
210 	if (p->p_flag & P_SA)
211 		saved = p->p_siglist;
212 	SIGSETNAND(set, td->td_sigmask);
213 	SIGSETNAND(p->p_siglist, set);
214 	SIGSETOR(td->td_siglist, set);
215 
216 	if (SIGPENDING(td)) {
217 		mtx_lock_spin(&sched_lock);
218 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
219 		mtx_unlock_spin(&sched_lock);
220 	}
221 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
222 		if (SIGSETEQ(saved, p->p_siglist))
223 			return;
224 		else {
225 			/* pending set changed */
226 			p->p_flag |= P_SIGEVENT;
227 			wakeup(&p->p_siglist);
228 		}
229 	}
230 }
231 
232 int
233 sigonstack(size_t sp)
234 {
235 	struct thread *td = curthread;
236 
237 	return ((td->td_pflags & TDP_ALTSTACK) ?
238 #if defined(COMPAT_43)
239 	    ((td->td_sigstk.ss_size == 0) ?
240 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
241 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
242 #else
243 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
244 #endif
245 	    : 0);
246 }
247 
248 static __inline int
249 sigprop(int sig)
250 {
251 
252 	if (sig > 0 && sig < NSIG)
253 		return (sigproptbl[_SIG_IDX(sig)]);
254 	return (0);
255 }
256 
257 int
258 sig_ffs(sigset_t *set)
259 {
260 	int i;
261 
262 	for (i = 0; i < _SIG_WORDS; i++)
263 		if (set->__bits[i])
264 			return (ffs(set->__bits[i]) + (i * 32));
265 	return (0);
266 }
267 
268 /*
269  * kern_sigaction
270  * sigaction
271  * freebsd4_sigaction
272  * osigaction
273  *
274  * MPSAFE
275  */
276 int
277 kern_sigaction(td, sig, act, oact, flags)
278 	struct thread *td;
279 	register int sig;
280 	struct sigaction *act, *oact;
281 	int flags;
282 {
283 	struct sigacts *ps;
284 	struct thread *td0;
285 	struct proc *p = td->td_proc;
286 
287 	if (!_SIG_VALID(sig))
288 		return (EINVAL);
289 
290 	PROC_LOCK(p);
291 	ps = p->p_sigacts;
292 	mtx_lock(&ps->ps_mtx);
293 	if (oact) {
294 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
295 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
296 		oact->sa_flags = 0;
297 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
298 			oact->sa_flags |= SA_ONSTACK;
299 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
300 			oact->sa_flags |= SA_RESTART;
301 		if (SIGISMEMBER(ps->ps_sigreset, sig))
302 			oact->sa_flags |= SA_RESETHAND;
303 		if (SIGISMEMBER(ps->ps_signodefer, sig))
304 			oact->sa_flags |= SA_NODEFER;
305 		if (SIGISMEMBER(ps->ps_siginfo, sig))
306 			oact->sa_flags |= SA_SIGINFO;
307 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
308 			oact->sa_flags |= SA_NOCLDSTOP;
309 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
310 			oact->sa_flags |= SA_NOCLDWAIT;
311 	}
312 	if (act) {
313 		if ((sig == SIGKILL || sig == SIGSTOP) &&
314 		    act->sa_handler != SIG_DFL) {
315 			mtx_unlock(&ps->ps_mtx);
316 			PROC_UNLOCK(p);
317 			return (EINVAL);
318 		}
319 
320 		/*
321 		 * Change setting atomically.
322 		 */
323 
324 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
325 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
326 		if (act->sa_flags & SA_SIGINFO) {
327 			ps->ps_sigact[_SIG_IDX(sig)] =
328 			    (__sighandler_t *)act->sa_sigaction;
329 			SIGADDSET(ps->ps_siginfo, sig);
330 		} else {
331 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
332 			SIGDELSET(ps->ps_siginfo, sig);
333 		}
334 		if (!(act->sa_flags & SA_RESTART))
335 			SIGADDSET(ps->ps_sigintr, sig);
336 		else
337 			SIGDELSET(ps->ps_sigintr, sig);
338 		if (act->sa_flags & SA_ONSTACK)
339 			SIGADDSET(ps->ps_sigonstack, sig);
340 		else
341 			SIGDELSET(ps->ps_sigonstack, sig);
342 		if (act->sa_flags & SA_RESETHAND)
343 			SIGADDSET(ps->ps_sigreset, sig);
344 		else
345 			SIGDELSET(ps->ps_sigreset, sig);
346 		if (act->sa_flags & SA_NODEFER)
347 			SIGADDSET(ps->ps_signodefer, sig);
348 		else
349 			SIGDELSET(ps->ps_signodefer, sig);
350 		if (sig == SIGCHLD) {
351 			if (act->sa_flags & SA_NOCLDSTOP)
352 				ps->ps_flag |= PS_NOCLDSTOP;
353 			else
354 				ps->ps_flag &= ~PS_NOCLDSTOP;
355 			if (act->sa_flags & SA_NOCLDWAIT) {
356 				/*
357 				 * Paranoia: since SA_NOCLDWAIT is implemented
358 				 * by reparenting the dying child to PID 1 (and
359 				 * trust it to reap the zombie), PID 1 itself
360 				 * is forbidden to set SA_NOCLDWAIT.
361 				 */
362 				if (p->p_pid == 1)
363 					ps->ps_flag &= ~PS_NOCLDWAIT;
364 				else
365 					ps->ps_flag |= PS_NOCLDWAIT;
366 			} else
367 				ps->ps_flag &= ~PS_NOCLDWAIT;
368 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
369 				ps->ps_flag |= PS_CLDSIGIGN;
370 			else
371 				ps->ps_flag &= ~PS_CLDSIGIGN;
372 		}
373 		/*
374 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
375 		 * and for signals set to SIG_DFL where the default is to
376 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
377 		 * have to restart the process.
378 		 */
379 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
380 		    (sigprop(sig) & SA_IGNORE &&
381 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
382 			if ((p->p_flag & P_SA) &&
383 			     SIGISMEMBER(p->p_siglist, sig)) {
384 				p->p_flag |= P_SIGEVENT;
385 				wakeup(&p->p_siglist);
386 			}
387 			/* never to be seen again */
388 			SIGDELSET(p->p_siglist, sig);
389 			mtx_lock_spin(&sched_lock);
390 			FOREACH_THREAD_IN_PROC(p, td0)
391 				SIGDELSET(td0->td_siglist, sig);
392 			mtx_unlock_spin(&sched_lock);
393 			if (sig != SIGCONT)
394 				/* easier in psignal */
395 				SIGADDSET(ps->ps_sigignore, sig);
396 			SIGDELSET(ps->ps_sigcatch, sig);
397 		} else {
398 			SIGDELSET(ps->ps_sigignore, sig);
399 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
400 				SIGDELSET(ps->ps_sigcatch, sig);
401 			else
402 				SIGADDSET(ps->ps_sigcatch, sig);
403 		}
404 #ifdef COMPAT_FREEBSD4
405 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
406 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
407 		    (flags & KSA_FREEBSD4) == 0)
408 			SIGDELSET(ps->ps_freebsd4, sig);
409 		else
410 			SIGADDSET(ps->ps_freebsd4, sig);
411 #endif
412 #ifdef COMPAT_43
413 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
414 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
415 		    (flags & KSA_OSIGSET) == 0)
416 			SIGDELSET(ps->ps_osigset, sig);
417 		else
418 			SIGADDSET(ps->ps_osigset, sig);
419 #endif
420 	}
421 	mtx_unlock(&ps->ps_mtx);
422 	PROC_UNLOCK(p);
423 	return (0);
424 }
425 
426 #ifndef _SYS_SYSPROTO_H_
427 struct sigaction_args {
428 	int	sig;
429 	struct	sigaction *act;
430 	struct	sigaction *oact;
431 };
432 #endif
433 /*
434  * MPSAFE
435  */
436 int
437 sigaction(td, uap)
438 	struct thread *td;
439 	register struct sigaction_args *uap;
440 {
441 	struct sigaction act, oact;
442 	register struct sigaction *actp, *oactp;
443 	int error;
444 
445 	actp = (uap->act != NULL) ? &act : NULL;
446 	oactp = (uap->oact != NULL) ? &oact : NULL;
447 	if (actp) {
448 		error = copyin(uap->act, actp, sizeof(act));
449 		if (error)
450 			return (error);
451 	}
452 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
453 	if (oactp && !error)
454 		error = copyout(oactp, uap->oact, sizeof(oact));
455 	return (error);
456 }
457 
458 #ifdef COMPAT_FREEBSD4
459 #ifndef _SYS_SYSPROTO_H_
460 struct freebsd4_sigaction_args {
461 	int	sig;
462 	struct	sigaction *act;
463 	struct	sigaction *oact;
464 };
465 #endif
466 /*
467  * MPSAFE
468  */
469 int
470 freebsd4_sigaction(td, uap)
471 	struct thread *td;
472 	register struct freebsd4_sigaction_args *uap;
473 {
474 	struct sigaction act, oact;
475 	register struct sigaction *actp, *oactp;
476 	int error;
477 
478 
479 	actp = (uap->act != NULL) ? &act : NULL;
480 	oactp = (uap->oact != NULL) ? &oact : NULL;
481 	if (actp) {
482 		error = copyin(uap->act, actp, sizeof(act));
483 		if (error)
484 			return (error);
485 	}
486 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
487 	if (oactp && !error)
488 		error = copyout(oactp, uap->oact, sizeof(oact));
489 	return (error);
490 }
491 #endif	/* COMAPT_FREEBSD4 */
492 
493 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
494 #ifndef _SYS_SYSPROTO_H_
495 struct osigaction_args {
496 	int	signum;
497 	struct	osigaction *nsa;
498 	struct	osigaction *osa;
499 };
500 #endif
501 /*
502  * MPSAFE
503  */
504 int
505 osigaction(td, uap)
506 	struct thread *td;
507 	register struct osigaction_args *uap;
508 {
509 	struct osigaction sa;
510 	struct sigaction nsa, osa;
511 	register struct sigaction *nsap, *osap;
512 	int error;
513 
514 	if (uap->signum <= 0 || uap->signum >= ONSIG)
515 		return (EINVAL);
516 
517 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
518 	osap = (uap->osa != NULL) ? &osa : NULL;
519 
520 	if (nsap) {
521 		error = copyin(uap->nsa, &sa, sizeof(sa));
522 		if (error)
523 			return (error);
524 		nsap->sa_handler = sa.sa_handler;
525 		nsap->sa_flags = sa.sa_flags;
526 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
527 	}
528 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
529 	if (osap && !error) {
530 		sa.sa_handler = osap->sa_handler;
531 		sa.sa_flags = osap->sa_flags;
532 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
533 		error = copyout(&sa, uap->osa, sizeof(sa));
534 	}
535 	return (error);
536 }
537 
538 #if !defined(__i386__) && !defined(__alpha__)
539 /* Avoid replicating the same stub everywhere */
540 int
541 osigreturn(td, uap)
542 	struct thread *td;
543 	struct osigreturn_args *uap;
544 {
545 
546 	return (nosys(td, (struct nosys_args *)uap));
547 }
548 #endif
549 #endif /* COMPAT_43 */
550 
551 /*
552  * Initialize signal state for process 0;
553  * set to ignore signals that are ignored by default.
554  */
555 void
556 siginit(p)
557 	struct proc *p;
558 {
559 	register int i;
560 	struct sigacts *ps;
561 
562 	PROC_LOCK(p);
563 	ps = p->p_sigacts;
564 	mtx_lock(&ps->ps_mtx);
565 	for (i = 1; i <= NSIG; i++)
566 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
567 			SIGADDSET(ps->ps_sigignore, i);
568 	mtx_unlock(&ps->ps_mtx);
569 	PROC_UNLOCK(p);
570 }
571 
572 /*
573  * Reset signals for an exec of the specified process.
574  */
575 void
576 execsigs(struct proc *p)
577 {
578 	struct sigacts *ps;
579 	int sig;
580 	struct thread *td;
581 
582 	/*
583 	 * Reset caught signals.  Held signals remain held
584 	 * through td_sigmask (unless they were caught,
585 	 * and are now ignored by default).
586 	 */
587 	PROC_LOCK_ASSERT(p, MA_OWNED);
588 	td = FIRST_THREAD_IN_PROC(p);
589 	ps = p->p_sigacts;
590 	mtx_lock(&ps->ps_mtx);
591 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
592 		sig = sig_ffs(&ps->ps_sigcatch);
593 		SIGDELSET(ps->ps_sigcatch, sig);
594 		if (sigprop(sig) & SA_IGNORE) {
595 			if (sig != SIGCONT)
596 				SIGADDSET(ps->ps_sigignore, sig);
597 			SIGDELSET(p->p_siglist, sig);
598 			/*
599 			 * There is only one thread at this point.
600 			 */
601 			SIGDELSET(td->td_siglist, sig);
602 		}
603 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
604 	}
605 	/*
606 	 * Reset stack state to the user stack.
607 	 * Clear set of signals caught on the signal stack.
608 	 */
609 	td->td_sigstk.ss_flags = SS_DISABLE;
610 	td->td_sigstk.ss_size = 0;
611 	td->td_sigstk.ss_sp = 0;
612 	td->td_pflags &= ~TDP_ALTSTACK;
613 	/*
614 	 * Reset no zombies if child dies flag as Solaris does.
615 	 */
616 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
617 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
618 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
619 	mtx_unlock(&ps->ps_mtx);
620 }
621 
622 /*
623  * kern_sigprocmask()
624  *
625  *	Manipulate signal mask.
626  */
627 int
628 kern_sigprocmask(td, how, set, oset, old)
629 	struct thread *td;
630 	int how;
631 	sigset_t *set, *oset;
632 	int old;
633 {
634 	int error;
635 
636 	PROC_LOCK(td->td_proc);
637 	if (oset != NULL)
638 		*oset = td->td_sigmask;
639 
640 	error = 0;
641 	if (set != NULL) {
642 		switch (how) {
643 		case SIG_BLOCK:
644 			SIG_CANTMASK(*set);
645 			SIGSETOR(td->td_sigmask, *set);
646 			break;
647 		case SIG_UNBLOCK:
648 			SIGSETNAND(td->td_sigmask, *set);
649 			signotify(td);
650 			break;
651 		case SIG_SETMASK:
652 			SIG_CANTMASK(*set);
653 			if (old)
654 				SIGSETLO(td->td_sigmask, *set);
655 			else
656 				td->td_sigmask = *set;
657 			signotify(td);
658 			break;
659 		default:
660 			error = EINVAL;
661 			break;
662 		}
663 	}
664 	PROC_UNLOCK(td->td_proc);
665 	return (error);
666 }
667 
668 /*
669  * sigprocmask() - MP SAFE
670  */
671 
672 #ifndef _SYS_SYSPROTO_H_
673 struct sigprocmask_args {
674 	int	how;
675 	const sigset_t *set;
676 	sigset_t *oset;
677 };
678 #endif
679 int
680 sigprocmask(td, uap)
681 	register struct thread *td;
682 	struct sigprocmask_args *uap;
683 {
684 	sigset_t set, oset;
685 	sigset_t *setp, *osetp;
686 	int error;
687 
688 	setp = (uap->set != NULL) ? &set : NULL;
689 	osetp = (uap->oset != NULL) ? &oset : NULL;
690 	if (setp) {
691 		error = copyin(uap->set, setp, sizeof(set));
692 		if (error)
693 			return (error);
694 	}
695 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
696 	if (osetp && !error) {
697 		error = copyout(osetp, uap->oset, sizeof(oset));
698 	}
699 	return (error);
700 }
701 
702 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
703 /*
704  * osigprocmask() - MP SAFE
705  */
706 #ifndef _SYS_SYSPROTO_H_
707 struct osigprocmask_args {
708 	int	how;
709 	osigset_t mask;
710 };
711 #endif
712 int
713 osigprocmask(td, uap)
714 	register struct thread *td;
715 	struct osigprocmask_args *uap;
716 {
717 	sigset_t set, oset;
718 	int error;
719 
720 	OSIG2SIG(uap->mask, set);
721 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
722 	SIG2OSIG(oset, td->td_retval[0]);
723 	return (error);
724 }
725 #endif /* COMPAT_43 */
726 
727 #ifndef _SYS_SYSPROTO_H_
728 struct sigpending_args {
729 	sigset_t	*set;
730 };
731 #endif
732 /*
733  * MPSAFE
734  */
735 int
736 sigwait(struct thread *td, struct sigwait_args *uap)
737 {
738 	siginfo_t info;
739 	sigset_t set;
740 	int error;
741 
742 	error = copyin(uap->set, &set, sizeof(set));
743 	if (error) {
744 		td->td_retval[0] = error;
745 		return (0);
746 	}
747 
748 	error = kern_sigtimedwait(td, set, &info, NULL);
749 	if (error) {
750 		if (error == ERESTART)
751 			return (error);
752 		td->td_retval[0] = error;
753 		return (0);
754 	}
755 
756 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
757 	/* Repost if we got an error. */
758 	if (error && info.si_signo) {
759 		PROC_LOCK(td->td_proc);
760 		tdsignal(td, info.si_signo, SIGTARGET_TD);
761 		PROC_UNLOCK(td->td_proc);
762 	}
763 	td->td_retval[0] = error;
764 	return (0);
765 }
766 /*
767  * MPSAFE
768  */
769 int
770 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
771 {
772 	struct timespec ts;
773 	struct timespec *timeout;
774 	sigset_t set;
775 	siginfo_t info;
776 	int error;
777 
778 	if (uap->timeout) {
779 		error = copyin(uap->timeout, &ts, sizeof(ts));
780 		if (error)
781 			return (error);
782 
783 		timeout = &ts;
784 	} else
785 		timeout = NULL;
786 
787 	error = copyin(uap->set, &set, sizeof(set));
788 	if (error)
789 		return (error);
790 
791 	error = kern_sigtimedwait(td, set, &info, timeout);
792 	if (error)
793 		return (error);
794 
795 	if (uap->info)
796 		error = copyout(&info, uap->info, sizeof(info));
797 	/* Repost if we got an error. */
798 	if (error && info.si_signo) {
799 		PROC_LOCK(td->td_proc);
800 		tdsignal(td, info.si_signo, SIGTARGET_TD);
801 		PROC_UNLOCK(td->td_proc);
802 	} else {
803 		td->td_retval[0] = info.si_signo;
804 	}
805 	return (error);
806 }
807 
808 /*
809  * MPSAFE
810  */
811 int
812 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
813 {
814 	siginfo_t info;
815 	sigset_t set;
816 	int error;
817 
818 	error = copyin(uap->set, &set, sizeof(set));
819 	if (error)
820 		return (error);
821 
822 	error = kern_sigtimedwait(td, set, &info, NULL);
823 	if (error)
824 		return (error);
825 
826 	if (uap->info)
827 		error = copyout(&info, uap->info, sizeof(info));
828 	/* Repost if we got an error. */
829 	if (error && info.si_signo) {
830 		PROC_LOCK(td->td_proc);
831 		tdsignal(td, info.si_signo, SIGTARGET_TD);
832 		PROC_UNLOCK(td->td_proc);
833 	} else {
834 		td->td_retval[0] = info.si_signo;
835 	}
836 	return (error);
837 }
838 
839 static int
840 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
841     struct timespec *timeout)
842 {
843 	struct sigacts *ps;
844 	sigset_t savedmask, sigset;
845 	struct proc *p;
846 	int error;
847 	int sig;
848 	int hz;
849 	int i;
850 
851 	p = td->td_proc;
852 	error = 0;
853 	sig = 0;
854 	SIG_CANTMASK(waitset);
855 
856 	PROC_LOCK(p);
857 	ps = p->p_sigacts;
858 	savedmask = td->td_sigmask;
859 
860 again:
861 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
862 		if (!SIGISMEMBER(waitset, i))
863 			continue;
864 		if (SIGISMEMBER(td->td_siglist, i)) {
865 			SIGFILLSET(td->td_sigmask);
866 			SIG_CANTMASK(td->td_sigmask);
867 			SIGDELSET(td->td_sigmask, i);
868 			mtx_lock(&ps->ps_mtx);
869 			sig = cursig(td);
870 			i = 0;
871 			mtx_unlock(&ps->ps_mtx);
872 		} else if (SIGISMEMBER(p->p_siglist, i)) {
873 			if (p->p_flag & P_SA) {
874 				p->p_flag |= P_SIGEVENT;
875 				wakeup(&p->p_siglist);
876 			}
877 			SIGDELSET(p->p_siglist, i);
878 			SIGADDSET(td->td_siglist, i);
879 			SIGFILLSET(td->td_sigmask);
880 			SIG_CANTMASK(td->td_sigmask);
881 			SIGDELSET(td->td_sigmask, i);
882 			mtx_lock(&ps->ps_mtx);
883 			sig = cursig(td);
884 			i = 0;
885 			mtx_unlock(&ps->ps_mtx);
886 		}
887 		if (sig) {
888 			td->td_sigmask = savedmask;
889 			signotify(td);
890 			goto out;
891 		}
892 	}
893 	if (error)
894 		goto out;
895 
896 	td->td_sigmask = savedmask;
897 	signotify(td);
898 	sigset = td->td_siglist;
899 	SIGSETOR(sigset, p->p_siglist);
900 	SIGSETAND(sigset, waitset);
901 	if (!SIGISEMPTY(sigset))
902 		goto again;
903 
904 	/*
905 	 * POSIX says this must be checked after looking for pending
906 	 * signals.
907 	 */
908 	if (timeout) {
909 		struct timeval tv;
910 
911 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
912 			error = EINVAL;
913 			goto out;
914 		}
915 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
916 			error = EAGAIN;
917 			goto out;
918 		}
919 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
920 		hz = tvtohz(&tv);
921 	} else
922 		hz = 0;
923 
924 	td->td_waitset = &waitset;
925 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
926 	td->td_waitset = NULL;
927 	if (error == 0) /* surplus wakeup ? */
928 		error = EINTR;
929 	goto again;
930 
931 out:
932 	if (sig) {
933 		sig_t action;
934 
935 		error = 0;
936 		mtx_lock(&ps->ps_mtx);
937 		action = ps->ps_sigact[_SIG_IDX(sig)];
938 		mtx_unlock(&ps->ps_mtx);
939 #ifdef KTRACE
940 		if (KTRPOINT(td, KTR_PSIG))
941 			ktrpsig(sig, action, &td->td_sigmask, 0);
942 #endif
943 		_STOPEVENT(p, S_SIG, sig);
944 
945 		SIGDELSET(td->td_siglist, sig);
946 		info->si_signo = sig;
947 		info->si_code = 0;
948 	}
949 	PROC_UNLOCK(p);
950 	return (error);
951 }
952 
953 /*
954  * MPSAFE
955  */
956 int
957 sigpending(td, uap)
958 	struct thread *td;
959 	struct sigpending_args *uap;
960 {
961 	struct proc *p = td->td_proc;
962 	sigset_t siglist;
963 
964 	PROC_LOCK(p);
965 	siglist = p->p_siglist;
966 	SIGSETOR(siglist, td->td_siglist);
967 	PROC_UNLOCK(p);
968 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
969 }
970 
971 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
972 #ifndef _SYS_SYSPROTO_H_
973 struct osigpending_args {
974 	int	dummy;
975 };
976 #endif
977 /*
978  * MPSAFE
979  */
980 int
981 osigpending(td, uap)
982 	struct thread *td;
983 	struct osigpending_args *uap;
984 {
985 	struct proc *p = td->td_proc;
986 	sigset_t siglist;
987 
988 	PROC_LOCK(p);
989 	siglist = p->p_siglist;
990 	SIGSETOR(siglist, td->td_siglist);
991 	PROC_UNLOCK(p);
992 	SIG2OSIG(siglist, td->td_retval[0]);
993 	return (0);
994 }
995 #endif /* COMPAT_43 */
996 
997 #if defined(COMPAT_43)
998 /*
999  * Generalized interface signal handler, 4.3-compatible.
1000  */
1001 #ifndef _SYS_SYSPROTO_H_
1002 struct osigvec_args {
1003 	int	signum;
1004 	struct	sigvec *nsv;
1005 	struct	sigvec *osv;
1006 };
1007 #endif
1008 /*
1009  * MPSAFE
1010  */
1011 /* ARGSUSED */
1012 int
1013 osigvec(td, uap)
1014 	struct thread *td;
1015 	register struct osigvec_args *uap;
1016 {
1017 	struct sigvec vec;
1018 	struct sigaction nsa, osa;
1019 	register struct sigaction *nsap, *osap;
1020 	int error;
1021 
1022 	if (uap->signum <= 0 || uap->signum >= ONSIG)
1023 		return (EINVAL);
1024 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
1025 	osap = (uap->osv != NULL) ? &osa : NULL;
1026 	if (nsap) {
1027 		error = copyin(uap->nsv, &vec, sizeof(vec));
1028 		if (error)
1029 			return (error);
1030 		nsap->sa_handler = vec.sv_handler;
1031 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1032 		nsap->sa_flags = vec.sv_flags;
1033 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1034 	}
1035 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1036 	if (osap && !error) {
1037 		vec.sv_handler = osap->sa_handler;
1038 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
1039 		vec.sv_flags = osap->sa_flags;
1040 		vec.sv_flags &= ~SA_NOCLDWAIT;
1041 		vec.sv_flags ^= SA_RESTART;
1042 		error = copyout(&vec, uap->osv, sizeof(vec));
1043 	}
1044 	return (error);
1045 }
1046 
1047 #ifndef _SYS_SYSPROTO_H_
1048 struct osigblock_args {
1049 	int	mask;
1050 };
1051 #endif
1052 /*
1053  * MPSAFE
1054  */
1055 int
1056 osigblock(td, uap)
1057 	register struct thread *td;
1058 	struct osigblock_args *uap;
1059 {
1060 	struct proc *p = td->td_proc;
1061 	sigset_t set;
1062 
1063 	OSIG2SIG(uap->mask, set);
1064 	SIG_CANTMASK(set);
1065 	PROC_LOCK(p);
1066 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1067 	SIGSETOR(td->td_sigmask, set);
1068 	PROC_UNLOCK(p);
1069 	return (0);
1070 }
1071 
1072 #ifndef _SYS_SYSPROTO_H_
1073 struct osigsetmask_args {
1074 	int	mask;
1075 };
1076 #endif
1077 /*
1078  * MPSAFE
1079  */
1080 int
1081 osigsetmask(td, uap)
1082 	struct thread *td;
1083 	struct osigsetmask_args *uap;
1084 {
1085 	struct proc *p = td->td_proc;
1086 	sigset_t set;
1087 
1088 	OSIG2SIG(uap->mask, set);
1089 	SIG_CANTMASK(set);
1090 	PROC_LOCK(p);
1091 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1092 	SIGSETLO(td->td_sigmask, set);
1093 	signotify(td);
1094 	PROC_UNLOCK(p);
1095 	return (0);
1096 }
1097 #endif /* COMPAT_43 */
1098 
1099 /*
1100  * Suspend process until signal, providing mask to be set
1101  * in the meantime.
1102  ***** XXXKSE this doesn't make sense under KSE.
1103  ***** Do we suspend the thread or all threads in the process?
1104  ***** How do we suspend threads running NOW on another processor?
1105  */
1106 #ifndef _SYS_SYSPROTO_H_
1107 struct sigsuspend_args {
1108 	const sigset_t *sigmask;
1109 };
1110 #endif
1111 /*
1112  * MPSAFE
1113  */
1114 /* ARGSUSED */
1115 int
1116 sigsuspend(td, uap)
1117 	struct thread *td;
1118 	struct sigsuspend_args *uap;
1119 {
1120 	sigset_t mask;
1121 	int error;
1122 
1123 	error = copyin(uap->sigmask, &mask, sizeof(mask));
1124 	if (error)
1125 		return (error);
1126 	return (kern_sigsuspend(td, mask));
1127 }
1128 
1129 int
1130 kern_sigsuspend(struct thread *td, sigset_t mask)
1131 {
1132 	struct proc *p = td->td_proc;
1133 
1134 	/*
1135 	 * When returning from sigsuspend, we want
1136 	 * the old mask to be restored after the
1137 	 * signal handler has finished.  Thus, we
1138 	 * save it here and mark the sigacts structure
1139 	 * to indicate this.
1140 	 */
1141 	PROC_LOCK(p);
1142 	td->td_oldsigmask = td->td_sigmask;
1143 	td->td_pflags |= TDP_OLDMASK;
1144 	SIG_CANTMASK(mask);
1145 	td->td_sigmask = mask;
1146 	signotify(td);
1147 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1148 		/* void */;
1149 	PROC_UNLOCK(p);
1150 	/* always return EINTR rather than ERESTART... */
1151 	return (EINTR);
1152 }
1153 
1154 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1155 /*
1156  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
1157  * convention: libc stub passes mask, not pointer, to save a copyin.
1158  */
1159 #ifndef _SYS_SYSPROTO_H_
1160 struct osigsuspend_args {
1161 	osigset_t mask;
1162 };
1163 #endif
1164 /*
1165  * MPSAFE
1166  */
1167 /* ARGSUSED */
1168 int
1169 osigsuspend(td, uap)
1170 	struct thread *td;
1171 	struct osigsuspend_args *uap;
1172 {
1173 	struct proc *p = td->td_proc;
1174 	sigset_t mask;
1175 
1176 	PROC_LOCK(p);
1177 	td->td_oldsigmask = td->td_sigmask;
1178 	td->td_pflags |= TDP_OLDMASK;
1179 	OSIG2SIG(uap->mask, mask);
1180 	SIG_CANTMASK(mask);
1181 	SIGSETLO(td->td_sigmask, mask);
1182 	signotify(td);
1183 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1184 		/* void */;
1185 	PROC_UNLOCK(p);
1186 	/* always return EINTR rather than ERESTART... */
1187 	return (EINTR);
1188 }
1189 #endif /* COMPAT_43 */
1190 
1191 #if defined(COMPAT_43)
1192 #ifndef _SYS_SYSPROTO_H_
1193 struct osigstack_args {
1194 	struct	sigstack *nss;
1195 	struct	sigstack *oss;
1196 };
1197 #endif
1198 /*
1199  * MPSAFE
1200  */
1201 /* ARGSUSED */
1202 int
1203 osigstack(td, uap)
1204 	struct thread *td;
1205 	register struct osigstack_args *uap;
1206 {
1207 	struct sigstack nss, oss;
1208 	int error = 0;
1209 
1210 	if (uap->nss != NULL) {
1211 		error = copyin(uap->nss, &nss, sizeof(nss));
1212 		if (error)
1213 			return (error);
1214 	}
1215 	oss.ss_sp = td->td_sigstk.ss_sp;
1216 	oss.ss_onstack = sigonstack(cpu_getstack(td));
1217 	if (uap->nss != NULL) {
1218 		td->td_sigstk.ss_sp = nss.ss_sp;
1219 		td->td_sigstk.ss_size = 0;
1220 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1221 		td->td_pflags |= TDP_ALTSTACK;
1222 	}
1223 	if (uap->oss != NULL)
1224 		error = copyout(&oss, uap->oss, sizeof(oss));
1225 
1226 	return (error);
1227 }
1228 #endif /* COMPAT_43 */
1229 
1230 #ifndef _SYS_SYSPROTO_H_
1231 struct sigaltstack_args {
1232 	stack_t	*ss;
1233 	stack_t	*oss;
1234 };
1235 #endif
1236 /*
1237  * MPSAFE
1238  */
1239 /* ARGSUSED */
1240 int
1241 sigaltstack(td, uap)
1242 	struct thread *td;
1243 	register struct sigaltstack_args *uap;
1244 {
1245 	stack_t ss, oss;
1246 	int error;
1247 
1248 	if (uap->ss != NULL) {
1249 		error = copyin(uap->ss, &ss, sizeof(ss));
1250 		if (error)
1251 			return (error);
1252 	}
1253 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1254 	    (uap->oss != NULL) ? &oss : NULL);
1255 	if (error)
1256 		return (error);
1257 	if (uap->oss != NULL)
1258 		error = copyout(&oss, uap->oss, sizeof(stack_t));
1259 	return (error);
1260 }
1261 
1262 int
1263 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1264 {
1265 	struct proc *p = td->td_proc;
1266 	int oonstack;
1267 
1268 	oonstack = sigonstack(cpu_getstack(td));
1269 
1270 	if (oss != NULL) {
1271 		*oss = td->td_sigstk;
1272 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1273 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1274 	}
1275 
1276 	if (ss != NULL) {
1277 		if (oonstack)
1278 			return (EPERM);
1279 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1280 			return (EINVAL);
1281 		if (!(ss->ss_flags & SS_DISABLE)) {
1282 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1283 				return (ENOMEM);
1284 			}
1285 			td->td_sigstk = *ss;
1286 			td->td_pflags |= TDP_ALTSTACK;
1287 		} else {
1288 			td->td_pflags &= ~TDP_ALTSTACK;
1289 		}
1290 	}
1291 	return (0);
1292 }
1293 
1294 /*
1295  * Common code for kill process group/broadcast kill.
1296  * cp is calling process.
1297  */
1298 static int
1299 killpg1(td, sig, pgid, all)
1300 	register struct thread *td;
1301 	int sig, pgid, all;
1302 {
1303 	register struct proc *p;
1304 	struct pgrp *pgrp;
1305 	int nfound = 0;
1306 
1307 	if (all) {
1308 		/*
1309 		 * broadcast
1310 		 */
1311 		sx_slock(&allproc_lock);
1312 		LIST_FOREACH(p, &allproc, p_list) {
1313 			PROC_LOCK(p);
1314 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1315 			    p == td->td_proc) {
1316 				PROC_UNLOCK(p);
1317 				continue;
1318 			}
1319 			if (p_cansignal(td, p, sig) == 0) {
1320 				nfound++;
1321 				if (sig)
1322 					psignal(p, sig);
1323 			}
1324 			PROC_UNLOCK(p);
1325 		}
1326 		sx_sunlock(&allproc_lock);
1327 	} else {
1328 		sx_slock(&proctree_lock);
1329 		if (pgid == 0) {
1330 			/*
1331 			 * zero pgid means send to my process group.
1332 			 */
1333 			pgrp = td->td_proc->p_pgrp;
1334 			PGRP_LOCK(pgrp);
1335 		} else {
1336 			pgrp = pgfind(pgid);
1337 			if (pgrp == NULL) {
1338 				sx_sunlock(&proctree_lock);
1339 				return (ESRCH);
1340 			}
1341 		}
1342 		sx_sunlock(&proctree_lock);
1343 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1344 			PROC_LOCK(p);
1345 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1346 				PROC_UNLOCK(p);
1347 				continue;
1348 			}
1349 			if (p->p_state == PRS_ZOMBIE) {
1350 				PROC_UNLOCK(p);
1351 				continue;
1352 			}
1353 			if (p_cansignal(td, p, sig) == 0) {
1354 				nfound++;
1355 				if (sig)
1356 					psignal(p, sig);
1357 			}
1358 			PROC_UNLOCK(p);
1359 		}
1360 		PGRP_UNLOCK(pgrp);
1361 	}
1362 	return (nfound ? 0 : ESRCH);
1363 }
1364 
1365 #ifndef _SYS_SYSPROTO_H_
1366 struct kill_args {
1367 	int	pid;
1368 	int	signum;
1369 };
1370 #endif
1371 /*
1372  * MPSAFE
1373  */
1374 /* ARGSUSED */
1375 int
1376 kill(td, uap)
1377 	register struct thread *td;
1378 	register struct kill_args *uap;
1379 {
1380 	register struct proc *p;
1381 	int error;
1382 
1383 	if ((u_int)uap->signum > _SIG_MAXSIG)
1384 		return (EINVAL);
1385 
1386 	if (uap->pid > 0) {
1387 		/* kill single process */
1388 		if ((p = pfind(uap->pid)) == NULL)
1389 			return (ESRCH);
1390 		error = p_cansignal(td, p, uap->signum);
1391 		if (error == 0 && uap->signum)
1392 			psignal(p, uap->signum);
1393 		PROC_UNLOCK(p);
1394 		return (error);
1395 	}
1396 	switch (uap->pid) {
1397 	case -1:		/* broadcast signal */
1398 		return (killpg1(td, uap->signum, 0, 1));
1399 	case 0:			/* signal own process group */
1400 		return (killpg1(td, uap->signum, 0, 0));
1401 	default:		/* negative explicit process group */
1402 		return (killpg1(td, uap->signum, -uap->pid, 0));
1403 	}
1404 	/* NOTREACHED */
1405 }
1406 
1407 #if defined(COMPAT_43)
1408 #ifndef _SYS_SYSPROTO_H_
1409 struct okillpg_args {
1410 	int	pgid;
1411 	int	signum;
1412 };
1413 #endif
1414 /*
1415  * MPSAFE
1416  */
1417 /* ARGSUSED */
1418 int
1419 okillpg(td, uap)
1420 	struct thread *td;
1421 	register struct okillpg_args *uap;
1422 {
1423 
1424 	if ((u_int)uap->signum > _SIG_MAXSIG)
1425 		return (EINVAL);
1426 	return (killpg1(td, uap->signum, uap->pgid, 0));
1427 }
1428 #endif /* COMPAT_43 */
1429 
1430 /*
1431  * Send a signal to a process group.
1432  */
1433 void
1434 gsignal(pgid, sig)
1435 	int pgid, sig;
1436 {
1437 	struct pgrp *pgrp;
1438 
1439 	if (pgid != 0) {
1440 		sx_slock(&proctree_lock);
1441 		pgrp = pgfind(pgid);
1442 		sx_sunlock(&proctree_lock);
1443 		if (pgrp != NULL) {
1444 			pgsignal(pgrp, sig, 0);
1445 			PGRP_UNLOCK(pgrp);
1446 		}
1447 	}
1448 }
1449 
1450 /*
1451  * Send a signal to a process group.  If checktty is 1,
1452  * limit to members which have a controlling terminal.
1453  */
1454 void
1455 pgsignal(pgrp, sig, checkctty)
1456 	struct pgrp *pgrp;
1457 	int sig, checkctty;
1458 {
1459 	register struct proc *p;
1460 
1461 	if (pgrp) {
1462 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1463 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1464 			PROC_LOCK(p);
1465 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
1466 				psignal(p, sig);
1467 			PROC_UNLOCK(p);
1468 		}
1469 	}
1470 }
1471 
1472 /*
1473  * Send a signal caused by a trap to the current thread.
1474  * If it will be caught immediately, deliver it with correct code.
1475  * Otherwise, post it normally.
1476  *
1477  * MPSAFE
1478  */
1479 void
1480 trapsignal(struct thread *td, int sig, u_long code)
1481 {
1482 	struct sigacts *ps;
1483 	struct proc *p;
1484 	siginfo_t siginfo;
1485 	int error;
1486 
1487 	p = td->td_proc;
1488 	if (td->td_pflags & TDP_SA) {
1489 		if (td->td_mailbox == NULL)
1490 			thread_user_enter(p, td);
1491 		PROC_LOCK(p);
1492 		if (td->td_mailbox) {
1493 			SIGDELSET(td->td_sigmask, sig);
1494 			mtx_lock_spin(&sched_lock);
1495 			/*
1496 			 * Force scheduling an upcall, so UTS has chance to
1497 			 * process the signal before thread runs again in
1498 			 * userland.
1499 			 */
1500 			if (td->td_upcall)
1501 				td->td_upcall->ku_flags |= KUF_DOUPCALL;
1502 			mtx_unlock_spin(&sched_lock);
1503 		} else {
1504 			/* UTS caused a sync signal */
1505 			p->p_code = code;	/* XXX for core dump/debugger */
1506 			p->p_sig = sig;		/* XXX to verify code */
1507 			sigexit(td, sig);
1508 		}
1509 	} else {
1510 		PROC_LOCK(p);
1511 	}
1512 	ps = p->p_sigacts;
1513 	mtx_lock(&ps->ps_mtx);
1514 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1515 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1516 		p->p_stats->p_ru.ru_nsignals++;
1517 #ifdef KTRACE
1518 		if (KTRPOINT(curthread, KTR_PSIG))
1519 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1520 			    &td->td_sigmask, code);
1521 #endif
1522 		if (!(td->td_pflags & TDP_SA))
1523 			(*p->p_sysent->sv_sendsig)(
1524 				ps->ps_sigact[_SIG_IDX(sig)], sig,
1525 				&td->td_sigmask, code);
1526 		else {
1527 			cpu_thread_siginfo(sig, code, &siginfo);
1528 			mtx_unlock(&ps->ps_mtx);
1529 			PROC_UNLOCK(p);
1530 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
1531 			    sizeof(siginfo));
1532 			PROC_LOCK(p);
1533 			/* UTS memory corrupted */
1534 			if (error)
1535 				sigexit(td, SIGILL);
1536 			SIGADDSET(td->td_sigmask, sig);
1537 			mtx_lock(&ps->ps_mtx);
1538 		}
1539 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1540 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1541 			SIGADDSET(td->td_sigmask, sig);
1542 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1543 			/*
1544 			 * See kern_sigaction() for origin of this code.
1545 			 */
1546 			SIGDELSET(ps->ps_sigcatch, sig);
1547 			if (sig != SIGCONT &&
1548 			    sigprop(sig) & SA_IGNORE)
1549 				SIGADDSET(ps->ps_sigignore, sig);
1550 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1551 		}
1552 		mtx_unlock(&ps->ps_mtx);
1553 	} else {
1554 		mtx_unlock(&ps->ps_mtx);
1555 		p->p_code = code;	/* XXX for core dump/debugger */
1556 		p->p_sig = sig;		/* XXX to verify code */
1557 		tdsignal(td, sig, SIGTARGET_TD);
1558 	}
1559 	PROC_UNLOCK(p);
1560 }
1561 
1562 static struct thread *
1563 sigtd(struct proc *p, int sig, int prop)
1564 {
1565 	struct thread *td, *signal_td;
1566 
1567 	PROC_LOCK_ASSERT(p, MA_OWNED);
1568 
1569 	/*
1570 	 * First find a thread in sigwait state and signal belongs to
1571 	 * its wait set. POSIX's arguments is that speed of delivering signal
1572 	 * to sigwait thread is faster than delivering signal to user stack.
1573 	 * If we can not find sigwait thread, then find the first thread in
1574 	 * the proc that doesn't have this signal masked, an exception is
1575 	 * if current thread is sending signal to its process, and it does not
1576 	 * mask the signal, it should get the signal, this is another fast
1577 	 * way to deliver signal.
1578 	 */
1579 	signal_td = NULL;
1580 	mtx_lock_spin(&sched_lock);
1581 	FOREACH_THREAD_IN_PROC(p, td) {
1582 		if (td->td_waitset != NULL &&
1583 		    SIGISMEMBER(*(td->td_waitset), sig)) {
1584 				mtx_unlock_spin(&sched_lock);
1585 				return (td);
1586 		}
1587 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
1588 			if (td == curthread)
1589 				signal_td = curthread;
1590 			else if (signal_td == NULL)
1591 				signal_td = td;
1592 		}
1593 	}
1594 	if (signal_td == NULL)
1595 		signal_td = FIRST_THREAD_IN_PROC(p);
1596 	mtx_unlock_spin(&sched_lock);
1597 	return (signal_td);
1598 }
1599 
1600 /*
1601  * Send the signal to the process.  If the signal has an action, the action
1602  * is usually performed by the target process rather than the caller; we add
1603  * the signal to the set of pending signals for the process.
1604  *
1605  * Exceptions:
1606  *   o When a stop signal is sent to a sleeping process that takes the
1607  *     default action, the process is stopped without awakening it.
1608  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1609  *     regardless of the signal action (eg, blocked or ignored).
1610  *
1611  * Other ignored signals are discarded immediately.
1612  *
1613  * MPSAFE
1614  */
1615 void
1616 psignal(struct proc *p, int sig)
1617 {
1618 	struct thread *td;
1619 	int prop;
1620 
1621 	if (!_SIG_VALID(sig))
1622 		panic("psignal(): invalid signal");
1623 
1624 	PROC_LOCK_ASSERT(p, MA_OWNED);
1625 	prop = sigprop(sig);
1626 
1627 	/*
1628 	 * Find a thread to deliver the signal to.
1629 	 */
1630 	td = sigtd(p, sig, prop);
1631 
1632 	tdsignal(td, sig, SIGTARGET_P);
1633 }
1634 
1635 /*
1636  * MPSAFE
1637  */
1638 void
1639 tdsignal(struct thread *td, int sig, sigtarget_t target)
1640 {
1641 	sigset_t saved;
1642 	struct proc *p = td->td_proc;
1643 
1644 	if (p->p_flag & P_SA)
1645 		saved = p->p_siglist;
1646 	do_tdsignal(td, sig, target);
1647 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
1648 		if (SIGSETEQ(saved, p->p_siglist))
1649 			return;
1650 		else {
1651 			/* pending set changed */
1652 			p->p_flag |= P_SIGEVENT;
1653 			wakeup(&p->p_siglist);
1654 		}
1655 	}
1656 }
1657 
1658 static void
1659 do_tdsignal(struct thread *td, int sig, sigtarget_t target)
1660 {
1661 	struct proc *p;
1662 	register sig_t action;
1663 	sigset_t *siglist;
1664 	struct thread *td0;
1665 	register int prop;
1666 	struct sigacts *ps;
1667 
1668 	if (!_SIG_VALID(sig))
1669 		panic("do_tdsignal(): invalid signal");
1670 
1671 	p = td->td_proc;
1672 	ps = p->p_sigacts;
1673 
1674 	PROC_LOCK_ASSERT(p, MA_OWNED);
1675 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1676 
1677 	prop = sigprop(sig);
1678 
1679 	/*
1680 	 * If the signal is blocked and not destined for this thread, then
1681 	 * assign it to the process so that we can find it later in the first
1682 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
1683 	 */
1684 	if (target == SIGTARGET_TD) {
1685 		siglist = &td->td_siglist;
1686 	} else {
1687 		if (!SIGISMEMBER(td->td_sigmask, sig))
1688 			siglist = &td->td_siglist;
1689 		else if (td->td_waitset != NULL &&
1690 			SIGISMEMBER(*(td->td_waitset), sig))
1691 			siglist = &td->td_siglist;
1692 		else
1693 			siglist = &p->p_siglist;
1694 	}
1695 
1696 	/*
1697 	 * If proc is traced, always give parent a chance;
1698 	 * if signal event is tracked by procfs, give *that*
1699 	 * a chance, as well.
1700 	 */
1701 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1702 		action = SIG_DFL;
1703 	} else {
1704 		/*
1705 		 * If the signal is being ignored,
1706 		 * then we forget about it immediately.
1707 		 * (Note: we don't set SIGCONT in ps_sigignore,
1708 		 * and if it is set to SIG_IGN,
1709 		 * action will be SIG_DFL here.)
1710 		 */
1711 		mtx_lock(&ps->ps_mtx);
1712 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
1713 		    (p->p_flag & P_WEXIT)) {
1714 			mtx_unlock(&ps->ps_mtx);
1715 			return;
1716 		}
1717 		if (((td->td_waitset == NULL) &&
1718 		     SIGISMEMBER(td->td_sigmask, sig)) ||
1719 		    ((td->td_waitset != NULL) &&
1720 		     SIGISMEMBER(td->td_sigmask, sig) &&
1721 		     !SIGISMEMBER(*(td->td_waitset), sig)))
1722 			action = SIG_HOLD;
1723 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1724 			action = SIG_CATCH;
1725 		else
1726 			action = SIG_DFL;
1727 		mtx_unlock(&ps->ps_mtx);
1728 	}
1729 
1730 	if (prop & SA_CONT) {
1731 		SIG_STOPSIGMASK(p->p_siglist);
1732 		/*
1733 		 * XXX Should investigate leaving STOP and CONT sigs only in
1734 		 * the proc's siglist.
1735 		 */
1736 		mtx_lock_spin(&sched_lock);
1737 		FOREACH_THREAD_IN_PROC(p, td0)
1738 			SIG_STOPSIGMASK(td0->td_siglist);
1739 		mtx_unlock_spin(&sched_lock);
1740 	}
1741 
1742 	if (prop & SA_STOP) {
1743 		/*
1744 		 * If sending a tty stop signal to a member of an orphaned
1745 		 * process group, discard the signal here if the action
1746 		 * is default; don't stop the process below if sleeping,
1747 		 * and don't clear any pending SIGCONT.
1748 		 */
1749 		if ((prop & SA_TTYSTOP) &&
1750 		    (p->p_pgrp->pg_jobc == 0) &&
1751 		    (action == SIG_DFL))
1752 		        return;
1753 		SIG_CONTSIGMASK(p->p_siglist);
1754 		mtx_lock_spin(&sched_lock);
1755 		FOREACH_THREAD_IN_PROC(p, td0)
1756 			SIG_CONTSIGMASK(td0->td_siglist);
1757 		mtx_unlock_spin(&sched_lock);
1758 		p->p_flag &= ~P_CONTINUED;
1759 	}
1760 
1761 	SIGADDSET(*siglist, sig);
1762 	signotify(td);			/* uses schedlock */
1763 	if (siglist == &td->td_siglist && (td->td_waitset != NULL) &&
1764 	    action != SIG_HOLD) {
1765 		td->td_waitset = NULL;
1766 	}
1767 
1768 	/*
1769 	 * Defer further processing for signals which are held,
1770 	 * except that stopped processes must be continued by SIGCONT.
1771 	 */
1772 	if (action == SIG_HOLD &&
1773 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
1774 		return;
1775 	/*
1776 	 * Some signals have a process-wide effect and a per-thread
1777 	 * component.  Most processing occurs when the process next
1778 	 * tries to cross the user boundary, however there are some
1779 	 * times when processing needs to be done immediatly, such as
1780 	 * waking up threads so that they can cross the user boundary.
1781 	 * We try do the per-process part here.
1782 	 */
1783 	if (P_SHOULDSTOP(p)) {
1784 		/*
1785 		 * The process is in stopped mode. All the threads should be
1786 		 * either winding down or already on the suspended queue.
1787 		 */
1788 		if (p->p_flag & P_TRACED) {
1789 			/*
1790 			 * The traced process is already stopped,
1791 			 * so no further action is necessary.
1792 			 * No signal can restart us.
1793 			 */
1794 			goto out;
1795 		}
1796 
1797 		if (sig == SIGKILL) {
1798 			/*
1799 			 * SIGKILL sets process running.
1800 			 * It will die elsewhere.
1801 			 * All threads must be restarted.
1802 			 */
1803 			p->p_flag &= ~P_STOPPED;
1804 			goto runfast;
1805 		}
1806 
1807 		if (prop & SA_CONT) {
1808 			/*
1809 			 * If SIGCONT is default (or ignored), we continue the
1810 			 * process but don't leave the signal in siglist as
1811 			 * it has no further action.  If SIGCONT is held, we
1812 			 * continue the process and leave the signal in
1813 			 * siglist.  If the process catches SIGCONT, let it
1814 			 * handle the signal itself.  If it isn't waiting on
1815 			 * an event, it goes back to run state.
1816 			 * Otherwise, process goes back to sleep state.
1817 			 */
1818 			p->p_flag &= ~P_STOPPED_SIG;
1819 			p->p_flag |= P_CONTINUED;
1820 			if (action == SIG_DFL) {
1821 				SIGDELSET(*siglist, sig);
1822 			} else if (action == SIG_CATCH) {
1823 				/*
1824 				 * The process wants to catch it so it needs
1825 				 * to run at least one thread, but which one?
1826 				 * It would seem that the answer would be to
1827 				 * run an upcall in the next KSE to run, and
1828 				 * deliver the signal that way. In a NON KSE
1829 				 * process, we need to make sure that the
1830 				 * single thread is runnable asap.
1831 				 * XXXKSE for now however, make them all run.
1832 				 */
1833 				goto runfast;
1834 			}
1835 			/*
1836 			 * The signal is not ignored or caught.
1837 			 */
1838 			mtx_lock_spin(&sched_lock);
1839 			thread_unsuspend(p);
1840 			mtx_unlock_spin(&sched_lock);
1841 			goto out;
1842 		}
1843 
1844 		if (prop & SA_STOP) {
1845 			/*
1846 			 * Already stopped, don't need to stop again
1847 			 * (If we did the shell could get confused).
1848 			 * Just make sure the signal STOP bit set.
1849 			 */
1850 			p->p_flag |= P_STOPPED_SIG;
1851 			SIGDELSET(*siglist, sig);
1852 			goto out;
1853 		}
1854 
1855 		/*
1856 		 * All other kinds of signals:
1857 		 * If a thread is sleeping interruptibly, simulate a
1858 		 * wakeup so that when it is continued it will be made
1859 		 * runnable and can look at the signal.  However, don't make
1860 		 * the PROCESS runnable, leave it stopped.
1861 		 * It may run a bit until it hits a thread_suspend_check().
1862 		 */
1863 		mtx_lock_spin(&sched_lock);
1864 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
1865 			sleepq_abort(td);
1866 		mtx_unlock_spin(&sched_lock);
1867 		goto out;
1868 		/*
1869 		 * Mutexes are short lived. Threads waiting on them will
1870 		 * hit thread_suspend_check() soon.
1871 		 */
1872 	}  else if (p->p_state == PRS_NORMAL) {
1873 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
1874 			!(prop & SA_STOP)) {
1875 			mtx_lock_spin(&sched_lock);
1876 			tdsigwakeup(td, sig, action);
1877 			mtx_unlock_spin(&sched_lock);
1878 			goto out;
1879 		}
1880 		if (prop & SA_STOP) {
1881 			if (p->p_flag & P_PPWAIT)
1882 				goto out;
1883 			p->p_flag |= P_STOPPED_SIG;
1884 			p->p_xstat = sig;
1885 			mtx_lock_spin(&sched_lock);
1886 			FOREACH_THREAD_IN_PROC(p, td0) {
1887 				if (TD_IS_SLEEPING(td0) &&
1888 				    (td0->td_flags & TDF_SINTR) &&
1889 				    !TD_IS_SUSPENDED(td0)) {
1890 					thread_suspend_one(td0);
1891 				} else if (td != td0) {
1892 					td0->td_flags |= TDF_ASTPENDING;
1893 				}
1894 			}
1895 			thread_stopped(p);
1896 			if (p->p_numthreads == p->p_suspcount) {
1897 				SIGDELSET(p->p_siglist, p->p_xstat);
1898 				FOREACH_THREAD_IN_PROC(p, td0)
1899 					SIGDELSET(td0->td_siglist, p->p_xstat);
1900 			}
1901 			mtx_unlock_spin(&sched_lock);
1902 			goto out;
1903 		}
1904 		else
1905 			goto runfast;
1906 		/* NOTREACHED */
1907 	} else {
1908 		/* Not in "NORMAL" state. discard the signal. */
1909 		SIGDELSET(*siglist, sig);
1910 		goto out;
1911 	}
1912 
1913 	/*
1914 	 * The process is not stopped so we need to apply the signal to all the
1915 	 * running threads.
1916 	 */
1917 
1918 runfast:
1919 	mtx_lock_spin(&sched_lock);
1920 	tdsigwakeup(td, sig, action);
1921 	thread_unsuspend(p);
1922 	mtx_unlock_spin(&sched_lock);
1923 out:
1924 	/* If we jump here, sched_lock should not be owned. */
1925 	mtx_assert(&sched_lock, MA_NOTOWNED);
1926 }
1927 
1928 /*
1929  * The force of a signal has been directed against a single
1930  * thread.  We need to see what we can do about knocking it
1931  * out of any sleep it may be in etc.
1932  */
1933 static void
1934 tdsigwakeup(struct thread *td, int sig, sig_t action)
1935 {
1936 	struct proc *p = td->td_proc;
1937 	register int prop;
1938 
1939 	PROC_LOCK_ASSERT(p, MA_OWNED);
1940 	mtx_assert(&sched_lock, MA_OWNED);
1941 	prop = sigprop(sig);
1942 
1943 	/*
1944 	 * Bring the priority of a thread up if we want it to get
1945 	 * killed in this lifetime.
1946 	 */
1947 	if (action == SIG_DFL && (prop & SA_KILL)) {
1948 		if (td->td_priority > PUSER)
1949 			td->td_priority = PUSER;
1950 	}
1951 
1952 	if (TD_ON_SLEEPQ(td)) {
1953 		/*
1954 		 * If thread is sleeping uninterruptibly
1955 		 * we can't interrupt the sleep... the signal will
1956 		 * be noticed when the process returns through
1957 		 * trap() or syscall().
1958 		 */
1959 		if ((td->td_flags & TDF_SINTR) == 0)
1960 			return;
1961 		/*
1962 		 * Process is sleeping and traced.  Make it runnable
1963 		 * so it can discover the signal in issignal() and stop
1964 		 * for its parent.
1965 		 */
1966 		if (p->p_flag & P_TRACED) {
1967 			p->p_flag &= ~P_STOPPED_TRACE;
1968 		} else {
1969 			/*
1970 			 * If SIGCONT is default (or ignored) and process is
1971 			 * asleep, we are finished; the process should not
1972 			 * be awakened.
1973 			 */
1974 			if ((prop & SA_CONT) && action == SIG_DFL) {
1975 				SIGDELSET(p->p_siglist, sig);
1976 				/*
1977 				 * It may be on either list in this state.
1978 				 * Remove from both for now.
1979 				 */
1980 				SIGDELSET(td->td_siglist, sig);
1981 				return;
1982 			}
1983 
1984 			/*
1985 			 * Give low priority threads a better chance to run.
1986 			 */
1987 			if (td->td_priority > PUSER)
1988 				td->td_priority = PUSER;
1989 		}
1990 		sleepq_abort(td);
1991 	} else {
1992 		/*
1993 		 * Other states do nothing with the signal immediately,
1994 		 * other than kicking ourselves if we are running.
1995 		 * It will either never be noticed, or noticed very soon.
1996 		 */
1997 #ifdef SMP
1998 		if (TD_IS_RUNNING(td) && td != curthread)
1999 			forward_signal(td);
2000 #endif
2001 	}
2002 }
2003 
2004 void
2005 ptracestop(struct thread *td, int sig)
2006 {
2007 	struct proc *p = td->td_proc;
2008 
2009 	PROC_LOCK_ASSERT(p, MA_OWNED);
2010 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2011 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
2012 
2013 	p->p_xstat = sig;
2014 	PROC_LOCK(p->p_pptr);
2015 	psignal(p->p_pptr, SIGCHLD);
2016 	PROC_UNLOCK(p->p_pptr);
2017 	stop(p);
2018 	mtx_lock_spin(&sched_lock);
2019 	thread_suspend_one(td);
2020 	PROC_UNLOCK(p);
2021 	DROP_GIANT();
2022 	mi_switch(SW_INVOL, NULL);
2023 	mtx_unlock_spin(&sched_lock);
2024 	PICKUP_GIANT();
2025 }
2026 
2027 /*
2028  * If the current process has received a signal (should be caught or cause
2029  * termination, should interrupt current syscall), return the signal number.
2030  * Stop signals with default action are processed immediately, then cleared;
2031  * they aren't returned.  This is checked after each entry to the system for
2032  * a syscall or trap (though this can usually be done without calling issignal
2033  * by checking the pending signal masks in cursig.) The normal call
2034  * sequence is
2035  *
2036  *	while (sig = cursig(curthread))
2037  *		postsig(sig);
2038  */
2039 static int
2040 issignal(td)
2041 	struct thread *td;
2042 {
2043 	struct proc *p;
2044 	struct sigacts *ps;
2045 	sigset_t sigpending;
2046 	int sig, prop;
2047 	struct thread *td0;
2048 
2049 	p = td->td_proc;
2050 	ps = p->p_sigacts;
2051 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2052 	PROC_LOCK_ASSERT(p, MA_OWNED);
2053 	for (;;) {
2054 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2055 
2056 		sigpending = td->td_siglist;
2057 		SIGSETNAND(sigpending, td->td_sigmask);
2058 
2059 		if (p->p_flag & P_PPWAIT)
2060 			SIG_STOPSIGMASK(sigpending);
2061 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2062 			return (0);
2063 		sig = sig_ffs(&sigpending);
2064 
2065 		if (p->p_stops & S_SIG) {
2066 			mtx_unlock(&ps->ps_mtx);
2067 			stopevent(p, S_SIG, sig);
2068 			mtx_lock(&ps->ps_mtx);
2069 		}
2070 
2071 		/*
2072 		 * We should see pending but ignored signals
2073 		 * only if P_TRACED was on when they were posted.
2074 		 */
2075 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2076 			SIGDELSET(td->td_siglist, sig);
2077 			continue;
2078 		}
2079 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2080 			/*
2081 			 * If traced, always stop.
2082 			 */
2083 			mtx_unlock(&ps->ps_mtx);
2084 			ptracestop(td, sig);
2085 			PROC_LOCK(p);
2086 			mtx_lock(&ps->ps_mtx);
2087 
2088 			/*
2089 			 * If parent wants us to take the signal,
2090 			 * then it will leave it in p->p_xstat;
2091 			 * otherwise we just look for signals again.
2092 			 */
2093 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2094 			sig = p->p_xstat;
2095 			if (sig == 0)
2096 				continue;
2097 
2098 			/*
2099 			 * If the traced bit got turned off, go back up
2100 			 * to the top to rescan signals.  This ensures
2101 			 * that p_sig* and p_sigact are consistent.
2102 			 */
2103 			if ((p->p_flag & P_TRACED) == 0)
2104 				continue;
2105 
2106 			/*
2107 			 * Put the new signal into td_siglist.  If the
2108 			 * signal is being masked, look for other signals.
2109 			 */
2110 			SIGADDSET(td->td_siglist, sig);
2111 			if (SIGISMEMBER(td->td_sigmask, sig))
2112 				continue;
2113 			signotify(td);
2114 		}
2115 
2116 		prop = sigprop(sig);
2117 
2118 		/*
2119 		 * Decide whether the signal should be returned.
2120 		 * Return the signal's number, or fall through
2121 		 * to clear it from the pending mask.
2122 		 */
2123 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2124 
2125 		case (intptr_t)SIG_DFL:
2126 			/*
2127 			 * Don't take default actions on system processes.
2128 			 */
2129 			if (p->p_pid <= 1) {
2130 #ifdef DIAGNOSTIC
2131 				/*
2132 				 * Are you sure you want to ignore SIGSEGV
2133 				 * in init? XXX
2134 				 */
2135 				printf("Process (pid %lu) got signal %d\n",
2136 					(u_long)p->p_pid, sig);
2137 #endif
2138 				break;		/* == ignore */
2139 			}
2140 			/*
2141 			 * If there is a pending stop signal to process
2142 			 * with default action, stop here,
2143 			 * then clear the signal.  However,
2144 			 * if process is member of an orphaned
2145 			 * process group, ignore tty stop signals.
2146 			 */
2147 			if (prop & SA_STOP) {
2148 				if (p->p_flag & P_TRACED ||
2149 		    		    (p->p_pgrp->pg_jobc == 0 &&
2150 				     prop & SA_TTYSTOP))
2151 					break;	/* == ignore */
2152 				mtx_unlock(&ps->ps_mtx);
2153 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2154 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2155 				p->p_flag |= P_STOPPED_SIG;
2156 				p->p_xstat = sig;
2157 				mtx_lock_spin(&sched_lock);
2158 				FOREACH_THREAD_IN_PROC(p, td0) {
2159 					if (TD_IS_SLEEPING(td0) &&
2160 					    (td0->td_flags & TDF_SINTR) &&
2161 					    !TD_IS_SUSPENDED(td0)) {
2162 						thread_suspend_one(td0);
2163 					} else if (td != td0) {
2164 						td0->td_flags |= TDF_ASTPENDING;
2165 					}
2166 				}
2167 				thread_stopped(p);
2168 				thread_suspend_one(td);
2169 				PROC_UNLOCK(p);
2170 				DROP_GIANT();
2171 				mi_switch(SW_INVOL, NULL);
2172 				mtx_unlock_spin(&sched_lock);
2173 				PICKUP_GIANT();
2174 				PROC_LOCK(p);
2175 				mtx_lock(&ps->ps_mtx);
2176 				break;
2177 			} else if (prop & SA_IGNORE) {
2178 				/*
2179 				 * Except for SIGCONT, shouldn't get here.
2180 				 * Default action is to ignore; drop it.
2181 				 */
2182 				break;		/* == ignore */
2183 			} else
2184 				return (sig);
2185 			/*NOTREACHED*/
2186 
2187 		case (intptr_t)SIG_IGN:
2188 			/*
2189 			 * Masking above should prevent us ever trying
2190 			 * to take action on an ignored signal other
2191 			 * than SIGCONT, unless process is traced.
2192 			 */
2193 			if ((prop & SA_CONT) == 0 &&
2194 			    (p->p_flag & P_TRACED) == 0)
2195 				printf("issignal\n");
2196 			break;		/* == ignore */
2197 
2198 		default:
2199 			/*
2200 			 * This signal has an action, let
2201 			 * postsig() process it.
2202 			 */
2203 			return (sig);
2204 		}
2205 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2206 	}
2207 	/* NOTREACHED */
2208 }
2209 
2210 /*
2211  * Put the argument process into the stopped state and notify the parent
2212  * via wakeup.  Signals are handled elsewhere.  The process must not be
2213  * on the run queue.  Must be called with the proc p locked.
2214  */
2215 static void
2216 stop(struct proc *p)
2217 {
2218 
2219 	PROC_LOCK_ASSERT(p, MA_OWNED);
2220 	p->p_flag |= P_STOPPED_SIG;
2221 	p->p_flag &= ~P_WAITED;
2222 	wakeup(p->p_pptr);
2223 }
2224 
2225 /*
2226  * MPSAFE
2227  */
2228 void
2229 thread_stopped(struct proc *p)
2230 {
2231 	struct proc *p1 = curthread->td_proc;
2232 	struct sigacts *ps;
2233 	int n;
2234 
2235 	PROC_LOCK_ASSERT(p, MA_OWNED);
2236 	mtx_assert(&sched_lock, MA_OWNED);
2237 	n = p->p_suspcount;
2238 	if (p == p1)
2239 		n++;
2240 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2241 		mtx_unlock_spin(&sched_lock);
2242 		stop(p);
2243 		PROC_LOCK(p->p_pptr);
2244 		ps = p->p_pptr->p_sigacts;
2245 		mtx_lock(&ps->ps_mtx);
2246 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2247 			mtx_unlock(&ps->ps_mtx);
2248 			psignal(p->p_pptr, SIGCHLD);
2249 		} else
2250 			mtx_unlock(&ps->ps_mtx);
2251 		PROC_UNLOCK(p->p_pptr);
2252 		mtx_lock_spin(&sched_lock);
2253 	}
2254 }
2255 
2256 /*
2257  * Take the action for the specified signal
2258  * from the current set of pending signals.
2259  */
2260 void
2261 postsig(sig)
2262 	register int sig;
2263 {
2264 	struct thread *td = curthread;
2265 	register struct proc *p = td->td_proc;
2266 	struct sigacts *ps;
2267 	sig_t action;
2268 	sigset_t returnmask;
2269 	int code;
2270 
2271 	KASSERT(sig != 0, ("postsig"));
2272 
2273 	PROC_LOCK_ASSERT(p, MA_OWNED);
2274 	ps = p->p_sigacts;
2275 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2276 	SIGDELSET(td->td_siglist, sig);
2277 	action = ps->ps_sigact[_SIG_IDX(sig)];
2278 #ifdef KTRACE
2279 	if (KTRPOINT(td, KTR_PSIG))
2280 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2281 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2282 #endif
2283 	if (p->p_stops & S_SIG) {
2284 		mtx_unlock(&ps->ps_mtx);
2285 		stopevent(p, S_SIG, sig);
2286 		mtx_lock(&ps->ps_mtx);
2287 	}
2288 
2289 	if (!(td->td_pflags & TDP_SA && td->td_mailbox) &&
2290 	    action == SIG_DFL) {
2291 		/*
2292 		 * Default action, where the default is to kill
2293 		 * the process.  (Other cases were ignored above.)
2294 		 */
2295 		mtx_unlock(&ps->ps_mtx);
2296 		sigexit(td, sig);
2297 		/* NOTREACHED */
2298 	} else {
2299 		if (td->td_pflags & TDP_SA && td->td_mailbox) {
2300 			if (sig == SIGKILL) {
2301 				mtx_unlock(&ps->ps_mtx);
2302 				sigexit(td, sig);
2303 			}
2304 		}
2305 
2306 		/*
2307 		 * If we get here, the signal must be caught.
2308 		 */
2309 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2310 		    ("postsig action"));
2311 		/*
2312 		 * Set the new mask value and also defer further
2313 		 * occurrences of this signal.
2314 		 *
2315 		 * Special case: user has done a sigsuspend.  Here the
2316 		 * current mask is not of interest, but rather the
2317 		 * mask from before the sigsuspend is what we want
2318 		 * restored after the signal processing is completed.
2319 		 */
2320 		if (td->td_pflags & TDP_OLDMASK) {
2321 			returnmask = td->td_oldsigmask;
2322 			td->td_pflags &= ~TDP_OLDMASK;
2323 		} else
2324 			returnmask = td->td_sigmask;
2325 
2326 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2327 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2328 			SIGADDSET(td->td_sigmask, sig);
2329 
2330 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2331 			/*
2332 			 * See kern_sigaction() for origin of this code.
2333 			 */
2334 			SIGDELSET(ps->ps_sigcatch, sig);
2335 			if (sig != SIGCONT &&
2336 			    sigprop(sig) & SA_IGNORE)
2337 				SIGADDSET(ps->ps_sigignore, sig);
2338 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2339 		}
2340 		p->p_stats->p_ru.ru_nsignals++;
2341 		if (p->p_sig != sig) {
2342 			code = 0;
2343 		} else {
2344 			code = p->p_code;
2345 			p->p_code = 0;
2346 			p->p_sig = 0;
2347 		}
2348 		if (td->td_pflags & TDP_SA && td->td_mailbox)
2349 			thread_signal_add(curthread, sig);
2350 		else
2351 			(*p->p_sysent->sv_sendsig)(action, sig,
2352 			    &returnmask, code);
2353 	}
2354 }
2355 
2356 /*
2357  * Kill the current process for stated reason.
2358  */
2359 void
2360 killproc(p, why)
2361 	struct proc *p;
2362 	char *why;
2363 {
2364 
2365 	PROC_LOCK_ASSERT(p, MA_OWNED);
2366 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2367 		p, p->p_pid, p->p_comm);
2368 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2369 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2370 	psignal(p, SIGKILL);
2371 }
2372 
2373 /*
2374  * Force the current process to exit with the specified signal, dumping core
2375  * if appropriate.  We bypass the normal tests for masked and caught signals,
2376  * allowing unrecoverable failures to terminate the process without changing
2377  * signal state.  Mark the accounting record with the signal termination.
2378  * If dumping core, save the signal number for the debugger.  Calls exit and
2379  * does not return.
2380  *
2381  * MPSAFE
2382  */
2383 void
2384 sigexit(td, sig)
2385 	struct thread *td;
2386 	int sig;
2387 {
2388 	struct proc *p = td->td_proc;
2389 
2390 	PROC_LOCK_ASSERT(p, MA_OWNED);
2391 	p->p_acflag |= AXSIG;
2392 	if (sigprop(sig) & SA_CORE) {
2393 		p->p_sig = sig;
2394 		/*
2395 		 * Log signals which would cause core dumps
2396 		 * (Log as LOG_INFO to appease those who don't want
2397 		 * these messages.)
2398 		 * XXX : Todo, as well as euid, write out ruid too
2399 		 * Note that coredump() drops proc lock.
2400 		 */
2401 		if (coredump(td) == 0)
2402 			sig |= WCOREFLAG;
2403 		if (kern_logsigexit)
2404 			log(LOG_INFO,
2405 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
2406 			    p->p_pid, p->p_comm,
2407 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
2408 			    sig &~ WCOREFLAG,
2409 			    sig & WCOREFLAG ? " (core dumped)" : "");
2410 	} else
2411 		PROC_UNLOCK(p);
2412 	exit1(td, W_EXITCODE(0, sig));
2413 	/* NOTREACHED */
2414 }
2415 
2416 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2417 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2418 	      sizeof(corefilename), "process corefile name format string");
2419 
2420 /*
2421  * expand_name(name, uid, pid)
2422  * Expand the name described in corefilename, using name, uid, and pid.
2423  * corefilename is a printf-like string, with three format specifiers:
2424  *	%N	name of process ("name")
2425  *	%P	process id (pid)
2426  *	%U	user id (uid)
2427  * For example, "%N.core" is the default; they can be disabled completely
2428  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2429  * This is controlled by the sysctl variable kern.corefile (see above).
2430  */
2431 
2432 static char *
2433 expand_name(name, uid, pid)
2434 	const char *name;
2435 	uid_t uid;
2436 	pid_t pid;
2437 {
2438 	const char *format, *appendstr;
2439 	char *temp;
2440 	char buf[11];		/* Buffer for pid/uid -- max 4B */
2441 	size_t i, l, n;
2442 
2443 	format = corefilename;
2444 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2445 	if (temp == NULL)
2446 		return (NULL);
2447 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2448 		switch (format[i]) {
2449 		case '%':	/* Format character */
2450 			i++;
2451 			switch (format[i]) {
2452 			case '%':
2453 				appendstr = "%";
2454 				break;
2455 			case 'N':	/* process name */
2456 				appendstr = name;
2457 				break;
2458 			case 'P':	/* process id */
2459 				sprintf(buf, "%u", pid);
2460 				appendstr = buf;
2461 				break;
2462 			case 'U':	/* user id */
2463 				sprintf(buf, "%u", uid);
2464 				appendstr = buf;
2465 				break;
2466 			default:
2467 				appendstr = "";
2468 			  	log(LOG_ERR,
2469 				    "Unknown format character %c in `%s'\n",
2470 				    format[i], format);
2471 			}
2472 			l = strlen(appendstr);
2473 			if ((n + l) >= MAXPATHLEN)
2474 				goto toolong;
2475 			memcpy(temp + n, appendstr, l);
2476 			n += l;
2477 			break;
2478 		default:
2479 			temp[n++] = format[i];
2480 		}
2481 	}
2482 	if (format[i] != '\0')
2483 		goto toolong;
2484 	return (temp);
2485 toolong:
2486 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2487 	    (long)pid, name, (u_long)uid);
2488 	free(temp, M_TEMP);
2489 	return (NULL);
2490 }
2491 
2492 /*
2493  * Dump a process' core.  The main routine does some
2494  * policy checking, and creates the name of the coredump;
2495  * then it passes on a vnode and a size limit to the process-specific
2496  * coredump routine if there is one; if there _is not_ one, it returns
2497  * ENOSYS; otherwise it returns the error from the process-specific routine.
2498  */
2499 
2500 static int
2501 coredump(struct thread *td)
2502 {
2503 	struct proc *p = td->td_proc;
2504 	register struct vnode *vp;
2505 	register struct ucred *cred = td->td_ucred;
2506 	struct flock lf;
2507 	struct nameidata nd;
2508 	struct vattr vattr;
2509 	int error, error1, flags, locked;
2510 	struct mount *mp;
2511 	char *name;			/* name of corefile */
2512 	off_t limit;
2513 
2514 	PROC_LOCK_ASSERT(p, MA_OWNED);
2515 	_STOPEVENT(p, S_CORE, 0);
2516 
2517 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2518 		PROC_UNLOCK(p);
2519 		return (EFAULT);
2520 	}
2521 
2522 	/*
2523 	 * Note that the bulk of limit checking is done after
2524 	 * the corefile is created.  The exception is if the limit
2525 	 * for corefiles is 0, in which case we don't bother
2526 	 * creating the corefile at all.  This layout means that
2527 	 * a corefile is truncated instead of not being created,
2528 	 * if it is larger than the limit.
2529 	 */
2530 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2531 	PROC_UNLOCK(p);
2532 	if (limit == 0)
2533 		return (EFBIG);
2534 
2535 	mtx_lock(&Giant);
2536 restart:
2537 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2538 	if (name == NULL) {
2539 		mtx_unlock(&Giant);
2540 		return (EINVAL);
2541 	}
2542 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2543 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2544 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2545 	free(name, M_TEMP);
2546 	if (error) {
2547 		mtx_unlock(&Giant);
2548 		return (error);
2549 	}
2550 	NDFREE(&nd, NDF_ONLY_PNBUF);
2551 	vp = nd.ni_vp;
2552 
2553 	/* Don't dump to non-regular files or files with links. */
2554 	if (vp->v_type != VREG ||
2555 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2556 		VOP_UNLOCK(vp, 0, td);
2557 		error = EFAULT;
2558 		goto out;
2559 	}
2560 
2561 	VOP_UNLOCK(vp, 0, td);
2562 	lf.l_whence = SEEK_SET;
2563 	lf.l_start = 0;
2564 	lf.l_len = 0;
2565 	lf.l_type = F_WRLCK;
2566 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2567 
2568 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2569 		lf.l_type = F_UNLCK;
2570 		if (locked)
2571 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2572 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2573 			return (error);
2574 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2575 			return (error);
2576 		goto restart;
2577 	}
2578 
2579 	VATTR_NULL(&vattr);
2580 	vattr.va_size = 0;
2581 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2582 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2583 	VOP_SETATTR(vp, &vattr, cred, td);
2584 	VOP_UNLOCK(vp, 0, td);
2585 	PROC_LOCK(p);
2586 	p->p_acflag |= ACORE;
2587 	PROC_UNLOCK(p);
2588 
2589 	error = p->p_sysent->sv_coredump ?
2590 	  p->p_sysent->sv_coredump(td, vp, limit) :
2591 	  ENOSYS;
2592 
2593 	if (locked) {
2594 		lf.l_type = F_UNLCK;
2595 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2596 	}
2597 	vn_finished_write(mp);
2598 out:
2599 	error1 = vn_close(vp, FWRITE, cred, td);
2600 	mtx_unlock(&Giant);
2601 	if (error == 0)
2602 		error = error1;
2603 	return (error);
2604 }
2605 
2606 /*
2607  * Nonexistent system call-- signal process (may want to handle it).
2608  * Flag error in case process won't see signal immediately (blocked or ignored).
2609  */
2610 #ifndef _SYS_SYSPROTO_H_
2611 struct nosys_args {
2612 	int	dummy;
2613 };
2614 #endif
2615 /*
2616  * MPSAFE
2617  */
2618 /* ARGSUSED */
2619 int
2620 nosys(td, args)
2621 	struct thread *td;
2622 	struct nosys_args *args;
2623 {
2624 	struct proc *p = td->td_proc;
2625 
2626 	PROC_LOCK(p);
2627 	psignal(p, SIGSYS);
2628 	PROC_UNLOCK(p);
2629 	return (ENOSYS);
2630 }
2631 
2632 /*
2633  * Send a SIGIO or SIGURG signal to a process or process group using
2634  * stored credentials rather than those of the current process.
2635  */
2636 void
2637 pgsigio(sigiop, sig, checkctty)
2638 	struct sigio **sigiop;
2639 	int sig, checkctty;
2640 {
2641 	struct sigio *sigio;
2642 
2643 	SIGIO_LOCK();
2644 	sigio = *sigiop;
2645 	if (sigio == NULL) {
2646 		SIGIO_UNLOCK();
2647 		return;
2648 	}
2649 	if (sigio->sio_pgid > 0) {
2650 		PROC_LOCK(sigio->sio_proc);
2651 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2652 			psignal(sigio->sio_proc, sig);
2653 		PROC_UNLOCK(sigio->sio_proc);
2654 	} else if (sigio->sio_pgid < 0) {
2655 		struct proc *p;
2656 
2657 		PGRP_LOCK(sigio->sio_pgrp);
2658 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2659 			PROC_LOCK(p);
2660 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2661 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2662 				psignal(p, sig);
2663 			PROC_UNLOCK(p);
2664 		}
2665 		PGRP_UNLOCK(sigio->sio_pgrp);
2666 	}
2667 	SIGIO_UNLOCK();
2668 }
2669 
2670 static int
2671 filt_sigattach(struct knote *kn)
2672 {
2673 	struct proc *p = curproc;
2674 
2675 	kn->kn_ptr.p_proc = p;
2676 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2677 
2678 	PROC_LOCK(p);
2679 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2680 	PROC_UNLOCK(p);
2681 
2682 	return (0);
2683 }
2684 
2685 static void
2686 filt_sigdetach(struct knote *kn)
2687 {
2688 	struct proc *p = kn->kn_ptr.p_proc;
2689 
2690 	PROC_LOCK(p);
2691 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2692 	PROC_UNLOCK(p);
2693 }
2694 
2695 /*
2696  * signal knotes are shared with proc knotes, so we apply a mask to
2697  * the hint in order to differentiate them from process hints.  This
2698  * could be avoided by using a signal-specific knote list, but probably
2699  * isn't worth the trouble.
2700  */
2701 static int
2702 filt_signal(struct knote *kn, long hint)
2703 {
2704 
2705 	if (hint & NOTE_SIGNAL) {
2706 		hint &= ~NOTE_SIGNAL;
2707 
2708 		if (kn->kn_id == hint)
2709 			kn->kn_data++;
2710 	}
2711 	return (kn->kn_data != 0);
2712 }
2713 
2714 struct sigacts *
2715 sigacts_alloc(void)
2716 {
2717 	struct sigacts *ps;
2718 
2719 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
2720 	ps->ps_refcnt = 1;
2721 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
2722 	return (ps);
2723 }
2724 
2725 void
2726 sigacts_free(struct sigacts *ps)
2727 {
2728 
2729 	mtx_lock(&ps->ps_mtx);
2730 	ps->ps_refcnt--;
2731 	if (ps->ps_refcnt == 0) {
2732 		mtx_destroy(&ps->ps_mtx);
2733 		free(ps, M_SUBPROC);
2734 	} else
2735 		mtx_unlock(&ps->ps_mtx);
2736 }
2737 
2738 struct sigacts *
2739 sigacts_hold(struct sigacts *ps)
2740 {
2741 	mtx_lock(&ps->ps_mtx);
2742 	ps->ps_refcnt++;
2743 	mtx_unlock(&ps->ps_mtx);
2744 	return (ps);
2745 }
2746 
2747 void
2748 sigacts_copy(struct sigacts *dest, struct sigacts *src)
2749 {
2750 
2751 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
2752 	mtx_lock(&src->ps_mtx);
2753 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
2754 	mtx_unlock(&src->ps_mtx);
2755 }
2756 
2757 int
2758 sigacts_shared(struct sigacts *ps)
2759 {
2760 	int shared;
2761 
2762 	mtx_lock(&ps->ps_mtx);
2763 	shared = ps->ps_refcnt > 1;
2764 	mtx_unlock(&ps->ps_mtx);
2765 	return (shared);
2766 }
2767