From 9b221e533c7c19c23c55540936d534acbf8f12a2 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 4 Aug 2019 21:54:54 -0700 Subject: [PATCH] report: list envvars using uv_os_environ() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit simplifies the diagnostic report's code for listing environment variables by using uv_os_environ() instead of platform specific code. PR-URL: https://github.com/nodejs/node/pull/28963 Reviewed-By: Saúl Ibarra Corretgé Reviewed-By: Jiawen Geng Reviewed-By: Anna Henningsen --- src/node_report.cc | 65 +++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/src/node_report.cc b/src/node_report.cc index 8f480e658f7b7b..088c63b9e8402d 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -20,10 +20,6 @@ #include #include -#ifndef _WIN32 -extern char** environ; -#endif - constexpr int NODE_REPORT_VERSION = 1; constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000; constexpr double SEC_PER_MICROS = 1e-6; @@ -552,6 +548,26 @@ static void PrintResourceUsage(JSONWriter* writer) { // Report operating system information. static void PrintSystemInformation(JSONWriter* writer) { + uv_env_item_t* envitems; + int envcount; + int r; + + writer->json_objectstart("environmentVariables"); + + { + Mutex::ScopedLock lock(node::per_process::env_var_mutex); + r = uv_os_environ(&envitems, &envcount); + } + + if (r == 0) { + for (int i = 0; i < envcount; i++) + writer->json_keyvalue(envitems[i].name, envitems[i].value); + + uv_os_free_environ(envitems, envcount); + } + + writer->json_objectend(); + #ifndef _WIN32 static struct { const char* description; @@ -576,45 +592,6 @@ static void PrintSystemInformation(JSONWriter* writer) { {"virtual_memory_kbytes", RLIMIT_AS} #endif }; -#endif // _WIN32 - writer->json_objectstart("environmentVariables"); - Mutex::ScopedLock lock(node::per_process::env_var_mutex); -#ifdef _WIN32 - LPWSTR lpszVariable; - LPWCH lpvEnv; - - // Get pointer to the environment block - lpvEnv = GetEnvironmentStringsW(); - if (lpvEnv != nullptr) { - // Variable strings are separated by null bytes, - // and the block is terminated by a null byte. - lpszVariable = reinterpret_cast(lpvEnv); - while (*lpszVariable) { - DWORD size = WideCharToMultiByte( - CP_UTF8, 0, lpszVariable, -1, nullptr, 0, nullptr, nullptr); - char* str = new char[size]; - WideCharToMultiByte( - CP_UTF8, 0, lpszVariable, -1, str, size, nullptr, nullptr); - std::string env(str); - int sep = env.rfind('='); - std::string key = env.substr(0, sep); - std::string value = env.substr(sep + 1); - writer->json_keyvalue(key, value); - lpszVariable += lstrlenW(lpszVariable) + 1; - } - FreeEnvironmentStringsW(lpvEnv); - } - writer->json_objectend(); -#else - std::string pair; - for (char** env = environ; *env != nullptr; ++env) { - std::string pair(*env); - int separator = pair.find('='); - std::string key = pair.substr(0, separator); - std::string str = pair.substr(separator + 1); - writer->json_keyvalue(key, str); - } - writer->json_objectend(); writer->json_objectstart("userLimits"); struct rlimit limit; @@ -638,7 +615,7 @@ static void PrintSystemInformation(JSONWriter* writer) { } } writer->json_objectend(); -#endif +#endif // _WIN32 PrintLoadedLibraries(writer); }