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

 StopSignalHandler (const std::string &name, ISvcLocator *svcLoc)
 
StatusCode initialize ()
 
StatusCode finalize ()
 
virtual void handle (const Incident &)
 
- Public Member Functions inherited from extends< BASE, Interfaces >
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::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
 ~extends () override=default
 Virtual destructor. More...
 
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::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
 ~extends () override=default
 Virtual destructor. More...
 
- Public Member Functions inherited from extend_interfaces< Interfaces...>
 ~extend_interfaces () override=default
 Virtual destructor. More...
 
 ~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

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
 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< BASE, Interfaces >
typedef extends base_class
 Typedef to this class. More...
 
typedef extend_interfaces< Interfaces...> extend_interfaces_base
 Typedef to the base of this class. More...
 
typedef extends base_class
 Typedef to this class. More...
 
typedef extend_interfaces< Interfaces...> extend_interfaces_base
 Typedef to the base of this class. More...
 
- 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...
 
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids...>::type
 take union of the ext_iids of all Interfaces... 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 341 of file SignalMonitorSvc.cpp.

Constructor & Destructor Documentation

Gaudi::Utils::StopSignalHandler::StopSignalHandler ( const std::string &  name,
ISvcLocator svcLoc 
)
inline

Definition at line 343 of file SignalMonitorSvc.cpp.

343  : base_class(name, svcLoc) {
344  m_usedSignals.reserve(2);
345  m_usedSignals.push_back("SIGINT");
346  m_usedSignals.push_back("SIGXCPU");
347  m_stopRequested = false;
348  declareProperty("Signals", m_usedSignals,
349  "List of signal names or numbers to use to schedule a stop. "
350  "If the signal is followed by a '+' the signal is propagated the previously "
351  "registered handler (if any).");
352  }
bool m_stopRequested
Flag to remember if the stop has been requested because of a signal.
std::vector< std::string > m_usedSignals
List of signal names or numbers (encoded as strings) to use to schedule a stop.
extends base_class
Typedef to this class.
Definition: extends.h:14

Member Function Documentation

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

Definition at line 399 of file SignalMonitorSvc.cpp.

399  {
402  // disable the monitoring of the signals
403  std::for_each( std::begin(m_signals), std::end(m_signals),
404  [&](const std::pair<int,bool>& s) {
405  // tell the signal monitor that we are interested in these signals
406  m_signalMonitor->ignoreSignal(s.first);
407  } );
408  m_signalMonitor.reset();
409  return Service::finalize();
410  }
const std::string BeginEvent
Processing of a new event has started.
Definition: Incident.h:60
StatusCode finalize() override
Definition: Service.cpp:187
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
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...
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
string s
Definition: gaudirun.py:246
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.
virtual void Gaudi::Utils::StopSignalHandler::handle ( const Incident )
inlinevirtual

Definition at line 412 of file SignalMonitorSvc.cpp.

412  {
413  if (!m_stopRequested) {
414  const SigMap& sigmap(SigMap::instance());
415  for (const auto& s : m_signals ) {
416  if (!m_signalMonitor->gotSignal(s.first)) continue;
417  warning() << "Received signal '" << sigmap.name(s.first)
418  << "' (" << s.first;
419  const std::string &desc = sigmap.desc(s.first);
420  if ( ! desc.empty() ) warning() << ", " << desc;
421  warning() << ")" << endmsg;
422  m_stopRequested = true;
423  // Report the termination by signal at the end of the application
426  error() << "Could not set return code of the application ("
427  << SignalOffset + s.first << ")"
428  << endmsg;
429  }
430 
431  }
432  if (m_stopRequested) {
433  auto ep = serviceLocator()->as<IEventProcessor>();
434  if (ep) {
435  warning() << "Scheduling a stop" << endmsg;
436  ep->stopRun().ignore();
437  }
438  else {
439  warning() << "Cannot stop the processing because the IEventProcessor interface cannot be retrieved." << endmsg;
440  }
441  }
442  }
443  }
bool m_stopRequested
Flag to remember if the stop has been requested because of a signal.
int maxevt auto ep
Definition: Bootstrap.cpp:286
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< Gaudi::ISignalMonitor > m_signalMonitor
Pointer to the signal monitor service.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
std::map< int, bool > m_signals
Map of monitored signal numbers to the flag telling if they have to be propagated or not...
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:50
string s
Definition: gaudirun.py:246
The IEventProcessor is the interface to process events.
constexpr int SignalOffset
Definition: AppReturnCode.h:34
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 458 of file SignalMonitorSvc.cpp.

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

Definition at line 353 of file SignalMonitorSvc.cpp.

353  {
355  if (sc.isFailure()) {
356  return sc;
357  }
358  std::string serviceName("Gaudi::Utils::SignalMonitorSvc");
359  m_signalMonitor = serviceLocator()->service(serviceName);
360  if ( ! m_signalMonitor ) {
361  error() << "Cannot retrieve " << serviceName << endmsg;
362  return StatusCode::FAILURE;
363  }
364  serviceName = "IncidentSvc";
365  m_incidentSvc = serviceLocator()->service(serviceName);
366  if ( ! m_incidentSvc ) {
367  error() << "Cannot retrieve " << serviceName << endmsg;
368  return StatusCode::FAILURE;
369  }
370  // Get the IMainAppStatus interface of the ApplicationMgr
371  m_appProperty = serviceLocator();
372  if ( ! m_appProperty ) {
373  warning() << "Cannot retrieve IProperty interface of ApplicationMgr, "
374  "the return code will not be changed" << endmsg;
375  }
376  // Decode the signal names
377  for (std::vector<std::string>::const_iterator signame = m_usedSignals.begin();
378  signame != m_usedSignals.end(); ++signame) {
379  auto sigid = i_decodeSignal(*signame);
380  if (sigid.first >= 0) {
381  m_signals[sigid.first] = sigid.second;
382  }
383  }
384  debug() << "Stopping on the signals:" << endmsg;
385  const SigMap& sigmap(SigMap::instance());
386  for (const auto& s : m_signals ) {
387  debug() << "\t" << sigmap.name(s.first) << ": "
388  << sigmap.desc(s.first) << " (" << s.first << ")";
389  if (s.second) debug() << " propagated";
390  debug() << endmsg;
391  // tell the signal monitor that we are interested in these signals
392  m_signalMonitor->monitorSignal(s.first, s.second);
393  }
394  m_stopRequested = false;
395  debug() << "Register to the IncidentSvc" << endmsg;
397  return StatusCode::SUCCESS;
398  }
const std::string BeginEvent
Processing of a new event has started.
Definition: Incident.h:60
StatusCode initialize() override
Definition: Service.cpp:62
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.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< Gaudi::ISignalMonitor > m_signalMonitor
Pointer to the signal monitor service.
std::vector< std::string > m_usedSignals
List of signal names or numbers (encoded as strings) to use to schedule a stop.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
std::map< int, bool > m_signals
Map of monitored signal numbers to the flag telling if they have to be propagated or not...
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
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.
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:246

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

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

Pointer to the incident service.

Definition at line 454 of file SignalMonitorSvc.cpp.

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

Pointer to the signal monitor service.

Definition at line 452 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 448 of file SignalMonitorSvc.cpp.

bool Gaudi::Utils::StopSignalHandler::m_stopRequested
private

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

Definition at line 450 of file SignalMonitorSvc.cpp.

std::vector<std::string> Gaudi::Utils::StopSignalHandler::m_usedSignals
private

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

Definition at line 446 of file SignalMonitorSvc.cpp.


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