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

@@ -20,8 +20,9 @@
*/
#include "usefull_macros.h"
#include <signal.h>
#include <sys/wait.h> // wait
#include <sys/wait.h> // wait
#include <sys/prctl.h> //prctl
#include <time.h> // time
#include "cmdlnopts.h"
#include "socket.h"
@@ -29,10 +30,13 @@
glob_pars *GP;
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 +60,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 +81,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

@@ -75,7 +75,6 @@ void try_connect(char *device){
fflush(stdout);
tty_init(device);
read_tty(tmpbuf, 4096); // clear rbuf
putlog("Connected to %s", device);
DBG("connected");
}

View File

@@ -390,9 +390,8 @@ 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 +406,6 @@ void openlogfile(char *name){
WARN(_("Can't open log file"));
return;
}
log_open_time = time(NULL);
logname = name;
}
@@ -417,15 +415,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);