migrate main loop function call from clone to pthread

This commit is contained in:
Sergey Popov 2012-03-23 17:46:32 +04:00
parent 3d8d32cf1c
commit 80d7cb6e16

View File

@ -1,6 +1,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <getopt.h> #include <getopt.h>
#include <confuse.h> #include <confuse.h>
@ -19,7 +20,7 @@ bool loop_locked=false; // flag for locking main loop while config re-reading
long failures_first=0; // count of failures while hosts checking, that triggers SMS sending long failures_first=0; // count of failures while hosts checking, that triggers SMS sending
long failures_second=0; // count of failures while hosts checking, that triggers host's reset long failures_second=0; // count of failures while hosts checking, that triggers host's reset
long failures_third=0; // count of failures while hosts checking, that clean failure statistics long failures_third=0; // count of failures while hosts checking, that clean failure statistics
pid_t mainloop_clone_pid; // pid of clone process, that containt main loop pthread_t mainloop_handle; // handle of main loop thread
void* allocate_memory(int bytes) void* allocate_memory(int bytes)
{ {
@ -179,12 +180,8 @@ void check_host(int num,host_decl* host)
host->helper_pid=pid; host->helper_pid=pid;
} }
int loop_function() void* loop_function(void* param)
{ {
// 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]);
@ -241,6 +238,7 @@ int loop_function()
continue; continue;
} }
} }
return NULL;
} }
void signal_handler(int signum) void signal_handler(int signum)
@ -249,9 +247,8 @@ void signal_handler(int signum)
{ {
// lock Main Loop // lock Main Loop
loop_locked=true; loop_locked=true;
// kill Main Loop clone process // kill Main Loop thread
kill(mainloop_clone_pid,SIGTERM); pthread_cancel(mainloop_handle);
waitpid(mainloop_clone_pid,NULL,__WCLONE);
// Stop IPC loop and remove IPC queue // Stop IPC loop and remove IPC queue
ipc_free(); ipc_free();
// free config structure // free config structure
@ -374,11 +371,13 @@ int main(int argc, char *argv[])
while (1) { while (1) {
if (!loop_locked) if (!loop_locked)
{ {
unsigned char child_stack[16384]; if(pthread_create(&mainloop_handle,NULL,loop_function, NULL) != 0)
mainloop_clone_pid=clone(loop_function,child_stack+8192,CLONE_VM,NULL); {
log_event("Thread creation failed");
}
sleep(60); /* wait 60 seconds */ sleep(60); /* wait 60 seconds */
// wait for clone process to prevent zombie // wait for the completion of thread
waitpid(mainloop_clone_pid,NULL,__WCLONE); pthread_join(mainloop_handle,NULL);
// log_event("Exiting"); // log_event("Exiting");
// exit(EXIT_SUCCESS); // exit(EXIT_SUCCESS);
} }