Skip to content

Commit c74d4e5

Browse files
author
Bogdan Degtyariov
committed
Bug 29394723/94444 REPORT RUN-TIME INFORMATION IN CONNECTION ATTRIBUTES, INSTEAD OF BUILT-TIME
1 parent 0802ab3 commit c74d4e5

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

common/session.cc

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ POP_SYS_WARNINGS
4747
#ifndef _WIN32
4848
#include <sys/types.h>
4949
#include <unistd.h>
50+
#include <sys/utsname.h>
5051
#endif
5152

5253
using namespace ::mysqlx::common;
@@ -85,22 +86,109 @@ void Settings_impl::set_client_opts(const Settings_impl &opts)
8586
set.commit();
8687
}
8788

89+
/*
90+
Get information about OS and platform architecture.
91+
platform - an output parameter containig the string with the
92+
platform architecture (such as 'i386' or 'x86_64' etc)
93+
94+
Returns the string containing the OS type and its version.
95+
Note: it returns the version, not the number in the name of the OS.
96+
In Windows it will be Windows-6.3.x instead of Windows-8.1
97+
*/
98+
std::string get_os_version_info(std::string &platform)
99+
{
100+
std::stringstream ver_info;
101+
#ifdef _WIN32
102+
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
103+
typedef long (NTAPI *tRtlGetVersion)(OSVERSIONINFO*);
104+
tRtlGetVersion pRtlGetVersion = nullptr;
105+
OSVERSIONINFO ver;
106+
_SYSTEM_INFO hw_info;
107+
108+
memset(&ver, 0, sizeof(OSVERSIONINFO));
109+
ver.dwOSVersionInfoSize = sizeof(sizeof(OSVERSIONINFO));
110+
111+
if (ntdll != nullptr)
112+
pRtlGetVersion = (tRtlGetVersion)GetProcAddress(ntdll, "RtlGetVersion");
113+
114+
if (pRtlGetVersion)
115+
{
116+
pRtlGetVersion(&ver);
117+
}
118+
else
119+
{
120+
PUSH_SYS_WARNINGS
121+
DISABLE_WARNING(4996)
122+
if (GetVersionEx(&ver) == 0)
123+
ver_info << "<unknown>";
124+
POP_SYS_WARNINGS
125+
}
126+
127+
// Check if version info was set to <unknown> because of error
128+
if (ver_info.str().length() == 0)
129+
ver_info << "Windows-"
130+
<< ver.dwMajorVersion << "."
131+
<< ver.dwMinorVersion << "."
132+
<< ver.dwBuildNumber;
133+
134+
GetSystemInfo(&hw_info);
135+
switch (hw_info.wProcessorArchitecture)
136+
{
137+
case PROCESSOR_ARCHITECTURE_AMD64:
138+
platform = "x86_64"; break;
139+
case PROCESSOR_ARCHITECTURE_ARM:
140+
platform = "arm"; break;
141+
case PROCESSOR_ARCHITECTURE_IA64:
142+
platform = "ia64"; break;
143+
case PROCESSOR_ARCHITECTURE_INTEL:
144+
platform = "i386"; break;
145+
case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
146+
platform = "i686"; break;
147+
case PROCESSOR_ARCHITECTURE_PPC:
148+
platform = "powerpc"; break;
149+
case PROCESSOR_ARCHITECTURE_MIPS:
150+
platform = "mips"; break;
151+
case PROCESSOR_ARCHITECTURE_ALPHA:
152+
case PROCESSOR_ARCHITECTURE_ALPHA64:
153+
platform = "alpha"; break;
154+
default:
155+
platform = "<unknown>";
156+
}
157+
158+
#else
159+
struct utsname ver;
160+
if (uname(&ver) == -1)
161+
{
162+
ver_info << "<unknown>";
163+
platform = "<unknown>";
164+
}
165+
else
166+
{
167+
ver_info << ver.sysname << "-" << ver.release;
168+
platform = ver.machine;
169+
}
170+
#endif
171+
172+
return ver_info.str();
173+
}
174+
88175
void mysqlx::common::Settings_impl::Data::init_connection_attr()
89176
{
90177
//Already initialized... nothing to do here!
91178
if (!m_connection_attr.empty())
92179
return;
93180

94181
std::string pid;
182+
std::string platform;
95183
#ifdef _WIN32
96184
pid = std::to_string(GetCurrentProcessId());
97185
#else
98186
pid = std::to_string(getpid());
99187
#endif
100188
m_connection_attr["_pid"] = pid;
101189

102-
m_connection_attr["_platform"] = MACHINE_TYPE;
103-
m_connection_attr["_os"] = SYSTEM_TYPE;
190+
m_connection_attr["_os"] = get_os_version_info(platform);
191+
m_connection_attr["_platform"] = platform;
104192
m_connection_attr["_source_host"] =
105193
cdk::foundation::connection::get_local_hostname();
106194
m_connection_attr["_client_name"] = PACKAGE_NAME;

devapi/tests/bugs-t.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,29 @@ TEST_F(Bugs, is_false)
660660
auto tbl = schema.getCollectionAsTable("is_false");
661661
EXPECT_EQ(1, tbl.select().where("doc->$.val is false").execute().count());
662662
}
663+
664+
TEST_F(Bugs, bug29394723)
665+
{
666+
SKIP_IF_NO_XPLUGIN
667+
// Check that the _os session attribute is present and not empty
668+
string _os = get_sess().sql("SELECT ATTR_VALUE FROM "
669+
"performance_schema.session_account_connect_attrs "
670+
"WHERE ATTR_NAME = '_os' AND "
671+
"PROCESSLIST_ID = CONNECTION_ID() AND "
672+
"LENGTH(ATTR_VALUE) > 0").execute().
673+
fetchOne()[0].get<string>();
674+
675+
cout << "_os: " << _os << endl;
676+
EXPECT_NE("", _os);
677+
678+
// Check that the _platform session attribute is present and not empty
679+
string _platform = get_sess().sql("SELECT ATTR_VALUE FROM "
680+
"performance_schema.session_account_connect_attrs "
681+
"WHERE ATTR_NAME = '_platform' AND "
682+
"PROCESSLIST_ID = CONNECTION_ID() AND "
683+
"LENGTH(ATTR_VALUE) > 0").execute().
684+
fetchOne()[0].get<string>();
685+
686+
cout << "_platform: " << _platform << endl;
687+
EXPECT_NE("", _platform);
688+
}

0 commit comments

Comments
 (0)