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