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