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 /src/mongo/util/version.cpp | |
parent | 9da7ca62a4bf54f0318eb4d632380741a54cb6bd (diff) | |
download | mongo-5ff59681de1f16392dd38658598996861b09d44e.tar.gz |
SERVER-56362 Add macOS version info to getBuildInfo()
Diffstat (limited to 'src/mongo/util/version.cpp')
-rw-r--r-- | src/mongo/util/version.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
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 { |