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