Skip to content

Commit

Permalink
Add FreeBSD support
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Nov 17, 2024
1 parent 407208e commit afa43d7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.25)
project(TgBot++ LANGUAGES CXX C)

####################### Set CMake Policy #######################
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion resources/about.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Hi! I'm <a href="https://t.me/_botusername_">_botname_</a>
- 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</blockquote>
<blockquote><b>BUILD INFO</b>:
Expand Down
2 changes: 1 addition & 1 deletion src/command_modules/support/popen_wdt/popen_wdt_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions src/mainDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <unistd.h>

#include <StructF.hpp>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <filesystem>
#include <limits>
#include <logging/AbslLogInit.hpp>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/socket/interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
20 changes: 14 additions & 6 deletions src/socket/interface/impl/helper/SocketHelperLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/socket/selector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (UNIX)
list(APPEND SELECTOR_SRC_LIST
SelectorLinuxEPollStub.cpp
)
else()
elseif(LINUX)
list(APPEND SELECTOR_SRC_LIST
SelectorLinuxEPoll.cpp
)
Expand Down

0 comments on commit afa43d7

Please sign in to comment.