Add some usefull functions from lib SOFA

This commit is contained in:
eddyem
2020-05-19 20:23:14 +03:00
parent 1a7a4db46e
commit 1250d0642b
20 changed files with 698 additions and 77 deletions

View File

@@ -22,17 +22,20 @@
#include <signal.h>
#include <sys/wait.h> // wait
#include <sys/prctl.h> //prctl
#include <time.h>
#include "cmdlnopts.h"
#include "socket.h"
// dome @ /dev/ttyS2
glob_pars *GP;
static pid_t childpid = 0;
void signals(int signo){
restore_console();
restore_tty();
putlog("exit with status %d", signo);
if(childpid){ // parent process
restore_tty();
putlog("exit with status %d", signo);
}
exit(signo);
}
@@ -56,13 +59,16 @@ int main(int argc, char **argv){
if(daemon(1, 0)){
ERR("daemon()");
}
time_t lastd = 0;
while(1){ // guard for dead processes
pid_t childpid = fork();
childpid = fork();
if(childpid){
putlog("create child with PID %d\n", childpid);
DBG("Created child with PID %d\n", childpid);
wait(NULL);
putlog("child %d died\n", childpid);
time_t t = time(NULL);
if(t - lastd > 600) // at least 10 minutes of work
putlog("child %d died\n", childpid);
lastd = t;
WARNX("Child %d died\n", childpid);
sleep(1);
}else{
@@ -74,9 +80,9 @@ int main(int argc, char **argv){
if(GP->device) try_connect(GP->device);
if(ping()){
putlog("Can't write command PING");
ERRX(_("Can't write command PING"));
}
putlog("Child %d connected to %s", getpid(), GP->device);
daemonize(GP->port);
signals(0); // newer reached
return 0;

View File

@@ -390,9 +390,9 @@ int str2double(double *num, const char *str){
return TRUE;
}
FILE *Flog = NULL; // log file descriptor
char *logname = NULL;
time_t log_open_time = 0;
static FILE *Flog = NULL; // log file descriptor
static char *logname = NULL;
/**
* Try to open log file
* if failed show warning message
@@ -407,7 +407,6 @@ void openlogfile(char *name){
WARN(_("Can't open log file"));
return;
}
log_open_time = time(NULL);
logname = name;
}
@@ -417,15 +416,6 @@ void openlogfile(char *name){
int putlog(const char *fmt, ...){
if(!Flog) return 0;
time_t t_now = time(NULL);
if(t_now - log_open_time > 86400){ // rotate log
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
fclose(Flog);
char newname[PATH_MAX];
snprintf(newname, PATH_MAX, "%s.old", logname);
if(rename(logname, newname)) WARN("rename()");
openlogfile(logname);
if(!Flog) return 0;
}
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
va_list ar;
va_start(ar, fmt);