mirror of
https://github.com/eddyem/small_tel.git
synced 2026-03-20 08:41:03 +03:00
103 lines
3.4 KiB
C
103 lines
3.4 KiB
C
/*
|
|
* This file is part of the weatherdaemon project.
|
|
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/wait.h> // wait
|
|
#include <sys/prctl.h> //prctl
|
|
#include <usefull_macros.h>
|
|
|
|
#include "cmdlnopts.h"
|
|
#include "sensors.h"
|
|
#include "server.h"
|
|
|
|
static pid_t childpid = 0;
|
|
|
|
void signals(int signo){
|
|
if(childpid){
|
|
LOGERR("Killed with status %d", signo);
|
|
closeplugins();
|
|
kill_servers();
|
|
usleep(1000); // let child close everything before dead
|
|
}else{
|
|
LOGERR("Main process exits with status %d", signo);
|
|
}
|
|
exit(signo);
|
|
}
|
|
|
|
static void getpipe(int _U_ signo){
|
|
WARNX("Get sigpipe!");
|
|
// TODO: check all sensors for disconnected one
|
|
signal(SIGPIPE, getpipe);
|
|
}
|
|
|
|
extern const char *__progname;
|
|
|
|
int main(int argc, char **argv){
|
|
glob_pars *GP = NULL;
|
|
sl_init();
|
|
signal(SIGTERM, signals); // kill (-15) - quit
|
|
signal(SIGHUP, SIG_IGN); // hup - ignore
|
|
signal(SIGINT, signals); // ctrl+C - quit
|
|
signal(SIGQUIT, signals); // ctrl+\ - quit
|
|
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
|
|
signal(SIGPIPE, getpipe); // socket disconnected
|
|
GP = parse_args(argc, argv);
|
|
if(!GP) ERRX("Error parsing args");
|
|
if(!GP->sockname) ERRX("Point command socket name");
|
|
if(GP->logfile){
|
|
sl_loglevel_e lvl = LOGLEVEL_ERR + GP->verb;
|
|
if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1;
|
|
DBG("Loglevel: %d", lvl);
|
|
if(!OPENLOG(GP->logfile, lvl, 1)) ERRX("Can't open log file");
|
|
LOGMSG("Started");
|
|
}
|
|
if(GP->pollt > 0){
|
|
if(!set_pollT((time_t)GP->pollt)) ERRX("Can't set polling time to %d seconds", GP->pollt);
|
|
}
|
|
int nopened = openplugins(GP->plugins, GP->nplugins);
|
|
if(nopened < 1){
|
|
LOGERR("No plugins found; exit!");
|
|
ERRX("Can't find any sensor plugin");
|
|
}
|
|
if(GP->nplugins && GP->nplugins != nopened) LOGWARN("Work without some plugins");
|
|
#ifndef EBUG
|
|
sl_check4running((char*)__progname, GP->pidfile);
|
|
while(1){ // guard for dead processes
|
|
childpid = fork();
|
|
if(childpid){
|
|
LOGDBG("create child with PID %d\n", childpid);
|
|
DBG("Created child with PID %d\n", childpid);
|
|
wait(NULL);
|
|
WARNX("Child %d died\n", childpid);
|
|
LOGWARN("Child %d died\n", childpid);
|
|
sleep(1);
|
|
}else{
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
|
|
break; // go out to normal functional
|
|
}
|
|
}
|
|
#endif
|
|
if(!start_servers(GP->port, GP->sockname)) ERRX("Can't run server's threads");
|
|
while(1);
|
|
//WARNX("TEST ends");
|
|
//signals(0);
|
|
return 0; // never reached
|
|
}
|