From 14a9324ec5c19d1d4a68fca91f3ded80c35957e0 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Mon, 3 Jun 2024 16:38:39 +0300 Subject: [PATCH] initial commit --- CMakeLists.txt | 96 +++++++++++++++++ Dummy_loccorr/CMakeLists.txt | 15 +++ Dummy_loccorr/main.c | 28 +++++ Dummy_telescope/CMakeLists.txt | 15 +++ Dummy_telescope/main.c | 28 +++++ cmdlnopts.c | 99 ++++++++++++++++++ cmdlnopts.h | 46 ++++++++ corrdevice.h | 23 ++++ loccorr.cflags | 1 + loccorr.config | 6 ++ loccorr.creator | 1 + loccorr.creator.user | 186 +++++++++++++++++++++++++++++++++ loccorr.cxxflags | 1 + loccorr.files | 10 ++ loccorr.includes | 1 + loccorrbase.h | 23 ++++ main.c | 128 +++++++++++++++++++++++ 17 files changed, 707 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Dummy_loccorr/CMakeLists.txt create mode 100644 Dummy_loccorr/main.c create mode 100644 Dummy_telescope/CMakeLists.txt create mode 100644 Dummy_telescope/main.c create mode 100644 cmdlnopts.c create mode 100644 cmdlnopts.h create mode 100644 corrdevice.h create mode 100644 loccorr.cflags create mode 100644 loccorr.config create mode 100644 loccorr.creator create mode 100644 loccorr.creator.user create mode 100644 loccorr.cxxflags create mode 100644 loccorr.files create mode 100644 loccorr.includes create mode 100644 loccorrbase.h create mode 100644 main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2aa913e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,96 @@ +cmake_minimum_required(VERSION 3.20) +set(PROJ loccorr) +set(MAJOR_VERSION "0") +set(MID_VERSION "0") +set(MINOR_VERSION "1") +set(VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}") + +project(${PROJ} VERSION ${VERSION} LANGUAGES C) +message("\n\nPlease, check current version: ${VERSION}\n\n") + +# list of options +# three-state DEBUG option +set(DEBUG OFF CACHE STRING "Debug mode: on/part/off (full debug, release with debug info, full release)") +set_property(CACHE DEBUG PROPERTY STRINGS PART ON OFF) +#option(DEBUG "Compile in debug mode" OFF) +option(LCDUMMY "Dummy local corrector plugin" ON) +option(TELDUMMY "Dummy telescope plugin" ON) + +# here is one of two variants: all .c in directory or .c files in list +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES) + +# default flags +message("\n\nDIR: ${CMAKE_CURRENT_SOURCE_DIR}\n\n") +# fmacro-prefix-map: https://stackoverflow.com/a/53848526/1965803 - replace full path with short +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W -Wextra -fno-builtin-strlen -fmacro-prefix-map=\"${CMAKE_CURRENT_SOURCE_DIR}\"/=\"\" ") + +message("Check install dir prefix: ${CMAKE_INSTALL_PREFIX}") + +add_definitions(-D_XOPEN_SOURCE=1234 -D_DEFAULT_SOURCE -D_GNU_SOURCE + -DPACKAGE_VERSION=\"${VERSION}\" -DMINOR_VERSION=\"${MINOR_VERSION}\" + -DMID_VERSION=\"${MID_VERSION}\" -DMAJOR_VERSION=\"${MAJOR_VESION}\") + +set(CMAKE_COLOR_MAKEFILE ON) + +# cmake -DDEBUG=on/part/off -> debug / release with debug info / release +if(DEBUG STREQUAL "PART") # part + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -march=native -fdata-sections -ffunction-sections -flto=auto") + add_definitions(-DEBUG) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -flto=auto") + set(CMAKE_BUILD_TYPE RelWithDebInfo) + set(CMAKE_VERBOSE_MAKEFILE "ON") +elseif(DEBUG) # on + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Og -g3 -ggdb -Werror") + add_definitions(-DEBUG) + set(CMAKE_BUILD_TYPE Debug) + set(CMAKE_VERBOSE_MAKEFILE "ON") +else() # off + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -march=native -fdata-sections -ffunction-sections -flto=auto") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -flto=auto") + set(CMAKE_BUILD_TYPE Release) +endif() + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") +#find_package(CFITSIO REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(${PROJ} REQUIRED usefull_macros improc ccdcapture) + +include(FindOpenMP) +if(OPENMP_FOUND) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") + add_definitions(-DOMP_FOUND) +endif() + +# Dummy plugins +if(LCDUMMY) + add_subdirectory(Dummy_loccorr) +endif() +if(TELDUMMY) + add_subdirectory(Dummy_telescope) +endif() + +# exe & lib files; install +include(GNUInstallDirs) +#add_library(${PROJLIB} SHARED ${LIBSRC}) +add_executable(${PROJ} ${SOURCES}) +target_link_libraries(${PROJ} ${${PROJ}_LIBRARIES} -lm ${CMAKE_DL_LIBS}) +#target_link_libraries(${PROJLIB} ${CFITSIO_LIBRARIES} ${${PROJLIB}_LIBRARIES}) +include_directories(${${PROJ}_INCLUDE_DIRS} .) +link_directories(${${PROJ}_LIBRARY_DIRS} ) +set(PCFILE "${CMAKE_BINARY_DIR}/${PROJLIB}.pc") +#configure_file("${PROJLIB}.pc.in" ${PCFILE} @ONLY) +#set_target_properties(${PROJLIB} PROPERTIES VERSION ${VERSION}) +#set_target_properties(${PROJLIB} PROPERTIES PUBLIC_HEADER ${LIBHEADER}) + +# Installation of the program +include(GNUInstallDirs) +install(TARGETS ${PROJ} + #LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) +#install(TARGETS ${PROJ} DESTINATION "bin") +#install(TARGETS ${PROJLIB} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +# PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +#install(TARGETS PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +#install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) diff --git a/Dummy_loccorr/CMakeLists.txt b/Dummy_loccorr/CMakeLists.txt new file mode 100644 index 0000000..272dac7 --- /dev/null +++ b/Dummy_loccorr/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.20) +set(EXTLIB lcdummy) + +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(${EXTLIB} REQUIRED usefull_macros) + +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC) +include_directories(${${EXTLIB}_INCLUDE_DIRS} ..) +link_directories(${${EXTLIB}_LIBRARY_DIRS}) + +add_library(${EXTLIB} SHARED ${SRC}) +target_link_libraries(${EXTLIB} ${${EXTLIB}_LIBRARIES} -fPIC) +install(TARGETS ${EXTLIB} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/Dummy_loccorr/main.c b/Dummy_loccorr/main.c new file mode 100644 index 0000000..e4d4fc4 --- /dev/null +++ b/Dummy_loccorr/main.c @@ -0,0 +1,28 @@ +/* + * This file is part of the loccorr 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 "corrdevice.h" +#include + +void test(){ + printf("Dummy local corrector tested\n"); +} + +__attribute__ ((visibility("default"))) corrdev_t device = { + .testdevice = test, +}; diff --git a/Dummy_telescope/CMakeLists.txt b/Dummy_telescope/CMakeLists.txt new file mode 100644 index 0000000..bf830cd --- /dev/null +++ b/Dummy_telescope/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.20) +set(EXTLIB teldummy) + +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(${EXTLIB} REQUIRED usefull_macros) + +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC) +include_directories(${${EXTLIB}_INCLUDE_DIRS} ..) +link_directories(${${EXTLIB}_LIBRARY_DIRS}) + +add_library(${EXTLIB} SHARED ${SRC}) +target_link_libraries(${EXTLIB} ${${EXTLIB}_LIBRARIES} -fPIC) +install(TARGETS ${EXTLIB} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/Dummy_telescope/main.c b/Dummy_telescope/main.c new file mode 100644 index 0000000..563936b --- /dev/null +++ b/Dummy_telescope/main.c @@ -0,0 +1,28 @@ +/* + * This file is part of the loccorr 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 "corrdevice.h" +#include + +void test(){ + printf("Dummy telescope tested\n"); +} + +__attribute__ ((visibility("default"))) corrdev_t device = { + .testdevice = test, +}; diff --git a/cmdlnopts.c b/cmdlnopts.c new file mode 100644 index 0000000..cf6f937 --- /dev/null +++ b/cmdlnopts.c @@ -0,0 +1,99 @@ +/* + * This file is part of the loccorr 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 "cmdlnopts.h" + +static int help; + +// DEFAULTS +// default global parameters +glob_pars Glob_pars = { + .captkey = -1, +}; + +// need this to proper work with only-long args +//#define NA (-__COUNTER__) +// TODO: fix the bug in usefull_macros first!!! +#define NA (0) + +/* + * Define command line options by filling structure: + * name has_arg flag val type argptr help +*/ +myoption cmdlnopts[] = { + {"lcplugin",NEED_ARG, NULL, 'L', arg_string, APTR(&Glob_pars.lcplugin), "local corrector plugin (e.g liblcesprif.so)"}, + {"telplugin", NEED_ARG, NULL, 'T', arg_string, APTR(&Glob_pars.telplugin), "telescope plugin (e.g. libbtaacs.so)"}, + {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"}, + {"verbose", NO_ARGS, NULL, 'V', arg_none, APTR(&Glob_pars.verbose), "verbose level (-V - messages, -VV - debug, -VVV - all shit)"}, + {"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&Glob_pars.logfile), "logging file name (if run as server)"}, + {"fitshead",NEED_ARG, NULL, 'H', arg_string, APTR(&Glob_pars.lcheader), "output FITS header with loccorr information"}, + {"path", NEED_ARG, NULL, NA, arg_string, APTR(&Glob_pars.path), "UNIX socket name (command socket)"}, + {"port", NEED_ARG, NULL, 'p', arg_string, APTR(&Glob_pars.port), "INET command socket port (can't be both UNIX and INET)"}, + {"imageport",NEED_ARG, NULL, NA, arg_string, APTR(&Glob_pars.jpegport), "INET JPEG socket port"}, + {"outfile", NEED_ARG, NULL, 'o', arg_string, APTR(&Glob_pars.jpegfile), "output JPEG image file name"}, + {"shmkey", NEED_ARG, NULL, 'k', arg_int, APTR(&Glob_pars.captkey), "image SHM key of capture process"}, + {"cport", NEED_ARG, NULL, NA, arg_string, APTR(&Glob_pars.captcmdport),"INET command socket port of capture process"}, + {"cpath", NEED_ARG, NULL, NA, arg_string, APTR(&Glob_pars.captpath), "UNIX command socket path or address of image capture server"}, + {"ciport", NEED_ARG, NULL, NA, arg_string, APTR(&Glob_pars.captimport),"INET image socket port of capture process"}, + {"unix", NO_ARGS, NULL, 'U', arg_int, APTR(&Glob_pars.cisun), "capture command channel is UNIX socket"}, + //{"", NEED_ARG, NULL, '', arg_double, APTR(&Glob_pars.), N_("")}, + end_option +}; + +/** + * Parse command line options and return dynamically allocated structure + * to global parameters + * @param argc - copy of argc from main + * @param argv - copy of argv from main + * @return allocated structure with global parameters + */ +void parse_args(int argc, char **argv){ + // format of help: "Usage: progname [args]\n" + change_helpstring("Version: " PACKAGE_VERSION "\nTo restart server kill it with SIGUSR1\n\nUsage: %s [args]\n\twhere args are:\n"); + // parse arguments + parseargs(&argc, &argv, cmdlnopts); + if(help) showhelp(-1, cmdlnopts); + DBG("argc=%d", argc); + if(argc > 0){ + WARNX("%d unused parameter[s]:", argc); + for(int i = 0; i < argc; ++i) + fprintf(stderr, "%4d: %s\n", i, argv[i]); + WARNX("Fix command line and run again"); + exit(-1); + } +} + +/** + * @brief verbose - print additional messages depending of Glob_pars.verbose + * @param levl - message level + * @param fmt - message + */ +void verbose(int levl, const char *fmt, ...){ + va_list ar; + if(levl > Glob_pars.verbose) return; + //printf("%s: ", __progname); + va_start(ar, fmt); + vprintf(fmt, ar); + va_end(ar); + printf("\n"); +} diff --git a/cmdlnopts.h b/cmdlnopts.h new file mode 100644 index 0000000..1f38ea5 --- /dev/null +++ b/cmdlnopts.h @@ -0,0 +1,46 @@ +/* + * This file is part of the loccorr 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 . + */ + +#pragma once +#include + +/* + * here are some typedef's for global data + */ +typedef struct{ + char *lcplugin; // local corrector plugin + char *telplugin; // telescope plugin + char *logfile; // when run as server log here + char *path; // UNIX socket name + char *port; // INET socket port + char *jpegport; // port to send/receive jpeg images (by default == port+1) + char *jpegfile; // output file name + char *lcheader; // output FITS header with loccorr information + char *captcmdport; // INET command socket port of capture process + char *captpath; // UNIX command socket path of capture process or image capture server address + char *captimport; // INET image socket port of capture process + int cisun; // capture command channel is UNIX socket + int captkey; // image SHM key of capture process + int verbose; // each '-V' increases it +} glob_pars; + +extern glob_pars Glob_pars; + +void parse_args(int argc, char **argv); +void verbose(int levl, const char *fmt, ...); + diff --git a/corrdevice.h b/corrdevice.h new file mode 100644 index 0000000..6d58c22 --- /dev/null +++ b/corrdevice.h @@ -0,0 +1,23 @@ +/* + * This file is part of the loccorr 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 . + */ + +#pragma once + +typedef struct{ + void (*testdevice); +} corrdev_t; diff --git a/loccorr.cflags b/loccorr.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/loccorr.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/loccorr.config b/loccorr.config new file mode 100644 index 0000000..96f0ad6 --- /dev/null +++ b/loccorr.config @@ -0,0 +1,6 @@ +#define EBUG +#define OMP_FOUND +#define _GNU_SOURCE +#define _XOPEN_SOURCE 666 +#define _DEFAULT_SOURCE +#define PACKAGE_VERSION "xxx" diff --git a/loccorr.creator b/loccorr.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/loccorr.creator @@ -0,0 +1 @@ +[General] diff --git a/loccorr.creator.user b/loccorr.creator.user new file mode 100644 index 0000000..bc3be8f --- /dev/null +++ b/loccorr.creator.user @@ -0,0 +1,186 @@ + + + + + + EnvironmentId + {cf63021e-ef53-49b0-b03b-2f2570cdf3b6} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + qt2 + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + 0 + false + false + false + 1 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + true + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 4 + true + + + + true + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {91347f2c-5221-46a7-80b1-0a054ca02f79} + 0 + 0 + 0 + + /home/eddy/Docs/SAO/Image_processing/Prefocal_corrector(local_corrector)/code/mk + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + 0 + true + + + 2 + + false + /home/eddy/Docs/SAO/Image_processing/Prefocal_corrector(local_corrector)/code/mk/loccorr + + ProjectExplorer.CustomExecutableRunConfiguration + + true + -p 12345 + true + true + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/loccorr.cxxflags b/loccorr.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/loccorr.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/loccorr.files b/loccorr.files new file mode 100644 index 0000000..488b241 --- /dev/null +++ b/loccorr.files @@ -0,0 +1,10 @@ +CMakeLists.txt +Dummy_loccorr/CMakeLists.txt +Dummy_loccorr/main.c +Dummy_telescope/CMakeLists.txt +Dummy_telescope/main.c +cmdlnopts.c +cmdlnopts.h +corrdevice.h +loccorrbase.h +main.c diff --git a/loccorr.includes b/loccorr.includes new file mode 100644 index 0000000..9c558e3 --- /dev/null +++ b/loccorr.includes @@ -0,0 +1 @@ +. diff --git a/loccorrbase.h b/loccorrbase.h new file mode 100644 index 0000000..357eefb --- /dev/null +++ b/loccorrbase.h @@ -0,0 +1,23 @@ +/* + * This file is part of the loccorr 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 . + */ + +#pragma once + +// max & min TCP socket port number +#define LC_PORTN_MAX (65535) +#define LC_PORTN_MIN (1024) diff --git a/main.c b/main.c new file mode 100644 index 0000000..4cb711e --- /dev/null +++ b/main.c @@ -0,0 +1,128 @@ +/* + * This file is part of the loccorr 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 +#include + +#include "cmdlnopts.h" +#include "loccorrbase.h" + +static pid_t childpid = 0; + +void signals(int signo){ + DBG("signo=%d", signo); + if(childpid){ // master process + if(signo == SIGUSR1){ // kill child + kill(childpid, signo); + signal(signo, signals); + return; + } + WARNX("Master killed with sig=%d", signo); + LOGERR("Master killed with sig=%d", signo); + exit(signo); + } + // slave: make proper suicide + if(signo){ + WARNX("Get signal %d - exit", signo); + LOGWARN("Get signal %d - exit", signo); + } + // do something here + DBG("exit(%d)", signo); + exit(signo); +} + +// check unix/net socket +static int chk(char *port, int path, const char *what){ + if(port){ + if(path){ + WARNX("Options `port` and UNIX-socket `path` can't be used together!"); + return 1; + } + int p = atoi(port); + if(p < LC_PORTN_MIN || p > LC_PORTN_MAX){ + WARNX("Wrong port value: %d", p); + return 1; + } + } + if(!path && !port){ + WARNX("Point port or path of %s", what); + return 1; + } + return 0; +} + +int main(int argc, char **argv){ + initial_setup(); + parse_args(argc, argv); + int r = chk(Glob_pars.port, (Glob_pars.path != NULL), "current server"); + if(r) return r; + r = chk(Glob_pars.captcmdport, (Glob_pars.captpath && Glob_pars.cisun), "image capture server (don't forget -U for UNIX-socket)"); + if(r) return r; + if(Glob_pars.captkey == -1 && !Glob_pars.captimport){ + WARNX("Point port or SHM key of image server"); + return 2; + } + if(!Glob_pars.lcplugin && !Glob_pars.telplugin){ + WARNX("Point local corrector and/or telesope plugin"); + return 3; + } + if(Glob_pars.logfile){ + int lvl = LOGLEVEL_WARN + Glob_pars.verbose; + DBG("level = %d", lvl); + if(lvl > LOGLEVEL_ANY) lvl = LOGLEVEL_ANY; + verbose(1, "Log file %s @ level %d\n", Glob_pars.logfile, lvl); + OPENLOG(Glob_pars.logfile, lvl, 1); + if(!globlog) WARNX("Can't create log file"); + } + signal(SIGINT, signals); + signal(SIGQUIT, signals); + signal(SIGABRT, signals); + signal(SIGTERM, signals); + signal(SIGHUP, SIG_IGN); + signal(SIGTSTP, SIG_IGN); + signal(SIGUSR1, signals); // restart server + + LOGMSG("Started"); + DBG("Started"); +#ifndef EBUG + unsigned int pause = 5; + while(1){ + childpid = fork(); + if(childpid){ // master + double t0 = dtime(); + LOGMSG("Created child with pid %d", childpid); + wait(NULL); + LOGERR("Child %d died", childpid); + if(dtime() - t0 < 1.) pause += 5; + else pause = 1; + if(pause > 900) pause = 900; + sleep(pause); // wait a little before respawn + }else{ // slave + prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies + break; + } + } +#endif + sleep(5); + return 0; +} +