[go: up one dir, main page]

Menu

[ab7a0e]: / libgnuworld / Signal.cc  Maximize  Restore  History

Download this file

296 lines (244 with data), 6.5 kB

  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Signal.cc
* Author: Daniel Karrels dan@karrels.com
* Copyright (C) 2003 Daniel Karrels <dan@karrels.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id: Signal.cc,v 1.11 2007/09/13 02:00:45 dan_karrels Exp $
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <queue>
#include <iostream>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "Signal.h"
#include "ELog.h"
const char rcsId[] = "$Id: Signal.cc,v 1.11 2007/09/13 02:00:45 dan_karrels Exp $" ;
namespace gnuworld
{
using std::queue ;
using std::cout ;
using std::endl ;
// This will need to be changed to work properly in some code.
//Signal sig ;
bool Signal::signalError = false ;
int Signal::readFD = -1 ;
int Signal::writeFD = -1 ;
Signal* Signal::theInstance = 0 ;
pthread_mutex_t Signal::singletonMutex = PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_t Signal::pipeMutex = PTHREAD_MUTEX_INITIALIZER ;
Signal::Signal()
{
// No need to initialize signalError here since it is only initialized
// directly above, and because this class models the Singleton
// pattern.
if( !openPipes() )
{
elog << "Signal> Failed to open FIFO pipes"
<< endl ;
// Failure to initialize pipes on startup is a critical
// failure.
::exit( 0 ) ;
}
// Catch some signals
#ifdef SIGINT
::signal( SIGINT, AddSignal ) ;
#endif
#ifdef SIGHUP
::signal( SIGHUP, AddSignal ) ;
#endif
#ifdef SIGPIPE
::signal( SIGPIPE, AddSignal ) ;
#endif
#ifdef SIGTERM
::signal( SIGTERM, AddSignal ) ;
#endif
#ifdef SIGPOLL
::signal( SIGPOLL, AddSignal ) ;
#endif
#ifdef SIGUSR1
::signal( SIGUSR1, AddSignal ) ;
#endif
#ifdef SIGUSR2
::signal( SIGUSR2, AddSignal ) ;
#endif
}
Signal::~Signal()
{
delete theInstance ; theInstance = 0 ;
closePipes() ;
::pthread_mutex_destroy( &singletonMutex ) ;
::pthread_mutex_destroy( &pipeMutex ) ;
}
bool Signal::isError()
{
return ((-1 == readFD) || (-1 == writeFD) || signalError) ;
}
void Signal::closePipes()
{
::pthread_mutex_lock( &pipeMutex ) ;
::close( readFD ) ;
::close( writeFD ) ;
::pthread_mutex_unlock( &pipeMutex ) ;
signalError = true ;
}
bool Signal::openPipes()
{
int rwFD[ 2 ] = { 0, 0 } ;
// Create the pipe
::pthread_mutex_lock( &pipeMutex ) ;
int pipeRet = ::pipe( rwFD ) ;
::pthread_mutex_unlock( &pipeMutex ) ;
if( pipeRet < 0 )
{
elog << "Signal::openPipes> pipe() failed: "
<< strerror( errno )
<< endl ;
signalError = true ;
return false ;
}
// Set the fd's to non-blocking
::pthread_mutex_lock( &pipeMutex ) ;
for( size_t i = 0 ; i < 2 ; ++i )
{
// Get current flags
int flags = ::fcntl( rwFD[ i ], F_GETFL ) ;
if( flags < 0 )
{
::pthread_mutex_unlock( &pipeMutex ) ;
elog << "Signal::openPipes> Failed to get flags "
<< "for pipe fd: "
<< strerror( errno )
<< endl ;
closePipes() ;
return false ;
}
// Update flags to indicate non-blocking
flags |= O_NONBLOCK ;
// Set new flags
if( ::fcntl( rwFD[ i ], F_SETFL, flags ) < 0 )
{
::pthread_mutex_unlock( &pipeMutex ) ;
elog << "Signal::openPipes> Failed to set flags "
<< "on pipe fd: "
<< strerror( errno )
<< endl ;
closePipes() ;
return false ;
}
} // for()
// All is well
readFD = rwFD[ 0 ] ;
writeFD = rwFD[ 1 ] ;
::pthread_mutex_unlock( &pipeMutex ) ;
signalError = false ;
return true ;
}
/**
* AddSignal() will not modify the open/close states of the pipes.
* If any error is detected, then signalError will be set to true
* and GetSignal() must handle the rest.
*/
void Signal::AddSignal( int whichSig )
{
errno = 0 ;
// Attempt to write this signal to the pipe.
if( ::write( writeFD, &whichSig, sizeof( int ) ) < 0 )
{
// Check for non-blocking type errors.
if( (EINTR != errno) && (EWOULDBLOCK != errno) &&
(EINTR != EAGAIN) )
{
// Critical error occured, readFD no longer valid
signalError = true ;
// Do not close the pipes here, the readFD may be
// in use. Let getSignal() handle updating the pipes.
// Do not attempt to create new pipes inside
// of the signal handler.
// The getSignal() below will attempt to reopen
// the pipes if it detects that one is closed.
return ;
} // if( errno )
} // if( ::read() )
// In general, outputting data during a signal handler is a very
// bad idea.
// This is for testing purposes only.
//std::cerr << "Signal::AddSignal> Successfully sent signal: "
// << whichSig
// << endl ;
} // AddSignal()
/**
* The semantics of the return statement are a little backwards here:
* - true indicates that the caller should check the value of theSignal
* for either a new signal or an error state (-1)
* - false indicates that no error and no signal are ready
*/
bool Signal::getSignal( int& theSignal )
{
errno = 0 ;
// Attempt to read the next signal from the pipe
::pthread_mutex_lock( &pipeMutex ) ;
int readResult = ::read( readFD, &theSignal, sizeof( int ) ) ;
::pthread_mutex_unlock( &pipeMutex ) ;
if( readResult < 0 )
{
// Check for non-blocking type errors.
if( (EINTR != errno) && (EWOULDBLOCK != errno) &&
(EINTR != EAGAIN) )
{
// Critical error occured, readFD no longer valid
// closePipes() will set signalError to true.
closePipes() ;
// Attempt to re-open the pipes
openPipes() ;
theSignal = -1 ;
return true ;
}
else
{
// No signal detected
return false ;
}
}
if( readResult != sizeof( int ) )
{
elog << "Signal::getSignal> Somehow read "
<< readResult
<< " bytes, where sizeof(int) is: "
<< sizeof( int )
<< endl ;
}
// Signal detected, read() returned ok
return true ;
}
Signal* Signal::getInstance()
{
::pthread_mutex_lock( &singletonMutex ) ;
if( 0 == theInstance )
{
theInstance = new Signal ;
}
::pthread_mutex_unlock( &singletonMutex ) ;
return theInstance ;
}
} // namespace gnuworld