1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #ifndef UTIL_VERSION_HEADER
32 #define UTIL_VERSION_HEADER
33 
34 #include <string>
35 #include <tuple>
36 #include <vector>
37 
38 #include "mongo/base/disallow_copying.h"
39 #include "mongo/base/string_data.h"
40 
41 namespace mongo {
42 
43 class BSONObjBuilder;
44 
45 /**
46  * An interface for accessing version information about the current process. A singleton instance of
47  * this interface is expected to be available via the 'instance' method in processes that need to be
48  * able to access version information.
49  */
50 class VersionInfoInterface {
51     MONGO_DISALLOW_COPYING(VersionInfoInterface);
52 
53 public:
54     using BuildInfoTuple = std::tuple<StringData, StringData, bool, bool>;
55 
56     virtual ~VersionInfoInterface() = default;
57 
58     /**
59      * The provided implementation of this interface will be returned by the 'instance' method
60      * below. Ownership of the object is not transferred.
61      */
62     static void enable(const VersionInfoInterface* handler);
63 
64     enum class NotEnabledAction {
65         kAbortProcess,
66         kFallback,
67     };
68 
69     /**
70      * Obtain the currently configured instance of the VersionInfoInterface. By default, if this
71      * method is called and no implementation has been configured with the 'enable' method above,
72      * the process will be terminated. If it is not acceptable to terminate the process, the above
73      * 'kFallback' constant can be provided and defaulted information will be provided.
74      */
75     static const VersionInfoInterface& instance(
76         NotEnabledAction action = NotEnabledAction::kAbortProcess) noexcept;
77 
78     /**
79      * Returns the major version as configured via MONGO_VERSION.
80      */
81     virtual int majorVersion() const noexcept = 0;
82 
83     /**
84      * Returns the minor version as configured via MONGO_VERSION.
85      */
86     virtual int minorVersion() const noexcept = 0;
87 
88     /**
89      * Returns the patch version as configured via MONGO_VERSION.
90      */
91     virtual int patchVersion() const noexcept = 0;
92 
93     /**
94      * Returns the extra version as configured via MONGO_VERSION.
95      */
96     virtual int extraVersion() const noexcept = 0;
97 
98     /**
99      * Returns a string representation of MONGO_VERSION.
100      */
101     virtual StringData version() const noexcept = 0;
102 
103     /**
104      * Returns a string representation of MONGO_GIT_HASH.
105      */
106     virtual StringData gitVersion() const noexcept = 0;
107 
108     /**
109      * Returns a vector describing the enabled modules.
110      */
111     virtual std::vector<StringData> modules() const = 0;
112 
113     /**
114      * Returns a string describing the configured memory allocator.
115      */
116     virtual StringData allocator() const noexcept = 0;
117 
118     /**
119      * Returns a string describing the configured javascript engine.
120      */
121     virtual StringData jsEngine() const noexcept = 0;
122 
123     /**
124      * Returns a string describing the minimum requred OS. Note that this method is currently only
125      * valid to call when running on Windows.
126      */
127     virtual StringData targetMinOS() const noexcept = 0;
128 
129     /**
130      * Returns a vector of tuples describing build information (e.g. LINKFLAGS, compiler, etc.).
131      */
132     virtual std::vector<BuildInfoTuple> buildInfo() const = 0;
133 
134     /**
135      * Returns the version of OpenSSL in use, if any, adorned with the provided prefix and suffix.
136      */
137     std::string openSSLVersion(StringData prefix = "", StringData suffix = "") const;
138 
139     /**
140      * Returns true if the running version has the same major and minor version as the provided
141      * string. Note that the minor version is checked, despite the name of this function.
142      */
143     bool isSameMajorVersion(const char* otherVersion) const noexcept;
144 
145     /**
146      * Uses the provided text to make a pretty representation of the version.
147      */
148     std::string makeVersionString(StringData binaryName) const;
149 
150     /**
151      * Appends the information associated with 'buildInfo', above, to the given builder.
152      */
153     void appendBuildInfo(BSONObjBuilder* result) const;
154 
155     /**
156      * Logs the result of 'targetMinOS', above.
157      */
158     void logTargetMinOS() const;
159 
160     /**
161      * Logs the result of 'buildInfo', above.
162      */
163     void logBuildInfo() const;
164 
165 protected:
166     constexpr VersionInfoInterface() = default;
167 };
168 
169 /**
170  * Returns a pretty string describing the current shell version.
171  */
172 std::string mongoShellVersion(const VersionInfoInterface& provider);
173 
174 /**
175  * Returns a pretty string describing the current mongos version.
176  */
177 std::string mongosVersion(const VersionInfoInterface& provider);
178 
179 /**
180  * Returns a pretty string describing the current mongod version.
181  */
182 std::string mongodVersion(const VersionInfoInterface& provider);
183 
184 }  // namespace mongo
185 
186 #endif  // UTIL_VERSION_HEADER
187