4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief SignalDispatcher non-inline non-template implementation */
26 #include "SignalEvent.hh"
27 #include "SignalEvent.ih"
30 #include <senf/Utils/senfassert.hh>
31 #include <senf/Utils/signalnames.hh>
32 #include "senf/Utils/IgnoreValue.hh"
34 //#include "SignalEvent.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 prefix_ senf::scheduler::detail::SignalDispatcher::SignalDispatcher()
41 if (pipe(sigPipe_) <0)
42 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
43 sigemptyset(&sigSet_);
44 detail::FdManager::instance().set(sigPipe_[0], detail::FdManager::EV_READ, this);
47 prefix_ senf::scheduler::detail::SignalDispatcher::~SignalDispatcher()
49 for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
50 ::signal(i->signal_, SIG_DFL);
51 detail::FIFORunner::instance().dequeue(&(*i));
53 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
54 detail::FdManager::instance().remove(sigPipe_[0]);
59 prefix_ void senf::scheduler::detail::SignalDispatcher::add(SignalEvent & event)
61 SignalSet::iterator i (handlers_.find(event));
62 if (i != handlers_.end())
63 throw DuplicateSignalRegistrationException()
64 << " for signal " << signalName(event.signal_) << " (" << event.signal_ << ")";
66 handlers_.insert(event);
67 sigaddset(&sigSet_, event.signal_);
68 detail::FIFORunner::instance().enqueue(&event);
72 sigaddset(&sig, event.signal_);
73 sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
75 // Need to re-register all handlers to update sa_mask
77 act.sa_sigaction = &sigHandler;
78 act.sa_mask = sigSet_;
79 act.sa_flags = SA_SIGINFO | SA_RESTART;
80 for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
81 if (i->signal_ == SIGCLD)
82 act.sa_flags |= SA_NOCLDSTOP;
84 act.sa_flags &= ~SA_NOCLDSTOP;
85 if (sigaction(i->signal_, &act, 0) < 0)
86 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
90 prefix_ void senf::scheduler::detail::SignalDispatcher::remove(SignalEvent & event)
92 ::signal(event.signal_, SIG_DFL);
93 detail::FIFORunner::instance().dequeue(&event);
94 handlers_.erase(event);
97 sigaddset(&sig, event.signal_);
98 sigprocmask(SIG_UNBLOCK, &sig, 0);
101 prefix_ void senf::scheduler::detail::SignalDispatcher::signal(int events)
104 if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
106 SignalSet::iterator i (handlers_.find(info.si_signo, FindNumericSignal()));
107 if (i == handlers_.end())
113 prefix_ void senf::scheduler::detail::SignalDispatcher::sigHandler(int signal,
114 ::siginfo_t * siginfo,
117 SENF_ASSERT( alive(), "Internal failure: Destroyed signal handler called" );
118 // The manpage says, si_signo is unused in linux so we set it here
119 siginfo->si_signo = signal;
120 // We can't do much on error anyway so we ignore errors here
121 senf::IGNORE( write(instance().sigPipe_[1], siginfo, sizeof(*siginfo)) );
124 prefix_ void senf::scheduler::SignalEvent::v_run()
129 prefix_ char const * senf::scheduler::SignalEvent::v_type()
135 prefix_ std::string senf::scheduler::SignalEvent::v_info()
141 ///////////////////////////////cc.e////////////////////////////////////////
143 //#include "SignalEvent.mpp"
149 // comment-column: 40
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"