1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// socket++ library. sig.C
// Copyright (C) 1992-1996 Gnanasekaran Swaminathan <gs4t@virginia.edu>
//
// Permission is granted to use at your own risk and distribute this software
// in source and binary forms provided the above copyright notice and this
// paragraph are preserved on all copies. This software is provided "as is"
// with no express or implied warranty.
#include <signal.h>
#include <sig.h>
//explicit template instantiation.
typedef sig::phnd phnd;
typedef sig::phndlist phndlist;
//template class list<phnd>;
//template class map<int, phndlist, less<int> >;
//static sigerr se; //commended out by Herbert Straub
// Change all se to sigerr
siginit siginit::init;
sig& sig::nal = *siginit::init.s;
typedef void (*sighnd_type) (int);
extern "C" {
static void sighandler (int signo) {
sig::nal.kill (signo);
}
}
sig::hnd::hnd (int s)
: signo (s)
{
sig::nal.set (signo, this);
}
sig::hnd::~hnd ()
{
sig::nal.unset (signo, this);
}
bool sig::set (int signo, sig::hnd* hnd)
{
if (hnd == 0) return false;
phndlist& v = smap [signo];
if (v.empty ()) {
#ifndef WIN32
struct sigaction sa;
if (sigaction (signo, 0, &sa) == -1) throw sigerr();
if (sa.sa_handler != sighnd_type (&sighandler)) {
// setting for the first time
sa.sa_handler = (void(*)(int)) sighnd_type (&sighandler);
if (sigemptyset (&sa.sa_mask) == -1) throw sigerr();
sa.sa_flags = 0;
if (sigaction (signo, &sa, 0) == -1) throw sigerr();
}
#endif //windows does not define sigaction
//basically, this is a way to handle some kind of error that can't exist on windows.
//see http://svn.haxx.se/dev/archive-2004-01/0685.shtml
v.push_back (hnd);
return true;
}
phndlist::iterator j = find (v.begin(), v.end (), hnd);
if (j == v.end ()) {
v.push_back (hnd);
return true;
}
return false;
}
bool sig::unset (int signo, sig::hnd* hnd)
{
if (hnd == 0) return false;
phndlist& v = smap [signo];
phndlist::iterator j = find (v.begin(), v.end (), hnd);
if (j != v.end ()) {
v.erase (j);
return true;
}
return false;
}
void sig::unset (int signo)
{
phndlist& v = smap [signo];
v.erase (v.begin (), v.end ());
#ifndef WIN32
struct sigaction sa;
if (sigaction (signo, 0, &sa) == -1) throw sigerr();
if (sa.sa_handler == sighnd_type (&sighandler)) {
sa.sa_handler = (void(*)(int)) sighnd_type (SIG_DFL);
if (sigemptyset (&sa.sa_mask) == -1) throw sigerr();
sa.sa_flags = 0;
if (sigaction (signo, &sa, 0) == -1) throw sigerr();
}
#endif //windows does not define sigaction
}
void sig::mask (int signo) const
{
#ifndef WIN32
sigset_t s;
if (sigemptyset (&s) == -1) throw sigerr();
if (sigaddset (&s, signo) == -1) throw sigerr();
if (sigprocmask (SIG_BLOCK, &s, 0) == -1) throw sigerr();
#endif //windows does not define sigset_t
}
void sig::unmask (int signo) const
{
#ifndef WIN32
sigset_t s;
if (sigemptyset (&s) == -1) throw sigerr();
if (sigaddset (&s, signo) == -1) throw sigerr();
if (sigprocmask (SIG_UNBLOCK, &s, 0) == -1) throw sigerr();
#endif //windows does not define sigset_t
}
void sig::mask (int siga, int sigb) const
{
#ifndef WIN32
struct sigaction sa;
if (sigaction (siga, 0, &sa) == -1) throw sigerr();
if (sa.sa_handler != sighnd_type (&sighandler)) {
sa.sa_handler = (void(*)(int)) sighnd_type (&sighandler);
if (sigemptyset (&sa.sa_mask) == -1) throw sigerr();
sa.sa_flags = 0;
}
if (sigaddset (&sa.sa_mask, sigb) == -1) throw sigerr();
if (sigaction (siga, &sa, 0) == -1) throw sigerr();
#endif //windows does not define sigaction
}
void sig::unmask (int siga, int sigb) const
{
#ifndef WIN32
struct sigaction sa;
if (sigaction (siga, 0, &sa) == -1) throw sigerr();
if (sa.sa_handler != sighnd_type (&sighandler)) {
sa.sa_handler = (void(*)(int)) sighnd_type (&sighandler);
if (sigemptyset (&sa.sa_mask) == -1) throw sigerr();
sa.sa_flags = 0;
} else {
if (sigdelset (&sa.sa_mask, sigb) == -1) throw sigerr();
}
if (sigaction (siga, &sa, 0) == -1) throw sigerr();
#endif //windows does not define sigaction
}
void sig::sysresume (int signo, bool set) const
{
#ifndef WIN32
struct sigaction sa;
if (sigaction (signo, 0, &sa) == -1) throw sigerr();
if (sa.sa_handler != sighnd_type (&sighandler)) {
sa.sa_handler = (void(*)(int)) sighnd_type (&sighandler);
if (sigemptyset (&sa.sa_mask) == -1) throw sigerr();
sa.sa_flags = 0;
}
#if !(defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__sun__) || defined(__linux__) || defined(__APPLE__))
// Early SunOS versions may have SA_INTERRUPT. I can't confirm.
if (set == false)
sa.sa_flags |= SA_INTERRUPT;
else
sa.sa_flags &= ~SA_INTERRUPT;
if (sigaction (signo, &sa, 0) == -1) throw sigerr();
#endif
#endif //windows does not define sigaction
}
struct procsig {
int signo;
procsig (int s): signo (s) {}
void operator () (phnd& ph) { (*ph) (signo); }
};
void sig::kill (int signo)
{
phndlist& v = smap [signo];
// struct procsig used to be here // LN
for_each (v.begin (), v.end (), procsig (signo));
}
sigset_t sig::pending () const
{
#ifndef WIN32
sigset_t s;
if (sigemptyset (&s) == -1) throw sigerr();
if (sigpending (&s) == -1) throw sigerr();
return s;
#else
return true;//is this the right behavior for windows?
#endif //windows does not define sigset_t
}
bool sig::ispending (int signo) const
{
#ifndef WIN32
sigset_t s = pending ();
switch (sigismember (&s, signo)) {
case 0: return false;
case 1: return true;
}
throw sigerr();
#else
return true;//is this the right behavior for windows?
#endif //windows does not define sigset_t
}
|