From e47a117639ad06ab70ef8757a518142aabea9fb0 Mon Sep 17 00:00:00 2001 From: Claudio Luck Date: Wed, 15 Aug 2018 04:06:15 +0200 Subject: [PATCH] JSON-104: mechanism to call hotfix binary once (trivial) --- server/hotfixer.cc | 129 +++++++++++++++++++++++++++++++++++++++++++++ server/hotfixer.hh | 20 +++++++ server/main.cc | 13 +++++ 3 files changed, 162 insertions(+) create mode 100644 server/hotfixer.cc create mode 100644 server/hotfixer.hh diff --git a/server/hotfixer.cc b/server/hotfixer.cc new file mode 100644 index 0000000..4e5b9df --- /dev/null +++ b/server/hotfixer.cc @@ -0,0 +1,129 @@ +#include "hotfixer.hh" +#include "logger.hh" + +#include + + +#ifdef _WIN32 +# ifndef HOTFIX_SENTINEL_FILE +# define HOTFIX_SENTINEL_FILE "json-hotfix-1.dat" +# endif +# define HOTFIX_BINARY "pep-hotfix.exe" +#else +# ifndef HOTFIX_SENTINEL_FILE +# define HOTFIX_SENTINEL_FILE "json-hotfix-1.dat" +# endif +# define HOTFIX_BINARY "pep-hotfix" +#endif + + +namespace fs = boost::filesystem; +namespace bp = boost::process; + + +namespace pEp +{ + namespace utility + { + + fs::path get_pep_dir() + { + const char *env_pephome = getenv("PEPHOME"); +#ifndef _WIN32 + const char *env_usrhome = getenv("HOME"); + const char *env_pepsub = ".pEp"; +#else + const char *env_usrhome = getenv("APPDATA"); + const char *env_pepsub = "pEp"; +#endif + fs::path pephome; + if (env_pephome) + pephome = fs::path(env_pephome); + else + { + if (!env_usrhome || !env_pepsub) + return fs::path(); // .empty() == true + fs::path p1 = env_usrhome; + fs::path p2 = env_pepsub; + pephome = p1 / p2; + } + return pephome; + } + + + // fs::path get_adapter_share_dir() + // { + // return fs::path("."); + // } + + + fs::path get_adapter_bin_dir() + { + return fs::path("../../bin"); + } + + + int is_error_logged(std::error_code ec) + { + int ret = 0; + if ((ret = ec.value())) { + Logger l("hotfix"); + l.info("%s error (%d): %s", ec.category().name(), ret, ec.message().c_str()); + ec.clear(); + return ret; + } + return 0; + } + + + bool hotfix_call_required() + { + Logger L("hotfix"); + fs::path pepdir = get_pep_dir(); + if (fs::exists(pepdir / HOTFIX_SENTINEL_FILE)) + return false; + L.info("hotfix required to run"); + return true; + } + + + int hotfix_call_execute() + { + int ret; + Logger L("hotfix"); + fs::path sent_path = get_pep_dir() / HOTFIX_SENTINEL_FILE; + fs::path hotfix_bin = get_adapter_bin_dir() / HOTFIX_BINARY; + std::error_code ec; + + bp::ipstream is; // reading spipe-stream + std::string line; + bp::child c(hotfix_bin, "argv1", "argv2", bp::std_out > is, ec); + if ((ret = is_error_logged(ec))) + return ret; + + while (c.running(ec) && std::getline(is, line) && !line.empty()) + L.info(line); + if ((ret = is_error_logged(ec))) { + c.wait(ec); + return ret; + } + c.wait(ec); + if ((ret = is_error_logged(ec))) + return ret; + + if ((ret = c.exit_code())) { + L.info("hotfix returned exit code %d, exiting...", ret); + return ret; + } + + fs::ofstream sent_file(sent_path); + sent_file << "# HOTFIX SENTINEL FILE, DO NOT REMOVE" << std::endl; + sent_file.close(); + + L.info("sentinel file created: %s", sent_path.c_str()); + + return 0; + } + + } +} diff --git a/server/hotfixer.hh b/server/hotfixer.hh new file mode 100644 index 0000000..61ef5b2 --- /dev/null +++ b/server/hotfixer.hh @@ -0,0 +1,20 @@ +#ifndef HOT_FIXER_HH +#define HOT_FIXER_HH + +#include + +using namespace boost::process; + +namespace pEp +{ + namespace utility + { + +bool hotfix_call_required(); + +int hotfix_call_execute(); + + } +} + +#endif diff --git a/server/main.cc b/server/main.cc index 72a7049..5728274 100644 --- a/server/main.cc +++ b/server/main.cc @@ -5,6 +5,7 @@ #include "daemonize.hh" #include "logger.hh" #include "nulllogger.hh" +#include "hotfixer.hh" #include #include @@ -101,6 +102,18 @@ try Logger L("main"); L.info("main logger started"); + + int hotfix_ret = 0; +#ifdef _WIN32 + if ((STATUS_HANDLE == 0) && pEp::utility::hotfix_call_required()) +#else + if (pEp::utility::hotfix_call_required()) +#endif + { + if ((hotfix_ret = pEp::utility::hotfix_call_execute()) != 0) + return hotfix_ret; + } + if(add_sharks) { ev_server::addSharks();