Gaudi::Utils::StopSignalHandler Class Reference

Service that stop the processing if a signal is received. More...

Inheritance diagram for Gaudi::Utils::StopSignalHandler:
Collaboration diagram for Gaudi::Utils::StopSignalHandler:

Public Member Functions

StatusCode initialize () override
 
StatusCode finalize () override
 
void handle (const Incident &) override
 
- Public Member Functions inherited from extends< Service, IIncidentListener >
void * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::stringgetInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
 ~extends () override=default
 Virtual destructor. More...
 
- Public Member Functions inherited from Service
const std::stringname () const override
 Retrieve name of the service. More...
 
StatusCode configure () override
 
StatusCode initialize () override
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode finalize () override
 
StatusCode terminate () override
 
Gaudi::StateMachine::State FSMState () const override
 
Gaudi::StateMachine::State targetFSMState () const override
 
StatusCode reinitialize () override
 
StatusCode restart () override
 
StatusCode sysInitialize () override
 Initialize Service. More...
 
StatusCode sysStart () override
 Initialize Service. More...
 
StatusCode sysStop () override
 Initialize Service. More...
 
StatusCode sysFinalize () override
 Finalize Service. More...
 
StatusCode sysReinitialize () override
 Re-initialize the Service. More...
 
StatusCode sysRestart () override
 Re-initialize the Service. More...
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor. More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator. More...
 
StatusCode setProperties ()
 Method for setting declared properties to the values specified for the job. More...
 
template<class T >
StatusCode service (const std::string &name, const T *&psvc, bool createIf=true) const
 Access a service by name, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode service (const std::string &name, T *&psvc, bool createIf=true) const
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
template<class T >
StatusCode service (const std::string &svcType, const std::string &svcName, T *&psvc) const
 Access a service by name and type, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode declarePrivateTool (ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
 Declare used Private tool. More...
 
template<class T >
StatusCode declarePublicTool (ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
 Declare used Public tool. More...
 
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked. More...
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 PropertyHolder ()=default
 
 ~PropertyHolder () override=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, TYPE &value, const std::string &doc="none")
 Helper to wrap a regular data member and use it as a regular property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, TYPE &value, const std::string &doc="none") const
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
 Declare a PropertyBase instance setting name and documentation. More...
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property. More...
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p) override
 set the property form another property More...
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string More...
 
StatusCode setProperty (const std::string &n, const std::string &v) override
 set the property from name and the value More...
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value More...
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property More...
 
const Gaudi::Details::PropertyBasegetProperty (const std::string &name) const override
 get the property by name More...
 
StatusCode getProperty (const std::string &n, std::string &v) const override
 convert the property to the string More...
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties More...
 
bool hasProperty (const std::string &name) const override
 Return true if we have a property with the given name. More...
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, GaudiHandleBase &ref, const std::string &doc="none")
 Specializations for various GaudiHandles. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, GaudiHandleArrayBase &ref, const std::string &doc="none")
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, DataObjectHandleBase &ref, const std::string &doc="none")
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor. More...
 
SmartIF< IMessageSvc > & msgSvc () const
 The standard message service. More...
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream. More...
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts. More...
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS) More...
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL) More...
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING) More...
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO) More...
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG) More...
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE) More...
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO) More...
 
MSG::Level msgLevel () const
 get the output level from the embedded MsgStream More...
 
MSG::Level outputLevel () const __attribute__((deprecated))
 Backward compatibility function for getting the output level. More...
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream More...
 
- Public Member Functions inherited from extend_interfaces< Interfaces... >
 ~extend_interfaces () override=default
 Virtual destructor. More...
 

Private Member Functions

std::pair< int, bool > i_decodeSignal (const std::string &sig)
 Function to translate the signal name to the signal number. More...
 

Private Attributes

Gaudi::Property< std::vector< std::string > > m_usedSignals
 List of signal names or numbers (encoded as strings) to use to schedule a stop. More...
 
