chaincodes: abstract away more chaincode behavior
[bitcoinplatinum.git] / src / bitcoind.cpp
blobeeca8655c98b52cd705bc2df77dfade3ad8da05c
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 "clientversion.h"
7 #include "rpcserver.h"
8 #include "init.h"
9 #include "main.h"
10 #include "noui.h"
11 #include "util.h"
13 #include <boost/algorithm/string/predicate.hpp>
14 #include <boost/filesystem.hpp>
15 #include <boost/thread.hpp>
17 /* Introduction text for doxygen: */
19 /*! \mainpage Developer documentation
21 * \section intro_sec Introduction
23 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
24 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
25 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
27 * The software is a community-driven open source project, released under the MIT license.
29 * \section Navigation
30 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
33 static bool fDaemon;
35 void WaitForShutdown(boost::thread_group* threadGroup)
37 bool fShutdown = ShutdownRequested();
38 // Tell the main threads to shutdown.
39 while (!fShutdown)
41 MilliSleep(200);
42 fShutdown = ShutdownRequested();
44 if (threadGroup)
46 threadGroup->interrupt_all();
47 threadGroup->join_all();
51 //////////////////////////////////////////////////////////////////////////////
53 // Start
55 bool AppInit(int argc, char* argv[])
57 boost::thread_group threadGroup;
59 bool fRet = false;
62 // Parameters
64 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
65 ParseParameters(argc, argv);
67 // Process help and version before taking care about datadir
68 if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
70 std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
72 if (mapArgs.count("-version"))
74 strUsage += LicenseInfo();
76 else
78 strUsage += "\n" + _("Usage:") + "\n" +
79 " bitcoind [options] " + _("Start Bitcoin Core Daemon") + "\n";
81 strUsage += "\n" + HelpMessage(HMM_BITCOIND);
84 fprintf(stdout, "%s", strUsage.c_str());
85 return false;
88 try
90 if (!boost::filesystem::is_directory(GetDataDir(false)))
92 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
93 return false;
95 try
97 ReadConfigFile(mapArgs, mapMultiArgs);
98 } catch (const std::exception& e) {
99 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
100 return false;
102 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
103 if (!SelectParamsFromCommandLine()) {
104 fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
105 return false;
108 // Command-line RPC
109 bool fCommandLine = false;
110 for (int i = 1; i < argc; i++)
111 if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
112 fCommandLine = true;
114 if (fCommandLine)
116 fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
117 exit(1);
119 #ifndef WIN32
120 fDaemon = GetBoolArg("-daemon", false);
121 if (fDaemon)
123 fprintf(stdout, "Bitcoin server starting\n");
125 // Daemonize
126 pid_t pid = fork();
127 if (pid < 0)
129 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
130 return false;
132 if (pid > 0) // Parent process, pid is child process id
134 return true;
136 // Child process falls through to rest of initialization
138 pid_t sid = setsid();
139 if (sid < 0)
140 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
142 #endif
143 SoftSetBoolArg("-server", true);
145 fRet = AppInit2(threadGroup);
147 catch (const std::exception& e) {
148 PrintExceptionContinue(&e, "AppInit()");
149 } catch (...) {
150 PrintExceptionContinue(NULL, "AppInit()");
153 if (!fRet)
155 threadGroup.interrupt_all();
156 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
157 // the startup-failure cases to make sure they don't result in a hang due to some
158 // thread-blocking-waiting-for-another-thread-during-startup case
159 } else {
160 WaitForShutdown(&threadGroup);
162 Shutdown();
164 return fRet;
167 int main(int argc, char* argv[])
169 SetupEnvironment();
171 // Connect bitcoind signal handlers
172 noui_connect();
174 return (AppInit(argc, argv) ? 0 : 1);