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 "SignalDispatcher.hh"
27 //#include "SignalDispatcher.ih"
30 #include "../Utils/senfassert.hh"
32 //#include "SignalDispatcher.mpp"
34 ///////////////////////////////cc.p////////////////////////////////////////
36 senf::scheduler::SignalDispatcher * senf::scheduler::SignalDispatcher::instance_ = 0;
38 prefix_ senf::scheduler::SignalDispatcher::SignalDispatcher(FdManager & manager,
40 : manager_ (manager), runner_ (runner), blocked_ (true)
42 SENF_ASSERT( !instance_ );
43 if (pipe(sigPipe_) <0)
44 SENF_THROW_SYSTEM_EXCEPTION("pipe()");
45 sigemptyset(&sigSet_);
47 manager_.set(sigPipe_[0], FdManager::EV_READ, this);
50 prefix_ senf::scheduler::SignalDispatcher::~SignalDispatcher()
52 for (HandlerMap::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
53 ::signal(i->first, SIG_DFL);
54 runner_.dequeue(&i->second);
56 sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
57 manager_.remove(sigPipe_[0]);
63 prefix_ void senf::scheduler::SignalDispatcher::add(int signal, Callback const & cb)
65 HandlerMap::iterator i (handlers_.find(signal));
66 if (i != handlers_.end()) {
71 i = handlers_.insert(std::make_pair(signal, SignalEvent(signal, cb))).first;
72 sigaddset(&sigSet_, signal);
73 runner_.enqueue(&i->second);
77 sigaddset(&sig, signal);
78 sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
80 // Need to re-register all handlers to update sa_mask
81 for (HandlerMap::iterator j (handlers_.begin()); i != handlers_.end(); ++i) {
83 act.sa_sigaction = &sigHandler;
84 act.sa_mask = sigSet_;
85 act.sa_flags = SA_SIGINFO | SA_RESTART;
86 if (j->first == SIGCLD)
87 act.sa_flags |= SA_NOCLDSTOP;
88 if (sigaction(j->first, &act, 0) < 0)
89 SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
93 prefix_ void senf::scheduler::SignalDispatcher::remove(int signal)
95 ::signal(signal, SIG_DFL);
98 sigaddset(&sig, signal);
99 sigprocmask(SIG_UNBLOCK, &sig, 0);
102 prefix_ void senf::scheduler::SignalDispatcher::signal(int events)
105 if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
107 HandlerMap::iterator i (handlers_.find(info.si_signo));
108 if (i == handlers_.end())
110 i->second.siginfo = info;
111 i->second.runnable = true;
114 prefix_ void senf::scheduler::SignalDispatcher::sigHandler(int signal, ::siginfo_t * siginfo,
117 SENF_ASSERT( instance_ );
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 write(instance_->sigPipe_[1], siginfo, sizeof(*siginfo));
124 ///////////////////////////////cc.e////////////////////////////////////////
126 //#include "SignalDispatcher.mpp"
132 // comment-column: 40
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"