kqueue/signal: Send kevent even if the signal is masked
[dragonfly.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  * 3. 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  * $FreeBSD: src/sys/kern/kern_sig.c,v 1.72.2.17 2003/05/16 16:34:34 obrien Exp $
36  */
37
38 #include "opt_ktrace.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sysproto.h>
44 #include <sys/signalvar.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vnode.h>
47 #include <sys/event.h>
48 #include <sys/proc.h>
49 #include <sys/nlookup.h>
50 #include <sys/pioctl.h>
51 #include <sys/acct.h>
52 #include <sys/fcntl.h>
53 #include <sys/lock.h>
54 #include <sys/wait.h>
55 #include <sys/ktrace.h>
56 #include <sys/syslog.h>
57 #include <sys/stat.h>
58 #include <sys/sysent.h>
59 #include <sys/sysctl.h>
60 #include <sys/malloc.h>
61 #include <sys/interrupt.h>
62 #include <sys/unistd.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/vkernel.h>
65
66 #include <sys/signal2.h>
67 #include <sys/thread2.h>
68 #include <sys/spinlock2.h>
69
70 #include <machine/cpu.h>
71 #include <machine/smp.h>
72
73 static int      coredump(struct lwp *, int);
74 static char     *expand_name(const char *, uid_t, pid_t);
75 static int      dokillpg(int sig, int pgid, int all);
76 static int      sig_ffs(sigset_t *set);
77 static int      sigprop(int sig);
78 static void     lwp_signotify(struct lwp *lp);
79 static void     lwp_signotify_remote(void *arg);
80 static int      kern_sigtimedwait(sigset_t set, siginfo_t *info,
81                     struct timespec *timeout);
82 static void     proc_stopwait(struct proc *p);
83
84 static int      filt_sigattach(struct knote *kn);
85 static void     filt_sigdetach(struct knote *kn);
86 static int      filt_signal(struct knote *kn, long hint);
87
88 struct filterops sig_filtops =
89         { FILTEROP_MPSAFE, filt_sigattach, filt_sigdetach, filt_signal };
90
91 static int      kern_logsigexit = 1;
92 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 
93     &kern_logsigexit, 0, 
94     "Log processes quitting on abnormal signals to syslog(3)");
95
96 /*
97  * Can process p, with pcred pc, send the signal sig to process q?
98  */
99 #define CANSIGNAL(q, sig) \
100         (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
101         ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
102
103 /*
104  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
105  */
106 #define CANSIGIO(ruid, uc, q) \
107         ((uc)->cr_uid == 0 || \
108             (ruid) == (q)->p_ucred->cr_ruid || \
109             (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
110             (ruid) == (q)->p_ucred->cr_uid || \
111             (uc)->cr_uid == (q)->p_ucred->cr_uid)
112
113 int sugid_coredump;
114 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 
115         &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
116
117 static int      do_coredump = 1;
118 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
119         &do_coredump, 0, "Enable/Disable coredumps");
120
121 /*
122  * Signal properties and actions.
123  * The array below categorizes the signals and their default actions
124  * according to the following properties:
125  */
126 #define SA_KILL         0x01            /* terminates process by default */
127 #define SA_CORE         0x02            /* ditto and coredumps */
128 #define SA_STOP         0x04            /* suspend process */
129 #define SA_TTYSTOP      0x08            /* ditto, from tty */
130 #define SA_IGNORE       0x10            /* ignore by default */
131 #define SA_CONT         0x20            /* continue if suspended */
132 #define SA_CANTMASK     0x40            /* non-maskable, catchable */
133 #define SA_CKPT         0x80            /* checkpoint process */
134
135
136 static int sigproptbl[NSIG] = {
137         SA_KILL,                /* SIGHUP */
138         SA_KILL,                /* SIGINT */
139         SA_KILL|SA_CORE,        /* SIGQUIT */
140         SA_KILL|SA_CORE,        /* SIGILL */
141         SA_KILL|SA_CORE,        /* SIGTRAP */
142         SA_KILL|SA_CORE,        /* SIGABRT */
143         SA_KILL|SA_CORE,        /* SIGEMT */
144         SA_KILL|SA_CORE,        /* SIGFPE */
145         SA_KILL,                /* SIGKILL */
146         SA_KILL|SA_CORE,        /* SIGBUS */
147         SA_KILL|SA_CORE,        /* SIGSEGV */
148         SA_KILL|SA_CORE,        /* SIGSYS */
149         SA_KILL,                /* SIGPIPE */
150         SA_KILL,                /* SIGALRM */
151         SA_KILL,                /* SIGTERM */
152         SA_IGNORE,              /* SIGURG */
153         SA_STOP,                /* SIGSTOP */
154         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
155         SA_IGNORE|SA_CONT,      /* SIGCONT */
156         SA_IGNORE,              /* SIGCHLD */
157         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
158         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
159         SA_IGNORE,              /* SIGIO */
160         SA_KILL,                /* SIGXCPU */
161         SA_KILL,                /* SIGXFSZ */
162         SA_KILL,                /* SIGVTALRM */
163         SA_KILL,                /* SIGPROF */
164         SA_IGNORE,              /* SIGWINCH  */
165         SA_IGNORE,              /* SIGINFO */
166         SA_KILL,                /* SIGUSR1 */
167         SA_KILL,                /* SIGUSR2 */
168         SA_IGNORE,              /* SIGTHR */
169         SA_CKPT,                /* SIGCKPT */ 
170         SA_KILL|SA_CKPT,        /* SIGCKPTEXIT */  
171         SA_IGNORE,
172         SA_IGNORE,
173         SA_IGNORE,
174         SA_IGNORE,
175         SA_IGNORE,
176         SA_IGNORE,
177         SA_IGNORE,
178         SA_IGNORE,
179         SA_IGNORE,
180         SA_IGNORE,
181         SA_IGNORE,
182         SA_IGNORE,
183         SA_IGNORE,
184         SA_IGNORE,
185         SA_IGNORE,
186         SA_IGNORE,
187         SA_IGNORE,
188         SA_IGNORE,
189         SA_IGNORE,
190         SA_IGNORE,
191         SA_IGNORE,
192         SA_IGNORE,
193         SA_IGNORE,
194         SA_IGNORE,
195         SA_IGNORE,
196         SA_IGNORE,
197         SA_IGNORE,
198         SA_IGNORE,
199         SA_IGNORE,
200         SA_IGNORE,
201
202 };
203
204 static __inline int
205 sigprop(int sig)
206 {
207
208         if (sig > 0 && sig < NSIG)
209                 return (sigproptbl[_SIG_IDX(sig)]);
210         return (0);
211 }
212
213 static __inline int
214 sig_ffs(sigset_t *set)
215 {
216         int i;
217
218         for (i = 0; i < _SIG_WORDS; i++)
219                 if (set->__bits[i])
220                         return (ffs(set->__bits[i]) + (i * 32));
221         return (0);
222 }
223
224 /* 
225  * No requirements. 
226  */
227 int
228 kern_sigaction(int sig, struct sigaction *act, struct sigaction *oact)
229 {
230         struct thread *td = curthread;
231         struct proc *p = td->td_proc;
232         struct lwp *lp;
233         struct sigacts *ps = p->p_sigacts;
234
235         if (sig <= 0 || sig > _SIG_MAXSIG)
236                 return (EINVAL);
237
238         lwkt_gettoken(&p->p_token);
239
240         if (oact) {
241                 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
242                 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
243                 oact->sa_flags = 0;
244                 if (SIGISMEMBER(ps->ps_sigonstack, sig))
245                         oact->sa_flags |= SA_ONSTACK;
246                 if (!SIGISMEMBER(ps->ps_sigintr, sig))
247                         oact->sa_flags |= SA_RESTART;
248                 if (SIGISMEMBER(ps->ps_sigreset, sig))
249                         oact->sa_flags |= SA_RESETHAND;
250                 if (SIGISMEMBER(ps->ps_signodefer, sig))
251                         oact->sa_flags |= SA_NODEFER;
252                 if (SIGISMEMBER(ps->ps_siginfo, sig))
253                         oact->sa_flags |= SA_SIGINFO;
254                 if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDSTOP)
255                         oact->sa_flags |= SA_NOCLDSTOP;
256                 if (sig == SIGCHLD && p->p_sigacts->ps_flag & PS_NOCLDWAIT)
257                         oact->sa_flags |= SA_NOCLDWAIT;
258         }
259         if (act) {
260                 /*
261                  * Check for invalid requests.  KILL and STOP cannot be
262                  * caught.
263                  */
264                 if (sig == SIGKILL || sig == SIGSTOP) {
265                         if (act->sa_handler != SIG_DFL) {
266                                 lwkt_reltoken(&p->p_token);
267                                 return (EINVAL);
268                         }
269                 }
270
271                 /*
272                  * Change setting atomically.
273                  */
274                 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
275                 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
276                 if (act->sa_flags & SA_SIGINFO) {
277                         ps->ps_sigact[_SIG_IDX(sig)] =
278                             (__sighandler_t *)act->sa_sigaction;
279                         SIGADDSET(ps->ps_siginfo, sig);
280                 } else {
281                         ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
282                         SIGDELSET(ps->ps_siginfo, sig);
283                 }
284                 if (!(act->sa_flags & SA_RESTART))
285                         SIGADDSET(ps->ps_sigintr, sig);
286                 else
287                         SIGDELSET(ps->ps_sigintr, sig);
288                 if (act->sa_flags & SA_ONSTACK)
289                         SIGADDSET(ps->ps_sigonstack, sig);
290                 else
291                         SIGDELSET(ps->ps_sigonstack, sig);
292                 if (act->sa_flags & SA_RESETHAND)
293                         SIGADDSET(ps->ps_sigreset, sig);
294                 else
295                         SIGDELSET(ps->ps_sigreset, sig);
296                 if (act->sa_flags & SA_NODEFER)
297                         SIGADDSET(ps->ps_signodefer, sig);
298                 else
299                         SIGDELSET(ps->ps_signodefer, sig);
300                 if (sig == SIGCHLD) {
301                         if (act->sa_flags & SA_NOCLDSTOP)
302                                 p->p_sigacts->ps_flag |= PS_NOCLDSTOP;
303                         else
304                                 p->p_sigacts->ps_flag &= ~PS_NOCLDSTOP;
305                         if (act->sa_flags & SA_NOCLDWAIT) {
306                                 /*
307                                  * Paranoia: since SA_NOCLDWAIT is implemented
308                                  * by reparenting the dying child to PID 1 (and
309                                  * trust it to reap the zombie), PID 1 itself
310                                  * is forbidden to set SA_NOCLDWAIT.
311                                  */
312                                 if (p->p_pid == 1)
313                                         p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
314                                 else
315                                         p->p_sigacts->ps_flag |= PS_NOCLDWAIT;
316                         } else {
317                                 p->p_sigacts->ps_flag &= ~PS_NOCLDWAIT;
318                         }
319                         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
320                                 ps->ps_flag |= PS_CLDSIGIGN;
321                         else
322                                 ps->ps_flag &= ~PS_CLDSIGIGN;
323                 }
324                 /*
325                  * Set bit in p_sigignore for signals that are set to SIG_IGN,
326                  * and for signals set to SIG_DFL where the default is to
327                  * ignore. However, don't put SIGCONT in p_sigignore, as we
328                  * have to restart the process.
329                  *
330                  * Also remove the signal from the process and lwp signal
331                  * list.
332                  */
333                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
334                     (sigprop(sig) & SA_IGNORE &&
335                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
336                         SIGDELSET(p->p_siglist, sig);
337                         FOREACH_LWP_IN_PROC(lp, p) {
338                                 spin_lock(&lp->lwp_spin);
339                                 SIGDELSET(lp->lwp_siglist, sig);
340                                 spin_unlock(&lp->lwp_spin);
341                         }
342                         if (sig != SIGCONT) {
343                                 /* easier in ksignal */
344                                 SIGADDSET(p->p_sigignore, sig);
345                         }
346                         SIGDELSET(p->p_sigcatch, sig);
347                 } else {
348                         SIGDELSET(p->p_sigignore, sig);
349                         if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
350                                 SIGDELSET(p->p_sigcatch, sig);
351                         else
352                                 SIGADDSET(p->p_sigcatch, sig);
353                 }
354         }
355         lwkt_reltoken(&p->p_token);
356         return (0);
357 }
358
359 int
360 sys_sigaction(struct sigaction_args *uap)
361 {
362         struct sigaction act, oact;
363         struct sigaction *actp, *oactp;
364         int error;
365
366         actp = (uap->act != NULL) ? &act : NULL;
367         oactp = (uap->oact != NULL) ? &oact : NULL;
368         if (actp) {
369                 error = copyin(uap->act, actp, sizeof(act));
370                 if (error)
371                         return (error);
372         }
373         error = kern_sigaction(uap->sig, actp, oactp);
374         if (oactp && !error) {
375                 error = copyout(oactp, uap->oact, sizeof(oact));
376         }
377         return (error);
378 }
379
380 /*
381  * Initialize signal state for process 0;
382  * set to ignore signals that are ignored by default.
383  */
384 void
385 siginit(struct proc *p)
386 {
387         int i;
388
389         for (i = 1; i <= NSIG; i++)
390                 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
391                         SIGADDSET(p->p_sigignore, i);
392 }
393
394 /*
395  * Reset signals for an exec of the specified process.
396  */
397 void
398 execsigs(struct proc *p)
399 {
400         struct sigacts *ps = p->p_sigacts;
401         struct lwp *lp;
402         int sig;
403
404         lp = ONLY_LWP_IN_PROC(p);
405
406         /*
407          * Reset caught signals.  Held signals remain held
408          * through p_sigmask (unless they were caught,
409          * and are now ignored by default).
410          */
411         while (SIGNOTEMPTY(p->p_sigcatch)) {
412                 sig = sig_ffs(&p->p_sigcatch);
413                 SIGDELSET(p->p_sigcatch, sig);
414                 if (sigprop(sig) & SA_IGNORE) {
415                         if (sig != SIGCONT)
416                                 SIGADDSET(p->p_sigignore, sig);
417                         SIGDELSET(p->p_siglist, sig);
418                         /* don't need spinlock */
419                         SIGDELSET(lp->lwp_siglist, sig);
420                 }
421                 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
422         }
423
424         /*
425          * Reset stack state to the user stack.
426          * Clear set of signals caught on the signal stack.
427          */
428         lp->lwp_sigstk.ss_flags = SS_DISABLE;
429         lp->lwp_sigstk.ss_size = 0;
430         lp->lwp_sigstk.ss_sp = NULL;
431         lp->lwp_flags &= ~LWP_ALTSTACK;
432         /*
433          * Reset no zombies if child dies flag as Solaris does.
434          */
435         p->p_sigacts->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
436         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
437                 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
438 }
439
440 /*
441  * kern_sigprocmask() - MP SAFE ONLY IF p == curproc
442  *
443  *      Manipulate signal mask.  This routine is MP SAFE *ONLY* if
444  *      p == curproc.
445  */
446 int
447 kern_sigprocmask(int how, sigset_t *set, sigset_t *oset)
448 {
449         struct thread *td = curthread;
450         struct lwp *lp = td->td_lwp;
451         struct proc *p = td->td_proc;
452         int error;
453
454         lwkt_gettoken(&p->p_token);
455
456         if (oset != NULL)
457                 *oset = lp->lwp_sigmask;
458
459         error = 0;
460         if (set != NULL) {
461                 switch (how) {
462                 case SIG_BLOCK:
463                         SIG_CANTMASK(*set);
464                         SIGSETOR(lp->lwp_sigmask, *set);
465                         break;
466                 case SIG_UNBLOCK:
467                         SIGSETNAND(lp->lwp_sigmask, *set);
468                         break;
469                 case SIG_SETMASK:
470                         SIG_CANTMASK(*set);
471                         lp->lwp_sigmask = *set;
472                         break;
473                 default:
474                         error = EINVAL;
475                         break;
476                 }
477         }
478
479         lwkt_reltoken(&p->p_token);
480
481         return (error);
482 }
483
484 /*
485  * sigprocmask()
486  *
487  * MPSAFE
488  */
489 int
490 sys_sigprocmask(struct sigprocmask_args *uap)
491 {
492         sigset_t set, oset;
493         sigset_t *setp, *osetp;
494         int error;
495
496         setp = (uap->set != NULL) ? &set : NULL;
497         osetp = (uap->oset != NULL) ? &oset : NULL;
498         if (setp) {
499                 error = copyin(uap->set, setp, sizeof(set));
500                 if (error)
501                         return (error);
502         }
503         error = kern_sigprocmask(uap->how, setp, osetp);
504         if (osetp && !error) {
505                 error = copyout(osetp, uap->oset, sizeof(oset));
506         }
507         return (error);
508 }
509
510 /*
511  * MPSAFE
512  */
513 int
514 kern_sigpending(struct __sigset *set)
515 {
516         struct lwp *lp = curthread->td_lwp;
517
518         *set = lwp_sigpend(lp);
519
520         return (0);
521 }
522
523 /*
524  * MPSAFE
525  */
526 int
527 sys_sigpending(struct sigpending_args *uap)
528 {
529         sigset_t set;
530         int error;
531
532         error = kern_sigpending(&set);
533
534         if (error == 0)
535                 error = copyout(&set, uap->set, sizeof(set));
536         return (error);
537 }
538
539 /*
540  * Suspend process until signal, providing mask to be set
541  * in the meantime.
542  *
543  * MPSAFE
544  */
545 int
546 kern_sigsuspend(struct __sigset *set)
547 {
548         struct thread *td = curthread;
549         struct lwp *lp = td->td_lwp;
550         struct proc *p = td->td_proc;
551         struct sigacts *ps = p->p_sigacts;
552
553         /*
554          * When returning from sigsuspend, we want
555          * the old mask to be restored after the
556          * signal handler has finished.  Thus, we
557          * save it here and mark the sigacts structure
558          * to indicate this.
559          */
560         lp->lwp_oldsigmask = lp->lwp_sigmask;
561         lp->lwp_flags |= LWP_OLDMASK;
562
563         SIG_CANTMASK(*set);
564         lp->lwp_sigmask = *set;
565         while (tsleep(ps, PCATCH, "pause", 0) == 0)
566                 /* void */;
567         /* always return EINTR rather than ERESTART... */
568         return (EINTR);
569 }
570
571 /*
572  * Note nonstandard calling convention: libc stub passes mask, not
573  * pointer, to save a copyin.
574  *
575  * MPSAFE
576  */
577 int
578 sys_sigsuspend(struct sigsuspend_args *uap)
579 {
580         sigset_t mask;
581         int error;
582
583         error = copyin(uap->sigmask, &mask, sizeof(mask));
584         if (error)
585                 return (error);
586
587         error = kern_sigsuspend(&mask);
588
589         return (error);
590 }
591
592 /*
593  * MPSAFE
594  */
595 int
596 kern_sigaltstack(struct sigaltstack *ss, struct sigaltstack *oss)
597 {
598         struct thread *td = curthread;
599         struct lwp *lp = td->td_lwp;
600         struct proc *p = td->td_proc;
601
602         if ((lp->lwp_flags & LWP_ALTSTACK) == 0)
603                 lp->lwp_sigstk.ss_flags |= SS_DISABLE;
604
605         if (oss)
606                 *oss = lp->lwp_sigstk;
607
608         if (ss) {
609                 if (ss->ss_flags & ~SS_DISABLE)
610                         return (EINVAL);
611                 if (ss->ss_flags & SS_DISABLE) {
612                         if (lp->lwp_sigstk.ss_flags & SS_ONSTACK)
613                                 return (EPERM);
614                         lp->lwp_flags &= ~LWP_ALTSTACK;
615                         lp->lwp_sigstk.ss_flags = ss->ss_flags;
616                 } else {
617                         if (ss->ss_size < p->p_sysent->sv_minsigstksz)
618                                 return (ENOMEM);
619                         lp->lwp_flags |= LWP_ALTSTACK;
620                         lp->lwp_sigstk = *ss;
621                 }
622         }
623
624         return (0);
625 }
626
627 /*
628  * MPSAFE
629  */
630 int
631 sys_sigaltstack(struct sigaltstack_args *uap)
632 {
633         stack_t ss, oss;
634         int error;
635
636         if (uap->ss) {
637                 error = copyin(uap->ss, &ss, sizeof(ss));
638                 if (error)
639                         return (error);
640         }
641
642         error = kern_sigaltstack(uap->ss ? &ss : NULL,
643             uap->oss ? &oss : NULL);
644
645         if (error == 0 && uap->oss)
646                 error = copyout(&oss, uap->oss, sizeof(*uap->oss));
647         return (error);
648 }
649
650 /*
651  * Common code for kill process group/broadcast kill.
652  * cp is calling process.
653  */
654 struct killpg_info {
655         int nfound;
656         int sig;
657 };
658
659 static int killpg_all_callback(struct proc *p, void *data);
660
661 static int
662 dokillpg(int sig, int pgid, int all)
663 {
664         struct killpg_info info;
665         struct proc *cp = curproc;
666         struct proc *p;
667         struct pgrp *pgrp;
668
669         info.nfound = 0;
670         info.sig = sig;
671
672         if (all) {
673                 /*
674                  * broadcast
675                  */
676                 allproc_scan(killpg_all_callback, &info);
677         } else {
678                 if (pgid == 0) {
679                         /*
680                          * zero pgid means send to my process group.
681                          */
682                         pgrp = cp->p_pgrp;
683                         pgref(pgrp);
684                 } else {
685                         pgrp = pgfind(pgid);
686                         if (pgrp == NULL)
687                                 return (ESRCH);
688                 }
689
690                 /*
691                  * Must interlock all signals against fork
692                  */
693                 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
694                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
695                         if (p->p_pid <= 1 || 
696                             p->p_stat == SZOMB ||
697                             (p->p_flags & P_SYSTEM) ||
698                             !CANSIGNAL(p, sig)) {
699                                 continue;
700                         }
701                         ++info.nfound;
702                         if (sig)
703                                 ksignal(p, sig);
704                 }
705                 lockmgr(&pgrp->pg_lock, LK_RELEASE);
706                 pgrel(pgrp);
707         }
708         return (info.nfound ? 0 : ESRCH);
709 }
710
711 static int
712 killpg_all_callback(struct proc *p, void *data)
713 {
714         struct killpg_info *info = data;
715
716         if (p->p_pid <= 1 || (p->p_flags & P_SYSTEM) ||
717             p == curproc || !CANSIGNAL(p, info->sig)) {
718                 return (0);
719         }
720         ++info->nfound;
721         if (info->sig)
722                 ksignal(p, info->sig);
723         return(0);
724 }
725
726 /*
727  * Send a general signal to a process or LWPs within that process.
728  *
729  * Note that new signals cannot be sent if a process is exiting or already
730  * a zombie, but we return success anyway as userland is likely to not handle
731  * the race properly.
732  * 
733  * No requirements.
734  */
735 int
736 kern_kill(int sig, pid_t pid, lwpid_t tid)
737 {
738         int t;
739
740         if ((u_int)sig > _SIG_MAXSIG)
741                 return (EINVAL);
742
743         if (pid > 0) {
744                 struct proc *p;
745                 struct lwp *lp = NULL;
746
747                 /*
748                  * Send a signal to a single process.  If the kill() is
749                  * racing an exiting process which has not yet been reaped
750                  * act as though the signal was delivered successfully but
751                  * don't actually try to deliver the signal.
752                  */
753                 if ((p = pfind(pid)) == NULL) {
754                         if ((p = zpfind(pid)) == NULL)
755                                 return (ESRCH);
756                         PRELE(p);
757                         return (0);
758                 }
759                 lwkt_gettoken(&p->p_token);
760                 if (!CANSIGNAL(p, sig)) {
761                         lwkt_reltoken(&p->p_token);
762                         PRELE(p);
763                         return (EPERM);
764                 }
765
766                 /*
767                  * NOP if the process is exiting.  Note that lwpsignal() is
768                  * called directly with P_WEXIT set to kill individual LWPs
769                  * during exit, which is allowed.
770                  */
771                 if (p->p_flags & P_WEXIT) {
772                         lwkt_reltoken(&p->p_token);
773                         PRELE(p);
774                         return (0);
775                 }
776                 if (tid != -1) {
777                         lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, tid);
778                         if (lp == NULL) {
779                                 lwkt_reltoken(&p->p_token);
780                                 PRELE(p);
781                                 return (ESRCH);
782                         }
783                 }
784                 if (sig)
785                         lwpsignal(p, lp, sig);
786                 lwkt_reltoken(&p->p_token);
787                 PRELE(p);
788
789                 return (0);
790         }
791
792         /*
793          * If we come here, pid is a special broadcast pid.
794          * This doesn't mix with a tid.
795          */
796         if (tid != -1)
797                 return (EINVAL);
798
799         switch (pid) {
800         case -1:                /* broadcast signal */
801                 t = (dokillpg(sig, 0, 1));
802                 break;
803         case 0:                 /* signal own process group */
804                 t = (dokillpg(sig, 0, 0));
805                 break;
806         default:                /* negative explicit process group */
807                 t = (dokillpg(sig, -pid, 0));
808                 break;
809         }
810         return t;
811 }
812
813 int
814 sys_kill(struct kill_args *uap)
815 {
816         int error;
817
818         error = kern_kill(uap->signum, uap->pid, -1);
819         return (error);
820 }
821
822 int
823 sys_lwp_kill(struct lwp_kill_args *uap)
824 {
825         int error;
826         pid_t pid = uap->pid;
827
828         /*
829          * A tid is mandatory for lwp_kill(), otherwise
830          * you could simply use kill().
831          */
832         if (uap->tid == -1)
833                 return (EINVAL);
834
835         /*
836          * To save on a getpid() function call for intra-process
837          * signals, pid == -1 means current process.
838          */
839         if (pid == -1)
840                 pid = curproc->p_pid;
841
842         error = kern_kill(uap->signum, pid, uap->tid);
843         return (error);
844 }
845
846 /*
847  * Send a signal to a process group.
848  */
849 void
850 gsignal(int pgid, int sig)
851 {
852         struct pgrp *pgrp;
853
854         if (pgid && (pgrp = pgfind(pgid)))
855                 pgsignal(pgrp, sig, 0);
856 }
857
858 /*
859  * Send a signal to a process group.  If checktty is 1,
860  * limit to members which have a controlling terminal.
861  *
862  * pg_lock interlocks against a fork that might be in progress, to
863  * ensure that the new child process picks up the signal.
864  */
865 void
866 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
867 {
868         struct proc *p;
869
870         /*
871          * Must interlock all signals against fork
872          */
873         if (pgrp) {
874                 pgref(pgrp);
875                 lockmgr(&pgrp->pg_lock, LK_EXCLUSIVE);
876                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
877                         if (checkctty == 0 || p->p_flags & P_CONTROLT)
878                                 ksignal(p, sig);
879                 }
880                 lockmgr(&pgrp->pg_lock, LK_RELEASE);
881                 pgrel(pgrp);
882         }
883 }
884
885 /*
886  * Send a signal caused by a trap to the current lwp.  If it will be caught
887  * immediately, deliver it with correct code.  Otherwise, post it normally.
888  *
889  * These signals may ONLY be delivered to the specified lwp and may never
890  * be delivered to the process generically.
891  */
892 void
893 trapsignal(struct lwp *lp, int sig, u_long code)
894 {
895         struct proc *p = lp->lwp_proc;
896         struct sigacts *ps = p->p_sigacts;
897
898         /*
899          * If we are a virtual kernel running an emulated user process
900          * context, switch back to the virtual kernel context before
901          * trying to post the signal.
902          */
903         if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
904                 struct trapframe *tf = lp->lwp_md.md_regs;
905                 tf->tf_trapno = 0;
906                 vkernel_trap(lp, tf);
907         }
908
909
910         if ((p->p_flags & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
911             !SIGISMEMBER(lp->lwp_sigmask, sig)) {
912                 lp->lwp_ru.ru_nsignals++;
913 #ifdef KTRACE
914                 if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
915                         ktrpsig(lp, sig, ps->ps_sigact[_SIG_IDX(sig)],
916                                 &lp->lwp_sigmask, code);
917 #endif
918                 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
919                                                 &lp->lwp_sigmask, code);
920                 SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
921                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
922                         SIGADDSET(lp->lwp_sigmask, sig);
923                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
924                         /*
925                          * See kern_sigaction() for origin of this code.
926                          */
927                         SIGDELSET(p->p_sigcatch, sig);
928                         if (sig != SIGCONT &&
929                             sigprop(sig) & SA_IGNORE)
930                                 SIGADDSET(p->p_sigignore, sig);
931                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
932                 }
933         } else {
934                 lp->lwp_code = code;    /* XXX for core dump/debugger */
935                 lp->lwp_sig = sig;      /* XXX to verify code */
936                 lwpsignal(p, lp, sig);
937         }
938 }
939
940 /*
941  * Find a suitable lwp to deliver the signal to.  Returns NULL if all
942  * lwps hold the signal blocked.
943  *
944  * Caller must hold p->p_token.
945  *
946  * Returns a lp or NULL.  If non-NULL the lp is held and its token is
947  * acquired.
948  */
949 static struct lwp *
950 find_lwp_for_signal(struct proc *p, int sig)
951 {
952         struct lwp *lp;
953         struct lwp *run, *sleep, *stop;
954
955         /*
956          * If the running/preempted thread belongs to the proc to which
957          * the signal is being delivered and this thread does not block
958          * the signal, then we can avoid a context switch by delivering
959          * the signal to this thread, because it will return to userland
960          * soon anyways.
961          */
962         lp = lwkt_preempted_proc();
963         if (lp != NULL && lp->lwp_proc == p) {
964                 LWPHOLD(lp);
965                 lwkt_gettoken(&lp->lwp_token);
966                 if (!SIGISMEMBER(lp->lwp_sigmask, sig)) {
967                         /* return w/ token held */
968                         return (lp);
969                 }
970                 lwkt_reltoken(&lp->lwp_token);
971                 LWPRELE(lp);
972         }
973
974         run = sleep = stop = NULL;
975         FOREACH_LWP_IN_PROC(lp, p) {
976                 /*
977                  * If the signal is being blocked by the lwp, then this
978                  * lwp is not eligible for receiving the signal.
979                  */
980                 LWPHOLD(lp);
981                 lwkt_gettoken(&lp->lwp_token);
982
983                 if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
984                         lwkt_reltoken(&lp->lwp_token);
985                         LWPRELE(lp);
986                         continue;
987                 }
988
989                 switch (lp->lwp_stat) {
990                 case LSRUN:
991                         if (sleep) {
992                                 lwkt_token_swap();
993                                 lwkt_reltoken(&sleep->lwp_token);
994                                 LWPRELE(sleep);
995                                 sleep = NULL;
996                                 run = lp;
997                         } else if (stop) {
998                                 lwkt_token_swap();
999                                 lwkt_reltoken(&stop->lwp_token);
1000                                 LWPRELE(stop);
1001                                 stop = NULL;
1002                                 run = lp;
1003                         } else {
1004                                 run = lp;
1005                         }
1006                         break;
1007                 case LSSLEEP:
1008                         if (lp->lwp_flags & LWP_SINTR) {
1009                                 if (sleep) {
1010                                         lwkt_reltoken(&lp->lwp_token);
1011                                         LWPRELE(lp);
1012                                 } else if (stop) {
1013                                         lwkt_token_swap();
1014                                         lwkt_reltoken(&stop->lwp_token);
1015                                         LWPRELE(stop);
1016                                         stop = NULL;
1017                                         sleep = lp;
1018                                 } else {
1019                                         sleep = lp;
1020                                 }
1021                         } else {
1022                                 lwkt_reltoken(&lp->lwp_token);
1023                                 LWPRELE(lp);
1024                         }
1025                         break;
1026                 case LSSTOP:
1027                         if (sleep) {
1028                                 lwkt_reltoken(&lp->lwp_token);
1029                                 LWPRELE(lp);
1030                         } else if (stop) {
1031                                 lwkt_reltoken(&lp->lwp_token);
1032                                 LWPRELE(lp);
1033                         } else {
1034                                 stop = lp;
1035                         }
1036                         break;
1037                 }
1038                 if (run)
1039                         break;
1040         }
1041
1042         if (run != NULL)
1043                 return (run);
1044         else if (sleep != NULL)
1045                 return (sleep);
1046         else
1047                 return (stop);
1048 }
1049
1050 /*
1051  * Send the signal to the process.  If the signal has an action, the action
1052  * is usually performed by the target process rather than the caller; we add
1053  * the signal to the set of pending signals for the process.
1054  *
1055  * Exceptions:
1056  *   o When a stop signal is sent to a sleeping process that takes the
1057  *     default action, the process is stopped without awakening it.
1058  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1059  *     regardless of the signal action (eg, blocked or ignored).
1060  *
1061  * Other ignored signals are discarded immediately.
1062  *
1063  * If the caller wishes to call this function from a hard code section the
1064  * caller must already hold p->p_token (see kern_clock.c).
1065  *
1066  * No requirements.
1067  */
1068 void
1069 ksignal(struct proc *p, int sig)
1070 {
1071         lwpsignal(p, NULL, sig);
1072 }
1073
1074 /*
1075  * The core for ksignal.  lp may be NULL, then a suitable thread
1076  * will be chosen.  If not, lp MUST be a member of p.
1077  *
1078  * If the caller wishes to call this function from a hard code section the
1079  * caller must already hold p->p_token.
1080  *
1081  * No requirements.
1082  */
1083 void
1084 lwpsignal(struct proc *p, struct lwp *lp, int sig)
1085 {
1086         struct proc *q;
1087         sig_t action;
1088         int prop;
1089
1090         if (sig > _SIG_MAXSIG || sig <= 0) {
1091                 kprintf("lwpsignal: signal %d\n", sig);
1092                 panic("lwpsignal signal number");
1093         }
1094
1095         KKASSERT(lp == NULL || lp->lwp_proc == p);
1096
1097         /*
1098          * We don't want to race... well, all sorts of things.  Get appropriate
1099          * tokens.
1100          *
1101          * Don't try to deliver a generic signal to an exiting process,
1102          * the signal structures could be in flux.  We check the LWP later
1103          * on.
1104          */
1105         PHOLD(p);
1106         lwkt_gettoken(&p->p_token);
1107         if (lp) {
1108                 LWPHOLD(lp);
1109                 lwkt_gettoken(&lp->lwp_token);
1110         } else if (p->p_flags & P_WEXIT) {
1111                 goto out;
1112         }
1113
1114         prop = sigprop(sig);
1115
1116         /*
1117          * If proc is traced, always give parent a chance;
1118          * if signal event is tracked by procfs, give *that*
1119          * a chance, as well.
1120          */
1121         if ((p->p_flags & P_TRACED) || (p->p_stops & S_SIG)) {
1122                 action = SIG_DFL;
1123         } else {
1124                 /*
1125                  * Do not try to deliver signals to an exiting lwp.  Note
1126                  * that we must still deliver the signal if P_WEXIT is set
1127                  * in the process flags.
1128                  */
1129                 if (lp && (lp->lwp_mpflags & LWP_MP_WEXIT)) {
1130                         if (lp) {
1131                                 lwkt_reltoken(&lp->lwp_token);
1132                                 LWPRELE(lp);
1133                         }
1134                         lwkt_reltoken(&p->p_token);
1135                         PRELE(p);
1136                         return;
1137                 }
1138
1139                 /*
1140                  * If the signal is being ignored, then we forget about
1141                  * it immediately.  NOTE: We don't set SIGCONT in p_sigignore,
1142                  * and if it is set to SIG_IGN, action will be SIG_DFL here.
1143                  */
1144                 if (SIGISMEMBER(p->p_sigignore, sig)) {
1145                         /*
1146                          * Even if a signal is set SIG_IGN, it may still be
1147                          * lurking in a kqueue.
1148                          */
1149                         KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1150                         if (lp) {
1151                                 lwkt_reltoken(&lp->lwp_token);
1152                                 LWPRELE(lp);
1153                         }
1154                         lwkt_reltoken(&p->p_token);
1155                         PRELE(p);
1156                         return;
1157                 }
1158                 if (SIGISMEMBER(p->p_sigcatch, sig))
1159                         action = SIG_CATCH;
1160                 else
1161                         action = SIG_DFL;
1162         }
1163
1164         /*
1165          * If continuing, clear any pending STOP signals.
1166          */
1167         if (prop & SA_CONT)
1168                 SIG_STOPSIGMASK(p->p_siglist);
1169         
1170         if (prop & SA_STOP) {
1171                 /*
1172                  * If sending a tty stop signal to a member of an orphaned
1173                  * process group, discard the signal here if the action
1174                  * is default; don't stop the process below if sleeping,
1175                  * and don't clear any pending SIGCONT.
1176                  */
1177                 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1178                     action == SIG_DFL) {
1179                         if (lp) {
1180                                 lwkt_reltoken(&lp->lwp_token);
1181                                 LWPRELE(lp);
1182                         }
1183                         lwkt_reltoken(&p->p_token);
1184                         PRELE(p);
1185                         return;
1186                 }
1187                 SIG_CONTSIGMASK(p->p_siglist);
1188                 p->p_flags &= ~P_CONTINUED;
1189         }
1190
1191         if (p->p_stat == SSTOP) {
1192                 /*
1193                  * Nobody can handle this signal, add it to the lwp or
1194                  * process pending list 
1195                  */
1196                 if (lp) {
1197                         spin_lock(&lp->lwp_spin);
1198                         SIGADDSET(lp->lwp_siglist, sig);
1199                         spin_unlock(&lp->lwp_spin);
1200                 } else {
1201                         SIGADDSET(p->p_siglist, sig);
1202                 }
1203
1204                 /*
1205                  * If the process is stopped and is being traced, then no
1206                  * further action is necessary.
1207                  */
1208                 if (p->p_flags & P_TRACED)
1209                         goto out;
1210
1211                 /*
1212                  * If the process is stopped and receives a KILL signal,
1213                  * make the process runnable.
1214                  */
1215                 if (sig == SIGKILL) {
1216                         proc_unstop(p, SSTOP);
1217                         goto active_process;
1218                 }
1219
1220                 /*
1221                  * If the process is stopped and receives a CONT signal,
1222                  * then try to make the process runnable again.
1223                  */
1224                 if (prop & SA_CONT) {
1225                         /*
1226                          * If SIGCONT is default (or ignored), we continue the
1227                          * process but don't leave the signal in p_siglist, as
1228                          * it has no further action.  If SIGCONT is held, we
1229                          * continue the process and leave the signal in
1230                          * p_siglist.  If the process catches SIGCONT, let it
1231                          * handle the signal itself.
1232                          *
1233                          * XXX what if the signal is being held blocked?
1234                          *
1235                          * Token required to interlock kern_wait().
1236                          * Reparenting can also cause a race so we have to
1237                          * hold (q).
1238                          */
1239                         q = p->p_pptr;
1240                         PHOLD(q);
1241                         lwkt_gettoken(&q->p_token);
1242                         p->p_flags |= P_CONTINUED;
1243                         wakeup(q);
1244                         if (action == SIG_DFL)
1245                                 SIGDELSET(p->p_siglist, sig);
1246                         proc_unstop(p, SSTOP);
1247                         lwkt_reltoken(&q->p_token);
1248                         PRELE(q);
1249                         if (action == SIG_CATCH)
1250                                 goto active_process;
1251                         goto out;
1252                 }
1253
1254                 /*
1255                  * If the process is stopped and receives another STOP
1256                  * signal, we do not need to stop it again.  If we did
1257                  * the shell could get confused.
1258                  *
1259                  * However, if the current/preempted lwp is part of the
1260                  * process receiving the signal, we need to keep it,
1261                  * so that this lwp can stop in issignal() later, as
1262                  * we don't want to wait until it reaches userret!
1263                  */
1264                 if (prop & SA_STOP) {
1265                         if (lwkt_preempted_proc() == NULL ||
1266                             lwkt_preempted_proc()->lwp_proc != p)
1267                                 SIGDELSET(p->p_siglist, sig);
1268                 }
1269
1270                 /*
1271                  * Otherwise the process is stopped and it received some
1272                  * signal, which does not change its stopped state.  When
1273                  * the process is continued a wakeup(p) will be issued which
1274                  * will wakeup any threads sleeping in tstop().
1275                  */
1276                 if (lp == NULL) {
1277                         /* NOTE: returns lp w/ token held */
1278                         lp = find_lwp_for_signal(p, sig);
1279                 }
1280                 goto out;
1281
1282                 /* NOTREACHED */
1283         }
1284         /* else not stopped */
1285 active_process:
1286
1287         /*
1288          * Never deliver a lwp-specific signal to a random lwp.
1289          */
1290         if (lp == NULL) {
1291                 /* NOTE: returns lp w/ token held */
1292                 lp = find_lwp_for_signal(p, sig);
1293                 if (lp) {
1294                         if (SIGISMEMBER(lp->lwp_sigmask, sig)) {
1295                                 lwkt_reltoken(&lp->lwp_token);
1296                                 LWPRELE(lp);
1297                                 lp = NULL;
1298                         }
1299                 }
1300         }
1301
1302         /*
1303          * Deliver to the process generically if (1) the signal is being
1304          * sent to any thread or (2) we could not find a thread to deliver
1305          * it to.
1306          */
1307         if (lp == NULL) {
1308                 KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1309                 SIGADDSET(p->p_siglist, sig);
1310                 goto out;
1311         }
1312
1313         /*
1314          * Deliver to a specific LWP whether it masks it or not.  It will
1315          * not be dispatched if masked but we must still deliver it.
1316          */
1317         if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1318             (p->p_flags & P_TRACED) == 0) {
1319                 p->p_nice = NZERO;
1320         }
1321
1322         /*
1323          * If the process receives a STOP signal which indeed needs to
1324          * stop the process, do so.  If the process chose to catch the
1325          * signal, it will be treated like any other signal.
1326          */
1327         if ((prop & SA_STOP) && action == SIG_DFL) {
1328                 /*
1329                  * If a child holding parent blocked, stopping
1330                  * could cause deadlock.  Take no action at this
1331                  * time.
1332                  */
1333                 if (p->p_flags & P_PPWAIT) {
1334                         SIGADDSET(p->p_siglist, sig);
1335                         goto out;
1336                 }
1337
1338                 /*
1339                  * Do not actually try to manipulate the process, but simply
1340                  * stop it.  Lwps will stop as soon as they safely can.
1341                  *
1342                  * Ignore stop if the process is exiting.
1343                  */
1344                 if ((p->p_flags & P_WEXIT) == 0) {
1345                         p->p_xstat = sig;
1346                         proc_stop(p, SSTOP);
1347                 }
1348                 goto out;
1349         }
1350
1351         /*
1352          * If it is a CONT signal with default action, just ignore it.
1353          */
1354         if ((prop & SA_CONT) && action == SIG_DFL)
1355                 goto out;
1356
1357         /*
1358          * Mark signal pending at this specific thread.
1359          */
1360         spin_lock(&lp->lwp_spin);
1361         SIGADDSET(lp->lwp_siglist, sig);
1362         spin_unlock(&lp->lwp_spin);
1363
1364         lwp_signotify(lp);
1365
1366 out:
1367         if (lp) {
1368                 lwkt_reltoken(&lp->lwp_token);
1369                 LWPRELE(lp);
1370         }
1371         lwkt_reltoken(&p->p_token);
1372         PRELE(p);
1373 }
1374
1375 /*
1376  * Notify the LWP that a signal has arrived.  The LWP does not have to be
1377  * sleeping on the current cpu.
1378  *
1379  * p->p_token and lp->lwp_token must be held on call.
1380  *
1381  * We can only safely schedule the thread on its current cpu and only if
1382  * one of the SINTR flags is set.  If an SINTR flag is set AND we are on
1383  * the correct cpu we are properly interlocked, otherwise we could be
1384  * racing other thread transition states (or the lwp is on the user scheduler
1385  * runq but not scheduled) and must not do anything.
1386  *
1387  * Since we hold the lwp token we know the lwp cannot be ripped out from
1388  * under us so we can safely hold it to prevent it from being ripped out
1389  * from under us if we are forced to IPI another cpu to make the local
1390  * checks there.
1391  *
1392  * Adjustment of lp->lwp_stat can only occur when we hold the lwp_token,
1393  * which we won't in an IPI so any fixups have to be done here, effectively
1394  * replicating part of what setrunnable() does.
1395  */
1396 static void
1397 lwp_signotify(struct lwp *lp)
1398 {
1399         thread_t dtd;
1400
1401         ASSERT_LWKT_TOKEN_HELD(&lp->lwp_proc->p_token);
1402         dtd = lp->lwp_thread;
1403
1404         crit_enter();
1405         if (lp == lwkt_preempted_proc()) {
1406                 /*
1407                  * lwp is on the current cpu AND it is currently running
1408                  * (we preempted it).
1409                  */
1410                 signotify();
1411         } else if (lp->lwp_flags & LWP_SINTR) {
1412                 /*
1413                  * lwp is sitting in tsleep() with PCATCH set
1414                  */
1415                 if (dtd->td_gd == mycpu) {
1416                         setrunnable(lp);
1417                 } else {
1418                         /*
1419                          * We can only adjust lwp_stat while we hold the
1420                          * lwp_token, and we won't in the IPI function.
1421                          */
1422                         LWPHOLD(lp);
1423                         if (lp->lwp_stat == LSSTOP)
1424                                 lp->lwp_stat = LSSLEEP;
1425                         lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1426                 }
1427         } else if (dtd->td_flags & TDF_SINTR) {
1428                 /*
1429                  * lwp is sitting in lwkt_sleep() with PCATCH set.
1430                  */
1431                 if (dtd->td_gd == mycpu) {
1432                         setrunnable(lp);
1433                 } else {
1434                         /*
1435                          * We can only adjust lwp_stat while we hold the
1436                          * lwp_token, and we won't in the IPI function.
1437                          */
1438                         LWPHOLD(lp);
1439                         if (lp->lwp_stat == LSSTOP)
1440                                 lp->lwp_stat = LSSLEEP;
1441                         lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1442                 }
1443         } else {
1444                 /*
1445                  * Otherwise the lwp is either in some uninterruptible state
1446                  * or it is on the userland scheduler's runqueue waiting to
1447                  * be scheduled to a cpu, or it is running in userland.  We
1448                  * generally want to send an IPI so a running target gets the
1449                  * signal ASAP, otherwise a scheduler-tick worth of latency
1450                  * will occur.
1451                  *
1452                  * Issue an IPI to the remote cpu to knock it into the kernel,
1453                  * remote cpu will issue the cpu-local signotify() if the IPI
1454                  * preempts the desired thread.
1455                  */
1456                 if (dtd->td_gd != mycpu) {
1457                         LWPHOLD(lp);
1458                         lwkt_send_ipiq(dtd->td_gd, lwp_signotify_remote, lp);
1459                 }
1460         }
1461         crit_exit();
1462 }
1463
1464 /*
1465  * This function is called via an IPI so we cannot call setrunnable() here
1466  * (because while we hold the lp we don't own its token, and can't get it
1467  * from an IPI).
1468  *
1469  * We are interlocked by virtue of being on the same cpu as the target.  If
1470  * we still are and LWP_SINTR or TDF_SINTR is set we can safely schedule
1471  * the target thread.
1472  */
1473 static void
1474 lwp_signotify_remote(void *arg)
1475 {
1476         struct lwp *lp = arg;
1477         thread_t td = lp->lwp_thread;
1478
1479         if (lp == lwkt_preempted_proc()) {
1480                 signotify();
1481                 LWPRELE(lp);
1482         } else if (td->td_gd == mycpu) {
1483                 if ((lp->lwp_flags & LWP_SINTR) ||
1484                     (td->td_flags & TDF_SINTR)) {
1485                         lwkt_schedule(td);
1486                 }
1487                 LWPRELE(lp);
1488         } else {
1489                 lwkt_send_ipiq(td->td_gd, lwp_signotify_remote, lp);
1490                 /* LWPHOLD() is forwarded to the target cpu */
1491         }
1492 }
1493
1494 /*
1495  * Caller must hold p->p_token
1496  */
1497 void
1498 proc_stop(struct proc *p, int sig)
1499 {
1500         struct proc *q;
1501         struct lwp *lp;
1502
1503         ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1504
1505         /*
1506          * If somebody raced us, be happy with it.  SCORE overrides SSTOP.
1507          */
1508         if (sig == SCORE) {
1509                 if (p->p_stat == SCORE || p->p_stat == SZOMB)
1510                         return;
1511         } else {
1512                 if (p->p_stat == SSTOP || p->p_stat == SCORE ||
1513                     p->p_stat == SZOMB) {
1514                         return;
1515                 }
1516         }
1517         p->p_stat = sig;
1518
1519         FOREACH_LWP_IN_PROC(lp, p) {
1520                 LWPHOLD(lp);
1521                 lwkt_gettoken(&lp->lwp_token);
1522
1523                 switch (lp->lwp_stat) {
1524                 case LSSTOP:
1525                         /*
1526                          * Do nothing, we are already counted in
1527                          * p_nstopped.
1528                          */
1529                         break;
1530
1531                 case LSSLEEP:
1532                         /*
1533                          * We're sleeping, but we will stop before
1534                          * returning to userspace, so count us
1535                          * as stopped as well.  We set LWP_MP_WSTOP
1536                          * to signal the lwp that it should not
1537                          * increase p_nstopped when reaching tstop().
1538                          *
1539                          * LWP_MP_WSTOP is protected by lp->lwp_token.
1540                          */
1541                         if ((lp->lwp_mpflags & LWP_MP_WSTOP) == 0) {
1542                                 atomic_set_int(&lp->lwp_mpflags, LWP_MP_WSTOP);
1543                                 ++p->p_nstopped;
1544                         }
1545                         break;
1546
1547                 case LSRUN:
1548                         /*
1549                          * We might notify ourself, but that's not
1550                          * a problem.
1551                          */
1552                         lwp_signotify(lp);
1553                         break;
1554                 }
1555                 lwkt_reltoken(&lp->lwp_token);
1556                 LWPRELE(lp);
1557         }
1558
1559         if (p->p_nstopped == p->p_nthreads) {
1560                 /*
1561                  * Token required to interlock kern_wait().  Reparenting can
1562                  * also cause a race so we have to hold (q).
1563                  */
1564                 q = p->p_pptr;
1565                 PHOLD(q);
1566                 lwkt_gettoken(&q->p_token);
1567                 p->p_flags &= ~P_WAITED;
1568                 wakeup(q);
1569                 if ((q->p_sigacts->ps_flag & PS_NOCLDSTOP) == 0)
1570                         ksignal(p->p_pptr, SIGCHLD);
1571                 lwkt_reltoken(&q->p_token);
1572                 PRELE(q);
1573         }
1574 }
1575
1576 /*
1577  * Caller must hold p_token
1578  */
1579 void
1580 proc_unstop(struct proc *p, int sig)
1581 {
1582         struct lwp *lp;
1583
1584         ASSERT_LWKT_TOKEN_HELD(&p->p_token);
1585
1586         if (p->p_stat != sig)
1587                 return;
1588
1589         p->p_stat = SACTIVE;
1590
1591         FOREACH_LWP_IN_PROC(lp, p) {
1592                 LWPHOLD(lp);
1593                 lwkt_gettoken(&lp->lwp_token);
1594
1595                 switch (lp->lwp_stat) {
1596                 case LSRUN:
1597                         /*
1598                          * Uh?  Not stopped?  Well, I guess that's okay.
1599                          */
1600                         if (bootverbose)
1601                                 kprintf("proc_unstop: lwp %d/%d not sleeping\n",
1602                                         p->p_pid, lp->lwp_tid);
1603                         break;
1604
1605                 case LSSLEEP:
1606                         /*
1607                          * Still sleeping.  Don't bother waking it up.
1608                          * However, if this thread was counted as
1609                          * stopped, undo this.
1610                          *
1611                          * Nevertheless we call setrunnable() so that it
1612                          * will wake up in case a signal or timeout arrived
1613                          * in the meantime.
1614                          *
1615                          * LWP_MP_WSTOP is protected by lp->lwp_token.
1616                          */
1617                         if (lp->lwp_mpflags & LWP_MP_WSTOP) {
1618                                 atomic_clear_int(&lp->lwp_mpflags,
1619                                                  LWP_MP_WSTOP);
1620                                 --p->p_nstopped;
1621                         } else {
1622                                 if (bootverbose)
1623                                         kprintf("proc_unstop: lwp %d/%d sleeping, not stopped\n",
1624                                                 p->p_pid, lp->lwp_tid);
1625                         }
1626                         /* FALLTHROUGH */
1627
1628                 case LSSTOP:
1629                         /*
1630                          * This handles any lwp's waiting in a tsleep with
1631                          * SIGCATCH.
1632                          */
1633                         lwp_signotify(lp);
1634                         break;
1635
1636                 }
1637                 lwkt_reltoken(&lp->lwp_token);
1638                 LWPRELE(lp);
1639         }
1640
1641         /*
1642          * This handles any lwp's waiting in tstop().  We have interlocked
1643          * the setting of p_stat by acquiring and releasing each lpw's
1644          * token.
1645          */
1646         wakeup(p);
1647 }
1648
1649 /*
1650  * Wait for all threads except the current thread to stop.
1651  */
1652 static void
1653 proc_stopwait(struct proc *p)
1654 {
1655         while ((p->p_stat == SSTOP || p->p_stat == SCORE) &&
1656                p->p_nstopped < p->p_nthreads - 1) {
1657                 tsleep_interlock(&p->p_nstopped, 0);
1658                 if (p->p_nstopped < p->p_nthreads - 1) {
1659                         tsleep(&p->p_nstopped, PINTERLOCKED, "stopwt", hz);
1660                 }
1661         }
1662 }
1663
1664 /* 
1665  * No requirements.
1666  */
1667 static int
1668 kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
1669 {
1670         sigset_t savedmask, set;
1671         struct proc *p = curproc;
1672         struct lwp *lp = curthread->td_lwp;
1673         int error, sig, hz, timevalid = 0;
1674         struct timespec rts, ets, ts;
1675         struct timeval tv;
1676
1677         error = 0;
1678         sig = 0;
1679         ets.tv_sec = 0;         /* silence compiler warning */
1680         ets.tv_nsec = 0;        /* silence compiler warning */
1681         SIG_CANTMASK(waitset);
1682         savedmask = lp->lwp_sigmask;
1683
1684         if (timeout) {
1685                 if (timeout->tv_sec >= 0 && timeout->tv_nsec >= 0 &&
1686                     timeout->tv_nsec < 1000000000) {
1687                         timevalid = 1;
1688                         getnanouptime(&rts);
1689                         ets = rts;
1690                         timespecadd(&ets, timeout);
1691                 }
1692         }
1693
1694         for (;;) {
1695                 set = lwp_sigpend(lp);
1696                 SIGSETAND(set, waitset);
1697                 if ((sig = sig_ffs(&set)) != 0) {
1698                         SIGFILLSET(lp->lwp_sigmask);
1699                         SIGDELSET(lp->lwp_sigmask, sig);
1700                         SIG_CANTMASK(lp->lwp_sigmask);
1701                         sig = issignal(lp, 1);
1702                         /*
1703                          * It may be a STOP signal, in the case, issignal
1704                          * returns 0, because we may stop there, and new
1705                          * signal can come in, we should restart if we got
1706                          * nothing.
1707                          */
1708                         if (sig == 0)
1709                                 continue;
1710                         else
1711                                 break;
1712                 }
1713
1714                 /*
1715                  * Previous checking got nothing, and we retried but still
1716                  * got nothing, we should return the error status.
1717                  */
1718                 if (error)
1719                         break;
1720
1721                 /*
1722                  * POSIX says this must be checked after looking for pending
1723                  * signals.
1724                  */
1725                 if (timeout) {
1726                         if (timevalid == 0) {
1727                                 error = EINVAL;
1728                                 break;
1729                         }
1730                         getnanouptime(&rts);
1731                         if (timespeccmp(&rts, &ets, >=)) {
1732                                 error = EAGAIN;
1733                                 break;
1734                         }
1735                         ts = ets;
1736                         timespecsub(&ts, &rts);
1737                         TIMESPEC_TO_TIMEVAL(&tv, &ts);
1738                         hz = tvtohz_high(&tv);
1739                 } else {
1740                         hz = 0;
1741                 }
1742
1743                 lp->lwp_sigmask = savedmask;
1744                 SIGSETNAND(lp->lwp_sigmask, waitset);
1745                 /*
1746                  * We won't ever be woken up.  Instead, our sleep will
1747                  * be broken in lwpsignal().
1748                  */
1749                 error = tsleep(&p->p_sigacts, PCATCH, "sigwt", hz);
1750                 if (timeout) {
1751                         if (error == ERESTART) {
1752                                 /* can not restart a timeout wait. */
1753                                 error = EINTR;
1754                         } else if (error == EAGAIN) {
1755                                 /* will calculate timeout by ourself. */
1756                                 error = 0;
1757                         }
1758                 }
1759                 /* Retry ... */
1760         }
1761
1762         lp->lwp_sigmask = savedmask;
1763         if (sig) {
1764                 error = 0;
1765                 bzero(info, sizeof(*info));
1766                 info->si_signo = sig;
1767                 spin_lock(&lp->lwp_spin);
1768                 lwp_delsig(lp, sig);    /* take the signal! */
1769                 spin_unlock(&lp->lwp_spin);
1770
1771                 if (sig == SIGKILL) {
1772                         sigexit(lp, sig);
1773                         /* NOT REACHED */
1774                 }
1775         }
1776
1777         return (error);
1778 }
1779
1780 /*
1781  * MPALMOSTSAFE
1782  */
1783 int
1784 sys_sigtimedwait(struct sigtimedwait_args *uap)
1785 {
1786         struct timespec ts;
1787         struct timespec *timeout;
1788         sigset_t set;
1789         siginfo_t info;
1790         int error;
1791
1792         if (uap->timeout) {
1793                 error = copyin(uap->timeout, &ts, sizeof(ts));
1794                 if (error)
1795                         return (error);
1796                 timeout = &ts;
1797         } else {
1798                 timeout = NULL;
1799         }
1800         error = copyin(uap->set, &set, sizeof(set));
1801         if (error)
1802                 return (error);
1803         error = kern_sigtimedwait(set, &info, timeout);
1804         if (error)
1805                 return (error);
1806         if (uap->info)
1807                 error = copyout(&info, uap->info, sizeof(info));
1808         /* Repost if we got an error. */
1809         /*
1810          * XXX lwp
1811          *
1812          * This could transform a thread-specific signal to another
1813          * thread / process pending signal.
1814          */
1815         if (error) {
1816                 ksignal(curproc, info.si_signo);
1817         } else {
1818                 uap->sysmsg_result = info.si_signo;
1819         }
1820         return (error);
1821 }
1822
1823 /*
1824  * MPALMOSTSAFE
1825  */
1826 int
1827 sys_sigwaitinfo(struct sigwaitinfo_args *uap)
1828 {
1829         siginfo_t info;
1830         sigset_t set;
1831         int error;
1832
1833         error = copyin(uap->set, &set, sizeof(set));
1834         if (error)
1835                 return (error);
1836         error = kern_sigtimedwait(set, &info, NULL);
1837         if (error)
1838                 return (error);
1839         if (uap->info)
1840                 error = copyout(&info, uap->info, sizeof(info));
1841         /* Repost if we got an error. */
1842         /*
1843          * XXX lwp
1844          *
1845          * This could transform a thread-specific signal to another
1846          * thread / process pending signal.
1847          */
1848         if (error) {
1849                 ksignal(curproc, info.si_signo);
1850         } else {
1851                 uap->sysmsg_result = info.si_signo;
1852         }
1853         return (error);
1854 }
1855
1856 /*
1857  * If the current process has received a signal that would interrupt a
1858  * system call, return EINTR or ERESTART as appropriate.
1859  */
1860 int
1861 iscaught(struct lwp *lp)
1862 {
1863         struct proc *p = lp->lwp_proc;
1864         int sig;
1865
1866         if (p) {
1867                 if ((sig = CURSIG(lp)) != 0) {
1868                         if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
1869                                 return (EINTR);                        
1870                         return (ERESTART);     
1871                 }                         
1872         }
1873         return(EWOULDBLOCK);
1874 }
1875
1876 /*
1877  * If the current process has received a signal (should be caught or cause
1878  * termination, should interrupt current syscall), return the signal number.
1879  * Stop signals with default action are processed immediately, then cleared;
1880  * they aren't returned.  This is checked after each entry to the system for
1881  * a syscall or trap (though this can usually be done without calling issignal
1882  * by checking the pending signal masks in the CURSIG macro).
1883  *
1884  * This routine is called via CURSIG/__cursig.  We will acquire and release
1885  * p->p_token but if the caller needs to interlock the test the caller must
1886  * also hold p->p_token.
1887  *
1888  *      while (sig = CURSIG(curproc))
1889  *              postsig(sig);
1890  *
1891  * MPSAFE
1892  */
1893 int
1894 issignal(struct lwp *lp, int maytrace)
1895 {
1896         struct proc *p = lp->lwp_proc;
1897         sigset_t mask;
1898         int sig, prop;
1899
1900         lwkt_gettoken(&p->p_token);
1901
1902         for (;;) {
1903                 int traced = (p->p_flags & P_TRACED) || (p->p_stops & S_SIG);
1904
1905                 /*
1906                  * If this process is supposed to stop, stop this thread.
1907                  */
1908                 if (p->p_stat == SSTOP || p->p_stat == SCORE)
1909                         tstop();
1910
1911                 mask = lwp_sigpend(lp);
1912                 SIGSETNAND(mask, lp->lwp_sigmask);
1913                 if (p->p_flags & P_PPWAIT)
1914                         SIG_STOPSIGMASK(mask);
1915                 if (SIGISEMPTY(mask)) {         /* no signal to send */
1916                         lwkt_reltoken(&p->p_token);
1917                         return (0);
1918                 }
1919                 sig = sig_ffs(&mask);
1920
1921                 STOPEVENT(p, S_SIG, sig);
1922
1923                 /*
1924                  * We should see pending but ignored signals
1925                  * only if P_TRACED was on when they were posted.
1926                  */
1927                 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1928                         spin_lock(&lp->lwp_spin);
1929                         lwp_delsig(lp, sig);
1930                         spin_unlock(&lp->lwp_spin);
1931                         continue;
1932                 }
1933                 if (maytrace &&
1934                     (p->p_flags & P_TRACED) &&
1935                     (p->p_flags & P_PPWAIT) == 0) {
1936                         /*
1937                          * If traced, always stop, and stay stopped until
1938                          * released by the parent.
1939                          *
1940                          * NOTE: SSTOP may get cleared during the loop,
1941                          * but we do not re-notify the parent if we have 
1942                          * to loop several times waiting for the parent
1943                          * to let us continue.
1944                          *
1945                          * XXX not sure if this is still true
1946                          */
1947                         p->p_xstat = sig;
1948                         proc_stop(p, SSTOP);
1949                         do {
1950                                 tstop();
1951                         } while (!trace_req(p) && (p->p_flags & P_TRACED));
1952
1953                         /*
1954                          * If parent wants us to take the signal,
1955                          * then it will leave it in p->p_xstat;
1956                          * otherwise we just look for signals again.
1957                          */
1958                         spin_lock(&lp->lwp_spin);
1959                         lwp_delsig(lp, sig);    /* clear old signal */
1960                         spin_unlock(&lp->lwp_spin);
1961                         sig = p->p_xstat;
1962                         if (sig == 0)
1963                                 continue;
1964
1965                         /*
1966                          * Put the new signal into p_siglist.  If the
1967                          * signal is being masked, look for other signals.
1968                          *
1969                          * XXX lwp might need a call to ksignal()
1970                          */
1971                         SIGADDSET(p->p_siglist, sig);
1972                         if (SIGISMEMBER(lp->lwp_sigmask, sig))
1973                                 continue;
1974
1975                         /*
1976                          * If the traced bit got turned off, go back up
1977                          * to the top to rescan signals.  This ensures
1978                          * that p_sig* and ps_sigact are consistent.
1979                          */
1980                         if ((p->p_flags & P_TRACED) == 0)
1981                                 continue;
1982                 }
1983
1984                 prop = sigprop(sig);
1985
1986                 /*
1987                  * Decide whether the signal should be returned.
1988                  * Return the signal's number, or fall through
1989                  * to clear it from the pending mask.
1990                  */
1991                 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1992                 case (intptr_t)SIG_DFL:
1993                         /*
1994                          * Don't take default actions on system processes.
1995                          */
1996                         if (p->p_pid <= 1) {
1997 #ifdef DIAGNOSTIC
1998                                 /*
1999                                  * Are you sure you want to ignore SIGSEGV
2000                                  * in init? XXX
2001                                  */
2002                                 kprintf("Process (pid %lu) got signal %d\n",
2003                                         (u_long)p->p_pid, sig);
2004 #endif
2005                                 break;          /* == ignore */
2006                         }
2007
2008                         /*
2009                          * Handle the in-kernel checkpoint action
2010                          */
2011                         if (prop & SA_CKPT) {
2012                                 checkpoint_signal_handler(lp);
2013                                 break;
2014                         }
2015
2016                         /*
2017                          * If there is a pending stop signal to process
2018                          * with default action, stop here,
2019                          * then clear the signal.  However,
2020                          * if process is member of an orphaned
2021                          * process group, ignore tty stop signals.
2022                          */
2023                         if (prop & SA_STOP) {
2024                                 if (p->p_flags & P_TRACED ||
2025                                     (p->p_pgrp->pg_jobc == 0 &&
2026                                     prop & SA_TTYSTOP))
2027                                         break;  /* == ignore */
2028                                 if ((p->p_flags & P_WEXIT) == 0) {
2029                                         p->p_xstat = sig;
2030                                         proc_stop(p, SSTOP);
2031                                         tstop();
2032                                 }
2033                                 break;
2034                         } else if (prop & SA_IGNORE) {
2035                                 /*
2036                                  * Except for SIGCONT, shouldn't get here.
2037                                  * Default action is to ignore; drop it.
2038                                  */
2039                                 break;          /* == ignore */
2040                         } else {
2041                                 lwkt_reltoken(&p->p_token);
2042                                 return (sig);
2043                         }
2044
2045                         /*NOTREACHED*/
2046
2047                 case (intptr_t)SIG_IGN:
2048                         /*
2049                          * Masking above should prevent us ever trying
2050                          * to take action on an ignored signal other
2051                          * than SIGCONT, unless process is traced.
2052                          */
2053                         if ((prop & SA_CONT) == 0 &&
2054                             (p->p_flags & P_TRACED) == 0)
2055                                 kprintf("issignal\n");
2056                         break;          /* == ignore */
2057
2058                 default:
2059                         /*
2060                          * This signal has an action, let
2061                          * postsig() process it.
2062                          */
2063                         lwkt_reltoken(&p->p_token);
2064                         return (sig);
2065                 }
2066                 spin_lock(&lp->lwp_spin);
2067                 lwp_delsig(lp, sig);            /* take the signal! */
2068                 spin_unlock(&lp->lwp_spin);
2069         }
2070         /* NOTREACHED */
2071 }
2072
2073 /*
2074  * Take the action for the specified signal
2075  * from the current set of pending signals.
2076  *
2077  * Caller must hold p->p_token
2078  */
2079 void
2080 postsig(int sig)
2081 {
2082         struct lwp *lp = curthread->td_lwp;
2083         struct proc *p = lp->lwp_proc;
2084         struct sigacts *ps = p->p_sigacts;
2085         sig_t action;
2086         sigset_t returnmask;
2087         int code;
2088
2089         KASSERT(sig != 0, ("postsig"));
2090
2091         KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
2092
2093         /*
2094          * If we are a virtual kernel running an emulated user process
2095          * context, switch back to the virtual kernel context before
2096          * trying to post the signal.
2097          */
2098         if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
2099                 struct trapframe *tf = lp->lwp_md.md_regs;
2100                 tf->tf_trapno = 0;
2101                 vkernel_trap(lp, tf);
2102         }
2103
2104         spin_lock(&lp->lwp_spin);
2105         lwp_delsig(lp, sig);
2106         spin_unlock(&lp->lwp_spin);
2107         action = ps->ps_sigact[_SIG_IDX(sig)];
2108 #ifdef KTRACE
2109         if (KTRPOINT(lp->lwp_thread, KTR_PSIG))
2110                 ktrpsig(lp, sig, action, lp->lwp_flags & LWP_OLDMASK ?
2111                         &lp->lwp_oldsigmask : &lp->lwp_sigmask, 0);
2112 #endif
2113         STOPEVENT(p, S_SIG, sig);
2114
2115         if (action == SIG_DFL) {
2116                 /*
2117                  * Default action, where the default is to kill
2118                  * the process.  (Other cases were ignored above.)
2119                  */
2120                 sigexit(lp, sig);
2121                 /* NOTREACHED */
2122         } else {
2123                 /*
2124                  * If we get here, the signal must be caught.
2125                  */
2126                 KASSERT(action != SIG_IGN && !SIGISMEMBER(lp->lwp_sigmask, sig),
2127                     ("postsig action"));
2128
2129                 /*
2130                  * Reset the signal handler if asked to
2131                  */
2132                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2133                         /*
2134                          * See kern_sigaction() for origin of this code.
2135                          */
2136                         SIGDELSET(p->p_sigcatch, sig);
2137                         if (sig != SIGCONT &&
2138                             sigprop(sig) & SA_IGNORE)
2139                                 SIGADDSET(p->p_sigignore, sig);
2140                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2141                 }
2142
2143                 /*
2144                  * Set the signal mask and calculate the mask to restore
2145                  * when the signal function returns.
2146                  *
2147                  * Special case: user has done a sigsuspend.  Here the
2148                  * current mask is not of interest, but rather the
2149                  * mask from before the sigsuspend is what we want
2150                  * restored after the signal processing is completed.
2151                  */
2152                 if (lp->lwp_flags & LWP_OLDMASK) {
2153                         returnmask = lp->lwp_oldsigmask;
2154                         lp->lwp_flags &= ~LWP_OLDMASK;
2155                 } else {
2156                         returnmask = lp->lwp_sigmask;
2157                 }
2158
2159                 SIGSETOR(lp->lwp_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2160                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2161                         SIGADDSET(lp->lwp_sigmask, sig);
2162
2163                 lp->lwp_ru.ru_nsignals++;
2164                 if (lp->lwp_sig != sig) {
2165                         code = 0;
2166                 } else {
2167                         code = lp->lwp_code;
2168                         lp->lwp_code = 0;
2169                         lp->lwp_sig = 0;
2170                 }
2171                 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
2172         }
2173 }
2174
2175 /*
2176  * Kill the current process for stated reason.
2177  */
2178 void
2179 killproc(struct proc *p, char *why)
2180 {
2181         log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", 
2182                 p->p_pid, p->p_comm,
2183                 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2184         ksignal(p, SIGKILL);
2185 }
2186
2187 /*
2188  * Force the current process to exit with the specified signal, dumping core
2189  * if appropriate.  We bypass the normal tests for masked and caught signals,
2190  * allowing unrecoverable failures to terminate the process without changing
2191  * signal state.  Mark the accounting record with the signal termination.
2192  * If dumping core, save the signal number for the debugger.  Calls exit and
2193  * does not return.
2194  *
2195  * This routine does not return.
2196  */
2197 void
2198 sigexit(struct lwp *lp, int sig)
2199 {
2200         struct proc *p = lp->lwp_proc;
2201
2202         lwkt_gettoken(&p->p_token);
2203         p->p_acflag |= AXSIG;
2204         if (sigprop(sig) & SA_CORE) {
2205                 lp->lwp_sig = sig;
2206
2207                 /*
2208                  * All threads must be stopped before we can safely coredump.
2209                  * Stop threads using SCORE, which cannot be overridden.
2210                  */
2211                 if (p->p_stat != SCORE) {
2212                         proc_stop(p, SCORE);
2213                         proc_stopwait(p);
2214
2215                         if (coredump(lp, sig) == 0)
2216                                 sig |= WCOREFLAG;
2217                         p->p_stat = SSTOP;
2218                 }
2219
2220                 /*
2221                  * Log signals which would cause core dumps
2222                  * (Log as LOG_INFO to appease those who don't want
2223                  * these messages.)
2224                  * XXX : Todo, as well as euid, write out ruid too
2225                  */
2226                 if (kern_logsigexit)
2227                         log(LOG_INFO,
2228                             "pid %d (%s), uid %d: exited on signal %d%s\n",
2229                             p->p_pid, p->p_comm,
2230                             p->p_ucred ? p->p_ucred->cr_uid : -1,
2231                             sig &~ WCOREFLAG,
2232                             sig & WCOREFLAG ? " (core dumped)" : "");
2233         }
2234         lwkt_reltoken(&p->p_token);
2235         exit1(W_EXITCODE(0, sig));
2236         /* NOTREACHED */
2237 }
2238
2239 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
2240 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2241               sizeof(corefilename), "process corefile name format string");
2242
2243 /*
2244  * expand_name(name, uid, pid)
2245  * Expand the name described in corefilename, using name, uid, and pid.
2246  * corefilename is a kprintf-like string, with three format specifiers:
2247  *      %N      name of process ("name")
2248  *      %P      process id (pid)
2249  *      %U      user id (uid)
2250  * For example, "%N.core" is the default; they can be disabled completely
2251  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2252  * This is controlled by the sysctl variable kern.corefile (see above).
2253  */
2254
2255 static char *
2256 expand_name(const char *name, uid_t uid, pid_t pid)
2257 {
2258         char *temp;
2259         char buf[11];           /* Buffer for pid/uid -- max 4B */
2260         int i, n;
2261         char *format = corefilename;
2262         size_t namelen;
2263
2264         temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
2265         if (temp == NULL)
2266                 return NULL;
2267         namelen = strlen(name);
2268         for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2269                 int l;
2270                 switch (format[i]) {
2271                 case '%':       /* Format character */
2272                         i++;
2273                         switch (format[i]) {
2274                         case '%':
2275                                 temp[n++] = '%';
2276                                 break;
2277                         case 'N':       /* process name */
2278                                 if ((n + namelen) > MAXPATHLEN) {
2279                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2280                                             pid, name, uid, temp, name);
2281                                         kfree(temp, M_TEMP);
2282                                         return NULL;
2283                                 }
2284                                 memcpy(temp+n, name, namelen);
2285                                 n += namelen;
2286                                 break;
2287                         case 'P':       /* process id */
2288                                 l = ksprintf(buf, "%u", pid);
2289                                 if ((n + l) > MAXPATHLEN) {
2290                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2291                                             pid, name, uid, temp, name);
2292                                         kfree(temp, M_TEMP);
2293                                         return NULL;
2294                                 }
2295                                 memcpy(temp+n, buf, l);
2296                                 n += l;
2297                                 break;
2298                         case 'U':       /* user id */
2299                                 l = ksprintf(buf, "%u", uid);
2300                                 if ((n + l) > MAXPATHLEN) {
2301                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
2302                                             pid, name, uid, temp, name);
2303                                         kfree(temp, M_TEMP);
2304                                         return NULL;
2305                                 }
2306                                 memcpy(temp+n, buf, l);
2307                                 n += l;
2308                                 break;
2309                         default:
2310                                 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
2311                         }
2312                         break;
2313                 default:
2314                         temp[n++] = format[i];
2315                 }
2316         }
2317         temp[n] = '\0';
2318         return temp;
2319 }
2320
2321 /*
2322  * Dump a process' core.  The main routine does some
2323  * policy checking, and creates the name of the coredump;
2324  * then it passes on a vnode and a size limit to the process-specific
2325  * coredump routine if there is one; if there _is not_ one, it returns
2326  * ENOSYS; otherwise it returns the error from the process-specific routine.
2327  *
2328  * The parameter `lp' is the lwp which triggered the coredump.
2329  */
2330
2331 static int
2332 coredump(struct lwp *lp, int sig)
2333 {
2334         struct proc *p = lp->lwp_proc;
2335         struct vnode *vp;
2336         struct ucred *cred = p->p_ucred;
2337         struct flock lf;
2338         struct nlookupdata nd;
2339         struct vattr vattr;
2340         int error, error1;
2341         char *name;                     /* name of corefile */
2342         off_t limit;
2343         
2344         STOPEVENT(p, S_CORE, 0);
2345
2346         if (((sugid_coredump == 0) && p->p_flags & P_SUGID) || do_coredump == 0)
2347                 return (EFAULT);
2348         
2349         /*
2350          * Note that the bulk of limit checking is done after
2351          * the corefile is created.  The exception is if the limit
2352          * for corefiles is 0, in which case we don't bother
2353          * creating the corefile at all.  This layout means that
2354          * a corefile is truncated instead of not being created,
2355          * if it is larger than the limit.
2356          */
2357         limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
2358         if (limit == 0)
2359                 return EFBIG;
2360
2361         name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
2362         if (name == NULL)
2363                 return (EINVAL);
2364         error = nlookup_init(&nd, name, UIO_SYSSPACE, NLC_LOCKVP);
2365         if (error == 0)
2366                 error = vn_open(&nd, NULL, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2367         kfree(name, M_TEMP);
2368         if (error) {
2369                 nlookup_done(&nd);
2370                 return (error);
2371         }
2372         vp = nd.nl_open_vp;
2373         nd.nl_open_vp = NULL;
2374         nlookup_done(&nd);
2375
2376         vn_unlock(vp);
2377         lf.l_whence = SEEK_SET;
2378         lf.l_start = 0;
2379         lf.l_len = 0;
2380         lf.l_type = F_WRLCK;
2381         error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, 0);
2382         if (error)
2383                 goto out2;
2384
2385         /* Don't dump to non-regular files or files with links. */
2386         if (vp->v_type != VREG ||
2387             VOP_GETATTR(vp, &vattr) || vattr.va_nlink != 1) {
2388                 error = EFAULT;
2389                 goto out1;
2390         }
2391
2392         /* Don't dump to files current user does not own */
2393         if (vattr.va_uid != p->p_ucred->cr_uid) {
2394                 error = EFAULT;
2395                 goto out1;
2396         }
2397
2398         VATTR_NULL(&vattr);
2399         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2400         vattr.va_size = 0;
2401         VOP_SETATTR(vp, &vattr, cred);
2402         p->p_acflag |= ACORE;
2403         vn_unlock(vp);
2404
2405         error = p->p_sysent->sv_coredump ?
2406                   p->p_sysent->sv_coredump(lp, sig, vp, limit) : ENOSYS;
2407
2408 out1:
2409         lf.l_type = F_UNLCK;
2410         VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, 0);
2411 out2:
2412         error1 = vn_close(vp, FWRITE, NULL);
2413         if (error == 0)
2414                 error = error1;
2415         return (error);
2416 }
2417
2418 /*
2419  * Nonexistent system call-- signal process (may want to handle it).
2420  * Flag error in case process won't see signal immediately (blocked or ignored).
2421  *
2422  * MPALMOSTSAFE
2423  */
2424 /* ARGSUSED */
2425 int
2426 sys_nosys(struct nosys_args *args)
2427 {
2428         lwpsignal(curproc, curthread->td_lwp, SIGSYS);
2429         return (EINVAL);
2430 }
2431
2432 /*
2433  * Send a SIGIO or SIGURG signal to a process or process group using
2434  * stored credentials rather than those of the current process.
2435  */
2436 void
2437 pgsigio(struct sigio *sigio, int sig, int checkctty)
2438 {
2439         if (sigio == NULL)
2440                 return;
2441                 
2442         if (sigio->sio_pgid > 0) {
2443                 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
2444                              sigio->sio_proc))
2445                         ksignal(sigio->sio_proc, sig);
2446         } else if (sigio->sio_pgid < 0) {
2447                 struct proc *p;
2448                 struct pgrp *pg = sigio->sio_pgrp;
2449
2450                 /*
2451                  * Must interlock all signals against fork
2452                  */
2453                 pgref(pg);
2454                 lockmgr(&pg->pg_lock, LK_EXCLUSIVE);
2455                 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
2456                         if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
2457                             (checkctty == 0 || (p->p_flags & P_CONTROLT)))
2458                                 ksignal(p, sig);
2459                 }
2460                 lockmgr(&pg->pg_lock, LK_RELEASE);
2461                 pgrel(pg);
2462         }
2463 }
2464
2465 static int
2466 filt_sigattach(struct knote *kn)
2467 {
2468         struct proc *p = curproc;
2469
2470         kn->kn_ptr.p_proc = p;
2471         kn->kn_flags |= EV_CLEAR;               /* automatically set */
2472
2473         /* XXX lock the proc here while adding to the list? */
2474         knote_insert(&p->p_klist, kn);
2475
2476         return (0);
2477 }
2478
2479 static void
2480 filt_sigdetach(struct knote *kn)
2481 {
2482         struct proc *p = kn->kn_ptr.p_proc;
2483
2484         knote_remove(&p->p_klist, kn);
2485 }
2486
2487 /*
2488  * signal knotes are shared with proc knotes, so we apply a mask to 
2489  * the hint in order to differentiate them from process hints.  This
2490  * could be avoided by using a signal-specific knote list, but probably
2491  * isn't worth the trouble.
2492  */
2493 static int
2494 filt_signal(struct knote *kn, long hint)
2495 {
2496         if (hint & NOTE_SIGNAL) {
2497                 hint &= ~NOTE_SIGNAL;
2498
2499                 if (kn->kn_id == hint)
2500                         kn->kn_data++;
2501         }
2502         return (kn->kn_data != 0);
2503 }