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