summaryrefslogtreecommitdiff
path: root/src/mongo/db/server_options.cpp
diff options
context:
space:
mode:
authorShaun Verch <shaun.verch@10gen.com>2013-09-18 14:02:36 -0400
committerShaun Verch <shaun.verch@10gen.com>2013-10-04 16:58:54 -0400
commit6fc951d492881a32754bc3e38e8b5eca78929197 (patch)
tree73c2afe3120a8a87cf3bc8379e53461b2cf05b1e /src/mongo/db/server_options.cpp
parent99372153dfe9dd9251e49371d872b5f27dfd6f2c (diff)
downloadmongo-6fc951d492881a32754bc3e38e8b5eca78929197.tar.gz
SERVER-8510 Get rid of CmdLine struct and use new option handling style
Diffstat (limited to 'src/mongo/db/server_options.cpp')
-rw-r--r--src/mongo/db/server_options.cpp273
1 files changed, 271 insertions, 2 deletions
diff --git a/src/mongo/db/server_options.cpp b/src/mongo/db/server_options.cpp
index bbc4957ee66..6c27cae17ef 100644
--- a/src/mongo/db/server_options.cpp
+++ b/src/mongo/db/server_options.cpp
@@ -28,10 +28,19 @@
#include "mongo/db/server_options.h"
+#ifdef _WIN32
+#include <direct.h>
+#endif
+
#include "mongo/base/status.h"
#include "mongo/bson/util/builder.h"
-#include "mongo/db/cmdline.h" // For CmdLine::DefaultDBPort
+#include "mongo/db/server_parameters.h"
+#include "mongo/logger/message_event_utf8_encoder.h"
+#include "mongo/util/cmdline_utils/censor_cmdline.h"
+#include "mongo/util/map_util.h"
+#include "mongo/util/mongoutils/str.h"
#include "mongo/util/net/listen.h" // For DEFAULT_MAX_CONN
+#include "mongo/util/net/ssl_options.h"
#include "mongo/util/options_parser/option_description.h"
#include "mongo/util/options_parser/option_section.h"
@@ -40,11 +49,16 @@ namespace mongo {
typedef moe::OptionDescription OD;
typedef moe::PositionalOptionDescription POD;
+ moe::OptionSection serverOptions;
+ moe::Environment serverParsedOptions;
+
+ ServerGlobalParams serverGlobalParams;
+
Status addGeneralServerOptions(moe::OptionSection* options) {
StringBuilder portInfoBuilder;
StringBuilder maxConnInfoBuilder;
- portInfoBuilder << "specify port number - " << CmdLine::DefaultDBPort << " by default";
+ portInfoBuilder << "specify port number - " << ServerGlobalParams::DefaultDBPort << " by default";
maxConnInfoBuilder << "max number of simultaneous connections - "
<< DEFAULT_MAX_CONN << " by default";
@@ -252,4 +266,259 @@ namespace mongo {
return Status::OK();
}
+ namespace {
+ // Helpers for option storage
+ Status setupBinaryName(const std::vector<std::string>& argv) {
+
+ if (argv.empty()) {
+ return Status(ErrorCodes::InternalError, "Cannot get binary name: argv array is empty");
+ }
+
+ // setup binary name
+ serverGlobalParams.binaryName = argv[0];
+ size_t i = serverGlobalParams.binaryName.rfind('/');
+ if (i != string::npos) {
+ serverGlobalParams.binaryName = serverGlobalParams.binaryName.substr(i + 1);
+ }
+ return Status::OK();
+ }
+
+ Status setupCwd() {
+ // setup cwd
+ char buffer[1024];
+#ifdef _WIN32
+ verify(_getcwd(buffer, 1000));
+#else
+ verify(getcwd(buffer, 1000));
+#endif
+ serverGlobalParams.cwd = buffer;
+ return Status::OK();
+ }
+
+ Status setArgvArray(const std::vector<std::string>& argv) {
+ BSONArrayBuilder b;
+ std::vector<std::string> censoredArgv = argv;
+ cmdline_utils::censorArgsVector(&censoredArgv);
+ for (size_t i=0; i < censoredArgv.size(); i++) {
+ b << censoredArgv[i];
+ }
+ serverGlobalParams.argvArray = b.arr();
+ return Status::OK();
+ }
+
+ Status setParsedOpts(const moe::Environment& params) {
+ serverGlobalParams.parsedOpts = params.toBSON();
+ cmdline_utils::censorBSONObj(&serverGlobalParams.parsedOpts);
+ return Status::OK();
+ }
+ } //namespace
+
+ void printCommandLineOpts() {
+ log() << "options: " << serverGlobalParams.parsedOpts << endl;
+ }
+
+ Status storeServerOptions(const moe::Environment& params,
+ const std::vector<std::string>& args) {
+
+ Status ret = setupBinaryName(args);
+ if (!ret.isOK()) {
+ return ret;
+ }
+
+ ret = setupCwd();
+ if (!ret.isOK()) {
+ return ret;
+ }
+
+ ret = setArgvArray(args);
+ if (!ret.isOK()) {
+ return ret;
+ }
+
+ ret = setParsedOpts(params);
+ if (!ret.isOK()) {
+ return ret;
+ }
+
+ if (params.count("verbose")) {
+ logger::globalLogDomain()->setMinimumLoggedSeverity(logger::LogSeverity::Debug(1));
+ }
+
+ for (string s = "vv"; s.length() <= 12; s.append("v")) {
+ if (params.count(s)) {
+ logger::globalLogDomain()->setMinimumLoggedSeverity(
+ logger::LogSeverity::Debug(s.length()));
+ }
+ }
+
+ if (params.count("enableExperimentalIndexStatsCmd")) {
+ serverGlobalParams.experimental.indexStatsCmdEnabled = true;
+ }
+ if (params.count("enableExperimentalStorageDetailsCmd")) {
+ serverGlobalParams.experimental.storageDetailsCmdEnabled = true;
+ }
+
+ if (params.count("port")) {
+ serverGlobalParams.port = params["port"].as<int>();
+ }
+
+ if (params.count("bind_ip")) {
+ serverGlobalParams.bind_ip = params["bind_ip"].as<std::string>();
+ }
+
+ if (params.count("clusterAuthMode")) {
+ serverGlobalParams.clusterAuthMode = params["clusterAuthMode"].as<std::string>();
+ }
+
+ if (params.count("quiet")) {
+ serverGlobalParams.quiet = true;
+ }
+
+ if (params.count("traceExceptions")) {
+ DBException::traceExceptions = true;
+ }
+
+ if (params.count("maxConns")) {
+ serverGlobalParams.maxConns = params["maxConns"].as<int>();
+
+ if (serverGlobalParams.maxConns < 5) {
+ return Status(ErrorCodes::BadValue, "maxConns has to be at least 5");
+ }
+ }
+
+ if (params.count("objcheck")) {
+ serverGlobalParams.objcheck = true;
+ }
+ if (params.count("noobjcheck")) {
+ if (params.count("objcheck")) {
+ return Status(ErrorCodes::BadValue, "can't have both --objcheck and --noobjcheck");
+ }
+ serverGlobalParams.objcheck = false;
+ }
+
+ if (params.count("bind_ip")) {
+ // passing in wildcard is the same as default behavior; remove and warn
+ if (serverGlobalParams.bind_ip == "0.0.0.0") {
+ std::cout << "warning: bind_ip of 0.0.0.0 is unnecessary; "
+ << "listens on all ips by default" << endl;
+ serverGlobalParams.bind_ip = "";
+ }
+ }
+
+#ifndef _WIN32
+ if (params.count("unixSocketPrefix")) {
+ serverGlobalParams.socket = params["unixSocketPrefix"].as<string>();
+ }
+
+ if (params.count("nounixsocket")) {
+ serverGlobalParams.noUnixSocket = true;
+ }
+
+ if (params.count("fork") && !params.count("shutdown")) {
+ serverGlobalParams.doFork = true;
+ }
+#endif // _WIN32
+
+ if (params.count("logTimestampFormat")) {
+ using logger::MessageEventDetailsEncoder;
+ std::string formatterName = params["logTimestampFormat"].as<string>();
+ if (formatterName == "ctime") {
+ MessageEventDetailsEncoder::setDateFormatter(dateToCtimeString);
+ }
+ else if (formatterName == "iso8601-utc") {
+ MessageEventDetailsEncoder::setDateFormatter(dateToISOStringUTC);
+ }
+ else if (formatterName == "iso8601-local") {
+ MessageEventDetailsEncoder::setDateFormatter(dateToISOStringLocal);
+ }
+ else {
+ StringBuilder sb;
+ sb << "Value of logTimestampFormat must be one of ctime, iso8601-utc " <<
+ "or iso8601-local; not \"" << formatterName << "\".";
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+ }
+ if (params.count("logpath")) {
+ serverGlobalParams.logpath = params["logpath"].as<string>();
+ if (serverGlobalParams.logpath.empty()) {
+ return Status(ErrorCodes::BadValue, "logpath cannot be empty if supplied");
+ }
+ }
+
+ serverGlobalParams.logWithSyslog = params.count("syslog");
+ serverGlobalParams.logAppend = params.count("logappend");
+ if (!serverGlobalParams.logpath.empty() && serverGlobalParams.logWithSyslog) {
+ return Status(ErrorCodes::BadValue, "Cant use both a logpath and syslog ");
+ }
+
+ if (serverGlobalParams.doFork && serverGlobalParams.logpath.empty() &&
+ !serverGlobalParams.logWithSyslog) {
+ return Status(ErrorCodes::BadValue, "--fork has to be used with --logpath or --syslog");
+ }
+
+ if (params.count("keyFile")) {
+ serverGlobalParams.keyFile = params["keyFile"].as<string>();
+ }
+
+ if ( params.count("pidfilepath")) {
+ serverGlobalParams.pidFile = params["pidfilepath"].as<string>();
+ }
+
+ if (params.count("setParameter")) {
+ std::vector<std::string> parameters =
+ params["setParameter"].as<std::vector<std::string> >();
+ for (size_t i = 0, length = parameters.size(); i < length; ++i) {
+ std::string name;
+ std::string value;
+ if (!mongoutils::str::splitOn(parameters[i], '=', name, value)) {
+ StringBuilder sb;
+ sb << "Illegal option assignment: \"" << parameters[i] << "\"";
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+ ServerParameter* parameter = mapFindWithDefault(
+ ServerParameterSet::getGlobal()->getMap(),
+ name,
+ static_cast<ServerParameter*>(NULL));
+ if (NULL == parameter) {
+ StringBuilder sb;
+ sb << "Illegal --setParameter parameter: \"" << name << "\"";
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+ if (!parameter->allowedToChangeAtStartup()) {
+ StringBuilder sb;
+ sb << "Cannot use --setParameter to set \"" << name << "\" at startup";
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+ Status status = parameter->setFromString(value);
+ if (!status.isOK()) {
+ StringBuilder sb;
+ sb << "Bad value for parameter \"" << name << "\": " << status.reason();
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+ }
+ }
+ if (!params.count("clusterAuthMode") && params.count("keyFile")){
+ serverGlobalParams.clusterAuthMode = "keyfile";
+ }
+
+#ifdef MONGO_SSL
+ ret = storeSSLServerOptions(params);
+ if (!ret.isOK()) {
+ return ret;
+ }
+#else // ifdef MONGO_SSL
+ // Keyfile is currently the only supported value if not using SSL
+ if (params.count("clusterAuthMode") && serverGlobalParams.clusterAuthMode != "keyfile") {
+ StringBuilder sb;
+ sb << "unsupported value for clusterAuthMode " << serverGlobalParams.clusterAuthMode;
+ return Status(ErrorCodes::BadValue, sb.str());
+ }
+#endif
+
+ return Status::OK();
+ }
+
+ // FIXME: This function will not return the correct value if someone renames the mongos binary
+ bool isMongos() { return serverGlobalParams.binaryName == "mongos"; }
+
} // namespace mongo