ATLAS Offline Software
AppStopAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 #include "AppStopAlg.h"
6 #include "GaudiKernel/IEventProcessor.h"
7 #include "csignal"
8 
9 
10 namespace
11 {
12  volatile std::sig_atomic_t gSignalStatus ATLAS_THREAD_SAFE = 0;
13 }
14 
15 
16 void signal_handler(int /*signal*/,siginfo_t *, void *) {
17  gSignalStatus = 1;
18 }
19 
20 
21 AppStopAlg::AppStopAlg(const std::string& name, ISvcLocator* pSvcLocator):
22  AthAlgorithm(name,pSvcLocator) {
23  declareProperty("SignalNumber",m_signal=15);
24  declareProperty("InstallAfterFirstEvent",m_installAfterFirst=true);
25 }
26 
27 
29  ATH_MSG_INFO("Handling signal: " << m_signal);
30 
31  struct sigaction act{};
32  struct sigaction oldact{};
33 
34  act.sa_handler=nullptr;
35  sigemptyset(&act.sa_mask);
36  act.sa_flags=SA_RESTART | SA_SIGINFO;
37  act.sa_sigaction=::signal_handler;
38  if (sigaction(m_signal,&act,&oldact)==-1) {
39  ATH_MSG_ERROR("Failed to install signal handler for signal "<< m_signal);
40  return StatusCode::FAILURE;
41  }
42 
43  //const void* oldhandle= (oldact.sa_flags & SA_SIGINFO) ? (void*)oldact.sa_sigaction : (void*)oldact.sa_handler;
44 
45  if (oldact.sa_handler==SIG_DFL || oldact.sa_handler==SIG_IGN) {
46  ATH_MSG_DEBUG("No previosly defined signal handler for signal " << m_signal);
47  }
48  else {
49  ATH_MSG_INFO("Replacing previously defined signal handler for signal " << m_signal);
50  }
51  m_handlerInstalled=true;
52  return StatusCode::SUCCESS;
53 }
54 
55 
57  if (!m_installAfterFirst) {
58  return installHandler();
59  }
60  else {
61  return StatusCode::SUCCESS;
62  }
63 }
64 
66  if (!m_handlerInstalled) {
67  return installHandler();
68  }
69 
70 
71  if (gSignalStatus!=0) {
72 
73  ATH_MSG_INFO("Got signal " << m_signal << ". Stopping the application");
74  IEventProcessor* appMgr=nullptr;
75  ATH_CHECK(service("ApplicationMgr",appMgr));
76 
77  if (!appMgr) {
78  ATH_MSG_ERROR("Failed to retrieve ApplicationMgr as IEventProcessor");
79  return StatusCode::FAILURE;
80  }
81  ATH_CHECK(appMgr->stopRun());
82  }
83 
84  return StatusCode::SUCCESS;
85 
86 }
signal_handler
void signal_handler(int, siginfo_t *, void *)
Definition: AppStopAlg.cxx:16
AppStopAlg::m_signal
int m_signal
member variables for algorithm properties:
Definition: AppStopAlg.h:37
AppStopAlg::installHandler
StatusCode installHandler()
Definition: AppStopAlg.cxx:28
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
siginfo_t
Definition: SealSignal.h:77
AppStopAlg::m_handlerInstalled
bool m_handlerInstalled
Definition: AppStopAlg.h:35
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
AppStopAlg::execute
StatusCode execute()
standard Athena-Algorithm method
Definition: AppStopAlg.cxx:65
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AppStopAlg.h
AthAlgorithm
Definition: AthAlgorithm.h:47
AppStopAlg::initialize
StatusCode initialize()
standard Athena-Algorithm method
Definition: AppStopAlg.cxx:56
AppStopAlg::AppStopAlg
AppStopAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: AppStopAlg.cxx:21
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
checker_macros.h
Define macros for attributes used to control the static checker.
AppStopAlg::m_installAfterFirst
bool m_installAfterFirst
Definition: AppStopAlg.h:38
sigemptyset
#define sigemptyset(x)
Definition: SealSignal.h:82