![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/simulator/SignalHandler.hpp> 00029 00030 #include <iostream> 00031 00032 using namespace wns::simulator; 00033 00034 SignalHandler::SignalHandler() : 00035 map_() 00036 { 00037 } 00038 00039 SignalHandler::~SignalHandler() 00040 { 00041 removeAllSignalHandlers(); 00042 } 00043 00044 void 00045 SignalHandler::removeSignalHandler(int signum) 00046 { 00047 // block all signals until we have removed the handler 00048 sigset_t allSignals; 00049 sigfillset(&allSignals); 00050 sigprocmask(SIG_BLOCK, &allSignals, NULL); 00051 if (!map_.knows(signum)) 00052 { 00053 // should never happen, otherwise the implementation is broken 00054 std::cerr << "openWNS: Tried to removed signal handler for signal " << signum <<"!!!\n"; 00055 std::cerr << " But no handler was registered."; 00056 } 00057 else 00058 { 00059 map_.erase(signum); 00060 // restore default signal handler 00061 struct sigaction action; 00062 sigfillset (&action.sa_mask); 00063 action.sa_flags = 0; 00064 action.sa_handler = SIG_DFL; 00065 sigaction(signum, &action, NULL); 00066 } 00067 sigprocmask(SIG_UNBLOCK, &allSignals, NULL); 00068 } 00069 00070 void 00071 SignalHandler::removeAllSignalHandlers() 00072 { 00073 // block all signals until we have removed all handlers 00074 sigset_t allSignals; 00075 sigfillset(&allSignals); 00076 sigprocmask(SIG_BLOCK, &allSignals, NULL); 00077 while(!map_.empty()) 00078 { 00079 Map::const_iterator itr = map_.begin(); 00080 // restore default signal handler 00081 struct sigaction action; 00082 sigfillset (&action.sa_mask); 00083 action.sa_flags = 0; 00084 action.sa_handler = SIG_DFL; 00085 sigaction(itr->first, &action, NULL); 00086 map_.erase(itr->first); 00087 } 00088 sigprocmask(SIG_UNBLOCK, &allSignals, NULL); 00089 } 00090 00091 void 00092 SignalHandler::catchSignal(int signum) 00093 { 00094 std::cerr << "\nopenWNS: caught signal " << signum << "\n"; 00095 // Get access to the global instance 00096 wns::simulator::SignalHandler& signalHandler = wns::simulator::GlobalSignalHandler::Instance(); 00097 if (!signalHandler.map_.knows(signum)) 00098 { 00099 // should never happen, otherwise the implementation is broken 00100 std::cerr << "openWNS: no signal handler defined!!!\n"; 00101 return; 00102 } 00103 00104 Handler* localHandler = signalHandler.map_.find(signum); 00105 00106 if (localHandler->num_slots() == 0) 00107 { 00108 std::cerr << "openWNS: no signal handler to call!\n"; 00109 return; 00110 } 00111 if (localHandler->num_slots() > 1) 00112 { 00113 std::cerr << "openWNS: more than one signal handler to call! Not calling any signal handler!\n"; 00114 return; 00115 } 00116 00117 // call the signal handler 00118 (*localHandler)(); 00119 } 00120
1.5.5