From 89041b9bb3fc47acdb3125919f8a50264acc6d4f Mon Sep 17 00:00:00 2001 From: Dean Jansa Date: Thu, 6 Oct 2005 20:10:20 +0000 Subject: Use hbeat interface which has been pulled out into its own set of files. --- Makefile | 2 +- qarsh.c | 87 ++++++++++++++++------------------------------------------------ 2 files changed, 22 insertions(+), 67 deletions(-) diff --git a/Makefile b/Makefile index 026b181..4bf184f 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ TARGETS := qarshd qarsh qacp btimed btimec all: $(TARGETS) qarshd: qarshd.c $(COMMON) -lxml2 -qarsh: qarsh.c $(COMMON) -lxml2 btime.c +qarsh: qarsh.c $(COMMON) -lxml2 btime.c hbeat.c qacp: qacp.c $(COMMON) -lxml2 btimed: btimed.c btimec: btimec.c btime.c diff --git a/qarsh.c b/qarsh.c index bd61c24..5780e95 100644 --- a/qarsh.c +++ b/qarsh.c @@ -25,21 +25,14 @@ #include "sockutil.h" #include "qarsh_packet.h" #include "btime.h" +#include "hbeat.h" #define QARSH_MINPORT 5010 -struct qarsh_hbeat_s { - int max_timeout; - enum {HOST_ALIVE, HOST_QUIET, HOST_TIMEOUT, HOST_REBOOT} rhost_state; - unsigned int last_rhost_btime; - time_t start_quiet_time; -}; - /* Globals */ int qarsh_fd = -1; /* The control connection to qarshd */ -char *qarshd_host; /* hostname of remote host */ -struct qarsh_hbeat_s qarsh_hbeat; +hbeat_t qarsh_hb; /* Heartbeat handle */ int signal_to_send = 0; int sigs_to_propogate[] = { SIGINT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2 }; sigset_t pselect_sigmask; @@ -94,7 +87,7 @@ sig_handler(int sig) void sig_alrm_handler(int sig) { - fprintf(stderr, "No heartbeat from %s\n", qarshd_host); + fprintf(stderr, "No heartbeat from remote host\n"); exit(1); } @@ -139,47 +132,6 @@ set_remote_user(char *user, char *group) } } -unsigned int -heartbeat(const char *host) -{ - unsigned int hbeat; - time_t current_time; - - /* User disabled heart beating */ - if (!qarsh_hbeat.max_timeout) { - return 1; - } - - hbeat = btime(host); - current_time = time(NULL); - - if (!hbeat && (qarsh_hbeat.rhost_state == HOST_QUIET)) { - if (current_time - qarsh_hbeat.start_quiet_time - > qarsh_hbeat.max_timeout) { - qarsh_hbeat.rhost_state = HOST_TIMEOUT; - return 0; - } - } else { - qarsh_hbeat.rhost_state = HOST_QUIET; - qarsh_hbeat.start_quiet_time = time(NULL); - } - - if (hbeat) { - if (qarsh_hbeat.last_rhost_btime == 0) { - qarsh_hbeat.last_rhost_btime = hbeat; - qarsh_hbeat.rhost_state = HOST_ALIVE; - qarsh_hbeat.start_quiet_time = 0; - } - - if (abs(hbeat - qarsh_hbeat.last_rhost_btime) > 5) { - qarsh_hbeat.rhost_state = HOST_REBOOT; - return 0; - } - } - - return 1; -} - int run_remote_cmd(char *cmdline) { @@ -283,8 +235,8 @@ run_remote_cmd(char *cmdline) &pselect_sigmask); if (nset == 0) { - if (!heartbeat(qarshd_host)) { - fprintf(stderr, "No heartbeat from %s\n", qarshd_host); + if (!hbeat(qarsh_hb)) { + fprintf(stderr, "No heartbeat from remote host"); /* Set our return packet as NULL so we exit * with unknown error. */ qp = NULL; @@ -303,7 +255,12 @@ run_remote_cmd(char *cmdline) signal_to_send = 0; } } else if (nset > 0) { - qarsh_hbeat.rhost_state = HOST_ALIVE; + /* We got traffic, poke the state into the hbeat because + * we may not have gotten the chance to call hbeat() above + * which would normally reset the state for us. + */ + hbeat_setstate(qarsh_hb, HOST_ALIVE); + if (nset && FD_ISSET(fileno(stdin), &testfds)) { bufsize = read(fileno(stdin), buf, 1024); if (bufsize > 0) { @@ -384,17 +341,12 @@ main(int argc, char *argv[]) struct sigaction sa; sigset_t sigmask; char *cp; + int max_timeout = 120; openlog("qarsh", LOG_PID, LOG_DAEMON); - /* Init our heartbeat info */ - qarsh_hbeat.max_timeout = 120; - qarsh_hbeat.rhost_state = HOST_ALIVE; - qarsh_hbeat.last_rhost_btime = 0; - qarsh_hbeat.start_quiet_time = 0; - if ((cp = getenv("QARSH_TIMEOUT")) != NULL) { - qarsh_hbeat.max_timeout = atoi(cp); + max_timeout = atoi(cp); } while ((c = getopt(argc, argv, "+p:l:g:t:")) != -1) { @@ -409,7 +361,7 @@ main(int argc, char *argv[]) port = atoi(optarg); break; case 't': - qarsh_hbeat.max_timeout = atoi(optarg); + max_timeout = atoi(optarg); break; case '?': default: @@ -452,8 +404,6 @@ main(int argc, char *argv[]) exit(1); } - qarshd_host = strdup(host); - memset(&sa, 0, sizeof sa); sigemptyset(&sigmask); sa.sa_mask = sigmask; @@ -461,7 +411,7 @@ main(int argc, char *argv[]) sa.sa_handler = sig_alrm_handler; sigaction(SIGALRM, &sa, NULL); - alarm(qarsh_hbeat.max_timeout); + alarm(max_timeout); qarsh_fd = connect_to_host(host, port); alarm(0); @@ -476,11 +426,16 @@ main(int argc, char *argv[]) return 127; } + qarsh_hb = hbeat_init(host, max_timeout); + if (!qarsh_hb) { + fprintf(stderr, "Could not init heartbeat to %s\n", host); + return 127; + } + set_remote_user(remuser, remgroup); ret = run_remote_cmd(args); close(qarsh_fd); free(args); - free(qarshd_host); return ret; } -- cgit