Skip to content

Commit

Permalink
Fix exit codes:
Browse files Browse the repository at this point in the history
- `--help`, `--version` etc should exit with `0` i.e. no error ("not enough args" case should still trigger an error)
- error reading config file should exit with `1`

Slightly refactor AppInitRPC/AppInitRawTx to return standard exit codes (EXIT_FAILURE/EXIT_SUCCESS) or CONTINUE_EXECUTION (-1)

Github-Pull: bitcoin#9067
Rebased-From: bd0de13
  • Loading branch information
UdjinM6 authored and luke-jr committed Dec 2, 2016
1 parent 5bcb05d commit f85ee01
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
28 changes: 19 additions & 9 deletions src/bitcoin-cli.cpp
Expand Up @@ -28,6 +28,7 @@ using namespace std;

static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
static const int CONTINUE_EXECUTION=-1;

std::string HelpMessageCli()
{
Expand Down Expand Up @@ -67,7 +68,11 @@ class CConnectionFailed : public std::runtime_error

};

static bool AppInitRPC(int argc, char* argv[])
//
// This function returns either one of EXIT_ codes when it's expected to stop the process or
// CONTINUE_EXECUTION when it's expected to continue further.
//
static int AppInitRPC(int argc, char* argv[])
{
//
// Parameters
Expand All @@ -85,31 +90,35 @@ static bool AppInitRPC(int argc, char* argv[])
}

fprintf(stdout, "%s", strUsage.c_str());
return false;
if (argc < 2) {
fprintf(stderr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
if (!boost::filesystem::is_directory(GetDataDir(false))) {
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
return false;
return EXIT_FAILURE;
}
try {
ReadConfigFile(mapArgs, mapMultiArgs);
} catch (const std::exception& e) {
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
return false;
return EXIT_FAILURE;
}
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
try {
SelectBaseParams(ChainNameFromCommandLine());
} catch (const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what());
return false;
return EXIT_FAILURE;
}
if (GetBoolArg("-rpcssl", false))
{
fprintf(stderr, "Error: SSL mode for RPC (-rpcssl) is no longer supported.\n");
return false;
return EXIT_FAILURE;
}
return true;
return CONTINUE_EXECUTION;
}


Expand Down Expand Up @@ -318,8 +327,9 @@ int main(int argc, char* argv[])
}

try {
if(!AppInitRPC(argc, argv))
return EXIT_FAILURE;
int ret = AppInitRPC(argc, argv);
if (ret != CONTINUE_EXECUTION)
return ret;
}
catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInitRPC()");
Expand Down
20 changes: 15 additions & 5 deletions src/bitcoin-tx.cpp
Expand Up @@ -30,8 +30,13 @@ using namespace std;

static bool fCreateBlank;
static map<string,UniValue> registers;
static const int CONTINUE_EXECUTION=-1;

static bool AppInitRawTx(int argc, char* argv[])
//
// This function returns either one of EXIT_ codes when it's expected to stop the process or
// CONTINUE_EXECUTION when it's expected to continue further.
//
static int AppInitRawTx(int argc, char* argv[])
{
//
// Parameters
Expand Down Expand Up @@ -89,9 +94,13 @@ static bool AppInitRawTx(int argc, char* argv[])
strUsage += HelpMessageOpt("set=NAME:JSON-STRING", _("Set register NAME to given JSON-STRING"));
fprintf(stdout, "%s", strUsage.c_str());

return false;
if (argc < 2) {
fprintf(stderr, "Error: too few parameters\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
return true;
return CONTINUE_EXECUTION;
}

static void RegisterSetJson(const string& key, const string& rawJson)
Expand Down Expand Up @@ -680,8 +689,9 @@ int main(int argc, char* argv[])
SetupEnvironment();

try {
if(!AppInitRawTx(argc, argv))
return EXIT_FAILURE;
int ret = AppInitRawTx(argc, argv);
if (ret != CONTINUE_EXECUTION)
return ret;
}
catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInitRawTx()");
Expand Down
2 changes: 1 addition & 1 deletion src/bitcoind.cpp
Expand Up @@ -93,7 +93,7 @@ bool AppInit(int argc, char* argv[])
}

fprintf(stdout, "%s", strUsage.c_str());
return false;
return true;
}

try
Expand Down
4 changes: 2 additions & 2 deletions src/qt/bitcoin.cpp
Expand Up @@ -573,7 +573,7 @@ int main(int argc, char *argv[])
{
HelpMessageDialog help(NULL, mapArgs.count("-version"));
help.showOrPrint();
return 1;
return 0;
}

/// 5. Now that settings and translations are available, ask user for data directory
Expand All @@ -594,7 +594,7 @@ int main(int argc, char *argv[])
} catch (const std::exception& e) {
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME),
QObject::tr("Error: Cannot parse configuration file: %1. Only use key=value syntax.").arg(e.what()));
return false;
return 1;
}

/// 7. Determine network (and switch to network specific options)
Expand Down

0 comments on commit f85ee01

Please sign in to comment.