SigAction.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                            SigAction.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (c) 1997 by Vladislav Grinchenko
00006 //
00007 //  This library is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU Library General Public
00009 //  License as published by the Free Software Foundation; either
00010 //  version 2 of the License, or (at your option) any later version.
00011 //------------------------------------------------------------------------------
00012 
00013 #ifndef _SigAction_h
00014 #define _SigAction_h
00015 
00016 // System includes
00017 //
00018 #include <signal.h>
00019 #include <errno.h>
00020 
00021 #include "assa/Assure.h"
00022 #include "assa/SigSet.h"
00023 
00024 // some convenient typedefs
00025 //
00026 extern "C" {
00027 typedef struct sigaction SIGACTION;
00028 typedef void (*C_SIG_HANDLER)( int );
00029 }
00030 
00031 namespace ASSA {
00032 
00092 class SigAction
00093 {
00094 public:
00098     SigAction();
00099 
00112     SigAction (C_SIG_HANDLER handler_, 
00113                SigSet*       sig_mask_ = 0, 
00114                int           flags_ = 0);
00115 
00131     SigAction (C_SIG_HANDLER handler_,
00132                int           signum_,
00133                SigSet*       sig_mask_ = 0, 
00134                int           flags_ = 0);
00135 
00147     int register_action (int signum_, SigAction* oaction_ = 0);
00148 
00157     int restore_action (int signum_, SigAction& oaction_);
00158 
00165     int retrieve_action (int signum_);
00166 
00170     void action (SIGACTION * sa_);
00171 
00175     SIGACTION * action ();
00176 
00180     void flags (int new_flags_);
00181 
00185     int flags ();
00186 
00189     void mask (SigSet & mask_set_);
00190 
00193     SigSet mask ();
00194 
00197     void handler (C_SIG_HANDLER sha_);
00198 
00201     C_SIG_HANDLER handler ();
00202 
00207     operator SIGACTION *();
00208   
00209 private: 
00211     SIGACTION m_sa;  
00212 };
00213 
00214 //-------------------------------------------------------------------------
00215 //------------------------Inline functions---------------------------------
00216 //-------------------------------------------------------------------------
00217 inline
00218 SigAction::
00219 SigAction ()
00220 {
00221     trace_with_mask("SigAction::SigAction", SIGACT);
00222 
00223     m_sa.sa_flags = 0;
00224     sigemptyset(&m_sa.sa_mask);
00225     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) 0;
00226 }
00227 
00228 inline
00229 SigAction::
00230 SigAction (C_SIG_HANDLER handler_, 
00231                       SigSet*       sig_mask_,
00232                       int           flags_)
00233 {
00234     trace_with_mask("SigAction::SigAction(,,)", SIGACT);
00235   
00236     m_sa.sa_flags = flags_;
00237     if (sig_mask_ == NULL) {
00238         sigemptyset(&m_sa.sa_mask);
00239     }
00240     else {
00241         /*---
00242           here, suppose to do bitwise structure assignment,
00243           but does it really do so?
00244           = *sig_mask_
00245              = *(sig_mask_.operator *())
00246                 = *(SigSet *tmp = &sig_mask_.m_sa) ????
00247         ---*/
00248         m_sa.sa_mask = **sig_mask_;
00249     }
00250     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
00251 }
00252 
00253 inline
00254 SigAction::
00255 SigAction (C_SIG_HANDLER handler_,
00256            int           signum_,
00257            SigSet*       sig_mask_,
00258            int           flags_)
00259 {
00260     trace_with_mask("SigAction::SigAction(,,,)", SIGACT);
00261   
00262     m_sa.sa_flags = flags_;
00263     if (sig_mask_ == NULL) {
00264         sigemptyset(&m_sa.sa_mask);
00265     }
00266     else {
00267         /*---  same problem as above... ---*/
00268         m_sa.sa_mask = **sig_mask_;
00269     }
00270     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
00271     
00272     /*--- installing disposition... ---*/
00273     sigaction (signum_, &m_sa, 0);
00274 }
00275 
00276 inline void
00277 SigAction::
00278 action (SIGACTION* sa_)
00279 {
00280     trace_with_mask("SigAction::action", SIGACT);
00281     m_sa = *sa_;
00282 }
00283 
00284 inline SIGACTION *
00285 SigAction::
00286 action ()
00287 {
00288     trace_with_mask("SigAction::action", SIGACT);
00289 
00290     return &m_sa;
00291 }
00292 
00293 inline void
00294 SigAction::
00295 flags (int new_flags_)
00296 {
00297     trace_with_mask("void SigAction::flags()", SIGACT);
00298 
00299     m_sa.sa_flags = new_flags_;
00300 }
00301 
00302 inline int
00303 SigAction::
00304 flags ()
00305 {
00306     trace_with_mask("int SigAction::flags()", SIGACT);
00307 
00308     return m_sa.sa_flags;
00309 }
00310 
00311 inline void
00312 SigAction::
00313 mask (SigSet & mask_set_)
00314 {
00315     trace_with_mask("void SigAction::mask()", SIGACT);
00316   
00317     m_sa.sa_mask = *mask_set_;
00318 }
00319 
00320 inline SigSet
00321 SigAction::
00322 mask ()
00323 {
00324     trace_with_mask("SigSet SigAction::mask()", SIGACT);
00325 
00326     SigSet tmpset(&m_sa.sa_mask);
00327     return tmpset;
00328 }
00329 
00330 inline void
00331 SigAction::
00332 handler (C_SIG_HANDLER sha_)
00333 {
00334     trace_with_mask("void SigAction::handler()", SIGACT);
00335 
00336     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) sha_;
00337 }
00338 
00339 inline C_SIG_HANDLER
00340 SigAction::
00341 handler ()
00342 {
00343     trace_with_mask("C_SIG_HANDLER SigAction::handler()", SIGACT);
00344 
00345     return (C_SIG_HANDLER) m_sa.sa_handler;
00346 }
00347 
00348 inline 
00349 SigAction::operator SIGACTION * ()
00350 {
00351     trace_with_mask("SigAction::operator SIGACTION * ()", SIGACT);
00352 
00353     return &m_sa;
00354 }
00355 
00356 inline int 
00357 SigAction::
00358 register_action (int signum_, SigAction* oaction_)
00359 {
00360     trace_with_mask("SigAction::register_action()", SIGACT);
00361 
00362     /*--- place here recursive mutex lock to guard ... ---*/
00363     struct sigaction *osa = oaction_ == 0 ? 0 : oaction_->action();
00364     return sigaction(signum_, &m_sa, osa);
00365 }
00366 
00367 inline int
00368 SigAction::
00369 restore_action (int signum_, SigAction& oaction_)
00370 {
00371     trace_with_mask("SigAction::restore_action()", SIGACT);
00372 
00373     m_sa = *oaction_.action();
00374     return sigaction(signum_, &m_sa, 0);
00375 }
00376 
00377 inline int 
00378 SigAction::
00379 retrieve_action (int signum_)
00380 {
00381     trace_with_mask("SigAction::retrieve_action()", SIGACT);
00382 
00383     return sigaction(signum_, 0, &m_sa);
00384 }
00385 
00386 } // end namespace ASSA
00387 
00388 
00389 #endif /* _SigAction_h */

Generated on Mon Dec 19 15:59:01 2005 for libassa by  doxygen 1.4.5