490c2c2715dcb648ff48e43190314d37db76d420
[senf.git] / Scheduler / SignalDispatcher.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 "SignalDispatcher.hh"
27 //#include "SignalDispatcher.ih"
28
29 // Custom includes
30 #include "../Utils/senfassert.hh"
31
32 //#include "SignalDispatcher.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 senf::scheduler::SignalDispatcher * senf::scheduler::SignalDispatcher::instance_ = 0;
37
38 prefix_ senf::scheduler::SignalDispatcher::SignalDispatcher(FdManager & manager,
39                                                             FIFORunner & runner)
40     : manager_ (manager), runner_ (runner), blocked_ (true)
41 {
42     SENF_ASSERT( !instance_ );
43     if (pipe(sigPipe_) <0)
44         SENF_THROW_SYSTEM_EXCEPTION("pipe()");
45     sigemptyset(&sigSet_);
46     instance_ = this;
47     manager_.set(sigPipe_[0], FdManager::EV_READ, this);
48 }
49
50 prefix_ senf::scheduler::SignalDispatcher::~SignalDispatcher()
51 {
52     for (HandlerMap::iterator i (handlers_.begin()); i != handlers_.end(); ++i) {
53         ::signal(i->first, SIG_DFL);
54         runner_.dequeue(&i->second);
55     }
56     sigprocmask(SIG_UNBLOCK, &sigSet_, 0);
57     manager_.remove(sigPipe_[0]);
58     close(sigPipe_[0]);
59     close(sigPipe_[1]);
60     instance_ = 0;
61 }
62
63 prefix_ void senf::scheduler::SignalDispatcher::add(int signal, Callback const & cb)
64 {
65     HandlerMap::iterator i (handlers_.find(signal));
66     if (i != handlers_.end()) {
67         i->second.cb = cb;
68         return;
69     }
70
71     i = handlers_.insert(std::make_pair(signal, SignalEvent(cb))).first;
72     sigaddset(&sigSet_, signal);
73     runner_.enqueue(&i->second);
74
75     sigset_t sig;
76     sigemptyset(&sig);
77     sigaddset(&sig, signal);
78     sigprocmask(blocked_ ? SIG_BLOCK : SIG_UNBLOCK, &sig, 0);
79
80     // Need to re-register all handlers to update sa_mask
81     for (HandlerMap::iterator j (handlers_.begin()); i != handlers_.end(); ++i) {
82         struct sigaction act;
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()");
90     }
91 }
92
93 prefix_ void senf::scheduler::SignalDispatcher::remove(int signal)
94 {
95     ::signal(signal, SIG_DFL);
96     sigset_t sig;
97     sigemptyset(&sig);
98     sigaddset(&sig, signal);
99     sigprocmask(SIG_UNBLOCK, &sig, 0);
100 }
101
102 prefix_ void senf::scheduler::SignalDispatcher::signal(int events)
103 {
104     siginfo_t info;
105     if (read(sigPipe_[0], &info, sizeof(info)) < int(sizeof(info)))
106         return;
107     HandlerMap::iterator i (handlers_.find(info.si_signo));
108     if (i == handlers_.end())
109         return;
110     i->second.siginfo = info;
111     i->second.runnable = true;
112 }
113
114 prefix_ void senf::scheduler::SignalDispatcher::sigHandler(int signal, ::siginfo_t * siginfo,
115                                                            void *)
116 {
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));
122 }
123
124 ///////////////////////////////cc.e////////////////////////////////////////
125 #undef prefix_
126 //#include "SignalDispatcher.mpp"
127
128 \f
129 // Local Variables:
130 // mode: c++
131 // fill-column: 100
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"
137 // End: