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>
33 //#include "SignalEvent.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
37 prefix_ senf::scheduler::detail::SignalDispatcher::SignalDispatcher()
40 if (pipe(sigPipe_) <0)
41 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
42 sigemptyset(&sigSet_);
43 detail::FdManager::instance().set(sigPipe_[0], detail::FdManager::EV_READ, this);
46 prefix_ senf::scheduler::detail::SignalDispatcher::~SignalDispatcher()
48 for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
49 ::signal(i->signal_, SIG_DFL);
50 detail::FIFORunner::instance().dequeue(&(*i));
52 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
53 detail::FdManager::instance().remove(sigPipe_[0]);
58 prefix_ void senf::scheduler::detail::SignalDispatcher::add(SignalEvent & event)
60 SignalSet::iterator i (handlers_.find(event));
61 if (i != handlers_.end())
62 throw DuplicateSignalRegistrationException()
63 << " for signal " << signalName(event.signal_) << " (" << event.signal_ << ")";
65 handlers_.insert(event);
66 sigaddset(&sigSet_, event.signal_);
67 detail::FIFORunner::instance().enqueue(&event);
71 sigaddset(&sig, event.signal_);
72 sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
74 // Need to re-register all handlers to update sa_mask
76 act.sa_sigaction = &sigHandler;
77 act.sa_mask = sigSet_;
78 act.sa_flags = SA_SIGINFO | SA_RESTART;
79 for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
80 if (i->signal_ == SIGCLD)
81 act.sa_flags |= SA_NOCLDSTOP;
83 act.sa_flags &= ~SA_NOCLDSTOP;
84 if (sigaction(i->signal_, &act, 0) < 0)
85 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
89 prefix_ void senf::scheduler::detail::SignalDispatcher::remove(SignalEvent & event)
91 ::signal(event.signal_, SIG_DFL);
92 detail::FIFORunner::instance().dequeue(&event);
93 handlers_.erase(event);
96 sigaddset(&sig, event.signal_);
97 sigprocmask(SIG_UNBLOCK, &sig, 0);
100 prefix_ void senf::scheduler::detail::SignalDispatcher::signal(int events)
103 if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
105 SignalSet::iterator i (handlers_.find(info.si_signo, FindNumericSignal()));
106 if (i == handlers_.end())
112 prefix_ void senf::scheduler::detail::SignalDispatcher::sigHandler(int signal, ::siginfo_t * siginfo,
115 SENF_ASSERT( alive() );
116 // The manpage says, si_signo is unused in linux so we set it here
117 siginfo->si_signo = signal;
118 // We can't do much on error anyway so we ignore errors here
119 write(instance().sigPipe_[1], siginfo, sizeof(*siginfo));
122 prefix_ void senf::scheduler::SignalEvent::v_run()
127 prefix_ char const * senf::scheduler::SignalEvent::v_type()
133 prefix_ std::string senf::scheduler::SignalEvent::v_info()
139 ///////////////////////////////cc.e////////////////////////////////////////
141 //#include "SignalEvent.mpp"
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"