1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "clientversion.h"
11 #include "ui_interface.h"
14 #include <boost/algorithm/string/predicate.hpp>
15 #include <boost/filesystem.hpp>
16 #include <boost/thread.hpp>
18 /* Introduction text for doxygen: */
20 /*! \mainpage Developer documentation
22 * \section intro_sec Introduction
24 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (http://www.bitcoin.org/),
25 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
26 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
28 * The software is a community-driven open source project, released under the MIT license.
31 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
36 void DetectShutdownThread(boost::thread_group* threadGroup)
38 bool fShutdown = ShutdownRequested();
39 // Tell the main threads to shutdown.
43 fShutdown = ShutdownRequested();
47 threadGroup->interrupt_all();
48 threadGroup->join_all();
52 //////////////////////////////////////////////////////////////////////////////
56 bool AppInit(int argc, char* argv[])
58 boost::thread_group threadGroup;
59 boost::thread* detectShutdownThread = NULL;
66 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
67 ParseParameters(argc, argv);
69 // Process help and version before taking care about datadir
70 if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
72 std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
74 if (mapArgs.count("-version"))
76 strUsage += LicenseInfo();
80 strUsage += "\n" + _("Usage:") + "\n" +
81 " bitcoind [options] " + _("Start Bitcoin Core Daemon") + "\n";
83 strUsage += "\n" + HelpMessage(HMM_BITCOIND);
86 fprintf(stdout, "%s", strUsage.c_str());
92 if (!boost::filesystem::is_directory(GetDataDir(false)))
94 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
99 ReadConfigFile(mapArgs, mapMultiArgs);
100 } catch(std::exception &e) {
101 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
104 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
105 if (!SelectParamsFromCommandLine()) {
106 fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
111 bool fCommandLine = false;
112 for (int i = 1; i < argc; i++)
113 if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
118 fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
122 fDaemon = GetBoolArg("-daemon", false);
125 fprintf(stdout, "Bitcoin server starting\n");
131 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
134 if (pid > 0) // Parent process, pid is child process id
138 // Child process falls through to rest of initialization
140 pid_t sid = setsid();
142 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
145 SoftSetBoolArg("-server", true);
147 detectShutdownThread = new boost::thread(boost::bind(&DetectShutdownThread, &threadGroup));
148 fRet = AppInit2(threadGroup);
150 catch (std::exception& e) {
151 PrintExceptionContinue(&e, "AppInit()");
153 PrintExceptionContinue(NULL, "AppInit()");
158 if (detectShutdownThread)
159 detectShutdownThread->interrupt();
161 threadGroup.interrupt_all();
162 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
163 // the startup-failure cases to make sure they don't result in a hang due to some
164 // thread-blocking-waiting-for-another-thread-during-startup case
167 if (detectShutdownThread)
169 detectShutdownThread->join();
170 delete detectShutdownThread;
171 detectShutdownThread = NULL;
178 int main(int argc, char* argv[])
182 // Connect bitcoind signal handlers
185 return (AppInit(argc, argv) ? 0 : 1);