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