Skip to content

Commit 322ef43

Browse files
added signal handling for proper termination
1 parent 2177268 commit 322ef43

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

devices/aliceHLTwrapper/aliceHLTWrapper.cxx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "WrapperDevice.h"
1919
#include <iostream>
20+
#include <csignal>
2021
#include <getopt.h>
2122
#include <memory>
2223
#include <cstring>
@@ -36,6 +37,33 @@ using std::stringstream;
3637
std::string address;
3738
};
3839

40+
FairMQDevice* gDevice=NULL;
41+
static void s_signal_handler (int signal)
42+
{
43+
cout << endl << "Caught signal " << signal << endl;
44+
45+
if (gDevice) {
46+
gDevice->ChangeState(FairMQDevice::STOP);
47+
gDevice->ChangeState(FairMQDevice::END);
48+
cout << "Shutdown complete. Bye!" << endl;
49+
} else {
50+
cerr << "No device to shut down, ignoring signal ..." << endl;
51+
}
52+
53+
exit(1);
54+
}
55+
56+
static void s_catch_signals (void)
57+
{
58+
struct sigaction action;
59+
action.sa_handler = s_signal_handler;
60+
action.sa_flags = 0;
61+
sigemptyset(&action.sa_mask);
62+
sigaction(SIGINT, &action, NULL);
63+
sigaction(SIGQUIT, &action, NULL);
64+
sigaction(SIGTERM, &action, NULL);
65+
}
66+
3967
int main(int argc, char** argv)
4068
{
4169
int iResult=0;
@@ -223,7 +251,16 @@ int main(int argc, char** argv)
223251
deviceArgs.push_back(argv[0]);
224252
if (iDeviceArg>0)
225253
deviceArgs.insert(deviceArgs.end(), argv+iDeviceArg, argv+argc);
226-
ALICE::HLT::WrapperDevice device(deviceArgs.size(), &deviceArgs[0]);
254+
255+
gDevice=new ALICE::HLT::WrapperDevice(deviceArgs.size(), &deviceArgs[0]);
256+
if (!gDevice) {
257+
cerr << "failed to create device" << endl;
258+
return -ENODEV;
259+
}
260+
s_catch_signals();
261+
262+
{ // scope for the device reference variable
263+
FairMQDevice& device=*gDevice;
227264

228265
device.SetTransport(transportFactory);
229266
device.SetProperty(FairMQDevice::Id, id.c_str());
@@ -256,9 +293,11 @@ int main(int argc, char** argv)
256293
{
257294
device.fRunningCondition.wait(lock);
258295
}
296+
} // scope for the device reference variable
259297

260-
device.ChangeState(FairMQDevice::STOP);
261-
device.ChangeState(FairMQDevice::END);
298+
FairMQDevice* almostdead=gDevice;
299+
gDevice=NULL;
300+
delete almostdead;
262301

263302
return iResult;
264303
}

0 commit comments

Comments
 (0)