Skip to content

Commit f53023d

Browse files
committed
Merge #9067: Fix exit codes
4441018 Every main()/exit() should return/use one of EXIT_ codes instead of magic numbers (UdjinM6) bd0de13 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` (UdjinM6)
2 parents b8f43e3 + 4441018 commit f53023d

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
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: 3 additions & 3 deletions
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
@@ -126,7 +126,7 @@ bool AppInit(int argc, char* argv[])
126126
if (fCommandLine)
127127
{
128128
fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
129-
exit(1);
129+
exit(EXIT_FAILURE);
130130
}
131131
if (GetBoolArg("-daemon", false))
132132
{
@@ -177,5 +177,5 @@ int main(int argc, char* argv[])
177177
// Connect bitcoind signal handlers
178178
noui_connect();
179179

180-
return (AppInit(argc, argv) ? 0 : 1);
180+
return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);
181181
}

src/qt/bitcoin.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ void BitcoinApplication::shutdownResult(int retval)
496496
void BitcoinApplication::handleRunawayException(const QString &message)
497497
{
498498
QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + message);
499-
::exit(1);
499+
::exit(EXIT_FAILURE);
500500
}
501501

502502
WId BitcoinApplication::getMainWinId() const
@@ -573,28 +573,28 @@ int main(int argc, char *argv[])
573573
{
574574
HelpMessageDialog help(NULL, mapArgs.count("-version"));
575575
help.showOrPrint();
576-
return 1;
576+
return EXIT_SUCCESS;
577577
}
578578

579579
/// 5. Now that settings and translations are available, ask user for data directory
580580
// User language is set up: pick a data directory
581581
if (!Intro::pickDataDirectory())
582-
return 0;
582+
return EXIT_SUCCESS;
583583

584584
/// 6. Determine availability of data directory and parse bitcoin.conf
585585
/// - Do not call GetDataDir(true) before this step finishes
586586
if (!boost::filesystem::is_directory(GetDataDir(false)))
587587
{
588588
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME),
589589
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(mapArgs["-datadir"])));
590-
return 1;
590+
return EXIT_FAILURE;
591591
}
592592
try {
593593
ReadConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME), mapArgs, mapMultiArgs);
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 EXIT_FAILURE;
598598
}
599599

600600
/// 7. Determine network (and switch to network specific options)
@@ -608,7 +608,7 @@ int main(int argc, char *argv[])
608608
SelectParams(ChainNameFromCommandLine());
609609
} catch(std::exception &e) {
610610
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME), QObject::tr("Error: %1").arg(e.what()));
611-
return 1;
611+
return EXIT_FAILURE;
612612
}
613613
#ifdef ENABLE_WALLET
614614
// Parse URIs on command line -- this can affect Params()
@@ -630,7 +630,7 @@ int main(int argc, char *argv[])
630630
// - Do this after creating app and setting up translations, so errors are
631631
// translated properly.
632632
if (PaymentServer::ipcSendCommandLine())
633-
exit(0);
633+
exit(EXIT_SUCCESS);
634634

635635
// Start up the payment server early, too, so impatient users that click on
636636
// bitcoin: links repeatedly have their payment requests routed to this process:

0 commit comments

Comments
 (0)