xref: /freebsd/sys/kern/kern_sig.c (revision 52ec752989b2e6d4e9a59a8ff25d8ff596d85e62)
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_compat.h"
45 #include "opt_ktrace.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/acct.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kse.h>
57 #include <sys/ktr.h>
58 #include <sys/ktrace.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/proc.h>
64 #include <sys/pioctl.h>
65 #include <sys/resourcevar.h>
66 #include <sys/smp.h>
67 #include <sys/stat.h>
68 #include <sys/sx.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
72 #include <sys/syslog.h>
73 #include <sys/sysproto.h>
74 #include <sys/unistd.h>
75 #include <sys/wait.h>
76 
77 #include <machine/cpu.h>
78 
79 #if defined (__alpha__) && !defined(COMPAT_43)
80 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
81 #endif
82 
83 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
84 
85 static int	coredump(struct thread *);
86 static char	*expand_name(const char *, uid_t, pid_t);
87 static int	killpg1(struct thread *td, int sig, int pgid, int all);
88 static int	issignal(struct thread *p);
89 static int	sigprop(int sig);
90 static void	stop(struct proc *);
91 static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
92 static int	filt_sigattach(struct knote *kn);
93 static void	filt_sigdetach(struct knote *kn);
94 static int	filt_signal(struct knote *kn, long hint);
95 static struct thread *sigtd(struct proc *p, int sig, int prop);
96 static int	kern_sigtimedwait(struct thread *td, sigset_t set,
97 				siginfo_t *info, struct timespec *timeout);
98 static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
99 
100 struct filterops sig_filtops =
101 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
102 
103 static int	kern_logsigexit = 1;
104 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
105     &kern_logsigexit, 0,
106     "Log processes quitting on abnormal signals to syslog(3)");
107 
108 /*
109  * Policy -- Can ucred cr1 send SIGIO to process cr2?
110  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
111  * in the right situations.
112  */
113 #define CANSIGIO(cr1, cr2) \
114 	((cr1)->cr_uid == 0 || \
115 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
116 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
117 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
118 	    (cr1)->cr_uid == (cr2)->cr_uid)
119 
120 int sugid_coredump;
121 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
122     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123 
124 static int	do_coredump = 1;
125 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
126 	&do_coredump, 0, "Enable/Disable coredumps");
127 
128 /*
129  * Signal properties and actions.
130  * The array below categorizes the signals and their default actions
131  * according to the following properties:
132  */
133 #define	SA_KILL		0x01		/* terminates process by default */
134 #define	SA_CORE		0x02		/* ditto and coredumps */
135 #define	SA_STOP		0x04		/* suspend process */
136 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
137 #define	SA_IGNORE	0x10		/* ignore by default */
138 #define	SA_CONT		0x20		/* continue if suspended */
139 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
140 #define	SA_PROC		0x80		/* deliverable to any thread */
141 
142 static int sigproptbl[NSIG] = {
143         SA_KILL|SA_PROC,		/* SIGHUP */
144         SA_KILL|SA_PROC,		/* SIGINT */
145         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
146         SA_KILL|SA_CORE,		/* SIGILL */
147         SA_KILL|SA_CORE,		/* SIGTRAP */
148         SA_KILL|SA_CORE,		/* SIGABRT */
149         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
150         SA_KILL|SA_CORE,		/* SIGFPE */
151         SA_KILL|SA_PROC,		/* SIGKILL */
152         SA_KILL|SA_CORE,		/* SIGBUS */
153         SA_KILL|SA_CORE,		/* SIGSEGV */
154         SA_KILL|SA_CORE,		/* SIGSYS */
155         SA_KILL|SA_PROC,		/* SIGPIPE */
156         SA_KILL|SA_PROC,		/* SIGALRM */
157         SA_KILL|SA_PROC,		/* SIGTERM */
158         SA_IGNORE|SA_PROC,		/* SIGURG */
159         SA_STOP|SA_PROC,		/* SIGSTOP */
160         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
161         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
162         SA_IGNORE|SA_PROC,		/* SIGCHLD */
163         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
164         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
165         SA_IGNORE|SA_PROC,		/* SIGIO */
166         SA_KILL,			/* SIGXCPU */
167         SA_KILL,			/* SIGXFSZ */
168         SA_KILL|SA_PROC,		/* SIGVTALRM */
169         SA_KILL|SA_PROC,		/* SIGPROF */
170         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
171         SA_IGNORE|SA_PROC,		/* SIGINFO */
172         SA_KILL|SA_PROC,		/* SIGUSR1 */
173         SA_KILL|SA_PROC,		/* SIGUSR2 */
174 };
175 
176 /*
177  * Determine signal that should be delivered to process p, the current
178  * process, 0 if none.  If there is a pending stop signal with default
179  * action, the process stops in issignal().
180  * XXXKSE   the check for a pending stop is not done under KSE
181  *
182  * MP SAFE.
183  */
184 int
185 cursig(struct thread *td)
186 {
187 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
188 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
189 	mtx_assert(&sched_lock, MA_NOTOWNED);
190 	return (SIGPENDING(td) ? issignal(td) : 0);
191 }
192 
193 /*
194  * Arrange for ast() to handle unmasked pending signals on return to user
195  * mode.  This must be called whenever a signal is added to td_siglist or
196  * unmasked in td_sigmask.
197  */
198 void
199 signotify(struct thread *td)
200 {
201 	struct proc *p;
202 	sigset_t set, saved;
203 
204 	p = td->td_proc;
205 
206 	PROC_LOCK_ASSERT(p, MA_OWNED);
207 
208 	/*
209 	 * If our mask changed we may have to move signal that were
210 	 * previously masked by all threads to our siglist.
211 	 */
212 	set = p->p_siglist;
213 	if (p->p_flag & P_SA)
214 		saved = p->p_siglist;
215 	SIGSETNAND(set, td->td_sigmask);
216 	SIGSETNAND(p->p_siglist, set);
217 	SIGSETOR(td->td_siglist, set);
218 
219 	if (SIGPENDING(td)) {
220 		mtx_lock_spin(&sched_lock);
221 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
222 		mtx_unlock_spin(&sched_lock);
223 	}
224 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
225 		if (SIGSETEQ(saved, p->p_siglist))
226 			return;
227 		else {
228 			/* pending set changed */
229 			p->p_flag |= P_SIGEVENT;
230 			wakeup(&p->p_siglist);
231 		}
232 	}
233 }
234 
235 int
236 sigonstack(size_t sp)
237 {
238 	struct thread *td = curthread;
239 
240 	return ((td->td_pflags & TDP_ALTSTACK) ?
241 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
242 	    ((td->td_sigstk.ss_size == 0) ?
243 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
244 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
245 #else
246 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
247 #endif
248 	    : 0);
249 }
250 
251 static __inline int
252 sigprop(int sig)
253 {
254 
255 	if (sig > 0 && sig < NSIG)
256 		return (sigproptbl[_SIG_IDX(sig)]);
257 	return (0);
258 }
259 
260 int
261 sig_ffs(sigset_t *set)
262 {
263 	int i;
264 
265 	for (i = 0; i < _SIG_WORDS; i++)
266 		if (set->__bits[i])
267 			return (ffs(set->__bits[i]) + (i * 32));
268 	return (0);
269 }
270 
271 /*
272  * kern_sigaction
273  * sigaction
274  * freebsd4_sigaction
275  * osigaction
276  *
277  * MPSAFE
278  */
279 int
280 kern_sigaction(td, sig, act, oact, flags)
281 	struct thread *td;
282 	register int sig;
283 	struct sigaction *act, *oact;
284 	int flags;
285 {
286 	struct sigacts *ps;
287 	struct thread *td0;
288 	struct proc *p = td->td_proc;
289 
290 	if (!_SIG_VALID(sig))
291 		return (EINVAL);
292 
293 	PROC_LOCK(p);
294 	ps = p->p_sigacts;
295 	mtx_lock(&ps->ps_mtx);
296 	if (oact) {
297 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
298 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
299 		oact->sa_flags = 0;
300 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
301 			oact->sa_flags |= SA_ONSTACK;
302 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
303 			oact->sa_flags |= SA_RESTART;
304 		if (SIGISMEMBER(ps->ps_sigreset, sig))
305 			oact->sa_flags |= SA_RESETHAND;
306 		if (SIGISMEMBER(ps->ps_signodefer, sig))
307 			oact->sa_flags |= SA_NODEFER;
308 		if (SIGISMEMBER(ps->ps_siginfo, sig))
309 			oact->sa_flags |= SA_SIGINFO;
310 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
311 			oact->sa_flags |= SA_NOCLDSTOP;
312 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
313 			oact->sa_flags |= SA_NOCLDWAIT;
314 	}
315 	if (act) {
316 		if ((sig == SIGKILL || sig == SIGSTOP) &&
317 		    act->sa_handler != SIG_DFL) {
318 			mtx_unlock(&ps->ps_mtx);
319 			PROC_UNLOCK(p);
320 			return (EINVAL);
321 		}
322 
323 		/*
324 		 * Change setting atomically.
325 		 */
326 
327 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
328 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
329 		if (act->sa_flags & SA_SIGINFO) {
330 			ps->ps_sigact[_SIG_IDX(sig)] =
331 			    (__sighandler_t *)act->sa_sigaction;
332 			SIGADDSET(ps->ps_siginfo, sig);
333 		} else {
334 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
335 			SIGDELSET(ps->ps_siginfo, sig);
336 		}
337 		if (!(act->sa_flags & SA_RESTART))
338 			SIGADDSET(ps->ps_sigintr, sig);
339 		else
340 			SIGDELSET(ps->ps_sigintr, sig);
341 		if (act->sa_flags & SA_ONSTACK)
342 			SIGADDSET(ps->ps_sigonstack, sig);
343 		else
344 			SIGDELSET(ps->ps_sigonstack, sig);
345 		if (act->sa_flags & SA_RESETHAND)
346 			SIGADDSET(ps->ps_sigreset, sig);
347 		else
348 			SIGDELSET(ps->ps_sigreset, sig);
349 		if (act->sa_flags & SA_NODEFER)
350 			SIGADDSET(ps->ps_signodefer, sig);
351 		else
352 			SIGDELSET(ps->ps_signodefer, sig);
353 #ifdef COMPAT_SUNOS
354 		if (act->sa_flags & SA_USERTRAMP)
355 			SIGADDSET(ps->ps_usertramp, sig);
356 		else
357 			SIGDELSET(ps->ps_usertramp, sig);
358 #endif
359 		if (sig == SIGCHLD) {
360 			if (act->sa_flags & SA_NOCLDSTOP)
361 				ps->ps_flag |= PS_NOCLDSTOP;
362 			else
363 				ps->ps_flag &= ~PS_NOCLDSTOP;
364 			if (act->sa_flags & SA_NOCLDWAIT) {
365 				/*
366 				 * Paranoia: since SA_NOCLDWAIT is implemented
367 				 * by reparenting the dying child to PID 1 (and
368 				 * trust it to reap the zombie), PID 1 itself
369 				 * is forbidden to set SA_NOCLDWAIT.
370 				 */
371 				if (p->p_pid == 1)
372 					ps->ps_flag &= ~PS_NOCLDWAIT;
373 				else
374 					ps->ps_flag |= PS_NOCLDWAIT;
375 			} else
376 				ps->ps_flag &= ~PS_NOCLDWAIT;
377 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
378 				ps->ps_flag |= PS_CLDSIGIGN;
379 			else
380 				ps->ps_flag &= ~PS_CLDSIGIGN;
381 		}
382 		/*
383 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
384 		 * and for signals set to SIG_DFL where the default is to
385 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
386 		 * have to restart the process.
387 		 */
388 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
389 		    (sigprop(sig) & SA_IGNORE &&
390 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
391 			if ((p->p_flag & P_SA) &&
392 			     SIGISMEMBER(p->p_siglist, sig)) {
393 				p->p_flag |= P_SIGEVENT;
394 				wakeup(&p->p_siglist);
395 			}
396 			/* never to be seen again */
397 			SIGDELSET(p->p_siglist, sig);
398 			mtx_lock_spin(&sched_lock);
399 			FOREACH_THREAD_IN_PROC(p, td0)
400 				SIGDELSET(td0->td_siglist, sig);
401 			mtx_unlock_spin(&sched_lock);
402 			if (sig != SIGCONT)
403 				/* easier in psignal */
404 				SIGADDSET(ps->ps_sigignore, sig);
405 			SIGDELSET(ps->ps_sigcatch, sig);
406 		} else {
407 			SIGDELSET(ps->ps_sigignore, sig);
408 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
409 				SIGDELSET(ps->ps_sigcatch, sig);
410 			else
411 				SIGADDSET(ps->ps_sigcatch, sig);
412 		}
413 #ifdef COMPAT_FREEBSD4
414 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
415 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
416 		    (flags & KSA_FREEBSD4) == 0)
417 			SIGDELSET(ps->ps_freebsd4, sig);
418 		else
419 			SIGADDSET(ps->ps_freebsd4, sig);
420 #endif
421 #ifdef COMPAT_43
422 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
423 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
424 		    (flags & KSA_OSIGSET) == 0)
425 			SIGDELSET(ps->ps_osigset, sig);
426 		else
427 			SIGADDSET(ps->ps_osigset, sig);
428 #endif
429 	}
430 	mtx_unlock(&ps->ps_mtx);
431 	PROC_UNLOCK(p);
432 	return (0);
433 }
434 
435 #ifndef _SYS_SYSPROTO_H_
436 struct sigaction_args {
437 	int	sig;
438 	struct	sigaction *act;
439 	struct	sigaction *oact;
440 };
441 #endif
442 /*
443  * MPSAFE
444  */
445 int
446 sigaction(td, uap)
447 	struct thread *td;
448 	register struct sigaction_args *uap;
449 {
450 	struct sigaction act, oact;
451 	register struct sigaction *actp, *oactp;
452 	int error;
453 
454 	actp = (uap->act != NULL) ? &act : NULL;
455 	oactp = (uap->oact != NULL) ? &oact : NULL;
456 	if (actp) {
457 		error = copyin(uap->act, actp, sizeof(act));
458 		if (error)
459 			return (error);
460 	}
461 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
462 	if (oactp && !error)
463 		error = copyout(oactp, uap->oact, sizeof(oact));
464 	return (error);
465 }
466 
467 #ifdef COMPAT_FREEBSD4
468 #ifndef _SYS_SYSPROTO_H_
469 struct freebsd4_sigaction_args {
470 	int	sig;
471 	struct	sigaction *act;
472 	struct	sigaction *oact;
473 };
474 #endif
475 /*
476  * MPSAFE
477  */
478 int
479 freebsd4_sigaction(td, uap)
480 	struct thread *td;
481 	register struct freebsd4_sigaction_args *uap;
482 {
483 	struct sigaction act, oact;
484 	register struct sigaction *actp, *oactp;
485 	int error;
486 
487 
488 	actp = (uap->act != NULL) ? &act : NULL;
489 	oactp = (uap->oact != NULL) ? &oact : NULL;
490 	if (actp) {
491 		error = copyin(uap->act, actp, sizeof(act));
492 		if (error)
493 			return (error);
494 	}
495 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
496 	if (oactp && !error)
497 		error = copyout(oactp, uap->oact, sizeof(oact));
498 	return (error);
499 }
500 #endif	/* COMAPT_FREEBSD4 */
501 
502 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
503 #ifndef _SYS_SYSPROTO_H_
504 struct osigaction_args {
505 	int	signum;
506 	struct	osigaction *nsa;
507 	struct	osigaction *osa;
508 };
509 #endif
510 /*
511  * MPSAFE
512  */
513 int
514 osigaction(td, uap)
515 	struct thread *td;
516 	register struct osigaction_args *uap;
517 {
518 	struct osigaction sa;
519 	struct sigaction nsa, osa;
520 	register struct sigaction *nsap, *osap;
521 	int error;
522 
523 	if (uap->signum <= 0 || uap->signum >= ONSIG)
524 		return (EINVAL);
525 
526 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
527 	osap = (uap->osa != NULL) ? &osa : NULL;
528 
529 	if (nsap) {
530 		error = copyin(uap->nsa, &sa, sizeof(sa));
531 		if (error)
532 			return (error);
533 		nsap->sa_handler = sa.sa_handler;
534 		nsap->sa_flags = sa.sa_flags;
535 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
536 	}
537 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
538 	if (osap && !error) {
539 		sa.sa_handler = osap->sa_handler;
540 		sa.sa_flags = osap->sa_flags;
541 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
542 		error = copyout(&sa, uap->osa, sizeof(sa));
543 	}
544 	return (error);
545 }
546 
547 #if !defined(__i386__) && !defined(__alpha__)
548 /* Avoid replicating the same stub everywhere */
549 int
550 osigreturn(td, uap)
551 	struct thread *td;
552 	struct osigreturn_args *uap;
553 {
554 
555 	return (nosys(td, (struct nosys_args *)uap));
556 }
557 #endif
558 #endif /* COMPAT_43 */
559 
560 /*
561  * Initialize signal state for process 0;
562  * set to ignore signals that are ignored by default.
563  */
564 void
565 siginit(p)
566 	struct proc *p;
567 {
568 	register int i;
569 	struct sigacts *ps;
570 
571 	PROC_LOCK(p);
572 	ps = p->p_sigacts;
573 	mtx_lock(&ps->ps_mtx);
574 	for (i = 1; i <= NSIG; i++)
575 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
576 			SIGADDSET(ps->ps_sigignore, i);
577 	mtx_unlock(&ps->ps_mtx);
578 	PROC_UNLOCK(p);
579 }
580 
581 /*
582  * Reset signals for an exec of the specified process.
583  */
584 void
585 execsigs(struct proc *p)
586 {
587 	struct sigacts *ps;
588 	int sig;
589 	struct thread *td;
590 
591 	/*
592 	 * Reset caught signals.  Held signals remain held
593 	 * through td_sigmask (unless they were caught,
594 	 * and are now ignored by default).
595 	 */
596 	PROC_LOCK_ASSERT(p, MA_OWNED);
597 	td = FIRST_THREAD_IN_PROC(p);
598 	ps = p->p_sigacts;
599 	mtx_lock(&ps->ps_mtx);
600 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
601 		sig = sig_ffs(&ps->ps_sigcatch);
602 		SIGDELSET(ps->ps_sigcatch, sig);
603 		if (sigprop(sig) & SA_IGNORE) {
604 			if (sig != SIGCONT)
605 				SIGADDSET(ps->ps_sigignore, sig);
606 			SIGDELSET(p->p_siglist, sig);
607 			/*
608 			 * There is only one thread at this point.
609 			 */
610 			SIGDELSET(td->td_siglist, sig);
611 		}
612 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
613 	}
614 	/*
615 	 * Reset stack state to the user stack.
616 	 * Clear set of signals caught on the signal stack.
617 	 */
618 	td->td_sigstk.ss_flags = SS_DISABLE;
619 	td->td_sigstk.ss_size = 0;
620 	td->td_sigstk.ss_sp = 0;
621 	td->td_pflags &= ~TDP_ALTSTACK;
622 	/*
623 	 * Reset no zombies if child dies flag as Solaris does.
624 	 */
625 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
626 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
627 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
628 	mtx_unlock(&ps->ps_mtx);
629 }
630 
631 /*
632  * kern_sigprocmask()
633  *
634  *	Manipulate signal mask.
635  */
636 int
637 kern_sigprocmask(td, how, set, oset, old)
638 	struct thread *td;
639 	int how;
640 	sigset_t *set, *oset;
641 	int old;
642 {
643 	int error;
644 
645 	PROC_LOCK(td->td_proc);
646 	if (oset != NULL)
647 		*oset = td->td_sigmask;
648 
649 	error = 0;
650 	if (set != NULL) {
651 		switch (how) {
652 		case SIG_BLOCK:
653 			SIG_CANTMASK(*set);
654 			SIGSETOR(td->td_sigmask, *set);
655 			break;
656 		case SIG_UNBLOCK:
657 			SIGSETNAND(td->td_sigmask, *set);
658 			signotify(td);
659 			break;
660 		case SIG_SETMASK:
661 			SIG_CANTMASK(*set);
662 			if (old)
663 				SIGSETLO(td->td_sigmask, *set);
664 			else
665 				td->td_sigmask = *set;
666 			signotify(td);
667 			break;
668 		default:
669 			error = EINVAL;
670 			break;
671 		}
672 	}
673 	PROC_UNLOCK(td->td_proc);
674 	return (error);
675 }
676 
677 /*
678  * sigprocmask() - MP SAFE
679  */
680 
681 #ifndef _SYS_SYSPROTO_H_
682 struct sigprocmask_args {
683 	int	how;
684 	const sigset_t *set;
685 	sigset_t *oset;
686 };
687 #endif
688 int
689 sigprocmask(td, uap)
690 	register struct thread *td;
691 	struct sigprocmask_args *uap;
692 {
693 	sigset_t set, oset;
694 	sigset_t *setp, *osetp;
695 	int error;
696 
697 	setp = (uap->set != NULL) ? &set : NULL;
698 	osetp = (uap->oset != NULL) ? &oset : NULL;
699 	if (setp) {
700 		error = copyin(uap->set, setp, sizeof(set));
701 		if (error)
702 			return (error);
703 	}
704 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
705 	if (osetp && !error) {
706 		error = copyout(osetp, uap->oset, sizeof(oset));
707 	}
708 	return (error);
709 }
710 
711 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
712 /*
713  * osigprocmask() - MP SAFE
714  */
715 #ifndef _SYS_SYSPROTO_H_
716 struct osigprocmask_args {
717 	int	how;
718 	osigset_t mask;
719 };
720 #endif
721 int
722 osigprocmask(td, uap)
723 	register struct thread *td;
724 	struct osigprocmask_args *uap;
725 {
726 	sigset_t set, oset;
727 	int error;
728 
729 	OSIG2SIG(uap->mask, set);
730 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
731 	SIG2OSIG(oset, td->td_retval[0]);
732 	return (error);
733 }
734 #endif /* COMPAT_43 */
735 
736 #ifndef _SYS_SYSPROTO_H_
737 struct sigpending_args {
738 	sigset_t	*set;
739 };
740 #endif
741 /*
742  * MPSAFE
743  */
744 int
745 sigwait(struct thread *td, struct sigwait_args *uap)
746 {
747 	siginfo_t info;
748 	sigset_t set;
749 	int error;
750 
751 	error = copyin(uap->set, &set, sizeof(set));
752 	if (error)
753 		return (error);
754 
755 	error = kern_sigtimedwait(td, set, &info, NULL);
756 	if (error)
757 		return (error);
758 
759 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
760 	/* Repost if we got an error. */
761 	if (error && info.si_signo) {
762 		PROC_LOCK(td->td_proc);
763 		tdsignal(td, info.si_signo, SIGTARGET_TD);
764 		PROC_UNLOCK(td->td_proc);
765 	}
766 	return (error);
767 }
768 /*
769  * MPSAFE
770  */
771 int
772 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
773 {
774 	struct timespec ts;
775 	struct timespec *timeout;
776 	sigset_t set;
777 	siginfo_t info;
778 	int error;
779 
780 	if (uap->timeout) {
781 		error = copyin(uap->timeout, &ts, sizeof(ts));
782 		if (error)
783 			return (error);
784 
785 		timeout = &ts;
786 	} else
787 		timeout = NULL;
788 
789 	error = copyin(uap->set, &set, sizeof(set));
790 	if (error)
791 		return (error);
792 
793 	error = kern_sigtimedwait(td, set, &info, timeout);
794 	if (error)
795 		return (error);
796 
797 	if (uap->info)
798 		error = copyout(&info, uap->info, sizeof(info));
799 	/* Repost if we got an error. */
800 	if (error && info.si_signo) {
801 		PROC_LOCK(td->td_proc);
802 		tdsignal(td, info.si_signo, SIGTARGET_TD);
803 		PROC_UNLOCK(td->td_proc);
804 	} else {
805 		td->td_retval[0] = info.si_signo;
806 	}
807 	return (error);
808 }
809 
810 /*
811  * MPSAFE
812  */
813 int
814 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
815 {
816 	siginfo_t info;
817 	sigset_t set;
818 	int error;
819 
820 	error = copyin(uap->set, &set, sizeof(set));
821 	if (error)
822 		return (error);
823 
824 	error = kern_sigtimedwait(td, set, &info, NULL);
825 	if (error)
826 		return (error);
827 
828 	if (uap->info)
829 		error = copyout(&info, uap->info, sizeof(info));
830 	/* Repost if we got an error. */
831 	if (error && info.si_signo) {
832 		PROC_LOCK(td->td_proc);
833 		tdsignal(td, info.si_signo, SIGTARGET_TD);
834 		PROC_UNLOCK(td->td_proc);
835 	} else {
836 		td->td_retval[0] = info.si_signo;
837 	}
838 	return (error);
839 }
840 
841 static int
842 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
843     struct timespec *timeout)
844 {
845 	struct sigacts *ps;
846 	sigset_t savedmask, sigset;
847 	struct proc *p;
848 	int error;
849 	int sig;
850 	int hz;
851 	int i;
852 
853 	p = td->td_proc;
854 	error = 0;
855 	sig = 0;
856 	SIG_CANTMASK(waitset);
857 
858 	PROC_LOCK(p);
859 	ps = p->p_sigacts;
860 	savedmask = td->td_sigmask;
861 
862 again:
863 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
864 		if (!SIGISMEMBER(waitset, i))
865 			continue;
866 		if (SIGISMEMBER(td->td_siglist, i)) {
867 			SIGFILLSET(td->td_sigmask);
868 			SIG_CANTMASK(td->td_sigmask);
869 			SIGDELSET(td->td_sigmask, i);
870 			mtx_lock(&ps->ps_mtx);
871 			sig = cursig(td);
872 			i = 0;
873 			mtx_unlock(&ps->ps_mtx);
874 		} else if (SIGISMEMBER(p->p_siglist, i)) {
875 			if (p->p_flag & P_SA) {
876 				p->p_flag |= P_SIGEVENT;
877 				wakeup(&p->p_siglist);
878 			}
879 			SIGDELSET(p->p_siglist, i);
880 			SIGADDSET(td->td_siglist, i);
881 			SIGFILLSET(td->td_sigmask);
882 			SIG_CANTMASK(td->td_sigmask);
883 			SIGDELSET(td->td_sigmask, i);
884 			mtx_lock(&ps->ps_mtx);
885 			sig = cursig(td);
886 			i = 0;
887 			mtx_unlock(&ps->ps_mtx);
888 		}
889 		if (sig) {
890 			td->td_sigmask = savedmask;
891 			signotify(td);
892 			goto out;
893 		}
894 	}
895 	if (error)
896 		goto out;
897 
898 	td->td_sigmask = savedmask;
899 	signotify(td);
900 	sigset = td->td_siglist;
901 	SIGSETOR(sigset, p->p_siglist);
902 	SIGSETAND(sigset, waitset);
903 	if (!SIGISEMPTY(sigset))
904 		goto again;
905 
906 	/*
907 	 * POSIX says this must be checked after looking for pending
908 	 * signals.
909 	 */
910 	if (timeout) {
911 		struct timeval tv;
912 
913 		if (timeout->tv_nsec < 0 || timeout->tv_nsec > 1000000000) {
914 			error = EINVAL;
915 			goto out;
916 		}
917 		if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
918 			error = EAGAIN;
919 			goto out;
920 		}
921 		TIMESPEC_TO_TIMEVAL(&tv, timeout);
922 		hz = tvtohz(&tv);
923 	} else
924 		hz = 0;
925 
926 	td->td_waitset = &waitset;
927 	error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
928 	td->td_waitset = NULL;
929 	if (error == 0) /* surplus wakeup ? */
930 		error = EINTR;
931 	goto again;
932 
933 out:
934 	if (sig) {
935 		sig_t action;
936 
937 		error = 0;
938 		mtx_lock(&ps->ps_mtx);
939 		action = ps->ps_sigact[_SIG_IDX(sig)];
940 		mtx_unlock(&ps->ps_mtx);
941 #ifdef KTRACE
942 		if (KTRPOINT(td, KTR_PSIG))
943 			ktrpsig(sig, action, &td->td_sigmask, 0);
944 #endif
945 		_STOPEVENT(p, S_SIG, sig);
946 
947 		SIGDELSET(td->td_siglist, sig);
948 		info->si_signo = sig;
949 		info->si_code = 0;
950 	}
951 	PROC_UNLOCK(p);
952 	return (error);
953 }
954 
955 /*
956  * MPSAFE
957  */
958 int
959 sigpending(td, uap)
960 	struct thread *td;
961 	struct sigpending_args *uap;
962 {
963 	struct proc *p = td->td_proc;
964 	sigset_t siglist;
965 
966 	PROC_LOCK(p);
967 	siglist = p->p_siglist;
968 	SIGSETOR(siglist, td->td_siglist);
969 	PROC_UNLOCK(p);
970 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
971 }
972 
973 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
974 #ifndef _SYS_SYSPROTO_H_
975 struct osigpending_args {
976 	int	dummy;
977 };
978 #endif
979 /*
980  * MPSAFE
981  */
982 int
983 osigpending(td, uap)
984 	struct thread *td;
985 	struct osigpending_args *uap;
986 {
987 	struct proc *p = td->td_proc;
988 	sigset_t siglist;
989 
990 	PROC_LOCK(p);
991 	siglist = p->p_siglist;
992 	SIGSETOR(siglist, td->td_siglist);
993 	PROC_UNLOCK(p);
994 	SIG2OSIG(siglist, td->td_retval[0]);
995 	return (0);
996 }
997 #endif /* COMPAT_43 */
998 
999 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1000 /*
1001  * Generalized interface signal handler, 4.3-compatible.
1002  */
1003 #ifndef _SYS_SYSPROTO_H_
1004 struct osigvec_args {
1005 	int	signum;
1006 	struct	sigvec *nsv;
1007 	struct	sigvec *osv;
1008 };
1009 #endif
1010 /*
1011  * MPSAFE
1012  */
1013 /* ARGSUSED */
1014 int
1015 osigvec(td, uap)
1016 	struct thread *td;
1017 	register struct osigvec_args *uap;
1018 {
1019 	struct sigvec vec;
1020 	struct sigaction nsa, osa;
1021 	register struct sigaction *nsap, *osap;
1022 	int error;
1023 
1024 	if (uap->signum <= 0 || uap->signum >= ONSIG)
1025 		return (EINVAL);
1026 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
1027 	osap = (uap->osv != NULL) ? &osa : NULL;
1028 	if (nsap) {
1029 		error = copyin(uap->nsv, &vec, sizeof(vec));
1030 		if (error)
1031 			return (error);
1032 		nsap->sa_handler = vec.sv_handler;
1033 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1034 		nsap->sa_flags = vec.sv_flags;
1035 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1036 #ifdef COMPAT_SUNOS
1037 		nsap->sa_flags |= SA_USERTRAMP;
1038 #endif
1039 	}
1040 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1041 	if (osap && !error) {
1042 		vec.sv_handler = osap->sa_handler;
1043 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
1044 		vec.sv_flags = osap->sa_flags;
1045 		vec.sv_flags &= ~SA_NOCLDWAIT;
1046 		vec.sv_flags ^= SA_RESTART;
1047 #ifdef COMPAT_SUNOS
1048 		vec.sv_flags &= ~SA_NOCLDSTOP;
1049 #endif
1050 		error = copyout(&vec, uap->osv, sizeof(vec));
1051 	}
1052 	return (error);
1053 }
1054 
1055 #ifndef _SYS_SYSPROTO_H_
1056 struct osigblock_args {
1057 	int	mask;
1058 };
1059 #endif
1060 /*
1061  * MPSAFE
1062  */
1063 int
1064 osigblock(td, uap)
1065 	register struct thread *td;
1066 	struct osigblock_args *uap;
1067 {
1068 	struct proc *p = td->td_proc;
1069 	sigset_t set;
1070 
1071 	OSIG2SIG(uap->mask, set);
1072 	SIG_CANTMASK(set);
1073 	PROC_LOCK(p);
1074 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1075 	SIGSETOR(td->td_sigmask, set);
1076 	PROC_UNLOCK(p);
1077 	return (0);
1078 }
1079 
1080 #ifndef _SYS_SYSPROTO_H_
1081 struct osigsetmask_args {
1082 	int	mask;
1083 };
1084 #endif
1085 /*
1086  * MPSAFE
1087  */
1088 int
1089 osigsetmask(td, uap)
1090 	struct thread *td;
1091 	struct osigsetmask_args *uap;
1092 {
1093 	struct proc *p = td->td_proc;
1094 	sigset_t set;
1095 
1096 	OSIG2SIG(uap->mask, set);
1097 	SIG_CANTMASK(set);
1098 	PROC_LOCK(p);
1099 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1100 	SIGSETLO(td->td_sigmask, set);
1101 	signotify(td);
1102 	PROC_UNLOCK(p);
1103 	return (0);
1104 }
1105 #endif /* COMPAT_43 || COMPAT_SUNOS */
1106 
1107 /*
1108  * Suspend process until signal, providing mask to be set
1109  * in the meantime.  Note nonstandard calling convention:
1110  * libc stub passes mask, not pointer, to save a copyin.
1111  ***** XXXKSE this doesn't make sense under KSE.
1112  ***** Do we suspend the thread or all threads in the process?
1113  ***** How do we suspend threads running NOW on another processor?
1114  */
1115 #ifndef _SYS_SYSPROTO_H_
1116 struct sigsuspend_args {
1117 	const sigset_t *sigmask;
1118 };
1119 #endif
1120 /*
1121  * MPSAFE
1122  */
1123 /* ARGSUSED */
1124 int
1125 sigsuspend(td, uap)
1126 	struct thread *td;
1127 	struct sigsuspend_args *uap;
1128 {
1129 	sigset_t mask;
1130 	int error;
1131 
1132 	error = copyin(uap->sigmask, &mask, sizeof(mask));
1133 	if (error)
1134 		return (error);
1135 	return (kern_sigsuspend(td, mask));
1136 }
1137 
1138 int
1139 kern_sigsuspend(struct thread *td, sigset_t mask)
1140 {
1141 	struct proc *p = td->td_proc;
1142 
1143 	/*
1144 	 * When returning from sigsuspend, we want
1145 	 * the old mask to be restored after the
1146 	 * signal handler has finished.  Thus, we
1147 	 * save it here and mark the sigacts structure
1148 	 * to indicate this.
1149 	 */
1150 	PROC_LOCK(p);
1151 	td->td_oldsigmask = td->td_sigmask;
1152 	td->td_pflags |= TDP_OLDMASK;
1153 	SIG_CANTMASK(mask);
1154 	td->td_sigmask = mask;
1155 	signotify(td);
1156 	while (msleep(p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1157 		/* void */;
1158 	PROC_UNLOCK(p);
1159 	/* always return EINTR rather than ERESTART... */
1160 	return (EINTR);
1161 }
1162 
1163 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
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 			if (td->td_flags & TDF_CVWAITQ)
1871 				cv_abort(td);
1872 			else
1873 				abortsleep(td);
1874 		}
1875 		mtx_unlock_spin(&sched_lock);
1876 		goto out;
1877 		/*
1878 		 * XXXKSE  What about threads that are waiting on mutexes?
1879 		 * Shouldn't they abort too?
1880 		 * No, hopefully mutexes are short lived.. They'll
1881 		 * eventually hit thread_suspend_check().
1882 		 */
1883 	}  else if (p->p_state == PRS_NORMAL) {
1884 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
1885 			!(prop & SA_STOP)) {
1886 			mtx_lock_spin(&sched_lock);
1887 			tdsigwakeup(td, sig, action);
1888 			mtx_unlock_spin(&sched_lock);
1889 			goto out;
1890 		}
1891 		if (prop & SA_STOP) {
1892 			if (p->p_flag & P_PPWAIT)
1893 				goto out;
1894 			p->p_flag |= P_STOPPED_SIG;
1895 			p->p_xstat = sig;
1896 			mtx_lock_spin(&sched_lock);
1897 			FOREACH_THREAD_IN_PROC(p, td0) {
1898 				if (TD_IS_SLEEPING(td0) &&
1899 				    (td0->td_flags & TDF_SINTR) &&
1900 				    !TD_IS_SUSPENDED(td0)) {
1901 					thread_suspend_one(td0);
1902 				} else if (td != td0) {
1903 					td0->td_flags |= TDF_ASTPENDING;
1904 				}
1905 			}
1906 			thread_stopped(p);
1907 			if (p->p_numthreads == p->p_suspcount) {
1908 				SIGDELSET(p->p_siglist, p->p_xstat);
1909 				FOREACH_THREAD_IN_PROC(p, td0)
1910 					SIGDELSET(td0->td_siglist, p->p_xstat);
1911 			}
1912 			mtx_unlock_spin(&sched_lock);
1913 			goto out;
1914 		}
1915 		else
1916 			goto runfast;
1917 		/* NOTREACHED */
1918 	} else {
1919 		/* Not in "NORMAL" state. discard the signal. */
1920 		SIGDELSET(*siglist, sig);
1921 		goto out;
1922 	}
1923 
1924 	/*
1925 	 * The process is not stopped so we need to apply the signal to all the
1926 	 * running threads.
1927 	 */
1928 
1929 runfast:
1930 	mtx_lock_spin(&sched_lock);
1931 	tdsigwakeup(td, sig, action);
1932 	thread_unsuspend(p);
1933 	mtx_unlock_spin(&sched_lock);
1934 out:
1935 	/* If we jump here, sched_lock should not be owned. */
1936 	mtx_assert(&sched_lock, MA_NOTOWNED);
1937 }
1938 
1939 /*
1940  * The force of a signal has been directed against a single
1941  * thread. We need to see what we can do about knocking it
1942  * out of any sleep it may be in etc.
1943  */
1944 static void
1945 tdsigwakeup(struct thread *td, int sig, sig_t action)
1946 {
1947 	struct proc *p = td->td_proc;
1948 	register int prop;
1949 
1950 	PROC_LOCK_ASSERT(p, MA_OWNED);
1951 	mtx_assert(&sched_lock, MA_OWNED);
1952 	prop = sigprop(sig);
1953 	/*
1954 	 * Bring the priority of a thread up if we want it to get
1955 	 * killed in this lifetime.
1956 	 */
1957 	if ((action == SIG_DFL) && (prop & SA_KILL)) {
1958 		if (td->td_priority > PUSER) {
1959 			td->td_priority = PUSER;
1960 		}
1961 	}
1962 	if (TD_IS_SLEEPING(td)) {
1963 		/*
1964 		 * If thread is sleeping uninterruptibly
1965 		 * we can't interrupt the sleep... the signal will
1966 		 * be noticed when the process returns through
1967 		 * trap() or syscall().
1968 		 */
1969 		if ((td->td_flags & TDF_SINTR) == 0) {
1970 			return;
1971 		}
1972 		/*
1973 		 * Process is sleeping and traced.  Make it runnable
1974 		 * so it can discover the signal in issignal() and stop
1975 		 * for its parent.
1976 		 */
1977 		if (p->p_flag & P_TRACED) {
1978 			p->p_flag &= ~P_STOPPED_TRACE;
1979 		} else {
1980 
1981 			/*
1982 			 * If SIGCONT is default (or ignored) and process is
1983 			 * asleep, we are finished; the process should not
1984 			 * be awakened.
1985 			 */
1986 			if ((prop & SA_CONT) && action == SIG_DFL) {
1987 				SIGDELSET(p->p_siglist, sig);
1988 				/*
1989 				 * It may be on either list in this state.
1990 				 * Remove from both for now.
1991 				 */
1992 				SIGDELSET(td->td_siglist, sig);
1993 				return;
1994 			}
1995 
1996 			/*
1997 			 * Raise priority to at least PUSER.
1998 			 */
1999 			if (td->td_priority > PUSER) {
2000 				td->td_priority = PUSER;
2001 			}
2002 		}
2003 		if (td->td_flags & TDF_CVWAITQ)
2004 			cv_abort(td);
2005 		else
2006 			abortsleep(td);
2007 	}
2008 #ifdef SMP
2009 	  else {
2010 		/*
2011 		 * Other states do nothing with the signal immediatly,
2012 		 * other than kicking ourselves if we are running.
2013 		 * It will either never be noticed, or noticed very soon.
2014 		 */
2015 		if (TD_IS_RUNNING(td) && td != curthread) {
2016 			forward_signal(td);
2017 		}
2018 	  }
2019 #endif
2020 }
2021 
2022 void
2023 ptracestop(struct thread *td, int sig)
2024 {
2025 	struct proc *p = td->td_proc;
2026 
2027 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2028 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
2029 
2030 	p->p_xstat = sig;
2031 	PROC_LOCK(p->p_pptr);
2032 	psignal(p->p_pptr, SIGCHLD);
2033 	PROC_UNLOCK(p->p_pptr);
2034 	mtx_lock_spin(&sched_lock);
2035 	stop(p);	/* uses schedlock too eventually */
2036 	thread_suspend_one(td);
2037 	PROC_UNLOCK(p);
2038 	DROP_GIANT();
2039 	p->p_stats->p_ru.ru_nivcsw++;
2040 	mi_switch();
2041 	mtx_unlock_spin(&sched_lock);
2042 	PICKUP_GIANT();
2043 }
2044 
2045 /*
2046  * If the current process has received a signal (should be caught or cause
2047  * termination, should interrupt current syscall), return the signal number.
2048  * Stop signals with default action are processed immediately, then cleared;
2049  * they aren't returned.  This is checked after each entry to the system for
2050  * a syscall or trap (though this can usually be done without calling issignal
2051  * by checking the pending signal masks in cursig.) The normal call
2052  * sequence is
2053  *
2054  *	while (sig = cursig(curthread))
2055  *		postsig(sig);
2056  */
2057 static int
2058 issignal(td)
2059 	struct thread *td;
2060 {
2061 	struct proc *p;
2062 	struct sigacts *ps;
2063 	sigset_t sigpending;
2064 	int sig, prop;
2065 	struct thread *td0;
2066 
2067 	p = td->td_proc;
2068 	ps = p->p_sigacts;
2069 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2070 	PROC_LOCK_ASSERT(p, MA_OWNED);
2071 	for (;;) {
2072 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2073 
2074 		sigpending = td->td_siglist;
2075 		SIGSETNAND(sigpending, td->td_sigmask);
2076 
2077 		if (p->p_flag & P_PPWAIT)
2078 			SIG_STOPSIGMASK(sigpending);
2079 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2080 			return (0);
2081 		sig = sig_ffs(&sigpending);
2082 
2083 		if (p->p_stops & S_SIG) {
2084 			mtx_unlock(&ps->ps_mtx);
2085 			stopevent(p, S_SIG, sig);
2086 			mtx_lock(&ps->ps_mtx);
2087 		}
2088 
2089 		/*
2090 		 * We should see pending but ignored signals
2091 		 * only if P_TRACED was on when they were posted.
2092 		 */
2093 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2094 			SIGDELSET(td->td_siglist, sig);
2095 			continue;
2096 		}
2097 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2098 			/*
2099 			 * If traced, always stop.
2100 			 */
2101 			mtx_unlock(&ps->ps_mtx);
2102 			ptracestop(td, sig);
2103 			PROC_LOCK(p);
2104 			mtx_lock(&ps->ps_mtx);
2105 
2106 			/*
2107 			 * If parent wants us to take the signal,
2108 			 * then it will leave it in p->p_xstat;
2109 			 * otherwise we just look for signals again.
2110 			 */
2111 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2112 			sig = p->p_xstat;
2113 			if (sig == 0)
2114 				continue;
2115 
2116 			/*
2117 			 * If the traced bit got turned off, go back up
2118 			 * to the top to rescan signals.  This ensures
2119 			 * that p_sig* and p_sigact are consistent.
2120 			 */
2121 			if ((p->p_flag & P_TRACED) == 0)
2122 				continue;
2123 
2124 			/*
2125 			 * Put the new signal into td_siglist.  If the
2126 			 * signal is being masked, look for other signals.
2127 			 */
2128 			SIGADDSET(td->td_siglist, sig);
2129 			if (SIGISMEMBER(td->td_sigmask, sig))
2130 				continue;
2131 			signotify(td);
2132 		}
2133 
2134 		prop = sigprop(sig);
2135 
2136 		/*
2137 		 * Decide whether the signal should be returned.
2138 		 * Return the signal's number, or fall through
2139 		 * to clear it from the pending mask.
2140 		 */
2141 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2142 
2143 		case (intptr_t)SIG_DFL:
2144 			/*
2145 			 * Don't take default actions on system processes.
2146 			 */
2147 			if (p->p_pid <= 1) {
2148 #ifdef DIAGNOSTIC
2149 				/*
2150 				 * Are you sure you want to ignore SIGSEGV
2151 				 * in init? XXX
2152 				 */
2153 				printf("Process (pid %lu) got signal %d\n",
2154 					(u_long)p->p_pid, sig);
2155 #endif
2156 				break;		/* == ignore */
2157 			}
2158 			/*
2159 			 * If there is a pending stop signal to process
2160 			 * with default action, stop here,
2161 			 * then clear the signal.  However,
2162 			 * if process is member of an orphaned
2163 			 * process group, ignore tty stop signals.
2164 			 */
2165 			if (prop & SA_STOP) {
2166 				if (p->p_flag & P_TRACED ||
2167 		    		    (p->p_pgrp->pg_jobc == 0 &&
2168 				     prop & SA_TTYSTOP))
2169 					break;	/* == ignore */
2170 				mtx_unlock(&ps->ps_mtx);
2171 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2172 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2173 				p->p_flag |= P_STOPPED_SIG;
2174 				p->p_xstat = sig;
2175 				mtx_lock_spin(&sched_lock);
2176 				FOREACH_THREAD_IN_PROC(p, td0) {
2177 					if (TD_IS_SLEEPING(td0) &&
2178 					    (td0->td_flags & TDF_SINTR) &&
2179 					    !TD_IS_SUSPENDED(td0)) {
2180 						thread_suspend_one(td0);
2181 					} else if (td != td0) {
2182 						td0->td_flags |= TDF_ASTPENDING;
2183 					}
2184 				}
2185 				thread_stopped(p);
2186 				thread_suspend_one(td);
2187 				PROC_UNLOCK(p);
2188 				DROP_GIANT();
2189 				p->p_stats->p_ru.ru_nivcsw++;
2190 				mi_switch();
2191 				mtx_unlock_spin(&sched_lock);
2192 				PICKUP_GIANT();
2193 				PROC_LOCK(p);
2194 				mtx_lock(&ps->ps_mtx);
2195 				break;
2196 			} else if (prop & SA_IGNORE) {
2197 				/*
2198 				 * Except for SIGCONT, shouldn't get here.
2199 				 * Default action is to ignore; drop it.
2200 				 */
2201 				break;		/* == ignore */
2202 			} else
2203 				return (sig);
2204 			/*NOTREACHED*/
2205 
2206 		case (intptr_t)SIG_IGN:
2207 			/*
2208 			 * Masking above should prevent us ever trying
2209 			 * to take action on an ignored signal other
2210 			 * than SIGCONT, unless process is traced.
2211 			 */
2212 			if ((prop & SA_CONT) == 0 &&
2213 			    (p->p_flag & P_TRACED) == 0)
2214 				printf("issignal\n");
2215 			break;		/* == ignore */
2216 
2217 		default:
2218 			/*
2219 			 * This signal has an action, let
2220 			 * postsig() process it.
2221 			 */
2222 			return (sig);
2223 		}
2224 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2225 	}
2226 	/* NOTREACHED */
2227 }
2228 
2229 /*
2230  * Put the argument process into the stopped state and notify the parent
2231  * via wakeup.  Signals are handled elsewhere.  The process must not be
2232  * on the run queue.  Must be called with the proc p locked and the scheduler
2233  * lock held.
2234  */
2235 static void
2236 stop(struct proc *p)
2237 {
2238 
2239 	PROC_LOCK_ASSERT(p, MA_OWNED);
2240 	p->p_flag |= P_STOPPED_SIG;
2241 	p->p_flag &= ~P_WAITED;
2242 	wakeup(p->p_pptr);
2243 }
2244 
2245 /*
2246  * MPSAFE
2247  */
2248 void
2249 thread_stopped(struct proc *p)
2250 {
2251 	struct proc *p1 = curthread->td_proc;
2252 	struct sigacts *ps;
2253 	int n;
2254 
2255 	PROC_LOCK_ASSERT(p, MA_OWNED);
2256 	mtx_assert(&sched_lock, MA_OWNED);
2257 	n = p->p_suspcount;
2258 	if (p == p1)
2259 		n++;
2260 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2261 		mtx_unlock_spin(&sched_lock);
2262 		stop(p);
2263 		PROC_LOCK(p->p_pptr);
2264 		ps = p->p_pptr->p_sigacts;
2265 		mtx_lock(&ps->ps_mtx);
2266 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2267 			mtx_unlock(&ps->ps_mtx);
2268 			psignal(p->p_pptr, SIGCHLD);
2269 		} else
2270 			mtx_unlock(&ps->ps_mtx);
2271 		PROC_UNLOCK(p->p_pptr);
2272 		mtx_lock_spin(&sched_lock);
2273 	}
2274 }
2275 
2276 /*
2277  * Take the action for the specified signal
2278  * from the current set of pending signals.
2279  */
2280 void
2281 postsig(sig)
2282 	register int sig;
2283 {
2284 	struct thread *td = curthread;
2285 	register struct proc *p = td->td_proc;
2286 	struct sigacts *ps;
2287 	sig_t action;
2288 	sigset_t returnmask;
2289 	int code;
2290 
2291 	KASSERT(sig != 0, ("postsig"));
2292 
2293 	PROC_LOCK_ASSERT(p, MA_OWNED);
2294 	ps = p->p_sigacts;
2295 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2296 	SIGDELSET(td->td_siglist, sig);
2297 	action = ps->ps_sigact[_SIG_IDX(sig)];
2298 #ifdef KTRACE
2299 	if (KTRPOINT(td, KTR_PSIG))
2300 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2301 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2302 #endif
2303 	if (p->p_stops & S_SIG) {
2304 		mtx_unlock(&ps->ps_mtx);
2305 		stopevent(p, S_SIG, sig);
2306 		mtx_lock(&ps->ps_mtx);
2307 	}
2308 
2309 	if (!(td->td_flags & TDF_SA && td->td_mailbox) &&
2310 	    action == SIG_DFL) {
2311 		/*
2312 		 * Default action, where the default is to kill
2313 		 * the process.  (Other cases were ignored above.)
2314 		 */
2315 		mtx_unlock(&ps->ps_mtx);
2316 		sigexit(td, sig);
2317 		/* NOTREACHED */
2318 	} else {
2319 		if (td->td_flags & TDF_SA && td->td_mailbox) {
2320 			if (sig == SIGKILL) {
2321 				mtx_unlock(&ps->ps_mtx);
2322 				sigexit(td, sig);
2323 			}
2324 		}
2325 
2326 		/*
2327 		 * If we get here, the signal must be caught.
2328 		 */
2329 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2330 		    ("postsig action"));
2331 		/*
2332 		 * Set the new mask value and also defer further
2333 		 * occurrences of this signal.
2334 		 *
2335 		 * Special case: user has done a sigsuspend.  Here the
2336 		 * current mask is not of interest, but rather the
2337 		 * mask from before the sigsuspend is what we want
2338 		 * restored after the signal processing is completed.
2339 		 */
2340 		if (td->td_pflags & TDP_OLDMASK) {
2341 			returnmask = td->td_oldsigmask;
2342 			td->td_pflags &= ~TDP_OLDMASK;
2343 		} else
2344 			returnmask = td->td_sigmask;
2345 
2346 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2347 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2348 			SIGADDSET(td->td_sigmask, sig);
2349 
2350 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2351 			/*
2352 			 * See kern_sigaction() for origin of this code.
2353 			 */
2354 			SIGDELSET(ps->ps_sigcatch, sig);
2355 			if (sig != SIGCONT &&
2356 			    sigprop(sig) & SA_IGNORE)
2357 				SIGADDSET(ps->ps_sigignore, sig);
2358 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2359 		}
2360 		p->p_stats->p_ru.ru_nsignals++;
2361 		if (p->p_sig != sig) {
2362 			code = 0;
2363 		} else {
2364 			code = p->p_code;
2365 			p->p_code = 0;
2366 			p->p_sig = 0;
2367 		}
2368 		if (td->td_flags & TDF_SA && td->td_mailbox)
2369 			thread_signal_add(curthread, sig);
2370 		else
2371 			(*p->p_sysent->sv_sendsig)(action, sig,
2372 			    &returnmask, code);
2373 	}
2374 }
2375 
2376 /*
2377  * Kill the current process for stated reason.
2378  */
2379 void
2380 killproc(p, why)
2381 	struct proc *p;
2382 	char *why;
2383 {
2384 
2385 	PROC_LOCK_ASSERT(p, MA_OWNED);
2386 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2387 		p, p->p_pid, p->p_comm);
2388 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2389 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2390 	psignal(p, SIGKILL);
2391 }
2392 
2393 /*
2394  * Force the current process to exit with the specified signal, dumping core
2395  * if appropriate.  We bypass the normal tests for masked and caught signals,
2396  * allowing unrecoverable failures to terminate the process without changing
2397  * signal state.  Mark the accounting record with the signal termination.
2398  * If dumping core, save the signal number for the debugger.  Calls exit and
2399  * does not return.
2400  *
2401  * MPSAFE
2402  */
2403 void
2404 sigexit(td, sig)
2405 	struct thread *td;
2406 	int sig;
2407 {
2408 	struct proc *p = td->td_proc;
2409 
2410 	PROC_LOCK_ASSERT(p, MA_OWNED);
2411 	p->p_acflag |= AXSIG;
2412 	if (sigprop(sig) & SA_CORE) {
2413 		p->p_sig = sig;
2414 		/*
2415 		 * Log signals which would cause core dumps
2416 		 * (Log as LOG_INFO to appease those who don't want
2417 		 * these messages.)
2418 		 * XXX : Todo, as well as euid, write out ruid too
2419 		 */
2420 		PROC_UNLOCK(p);
2421 		if (!mtx_owned(&Giant))
2422 			mtx_lock(&Giant);
2423 		if (coredump(td) == 0)
2424 			sig |= WCOREFLAG;
2425 		if (kern_logsigexit)
2426 			log(LOG_INFO,
2427 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
2428 			    p->p_pid, p->p_comm,
2429 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
2430 			    sig &~ WCOREFLAG,
2431 			    sig & WCOREFLAG ? " (core dumped)" : "");
2432 	} else {
2433 		PROC_UNLOCK(p);
2434 		if (!mtx_owned(&Giant))
2435 			mtx_lock(&Giant);
2436 	}
2437 	exit1(td, W_EXITCODE(0, sig));
2438 	/* NOTREACHED */
2439 }
2440 
2441 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2442 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2443 	      sizeof(corefilename), "process corefile name format string");
2444 
2445 /*
2446  * expand_name(name, uid, pid)
2447  * Expand the name described in corefilename, using name, uid, and pid.
2448  * corefilename is a printf-like string, with three format specifiers:
2449  *	%N	name of process ("name")
2450  *	%P	process id (pid)
2451  *	%U	user id (uid)
2452  * For example, "%N.core" is the default; they can be disabled completely
2453  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2454  * This is controlled by the sysctl variable kern.corefile (see above).
2455  */
2456 
2457 static char *
2458 expand_name(name, uid, pid)
2459 	const char *name;
2460 	uid_t uid;
2461 	pid_t pid;
2462 {
2463 	const char *format, *appendstr;
2464 	char *temp;
2465 	char buf[11];		/* Buffer for pid/uid -- max 4B */
2466 	size_t i, l, n;
2467 
2468 	format = corefilename;
2469 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2470 	if (temp == NULL)
2471 		return (NULL);
2472 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2473 		switch (format[i]) {
2474 		case '%':	/* Format character */
2475 			i++;
2476 			switch (format[i]) {
2477 			case '%':
2478 				appendstr = "%";
2479 				break;
2480 			case 'N':	/* process name */
2481 				appendstr = name;
2482 				break;
2483 			case 'P':	/* process id */
2484 				sprintf(buf, "%u", pid);
2485 				appendstr = buf;
2486 				break;
2487 			case 'U':	/* user id */
2488 				sprintf(buf, "%u", uid);
2489 				appendstr = buf;
2490 				break;
2491 			default:
2492 				appendstr = "";
2493 			  	log(LOG_ERR,
2494 				    "Unknown format character %c in `%s'\n",
2495 				    format[i], format);
2496 			}
2497 			l = strlen(appendstr);
2498 			if ((n + l) >= MAXPATHLEN)
2499 				goto toolong;
2500 			memcpy(temp + n, appendstr, l);
2501 			n += l;
2502 			break;
2503 		default:
2504 			temp[n++] = format[i];
2505 		}
2506 	}
2507 	if (format[i] != '\0')
2508 		goto toolong;
2509 	return (temp);
2510 toolong:
2511 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2512 	    (long)pid, name, (u_long)uid);
2513 	free(temp, M_TEMP);
2514 	return (NULL);
2515 }
2516 
2517 /*
2518  * Dump a process' core.  The main routine does some
2519  * policy checking, and creates the name of the coredump;
2520  * then it passes on a vnode and a size limit to the process-specific
2521  * coredump routine if there is one; if there _is not_ one, it returns
2522  * ENOSYS; otherwise it returns the error from the process-specific routine.
2523  */
2524 
2525 static int
2526 coredump(struct thread *td)
2527 {
2528 	struct proc *p = td->td_proc;
2529 	register struct vnode *vp;
2530 	register struct ucred *cred = td->td_ucred;
2531 	struct flock lf;
2532 	struct nameidata nd;
2533 	struct vattr vattr;
2534 	int error, error1, flags, locked;
2535 	struct mount *mp;
2536 	char *name;			/* name of corefile */
2537 	off_t limit;
2538 
2539 	PROC_LOCK(p);
2540 	_STOPEVENT(p, S_CORE, 0);
2541 
2542 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2543 		PROC_UNLOCK(p);
2544 		return (EFAULT);
2545 	}
2546 
2547 	/*
2548 	 * Note that the bulk of limit checking is done after
2549 	 * the corefile is created.  The exception is if the limit
2550 	 * for corefiles is 0, in which case we don't bother
2551 	 * creating the corefile at all.  This layout means that
2552 	 * a corefile is truncated instead of not being created,
2553 	 * if it is larger than the limit.
2554 	 */
2555 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2556 	if (limit == 0) {
2557 		PROC_UNLOCK(p);
2558 		return EFBIG;
2559 	}
2560 	PROC_UNLOCK(p);
2561 
2562 restart:
2563 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2564 	if (name == NULL)
2565 		return (EINVAL);
2566 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2567 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2568 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2569 	free(name, M_TEMP);
2570 	if (error)
2571 		return (error);
2572 	NDFREE(&nd, NDF_ONLY_PNBUF);
2573 	vp = nd.ni_vp;
2574 
2575 	/* Don't dump to non-regular files or files with links. */
2576 	if (vp->v_type != VREG ||
2577 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2578 		VOP_UNLOCK(vp, 0, td);
2579 		error = EFAULT;
2580 		goto out2;
2581 	}
2582 
2583 	VOP_UNLOCK(vp, 0, td);
2584 	lf.l_whence = SEEK_SET;
2585 	lf.l_start = 0;
2586 	lf.l_len = 0;
2587 	lf.l_type = F_WRLCK;
2588 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2589 
2590 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2591 		lf.l_type = F_UNLCK;
2592 		if (locked)
2593 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2594 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2595 			return (error);
2596 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2597 			return (error);
2598 		goto restart;
2599 	}
2600 
2601 	VATTR_NULL(&vattr);
2602 	vattr.va_size = 0;
2603 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2604 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2605 	VOP_SETATTR(vp, &vattr, cred, td);
2606 	VOP_UNLOCK(vp, 0, td);
2607 	PROC_LOCK(p);
2608 	p->p_acflag |= ACORE;
2609 	PROC_UNLOCK(p);
2610 
2611 	error = p->p_sysent->sv_coredump ?
2612 	  p->p_sysent->sv_coredump(td, vp, limit) :
2613 	  ENOSYS;
2614 
2615 	if (locked) {
2616 		lf.l_type = F_UNLCK;
2617 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2618 	}
2619 	vn_finished_write(mp);
2620 out2:
2621 	error1 = vn_close(vp, FWRITE, cred, td);
2622 	if (error == 0)
2623 		error = error1;
2624 	return (error);
2625 }
2626 
2627 /*
2628  * Nonexistent system call-- signal process (may want to handle it).
2629  * Flag error in case process won't see signal immediately (blocked or ignored).
2630  */
2631 #ifndef _SYS_SYSPROTO_H_
2632 struct nosys_args {
2633 	int	dummy;
2634 };
2635 #endif
2636 /*
2637  * MPSAFE
2638  */
2639 /* ARGSUSED */
2640 int
2641 nosys(td, args)
2642 	struct thread *td;
2643 	struct nosys_args *args;
2644 {
2645 	struct proc *p = td->td_proc;
2646 
2647 	PROC_LOCK(p);
2648 	psignal(p, SIGSYS);
2649 	PROC_UNLOCK(p);
2650 	return (ENOSYS);
2651 }
2652 
2653 /*
2654  * Send a SIGIO or SIGURG signal to a process or process group using
2655  * stored credentials rather than those of the current process.
2656  */
2657 void
2658 pgsigio(sigiop, sig, checkctty)
2659 	struct sigio **sigiop;
2660 	int sig, checkctty;
2661 {
2662 	struct sigio *sigio;
2663 
2664 	SIGIO_LOCK();
2665 	sigio = *sigiop;
2666 	if (sigio == NULL) {
2667 		SIGIO_UNLOCK();
2668 		return;
2669 	}
2670 	if (sigio->sio_pgid > 0) {
2671 		PROC_LOCK(sigio->sio_proc);
2672 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2673 			psignal(sigio->sio_proc, sig);
2674 		PROC_UNLOCK(sigio->sio_proc);
2675 	} else if (sigio->sio_pgid < 0) {
2676 		struct proc *p;
2677 
2678 		PGRP_LOCK(sigio->sio_pgrp);
2679 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2680 			PROC_LOCK(p);
2681 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2682 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2683 				psignal(p, sig);
2684 			PROC_UNLOCK(p);
2685 		}
2686 		PGRP_UNLOCK(sigio->sio_pgrp);
2687 	}
2688 	SIGIO_UNLOCK();
2689 }
2690 
2691 static int
2692 filt_sigattach(struct knote *kn)
2693 {
2694 	struct proc *p = curproc;
2695 
2696 	kn->kn_ptr.p_proc = p;
2697 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2698 
2699 	PROC_LOCK(p);
2700 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2701 	PROC_UNLOCK(p);
2702 
2703 	return (0);
2704 }
2705 
2706 static void
2707 filt_sigdetach(struct knote *kn)
2708 {
2709 	struct proc *p = kn->kn_ptr.p_proc;
2710 
2711 	PROC_LOCK(p);
2712 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2713 	PROC_UNLOCK(p);
2714 }
2715 
2716 /*
2717  * signal knotes are shared with proc knotes, so we apply a mask to
2718  * the hint in order to differentiate them from process hints.  This
2719  * could be avoided by using a signal-specific knote list, but probably
2720  * isn't worth the trouble.
2721  */
2722 static int
2723 filt_signal(struct knote *kn, long hint)
2724 {
2725 
2726 	if (hint & NOTE_SIGNAL) {
2727 		hint &= ~NOTE_SIGNAL;
2728 
2729 		if (kn->kn_id == hint)
2730 			kn->kn_data++;
2731 	}
2732 	return (kn->kn_data != 0);
2733 }
2734 
2735 struct sigacts *
2736 sigacts_alloc(void)
2737 {
2738 	struct sigacts *ps;
2739 
2740 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
2741 	ps->ps_refcnt = 1;
2742 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
2743 	return (ps);
2744 }
2745 
2746 void
2747 sigacts_free(struct sigacts *ps)
2748 {
2749 
2750 	mtx_lock(&ps->ps_mtx);
2751 	ps->ps_refcnt--;
2752 	if (ps->ps_refcnt == 0) {
2753 		mtx_destroy(&ps->ps_mtx);
2754 		free(ps, M_SUBPROC);
2755 	} else
2756 		mtx_unlock(&ps->ps_mtx);
2757 }
2758 
2759 struct sigacts *
2760 sigacts_hold(struct sigacts *ps)
2761 {
2762 	mtx_lock(&ps->ps_mtx);
2763 	ps->ps_refcnt++;
2764 	mtx_unlock(&ps->ps_mtx);
2765 	return (ps);
2766 }
2767 
2768 void
2769 sigacts_copy(struct sigacts *dest, struct sigacts *src)
2770 {
2771 
2772 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
2773 	mtx_lock(&src->ps_mtx);
2774 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
2775 	mtx_unlock(&src->ps_mtx);
2776 }
2777 
2778 int
2779 sigacts_shared(struct sigacts *ps)
2780 {
2781 	int shared;
2782 
2783 	mtx_lock(&ps->ps_mtx);
2784 	shared = ps->ps_refcnt > 1;
2785 	mtx_unlock(&ps->ps_mtx);
2786 	return (shared);
2787 }
2788