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