move Main Loop to a separate clone process

This commit is contained in:
Sergey Popov 2011-12-18 12:07:40 +04:00
parent 52b69a0196
commit 7ecbe35d83

17
yasnd.c
View File

@ -22,6 +22,7 @@ char* phone_connection=NULL; // gammu phone connection type, for example: "at"
char* recipient_number=NULL; // recipient of sms alerts char* recipient_number=NULL; // recipient of sms alerts
bool loop_locked=false ; // flag for locking main loop while config re-reading bool loop_locked=false ; // flag for locking main loop while config re-reading
int failures_first=3 ; // count of failures while hosts checking, that triggers first SMS sending int failures_first=3 ; // count of failures while hosts checking, that triggers first SMS sending
pid_t mainloop_clone_pid; // pid of clone process, that containt main loop
void log_debug(const char *message,int verbosity) void log_debug(const char *message,int verbosity)
{ {
@ -158,8 +159,12 @@ void reset_pin(int pin_num)
outb (0, LPTPORT); outb (0, LPTPORT);
} }
void loop_function() int loop_function()
{ {
// set default signal handlers
signal(SIGTERM,SIG_DFL);
signal(SIGHUP,SIG_DFL);
//
for (int i=0;i<hosts_count;i++) for (int i=0;i<hosts_count;i++)
{ {
check_host(i,&hosts[i]); check_host(i,&hosts[i]);
@ -223,9 +228,9 @@ void signal_handler(int signum)
log_event("SIGHUP captured, daemon re-read config"); log_event("SIGHUP captured, daemon re-read config");
// lock Main Loop // lock Main Loop
loop_locked=true; loop_locked=true;
// kill Main Loop clone process
// STUB: need to kill all childs here kill(mainloop_clone_pid,SIGTERM);
waitpid(mainloop_clone_pid,NULL,__WCLONE);
// free all resources // free all resources
free_resources(); free_resources();
// Init IPC loop again // Init IPC loop again
@ -302,7 +307,9 @@ int main(void) {
while (1) { while (1) {
if (!loop_locked) if (!loop_locked)
{ {
loop_function(); unsigned char child_stack[16384];
mainloop_clone_pid=clone(loop_function,child_stack+8192,CLONE_VM,NULL);
waitpid(mainloop_clone_pid,NULL,__WCLONE);
sleep(60); /* wait 60 seconds */ sleep(60); /* wait 60 seconds */
// log_event("Exiting"); // log_event("Exiting");
// exit(EXIT_SUCCESS); // exit(EXIT_SUCCESS);