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