Skip to content

Commit 61c569a

Browse files
committed
refactor: decouple early return commands from AppInit
Cleaned up the init flow to make it more obvious when the 'exit_status' value will and won't be returned. This is because it was confusing that `AppInit` was returning true under two different circumstances: 1) When bitcoind was launched only to retrieve the "-help" or "-version" information. In this case, the app was not initialized. 2) When the user triggers a shutdown. In this case, the app was fully initialized.
1 parent 4927167 commit 61c569a

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

src/bitcoind.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,30 @@ int fork_daemon(bool nochdir, bool noclose, TokenPipeEnd& endpoint)
112112

113113
#endif
114114

115-
static bool AppInit(NodeContext& node, int argc, char* argv[])
115+
static bool ParseArgs(ArgsManager& args, int argc, char* argv[])
116116
{
117-
bool fRet = false;
118-
119-
util::ThreadSetInternalName("init");
120-
121117
// If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
122-
ArgsManager& args = *Assert(node.args);
123118
SetupServerArgs(args);
124119
std::string error;
125120
if (!args.ParseParameters(argc, argv, error)) {
126121
return InitError(Untranslated(strprintf("Error parsing command line arguments: %s", error)));
127122
}
128123

124+
if (auto error = common::InitConfig(args)) {
125+
return InitError(error->message, error->details);
126+
}
127+
128+
// Error out when loose non-argument tokens are encountered on command line
129+
for (int i = 1; i < argc; i++) {
130+
if (!IsSwitchChar(argv[i][0])) {
131+
return InitError(Untranslated(strprintf("Command line contains unexpected token '%s', see bitcoind -h for a list of options.", argv[i])));
132+
}
133+
}
134+
return true;
135+
}
136+
137+
static bool ProcessInitCommands(ArgsManager& args)
138+
{
129139
// Process help and version before taking care about datadir
130140
if (HelpRequested(args) || args.IsArgSet("-version")) {
131141
std::string strUsage = PACKAGE_NAME " version " + FormatFullVersion() + "\n";
@@ -142,6 +152,14 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
142152
return true;
143153
}
144154

155+
return false;
156+
}
157+
158+
static bool AppInit(NodeContext& node)
159+
{
160+
bool fRet = false;
161+
ArgsManager& args = *Assert(node.args);
162+
145163
#if HAVE_DECL_FORK
146164
// Communication with parent after daemonizing. This is used for signalling in the following ways:
147165
// - a boolean token is sent when the initialization process (all the Init* functions) have finished to indicate
@@ -153,17 +171,6 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
153171
std::any context{&node};
154172
try
155173
{
156-
if (auto error = common::InitConfig(args)) {
157-
return InitError(error->message, error->details);
158-
}
159-
160-
// Error out when loose non-argument tokens are encountered on command line
161-
for (int i = 1; i < argc; i++) {
162-
if (!IsSwitchChar(argv[i][0])) {
163-
return InitError(Untranslated(strprintf("Command line contains unexpected token '%s', see bitcoind -h for a list of options.", argv[i])));
164-
}
165-
}
166-
167174
// -server defaults to true for bitcoind but not for the GUI so do this here
168175
args.SoftSetBoolArg("-server", true);
169176
// Set this early so that parameter interactions go to console
@@ -236,14 +243,6 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
236243
}
237244
#endif
238245
SetSyscallSandboxPolicy(SyscallSandboxPolicy::SHUTOFF);
239-
if (fRet) {
240-
WaitForShutdown();
241-
} else {
242-
node.exit_status = EXIT_FAILURE;
243-
}
244-
Interrupt(node);
245-
Shutdown(node);
246-
247246
return fRet;
248247
}
249248

@@ -266,5 +265,22 @@ MAIN_FUNCTION
266265
// Connect bitcoind signal handlers
267266
noui_connect();
268267

269-
return AppInit(node, argc, argv) ? node.exit_status.load() : EXIT_FAILURE;
268+
util::ThreadSetInternalName("init");
269+
270+
// Interpret command line arguments
271+
ArgsManager& args = *Assert(node.args);
272+
if (!ParseArgs(args, argc, argv)) return EXIT_FAILURE;
273+
// Process early info return commands such as -help or -version
274+
if (ProcessInitCommands(args)) return EXIT_SUCCESS;
275+
276+
// Start application
277+
if (AppInit(node)) {
278+
WaitForShutdown();
279+
} else {
280+
node.exit_status = EXIT_FAILURE;
281+
}
282+
Interrupt(node);
283+
Shutdown(node);
284+
285+
return node.exit_status;
270286
}

0 commit comments

Comments
 (0)