std::map< int, bool > m_signals
 Map of monitored signal numbers to the flag telling if they have to be propagated or not. More...
 
bool m_stopRequested = false
 Flag to remember if the stop has been requested because of a signal. More...
 
SmartIF< Gaudi::ISignalMonitorm_signalMonitor
 Pointer to the signal monitor service. More...
 
SmartIF< IIncidentSvcm_incidentSvc
 Pointer to the incident service. More...
 
SmartIF< IPropertym_appProperty
 Pointer to the interface to set the return code of the application. More...
 

Additional Inherited Members

- Public Types inherited from extends< Service, IIncidentListener >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces... >
 Typedef to the base of this class. More...
 
- Public Types inherited from Service
typedef Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
 
- Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl = PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 Typedef used to refer to this class from derived classes, as in. More...
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class = CommonMessaging
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids... >::type
 take union of the ext_iids of all Interfaces... More...
 
- Protected Member Functions inherited from Service
 ~Service () override
 Standard Destructor. More...
 
int outputLevel () const
 get the Service's output level More...
 
- Protected Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
Gaudi::Details::PropertyBaseproperty (const std::string &name) const
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream. More...
 
- Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::Property< int > m_outputLevel {this, "OutputLevel", MSG::NIL, "output level"}
 
Gaudi::Property< bool > m_auditInit {this, "AuditServices", false, "[[deprecated]] unused"}
 
Gaudi::Property< bool > m_auditorInitialize {this, "AuditInitialize", false, "trigger auditor on initialize()"}
 
Gaudi::Property< bool > m_auditorStart {this, "AuditStart", false, "trigger auditor on start()"}
 
Gaudi::Property< bool > m_auditorStop {this, "AuditStop", false, "trigger auditor on stop()"}
 
Gaudi::Property< bool > m_auditorFinalize {this, "AuditFinalize", false, "trigger auditor on finalize()"}
 
Gaudi::Property< bool > m_auditorReinitialize {this, "AuditReinitialize", false, "trigger auditor on reinitialize()"}
 
Gaudi::Property< bool > m_auditorRestart {this, "AuditRestart", false, "trigger auditor on restart()"}
 
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service. More...
 

Detailed Description

Service that stop the processing if a signal is received.

The signals to be intercepted have to be declared in the property Signals as a list of strings (signal names or numbers). If '+' is appended to the signal name, then the signal is propagated to the signal handlers already registered when this service is initialized.

Definition at line 350 of file SignalMonitorSvc.cpp.

Member Function Documentation

StatusCode Gaudi::Utils::StopSignalHandler::finalize ( )
inlineoverride

Definition at line 400 of file SignalMonitorSvc.cpp.

401  {
402  m_incidentSvc->removeListener( this, IncidentType::BeginEvent );
404  // disable the monitoring of the signals
406  // tell the signal monitor that we are interested in these signals
407  m_signalMonitor->ignoreSignal( s.first );
408  } );
409  m_signalMonitor.reset();
410  return Service::finalize();
411  }
StatusCode finalize() override
Definition: Service.cpp:174
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
T end(T...args)
SmartIF< Gaudi::ISignalMonitor > m_signalMonitor
Pointer to the signal monitor service.
std::map< int, bool > m_signals
Map of monitored signal numbers to the flag telling if they have to be propagated or not...
T begin(T...args)
string s
Definition: gaudirun.py:245
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
T for_each(T...args)
void Gaudi::Utils::StopSignalHandler::handle ( const Incident )
inlineoverride

Definition at line 413 of file SignalMonitorSvc.cpp.

