diff options
author | Matt Kneiser <matt.kneiser@mongodb.com> | 2022-07-05 17:09:20 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-07-07 18:26:31 +0000 |
commit | c44db4ac9d0c4c368835dac9f2fbd5869d885f17 (patch) | |
tree | c07de8fbd279573725d877619f263a684ec420f4 /src/mongo/shell | |
parent | 2f2dc04da0c7fd853a2ff68c245c5f6e28f50fa1 (diff) | |
download | mongo-c44db4ac9d0c4c368835dac9f2fbd5869d885f17.tar.gz |
SERVER-9434 Audit and Normalize Process Exit Codes
- Put all references to ExitCode enumerators under an enum class to namespace
all the enumerators and avoid polluting the mongo namespace with possible
naming collisions.
- Added magic-number exit codes to exit_code.h like 50 & 51
(LauncherMiddleError & LauncherError respectively).
- Reserved a range of exit codes to account for FreeBSD's usage of 64-78.
- Renamed all enums with removal of EXIT_ which could conflict or get confused
with built-in macros.
- camelCased all ExitCode enum values.
- Added the generic - ExitCode::fail - for returning 1 as failure.
- Added explicit dependency on the exit_code.h header for all files using
ExitCode's.
- Removed all references to implementation-defined exit codes like EXIT_FAILURE
and EXIT_SUCCESS.
- Narrowed return values of the shell's undocumented quit() argument to portable
values within 0-255, 0/ExitCode::clean otherwise.
- Deprecated 8 unused ExitCode's
- java
- oomMalloc
- oomRealloc
- fs
- clockSkew
- windowsServiceStop
- possibleCorruption
- test
- Wrapped the 2 Windows-only ExitCode's in #ifdef's
- ntServiceError
- windowsServiceStop
Diffstat (limited to 'src/mongo/shell')
-rw-r--r-- | src/mongo/shell/mongo_main.cpp | 10 | ||||
-rw-r--r-- | src/mongo/shell/servers.js | 6 | ||||
-rw-r--r-- | src/mongo/shell/shell_options_init.cpp | 4 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils_launcher.cpp | 15 |
4 files changed, 19 insertions, 16 deletions
diff --git a/src/mongo/shell/mongo_main.cpp b/src/mongo/shell/mongo_main.cpp index 51ccb760ee7..7b1dd9dce47 100644 --- a/src/mongo/shell/mongo_main.cpp +++ b/src/mongo/shell/mongo_main.cpp @@ -74,6 +74,7 @@ #include "mongo/util/ctype.h" #include "mongo/util/errno_util.h" #include "mongo/util/exit.h" +#include "mongo/util/exit_code.h" #include "mongo/util/file.h" #include "mongo/util/net/ocsp/ocsp_manager.h" #include "mongo/util/net/ssl_options.h" @@ -306,7 +307,7 @@ void killOps() { } extern "C" void quitNicely(int sig) { - shutdown(EXIT_CLEAN); + shutdown(ExitCode::clean); } // the returned string is allocated with strdup() or malloc() and must be freed by calling free() @@ -354,7 +355,7 @@ std::string getURIFromArgs(const std::string& arg, if ((arg.find('/') != std::string::npos) && (host.size() || port.size())) { std::cerr << "If a full URI is provided, you cannot also specify --host or --port" << std::endl; - quickExit(-1); + quickExit(ExitCode::badOptions); } const auto parseDbHost = [port](const std::string& db, const std::string& host) -> std::string { @@ -422,7 +423,7 @@ std::string getURIFromArgs(const std::string& arg, std::cerr << "connection string bears different port than provided by --port" << std::endl; - quickExit(-1); + quickExit(ExitCode::badOptions); } ss << ':' << uriEncode(myport); } else if (port.size()) { @@ -900,7 +901,8 @@ int mongo_main(int argc, char* argv[]) { pids.begin(), pids.end(), std::ostream_iterator<ProcessId>(std::cout, " ")); std::cout << std::endl; - if (mongo::shell_utils::KillMongoProgramInstances() != EXIT_SUCCESS) { + if (mongo::shell_utils::KillMongoProgramInstances() != + static_cast<int>(ExitCode::clean)) { std::cout << "one more more child processes exited with an error during " << shellGlobalParams.files[i] << std::endl; std::cout << "exiting with code " << static_cast<int>(kProcessTerminationError) diff --git a/src/mongo/shell/servers.js b/src/mongo/shell/servers.js index c31bc887c69..1fbc8c04a43 100644 --- a/src/mongo/shell/servers.js +++ b/src/mongo/shell/servers.js @@ -1085,7 +1085,7 @@ MongoRunner.StopError.prototype.constructor = MongoRunner.StopError; // Constants for exit codes of MongoDB processes // On Windows, std::abort causes the process to exit with return code 14. -MongoRunner.EXIT_ABORT = _isWindows() ? 14 : -6; +MongoRunner.EXIT_ABORT = _isWindows() ? 14 : 6; MongoRunner.EXIT_CLEAN = 0; MongoRunner.EXIT_BADOPTIONS = 2; MongoRunner.EXIT_REPLICATION_ERROR = 3; @@ -1093,7 +1093,7 @@ MongoRunner.EXIT_NEED_UPGRADE = 4; MongoRunner.EXIT_SHARDING_ERROR = 5; // SIGKILL is translated to TerminateProcess() on Windows, which causes the program to // terminate with exit code 1. -MongoRunner.EXIT_SIGKILL = _isWindows() ? 1 : -9; +MongoRunner.EXIT_SIGKILL = _isWindows() ? 1 : 9; MongoRunner.EXIT_KILL = 12; MongoRunner.EXIT_ABRUPT = 14; MongoRunner.EXIT_NTSERVICE_ERROR = 20; @@ -1143,7 +1143,7 @@ var stopMongoProgram = function(conn, signal, opts, waitpid) { "it is usually the object returned from MongoRunner.runMongod/s"); } - signal = parseInt(signal) || 15; + signal = parseInt(signal) || SIGTERM; opts = opts || {}; waitpid = (waitpid === undefined) ? true : waitpid; diff --git a/src/mongo/shell/shell_options_init.cpp b/src/mongo/shell/shell_options_init.cpp index b89b024a2b6..6924eaa1954 100644 --- a/src/mongo/shell/shell_options_init.cpp +++ b/src/mongo/shell/shell_options_init.cpp @@ -40,7 +40,7 @@ namespace mongo { MONGO_STARTUP_OPTIONS_VALIDATE(MongoShellOptions)(InitializerContext* context) { if (!handlePreValidationMongoShellOptions(moe::startupOptionsParsed, context->args())) { - quickExit(EXIT_SUCCESS); + quickExit(ExitCode::clean); } uassertStatusOK(moe::startupOptionsParsed.validate()); } @@ -50,7 +50,7 @@ MONGO_STARTUP_OPTIONS_STORE(MongoShellOptions)(InitializerContext* context) { if (!ret.isOK()) { std::cerr << ret.toString() << std::endl; std::cerr << "try '" << context->args()[0] << " --help' for more information" << std::endl; - quickExit(EXIT_BADOPTIONS); + quickExit(ExitCode::badOptions); } } } // namespace mongo diff --git a/src/mongo/shell/shell_utils_launcher.cpp b/src/mongo/shell/shell_utils_launcher.cpp index 6518ec284c0..195dc6b19f9 100644 --- a/src/mongo/shell/shell_utils_launcher.cpp +++ b/src/mongo/shell/shell_utils_launcher.cpp @@ -72,6 +72,7 @@ #include "mongo/util/ctype.h" #include "mongo/util/destructor_guard.h" #include "mongo/util/exit.h" +#include "mongo/util/exit_code.h" #include "mongo/util/net/hostandport.h" #include "mongo/util/quick_exit.h" #include "mongo/util/scopeguard.h" @@ -304,7 +305,7 @@ bool ProgramRegistry::waitForPid(const ProcessId pid, const bool block, int* con if (WIFEXITED(status)) { code = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { - code = -WTERMSIG(status); + code = WTERMSIG(status); } else { MONGO_UNREACHABLE; } @@ -821,7 +822,7 @@ void ProgramRunner::launchProcess(int child_stdout) { if (dup2(child_stdout, STDOUT_FILENO) == -1 || dup2(child_stdout, STDERR_FILENO) == -1) { // Async signal unsafe code reporting a terminal error condition. perror("Unable to dup2 child output: "); - _exit(-1); // do not pass go, do not call atexit handlers + _exit(static_cast<int>(ExitCode::fail)); // do not pass go, do not call atexit handlers } execve(argvStorage[0], @@ -831,7 +832,7 @@ void ProgramRunner::launchProcess(int child_stdout) { // Async signal unsafe code reporting a terminal error condition. perror(execErrMsg.c_str()); - _exit(-1); + _exit(static_cast<int>(ExitCode::fail)); } #endif @@ -1146,13 +1147,13 @@ int killDb(int port, ProcessId _pid, int signal, const BSONObj& opt, bool waitPi return 0; } - int exitCode = EXIT_FAILURE; + int exitCode = static_cast<int>(ExitCode::fail); try { LOGV2_INFO(22819, "Waiting for process to terminate.", "pid"_attr = pid); registry.waitForPid(pid, true, &exitCode); } catch (...) { LOGV2_WARNING(22828, "Process failed to terminate.", "pid"_attr = pid); - return EXIT_FAILURE; + return static_cast<int>(ExitCode::fail); } if (signal == SIGKILL) { @@ -1244,11 +1245,11 @@ BSONObj ConvertTrafficRecordingToBSON(const BSONObj& a, void* data) { int KillMongoProgramInstances() { vector<ProcessId> pids; registry.getRegisteredPids(pids); - int returnCode = EXIT_SUCCESS; + int returnCode = static_cast<int>(ExitCode::clean); for (auto&& pid : pids) { int port = registry.portForPid(pid); int code = killDb(port != -1 ? port : 0, pid, SIGTERM); - if (code != EXIT_SUCCESS) { + if (code != static_cast<int>(ExitCode::clean)) { LOGV2_INFO( 22823, "Process exited with error code", "pid"_attr = pid, "code"_attr = code); returnCode = code; |