/* * This file is part of the weatherdaemon project. * Copyright 2025 Edward V. Emelianov . * * 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 . */ #include #include #include #include // wait #include //prctl #include #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); } 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 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"); } 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 }