From 91fa5b3e6e7f50e54b3916667d33e28d89b9815d Mon Sep 17 00:00:00 2001 From: eddyem Date: Wed, 3 May 2017 10:30:24 +0300 Subject: [PATCH] add watchdog for socket's client --- allsky_logger/imfunctions.c | 28 +++++++++++++----------- allsky_logger/main.c | 1 + allsky_logger/socket.c | 39 ++++++++++++++++++++++------------ allsky_logger/usefull_macros.c | 2 +- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/allsky_logger/imfunctions.c b/allsky_logger/imfunctions.c index 61cf076..68e1374 100644 --- a/allsky_logger/imfunctions.c +++ b/allsky_logger/imfunctions.c @@ -141,21 +141,25 @@ static void gotodir(char *relpath){ // create directory structure * @return 1 if it's dark */ int fits_is_dark(char *name){ - int ret = 0; - mmapbuf *buf = My_mmap(name); - if(!buf) return 0; - char *key = memmem(buf->data, buf->len, "IMAGETYP", 8); - if(!key) goto light; - if(buf->len - (key - buf->data) < 30) goto light; - key += 9; - char *dark = memmem(key, 20, "'dark", 5); - if(dark){ - DBG("DARK!"); + fitsfile *fptr = NULL; + int status = 0, ret = 0; + if(fits_open_file(&fptr, name, READONLY, &status)){ + putlog("%s not a fits file!", name); + goto light; + } + // test image size + char imtype[FLEN_KEYWORD]; + if(fits_read_key(fptr, TSTRING, "IMAGETYP", imtype, NULL, &status)){ + putlog("Err: IMAGETYP not found"); + goto light; + } + if(strncmp(imtype, "dark", 4) == 0){ + putlog("Dark image"); ret = 1; } light: - My_munmap(buf); - DBG("LIGHT"); + status = 0; + if(fptr) fits_close_file(fptr, &status); return ret; } diff --git a/allsky_logger/main.c b/allsky_logger/main.c index 8437840..dc646b5 100644 --- a/allsky_logger/main.c +++ b/allsky_logger/main.c @@ -27,6 +27,7 @@ #include "socket.h" void signals(int signo){ + putlog("Main process died"); exit(signo); } diff --git a/allsky_logger/socket.c b/allsky_logger/socket.c index 36275f3..16e17d5 100644 --- a/allsky_logger/socket.c +++ b/allsky_logger/socket.c @@ -174,8 +174,8 @@ static int waittoread(int sock){ } break; }while(1); - if(FD_ISSET(sock, &fds)) return 1; if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed + if(FD_ISSET(sock, &fds)) return 1; return 0; } @@ -308,7 +308,15 @@ static void client_(char *FITSpath, int infd, int sock){ } DBG("Start polling"); putlog("Start polling"); + time_t wd_time = time(NULL); // watchdog time + time_t wd_period = 60; // watchdog period: 1 minute while(1){ + // check watchdog + if(time(NULL) - wd_time > wd_period){ + putlog("Watchdog triggered, need reconnection!"); + if(sock > 0) close(sock); + sock = try_to_connect(); + } poll_num = poll(&fds, 1, 1); if(poll_num == -1){ if (errno == EINTR) @@ -325,6 +333,7 @@ static void client_(char *FITSpath, int infd, int sock){ putlog("read() error"); ERR("read"); }else{ + putlog("file changed"); DBG("file changed"); usleep(100000); // wait a little for file changes if(dtime() - lastTstorage > minstoragetime){ @@ -347,8 +356,12 @@ static void client_(char *FITSpath, int infd, int sock){ } if(sock < 0) continue; int rd = waittoread(sock); - if(rd < 0) sock = -1; // signal that socket disconnected & we need to try to connect - if(rd < 1) continue; + if(rd < 0){ // error + close(sock); + sock = -1; // signal that socket disconnected & we need to try to connect + } + if(rd < 1) continue; // nothing to read + wd_time = time(NULL); // refresh timer - socket is alive size_t offset = 0; do{ rlc(offset); @@ -383,16 +396,6 @@ static void client_(char *FITSpath, int infd, int sock){ */ void daemonize(){ FNAME(); - char resolved_path[PATH_MAX]; - // get full path to FITS file - if(!realpath(G->filename, resolved_path)){ - putlog("realpath() error"); - ERR("realpath()"); - } - // open FITS file & add it to inotify - int fd = watch_fits(G->filename); - // CD to archive directory if user wants - test_path(G->cwd); minstoragetime = G->min_storage_time; // run fork before socket opening to prevent daemon's death if there's no network #ifndef EBUG @@ -416,7 +419,17 @@ void daemonize(){ } } #endif + char resolved_path[PATH_MAX]; + // get full path to FITS file + if(!realpath(G->filename, resolved_path)){ + putlog("realpath() error"); + ERR("realpath()"); + } + // CD to archive directory if user wants + test_path(G->cwd); int sock = try_to_connect(); + // open FITS file & add it to inotify + int fd = watch_fits(G->filename); client_(resolved_path, fd, sock); if(sock > 0) close(sock); signals(0); diff --git a/allsky_logger/usefull_macros.c b/allsky_logger/usefull_macros.c index 67ab386..c9b8c99 100644 --- a/allsky_logger/usefull_macros.c +++ b/allsky_logger/usefull_macros.c @@ -399,7 +399,7 @@ void openlogfile(char *name){ WARNX(_("Need filename")); return; } - green(_("Try to open log file %s in append mode\n"), name); + //green(_("Try to open log file %s in append mode\n"), name); if(!(Flog = fopen(name, "a"))){ WARN(_("Can't open log file")); return;