add watchdog for socket's client

This commit is contained in:
eddyem 2017-05-03 10:30:24 +03:00
parent e7aaa20af0
commit 91fa5b3e6e
4 changed files with 44 additions and 26 deletions

View File

@ -141,21 +141,25 @@ static void gotodir(char *relpath){ // create directory structure
* @return 1 if it's dark * @return 1 if it's dark
*/ */
int fits_is_dark(char *name){ int fits_is_dark(char *name){
int ret = 0; fitsfile *fptr = NULL;
mmapbuf *buf = My_mmap(name); int status = 0, ret = 0;
if(!buf) return 0; if(fits_open_file(&fptr, name, READONLY, &status)){
char *key = memmem(buf->data, buf->len, "IMAGETYP", 8); putlog("%s not a fits file!", name);
if(!key) goto light; goto light;
if(buf->len - (key - buf->data) < 30) goto light; }
key += 9; // test image size
char *dark = memmem(key, 20, "'dark", 5); char imtype[FLEN_KEYWORD];
if(dark){ if(fits_read_key(fptr, TSTRING, "IMAGETYP", imtype, NULL, &status)){
DBG("DARK!"); putlog("Err: IMAGETYP not found");
goto light;
}
if(strncmp(imtype, "dark", 4) == 0){
putlog("Dark image");
ret = 1; ret = 1;
} }
light: light:
My_munmap(buf); status = 0;
DBG("LIGHT"); if(fptr) fits_close_file(fptr, &status);
return ret; return ret;
} }

View File

@ -27,6 +27,7 @@
#include "socket.h" #include "socket.h"
void signals(int signo){ void signals(int signo){
putlog("Main process died");
exit(signo); exit(signo);
} }

View File

@ -174,8 +174,8 @@ static int waittoread(int sock){
} }
break; break;
}while(1); }while(1);
if(FD_ISSET(sock, &fds)) return 1;
if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed
if(FD_ISSET(sock, &fds)) return 1;
return 0; return 0;
} }
@ -308,7 +308,15 @@ static void client_(char *FITSpath, int infd, int sock){
} }
DBG("Start polling"); DBG("Start polling");
putlog("Start polling"); putlog("Start polling");
time_t wd_time = time(NULL); // watchdog time
time_t wd_period = 60; // watchdog period: 1 minute
while(1){ 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); poll_num = poll(&fds, 1, 1);
if(poll_num == -1){ if(poll_num == -1){
if (errno == EINTR) if (errno == EINTR)
@ -325,6 +333,7 @@ static void client_(char *FITSpath, int infd, int sock){
putlog("read() error"); putlog("read() error");
ERR("read"); ERR("read");
}else{ }else{
putlog("file changed");
DBG("file changed"); DBG("file changed");
usleep(100000); // wait a little for file changes usleep(100000); // wait a little for file changes
if(dtime() - lastTstorage > minstoragetime){ if(dtime() - lastTstorage > minstoragetime){
@ -347,8 +356,12 @@ static void client_(char *FITSpath, int infd, int sock){
} }
if(sock < 0) continue; if(sock < 0) continue;
int rd = waittoread(sock); int rd = waittoread(sock);
if(rd < 0) sock = -1; // signal that socket disconnected & we need to try to connect if(rd < 0){ // error
if(rd < 1) continue; 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; size_t offset = 0;
do{ do{
rlc(offset); rlc(offset);
@ -383,16 +396,6 @@ static void client_(char *FITSpath, int infd, int sock){
*/ */
void daemonize(){ void daemonize(){
FNAME(); 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; minstoragetime = G->min_storage_time;
// run fork before socket opening to prevent daemon's death if there's no network // run fork before socket opening to prevent daemon's death if there's no network
#ifndef EBUG #ifndef EBUG
@ -416,7 +419,17 @@ void daemonize(){
} }
} }
#endif #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(); int sock = try_to_connect();
// open FITS file & add it to inotify
int fd = watch_fits(G->filename);
client_(resolved_path, fd, sock); client_(resolved_path, fd, sock);
if(sock > 0) close(sock); if(sock > 0) close(sock);
signals(0); signals(0);

View File

@ -399,7 +399,7 @@ void openlogfile(char *name){
WARNX(_("Need filename")); WARNX(_("Need filename"));
return; 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"))){ if(!(Flog = fopen(name, "a"))){
WARN(_("Can't open log file")); WARN(_("Can't open log file"));
return; return;