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