Line data Source code
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.
5 :
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"
16 :
17 : #include <boost/algorithm/string/predicate.hpp>
18 : #include <boost/filesystem.hpp>
19 : #include <boost/thread.hpp>
20 :
21 : #include <stdio.h>
22 :
23 : /* Introduction text for doxygen: */
24 :
25 : /*! \mainpage Developer documentation
26 : *
27 : * \section intro_sec Introduction
28 : *
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.
32 : *
33 : * The software is a community-driven open source project, released under the MIT license.
34 : *
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.
37 : */
38 :
39 : static bool fDaemon;
40 :
41 94 : void WaitForShutdown(boost::thread_group* threadGroup)
42 : {
43 94 : bool fShutdown = ShutdownRequested();
44 : // Tell the main threads to shutdown.
45 4876 : while (!fShutdown)
46 : {
47 4688 : MilliSleep(200);
48 4688 : fShutdown = ShutdownRequested();
49 : }
50 94 : if (threadGroup)
51 : {
52 94 : Interrupt(*threadGroup);
53 94 : threadGroup->join_all();
54 : }
55 94 : }
56 :
57 : //////////////////////////////////////////////////////////////////////////////
58 : //
59 : // Start
60 : //
61 94 : bool AppInit(int argc, char* argv[])
62 : {
63 94 : boost::thread_group threadGroup;
64 188 : CScheduler scheduler;
65 :
66 94 : bool fRet = false;
67 :
68 : //
69 : // Parameters
70 : //
71 : // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
72 94 : ParseParameters(argc, argv);
73 :
74 : // Process help and version before taking care about datadir
75 940 : if (mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version"))
76 : {
77 0 : std::string strUsage = _("Bitcoin Core Daemon") + " " + _("version") + " " + FormatFullVersion() + "\n";
78 :
79 0 : if (mapArgs.count("-version"))
80 : {
81 0 : strUsage += LicenseInfo();
82 : }
83 : else
84 : {
85 0 : strUsage += "\n" + _("Usage:") + "\n" +
86 0 : " bitcoind [options] " + _("Start Bitcoin Core Daemon") + "\n";
87 :
88 0 : strUsage += "\n" + HelpMessage(HMM_BITCOIND);
89 : }
90 :
91 0 : fprintf(stdout, "%s", strUsage.c_str());
92 0 : return false;
93 : }
94 :
95 : try
96 : {
97 188 : if (!boost::filesystem::is_directory(GetDataDir(false)))
98 : {
99 0 : fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
100 0 : return false;
101 : }
102 : try
103 : {
104 94 : ReadConfigFile(mapArgs, mapMultiArgs);
105 0 : } catch (const std::exception& e) {
106 0 : fprintf(stderr,"Error reading configuration file: %s\n", e.what());
107 : return false;
108 : }
109 : // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
110 94 : if (!SelectParamsFromCommandLine()) {
111 0 : fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
112 : return false;
113 : }
114 :
115 : // Command-line RPC
116 : bool fCommandLine = false;
117 433 : for (int i = 1; i < argc; i++)
118 433 : if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
119 0 : fCommandLine = true;
120 :
121 94 : if (fCommandLine)
122 : {
123 0 : fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
124 0 : exit(1);
125 : }
126 : #ifndef WIN32
127 282 : fDaemon = GetBoolArg("-daemon", false);
128 94 : if (fDaemon)
129 : {
130 0 : fprintf(stdout, "Bitcoin server starting\n");
131 :
132 : // Daemonize
133 0 : pid_t pid = fork();
134 0 : if (pid < 0)
135 : {
136 0 : fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
137 : return false;
138 : }
139 0 : if (pid > 0) // Parent process, pid is child process id
140 : {
141 : return true;
142 : }
143 : // Child process falls through to rest of initialization
144 :
145 0 : pid_t sid = setsid();
146 0 : if (sid < 0)
147 0 : fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
148 : }
149 : #endif
150 282 : SoftSetBoolArg("-server", true);
151 :
152 94 : fRet = AppInit2(threadGroup, scheduler);
153 : }
154 0 : catch (const std::exception& e) {
155 0 : PrintExceptionContinue(&e, "AppInit()");
156 0 : } catch (...) {
157 0 : PrintExceptionContinue(NULL, "AppInit()");
158 : }
159 :
160 94 : if (!fRet)
161 : {
162 0 : 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 94 : WaitForShutdown(&threadGroup);
168 : }
169 94 : Shutdown();
170 :
171 94 : return fRet;
172 : }
173 :
174 94 : int main(int argc, char* argv[])
175 : {
176 94 : SetupEnvironment();
177 :
178 : // Connect bitcoind signal handlers
179 94 : noui_connect();
180 :
181 94 : return (AppInit(argc, argv) ? 0 : 1);
182 282 : }
|