1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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"
10 #include "chainparams.h"
11 #include "clientversion.h"
14 #include "rpc/server.h"
17 #include "scheduler.h"
19 #include "httpserver.h"
21 #include "utilstrencodings.h"
23 #include <boost/thread.hpp>
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.
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.
50 fShutdown
= ShutdownRequested();
54 Interrupt(*threadGroup
);
55 threadGroup
->join_all();
59 //////////////////////////////////////////////////////////////////////////////
63 bool AppInit(int argc
, char* argv
[])
65 boost::thread_group threadGroup
;
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());
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());
99 if (!fs::is_directory(GetDataDir(false)))
101 fprintf(stderr
, "Error: Specified data directory \"%s\" does not exist.\n", gArgs
.GetArg("-datadir", "").c_str());
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());
111 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
113 SelectParams(ChainNameFromCommandLine());
114 } catch (const std::exception
& e
) {
115 fprintf(stderr
, "Error: %s\n", e
.what());
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
]);
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
131 InitParameterInteraction();
132 if (!AppInitBasicSetup())
134 // InitError will have been called with detailed error, which ends up on console
137 if (!AppInitParameterInteraction())
139 // InitError will have been called with detailed error, which ends up on console
142 if (!AppInitSanityChecks())
144 // InitError will have been called with detailed error, which ends up on console
147 if (gArgs
.GetBoolArg("-daemon", false))
150 fprintf(stdout
, "Bitcoin server starting\n");
153 if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
154 fprintf(stderr
, "Error: daemon() failed: %s\n", strerror(errno
));
158 fprintf(stderr
, "Error: -daemon is not supported on this operating system\n");
160 #endif // HAVE_DECL_DAEMON
162 // Lock data directory after daemonization
163 if (!AppInitLockDataDirectory())
165 // If locking the data directory failed, exit immediately
168 fRet
= AppInitMain(threadGroup
, scheduler
);
170 catch (const std::exception
& e
) {
171 PrintExceptionContinue(&e
, "AppInit()");
173 PrintExceptionContinue(nullptr, "AppInit()");
178 Interrupt(threadGroup
);
179 threadGroup
.join_all();
181 WaitForShutdown(&threadGroup
);
188 int main(int argc
, char* argv
[])
192 // Connect bitcoind signal handlers
195 return (AppInit(argc
, argv
) ? EXIT_SUCCESS
: EXIT_FAILURE
);