int main(int argc, char *argv[]) { int rtn; signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_IGN); signal(SIGINT, wd_exit); signal(SIGQUIT, wd_exit); signal(SIGTERM, wd_exit); signal(SIGPIPE, SIG_IGN); mypid = getpid(); rtn = pool_init_config(); pool_config->recovery_user = "******"; pool_config->trusted_servers = "paris"; pool_config->delegate_IP = "192.168.100.99"; pool_config->pgpool2_hostname = "vm1"; pool_config->port = 5432; pool_config->wd_port = 9999; pool_config->other_wd->num_wd = 2; strcpy(pool_config->other_wd->wd_info[0].hostname, "vm2"); pool_config->other_wd->wd_info[0].pgpool_port = 5432; pool_config->other_wd->wd_info[0].wd_port = 9999; pool_config->other_wd->wd_info[0].status = WD_INIT; strcpy(pool_config->other_wd->wd_info[1].hostname, "paris"); pool_config->other_wd->wd_info[1].pgpool_port = 5432; pool_config->other_wd->wd_info[1].wd_port = 9999; pool_config->other_wd->wd_info[1].status = WD_INIT; pool_config->ping_path = "/bin"; pool_config->ifconfig_path = "/sbin"; pool_config->if_up_cmd = "ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0"; pool_config->if_down_cmd = "ifconfig eth0:0 down"; pool_config->wd_interval = 3; pool_config->wd_life_point = 1; wd_init(); wd_child(1); for (;;) { wdlist_dump(); sleep(5); } }
pid_t wd_main(int fork_wait_time) { int i; if (!pool_config->use_watchdog) return 0; /* check pool_config data */ wd_check_config(); /* initialize */ wd_init(); wd_ppid = getpid(); /* launch child process */ child_pid = wd_child(1); if (!strcmp(pool_config->wd_lifecheck_method, MODE_HEARTBEAT)) { for (i = 0; i < pool_config->num_hb_if; i++) { /* heartbeat receiver process */ hb_receiver_pid[i] = wd_hb_receiver(1, &(pool_config->hb_if[i])); /* heartbeat sender process */ hb_sender_pid[i] = wd_hb_sender(1, &(pool_config->hb_if[i])); } } /* fork lifecheck process*/ lifecheck_pid = fork_a_lifecheck(fork_wait_time); return lifecheck_pid; }
int wd_reaper_watchdog(pid_t pid, int status) { int i; /* watchdog lifecheck process exits */ if (pid == lifecheck_pid) { if (WIFSIGNALED(status)) pool_debug("watchdog lifecheck process %d exits with status %d by signal %d", pid, status, WTERMSIG(status)); else pool_debug("watchdog lifecheck process %d exits with status %d", pid, status); lifecheck_pid = fork_a_lifecheck(1); if (lifecheck_pid < 0) { pool_error("wd_reaper: fork a watchdog lifecheck process failed"); return 0; } pool_log("fork a new watchdog lifecheck pid %d", lifecheck_pid); } /* watchdog child process exits */ else if (pid == child_pid) { if (WIFSIGNALED(status)) pool_debug("watchdog child process %d exits with status %d by signal %d", pid, status, WTERMSIG(status)); else pool_debug("watchdog child process %d exits with status %d", pid, status); child_pid = wd_child(1); if (child_pid < 0) { pool_error("wd_reaper: fork a watchdog child process failed"); return 0; } pool_log("fork a new watchdog child pid %d", child_pid); } /* receiver/sender process exits */ else { for (i = 0; i < pool_config->num_hb_if; i++) { if (pid == hb_receiver_pid[i]) { if (WIFSIGNALED(status)) pool_debug("watchdog heartbeat receiver process %d exits with status %d by signal %d", pid, status, WTERMSIG(status)); else pool_debug("watchdog heartbeat receiver process %d exits with status %d", pid, status); hb_receiver_pid[i] = wd_hb_receiver(1, pool_config->hb_if[i]); if (hb_receiver_pid[i] < 0) { pool_error("wd_reaper: fork a watchdog heartbeat receiver process failed"); return 0; } pool_log("fork a new watchdog heartbeat receiver: pid %d", hb_receiver_pid[i]); break; } else if (pid == hb_sender_pid[i]) { if (WIFSIGNALED(status)) pool_debug("watchdog heartbeat sender process %d exits with status %d by signal %d", pid, status, WTERMSIG(status)); else pool_debug("watchdog heartbeat sender process %d exits with status %d", pid, status); hb_sender_pid[i] = wd_hb_sender(1, pool_config->hb_if[i]); if (hb_sender_pid[i] < 0) { pool_error("wd_reaper: fork a watchdog heartbeat sender process failed"); return 0; } pool_log("fork a new watchdog heartbeat sender: pid %d", hb_sender_pid[i]); break; } } } return 1; }
pid_t wd_main(int fork_wait_time) { int status = WD_INIT; int i; if (!pool_config->use_watchdog) { return 0; } /* check pool_config data */ status = wd_check_config(); if (status != WD_OK) { pool_error("watchdog: wd_check_config failed"); return 0; } /* initialize */ status = wd_init(); if (status != WD_OK) { pool_error("watchdog: wd_init failed"); return 0; } wd_ppid = getpid(); /* launch child process */ child_pid = wd_child(1); if (child_pid < 0 ) { pool_error("launch wd_child failed"); return 0; } if (!strcmp(pool_config->wd_lifecheck_method, MODE_HEARTBEAT)) { for (i = 0; i < pool_config->num_hb_if; i++) { /* heartbeat receiver process */ hb_receiver_pid[i] = wd_hb_receiver(1, pool_config->hb_if[i]); if (hb_receiver_pid[i] < 0 ) { pool_error("launch wd_hb_receiver failed"); return hb_receiver_pid[i]; } /* heartbeat sender process */ hb_sender_pid[i] = wd_hb_sender(1, pool_config->hb_if[i]); if (hb_sender_pid[i] < 0 ) { pool_error("launch wd_hb_sender failed"); return hb_sender_pid[i]; } } } /* fork lifecheck process*/ lifecheck_pid = fork_a_lifecheck(fork_wait_time); if (lifecheck_pid < 0 ) { pool_error("launch lifecheck process failed"); return 0; } return lifecheck_pid; }
/* restart watchdog process specified by pid */ int wd_reaper_watchdog(pid_t pid, int status) { int i; /* watchdog lifecheck process exits */ if (pid == lifecheck_pid) { if (WIFSIGNALED(status)) ereport(DEBUG1, (errmsg("watchdog lifecheck process with PID:%d exits with status %d by signal %d", pid, status, WTERMSIG(status)))); else ereport(DEBUG1, (errmsg("watchdog lifecheck process with PID:%d exits with status %d", pid, status))); lifecheck_pid = fork_a_lifecheck(1); ereport(LOG, (errmsg("fork a new watchdog lifecheck pid %d", lifecheck_pid))); } /* watchdog child process exits */ else if (pid == child_pid) { if (WIFSIGNALED(status)) ereport(DEBUG1, (errmsg("watchdog child process with PID:%d exits with status %d by signal %d", pid, status,WTERMSIG(status)))); else ereport(DEBUG1, (errmsg("watchdog child process with PID:%d exits with status %d", pid, status))); child_pid = wd_child(1); ereport(LOG, (errmsg("fork a new watchdog child pid %d", child_pid))); } /* receiver/sender process exits */ else { for (i = 0; i < pool_config->num_hb_if; i++) { if (pid == hb_receiver_pid[i]) { if (WIFSIGNALED(status)) ereport(DEBUG1, (errmsg("watchdog heartbeat receiver process with PID:%d exits with status %d by signal %d", pid, status, WTERMSIG(status)))); else ereport(DEBUG1, (errmsg("watchdog heartbeat receiver process with PID:%d exits with status %d", pid, status))); hb_receiver_pid[i] = wd_hb_receiver(1, &(pool_config->hb_if[i])); ereport(LOG, (errmsg("fork a new watchdog heartbeat receiver with pid %d", hb_receiver_pid[i]))); break; } else if (pid == hb_sender_pid[i]) { if (WIFSIGNALED(status)) ereport(DEBUG1, (errmsg("watchdog heartbeat sender process with PID:%d exits with status %d by signal %d", pid, status, WTERMSIG(status)))); else ereport(DEBUG1, (errmsg("watchdog heartbeat sender process with PID:%d exits with status %d", pid, status))); hb_sender_pid[i] = wd_hb_sender(1, &(pool_config->hb_if[i])); ereport(LOG, (errmsg("fork a new watchdog heartbeat sender with PID:%d", hb_sender_pid[i]))); break; } } } return 1; }