]> granicus.if.org Git - icinga2/commitdiff
DSL: Implement getenv() 6786/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Mon, 19 Nov 2018 13:59:20 +0000 (14:59 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Mon, 19 Nov 2018 17:11:16 +0000 (18:11 +0100)
This patch also restores Utility::GetFromEnvironment()
and replaces the hardcoded `getenv()` calls.

doc/18-library-reference.md
icinga-app/icinga.cpp
lib/base/application.cpp
lib/base/scriptutils.cpp
lib/base/scriptutils.hpp
lib/base/utility.cpp
lib/base/utility.hpp
lib/cli/consolecommand.cpp

index 30277bfdf889e0e78f15ec7de50c954165fa50c0..b1c58c018a9882c9b217cded3efaeda8a9781873 100644 (file)
@@ -425,6 +425,22 @@ Example:
        warn = null
     }
 
+### getenv <a id="global-functions-getenv"></a>
+
+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 <a id="global-functions-dirname"></a>
 
 Signature:
index b97e79e8337bfc6f74d803e144d36ec21081b459..e8d3f5bf37bcf8fc611cabaecdfa8dcc6c0bb426 100644 (file)
@@ -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;
index 2ccf275b6b67da5175342e65cb1e6f150a55202f..df93710b71016312d91a152046439f3544e43b0b 100644 (file)
@@ -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<String> paths = String(pathEnv).Split(":");
 
                        bool foundPath = false;
index 6d1c5a063666049dd5dcd0b49ee2bbc1a45238e2..9337ae4646281d25f37d3bc6bffc8f845377057c 100644 (file)
@@ -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<Value>& args)
 
        return Array::FromVector(paths);
 }
+
+String ScriptUtils::GetEnv(const String& key)
+{
+       return Utility::GetFromEnvironment(key);
+}
index 92449188f1ff784c4c27fed414a68ce12d1cefc1..778ab95874afd9fc7af4c4da8a265cb670d4b034 100644 (file)
@@ -58,6 +58,7 @@ public:
        static double Ptr(const Object::Ptr& object);
        static Value Glob(const std::vector<Value>& args);
        static Value GlobRecursive(const std::vector<Value>& args);
+       static String GetEnv(const String& key);
 
 private:
        ScriptUtils();
index cf8bc588fce639f7e828ace14137ae05f1339b0e..466a236dee52db88ff2d90ab88aad00d6551322d 100644 (file)
@@ -36,6 +36,7 @@
 #include <ios>
 #include <fstream>
 #include <iostream>
+#include <stdlib.h>
 #include <future>
 
 #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 */
 }
index e75ebfefafc1398fe2f9e3cdf2f70b3960e8aff4..b07ca1474ebf3f16e0c0cfc86b1c896db5113c13 100644 (file)
@@ -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();
index 15d5ecc77aae14e870171592e0a7a7eed6070b6a..86019022a8f6f541aa44f9527fbadb2b36259e04 100644 (file)
@@ -245,8 +245,8 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::stri
                        << "Type $help to view available commands.\n";
 
 
-       const char *addrEnv = getenv("ICINGA2_API_URL");
-       if (addrEnv)
+       String addrEnv = Utility::GetFromEnvironment("ICINGA2_API_URL");
+       if (!addrEnv.IsEmpty())
                addr = addrEnv;
 
        if (vm.count("connect"))
@@ -290,12 +290,12 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
        int next_line = 1;
 
 #ifdef HAVE_EDITLINE
-       char *homeEnv = getenv("HOME");
+       String homeEnv = Utility::GetFromEnvironment("HOME");
 
        String historyPath;
        std::fstream historyfp;
 
-       if (homeEnv) {
+       if (!homeEnv.IsEmpty()) {
                historyPath = String(homeEnv) + "/.icinga2_history";
 
                historyfp.open(historyPath.CStr(), std::fstream::in);
@@ -321,12 +321,12 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
                        return EXIT_FAILURE;
                }
 
-               const char *usernameEnv = getenv("ICINGA2_API_USERNAME");
-               const char *passwordEnv = getenv("ICINGA2_API_PASSWORD");
+               String usernameEnv = Utility::GetFromEnvironment("ICINGA2_API_USERNAME");
+               String passwordEnv = Utility::GetFromEnvironment("ICINGA2_API_PASSWORD");
 
-               if (usernameEnv)
+               if (!usernameEnv.IsEmpty())
                        url->SetUsername(usernameEnv);
-               if (passwordEnv)
+               if (!passwordEnv.IsEmpty())
                        url->SetPassword(passwordEnv);
 
                if (url->GetPort().IsEmpty())