From f3cd3eb8700cf03409786d8177d4f0ed0c67e488 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 19 Nov 2018 14:59:20 +0100 Subject: [PATCH] DSL: Implement getenv() This patch also restores Utility::GetFromEnvironment() and replaces the hardcoded `getenv()` calls. --- doc/18-library-reference.md | 16 ++++++++++++++++ icinga-app/icinga.cpp | 3 ++- lib/base/application.cpp | 4 ++-- lib/base/scriptutils.cpp | 6 ++++++ lib/base/scriptutils.hpp | 1 + lib/base/utility.cpp | 13 ++++++++----- lib/base/utility.hpp | 2 ++ lib/cli/consolecommand.cpp | 16 ++++++++-------- 8 files changed, 45 insertions(+), 16 deletions(-) diff --git a/doc/18-library-reference.md b/doc/18-library-reference.md index 30277bfdf..b1c58c018 100644 --- a/doc/18-library-reference.md +++ b/doc/18-library-reference.md @@ -425,6 +425,22 @@ Example: warn = null } +### getenv + +Signature: + + function getenv(key) + +Returns the value from the specified environment variable key. + +Example: + + $ MY_ENV_VAR=icinga2 icinga2 console + Icinga 2 (version: v2.11.0) + Type $help to view available commands. + <1> => getenv("MY_ENV_VAR") + "icinga2" + ### dirname Signature: diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index b97e79e83..e8d3f5bf3 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -925,7 +925,8 @@ static VOID WINAPI ServiceMain(DWORD argc, LPSTR *argv) int main(int argc, char **argv) { #ifndef _WIN32 - if (!getenv("ICINGA2_KEEP_FDS")) { + String keepFDs = Utility::GetFromEnvironment("ICINGA2_KEEP_FDS"); + if (keepFDs.IsEmpty()) { rlimit rl; if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) { rlim_t maxfds = rl.rlim_max; diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 2ccf275b6..df93710b7 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -491,8 +491,8 @@ String Application::GetExePath(const String& argv0) } if (!foundSlash) { - const char *pathEnv = getenv("PATH"); - if (pathEnv) { + String pathEnv = Utility::GetFromEnvironment("PATH"); + if (!pathEnv.IsEmpty()) { std::vector paths = String(pathEnv).Split(":"); bool foundPath = false; diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 6d1c5a063..9337ae464 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -61,6 +61,7 @@ REGISTER_SAFE_FUNCTION(System, bool, &ScriptUtils::CastBool, "value"); REGISTER_SAFE_FUNCTION(System, get_time, &Utility::GetTime, ""); REGISTER_SAFE_FUNCTION(System, basename, &Utility::BaseName, "path"); REGISTER_SAFE_FUNCTION(System, dirname, &Utility::DirName, "path"); +REGISTER_SAFE_FUNCTION(System, getenv, &ScriptUtils::GetEnv, "value"); REGISTER_SAFE_FUNCTION(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim, "component"); REGISTER_SAFE_FUNCTION(System, track_parents, &ScriptUtils::TrackParents, "child"); REGISTER_SAFE_FUNCTION(System, escape_shell_cmd, &Utility::EscapeShellCmd, "cmd"); @@ -530,3 +531,8 @@ Value ScriptUtils::GlobRecursive(const std::vector& args) return Array::FromVector(paths); } + +String ScriptUtils::GetEnv(const String& key) +{ + return Utility::GetFromEnvironment(key); +} diff --git a/lib/base/scriptutils.hpp b/lib/base/scriptutils.hpp index 92449188f..778ab9587 100644 --- a/lib/base/scriptutils.hpp +++ b/lib/base/scriptutils.hpp @@ -58,6 +58,7 @@ public: static double Ptr(const Object::Ptr& object); static Value Glob(const std::vector& args); static Value GlobRecursive(const std::vector& args); + static String GetEnv(const String& key); private: ScriptUtils(); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index cf8bc588f..466a236de 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #ifdef __FreeBSD__ @@ -1936,16 +1937,18 @@ String Utility::GetIcingaDataPath() #endif /* _WIN32 */ +/** + * Retrieve the environment variable value by given key. + * + * @param env Environment variable name. + */ + String Utility::GetFromEnvironment(const String& env) { -#ifndef _WIN32 const char *envValue = getenv(env.CStr()); + if (envValue == NULL) return String(); else return String(envValue); -#else /* _WIN32 */ - // While getenv exists on Windows, we don't set them. Therefore there is no reason to read them. - return String(); -#endif /* _WIN32 */ } diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index e75ebfefa..b07ca1474 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -69,6 +69,8 @@ public: static String DirName(const String& path); static String BaseName(const String& path); + static String GetEnv(const String& key); + static void NullDeleter(void *); static double GetTime(); diff --git a/lib/cli/consolecommand.cpp b/lib/cli/consolecommand.cpp index 15d5ecc77..86019022a 100644 --- a/lib/cli/consolecommand.cpp +++ b/lib/cli/consolecommand.cpp @@ -245,8 +245,8 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vectorSetUsername(usernameEnv); - if (passwordEnv) + if (!passwordEnv.IsEmpty()) url->SetPassword(passwordEnv); if (url->GetPort().IsEmpty())