The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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 const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast.
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface.
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames.
 
- Public Member Functions inherited from Service
const std::string & name () const override
 Retrieve name of the service.
 
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.
 
StatusCode sysStart () override
 Initialize Service.
 
StatusCode sysStop () override
 Initialize Service.
 
StatusCode sysFinalize () override
 Finalize Service.
 
StatusCode sysReinitialize () override
 Re-initialize the Service.
 
StatusCode sysRestart () override
 Re-initialize the Service.
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor.
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator.
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
template<class T>
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
 
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, bool createIf=true)
 
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
 Declare used tool.
 
template<class T>
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
 
template<class T>
void addToolsArray (ToolHandleArray< T > &hndlArr)
 
const std::vector< IAlgTool * > & tools () const
 
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked.
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 
 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property.
 
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.
 
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.
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property.
 
StatusCode setProperty (const std::string &name, const Gaudi::Details::PropertyBase &p) override
 set the property from another property with a different name
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property
 
const Gaudi::Details::PropertyBasegetProperty (std::string_view name) const override
 get the property by name
 
StatusCode getProperty (std::string_view n, std::string &v) const override
 convert the property to the string
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties
 
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name.
 
Gaudi::Details::PropertyBaseproperty (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected
 
void bindPropertiesTo (Gaudi::Interfaces::IOptionsSvc &optsSvc)
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 
- Public Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream)
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor.
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service.
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream.
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts.
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS)
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL)
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING)
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO)
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG)
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE)
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO)
 

Private Member Functions

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

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.
 
std::map< int, bool > m_signals
 Map of monitored signal numbers to the flag telling if they have to be propagated or not.
 
bool m_stopRequested = false
 Flag to remember if the stop has been requested because of a signal.
 
SmartIF< Gaudi::ISignalMonitorm_signalMonitor
 Pointer to the signal monitor service.
 
SmartIF< IIncidentSvcm_incidentSvc
 Pointer to the incident service.
 
SmartIF< IPropertym_appProperty
 Pointer to the interface to set the return code of the application.
 

Additional Inherited Members

- Public Types inherited from extends< Service, IIncidentListener >
using base_class
 Typedef to this class.
 
using extend_interfaces_base
 Typedef to the base of this class.
 
- Public Types inherited from Service
using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>
 
- Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl
 Typedef used to refer to this class from derived classes, as in.
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids
 take union of the ext_iids of all Interfaces...
 
- Protected Member Functions inherited from Service
std::vector< IAlgTool * > & tools ()
 
 ~Service () override
 
int outputLevel () const
 get the Service's output level
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const
 Set up local caches.
 
MSG::Level resetMessaging ()
 Reinitialize internal states.
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream.
 
- Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state.
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state.
 
Gaudi::Property< int > m_outputLevel { this, "OutputLevel", MSG::NIL, "output level" }
 flag indicating whether ToolHandle tools have been added to m_tools
 
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()" }
 
Gaudi::Property< bool > m_autoRetrieveTools
 
Gaudi::Property< bool > m_checkToolDeps
 
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service.
 

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 293 of file SignalMonitorSvc.cpp.

Member Function Documentation

◆ finalize()

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

Definition at line 337 of file SignalMonitorSvc.cpp.

337 {
338 m_incidentSvc->removeListener( this, IncidentType::BeginEvent );
339 m_incidentSvc.reset();
340 // disable the monitoring of the signals
341 std::for_each( std::begin( m_signals ), std::end( m_signals ), [&]( const std::pair<int, bool>& s ) {
342 // tell the signal monitor that we are interested in these signals
343 m_signalMonitor->ignoreSignal( s.first );
344 } );
345 m_signalMonitor.reset();
346 return Service::finalize();
347 }
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
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.
StatusCode finalize() override
Definition Service.cpp:223

◆ handle()

void Gaudi::Utils::StopSignalHandler::handle ( const Incident & )
inlineoverride

Definition at line 349 of file SignalMonitorSvc.cpp.

349 {
350 if ( !m_stopRequested ) {
351 const SigMap& sigmap( SigMap::instance() );
352 for ( const auto& s : m_signals ) {
353 if ( !m_signalMonitor->gotSignal( s.first ) ) continue;
354 warning() << "Received signal '" << sigmap.name( s.first ) << "' (" << s.first;
355 const std::string& desc = sigmap.desc( s.first );
356 if ( !desc.empty() ) warning() << ", " << desc;
357 warning() << ")" << endmsg;
358 m_stopRequested = true;
359 // Report the termination by signal at the end of the application
361 if ( Gaudi::setAppReturnCode( m_appProperty, SignalOffset + s.first ).isFailure() ) {
362 error() << "Could not set return code of the application (" << SignalOffset + s.first << ")" << endmsg;
363 }
364 }
365 if ( m_stopRequested ) {
366 auto ep = serviceLocator()->as<IEventProcessor>();
367 if ( ep ) {
368 warning() << "Scheduling a stop" << endmsg;
369 ep->stopRun().ignore();
370 } else {
371 warning() << "Cannot stop the processing because the IEventProcessor interface cannot be retrieved."
372 << endmsg;
373 }
374 }
375 }
376 }
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
bool m_stopRequested
Flag to remember if the stop has been requested because of a signal.
SmartIF< IProperty > m_appProperty
Pointer to the interface to set the return code of the application.
SmartIF< IFace > as()
Definition ISvcLocator.h:64
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
bool isFailure() const
Definition StatusCode.h:129
constexpr int SignalOffset
StatusCode setAppReturnCode(SmartIF< IProperty > &appmgr, int value, bool force=false)
Set the application return code.

