From afa43d727512faa19d687e2476910fe7df09ec7d Mon Sep 17 00:00:00 2001 From: royna2544 Date: Sun, 17 Nov 2024 17:02:14 +0900 Subject: [PATCH] Add FreeBSD support --- CMakeLists.txt | 4 ++-- resources/about.html.in | 2 +- .../support/popen_wdt/popen_wdt_posix.c | 2 +- src/mainDaemon.cpp | 10 ++++++---- src/socket/interface/CMakeLists.txt | 2 +- .../impl/helper/SocketHelperLinux.cpp | 20 +++++++++++++------ src/socket/selector/CMakeLists.txt | 2 +- 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8254d888..51d6dad6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.25) project(TgBot++ LANGUAGES CXX C) ####################### Set CMake Policy ####################### @@ -115,7 +115,7 @@ set(GLOBAL_DEFINITIONS -D__TGBOT__) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld") set(CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=lld") - if (APPLE OR ANDROID) + if (APPLE OR ANDROID OR BSD) list(APPEND GLOBAL_COMPILE_OPTIONS -fexperimental-library) endif() endif() diff --git a/resources/about.html.in b/resources/about.html.in index 44e95415..c0908270 100644 --- a/resources/about.html.in +++ b/resources/about.html.in @@ -4,7 +4,7 @@ Hi! I'm _botname_ - Proudly backed by C/C++ - Spam purge feature - Regex command support -- Runs on Windows (MSYS2/MSVC), macOS, Linux, Android (Termux) +- Runs on Windows (MSYS2/MSVC), macOS, Linux, Android (Termux), BSD-like systems - Exports a network control interface with IPv4 and IPv6 - Client apps supported for Win32/Android
BUILD INFO: diff --git a/src/command_modules/support/popen_wdt/popen_wdt_posix.c b/src/command_modules/support/popen_wdt/popen_wdt_posix.c index 4f2583ca..2920558d 100644 --- a/src/command_modules/support/popen_wdt/popen_wdt_posix.c +++ b/src/command_modules/support/popen_wdt/popen_wdt_posix.c @@ -135,7 +135,7 @@ bool popen_watchdog_start(popen_watchdog_data_t **data_in) { // Force "C" locale to avoid locale-dependent behavior setenv("LC_ALL", "C", true); // Set process group ID it its pid. - if (setpgrp() == -1) { + if (setpgid(0, 0) == -1) { POPEN_WDT_DBGLOG("Failed to set process group: %s", strerror(errno)); _exit(127); diff --git a/src/mainDaemon.cpp b/src/mainDaemon.cpp index c97f2754..bd0893e3 100644 --- a/src/mainDaemon.cpp +++ b/src/mainDaemon.cpp @@ -8,10 +8,10 @@ #include #include +#include #include #include #include -#include #include #include #include @@ -94,7 +94,7 @@ int main(const int argc, char** argv) { if (pid == 0) { // Set the process group ID to the same as the process ID - setpgrp(); + setpgid(0, 0); // Execute the bot executable with the provided arguments LOG(INFO) << "Executing the child process, " << argv[0] << " with pid " << getpid(); @@ -139,13 +139,15 @@ int main(const int argc, char** argv) { } } constexpr std::chrono::seconds sleep_secs(10); - LOG(INFO) << fmt::format("Consumed child death, sleeping for {}", sleep_secs); + LOG(INFO) << fmt::format("Consumed child death, sleeping for {}", + sleep_secs); std::this_thread::sleep_for(sleep_secs); if (!std::filesystem::exists(kPidFile)) { LOG(INFO) << "Pid file gone, exiting"; return EXIT_SUCCESS; } - std::filesystem::remove(kPidFile); // Remove the pid file after termination + std::filesystem::remove( + kPidFile); // Remove the pid file after termination goto redo_vfork; // Restart the daemon process loop } } diff --git a/src/socket/interface/CMakeLists.txt b/src/socket/interface/CMakeLists.txt index efdf2b26..62447c47 100644 --- a/src/socket/interface/CMakeLists.txt +++ b/src/socket/interface/CMakeLists.txt @@ -13,7 +13,7 @@ if (UNIX) list(APPEND TGBOTSOCKET_SRC_LIST impl/helper/SocketHelperDarwin.cpp ) - elseif(LINUX OR ANDROID) + elseif(LINUX OR ANDROID OR BSD) list(APPEND TGBOTSOCKET_SRC_LIST impl/helper/SocketHelperLinux.cpp ) diff --git a/src/socket/interface/impl/helper/SocketHelperLinux.cpp b/src/socket/interface/impl/helper/SocketHelperLinux.cpp index a5a956a3..e1003110 100644 --- a/src/socket/interface/impl/helper/SocketHelperLinux.cpp +++ b/src/socket/interface/impl/helper/SocketHelperLinux.cpp @@ -4,12 +4,20 @@ void SocketInterfaceUnix::bindToInterface(const socket_handle_t sock, const std::string& iface) { +#ifdef SO_BINDTODEVICE struct ifreq intf {}; + strncpy(intf.ifr_name, iface.c_str(), IFNAMSIZ); + if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &intf, sizeof(intf)) < + 0) { + PLOG(ERROR) << "setsockopt(SO_BINDTODEVICE) failed"; + return; + } +#endif int opt = 1; - memset(&intf, 0, sizeof(intf)); - strncpy(intf.ifr_ifrn.ifrn_name, iface.c_str(), IFNAMSIZ); - setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &intf, sizeof(intf)); - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, - sizeof(opt)); -} + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, + sizeof(opt)) < 0) { + PLOG(ERROR) << "setsockopt(SO_REUSEADDR/SO_REUSEPORT) failed"; + return; + } +} \ No newline at end of file diff --git a/src/socket/selector/CMakeLists.txt b/src/socket/selector/CMakeLists.txt index 6ab2bfe7..0ebbd9c4 100644 --- a/src/socket/selector/CMakeLists.txt +++ b/src/socket/selector/CMakeLists.txt @@ -9,7 +9,7 @@ if (UNIX) list(APPEND SELECTOR_SRC_LIST SelectorLinuxEPollStub.cpp ) - else() + elseif(LINUX) list(APPEND SELECTOR_SRC_LIST SelectorLinuxEPoll.cpp )