Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / bitcoind.cpp
blobb512f74c227003c3393aa099f4c4fe3cba55f818
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"
8 #include "rpcserver.h"
9 #include "init.h"
10 #include "noui.h"
11 #include "scheduler.h"
12 #include "util.h"
13 #include "httpserver.h"
14 #include "httprpc.h"
15 #include "rpcserver.h"
17 #include <boost/algorithm/string/predicate.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/thread.hpp>
21 #include <stdio.h>
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.
35 * \section Navigation
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.
39 static bool fDaemon;
41 void WaitForShutdown(boost::thread_group* threadGroup)
43 bool fShutdown = ShutdownRequested();
44 // Tell the main threads to shutdown.
45 while (!fShutdown)
47 MilliSleep(200);
48 fShutdown = ShutdownRequested();
50 if (threadGroup)
52 Interrupt(*threadGroup);
53 threadGroup->join_all();
57 //////////////////////////////////////////////////////////////////////////////
59 // Start
61 bool AppInit(int argc, char* argv[])
63 boost::thread_group threadGroup;
64 CScheduler scheduler;
66 bool fRet = false;
69 // Parameters
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();
83 else
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());
92 return false;
95 try
97 if (!boost::filesystem::is_directory(GetDataDir(false)))
99 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
100 return false;
104 ReadConfigFile(mapArgs, mapMultiArgs);
105 } catch (const std::exception& e) {
106 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
107 return false;
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");
112 return false;
115 // Command-line RPC
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:"))
119 fCommandLine = true;
121 if (fCommandLine)
123 fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
124 exit(1);
126 #ifndef WIN32
127 fDaemon = GetBoolArg("-daemon", false);
128 if (fDaemon)
130 fprintf(stdout, "Bitcoin server starting\n");
132 // Daemonize
133 pid_t pid = fork();
134 if (pid < 0)
136 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
137 return false;
139 if (pid > 0) // Parent process, pid is child process id
141 return true;
143 // Child process falls through to rest of initialization
145 pid_t sid = setsid();
146 if (sid < 0)
147 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
149 #endif
150 SoftSetBoolArg("-server", true);
152 fRet = AppInit2(threadGroup, scheduler);
154 catch (const std::exception& e) {
155 PrintExceptionContinue(&e, "AppInit()");
156 } catch (...) {
157 PrintExceptionContinue(NULL, "AppInit()");
160 if (!fRet)
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
166 } else {
167 WaitForShutdown(&threadGroup);
169 Shutdown();
171 return fRet;
174 int main(int argc, char* argv[])
176 SetupEnvironment();
178 // Connect bitcoind signal handlers
179 noui_connect();
181 return (AppInit(argc, argv) ? 0 : 1);