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