414  {
415  if ( !m_stopRequested ) {
416  const SigMap& sigmap( SigMap::instance() );
417  for ( const auto& s : m_signals ) {
418  if ( !m_signalMonitor->gotSignal( s.first ) ) continue;
419  warning() << "Received signal '" << sigmap.name( s.first ) << "' (" << s.first;
420  const std::string& desc = sigmap.desc( s.first );
421  if ( !desc.empty() ) warning() << ", " << desc;
422  warning() << ")" << endmsg;
423  m_stopRequested = true;
424  // Report the termination by signal at the end of the application
427  error() << "Could not set return code of the application (" << SignalOffset + s.first << ")" << endmsg;
428  }
429  }
430  if ( m_stopRequested ) {
431  auto ep = serviceLocator()->as<IEventProcessor>();
432  if ( ep ) {
433  warning() << "Scheduling a stop" << endmsg;
434  ep->stopRun().ignore();
435  } else {
436  warning() << "Cannot stop the processing because the IEventProcessor interface cannot be retrieved."
437  << endmsg;
438  }
439  }
440  }
441  }
T empty(T...args)
bool m_stopRequested
Flag to remember if the stop has been requested because of a signal.
SmartIF< IFace > as()
Definition: ISvcLocator.h:106
SmartIF< Gaudi::ISignalMonitor > m_signalMonitor
Pointer to the signal monitor service.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
STL class.
std::map< int, bool > m_signals
Map of monitored signal numbers to the flag telling if they have to be propagated or not...
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
SmartIF< IProperty > m_appProperty
Pointer to the interface to set the return code of the application.
StatusCode setAppReturnCode(SmartIF< IProperty > &appmgr, int value, bool force=false)
Set the application return code.
Definition: AppReturnCode.h:51
string s
Definition: gaudirun.py:245
The IEventProcessor is the interface to process events.
constexpr int SignalOffset
Definition: AppReturnCode.h:36
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::pair<int, bool> Gaudi::Utils::StopSignalHandler::i_decodeSignal ( const std::string sig)
inlineprivate

Function to translate the signal name to the signal number.

Definition at line 463 of file SignalMonitorSvc.cpp.

