MP Implmentation 3B/4: Remove Xcpuast and Xforward_irq, replacing them
[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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)kern_sig.c  8.7 (Berkeley) 4/18/94
39  * $FreeBSD: src/sys/kern/kern_sig.c,v 1.72.2.17 2003/05/16 16:34:34 obrien Exp $
40  * $DragonFly: src/sys/kern/kern_sig.c,v 1.9 2003/07/11 01:23:24 dillon Exp $
41  */
42
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/sysproto.h>
50 #include <sys/signalvar.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53 #include <sys/event.h>
54 #include <sys/proc.h>
55 #include <sys/namei.h>
56 #include <sys/pioctl.h>
57 #include <sys/systm.h>
58 #include <sys/acct.h>
59 #include <sys/fcntl.h>
60 #include <sys/wait.h>
61 #include <sys/ktrace.h>
62 #include <sys/syslog.h>
63 #include <sys/stat.h>
64 #include <sys/sysent.h>
65 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
67 #include <sys/unistd.h>
68
69
70 #include <machine/ipl.h>
71 #include <machine/cpu.h>
72 #include <machine/smp.h>
73
74 #define ONSIG   32              /* NSIG for osig* syscalls.  XXX. */
75
76 static int coredump     __P((struct proc *));
77 static int do_sigaction __P((int sig, struct sigaction *act,
78                              struct sigaction *oact, int old));
79 static int do_sigprocmask __P((int how, sigset_t *set,
80                                sigset_t *oset, int old));
81 static char *expand_name __P((const char *, uid_t, pid_t));
82 static int killpg1      __P((int sig, int pgid, int all));
83 static int sig_ffs      __P((sigset_t *set));
84 static int sigprop      __P((int sig));
85 static void stop        __P((struct proc *));
86 #ifdef SMP
87 static void signotify_remote(void *arg);
88 #endif
89
90 static int      filt_sigattach(struct knote *kn);
91 static void     filt_sigdetach(struct knote *kn);
92 static int      filt_signal(struct knote *kn, long hint);
93
94 struct filterops sig_filtops =
95         { 0, filt_sigattach, filt_sigdetach, filt_signal };
96
97 static int      kern_logsigexit = 1;
98 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW, 
99     &kern_logsigexit, 0, 
100     "Log processes quitting on abnormal signals to syslog(3)");
101
102 /*
103  * Can process p, with pcred pc, send the signal sig to process q?
104  */
105 #define CANSIGNAL(q, sig) \
106         (!p_trespass(curproc->p_ucred, (q)->p_ucred) || \
107         ((sig) == SIGCONT && (q)->p_session == curproc->p_session))
108
109 /*
110  * Policy -- Can real uid ruid with ucred uc send a signal to process q?
111  */
112 #define CANSIGIO(ruid, uc, q) \
113         ((uc)->cr_uid == 0 || \
114             (ruid) == (q)->p_ucred->cr_ruid || \
115             (uc)->cr_uid == (q)->p_ucred->cr_ruid || \
116             (ruid) == (q)->p_ucred->cr_uid || \
117             (uc)->cr_uid == (q)->p_ucred->cr_uid)
118
119 int sugid_coredump;
120 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW, 
121     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
122
123 static int      do_coredump = 1;
124 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
125         &do_coredump, 0, "Enable/Disable coredumps");
126
127 /*
128  * Signal properties and actions.
129  * The array below categorizes the signals and their default actions
130  * according to the following properties:
131  */
132 #define SA_KILL         0x01            /* terminates process by default */
133 #define SA_CORE         0x02            /* ditto and coredumps */
134 #define SA_STOP         0x04            /* suspend process */
135 #define SA_TTYSTOP      0x08            /* ditto, from tty */
136 #define SA_IGNORE       0x10            /* ignore by default */
137 #define SA_CONT         0x20            /* continue if suspended */
138 #define SA_CANTMASK     0x40            /* non-maskable, catchable */
139
140 static int sigproptbl[NSIG] = {
141         SA_KILL,                /* SIGHUP */
142         SA_KILL,                /* SIGINT */
143         SA_KILL|SA_CORE,        /* SIGQUIT */
144         SA_KILL|SA_CORE,        /* SIGILL */
145         SA_KILL|SA_CORE,        /* SIGTRAP */
146         SA_KILL|SA_CORE,        /* SIGABRT */
147         SA_KILL|SA_CORE,        /* SIGEMT */
148         SA_KILL|SA_CORE,        /* SIGFPE */
149         SA_KILL,                /* SIGKILL */
150         SA_KILL|SA_CORE,        /* SIGBUS */
151         SA_KILL|SA_CORE,        /* SIGSEGV */
152         SA_KILL|SA_CORE,        /* SIGSYS */
153         SA_KILL,                /* SIGPIPE */
154         SA_KILL,                /* SIGALRM */
155         SA_KILL,                /* SIGTERM */
156         SA_IGNORE,              /* SIGURG */
157         SA_STOP,                /* SIGSTOP */
158         SA_STOP|SA_TTYSTOP,     /* SIGTSTP */
159         SA_IGNORE|SA_CONT,      /* SIGCONT */
160         SA_IGNORE,              /* SIGCHLD */
161         SA_STOP|SA_TTYSTOP,     /* SIGTTIN */
162         SA_STOP|SA_TTYSTOP,     /* SIGTTOU */
163         SA_IGNORE,              /* SIGIO */
164         SA_KILL,                /* SIGXCPU */
165         SA_KILL,                /* SIGXFSZ */
166         SA_KILL,                /* SIGVTALRM */
167         SA_KILL,                /* SIGPROF */
168         SA_IGNORE,              /* SIGWINCH  */
169         SA_IGNORE,              /* SIGINFO */
170         SA_KILL,                /* SIGUSR1 */
171         SA_KILL,                /* SIGUSR2 */
172 };
173
174 static __inline int
175 sigprop(int sig)
176 {
177
178         if (sig > 0 && sig < NSIG)
179                 return (sigproptbl[_SIG_IDX(sig)]);
180         return (0);
181 }
182
183 static __inline int
184 sig_ffs(sigset_t *set)
185 {
186         int i;
187
188         for (i = 0; i < _SIG_WORDS; i++)
189                 if (set->__bits[i])
190                         return (ffs(set->__bits[i]) + (i * 32));
191         return (0);
192 }
193
194 /*
195  * do_sigaction
196  * sigaction
197  * osigaction
198  */
199 static int
200 do_sigaction(int sig, struct sigaction *act, struct sigaction *oact, int old)
201 {
202         struct proc *p = curproc;
203         struct sigacts *ps = p->p_sigacts;
204
205         if (sig <= 0 || sig > _SIG_MAXSIG)
206                 return (EINVAL);
207
208         if (oact) {
209                 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
210                 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
211                 oact->sa_flags = 0;
212                 if (SIGISMEMBER(ps->ps_sigonstack, sig))
213                         oact->sa_flags |= SA_ONSTACK;
214                 if (!SIGISMEMBER(ps->ps_sigintr, sig))
215                         oact->sa_flags |= SA_RESTART;
216                 if (SIGISMEMBER(ps->ps_sigreset, sig))
217                         oact->sa_flags |= SA_RESETHAND;
218                 if (SIGISMEMBER(ps->ps_signodefer, sig))
219                         oact->sa_flags |= SA_NODEFER;
220                 if (SIGISMEMBER(ps->ps_siginfo, sig))
221                         oact->sa_flags |= SA_SIGINFO;
222                 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDSTOP)
223                         oact->sa_flags |= SA_NOCLDSTOP;
224                 if (sig == SIGCHLD && p->p_procsig->ps_flag & PS_NOCLDWAIT)
225                         oact->sa_flags |= SA_NOCLDWAIT;
226         }
227         if (act) {
228                 if ((sig == SIGKILL || sig == SIGSTOP) &&
229                     act->sa_handler != SIG_DFL)
230                         return (EINVAL);
231
232                 /*
233                  * Change setting atomically.
234                  */
235                 (void) splhigh();
236
237                 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
238                 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
239                 if (act->sa_flags & SA_SIGINFO) {
240                         ps->ps_sigact[_SIG_IDX(sig)] =
241                             (__sighandler_t *)act->sa_sigaction;
242                         SIGADDSET(ps->ps_siginfo, sig);
243                 } else {
244                         ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
245                         SIGDELSET(ps->ps_siginfo, sig);
246                 }
247                 if (!(act->sa_flags & SA_RESTART))
248                         SIGADDSET(ps->ps_sigintr, sig);
249                 else
250                         SIGDELSET(ps->ps_sigintr, sig);
251                 if (act->sa_flags & SA_ONSTACK)
252                         SIGADDSET(ps->ps_sigonstack, sig);
253                 else
254                         SIGDELSET(ps->ps_sigonstack, sig);
255                 if (act->sa_flags & SA_RESETHAND)
256                         SIGADDSET(ps->ps_sigreset, sig);
257                 else
258                         SIGDELSET(ps->ps_sigreset, sig);
259                 if (act->sa_flags & SA_NODEFER)
260                         SIGADDSET(ps->ps_signodefer, sig);
261                 else
262                         SIGDELSET(ps->ps_signodefer, sig);
263 #ifdef COMPAT_SUNOS
264                 if (act->sa_flags & SA_USERTRAMP)
265                         SIGADDSET(ps->ps_usertramp, sig);
266                 else
267                         SIGDELSET(ps->ps_usertramp, seg);
268 #endif
269                 if (sig == SIGCHLD) {
270                         if (act->sa_flags & SA_NOCLDSTOP)
271                                 p->p_procsig->ps_flag |= PS_NOCLDSTOP;
272                         else
273                                 p->p_procsig->ps_flag &= ~PS_NOCLDSTOP;
274                         if (act->sa_flags & SA_NOCLDWAIT) {
275                                 /*
276                                  * Paranoia: since SA_NOCLDWAIT is implemented
277                                  * by reparenting the dying child to PID 1 (and
278                                  * trust it to reap the zombie), PID 1 itself
279                                  * is forbidden to set SA_NOCLDWAIT.
280                                  */
281                                 if (p->p_pid == 1)
282                                         p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
283                                 else
284                                         p->p_procsig->ps_flag |= PS_NOCLDWAIT;
285                         } else
286                                 p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
287                 }
288                 /*
289                  * Set bit in p_sigignore for signals that are set to SIG_IGN,
290                  * and for signals set to SIG_DFL where the default is to
291                  * ignore. However, don't put SIGCONT in p_sigignore, as we
292                  * have to restart the process.
293                  */
294                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
295                     (sigprop(sig) & SA_IGNORE &&
296                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
297                         /* never to be seen again */
298                         SIGDELSET(p->p_siglist, sig);
299                         if (sig != SIGCONT)
300                                 /* easier in psignal */
301                                 SIGADDSET(p->p_sigignore, sig);
302                         SIGDELSET(p->p_sigcatch, sig);
303                 } else {
304                         SIGDELSET(p->p_sigignore, sig);
305                         if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
306                                 SIGDELSET(p->p_sigcatch, sig);
307                         else
308                                 SIGADDSET(p->p_sigcatch, sig);
309                 }
310                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
311                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL || !old)
312                         SIGDELSET(ps->ps_osigset, sig);
313                 else
314                         SIGADDSET(ps->ps_osigset, sig);
315
316                 (void) spl0();
317         }
318         return (0);
319 }
320
321 #ifndef _SYS_SYSPROTO_H_
322 struct sigaction_args {
323         int     sig;
324         struct  sigaction *act;
325         struct  sigaction *oact;
326 };
327 #endif
328 /* ARGSUSED */
329 int
330 sigaction(struct sigaction_args *uap)
331 {
332         struct sigaction act, oact;
333         register struct sigaction *actp, *oactp;
334         int error;
335
336         actp = (uap->act != NULL) ? &act : NULL;
337         oactp = (uap->oact != NULL) ? &oact : NULL;
338         if (actp) {
339                 error = copyin(uap->act, actp, sizeof(act));
340                 if (error)
341                         return (error);
342         }
343         error = do_sigaction(uap->sig, actp, oactp, 0);
344         if (oactp && !error) {
345                 error = copyout(oactp, uap->oact, sizeof(oact));
346         }
347         return (error);
348 }
349
350 #ifndef _SYS_SYSPROTO_H_
351 struct osigaction_args {
352         int     signum;
353         struct  osigaction *nsa;
354         struct  osigaction *osa;
355 };
356 #endif
357 /* ARGSUSED */
358 int
359 osigaction(struct osigaction_args *uap)
360 {
361         struct osigaction sa;
362         struct sigaction nsa, osa;
363         register struct sigaction *nsap, *osap;
364         int error;
365
366         if (uap->signum <= 0 || uap->signum >= ONSIG)
367                 return (EINVAL);
368         nsap = (uap->nsa != NULL) ? &nsa : NULL;
369         osap = (uap->osa != NULL) ? &osa : NULL;
370         if (nsap) {
371                 error = copyin(uap->nsa, &sa, sizeof(sa));
372                 if (error)
373                         return (error);
374                 nsap->sa_handler = sa.sa_handler;
375                 nsap->sa_flags = sa.sa_flags;
376                 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
377         }
378         error = do_sigaction(uap->signum, nsap, osap, 1);
379         if (osap && !error) {
380                 sa.sa_handler = osap->sa_handler;
381                 sa.sa_flags = osap->sa_flags;
382                 SIG2OSIG(osap->sa_mask, sa.sa_mask);
383                 error = copyout(&sa, uap->osa, sizeof(sa));
384         }
385         return (error);
386 }
387
388 /*
389  * Initialize signal state for process 0;
390  * set to ignore signals that are ignored by default.
391  */
392 void
393 siginit(p)
394         struct proc *p;
395 {
396         register int i;
397
398         for (i = 1; i <= NSIG; i++)
399                 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
400                         SIGADDSET(p->p_sigignore, i);
401 }
402
403 /*
404  * Reset signals for an exec of the specified process.
405  */
406 void
407 execsigs(p)
408         register struct proc *p;
409 {
410         register struct sigacts *ps = p->p_sigacts;
411         register int sig;
412
413         /*
414          * Reset caught signals.  Held signals remain held
415          * through p_sigmask (unless they were caught,
416          * and are now ignored by default).
417          */
418         while (SIGNOTEMPTY(p->p_sigcatch)) {
419                 sig = sig_ffs(&p->p_sigcatch);
420                 SIGDELSET(p->p_sigcatch, sig);
421                 if (sigprop(sig) & SA_IGNORE) {
422                         if (sig != SIGCONT)
423                                 SIGADDSET(p->p_sigignore, sig);
424                         SIGDELSET(p->p_siglist, sig);
425                 }
426                 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
427         }
428         /*
429          * Reset stack state to the user stack.
430          * Clear set of signals caught on the signal stack.
431          */
432         p->p_sigstk.ss_flags = SS_DISABLE;
433         p->p_sigstk.ss_size = 0;
434         p->p_sigstk.ss_sp = 0;
435         p->p_flag &= ~P_ALTSTACK;
436         /*
437          * Reset no zombies if child dies flag as Solaris does.
438          */
439         p->p_procsig->ps_flag &= ~PS_NOCLDWAIT;
440 }
441
442 /*
443  * do_sigprocmask() - MP SAFE ONLY IF p == curproc
444  *
445  *      Manipulate signal mask.  This routine is MP SAFE *ONLY* if
446  *      p == curproc.  Also remember that in order to remain MP SAFE
447  *      no spl*() calls may be made.
448  */
449 static int
450 do_sigprocmask(int how, sigset_t *set, sigset_t *oset, int old)
451 {
452         struct proc *p = curproc;
453         int error;
454
455         if (oset != NULL)
456                 *oset = p->p_sigmask;
457
458         error = 0;
459         if (set != NULL) {
460                 switch (how) {
461                 case SIG_BLOCK:
462                         SIG_CANTMASK(*set);
463                         SIGSETOR(p->p_sigmask, *set);
464                         break;
465                 case SIG_UNBLOCK:
466                         SIGSETNAND(p->p_sigmask, *set);
467                         break;
468                 case SIG_SETMASK:
469                         SIG_CANTMASK(*set);
470                         if (old)
471                                 SIGSETLO(p->p_sigmask, *set);
472                         else
473                                 p->p_sigmask = *set;
474                         break;
475                 default:
476                         error = EINVAL;
477                         break;
478                 }
479         }
480         return (error);
481 }
482
483 /*
484  * sigprocmask() - MP SAFE
485  */
486
487 #ifndef _SYS_SYSPROTO_H_
488 struct sigprocmask_args {
489         int     how;
490         const sigset_t *set;
491         sigset_t *oset;
492 };
493 #endif
494 int
495 sigprocmask(struct sigprocmask_args *uap)
496 {
497         sigset_t set, oset;
498         sigset_t *setp, *osetp;
499         int error;
500
501         setp = (uap->set != NULL) ? &set : NULL;
502         osetp = (uap->oset != NULL) ? &oset : NULL;
503         if (setp) {
504                 error = copyin(uap->set, setp, sizeof(set));
505                 if (error)
506                         return (error);
507         }
508         error = do_sigprocmask(uap->how, setp, osetp, 0);
509         if (osetp && !error) {
510                 error = copyout(osetp, uap->oset, sizeof(oset));
511         }
512         return (error);
513 }
514
515 /*
516  * osigprocmask() - MP SAFE
517  */
518
519 #ifndef _SYS_SYSPROTO_H_
520 struct osigprocmask_args {
521         int     how;
522         osigset_t mask;
523 };
524 #endif
525 int
526 osigprocmask(struct osigprocmask_args *uap)
527 {
528         sigset_t set, oset;
529         int error;
530
531         OSIG2SIG(uap->mask, set);
532         error = do_sigprocmask(uap->how, &set, &oset, 1);
533         SIG2OSIG(oset, curproc->p_retval[0]);
534         return (error);
535 }
536
537 #ifndef _SYS_SYSPROTO_H_
538 struct sigpending_args {
539         sigset_t        *set;
540 };
541 #endif
542 /* ARGSUSED */
543 int
544 sigpending(struct sigpending_args *uap)
545 {
546         struct proc *p = curproc;
547
548         return (copyout(&p->p_siglist, uap->set, sizeof(sigset_t)));
549 }
550
551 #ifndef _SYS_SYSPROTO_H_
552 struct osigpending_args {
553         int     dummy;
554 };
555 #endif
556 /* ARGSUSED */
557 int
558 osigpending(struct osigpending_args *uap)
559 {
560         struct proc *p = curproc;
561
562         SIG2OSIG(p->p_siglist, p->p_retval[0]);
563         return (0);
564 }
565
566 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
567 /*
568  * Generalized interface signal handler, 4.3-compatible.
569  */
570 #ifndef _SYS_SYSPROTO_H_
571 struct osigvec_args {
572         int     signum;
573         struct  sigvec *nsv;
574         struct  sigvec *osv;
575 };
576 #endif
577 /* ARGSUSED */
578 int
579 osigvec(struct osigvec_args *uap)
580 {
581         struct sigvec vec;
582         struct sigaction nsa, osa;
583         register struct sigaction *nsap, *osap;
584         int error;
585
586         if (uap->signum <= 0 || uap->signum >= ONSIG)
587                 return (EINVAL);
588         nsap = (uap->nsv != NULL) ? &nsa : NULL;
589         osap = (uap->osv != NULL) ? &osa : NULL;
590         if (nsap) {
591                 error = copyin(uap->nsv, &vec, sizeof(vec));
592                 if (error)
593                         return (error);
594                 nsap->sa_handler = vec.sv_handler;
595                 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
596                 nsap->sa_flags = vec.sv_flags;
597                 nsap->sa_flags ^= SA_RESTART;   /* opposite of SV_INTERRUPT */
598 #ifdef COMPAT_SUNOS
599                 nsap->sa_flags |= SA_USERTRAMP;
600 #endif
601         }
602         error = do_sigaction(uap->signum, nsap, osap, 1);
603         if (osap && !error) {
604                 vec.sv_handler = osap->sa_handler;
605                 SIG2OSIG(osap->sa_mask, vec.sv_mask);
606                 vec.sv_flags = osap->sa_flags;
607                 vec.sv_flags &= ~SA_NOCLDWAIT;
608                 vec.sv_flags ^= SA_RESTART;
609 #ifdef COMPAT_SUNOS
610                 vec.sv_flags &= ~SA_NOCLDSTOP;
611 #endif
612                 error = copyout(&vec, uap->osv, sizeof(vec));
613         }
614         return (error);
615 }
616
617 #ifndef _SYS_SYSPROTO_H_
618 struct osigblock_args {
619         int     mask;
620 };
621 #endif
622 int
623 osigblock(struct osigblock_args *uap)
624 {
625         struct proc *p = curproc;
626         sigset_t set;
627
628         OSIG2SIG(uap->mask, set);
629         SIG_CANTMASK(set);
630         (void) splhigh();
631         SIG2OSIG(p->p_sigmask, p->p_retval[0]);
632         SIGSETOR(p->p_sigmask, set);
633         (void) spl0();
634         return (0);
635 }
636
637 #ifndef _SYS_SYSPROTO_H_
638 struct osigsetmask_args {
639         int     mask;
640 };
641 #endif
642 int
643 osigsetmask(struct osigsetmask_args *uap)
644 {
645         struct proc *p = curproc;
646         sigset_t set;
647
648         OSIG2SIG(uap->mask, set);
649         SIG_CANTMASK(set);
650         (void) splhigh();
651         SIG2OSIG(p->p_sigmask, p->p_retval[0]);
652         SIGSETLO(p->p_sigmask, set);
653         (void) spl0();
654         return (0);
655 }
656 #endif /* COMPAT_43 || COMPAT_SUNOS */
657
658 /*
659  * Suspend process until signal, providing mask to be set
660  * in the meantime.  Note nonstandard calling convention:
661  * libc stub passes mask, not pointer, to save a copyin.
662  */
663 #ifndef _SYS_SYSPROTO_H_
664 struct sigsuspend_args {
665         const sigset_t *sigmask;
666 };
667 #endif
668 /* ARGSUSED */
669 int
670 sigsuspend(struct sigsuspend_args *uap)
671 {
672         struct proc *p = curproc;
673         sigset_t mask;
674         struct sigacts *ps = p->p_sigacts;
675         int error;
676
677         error = copyin(uap->sigmask, &mask, sizeof(mask));
678         if (error)
679                 return (error);
680
681         /*
682          * When returning from sigsuspend, we want
683          * the old mask to be restored after the
684          * signal handler has finished.  Thus, we
685          * save it here and mark the sigacts structure
686          * to indicate this.
687          */
688         p->p_oldsigmask = p->p_sigmask;
689         p->p_flag |= P_OLDMASK;
690
691         SIG_CANTMASK(mask);
692         p->p_sigmask = mask;
693         while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
694                 /* void */;
695         /* always return EINTR rather than ERESTART... */
696         return (EINTR);
697 }
698
699 #ifndef _SYS_SYSPROTO_H_
700 struct osigsuspend_args {
701         osigset_t mask;
702 };
703 #endif
704 /* ARGSUSED */
705 int
706 osigsuspend(struct osigsuspend_args *uap)
707 {
708         sigset_t mask;
709         struct proc *p = curproc;
710         struct sigacts *ps = p->p_sigacts;
711
712         p->p_oldsigmask = p->p_sigmask;
713         p->p_flag |= P_OLDMASK;
714         OSIG2SIG(uap->mask, mask);
715         SIG_CANTMASK(mask);
716         SIGSETLO(p->p_sigmask, mask);
717         while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "opause", 0) == 0)
718                 /* void */;
719         /* always return EINTR rather than ERESTART... */
720         return (EINTR);
721 }
722
723 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
724 #ifndef _SYS_SYSPROTO_H_
725 struct osigstack_args {
726         struct  sigstack *nss;
727         struct  sigstack *oss;
728 };
729 #endif
730 /* ARGSUSED */
731 int
732 osigstack(struct osigstack_args *uap)
733 {
734         struct proc *p = curproc;
735         struct sigstack ss;
736         int error = 0;
737
738         ss.ss_sp = p->p_sigstk.ss_sp;
739         ss.ss_onstack = p->p_sigstk.ss_flags & SS_ONSTACK;
740         if (uap->oss && (error = copyout(&ss, uap->oss,
741             sizeof(struct sigstack))))
742                 return (error);
743         if (uap->nss && (error = copyin(uap->nss, &ss, sizeof(ss))) == 0) {
744                 p->p_sigstk.ss_sp = ss.ss_sp;
745                 p->p_sigstk.ss_size = 0;
746                 p->p_sigstk.ss_flags |= ss.ss_onstack & SS_ONSTACK;
747                 p->p_flag |= P_ALTSTACK;
748         }
749         return (error);
750 }
751 #endif /* COMPAT_43 || COMPAT_SUNOS */
752
753 #ifndef _SYS_SYSPROTO_H_
754 struct sigaltstack_args {
755         stack_t *ss;
756         stack_t *oss;
757 };
758 #endif
759 /* ARGSUSED */
760 int
761 sigaltstack(struct sigaltstack_args *uap)
762 {
763         struct proc *p = curproc;
764         stack_t ss;
765         int error;
766
767         if ((p->p_flag & P_ALTSTACK) == 0)
768                 p->p_sigstk.ss_flags |= SS_DISABLE;
769         if (uap->oss && (error = copyout(&p->p_sigstk, uap->oss,
770             sizeof(stack_t))))
771                 return (error);
772         if (uap->ss == 0)
773                 return (0);
774         if ((error = copyin(uap->ss, &ss, sizeof(ss))))
775                 return (error);
776         if (ss.ss_flags & SS_DISABLE) {
777                 if (p->p_sigstk.ss_flags & SS_ONSTACK)
778                         return (EINVAL);
779                 p->p_flag &= ~P_ALTSTACK;
780                 p->p_sigstk.ss_flags = ss.ss_flags;
781                 return (0);
782         }
783         if (ss.ss_size < p->p_sysent->sv_minsigstksz)
784                 return (ENOMEM);
785         p->p_flag |= P_ALTSTACK;
786         p->p_sigstk = ss;
787         return (0);
788 }
789
790 /*
791  * Common code for kill process group/broadcast kill.
792  * cp is calling process.
793  */
794 int
795 killpg1(int sig, int pgid, int all)
796 {
797         struct proc *cp = curproc;
798         struct proc *p;
799         struct pgrp *pgrp;
800         int nfound = 0;
801
802         if (all)
803                 /*
804                  * broadcast
805                  */
806                 LIST_FOREACH(p, &allproc, p_list) {
807                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
808                             p == cp || !CANSIGNAL(p, sig))
809                                 continue;
810                         nfound++;
811                         if (sig)
812                                 psignal(p, sig);
813                 }
814         else {
815                 if (pgid == 0)
816                         /*
817                          * zero pgid means send to my process group.
818                          */
819                         pgrp = cp->p_pgrp;
820                 else {
821                         pgrp = pgfind(pgid);
822                         if (pgrp == NULL)
823                                 return (ESRCH);
824                 }
825                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
826                         if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
827                             p->p_stat == SZOMB ||
828                             !CANSIGNAL(p, sig))
829                                 continue;
830                         nfound++;
831                         if (sig)
832                                 psignal(p, sig);
833                 }
834         }
835         return (nfound ? 0 : ESRCH);
836 }
837
838 #ifndef _SYS_SYSPROTO_H_
839 struct kill_args {
840         int     pid;
841         int     signum;
842 };
843 #endif
844 /* ARGSUSED */
845 int
846 kill(struct kill_args *uap)
847 {
848         struct proc *p;
849
850         if ((u_int)uap->signum > _SIG_MAXSIG)
851                 return (EINVAL);
852         if (uap->pid > 0) {
853                 /* kill single process */
854                 if ((p = pfind(uap->pid)) == NULL)
855                         return (ESRCH);
856                 if (!CANSIGNAL(p, uap->signum))
857                         return (EPERM);
858                 if (uap->signum)
859                         psignal(p, uap->signum);
860                 return (0);
861         }
862         switch (uap->pid) {
863         case -1:                /* broadcast signal */
864                 return (killpg1(uap->signum, 0, 1));
865         case 0:                 /* signal own process group */
866                 return (killpg1(uap->signum, 0, 0));
867         default:                /* negative explicit process group */
868                 return (killpg1(uap->signum, -uap->pid, 0));
869         }
870         /* NOTREACHED */
871 }
872
873 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
874 #ifndef _SYS_SYSPROTO_H_
875 struct okillpg_args {
876         int     pgid;
877         int     signum;
878 };
879 #endif
880 /* ARGSUSED */
881 int
882 okillpg(struct okillpg_args *uap)
883 {
884         if ((u_int)uap->signum > _SIG_MAXSIG)
885                 return (EINVAL);
886         return (killpg1(uap->signum, uap->pgid, 0));
887 }
888 #endif /* COMPAT_43 || COMPAT_SUNOS */
889
890 /*
891  * Send a signal to a process group.
892  */
893 void
894 gsignal(int pgid, int sig)
895 {
896         struct pgrp *pgrp;
897
898         if (pgid && (pgrp = pgfind(pgid)))
899                 pgsignal(pgrp, sig, 0);
900 }
901
902 /*
903  * Send a signal to a process group.  If checktty is 1,
904  * limit to members which have a controlling terminal.
905  */
906 void
907 pgsignal(pgrp, sig, checkctty)
908         struct pgrp *pgrp;
909         int sig, checkctty;
910 {
911         register struct proc *p;
912
913         if (pgrp)
914                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
915                         if (checkctty == 0 || p->p_flag & P_CONTROLT)
916                                 psignal(p, sig);
917 }
918
919 /*
920  * Send a signal caused by a trap to the current process.
921  * If it will be caught immediately, deliver it with correct code.
922  * Otherwise, post it normally.
923  */
924 void
925 trapsignal(p, sig, code)
926         struct proc *p;
927         register int sig;
928         u_long code;
929 {
930         struct sigacts *ps = p->p_sigacts;
931
932         if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
933             !SIGISMEMBER(p->p_sigmask, sig)) {
934                 p->p_stats->p_ru.ru_nsignals++;
935 #ifdef KTRACE
936                 if (KTRPOINT(p->p_thread, KTR_PSIG))
937                         ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
938                                 &p->p_sigmask, code);
939 #endif
940                 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
941                                                 &p->p_sigmask, code);
942                 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
943                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
944                         SIGADDSET(p->p_sigmask, sig);
945                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
946                         /*
947                          * See do_sigaction() for origin of this code.
948                          */
949                         SIGDELSET(p->p_sigcatch, sig);
950                         if (sig != SIGCONT &&
951                             sigprop(sig) & SA_IGNORE)
952                                 SIGADDSET(p->p_sigignore, sig);
953                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
954                 }
955         } else {
956                 p->p_code = code;       /* XXX for core dump/debugger */
957                 p->p_sig = sig;         /* XXX to verify code */
958                 psignal(p, sig);
959         }
960 }
961
962 /*
963  * Send the signal to the process.  If the signal has an action, the action
964  * is usually performed by the target process rather than the caller; we add
965  * the signal to the set of pending signals for the process.
966  *
967  * Exceptions:
968  *   o When a stop signal is sent to a sleeping process that takes the
969  *     default action, the process is stopped without awakening it.
970  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
971  *     regardless of the signal action (eg, blocked or ignored).
972  *
973  * Other ignored signals are discarded immediately.
974  */
975
976 void
977 psignal(p, sig)
978         register struct proc *p;
979         register int sig;
980 {
981         int s, prop;
982         sig_t action;
983
984         if (sig > _SIG_MAXSIG || sig <= 0) {
985                 printf("psignal: signal %d\n", sig);
986                 panic("psignal signal number");
987         }
988
989         s = splhigh();
990         KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
991         splx(s);
992
993         prop = sigprop(sig);
994
995         /*
996          * If proc is traced, always give parent a chance;
997          * if signal event is tracked by procfs, give *that*
998          * a chance, as well.
999          */
1000         if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG))
1001                 action = SIG_DFL;
1002         else {
1003                 /*
1004                  * If the signal is being ignored,
1005                  * then we forget about it immediately.
1006                  * (Note: we don't set SIGCONT in p_sigignore,
1007                  * and if it is set to SIG_IGN,
1008                  * action will be SIG_DFL here.)
1009                  */
1010                 if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1011                         return;
1012                 if (SIGISMEMBER(p->p_sigmask, sig))
1013                         action = SIG_HOLD;
1014                 else if (SIGISMEMBER(p->p_sigcatch, sig))
1015                         action = SIG_CATCH;
1016                 else
1017                         action = SIG_DFL;
1018         }
1019
1020         if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
1021             (p->p_flag & P_TRACED) == 0)
1022                 p->p_nice = NZERO;
1023
1024         if (prop & SA_CONT)
1025                 SIG_STOPSIGMASK(p->p_siglist);
1026
1027         if (prop & SA_STOP) {
1028                 /*
1029                  * If sending a tty stop signal to a member of an orphaned
1030                  * process group, discard the signal here if the action
1031                  * is default; don't stop the process below if sleeping,
1032                  * and don't clear any pending SIGCONT.
1033                  */
1034                 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1035                     action == SIG_DFL)
1036                         return;
1037                 SIG_CONTSIGMASK(p->p_siglist);
1038         }
1039         SIGADDSET(p->p_siglist, sig);
1040
1041         /*
1042          * Defer further processing for signals which are held,
1043          * except that stopped processes must be continued by SIGCONT.
1044          */
1045         if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP))
1046                 return;
1047         s = splhigh();
1048         switch (p->p_stat) {
1049
1050         case SSLEEP:
1051                 /*
1052                  * If process is sleeping uninterruptibly
1053                  * we can't interrupt the sleep... the signal will
1054                  * be noticed when the process returns through
1055                  * trap() or syscall().
1056                  */
1057                 if ((p->p_flag & P_SINTR) == 0)
1058                         goto out;
1059                 /*
1060                  * Process is sleeping and traced... make it runnable
1061                  * so it can discover the signal in issignal() and stop
1062                  * for the parent.
1063                  */
1064                 if (p->p_flag & P_TRACED)
1065                         goto run;
1066                 /*
1067                  * If SIGCONT is default (or ignored) and process is
1068                  * asleep, we are finished; the process should not
1069                  * be awakened.
1070                  */
1071                 if ((prop & SA_CONT) && action == SIG_DFL) {
1072                         SIGDELSET(p->p_siglist, sig);
1073                         goto out;
1074                 }
1075                 /*
1076                  * When a sleeping process receives a stop
1077                  * signal, process immediately if possible.
1078                  * All other (caught or default) signals
1079                  * cause the process to run.
1080                  */
1081                 if (prop & SA_STOP) {
1082                         if (action != SIG_DFL)
1083                                 goto run;
1084                         /*
1085                          * If a child holding parent blocked,
1086                          * stopping could cause deadlock.
1087                          */
1088                         if (p->p_flag & P_PPWAIT)
1089                                 goto out;
1090                         SIGDELSET(p->p_siglist, sig);
1091                         p->p_xstat = sig;
1092                         if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1093                                 psignal(p->p_pptr, SIGCHLD);
1094                         stop(p);
1095                         goto out;
1096                 } else
1097                         goto run;
1098                 /*NOTREACHED*/
1099
1100         case SSTOP:
1101                 /*
1102                  * If traced process is already stopped,
1103                  * then no further action is necessary.
1104                  */
1105                 if (p->p_flag & P_TRACED)
1106                         goto out;
1107
1108                 /*
1109                  * Kill signal always sets processes running.
1110                  */
1111                 if (sig == SIGKILL)
1112                         goto run;
1113
1114                 if (prop & SA_CONT) {
1115                         /*
1116                          * If SIGCONT is default (or ignored), we continue the
1117                          * process but don't leave the signal in p_siglist, as
1118                          * it has no further action.  If SIGCONT is held, we
1119                          * continue the process and leave the signal in
1120                          * p_siglist.  If the process catches SIGCONT, let it
1121                          * handle the signal itself.  If it isn't waiting on
1122                          * an event, then it goes back to run state.
1123                          * Otherwise, process goes back to sleep state.
1124                          */
1125                         if (action == SIG_DFL)
1126                                 SIGDELSET(p->p_siglist, sig);
1127                         if (action == SIG_CATCH)
1128                                 goto run;
1129                         if (p->p_wchan == 0)
1130                                 goto run;
1131                         clrrunnable(p, SSLEEP);
1132                         goto out;
1133                 }
1134
1135                 if (prop & SA_STOP) {
1136                         /*
1137                          * Already stopped, don't need to stop again.
1138                          * (If we did the shell could get confused.)
1139                          */
1140                         SIGDELSET(p->p_siglist, sig);
1141                         goto out;
1142                 }
1143
1144                 /*
1145                  * If process is sleeping interruptibly, then simulate a
1146                  * wakeup so that when it is continued, it will be made
1147                  * runnable and can look at the signal.  But don't make
1148                  * the process runnable, leave it stopped.
1149                  */
1150                 if (p->p_wchan && (p->p_flag & P_SINTR))
1151                         unsleep(p->p_thread);
1152                 goto out;
1153
1154         default:
1155                 /*
1156                  * SRUN, SIDL, SZOMB do nothing with the signal,
1157                  * other than kicking ourselves if we are running.
1158                  * It will either never be noticed, or noticed very soon.
1159                  *
1160                  * For SMP we may have to forward the request to another cpu.
1161                  * YYY the MP lock prevents the target process from moving
1162                  * to another cpu, see kern/kern_switch.c
1163                  */
1164 #ifdef SMP
1165                 if (p == lwkt_preempted_proc()) {
1166                         signotify(p);
1167                 } else {
1168                         int cpuid = p->p_thread->td_cpu;
1169                         if (cpuid != mycpu->gd_cpuid)
1170                                 lwkt_send_ipiq(cpuid, signotify_remote, p);
1171                 }
1172 #else
1173                 if (p == lwkt_preempted_proc())
1174                         signotify(p);
1175 #endif
1176                 goto out;
1177         }
1178         /*NOTREACHED*/
1179 run:
1180         setrunnable(p);
1181 out:
1182         splx(s);
1183 }
1184
1185 #ifdef SMP
1186
1187 /*
1188  * This function is called via an IPI.  We will be in a critical section but
1189  * the MP lock will NOT be held.  Also note that by the time the ipi message
1190  * gets to us the process 'p' (arg) may no longer be scheduled or even valid.
1191  */
1192 static void
1193 signotify_remote(void *arg)
1194 {
1195         struct proc *p = arg;
1196         if (p == lwkt_preempted_proc())
1197                 signotify(p);
1198 }
1199
1200 #endif
1201
1202 /*
1203  * If the current process has received a signal (should be caught or cause
1204  * termination, should interrupt current syscall), return the signal number.
1205  * Stop signals with default action are processed immediately, then cleared;
1206  * they aren't returned.  This is checked after each entry to the system for
1207  * a syscall or trap (though this can usually be done without calling issignal
1208  * by checking the pending signal masks in the CURSIG macro.) The normal call
1209  * sequence is
1210  *
1211  *      while (sig = CURSIG(curproc))
1212  *              postsig(sig);
1213  */
1214 int
1215 issignal(p)
1216         register struct proc *p;
1217 {
1218         sigset_t mask;
1219         register int sig, prop;
1220
1221         for (;;) {
1222                 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1223
1224                 mask = p->p_siglist;
1225                 SIGSETNAND(mask, p->p_sigmask);
1226                 if (p->p_flag & P_PPWAIT)
1227                         SIG_STOPSIGMASK(mask);
1228                 if (!SIGNOTEMPTY(mask))         /* no signal to send */
1229                         return (0);
1230                 sig = sig_ffs(&mask);
1231
1232                 STOPEVENT(p, S_SIG, sig);
1233
1234                 /*
1235                  * We should see pending but ignored signals
1236                  * only if P_TRACED was on when they were posted.
1237                  */
1238                 if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1239                         SIGDELSET(p->p_siglist, sig);
1240                         continue;
1241                 }
1242                 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1243                         /*
1244                          * If traced, always stop, and stay
1245                          * stopped until released by the parent.
1246                          */
1247                         p->p_xstat = sig;
1248                         psignal(p->p_pptr, SIGCHLD);
1249                         do {
1250                                 stop(p);
1251                                 mi_switch();
1252                         } while (!trace_req(p) && p->p_flag & P_TRACED);
1253
1254                         /*
1255                          * If parent wants us to take the signal,
1256                          * then it will leave it in p->p_xstat;
1257                          * otherwise we just look for signals again.
1258                          */
1259                         SIGDELSET(p->p_siglist, sig);   /* clear old signal */
1260                         sig = p->p_xstat;
1261                         if (sig == 0)
1262                                 continue;
1263
1264                         /*
1265                          * Put the new signal into p_siglist.  If the
1266                          * signal is being masked, look for other signals.
1267                          */
1268                         SIGADDSET(p->p_siglist, sig);
1269                         if (SIGISMEMBER(p->p_sigmask, sig))
1270                                 continue;
1271
1272                         /*
1273                          * If the traced bit got turned off, go back up
1274                          * to the top to rescan signals.  This ensures
1275                          * that p_sig* and ps_sigact are consistent.
1276                          */
1277                         if ((p->p_flag & P_TRACED) == 0)
1278                                 continue;
1279                 }
1280
1281                 prop = sigprop(sig);
1282
1283                 /*
1284                  * Decide whether the signal should be returned.
1285                  * Return the signal's number, or fall through
1286                  * to clear it from the pending mask.
1287                  */
1288                 switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1289
1290                 case (int)SIG_DFL:
1291                         /*
1292                          * Don't take default actions on system processes.
1293                          */
1294                         if (p->p_pid <= 1) {
1295 #ifdef DIAGNOSTIC
1296                                 /*
1297                                  * Are you sure you want to ignore SIGSEGV
1298                                  * in init? XXX
1299                                  */
1300                                 printf("Process (pid %lu) got signal %d\n",
1301                                         (u_long)p->p_pid, sig);
1302 #endif
1303                                 break;          /* == ignore */
1304                         }
1305                         /*
1306                          * If there is a pending stop signal to process
1307                          * with default action, stop here,
1308                          * then clear the signal.  However,
1309                          * if process is member of an orphaned
1310                          * process group, ignore tty stop signals.
1311                          */
1312                         if (prop & SA_STOP) {
1313                                 if (p->p_flag & P_TRACED ||
1314                                     (p->p_pgrp->pg_jobc == 0 &&
1315                                     prop & SA_TTYSTOP))
1316                                         break;  /* == ignore */
1317                                 p->p_xstat = sig;
1318                                 stop(p);
1319                                 if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1320                                         psignal(p->p_pptr, SIGCHLD);
1321                                 mi_switch();
1322                                 break;
1323                         } else if (prop & SA_IGNORE) {
1324                                 /*
1325                                  * Except for SIGCONT, shouldn't get here.
1326                                  * Default action is to ignore; drop it.
1327                                  */
1328                                 break;          /* == ignore */
1329                         } else
1330                                 return (sig);
1331                         /*NOTREACHED*/
1332
1333                 case (int)SIG_IGN:
1334                         /*
1335                          * Masking above should prevent us ever trying
1336                          * to take action on an ignored signal other
1337                          * than SIGCONT, unless process is traced.
1338                          */
1339                         if ((prop & SA_CONT) == 0 &&
1340                             (p->p_flag & P_TRACED) == 0)
1341                                 printf("issignal\n");
1342                         break;          /* == ignore */
1343
1344                 default:
1345                         /*
1346                          * This signal has an action, let
1347                          * postsig() process it.
1348                          */
1349                         return (sig);
1350                 }
1351                 SIGDELSET(p->p_siglist, sig);           /* take the signal! */
1352         }
1353         /* NOTREACHED */
1354 }
1355
1356 /*
1357  * Put the argument process into the stopped state and notify the parent
1358  * via wakeup.  Signals are handled elsewhere.  The process must not be
1359  * on the run queue.
1360  */
1361 void
1362 stop(p)
1363         register struct proc *p;
1364 {
1365
1366         p->p_stat = SSTOP;
1367         p->p_flag &= ~P_WAITED;
1368         wakeup((caddr_t)p->p_pptr);
1369 }
1370
1371 /*
1372  * Take the action for the specified signal
1373  * from the current set of pending signals.
1374  */
1375 void
1376 postsig(sig)
1377         register int sig;
1378 {
1379         struct proc *p = curproc;
1380         struct sigacts *ps = p->p_sigacts;
1381         sig_t action;
1382         sigset_t returnmask;
1383         int code;
1384
1385         KASSERT(sig != 0, ("postsig"));
1386
1387         SIGDELSET(p->p_siglist, sig);
1388         action = ps->ps_sigact[_SIG_IDX(sig)];
1389 #ifdef KTRACE
1390         if (KTRPOINT(p->p_thread, KTR_PSIG))
1391                 ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1392                     &p->p_oldsigmask : &p->p_sigmask, 0);
1393 #endif
1394         STOPEVENT(p, S_SIG, sig);
1395
1396         if (action == SIG_DFL) {
1397                 /*
1398                  * Default action, where the default is to kill
1399                  * the process.  (Other cases were ignored above.)
1400                  */
1401                 sigexit(p, sig);
1402                 /* NOTREACHED */
1403         } else {
1404                 /*
1405                  * If we get here, the signal must be caught.
1406                  */
1407                 KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1408                     ("postsig action"));
1409                 /*
1410                  * Set the new mask value and also defer further
1411                  * occurrences of this signal.
1412                  *
1413                  * Special case: user has done a sigsuspend.  Here the
1414                  * current mask is not of interest, but rather the
1415                  * mask from before the sigsuspend is what we want
1416                  * restored after the signal processing is completed.
1417                  */
1418                 (void) splhigh();
1419                 if (p->p_flag & P_OLDMASK) {
1420                         returnmask = p->p_oldsigmask;
1421                         p->p_flag &= ~P_OLDMASK;
1422                 } else
1423                         returnmask = p->p_sigmask;
1424
1425                 SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1426                 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1427                         SIGADDSET(p->p_sigmask, sig);
1428
1429                 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1430                         /*
1431                          * See do_sigaction() for origin of this code.
1432                          */
1433                         SIGDELSET(p->p_sigcatch, sig);
1434                         if (sig != SIGCONT &&
1435                             sigprop(sig) & SA_IGNORE)
1436                                 SIGADDSET(p->p_sigignore, sig);
1437                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1438                 }
1439                 (void) spl0();
1440                 p->p_stats->p_ru.ru_nsignals++;
1441                 if (p->p_sig != sig) {
1442                         code = 0;
1443                 } else {
1444                         code = p->p_code;
1445                         p->p_code = 0;
1446                         p->p_sig = 0;
1447                 }
1448                 (*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1449         }
1450 }
1451
1452 /*
1453  * Kill the current process for stated reason.
1454  */
1455 void
1456 killproc(p, why)
1457         struct proc *p;
1458         char *why;
1459 {
1460         log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1461                 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1462         psignal(p, SIGKILL);
1463 }
1464
1465 /*
1466  * Force the current process to exit with the specified signal, dumping core
1467  * if appropriate.  We bypass the normal tests for masked and caught signals,
1468  * allowing unrecoverable failures to terminate the process without changing
1469  * signal state.  Mark the accounting record with the signal termination.
1470  * If dumping core, save the signal number for the debugger.  Calls exit and
1471  * does not return.
1472  */
1473 void
1474 sigexit(struct proc *p, int sig)
1475 {
1476         p->p_acflag |= AXSIG;
1477         if (sigprop(sig) & SA_CORE) {
1478                 p->p_sig = sig;
1479                 /*
1480                  * Log signals which would cause core dumps
1481                  * (Log as LOG_INFO to appease those who don't want
1482                  * these messages.)
1483                  * XXX : Todo, as well as euid, write out ruid too
1484                  */
1485                 if (coredump(p) == 0)
1486                         sig |= WCOREFLAG;
1487                 if (kern_logsigexit)
1488                         log(LOG_INFO,
1489                             "pid %d (%s), uid %d: exited on signal %d%s\n",
1490                             p->p_pid, p->p_comm,
1491                             p->p_ucred ? p->p_ucred->cr_uid : -1,
1492                             sig &~ WCOREFLAG,
1493                             sig & WCOREFLAG ? " (core dumped)" : "");
1494         }
1495         exit1(W_EXITCODE(0, sig));
1496         /* NOTREACHED */
1497 }
1498
1499 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1500 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1501               sizeof(corefilename), "process corefile name format string");
1502
1503 /*
1504  * expand_name(name, uid, pid)
1505  * Expand the name described in corefilename, using name, uid, and pid.
1506  * corefilename is a printf-like string, with three format specifiers:
1507  *      %N      name of process ("name")
1508  *      %P      process id (pid)
1509  *      %U      user id (uid)
1510  * For example, "%N.core" is the default; they can be disabled completely
1511  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1512  * This is controlled by the sysctl variable kern.corefile (see above).
1513  */
1514
1515 static char *
1516 expand_name(name, uid, pid)
1517 const char *name; uid_t uid; pid_t pid; {
1518         char *temp;
1519         char buf[11];           /* Buffer for pid/uid -- max 4B */
1520         int i, n;
1521         char *format = corefilename;
1522         size_t namelen;
1523
1524         temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1525         if (temp == NULL)
1526                 return NULL;
1527         namelen = strlen(name);
1528         for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1529                 int l;
1530                 switch (format[i]) {
1531                 case '%':       /* Format character */
1532                         i++;
1533                         switch (format[i]) {
1534                         case '%':
1535                                 temp[n++] = '%';
1536                                 break;
1537                         case 'N':       /* process name */
1538                                 if ((n + namelen) > MAXPATHLEN) {
1539                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1540                                             pid, name, uid, temp, name);
1541                                         free(temp, M_TEMP);
1542                                         return NULL;
1543                                 }
1544                                 memcpy(temp+n, name, namelen);
1545                                 n += namelen;
1546                                 break;
1547                         case 'P':       /* process id */
1548                                 l = sprintf(buf, "%u", pid);
1549                                 if ((n + l) > MAXPATHLEN) {
1550                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1551                                             pid, name, uid, temp, name);
1552                                         free(temp, M_TEMP);
1553                                         return NULL;
1554                                 }
1555                                 memcpy(temp+n, buf, l);
1556                                 n += l;
1557                                 break;
1558                         case 'U':       /* user id */
1559                                 l = sprintf(buf, "%u", uid);
1560                                 if ((n + l) > MAXPATHLEN) {
1561                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1562                                             pid, name, uid, temp, name);
1563                                         free(temp, M_TEMP);
1564                                         return NULL;
1565                                 }
1566                                 memcpy(temp+n, buf, l);
1567                                 n += l;
1568                                 break;
1569                         default:
1570                                 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1571                         }
1572                         break;
1573                 default:
1574                         temp[n++] = format[i];
1575                 }
1576         }
1577         temp[n] = '\0';
1578         return temp;
1579 }
1580
1581 /*
1582  * Dump a process' core.  The main routine does some
1583  * policy checking, and creates the name of the coredump;
1584  * then it passes on a vnode and a size limit to the process-specific
1585  * coredump routine if there is one; if there _is not_ one, it returns
1586  * ENOSYS; otherwise it returns the error from the process-specific routine.
1587  */
1588
1589 static int
1590 coredump(struct proc *p)
1591 {
1592         struct vnode *vp;
1593         struct ucred *cred = p->p_ucred;
1594         struct thread *td = p->p_thread;
1595         struct flock lf;
1596         struct nameidata nd;
1597         struct vattr vattr;
1598         int error, error1;
1599         char *name;                     /* name of corefile */
1600         off_t limit;
1601         
1602         STOPEVENT(p, S_CORE, 0);
1603
1604         if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0)
1605                 return (EFAULT);
1606         
1607         /*
1608          * Note that the bulk of limit checking is done after
1609          * the corefile is created.  The exception is if the limit
1610          * for corefiles is 0, in which case we don't bother
1611          * creating the corefile at all.  This layout means that
1612          * a corefile is truncated instead of not being created,
1613          * if it is larger than the limit.
1614          */
1615         limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1616         if (limit == 0)
1617                 return 0;
1618
1619         name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1620         if (name == NULL)
1621                 return (EINVAL);
1622         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
1623         error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1624         free(name, M_TEMP);
1625         if (error)
1626                 return (error);
1627         NDFREE(&nd, NDF_ONLY_PNBUF);
1628         vp = nd.ni_vp;
1629
1630         VOP_UNLOCK(vp, 0, td);
1631         lf.l_whence = SEEK_SET;
1632         lf.l_start = 0;
1633         lf.l_len = 0;
1634         lf.l_type = F_WRLCK;
1635         error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
1636         if (error)
1637                 goto out2;
1638
1639         /* Don't dump to non-regular files or files with links. */
1640         if (vp->v_type != VREG ||
1641             VOP_GETATTR(vp, &vattr, td) || vattr.va_nlink != 1) {
1642                 error = EFAULT;
1643                 goto out1;
1644         }
1645
1646         VATTR_NULL(&vattr);
1647         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1648         vattr.va_size = 0;
1649         VOP_LEASE(vp, td, cred, LEASE_WRITE);
1650         VOP_SETATTR(vp, &vattr, cred, td);
1651         p->p_acflag |= ACORE;
1652         VOP_UNLOCK(vp, 0, td);
1653
1654         error = p->p_sysent->sv_coredump ?
1655           p->p_sysent->sv_coredump(p, vp, limit) :
1656           ENOSYS;
1657
1658 out1:
1659         lf.l_type = F_UNLCK;
1660         VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
1661 out2:
1662         error1 = vn_close(vp, FWRITE, td);
1663         if (error == 0)
1664                 error = error1;
1665         return (error);
1666 }
1667
1668 /*
1669  * Nonexistent system call-- signal process (may want to handle it).
1670  * Flag error in case process won't see signal immediately (blocked or ignored).
1671  */
1672 #ifndef _SYS_SYSPROTO_H_
1673 struct nosys_args {
1674         int     dummy;
1675 };
1676 #endif
1677 /* ARGSUSED */
1678 int
1679 nosys(struct nosys_args *args)
1680 {
1681         psignal(curproc, SIGSYS);
1682         return (EINVAL);
1683 }
1684
1685 /*
1686  * Send a SIGIO or SIGURG signal to a process or process group using
1687  * stored credentials rather than those of the current process.
1688  */
1689 void
1690 pgsigio(struct sigio *sigio, int sig, int checkctty)
1691 {
1692         if (sigio == NULL)
1693                 return;
1694                 
1695         if (sigio->sio_pgid > 0) {
1696                 if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred,
1697                              sigio->sio_proc))
1698                         psignal(sigio->sio_proc, sig);
1699         } else if (sigio->sio_pgid < 0) {
1700                 struct proc *p;
1701
1702                 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist)
1703                         if (CANSIGIO(sigio->sio_ruid, sigio->sio_ucred, p) &&
1704                             (checkctty == 0 || (p->p_flag & P_CONTROLT)))
1705                                 psignal(p, sig);
1706         }
1707 }
1708
1709 static int
1710 filt_sigattach(struct knote *kn)
1711 {
1712         struct proc *p = curproc;
1713
1714         kn->kn_ptr.p_proc = p;
1715         kn->kn_flags |= EV_CLEAR;               /* automatically set */
1716
1717         /* XXX lock the proc here while adding to the list? */
1718         SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1719
1720         return (0);
1721 }
1722
1723 static void
1724 filt_sigdetach(struct knote *kn)
1725 {
1726         struct proc *p = kn->kn_ptr.p_proc;
1727
1728         SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1729 }
1730
1731 /*
1732  * signal knotes are shared with proc knotes, so we apply a mask to 
1733  * the hint in order to differentiate them from process hints.  This
1734  * could be avoided by using a signal-specific knote list, but probably
1735  * isn't worth the trouble.
1736  */
1737 static int
1738 filt_signal(struct knote *kn, long hint)
1739 {
1740
1741         if (hint & NOTE_SIGNAL) {
1742                 hint &= ~NOTE_SIGNAL;
1743
1744                 if (kn->kn_id == hint)
1745                         kn->kn_data++;
1746         }
1747         return (kn->kn_data != 0);
1748 }