The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sig.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 3f611ab3f75e18a4004e3e8282d69c67


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.