/home/vlg/develop/libASSA/libassa/assa/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 
00033 #if !defined(WIN32)
00034 
00094 class SigAction
00095 {
00096 public:
00100     SigAction();
00101 
00114     SigAction (C_SIG_HANDLER handler_, 
00115                SigSet*       sig_mask_ = 0, 
00116                int           flags_ = 0);
00117 
00133     SigAction (C_SIG_HANDLER handler_,
00134                int           signum_,
00135                SigSet*       sig_mask_ = 0, 
00136                int           flags_ = 0);
00137 
00149     int register_action (int signum_, SigAction* oaction_ = 0);
00150 
00159     int restore_action (int signum_, SigAction& oaction_);
00160 
00167     int retrieve_action (int signum_);
00168 
00172     void action (SIGACTION * sa_);
00173 
00177     SIGACTION * action ();
00178 
00182     void flags (int new_flags_);
00183 
00187     int flags ();
00188 
00191     void mask (SigSet & mask_set_);
00192 
00195     SigSet mask ();
00196 
00199     void handler (C_SIG_HANDLER sha_);
00200 
00203     C_SIG_HANDLER handler ();
00204 
00209     operator SIGACTION *();
00210   
00211 private: 
00213     SIGACTION m_sa;  
00214 };
00215 
00216 //-------------------------------------------------------------------------
00217 //------------------------Inline functions---------------------------------
00218 //-------------------------------------------------------------------------
00219 inline
00220 SigAction::
00221 SigAction ()
00222 {
00223     trace_with_mask("SigAction::SigAction", SIGACT);
00224 
00225     m_sa.sa_flags = 0;
00226     sigemptyset(&m_sa.sa_mask);
00227     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) 0;
00228 }
00229 
00230 inline
00231 SigAction::
00232 SigAction (C_SIG_HANDLER handler_, 
00233                       SigSet*       sig_mask_,
00234                       int           flags_)
00235 {
00236     trace_with_mask("SigAction::SigAction(,,)", SIGACT);
00237   
00238     m_sa.sa_flags = flags_;
00239     if (sig_mask_ == NULL) {
00240         sigemptyset(&m_sa.sa_mask);
00241     }
00242     else {
00243         /*---
00244           here, suppose to do bitwise structure assignment,
00245           but does it really do so?
00246           = *sig_mask_
00247              = *(sig_mask_.operator *())
00248                 = *(SigSet *tmp = &sig_mask_.m_sa) ????
00249         ---*/
00250         m_sa.sa_mask = **sig_mask_;
00251     }
00252     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
00253 }
00254 
00255 inline
00256 SigAction::
00257 SigAction (C_SIG_HANDLER handler_,
00258            int           signum_,
00259            SigSet*       sig_mask_,
00260            int           flags_)
00261 {
00262     trace_with_mask("SigAction::SigAction(,,,)", SIGACT);
00263   
00264     m_sa.sa_flags = flags_;
00265     if (sig_mask_ == NULL) {
00266         sigemptyset(&m_sa.sa_mask);
00267     }
00268     else {
00269         /*---  same problem as above... ---*/
00270         m_sa.sa_mask = **sig_mask_;
00271     }
00272     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
00273     
00274     /*--- installing disposition... ---*/
00275     sigaction (signum_, &m_sa, 0);
00276 }
00277 
00278 inline void
00279 SigAction::
00280 action (SIGACTION* sa_)
00281 {
00282     trace_with_mask("SigAction::action", SIGACT);
00283     m_sa = *sa_;
00284 }
00285 
00286 inline SIGACTION *
00287 SigAction::
00288 action ()
00289 {
00290     trace_with_mask("SigAction::action", SIGACT);
00291 
00292     return &m_sa;
00293 }
00294 
00295 inline void
00296 SigAction::
00297 flags (int new_flags_)
00298 {
00299     trace_with_mask("void SigAction::flags()", SIGACT);
00300 
00301     m_sa.sa_flags = new_flags_;
00302 }
00303 
00304 inline int
00305 SigAction::
00306 flags ()
00307 {
00308     trace_with_mask("int SigAction::flags()", SIGACT);
00309 
00310     return m_sa.sa_flags;
00311 }
00312 
00313 inline void
00314 SigAction::
00315 mask (SigSet & mask_set_)
00316 {
00317     trace_with_mask("void SigAction::mask()", SIGACT);
00318   
00319     m_sa.sa_mask = *mask_set_;
00320 }
00321 
00322 inline SigSet
00323 SigAction::
00324 mask ()
00325 {
00326     trace_with_mask("SigSet SigAction::mask()", SIGACT);
00327 
00328     SigSet tmpset(&m_sa.sa_mask);
00329     return tmpset;
00330 }
00331 
00332 inline void
00333 SigAction::
00334 handler (C_SIG_HANDLER sha_)
00335 {
00336     trace_with_mask("void SigAction::handler()", SIGACT);
00337 
00338     *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) sha_;
00339 }
00340 
00341 inline C_SIG_HANDLER
00342 SigAction::
00343 handler ()
00344 {
00345     trace_with_mask("C_SIG_HANDLER SigAction::handler()", SIGACT);
00346 
00347     return (C_SIG_HANDLER) m_sa.sa_handler;
00348 }
00349 
00350 inline 
00351 SigAction::operator SIGACTION * ()
00352 {
00353     trace_with_mask("SigAction::operator SIGACTION * ()", SIGACT);
00354 
00355     return &m_sa;
00356 }
00357 
00358 inline int 
00359 SigAction::
00360 register_action (int signum_, SigAction* oaction_)
00361 {
00362     trace_with_mask("SigAction::register_action()", SIGACT);
00363 
00364     /*--- place here recursive mutex lock to guard ... ---*/
00365     struct sigaction *osa = oaction_ == 0 ? 0 : oaction_->action();
00366     return sigaction(signum_, &m_sa, osa);
00367 }
00368 
00369 inline int
00370 SigAction::
00371 restore_action (int signum_, SigAction& oaction_)
00372 {
00373     trace_with_mask("SigAction::restore_action()", SIGACT);
00374 
00375     m_sa = *oaction_.action();
00376     return sigaction(signum_, &m_sa, 0);
00377 }
00378 
00379 inline int 
00380 SigAction::
00381 retrieve_action (int signum_)
00382 {
00383     trace_with_mask("SigAction::retrieve_action()", SIGACT);
00384 
00385     return sigaction(signum_, 0, &m_sa);
00386 }
00387 
00388 #endif // !defined(WIN32)
00389 
00390 } // end namespace ASSA
00391 
00392 
00393 #endif /* _SigAction_h */

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6