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;