Merge #11997: [tests] util_tests.cpp: actually check ignored args
[bitcoinplatinum.git] / src / bitcoind.cpp
blobde19787a169365d976e10ba49eac650b3b45216f
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #if defined(HAVE_CONFIG_H)
7 #include <config/bitcoin-config.h>
8 #endif
10 #include <chainparams.h>
11 #include <clientversion.h>
12 #include <compat.h>
13 #include <fs.h>
14 #include <rpc/server.h>
15 #include <init.h>
16 #include <noui.h>
17 #include <scheduler.h>
18 #include <util.h>
19 #include <httpserver.h>
20 #include <httprpc.h>
21 #include <utilstrencodings.h>
23 #include <boost/thread.hpp>
25 #include <stdio.h>
27 /* Introduction text for doxygen: */
29 /*! \mainpage Developer documentation
31 * \section intro_sec Introduction
33 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
34 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
35 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
37 * The software is a community-driven open source project, released under the MIT license.
39 * \section Navigation
40 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
43 void WaitForShutdown(boost::thread_group* threadGroup)
45 bool fShutdown = ShutdownRequested();
46 // Tell the main threads to shutdown.
47 while (!fShutdown)
49 MilliSleep(200);
50 fShutdown = ShutdownRequested();
52 if (threadGroup)
54 Interrupt(*threadGroup);
55 threadGroup->join_all();
59 //////////////////////////////////////////////////////////////////////////////
61 // Start
63 bool AppInit(int argc, char* argv[])
65 boost::thread_group threadGroup;
66 CScheduler scheduler;
68 bool fRet = false;
71 // Parameters
73 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
74 gArgs.ParseParameters(argc, argv);
76 // Process help and version before taking care about datadir
77 if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
79 std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
81 if (gArgs.IsArgSet("-version"))
83 strUsage += FormatParagraph(LicenseInfo());
85 else
87 strUsage += "\n" + _("Usage:") + "\n" +
88 " bitcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
90 strUsage += "\n" + HelpMessage(HMM_BITCOIND);
93 fprintf(stdout, "%s", strUsage.c_str());
94 return true;
97 try
99 if (!fs::is_directory(GetDataDir(false)))
101 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
102 return false;
106 gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
107 } catch (const std::exception& e) {
108 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
109 return false;
111 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
112 try {
113 SelectParams(ChainNameFromCommandLine());
114 } catch (const std::exception& e) {
115 fprintf(stderr, "Error: %s\n", e.what());
116 return false;
119 // Error out when loose non-argument tokens are encountered on command line
120 for (int i = 1; i < argc; i++) {
121 if (!IsSwitchChar(argv[i][0])) {
122 fprintf(stderr, "Error: Command line contains unexpected token '%s', see bitcoind -h for a list of options.\n", argv[i]);
123 return false;
127 // -server defaults to true for bitcoind but not for the GUI so do this here
128 gArgs.SoftSetBoolArg("-server", true);
129 // Set this early so that parameter interactions go to console
130 InitLogging();
131 InitParameterInteraction();
132 if (!AppInitBasicSetup())
134 // InitError will have been called with detailed error, which ends up on console
135 return false;
137 if (!AppInitParameterInteraction())
139 // InitError will have been called with detailed error, which ends up on console
140 return false;
142 if (!AppInitSanityChecks())
144 // InitError will have been called with detailed error, which ends up on console
145 return false;
147 if (gArgs.GetBoolArg("-daemon", false))
149 #if HAVE_DECL_DAEMON
150 fprintf(stdout, "Bitcoin server starting\n");
152 // Daemonize
153 if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
154 fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
155 return false;
157 #else
158 fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
159 return false;
160 #endif // HAVE_DECL_DAEMON
162 // Lock data directory after daemonization
163 if (!AppInitLockDataDirectory())
165 // If locking the data directory failed, exit immediately
166 return false;
168 fRet = AppInitMain(threadGroup, scheduler);
170 catch (const std::exception& e) {
171 PrintExceptionContinue(&e, "AppInit()");
172 } catch (...) {
173 PrintExceptionContinue(nullptr, "AppInit()");
176 if (!fRet)
178 Interrupt(threadGroup);
179 threadGroup.join_all();
180 } else {
181 WaitForShutdown(&threadGroup);
183 Shutdown();
185 return fRet;
188 int main(int argc, char* argv[])
190 SetupEnvironment();
192 // Connect bitcoind signal handlers
193 noui_connect();
195 return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);