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