From bbc1a30c5dd75192728c4e3133a0d983458b4b7a Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 22 Apr 2024 13:49:26 +0100 Subject: [PATCH 1/3] Allow to buildon Windows again. --- DAPNETGateway.cpp | 4 +++- DAPNETGateway.vcxproj | 12 ++++++++++++ UDPSocket.h | 3 ++- prebuild.cmd | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 prebuild.cmd diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 834c5ba..a66b521 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -22,6 +22,8 @@ #include "Thread.h" #include "Timer.h" #include "Log.h" +#include "GitVersion.h" + #include "REGEX.h" #include @@ -80,7 +82,7 @@ int main(int argc, char** argv) for (int currentArg = 1; currentArg < argc; ++currentArg) { std::string arg = argv[currentArg]; if ((arg == "-v") || (arg == "--version")) { - ::fprintf(stdout, "DAPNETGateway version %s\n", VERSION); + ::fprintf(stdout, "DAPNETGateway version %s git #%.7s\n", VERSION, gitversion); return 0; } else if (arg.substr(0, 1) == "-") { ::fprintf(stderr, "Usage: DAPNETGateway [-v|--version] [filename]\n"); diff --git a/DAPNETGateway.vcxproj b/DAPNETGateway.vcxproj index dc023f7..0a39333 100644 --- a/DAPNETGateway.vcxproj +++ b/DAPNETGateway.vcxproj @@ -127,6 +127,9 @@ true ws2_32.lib;%(AdditionalDependencies) + + prebuild.cmd + @@ -142,6 +145,9 @@ true ws2_32.lib;%(AdditionalDependencies) + + prebuild.cmd + @@ -161,6 +167,9 @@ true ws2_32.lib;%(AdditionalDependencies) + + prebuild.cmd + @@ -180,6 +189,9 @@ true ws2_32.lib;%(AdditionalDependencies) + + prebuild.cmd + diff --git a/UDPSocket.h b/UDPSocket.h index 28e350c..75228a3 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -69,10 +69,11 @@ private: unsigned short m_localPort; #if defined(_WIN32) || defined(_WIN64) SOCKET m_fd; + int m_af; #else int m_fd; -#endif sa_family_t m_af; +#endif }; #endif diff --git a/prebuild.cmd b/prebuild.cmd new file mode 100644 index 0000000..2dc5f9d --- /dev/null +++ b/prebuild.cmd @@ -0,0 +1,38 @@ +@echo off +REM This pre-build file is for MSVS VC++. It parses the git master hash and +REM converts it into GitVersion.h for compiling into builds. [George M1GEO] + +cd %1 +setlocal enabledelayedexpansion +set HEADFILE=.git\HEAD +set HASHFILE=0 +if exist %HEADFILE% ( + for /F "tokens=4 delims=/:" %%a in ('type %HEADFILE%') do set HEADBRANCH=%%a + set HASHFILE=.git\refs\heads\!HEADBRANCH! + echo Found Git HEAD file: %HEADFILE% + echo Git HEAD branch: !HEADBRANCH! + echo Git HASH file: !HASHFILE! + call :USEHASH +) else ( + echo No head file :( + call :USENULL +) + +goto :EOF + +:USENULL +set GITHASH=0000000000000000000000000000000000000000 +goto :WRITEGITVERSIONHEADER + +:USEHASH +for /f %%i in ('type !HASHFILE!') do set GITHASH=%%i +goto :WRITEGITVERSIONHEADER + +:WRITEGITVERSIONHEADER +echo // File contains Git commit ID SHA1 present at buildtime (prebuild.cmd) > GitVersion.h +echo const char *gitversion = "%GITHASH%"; >> GitVersion.h +echo Current Git HASH: %GITHASH% +goto :FINISHED + +:FINISHED +echo GitVersion.h written... From bb3463b14cff1b2ac0c558baa5393c8a8d2bbcf2 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 22 Apr 2024 14:06:52 +0100 Subject: [PATCH 2/3] Add the gitversion to the Makefile. --- Makefile | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a789596..ff6d6dc 100644 --- a/Makefile +++ b/Makefile @@ -8,14 +8,30 @@ OBJECTS = Conf.o DAPNETGateway.o DAPNETNetwork.o Log.o POCSAGMessage.o POCSAGNet all: DAPNETGateway -DAPNETGateway: $(OBJECTS) +DAPNETGateway: GitVersion.h $(OBJECTS) $(CXX) $(OBJECTS) $(CFLAGS) $(LIBS) -o DAPNETGateway %.o: %.cpp $(CXX) $(CFLAGS) -c -o $@ $< +DAPNETGateway.o: GitVersion.h FORCE + +.PHONY: GitVersion.h + +FORCE: + + install: install -m 755 DAPNETGateway /usr/local/bin/ clean: $(RM) DAPNETGateway *.o *.d *.bak *~ + +# Export the current git version if the index file exists, else 000... +GitVersion.h: +ifneq ("$(wildcard .git/index)","") + echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@ +else + echo "const char *gitversion = \"0000000000000000000000000000000000000000\";" > $@ +endif + From bcd41d0ab64b072372fbb6f0126484f303e964af Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 28 Aug 2024 17:50:50 +0100 Subject: [PATCH 3/3] Improve the signal handling. --- DAPNETGateway.cpp | 72 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index a66b521..71e8ee1 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -1,5 +1,5 @@ /* -* Copyright (C) 2018,2020 by Jonathan Naylor G4KLX +* Copyright (C) 2018,2020,2024 by Jonathan Naylor G4KLX * * 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 @@ -43,6 +43,17 @@ const char* DEFAULT_INI_FILE = "DAPNETGateway.ini"; const char* DEFAULT_INI_FILE = "/etc/DAPNETGateway.ini"; #endif +static bool m_killed = false; +static int m_signal = 0; + +#if !defined(_WIN32) && !defined(_WIN64) +static void sigHandler(int signum) +{ + m_killed = true; + m_signal = signum; +} +#endif + #include #include @@ -93,11 +104,40 @@ int main(int argc, char** argv) } } - CDAPNETGateway* gateway = new CDAPNETGateway(std::string(iniFile)); +#if !defined(_WIN32) && !defined(_WIN64) + ::signal(SIGINT, sigHandler); + ::signal(SIGTERM, sigHandler); + ::signal(SIGHUP, sigHandler); +#endif - int ret = gateway->run(); + int ret = 0; - delete gateway; + do { + m_signal = 0; + m_killed = false; + + CDAPNETGateway* gateway = new CDAPNETGateway(std::string(iniFile)); + ret = gateway->run(); + + delete gateway; + + switch (m_signal) { + case 2: + ::LogInfo("DAPNETGateway-%s exited on receipt of SIGINT", VERSION); + break; + case 15: + ::LogInfo("DAPNETGateway-%s exited on receipt of SIGTERM", VERSION); + break; + case 1: + ::LogInfo("DAPNETGateway-%s is restarting on receipt of SIGHUP", VERSION); + break; + default: + ::LogInfo("DAPNETGateway-%s exited on receipt of an unknown signal", VERSION); + break; + } + } while (m_signal == 1); + + ::LogFinalise(); return ret; } @@ -146,7 +186,7 @@ int CDAPNETGateway::run() pid_t pid = ::fork(); if (pid == -1) { ::fprintf(stderr, "Couldn't fork() , exiting\n"); - return -1; + return 1; } else if (pid != 0) { exit(EXIT_SUCCESS); } @@ -154,13 +194,13 @@ int CDAPNETGateway::run() // Create new session and process group if (::setsid() == -1) { ::fprintf(stderr, "Couldn't setsid(), exiting\n"); - return -1; + return 1; } // Set the working directory to the root directory if (::chdir("/") == -1) { ::fprintf(stderr, "Couldn't cd /, exiting\n"); - return -1; + return 1; } // If we are currently root... @@ -168,7 +208,7 @@ int CDAPNETGateway::run() struct passwd* user = ::getpwnam("mmdvm"); if (user == NULL) { ::fprintf(stderr, "Could not get the mmdvm user, exiting\n"); - return -1; + return 1; } uid_t mmdvm_uid = user->pw_uid; @@ -177,18 +217,18 @@ int CDAPNETGateway::run() // Set user and group ID's to mmdvm:mmdvm if (setgid(mmdvm_gid) != 0) { ::fprintf(stderr, "Could not set mmdvm GID, exiting\n"); - return -1; + return 1; } if (setuid(mmdvm_uid) != 0) { ::fprintf(stderr, "Could not set mmdvm UID, exiting\n"); - return -1; + return 1; } // Double check it worked (AKA Paranoia) if (setuid(0) != -1) { ::fprintf(stderr, "It's possible to regain root - something is wrong!, exiting\n"); - return -1; + return 1; } } } @@ -223,8 +263,6 @@ int CDAPNETGateway::run() ret = m_pocsagNetwork->open(); if (!ret) { ::LogError("Cannot open the repeater network port"); - ::LogFinalise(); - return 1; } @@ -235,8 +273,6 @@ int CDAPNETGateway::run() if (dapnetAuthKey.length() == 0 || dapnetAuthKey == "TOPSECRET") { ::LogError("AuthKey not set or invalid"); - ::LogFinalise(); - return 1; } @@ -248,7 +284,6 @@ int CDAPNETGateway::run() delete m_dapnetNetwork; ::LogError("Cannot open the DAPNET network port"); - ::LogFinalise(); return 1; } @@ -263,7 +298,6 @@ int CDAPNETGateway::run() delete m_dapnetNetwork; ::LogError("Cannot login to the DAPNET network"); - ::LogFinalise(); return 1; } @@ -285,7 +319,7 @@ int CDAPNETGateway::run() regexWhitelist = m_regexWhitelist->get(); - for (;;) { + while (!m_killed) { unsigned char buffer[200U]; if (m_pocsagNetwork->read(buffer) > 0U) { @@ -400,8 +434,6 @@ int CDAPNETGateway::run() m_dapnetNetwork->close(); delete m_dapnetNetwork; - ::LogFinalise(); - return 0; }