From 0ca098eb31b154d727ad138f1b9d5f5945d4ee2f Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Sat, 19 Jun 2021 10:16:28 +0200 Subject: [PATCH] helpers: modify exec_initscript() for usage with systemctl Current behavior is untouched, but return value is boolean and so far, the function obviously has returned true on error. So far this was currently not evaluated but is changed now. Otherwise, "bool" makes not really sense or was this intended? --- src/system/helpers.cpp | 28 ++++++++++++++++++++++------ src/system/helpers.h | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index c416ac697..e2f3a2776 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -467,14 +467,30 @@ bool exec_controlscript(std::string script) return ret; } -bool exec_initscript(std::string script, std::string command) +bool exec_initscript(std::string script, std::string command, std::string system_command) { - dprintf(DEBUG_NORMAL, "executing service %s %s\n", script.c_str(), command.c_str()); - int ret = my_system(3, "service", script.c_str(), command.c_str()); - if (ret) - dprintf(DEBUG_NORMAL, "exec init script [%s] failed\n", script.c_str()); + std::string user = getenv("USER"); + if (user != "root") + dprintf(DEBUG_NORMAL, "[helpers] [%s - %d] NOTE: current user is not root!\n", __func__, __LINE__); - return ret; + int ret = 1; + if (system_command == "service") + { + dprintf(DEBUG_DEBUG, "executing %s %s %s\n", system_command.c_str(), script.c_str(), command.c_str()); + ret = my_system(3, system_command.c_str(), script.c_str(), command.c_str()); + } + else if (system_command == "systemctl") + { + dprintf(DEBUG_DEBUG, "executing %s %s %s\n", system_command.c_str(), command.c_str(), script.c_str()); + ret = my_system(3, system_command.c_str(), command.c_str(), script.c_str()); + } + else + dprintf(DEBUG_NORMAL, "Error: [helpers] [%s - %d] no system command defined\n", __func__, __LINE__); + + if (ret) + dprintf(DEBUG_NORMAL, "Error: [helpers] exec init script [%s] failed\n", system_command.c_str()); + + return ret == 0 ? true : false; } std::string backtick(std::string command) diff --git a/src/system/helpers.h b/src/system/helpers.h index 27ca77fbe..b4831ae23 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -59,7 +59,7 @@ int mySleep(int sec); std::string find_executable(const char *name); bool exec_controlscript(std::string script); -bool exec_initscript(std::string script, std::string command = "start"); +bool exec_initscript(std::string script, std::string command = "start", std::string system_command = "service"); /* basically what "foo=`command`" does in the shell */ std::string backtick(std::string command);