Systemd integration

This commit is contained in:
xenia 2018-10-17 13:01:46 -04:00
parent 0a8d9e5287
commit 695723f146
5 changed files with 93 additions and 13 deletions

View File

@ -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(); }

View File

@ -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;

11
h804tun.service Normal file
View File

@ -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

54
install.sh Executable file
View File

@ -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

View File

@ -135,11 +135,13 @@ bool handle_ll(const std::array<uint8_t, h804::tun::TUN_MTU>& 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;
}