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/servers/pm/signal.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 /* This file handles signals, which are asynchronous events and are generally
    2  * a messy and unpleasant business.  Signals can be generated by the KILL
    3  * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
    4  * In all cases control eventually passes to check_sig() to see which processes
    5  * can be signaled.  The actual signaling is done by sig_proc().
    6  *
    7  * The entry points into this file are:
    8  *   do_sigaction:   perform the SIGACTION system call
    9  *   do_sigpending:  perform the SIGPENDING system call
   10  *   do_sigprocmask: perform the SIGPROCMASK system call
   11  *   do_sigreturn:   perform the SIGRETURN system call
   12  *   do_sigsuspend:  perform the SIGSUSPEND system call
   13  *   do_kill:   perform the KILL system call
   14  *   do_alarm:  perform the ALARM system call by calling set_alarm()
   15  *   set_alarm: tell the clock task to start or stop a timer
   16  *   do_pause:  perform the PAUSE system call
   17  *   ksig_pending: the kernel notified about pending signals
   18  *   sig_proc:  interrupt or terminate a signaled process
   19  *   check_sig: check which processes to signal with sig_proc()
   20  *   check_pending:  check if a pending signal can now be delivered
   21  */
   22 
   23 #include "pm.h"
   24 #include <sys/stat.h>
   25 #include <sys/ptrace.h>
   26 #include <minix/callnr.h>
   27 #include <minix/com.h>
   28 #include <signal.h>
   29 #include <sys/sigcontext.h>
   30 #include <string.h>
   31 #include "mproc.h"
   32 #include "param.h"
   33 
   34 #define CORE_MODE       0777    /* mode to use on core image files */
   35 #define DUMPED          0200    /* bit set in status when core dumped */
   36 
   37 FORWARD _PROTOTYPE( void dump_core, (struct mproc *rmp)                 );
   38 FORWARD _PROTOTYPE( void unpause, (int pro)                             );
   39 FORWARD _PROTOTYPE( void handle_sig, (int proc_nr, sigset_t sig_map)    );
   40 FORWARD _PROTOTYPE( void cause_sigalrm, (struct timer *tp)              );
   41 
   42 /*===========================================================================*
   43  *                              do_sigaction                                 *
   44  *===========================================================================*/
   45 PUBLIC int do_sigaction()
   46 {
   47   int r;
   48   struct sigaction svec;
   49   struct sigaction *svp;
   50 
   51   if (m_in.sig_nr == SIGKILL) return(OK);
   52   if (m_in.sig_nr < 1 || m_in.sig_nr > _NSIG) return (EINVAL);
   53   svp = &mp->mp_sigact[m_in.sig_nr];
   54   if ((struct sigaction *) m_in.sig_osa != (struct sigaction *) NULL) {
   55         r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp,
   56                 who, (vir_bytes) m_in.sig_osa, (phys_bytes) sizeof(svec));
   57         if (r != OK) return(r);
   58   }
   59 
   60   if ((struct sigaction *) m_in.sig_nsa == (struct sigaction *) NULL) 
   61         return(OK);
   62 
   63   /* Read in the sigaction structure. */
   64   r = sys_datacopy(who, (vir_bytes) m_in.sig_nsa,
   65                 PM_PROC_NR, (vir_bytes) &svec, (phys_bytes) sizeof(svec));
   66   if (r != OK) return(r);
   67 
   68   if (svec.sa_handler == SIG_IGN) {
   69         sigaddset(&mp->mp_ignore, m_in.sig_nr);
   70         sigdelset(&mp->mp_sigpending, m_in.sig_nr);
   71         sigdelset(&mp->mp_catch, m_in.sig_nr);
   72         sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
   73   } else if (svec.sa_handler == SIG_DFL) {
   74         sigdelset(&mp->mp_ignore, m_in.sig_nr);
   75         sigdelset(&mp->mp_catch, m_in.sig_nr);
   76         sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
   77   } else if (svec.sa_handler == SIG_MESS) {
   78         if (! (mp->mp_flags & PRIV_PROC)) return(EPERM);
   79         sigdelset(&mp->mp_ignore, m_in.sig_nr);
   80         sigaddset(&mp->mp_sig2mess, m_in.sig_nr);
   81         sigdelset(&mp->mp_catch, m_in.sig_nr);
   82   } else {
   83         sigdelset(&mp->mp_ignore, m_in.sig_nr);
   84         sigaddset(&mp->mp_catch, m_in.sig_nr);
   85         sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
   86   }
   87   mp->mp_sigact[m_in.sig_nr].sa_handler = svec.sa_handler;
   88   sigdelset(&svec.sa_mask, SIGKILL);
   89   mp->mp_sigact[m_in.sig_nr].sa_mask = svec.sa_mask;
   90   mp->mp_sigact[m_in.sig_nr].sa_flags = svec.sa_flags;
   91   mp->mp_sigreturn = (vir_bytes) m_in.sig_ret;
   92   return(OK);
   93 }
   94 
   95 /*===========================================================================*
   96  *                              do_sigpending                                *
   97  *===========================================================================*/
   98 PUBLIC int do_sigpending()
   99 {
  100   mp->mp_reply.reply_mask = (long) mp->mp_sigpending;
  101   return OK;
  102 }
  103 
  104 /*===========================================================================*
  105  *                              do_sigprocmask                               *
  106  *===========================================================================*/
  107 PUBLIC int do_sigprocmask()
  108 {
  109 /* Note that the library interface passes the actual mask in sigmask_set,
  110  * not a pointer to the mask, in order to save a copy.  Similarly,
  111  * the old mask is placed in the return message which the library
  112  * interface copies (if requested) to the user specified address.
  113  *
  114  * The library interface must set SIG_INQUIRE if the 'act' argument
  115  * is NULL.
  116  */
  117 
  118   int i;
  119 
  120   mp->mp_reply.reply_mask = (long) mp->mp_sigmask;
  121 
  122   switch (m_in.sig_how) {
  123       case SIG_BLOCK:
  124         sigdelset((sigset_t *)&m_in.sig_set, SIGKILL);
  125         for (i = 1; i <= _NSIG; i++) {
  126                 if (sigismember((sigset_t *)&m_in.sig_set, i))
  127                         sigaddset(&mp->mp_sigmask, i);
  128         }
  129         break;
  130 
  131       case SIG_UNBLOCK:
  132         for (i = 1; i <= _NSIG; i++) {
  133                 if (sigismember((sigset_t *)&m_in.sig_set, i))
  134                         sigdelset(&mp->mp_sigmask, i);
  135         }
  136         check_pending(mp);
  137         break;
  138 
  139       case SIG_SETMASK:
  140         sigdelset((sigset_t *) &m_in.sig_set, SIGKILL);
  141         mp->mp_sigmask = (sigset_t) m_in.sig_set;
  142         check_pending(mp);
  143         break;
  144 
  145       case SIG_INQUIRE:
  146         break;
  147 
  148       default:
  149         return(EINVAL);
  150         break;
  151   }
  152   return OK;
  153 }
  154 
  155 /*===========================================================================*
  156  *                              do_sigsuspend                                *
  157  *===========================================================================*/
  158 PUBLIC int do_sigsuspend()
  159 {
  160   mp->mp_sigmask2 = mp->mp_sigmask;     /* save the old mask */
  161   mp->mp_sigmask = (sigset_t) m_in.sig_set;
  162   sigdelset(&mp->mp_sigmask, SIGKILL);
  163   mp->mp_flags |= SIGSUSPENDED;
  164   check_pending(mp);
  165   return(SUSPEND);
  166 }
  167 
  168 /*===========================================================================*
  169  *                              do_sigreturn                                 *
  170  *===========================================================================*/
  171 PUBLIC int do_sigreturn()
  172 {
  173 /* A user signal handler is done.  Restore context and check for
  174  * pending unblocked signals.
  175  */
  176 
  177   int r;
  178 
  179   mp->mp_sigmask = (sigset_t) m_in.sig_set;
  180   sigdelset(&mp->mp_sigmask, SIGKILL);
  181 
  182   r = sys_sigreturn(who, (struct sigmsg *) m_in.sig_context);
  183   check_pending(mp);
  184   return(r);
  185 }
  186 
  187 /*===========================================================================*
  188  *                              do_kill                                      *
  189  *===========================================================================*/
  190 PUBLIC int do_kill()
  191 {
  192 /* Perform the kill(pid, signo) system call. */
  193 
  194   return check_sig(m_in.pid, m_in.sig_nr);
  195 }
  196 
  197 /*===========================================================================*
  198  *                              ksig_pending                                 *
  199  *===========================================================================*/
  200 PUBLIC int ksig_pending()
  201 {
  202 /* Certain signals, such as segmentation violations originate in the kernel.
  203  * When the kernel detects such signals, it notifies the PM to take further 
  204  * action. The PM requests the kernel to send messages with the process
  205  * slot and bit map for all signaled processes. The File System, for example,
  206  * uses this mechanism to signal writing on broken pipes (SIGPIPE). 
  207  *
  208  * The kernel has notified the PM about pending signals. Request pending
  209  * signals until all signals are handled. If there are no more signals,
  210  * NONE is returned in the process number field.
  211  */ 
  212  int proc_nr;
  213  sigset_t sig_map;
  214 
  215  while (TRUE) {
  216    sys_getksig(&proc_nr, &sig_map);     /* get an arbitrary pending signal */
  217    if (NONE == proc_nr) {               /* stop if no more pending signals */
  218         break;
  219    } else {
  220         handle_sig(proc_nr, sig_map);   /* handle the received signal */
  221         sys_endksig(proc_nr);           /* tell kernel it's done */
  222    }
  223  } 
  224  return(SUSPEND);                       /* prevents sending reply */
  225 }
  226 
  227 /*===========================================================================*
  228  *                              handle_sig                                   *
  229  *===========================================================================*/
  230 PRIVATE void handle_sig(proc_nr, sig_map)
  231 int proc_nr;
  232 sigset_t sig_map;
  233 {
  234   register struct mproc *rmp;
  235   int i;
  236   pid_t proc_id, id;
  237 
  238   rmp = &mproc[proc_nr];
  239   if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) return;
  240   proc_id = rmp->mp_pid;
  241   mp = &mproc[0];                       /* pretend signals are from PM */
  242   mp->mp_procgrp = rmp->mp_procgrp;     /* get process group right */
  243 
  244   /* Check each bit in turn to see if a signal is to be sent.  Unlike
  245    * kill(), the kernel may collect several unrelated signals for a
  246    * process and pass them to PM in one blow.  Thus loop on the bit
  247    * map. For SIGINT, SIGWINCH and SIGQUIT, use proc_id 0 to indicate
  248    * a broadcast to the recipient's process group.  For SIGKILL, use
  249    * proc_id -1 to indicate a systemwide broadcast.
  250    */
  251   for (i = 1; i <= _NSIG; i++) {
  252         if (!sigismember(&sig_map, i)) continue;
  253         switch (i) {
  254             case SIGINT:
  255             case SIGQUIT:
  256             case SIGWINCH:
  257                 id = 0; break;  /* broadcast to process group */
  258             case SIGKILL:
  259                 id = -1; break; /* broadcast to all except INIT */
  260             default:
  261                 id = proc_id;
  262                 break;
  263         }
  264         check_sig(id, i);
  265   }
  266 }
  267 
  268 /*===========================================================================*
  269  *                              do_alarm                                     *
  270  *===========================================================================*/
  271 PUBLIC int do_alarm()
  272 {
  273 /* Perform the alarm(seconds) system call. */
  274   return(set_alarm(who, m_in.seconds));
  275 }
  276 
  277 /*===========================================================================*
  278  *                              set_alarm                                    *
  279  *===========================================================================*/
  280 PUBLIC int set_alarm(proc_nr, sec)
  281 int proc_nr;                    /* process that wants the alarm */
  282 int sec;                        /* how many seconds delay before the signal */
  283 {
  284 /* This routine is used by do_alarm() to set the alarm timer.  It is also used
  285  * to turn the timer off when a process exits with the timer still on.
  286  */
  287   clock_t ticks;        /* number of ticks for alarm */
  288   clock_t exptime;      /* needed for remaining time on previous alarm */
  289   clock_t uptime;       /* current system time */
  290   int remaining;        /* previous time left in seconds */
  291   int s;
  292 
  293   /* First determine remaining time of previous alarm, if set. */
  294   if (mproc[proc_nr].mp_flags & ALARM_ON) {
  295         if ( (s=getuptime(&uptime)) != OK) 
  296                 panic(__FILE__,"set_alarm couldn't get uptime", s);
  297         exptime = *tmr_exp_time(&mproc[proc_nr].mp_timer);
  298         remaining = (int) ((exptime - uptime + (HZ-1))/HZ);
  299         if (remaining < 0) remaining = 0;       
  300   } else {
  301         remaining = 0; 
  302   }
  303 
  304   /* Tell the clock task to provide a signal message when the time comes.
  305    *
  306    * Large delays cause a lot of problems.  First, the alarm system call
  307    * takes an unsigned seconds count and the library has cast it to an int.
  308    * That probably works, but on return the library will convert "negative"
  309    * unsigneds to errors.  Presumably no one checks for these errors, so
  310    * force this call through.  Second, If unsigned and long have the same
  311    * size, converting from seconds to ticks can easily overflow.  Finally,
  312    * the kernel has similar overflow bugs adding ticks.
  313    *
  314    * Fixing this requires a lot of ugly casts to fit the wrong interface
  315    * types and to avoid overflow traps.  ALRM_EXP_TIME has the right type
  316    * (clock_t) although it is declared as long.  How can variables like
  317    * this be declared properly without combinatorial explosion of message
  318    * types?
  319    */
  320   ticks = (clock_t) (HZ * (unsigned long) (unsigned) sec);
  321   if ( (unsigned long) ticks / HZ != (unsigned) sec)
  322         ticks = LONG_MAX;       /* eternity (really TMR_NEVER) */
  323 
  324   if (ticks != 0) {
  325         pm_set_timer(&mproc[proc_nr].mp_timer, ticks, cause_sigalrm, proc_nr);
  326         mproc[proc_nr].mp_flags |=  ALARM_ON;
  327   } else if (mproc[proc_nr].mp_flags & ALARM_ON) {
  328         pm_cancel_timer(&mproc[proc_nr].mp_timer);
  329         mproc[proc_nr].mp_flags &= ~ALARM_ON;
  330   }
  331   return(remaining);
  332 }
  333 
  334 /*===========================================================================*
  335  *                              cause_sigalrm                                *
  336  *===========================================================================*/
  337 PRIVATE void cause_sigalrm(tp)
  338 struct timer *tp;
  339 {
  340   int proc_nr;
  341   register struct mproc *rmp;
  342 
  343   proc_nr = tmr_arg(tp)->ta_int;        /* get process from timer */
  344   rmp = &mproc[proc_nr];
  345 
  346   if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) return;
  347   if ((rmp->mp_flags & ALARM_ON) == 0) return;
  348   rmp->mp_flags &= ~ALARM_ON;
  349   check_sig(rmp->mp_pid, SIGALRM);
  350 }
  351 
  352 /*===========================================================================*
  353  *                              do_pause                                     *
  354  *===========================================================================*/
  355 PUBLIC int do_pause()
  356 {
  357 /* Perform the pause() system call. */
  358 
  359   mp->mp_flags |= PAUSED;
  360   return(SUSPEND);
  361 }
  362 
  363 /*===========================================================================*
  364  *                              sig_proc                                     *
  365  *===========================================================================*/
  366 PUBLIC void sig_proc(rmp, signo)
  367 register struct mproc *rmp;     /* pointer to the process to be signaled */
  368 int signo;                      /* signal to send to process (1 to _NSIG) */
  369 {
  370 /* Send a signal to a process.  Check to see if the signal is to be caught,
  371  * ignored, tranformed into a message (for system processes) or blocked.  
  372  *  - If the signal is to be transformed into a message, request the KERNEL to
  373  * send the target process a system notification with the pending signal as an 
  374  * argument. 
  375  *  - If the signal is to be caught, request the KERNEL to push a sigcontext 
  376  * structure and a sigframe structure onto the catcher's stack.  Also, KERNEL 
  377  * will reset the program counter and stack pointer, so that when the process 
  378  * next runs, it will be executing the signal handler. When the signal handler 
  379  * returns,  sigreturn(2) will be called.  Then KERNEL will restore the signal 
  380  * context from the sigcontext structure.
  381  * If there is insufficient stack space, kill the process.
  382  */
  383 
  384   vir_bytes new_sp;
  385   int s;
  386   int slot;
  387   int sigflags;
  388   struct sigmsg sm;
  389 
  390   slot = (int) (rmp - mproc);
  391   if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) {
  392         printf("PM: signal %d sent to %s process %d\n",
  393                 signo, (rmp->mp_flags & ZOMBIE) ? "zombie" : "dead", slot);
  394         panic(__FILE__,"", NO_NUM);
  395   }
  396   if ((rmp->mp_flags & TRACED) && signo != SIGKILL) {
  397         /* A traced process has special handling. */
  398         unpause(slot);
  399         stop_proc(rmp, signo);  /* a signal causes it to stop */
  400         return;
  401   }
  402   /* Some signals are ignored by default. */
  403   if (sigismember(&rmp->mp_ignore, signo)) { 
  404         return;
  405   }
  406   if (sigismember(&rmp->mp_sigmask, signo)) {
  407         /* Signal should be blocked. */
  408         sigaddset(&rmp->mp_sigpending, signo);
  409         return;
  410   }
  411 #if ENABLE_SWAP
  412   if (rmp->mp_flags & ONSWAP) {
  413         /* Process is swapped out, leave signal pending. */
  414         sigaddset(&rmp->mp_sigpending, signo);
  415         swap_inqueue(rmp);
  416         return;
  417   }
  418 #endif
  419   sigflags = rmp->mp_sigact[signo].sa_flags;
  420   if (sigismember(&rmp->mp_catch, signo)) {
  421         if (rmp->mp_flags & SIGSUSPENDED)
  422                 sm.sm_mask = rmp->mp_sigmask2;
  423         else
  424                 sm.sm_mask = rmp->mp_sigmask;
  425         sm.sm_signo = signo;
  426         sm.sm_sighandler = (vir_bytes) rmp->mp_sigact[signo].sa_handler;
  427         sm.sm_sigreturn = rmp->mp_sigreturn;
  428         if ((s=get_stack_ptr(slot, &new_sp)) != OK)
  429                 panic(__FILE__,"couldn't get new stack pointer",s);
  430         sm.sm_stkptr = new_sp;
  431 
  432         /* Make room for the sigcontext and sigframe struct. */
  433         new_sp -= sizeof(struct sigcontext)
  434                                  + 3 * sizeof(char *) + 2 * sizeof(int);
  435 
  436         if (adjust(rmp, rmp->mp_seg[D].mem_len, new_sp) != OK)
  437                 goto doterminate;
  438 
  439         rmp->mp_sigmask |= rmp->mp_sigact[signo].sa_mask;
  440         if (sigflags & SA_NODEFER)
  441                 sigdelset(&rmp->mp_sigmask, signo);
  442         else
  443                 sigaddset(&rmp->mp_sigmask, signo);
  444 
  445         if (sigflags & SA_RESETHAND) {
  446                 sigdelset(&rmp->mp_catch, signo);
  447                 rmp->mp_sigact[signo].sa_handler = SIG_DFL;
  448         }
  449 
  450         if (OK == (s=sys_sigsend(slot, &sm))) {
  451 
  452                 sigdelset(&rmp->mp_sigpending, signo);
  453                 /* If process is hanging on PAUSE, WAIT, SIGSUSPEND, tty, 
  454                  * pipe, etc., release it.
  455                  */
  456                 unpause(slot);
  457                 return;
  458         }
  459         panic(__FILE__, "warning, sys_sigsend failed", s);
  460   }
  461   else if (sigismember(&rmp->mp_sig2mess, signo)) {
  462         if (OK != (s=sys_kill(slot,signo)))
  463                 panic(__FILE__, "warning, sys_kill failed", s);
  464         return;
  465   }
  466 
  467 doterminate:
  468   /* Signal should not or cannot be caught.  Take default action. */
  469   if (sigismember(&ign_sset, signo)) return;
  470 
  471   rmp->mp_sigstatus = (char) signo;
  472   if (sigismember(&core_sset, signo)) {
  473 #if ENABLE_SWAP
  474         if (rmp->mp_flags & ONSWAP) {
  475                 /* Process is swapped out, leave signal pending. */
  476                 sigaddset(&rmp->mp_sigpending, signo);
  477                 swap_inqueue(rmp);
  478                 return;
  479         }
  480 #endif
  481         /* Switch to the user's FS environment and dump core. */
  482         tell_fs(CHDIR, slot, FALSE, 0);
  483         dump_core(rmp);
  484   }
  485   pm_exit(rmp, 0);              /* terminate process */
  486 }
  487 
  488 /*===========================================================================*
  489  *                              check_sig                                    *
  490  *===========================================================================*/
  491 PUBLIC int check_sig(proc_id, signo)
  492 pid_t proc_id;                  /* pid of proc to sig, or 0 or -1, or -pgrp */
  493 int signo;                      /* signal to send to process (0 to _NSIG) */
  494 {
  495 /* Check to see if it is possible to send a signal.  The signal may have to be
  496  * sent to a group of processes.  This routine is invoked by the KILL system
  497  * call, and also when the kernel catches a DEL or other signal.
  498  */
  499 
  500   register struct mproc *rmp;
  501   int count;                    /* count # of signals sent */
  502   int error_code;
  503 
  504   if (signo < 0 || signo > _NSIG) return(EINVAL);
  505 
  506   /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
  507   if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
  508 
  509   /* Search the proc table for processes to signal.  (See forkexit.c about
  510    * pid magic.)
  511    */
  512   count = 0;
  513   error_code = ESRCH;
  514   for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
  515         if (!(rmp->mp_flags & IN_USE)) continue;
  516         if ((rmp->mp_flags & ZOMBIE) && signo != 0) continue;
  517 
  518         /* Check for selection. */
  519         if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
  520         if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
  521         if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
  522         if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
  523 
  524         /* Check for permission. */
  525         if (mp->mp_effuid != SUPER_USER
  526             && mp->mp_realuid != rmp->mp_realuid
  527             && mp->mp_effuid != rmp->mp_realuid
  528             && mp->mp_realuid != rmp->mp_effuid
  529             && mp->mp_effuid != rmp->mp_effuid) {
  530                 error_code = EPERM;
  531                 continue;
  532         }
  533 
  534         count++;
  535         if (signo == 0) continue;
  536 
  537         /* 'sig_proc' will handle the disposition of the signal.  The
  538          * signal may be caught, blocked, ignored, or cause process
  539          * termination, possibly with core dump.
  540          */
  541         sig_proc(rmp, signo);
  542 
  543         if (proc_id > 0) break; /* only one process being signaled */
  544   }
  545 
  546   /* If the calling process has killed itself, don't reply. */
  547   if ((mp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) return(SUSPEND);
  548   return(count > 0 ? OK : error_code);
  549 }
  550 
  551 /*===========================================================================*
  552  *                              check_pending                                *
  553  *===========================================================================*/
  554 PUBLIC void check_pending(rmp)
  555 register struct mproc *rmp;
  556 {
  557   /* Check to see if any pending signals have been unblocked.  The
  558    * first such signal found is delivered.
  559    *
  560    * If multiple pending unmasked signals are found, they will be
  561    * delivered sequentially.
  562    *
  563    * There are several places in this file where the signal mask is
  564    * changed.  At each such place, check_pending() should be called to
  565    * check for newly unblocked signals.
  566    */
  567 
  568   int i;
  569 
  570   for (i = 1; i <= _NSIG; i++) {
  571         if (sigismember(&rmp->mp_sigpending, i) &&
  572                 !sigismember(&rmp->mp_sigmask, i)) {
  573                 sigdelset(&rmp->mp_sigpending, i);
  574                 sig_proc(rmp, i);
  575                 break;
  576         }
  577   }
  578 }
  579 
  580 /*===========================================================================*
  581  *                              unpause                                      *
  582  *===========================================================================*/
  583 PRIVATE void unpause(pro)
  584 int pro;                        /* which process number */
  585 {
  586 /* A signal is to be sent to a process.  If that process is hanging on a
  587  * system call, the system call must be terminated with EINTR.  Possible
  588  * calls are PAUSE, WAIT, READ and WRITE, the latter two for pipes and ttys.
  589  * First check if the process is hanging on an PM call.  If not, tell FS,
  590  * so it can check for READs and WRITEs from pipes, ttys and the like.
  591  */
  592 
  593   register struct mproc *rmp;
  594 
  595   rmp = &mproc[pro];
  596 
  597   /* Check to see if process is hanging on a PAUSE, WAIT or SIGSUSPEND call. */
  598   if (rmp->mp_flags & (PAUSED | WAITING | SIGSUSPENDED)) {
  599         rmp->mp_flags &= ~(PAUSED | WAITING | SIGSUSPENDED);
  600         setreply(pro, EINTR);
  601         return;
  602   }
  603 
  604   /* Process is not hanging on an PM call.  Ask FS to take a look. */
  605   tell_fs(UNPAUSE, pro, 0, 0);
  606 }
  607 
  608 /*===========================================================================*
  609  *                              dump_core                                    *
  610  *===========================================================================*/
  611 PRIVATE void dump_core(rmp)
  612 register struct mproc *rmp;     /* whose core is to be dumped */
  613 {
  614 /* Make a core dump on the file "core", if possible. */
  615 
  616   int s, fd, seg, slot;
  617   vir_bytes current_sp;
  618   long trace_data, trace_off;
  619 
  620   slot = (int) (rmp - mproc);
  621 
  622   /* Can core file be written?  We are operating in the user's FS environment,
  623    * so no special permission checks are needed.
  624    */
  625   if (rmp->mp_realuid != rmp->mp_effuid) return;
  626   if ( (fd = open(core_name, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK,
  627                                                 CORE_MODE)) < 0) return;
  628   rmp->mp_sigstatus |= DUMPED;
  629 
  630   /* Make sure the stack segment is up to date.
  631    * We don't want adjust() to fail unless current_sp is preposterous,
  632    * but it might fail due to safety checking.  Also, we don't really want 
  633    * the adjust() for sending a signal to fail due to safety checking.  
  634    * Maybe make SAFETY_BYTES a parameter.
  635    */
  636   if ((s=get_stack_ptr(slot, &current_sp)) != OK)
  637         panic(__FILE__,"couldn't get new stack pointer",s);
  638   adjust(rmp, rmp->mp_seg[D].mem_len, current_sp);
  639 
  640   /* Write the memory map of all segments to begin the core file. */
  641   if (write(fd, (char *) rmp->mp_seg, (unsigned) sizeof rmp->mp_seg)
  642       != (unsigned) sizeof rmp->mp_seg) {
  643         close(fd);
  644         return;
  645   }
  646 
  647   /* Write out the whole kernel process table entry to get the regs. */
  648   trace_off = 0;
  649   while (sys_trace(T_GETUSER, slot, trace_off, &trace_data) == OK) {
  650         if (write(fd, (char *) &trace_data, (unsigned) sizeof (long))
  651             != (unsigned) sizeof (long)) {
  652                 close(fd);
  653                 return;
  654         }
  655         trace_off += sizeof (long);
  656   }
  657 
  658   /* Loop through segments and write the segments themselves out. */
  659   for (seg = 0; seg < NR_LOCAL_SEGS; seg++) {
  660         rw_seg(1, fd, slot, seg,
  661                 (phys_bytes) rmp->mp_seg[seg].mem_len << CLICK_SHIFT);
  662   }
  663   close(fd);
  664 }
  665 

Cache object: 10f27f7eb668f21074647113800b09cb


[ 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.