◆ i_decodeSignal()

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 398 of file SignalMonitorSvc.cpp.

398 {
399 debug() << "Decoding signal declaration '" << sig << "'" << endmsg;
400 if ( sig.empty() || sig == "+" ) {
401 debug() << "Empty signal, ignored" << endmsg;
402 return { -1, false }; // silently ignore empty strings
403 }
404 const SigMap& sigmap( SigMap::instance() );
405 std::string signal = sig;
406 bool propagate = false;
407 // Check if the signal must be propagated
408 if ( signal[signal.size() - 1] == '+' ) {
409 debug() << "Must be propagated to previously registered signal handlers" << endmsg;
410 propagate = true;
411 signal.erase( signal.size() - 1, 1 ); // remove the '+' at the end of the string
412 }
413 int signum = -1;
414 // check if the signal is a number
415 if ( std::isdigit( signal[0] ) ) {
416 std::istringstream ss( signal );
417 ss >> signum;
418 } else {
419 // try to find the signal name in the list of known signals
420 signum = sigmap.signum( signal );
421 }
422 if ( signum < 0 ) {
423 warning() << "Cannot understand signal identifier '" << sig << "', ignored" << endmsg;
424 } else {
425 verbose() << "Matched signal '" << sigmap.name( signum ) << "' (" << signum;
426 const std::string& desc = sigmap.desc( signum );
427 if ( !desc.empty() ) { verbose() << ", " << desc; }
428 verbose() << ")" << endmsg;
429 }
430 return { signum, propagate };
431 }
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)

◆ initialize()

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

Definition at line 296 of file SignalMonitorSvc.cpp.

296 {
297 StatusCode sc = Service::initialize();
298 if ( sc.isFailure() ) { return sc; }
299 std::string serviceName( "Gaudi::Utils::SignalMonitorSvc" );
300 m_signalMonitor = serviceLocator()->service( serviceName );
301 if ( !m_signalMonitor ) {
302 error() << "Cannot retrieve " << serviceName << endmsg;
303 return StatusCode::FAILURE;
304 }
305 serviceName = "IncidentSvc";
306 m_incidentSvc = serviceLocator()->service( serviceName );
307 if ( !m_incidentSvc ) {
308 error() << "Cannot retrieve " << serviceName << endmsg;
309 return StatusCode::FAILURE;
310 }
311 // Get the IMainAppStatus interface of the ApplicationMgr
313 if ( !m_appProperty ) {
314 warning() << "Cannot retrieve IProperty interface of ApplicationMgr, "
315 "the return code will not be changed"
316 << endmsg;
317 }
318 // Decode the signal names
319 for ( const auto& signame : m_usedSignals ) {
320 auto sigid = i_decodeSignal( signame );
321 if ( sigid.first >= 0 ) { m_signals[sigid.first] = sigid.second; }
322 }
323 debug() << "Stopping on the signals:" << endmsg;
324 const SigMap& sigmap( SigMap::instance() );
325 for ( const auto& s : m_signals ) {
326 debug() << "\t" << sigmap.name( s.first ) << ": " << sigmap.desc( s.first ) << " (" << s.first << ")";
327 if ( s.second ) debug() << " propagated";
328 debug() << endmsg;
329 // tell the signal monitor that we are interested in these signals
330 m_signalMonitor->monitorSignal( s.first, s.second );
331 }
332 m_stopRequested = false;
333 debug() << "Register to the IncidentSvc" << endmsg;
334 m_incidentSvc->addListener( this, IncidentType::BeginEvent );
335 return StatusCode::SUCCESS;
336 }
Gaudi::Property< std::vector< std::string > > m_usedSignals
List of signal names or numbers (encoded as strings) to use to schedule a stop.
std::pair< int, bool > i_decodeSignal(const std::string &sig)
Function to translate the signal name to the signal number.
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
StatusCode initialize() override
Definition Service.cpp:118
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100

Member Data Documentation

◆ m_appProperty

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

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

Definition at line 396 of file SignalMonitorSvc.cpp.

◆ m_incidentSvc

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

Pointer to the incident service.

Definition at line 394 of file SignalMonitorSvc.cpp.

◆ m_signalMonitor

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

Pointer to the signal monitor service.

Definition at line 392 of file SignalMonitorSvc.cpp.

◆ m_signals

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 388 of file SignalMonitorSvc.cpp.

◆ m_stopRequested

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

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

Definition at line 390 of file SignalMonitorSvc.cpp.

◆ m_usedSignals

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 380 of file SignalMonitorSvc.cpp.

380 {
381 this,
382 "Signals",
383 { "SIGINT", "SIGXCPU" },
384 "List of signal names or numbers to use to schedule a stop. "
385 "If the signal is followed by a '+' the signal is propagated the previously "
386 "registered handler (if any)." };

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