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