[Cppcms-users] Can't catch signals after service::run()
Brought to you by:
artyom-beilis
|
From: CN <cn...@fa...> - 2013-12-09 15:35:54
|
I have read http://cppcms.com/wikipp/en/page/cppcms_1x_reload_application and the mailing list archive dated 2013-11-19 08:05:50 GMT before posting my question. I need to do some clean-up tasks after the program is killed (kill <pid>), but before it terminates. As shown below, version 1 can catch signals. Version 2 fails to catch signals. I wonder this is because cppcms::service::run() overwrites all the signal specifications set before it. How do I circumvent it? //------------------------------------------------ #include <iostream> #include <signal.h> #include <unistd.h> #include <cstring> #include <cppcms/service.h> #include <cppcms/thread_pool.h> void signal_handler(int signal_number); static bool got_signal=false; int main(int argc,char ** argv) { struct sigaction actions; memset(&actions,0,sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags=0; actions.sa_handler=signal_handler; int sig_result; if((sig_result=sigaction(SIGHUP,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGHUP) failed: " << sig_result << std::endl; std::terminate(); } if((sig_result=sigaction(SIGTERM,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGTERM) failed: " << sig_result << std::endl; std::terminate(); } /* //version 1: signal can be caught. while(true){ std::cerr<<"loop"<<std::endl; if(got_signal){ std::cerr<<"caught signal"<<std::endl; break; } sleep(1); } */ //version 2: signal can NOT be caught. cppcms::thread_pool pool(3); //pools.post(...); cppcms::service s(argc,argv); s.run(); if(got_signal){ pool.stop(); //Stop all in progress threads before program terminates. std::cerr<<"caught signal"<<std::endl; } return 0; } void signal_handler(int signal_number) { got_signal=true; } //------------------------------------------------ Regards, CN -- http://www.fastmail.fm - mmm... Fastmail... |