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