Skip to content

Commit 2528dee

Browse files
cjihrigtargos
authored andcommitted
report: list envvars using uv_os_environ()
This commit simplifies the diagnostic report's code for listing environment variables by using uv_os_environ() instead of platform specific code. PR-URL: #28963 Reviewed-By: Saúl Ibarra Corretgé <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 2c91c65 commit 2528dee

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

src/node_report.cc

+21-44
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
#include <fstream>
2121
#include <iomanip>
2222

23-
#ifndef _WIN32
24-
extern char** environ;
25-
#endif
26-
2723
constexpr int NODE_REPORT_VERSION = 1;
2824
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
2925
constexpr double SEC_PER_MICROS = 1e-6;
@@ -552,6 +548,26 @@ static void PrintResourceUsage(JSONWriter* writer) {
552548

553549
// Report operating system information.
554550
static void PrintSystemInformation(JSONWriter* writer) {
551+
uv_env_item_t* envitems;
552+
int envcount;
553+
int r;
554+
555+
writer->json_objectstart("environmentVariables");
556+
557+
{
558+
Mutex::ScopedLock lock(node::per_process::env_var_mutex);
559+
r = uv_os_environ(&envitems, &envcount);
560+
}
561+
562+
if (r == 0) {
563+
for (int i = 0; i < envcount; i++)
564+
writer->json_keyvalue(envitems[i].name, envitems[i].value);
565+
566+
uv_os_free_environ(envitems, envcount);
567+
}
568+
569+
writer->json_objectend();
570+
555571
#ifndef _WIN32
556572
static struct {
557573
const char* description;
@@ -576,45 +592,6 @@ static void PrintSystemInformation(JSONWriter* writer) {
576592
{"virtual_memory_kbytes", RLIMIT_AS}
577593
#endif
578594
};
579-
#endif // _WIN32
580-
writer->json_objectstart("environmentVariables");
581-
Mutex::ScopedLock lock(node::per_process::env_var_mutex);
582-
#ifdef _WIN32
583-
LPWSTR lpszVariable;
584-
LPWCH lpvEnv;
585-
586-
// Get pointer to the environment block
587-
lpvEnv = GetEnvironmentStringsW();
588-
if (lpvEnv != nullptr) {
589-
// Variable strings are separated by null bytes,
590-
// and the block is terminated by a null byte.
591-
lpszVariable = reinterpret_cast<LPWSTR>(lpvEnv);
592-
while (*lpszVariable) {
593-
DWORD size = WideCharToMultiByte(
594-
CP_UTF8, 0, lpszVariable, -1, nullptr, 0, nullptr, nullptr);
595-
char* str = new char[size];
596-
WideCharToMultiByte(
597-
CP_UTF8, 0, lpszVariable, -1, str, size, nullptr, nullptr);
598-
std::string env(str);
599-
int sep = env.rfind('=');
600-
std::string key = env.substr(0, sep);
601-
std::string value = env.substr(sep + 1);
602-
writer->json_keyvalue(key, value);
603-
lpszVariable += lstrlenW(lpszVariable) + 1;
604-
}
605-
FreeEnvironmentStringsW(lpvEnv);
606-
}
607-
writer->json_objectend();
608-
#else
609-
std::string pair;
610-
for (char** env = environ; *env != nullptr; ++env) {
611-
std::string pair(*env);
612-
int separator = pair.find('=');
613-
std::string key = pair.substr(0, separator);
614-
std::string str = pair.substr(separator + 1);
615-
writer->json_keyvalue(key, str);
616-
}
617-
writer->json_objectend();
618595

619596
writer->json_objectstart("userLimits");
620597
struct rlimit limit;
@@ -638,7 +615,7 @@ static void PrintSystemInformation(JSONWriter* writer) {
638615
}
639616
}
640617
writer->json_objectend();
641-
#endif
618+
#endif // _WIN32
642619

643620
PrintLoadedLibraries(writer);
644621
}

0 commit comments

Comments
 (0)