Skip to content

Commit bd0de13

Browse files
committed
Fix exit codes:
- `--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)
1 parent a4fd8df commit bd0de13

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

src/bitcoin-cli.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using namespace std;
2828

2929
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
3030
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
31+
static const int CONTINUE_EXECUTION=-1;
3132

3233
std::string HelpMessageCli()
3334
{
@@ -67,7 +68,11 @@ class CConnectionFailed : public std::runtime_error
6768

6869
};
6970

70-
static bool AppInitRPC(int argc, char* argv[])
71+
//
72+
// This function returns either one of EXIT_ codes when it's expected to stop the process or
73+
// CONTINUE_EXECUTION when it's expected to continue further.
74+
//
75+
static int AppInitRPC(int argc, char* argv[])
7176
{
7277
//
7378
// Parameters
@@ -85,31 +90,35 @@ static bool AppInitRPC(int argc, char* argv[])
8590
}
8691

8792
fprintf(stdout, "%s", strUsage.c_str());
88-
return false;
93+
if (argc < 2) {
94+
fprintf(stderr, "Error: too few parameters\n");
95+
return EXIT_FAILURE;
96+
}
97+
return EXIT_SUCCESS;
8998
}
9099
if (!boost::filesystem::is_directory(GetDataDir(false))) {
91100
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
92-
return false;
101+
return EXIT_FAILURE;
93102
}
94103
try {
95104
ReadConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME), mapArgs, mapMultiArgs);
96105
} catch (const std::exception& e) {
97106
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
98-
return false;
107+
return EXIT_FAILURE;
99108
}
100109
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
101110
try {
102111
SelectBaseParams(ChainNameFromCommandLine());
103112
} catch (const std::exception& e) {
104113
fprintf(stderr, "Error: %s\n", e.what());
105-
return false;
114+
return EXIT_FAILURE;
106115
}
107116
if (GetBoolArg("-rpcssl", false))
108117
{
109118
fprintf(stderr, "Error: SSL mode for RPC (-rpcssl) is no longer supported.\n");
110-
return false;
119+
return EXIT_FAILURE;
111120
}
112-
return true;
121+
return CONTINUE_EXECUTION;
113122
}
114123

115124

@@ -354,8 +363,9 @@ int main(int argc, char* argv[])
354363
}
355364

356365
try {
357-
if(!AppInitRPC(argc, argv))
358-
return EXIT_FAILURE;
366+
int ret = AppInitRPC(argc, argv);
367+
if (ret != CONTINUE_EXECUTION)
368+
return ret;
359369
}
360370
catch (const std::exception& e) {
361371
PrintExceptionContinue(&e, "AppInitRPC()");

src/bitcoin-tx.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ using namespace std;
3030

3131
static bool fCreateBlank;
3232
static map<string,UniValue> registers;
33+
static const int CONTINUE_EXECUTION=-1;
3334

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

92-
return false;
97+
if (argc < 2) {
98+
fprintf(stderr, "Error: too few parameters\n");
99+
return EXIT_FAILURE;
100+
}
101+
return EXIT_SUCCESS;
93102
}
94-
return true;
103+
return CONTINUE_EXECUTION;
95104
}
96105

97106
static void RegisterSetJson(const string& key, const string& rawJson)
@@ -678,8 +687,9 @@ int main(int argc, char* argv[])
678687
SetupEnvironment();
679688

680689
try {
681-
if(!AppInitRawTx(argc, argv))
682-
return EXIT_FAILURE;
690+
int ret = AppInitRawTx(argc, argv);
691+
if (ret != CONTINUE_EXECUTION)
692+
return ret;
683693
}
684694
catch (const std::exception& e) {
685695
PrintExceptionContinue(&e, "AppInitRawTx()");

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool AppInit(int argc, char* argv[])
9292
}
9393

9494
fprintf(stdout, "%s", strUsage.c_str());
95-
return false;
95+
return true;
9696
}
9797

9898
try

src/qt/bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ int main(int argc, char *argv[])
573573
{
574574
HelpMessageDialog help(NULL, mapArgs.count("-version"));
575575
help.showOrPrint();
576-
return 1;
576+
return 0;
577577
}
578578

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

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

0 commit comments

Comments
 (0)