464  {
465  debug() << "Decoding signal declaration '" << sig << "'" << endmsg;
466  if ( sig.empty() || sig == "+" ) {
467  debug() << "Empty signal, ignored" << endmsg;
468  return {-1, false}; // silently ignore empty strings
469  }
470  const SigMap& sigmap( SigMap::instance() );
471  std::string signal = sig;
472  bool propagate = false;
473  // Check if the signal must be propagated
474  if ( signal[signal.size() - 1] == '+' ) {
475  debug() << "Must be propagated to previously registered signal handlers" << endmsg;
476  propagate = true;
477  signal.erase( signal.size() - 1, 1 ); // remove the '+' at the end of the string
478  }
479  int signum = -1;
480  // check if the signal is a number
481  if ( std::isdigit( signal[0] ) ) {
482  std::istringstream ss( signal );
483  ss >> signum;
484  } else {
485  // try to find the signal name in the list of known signals
486  signum = sigmap.signum( signal );
487  }
488  if ( signum < 0 ) {
489  warning() << "Cannot understand signal identifier '" << sig << "', ignored" << endmsg;
490  } else {
491  verbose() << "Matched signal '" << sigmap.name( signum ) << "' (" << signum;
492  const std::string& desc = sigmap.desc( signum );
493  if ( !desc.empty() ) {
494  verbose() << ", " << desc;
495  }
496  verbose() << ")" << endmsg;
497  }
498  return {signum, propagate};
499  }
T empty(T...args)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
STL class.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
T isdigit(T...args)
T erase(T...args)
T signal(T...args)
T size(T...args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode Gaudi::Utils::StopSignalHandler::initialize ( )
inlineoverride

Definition at line 354 of file SignalMonitorSvc.cpp.

355  {
357  if ( sc.isFailure() ) {
358  return sc;
359  }
360  std::string serviceName( "Gaudi::Utils::SignalMonitorSvc" );
361  m_signalMonitor = serviceLocator()->service( serviceName );
362  if ( !m_signalMonitor ) {
363  error() << "Cannot retrieve " << serviceName << endmsg;
364  return StatusCode::FAILURE;
365  }
366  serviceName = "IncidentSvc";
367  m_incidentSvc = serviceLocator()->service( serviceName );
368  if ( !m_incidentSvc ) {
369  error() << "Cannot retrieve " << serviceName << endmsg;
370  return StatusCode::FAILURE;
371  }
372  // Get the IMainAppStatus interface of the ApplicationMgr
374  if ( !m_appProperty ) {
375  warning() << "Cannot retrieve IProperty interface of ApplicationMgr, "
376  "the return code will not be changed"
377  << endmsg;
378  }
379  // Decode the signal names
380  for ( const auto& signame : m_usedSignals ) {
381  auto sigid = i_decodeSignal( signame );
382  if ( sigid.first >= 0 ) {
383  m_signals[sigid.first] = sigid.second;
384  }
385  }
386  debug() << "Stopping on the signals:" << endmsg;
387  const SigMap& sigmap( SigMap::instance() );
388  for ( const auto& s : m_signals ) {
389  debug() << "\t" << sigmap.name( s.first ) << ": " << sigmap.desc( s.first ) << " (" << s.first << ")";
390  if ( s.second ) debug() << " propagated";
391  debug() << endmsg;
392  // tell the signal monitor that we are interested in these signals
393  m_signalMonitor->monitorSignal( s.first, s.second );
394  }
395  m_stopRequested = false;
396  debug() << "Register to the IncidentSvc" << endmsg;
397  m_incidentSvc->addListener( this, IncidentType::BeginEvent );
398  return StatusCode::SUCCESS;
399  }
StatusCode initialize() override
Definition: Service.cpp:64
bool m_stopRequested
Flag to remember if the stop has been requested because of a signal.
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
SmartIF< Gaudi::ISignalMonitor > m_signalMonitor
Pointer to the signal monitor service.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
std::map< int, bool > m_signals
Map of monitored signal numbers to the flag telling if they have to be propagated or not...
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Gaudi::Property< std::vector< std::string > > m_usedSignals
List of signal names or numbers (encoded as strings) to use to schedule a stop.
SmartIF< IProperty > m_appProperty
Pointer to the interface to set the return code of the application.
std::pair< int, bool > i_decodeSignal(const std::string &sig)
Function to translate the signal name to the signal number.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
string s
Definition: gaudirun.py:245
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244

Member Data Documentation

SmartIF<IProperty> Gaudi::Utils::StopSignalHandler::m_appProperty
private

Pointer to the interface to set the return code of the application.

Definition at line 461 of file SignalMonitorSvc.cpp.

SmartIF<IIncidentSvc> Gaudi::Utils::StopSignalHandler::m_incidentSvc
private

Pointer to the incident service.

Definition at line 459 of file SignalMonitorSvc.cpp.

SmartIF<Gaudi::ISignalMonitor> Gaudi::Utils::StopSignalHandler::m_signalMonitor
private

Pointer to the signal monitor service.

Definition at line 457 of file SignalMonitorSvc.cpp.

std::map<int, bool> Gaudi::Utils::StopSignalHandler::m_signals
private

Map of monitored signal numbers to the flag telling if they have to be propagated or not.

Definition at line 453 of file SignalMonitorSvc.cpp.

bool Gaudi::Utils::StopSignalHandler::m_stopRequested = false
private

Flag to remember if the stop has been requested because of a signal.

Definition at line 455 of file SignalMonitorSvc.cpp.

Gaudi::Property<std::vector<std::string> > Gaudi::Utils::StopSignalHandler::m_usedSignals
private
Initial value:
{
this,
"Signals",
{"SIGINT", "SIGXCPU"},
"List of signal names or numbers to use to schedule a stop. "
"If the signal is followed by a '+' the signal is propagated the previously "
"registered handler (if any)."}

List of signal names or numbers (encoded as strings) to use to schedule a stop.

Definition at line 445 of file SignalMonitorSvc.cpp.


The documentation for this class was generated from the following file: