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