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 #include "mongo/base/initializer.h"
32
33 #include "mongo/base/global_initializer.h"
34 #include "mongo/util/assert_util.h"
35 #include "mongo/util/quick_exit.h"
36 #include <iostream>
37
38 namespace mongo {
39
Initializer()40 Initializer::Initializer() {}
~Initializer()41 Initializer::~Initializer() {}
42
execute(const InitializerContext::ArgumentVector & args,const InitializerContext::EnvironmentMap & env) const43 Status Initializer::execute(const InitializerContext::ArgumentVector& args,
44 const InitializerContext::EnvironmentMap& env) const {
45 std::vector<std::string> sortedNodes;
46 Status status = _graph.topSort(&sortedNodes);
47 if (Status::OK() != status)
48 return status;
49
50 InitializerContext context(args, env);
51
52 for (size_t i = 0; i < sortedNodes.size(); ++i) {
53 InitializerFunction fn = _graph.getInitializerFunction(sortedNodes[i]);
54 if (!fn) {
55 return Status(ErrorCodes::InternalError,
56 "topSort returned a node that has no associated function: \"" +
57 sortedNodes[i] + '"');
58 }
59 try {
60 status = fn(&context);
61 } catch (const DBException& xcp) {
62 return xcp.toStatus();
63 }
64
65 if (Status::OK() != status)
66 return status;
67 }
68 return Status::OK();
69 }
70
runGlobalInitializers(const InitializerContext::ArgumentVector & args,const InitializerContext::EnvironmentMap & env)71 Status runGlobalInitializers(const InitializerContext::ArgumentVector& args,
72 const InitializerContext::EnvironmentMap& env) {
73 return getGlobalInitializer().execute(args, env);
74 }
75
runGlobalInitializers(int argc,const char * const * argv,const char * const * envp)76 Status runGlobalInitializers(int argc, const char* const* argv, const char* const* envp) {
77 InitializerContext::ArgumentVector args(argc);
78 std::copy(argv, argv + argc, args.begin());
79
80 InitializerContext::EnvironmentMap env;
81
82 if (envp) {
83 for (; *envp; ++envp) {
84 const char* firstEqualSign = strchr(*envp, '=');
85 if (!firstEqualSign) {
86 return Status(ErrorCodes::BadValue, "malformed environment block");
87 }
88 env[std::string(*envp, firstEqualSign)] = std::string(firstEqualSign + 1);
89 }
90 }
91
92 return runGlobalInitializers(args, env);
93 }
94
runGlobalInitializersOrDie(int argc,const char * const * argv,const char * const * envp)95 void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp) {
96 Status status = runGlobalInitializers(argc, argv, envp);
97 if (!status.isOK()) {
98 std::cerr << "Failed global initialization: " << status << std::endl;
99 quickExit(1);
100 }
101 }
102
103 } // namespace mongo
104