1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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 #include "chainparams.h"
7 #include "clientversion.h"
11 #include "scheduler.h"
13 #include "httpserver.h"
15 #include "rpcserver.h"
17 #include <boost/algorithm/string/predicate.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/thread.hpp>
23 /* Introduction text for doxygen: */
25 /*! \mainpage Developer documentation
27 * \section intro_sec Introduction
29 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
30 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
31 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
33 * The software is a community-driven open source project, released under the MIT license.
36 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
41 void WaitForShutdown(boost::thread_group
* threadGroup
)
43 bool fShutdown
= ShutdownRequested();
44 // Tell the main threads to shutdown.
48 fShutdown
= ShutdownRequested();
52 Interrupt(*threadGroup
);
53 threadGroup
->join_all();
57 //////////////////////////////////////////////////////////////////////////////
61 bool AppInit(int argc
, char* argv
[])
63 boost::thread_group threadGroup
;
71 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
72 ParseParameters(argc
, argv
);
74 // Process help and version before taking care about datadir
75 if (mapArgs
.count("-?") || mapArgs
.count("-help") || mapArgs
.count("-version"))
77 std::string strUsage
= _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
79 if (mapArgs
.count("-version"))
81 strUsage
+= LicenseInfo();
85 strUsage
+= "\n" + _("Usage:") + "\n" +
86 " bitcoind [options] " + _("Start Bitcoin Core Daemon") + "\n";
88 strUsage
+= "\n" + HelpMessage(HMM_BITCOIND
);
91 fprintf(stdout
, "%s", strUsage
.c_str());
97 if (!boost::filesystem::is_directory(GetDataDir(false)))
99 fprintf(stderr
, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs
["-datadir"].c_str());
104 ReadConfigFile(mapArgs
, mapMultiArgs
);
105 } catch (const std::exception
& e
) {
106 fprintf(stderr
,"Error reading configuration file: %s\n", e
.what());
109 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
110 if (!SelectParamsFromCommandLine()) {
111 fprintf(stderr
, "Error: Invalid combination of -regtest and -testnet.\n");
116 bool fCommandLine
= false;
117 for (int i
= 1; i
< argc
; i
++)
118 if (!IsSwitchChar(argv
[i
][0]) && !boost::algorithm::istarts_with(argv
[i
], "bitcoin:"))
123 fprintf(stderr
, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
127 fDaemon
= GetBoolArg("-daemon", false);
130 fprintf(stdout
, "Bitcoin server starting\n");
136 fprintf(stderr
, "Error: fork() returned %d errno %d\n", pid
, errno
);
139 if (pid
> 0) // Parent process, pid is child process id
143 // Child process falls through to rest of initialization
145 pid_t sid
= setsid();
147 fprintf(stderr
, "Error: setsid() returned %d errno %d\n", sid
, errno
);
150 SoftSetBoolArg("-server", true);
152 fRet
= AppInit2(threadGroup
, scheduler
);
154 catch (const std::exception
& e
) {
155 PrintExceptionContinue(&e
, "AppInit()");
157 PrintExceptionContinue(NULL
, "AppInit()");
162 Interrupt(threadGroup
);
163 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
164 // the startup-failure cases to make sure they don't result in a hang due to some
165 // thread-blocking-waiting-for-another-thread-during-startup case
167 WaitForShutdown(&threadGroup
);
174 int main(int argc
, char* argv
[])
178 // Connect bitcoind signal handlers
181 return (AppInit(argc
, argv
) ? 0 : 1);