a521bd8c61ea0ce9ea855f3abe3114c1bd6f9402
[senf.git] / 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 "../Utils/senfassert.hh"
31 #include "../Utils/signalnames.hh"
32
33 //#include "SignalEvent.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 prefix_ senf::scheduler::detail::SignalDispatcher::SignalDispatcher()
38     : blocked_ (true)
39 {
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);
44 }
45
46 prefix_ senf::scheduler::detail::SignalDispatcher::~SignalDispatcher()
47 {
48     for (SignalSet::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
49         ::signal(i->signal_, SIG_DFL);
50         detail::FIFORunner::instance().dequeue(&(*i));
51     }
52     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
53     detail::FdManager::instance().remove(sigPipe_[0]);
54     close(sigPipe_[0]);
55     close(sigPipe_[1]);
56 }
57
58 prefix_ void senf::scheduler::detail::SignalDispatcher::add(SignalEvent & event)
59 {
60     SignalSet::iterator i (handlers_.find(event));
61     if (i != handlers_.end())
62         throw DuplicateSignalRegistrationException() 
63             << " for signal " << signalName(event.signal_) << " (" << event.signal_ << ")";
64
65     handlers_.insert(event);
66     sigaddset(&sigSet_, event.signal_);
67     detail::FIFORunner::instance().enqueue(&event);
68
69     sigset_t sig;
70     sigemptyset(&sig);
71     sigaddset(&sig, event.signal_);
72     sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
73
74     // Need to re-register all handlers to update sa_mask
75     struct sigaction act;
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;
82         else
83             act.sa_flags &= ~SA_NOCLDSTOP;
84         if (sigaction(i->signal_, &act, 0) < 0)
85             SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
86     }
87 }
88
89 prefix_ void senf::scheduler::detail::SignalDispatcher::remove(SignalEvent & event)
90 {
91     ::signal(event.signal_, SIG_DFL);
92     detail::FIFORunner::instance().dequeue(&event);
93     handlers_.erase(event);
94     sigset_t sig;
95     sigemptyset(&sig);
96     sigaddset(&sig, event.signal_);
97     sigprocmask(SIG_UNBLOCK, &sig, 0);
98 }
99
100 prefix_ void senf::scheduler::detail::SignalDispatcher::signal(int events)
101 {
102     siginfo_t info;
103     if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
104         return;
105     SignalSet::iterator i (handlers_.find(info.si_signo, FindNumericSignal()));
106     if (i == handlers_.end())
107         return;
108     i->siginfo_ = info;
109     i->setRunnable();
110 }
111
112 prefix_ void senf::scheduler::detail::SignalDispatcher::sigHandler(int signal, ::siginfo_t * siginfo,
113                                                            void *)
114 {
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));
120 }
121
122 prefix_ void senf::scheduler::SignalEvent::v_run()
123 {
124     cb_(siginfo_);
125 }
126
127 prefix_ char const * senf::scheduler::SignalEvent::v_type()
128     const
129 {
130     return "si";
131 }
132
133 prefix_ std::string senf::scheduler::SignalEvent::v_info()
134     const
135 {
136     return "";
137 }
138
139 ///////////////////////////////cc.e////////////////////////////////////////
140 #undef prefix_
141 //#include "SignalEvent.mpp"
142
143 \f
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
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"
152 // End: