diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2021-04-26 19:06:11 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-04-30 16:13:34 +0000 |
commit | 5ff59681de1f16392dd38658598996861b09d44e (patch) | |
tree | f63e6f7bbd786861f5da39751c15c73c31eb32a3 | |
parent | 9da7ca62a4bf54f0318eb4d632380741a54cb6bd (diff) | |
download | mongo-5ff59681de1f16392dd38658598996861b09d44e.tar.gz |
SERVER-56362 Add macOS version info to getBuildInfo()
-rw-r--r-- | jstests/ssl/libs/ssl_helpers.js | 27 | ||||
-rw-r--r-- | src/mongo/util/version.cpp | 32 |
2 files changed, 59 insertions, 0 deletions
diff --git a/jstests/ssl/libs/ssl_helpers.js b/jstests/ssl/libs/ssl_helpers.js index baa40b148cc..47edce2196e 100644 --- a/jstests/ssl/libs/ssl_helpers.js +++ b/jstests/ssl/libs/ssl_helpers.js @@ -235,6 +235,33 @@ function determineSSLProvider() { } } +function isMacOS(minVersion) { + 'use strict'; + + function parseVersion(version) { + // Intentionally leave the end of string unanchored. + // This allows vesions like: 10.15.7-pl2 or other extra data. + const v = version.match(/^(\d+)\.(\d+)\.(\d+)/); + assert(v !== null, "Invalid version string '" + version + "'"); + return (v[1] << 16) | (v[2] << 8) | (v[3]); + } + + const macOS = getBuildInfo().macOS; + if (macOS === undefined) { + // Not macOS at all. + return false; + } + + if (minVersion === undefined) { + // Don't care what version, but it's macOS. + return true; + } + + assert(macOS.osProductVersion !== undefined, + "Expected getBuildInfo() field 'macOS.osProductVersion' not present"); + return parseVersion(minVersion) <= parseVersion(macOS.osProductVersion); +} + function requireSSLProvider(required, fn) { 'use strict'; if ((typeof required) === 'string') { diff --git a/src/mongo/util/version.cpp b/src/mongo/util/version.cpp index eb745616e30..ae021552818 100644 --- a/src/mongo/util/version.cpp +++ b/src/mongo/util/version.cpp @@ -44,6 +44,10 @@ #include <fmt/format.h> #include <sstream> +#ifdef __APPLE__ +#include <sys/sysctl.h> +#endif + #include "mongo/base/string_data.h" #include "mongo/bson/bsonobj.h" #include "mongo/bson/json.h" @@ -131,6 +135,31 @@ std::string VersionInfoInterface::makeVersionString(StringData binaryName) const return format(FMT_STRING("{} v{}"), binaryName, version()); } +#ifdef __APPLE__ +namespace { +const std::map<std::string, std::string> kMacOSInfoMap = { + // sysctl name, followed by label for returned info object. + {"kern.osproductversion", "osProductVersion"}, + {"kern.osrelease", "osRelease"}, + {"kern.version", "version"}, +}; +// Collect macOS specific detail about the running version. +void appendMacOSInfo(BSONObjBuilder* builder) { + BSONObjBuilder macOS(builder->subobjStart("macOS")); + char buffer[2048]; + + for (const auto& item : kMacOSInfoMap) { + std::size_t buffer_len = sizeof(buffer) - 1; + if ((sysctlbyname(item.first.c_str(), buffer, &buffer_len, nullptr, 0) == 0) && + (buffer_len > 1)) { + // buffer_len returned by macOS includes the trailing nul byte. + macOS.append(item.second, StringData(buffer, buffer_len - 1)); + } + } +} +} // namespace +#endif + void VersionInfoInterface::appendBuildInfo(BSONObjBuilder* result) const { BSONObjBuilder& o = *result; o.append("version", version()); @@ -177,6 +206,9 @@ void VersionInfoInterface::appendBuildInfo(BSONObjBuilder* result) const { o.append("bits", (int)sizeof(void*) * CHAR_BIT); o.appendBool("debug", kDebugBuild); o.appendNumber("maxBsonObjectSize", BSONObjMaxUserSize); +#ifdef __APPLE__ + appendMacOSInfo(&o); +#endif } std::string VersionInfoInterface::openSSLVersion(StringData prefix, StringData suffix) const { |