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