From bff71552d8151742a1b32cfef5ce147a083741cb Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Tue, 22 Jun 2021 21:39:51 +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? Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/d2d22e57483f013d3b819835f395a5363808a07e Author: Thilo Graf Date: 2021-06-22 (Tue, 22 Jun 2021) --- 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 e585910cc..e3ac61602 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -568,14 +568,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 83df327b8..8cd975508 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -61,7 +61,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);