From 695723f14689ca81c3ae6e7aaf98c7c778f431a9 Mon Sep 17 00:00:00 2001 From: haskal Date: Wed, 17 Oct 2018 13:01:46 -0400 Subject: [PATCH] Systemd integration --- common.cpp | 10 +++++++-- common.h | 2 ++ h804tun.service | 11 ++++++++++ install.sh | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 29 ++++++++++++++++---------- 5 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 h804tun.service create mode 100755 install.sh diff --git a/common.cpp b/common.cpp index cf49121..0e4392e 100644 --- a/common.cpp +++ b/common.cpp @@ -11,6 +11,12 @@ void daemon_notify_ready() { #endif } +void daemon_notify(const std::string& args) { +#ifdef H804_SYSTEMD_ENABLE + sd_notify(0, args.c_str()); +#endif +} + // clang-format off const char* _log_text[(size_t)LogLevel::_SIZE] = { #ifdef H804_SYSTEMD_ENABLE @@ -63,10 +69,10 @@ void _Logger::operator<<(const std::string& str) const { _log(level, str); } void _Logger::operator<<(const char* str) const { _log(level, str); } -errno_exception::errno_exception() noexcept : _what(strerror(errno)) {} +errno_exception::errno_exception() noexcept : _what(strerror(errno)), saved_errno(errno) {} errno_exception::errno_exception(const char* msg) noexcept - : _what(std::string(msg) + ": " + strerror(errno)) {} + : _what(std::string(msg) + ": " + strerror(errno)), saved_errno(errno) {} const char* errno_exception::what() const noexcept { return this->_what.c_str(); } diff --git a/common.h b/common.h index 40f7dcf..17039f4 100644 --- a/common.h +++ b/common.h @@ -11,6 +11,7 @@ namespace h804 { enum class LogLevel : size_t { DEBUG, INFO, NOTICE, WARN, ERROR, _SIZE }; void daemon_notify_ready(); +void daemon_notify(const std::string& args); void _log(const LogLevel, const std::string&); void _log(const LogLevel, const char*); @@ -33,6 +34,7 @@ class errno_exception : public std::exception { const std::string _what; public: + const int saved_errno; errno_exception() noexcept; errno_exception(const char* msg) noexcept; const char* what() const noexcept; diff --git a/h804tun.service b/h804tun.service new file mode 100644 index 0000000..3c70e2a --- /dev/null +++ b/h804tun.service @@ -0,0 +1,11 @@ +[Unit] +Description=H804Tun - Virtual Layer 2 Overlay Network +Requires=network-online.target +After=network-online.target + +[Service] +Type=notify +ExecStart=/opt/h804tun/h804tun /opt/h804tun/h804tun.conf + +[Install] +WantedBy=multi-user.target diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..fdadeb9 --- /dev/null +++ b/install.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +function do_uninstall { + set -x + systemctl stop h804tun + rm -rf /opt/h804tun + systemctl disable h804tun + rm /etc/systemd/system/h804tun.conf + systemctl daemon-reload +} + +function do_build { + echo "Building..." + rm -rf build/ + mkdir -p build/ + pushd build/ + cmake -DWITH_SYSTEMD=ON .. + cmake --build . + if [ -f /opt/h804tun/h804tun.conf ]; then + head -n 2 /opt/h804tun/h804tun.conf > h804tun.conf + else + echo "INTERFACE_NAME" > h804tun.conf + echo "STATIC_IP" >> h804tun.conf + fi + echo "" >> h804tun.conf + echo "# Interfaces: " >> h804tun.conf + ip link | sed 's/^/# /' >> h804tun.conf + if [ -z "$EDITOR" ]; then + EDITOR=ed + fi + $EDITOR h804tun.conf + popd +} + +function do_install { + set -x + systemctl stop h804tun + mkdir -p /opt/h804tun + cp build/h804tun /opt/h804tun/h804tun + cp build/h804tun.conf /opt/h804tun/h804tun.conf + cp h804tun.service /etc/systemd/system + systemctl daemon-reload +} + +if [ $# == 0 ] || [ $1 == "install" ]; then + do_build + sudo $0 do_install +elif [ $1 == "uninstall" ]; then + sudo $0 do_uninstall +elif [ $1 == "do_install" ]; then + do_install +elif [ $1 == "do_uninstall" ]; then + do_uninstall +fi diff --git a/main.cpp b/main.cpp index 4d5e213..5e2319d 100644 --- a/main.cpp +++ b/main.cpp @@ -135,11 +135,13 @@ bool handle_ll(const std::array& buf, size_t size, } int main(int argc, char* argv[]) { - (void)argc; - (void)argv; + if (argc < 2) { + error << "Need conf"; + return 1; + } try { - std::ifstream infile("h804tun.conf"); + std::ifstream infile(argv[1]); std::string interface; std::string ip; if (!std::getline(infile, interface)) { @@ -153,13 +155,6 @@ int main(int argc, char* argv[]) { h804::tun::TunnelDevice device{ip, "255.255.255.0"}; h804::tun::EthSocket llsock{interface.c_str()}; info << "Started"; - std::string cmd1("ip link show "); - cmd1 += device.get_name(); - system(cmd1.c_str()); - cmd1 = "ip addr show "; - cmd1 += device.get_name(); - system(cmd1.c_str()); - system("ip route"); struct sigaction handler; handler.sa_handler = [](int) { @@ -222,6 +217,8 @@ int main(int argc, char* argv[]) { std::thread eth_thread_obj(eth_thread); std::thread share_thread_obj(share_thread); + h804::daemon_notify("READY=1\nSTATUS=Tunnel running"); + tun_thread_obj.join(); eth_thread_obj.join(); share_thread_obj.join(); @@ -232,9 +229,19 @@ int main(int argc, char* argv[]) { // } info << "Exiting"; - } catch (std::exception& e) { + } catch (const h804::errno_exception& e) { error << "Caught exception:"; error << e.what(); + h804::daemon_notify_ready(); + h804::daemon_notify(std::string("STOPPING=1\nSTATUS=") + e.what() + + "\nERRNO=" + std::to_string(e.saved_errno)); + return 1; + } catch (const std::exception& e) { + error << "Caught exception:"; + error << e.what(); + h804::daemon_notify_ready(); + h804::daemon_notify(std::string("STOPPING=1\nERRNO=131\nSTATUS=") + e.what()); + return 1; } return 0; }