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