interrupts.cc 2.63 KB
Newer Older
dauncey's avatar
dauncey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#ifndef interrupts_CC
#define interrupts_CC

#include <signal.h>
#include <unistd.h>

#include <string>

bool continueJob=true;
std::vector<std::string> sSignal;

void signalHandler(int signal) {
  std::string theSignal("Unknown");
  if(signal<(int)sSignal.size()) theSignal=sSignal[signal];

  std::cout << "Process " << getpid() << " received signal "
            << signal << " = " << theSignal << std::endl;
  std::cerr << "Process " << getpid() << " received signal "
            << signal << " = " << theSignal << std::endl;

  continueJob=false;
}

void defineSignals() {
  if(sSignal.size()==0) {
    sSignal.push_back("Unknown");
    sSignal.push_back("SIGHUP");
    sSignal.push_back("SIGINT");
    sSignal.push_back("SIGQUIT");
    sSignal.push_back("SIGILL");
    sSignal.push_back("SIGTRAP");
    sSignal.push_back("SIGABRT");
    sSignal.push_back("SIGBUS");
    sSignal.push_back("SIGFPE");
    sSignal.push_back("SIGKILL");
    sSignal.push_back("SIGUSR1");
    sSignal.push_back("SIGSEGV");
    sSignal.push_back("SIGUSR2");
    sSignal.push_back("SIGPIPE");
    sSignal.push_back("SIGALRM");
    sSignal.push_back("SIGTERM");
    sSignal.push_back("SIGSTKFLT");
    sSignal.push_back("SIGCHLD");
    sSignal.push_back("SIGCONT");
    sSignal.push_back("SIGSTOP");
    sSignal.push_back("SIGTSTP");
    sSignal.push_back("SIGTTIN");
    sSignal.push_back("SIGTTOU");
    sSignal.push_back("SIGURG");
    sSignal.push_back("SIGXCPU");
    sSignal.push_back("SIGXFSZ");
    sSignal.push_back("SIGVTALRM");
    sSignal.push_back("SIGPROF");
    sSignal.push_back("SIGWINCH");
    sSignal.push_back("SIGPOLL");
    sSignal.push_back("SIGPWR");
    sSignal.push_back("SIGSYS");
    sSignal.push_back("SIGRTMIN");
  }
}

bool catchSignal(std::string sig) {
  defineSignals();

  for(unsigned s(0);s<sSignal.size();s++) {
    if(sSignal[s]==sig) {
      std::cout << "Setting catch for signal = " << s << " = " << sig << std::endl;
      signal(s,signalHandler);
      return true;
    }
  }

  return false;
}

void catchSignals() {
  /*
  std::vector<std::string> vSig;
  vSig.push_back("SIGINT");
  vSig.push_back("SIGUSR1");
  vSig.push_back("SIGUSR2");
  vSig.push_back("SIGTERM");
  vSig.push_back("SIGXCPU");
  */
  
  defineSignals();

  //BACKTRACE_AND_ASSERT(catchSignal("SIGINT"));
  
  // Catch signals, mainly for batch
  for(unsigned s(0);s<sSignal.size();s++) {
    if(sSignal[s]=="SIGINT"  ||
       sSignal[s]=="SIGUSR1" ||
       sSignal[s]=="SIGUSR2" ||
       sSignal[s]=="SIGTERM" ||
       sSignal[s]=="SIGXCPU") {
      std::cout << "Setting catch for signal = " << s
		<< " = " << sSignal[s] << std::endl;
      signal(s,signalHandler);
    }
  }
}

#endif