"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/libburn/libdax_sigmgr.c" (30 Jan 2021, 4359 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "libdax_sigmgr.c" see the Fossies "Dox" file reference documentation.

    1 
    2 /* libdax_sigmgr
    3    Signal management facility of libdax and libburn.
    4    Copyright (C) 2006 Thomas Schmitt <scdbackup@gmx.net>, provided under GPLv2+
    5 */
    6 
    7 #ifdef HAVE_CONFIG_H
    8 #include "../config.h"
    9 #endif
   10 
   11 #include <stdio.h>
   12 #include <sys/types.h>
   13 #include <unistd.h>
   14 #include <string.h>
   15 #include <stdlib.h>
   16 #include <errno.h>
   17 #include <sys/time.h>
   18 
   19 #include <signal.h>
   20 typedef void (*sighandler_t)(int);
   21 
   22 
   23 /* Only this single source module is entitled to do this */ 
   24 #define LIBDAX_SIGMGR_H_INTERNAL 1
   25 
   26 /* All participants in the abort system must do this */
   27 #include "libdax_sigmgr.h"
   28 
   29 
   30 /* Signals to be caught */
   31 static int signal_list[]= {
   32  SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT,
   33  SIGFPE, SIGSEGV, SIGPIPE, SIGALRM, SIGTERM,
   34  SIGUSR1, SIGUSR2, SIGXCPU, SIGTSTP, SIGTTIN,
   35  SIGTTOU,
   36  SIGBUS, SIGPOLL, SIGPROF, SIGSYS, SIGTRAP,
   37  SIGVTALRM, SIGXCPU, SIGXFSZ, -1
   38 };
   39 static char *signal_name_list[]= {
   40  "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGABRT",
   41  "SIGFPE", "SIGSEGV", "SIGPIPE", "SIGALRM", "SIGTERM",
   42  "SIGUSR1", "SIGUSR2", "SIGXCPU", "SIGTSTP", "SIGTTIN",
   43  "SIGTTOU",
   44  "SIGBUS", "SIGPOLL", "SIGPROF", "SIGSYS", "SIGTRAP",
   45  "SIGVTALRM", "SIGXCPU", "SIGXFSZ", "@"
   46 };
   47 static int signal_list_count= 24;
   48 
   49 /* Signals not to be caught */
   50 static int non_signal_list[]= {
   51   SIGKILL, SIGCHLD, SIGSTOP, SIGURG, SIGWINCH, -1
   52 };
   53 static int non_signal_list_count= 5;
   54 
   55 
   56 /* Points to the globally activated signal handling manager */
   57 struct libdax_sigmgr *libdax_sigmgr_activated= NULL;
   58 
   59 
   60 /* ------------------------------ libdax_sigmgr ---------------------------- */
   61 
   62 
   63 int libdax_sigmgr_new(struct libdax_sigmgr **m, pid_t control_pid, int flag)
   64 {
   65  struct libdax_sigmgr *o;
   66 
   67  (*m)= o= (struct libdax_sigmgr *) malloc(sizeof(struct libdax_sigmgr));
   68  if(o==NULL)
   69    return(-1);
   70  o->control_pid= control_pid;
   71  o->api_chain= NULL;
   72  o->msg[0]= 0;
   73  o->exiting= 0;
   74  return(1);
   75 }
   76 
   77 
   78 int libdax_sigmgr_destroy(struct libdax_sigmgr **m, int flag)
   79 {
   80  struct libdax_sigmgr *o;
   81 
   82  o= *m;
   83  if(o==NULL)
   84    return(0);
   85  free((char *) o);
   86  *m= NULL;
   87  return(1);
   88 }
   89 
   90 
   91 static void libdax_sigmgr_central_handler(int signum)
   92 {
   93  int i, ret;
   94  struct libdax_sigmgr *o;
   95 
   96  o= libdax_sigmgr_activated;
   97  if(o==NULL)
   98    return;
   99  if(o->control_pid != getpid())
  100    return;
  101  if(o->exiting) {
  102 /*
  103    fprintf(stderr,"libdax_sigmgr: ABORT : repeat by pid=%d, signum=%d\n",
  104            getpid(),signum);
  105 */
  106    return;
  107  }
  108  o->exiting= 1;
  109  sprintf(o->msg,"UNIX-SIGNAL caught:  %d  errno= %d",signum,errno);
  110  for(i= 0; i<signal_list_count; i++)
  111    if(signum==signal_list[i]) {
  112      sprintf(o->msg,"UNIX-SIGNAL:  %s  errno= %d",
  113              signal_name_list[i],errno);
  114  break;
  115    }
  116 /*
  117  fprintf(stderr,"libdax_sigmgr: ABORT : %s\n", o->msg);
  118 */
  119  alarm(0);
  120  ret= libdax_api_handle_abort(o->api_chain, o->msg, 0);
  121  if(ret == -2)
  122    return;
  123  if(ret<0)
  124    ret= 1;
  125  exit(ret);
  126 }
  127 
  128 
  129 /* Set global signal handling.
  130    @param o The signal manager to respond to signals in mode 0
  131    @param api_chain One of the libdax_api objects in the chain to handle
  132    @param flag Bitfield for control purposes (unused yet, submit 0)
  133                bit0-2= activation mode
  134                        0 set to use of item handlers
  135                        1 set to default handlers
  136                        2 set to ignore
  137                bit3= set SIGABRT to handler (makes sense with bits 0 or 1)
  138 */
  139 int libdax_sigmgr_activate(struct libdax_sigmgr *o, 
  140                            struct libdax_api *api_chain,
  141                            int flag)
  142 {
  143  int mode, i, j, max_sig= -1, min_sig= 0x7fffffff;
  144  sighandler_t sig_handler;
  145 
  146  mode= flag&7;
  147  o->api_chain= api_chain;
  148  libdax_sigmgr_activated= o;
  149  if(mode==0)
  150    sig_handler= libdax_sigmgr_central_handler;
  151  else if(mode==1)
  152    sig_handler= SIG_DFL;
  153  else if(mode==2)
  154    sig_handler= SIG_IGN;
  155  else
  156    return(-1);
  157  /* set all signal numbers between the lowest and highest in the list
  158     except those in the non-signal list */
  159  for(i= 0; i<signal_list_count; i++) {
  160    if(signal_list[i]>max_sig)
  161      max_sig= signal_list[i];
  162    if(signal_list[i]<min_sig)
  163      min_sig= signal_list[i];
  164  }
  165  for(i= min_sig; i<=max_sig; i++) {
  166    for(j= 0; j<non_signal_list_count; j++)
  167      if(i==non_signal_list[j])
  168    break;
  169    if(j>=non_signal_list_count) {
  170      if(i==SIGABRT && (flag&8))
  171        signal(i,libdax_sigmgr_central_handler);
  172      else
  173        signal(i,sig_handler);
  174    }
  175  }
  176  return(1);
  177 }
  178 
  179