Skip to content

Commit 6d39a54

Browse files
cjihrigaddaleax
authored andcommitted
report: use libuv calls for OS and machine info
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>
1 parent baa0865 commit 6d39a54

File tree

3 files changed

+27
-108
lines changed

3 files changed

+27
-108
lines changed

doc/api/report.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ is provided below for reference.
5757
"release": {
5858
"name": "node"
5959
},
60-
"osVersion": "Linux 3.10.0-862.el7.x86_64 #1 SMP Wed Mar 21 18:14:51 EDT 2018",
61-
"machine": "test_machine x86_64"
60+
"osName": "Linux",
61+
"osRelease": "3.10.0-862.el7.x86_64",
62+
"osVersion": "#1 SMP Wed Mar 21 18:14:51 EDT 2018",
63+
"osMachine": "x86_64",
64+
"host": "test_machine"
6265
},
6366
"javascriptStack": {
6467
"message": "Error: *** test-exception.js: throwing uncaught Error",

node.gyp

-6
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,11 @@
325325
['OS=="win"', {
326326
'libraries': [
327327
'dbghelp.lib',
328-
'Netapi32.lib',
329328
'PsApi.lib',
330329
'Ws2_32.lib',
331330
],
332331
'dll_files': [
333332
'dbghelp.dll',
334-
'Netapi32.dll',
335333
'PsApi.dll',
336334
'Ws2_32.dll',
337335
],
@@ -662,13 +660,11 @@
662660
['OS=="win"', {
663661
'libraries': [
664662
'dbghelp.lib',
665-
'Netapi32.lib',
666663
'PsApi.lib',
667664
'Ws2_32.lib',
668665
],
669666
'dll_files': [
670667
'dbghelp.dll',
671-
'Netapi32.dll',
672668
'PsApi.dll',
673669
'Ws2_32.dll',
674670
],
@@ -1027,13 +1023,11 @@
10271023
['OS=="win"', {
10281024
'libraries': [
10291025
'dbghelp.lib',
1030-
'Netapi32.lib',
10311026
'PsApi.lib',
10321027
'Ws2_32.lib',
10331028
],
10341029
'dll_files': [
10351030
'dbghelp.dll',
1036-
'Netapi32.dll',
10371031
'PsApi.dll',
10381032
'Ws2_32.dll',
10391033
],

src/node_report.cc

+22-100
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <cxxabi.h>
2929
#include <dlfcn.h>
3030
#include <inttypes.h>
31-
#include <sys/utsname.h>
3231
#endif
3332

3433
#include <fcntl.h>
@@ -48,6 +47,15 @@
4847
extern char** environ;
4948
#endif
5049

50+
#ifdef __POSIX__
51+
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
52+
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
53+
#endif // __POSIX__
54+
55+
#ifndef MAXHOSTNAMELEN
56+
# define MAXHOSTNAMELEN 256
57+
#endif // MAXHOSTNAMELEN
58+
5159
namespace report {
5260
using node::arraysize;
5361
using node::Environment;
@@ -351,107 +359,21 @@ static void PrintVersionInformation(JSONWriter* writer) {
351359
// Report release metadata.
352360
PrintRelease(writer);
353361

354-
// Report operating system and machine information (Windows)
355-
#ifdef _WIN32
356-
{
357-
// Level 101 to obtain the server name, type, and associated details.
358-
// ref: https://docs.microsoft.com/en-us/windows/desktop/
359-
// api/lmserver/nf-lmserver-netservergetinfo
360-
const DWORD level = 101;
361-
LPSERVER_INFO_101 os_info = nullptr;
362-
NET_API_STATUS nStatus =
363-
NetServerGetInfo(nullptr, level, reinterpret_cast<LPBYTE*>(&os_info));
364-
if (nStatus == NERR_Success) {
365-
LPSTR os_name = "Windows";
366-
const DWORD major = os_info->sv101_version_major & MAJOR_VERSION_MASK;
367-
const DWORD type = os_info->sv101_type;
368-
const bool isServer = (type & SV_TYPE_DOMAIN_CTRL) ||
369-
(type & SV_TYPE_DOMAIN_BAKCTRL) ||
370-
(type & SV_TYPE_SERVER_NT);
371-
switch (major) {
372-
case 5:
373-
switch (os_info->sv101_version_minor) {
374-
case 0:
375-
os_name = "Windows 2000";
376-
break;
377-
default:
378-
os_name = (isServer ? "Windows Server 2003" : "Windows XP");
379-
}
380-
break;
381-
case 6:
382-
switch (os_info->sv101_version_minor) {
383-
case 0:
384-
os_name = (isServer ? "Windows Server 2008" : "Windows Vista");
385-
break;
386-
case 1:
387-
os_name = (isServer ? "Windows Server 2008 R2" : "Windows 7");
388-
break;
389-
case 2:
390-
os_name = (isServer ? "Windows Server 2012" : "Windows 8");
391-
break;
392-
case 3:
393-
os_name = (isServer ? "Windows Server 2012 R2" : "Windows 8.1");
394-
break;
395-
default:
396-
os_name = (isServer ? "Windows Server" : "Windows Client");
397-
}
398-
break;
399-
case 10:
400-
os_name = (isServer ? "Windows Server 2016" : "Windows 10");
401-
break;
402-
default:
403-
os_name = (isServer ? "Windows Server" : "Windows Client");
404-
}
405-
writer->json_keyvalue("osVersion", os_name);
406-
407-
// Convert and report the machine name and comment fields
408-
// (these are LPWSTR types)
409-
size_t count;
410-
char name_buf[256];
411-
wcstombs_s(
412-
&count, name_buf, sizeof(name_buf), os_info->sv101_name, _TRUNCATE);
413-
if (os_info->sv101_comment != nullptr) {
414-
char comment_buf[256];
415-
wcstombs_s(&count,
416-
comment_buf,
417-
sizeof(comment_buf),
418-
os_info->sv101_comment,
419-
_TRUNCATE);
420-
buf << name_buf << " " << comment_buf;
421-
writer->json_keyvalue("machine", buf.str());
422-
buf.flush();
423-
} else {
424-
writer->json_keyvalue("machine", name_buf);
425-
}
362+
// Report operating system and machine information
363+
uv_utsname_t os_info;
426364

427-
if (os_info != nullptr) {
428-
NetApiBufferFree(os_info);
429-
}
430-
} else {
431-
// NetServerGetInfo() failed, fallback to use GetComputerName() instead
432-
TCHAR machine_name[256];
433-
DWORD machine_name_size = 256;
434-
writer->json_keyvalue("osVersion", "Windows");
435-
if (GetComputerName(machine_name, &machine_name_size)) {
436-
writer->json_keyvalue("machine", machine_name);
437-
}
438-
}
439-
}
440-
#else
441-
// Report operating system and machine information (Unix/OSX)
442-
struct utsname os_info;
443-
if (uname(&os_info) >= 0) {
444-
#ifdef _AIX
445-
buf << os_info.sysname << " " << os_info.version << "." << os_info.release;
446-
#else
447-
buf << os_info.sysname << " " << os_info.release << " " << os_info.version;
448-
#endif /* _AIX */
449-
writer->json_keyvalue("osVersion", buf.str());
450-
buf.str("");
451-
buf << os_info.nodename << " " << os_info.machine;
452-
writer->json_keyvalue("machine", buf.str());
365+
if (uv_os_uname(&os_info) == 0) {
366+
writer->json_keyvalue("osName", os_info.sysname);
367+
writer->json_keyvalue("osRelease", os_info.release);
368+
writer->json_keyvalue("osVersion", os_info.version);
369+
writer->json_keyvalue("osMachine", os_info.machine);
453370
}
454-
#endif
371+
372+
char host[MAXHOSTNAMELEN + 1];
373+
size_t host_size = sizeof(host);
374+
375+
if (uv_os_gethostname(host, &host_size) == 0)
376+
writer->json_keyvalue("host", host);
455377
}
456378

457379
// Report the JavaScript stack.

0 commit comments

Comments
 (0)