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