/* * This file is part of the Snippets project. * Copyright 2024 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 #include // wait #include //prctl #include #include "args.h" #include "fitshdr.h" #include "server.h" #include "mount.h" #include "stellarium.h" static pid_t childpid = -1; // PID of child process static parameters_t *G = NULL; void signals(int sig){ if(childpid){ // single or parent process if(G->pidfile) unlink(G->pidfile); // remove pidfile if(childpid > 0) kill(childpid, SIGTERM); } if(sig){ signal(sig, SIG_IGN); DBG("Get signal %d, quit.\n", sig); } if(childpid > 0){ // parent process LOGERR("PID %d exit with status %d", getpid(), sig); isrunning = false; }else{ LOGWARN("Child %d died with %d", getpid(), sig); server_stop(); stellarium_stop(); } DBG("EXIT"); } int main(int argc, char **argv){ sl_init(); G = parse_cmdline(&argc, &argv); if(!G) return 1; sl_loglevel_e lvl = G->verbose + LOGLEVEL_ERR; if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1; DBG("verb: %d, level: %d", G->verbose, lvl); if(!set_header_name(G->crdsfile)) return 1; if(G->sleept < 1){ WARNX("Sleeping time sould be positive value (%d)", G->sleept); return 2; } if(!server_setsleept(G->sleept)){ WARNX("Can't set sleep time to %d", G->sleept); return 3; } if(G->emulation) set_emulation_mode(); else if(!mount_set_dev(G->device, G->serspeed, G->sertmout)){ WARNX("Can't open device %s @ %d", G->device, G->serspeed); return 4; } if(!mount_set_name(G->mountname)){ WARNX("Can't set mount name to %s", G->mountname); return 5; } if(G->parkA){ double A = 0.; if(!str2coord(G->parkA, &A) || !mount_setParkAz(A)) WARNX("Can't set parking azimuth to %g", A); } if(G->parkZ){ double Z = 0.; if(!str2coord(G->parkZ, &Z) || !mount_setParkZD(Z)) WARNX("Can't set parking zenith distance to %g", Z); } if(fabs(G->DUT1) > FLT_EPSILON || fabs(G->polarx) > FLT_EPSILON || fabs(G->polary) > FLT_EPSILON){ almDut_t par = {.DUT1 = G->DUT1, .px = G->polarx, .py = G->polary}; setDUT(&par); } if(G->altitude || G->longitude || G->latitude){ placeData_t place; double val; getPlaceData(&place); if(G->altitude && sl_str2d(&val, G->altitude)) place.salt = val; if(G->longitude && str2coord(G->longitude, &val)) place.slong = DEG2RAD(val); if(G->latitude && str2coord(G->latitude, &val)) place.slat = DEG2RAD(val); setPlaceData(&place); } signal(SIGTERM, signals); signal(SIGINT, signals); signal(SIGQUIT, signals); signal(SIGTSTP, SIG_IGN); signal(SIGHUP, SIG_IGN); //signal(SIGHUP, signals); // re-read config file sl_check4running((char*)__progname, G->pidfile); if(G->logfile) OPENLOG(G->logfile, lvl, 1); DBG("Started"); LOGMSG("Started, master PID=%d", getpid()); #ifndef EBUG time_t lastd = 0; while(isrunning){ // guard for dead processes childpid = fork(); if(childpid < 0){ LOGERR("fork() returns error"); sleep(5); continue; } if(childpid){ DBG("Created child with PID %d\n", childpid); wait(NULL); time_t t = time(NULL); if(t - lastd > 600){ // at least 10 minutes of work LOGERR("Child %d died\n", childpid); } lastd = t; WARNX("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 } } if(!isrunning) return 0; #endif server_sock_t sockt = { .cmd_isunix = G->isunix, .cmdnode = G->cmdnode, .stellport = G->port, .maxclients = G->maxclients, }; if(!server_check(&sockt)){ LOGERR("Can't run servers"); ERRX("Can't run servers"); } DBG("Run server"); server_run(); DBG("Server died"); return 0; }