Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Scheduler / SignalEvent.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief SignalDispatcher non-inline non-template implementation */
25
26 #include "SignalEvent.hh"
27 #include "SignalEvent.ih"
28
29 // Custom includes
30 #include <senf/Utils/senfassert.hh>
31 #include <senf/Utils/signalnames.hh>
32 #include "senf/Utils/IgnoreValue.hh"
33
34 //#include "SignalEvent.mpp"
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 prefix_ senf::scheduler::detail::SignalDispatcher::SignalDispatcher()
39     : blocked_ (true)
40 {
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);
45 }
46
47 prefix_ senf::scheduler::detail::SignalDispatcher::~SignalDispatcher()
48 {
49     for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
50         ::signal(i->signal_, SIG_DFL);
51         detail::FIFORunner::instance().dequeue(&(*i));
52     }
53     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
54     detail::FdManager::instance().remove(sigPipe_[0]);
55     close(sigPipe_[0]);
56     close(sigPipe_[1]);
57 }
58
59 prefix_ void senf::scheduler::detail::SignalDispatcher::add(SignalEvent & event)
60 {
61     SignalSet::iterator i (handlers_.find(event));
62     if (i != handlers_.end())
63         throw DuplicateSignalRegistrationException()
64             << " for signal " << signalName(event.signal_) << " (" << event.signal_ << ")";
65
66     handlers_.insert(event);
67     sigaddset(&sigSet_, event.signal_);
68     detail::FIFORunner::instance().enqueue(&event);
69
70     sigset_t sig;
71     sigemptyset(&sig);
72     sigaddset(&sig, event.signal_);
73     sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
74
75     // Need to re-register all handlers to update sa_mask
76     struct sigaction act;
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;
83         else
84             act.sa_flags &= ~SA_NOCLDSTOP;
85         if (sigaction(i->signal_, &act, 0) < 0)
86             SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
87     }
88 }
89
90 prefix_ void senf::scheduler::detail::SignalDispatcher::remove(SignalEvent & event)
91 {
92     ::signal(event.signal_, SIG_DFL);
93     detail::FIFORunner::instance().dequeue(&event);
94     handlers_.erase(event);
95     sigset_t sig;
96     sigemptyset(&sig);
97     sigaddset(&sig, event.signal_);
98     sigprocmask(SIG_UNBLOCK, &sig, 0);
99 }
100
101 prefix_ void senf::scheduler::detail::SignalDispatcher::signal(int events)
102 {
103     siginfo_t info;
104     if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
105         return;
106     SignalSet::iterator i (handlers_.find(info.si_signo, FindNumericSignal()));
107     if (i == handlers_.end())
108         return;
109     i->siginfo_ = info;
110     i->setRunnable();
111 }
112
113 prefix_ void senf::scheduler::detail::SignalDispatcher::sigHandler(int signal,
114                                                                    ::siginfo_t * siginfo,
115                                                                    void *)
116 {
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)) );
122 }
123
124 prefix_ void senf::scheduler::SignalEvent::v_run()
125 {
126     cb_(siginfo_);
127 }
128
129 prefix_ char const * senf::scheduler::SignalEvent::v_type()
130     const
131 {
132     return "si";
133 }
134
135 prefix_ std::string senf::scheduler::SignalEvent::v_info()
136     const
137 {
138     return "";
139 }
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143 //#include "SignalEvent.mpp"
144
145
146 // Local Variables:
147 // mode: c++
148 // fill-column: 100
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"
154 // End: