Skip to content

Commit

Permalink
report: use libuv calls for OS and machine info
Browse files Browse the repository at this point in the history
PR-URL: #25900
Fixes: #25843
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
cjihrig committed Feb 5, 2019
1 parent 50f9062 commit 9bbe29d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 108 deletions.
7 changes: 5 additions & 2 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ is provided below for reference.
"release": {
"name": "node"
},
"osVersion": "Linux 3.10.0-862.el7.x86_64 #1 SMP Wed Mar 21 18:14:51 EDT 2018",
"machine": "test_machine x86_64"
"osName": "Linux",
"osRelease": "3.10.0-862.el7.x86_64",
"osVersion": "#1 SMP Wed Mar 21 18:14:51 EDT 2018",
"osMachine": "x86_64",
"host": "test_machine"
},
"javascriptStack": {
"message": "Error: *** test-exception.js: throwing uncaught Error",
Expand Down
6 changes: 0 additions & 6 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,11 @@
['OS=="win"', {
'libraries': [
'dbghelp.lib',
'Netapi32.lib',
'PsApi.lib',
'Ws2_32.lib',
],
'dll_files': [
'dbghelp.dll',
'Netapi32.dll',
'PsApi.dll',
'Ws2_32.dll',
],
Expand Down Expand Up @@ -676,13 +674,11 @@
['OS=="win"', {
'libraries': [
'dbghelp.lib',
'Netapi32.lib',
'PsApi.lib',
'Ws2_32.lib',
],
'dll_files': [
'dbghelp.dll',
'Netapi32.dll',
'PsApi.dll',
'Ws2_32.dll',
],
Expand Down Expand Up @@ -1041,13 +1037,11 @@
['OS=="win"', {
'libraries': [
'dbghelp.lib',
'Netapi32.lib',
'PsApi.lib',
'Ws2_32.lib',
],
'dll_files': [
'dbghelp.dll',
'Netapi32.dll',
'PsApi.dll',
'Ws2_32.dll',
],
Expand Down
122 changes: 22 additions & 100 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <cxxabi.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <sys/utsname.h>
#endif

#include <fcntl.h>
Expand All @@ -48,6 +47,15 @@
extern char** environ;
#endif

#ifdef __POSIX__
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
#endif // __POSIX__

#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif // MAXHOSTNAMELEN

namespace report {
using node::arraysize;
using node::Environment;
Expand Down Expand Up @@ -351,107 +359,21 @@ static void PrintVersionInformation(JSONWriter* writer) {
// Report release metadata.
PrintRelease(writer);

// Report operating system and machine information (Windows)
#ifdef _WIN32
{
// Level 101 to obtain the server name, type, and associated details.
// ref: https://docs.microsoft.com/en-us/windows/desktop/
// api/lmserver/nf-lmserver-netservergetinfo
const DWORD level = 101;
LPSERVER_INFO_101 os_info = nullptr;
NET_API_STATUS nStatus =
NetServerGetInfo(nullptr, level, reinterpret_cast<LPBYTE*>(&os_info));
if (nStatus == NERR_Success) {
LPSTR os_name = "Windows";
const DWORD major = os_info->sv101_version_major & MAJOR_VERSION_MASK;
const DWORD type = os_info->sv101_type;
const bool isServer = (type & SV_TYPE_DOMAIN_CTRL) ||
(type & SV_TYPE_DOMAIN_BAKCTRL) ||
(type & SV_TYPE_SERVER_NT);
switch (major) {
case 5:
switch (os_info->sv101_version_minor) {
case 0:
os_name = "Windows 2000";
break;
default:
os_name = (isServer ? "Windows Server 2003" : "Windows XP");
}
break;
case 6:
switch (os_info->sv101_version_minor) {
case 0:
os_name = (isServer ? "Windows Server 2008" : "Windows Vista");
break;
case 1:
os_name = (isServer ? "Windows Server 2008 R2" : "Windows 7");
break;
case 2:
os_name = (isServer ? "Windows Server 2012" : "Windows 8");
break;
case 3:
os_name = (isServer ? "Windows Server 2012 R2" : "Windows 8.1");
break;
default:
os_name = (isServer ? "Windows Server" : "Windows Client");
}
break;
case 10:
os_name = (isServer ? "Windows Server 2016" : "Windows 10");
break;
default:
os_name = (isServer ? "Windows Server" : "Windows Client");
}
writer->json_keyvalue("osVersion", os_name);

// Convert and report the machine name and comment fields
// (these are LPWSTR types)
size_t count;
char name_buf[256];
wcstombs_s(
&count, name_buf, sizeof(name_buf), os_info->sv101_name, _TRUNCATE);
if (os_info->sv101_comment != nullptr) {
char comment_buf[256];
wcstombs_s(&count,
comment_buf,
sizeof(comment_buf),
os_info->sv101_comment,
_TRUNCATE);
buf << name_buf << " " << comment_buf;
writer->json_keyvalue("machine", buf.str());
buf.flush();
} else {
writer->json_keyvalue("machine", name_buf);
}
// Report operating system and machine information
uv_utsname_t os_info;

if (os_info != nullptr) {
NetApiBufferFree(os_info);
}
} else {
// NetServerGetInfo() failed, fallback to use GetComputerName() instead
TCHAR machine_name[256];
DWORD machine_name_size = 256;
writer->json_keyvalue("osVersion", "Windows");
if (GetComputerName(machine_name, &machine_name_size)) {
writer->json_keyvalue("machine", machine_name);
}
}
}
#else
// Report operating system and machine information (Unix/OSX)
struct utsname os_info;
if (uname(&os_info) >= 0) {
#ifdef _AIX
buf << os_info.sysname << " " << os_info.version << "." << os_info.release;
#else
buf << os_info.sysname << " " << os_info.release << " " << os_info.version;
#endif /* _AIX */
writer->json_keyvalue("osVersion", buf.str());
buf.str("");
buf << os_info.nodename << " " << os_info.machine;
writer->json_keyvalue("machine", buf.str());
if (uv_os_uname(&os_info) == 0) {
writer->json_keyvalue("osName", os_info.sysname);
writer->json_keyvalue("osRelease", os_info.release);
writer->json_keyvalue("osVersion", os_info.version);
writer->json_keyvalue("osMachine", os_info.machine);
}
#endif

char host[MAXHOSTNAMELEN + 1];
size_t host_size = sizeof(host);

if (uv_os_gethostname(host, &host_size) == 0)
writer->json_keyvalue("host", host);
}

// Report the JavaScript stack.
Expand Down

0 comments on commit 9bbe29d

Please sign in to comment.