home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xdbx / part01 / signals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  3.9 KB  |  120 lines

  1. /*****************************************************************************
  2.  *
  3.  *  xdbx - X Window System interface to the dbx debugger
  4.  *
  5.  *  Copyright 1989 The University of Texas at Austin
  6.  *  Copyright 1990 Microelectronics and Computer Technology Corporation
  7.  *
  8.  *  Permission to use, copy, modify, and distribute this software and its
  9.  *  documentation for any purpose and without fee is hereby granted,
  10.  *  provided that the above copyright notice appear in all copies and that
  11.  *  both that copyright notice and this permission notice appear in
  12.  *  supporting documentation, and that the name of The University of Texas
  13.  *  and Microelectronics and Computer Technology Corporation (MCC) not be 
  14.  *  used in advertising or publicity pertaining to distribution of
  15.  *  the software without specific, written prior permission.  The
  16.  *  University of Texas and MCC makes no representations about the 
  17.  *  suitability of this software for any purpose.  It is provided "as is" 
  18.  *  without express or implied warranty.
  19.  *
  20.  *  THE UNIVERSITY OF TEXAS AND MCC DISCLAIMS ALL WARRANTIES WITH REGARD TO
  21.  *  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22.  *  FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TEXAS OR MCC BE LIABLE FOR
  23.  *  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  24.  *  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  25.  *  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  26.  *  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  *
  28.  *  Author:      Po Cheung
  29.  *  Created:       March 10, 1989
  30.  *
  31.  *****************************************************************************/
  32.  
  33. /* signals.c
  34.  *
  35.  *   Signal handling for xdbx and dbx.
  36.  *
  37.  *   kill_hanlder():    For SIGINT, SIGQUIT, SIGILL, SIGBUS, SIGTERM
  38.  *            print error message and exit with signal status.
  39.  *   quit_handler():    SIGCHLD, wait for dbx to die and exit gracefully.
  40.  *   stop_handler():    SIGTSTP, stop dbx process, then stop own process.
  41.  *   cont_handler():    SIGCONT, continue dbx process.
  42.  *   trap_signals():    Install signal handlers.
  43.  */
  44.  
  45. #include <signal.h>
  46. #include <sys/wait.h>
  47. #include "global.h"
  48.  
  49. /*  Kill the dbx child process and then exits. */
  50. /*  ARGSUSED */
  51. static void kill_handler(sig, code, scp, addr)
  52.     int sig, code;
  53.     struct sigcontext *scp;
  54.     char *addr;
  55. {
  56.     if (FalseSignal) {
  57.     FalseSignal = FALSE;
  58.     return;
  59.     }
  60.     kill(dbxpid, SIGKILL);
  61.     switch (sig) {
  62.       case SIGINT  : fprintf(stderr, "Interrupt\n"); break;
  63.       case SIGQUIT : fprintf(stderr, "Quit\n"); break;
  64.       case SIGILL  : fprintf(stderr, "Illegal instruction\n"); break;
  65.       case SIGBUS  : fprintf(stderr, "Bus error\n"); break;
  66.       case SIGSEGV : fprintf(stderr, "Segmentation violation\n"); break;
  67.       case SIGTERM : fprintf(stderr, "Soft kill\n"); break;
  68.     }
  69.     exit(sig);
  70. }
  71.  
  72.  
  73. static void quit_handler()
  74. {
  75.     union wait status;
  76.  
  77.     /*  wait for the child to report its status; if the child has died, 
  78.      *  exit gracefully.
  79.      */
  80.     wait3(&status, WNOHANG|WUNTRACED, NULL);
  81.     if ((WIFEXITED(status) || WIFSIGNALED(status)) && !WIFSTOPPED(status)) {
  82.         exit(1);
  83.     }
  84. }
  85.  
  86.  
  87. static void stop_handler()
  88. {
  89.     if (dbxpid)
  90.     kill(dbxpid, SIGSTOP);    /* stop dbx process */
  91.     kill(0, SIGSTOP);        /* stop own process */
  92. }
  93.  
  94.  
  95. static void cont_handler()
  96. {
  97.     if (dbxpid) {
  98.     sleep(1);        /* we need this */
  99.     kill(dbxpid, SIGCONT);    /* continue dbx after stop */
  100.     }
  101. }
  102.  
  103.  
  104. /*
  105.  *  Trap signals to xdbx so that the child process can be handled properly.
  106.  */
  107. void trap_signals()
  108. {
  109.     signal(SIGINT,  kill_handler);
  110.     signal(SIGQUIT, kill_handler);
  111.     signal(SIGILL,  kill_handler);
  112.     signal(SIGBUS,  kill_handler);
  113.     signal(SIGSEGV, kill_handler);
  114.     signal(SIGTERM, kill_handler);
  115.  
  116.     signal(SIGTSTP, stop_handler);    /* stop signal from keyboard */
  117.     signal(SIGCONT, cont_handler);    /* continue after stop */
  118.     signal(SIGCHLD, quit_handler);    /* child status has changed */
  119. }
  120.