csignals.C

Go to the documentation of this file.
00001 /*!@file Util/csignals.C trivial wrapper for catching ANSI C signals */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/csignals.C $
00035 // $Id: csignals.C 7457 2006-11-20 17:43:13Z rjpeters $
00036 //
00037 
00038 #ifndef UTIL_CSIGNALS_C_DEFINED
00039 #define UTIL_CSIGNALS_C_DEFINED
00040 
00041 #include "Util/csignals.H"
00042 
00043 #include <cstdio>
00044 #include <signal.h>
00045 
00046 #include "Util/log.H"
00047 
00048 namespace
00049 {
00050   volatile int* signum_return = 0;
00051 
00052   void handlesignal(int s) throw()
00053   {
00054     if (MYLOGPREFIX != 0)
00055       fprintf(stderr, "[%s] ****** %s ******\n", MYLOGPREFIX, signame(s));
00056     else
00057       fprintf(stderr, "****** %s ******\n", signame(s));
00058 
00059     if (signum_return != 0)
00060       *signum_return = s;
00061   }
00062 }
00063 
00064 void catchsignals(volatile int* dest) throw()
00065 {
00066   // see comments in header file for why we only catch these
00067   // "non-fatal" signals and don't catch fatal signals such as
00068   // SIGQUIT, SIGABRT, SIGBUS, SIGFPE, SIGKILL, or SIGSEGV
00069 
00070   // before we install our signal handler, check first to see if the
00071   // signal already has a non-default handler, in which case we want
00072   // to leave that one as is -- e.g. if the user runs a program with
00073   // 'nohup', then SIGHUP will be set to SIG_IGN instead of SIG_DFL,
00074   // and in that case we want to leave it set to SIG_IGN rather than
00075   // overriding it to our signal handler which would defeat the
00076   // purpose of running with 'nohup' in the first place
00077 
00078 #ifndef HAVE_SIGHANDLER_T
00079   typedef void (*handler)(int);
00080 #else
00081   typedef sighandler_t handler;
00082 #endif
00083 
00084 #define INSTALL_SIG_HANDLER(s, h)               \
00085   do {                                          \
00086     handler old = signal(s, 0);                 \
00087     if (old == SIG_DFL) signal(s, h);           \
00088     else                signal(s, old);         \
00089   } while (0)
00090 
00091   INSTALL_SIG_HANDLER(SIGHUP,  &handlesignal);
00092   INSTALL_SIG_HANDLER(SIGINT,  &handlesignal);
00093   INSTALL_SIG_HANDLER(SIGTERM, &handlesignal);
00094   INSTALL_SIG_HANDLER(SIGALRM, &handlesignal);
00095   INSTALL_SIG_HANDLER(SIGPIPE, &handlesignal);
00096 
00097 #undef INSTALL_SIG_HANDLER
00098 
00099   signum_return = dest;
00100 }
00101 
00102 const char* signame(int num) throw()
00103 {
00104   switch (num)
00105     {
00106 #define CASE_SIGNAL_NAME(x) case x: return #x
00107 
00108       CASE_SIGNAL_NAME(SIGHUP); break;
00109       CASE_SIGNAL_NAME(SIGINT); break;
00110       CASE_SIGNAL_NAME(SIGQUIT); break;
00111       CASE_SIGNAL_NAME(SIGILL); break;
00112       CASE_SIGNAL_NAME(SIGTRAP); break;
00113       CASE_SIGNAL_NAME(SIGABRT); break;
00114       CASE_SIGNAL_NAME(SIGBUS); break;
00115       CASE_SIGNAL_NAME(SIGFPE); break;
00116       CASE_SIGNAL_NAME(SIGKILL); break;
00117       CASE_SIGNAL_NAME(SIGUSR1); break;
00118       CASE_SIGNAL_NAME(SIGSEGV); break;
00119       CASE_SIGNAL_NAME(SIGUSR2); break;
00120       CASE_SIGNAL_NAME(SIGPIPE); break;
00121       CASE_SIGNAL_NAME(SIGALRM); break;
00122       CASE_SIGNAL_NAME(SIGTERM); break;
00123       CASE_SIGNAL_NAME(SIGCHLD); break;
00124       CASE_SIGNAL_NAME(SIGCONT); break;
00125       CASE_SIGNAL_NAME(SIGSTOP); break;
00126       CASE_SIGNAL_NAME(SIGTSTP); break;
00127       CASE_SIGNAL_NAME(SIGTTIN); break;
00128       CASE_SIGNAL_NAME(SIGTTOU); break;
00129       CASE_SIGNAL_NAME(SIGURG); break;
00130       CASE_SIGNAL_NAME(SIGXCPU); break;
00131       CASE_SIGNAL_NAME(SIGXFSZ); break;
00132       CASE_SIGNAL_NAME(SIGVTALRM); break;
00133       CASE_SIGNAL_NAME(SIGPROF); break;
00134       CASE_SIGNAL_NAME(SIGWINCH); break;
00135       CASE_SIGNAL_NAME(SIGIO); break;
00136       CASE_SIGNAL_NAME(SIGSYS); break;
00137 
00138 #undef CASE_SIGNAL_NAME
00139     }
00140 
00141   return "unknown";
00142 }
00143 
00144 // ######################################################################
00145 /* So things look consistent in everyone's emacs... */
00146 /* Local Variables: */
00147 /* indent-tabs-mode: nil */
00148 /* End: */
00149 
00150 #endif // UTIL_CSIGNALS_C_DEFINED
Generated on Sun May 8 08:42:25 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3