From 83a465a4c847f32bcf71ac8e759c7f6b6a3de099 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Asaba Date: Wed, 20 Jun 2007 08:23:26 +0000 Subject: [PATCH] Fix signal handling for failover. Failover operation is executed in signal safe places. --- main.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 81992b4..5d8fc9f 100644 --- a/main.c +++ b/main.c @@ -46,6 +46,16 @@ #include "version.h" +#define CHECK_FAILOVER_REQUEST \ + do \ + { \ + if (failover_request) \ + { \ + failover(failover_request); \ + failover_request = 0; \ + } \ + } while (0) + #define PGPOOLMAXLITSENQUEUELENGTH 10000 static void daemonize(void); static int read_pid_file(void); @@ -54,6 +64,7 @@ static pid_t fork_a_child(int unix_fd, int inet_fd); static int create_unix_domain_socket(void); static int create_inet_domain_socket(const char *hostname); static void myexit(int code); +static void failover(int sig); static RETSIGTYPE exit_handler(int sig); static RETSIGTYPE reap_handler(int sig); @@ -86,6 +97,7 @@ static int stop_sig = SIGTERM; /* stopping signal default value */ static int switch_over_sig = SIGUSR1; /* switch over signal default value */ static int health_check_timer_expired; /* non 0 if health check timer expired */ +static volatile int failover_request = 0; int myargc; char **myargv; @@ -311,6 +323,8 @@ int main(int argc, char **argv) */ for (;;) { + CHECK_FAILOVER_REQUEST; + /* do we need health checking for PostgreSQL? */ if (!degenerated && pool_config.health_check_period > 0) { @@ -341,11 +355,11 @@ int main(int argc, char **argv) { if (sts == -1) { - failover_handler(SIGUSR1); /* master down */ + failover(SIGUSR1); /* master down */ } else if (sts == -2) { - failover_handler(SIGUSR2); /* secondary down */ + failover(SIGUSR2); /* secondary down */ } } @@ -359,6 +373,7 @@ int main(int argc, char **argv) while (sleep_time > 0) { sleep_time = sleep(sleep_time); + CHECK_FAILOVER_REQUEST; } } else @@ -753,7 +768,6 @@ static RETSIGTYPE exit_handler(int sig) myexit(0); } - /* * handle SIGUSR1/SIGUSR2 (backend connection error, fail over request, if possible) * @@ -761,6 +775,14 @@ static RETSIGTYPE exit_handler(int sig) * if sig == SIGUSR2, we assume that the secondary has been down. */ static RETSIGTYPE failover_handler(int sig) +{ + failover_request = sig; +} + +/* + * Process failover request + */ +static void failover(int sig) { int i; int replication = 0; -- 2.39.5