1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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"
13 #include "rpc/server.h"
16 #include "scheduler.h"
18 #include "httpserver.h"
20 #include "utilstrencodings.h"
22 #include <boost/algorithm/string/predicate.hpp>
23 #include <boost/filesystem.hpp>
24 #include <boost/thread.hpp>
28 /* Introduction text for doxygen: */
30 /*! \mainpage Developer documentation
32 * \section intro_sec Introduction
34 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
35 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
36 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
38 * The software is a community-driven open source project, released under the MIT license.
41 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
44 void WaitForShutdown(boost::thread_group
* threadGroup
)
46 bool fShutdown
= ShutdownRequested();
47 // Tell the main threads to shutdown.
51 fShutdown
= ShutdownRequested();
55 Interrupt(*threadGroup
);
56 threadGroup
->join_all();
60 //////////////////////////////////////////////////////////////////////////////
64 bool AppInit(int argc
, char* argv
[])
66 boost::thread_group threadGroup
;
74 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
75 ParseParameters(argc
, argv
);
77 // Process help and version before taking care about datadir
78 if (mapArgs
.count("-?") || mapArgs
.count("-h") || mapArgs
.count("-help") || mapArgs
.count("-version"))
80 std::string strUsage
= strprintf(_("%s Daemon"), _(PACKAGE_NAME
)) + " " + _("version") + " " + FormatFullVersion() + "\n";
82 if (mapArgs
.count("-version"))
84 strUsage
+= FormatParagraph(LicenseInfo());
88 strUsage
+= "\n" + _("Usage:") + "\n" +
89 " bitcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME
)) + "\n";
91 strUsage
+= "\n" + HelpMessage(HMM_BITCOIND
);
94 fprintf(stdout
, "%s", strUsage
.c_str());
100 if (!boost::filesystem::is_directory(GetDataDir(false)))
102 fprintf(stderr
, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs
["-datadir"].c_str());
107 ReadConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME
), mapArgs
, mapMultiArgs
);
108 } catch (const std::exception
& e
) {
109 fprintf(stderr
,"Error reading configuration file: %s\n", e
.what());
112 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
114 SelectParams(ChainNameFromCommandLine());
115 } catch (const std::exception
& e
) {
116 fprintf(stderr
, "Error: %s\n", e
.what());
121 bool fCommandLine
= false;
122 for (int i
= 1; i
< argc
; i
++)
123 if (!IsSwitchChar(argv
[i
][0]) && !boost::algorithm::istarts_with(argv
[i
], "bitcoin:"))
128 fprintf(stderr
, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
131 // -server defaults to true for bitcoind but not for the GUI so do this here
132 SoftSetBoolArg("-server", true);
133 // Set this early so that parameter interactions go to console
135 InitParameterInteraction();
136 if (!AppInitBasicSetup())
138 // InitError will have been called with detailed error, which ends up on console
141 if (!AppInitParameterInteraction())
143 // InitError will have been called with detailed error, which ends up on console
146 if (!AppInitSanityChecks())
148 // InitError will have been called with detailed error, which ends up on console
151 if (GetBoolArg("-daemon", false))
154 fprintf(stdout
, "Bitcoin server starting\n");
157 if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
158 fprintf(stderr
, "Error: daemon() failed: %s\n", strerror(errno
));
162 fprintf(stderr
, "Error: -daemon is not supported on this operating system\n");
164 #endif // HAVE_DECL_DAEMON
167 fRet
= AppInitMain(threadGroup
, scheduler
);
169 catch (const std::exception
& e
) {
170 PrintExceptionContinue(&e
, "AppInit()");
172 PrintExceptionContinue(NULL
, "AppInit()");
177 Interrupt(threadGroup
);
178 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
179 // the startup-failure cases to make sure they don't result in a hang due to some
180 // thread-blocking-waiting-for-another-thread-during-startup case
182 WaitForShutdown(&threadGroup
);
189 int main(int argc
, char* argv
[])
193 // Connect bitcoind signal handlers
196 return (AppInit(argc
, argv
) ? EXIT_SUCCESS
: EXIT_FAILURE
);