Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

SignalEvent.cc

Go to the documentation of this file.
00001 // $Id: SignalEvent.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "SignalEvent.hh"
00027 #include "SignalEvent.ih"
00028 
00029 // Custom includes
00030 #include <senf/Utils/senfassert.hh>
00031 #include <senf/Utils/signalnames.hh>
00032 #include "senf/Utils/IgnoreValue.hh"
00033 
00034 //#include "SignalEvent.mpp"
00035 #define prefix_
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 
00038 prefix_ senf::scheduler::detail::SignalDispatcher::SignalDispatcher()
00039     : blocked_ (true)
00040 {
00041     if (pipe(sigPipe_) <0)
00042         SENF_THROW_SYSTEM_EXCEPTION("pipe()");
00043     sigemptyset(&sigSet_);
00044     detail::FdManager::instance().set(sigPipe_[0], detail::FdManager::EV_READ, this);
00045 }
00046 
00047 prefix_ senf::scheduler::detail::SignalDispatcher::~SignalDispatcher()
00048 {
00049     for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
00050         ::signal(i->signal_, SIG_DFL);
00051         detail::FIFORunner::instance().dequeue(&(*i));
00052     }
00053     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
00054     detail::FdManager::instance().remove(sigPipe_[0]);
00055     close(sigPipe_[0]);
00056     close(sigPipe_[1]);
00057 }
00058 
00059 prefix_ void senf::scheduler::detail::SignalDispatcher::add(SignalEvent & event)
00060 {
00061     SignalSet::iterator i (handlers_.find(event));
00062     if (i != handlers_.end())
00063         throw DuplicateSignalRegistrationException()
00064             << " for signal " << signalName(event.signal_) << " (" << event.signal_ << ")";
00065 
00066     handlers_.insert(event);
00067     sigaddset(&sigSet_, event.signal_);
00068     detail::FIFORunner::instance().enqueue(&event);
00069 
00070     sigset_t sig;
00071     sigemptyset(&sig);
00072     sigaddset(&sig, event.signal_);
00073     sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
00074 
00075     // Need to re-register all handlers to update sa_mask
00076     struct sigaction act;
00077     act.sa_sigaction = &sigHandler;
00078     act.sa_mask = sigSet_;
00079     act.sa_flags = SA_SIGINFO | SA_RESTART;
00080     for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
00081         if (i->signal_ == SIGCLD)
00082             act.sa_flags |= SA_NOCLDSTOP;
00083         else
00084             act.sa_flags &= ~SA_NOCLDSTOP;
00085         if (sigaction(i->signal_, &act, 0) < 0)
00086             SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
00087     }
00088 }
00089 
00090 prefix_ void senf::scheduler::detail::SignalDispatcher::remove(SignalEvent & event)
00091 {
00092     ::signal(event.signal_, SIG_DFL);
00093     detail::FIFORunner::instance().dequeue(&event);
00094     handlers_.erase(event);
00095     sigset_t sig;
00096     sigemptyset(&sig);
00097     sigaddset(&sig, event.signal_);
00098     sigprocmask(SIG_UNBLOCK, &sig, 0);
00099 }
00100 
00101 prefix_ void senf::scheduler::detail::SignalDispatcher::signal(int events)
00102 {
00103     siginfo_t info;
00104     if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
00105         return;
00106     SignalSet::iterator i (handlers_.find(info.si_signo, FindNumericSignal()));
00107     if (i == handlers_.end())
00108         return;
00109     i->siginfo_ = info;
00110     i->setRunnable();
00111 }
00112 
00113 prefix_ void senf::scheduler::detail::SignalDispatcher::sigHandler(int signal,
00114                                                                    ::siginfo_t * siginfo,
00115                                                                    void *)
00116 {
00117     SENF_ASSERT( alive(), "Internal failure: Destroyed signal handler called" );
00118     // The manpage says, si_signo is unused in linux so we set it here
00119     siginfo->si_signo = signal;
00120     // We can't do much on error anyway so we ignore errors here
00121     senf::IGNORE( write(instance().sigPipe_[1], siginfo, sizeof(*siginfo)) );
00122 }
00123 
00124 prefix_ void senf::scheduler::SignalEvent::v_run()
00125 {
00126     cb_(siginfo_);
00127 }
00128 
00129 prefix_ char const * senf::scheduler::SignalEvent::v_type()
00130     const
00131 {
00132     return "si";
00133 }
00134 
00135 prefix_ std::string senf::scheduler::SignalEvent::v_info()
00136     const
00137 {
00138     return "";
00139 }
00140 
00141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00142 #undef prefix_
00143 //#include "SignalEvent.mpp"
00144 
00145 
00146 // Local Variables:
00147 // mode: c++
00148 // fill-column: 100
00149 // comment-column: 40
00150 // c-file-style: "senf"
00151 // indent-tabs-mode: nil
00152 // ispell-local-dictionary: "american"
00153 // compile-command: "scons -u test"
00154 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research