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